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

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