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, linux-kernel@vger.kernel.org,
	kernelnewbies@kernelnewbies.org,
	Ian Pilcher <arequipeno@gmail.com>
Subject: [RFC PATCH 7/8] block: Add block device attributes to set & clear LED triggers
Date: Wed, 28 Jul 2021 20:53:43 -0500	[thread overview]
Message-ID: <20210729015344.3366750-8-arequipeno@gmail.com> (raw)
In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com>

* Set/store functions in block/blk-ledtrig.c

* Add device attributes to disk_attrs in block/genhd.c

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

diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c
index 7c8fdff88683..2d324df45149 100644
--- a/block/blk-ledtrig.c
+++ b/block/blk-ledtrig.c
@@ -479,3 +479,68 @@ bool blk_ledtrig_clear(struct gendisk *const gd)
 	return changed;
 }
 EXPORT_SYMBOL_GPL(blk_ledtrig_clear);
+
+
+/*
+ *
+ *	Set, clear & show LED triggers via sysfs device attributes
+ *
+ *	(See dev_attr_led_trigger and disk_attrs in genhd.c)
+ *
+ */
+
+ssize_t blk_ledtrig_devattr_store(struct device *const dev,
+				  struct device_attribute *const attr,
+				  const char *const buf, const size_t count)
+{
+	struct gendisk *const gd = dev_to_disk(dev);
+	const char *const name = blk_ledtrig_skip_whitespace(buf);
+	const char *const endp = blk_ledtrig_find_whitespace(name);
+	const ptrdiff_t name_len = endp - name;		// always >= 0
+	int ret;
+
+	if (name_len == 0)
+		ret = blk_ledtrig_clear(gd);
+	else
+		ret = __blk_ledtrig_set(gd, name, name_len);
+
+	if (ret < 0)
+		return ret;
+
+	return blk_ledtrig_skip_whitespace(endp) - buf;
+}
+
+ssize_t blk_ledtrig_devattr_show(struct device *const dev,
+				 struct device_attribute *const attr,
+				 char *const buf)
+{
+	struct gendisk *const gd = dev_to_disk(dev);
+	const struct blk_ledtrig *t;
+	size_t name_len;
+	int ret;
+
+	ret = mutex_lock_interruptible(&gd->ledtrig_mutex);
+	if (unlikely(ret != 0))
+		return ret;
+
+	t = gd->ledtrig;
+
+	if (t != NULL) {
+		name_len = strlen(t->name);
+		if (likely(name_len < PAGE_SIZE - 1))
+			memcpy(buf, t->name, name_len);
+	}
+
+	mutex_unlock(&gd->ledtrig_mutex);
+
+	if (t == NULL)
+		return sprintf(buf, "(none)\n");
+
+	if (unlikely(name_len >= PAGE_SIZE - 1))
+		return -EOVERFLOW;
+
+	buf[name_len] = '\n';
+	buf[name_len + 1] = 0;
+
+	return (ssize_t)(name_len + 1);
+}
diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h
index 9b718d45783f..5d228905edbf 100644
--- a/block/blk-ledtrig.h
+++ b/block/blk-ledtrig.h
@@ -19,6 +19,14 @@ static inline void blk_ledtrig_disk_init(struct gendisk *const gd)
 	mutex_init(&gd->ledtrig_mutex);
 }
 
+ssize_t blk_ledtrig_devattr_store(struct device *const dev,
+				  struct device_attribute *const attr,
+				  const char *const buf, const size_t count);
+
+ssize_t blk_ledtrig_devattr_show(struct device *const dev,
+				 struct device_attribute *const attr,
+				 char *const buf);
+
 #else	// CONFIG_BLK_LED_TRIGGERS
 
 static inline void blk_ledtrig_init(void) {}
diff --git a/block/genhd.c b/block/genhd.c
index fb1617f21d79..fd37efe74d48 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1014,6 +1014,12 @@ 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_trigger =
+	__ATTR(led_trigger, 0644,
+	       blk_ledtrig_devattr_show, blk_ledtrig_devattr_store);
+#endif
+
 static struct attribute *disk_attrs[] = {
 	&dev_attr_range.attr,
 	&dev_attr_ext_range.attr,
@@ -1035,6 +1041,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_trigger.attr,
 #endif
 	NULL
 };
-- 
2.31.1


  parent reply	other threads:[~2021-07-29  1:54 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-29  1:53 [RFC PATCH 0/8] Add configurable block device LED triggers Ian Pilcher
2021-07-29  1:53 ` [RFC PATCH 1/8] docs: Add block device LED trigger documentation Ian Pilcher
2021-07-29  3:09   ` Valdis Klētnieks
2021-07-29 15:52     ` Ian Pilcher
2021-07-30  5:22       ` Greg KH
2021-07-29  5:53   ` Greg KH
2021-07-29 11:59   ` Marek Behún
2021-07-29 18:03     ` Ian Pilcher
2021-07-29  1:53 ` [RFC PATCH 2/8] block: Add block device LED trigger list Ian Pilcher
2021-07-29  3:14   ` Valdis Klētnieks
2021-07-29 15:55     ` Ian Pilcher
2021-07-29  1:53 ` [RFC PATCH 3/8] block: Add kernel APIs to create & delete block device LED triggers Ian Pilcher
2021-07-29  3:45   ` Valdis Klētnieks
2021-07-29 16:16     ` Ian Pilcher
2021-07-29  5:52   ` Greg KH
2021-07-29  1:53 ` [RFC PATCH 4/8] block: Add block class attributes to manage LED trigger list Ian Pilcher
2021-07-29  5:54   ` Greg KH
2021-07-29  1:53 ` [RFC PATCH 5/8] block: Add block device LED trigger info to struct genhd Ian Pilcher
2021-07-29  1:53 ` [RFC PATCH 6/8] block: Add kernel APIs to set & clear per-block device LED triggers Ian Pilcher
2021-07-29  1:53 ` Ian Pilcher [this message]
2021-07-29  1:53 ` [RFC PATCH 8/8] block: Blink device LED when request is sent to low-level driver Ian Pilcher
2021-07-29  8:54 ` [RFC PATCH 0/8] Add configurable block device LED triggers Pavel Machek
2021-07-29 17:03   ` Ian Pilcher
2021-07-29 18:35     ` Pavel Machek
2021-07-29 19:14       ` 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=20210729015344.3366750-8-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).