kernelnewbies.kernelnewbies.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, linux-kernel@vger.kernel.org, pavel@ucw.cz,
	kernelnewbies@kernelnewbies.org
Subject: [RFC PATCH v2 04/10] block: Add functions to set & clear block device LEDs
Date: Sun,  8 Aug 2021 22:32:11 -0500	[thread overview]
Message-ID: <20210809033217.1113444-5-arequipeno@gmail.com> (raw)
In-Reply-To: <20210809033217.1113444-1-arequipeno@gmail.com>

Create a symlink from /sys/class/leds/<LED>/block_devices to each block
device that is associated with that LED

Ensure device LED is cleared when device is removed

Signed-off-by: Ian Pilcher <arequipeno@gmail.com>
---
 block/blk-ledtrig.c | 93 +++++++++++++++++++++++++++++++++++++++++++++
 block/blk-ledtrig.h |  3 ++
 block/genhd.c       |  1 +
 3 files changed, 97 insertions(+)

diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c
index c5ad57ed9c3b..280fa9edc2dd 100644
--- a/block/blk-ledtrig.c
+++ b/block/blk-ledtrig.c
@@ -6,9 +6,13 @@
  *	Copyright 2021 Ian Pilcher <arequipeno@gmail.com>
  */
 
+#include <linux/genhd.h>
 #include <linux/leds.h>
 #include <linux/mutex.h>
 
+#include "blk-ledtrig.h"
+
+
 /*
  *
  *	Trigger mutex and LED list
@@ -46,3 +50,92 @@ static struct blk_ledtrig_led *blk_ledtrig_find(const char *const led_name,
 
 	return NULL;
 }
+
+
+/*
+ *
+ *	Clear a block device's LED
+ *
+ */
+
+// Also called from blk_ledtrig_dev_set()
+static void blk_ledtrig_dev_cleanup(struct gendisk *const disk,
+				    struct blk_ledtrig_led *const old_led)
+{
+	sysfs_remove_link(old_led->dir, disk->disk_name);
+	list_del(&disk->led_dev_list_node);
+}
+
+// Also called from blk_ledtrig_deactivate()
+static void blk_ledtrig_dev_clear_locked(struct gendisk *const disk,
+					 struct blk_ledtrig_led *const old_led)
+{
+	RCU_INIT_POINTER(disk->led, NULL);
+	if (old_led != NULL)
+		blk_ledtrig_dev_cleanup(disk, old_led);
+}
+
+// Also called from genhd.c:del_gendisk()
+void blk_ledtrig_dev_clear(struct gendisk *const disk)
+{
+	struct blk_ledtrig_led *old_led;
+
+	mutex_lock(&blk_ledtrig_mutex);
+	old_led = rcu_dereference_protected(disk->led,
+					lockdep_is_held(&blk_ledtrig_mutex));
+	blk_ledtrig_dev_clear_locked(disk, old_led);
+	mutex_unlock(&blk_ledtrig_mutex);
+}
+
+
+/*
+ *
+ *	Set a block device's LED
+ *
+ */
+
+static int blk_ledtrig_dev_set(struct gendisk *const disk,
+			       const char *const led_name,
+			       const size_t name_len)
+{
+	struct blk_ledtrig_led *new_led, *old_led;
+	int ret;
+
+	ret = mutex_lock_interruptible(&blk_ledtrig_mutex);
+	if (ret != 0)
+		goto led_set_exit_return;
+
+	new_led = blk_ledtrig_find(led_name, name_len);
+	if (new_led == NULL) {
+		pr_info("no LED named %.*s associated with blkdev trigger\n",
+			(int)name_len, led_name);
+		ret = -ENODEV;
+		goto led_set_exit_unlock;
+	}
+
+	old_led = rcu_dereference_protected(disk->led,
+					lockdep_is_held(&blk_ledtrig_mutex));
+
+	if (old_led == new_led) {
+		ret = 0;
+		goto led_set_exit_unlock;
+	}
+
+	ret = sysfs_create_link(new_led->dir, &disk_to_dev(disk)->kobj,
+				disk->disk_name);
+	if (ret != 0)
+		goto led_set_exit_unlock;
+
+	if (old_led != NULL)
+		blk_ledtrig_dev_cleanup(disk, old_led);
+
+	rcu_assign_pointer(disk->led, new_led);
+	list_add(&disk->led_dev_list_node, &new_led->dev_list);
+
+	ret = 0;
+
+led_set_exit_unlock:
+	mutex_unlock(&blk_ledtrig_mutex);
+led_set_exit_return:
+	return ret;
+}
diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h
index 95a79d2fe447..66a1302a4174 100644
--- a/block/blk-ledtrig.h
+++ b/block/blk-ledtrig.h
@@ -16,9 +16,12 @@ static inline void blk_ledtrig_disk_init(struct gendisk *const disk)
 	RCU_INIT_POINTER(disk->led, NULL);
 }
 
