Thread (37 messages) 37 messages, 5 authors, 2021-09-14

Re: [PATCH v2 06/15] leds: trigger: blkdev: Add function to get gendisk by name

From: Greg KH <gregkh@linuxfoundation.org>
Date: 2021-09-10 06:45:11
Also in: linux-block, lkml

On Thu, Sep 09, 2021 at 05:25:04PM -0500, Ian Pilcher wrote:
quoted hunk ↗ jump to hunk
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 <redacted>
---
 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;
You now have bumped the reference count on this structure.  Where do you
decrement it when you are finished with it?

thanks,

greg k-h
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help