Blame SOURCES/lvm2-2_03_12-devs-remove-invalid-path-name-aliases.patch

31f061
 lib/device/dev-cache.c    | 161 ++++++++++++++++++++++++++++++++++++----------
31f061
 test/shell/dev-aliases.sh |  53 +++++++++++++++
31f061
 2 files changed, 179 insertions(+), 35 deletions(-)
31f061
 create mode 100644 test/shell/dev-aliases.sh
31f061
31f061
diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
31f061
index d5f18ff..8082efa 100644
31f061
--- a/lib/device/dev-cache.c
31f061
+++ b/lib/device/dev-cache.c
31f061
@@ -1428,60 +1428,151 @@ struct device *dev_hash_get(const char *name)
31f061
 	return (struct device *) dm_hash_lookup(_cache.names, name);
31f061
 }
31f061
 
31f061
+static void _remove_alias(struct device *dev, const char *name)
31f061
+{
31f061
+	struct dm_str_list *strl;
31f061
+
31f061
+	dm_list_iterate_items(strl, &dev->aliases) {
31f061
+		if (!strcmp(strl->str, name)) {
31f061
+			dm_list_del(&strl->list);
31f061
+			return;
31f061
+		}
31f061
+	}
31f061
+}
31f061
+
31f061
+/*
31f061
+ * Check that paths for this dev still refer to the same dev_t.  This is known
31f061
+ * to drop invalid paths in the case where lvm deactivates an LV, which causes
31f061
+ * that LV path to go away, but that LV path is not removed from dev-cache (it
31f061
+ * probably should be).  Later a new path to a different LV is added to
31f061
+ * dev-cache, where the new LV has the same major:minor as the previously
31f061
+ * deactivated LV.  The new LV will find the existing struct dev, and that
31f061
+ * struct dev will have dev->aliases entries that refer to the name of the old
31f061
+ * deactivated LV.  Those old paths are all invalid and are dropped here.
31f061
+ */
31f061
+
31f061
+static void _verify_aliases(struct device *dev, const char *newname)
31f061
+{
31f061
+	struct dm_str_list *strl, *strl2;
31f061
+	struct stat st;
31f061
+
31f061
+	dm_list_iterate_items_safe(strl, strl2, &dev->aliases) {
31f061
+		/* newname was just stat'd and added by caller */
31f061
+		if (newname && !strcmp(strl->str, newname))
31f061
+			continue;
31f061
+
31f061
+		if (stat(strl->str, &st) || (st.st_rdev != dev->dev)) {
31f061
+			log_debug("Drop invalid path %s for %d:%d (new path %s).",
31f061
+				  strl->str, (int)MAJOR(dev->dev), (int)MINOR(dev->dev), newname ?: "");
31f061
+			dm_hash_remove(_cache.names, strl->str);
31f061
+			dm_list_del(&strl->list);
31f061
+		}
31f061
+	}
31f061
+}
31f061
+
31f061
 struct device *dev_cache_get(struct cmd_context *cmd, const char *name, struct dev_filter *f)
