[RFC PATCH 2/8] block: Add block device LED trigger list

From: Ian Pilcher
Date: Wed Jul 28 2021 - 21:54:40 EST


* New config option (CONFIG_BLK_LED_TRIGGERS) to enable/disable
block device LED triggers

* New file - block/blk-ledtrig.c

* Use a linked list of dynamically allocated triggers. There
aren't likely to be that many of them, and the list is only
searched when creating/deleting a trigger or setting/clearing
a device/trigger association - none of which should occur very
often.

Signed-off-by: Ian Pilcher <arequipeno@xxxxxxxxx>
---
block/Kconfig | 10 +++++++++
block/Makefile | 1 +
block/blk-ledtrig.c | 51 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 62 insertions(+)
create mode 100644 block/blk-ledtrig.c

diff --git a/block/Kconfig b/block/Kconfig
index fd732aede922..051488413d6e 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -220,6 +220,16 @@ config BLK_INLINE_ENCRYPTION_FALLBACK
by falling back to the kernel crypto API when inline
encryption hardware is not present.

+config BLK_LED_TRIGGERS
+ bool "Enable block device LED triggers"
+ depends on LEDS_TRIGGERS
+ help
+ Enabling this allows LED triggers to be created and
+ associated with block devices via sysfs/udev (or an
+ in-kernel API). These trigers can be used to drive
+ physical or user-space activity indicators. See
+ Documentation/block/led-triggers.rst.
+
menu "Partition Types"

source "block/partitions/Kconfig"
diff --git a/block/Makefile b/block/Makefile
index bfbe4e13ca1e..bcd97ee26462 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -42,3 +42,4 @@ obj-$(CONFIG_BLK_SED_OPAL) += sed-opal.o
obj-$(CONFIG_BLK_PM) += blk-pm.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += keyslot-manager.o blk-crypto.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
+obj-$(CONFIG_BLK_LED_TRIGGERS) += blk-ledtrig.o
diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c
new file mode 100644
index 000000000000..345a3b6bdbc6
--- /dev/null
+++ b/block/blk-ledtrig.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Block device LED triggers
+ *
+ * Copyright 2021 Ian Pilcher <arequipeno@xxxxxxxxx>
+ */
+
+#include <linux/leds.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+
+
+/*
+ *
+ * The list of block device LED triggers
+ *
+ */
+
+struct blk_ledtrig {
+ struct led_trigger trigger;
+ struct list_head list_node;
+ struct mutex refcount_mutex;
+ int refcount;
+ char name[];
+};
+
+LIST_HEAD(blk_ledtrig_list);
+DEFINE_MUTEX(blk_ledtrig_list_mutex);
+
+static inline
+struct blk_ledtrig *blk_ledtrig_from_node(struct list_head *const node)
+{
+ return container_of(node, struct blk_ledtrig, list_node);
+}
+
+// Caller must hold blk_ledtrig_list_mutex
+static struct blk_ledtrig *blk_ledtrig_find(const char *const name,
+ const size_t len)
+{
+ struct blk_ledtrig *t;
+ struct list_head *n;
+
+ list_for_each(n, &blk_ledtrig_list) {
+ t = blk_ledtrig_from_node(n);
+ if (strlen(t->name) == len && memcmp(name, t->name, len) == 0)
+ return t;
+ }
+
+ return NULL;
+}
--
2.31.1