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

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