+void blk_ledtrig_dev_clear(struct gendisk *const disk);
+
 #else	// CONFIG_BLK_LED_TRIGGERS
 
 static inline void blk_ledtrig_disk_init(const struct gendisk *disk) {}
+static inline void blk_ledtrig_dev_clear(const struct gendisk *disk) {}
 
 #endif	// CONFIG_BLK_LED_TRIGGERS
 
diff --git a/block/genhd.c b/block/genhd.c
index b168172e664b..9fa734aeab0f 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -583,6 +583,7 @@ void del_gendisk(struct gendisk *disk)
 	if (WARN_ON_ONCE(!disk->queue))
 		return;
 
+	blk_ledtrig_dev_clear(disk);
 	blk_integrity_del(disk);
 	disk_del_events(disk);
 
-- 
2.31.1


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  parent reply	other threads:[~2021-08-09  3:35 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-09  3:32 [RFC PATCH v2 00/10] Add configurable block device LED triggers Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 01/10] docs: Add block device LED trigger documentation Ian Pilcher
2021-08-10 13:49   ` Pavel Machek
2021-08-09  3:32 ` [RFC PATCH v2 02/10] block: Add file (blk-ledtrig.c) for block device LED trigger implementation Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 03/10] block: Add block device LED trigger fields to gendisk structure Ian Pilcher
2021-08-09  3:32 ` Ian Pilcher [this message]
2021-08-09  3:32 ` [RFC PATCH v2 05/10] block: Add block device sysfs attribute to set/clear/show LED Ian Pilcher
2021-08-09  4:21   ` Jackie Liu
2021-08-09 15:44     ` Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 06/10] block: Add activate and deactivate functions for block device LED trigger Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 07/10] block: Add sysfs attributes to LEDs associated with blkdev trigger Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 08/10] block: Add init function for block device LED trigger Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 09/10] block: Blink device LED (if any) when request is sent to its driver Ian Pilcher
2021-08-09  3:32 ` [RFC PATCH v2 10/10] block: Add config option to enable block device LED triggers Ian Pilcher
2021-08-09 18:56 ` [RFC PATCH v2 00/10] Add configurable " Marek Behún
2021-08-09 19:07   ` Pali Rohár
2021-08-09 19:54   ` Ian Pilcher
2021-08-09 22:43     ` Marek Behún
2021-08-09 23:50       ` Ian Pilcher
2021-08-10  6:35         ` Greg KH
2021-08-10 13:38           ` Marek Behún
2021-08-10 14:48             ` Greg KH
2021-08-10 15:55               ` Ian Pilcher
2021-08-10 16:24                 ` Greg KH
2021-08-10 16:39                   ` Marek Behún
2021-08-10 16:43                   ` Ian Pilcher
2021-08-11  6:26         ` Christoph Hellwig
2021-08-11 10:50           ` Marek Behún

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=20210809033217.1113444-5-arequipeno@gmail.com \
    --to=arequipeno@gmail.com \
    --cc=axboe@kernel.dk \
    --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).