[RFC PATCH v3 02/18] block: Add get_disk_by_name() for use by blkdev LED trigger

Ian Pilcher arequipeno at gmail.com
Wed Aug 18 22:50:37 EDT 2021


Add API that gets a "handle" (pointer & incremented reference count) to a
block device (struct gendisk) by name.  Used by the block device LED
trigger when configuring which device(s) an LED should monitor.

Signed-off-by: Ian Pilcher <arequipeno at gmail.com>
---
 block/genhd.c         | 25 +++++++++++++++++++++++++
 include/linux/genhd.h | 10 ++++++++++
 2 files changed, 35 insertions(+)

diff --git a/block/genhd.c b/block/genhd.c
index 298ee78c1bda..e6d7bb709d62 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1362,3 +1362,28 @@ int bdev_read_only(struct block_device *bdev)
 	return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
 }
 EXPORT_SYMBOL(bdev_read_only);
+
+static int match_disk_name(struct device *const dev, const void *const name)
+{
+	return dev->type == &disk_type
+			&& strcmp(name, dev_to_disk(dev)->disk_name) == 0;
+}
+
+/**
+ * get_disk_by_name - get a gendisk by name
+ * @name:	the name of the disk
+ *
+ * Returns a pointer to the gendisk named @name (if it exists), @NULL if not.
+ * Increments the disk's reference count, so caller must call put_device().
+ */
+struct gendisk *get_disk_by_name(const char *const name)
+{
+	struct device *dev;
+
+	dev = class_find_device(&block_class, NULL, name, match_disk_name);
+	if (dev == NULL)
+		return NULL;
+
+	return dev_to_disk(dev);
+}
+EXPORT_SYMBOL_GPL(get_disk_by_name);
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index 13b34177cc85..b26bbf2d9048 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -342,4 +342,14 @@ static inline void printk_all_partitions(void)
 }
 #endif /* CONFIG_BLOCK */
 
+/* for blkdev LED trigger (drivers/leds/trigger/ledtrig-blkdev.c) */
+#ifdef CONFIG_BLOCK
+struct gendisk *get_disk_by_name(const char *name);
+#else
+static inline struct gendisk *get_disk_by_name(const char *name)
+{
+	return NULL;
+}
+#endif
+
 #endif /* _LINUX_GENHD_H */
-- 
2.31.1




More information about the Kernelnewbies mailing list