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

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