Blame SOURCES/0062-vgimportdevices-fix-locking-when-creating-devices-fi.patch

f358f0
From 0b9d9963b8f15a6f12a0149a62809fa9b846c5c5 Mon Sep 17 00:00:00 2001
f358f0
From: David Teigland <teigland@redhat.com>
f358f0
Date: Tue, 30 Aug 2022 14:40:48 -0500
f358f0
Subject: [PATCH 2/4] vgimportdevices: fix locking when creating devices file
f358f0
f358f0
Take the devices file lock before creating a new devices file.
f358f0
(Was missed by the change to preemptively create the devices
f358f0
file prior to setup_devices(), which was done to improve the
f358f0
error path.)
f358f0
---
f358f0
 lib/device/dev-cache.c  |  7 +++----
f358f0
 lib/device/device_id.c  |  1 +
f358f0
 tools/vgimportdevices.c | 10 ++++++++--
f358f0
 3 files changed, 12 insertions(+), 6 deletions(-)
f358f0
f358f0
diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
f358f0
index 65e1cb138..0c9aaf785 100644
f358f0
--- a/lib/device/dev-cache.c
f358f0
+++ b/lib/device/dev-cache.c
f358f0
@@ -1956,10 +1956,9 @@ static int _setup_devices(struct cmd_context *cmd, int no_file_match)
f358f0
 
f358f0
 	if (!file_exists) {
f358f0
 		/*
f358f0
-		 * pvcreate/vgcreate/vgimportdevices/lvmdevices-add create
f358f0
-		 * a new devices file here if it doesn't exist.
f358f0
-		 * They have the create_edit_devices_file flag set.
f358f0
-		 * First they create/lock-ex the devices file lockfile.
f358f0
+		 * pvcreate/vgcreate create a new devices file here if it
f358f0
+		 * doesn't exist.  They have create_edit_devices_file=1.
f358f0
+		 * First create/lock-ex the devices file lockfile.
f358f0
 		 * Other commands will not use a devices file if none exists.
f358f0
 		 */
f358f0
 		lock_mode = LOCK_EX;
f358f0
diff --git a/lib/device/device_id.c b/lib/device/device_id.c
f358f0
index c3816a66c..780e08404 100644
f358f0
--- a/lib/device/device_id.c
f358f0
+++ b/lib/device/device_id.c
f358f0
@@ -2429,6 +2429,7 @@ static int _lock_devices_file(struct cmd_context *cmd, int mode, int nonblock, i
f358f0
 
f358f0
 	if (_devices_file_locked == mode) {
f358f0
 		/* can happen when a command holds an ex lock and does an update in device_ids_validate */
f358f0
+		/* can happen when vgimportdevices calls this directly, followed later by setup_devices */
f358f0
 		if (held)
f358f0
 			*held = 1;
f358f0
 		return 1;
f358f0
diff --git a/tools/vgimportdevices.c b/tools/vgimportdevices.c
f358f0
index 9ade1b9e4..23c2718ff 100644
f358f0
--- a/tools/vgimportdevices.c
f358f0
+++ b/tools/vgimportdevices.c
f358f0
@@ -132,8 +132,10 @@ int vgimportdevices(struct cmd_context *cmd, int argc, char **argv)
f358f0
 		return ECMD_FAILED;
f358f0
 
f358f0
 	/*
f358f0
-	 * Prepare devices file preemptively because the error path for this
f358f0
-	 * case from process_each is not as clean.
f358f0
+	 * Prepare/create devices file preemptively because the error path for
f358f0
+	 * this case from process_each/setup_devices is not as clean.
f358f0
+	 * This means that when setup_devices is called, it the devices
f358f0
+	 * file steps will be redundant, and need to handle being repeated.
f358f0
 	 */
f358f0
 	if (!setup_devices_file(cmd)) {
f358f0
 		log_error("Failed to set up devices file.");
f358f0
@@ -143,6 +145,10 @@ int vgimportdevices(struct cmd_context *cmd, int argc, char **argv)
f358f0
 		log_error("Devices file not enabled.");
f358f0
 		return ECMD_FAILED;
f358f0
 	}
f358f0
+	if (!lock_devices_file(cmd, LOCK_EX)) {
f358f0
+		log_error("Failed to lock the devices file.");
f358f0
+		return ECMD_FAILED;
f358f0
+	}
f358f0
 	if (!devices_file_exists(cmd)) {
f358f0
 	       	if (!devices_file_touch(cmd)) {
f358f0
 			log_error("Failed to create devices file.");
f358f0
-- 
f358f0
2.38.1
f358f0