Commit Graph
104 Commits
Author SHA1 Message Date
Aidan Dang fd83c98e8a Implement --luks-sector-size for homed 2022-10-07 16:36:04 +02:00
Yu Watanabe 4f0ad43e24 loop-util: re-introduce loop_device_open() which takes sd_device object
Then, this makes loop_device_open_from_fd() or _from_path() be wrappers
of loop_device_open() with block_device_new_from_fd() or _from_path(),
respectively.
2022-09-28 17:42:37 +09:00
Yu Watanabe de3b7f168e loop-util: rename loop_device_open() -> loop_device_open_from_path()
No functional changes, just preparation for later commits.
2022-09-28 17:42:37 +09:00
Yu Watanabe 36d5eb0bff loop-util: introduce reference counter for LoopDevice 2022-09-18 07:56:32 +09:00
Yu Watanabe 833106b876 loop-util: do not try to enumerate partitions twice 2022-09-09 05:16:41 +09:00
Yu Watanabe 53274fdb59 loop-util: always check if backing file is not attached yet
An image file without partition table may be attached.
2022-09-09 05:16:01 +09:00
Yu Watanabe da4fd28871 loop-util: open loopback block device in loop_configure()
And make it return LoopDevice object on success.
No functional changes, just refactoring.
2022-09-09 04:31:10 +09:00
Yu Watanabe bb273a5145 loop-util: save the flag about if LOOP_CONFIGURE ioctl works or not in loop_configure()
When LOOP_CONFIGURE fail, then it is not necessary to call it on any
later call of loop_configure(). Let's save the flag in a static variable
in the function.
2022-09-09 04:31:10 +09:00
Yu Watanabe 54ba7daf23 loop-util: split out several functions fron loop_configure() and loop_device_make_internal()
The two functions were quite long, and hard to understand its logic.
No functional change, just refactoring.
2022-09-09 04:31:10 +09:00
Yu Watanabe 30633dbd32 Merge pull request #24571 from yuwata/dissect-loop-image-use-backing-file
dissect: save image path to LoopDevice and use it when dissect loop device
2022-09-07 23:45:29 +09:00
Yu Watanabe e77cab8248 loop-util: save backing file of loopback block device
It will be used in later commits.
2022-09-07 20:45:24 +09:00
Yu Watanabe aa0295f1d9 loop-util: move device_has_block_children() to blockdev-util.c
As the function is not only for loopback block device.

No actual code changes, just refactoring.
2022-09-07 20:45:24 +09:00
Yu Watanabe 46c3a28845 blockdev-util: make block_device_remove_all_partitions() take sd_device object
Then, it is not necessary to recreate sd_device object when we already
have.
2022-09-07 20:45:20 +09:00
Yu Watanabe 10719a6ff8 loop-util: fix leak of file descriptor on failure 2022-09-07 07:52:32 +09:00
Yu Watanabe cc5bae6cc2 loop-util: store sd_device object for the loop device
It will be used in later commits.
2022-09-06 11:26:00 +09:00
Yu Watanabe e42270b6a6 loop-util: lock_fd must be closed before calling LOOP_CLR_FD
Follow-up for 7f52206a2b.

C.f. 87862cc2b4.
2022-09-05 04:31:47 +09:00
Yu Watanabe 9bf8600774 loop-util: drop unnecessary initializations 2022-09-05 04:31:29 +09:00
Yu Watanabe 1996ad2854 loop-util: use loop_device_open_full() when whole block device is passed to loop_device_make()
This also fixes a leak of lock_fd, which introduced by
7f52206a2b, when fd is for a block device,
and size or offset is non-zero.