31f061
 {
31f061
-	struct stat buf;
31f061
-	struct device *d = (struct device *) dm_hash_lookup(_cache.names, name);
31f061
-	int info_available = 0;
31f061
-	int ret = 1;
31f061
+	struct device *dev = (struct device *) dm_hash_lookup(_cache.names, name);
31f061
+	struct stat st;
31f061
+	int ret;
31f061
 
31f061
-	if (d && (d->flags & DEV_REGULAR))
31f061
-		return d;
31f061
+	/*
31f061
+	 * DEV_REGULAR means that is "dev" is actually a file, not a device.
31f061
+	 * FIXME: I don't think dev-cache is used for files any more and this
31f061
+	 * can be dropped?
31f061
+	 */
31f061
+	if (dev && (dev->flags & DEV_REGULAR))
31f061
+		return dev;
31f061
+
31f061
+	/*
31f061
+	 * The requested path is invalid, remove any dev-cache
31f061
+	 * info for it.
31f061
+	 */
31f061
+	if (stat(name, &st)) {
31f061
+		if (dev) {
31f061
+			log_print("Device path %s is invalid for %d:%d %s.",
31f061
+				  name, (int)MAJOR(dev->dev), (int)MINOR(dev->dev), dev_name(dev));
31f061
 
31f061
-	/* If the entry's wrong, remove it */
31f061
-	if (stat(name, &buf) < 0) {
31f061
-		if (d)
31f061
 			dm_hash_remove(_cache.names, name);
31f061
-		log_sys_very_verbose("stat", name);
31f061
-		d = NULL;
31f061
-	} else
31f061
-		info_available = 1;
31f061
 
31f061
-	if (d && (buf.st_rdev != d->dev)) {
31f061
-		dm_hash_remove(_cache.names, name);
31f061
-		d = NULL;
31f061
-	}
31f061
+			_remove_alias(dev, name);
31f061
 
31f061
-	if (!d) {
31f061
-		_insert(name, info_available ? &buf : NULL, 0, obtain_device_list_from_udev());
31f061
-		d = (struct device *) dm_hash_lookup(_cache.names, name);
31f061
-		if (!d) {
31f061
-			log_debug_devs("Device name not found in dev_cache repeat dev_cache_scan for %s", name);
31f061
-			dev_cache_scan();
31f061
-			d = (struct device *) dm_hash_lookup(_cache.names, name);
31f061
+			/* Remove any other names in dev->aliases that are incorrect. */
31f061
+			_verify_aliases(dev, NULL);
31f061
 		}
31f061
+		return NULL;
31f061
 	}
31f061
 
31f061
-	if (!d)
31f061
+	if (!S_ISBLK(st.st_mode)) {
31f061
+		log_debug("Not a block device %s.", name);
31f061
 		return NULL;
31f061
+	}
31f061
 
31f061
-	if (d && (d->flags & DEV_REGULAR))
31f061
-		return d;
31f061
+	/*
31f061
+	 * dev-cache has incorrect info for the requested path.
31f061
+	 * Remove incorrect info and then add new dev-cache entry.
31f061
+	 */
31f061
+	if (dev && (st.st_rdev != dev->dev)) {
31f061
+		log_print("Device path %s does not match %d:%d %s.",
31f061
+			  name, (int)MAJOR(dev->dev), (int)MINOR(dev->dev), dev_name(dev));
31f061
+
31f061
+		dm_hash_remove(_cache.names, name);
31f061
+
31f061
+		_remove_alias(dev, name);
31f061
+
31f061
+		/* Remove any other names in dev->aliases that are incorrect. */
31f061
+		_verify_aliases(dev, NULL);
31f061
+
31f061
+		/* Add new dev-cache entry next. */
31f061
+		dev = NULL;
31f061
+	}
31f061
+
31f061
+	/*
31f061
+	 * Either add a new struct dev for st_rdev and name,
31f061
+	 * or add name as a new alias for an existing struct dev
31f061
+	 * for st_rdev.
31f061
+	 */
31f061
+	if (!dev) {
31f061
+		_insert_dev(name, st.st_rdev);
31f061
 
31f061
-	if (f && !(d->flags & DEV_REGULAR)) {
31f061
-		ret = f->passes_filter(cmd, f, d, NULL);
31f061
+		/* Get the struct dev that was just added. */
31f061
+		dev = (struct device *) dm_hash_lookup(_cache.names, name);
31f061
 
31f061
-		if (ret == -EAGAIN) {
31f061
-			log_debug_devs("get device by name defer filter %s", dev_name(d));
31f061
-			d->flags |= DEV_FILTER_AFTER_SCAN;
31f061
-			ret = 1;
31f061
+		if (!dev) {
31f061
+			log_error("Failed to get device %s", name);
31f061
+			return NULL;
31f061
 		}
31f061
+
31f061
+		_verify_aliases(dev, name);
31f061
 	}
31f061
 
31f061
-	if (f && !(d->flags & DEV_REGULAR) && !ret)
31f061
+	/*
31f061
+	 * The caller passed a filter if they only want the dev if it
31f061
+	 * passes filters.
31f061
+	 */
31f061
+
31f061
+	if (!f)
31f061
+		return dev;
31f061
+
31f061
+	ret = f->passes_filter(cmd, f, dev, NULL);
31f061
+
31f061
+	/*
31f061
+	 * This might happen if this function is called before
31f061
+	 * filters can do i/o.  I don't think this will happen
31f061
+	 * any longer and this EAGAIN case can be removed.
31f061
+	 */
31f061
+	if (ret == -EAGAIN) {
31f061
+		log_debug_devs("dev_cache_get filter deferred %s", dev_name(dev));
31f061
+		dev->flags |= DEV_FILTER_AFTER_SCAN;
31f061
+		ret = 1;
31f061
+	}
31f061
+
31f061
+	if (!ret) {
31f061
+		log_debug_devs("dev_cache_get filter excludes %s", dev_name(dev));
31f061
 		return NULL;
31f061
+	}
31f061
 
31f061
-	return d;
31f061
+	return dev;
31f061
 }
31f061
 
31f061
 static struct device *_dev_cache_seek_devt(dev_t dev)
31f061
diff --git a/test/shell/dev-aliases.sh b/test/shell/dev-aliases.sh
31f061
new file mode 100644
31f061
index 0000000..c97cd5d
31f061
--- /dev/null
31f061
+++ b/test/shell/dev-aliases.sh
31f061
@@ -0,0 +1,53 @@
31f061
+#!/usr/bin/env bash
31f061
+
31f061
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
31f061
+#
31f061
+# This copyrighted material is made available to anyone wishing to use,
31f061
+# modify, copy, or redistribute it subject to the terms and conditions
31f061
+# of the GNU General Public License v.2.
31f061
+#
31f061
+# You should have received a copy of the GNU General Public License
31f061
+# along with this program; if not, write to the Free Software Foundation,
31f061
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31f061
+
31f061
+SKIP_WITH_LVMPOLLD=1
31f061
+
31f061
+. lib/inittest
31f061
+
31f061
+aux prepare_devs 3
31f061
+
31f061
+vgcreate $vg $dev1 $dev2 $dev3
31f061
+
31f061
+#
31f061
+# This lvconvert command will deactivate LV1, then internally create a new
31f061
+# lv, lvol0, as a poolmetadataspare, then activate lvol0 to zero it.
31f061
+# lvol0 will get the same major:minor that LV1 had.  When the code gets
31f061
+# the struct dev for lvol0, the new path to lvol0 is added to the
31f061
+# dev-cache with it's major:minor.  That major:minor already exists in
31f061
+# dev-cache and has the stale LV1 as an alias.  So the path to lvol0 is
31f061
+# added as an alias to the existing struct dev (with the correct
31f061
+# major:minor), but that struct dev has the stale LV1 path on its aliases
31f061
+# list.  The code will now validate all the aliases before returning the
31f061
+# dev for lvol0, and will find that the LV1 path is stale and remove it
31f061
+# from the aliases.  That will prevent the stale path from being used for
31f061
+# the dev in place of the new path.
31f061
+#
31f061
+# The preferred_name is set to /dev/mapper so that if the stale path still
31f061
+# exists, that stale path would be used as the name for the dev, and the
31f061
+# wiping code would fail to open that stale name.
31f061
+#
31f061
+
31f061
+lvcreate -n $lv1 -L32M $vg $dev1
31f061
+lvcreate -n $lv2 -L16M $vg $dev2
31f061
+lvconvert -y --type cache-pool --poolmetadata $lv2 --cachemode writeback $vg/$lv1 --config='devices { preferred_names=["/dev/mapper/"] }' 
31f061
+lvremove -y $vg/$lv1
31f061
+
31f061
+lvcreate -n $lv1 -L32M $vg $dev1
31f061
+lvcreate -n $lv2 -L16M $vg $dev2
31f061
+lvconvert -y --type cache-pool --poolmetadata $lv2 $vg/$lv1
31f061
+lvremove -y $vg/$lv1
31f061
+
31f061
+# TODO: add more validation of dev aliases being specified as command
31f061
+# args in combination with various preferred_names settings.
31f061
+
31f061
+vgremove -ff  $vg