Skip to contentSkip to conversation

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

PATCH Superseded Sidong Yang 2 participants 2 messages Nov 9, 2020 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

Add available space information from statfs(). This can be different from 'Free (estimated)' in some cases. This patch provide more information about filesystem usage.

Issue: #306

---
 cmds/filesystem-usage.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
cmds/filesystem-usage.c +11 -0
index ab60d769..c17e26c3 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,13 @@ 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) {
+		error("cannot get space info with statfs() on '%s': %m", path);
+		ret = 1;
+		goto exit;
+	}
+
 	printf("Overall:\n");
 
 	printf("    Device size:\t\t%*s\n", width,
@@ -572,6 +581,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.17.1
#1 lore raw
Quoted 31 lines · Sidong Yang

Add available space information from statfs(). This can be different from 'Free (estimated)' in some cases. This patch provide more information about filesystem usage.

Issue: #306

---
 cmds/filesystem-usage.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
cmds/filesystem-usage.c +2 -0
index ab60d769..c17e26c3 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 563

Quoted 6 lines
cmds/filesystem-usage.c +3 -0
@@ -556,6 +558,13 @@ 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) {
+		error("cannot get space info with statfs() on '%s': %m", path);

I wonder when statfs would not work and I don't think it should be a hard error, maybe a warning or some stub that says the information was not available.

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

Quoted 13 lines
cmds/filesystem-usage.c +6 -0
+		ret = 1;
+		goto exit;
+	}
+
 	printf("Overall:\n");
 
 	printf("    Device size:\t\t%*s\n", width,
@@ -572,6 +581,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));

It's good to paste sample output of the command to the changelog to have a second thought on how it actually looks. A simple 'Avail' is without context and I as a user have no idea what it means or how to interpret the value. Please try again.