From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 0/5] btrfs-progs: device_get_partition_size*() enhancement and cleanup
Date: Sat, 2 Aug 2025 16:25:46 +0930 [thread overview]
Message-ID: <cover.1754116463.git.wqu@suse.com> (raw)
Inspired by recent fixes from Zotan to device_get_partition_size_sysfs(),
it turns out the idea of falling back to use sysfs is not sound in the first
place.
The original issue is unprivileged user can cause `btrfs device usage`
to output incorrect values:
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
It turns out that, the "unprivileged" part means, the user doesn't have
read access to the raw device.
It's common that a lot of distros have the following mode for raw block
devices:
brw-rw---- 1 root disk 254, 32 Aug 2 13:35 /dev/vdc
So unprivileged users can not do read access to block devices thus
failed to grab the block device size, and this is normally considered as
a safety method.
But if the end user is inside disk group, everything works fine:
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
And the original bug is that the sysfs interface only gives the device
size in block unit, and block size can be hard to fetch (partition and
dm have different path to grab the block size).
This makes me wonder, why we need the sysfs interface in the first
place.
If the unprivileged user has no access to a block device, we should
not workaround it, but give a proper error/warning message.
So this series address the root problem, no proper error code for
device_get_partition_size*() helpers.
With proper error handling and messages, we shouldn't even need to use
sysfs to workaround the problem.
With the sysfs fallback removed, a completely unprivileged user will get
the following result:
WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
WARNING: failed to get device size for devid 1: Permission denied
/dev/mapper/test-scratch1, ID: 1
Device size: 0.00B
Device slack: 0.00B
Unallocated: N/A
Which should give enough for the end user without weird output at all.
Qu Wenruo (5):
btrfs-progs: remove the unnecessary device_get_partition_size() call
btrfs-progs: add error handling for device_get_partition_size()
btrfs-progs: add error handling for
device_get_partition_size_fd_stat()
btrfs-progs: remove device_get_partition_size_fd()
btrfs-progs: remove device_get_partition_size_sysfs()
check/main.c | 8 +++-
check/mode-lowmem.c | 8 +++-
cmds/filesystem-usage.c | 11 ++++--
cmds/replace.c | 14 ++++++-
common/device-utils.c | 87 ++++++++++-------------------------------
common/device-utils.h | 5 +--
image/common.c | 7 +++-
kernel-shared/volumes.c | 9 ++++-
kernel-shared/zoned.c | 18 ++++++---
mkfs/common.c | 16 ++++++--
mkfs/main.c | 12 ++++--
11 files changed, 103 insertions(+), 92 deletions(-)
--
2.50.1
next reply other threads:[~2025-08-02 6:56 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-02 6:55 Qu Wenruo [this message]
2025-08-02 6:55 ` [PATCH 1/5] btrfs-progs: remove the unnecessary device_get_partition_size() call Qu Wenruo
2025-08-02 6:55 ` [PATCH 2/5] btrfs-progs: add error handling for device_get_partition_size() Qu Wenruo
2025-08-05 19:08 ` Boris Burkov
2025-08-05 22:41 ` Qu Wenruo
2025-08-05 23:33 ` Boris Burkov
2025-08-02 6:55 ` [PATCH 3/5] btrfs-progs: add error handling for device_get_partition_size_fd_stat() Qu Wenruo
2025-08-02 6:55 ` [PATCH 4/5] btrfs-progs: remove device_get_partition_size_fd() Qu Wenruo
2025-08-02 6:55 ` [PATCH 5/5] btrfs-progs: remove device_get_partition_size_sysfs() Qu Wenruo
2025-08-05 19:15 ` Boris Burkov
2025-08-05 23:04 ` Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cover.1754116463.git.wqu@suse.com \
--to=wqu@suse.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).