Skip to contentSkip to conversation

btrfs-progs: filesystem usage: add avail info from statfs()

PATCH In review Sidong Yang 2 participants 2 messages Nov 10, 2020 – Dec 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 btrfs-progs: filesystem usage: add avail info from statfs()

dd available space information from statfs(). This can be different from 'Free (estimated)' in some cases. This patch provide more information about filesystem usage like below. and update document for this.

Overall:

    Device size:                   5.00GiB
    Device allocated:              1.02GiB
    Device unallocated:            3.98GiB

Device missing: 0.00B
Used: 88.00KiB
Free (estimated): 4.48GiB (min: 2.49GiB)
Avail: 4.48GiB
Data ratio: 1.00
Metadata ratio: 2.00
Global reserve: 832.00KiB (used: 0.00B)
Multiple profiles: no

Issue: #306

---
 Documentation/btrfs-filesystem.asciidoc |  3 +++
 cmds/filesystem-usage.c                 | 10 ++++++++++
 2 files changed, 13 insertions(+)
Documentation/btrfs-filesystem.asciidoc +3 -0
index 6a5561ed..18623afe 100644
@@ -276,6 +276,7 @@ Overall:
     Device missing:                  0.00B
     Used:                          1.14TiB
     Free (estimated):            692.57GiB      (min: 692.57GiB)
+    Avail:                       692.57GiB
     Data ratio:                       1.00
     Metadata ratio:                   1.00
     Global reserve:              512.00MiB      (used: 0.00B)
@@ -295,6 +296,8 @@ including the reserved space
 data, including currently allocated space and estimating the usage of the
 unallocated space based on the block group profiles, the 'min' is the lower bound
 of the estimate in case multiple profiles are present
+* 'Avail' -- the amount of space available for data. it's get by statfs() system
+call that can be different from 'Free (estimated)' in some cases
 * 'Data ratio' -- ratio of total space for data including redundancy or parity to
 the effectively usable data space, eg. single is 1.0, RAID1 is 2.0 and for RAID5/6
 it depends on the number of devices
cmds/filesystem-usage.c +10 -0
index ab60d769..ed743a61 100644
@@ -19,6 +19,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <sys/vfs.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <getopt.h>
@@ -430,6 +431,7 @@ static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
 	u64 free_min = 0;
 	double max_data_ratio = 1.0;
 	int mixed = 0;
+	struct statfs statfs_buf;
 
 	sargs = load_space_info(fd, path);
 	if (!sargs) {
@@ -556,6 +558,12 @@ static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
 	if (unit_mode != UNITS_HUMAN)
 		width = 18;
 
+	ret = statfs(path, &statfs_buf);
+	if (ret) {
+		warning("cannot get space info with statfs() on '%s': %m", path);
+		ret = 0;
+	}
+
 	printf("Overall:\n");
 
 	printf("    Device size:\t\t%*s\n", width,
@@ -572,6 +580,8 @@ static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
 		width,
 		pretty_size_mode(free_estimated, unit_mode));
 	printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
+	printf("    Avail:\t\t\t%*s\n", width,
+		pretty_size_mode(statfs_buf.f_bavail * statfs_buf.f_bsize, unit_mode));
 	printf("    Data ratio:\t\t\t%*.2f\n",
 		width, data_ratio);
 	printf("    Metadata ratio:\t\t%*.2f\n",
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Sidong Yang

dd available space information from statfs(). This can be different from 'Free (estimated)' in some cases. This patch provide more information about filesystem usage like below. and update document for this.

Quoted 8 lines

Overall:

    Device size:                   5.00GiB
    Device allocated:              1.02GiB
    Device unallocated:            3.98GiB

Device missing: 0.00B
Used: 88.00KiB
Free (estimated): 4.48GiB (min: 2.49GiB)
Avail: 4.48GiB

My idea was to print

      Free (statfs, df):		...

'Avail' without context is useless.

Quoted 54 lines

Data ratio: 1.00
Metadata ratio: 2.00
Global reserve: 832.00KiB (used: 0.00B)
Multiple profiles: no

Issue: #306

---
 Documentation/btrfs-filesystem.asciidoc |  3 +++
 cmds/filesystem-usage.c                 | 10 ++++++++++
 2 files changed, 13 insertions(+)
Documentation/btrfs-filesystem.asciidoc +3 -0
index 6a5561ed..18623afe 100644
@@ -276,6 +276,7 @@ Overall:
     Device missing:                  0.00B
     Used:                          1.14TiB
     Free (estimated):            692.57GiB      (min: 692.57GiB)
+    Avail:                       692.57GiB
     Data ratio:                       1.00
     Metadata ratio:                   1.00
     Global reserve:              512.00MiB      (used: 0.00B)
@@ -295,6 +296,8 @@ including the reserved space
 data, including currently allocated space and estimating the usage of the
 unallocated space based on the block group profiles, the 'min' is the lower bound
 of the estimate in case multiple profiles are present
+* 'Avail' -- the amount of space available for data. it's get by statfs() system
+call that can be different from 'Free (estimated)' in some cases
 * 'Data ratio' -- ratio of total space for data including redundancy or parity to
 the effectively usable data space, eg. single is 1.0, RAID1 is 2.0 and for RAID5/6
 it depends on the number of devices
cmds/filesystem-usage.c +2 -0
index ab60d769..ed743a61 100644
@@ -19,6 +19,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <sys/vfs.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <getopt.h>
@@ -430,6 +431,7 @@ static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
 	u64 free_min = 0;
 	double max_data_ratio = 1.0;
 	int mixed = 0;
+	struct statfs statfs_buf;
 
 	sargs = load_space_info(fd, path);
 	if (!sargs) {

cmds/filesystem-usage.c static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo, · line 565

Quoted 8 lines
cmds/filesystem-usage.c +5 -0
@@ -556,6 +558,12 @@ static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
 	if (unit_mode != UNITS_HUMAN)
 		width = 18;
 
+	ret = statfs(path, &statfs_buf);
+	if (ret) {
+		warning("cannot get space info with statfs() on '%s': %m", path);
+		ret = 0;
+	}

Yeah, that's better than hard failure.

With the text updated, patch added to devel, thanks.