From 7cb349f0cae22cc9b0b17d8a52ca0fcd0435160f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 1 Sep 2022 21:34:58 +0200 Subject: [PATCH] loop-util: make clearer how LoopDevice objects that do not encapsulate an actual loopback device are set up --- src/shared/loop-util.c | 4 ++-- src/shared/loop-util.h | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 62e8969bed9..32941c7fa1f 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -719,7 +719,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { /* Let's open the control device early, and lock it, so that we can release our block device and * delete it in a synchronized fashion, and allocators won't needlessly see the block device as free * while we are about to delete it. */ - if (d->nr >= 0 && !d->relinquished) { + if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) { 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, cannot remove loop device '%s', ignoring: %m", strna(d->node)); @@ -733,7 +733,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { if (fsync(d->fd) < 0) log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m"); - if (d->nr >= 0 && !d->relinquished) { + if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) { /* We are supposed to clear the loopback device. Let's do this synchronously: lock * the device, manually remove all partitions and then clear it. This should ensure * udev doesn't concurrently access the devices, and we can be reasonably sure that diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index f5c37383eac..bdbdac60eb0 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -11,7 +11,7 @@ typedef struct LoopDevice LoopDevice; struct LoopDevice { int fd; int lock_fd; - int nr; + int nr; /* The loopback device index (i.e. 4 for /dev/loop4); if this object encapsulates a non-loopback block device, set to -1 */ dev_t devno; char *node; bool relinquished; @@ -20,6 +20,9 @@ struct LoopDevice { usec_t timestamp_not_before; /* CLOCK_MONOTONIC timestamp taken immediately before attaching the loopback device, or USEC_INFINITY if we don't know */ }; +/* Returns true if LoopDevice object is not actually a loopback device but some other block device we just wrap */ +#define LOOP_DEVICE_IS_FOREIGN(d) ((d)->nr < 0) + int loop_device_make(int fd, int open_flags, uint64_t offset, uint64_t size, uint32_t loop_flags, int lock_op, LoopDevice **ret); int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, int lock_op, LoopDevice **ret); int loop_device_open(const char *loop_path, int open_flags, int lock_op, LoopDevice **ret); -- 2.47.3