summaryrefslogtreecommitdiff
path: root/src/dissect
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-09-01 15:00:30 +0200
committerLennart Poettering <lennart@poettering.net>2022-09-01 22:05:32 +0200
commit7f52206a2bc128f9ae8306db43aa6e2f7d916f82 (patch)
tree71abcc3bae9c57aa3ceb041e2a3cfea0c193c95b /src/dissect
parent234c2e16e5686d1a49fe84e534d4edb2db8d3719 (diff)
downloadsystemd-7f52206a2bc128f9ae8306db43aa6e2f7d916f82.tar.gz
loop-util: rework how we lock loopback block devices
Let's rework how we lock loopback block devices in two ways: 1. Lock a separate fd, instead of the main block device fd. We already did that for our internal locking when allocating loopback block devices, but do so for the exposed locking (i.e. loop_device_flock()), too, so that the lock is independent of the main fd we actually use of IO. 2. Instead of locking the device during allocation of the loopback device, then unlocking it (which will make udev run), and then re-locking things if we need, let's instead just keep the lock the whole time, to make things a bit safer and faster, and not have to wait for udev at all. This is done by adding a "lock_op" parameter to loop device allocation functions that declares the initial state of the lock, and is one of LOCK_UN/LOCK_SH/LOCK_EX. This change also shortens a lot of code, since we allocate + immediately lock loopback devices pretty much everywhere.
Diffstat (limited to 'src/dissect')
-rw-r--r--src/dissect/dissect.c13
1 files changed, 2 insertions, 11 deletions
diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c
index 858ed6a8f9..f0094a390f 100644
--- a/src/dissect/dissect.c
+++ b/src/dissect/dissect.c
@@ -885,14 +885,10 @@ static int action_umount(const char *path) {
return log_error_errno(r, "Failed to get devname of block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
- r = loop_device_open(devname, 0, &d);
+ r = loop_device_open(devname, 0, LOCK_EX, &d);
if (r < 0)
return log_error_errno(r, "Failed to open loop device '%s': %m", devname);
- r = loop_device_flock(d, LOCK_EX);
- if (r < 0)
- return log_error_errno(r, "Failed to lock loop device '%s': %m", devname);
-
/* We've locked the loop device, now we're ready to unmount. To allow the unmount to succeed, we have
* to close the O_PATH fd we opened earlier. */
fd = safe_close(fd);
@@ -944,16 +940,11 @@ static int run(int argc, char *argv[]) {
arg_image,
FLAGS_SET(arg_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR,
FLAGS_SET(arg_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
+ LOCK_SH,
&d);
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device for %s: %m", arg_image);
- /* Make sure udevd doesn't issue BLKRRPART underneath us thus making devices disappear in the middle,
- * that we assume already are there. */
- r = loop_device_flock(d, LOCK_SH);
- if (r < 0)
- return log_error_errno(r, "Failed to lock loopback device: %m");
-
r = dissect_image_and_warn(
d->fd,
arg_image,