Commit Graph
60 Commits
Author SHA1 Message Date
Mike Yuan cc9a9d6a15 tree-wide: use path_split_prefix_filename() where appropriate 2026-02-09 09:07:15 +01:00
Chris Down b38943b01b blockdev-util: Iterate through all DM layers
get_block_device_harder_fd() currently only traverses one level of
device mapper stacking when looking for the underlying block device.
This causes issues with nested DM setups like dm-crypt on top of
dm-integrity, where we don't traverse enough to get the actual physical
device.

Fix this by iterating through all DM layers until we reach a device with
no underlying device.

Fixes: #40419
2026-01-23 21:27:12 +08:00
Chris Down 7b8c4d01e6 blockdev-util: Add recursive lookup flag
Originating device lookups currently use separate entry points and
callers must decide between the single-step helper and manual looping.

Add a recursive flag to block_device_get_originating() and
block_get_originating(), and update callers to pass an explicit value.
2026-01-23 13:26:08 +08:00
Yu Watanabe 93378148d4 tree-wide: normalize comment style 2025-12-15 08:50:13 +09:00
Lennart Poettering e582484789 tree-wide: open block device locks in writable mode
udev's block device locking protocol has one pitfall not even the
example in the documentation got right so far (even though this is
explained in all detail above): udev's rescanning is only triggered when
an fd that is opened for writing is closed. This means that if a
separate locking fd is opened on a block device – one that is maintained
independently of the fd actually used for writing – it must be opened for
writing too, so that closing the lock definitely triggers a rescan. This
matters in cases where the lock fd is kept for longer than the fd used
for writing to disk. (Because otherwise udev might get the
IN_CLOSE_WRITE event, but when it tries to rescan will find the device
locked, and never retry because no IN_CLOSE_WRITE is triggred anymore.)

Let's fix that across the codebase, at 4 places:

1. in makefs (a lock fd is kept, and mkfs then invoked as child, which
   uses a different fd, and the lock fd is closed only once the child
   died)

2. in udevadm lock (embarassing!): which is intended to be used to wrap tools
   that modify disk contents, very similar to the makefs case. The lock
   is also kept until after the tool exited.

3. In storagetm: the kernel nvme-tcp layer writes to the device
   directly, we just keep a lock fd.

4. the example in BLOCK_DEVICE_LOCKING.md
2025-10-22 22:56:02 +02:00
Lennart Poettering aa47d8ade1 udev: switch over to rereadpt() rather than raw BLKRRPART
Fixes: #38672
2025-10-22 22:56:01 +02:00
Lennart Poettering 524ebfe28a blockdev-util: split out partition device node generation from dissect-util.c 2025-10-22 22:56:01 +02:00
Lennart Poettering bb45a893c2 blockdev-util: in blockdev_partscan_enabled() check if we are operating on block device first
The function makes no sense on any other type of fd, hence we better
check this explicitly.
2025-10-22 22:56:01 +02:00
Lennart Poettering a251345cf5 blockdev-util: rename BlockDeviceLookupFlag to plural
This is a flags type and a flag function argument, let's name it in
plural, because it allows many flags combinations. Internally, the
implementation already used plural, but let's fix the prototypes too.
2025-10-22 22:56:01 +02:00
Daan De Meyer 69a283c5f2 shared: Clean up includes
Split out of #37344.
2025-05-24 14:00:44 +02:00
Daan De Meyer bdcd47a269 blockdev-util: Remove dependency on string-util.h
Let's insist on a string literal in SYS_BLOCK_PATH_MAX() so that
we don't accidentally allocate VLAs and let's inline strempty() in
xsprintf_sys_block_path() so we don't need to include string-util.h
in blockdev-util.h

We'll remove the actual string-util.h include as part of #37344.
2025-05-14 16:56:05 +02:00
Yu Watanabe ab1bd9daed device-util: introduce several more helper functions
This also makes device_in_subsystem() and device_is_devtype() return
negative error on critical error
2025-05-14 02:06:02 +09:00
Mike Yuan 60997a87ef blockdev-util: don't use mixed style of retval in block_device_get_originating
We have two typical styles of 'ret' param assignment + retval:

