Blame SOURCES/0002-Revert-pvscan-only-add-device-args-to-dev-cache.patch

b6fb8c
From 2091305b796d5552fd991c527a0359a0b4d8fde0 Mon Sep 17 00:00:00 2001
b6fb8c
From: David Teigland <teigland@redhat.com>
b6fb8c
Date: Mon, 20 Dec 2021 13:38:23 -0600
38b7b2
Subject: [PATCH 02/54] Revert "pvscan: only add device args to dev cache"
b6fb8c
b6fb8c
This reverts commit 33e47182f773c1a902b533580b63a803906de55d.
b6fb8c
---
b6fb8c
 lib/device/dev-cache.c          | 204 +++-----------------------------
b6fb8c
 lib/device/dev-cache.h          |   6 +-
b6fb8c
 lib/device/device_id.c          |  27 ++---
b6fb8c
 lib/device/device_id.h          |   1 -
b6fb8c
 test/shell/devicesfile-basic.sh |   2 +-
b6fb8c
 tools/pvscan.c                  |  58 ++++-----
b6fb8c
 6 files changed, 52 insertions(+), 246 deletions(-)
b6fb8c
b6fb8c
diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
b6fb8c
index 33b75a9a9..c6e5f68cf 100644
b6fb8c
--- a/lib/device/dev-cache.c
b6fb8c
+++ b/lib/device/dev-cache.c
b6fb8c
@@ -1852,7 +1852,7 @@ int setup_devices_file(struct cmd_context *cmd)
b6fb8c
  * Add all system devices to dev-cache, and attempt to
b6fb8c
  * match all devices_file entries to dev-cache entries.
b6fb8c
  */
b6fb8c
-int setup_devices(struct cmd_context *cmd)
b6fb8c
+static int _setup_devices(struct cmd_context *cmd, int no_file_match)
b6fb8c
 {
b6fb8c
 	int file_exists;
b6fb8c
 	int lock_mode = 0;
b6fb8c
@@ -1979,6 +1979,13 @@ int setup_devices(struct cmd_context *cmd)
b6fb8c
 	 */
b6fb8c
 	dev_cache_scan(cmd);
b6fb8c
 
b6fb8c
+	/*
b6fb8c
+	 * The caller uses "no_file_match" if it wants to match specific devs
b6fb8c
+	 * itself, instead of matching everything in device_ids_match.
b6fb8c
+	 */
b6fb8c
+	if (no_file_match && cmd->enable_devices_file)
b6fb8c
+		return 1;
b6fb8c
+
b6fb8c
 	/*
b6fb8c
 	 * Match entries from cmd->use_devices with device structs in dev-cache.
b6fb8c
 	 */
b6fb8c
@@ -1987,6 +1994,16 @@ int setup_devices(struct cmd_context *cmd)
b6fb8c
 	return 1;
b6fb8c
 }
b6fb8c
 
