On 10/08/2018 04:40 PM, Hans van Kranenburg wrote:
> On 10/08/2018 04:27 PM, Holger Hoffstätte wrote:
>> (moving the discussion here from GH [1])
>>
>> Apparently there is something weird going on with the device stats
>> ioctls. I cannot get them to work as regular user, while they work
>> for David. A friend confirms the same issue on his system - no access
>> as non-root.
>>
>> So I made a new empty fs, mounted it, built btrfs-progs-4.17.1 with
>> debug symbols and stepped into search_chunk_tree_for_fs_info().
>> Everything is fine, all args are correct, right until:
>>
>> (gdb) s
>> 1614 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_args);
>> (gdb) s
>> 1615 if (ret < 0)
>> (gdb) p ret
>> $4 = -1
>> (gdb) p search_args
>> $5 = {key = {tree_id = 3, min_objectid = 1, max_objectid = 1, min_offset
>> = 1,
>> max_offset = 18446744073709551615, min_transid = 0, max_transid =
>> 18446744073709551615,
>> min_type = 216, max_type = 216, nr_items = 30, unused = 0, unused1 = 0,
>> unused2 = 0,
>> unused3 = 0, unused4 = 0}, buf = '\000' <repeats 3991 times>}
>>
>> Looking at the kernel side of things in fs/btrfs/ioctl.c I see both
>> BTRFS_IOC_TREE_SEARCH[_V2} unconditionally require CAP_SYS_ADMIN.
>
> That's the tree search ioctl, for reading arbitrary metadata.
>
> The device stats ioctl is IOC_GET_DEV_STATS...
>
> I can do the device stats ioctl as normal user:
>
> import btrfs
> fs = btrfs.FileSystem('/')
> btrfs.utils.pretty_print(fs.dev_stats(1))
>
> <btrfs.ioctl.DevStats>
> devid: 1
> nr_items: 5
> flags: 0
> write_errs: 0
> read_errs: 0
> flush_errs: 0
> generation_errs: 0
> corruption_errs: 0
By the way, I can also do BTRFS_IOC_FS_INFO, BTRFS_IOC_DEV_INFO and
BTRFS_IOC_SPACE_INFO as normal user.
However, while fs_info tells me that there are num_devices devices,
there's no place where you can actually get which devids these are, and
you need to provide them one by one to dev_info and dev_stats... :
btrfs.utils.pretty_print(fs.fs_info())
<btrfs.ioctl.FsInfo>
max_id: 1
num_devices: 1
nodesize: 4096
sectorsize: 4096
clone_alignment: 4096
fsid: 91077ca5-6559-4a90-9d03-912d3a33412e
btrfs.utils.pretty_print(fs.dev_info(1))
<btrfs.ioctl.DevInfo>
devid: 1
bytes_used: 60699967488
total_bytes: 107374182400
path: /dev/xvda
uuid: 7e998baa-b533-4476-9132-d7d748d28044
btrfs.utils.pretty_print(fs.space_info())
-
<btrfs.ioctl.SpaceInfo>
flags: Data, single
total_bytes: 54.00GiB
used_bytes: 53.27GiB
-
<btrfs.ioctl.SpaceInfo>
flags: System, single
total_bytes: 32.00MiB
used_bytes: 12.00KiB
-
<btrfs.ioctl.SpaceInfo>
flags: Metadata, single
total_bytes: 2.50GiB
used_bytes: 1.30GiB
-
<btrfs.ioctl.SpaceInfo>
flags: GlobalReserve, single
total_bytes: 181.02MiB
used_bytes: 0.00B
>
>> So why can Dave get his dev stats as unprivileged user?
>> Does this work for anybody else? And why? :)
>>
>> cheers
>> Holger
>>
>> [1]
>> https://github.com/prometheus/node_exporter/issues/1100#issuecomment-427823190
>>
>
>
--
Hans van Kranenburg