summaryrefslogtreecommitdiff
path: root/lib/blkid/devname.c
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2009-04-27 15:00:58 +0200
committerTheodore Ts'o <tytso@mit.edu>2009-05-02 22:39:20 -0400
commit46f3eeca59a1fc8233790bbed604e4634e0f3fbe (patch)
treef951927ee1aa18dcfd627a78b03912cf8b9916d4 /lib/blkid/devname.c
parent4271e23942bdc60e1fa6c0b26bc666a94a8b3e1d (diff)
downloade2fsprogs-46f3eeca59a1fc8233790bbed604e4634e0f3fbe.tar.gz
blkid: use /sys/block/dm-<N>/dm/name
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> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Diffstat (limited to 'lib/blkid/devname.c')
-rw-r--r--lib/blkid/devname.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/blkid/devname.c b/lib/blkid/devname.c
index ec19a2e8..b151354d 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;
}
@@ -211,6 +237,8 @@ static void probe_one(blkid_cache cache, const char *ptname,
}
/* Do a short-cut scan of /dev/mapper first */
if (!devname)
+ devname = get_dm_name(ptname);
+ if (!devname)
blkid__scan_dir("/dev/mapper", devno, 0, &devname);
if (!devname) {
devname = blkid_devno_to_devname(devno);