From: Lennart Poettering Date: Thu, 4 Nov 2021 14:01:33 +0000 (+0100) Subject: loop-util: reopen device node if we shortcut loop device creation X-Git-Tag: v250-rc1~357 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d7654742eeb808c9a82fcf4ef3591d5cd582190f;p=thirdparty%2Fsystemd.git loop-util: reopen device node if we shortcut loop device creation 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 --- diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 2da101b748b..49afa4dac6c 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -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)