diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-09-01 15:00:30 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2022-09-01 22:05:32 +0200 |
commit | 7f52206a2bc128f9ae8306db43aa6e2f7d916f82 (patch) | |
tree | 71abcc3bae9c57aa3ceb041e2a3cfea0c193c95b /src/home | |
parent | 234c2e16e5686d1a49fe84e534d4edb2db8d3719 (diff) | |
download | systemd-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/home')
-rw-r--r-- | src/home/homework-luks.c | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 68a5ac9c2e..eea282fe12 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -1284,7 +1284,7 @@ int home_setup_luks( return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine backing device for DM %s.", setup->dm_name); if (!setup->loop) { - r = loop_device_open(n, O_RDWR, &setup->loop); + r = loop_device_open(n, O_RDWR, LOCK_UN, &setup->loop); if (r < 0) return log_error_errno(r, "Failed to open loopback device %s: %m", n); } @@ -1378,7 +1378,7 @@ int home_setup_luks( return r; } - r = loop_device_make(setup->image_fd, O_RDWR, offset, size, 0, &setup->loop); + r = loop_device_make(setup->image_fd, O_RDWR, offset, size, 0, LOCK_UN, &setup->loop); if (r == -ENOENT) { log_error_errno(r, "Loopback block device support is not available on this system."); return -ENOLINK; /* make recognizable */ @@ -2299,7 +2299,7 @@ int home_create_luks( log_info("Writing of partition table completed."); - r = loop_device_make(setup->image_fd, O_RDWR, partition_offset, partition_size, 0, &setup->loop); + r = loop_device_make(setup->image_fd, O_RDWR, partition_offset, partition_size, 0, LOCK_EX, &setup->loop); if (r < 0) { if (r == -ENOENT) { /* this means /dev/loop-control doesn't exist, i.e. we are in a container * or similar and loopback bock devices are not available, return a @@ -2311,10 +2311,6 @@ int home_create_luks( return log_error_errno(r, "Failed to set up loopback device for %s: %m", setup->temporary_image_path); } - r = loop_device_flock(setup->loop, LOCK_EX); /* make sure udev won't read before we are done */ - if (r < 0) - return log_error_errno(r, "Failed to take lock on loop device: %m"); - log_info("Setting up loopback device %s completed.", setup->loop->node ?: ip); r = luks_format(setup->loop->node, |