diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-12-06 11:35:57 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-12-15 21:06:42 +0100 |
commit | f2d9213fee0fe0197845734e7c0554b1f8afc6d1 (patch) | |
tree | 74ec3f8bace9b391b75c6eb1ea9025f0af7c2eb1 /src/shared/loop-util.c | |
parent | shared/loop-util: fix error handling in loop_device_make_full() (diff) | |
download | systemd-f2d9213fee0fe0197845734e7c0554b1f8afc6d1.tar.xz systemd-f2d9213fee0fe0197845734e7c0554b1f8afc6d1.zip |
shared/loop-util: spin on LOOP_CTL_REMOVE
If we call LOOP_CLR_FD and LOOP_CTL_REMOVE too rapidly, the kernel cannot deal
with that (5.3.13-300.fc31.x86_64 running on dual core
Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz).
$ sudo strace -eioctl build/test-dissect-image /tmp/foobar3.img
ioctl(3, TCGETS, 0x7ffcee47de20) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(4, LOOP_CTL_GET_FREE) = 9
ioctl(5, LOOP_SET_FD, 3) = 0
ioctl(5, LOOP_SET_STATUS64, {lo_offset=0, lo_number=0, lo_flags=LO_FLAGS_READ_ONLY|LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN, lo_file_name="", ...}) = 0
ioctl(5, BLKGETSIZE64, [299999744]) = 0
ioctl(5, CDROM_GET_CAPABILITY, 0) = -1 EINVAL (Invalid argument)
ioctl(5, BLKSSZGET, [512]) = 0
Waiting for device (parent + 0 partitions) to appear...
Found root partition, writable of type btrfs at #-1 (/dev/block/7:9)
ioctl(5, LOOP_CLR_FD) = 0
ioctl(3, LOOP_CTL_REMOVE, 9) = -1 EBUSY (Device or resource busy)
Failed to remove loop device: Device or resource busy
This seems to be clear race condition, and attaching strace is generally enough
to "win" the race. But even with strace attached, we will fail occasionally.
Let's wait a bit and retry. With the wait, on my machine, the second attempt
always succeeds:
...
Found root partition, writable of type btrfs at #-1 (/dev/block/7:9)
ioctl(5, LOOP_CLR_FD) = 0
ioctl(3, LOOP_CTL_REMOVE, 9) = -1 EBUSY (Device or resource busy)
ioctl(3, LOOP_CTL_REMOVE, 9) = 9
+++ exited with 0 +++
Without the wait, all 64 attempts will occasionally fail.
Diffstat (limited to '')
-rw-r--r-- | src/shared/loop-util.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index c2d28a348c..8b4d13e616 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -11,6 +11,7 @@ #include <linux/loop.h> #include <sys/file.h> #include <sys/ioctl.h> +#include <unistd.h> #include "alloc-util.h" #include "fd-util.h" @@ -19,6 +20,7 @@ #include "parse-util.h" #include "stat-util.h" #include "stdio-util.h" +#include "string-util.h" static void cleanup_clear_loop_close(int *fd) { if (*fd >= 0) { @@ -166,7 +168,6 @@ LoopDevice* loop_device_unref(LoopDevice *d) { return NULL; if (d->fd >= 0) { - if (d->nr >= 0 && !d->relinquished) { if (ioctl(d->fd, LOOP_CLR_FD) < 0) log_debug_errno(errno, "Failed to clear loop device: %m"); @@ -181,11 +182,19 @@ LoopDevice* loop_device_unref(LoopDevice *d) { control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (control < 0) - log_debug_errno(errno, "Failed to open loop control device: %m"); - else { - if (ioctl(control, LOOP_CTL_REMOVE, d->nr) < 0) - log_debug_errno(errno, "Failed to remove loop device: %m"); - } + log_warning_errno(errno, + "Failed to open loop control device, cannot remove loop device %s: %m", + strna(d->node)); + else + for (unsigned n_attempts = 0;;) { + if (ioctl(control, LOOP_CTL_REMOVE, d->nr) >= 0) + break; + if (errno != EBUSY || ++n_attempts >= 64) { + log_warning_errno(errno, "Failed to remove device %s: %m", strna(d->node)); + break; + } + usleep(50 * USEC_PER_MSEC); + } } free(d->node); |