]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: reopen device node if we shortcut loop device creation
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Nov 2021 14:01:33 +0000 (15:01 +0100)
committerTopi Miettinen <topimiettinen@users.noreply.github.com>
Fri, 5 Nov 2021 07:08:16 +0000 (07:08 +0000)
The LoopDevice object supports a shortcut: if the backing fd we are
supposed to create a loopback device of refers to a
block device alrady then we'll use it as is – if we can – instead of
setting up an unnecessary loopback device that would be pretty much
the same as its backing device.

Previously, when doing this we'd just dup() the original backing fd and
use that. But that's problematic in case O_DIRECT was set on the fd,
since we'll keep that flag set on our copy too, which means we can't do
simple, regular IO on it anymore.

Thus, let's reopen the inode in this case with the exact access flags
we'd apply if we'd actually allocate and open a new loopback device.

Fixes: #21176
src/shared/loop-util.c

index 2da101b748beb1f5f7940fb1211f160eac6a6912..49afa4dac6c9d0a52ce8a9eb68adc01bb89d3b1f 100644 (file)
@@ -442,11 +442,16 @@ static int loop_device_make_internal(
                         _cleanup_close_ int copy = -1;
                         uint64_t diskseq = 0;
 
-                        /* If this is already a block device, store a copy of the fd as it is */
-
-                        copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+                        /* If this is already a block device and we are supposed to cover the whole of it
+                         * then store an fd to the original open device node — and do not actually create an
+                         * unnecessary loopback device for it. Note that we reopen the inode here, instead of
+                         * keeping just a dup() clone of it around, since we want to ensure that the O_DIRECT
+                         * flag of the handle we keep is off, we have our own file index, and have the right
+                         * read/write mode in effect. */
+
+                        copy = fd_reopen(fd, open_flags|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
                         if (copy < 0)
-                                return -errno;
+                                return copy;
 
                         r = loop_get_diskseq(copy, &diskseq);
                         if (r < 0 && r != -EOPNOTSUPP)