Fixes another issue in #24147.
2022-09-05 03:50:05 +09:00
Yu Watanabe a8d8a61980 loop-util: introduce loop_device_open_full() 2022-09-05 03:29:06 +09:00
Yu Watanabe 9b5626d67a loop-util: fix LoopDevice.devno assigned by loop_device_open() 2022-09-05 03:29:06 +09:00
Yu Watanabe ffcb33241e loop-util: also set LoopDevice.diskseq when created with loop_device_open() 2022-09-05 03:29:06 +09:00
Yu Watanabe d12c0f4cfe loop-util: fix memleak when fd is for a block device with non-zero offset or size 2022-09-03 09:47:53 +09:00
Yu Watanabe 062d511776 loop-util: always set LoopDevice.node 2022-09-03 00:42:31 +09:00
Yu Watanabe 672780cde1 loop-util: rename loopdev -> node
Preparation for the next commit. No functional change.
2022-09-03 00:42:31 +09:00
Lennart Poettering 7cb349f0ca loop-util: make clearer how LoopDevice objects that do not encapsulate an actual loopback device are set up 2022-09-01 22:06:19 +02:00
Lennart Poettering 4c1d50e65c loop-util: lock the control device around clearing the loopback device and deleting it
This mirrors what we already do during allocation. We lock the control
device first, and then release the block device and then delete it.

This makes things substantially more robust as long all participants do
such locking: we won't attempt to delete a block device somebody else
already is using.
2022-09-01 22:06:19 +02:00
Lennart Poettering 87862cc2b4 loop-util: close lock fd before trying LOOP_CLR_FD in failure path
If the loopback device is open more than once LOOP_CLR_FD will fail,
hence close the lock fd first explicitly, so there's definitely only one
fd left.
2022-09-01 22:06:19 +02:00
Lennart Poettering 247738b4f5 loop-util: drop code to attach empty file
Back when I wrote this code I wasn't aware of BLKPG and what it can do.
Hence I came up with this hack to attach an empty file to delete all
partitions. But today we can do better with BLKPG: let's just explicitly
remove all partitions, and then try again.
2022-09-01 22:06:19 +02:00
Lennart Poettering 7f52206a2b loop-util: rework how we lock loopback block devices
Let's rework how we lock loopback block devices in two ways:

1. Lock a separate fd, instead of the main block device fd. We already
   did that for our internal locking when allocating loopback block
   devices, but do so for the exposed locking (i.e.
   loop_device_flock()), too, so that the lock is independent of the
   main fd we actually use of IO.

2. Instead of locking the device during allocation of the loopback
   device, then unlocking it (which will make udev run), and then
   re-locking things if we need, let's instead just keep the lock the
   whole time, to make things a bit safer and faster, and not have to
   wait for udev at all. This is done by adding a "lock_op" parameter to
   loop device allocation functions that declares the initial state of
   the lock, and is one of LOCK_UN/LOCK_SH/LOCK_EX. This change also
   shortens a lot of code, since we allocate + immediately lock loopback
   devices pretty much everywhere.
2022-09-01 22:05:32 +02:00
Lennart Poettering 3a6ed1e19d loop-util: when clearing a loopback device delete partitions first, and take BSD lock
Whenever we release a loopback device, let's first synchronously delete
all partitions, so that we know that's complete and not done
asynchronously in the background. Take a BSD lock on the device while
doing so, so that udev won't make the devices busy while we do this.
2022-09-01 20:41:08 +02:00
Lennart Poettering ff27ef4b59 loop: convert impossibe EBADF cases into asserts 2022-09-01 20:40:01 +02:00
Lennart Poettering ed13feff1e loop-util: use DEVNUM_FORMAT_STR more 2022-09-01 16:00:45 +02:00
Lennart Poettering 91e1ce1a7c loop-util: move resize partition ioctl call to blockdev-util.[ch]
The other BLKPG calls have wrappers in blockdev-util.[ch], let's place
them all there.

