Skip to contentSkip to conversation

fi show, dev scan and lblkid

PATCH Superseded Anand Jain 2 participants 7 messages Sep 13, 2013 – Sep 16, 2013 Btrfs file system

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

v3 cover fi show, dev scan and lblkid

This patch set is about
use of lblkid for the btrfs disk scan and using this lblkid scan for the fi show and dev scan cli

This includes the comments from David (to use lblkid) and Zach to show (btrfs fi show) info from kernel when disk is mounted and from the disks directly when disk is unmounted by default

v3:
rebase on to 20130909
accepts Davids review comments

v2:
In an effort fix checkpath.pl errors as suggested in Wang's review, the two patches as below is merged into one

btrfs-progs: read from the kernel when disk is mounted
btrfs-progs: use lblkid to scan and filesystem show improvements

Anand Jain (2):
btrfs-progs: move out print in cmd_df to another function
btrfs-progs: use kernel for mounted and lblkid to scan disks

 cmds-device.c     |  19 ++-
 cmds-filesystem.c | 354 ++++++++++++++++++++++++++++++++++++++----------------
 ctree.h           |  11 ++
 man/btrfs.8.in    |  17 +--
 utils.c           |  55 ++++++++-
 utils.h           |   5 +
 6 files changed, 338 insertions(+), 123 deletions(-)
Signature · 1 line
1.8.4.rc4.1.g0d8beaa
author
#1 lore raw

PATCH v3 1/2 btrfs-progs: move out print in cmd_df to another function

This is a prepatory work for the btrfs fi show command fixes. So that we have a function get_df to get the fs sizes

v5:
rebase on 20130909
accepts Davids review comments
v4:
fixes checkpatch.pl errors as suggested by Wang
v3:
accepts Zach review comments
v2:
combined the other patches as below and rebase
btrfs-progs: get string for the group profile and type

---
 cmds-filesystem.c | 183 +++++++++++++++++++++++++++++-------------------------
 ctree.h           |  11 ++++
 2 files changed, 108 insertions(+), 86 deletions(-)
cmds-filesystem.c +97 -86
index 815d59a..e1db9e7 100644
@@ -44,32 +44,53 @@ static const char * const cmd_df_usage[] = {
 	NULL
 };
 
-static int cmd_df(int argc, char **argv)
+static char *group_type_str(u64 flag)
 {
-	struct btrfs_ioctl_space_args *sargs, *sargs_orig;
-	u64 count = 0, i;
-	int ret;
-	int fd;
-	int e;
-	char *path;
-	DIR  *dirstream = NULL;
-
-	if (check_argc_exact(argc, 2))
-		usage(cmd_df_usage);
-
-	path = argv[1];
-
-	fd = open_file_or_dir(path, &dirstream);
-	if (fd < 0) {
-		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
-		return 1;
+	switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
+	case BTRFS_BLOCK_GROUP_DATA:
+		return "Data";
+	case BTRFS_BLOCK_GROUP_SYSTEM:
+		return "System";
+	case BTRFS_BLOCK_GROUP_METADATA:
+		return "Metadata";
+	case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
+		return "Data+Metadata";
+	default:
+		return "unknown";
 	}
+}
 
-	sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
-	if (!sargs) {
-		ret = -ENOMEM;
-		goto out;
+static char *group_profile_str(u64 flag)
+{
+	switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
+	case 0:
+		return "single";
+	case BTRFS_BLOCK_GROUP_RAID0:
+		return "RAID0";
+	case BTRFS_BLOCK_GROUP_RAID1:
+		return "RAID1";
+	case BTRFS_BLOCK_GROUP_RAID5:
+		return "RAID5";
+	case BTRFS_BLOCK_GROUP_RAID6:
+		return "RAID6";
+	case BTRFS_BLOCK_GROUP_DUP:
+		return "DUP";
+	case BTRFS_BLOCK_GROUP_RAID10:
+		return "RAID10";
+	default:
+		return "unknown";
 	}
+}
+
+static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
+{
+	u64 count = 0;
+	int ret, e;
+	struct btrfs_ioctl_space_args *sargs;
+
+	sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
+	if (!sargs)
+		return -ENOMEM;
 
 	sargs->space_slots = 0;
 	sargs->total_spaces = 0;
@@ -77,89 +98,79 @@ static int cmd_df(int argc, char **argv)
 	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
 	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
-			path, strerror(e));
-		goto out;
+		fprintf(stderr, "ERROR: couldn't get space info - %s\n",
+			strerror(e));
+		free(sargs);
+		return ret;
 	}
 	if (!sargs->total_spaces) {
-		ret = 0;
-		goto out;
+		free(sargs);
+		return 0;
 	}
