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

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