blockdev-util: "partscan" sysattr now directly shows the enabled state

See also: https://lore.kernel.org/r/20240502130033.1958492-3-hch@lst.de
This commit is contained in:
Mike Yuan 2024-05-26 15:54:06 +08:00 committed by Luca Boccassi
parent fbe1e72eff
commit 100bed702b

View file

@ -375,9 +375,13 @@ int blockdev_partscan_enabled(int fd) {
* With https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3),
* the 'capability' sysfs attribute is deprecated, hence we cannot check the flag from it.
*
* To support both old and new kernels, we need to do the following: first check 'ext_range' sysfs
* attribute, and if '1' we can conclude partition scanning is disabled, otherwise check 'capability'
* sysattr for older version. */
* With https://github.com/torvalds/linux/commit/a4217c6740dc64a3eb6815868a9260825e8c68c6
* (backported to v6.9), the partscan status is directly exposed as 'partscan' sysattr.
*
* To support both old and new kernels, we need to do the following: first check 'partscan' attr
* where the information is made directly available; then, fall back to 'ext_range' sysfs attribute,
* and if '1' we can conclude partition scanning is disabled; otherwise check 'capability' sysattr
* for ancient version. */
assert(fd >= 0);
@ -385,6 +389,10 @@ int blockdev_partscan_enabled(int fd) {
if (r < 0)
return r;
r = device_get_sysattr_bool(dev, "partscan");
if (r != -ENOENT)
return r;
r = device_get_sysattr_int(dev, "ext_range", &ext_range);
if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
* partition block device, not the whole block device. And that means we have no