]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: make clearer how LoopDevice objects that do not encapsulate an actual... 24530/head
authorLennart Poettering <lennart@poettering.net>
Thu, 1 Sep 2022 19:34:58 +0000 (21:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 1 Sep 2022 20:06:19 +0000 (22:06 +0200)
src/shared/loop-util.c
src/shared/loop-util.h

index 62e8969bed90933382ab7189fbfb733286506015..32941c7fa1f0048cdcfd660088d2ca9a8f077ca3 100644 (file)
@@ -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
index f5c37383eac1e15558c40e1294a29f4ab67c99df..bdbdac60eb0169c5e95d5310ae7848ac8a2940fb 100644 (file)
@@ -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);