]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Only lock block device once
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 3 Aug 2022 09:37:17 +0000 (11:37 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 4 Aug 2022 00:03:47 +0000 (01:03 +0100)
Let's lock the backing fd instead of locking/unlocking multiple
times when doing multiple operations with repart. It doesn't make
much sense for anything else to touch the block device while there
are still repart operations pending on it. By keeping the lock over
the full duration of repart, we avoid anything else from interfering
with the block device inbetween operations.

src/partition/repart.c

index f377908a338f2d12dc2ced5798a2a5a8f0b1be81..5cbe59cdf72cd2f8797c1d79db3e8d95a3574bcd 100644 (file)
@@ -1615,11 +1615,11 @@ static int context_load_partition_table(
                 *backing_fd = fd_reopen(fdisk_get_devfd(c), O_RDONLY|O_CLOEXEC);
                 if (*backing_fd < 0)
                         return log_error_errno(*backing_fd, "Failed to duplicate fdisk fd: %m");
-        }
 
-        /* Tell udev not to interfere while we are processing the device */
-        if (flock(fdisk_get_devfd(c), arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
-                return log_error_errno(errno, "Failed to lock block device: %m");
+                /* Tell udev not to interfere while we are processing the device */
+                if (flock(*backing_fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
+                        return log_error_errno(errno, "Failed to lock block device: %m");
+        }
 
         /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
          * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use