Blame SOURCES/0006-filter-sysfs-skip-when-device-id-is-set.patch

fabacb
From f73be4480a5dd104a77e3ef84d7dcc80b834e593 Mon Sep 17 00:00:00 2001
fabacb
From: David Teigland <teigland@redhat.com>
fabacb
Date: Tue, 2 Nov 2021 15:42:26 -0500
4d51e5
Subject: [PATCH 06/54] filter-sysfs: skip when device id is set
fabacb
fabacb
When a device id is set for a device, using an idtype other
fabacb
than devname, it means that sysfs has been used with the device
fabacb
to match the device id.  So, checking for a sysfs entry for the
fabacb
device in filter-sysfs is redundant.  For any other cases not
fabacb
covered by this (e.g. devname ids), have filter-sysfs simply
fabacb
stat /sys/dev/block/major:minor to test if the device exists
fabacb
in sysfs.
fabacb
fabacb
The extensive processing done by filter-sysfs init is removed.
fabacb
It was taking an immense amount of time with many devices, e.g.
fabacb
. 1024 PVs in 520 VGs
fabacb
. 520 concurrent vgchange -ay <vgname> commands
fabacb
. vgchange scans only PVs in the named VG (based on pvs_online
fabacb
  files from a pending patch)
fabacb
fabacb
A large number of the vgchange commands were taking over 1 min,
fabacb
and nearly half of that time was used by filter-sysfs init.
fabacb
With this patch, the vgchange commands take about half the time.
fabacb
---
fabacb
 lib/commands/toolcontext.c |  24 ++-
fabacb
 lib/filters/filter-sysfs.c | 296 +++----------------------------------
fabacb
 2 files changed, 32 insertions(+), 288 deletions(-)
fabacb
fabacb
diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c
fabacb
index 1b7170de1..a0c78ddd6 100644
fabacb
--- a/lib/commands/toolcontext.c
fabacb
+++ b/lib/commands/toolcontext.c
fabacb
@@ -1143,19 +1143,6 @@ static struct dev_filter *_init_filter_chain(struct cmd_context *cmd)
fabacb
 	 * Update MAX_FILTERS definition above when adding new filters.
fabacb
 	 */
fabacb
 
fabacb
-	/*
fabacb
-	 * sysfs filter. Only available on 2.6 kernels.  Non-critical.
fabacb
-	 * Listed first because it's very efficient at eliminating
fabacb
-	 * unavailable devices.
fabacb
-	 *
fabacb
-	 * TODO: I suspect that using the lvm_type and device_id
fabacb
-	 * filters before this one may be more efficient.
fabacb
-	 */
fabacb
-	if (find_config_tree_bool(cmd, devices_sysfs_scan_CFG, NULL)) {
fabacb
-		if ((filters[nr_filt] = sysfs_filter_create()))
fabacb
-			nr_filt++;
fabacb
-	}
fabacb
-
fabacb
 	/* internal filter used by command processing. */
fabacb
 	if (!(filters[nr_filt] = internal_filter_create())) {
fabacb
 		log_error("Failed to create internal device filter");
fabacb
@@ -1195,6 +1182,17 @@ static struct dev_filter *_init_filter_chain(struct cmd_context *cmd)
fabacb
 	}
fabacb
 	nr_filt++;
fabacb
 
fabacb
+	/*
fabacb
+	 * sysfs filter. Only available on 2.6 kernels.  Non-critical.
fabacb
+	 * Eliminates unavailable devices.
fabacb
+	 * TODO: this may be unnecessary now with device ids
fabacb
+	 * (currently not used for devs match to device id using syfs)
fabacb
+	 */
fabacb
+	if (find_config_tree_bool(cmd, devices_sysfs_scan_CFG, NULL)) {
fabacb
+		if ((filters[nr_filt] = sysfs_filter_create()))
fabacb
+			nr_filt++;
fabacb
+	}
fabacb
+
fabacb
 	/* usable device filter. Required. */
