linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Pilcher <arequipeno@gmail.com>
To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org
Cc: axboe@kernel.dk, pavel@ucw.cz, kabel@kernel.org,
	linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org
Subject: [RFC PATCH v3 11/18] ledtrig-blkdev: Add function to associate a device with an LED
Date: Wed, 18 Aug 2021 21:50:46 -0500	[thread overview]
Message-ID: <20210819025053.222710-12-arequipeno@gmail.com> (raw)
In-Reply-To: <20210819025053.222710-1-arequipeno@gmail.com>

If this is the first LED associated with the device, create the
/sys/block/<disk>/blkdev_leds directory.  Otherwise, increment its
reference count.

Create symlinks in /sys/class/leds/<led>/block_devices and
/sys/block/<disk>/blkdev_leds

If this the first device associated with any LED, schedule delayed work
to periodically check associated devices and blink LEDs

Signed-off-by: Ian Pilcher <arequipeno@gmail.com>
---
 drivers/leds/trigger/ledtrig-blkdev.c | 168 ++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)

diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c
index 2072cc904616..a1646752b9a0 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.c
+++ b/drivers/leds/trigger/ledtrig-blkdev.c
@@ -312,3 +312,171 @@ void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd)
 	mutex_unlock(&ledtrig_blkdev_mutex);
 }
 EXPORT_SYMBOL_GPL(ledtrig_blkdev_disk_cleanup);
