diff mbox

blkid: use /sys/block/dm-<N>/dm/name

Message ID 1240837258-5440-2-git-send-email-kzak@redhat.com
State Accepted, archived
Headers show

Commit Message

Karel Zak April 27, 2009, 1 p.m. UTC
The Linux kernel (since 2.6.29, patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128)
exports the real DM device names in /sys/block/<ptname>/dm/name.

The sysfs based solution is nicer and faster than scan for devno in
/dev/mapper/.

CC: Milan Broz <mbroz@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 lib/blkid/devname.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)

Comments

Theodore Ts'o May 3, 2009, 2:41 a.m. UTC | #1
On Mon, Apr 27, 2009 at 03:00:58PM +0200, Karel Zak wrote:
> The Linux kernel (since 2.6.29, patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128)
> exports the real DM device names in /sys/block/<ptname>/dm/name.

Thanks, applied.

					- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/blkid/devname.c b/lib/blkid/devname.c
index 59c0919..4dde766 100644
--- a/lib/blkid/devname.c
+++ b/lib/blkid/devname.c
@@ -153,6 +153,30 @@  static int is_dm_leaf(const char *devname)
 }
 
 /*
+ * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
+ * provides the real DM device names in /sys/block/<ptname>/dm/name
+ */
+static char *get_dm_name(const char *ptname)
+{
+	FILE	*f;
+	size_t	sz;
+	char	path[256], name[256], *res = NULL;
+
+	snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
+	if ((f = fopen(path, "r")) == NULL)
+		return NULL;
+
+	/* read "<name>\n" from sysfs */
+	if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
+		name[sz - 1] = '\0';
+		snprintf(path, sizeof(path), "/dev/mapper/%s", name);
+		res = blkid_strdup(path);
+	}
+	fclose(f);
+	return res;
+}
+
+/*
  * Probe a single block device to add to the device cache.
  */
 static void probe_one(blkid_cache cache, const char *ptname,
@@ -183,7 +207,9 @@  static void probe_one(blkid_cache cache, const char *ptname,
 	 * to standard /dev/mapper/<name>.
 	 */
 	if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
-		blkid__scan_dir("/dev/mapper", devno, 0, &devname);
+		devname = get_dm_name(ptname);
+		if (!devname)
+			blkid__scan_dir("/dev/mapper", devno, 0, &devname);
 		if (devname)
 			goto get_dev;
 	}