mirror of
https://github.com/systemd/systemd.git
synced 2026-08-03 06:30:30 +00:00
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
This commit is contained in:
committed by
Topi Miettinen
parent
7544461cd0
commit
d7654742ee
@@ -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 */
|
||||
/* 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 = fcntl(fd, F_DUPFD_CLOEXEC, 3);
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user