[PATCH 06/18] ledtrig-blkdev: Add function to get gendisk by name

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Add (inline) helper function to compare C string with non-terminated
character sequence (within the buffer passed to a sysfs attribute store
function).

Signed-off-by: Ian Pilcher <arequipeno@xxxxxxxxx>
---
 drivers/leds/trigger/ledtrig-blkdev-core.c | 43 ++++++++++++++++++++++
 drivers/leds/trigger/ledtrig-blkdev.h      | 12 ++++++
 2 files changed, 55 insertions(+)

diff --git a/drivers/leds/trigger/ledtrig-blkdev-core.c b/drivers/leds/trigger/ledtrig-blkdev-core.c
index 102cdbe26d66..87d439f425ad 100644
--- a/drivers/leds/trigger/ledtrig-blkdev-core.c
+++ b/drivers/leds/trigger/ledtrig-blkdev-core.c
@@ -33,3 +33,46 @@ void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd)
 
 	mutex_unlock(&ledtrig_blkdev_mutex);
 }
+
+
+/*
+ *
+ *	ledtrig_blkdev_get_disk() - get a gendisk by name
+ *
+ *	Must be built in for access to block_class and disk_type
+ *	Caller must call put_disk()
+ *
+ */
+
+/* Non-null-terminated character sequence of known length */
+struct ledtrig_blkdev_gdname {
+	const char	*buf;
+	size_t		len;
+};
+
+/* Match function for ledtrig_blkdev_get_disk() */
+static int blkdev_match_gdname(struct device *const dev, const void *const data)
+{
+	const struct ledtrig_blkdev_gdname *const gdname = data;
+
+	if (dev->type != &disk_type)
+		return 0;
+
+	return ledtrig_blkdev_streq(dev_to_disk(dev)->disk_name,
+				    gdname->buf, gdname->len);
+}
+
+struct gendisk *ledtrig_blkdev_get_disk(const char *const name,
+					const size_t len)
+{
+	const struct ledtrig_blkdev_gdname gdname = { .buf = name, .len = len };
+	struct device *dev;
+
+	dev = class_find_device(&block_class, NULL,
+				&gdname, blkdev_match_gdname);
+	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 914fb1523a2f..9a3528fd183a 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.h
+++ b/drivers/leds/trigger/ledtrig-blkdev.h
@@ -11,5 +11,17 @@
 
 extern struct mutex ledtrig_blkdev_mutex;
 extern void (*__ledtrig_blkdev_disk_cleanup)(struct gendisk *gd);
+extern struct gendisk *ledtrig_blkdev_get_disk(const char *name, size_t len);
+
+/*
+ * Compare a null-terminated C string with a non-null-terminated character
+ * sequence of a known length.  Returns true (1) if equal, false (0) if not.
+ */
+static inline bool ledtrig_blkdev_streq(const char *const cstr,
+					const char *const cbuf,
+					const size_t buf_len)
+{
+	return strlen(cstr) == buf_len && memcmp(cstr, cbuf, buf_len) == 0;
+}
 
 #endif	/* __LEDTRIG_BLKDEV_H */
-- 
2.31.1




[Index of Archives]     [Linux RAID]     [Linux SCSI]     [Linux ATA RAID]     [IDE]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Device Mapper]

  Powered by Linux