diff mbox series

[RFC,7/8] block: Add block device attributes to set & clear LED triggers

Message ID 20210729015344.3366750-8-arequipeno@gmail.com
State New
Headers show
Series Add configurable block device LED triggers | expand

Commit Message

Ian Pilcher July 29, 2021, 1:53 a.m. UTC
* 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 mbox series

Patch

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
 };