summaryrefslogtreecommitdiff
path: root/src/partition
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/partition
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/partition')
-rw-r--r--src/partition/repart.c12
1 files changed, 2 insertions, 10 deletions
diff --git a/src/partition/repart.c b/src/partition/repart.c
index 815e81052b..bb95c0aab5 100644
--- a/src/partition/repart.c
+++ b/src/partition/repart.c
@@ -2789,14 +2789,10 @@ static int context_copy_blocks(Context *context) {
assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
if (p->encrypt != ENCRYPT_OFF) {
- r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, &d);
+ r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
if (r < 0)
return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
- r = loop_device_flock(d, LOCK_EX);
- if (r < 0)
- return log_error_errno(r, "Failed to lock loopback device: %m");
-
r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
if (r < 0)
return log_error_errno(r, "Failed to encrypt device: %m");
@@ -3038,14 +3034,10 @@ static int context_mkfs(Context *context) {
/* Loopback block devices are not only useful to turn regular files into block devices, but
* also to cut out sections of block devices into new block devices. */
- r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, &d);
+ r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
if (r < 0)
return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
- r = loop_device_flock(d, LOCK_EX);
- if (r < 0)
- return log_error_errno(r, "Failed to lock loopback device: %m");
-
if (p->encrypt != ENCRYPT_OFF) {
r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
if (r < 0)