b6fb8c
+int setup_devices(struct cmd_context *cmd)
b6fb8c
+{
b6fb8c
+	return _setup_devices(cmd, 0);
b6fb8c
+}
b6fb8c
+
b6fb8c
+int setup_devices_no_file_match(struct cmd_context *cmd)
b6fb8c
+{
b6fb8c
+	return _setup_devices(cmd, 1);
b6fb8c
+}
b6fb8c
+
b6fb8c
 /*
b6fb8c
  * The alternative to setup_devices() when the command is interested
b6fb8c
  * in using only one PV.
b6fb8c
@@ -2055,188 +2072,3 @@ int setup_device(struct cmd_context *cmd, const char *devname)
b6fb8c
 	return 1;
b6fb8c
 }
b6fb8c
 
b6fb8c
-/*
b6fb8c
- * pvscan --cache is specialized/optimized to look only at command args,
b6fb8c
- * so this just sets up the devices file, then individual devices are
b6fb8c
- * added to dev-cache and matched with device_ids later in pvscan.
b6fb8c
- */
b6fb8c
-
b6fb8c
-int setup_devices_for_pvscan_cache(struct cmd_context *cmd)
b6fb8c
-{
b6fb8c
-	if (cmd->enable_devices_list) {
b6fb8c
-		if (!_setup_devices_list(cmd))
b6fb8c
-			return_0;
b6fb8c
-		return 1;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	if (!setup_devices_file(cmd))
b6fb8c
-		return_0;
b6fb8c
-
b6fb8c
-	if (!cmd->enable_devices_file)
b6fb8c
-		return 1;
b6fb8c
-
b6fb8c
-	if (!devices_file_exists(cmd)) {
b6fb8c
-		log_debug("Devices file not found, ignoring.");
b6fb8c
-		cmd->enable_devices_file = 0;
b6fb8c
-		return 1;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	if (!lock_devices_file(cmd, LOCK_SH)) {
b6fb8c
-		log_error("Failed to lock the devices file to read.");
b6fb8c
-		return 0;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	if (!device_ids_read(cmd)) {
b6fb8c
-		log_error("Failed to read the devices file.");
b6fb8c
-		unlock_devices_file(cmd);
b6fb8c
-		return 0;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	unlock_devices_file(cmd);
b6fb8c
-	return 1;
b6fb8c
-}
b6fb8c
-
b6fb8c
-
b6fb8c
-/* Get a device name from a devno. */
b6fb8c
-
b6fb8c
-static char *_get_devname_from_devno(struct cmd_context *cmd, dev_t devno)
b6fb8c
-{
b6fb8c
-	char path[PATH_MAX];
b6fb8c
-	char devname[PATH_MAX];
b6fb8c
-	char namebuf[NAME_LEN];
b6fb8c
-	char line[1024];
b6fb8c
-	int major = MAJOR(devno);
b6fb8c
-	int minor = MINOR(devno);
b6fb8c
-	int line_major;
b6fb8c
-	int line_minor;
b6fb8c
-	uint64_t line_blocks;
b6fb8c
-	DIR *dir;
b6fb8c
-	struct dirent *dirent;
b6fb8c
-	FILE *fp;
b6fb8c
-
b6fb8c
-	/*
b6fb8c
-	 * $ ls /sys/dev/block/8:0/device/block/
b6fb8c
-	 * sda
b6fb8c
-	 */
b6fb8c
-	if (major_is_scsi_device(cmd->dev_types, major)) {
b6fb8c
-		if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d/device/block",
b6fb8c
-				dm_sysfs_dir(), major, minor) < 0) {
b6fb8c
-			return NULL;
b6fb8c
-		}
b6fb8c
-
b6fb8c
-		if (!(dir = opendir(path)))
b6fb8c
-			return NULL;
b6fb8c
-
b6fb8c
-		while ((dirent = readdir(dir))) {
b6fb8c
-			if (dirent->d_name[0] == '.')
b6fb8c
-				continue;
b6fb8c
-			if (dm_snprintf(devname, sizeof(devname), "/dev/%s", dirent->d_name) < 0) {
b6fb8c
-				devname[0] = '\0';
b6fb8c
-				stack;
b6fb8c
-			}
b6fb8c
-			break;
b6fb8c
-		}
b6fb8c
-		closedir(dir);
b6fb8c
-
b6fb8c
-		if (devname[0]) {
b6fb8c
-			log_debug("Found %s for %d:%d from sys", devname, major, minor);
b6fb8c
-			return _strdup(devname);
b6fb8c
-		}
b6fb8c
-		return NULL;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	/*
b6fb8c
-	 * $ cat /sys/dev/block/253:3/dm/name
b6fb8c
-	 * mpatha
b6fb8c
-	 */
b6fb8c
-	if (major == cmd->dev_types->device_mapper_major) {
b6fb8c
-		if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d/dm/name",
b6fb8c
-				dm_sysfs_dir(), major, minor) < 0) {
b6fb8c
-			return NULL;
b6fb8c
-		}
b6fb8c
-
b6fb8c
-		if (!get_sysfs_value(path, namebuf, sizeof(namebuf), 0))
b6fb8c
-			return NULL;
b6fb8c
-
b6fb8c
-		if (dm_snprintf(devname, sizeof(devname), "/dev/mapper/%s", namebuf) < 0) {
b6fb8c
-			devname[0] = '\0';
b6fb8c
-			stack;
b6fb8c
-		}
b6fb8c
-
b6fb8c
-		if (devname[0]) {
b6fb8c
-			log_debug("Found %s for %d:%d from sys", devname, major, minor);
b6fb8c
-			return _strdup(devname);
b6fb8c
-		}
b6fb8c
-		return NULL;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	/*
b6fb8c
-	 * /proc/partitions lists
b6fb8c
-	 * major minor #blocks name
b6fb8c
-	 */
b6fb8c
-
b6fb8c
-	if (!(fp = fopen("/proc/partitions", "r")))
b6fb8c
-		return NULL;
b6fb8c
-
b6fb8c
-	while (fgets(line, sizeof(line), fp)) {
b6fb8c
-		if (sscanf(line, "%u %u %llu %s", &line_major, &line_minor, (unsigned long long *)&line_blocks, namebuf) != 4)
b6fb8c
-			continue;
b6fb8c
-		if (line_major != major)
b6fb8c
-			continue;
b6fb8c
-		if (line_minor != minor)
b6fb8c
-			continue;
b6fb8c
-
b6fb8c
-		if (dm_snprintf(devname, sizeof(devname), "/dev/%s", namebuf) < 0) {
b6fb8c
-			devname[0] = '\0';
b6fb8c
-			stack;
b6fb8c
-		}
b6fb8c
-		break;
b6fb8c
-	}
b6fb8c
-	fclose(fp);
b6fb8c
-
b6fb8c
-	if (devname[0]) {
b6fb8c
-		log_debug("Found %s for %d:%d from proc", devname, major, minor);
b6fb8c
-		return _strdup(devname);
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	/*
b6fb8c
-	 * If necessary, this could continue searching by stat'ing /dev entries.
b6fb8c
-	 */
b6fb8c
-
b6fb8c
-	return NULL;
b6fb8c
-}
b6fb8c
-
b6fb8c
-int setup_devname_in_dev_cache(struct cmd_context *cmd, const char *devname)
b6fb8c
-{
b6fb8c
-	struct stat buf;
b6fb8c
-	struct device *dev;
b6fb8c
-
b6fb8c
-	if (stat(devname, &buf) < 0) {
b6fb8c
-		log_error("Cannot access device %s.", devname);
b6fb8c
-		return 0;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	if (!S_ISBLK(buf.st_mode)) {
b6fb8c
-		log_error("Invaild device type %s.", devname);
b6fb8c
-		return 0;
b6fb8c
-	}
b6fb8c
-
b6fb8c
-	if (!_insert_dev(devname, buf.st_rdev))
b6fb8c
-		return_0;
b6fb8c
-
b6fb8c
-	if (!(dev = (struct device *) dm_hash_lookup(_cache.names, devname)))
b6fb8c
-		return_0;
b6fb8c
-
b6fb8c
-	return 1;
b6fb8c
-}
b6fb8c
-
b6fb8c
-int setup_devno_in_dev_cache(struct cmd_context *cmd, dev_t devno)
b6fb8c
-{
b6fb8c
-	const char *devname;
b6fb8c
-
b6fb8c
-	if (!(devname = _get_devname_from_devno(cmd, devno)))
b6fb8c
-		return_0;
b6fb8c
-
b6fb8c
-	return setup_devname_in_dev_cache(cmd, devname);
b6fb8c
-}
b6fb8c
-
b6fb8c
diff --git a/lib/device/dev-cache.h b/lib/device/dev-cache.h
b6fb8c
index 143848d6d..635dc4fc9 100644
b6fb8c
--- a/lib/device/dev-cache.h
b6fb8c
+++ b/lib/device/dev-cache.h
b6fb8c
@@ -77,11 +77,7 @@ int get_dm_uuid_from_sysfs(char *buf, size_t buf_size, int major, int minor);
b6fb8c
 
b6fb8c
 int setup_devices_file(struct cmd_context *cmd);
b6fb8c
 int setup_devices(struct cmd_context *cmd);
b6fb8c
+int setup_devices_no_file_match(struct cmd_context *cmd);
b6fb8c
 int setup_device(struct cmd_context *cmd, const char *devname);
b6fb8c
 
b6fb8c
-/* Normal device setup functions are split up for pvscan optimization. */
b6fb8c
-int setup_devices_for_pvscan_cache(struct cmd_context *cmd);
b6fb8c
-int setup_devname_in_dev_cache(struct cmd_context *cmd, const char *devname);
b6fb8c
-int setup_devno_in_dev_cache(struct cmd_context *cmd, dev_t devno);
b6fb8c
-
b6fb8c
 #endif
b6fb8c
diff --git a/lib/device/device_id.c b/lib/device/device_id.c
b6fb8c
index 167bf661b..eb06109ff 100644
b6fb8c
--- a/lib/device/device_id.c
b6fb8c
+++ b/lib/device/device_id.c
b6fb8c
@@ -1534,22 +1534,6 @@ int device_ids_match_dev(struct cmd_context *cmd, struct device *dev)
b6fb8c
  * passes the filter.
b6fb8c
  */
b6fb8c
 
b6fb8c
-void device_ids_match_device_list(struct cmd_context *cmd)
b6fb8c
-{
b6fb8c
-	struct dev_use *du;
b6fb8c
-
b6fb8c
-	dm_list_iterate_items(du, &cmd->use_devices) {
b6fb8c
-		if (du->dev)
b6fb8c
-			continue;
b6fb8c
-		if (!(du->dev = dev_cache_get(cmd, du->devname, NULL))) {
b6fb8c
-			log_warn("Device not found for %s.", du->devname);
b6fb8c
-		} else {
b6fb8c
-			/* Should we set dev->id?  Which idtype?  Use --deviceidtype? */
b6fb8c
-			du->dev->flags |= DEV_MATCHED_USE_ID;
b6fb8c
-		}
b6fb8c
-	}
b6fb8c
-}
b6fb8c
-
b6fb8c
 void device_ids_match(struct cmd_context *cmd)
b6fb8c
 {
b6fb8c
 	struct dev_iter *iter;
b6fb8c
@@ -1557,7 +1541,16 @@ void device_ids_match(struct cmd_context *cmd)
b6fb8c
 	struct device *dev;
b6fb8c
 
b6fb8c
 	if (cmd->enable_devices_list) {
b6fb8c
-		device_ids_match_device_list(cmd);
b6fb8c
+		dm_list_iterate_items(du, &cmd->use_devices) {
b6fb8c
+			if (du->dev)
b6fb8c
+				continue;
b6fb8c
+			if (!(du->dev = dev_cache_get(cmd, du->devname, NULL))) {
b6fb8c
+				log_warn("Device not found for %s.", du->devname);
b6fb8c
+			} else {
b6fb8c
+				/* Should we set dev->id?  Which idtype?  Use --deviceidtype? */
b6fb8c
+				du->dev->flags |= DEV_MATCHED_USE_ID;
b6fb8c
+			}
b6fb8c
+		}
b6fb8c
 		return;
b6fb8c
 	}
b6fb8c
 
b6fb8c
diff --git a/lib/device/device_id.h b/lib/device/device_id.h
b6fb8c
index 0ada35c94..939b3a0f4 100644
b6fb8c
--- a/lib/device/device_id.h
b6fb8c
+++ b/lib/device/device_id.h
b6fb8c
@@ -32,7 +32,6 @@ int device_id_add(struct cmd_context *cmd, struct device *dev, const char *pvid,
b6fb8c
 void device_id_pvremove(struct cmd_context *cmd, struct device *dev);
b6fb8c
 void device_ids_match(struct cmd_context *cmd);
b6fb8c
 int device_ids_match_dev(struct cmd_context *cmd, struct device *dev);
b6fb8c
-void device_ids_match_device_list(struct cmd_context *cmd);
b6fb8c
 void device_ids_validate(struct cmd_context *cmd, struct dm_list *scanned_devs, int *device_ids_invalid, int noupdate);
b6fb8c
 int device_ids_version_unchanged(struct cmd_context *cmd);
b6fb8c
 void device_ids_find_renamed_devs(struct cmd_context *cmd, struct dm_list *dev_list, int *search_count, int noupdate);
b6fb8c
diff --git a/test/shell/devicesfile-basic.sh b/test/shell/devicesfile-basic.sh
b6fb8c
index 9c3455c76..7ba9e2c7f 100644
b6fb8c
--- a/test/shell/devicesfile-basic.sh
b6fb8c
+++ b/test/shell/devicesfile-basic.sh
b6fb8c
@@ -283,7 +283,7 @@ not ls "$RUNDIR/lvm/pvs_online/$PVID3"
b6fb8c
 # arg in devices list
b6fb8c
 _clear_online_files
b6fb8c
 pvscan --devices "$dev3" --cache -aay "$dev3"
b6fb8c
-pvscan --devices "$dev4","$dev3" --cache -aay "$dev4"
b6fb8c
+pvscan --devices "$dev4" --cache -aay "$dev4"
b6fb8c
 check lv_field $vg2/$lv2 lv_active "active"
b6fb8c
 vgchange -an $vg2
b6fb8c
 
b6fb8c
diff --git a/tools/pvscan.c b/tools/pvscan.c
b6fb8c
index 95d593d57..8e2611361 100644
b6fb8c
--- a/tools/pvscan.c
b6fb8c
+++ b/tools/pvscan.c
b6fb8c
@@ -857,21 +857,11 @@ static int _get_devs_from_saved_vg(struct cmd_context *cmd, const char *vgname,
b6fb8c
 
b6fb8c
 		devno = MKDEV(file_major, file_minor);
b6fb8c
 
b6fb8c
-		if (!setup_devno_in_dev_cache(cmd, devno)) {
b6fb8c
-			log_error_pvscan(cmd, "No device set up for %d:%d PVID %s", file_major, file_minor, pvid);
b6fb8c
-			goto bad;
b6fb8c
-		}
b6fb8c
-
b6fb8c
 		if (!(dev = dev_cache_get_by_devt(cmd, devno, NULL, NULL))) {
b6fb8c
 			log_error_pvscan(cmd, "No device found for %d:%d PVID %s", file_major, file_minor, pvid);
b6fb8c
 			goto bad;
b6fb8c
 		}
b6fb8c
 
b6fb8c
-		/*
b6fb8c
-		 * Do not need to match device_id here, see comment after
b6fb8c
-		 * get_devs_from_saved_vg about relying on pvid online file.
b6fb8c
-		 */
b6fb8c
-
b6fb8c
 		name1 = dev_name(dev);
b6fb8c
 		name2 = pvl->pv->device_hint;
b6fb8c
 
b6fb8c
@@ -1109,11 +1099,17 @@ static int _pvscan_aa(struct cmd_context *cmd, struct pvscan_aa_params *pp,
b6fb8c
 		 * PROCESS_SKIP_SCAN: we have already done lvmcache_label_scan
b6fb8c
 		 * so tell process_each to skip it.
b6fb8c
 		 */
b6fb8c
+		if (do_all)
b6fb8c
+			read_flags |= PROCESS_SKIP_SCAN;
b6fb8c
 
b6fb8c
+		/*
b6fb8c
+		 * When the command is processing specific devs (not all), it
b6fb8c
+		 * has done setup_devices_no_file_match() to avoid matching ids
b6fb8c
+		 * fo all devs unnecessarily, but now that we're falling back
b6fb8c
+		 * to process_each_vg() we need to complete the id matching.
b6fb8c
+		 */
b6fb8c
 		if (!do_all)
b6fb8c
-			lvmcache_label_scan(cmd);
b6fb8c
-
b6fb8c
-		read_flags |= PROCESS_SKIP_SCAN;
b6fb8c
+			device_ids_match(cmd);
b6fb8c
 
b6fb8c
 		ret = process_each_vg(cmd, 0, NULL, NULL, vgnames, read_flags, 0, handle, _pvscan_aa_single);
b6fb8c
 	}
b6fb8c
@@ -1196,15 +1192,11 @@ static int _get_args_devs(struct cmd_context *cmd, struct dm_list *pvscan_args,
b6fb8c
 	/* in common usage, no dev will be found for a devno */
b6fb8c
 
b6fb8c
 	dm_list_iterate_items(arg, pvscan_args) {
b6fb8c
-		if (arg->devname) {
b6fb8c
-			if (!setup_devname_in_dev_cache(cmd, arg->devname))
b6fb8c
-				log_error_pvscan(cmd, "No device set up for name arg %s", arg->devname);
b6fb8c
+		if (arg->devname)
b6fb8c
 			arg->dev = dev_cache_get(cmd, arg->devname, NULL);
b6fb8c
-		} else if (arg->devno) {
b6fb8c
-			if (!setup_devno_in_dev_cache(cmd, arg->devno))
b6fb8c
-				log_error_pvscan(cmd, "No device set up for devno arg %d", (int)arg->devno);
b6fb8c
+		else if (arg->devno)
b6fb8c
 			arg->dev = dev_cache_get_by_devt(cmd, arg->devno, NULL, NULL);
b6fb8c
-		} else
b6fb8c
+		else
b6fb8c
 			return_0;
b6fb8c
 	}
b6fb8c
 
b6fb8c
@@ -1680,13 +1672,11 @@ static int _pvscan_cache_args(struct cmd_context *cmd, int argc, char **argv,
b6fb8c
 	cmd->pvscan_cache_single = 1;
b6fb8c
 
b6fb8c
 	/*
b6fb8c
-	 * Special pvscan-specific setup steps to avoid looking
b6fb8c
-	 * at any devices except for device args.
b6fb8c
-	 * Read devices file and determine if devices file will be used.
b6fb8c
-	 * Does not do dev_cache_scan (adds nothing to dev-cache), and
b6fb8c
-	 * does not do any device id matching.
b6fb8c
+	 * "no_file_match" means that when the devices file is used,
b6fb8c
+	 * setup_devices will skip matching devs to devices file entries.
b6fb8c
+	 * Specific devs must be matched later with device_ids_match_dev().
b6fb8c
 	 */
b6fb8c
-	if (!setup_devices_for_pvscan_cache(cmd)) {
b6fb8c
+	if (!setup_devices_no_file_match(cmd)) {
b6fb8c
 		log_error_pvscan(cmd, "Failed to set up devices.");
b6fb8c
 		return 0;
b6fb8c
 	}
b6fb8c
@@ -1745,21 +1735,17 @@ static int _pvscan_cache_args(struct cmd_context *cmd, int argc, char **argv,
b6fb8c
 	log_debug("pvscan_cache_args: filter devs nodata");
b6fb8c
 
b6fb8c
 	/*
b6fb8c
-	 * Match dev args with the devices file because special/optimized
b6fb8c
-	 * device setup was used above which does not check the devices file.
b6fb8c
-	 * If a match fails here do not exclude it, that will be done below by
b6fb8c
-	 * passes_filter() which runs filter-deviceid. The
b6fb8c
-	 * relax_deviceid_filter case needs to be able to work around
b6fb8c
+	 * Match dev args with the devices file because
b6fb8c
+	 * setup_devices_no_file_match() was used above which skipped checking
b6fb8c
+	 * the devices file.  If a match fails here do not exclude it, that
b6fb8c
+	 * will be done below by passes_filter() which runs filter-deviceid.
b6fb8c
+	 * The relax_deviceid_filter case needs to be able to work around
b6fb8c
 	 * unmatching devs.
b6fb8c
 	 */
b6fb8c
-
b6fb8c
 	if (cmd->enable_devices_file) {
b6fb8c
-		dm_list_iterate_items(devl, &pvscan_devs)
b6fb8c
+		dm_list_iterate_items_safe(devl, devl2, &pvscan_devs)
b6fb8c
 			device_ids_match_dev(cmd, devl->dev);
b6fb8c
-
b6fb8c
 	}
b6fb8c
-	if (cmd->enable_devices_list)
b6fb8c
-		device_ids_match_device_list(cmd);
b6fb8c
 
b6fb8c
 	if (cmd->enable_devices_file && device_ids_use_devname(cmd)) {
b6fb8c
 		relax_deviceid_filter = 1;
b6fb8c
-- 
38b7b2
2.34.3
b6fb8c