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

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