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

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