[patch] support /sys/block being symlinks instead of directories

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

 



Hi,
this patch removes the assumption that device directories in /sys are
always real directories. With the work-in-progress unification of all
sysfs devices, they will show up as symlinks pointing into a single
device tree if the kernel does not set CONFIG_SYSFS_DEPRECATED.

It also adds direct support for a future "block as a class", which will
not have any hierarchy to recurse to find all block devices.

 $ diffstat  lvm2-future-proof-sysfs.patch
 filter-sysfs.c |   91 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 56 insertions(+), 35 deletions(-)

Thanks,
Kay

--- lib/filters/filter-sysfs.c.orig	2007-08-11 11:51:59.000000000 +0200
+++ lib/filters/filter-sysfs.c	2007-08-12 15:23:21.000000000 +0200
@@ -20,12 +20,14 @@
 
 #include <dirent.h>
 
-static int _locate_sysfs_blocks(const char *proc, char *path, size_t len)
+static int _locate_sysfs_blocks(const char *proc, char *path, size_t len,
+				int *sys_depth)
 {
 	char proc_mounts[PATH_MAX];
-	int r = 0;
 	FILE *fp;
 	char *split[4], buffer[PATH_MAX + 16];
+	const char *sys_mnt = NULL;
+	struct stat info;
 
 	if (!*proc) {
 		log_verbose("No proc filesystem found: skipping sysfs filter");
@@ -46,10 +48,7 @@
 	while (fgets(buffer, sizeof(buffer), fp)) {
 		if (dm_split_words(buffer, 4, 0, split) == 4 &&
 		    !strcmp(split[2], "sysfs")) {
-			if (dm_snprintf(path, len, "%s/%s", split[1],
-					 "block") >= 0) {
-				r = 1;
-			}
+			sys_mnt = split[1];
 			break;
 		}
 	}
@@ -57,7 +56,34 @@
 	if (fclose(fp))
 		log_sys_error("fclose", proc_mounts);
 
-	return r;
+	if (!sys_mnt) {
+		log_error("Failed to find sysfs mount point");
+		return 0;
+	}
+
+	if (dm_snprintf(path, len, "%s/%s", sys_mnt,
+			"subsystem/block/devices") >= 0) {
+		if (stat(path, &info) >= 0) {
+			*sys_depth = 0;
+			return 1;
+		}
+	}
+
+	if (dm_snprintf(path, len, "%s/%s", sys_mnt, "class/block") >= 0) {
+		if (stat(path, &info) >= 0) {
+			*sys_depth = 0;
+			return 1;
+		}
+	}
+
+	if (dm_snprintf(path, len, "%s/%s", sys_mnt, "block") >= 0) {
+		if (stat(path, &info) >= 0) {
+			*sys_depth = 1;
+			return 1;
+		}
+	}
+
+	return 0;
 }
 
 /*----------------------------------------------------------------
@@ -72,11 +98,13 @@
 struct dev_set {
 	struct dm_pool *mem;
 	const char *sys_block;
+	int sys_depth;
 	int initialised;
 	struct entry *slots[SET_BUCKETS];
 };
 
-static struct dev_set *_dev_set_create(struct dm_pool *mem, const char *sys_block)
+static struct dev_set *_dev_set_create(struct dm_pool *mem,
+				       const char *sys_block, int sys_depth)
 {
 	struct dev_set *ds;
 
@@ -85,6 +113,7 @@
 
 	ds->mem = mem;
 	ds->sys_block = dm_pool_strdup(mem, sys_block);
+	ds->sys_depth = sys_depth;
 	ds->initialised = 0;
 
 	return ds;
@@ -168,13 +197,13 @@
 /*
  * Recurse through sysfs directories, inserting any devs found.
  */
-static int _read_devs(struct dev_set *ds, const char *dir)
+static int _read_devs(struct dev_set *ds, const char *dir, int sysfs_depth)
 {
         struct dirent *d;
         DIR *dr;
-	unsigned char dtype;
 	struct stat info;
 	char path[PATH_MAX];
+	char file[PATH_MAX];
 	dev_t dev = { 0 };
 	int r = 1;
 
@@ -194,31 +223,22 @@
 			continue;
 		}
 
-		dtype = d->d_type;
-
-		if (dtype == DT_UNKNOWN) {
-			if (lstat(path, &info) >= 0) {
-				if (S_ISLNK(info.st_mode))
-					dtype = DT_LNK;
-				else if (S_ISDIR(info.st_mode))
-					dtype = DT_DIR;
-				else if (S_ISREG(info.st_mode))
-					dtype = DT_REG;
-			}
+		/* devices have a "dev" file */
+		if (dm_snprintf(file, sizeof(file), "%s/dev", path) < 0) {
+			log_error("sysfs path name too long: %s in %s",
+				  d->d_name, dir);
+			continue;
 		}
 
-		if (dtype == DT_DIR) {
-			if (!_read_devs(ds, path)) {
-				r = 0;
-				break;
-			}
+		if (stat(file, &info) >= 0) {
+			/* * recurse if we found a device and expect subdirs */
+			if (sysfs_depth > 0)
+				_read_devs(ds, path, sysfs_depth - 1);
+
+			/* add the device we have found */
+			if (_read_dev(file, &dev))
+				_set_insert(ds, dev);
 		}
-
-		if ((dtype == DT_REG && !strcmp(d->d_name, "dev")))
-			if (!_read_dev(path, &dev) || !_set_insert(ds, dev)) {
-				r = 0;
-				break;
-			}
 	}
 
         if (closedir(dr))
@@ -229,7 +249,7 @@
 
 static int _init_devs(struct dev_set *ds)
 {
-	if (!_read_devs(ds, ds->sys_block)) {
+	if (!_read_devs(ds, ds->sys_block, ds->sys_depth)) {
 		ds->initialised = -1;
 		return 0;
 	}
@@ -267,11 +287,12 @@
 struct dev_filter *sysfs_filter_create(const char *proc)
 {
 	char sys_block[PATH_MAX];
+	int sys_depth;
 	struct dm_pool *mem;
 	struct dev_set *ds;
 	struct dev_filter *f;
 
-	if (!_locate_sysfs_blocks(proc, sys_block, sizeof(sys_block)))
+	if (!_locate_sysfs_blocks(proc, sys_block, sizeof(sys_block), &sys_depth))
 		return NULL;
 
 	if (!(mem = dm_pool_create("sysfs", 256))) {
@@ -279,7 +300,7 @@
 		return NULL;
 	}
 
-	if (!(ds = _dev_set_create(mem, sys_block))) {
+	if (!(ds = _dev_set_create(mem, sys_block, sys_depth))) {
 		log_error("sysfs dev_set creation failed");
 		goto bad;
 	}
_______________________________________________
linux-lvm mailing list
linux-lvm@redhat.com
https://www.redhat.com/mailman/listinfo/linux-lvm
read the LVM HOW-TO at http://tldp.org/HOWTO/LVM-HOWTO/

[Index of Archives]     [Gluster Users]     [Kernel Development]     [Linux Clusters]     [Device Mapper]     [Security]     [Bugtraq]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]

  Powered by Linux