From: Ian Pilcher <arequipeno@gmail.com>
To: axboe@kernel.dk, pavel@ucw.cz
Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org,
kabel@kernel.org
Subject: [PATCH v2 11/15] leds: trigger: blkdev: Enable linking block devices to LEDs
Date: Thu, 9 Sep 2021 17:25:09 -0500 [thread overview]
Message-ID: <20210909222513.2184795-12-arequipeno@gmail.com> (raw)
In-Reply-To: <20210909222513.2184795-1-arequipeno@gmail.com>
Add /sys/class/leds/<led>/link_device sysfs attribute
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 | 160 ++++++++++++++++++++++++++
1 file changed, 160 insertions(+)
diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c
index 6f78a9515976..26509837f037 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.c
+++ b/drivers/leds/trigger/ledtrig-blkdev.c
@@ -71,6 +71,9 @@ static unsigned int ledtrig_blkdev_interval;
static void blkdev_process(struct work_struct *const work);
static DECLARE_DELAYED_WORK(ledtrig_blkdev_work, blkdev_process);
+/* Total number of device-to-LED associations */
+static unsigned int ledtrig_blkdev_count;
+
/*
*
@@ -220,6 +223,162 @@ static int blkdev_activate(struct led_classdev *const led_dev)
}
+/*
+ *
+ * link_device sysfs attribute - assocate a block device with this 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 = kobject_create_and_add("linked_leds", &disk_to_dev(gd)->kobj);
+ if (dir == NULL) {
+ kfree(disk);
+ return -ENOMEM;
+ }
+
+ 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_link_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)
+ return true;
+ }
+
+ return false;
+}
+
+static ssize_t link_device_store(struct device *const dev,
+ struct device_attribute *const attr,
+ const char *const buf, const size_t count)
+{
+ struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev);
+ struct gendisk *gd;
+ int ret;
+
+ ret = mutex_lock_interruptible(&ledtrig_blkdev_mutex);
+ if (ret != 0)
+ goto exit_return;
+
+ gd = ledtrig_blkdev_get_disk(buf);
+ if (gd == NULL) {
+ ret = -ENODEV;
+ goto exit_unlock;
+ }
+
+ if (blkdev_already_linked(led, gd)) {
+ ret = -EEXIST;
+ goto exit_put_disk;
+ }
+
+ ret = blkdev_disk_link_locked(led, gd);
+
+exit_put_disk:
+ if (ret != 0)
+ put_disk(gd);
+exit_unlock:
+ mutex_unlock(&ledtrig_blkdev_mutex);
+exit_return:
+ return (ret == 0) ? count : ret;
+}
+
+static DEVICE_ATTR_WO(link_device);
+
+
/*
*
* Initialization - register the trigger
@@ -227,6 +386,7 @@ static int blkdev_activate(struct led_classdev *const led_dev)
*/
static struct attribute *ledtrig_blkdev_attrs[] = {
+ &dev_attr_link_device.attr,
NULL
};
--
2.31.1
next prev parent reply other threads:[~2021-09-09 22:25 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-09 22:24 [PATCH v2 00/15] Introduce block device LED trigger Ian Pilcher
2021-09-09 22:24 ` [PATCH v2 01/15] docs: Add block device (blkdev) LED trigger documentation Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 02/15] leds: trigger: blkdev: Add build infrastructure Ian Pilcher
2021-09-10 1:32 ` Marek Behún
2021-09-09 22:25 ` [PATCH v2 03/15] leds: trigger: blkdev: Add functions needed by block changes Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 04/15] block: Add block device LED trigger integrations Ian Pilcher
2021-09-09 23:27 ` Chaitanya Kulkarni
2021-09-10 1:23 ` Marek Behún
2021-09-10 15:00 ` Ian Pilcher
2021-09-10 1:38 ` Marek Behún
2021-09-09 22:25 ` [PATCH v2 05/15] leds: trigger: blkdev: Complete functions called by block subsys Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 06/15] leds: trigger: blkdev: Add function to get gendisk by name Ian Pilcher
2021-09-10 6:45 ` Greg KH
2021-09-10 15:17 ` Ian Pilcher
2021-09-10 15:23 ` Greg KH
2021-09-10 16:28 ` Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 07/15] leds: trigger: blkdev: Add constants and types Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 08/15] leds: trigger: blkdev: Add stub LED trigger structure Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 09/15] leds: trigger: blkdev: Check devices for activity and blink LEDs Ian Pilcher
2021-09-10 1:48 ` Marek Behún
2021-09-10 2:17 ` Marek Behún
2021-09-10 15:09 ` Ian Pilcher
2021-09-10 15:12 ` Marek Behún
2021-09-10 21:23 ` Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 10/15] leds: trigger: blkdev: Add LED trigger activate function Ian Pilcher
2021-09-10 6:47 ` Greg KH
2021-09-10 16:10 ` Ian Pilcher
2021-09-09 22:25 ` Ian Pilcher [this message]
2021-09-10 6:48 ` [PATCH v2 11/15] leds: trigger: blkdev: Enable linking block devices to LEDs Greg KH
2021-09-10 16:25 ` Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 12/15] leds: trigger: blkdev: Enable unlinking block devices from LEDs Ian Pilcher
2021-09-14 9:58 ` Dan Carpenter
2021-09-09 22:25 ` [PATCH v2 13/15] leds: trigger: blkdev: Add LED trigger deactivate function Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 14/15] leds: trigger: blkdev: Add remaining sysfs attributes Ian Pilcher
2021-09-09 22:25 ` [PATCH v2 15/15] leds: trigger: blkdev: Add disk cleanup and init/exit functions Ian Pilcher
2021-09-10 2:09 ` [PATCH v2 00/15] Introduce block device LED trigger Marek Behún
2021-09-10 14:04 ` 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=20210909222513.2184795-12-arequipeno@gmail.com \
--to=arequipeno@gmail.com \
--cc=axboe@kernel.dk \
--cc=gregkh@linuxfoundation.org \
--cc=kabel@kernel.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).