No change in behaviour.
2022-09-01 15:59:54 +02:00
Lennart Poettering 9d72a3cf70 loop: make 'Failed to configure loopback device' log message clearer
We print the very same log message for loopback block devices and for
loopback network devices. Let's better be clear what kind it is.
2022-08-29 15:15:08 +02:00
Yu Watanabe ca8228295e tree-wide: use devpath_from_devnum() and device_open_from_devnum()
Fixes #24465.
2022-08-28 10:10:50 +09:00
Yu Watanabe 5c467ef4fb loop-util: use filter provided by sd_device_enumerator 2022-08-27 11:32:11 +00:00
Daan De Meyer 24d59aeed3 loop-util: Add loop_device_unrelinquish()
Allows taking ownership of a loop device which makes sure that
loop_device_unrefp() will try to destroy it when it runs.
2022-08-03 20:55:32 +02:00
Lennart Poettering 7176f06c9e basic: split out dev_t related calls into new devno-util.[ch]
No actual code changes, just splitting out of some dev_t handling
related calls from stat-util.[ch], they are quite a number already, and
deserve their own module now I think.

Also, try to settle on the name "devnum" as the name for the concept,
instead of "devno" or "dev" or "devid". "devnum" is the name exported in
udev APIs, hence probably best to stick to that. (this just renames a
few symbols to "devum", local variables are left untouched, to make the
patch not too invasive)

No actual code changes.
2022-04-13 16:26:31 +02:00
Lennart Poettering 3b195f63fc loop-util: add debug message with details about acquired loopback device 2022-04-07 18:55:58 +02:00
Lennart Poettering 3e9210577d loop-util: explicitly close loopback block device before sleeping
attach_empty() file takes a BSD file lock on the device, and we really
should release that before going to sleep. hence explicitly close the
block device before the sleep instead of relying on _cleanup_ to close
it after the sleep.
2022-04-07 18:55:58 +02:00
Lennart Poettering 49043f8115 loop-util: use ERRNO_IS_DEVICE_ABSENT() macro where appropriate 2022-04-07 18:55:58 +02:00
Lennart Poettering cc53046620 loop-util: take a LOCK_EX BSD file lock on control device while we acquire a loopback device 2022-04-07 18:55:58 +02:00
Lennart Poettering 7ffc7f3fcc loop-util: slightly rework device_has_block_children()
Let's match by devtype, i.e. the official way to distinguish "whole"
block devices from partitions.

Also add debug logging for devices we thus ignore.
2022-04-07 18:55:58 +02:00
Lennart Poettering a145f8c06c loop-util: let's cut trailing whitespace, not trailing lines
This doesn't really make any real difference, given the file should only
contain a single line. But it's conceptually more correct to just remove
the trailing newline/whitespace then the whole lines coming after that.
i.e. if the file actually contains more lines than one, this should
probably be considered an error.
2022-04-07 18:55:58 +02:00
Yu Watanabe 7e93a65868 fd-util: rename loop_get_diskseq() -> fd_get_diskseq()
And move it from loop-util.[ch] -> fd-util.[ch]
2022-04-01 15:13:18 +09:00
Yu Watanabe 2076612f84 basic/missing: move BLKGETDISKSEQ to missing_fs.h
As it is defined at linux/fs.h.
2022-04-01 15:13:18 +09:00
Lennart Poettering 7c248223eb tree-wide: use new RET_NERRNO() helper at various places 2021-11-16 08:04:09 +01:00
Lennart Poettering d7654742ee 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
2021-11-05 07:08:16 +00:00
Lennart Poettering 0193b93eb5 loop-util: call loop_device_make_internal() at the right place
The whole reason loop_device_make_internal() exists (as opposed to just
loop_device_make()) is to avoid mangling the loop flags value/call
getenv twice. Hence let's actually call it when we already mangled the
flags value.
2021-10-20 09:57:16 +02:00
Lennart Poettering aa4d3aa3ef loop-util: add debug logging about O_RDWR vs. O_RDONLY + O_DIRECT mode
Once we managed to open the file let's log what we wanted and what we
got.
2021-10-20 09:56:20 +02:00