Blame SOURCES/0030-lvmdevices-fix-checks-when-adding-entries.patch

ab86b0
From 4e72068216b006edc69c8bafba5198051e3ed1dd Mon Sep 17 00:00:00 2001
ab86b0
From: David Teigland <teigland@redhat.com>
ab86b0
Date: Tue, 25 Jan 2022 11:35:36 -0600
ab86b0
Subject: [PATCH 30/54] lvmdevices: fix checks when adding entries
ab86b0
ab86b0
Removes some incorrect and unnecessary checks for other entries
ab86b0
when adding a new devices.  The removed checks and corrections were
ab86b0
mostly redundant with what is already done by device id matching.
ab86b0
Other checking is reworked so the warnings are a bit different.
ab86b0
---
ab86b0
 lib/device/device_id.c | 153 +++++++++++++----------------------------
ab86b0
 1 file changed, 48 insertions(+), 105 deletions(-)
ab86b0
ab86b0
diff --git a/lib/device/device_id.c b/lib/device/device_id.c
ab86b0
index 625576ec6..ccc5f43a1 100644
ab86b0
--- a/lib/device/device_id.c
ab86b0
+++ b/lib/device/device_id.c
ab86b0
@@ -935,6 +935,10 @@ int device_id_add(struct cmd_context *cmd, struct device *dev, const char *pvid_
ab86b0
 	struct dev_use *du, *update_du = NULL, *du_dev, *du_pvid, *du_devname, *du_devid;
ab86b0
 	struct dev_id *id;
ab86b0
 	int found_id = 0;
ab86b0
+	int part = 0;
ab86b0
+
ab86b0
+	if (!dev_get_partition_number(dev, &part))
ab86b0
+		return_0;
ab86b0
 
ab86b0
 	/*
ab86b0
 	 * When enable_devices_file=0 and pending_devices_file=1 we let
ab86b0
@@ -953,10 +957,6 @@ int device_id_add(struct cmd_context *cmd, struct device *dev, const char *pvid_
ab86b0
 	 */
ab86b0
 	memcpy(&pvid, pvid_arg, ID_LEN);
ab86b0
 
ab86b0
-	du_dev = get_du_for_dev(cmd, dev);
ab86b0
-	du_pvid = get_du_for_pvid(cmd, pvid);
ab86b0
-	du_devname = _get_du_for_devname(cmd, dev_name(dev));
ab86b0
-
ab86b0
 	/*
ab86b0
 	 * Choose the device_id type for the device being added.
ab86b0
 	 *
ab86b0
@@ -1072,6 +1072,9 @@ id_done:
ab86b0
 	idtype = 0;
ab86b0
 
ab86b0
 	/*
ab86b0
+	 * "dev" is the device we are adding.
ab86b0
+	 * "id" is the device_id it's using, set in dev->id.
ab86b0
+	 *
ab86b0
 	 * Update the cmd->use_devices list for the new device.  The
ab86b0
 	 * use_devices list will be used to update the devices file.
ab86b0
 	 *
ab86b0
@@ -1083,23 +1086,57 @@ id_done:
ab86b0
 	 * those other entries to fix any incorrect info.
ab86b0
 	 */
ab86b0
 
ab86b0
+	/* Is there already an entry matched to this device? */
ab86b0
+	du_dev = get_du_for_dev(cmd, dev);
ab86b0
+
ab86b0
+	/* Is there already an entry matched to this device's pvid? */
ab86b0
+	du_pvid = get_du_for_pvid(cmd, pvid);
ab86b0
+
ab86b0
+	/* Is there already an entry using this device's name? */
ab86b0
+	du_devname = _get_du_for_devname(cmd, dev_name(dev));
ab86b0
+
ab86b0
+	/* Is there already an entry using the device_id for this device? */
ab86b0
 	du_devid = _get_du_for_device_id(cmd, id->idtype, id->idname);
ab86b0
 
ab86b0
 	if (du_dev)
ab86b0
-		log_debug("device_id_add %s pvid %s matches du_dev %p dev %s",
ab86b0
+		log_debug("device_id_add %s pvid %s matches entry %p dev %s",
ab86b0
 			  dev_name(dev), pvid, du_dev, dev_name(du_dev->dev));
ab86b0
 	if (du_pvid)
ab86b0
-		log_debug("device_id_add %s pvid %s matches du_pvid %p dev %s pvid %s",
ab86b0
+		log_debug("device_id_add %s pvid %s matches entry %p dev %s with same pvid %s",
ab86b0
 			  dev_name(dev), pvid, du_pvid, du_pvid->dev ? dev_name(du_pvid->dev) : ".",
ab86b0
 			  du_pvid->pvid);
ab86b0
 	if (du_devid)
ab86b0
-		log_debug("device_id_add %s pvid %s matches du_devid %p dev %s pvid %s",
ab86b0
+		log_debug("device_id_add %s pvid %s matches entry %p dev %s with same device_id %d %s",
ab86b0
 			  dev_name(dev), pvid, du_devid, du_devid->dev ? dev_name(du_devid->dev) : ".",
ab86b0
-			  du_devid->pvid);
ab86b0
+			  du_devid->idtype, du_devid->idname);
ab86b0
 	if (du_devname)
ab86b0
-		log_debug("device_id_add %s pvid %s matches du_devname %p dev %s pvid %s",
ab86b0
+		log_debug("device_id_add %s pvid %s matches entry %p dev %s with same devname %s",
ab86b0
 			  dev_name(dev), pvid, du_devname, du_devname->dev ? dev_name(du_devname->dev) : ".",
ab86b0
-			  du_devname->pvid);
ab86b0
+			  du_devname->devname);
ab86b0
+
ab86b0
+	if (du_pvid && (du_pvid->dev != dev))
ab86b0
+		log_warn("WARNING: adding device %s with PVID %s which is already used for %s.",
ab86b0
+			 dev_name(dev), pvid, du_pvid->dev ? dev_name(du_pvid->dev) : "missing device");
ab86b0
+
ab86b0
+	if (du_devid && (du_devid->dev != dev)) {
ab86b0
+		if (!du_devid->dev) {
ab86b0
+			log_warn("WARNING: adding device %s with idname %s which is already used for missing device.",
ab86b0
+				 dev_name(dev), id->idname);
ab86b0
+		} else {
ab86b0
+			int ret1, ret2;
ab86b0
+			dev_t devt1, devt2;
ab86b0
+			/* Check if both entries are partitions of the same device. */
ab86b0
+			ret1 = dev_get_primary_dev(cmd->dev_types, dev, &devt1);
ab86b0
+			ret2 = dev_get_primary_dev(cmd->dev_types, du_devid->dev, &devt2);
ab86b0
+			if ((ret1 == 2) && (ret2 == 2) && (devt1 == devt2)) {
ab86b0
+				log_debug("Using separate entries for partitions of same device %s part %d %s part %d.",
ab86b0
+					  dev_name(dev), part, dev_name(du_devid->dev), du_devid->part);
ab86b0
+			} else {
ab86b0
+				log_warn("WARNING: adding device %s with idname %s which is already used for %s.",
ab86b0
+					 dev_name(dev), id->idname, dev_name(du_devid->dev));
ab86b0
+			}
ab86b0
+		}
ab86b0
+	}
ab86b0
 
ab86b0
 	/*
ab86b0
 	 * If one of the existing entries (du_dev, du_pvid, du_devid, du_devname)
ab86b0
@@ -1112,29 +1149,6 @@ id_done:
ab86b0
 		dm_list_del(&update_du->list);
ab86b0
 		update_matching_kind = "device";
ab86b0
 		update_matching_name = dev_name(dev);
ab86b0
-
ab86b0
-		if (du_devid && (du_devid != du_dev)) {
ab86b0
-			log_warn("WARNING: device %s (%s) and %s (%s) have duplicate device ID.",
ab86b0
-				 dev_name(dev), id->idname,
ab86b0
-				 (du_pvid && du_pvid->dev) ? dev_name(du_pvid->dev) : "none",
ab86b0
-				 du_pvid ? du_pvid->idname : "");
ab86b0
-		}
ab86b0
-
ab86b0
-		if (du_pvid && (du_pvid != du_dev)) {
ab86b0
-			log_warn("WARNING: device %s (%s) and %s (%s) have duplicate PVID %s",
ab86b0
-				 dev_name(dev), id->idname,
ab86b0
-				 du_pvid->dev ? dev_name(du_pvid->dev) : "none", du_pvid->idname,
ab86b0
-				 pvid);
ab86b0
-		}
ab86b0
-
ab86b0
-		if (du_devname && (du_devname != du_dev)) {
ab86b0
-			/* clear devname in another entry with our devname */
ab86b0
-			log_warn("Devices file PVID %s clearing wrong DEVNAME %s.",
ab86b0
-				 du_devname->pvid, du_devname->devname);
ab86b0
-			free(du_devname->devname);
ab86b0
-			du_devname->devname = NULL;
ab86b0
-		}
ab86b0
-
ab86b0
 	} else if (du_pvid) {
ab86b0
 		/*
ab86b0
 		 * If the device_id of the existing entry for PVID is the same
ab86b0
@@ -1154,11 +1168,6 @@ id_done:
ab86b0
 			update_matching_kind = "PVID";
ab86b0
 			update_matching_name = pvid;
ab86b0
 		} else {
ab86b0
-			log_warn("WARNING: device %s (%s) and %s (%s) have duplicate PVID %s",
ab86b0
-				 dev_name(dev), id->idname,
ab86b0
-				 du_pvid->dev ? dev_name(du_pvid->dev) : "none", du_pvid->idname,
ab86b0
-				 pvid);
ab86b0
-
ab86b0
 			if (!cmd->current_settings.yes &&
ab86b0
 			    yes_no_prompt("Add device with duplicate PV to devices file?") == 'n') {
ab86b0
 				log_print("Device not added.");
ab86b0
@@ -1166,21 +1175,6 @@ id_done:
ab86b0
 				return 1;
ab86b0
 			}
ab86b0
 		}
ab86b0
-
ab86b0
-		if (du_devid && (du_devid != du_pvid)) {
ab86b0
-			/* warn about another entry using the same device_id */
ab86b0
-			log_warn("WARNING: duplicate device_id %s for PVIDs %s %s",
ab86b0
-				 du_devid->idname, du_devid->pvid, du_pvid->pvid);
ab86b0
-		}
ab86b0
-
ab86b0
-		if (du_devname && (du_devname != du_pvid)) {
ab86b0
-			/* clear devname in another entry with our devname */
ab86b0
-			log_warn("Devices file PVID %s clearing wrong DEVNAME %s.",
ab86b0
-				 du_devname->pvid, du_devname->devname);
ab86b0
-			free(du_devname->devname);
ab86b0
-			du_devname->devname = NULL;
ab86b0
-		}
ab86b0
-
ab86b0
 	} else if (du_devid) {
ab86b0
 		/*
ab86b0
 		 * Do we create a new du or update the existing du?
ab86b0
@@ -1195,64 +1189,13 @@ id_done:
ab86b0
 		 * the same device_id (create a new du for dev.)
ab86b0
 		 * If not, then update the existing du_devid.
ab86b0
 		 */
ab86b0
-		
ab86b0
-		if (du_devid->dev != dev)
ab86b0
-			check_idname = device_id_system_read(cmd, du_devid->dev, id->idtype);
ab86b0
-
ab86b0
-		if (check_idname && !strcmp(check_idname, id->idname)) {
ab86b0
-			int ret1, ret2;
ab86b0
-			dev_t devt1, devt2;
ab86b0
-
ab86b0
-			/*
ab86b0
-			 * two different devices have the same device_id,
ab86b0
-			 * create a new du for the device being added
ab86b0
-			 */
ab86b0
-
ab86b0
-			/* dev_is_partitioned() the dev open to read it. */
ab86b0
-			if (!label_scan_open(du_devid->dev))
ab86b0
-				log_warn("Cannot open %s", dev_name(du_devid->dev));
ab86b0
-
ab86b0
-			if (dev_is_partitioned(cmd, du_devid->dev)) {
ab86b0
-				/* Check if existing entry is whole device and new entry is a partition of it. */
ab86b0
-				ret1 = dev_get_primary_dev(cmd->dev_types, dev, &devt1);
ab86b0
-				if ((ret1 == 2) && (devt1 == du_devid->dev->dev))
ab86b0
-					log_warn("Remove partitioned device %s from devices file.", dev_name(du_devid->dev));
ab86b0
-			} else {
ab86b0
-				/* Check if both entries are partitions of the same device. */
ab86b0
-				ret1 = dev_get_primary_dev(cmd->dev_types, dev, &devt1);
ab86b0
-				ret2 = dev_get_primary_dev(cmd->dev_types, du_devid->dev, &devt2);
ab86b0
-
ab86b0
-				if ((ret1 == 2) && (ret2 == 2) && (devt1 == devt2)) {
ab86b0
-					log_warn("Partitions %s %s have same device_id %s",
ab86b0
-						 dev_name(dev), dev_name(du_devid->dev), id->idname);
ab86b0
-				} else {
ab86b0
-					log_warn("Duplicate device_id %s %s for %s and %s",
ab86b0
-						 idtype_to_str(id->idtype), check_idname,
ab86b0
-						 dev_name(dev), dev_name(du_devid->dev));
ab86b0
-				}
ab86b0
-			}
ab86b0
-		} else {
ab86b0
+		if (du_devid->dev == dev) {
ab86b0
 			/* update the existing entry with matching devid */
ab86b0
 			update_du = du_devid;
ab86b0
 			dm_list_del(&update_du->list);
ab86b0
 			update_matching_kind = "device_id";
ab86b0
 			update_matching_name = id->idname;
ab86b0
 		}
ab86b0
-
ab86b0
-		if (du_devname && (du_devname != du_devid)) {
ab86b0
-			/* clear devname in another entry with our devname */
ab86b0
-			log_warn("Devices file PVID %s clearing wrong DEVNAME %s",
ab86b0
-				 du_devname->pvid, du_devname->devname);
ab86b0
-			free(du_devname->devname);
ab86b0
-			du_devname->devname = NULL;
ab86b0
-		}
ab86b0
-
ab86b0
-	} else if (du_devname) {
ab86b0
-		/* clear devname in another entry with our devname */
ab86b0
-		log_warn("Devices file PVID %s clearing wrong DEVNAME %s",
ab86b0
-			 du_devname->pvid, du_devname->devname);
ab86b0
-		free(du_devname->devname);
ab86b0
-		du_devname->devname = NULL;
ab86b0
 	}
ab86b0
 
ab86b0
 	free((void *)check_idname);
ab86b0
-- 
ab86b0
2.34.3
ab86b0