Blame SOURCES/0039-devices-use-dev-cache-aliases-handling-from-label-sc.patch

ab86b0
From 932b9720bb074e49ac920642b3fe9c3d84019787 Mon Sep 17 00:00:00 2001
ab86b0
From: David Teigland <teigland@redhat.com>
ab86b0
Date: Mon, 28 Feb 2022 17:37:12 -0600
ab86b0
Subject: [PATCH 39/54] devices: use dev-cache aliases handling from label scan
ab86b0
 functions
ab86b0
ab86b0
The label scan functions where doing some device alias validation
ab86b0
which is now better handled by the dev-cache layer, so just use
ab86b0
that.
ab86b0
---
ab86b0
 lib/device/dev-cache.c |   4 +-
ab86b0
 lib/device/dev-cache.h |   1 +
ab86b0
 lib/label/label.c      | 143 ++++++++++-------------------------------
ab86b0
 3 files changed, 36 insertions(+), 112 deletions(-)
ab86b0
ab86b0
diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
ab86b0
index 58e67e130..b0759b06c 100644
ab86b0
--- a/lib/device/dev-cache.c
ab86b0
+++ b/lib/device/dev-cache.c
ab86b0
@@ -1410,7 +1410,7 @@ static void _remove_alias(struct device *dev, const char *name)
ab86b0
  * deactivated LV.  Those old paths are all invalid and are dropped here.
ab86b0
  */
ab86b0
 
ab86b0
-static void _verify_aliases(struct device *dev)
ab86b0
+void dev_cache_verify_aliases(struct device *dev)
ab86b0
 {
ab86b0
 	struct dm_str_list *strl, *strl2;
ab86b0
 	struct stat st;
ab86b0
@@ -1459,7 +1459,7 @@ static struct device *_dev_cache_get(struct cmd_context *cmd, const char *name,
ab86b0
 			_remove_alias(dev, name);
ab86b0
 
ab86b0
 			/* Remove any other names in dev->aliases that are incorrect. */
ab86b0
-			_verify_aliases(dev);
ab86b0
+			dev_cache_verify_aliases(dev);
ab86b0
 		}
ab86b0
 		return NULL;
ab86b0
 	}
ab86b0
diff --git a/lib/device/dev-cache.h b/lib/device/dev-cache.h
ab86b0
index 51c3fc6c3..321a56d7b 100644
ab86b0
--- a/lib/device/dev-cache.h
ab86b0
+++ b/lib/device/dev-cache.h
ab86b0
@@ -55,6 +55,7 @@ int dev_cache_add_dir(const char *path);
ab86b0
 struct device *dev_cache_get(struct cmd_context *cmd, const char *name, struct dev_filter *f);
ab86b0
 struct device *dev_cache_get_existing(struct cmd_context *cmd, const char *name, struct dev_filter *f);
ab86b0
 struct device *dev_cache_get_by_devt(struct cmd_context *cmd, dev_t devt);
ab86b0
+void dev_cache_verify_aliases(struct device *dev);
ab86b0
 
ab86b0
 struct device *dev_hash_get(const char *name);
ab86b0
 
ab86b0
diff --git a/lib/label/label.c b/lib/label/label.c
ab86b0
index cf707f7a3..06958b502 100644
ab86b0
--- a/lib/label/label.c
ab86b0
+++ b/lib/label/label.c
ab86b0
@@ -458,7 +458,6 @@ static int _scan_dev_open(struct device *dev)
ab86b0
 	const char *name;
ab86b0
 	const char *modestr;
ab86b0
 	struct stat sbuf;
ab86b0
-	int retried = 0;
ab86b0
 	int flags = 0;
ab86b0
 	int fd, di;
ab86b0
 
ab86b0
@@ -478,14 +477,23 @@ static int _scan_dev_open(struct device *dev)
ab86b0
 		return 0;
ab86b0
 	}
ab86b0
 
ab86b0
+ next_name:
ab86b0
 	/*
ab86b0
 	 * All the names for this device (major:minor) are kept on
ab86b0
 	 * dev->aliases, the first one is the primary/preferred name.
ab86b0
+	 *
ab86b0
+	 * The default name preferences in dev-cache mean that the first
ab86b0
+	 * name in dev->aliases is not a symlink for scsi devices, but is
ab86b0
+	 * the /dev/mapper/ symlink for mpath devices.
ab86b0
+	 *
ab86b0
+	 * If preferred names are set to symlinks, should this
ab86b0
+	 * first attempt to open using a non-symlink?
ab86b0
+	 *
ab86b0
+	 * dm_list_first() returns NULL if the list is empty.
ab86b0
 	 */
