mirror of https://lore.kernel.org/lkml/
 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, linux-kernel@vger.kernel.org,
	kernelnewbies@kernelnewbies.org
Subject: [RFC PATCH v2 05/10] block: Add block device sysfs attribute to set/clear/show LED
Date: Sun,  8 Aug 2021 22:32:12 -0500	[thread overview]
Message-ID: <20210809033217.1113444-6-arequipeno@gmail.com> (raw)
In-Reply-To: <20210809033217.1113444-1-arequipeno@gmail.com>

Add show & store functions in blk-ledtrig.c (attributes defined in genhd.c)

Show function shows all available LEDs (LEDs associated with blkdev trigger);
currently associated LED is shown in square brackets ([])

Store function accepts either all whitespace or "none" to clear LED

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

diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c
index 280fa9edc2dd..1af94dc7ea51 100644
--- a/block/blk-ledtrig.c
+++ b/block/blk-ledtrig.c
@@ -6,6 +6,7 @@
  *	Copyright 2021 Ian Pilcher <arequipeno@gmail.com>
  */
 
+#include <linux/ctype.h>
 #include <linux/genhd.h>
 #include <linux/leds.h>
 #include <linux/mutex.h>
@@ -139,3 +140,111 @@ static int blk_ledtrig_dev_set(struct gendisk *const disk,
 led_set_exit_return:
 	return ret;
 }
+
+
+/*
+ *
+ *	sysfs attribute store function to set or clear device LED
+ *
+ */
+
+// Returns a pointer to the first non-whitespace character in s (or a pointer
+// to the terminating null).
+static const char *blk_ledtrig_skip_whitespace(const char *s)
+{
+	while (*s != 0 && isspace(*s))
+		++s;
+
+	return s;
+}
+
+// Returns a pointer to the first whitespace character in s (or a pointer to
+// the terminating null), which is effectively a pointer to the position *after*
+// the last character in the non-whitespace token at the beginning of s.  (s is
+// expected to be the result of a previous call to blk_ledtrig_skip_whitespace.)
+static const char *blk_ledtrig_find_whitespace(const char *s)
+{
+	while (*s != 0 && !isspace(*s))
+		++s;
+
+	return s;
+}
+
+static bool blk_ledtrig_name_is_none(const char *const name, const size_t len)
+{
+	static const char none[4] = "none";	// no terminating null
+
+	return len == sizeof(none) && memcmp(name, none, sizeof(none)) == 0;
+}
+
+ssize_t blk_ledtrig_dev_led_store(struct device *const dev,
+				  struct device_attribute *const attr,
+				  const char *const buf, const size_t count)
+{
+	struct gendisk *const disk = dev_to_disk(dev);
+	const char *const led_name = blk_ledtrig_skip_whitespace(buf);
+	const char *const endp = blk_ledtrig_find_whitespace(led_name);
+	const ptrdiff_t name_len = endp - led_name;	// always >= 0
+	int ret;
+
+	if (name_len == 0 || blk_ledtrig_name_is_none(led_name, name_len)) {
+		blk_ledtrig_dev_clear(disk);
+		ret = 0;
+	} else {
+		ret = blk_ledtrig_dev_set(disk, led_name, name_len);
+	}
+
+	if (ret < 0)
+		return ret;
+
+	return count;
+}
+
+
+/*
+ *
+ *	sysfs attribute show function for device LED
+ *
+ */
+
+ssize_t blk_ledtrig_dev_led_show(struct device *const dev,
+				 struct device_attribute *const attr,
+				 char *const buf)
+{
+	struct gendisk *const disk = dev_to_disk(dev);
+	struct blk_ledtrig_led *bd_led, *disk_led;
+	int ret, c = 0;
+
+	ret = mutex_lock_interruptible(&blk_ledtrig_mutex);
+	if (ret != 0)
+		goto led_show_exit_return;
+
+	disk_led = rcu_dereference_protected(disk->led,
+					lockdep_is_held(&blk_ledtrig_mutex));
+
+	if (disk_led == NULL)
+		c += sprintf(buf, "[none]");
+	else
+		c += sprintf(buf, "none");
+
+	list_for_each_entry(bd_led, &blk_ledtrig_leds, leds_list_node) {
+
+		ret = snprintf(buf + c, PAGE_SIZE - c - 1,
+			       bd_led == disk_led ? " [%s]" : " %s",
+			       bd_led->led->name);
+		if (ret >= PAGE_SIZE - c - 1) {
+			ret = -EOVERFLOW;
+			goto led_show_exit_unlock;
+		}
+
+		c += ret;
+	}
+
+	buf[c] = '\n';
+	ret = c + 1;
+
+led_show_exit_unlock:
+	mutex_unlock(&blk_ledtrig_mutex);
+led_show_exit_return:
+	return ret;
+}
diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h
index 66a1302a4174..771000d43647 100644
--- a/block/blk-ledtrig.h
+++ b/block/blk-ledtrig.h
@@ -18,6 +18,14 @@ static inline void blk_ledtrig_disk_init(struct gendisk *const disk)
 
 void blk_ledtrig_dev_clear(struct gendisk *const disk);
 
+ssize_t blk_ledtrig_dev_led_store(struct device *const dev,
+				  struct device_attribute *const attr,
+				  const char *const buf, const size_t count);
+
+ssize_t blk_ledtrig_dev_led_show(struct device *const dev,
+				 struct device_attribute *const attr,
+				 char *const buf);
+
 #else	// CONFIG_BLK_LED_TRIGGERS
 
 static inline void blk_ledtrig_disk_init(const struct gendisk *disk) {}
diff --git a/block/genhd.c b/block/genhd.c
index 9fa734aeab0f..d5413a633410 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1012,6 +1012,11 @@ static struct device_attribute dev_attr_fail_timeout =
 	__ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
 #endif
 
+#ifdef CONFIG_BLK_LED_TRIGGERS
+static struct device_attribute dev_attr_led =
+	__ATTR(led, 0644, blk_ledtrig_dev_led_show, blk_ledtrig_dev_led_store);
+#endif
+
 static struct attribute *disk_attrs[] = {
 	&dev_attr_range.attr,
 	&dev_attr_ext_range.attr,
@@ -1033,6 +1038,9 @@ static struct attribute *disk_attrs[] = {
 #endif
 #ifdef CONFIG_FAIL_IO_TIMEOUT
 	&dev_attr_fail_timeout.attr,
+#endif
+#ifdef CONFIG_BLK_LED_TRIGGERS
+	&dev_attr_led.attr,
 #endif
 	NULL
 };
-- 
2.31.1


  parent reply	other threads:[~2021-08-09  3:33 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 ` [RFC PATCH v2 04/10] block: Add functions to set & clear block device LEDs Ian Pilcher
2021-08-09  3:32 ` Ian Pilcher [this message]
2021-08-09  4:21   ` [RFC PATCH v2 05/10] block: Add block device sysfs attribute to set/clear/show LED 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-6-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

all inboxes | Powered by JetHome®