fabacb
 	if (!(filters[nr_filt] = usable_filter_create(cmd, cmd->dev_types, FILTER_MODE_NO_LVMETAD))) {
fabacb
 		log_error("Failed to create usabled device filter");
fabacb
diff --git a/lib/filters/filter-sysfs.c b/lib/filters/filter-sysfs.c
fabacb
index 32ac324dd..672211057 100644
fabacb
--- a/lib/filters/filter-sysfs.c
fabacb
+++ b/lib/filters/filter-sysfs.c
fabacb
@@ -17,288 +17,49 @@
fabacb
 
fabacb
 #ifdef __linux__
fabacb
 
fabacb
-#include <sys/sysmacros.h>
fabacb
-#include <dirent.h>
fabacb
-
fabacb
-static int _locate_sysfs_blocks(const char *sysfs_dir, char *path, size_t len,
fabacb
-				unsigned *sysfs_depth)
fabacb
+static int _accept_p(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
fabacb
 {
fabacb
+	char path[PATH_MAX];
fabacb
+	const char *sysfs_dir;
fabacb
 	struct stat info;
fabacb
-	unsigned i;
fabacb
-	static const struct dir_class {
fabacb
-		const char path[32];
fabacb
-		int depth;
fabacb
-	} classes[] = {
fabacb
-		/*
fabacb
-		 * unified classification directory for all kernel subsystems
fabacb
-		 *
fabacb
-		 * /sys/subsystem/block/devices
fabacb
-		 * |-- sda -> ../../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
fabacb
-		 * |-- sda1 -> ../../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1
fabacb
-		 *  `-- sr0 -> ../../../devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
fabacb
-		 *
fabacb
-		 */
fabacb
-		{ "subsystem/block/devices", 0 },
fabacb
-
fabacb
-		/*
fabacb
-		 * block subsystem as a class
fabacb
-		 *
fabacb
-		 * /sys/class/block
fabacb
-		 * |-- sda -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
fabacb
-		 * |-- sda1 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1
fabacb
-		 *  `-- sr0 -> ../../devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
fabacb
-		 *
fabacb
-		 */
fabacb
-		{ "class/block", 0 },
fabacb
-
fabacb
-		/*
fabacb
-		 * old block subsystem layout with nested directories
fabacb
-		 *
fabacb
-		 * /sys/block/
fabacb
-		 * |-- sda
fabacb
-		 * |   |-- capability
fabacb
-		 * |   |-- dev
fabacb
-		 * ...
fabacb
-		 * |   |-- sda1
fabacb
-		 * |   |   |-- dev
fabacb
-		 * ...
fabacb
-		 * |
fabacb
-		 * `-- sr0
fabacb
-		 *     |-- capability
fabacb
-		 *     |-- dev
fabacb
-		 * ...
fabacb
-		 *
fabacb
-		 */
fabacb
-
fabacb
-		{ "block", 1 }
fabacb
-	};
fabacb
-
fabacb
-	for (i = 0; i < DM_ARRAY_SIZE(classes); ++i)
fabacb
-		if ((dm_snprintf(path, len, "%s%s", sysfs_dir, classes[i].path) >= 0) &&
fabacb
-		    (stat(path, &info) == 0)) {
fabacb
-			*sysfs_depth = classes[i].depth;
fabacb
-			return 1;
fabacb
-		}
fabacb
-
fabacb
-	return 0;
fabacb
-}
fabacb
-
fabacb
-/*----------------------------------------------------------------
fabacb
- * We need to store a set of dev_t.
fabacb
- *--------------------------------------------------------------*/
fabacb
-struct entry {
fabacb
-	struct entry *next;
fabacb
-	dev_t dev;
fabacb
-};
fabacb
-
fabacb
-#define SET_BUCKETS 64
fabacb
-struct dev_set {
fabacb
-	struct dm_pool *mem;
fabacb
-	const char *sys_block;
fabacb
-	unsigned sysfs_depth;
fabacb
-	int initialised;
fabacb
-	struct entry *slots[SET_BUCKETS];
fabacb
-};
fabacb
-
fabacb
-static struct dev_set *_dev_set_create(struct dm_pool *mem,
fabacb
-				       const char *sys_block,
fabacb
-				       unsigned sysfs_depth)
fabacb
-{
fabacb
-	struct dev_set *ds;
fabacb
-
fabacb
-	if (!(ds = dm_pool_zalloc(mem, sizeof(*ds))))
fabacb
-		return NULL;
fabacb
-
fabacb
-	ds->mem = mem;
fabacb
-	if (!(ds->sys_block = dm_pool_strdup(mem, sys_block)))
fabacb
-		return NULL;
fabacb
-
fabacb
-	ds->sysfs_depth = sysfs_depth;
fabacb
-	ds->initialised = 0;
fabacb
-
fabacb
-	return ds;
fabacb
-}
fabacb
-
fabacb
-static unsigned _hash_dev(dev_t dev)
fabacb
-{
fabacb
-	return (major(dev) ^ minor(dev)) & (SET_BUCKETS - 1);
fabacb
-}
fabacb
 
fabacb
-/*
fabacb
- * Doesn't check that the set already contains dev.
fabacb
- */
fabacb
-static int _set_insert(struct dev_set *ds, dev_t dev)
fabacb
-{
fabacb
-	struct entry *e;
fabacb
-	unsigned h = _hash_dev(dev);
fabacb
-
fabacb
-	if (!(e = dm_pool_alloc(ds->mem, sizeof(*e))))
fabacb
-		return 0;
fabacb
-
fabacb
-	e->next = ds->slots[h];
fabacb
-	e->dev = dev;
fabacb
-	ds->slots[h] = e;
fabacb
-
fabacb
-	return 1;
fabacb
-}
fabacb
+	dev->filtered_flags &= ~DEV_FILTERED_SYSFS;
fabacb
 
fabacb
-static int _set_lookup(struct dev_set *ds, dev_t dev)
fabacb
-{
fabacb
-	unsigned h = _hash_dev(dev);
fabacb
-	struct entry *e;
fabacb
+	/*
fabacb
+	 * Any kind of device id other than devname has been set
fabacb
+	 * using sysfs so we know that sysfs info exists for dev.
fabacb
+	 */
fabacb
+	if (dev->id && dev->id->idtype && (dev->id->idtype != DEV_ID_TYPE_DEVNAME))
fabacb
+		return 1;
fabacb
 
fabacb
-	for (e = ds->slots[h]; e; e = e->next)
fabacb
-		if (e->dev == dev)
fabacb
+	sysfs_dir = dm_sysfs_dir();
fabacb
+	if (sysfs_dir && *sysfs_dir) {
fabacb
+		if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d",
fabacb
+				sysfs_dir, (int)MAJOR(dev->dev), (int)MINOR(dev->dev)) < 0) {
fabacb
+			log_debug("failed to create sysfs path");
fabacb
 			return 1;
fabacb
-
fabacb
-	return 0;
fabacb
-}
fabacb
-
fabacb
-/*----------------------------------------------------------------
fabacb
- * filter methods
fabacb
- *--------------------------------------------------------------*/
fabacb
-static int _parse_dev(const char *file, FILE *fp, dev_t *result)
fabacb
-{
fabacb
-	unsigned major, minor;
fabacb
-	char buffer[64];
fabacb
-
fabacb
-	if (!fgets(buffer, sizeof(buffer), fp)) {
fabacb
-		log_error("Empty sysfs device file: %s", file);
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
-	if (sscanf(buffer, "%u:%u", &major, &minor) != 2) {
fabacb
-		log_error("Incorrect format for sysfs device file: %s.", file);
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
-	*result = makedev(major, minor);
fabacb
-	return 1;
fabacb
-}
fabacb
-
fabacb
-static int _read_dev(const char *file, dev_t *result)
fabacb
-{
fabacb
-	int r;
fabacb
-	FILE *fp;
fabacb
-
fabacb
-	if (!(fp = fopen(file, "r"))) {
fabacb
-		log_sys_error("fopen", file);
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
-	r = _parse_dev(file, fp, result);
fabacb
-
fabacb
-	if (fclose(fp))
fabacb
-		log_sys_error("fclose", file);
fabacb
-
fabacb
-	return r;
fabacb
-}
fabacb
-
fabacb
-/*
fabacb
- * Recurse through sysfs directories, inserting any devs found.
fabacb
- */
fabacb
-static int _read_devs(struct dev_set *ds, const char *dir, unsigned sysfs_depth)
fabacb
-{
fabacb
-	struct dirent *d;
fabacb
-	DIR *dr;
fabacb
-	struct stat info;
fabacb
-	char path[PATH_MAX];
fabacb
-	char file[PATH_MAX];
fabacb
-	dev_t dev = { 0 };
fabacb
-	int r = 1;
fabacb
-
fabacb
-	if (!(dr = opendir(dir))) {
fabacb
-		log_sys_error("opendir", dir);
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
-	while ((d = readdir(dr))) {
fabacb
-		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
fabacb
-			continue;
fabacb
-
fabacb
-		if (dm_snprintf(path, sizeof(path), "%s/%s", dir,
fabacb
-				 d->d_name) < 0) {
fabacb
-			log_warn("WARNING: sysfs path name too long: %s in %s.",
fabacb
-				 d->d_name, dir);
fabacb
-			continue;
fabacb
 		}
fabacb
 
fabacb
-		/* devices have a "dev" file */
fabacb
-		if (dm_snprintf(file, sizeof(file), "%s/dev", path) < 0) {
fabacb
-			log_warn("WARNING: sysfs path name too long: %s in %s.",
fabacb
-				 d->d_name, dir);
fabacb
-			continue;
fabacb
-		}
fabacb
-
fabacb
-		if (!stat(file, &info)) {
fabacb
-			/* recurse if we found a device and expect subdirs */
fabacb
-			if (sysfs_depth)
fabacb
-				_read_devs(ds, path, sysfs_depth - 1);
fabacb
-
fabacb
-			/* add the device we have found */
fabacb
-			if (_read_dev(file, &dev))
fabacb
-				_set_insert(ds, dev);
fabacb
+		if (lstat(path, &info)) {
fabacb
+			log_debug_devs("%s: Skipping (sysfs)", dev_name(dev));
fabacb
+			dev->filtered_flags |= DEV_FILTERED_SYSFS;
fabacb
+			return 0;
fabacb
 		}
fabacb
 	}
fabacb
 
fabacb
-	if (closedir(dr))
fabacb
-		log_sys_debug("closedir", dir);
fabacb
-
fabacb
-	return r;
fabacb
-}
fabacb
-
fabacb
-static int _init_devs(struct dev_set *ds)
fabacb
-{
fabacb
-	if (!_read_devs(ds, ds->sys_block, ds->sysfs_depth)) {
fabacb
-		ds->initialised = -1;
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
-	ds->initialised = 1;
fabacb
-
fabacb
-	return 1;
fabacb
-}
fabacb
-
fabacb
-
fabacb
-static int _accept_p(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
fabacb
-{
fabacb
-	struct dev_set *ds = (struct dev_set *) f->private;
fabacb
-
fabacb
-	dev->filtered_flags &= ~DEV_FILTERED_SYSFS;
fabacb
-
fabacb
-	if (!ds->initialised)
fabacb
-		_init_devs(ds);
fabacb
-
fabacb
-	/* Pass through if initialisation failed */
fabacb
-	if (ds->initialised != 1)
fabacb
-		return 1;
fabacb
-
fabacb
-	if (!_set_lookup(ds, dev->dev)) {
fabacb
-		log_debug_devs("%s: Skipping (sysfs)", dev_name(dev));
fabacb
-		dev->filtered_flags |= DEV_FILTERED_SYSFS;
fabacb
-		return 0;
fabacb
-	}
fabacb
-
fabacb
 	return 1;
fabacb
 }
fabacb
 
fabacb
 static void _destroy(struct dev_filter *f)
fabacb
 {
fabacb
-	struct dev_set *ds = (struct dev_set *) f->private;
fabacb
-
fabacb
 	if (f->use_count)
fabacb
 		log_error(INTERNAL_ERROR "Destroying sysfs filter while in use %u times.", f->use_count);
fabacb
-
fabacb
-	dm_pool_destroy(ds->mem);
fabacb
+	free(f);
fabacb
 }
fabacb
 
fabacb
 struct dev_filter *sysfs_filter_create(void)
fabacb
 {
fabacb
 	const char *sysfs_dir = dm_sysfs_dir();
fabacb
-	char sys_block[PATH_MAX];
fabacb
-	unsigned sysfs_depth;
fabacb
-	struct dm_pool *mem;
fabacb
-	struct dev_set *ds;
fabacb
 	struct dev_filter *f;
fabacb
 
fabacb
 	if (!*sysfs_dir) {
fabacb
@@ -306,26 +67,12 @@ struct dev_filter *sysfs_filter_create(void)
fabacb
 		return NULL;
fabacb
 	}
fabacb
 
fabacb
-	if (!_locate_sysfs_blocks(sysfs_dir, sys_block, sizeof(sys_block), &sysfs_depth))
fabacb
-		return NULL;
fabacb
-
fabacb
-	if (!(mem = dm_pool_create("sysfs", 256))) {
fabacb
-		log_error("sysfs pool creation failed");
fabacb
-		return NULL;
fabacb
-	}
fabacb
-
fabacb
-	if (!(ds = _dev_set_create(mem, sys_block, sysfs_depth))) {
fabacb
-		log_error("sysfs dev_set creation failed");
fabacb
-		goto bad;
fabacb
-	}
fabacb
-
fabacb
-	if (!(f = dm_pool_zalloc(mem, sizeof(*f))))
fabacb
+	if (!(f = zalloc(sizeof(*f))))
fabacb
 		goto_bad;
fabacb
 
fabacb
 	f->passes_filter = _accept_p;
fabacb
 	f->destroy = _destroy;
fabacb
 	f->use_count = 0;
fabacb
-	f->private = ds;
fabacb
 	f->name = "sysfs";
fabacb
 
fabacb
 	log_debug_devs("Sysfs filter initialised.");
fabacb
@@ -333,7 +80,6 @@ struct dev_filter *sysfs_filter_create(void)
fabacb
 	return f;
fabacb
 
fabacb
  bad:
fabacb
-	dm_pool_destroy(mem);
fabacb
 	return NULL;
fabacb
 }
fabacb
 
fabacb
-- 
4d51e5
2.34.3
fabacb