* [PATCH 1/3] btrfs-progs: remove use BLKGETSIZE64
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
@ 2020-03-15 15:24 ` Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 2/3] btrfs-progs: Add BTRFS_IOC_GET_CHUNK_INFO ioctl Goffredo Baroncelli
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-15 15:24 UTC (permalink / raw)
To: linux-btrfs; +Cc: Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
Allow the get_partition_size() to be called without
root privileges.
Signed-off-by: Goffredo baroncelli <kreijack@inwind.it>
---
common/device-utils.c | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
diff --git a/common/device-utils.c b/common/device-utils.c
index b03d62fa..9519dbce 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -17,6 +17,8 @@
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/statfs.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -238,17 +240,28 @@ u64 disk_size(const char *path)
u64 get_partition_size(const char *dev)
{
- u64 result;
- int fd = open(dev, O_RDONLY);
+ struct stat statbuf;
+ int r;
+ int fd;
+ char buf[100];
- if (fd < 0)
+ r = stat(dev, &statbuf);
+ if (r != 0)
return 0;
- if (ioctl(fd, BLKGETSIZE64, &result) < 0) {
- close(fd);
- return 0;
- }
+
+ snprintf(buf, sizeof(buf),
+ "/sys/dev/block/%d:%d/size", gnu_dev_major(statbuf.st_rdev),
+ gnu_dev_minor(statbuf.st_rdev));
+
+ fd = open(buf, O_RDONLY);
+ BUG_ON(fd < 0);
+
+ r = read(fd, buf, sizeof(buf)-1);
close(fd);
- return result;
+ BUG_ON(r < 0);
+ buf[r] = 0;
+
+ return atoll(buf) * 512;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/3] btrfs-progs: Add BTRFS_IOC_GET_CHUNK_INFO ioctl.
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 1/3] btrfs-progs: remove use BLKGETSIZE64 Goffredo Baroncelli
@ 2020-03-15 15:24 ` Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 3/3] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-15 15:24 UTC (permalink / raw)
To: linux-btrfs; +Cc: Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
---
ioctl.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/ioctl.h b/ioctl.h
index 4e7efd94..bdf70849 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -775,6 +775,64 @@ struct btrfs_ioctl_get_subvol_rootref_args {
};
BUILD_ASSERT(sizeof(struct btrfs_ioctl_get_subvol_rootref_args) == 4096);
+struct btrfs_chunk_info_stripe {
+ __u64 devid;
+ __u64 offset;
+ __u8 dev_uuid[BTRFS_UUID_SIZE];
+};
+
+struct btrfs_chunk_info {
+ /* logical start of this chunk */
+ __u64 offset;
+ /* size of this chunk in bytes */
+ __u64 length;
+
+ __u64 stripe_len;
+ __u64 type;
+
+ /* 2^16 stripes is quite a lot, a second limit is the size of a single
+ * item in the btree
+ */
+ __u16 num_stripes;
+
+ /* sub stripes only matter for raid10 */
+ __u16 sub_stripes;
+
+ struct btrfs_chunk_info_stripe stripes[1];
+ /* additional stripes go here */
+};
+
+struct btrfs_ioctl_chunk_info {
+ /* offset to start the search; after the ioctl, this field contains
+ * the next offset to start a search
+ */
+ u64 offset; /* in/out */
+ /* size of the passed buffer, including btrfs_ioctl_chunk_info */
+ u32 buf_size; /* in */
+ /* number of items returned */
+ u32 items_count; /* out */
+};
+
+static inline struct btrfs_chunk_info *
+btrfs_first_chunk_info(struct btrfs_ioctl_chunk_info *bici)
+{
+ return (struct btrfs_chunk_info *)((char *)bici +
+ sizeof(struct btrfs_ioctl_chunk_info));
+}
+
+static inline int btrfs_chunk_info_size(struct btrfs_chunk_info *ci)
+{
+ return sizeof(struct btrfs_chunk_info) +
+ sizeof(struct btrfs_chunk_info_stripe) * (ci->num_stripes-1);
+}
+
+static inline struct btrfs_chunk_info *
+btrfs_next_chunk_info(struct btrfs_chunk_info *ci)
+{
+ return (struct btrfs_chunk_info *)((char *)ci +
+ btrfs_chunk_info_size(ci));
+}
+
/* Error codes as returned by the kernel */
enum btrfs_err_code {
notused,
@@ -945,6 +1003,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
struct btrfs_ioctl_ino_lookup_user_args)
#define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \
struct btrfs_ioctl_vol_args_v2)
+#define BTRFS_IOC_GET_CHUNK_INFO _IOR(BTRFS_IOCTL_MAGIC, 64, \
+ struct btrfs_ioctl_chunk_info)
#ifdef __cplusplus
}
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/3] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 1/3] btrfs-progs: remove use BLKGETSIZE64 Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 2/3] btrfs-progs: Add BTRFS_IOC_GET_CHUNK_INFO ioctl Goffredo Baroncelli
@ 2020-03-15 15:24 ` Goffredo Baroncelli
2020-03-25 20:12 ` [RFC] " Goffredo Baroncelli
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-15 15:24 UTC (permalink / raw)
To: linux-btrfs; +Cc: Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
Use the new ioctl BTRFS_IOC_GET_CHUNK_INFO in load_chunk_info() instead of
the BTRFS_IOC_TREE_SEARCH. The old method is still present as fallback for
compatibility reason.
The goal is to avoid BTRFS_IOC_TREE_SEARCH because it requires root
privileges.
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
---
cmds/filesystem-usage.c | 108 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 105 insertions(+), 3 deletions(-)
diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
index aa7065d5..79792ee1 100644
--- a/cmds/filesystem-usage.c
+++ b/cmds/filesystem-usage.c
@@ -39,7 +39,7 @@
/*
* Add the chunk info to the chunk_info list
*/
-static int add_info_to_list(struct chunk_info **info_ptr,
+static int legacy_add_info_to_list(struct chunk_info **info_ptr,
int *info_count,
struct btrfs_chunk *chunk)
{
@@ -130,7 +130,8 @@ static int cmp_chunk_info(const void *a, const void *b)
((struct chunk_info *)b)->type);
}
-static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
+static int legacy_load_chunk_info(int fd, struct chunk_info **info_ptr,
+ int *info_count)
{
int ret;
struct btrfs_ioctl_search_args args;
@@ -182,7 +183,7 @@ static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count
off += sizeof(*sh);
item = (struct btrfs_chunk *)(args.buf + off);
- ret = add_info_to_list(info_ptr, info_count, item);
+ ret = legacy_add_info_to_list(info_ptr, info_count, item);
if (ret) {
*info_ptr = NULL;
return 1;
@@ -215,6 +216,107 @@ static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count
return 0;
}
+/*
+ * Add the chunk info to the chunk_info list
+ */
+static int add_info_to_list(struct chunk_info **info_ptr,
+ int *info_count,
+ struct btrfs_chunk_info *chunk)
+{
+
+ u64 type = chunk->type;
+ u64 size = chunk->length;
+ int num_stripes = chunk->num_stripes;
+ int j;
+
+ for (j = 0 ; j < num_stripes ; j++) {
+ int i;
+ struct chunk_info *p = NULL;
+ u64 devid;
+
+ devid = chunk->stripes[j].devid;
+
+ for (i = 0 ; i < *info_count ; i++)
+ if ((*info_ptr)[i].type == type &&
+ (*info_ptr)[i].devid == devid &&
+ (*info_ptr)[i].num_stripes == num_stripes) {
+ p = (*info_ptr) + i;
+ break;
+ }
+
+ if (!p) {
+ int tmp = sizeof(struct btrfs_chunk) *
+ (*info_count + 1);
+ struct chunk_info *res = realloc(*info_ptr, tmp);
+
+ if (!res) {
+ free(*info_ptr);
+ error("not enough memory");
+ return -ENOMEM;
+ }
+
+ *info_ptr = res;
+ p = res + *info_count;
+ (*info_count)++;
+
+ p->devid = devid;
+ p->type = type;
+ p->size = 0;
+ p->num_stripes = num_stripes;
+ }
+
+ p->size += size;
+
+ }
+
+ return 0;
+
+}
+
+static int load_chunk_info(int fd, struct chunk_info **info_ptr,
+ int *info_count)
+{
+
+ char buf[4096];
+ struct btrfs_ioctl_chunk_info *bici =
+ (struct btrfs_ioctl_chunk_info *)buf;
+ int cont;
+
+ bici->buf_size = sizeof(buf);
+ bici->offset = (u64)0;
+
+ do {
+ int i;
+ struct btrfs_chunk_info *ci;
+ int ret;
+
+ cont = false;
+ ret = ioctl(fd, BTRFS_IOC_GET_CHUNK_INFO, bici);
+ if (ret < 0) {
+ int e = errno;
+
+ if (e == ENOTTY)
+ return legacy_load_chunk_info(fd, info_ptr,
+ info_count);
+ else if (e == EAGAIN)
+ cont = true;
+ else
+ return -e;
+ }
+
+ ci = btrfs_first_chunk_info(bici);
+ for (i = 0 ; i < bici->items_count ; i++) {
+ add_info_to_list(info_ptr, info_count, ci);
+ ci = btrfs_next_chunk_info(ci);
+ }
+
+ } while (cont);
+
+ qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
+ cmp_chunk_info);
+
+ return 0;
+}
/*
* Helper to sort the struct btrfs_ioctl_space_info
*/
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
` (2 preceding siblings ...)
2020-03-15 15:24 ` [PATCH 3/3] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
@ 2020-03-25 20:12 ` Goffredo Baroncelli
2020-03-31 19:17 ` Goffredo Baroncelli
2020-03-31 19:18 ` Goffredo Baroncelli
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-25 20:12 UTC (permalink / raw)
To: linux-btrfs
PING,
does someone find interest on this kind of patch ?
BR
G.Baroncelli
On 3/15/20 4:24 PM, Goffredo Baroncelli wrote:
>
> Hi,
>
> this is a repost of an old patch (~2017). At the time it din't received
> any feedback. I repost it hoping that it still be interested.
>
> This patch set is the btrfs-prog related one; another one related to the
> kernel is send separately.
>
> This patch set createa a new ioctl BTRFS_IOC_GET_CHUNK_INFO.
> The aim is to replace the BTRFS_IOC_TREE_SEARCH ioctl
> used by "btrfs fi usage" to obtain information about the
> chunks/block groups.
>
> The problems in using the BTRFS_IOC_TREE_SEARCH is that it access
> the very low data structure of BTRFS. This means:
> 1) this would be complicated a possible change of the disk format
> 2) it requires the root privileges
>
> The BTRFS_IOC_GET_CHUNK_INFO ioctl can be called even from a not root
> user: I think that the data exposed are not sensibile data.
>
> These patches allow to use "btrfs fi usage" without root privileges.
>
> before:
> -------------------------------------------
>
> $ btrfs fi us /
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
>
> after:
> -----------------------------------------------
> $ ./btrfs fi us /
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
> /dev/sdd3 23.00GiB
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
> /dev/sdd3 3.00GiB
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
> /dev/sdd3 32.00MiB
>
> Unallocated:
> /dev/sdd3 73.97GiB
>
> Comments are welcome
> BR
> G.Baroncelli
>
>
--
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
` (3 preceding siblings ...)
2020-03-25 20:12 ` [RFC] " Goffredo Baroncelli
@ 2020-03-31 19:17 ` Goffredo Baroncelli
2020-03-31 19:18 ` Goffredo Baroncelli
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-31 19:17 UTC (permalink / raw)
To: linux-btrfs
Gentle ping.
BR
G.Baroncelli
On 3/15/20 4:24 PM, Goffredo Baroncelli wrote:> > Hi,>
> this is a repost of an old patch (~2017). At the time it din't received
> any feedback. I repost it hoping that it still be interested.
>
> This patch set is the btrfs-prog related one; another one related to the
> kernel is send separately.
>
> This patch set createa a new ioctl BTRFS_IOC_GET_CHUNK_INFO.
> The aim is to replace the BTRFS_IOC_TREE_SEARCH ioctl
> used by "btrfs fi usage" to obtain information about the
> chunks/block groups.
>
> The problems in using the BTRFS_IOC_TREE_SEARCH is that it access
> the very low data structure of BTRFS. This means:
> 1) this would be complicated a possible change of the disk format
> 2) it requires the root privileges
>
> The BTRFS_IOC_GET_CHUNK_INFO ioctl can be called even from a not root
> user: I think that the data exposed are not sensibile data.
>
> These patches allow to use "btrfs fi usage" without root privileges.
>
> before:
> -------------------------------------------
>
> $ btrfs fi us /
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
>
> after:
> -----------------------------------------------
> $ ./btrfs fi us /
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
> /dev/sdd3 23.00GiB
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
> /dev/sdd3 3.00GiB
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
> /dev/sdd3 32.00MiB
>
> Unallocated:
> /dev/sdd3 73.97GiB
>
> Comments are welcome
> BR
> G.Baroncelli
>
>
--
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
` (4 preceding siblings ...)
2020-03-31 19:17 ` Goffredo Baroncelli
@ 2020-03-31 19:18 ` Goffredo Baroncelli
5 siblings, 0 replies; 7+ messages in thread
From: Goffredo Baroncelli @ 2020-03-31 19:18 UTC (permalink / raw)
To: linux-btrfs
Gentle ping.
BR
G.Baroncelli
On 3/15/20 4:24 PM, Goffredo Baroncelli wrote:
>
> Hi,
>
> this is a repost of an old patch (~2017). At the time it din't received
> any feedback. I repost it hoping that it still be interested.
>
> This patch set is the btrfs-prog related one; another one related to the
> kernel is send separately.
>
> This patch set createa a new ioctl BTRFS_IOC_GET_CHUNK_INFO.
> The aim is to replace the BTRFS_IOC_TREE_SEARCH ioctl
> used by "btrfs fi usage" to obtain information about the
> chunks/block groups.
>
> The problems in using the BTRFS_IOC_TREE_SEARCH is that it access
> the very low data structure of BTRFS. This means:
> 1) this would be complicated a possible change of the disk format
> 2) it requires the root privileges
>
> The BTRFS_IOC_GET_CHUNK_INFO ioctl can be called even from a not root
> user: I think that the data exposed are not sensibile data.
>
> These patches allow to use "btrfs fi usage" without root privileges.
>
> before:
> -------------------------------------------
>
> $ btrfs fi us /
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
>
> after:
> -----------------------------------------------
> $ ./btrfs fi us /
> Overall:
> Device size: 100.00GiB
> Device allocated: 26.03GiB
> Device unallocated: 73.97GiB
> Device missing: 0.00B
> Used: 17.12GiB
> Free (estimated): 80.42GiB (min: 80.42GiB)
> Data ratio: 1.00
> Metadata ratio: 1.00
> Global reserve: 53.12MiB (used: 0.00B)
>
> Data,single: Size:23.00GiB, Used:16.54GiB (71.93%)
> /dev/sdd3 23.00GiB
>
> Metadata,single: Size:3.00GiB, Used:588.94MiB (19.17%)
> /dev/sdd3 3.00GiB
>
> System,single: Size:32.00MiB, Used:16.00KiB (0.05%)
> /dev/sdd3 32.00MiB
>
> Unallocated:
> /dev/sdd3 73.97GiB
>
> Comments are welcome
> BR
> G.Baroncelli
>
>
--
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
^ permalink raw reply [flat|nested] 7+ messages in thread