-
 	count = sargs->total_spaces;
+	free(sargs);
 
-	sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
+	sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
 			(count * sizeof(struct btrfs_ioctl_space_info)));
-	if (!sargs) {
-		sargs = sargs_orig;
+	if (!sargs)
 		ret = -ENOMEM;
-		goto out;
-	}
 
 	sargs->space_slots = count;
 	sargs->total_spaces = 0;
-
 	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
 	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
-			path, strerror(e));
-		goto out;
+		fprintf(stderr, "ERROR: get space info count %llu - %s\n",
+				count, strerror(e));
+		free(sargs);
+		return ret;
 	}
+	*sargs_ret = sargs;
+	return 0;
+}
 
-	for (i = 0; i < sargs->total_spaces; i++) {
-		char description[80];
-		int written = 0;
-		u64 flags = sargs->spaces[i].flags;
-
-		memset(description, 0, 80);
-
-		if (flags & BTRFS_BLOCK_GROUP_DATA) {
-			if (flags & BTRFS_BLOCK_GROUP_METADATA) {
-				snprintf(description, 14, "%s",
-					 "Data+Metadata");
-				written += 13;
-			} else {
-				snprintf(description, 5, "%s", "Data");
-				written += 4;
-			}
-		} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
-			snprintf(description, 7, "%s", "System");
-			written += 6;
-		} else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
-			snprintf(description, 9, "%s", "Metadata");
-			written += 8;
-		}
+static void print_df(struct btrfs_ioctl_space_args *sargs)
+{
+	u64 i;
+	struct btrfs_ioctl_space_info *sp = sargs->spaces;
+
+	for (i = 0; i < sargs->total_spaces; i++, sp++) {
+		printf("%s, %s: total=%s, used=%s\n",
+			group_type_str(sp->flags),
+			group_profile_str(sp->flags),
+			pretty_size(sp->total_bytes),
+			pretty_size(sp->used_bytes));
+	}
+}
 
-		if (flags & BTRFS_BLOCK_GROUP_RAID0) {
-			snprintf(description+written, 8, "%s", ", RAID0");
-			written += 7;
-		} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
-			snprintf(description+written, 8, "%s", ", RAID1");
-			written += 7;
-		} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
-			snprintf(description+written, 6, "%s", ", DUP");
-			written += 5;
-		} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
-			snprintf(description+written, 9, "%s", ", RAID10");
-			written += 8;
-		} else if (flags & BTRFS_BLOCK_GROUP_RAID5) {
-			snprintf(description+written, 9, "%s", ", RAID5");
-			written += 7;
-		} else if (flags & BTRFS_BLOCK_GROUP_RAID6) {
-			snprintf(description+written, 9, "%s", ", RAID6");
-			written += 7;
-		}
+static int cmd_df(int argc, char **argv)
+{
+	struct btrfs_ioctl_space_args *sargs = NULL;
+	int ret;
+	int fd;
+	char *path;
+	DIR  *dirstream = NULL;
 
-		printf("%s: total=%s, used=%s\n", description,
-			pretty_size(sargs->spaces[i].total_bytes),
-			pretty_size(sargs->spaces[i].used_bytes));
+	if (check_argc_exact(argc, 2))
+		usage(cmd_df_usage);
+
+	path = argv[1];
+
+	fd = open_file_or_dir(path, &dirstream);
+	if (fd < 0) {
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
+		return 1;
 	}
-out:
-	close_file_or_dir(fd, dirstream);
-	free(sargs);
+	ret = get_df(fd, &sargs);
+
+	if (!ret && sargs) {
+		print_df(sargs);
+		free(sargs);
+	} else
+		fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
 
-	return !!ret;
+	close_file_or_dir(fd, dirstream);
+	return ret;
 }
 
 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
