Blame SOURCES/0009-device_id-match-different-dm-device-names.patch

429878
From 459d931a9bfe4c9adcbbf2e76fdf35fda5b13c61 Mon Sep 17 00:00:00 2001
429878
From: David Teigland <teigland@redhat.com>
429878
Date: Fri, 12 Nov 2021 16:42:51 -0600
ab86b0
Subject: [PATCH 09/54] device_id: match different dm device names
429878
429878
If a devices file entry for a dm device is using the devname
429878
for the device id, then recognize different dm names as matching.
429878
---
429878
 lib/device/device_id.c | 81 +++++++++++++++++++++++++++++++++++-------
429878
 1 file changed, 69 insertions(+), 12 deletions(-)
429878
429878
diff --git a/lib/device/device_id.c b/lib/device/device_id.c
429878
index eb06109ff..dea739fc4 100644
429878
--- a/lib/device/device_id.c
429878
+++ b/lib/device/device_id.c
429878
@@ -1360,6 +1360,10 @@ void device_id_update_vg_uuid(struct cmd_context *cmd, struct volume_group *vg,
429878
 
429878
 static int _idtype_compatible_with_major_number(struct cmd_context *cmd, int idtype, int major)
429878
 {
429878
+	/* devname can be used with any kind of device */
429878
+	if (idtype == DEV_ID_TYPE_DEVNAME)
429878
+		return 1;
429878
+
429878
 	if (idtype == DEV_ID_TYPE_MPATH_UUID ||
429878
 	    idtype == DEV_ID_TYPE_CRYPT_UUID ||
429878
 	    idtype == DEV_ID_TYPE_LVMLV_UUID)
429878
@@ -1388,6 +1392,43 @@ static int _idtype_compatible_with_major_number(struct cmd_context *cmd, int idt
429878
 	return 1;
429878
 }
429878
 
429878
+static int _match_dm_devnames(struct cmd_context *cmd, struct device *dev,
429878
+			      struct dev_id *id, struct dev_use *du)
429878
+{
429878
+	struct stat buf;
429878
+
429878
+	if (MAJOR(dev->dev) != cmd->dev_types->device_mapper_major)
429878
+		return 0;
429878
+
429878
+	if (id->idname && du->idname && !strcmp(id->idname, du->idname))
429878
+		return 1;
429878
+
429878
+	if (du->idname && !strcmp(du->idname, dev_name(dev))) {
429878
+		log_debug("Match device_id %s %s to %s: ignoring idname %s",
429878
+			  idtype_to_str(du->idtype), du->idname, dev_name(dev), id->idname ?: ".");
429878
+		return 1;
429878
+	}
429878
+
429878
+	if (!du->idname)
429878
+		return 0;
429878
+
429878
+	/* detect that a du entry is for a dm device */
429878
+
429878
+	if (!strncmp(du->idname, "/dev/dm-", 8) || !strncmp(du->idname, "/dev/mapper/", 12)) {
429878
+		if (stat(du->idname, &buf))
429878
+			return 0;
429878
+
429878
+		if ((MAJOR(buf.st_rdev) == cmd->dev_types->device_mapper_major) &&
429878
+		    (MINOR(buf.st_rdev) == MINOR(dev->dev))) {
429878
+			log_debug("Match device_id %s %s to %s: using other dm name, ignoring %s",
429878
+				  idtype_to_str(du->idtype), du->idname, dev_name(dev), id->idname ?: ".");
429878
+			return 1;
429878
+		}
429878
+	}
429878
+
429878
+	return 0;
429878
+}
429878
+
429878
 /*
429878
  * check for dev->ids entry with du->idtype, if found compare it,
429878
  * if not, system_read of this type and add entry to dev->ids, compare it.
429878
@@ -1408,35 +1449,52 @@ static int _match_du_to_dev(struct cmd_context *cmd, struct dev_use *du, struct
429878
 	 * so we can skip trying to match certain du entries based simply on
429878
 	 * the major number of dev.
429878
 	 */
429878
-	if (!_idtype_compatible_with_major_number(cmd, du->idtype, (int)MAJOR(dev->dev)))
429878
+	if (!_idtype_compatible_with_major_number(cmd, du->idtype, (int)MAJOR(dev->dev))) {
429878
+		/*
429878
+		log_debug("Mismatch device_id %s %s to %s: wrong major",
429878
+			  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev));
429878
+		*/
429878
 		return 0;
429878
+	}
429878
 
429878
 	if (!dev_get_partition_number(dev, &part)) {
429878
-		log_debug("compare %s failed to get dev partition", dev_name(dev));
429878
+		/*
429878
+		log_debug("Mismatch device_id %s %s to %s: no partition",
429878
+			  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev));
429878
+		*/
429878
 		return 0;
429878
 	}
429878
 	if (part != du->part) {
429878
 		/*
429878
-		log_debug("compare mis %s %s part %d to %s part %d",
429878
-			  idtype_to_str(du->idtype), du->idname ?: ".", du->part, dev_name(dev), part);
429878
+		log_debug("Mismatch device_id %s %s to %s: wrong partition %d vs %d",
429878
+			  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev), du->part, part);
429878
 		*/
429878
 		return 0;
429878
 	}
429878
 
429878
 	dm_list_iterate_items(id, &dev->ids) {
429878
 		if (id->idtype == du->idtype) {
429878
-			if (id->idname && !strcmp(id->idname, du->idname)) {
429878
+			if ((id->idtype == DEV_ID_TYPE_DEVNAME) && _match_dm_devnames(cmd, dev, id, du)) {
429878
+				/* dm devs can have differing names that we know still match */
429878
+				du->dev = dev;
429878
+				dev->id = id;
429878
+				dev->flags |= DEV_MATCHED_USE_ID;
429878
+				log_debug("Match device_id %s %s to %s: dm names",
429878
+					  idtype_to_str(du->idtype), du->idname, dev_name(dev));
429878
+				return 1;
429878
+
429878
+			} else if (id->idname && !strcmp(id->idname, du->idname)) {
429878
 				du->dev = dev;
429878
 				dev->id = id;
429878
 				dev->flags |= DEV_MATCHED_USE_ID;
429878
 				log_debug("Match device_id %s %s to %s",
429878
 					  idtype_to_str(du->idtype), du->idname, dev_name(dev));
429878
 				return 1;
429878
+
429878
 			} else {
429878
 				/*
429878
-				log_debug("compare mis %s %s to %s %s",
429878
-			  		  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev),
429878
-					  ((id->idtype != DEV_ID_TYPE_DEVNAME) && id->idname) ? id->idname : "");
429878
+				log_debug("Mismatch device_id %s %s to %s: idname %s",
429878
+			  		  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev), id->idname ?: ":");
429878
 				*/
429878
 				return 0;
429878
 			}
429878
@@ -1456,7 +1514,7 @@ static int _match_du_to_dev(struct cmd_context *cmd, struct dev_use *du, struct
429878
 		id->dev = dev;
429878
 		dm_list_add(&dev->ids, &id->list);
429878
 		/*
429878
-		log_debug("compare mis %s %s to %s no idtype",
429878
+		log_debug("Mismatch device_id %s %s to %s: no idtype",
429878
 			  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev));
429878
 		*/
429878
 		return 0;
429878
@@ -1481,9 +1539,8 @@ static int _match_du_to_dev(struct cmd_context *cmd, struct dev_use *du, struct
429878
 	}
429878
 
429878
 	/*
429878
-	log_debug("compare mis %s %s to %s %s",
429878
-		  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev),
429878
-		  ((id->idtype != DEV_ID_TYPE_DEVNAME) && id->idname) ? id->idname : "");
429878
+	log_debug("Mismatch device_id %s %s to %s: idname %s",
429878
+		  idtype_to_str(du->idtype), du->idname ?: ".", dev_name(dev), idname);
429878
 	*/
429878
 	return 0;
429878
 }
429878
-- 
ab86b0
2.34.3
429878