From 8ac74315577182c679134aa700f9960f14cbe947 Mon Sep 17 00:00:00 2001 From: liuh Date: Wed, 3 Sep 2025 19:09:39 +0800 Subject: [PATCH] btrfs-progs: fix the wrong size from device_get_partition_size_sysfs() --- ...the-wrong-size-from-device_get_parti.patch | 75 +++++++++++++++++++ btrfs-progs.spec | 7 +- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 0016-btrfs-progs-fix-the-wrong-size-from-device_get_parti.patch diff --git a/0016-btrfs-progs-fix-the-wrong-size-from-device_get_parti.patch b/0016-btrfs-progs-fix-the-wrong-size-from-device_get_parti.patch new file mode 100644 index 0000000..a8e13b8 --- /dev/null +++ b/0016-btrfs-progs-fix-the-wrong-size-from-device_get_parti.patch @@ -0,0 +1,75 @@ +From: Zoltan Racz +Date: Thu, 31 Jul 2025 15:53:07 +0300 +Subject: [PATCH] btrfs-progs: fix the wrong size from + device_get_partition_size_sysfs() + +[BUG] +When an unprivileged user, who can not access the block device, run +"btrfs dev usage", it's very common to result the following incorrect +output: + + $ btrfs dev usage /mnt/btrfs/ + WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root + /dev/mapper/test-scratch1, ID: 1 + Device size: 20.00MiB <<< + Device slack: 16.00EiB <<< + Unallocated: N/A + +Note if the unprivileged user has read access to the raw block file, it +will work as expected: + + $ btrfs dev usage /mnt/btrfs/ + WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root + /dev/mapper/test-scratch1, ID: 1 + Device size: 10.00GiB + Device slack: 0.00B + Unallocated: N/A + +[CAUSE] +When device_get_partition_size() is called, firstly the function checks +if we can do a read-only open() on the block device. + +However under most distros, block devices are only accessible by root +and "disk" group. + +If the unprivileged user is not in "disk" group, the open() will fail +and we have to fallback to device_get_partition_size_sysfs() as the +fallback. + +The function device_get_partition_size_sysfs() will use +"/sys/block//size" as the size of the disk. + +But according to the kernel source code, the "size" attribute is +implemented by returning bdev_nr_sectors(), and that result is always in +sector unit (512 bytes). + +So if device_get_partition_size_sysfs() returns the value directly, it's +512 times smaller than the original size, causing errors. + +[FIX] +Just do the proper left shift to return size in bytes. + +Issue: #979 +Signed-off-by: Qu Wenruo +--- + common/device-utils.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/common/device-utils.c b/common/device-utils.c +index f86120a..d4c188c 100644 +--- a/common/device-utils.c ++++ b/common/device-utils.c +@@ -367,7 +367,9 @@ static u64 device_get_partition_size_sysfs(const char *dev) + return 0; + } + close(sysfd); +- return size; ++ ++ /* /size value is in sector (512B) unit. */ ++ return size << SECTOR_SHIFT; + } + + u64 device_get_partition_size(const char *dev) +-- +2.43.0 + diff --git a/btrfs-progs.spec b/btrfs-progs.spec index b704dc2..1b418b1 100644 --- a/btrfs-progs.spec +++ b/btrfs-progs.spec @@ -1,6 +1,6 @@ Name: btrfs-progs Version: 6.6.3 -Release: 15 +Release: 16 Summary: btrfs userspace programs License: GPLv2 and GPL+ and LGPL-2.1+ and GPL-3.0+ and LGPL-2.1 and MIT URL: https://btrfs.wiki.kernel.org/index.php/Main_Page @@ -21,6 +21,7 @@ Patch0012: 0012-btrfs-progs-convert-insert-a-dummy-inode-item-before.patch Patch0013: 0013-btrfs-progs-corrupt-block-fix-memory-leak-in-debug_c.patch Patch0014: 0014-btrfs-progs-image-fix-the-bug-that-filename-sanitiza.patch Patch0015: 0015-btrfs-progs-convert-fix-the-filename-output-when-rol.patch +Patch0016: 0016-btrfs-progs-fix-the-wrong-size-from-device_get_parti.patch BuildRequires: python3-devel >= 3.4 BuildRequires: libacl-devel, e2fsprogs-devel, libblkid-devel, libuuid-devel, zlib-devel, libzstd-devel, lzo-devel, systemd-devel @@ -86,6 +87,10 @@ make mandir=%{_mandir} bindir=%{_sbindir} libdir=%{_libdir} incdir=%{_includedir %{_mandir}/man8/*.gz %changelog +* Wed Sep 3 2025 liuh - 6.6.3-16 +- sync patch from community + btrfs-progs: fix the wrong size from device_get_partition_size_sysfs() + * Mon Oct 28 2024 liuh - 6.6.3-15 - btrfs-progs: convert: fix the filename output when rolling back -- Gitee