ctree.h +11 -0
index 3d6596c..e928a8e 100644
@@ -836,6 +836,17 @@ struct btrfs_csum_item {
 #define BTRFS_BLOCK_GROUP_RAID6    (1ULL << 8)
 #define BTRFS_BLOCK_GROUP_RESERVED	BTRFS_AVAIL_ALLOC_BIT_SINGLE
 
+#define BTRFS_BLOCK_GROUP_TYPE_MASK	(BTRFS_BLOCK_GROUP_DATA |    \
+					 BTRFS_BLOCK_GROUP_SYSTEM |  \
+					 BTRFS_BLOCK_GROUP_METADATA)
+
+#define BTRFS_BLOCK_GROUP_PROFILE_MASK	(BTRFS_BLOCK_GROUP_RAID0 |   \
+					 BTRFS_BLOCK_GROUP_RAID1 |   \
+					 BTRFS_BLOCK_GROUP_RAID5 |   \
+					 BTRFS_BLOCK_GROUP_RAID6 |   \
+					 BTRFS_BLOCK_GROUP_DUP |     \
+					 BTRFS_BLOCK_GROUP_RAID10)
+
 /* used in struct btrfs_balance_args fields */
 #define BTRFS_AVAIL_ALLOC_BIT_SINGLE	(1ULL << 48)
 
Signature · 1 line
1.8.4.rc4.1.g0d8beaa
#2 lore raw

Re: [PATCH 1/2] btrfs-progs: move out print in cmd_df to another function