ab86b0
 	if (!(name_list = dm_list_first(&dev->aliases))) {
ab86b0
-		/* Shouldn't happen */
ab86b0
-		log_error("Device open %s %d:%d has no path names.",
ab86b0
-			  dev_name(dev), (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
ab86b0
+		log_error("Device open %d:%d has no path names.",
ab86b0
+			  (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
ab86b0
 		return 0;
ab86b0
 	}
ab86b0
 	name_sl = dm_list_item(name_list, struct dm_str_list);
ab86b0
@@ -513,50 +521,34 @@ static int _scan_dev_open(struct device *dev)
ab86b0
 		modestr = "ro";
ab86b0
 	}
ab86b0
 
ab86b0
-retry_open:
ab86b0
-
ab86b0
 	fd = open(name, flags, 0777);
ab86b0
-
ab86b0
 	if (fd < 0) {
ab86b0
 		if ((errno == EBUSY) && (flags & O_EXCL)) {
ab86b0
 			log_error("Can't open %s exclusively.  Mounted filesystem?",
ab86b0
 				  dev_name(dev));
ab86b0
+			return 0;
ab86b0
 		} else {
ab86b0
-			int major, minor;
ab86b0
-
ab86b0
 			/*
ab86b0
-			 * Shouldn't happen, if it does, print stat info to help figure
ab86b0
-			 * out what's wrong.
ab86b0
+			 * drop name from dev->aliases and use verify_aliases to
ab86b0
+			 * drop any other invalid aliases before retrying open with
ab86b0
+			 * any remaining valid paths.
ab86b0
 			 */
ab86b0
-
ab86b0
-			major = (int)MAJOR(dev->dev);
ab86b0
-			minor = (int)MINOR(dev->dev);
ab86b0
-
ab86b0
-			log_error("Device open %s %d:%d failed errno %d", name, major, minor, errno);
ab86b0
-
ab86b0
-			if (stat(name, &sbuf)) {
ab86b0
-				log_debug_devs("Device open %s %d:%d stat failed errno %d",
ab86b0
-					       name, major, minor, errno);
ab86b0
-			} else if (sbuf.st_rdev != dev->dev) {
ab86b0
-				log_debug_devs("Device open %s %d:%d stat %d:%d does not match.",
ab86b0
-					       name, major, minor,
ab86b0
-					       (int)MAJOR(sbuf.st_rdev), (int)MINOR(sbuf.st_rdev));
ab86b0
-			}
ab86b0
-
ab86b0
-			if (!retried) {
ab86b0
-				/*
ab86b0
-				 * FIXME: remove this, the theory for this retry is that
ab86b0
-				 * there may be a udev race that we can sometimes mask by
ab86b0
-				 * retrying.  This is here until we can figure out if it's
ab86b0
-				 * needed and if so fix the real problem.
ab86b0
-				 */
ab86b0
-				usleep(5000);
ab86b0
-				log_debug_devs("Device open %s retry", dev_name(dev));
ab86b0
-				retried = 1;
ab86b0
-				goto retry_open;
ab86b0
-			}
ab86b0
+			log_debug("Drop alias for %d:%d failed open %s (%d)",
ab86b0
+				  (int)MAJOR(dev->dev), (int)MINOR(dev->dev), name, errno);
ab86b0
+			dev_cache_failed_path(dev, name);
ab86b0
+			dev_cache_verify_aliases(dev);
ab86b0
+			goto next_name;
ab86b0
 		}
ab86b0
-		return 0;
ab86b0
+	}
ab86b0
+
ab86b0
+	/* Verify that major:minor from the path still match dev. */
ab86b0
+	if ((fstat(fd, &sbuf) < 0) || (sbuf.st_rdev != dev->dev)) {
ab86b0
+		log_warn("Invalid path %s for device %d:%d, trying different path.",
ab86b0
+			 name, (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
ab86b0
+		(void)close(fd);
ab86b0
+		dev_cache_failed_path(dev, name);
ab86b0
+		dev_cache_verify_aliases(dev);
ab86b0
+		goto next_name;
ab86b0
 	}
ab86b0
 
ab86b0
 	dev->flags |= DEV_IN_BCACHE;
ab86b0
@@ -604,37 +596,6 @@ static int _scan_dev_close(struct device *dev)
ab86b0
 	return 1;
ab86b0
 }
ab86b0
 
ab86b0
-static void _drop_bad_aliases(struct device *dev)
ab86b0
-{
ab86b0
-	struct dm_str_list *strl, *strl2;
ab86b0
-	const char *name;
ab86b0
-	struct stat sbuf;
ab86b0
-	int major = (int)MAJOR(dev->dev);
ab86b0
-	int minor = (int)MINOR(dev->dev);
ab86b0
-	int bad;
ab86b0
-
ab86b0
-	dm_list_iterate_items_safe(strl, strl2, &dev->aliases) {
ab86b0
-		name = strl->str;
ab86b0
-		bad = 0;
ab86b0
-
ab86b0
-		if (stat(name, &sbuf)) {
ab86b0
-			bad = 1;
ab86b0
-			log_debug_devs("Device path check %d:%d %s stat failed errno %d",
ab86b0
-					major, minor, name, errno);
ab86b0
-		} else if (sbuf.st_rdev != dev->dev) {
ab86b0
-			bad = 1;
ab86b0
-			log_debug_devs("Device path check %d:%d %s stat %d:%d does not match.",
ab86b0
-				       major, minor, name,
ab86b0
-				       (int)MAJOR(sbuf.st_rdev), (int)MINOR(sbuf.st_rdev));
ab86b0
-		}
ab86b0
-
ab86b0
-		if (bad) {
ab86b0
-			log_debug_devs("Device path check %d:%d dropping path %s.", major, minor, name);
ab86b0
-			dev_cache_failed_path(dev, name);
ab86b0
-		}
ab86b0
-	}
ab86b0
-}
ab86b0
-
ab86b0
 // Like bcache_invalidate, only it throws any dirty data away if the
ab86b0
 // write fails.
ab86b0
 static void _invalidate_di(struct bcache *cache, int di)
ab86b0
@@ -662,10 +623,8 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f,
ab86b0
 	char headers_buf[HEADERS_BUF_SIZE];
ab86b0
 	struct dm_list wait_devs;
ab86b0
 	struct dm_list done_devs;
ab86b0
-	struct dm_list reopen_devs;
ab86b0
 	struct device_list *devl, *devl2;
ab86b0
 	struct block *bb;
ab86b0
-	int retried_open = 0;
ab86b0
 	int scan_read_errors = 0;
ab86b0
 	int scan_process_errors = 0;
ab86b0
 	int scan_failed_count = 0;
ab86b0
@@ -676,7 +635,6 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f,
ab86b0
 
ab86b0
 	dm_list_init(&wait_devs);
ab86b0
 	dm_list_init(&done_devs);
ab86b0
-	dm_list_init(&reopen_devs);
ab86b0
 
ab86b0
 	log_debug_devs("Scanning %d devices for VG info", dm_list_size(devs));
ab86b0
 
ab86b0
@@ -700,9 +658,9 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f,
ab86b0
 
ab86b0
 		if (!_in_bcache(devl->dev)) {
ab86b0
 			if (!_scan_dev_open(devl->dev)) {
ab86b0
-				log_debug_devs("Scan failed to open %s.", dev_name(devl->dev));
ab86b0
+				log_debug_devs("Scan failed to open %d:%d %s.",
ab86b0
+					       (int)MAJOR(devl->dev->dev), (int)MINOR(devl->dev->dev), dev_name(devl->dev));
ab86b0
 				dm_list_del(&devl->list);
ab86b0
-				dm_list_add(&reopen_devs, &devl->list);
ab86b0
 				devl->dev->flags |= DEV_SCAN_NOT_READ;
ab86b0
 				continue;
ab86b0
 			}
ab86b0
@@ -786,41 +744,6 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f,
ab86b0
 	if (!dm_list_empty(devs))
ab86b0
 		goto scan_more;
ab86b0
 
ab86b0
-	/*
ab86b0
-	 * We're done scanning all the devs.  If we failed to open any of them
ab86b0
-	 * the first time through, refresh device paths and retry.  We failed
ab86b0
-	 * to open the devs on the reopen_devs list.
ab86b0
-	 *
ab86b0
-	 * FIXME: it's not clear if or why this helps.
ab86b0
-	 */
ab86b0
-	if (!dm_list_empty(&reopen_devs)) {
ab86b0
-		if (retried_open) {
ab86b0
-			/* Don't try again. */
ab86b0
-			scan_failed_count += dm_list_size(&reopen_devs);
ab86b0
-			dm_list_splice(&done_devs, &reopen_devs);
ab86b0
-			goto out;
ab86b0
-		}
ab86b0
-		retried_open = 1;
ab86b0
-
ab86b0
-		dm_list_iterate_items_safe(devl, devl2, &reopen_devs) {
ab86b0
-			_drop_bad_aliases(devl->dev);
ab86b0
-
ab86b0
-			if (dm_list_empty(&devl->dev->aliases)) {
ab86b0
-				log_warn("WARNING: Scan ignoring device %d:%d with no paths.",
ab86b0
-					 (int)MAJOR(devl->dev->dev),
ab86b0
-					 (int)MINOR(devl->dev->dev));
ab86b0
-					 
ab86b0
-				dm_list_del(&devl->list);
ab86b0
-				lvmcache_del_dev(devl->dev);
ab86b0
-				scan_failed_count++;
ab86b0
-			}
ab86b0
-		}
ab86b0
-
ab86b0
-		/* Put devs that failed to open back on the original list to retry. */
ab86b0
-		dm_list_splice(devs, &reopen_devs);
ab86b0
-		goto scan_more;
ab86b0
-	}
ab86b0
-out:
ab86b0
 	log_debug_devs("Scanned devices: read errors %d process errors %d failed %d",
ab86b0
 			scan_read_errors, scan_process_errors, scan_failed_count);
ab86b0
 
ab86b0
-- 
ab86b0
2.34.3
ab86b0