1) < 0 on actual error, 0 on nothing (ret == NULL), > 0 on something
2) recognizable errno on nothing, < 0 on other errors, >= 0 on something

but never use both at the same time.
2025-05-05 18:39:05 +02:00
Daan De Meyer bcf716ca01 tree-wide: Remove dead code
This code seems to not be used anywhere at all, so let's drop it.

Follow up for ba8d00e859
Follow up for 85686b37b0
Follow up for aff853f8ea
Follow up for 36dad381a2
2025-04-24 10:15:19 +09:00
Mike Yuan 8b0818fa45 shared/blockdev-util: shorten the code a bit
If btrfs_get_block_device_fd() returns 0, ret is already initialized.
2025-01-02 15:29:34 +01:00
Lennart Poettering bff5d2fd5a blockdev-util: add partscan check function that takes an sd_device* 2024-07-01 16:35:56 +02:00
Mike Yuan a6117106e4 blockdev-util: update comment for blockdev_partscan_enabled
Follow-up for aa6fe772e1

Also, the 'partscan' sysattr has been backported to v6.6.
2024-05-30 10:57:30 +02:00
Lennart Poettering fa65917623 blockdev-util: partition block devices never have partition scanning enabled 2024-05-28 11:08:06 +02:00
Yu Watanabe bab8c851da blockdev-util: also check loop/partscan sysattr
With https://github.com/torvalds/linux/commit/b9684a71fca793213378dd410cd11675d973eaa1 (v5.19),
we cannot check partition scanning is enabled for a loopback block device
without checking the attribute.
2024-05-27 09:33:46 +09:00
Yu Watanabe f0c2668c99 blockdev-util: also check newer value of GENHD_FL_NO_PART flag
With https://github.com/torvalds/linux/commit/430cc5d3ab4d0ba0bd011cfbb0035e46ba92920c,
the value of GENHD_FL_NO_PART, previously named as GENHD_FL_NO_PART_SCAN,
is changed from 0x0200 to 0x0004. So, we need to check both flags.
2024-05-27 09:33:09 +09:00
Mike Yuan 100bed702b blockdev-util: "partscan" sysattr now directly shows the enabled state
See also: https://lore.kernel.org/r/20240502130033.1958492-3-hch@lst.de
2024-05-26 13:52:31 +02:00
Yu Watanabe 955d5012e0 blockdev-util: fix typo
Follow-up for 33ff155957.
2024-04-18 13:08:14 +09:00
Yu Watanabe 33ff155957 blockdev-util: also read 'ext_range' sysattr to check if the partscan is enabled
The 'capability' sysattr was deprecated by
https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3).
2024-04-18 09:48:26 +09:00
Yu Watanabe fb53ee0a64 tree-wide: use device_in_subsystem() and device_is_devtype() 2023-12-23 03:52:21 +09:00
Lennart Poettering 01db9c85cf blockdev-util: add new helper blockdev_get_device_size()
This function is just a wrapper around the BLKGETSIZE64. Which is a
pretty simple ioctl. The only reason to wrap it, is that the headers we
need to call it are a bit messy (as "linux/fs.h" is incompatible with
certain glibc headers). Hence add the simple helper that wraps it and
allows us to do the header mess needed in one file only.

It's also nicely symmetric to blockdev_get_sector_size().
2023-12-13 17:35:22 +01:00
Lennart Poettering 50ab93eb0e blockdev-util: export block_device_get_originating() 2023-11-02 14:19:32 +01:00
Daan De Meyer a1af83728f device-util: Declare iterator variables inline 2023-07-12 20:05:18 +02:00
Lennart Poettering c56be2c294 bootctl: add new --print-root-device option
We already have this nice code in system that determines the block
device backing the root file system, but it's only used internally in
systemd-gpt-generator. Let's make this more accessible and expose it
directly in bootctl.

It doesn't fit immediately into the topic of bootctl, but I think it's
close enough and behaves very similar to the existing "bootctl
--print-boot-path" and "--print-esp-path" tools.