cmds-filesystem.c static const char * const cmd_df_usage[] = { · line 86

Quoted 2 lines · Anand Jain
cmds-filesystem.c +2 -0
+static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
+{

This function returns full error codes, that's ok,

cmds-filesystem.c static int cmd_df(int argc, char **argv) · line 104

Quoted 11 lines
cmds-filesystem.c +4 -3
@@ -77,89 +98,79 @@ static int cmd_df(int argc, char **argv)
 	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
 	e = errno;
 	if (ret) {
-		fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
-			path, strerror(e));
-		goto out;
+		fprintf(stderr, "ERROR: couldn't get space info - %s\n",
+			strerror(e));
+		free(sargs);
+		return ret;

cmd_df should return the 0-1 normalized value after the cleanup that Wang Shilong has done. You can leave the common exit block (ie. goto + free/return as before) and use return !!ret.

author
#3 lore raw

PATCH v3 2/2 btrfs-progs: use kernel for mounted and lblkid to scan disks

In reply to Anand Jain

As of now btrfs filesystem show reads directly from disks. So sometimes output can be stale, mainly when user wants to cross verify their operation like, label or device delete or add... etc. so this patch will read from the kernel ioctl if it finds that disk is mounted.

Further, to scan for the disks this patch will use lblkid which would replace the our own scan of /proc/partitions

Further, this patch adds new parameters mounted and
group profile info in the filesystem show output
---------------
btrfs fi show
Label: none uuid: a2446ecf-68c5-4815-8b63-099d10fc373c mounted: /btrfs
Group profile: metadata: single metadata: DUP data: single Total devices 1 FS bytes used 32.00KiB
devid 1 size 1.98GiB used 238.25MiB path /dev/mapper/mpatha

Label: none uuid: aea4f4a7-39f2-43dc-bee1-03533551c1a0 (unmounted)
Total devices 1 FS bytes used 28.00KiB
devid 1 size 2.00GiB used 240.75MiB path /dev/mapper/mpathb
------------

v2:
accepts David suggested

---
 cmds-device.c     |  19 ++++--
 cmds-filesystem.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++-------
 man/btrfs.8.in    |  17 +++---
 utils.c           |  55 +++++++++++++++++-
 utils.h           |   5 ++
 5 files changed, 230 insertions(+), 37 deletions(-)
cmds-device.c +13 -6
index 800a050..94ba2f2 100644
@@ -179,7 +179,7 @@ static const char * const cmd_scan_dev_usage[] = {
 static int cmd_scan_dev(int argc, char **argv)
 {
 	int	i, fd, e;
-	int	where = BTRFS_SCAN_PROC;
+	int	where = 0;
 	int	devstart = 1;
 
 	if( argc > 1 && !strcmp(argv[1],"--all-devices")){
@@ -193,14 +193,21 @@ static int cmd_scan_dev(int argc, char **argv)
 	if(argc<=devstart){
 		int ret;
 		printf("Scanning for Btrfs filesystems\n");
-		ret = scan_for_btrfs(where, 1);
-		if (ret){
-			fprintf(stderr, "ERROR: error %d while scanning\n", ret);
-			return 1;
-		}
+		if (where == BTRFS_SCAN_DEV) {
+			ret = scan_for_btrfs(BTRFS_SCAN_DEV,
+						BTRFS_UPDATE_KERNEL);
+			if (ret) {
+				fprintf(stderr,
+					"ERROR: %d while scanning\n", ret);
+				return 1;
+			}
+		} else
+			scan_for_btrfs_v2(BTRFS_UPDATE_KERNEL);
+
 		return 0;
 	}
 
+	/* if its here that means scan the specificed device/file */
 	fd = open("/dev/btrfs-control", O_RDWR);
 	if (fd < 0) {
 		perror("failed to open /dev/btrfs-control");
cmds-filesystem.c +151 -20

209 lines not on this page · raw message

man/btrfs.8.in +9 -8
index 61ffe8d..6a4f5bc 100644
@@ -25,7 +25,7 @@ btrfs \- control a btrfs filesystem
 .PP
 \fBbtrfs\fP \fBfilesystem df\fP\fI <path>\fP
 .PP
-\fBbtrfs\fP \fBfilesystem show\fP [--all-devices|\fI<uuid>\fP|\fI<label>\fP]\fP
+\fBbtrfs\fP \fBfilesystem show\fP [\fI--mounted\fP|\fI--all-devices\fP|\fI<uuid>\fP]\fP
 .PP
 \fBbtrfs\fP \fBfilesystem sync\fP\fI <path> \fP
 .PP
@@ -51,7 +51,7 @@ btrfs \- control a btrfs filesystem
 .PP
 \fBbtrfs\fP \fBdevice delete\fP \fI<device>\fP [\fI<device>...\fP] \fI<path>\fP
 .PP
-\fBbtrfs\fP \fBdevice scan\fP [--all-devices|\fI<device> \fP[\fI<device>...\fP]
+\fBbtrfs\fP \fBdevice scan\fP [\fI--all-devices\fP|\fI<device> \P[\fI<device>...\fP]
 .PP
 \fBbtrfs\fP \fBdevice ready\fP\fI <device>\fP
 .PP
@@ -259,11 +259,12 @@ Show information of a given subvolume in the \fI<path>\fR.
 Show space usage information for a mount point.
 .TP
 
-\fBfilesystem show\fR [--all-devices|\fI<uuid>\fR|\fI<label>\fR]\fR
-Show the btrfs filesystem with some additional info. If no \fIUUID\fP or
-\fIlabel\fP is passed, \fBbtrfs\fR show info of all the btrfs filesystem.
+\fBfilesystem show\fR [\fI--mounted\fP|\fI--all-devices\fP|\fI<uuid>\fR]\fR
+Show the btrfs filesystem with some additional info. If no option or \fIUUID\fP
+is passed, \fBbtrfs\fR shows information of all the btrfs filesystem both mounted
+and unmounted.
+If \fB--mounted\fP is passed, it would probe btrfs kernel to list mounted btrfs filesystem(s);
 If \fB--all-devices\fP is passed, all the devices under /dev are scanned;
-otherwise the devices list is extracted from the /proc/partitions file.
 .TP
 
 \fBfilesystem sync\fR\fI <path> \fR
@@ -396,8 +397,8 @@ Remove device(s) from a filesystem identified by \fI<path>\fR.
 
 \fBdevice scan\fR [--all-devices|\fI<device> \fP[\fI<device>...\fP]\fR
 If one or more devices are passed, these are scanned for a btrfs filesystem. 
-If no devices are passed, \fBbtrfs\fR scans all the block devices listed
-in the /proc/partitions file.
+If no devices are passed, \fBbtrfs\fR uses block devices containing btrfs
+filesystem as listed by blkid.
 Finally, if \fB--all-devices\fP is passed, all the devices under /dev are 
 scanned.
 .TP
utils.c +52 -3

74 lines not on this page · raw message

utils.h +5 -0
index 6c2553a..616bae1 100644
@@ -27,6 +27,9 @@
 
 #define BTRFS_SCAN_PROC	1
 #define BTRFS_SCAN_DEV		2
+#define BTRFS_SCAN_MOUNTED	3
+
+#define BTRFS_UPDATE_KERNEL	1
 
 int make_btrfs(int fd, const char *device, const char *label,
 	       u64 blocks[6], u64 num_bytes, u32 nodesize,
@@ -74,10 +77,12 @@ u64 btrfs_device_size(int fd, struct stat *st);
 #define strncpy_null(dest, src) __strncpy__null(dest, src, sizeof(dest))
 int test_dev_for_mkfs(char *file, int force_overwrite, char *estr);
 int scan_for_btrfs(int where, int update_kernel);
+void scan_for_btrfs_v2(int update_kernel);
 int get_label_mounted(const char *mount_path, char *labelp);
 int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
 	u64 dev_cnt, int mixed, char *estr);
 int is_vol_small(char *file);
 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
 			   int verify);
+int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
 #endif
Signature · 1 line
1.8.4.rc4.1.g0d8beaa
#4 lore raw

Re: [PATCH 2/2] btrfs-progs: use kernel for mounted and lblkid to scan disks

Quoted 2 lines · Anand Jain

Further, this patch adds new parameters mounted and group profile info in the filesystem show output

Sorry to bother you, but the 'further' part of the patch should be separated out. The core change of the patch is to use libblkid and the ioctl.

Quoted 4 lines

---------------
btrfs fi show
Label: none uuid: a2446ecf-68c5-4815-8b63-099d10fc373c mounted: /btrfs
Group profile: metadata: single metadata: DUP data: single

I didn't notice that before, this does not bring much information without the numbers as 'fi df' does. Please drop the printf lines

Quoted 4 lines
cmds-filesystem.c +0 -0
@@ -246,34 +338,73 @@ static int cmd_show(int argc, char **argv)
 	struct list_head *cur_uuid;
 	char *search = NULL;
 	int ret;

cmds-filesystem.c static int cmd_show(int argc, char **argv) · line 349

Quoted 10 lines
cmds-filesystem.c +4 -1
-	int where = BTRFS_SCAN_PROC;
+	int where = 0;
 	int searchstart = 1;
 
 	if( argc > 1 && !strcmp(argv[1],"--all-devices")){
 		where = BTRFS_SCAN_DEV;
 		searchstart += 1;
+	} else if (argc > 1 && !strcmp(argv[1], "--mounted")) {
+		where = BTRFS_SCAN_MOUNTED;
+		searchstart += 1;

getopt was not used here, but with the new options it probably should be, because

Quoted 7 lines
cmds-filesystem.c +0 -7
-	if (check_argc_max(argc, searchstart + 1))
-		usage(cmd_show_usage);
-
-	ret = scan_for_btrfs(where, 0);
-
-	if (ret){
-		fprintf(stderr, "ERROR: error %d while scanning\n", ret);

cmds-filesystem.c static int cmd_show(int argc, char **argv) · line 360

Quoted 10 lines
cmds-filesystem.c +9 -1
-		return 1;
+	if (where == BTRFS_SCAN_DEV) {
+		if (check_argc_max(argc, searchstart + 1))
+			usage(cmd_show_usage);
+	} else if (where == BTRFS_SCAN_MOUNTED) {
+		if (check_argc_max(argc, searchstart))
+			usage(cmd_show_usage);
+	} else {
+		if (check_argc_max(argc, searchstart))
+			usage(cmd_show_usage);

this stops to work:

$ btrfs fi show SOME-UUID
btrfs filesystem show: too many arguments

thanks,
david

author
#5 lore raw

Re: [PATCH 2/2] btrfs-progs: use kernel for mounted and lblkid to scan disks

Quoted 7 lines
Quoted 4 lines

---------------
btrfs fi show
Label: none uuid: a2446ecf-68c5-4815-8b63-099d10fc373c mounted: /btrfs
Group profile: metadata: single metadata: DUP data: single

I didn't notice that before, this does not bring much information without the numbers as 'fi df' does. Please drop the printf lines

Thanks for the review.
IMO group profile is useful info to have it at both the places. end user using this new btrfs fi show would surely notice that it will reduce other commands normally used following the current btrfs fi show command. (like mount and btrfs fi df). of course user should use fi df to know detailed info about the sizes.

-------
btrfs fi show
Label: none uuid: 2c654c36-ba53-4cc8-9c95-f0bee667b9a6 mounted: /btrfs
Group profile: Data+Metadata: single
Total devices 2 FS bytes used 3.17MiB
devid 1 size 1.98GiB used 12.00MiB path /dev/mapper/mpatha
devid 2 size 2.00GiB used 416.00MiB path /dev/mapper/mpathb
-------

Thanks, Anand

#6 lore raw

Re: [PATCH 2/2] btrfs-progs: use kernel for mounted and lblkid to scan disks

Quoted 5 lines · Anand Jain

IMO group profile is useful info to have it at both the places. end user using this new btrfs fi show would surely notice that it will reduce other commands normally used following the current btrfs fi show command. (like mount and btrfs fi df). of course user should use fi df to know detailed info about the sizes.

Yeah, the output of df/show needs reworking, but please do not mix unrelated changes to other patches, although it may be too tempting in this particular case.

david