[PATCH v2 06/15] leds: trigger: blkdev: Add function to get gendisk by name
From: Ian Pilcher
Date: Thu Sep 09 2021 - 18:25:36 EST
Add ledtrig_blkdev_get_disk() to find block device by name and increment
its reference count. (Caller must call put_disk().) Must be built-in to
access block_class and disk_type symbols.
Signed-off-by: Ian Pilcher <arequipeno@xxxxxxxxx>
---
drivers/leds/trigger/ledtrig-blkdev-core.c | 20 ++++++++++++++++++++
drivers/leds/trigger/ledtrig-blkdev.h | 3 +++
2 files changed, 23 insertions(+)
diff --git a/drivers/leds/trigger/ledtrig-blkdev-core.c b/drivers/leds/trigger/ledtrig-blkdev-core.c
index d7b02e760b06..5fd741aa14a6 100644
--- a/drivers/leds/trigger/ledtrig-blkdev-core.c
+++ b/drivers/leds/trigger/ledtrig-blkdev-core.c
@@ -33,3 +33,23 @@ void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd)
mutex_unlock(&ledtrig_blkdev_mutex);
}
+
+/* class_find_device() callback. Must be built-in to access disk_type. */
+static int blkdev_match_name(struct device *const dev, const void *const name)
+{
+ return dev->type == &disk_type
+ && sysfs_streq(dev_to_disk(dev)->disk_name, name);
+}
+
+/* Must be built-in to access block_class */
+struct gendisk *ledtrig_blkdev_get_disk(const char *const name)
+{
+ struct device *dev;
+
+ dev = class_find_device(&block_class, NULL, name, blkdev_match_name);
+ if (dev == NULL)
+ return NULL;
+
+ return dev_to_disk(dev);
+}
+EXPORT_SYMBOL_NS_GPL(ledtrig_blkdev_get_disk, LEDTRIG_BLKDEV);
diff --git a/drivers/leds/trigger/ledtrig-blkdev.h b/drivers/leds/trigger/ledtrig-blkdev.h
index b1294da17ba3..a6ff24fad0c2 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.h
+++ b/drivers/leds/trigger/ledtrig-blkdev.h
@@ -12,4 +12,7 @@
extern struct mutex ledtrig_blkdev_mutex;
extern void (*__ledtrig_blkdev_disk_cleanup)(struct gendisk *gd);
+/* Caller must call put_disk() */
+struct gendisk *ledtrig_blkdev_get_disk(const char *const name);
+
#endif /* __LEDTRIG_BLKDEV_H */
--
2.31.1