If --print-root-device (or -R) is specified once, will show the block device
backing the root fs, and if specified twice (probably easier: -RR) it
will show the whole block device that block device belongs to in case it
is a partition block device.

Suggested use:

        # cfdisk `bootctl -RR`

To get access to the partition table, behind the OS install, for
whatever it might be.
2023-02-21 18:19:38 +01:00
Lennart Poettering 65046b92dc blockdev-util: add simple wrapper around BLKSSZGET
Just adds some typesafety and generates an error if the field is not
initialized in the block device yet.
2023-01-18 10:10:57 +01:00
Daan De Meyer 5228c58ebe dissect-image: Notify btrfs when we're done using a loop device
Let's explicitly let btrfs know when we're done using a loop device.
Otherwise, btrfs will keep the device UUID cached which will result
in mount() failures if we ever generate a device or filesystem with
the same UUID again.
2023-01-12 16:36:09 +01:00
Zbigniew Jędrzejewski-Szmek 254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Lennart Poettering 698bc1861e blockdev-util: move O_PATH fd conversion into btrfs_get_block_device_fd() to shorten things
And let's use a simple call to the new fd_reopen_condition() helper
there.
2022-11-29 15:34:27 +01:00
Yu Watanabe fd690a4b7f blockdev-util: introduce block_device_new_from_fd() and block_device_new_from_path()
These are possible replacements of get_block_device(),
get_block_device_harder(), path_get_whole_disk(), and friends.
2022-09-28 17:42:23 +09:00
Yu Watanabe 612fc70fc0 blockdev-util: re-implement block_get_originating() by using sd_device
And split out the core logic as block_device_get_originating().
Hopefully, this changes no behavior. Just refactoring and preparation
for later commits.
2022-09-28 17:41:09 +09:00
Yu Watanabe 446f8db66b blockdev-util: reopen file descriptor only when O_PATH is set
Prompted by 13879c54d4.
2022-09-28 17:41:09 +09:00
Yu Watanabe 7e4a0387c4 blockdev-util: split-out fd_get_devnum()
No functional changes, just preparation for later commits.
2022-09-28 17:41:09 +09:00
Lennart Poettering 13879c54d4 blockdev-util: make sure get_block_device_fd() works reliably for O_PATH, too 2022-09-20 13:52:01 +02:00
Yu Watanabe 2f9d231738 udev: use block_device_is_whole_disk()
No functional changes, just refactoring.

Note, this also makes synthesize_change() propagate the error from
synthesize_change_one(). However, the caller of synthesize_change()
ignores the failure anyway, hence the change does not take any effect.
2022-09-20 09:44:19 +02:00
Yu Watanabe 5a27af52c7 blockdev-util: change return value when a partition device is passed to partition_enumerator_new()
To make it consistent what returned by block_device_get_whole_disk().
2022-09-18 22:54:20 +09:00
Yu Watanabe 55a437f8b2 blockdev-util: introduce block_device_get_whole_disk() 2022-09-18 22:54:20 +09:00
Yu Watanabe c07186ec03 blockdev-util: split-out block_device_is_whole_disk()
No functional changes, just preparation for later commits.
2022-09-18 22:54:20 +09:00
Yu Watanabe 86f9b69a6a blockdev-util: split out blockdev_reread_partition_table()
No functional changes, just refactoring.
2022-09-10 01:19:41 +09:00
Yu Watanabe 9409174ee7 udevd: use partition enumerator at one more place
No functional changes, just refactoring.
2022-09-10 00:48:34 +09:00
Yu Watanabe 9cce72ee5d Merge pull request #24611 from yuwata/loop-util-refactoring
loop-util: several cleanups
2022-09-09 23:31:19 +09:00
Yu Watanabe e0993236d3 blockdevi-util: also check sysname when enumerating partitions 2022-09-09 22:30:19 +09:00
Yu Watanabe 833106b876 loop-util: do not try to enumerate partitions twice 2022-09-09 05:16:41 +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 af15ee0368 blockdev-util: check if provided sd_device is for a whole block device
And split out partition_generator_new(), to make it usable in other
functions.
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
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