+
+
+/*
+ *
+ *	Associate a block device with an LED
+ *
+ */
+
+/* Gets or allocs & initializes the blkdev disk for a gendisk */
+static int blkdev_get_disk(struct gendisk *const gd)
+{
+	struct ledtrig_blkdev_disk *disk;
+	struct kobject *dir;
+
+	if (gd->ledtrig != NULL) {
+		kobject_get(gd->ledtrig->dir);
+		return 0;
+	}
+
+	disk = kmalloc(sizeof(*disk), GFP_KERNEL);
+	if (disk == NULL)
+		return -ENOMEM;
+
+	dir = blkdev_mkdir("blkdev_leds", &disk_to_dev(gd)->kobj);
+	if (IS_ERR(dir)) {
+		kfree(disk);
+		return PTR_ERR(dir);
+	}
+
+	INIT_HLIST_HEAD(&disk->leds);
+	disk->gd = gd;
+	disk->dir = dir;
+	disk->read_ios = 0;
+	disk->write_ios = 0;
+
+	gd->ledtrig = disk;
+
+	return 0;
+}
+
+static void blkdev_put_disk(struct ledtrig_blkdev_disk *const disk)
+{
+	kobject_put(disk->dir);
+
+	if (hlist_empty(&disk->leds)) {
+		disk->gd->ledtrig = NULL;
+		kfree(disk);
+	}
+}
+
+static int blkdev_disk_add_locked(struct ledtrig_blkdev_led *const led,
+				  struct gendisk *const gd)
+{
+	struct ledtrig_blkdev_link *link;
+	struct ledtrig_blkdev_disk *disk;
+	unsigned long delay;
+	int ret;
+
+	link = kmalloc(sizeof(*link), GFP_KERNEL);
+	if (link == NULL) {
+		ret = -ENOMEM;
+		goto error_return;
+	}
+
+	ret = blkdev_get_disk(gd);
+	if (ret != 0)
+		goto error_free_link;
+
+	disk = gd->ledtrig;
+
+	ret = sysfs_create_link(disk->dir, &led->led_dev->dev->kobj,
+				led->led_dev->name);
+	if (ret != 0)
+		goto error_put_disk;
+
+	ret = sysfs_create_link(led->dir, &disk_to_dev(gd)->kobj,
+				gd->disk_name);
+	if (ret != 0)
+		goto error_remove_link;
+
+	link->disk = disk;
+	link->led = led;
+	hlist_add_head(&link->led_disks_node, &led->disks);
+	hlist_add_head(&link->disk_leds_node, &disk->leds);
+
+	if (ledtrig_blkdev_count == 0) {
+		delay = READ_ONCE(ledtrig_blkdev_interval);
+		WARN_ON(!schedule_delayed_work(&ledtrig_blkdev_work, delay));
+	}
+
+	++ledtrig_blkdev_count;
+
+	return 0;
+
+error_remove_link:
+	sysfs_remove_link(disk->dir, led->led_dev->name);
+error_put_disk:
+	blkdev_put_disk(disk);
+error_free_link:
+	kfree(link);
+error_return:
+	return ret;
+}
+
+static bool blkdev_already_linked(const struct ledtrig_blkdev_led *const led,
+				  const struct gendisk *const gd)
+{
+	const struct ledtrig_blkdev_link *link;
+
+	if (gd->ledtrig == NULL)
+		return false;
+
+	hlist_for_each_entry(link, &gd->ledtrig->leds, disk_leds_node) {
+
+		if (link->led == led) {
+			pr_info("blkdev LED: %s already associated with %s\n",
+				gd->disk_name, led->led_dev->name);
+			return true;
+		}
+	}
+
+	return false;
+}
+
+static int blkdev_disk_add(struct ledtrig_blkdev_led *const led,
+			   const char *const disk_name, const size_t name_len)
+{
+	static char name[DISK_NAME_LEN];	/* only used w/ mutex locked */
+	struct gendisk *gd;
+	int ret;
+
+	if (name_len >= DISK_NAME_LEN) {
+		pr_info("blkdev LED: invalid device name %.*s\n",
+			(int)name_len, disk_name);
+		ret = -EINVAL;
+		goto exit_return;
+	}
+
+	ret = mutex_lock_interruptible(&ledtrig_blkdev_mutex);
+	if (ret != 0)
+		goto exit_return;
+
+	memcpy(name, disk_name, name_len);
+	name[name_len] = 0;
+	gd = get_disk_by_name(name);	/* increments disk's refcount */
+
+	if (gd == NULL) {
+		pr_info("blkdev LED: no such block device %.*s\n",
+			(int)name_len, disk_name);
+		ret = -ENODEV;
+		goto exit_unlock;
+	}
+
+	if (blkdev_already_linked(led, gd)) {
+		ret = -EEXIST;
+		goto exit_put_dev;
+	}
+
+	ret = blkdev_disk_add_locked(led, gd);
+
+exit_put_dev:
+	if (ret != 0)
+		put_device(disk_to_dev(gd));
+exit_unlock:
+	mutex_unlock(&ledtrig_blkdev_mutex);
+exit_return:
+	return ret;
+}
-- 
2.31.1


  parent reply	other threads:[~2021-08-19  2:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19  2:50 [RFC PATCH v3 00/18] Add block device LED trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 01/18] docs: Add block device (blkdev) LED trigger documentation Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 02/18] block: Add get_disk_by_name() for use by blkdev LED trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 03/18] ledtrig-blkdev: Add file (ledtrig-blkdev.c) for block device " Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 04/18] ledtrig-blkdev: Add misc. helper functions to blkdev " Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 05/18] ledtrig-blkdev: Periodically check devices for activity & blink LEDs Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 06/18] block: Add LED trigger pointer to struct gendisk Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 07/18] ledtrig-blkdev: Add function to initialize gendisk ledtrig member Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 08/18] ledtrig-blkdev: Add function to remove LED/device association Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 09/18] ledtrig-blkdev: Add function to disassociate a device from all LEDs Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 10/18] block: Call LED trigger init/cleanup functions Ian Pilcher
2021-08-19  2:50 ` Ian Pilcher [this message]
2021-08-19  2:50 ` [RFC PATCH v3 12/18] ledtrig-blkdev: Add sysfs attributes to [dis]associate LEDs & devices Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 13/18] ledtrig-blkdev: Add blink_time & interval sysfs attributes Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 14/18] ledtrig-blkdev: Add mode (read/write/rw) sysfs attributue Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 15/18] ledtrig-blkdev: Add function to associate blkdev trigger with LED Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 16/18] ledtrig-blkdev: Add function to disassociate an LED from the trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 17/18] ledtrig-blkdev: Add initialization function Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 18/18] ledtrig-blkdev: Add config option to enable the trigger Ian Pilcher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210819025053.222710-12-arequipeno@gmail.com \
    --to=arequipeno@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=kabel@kernel.org \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).