All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] fi show, dev scan and lblkid
@ 2013-08-16 12:48 Anand Jain
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-16 12:48 UTC (permalink / raw)
  To: linux-btrfs

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

Anand Jain (3):
  btrfs-progs: move out print in cmd_df to another function
  btrfs-progs: read from the kernel when disk is mounted
  btrfs-progs: use lblkid to scan and filesystem show improvements

 cmds-device.c     |   21 +---
 cmds-filesystem.c |  368 +++++++++++++++++++++++++++++++++++------------------
 ctree.h           |   11 ++
 utils.c           |   54 ++++++++-
 utils.h           |    7 +-
 5 files changed, 315 insertions(+), 146 deletions(-)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function
  2013-08-16 12:48 [PATCH 0/3] fi show, dev scan and lblkid Anand Jain
@ 2013-08-16 12:48 ` Anand Jain
  2013-08-19 19:20   ` Zach Brown
                     ` (2 more replies)
  2013-08-16 12:48 ` [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted Anand Jain
  2013-08-16 12:48 ` [PATCH 3/3] btrfs-progs: use lblkid to scan and filesystem show improvements Anand Jain
  2 siblings, 3 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-16 12:48 UTC (permalink / raw)
  To: linux-btrfs

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

v2:
combined the other patches as below and rebase
 btrfs-progs: get string for the group profile and type

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-filesystem.c |  194 +++++++++++++++++++++++++++++++----------------------
 ctree.h           |   11 +++
 2 files changed, 124 insertions(+), 81 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index a4e30ea..be8afde 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -44,28 +44,51 @@ 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];
+	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 "mixed";
+	default:
+		return "unknown";
+	}
+}
 
-	fd = open_file_or_dir(path, &dirstream);
-	if (fd < 0) {
-		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
-		return 12;
+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_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
+	sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
 	if (!sargs)
 		return -ENOMEM;
 
@@ -75,89 +98,98 @@ 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;
+static void print_df(struct btrfs_ioctl_space_args *sargs)
+{
+	char description[80];
+	char *total_bytes;
+	char *used_bytes;
+	u64 flags;
+	u64 i;
+	int written;
+	char g_str[64];
+	int g_sz;
 
+	for (i = 0; i < sargs->total_spaces; i++) {
+		flags = sargs->spaces[i].flags;
+		written = 0;
 		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;
-		}
+		strcpy(g_str, group_type_str(flags));
+		g_sz = strlen(g_str);
+		snprintf(description, g_sz + 1, "%s", g_str);
+		written += g_sz;
 
-		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;
-		}
+		strcpy(g_str, group_profile_str(flags));
+		g_sz = strlen(g_str);
+		snprintf(description+written, g_sz + 3, ", %s", g_str);
+		written += g_sz + 2;
 
-		printf("%s: total=%s, used=%s\n", description,
-			pretty_size(sargs->spaces[i].total_bytes),
-			pretty_size(sargs->spaces[i].used_bytes));
+		total_bytes = pretty_size(sargs->spaces[i].total_bytes);
+		used_bytes = pretty_size(sargs->spaces[i].used_bytes);
+		printf("%s: total=%s, used=%s\n", description, total_bytes,
+		       used_bytes);
 	}
-out:
-	close_file_or_dir(fd, dirstream);
-	free(sargs);
+}
 
-	return 0;
+static int cmd_df(int argc, char **argv)
+{
+	struct btrfs_ioctl_space_args *sargs = NULL;
+	int ret;
+	int fd;
+	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 12;
+	}
+	ret = get_df(fd, &sargs);
+
+	if (!ret && sargs) {
+		print_df(sargs);
+		free(sargs);
+	} else
+		fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
+
+	close_file_or_dir(fd, dirstream);
+	return ret;
 }
 
 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
diff --git a/ctree.h b/ctree.h
index 3c65d0a..3322c68 100644
--- a/ctree.h
+++ b/ctree.h
@@ -828,6 +828,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)
 
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted
  2013-08-16 12:48 [PATCH 0/3] fi show, dev scan and lblkid Anand Jain
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
@ 2013-08-16 12:48 ` Anand Jain
  2013-08-30  2:07   ` Wang Shilong
  2013-08-16 12:48 ` [PATCH 3/3] btrfs-progs: use lblkid to scan and filesystem show improvements Anand Jain
  2 siblings, 1 reply; 10+ messages in thread
From: Anand Jain @ 2013-08-16 12:48 UTC (permalink / raw)
  To: linux-btrfs

As of now btrfs filesystem show reads directly from
disks. So sometimes output can be stale, mainly when
user want to verify their last operation like,
labeling or device delete or add... etc.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-filesystem.c |  166 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 157 insertions(+), 9 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index be8afde..508ba90 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,9 @@
 #include <errno.h>
 #include <uuid/uuid.h>
 #include <ctype.h>
+#include <mntent.h>
+#include <fcntl.h>
+#include <linux/limits.h>
 
 #include "kerncompat.h"
 #include "ctree.h"
@@ -251,8 +254,124 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
 	printf("\n");
 }
 
+/* adds up all the used spaces as reported by the space info ioctl
+ */
+static u64 cal_used_bytes(struct btrfs_ioctl_space_args *si)
+{
+	u64 ret = 0;
+	int i;
+	for (i = 0; i < si->total_spaces; i++)
+		ret += si->spaces[i].used_bytes;
+	return ret;
+}
+
+static int print_one_fs(struct btrfs_ioctl_fs_info_args *fi,
+		struct btrfs_ioctl_dev_info_args *di_n,
+		struct btrfs_ioctl_space_args *si_n, char *label, char *path)
+{
+	int i;
+	char uuidbuf[37];
+	struct btrfs_ioctl_dev_info_args *di = di_n;
+	u64 flags;
+
+	uuid_unparse(fi->fsid, uuidbuf);
+	printf("Label: %s  uuid: %s mounted: %s\n",
+		strlen(label)?label:"none", uuidbuf, path);
+	printf("\tGroup profile:");
+	for (i = si_n->total_spaces - 1; i >= 0; i--) {
+		flags = si_n->spaces[i].flags;
+		if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
+			continue;
+		printf(" %s: %s", group_type_str(flags),
+					group_profile_str(flags));
+		printf(" ");
+	}
+	printf("\n");
+
+	printf("\tTotal devices %llu FS bytes used %s\n",
+				fi->num_devices,
+			pretty_size(cal_used_bytes(si_n)));
+
+	for (i = 0; i < fi->num_devices; i++) {
+		di = (struct btrfs_ioctl_dev_info_args *)&di_n[i];
+		printf("\tdevid    %llu size %s used %s path %s\n",
+			di->devid,
+			pretty_size(di->total_bytes),
+			pretty_size(di->bytes_used),
+			di->path);
+	}
+
+	printf("\n");
+	return 0;
+}
+
+/* This function checks if the given input parameter is
+ * an uuid or a path
+ * return -1: some error in the given input
+ * return 0: unknow input
+ * return 1: given input is uuid
+ * return 2: given input is path
+ */
+static int check_arg_type(char *input, u8 *processed)
+{
+	int ret = 0;
+	if (!uuid_parse(input, processed))
+		ret = 1;
+	else if (realpath(input, (char *)processed))
+		ret = 2;
+	return ret;
+}
+
+static int btrfs_scan_kernel(void *input, int type)
+{
+	int ret = 0, fd;
+	FILE *f;
+	struct mntent *mnt;
+	struct btrfs_ioctl_fs_info_args fi;
+	struct btrfs_ioctl_dev_info_args *di = NULL;
+	struct btrfs_ioctl_space_args *si;
+	char label[BTRFS_LABEL_SIZE];
+
+	if ((f = setmntent ("/proc/mounts", "r")) == NULL)
+		return -errno;
+
+	while ((mnt = getmntent (f)) != NULL) {
+		if (strcmp(mnt->mnt_type, "btrfs"))
+			continue;
+		ret = get_fs_info(mnt->mnt_dir, &fi, &di);
+		if (ret)
+			return ret;
+
+		switch (type) {
+		case 0:
+			break;
+		case 1:
+			if (uuid_compare(fi.fsid, (u8 *)input))
+				continue;
+			break;
+		case 2:
+			if (strcmp(input, mnt->mnt_dir))
+				continue;
+			break;
+		default:
+			break;
+		}
+
+		fd = open(mnt->mnt_dir, O_RDONLY);
+		if (fd > 0 && !get_df(fd, &si)) {
+			get_label_mounted(mnt->mnt_dir, label);
+			print_one_fs(&fi, di, si, label, mnt->mnt_dir);
+			free(si);
+		}
+		if (fd > 0)
+			close(fd);
+		free(di);
+	}
+	return ret;
+}
+
 static const char * const cmd_show_usage[] = {
-	"btrfs filesystem show [--all-devices|<uuid>]",
+	"btrfs filesystem show [--all-devices|--mapper|--kernel|<uuid>]",
 	"Show the structure of a filesystem",
 	"If no argument is given, structure of all present filesystems is shown.",
 	NULL
@@ -264,23 +383,52 @@ static int cmd_show(int argc, char **argv)
 	struct btrfs_fs_devices *fs_devices;
 	struct list_head *cur_uuid;
 	char *search = 0;
-	int ret;
+	int ret = 0;
 	int where = BTRFS_SCAN_PROC;
 	int searchstart = 1;
+	u8 processed[PATH_MAX];
 
-	if( argc > 1 && !strcmp(argv[1],"--all-devices")){
+	if( argc > 1 && !strcmp(argv[1], "--all-devices")){
 		where = BTRFS_SCAN_DEV;
 		searchstart += 1;
+	} else if (argc > 1 && !strcmp(argv[1], "--kernel")) {
+		where = 0;
+		searchstart += 1;
 	}
 
-	if (check_argc_max(argc, searchstart + 1))
-		usage(cmd_show_usage);
+	if (!where) {
+		/* option --kernel*/
+		if (! (searchstart < argc))
+			ret = btrfs_scan_kernel(NULL, 0);
+
+		while (searchstart < argc) {
+			ret = check_arg_type(argv[searchstart], processed);
+			if (ret < 0) {
+				fprintf(stderr, "ERROR at input %s\n",
+							argv[searchstart]);
+				return 1;
+			}
+			if (!ret) {
+				fprintf(stderr, "ERROR unknown %s\n",
+					argv[searchstart]);
+				return 1;
+			}
 
-	ret = scan_for_btrfs(where, 0);
+			ret = btrfs_scan_kernel(processed, ret);
+			if (ret)
+				break;
+			searchstart++;
+		}
+		if (ret)
+			fprintf(stderr, "ERROR: scan kernel failed, %d\n",
+				ret);
+		return ret;
+	}
 
-	if (ret){
-		fprintf(stderr, "ERROR: error %d while scanning\n", ret);
-		return 18;
+	ret = scan_for_btrfs(where, 0);
+	if (ret) {
+		fprintf(stderr, "ERROR: %d while scanning\n", ret);
+		return 1;
 	}
 	
 	if(searchstart < argc)
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/3] btrfs-progs: use lblkid to scan and filesystem show improvements
  2013-08-16 12:48 [PATCH 0/3] fi show, dev scan and lblkid Anand Jain
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
  2013-08-16 12:48 ` [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted Anand Jain
@ 2013-08-16 12:48 ` Anand Jain
  2 siblings, 0 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-16 12:48 UTC (permalink / raw)
  To: linux-btrfs

create helper functions to use lblkid to scan for the
btrfs disks

When we are using the lblkid for scanning for btrfs disks,
we don't have to worry about the manually scanning the
/dev or /dev/mapper which means we don't need the
--all-devices (and proposed --mapper) options.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-device.c     |   21 ++-------
 cmds-filesystem.c |  126 +++++++++++++---------------------------------------
 utils.c           |   54 +++++++++++++++++++++-
 utils.h           |    7 +++-
 4 files changed, 93 insertions(+), 115 deletions(-)

diff --git a/cmds-device.c b/cmds-device.c
index be2aaff..833ad30 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -186,7 +186,7 @@ static int cmd_rm_dev(int argc, char **argv)
 }
 
 static const char * const cmd_scan_dev_usage[] = {
-	"btrfs device scan [<--all-devices>|<device> [<device>...]]",
+	"btrfs device scan [<device>...]",
 	"Scan devices for a btrfs filesystem",
 	NULL
 };
@@ -194,28 +194,15 @@ 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	devstart = 1;
 
-	if( argc > 1 && !strcmp(argv[1],"--all-devices")){
-		if (check_argc_max(argc, 2))
-			usage(cmd_scan_dev_usage);
-
-		where = BTRFS_SCAN_DEV;
-		devstart += 1;
-	}
-
-	if(argc<=devstart){
-		int ret;
+	if (argc <= devstart) {
 		printf("Scanning for Btrfs filesystems\n");
-		ret = scan_for_btrfs(where, 1);
-		if (ret){
-			fprintf(stderr, "ERROR: error %d while scanning\n", ret);
-			return 18;
-		}
+		scan_for_btrfs_v2(BTRFS_UPDATE_KERNEL);
 		return 0;
 	}
 
+	/* there are specific device/file to scan*/
 	fd = open("/dev/btrfs-control", O_RDWR);
 	if (fd < 0) {
 		perror("failed to open /dev/btrfs-control");
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 508ba90..9a325ba 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -195,27 +195,6 @@ static int cmd_df(int argc, char **argv)
 	return ret;
 }
 
-static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
-{
-	char uuidbuf[37];
-	struct list_head *cur;
-	struct btrfs_device *device;
-	int search_len = strlen(search);
-
-	search_len = min(search_len, 37);
-	uuid_unparse(fs_devices->fsid, uuidbuf);
-	if (!strncmp(uuidbuf, search, search_len))
-		return 1;
-
-	list_for_each(cur, &fs_devices->devices) {
-		device = list_entry(cur, struct btrfs_device, dev_list);
-		if ((device->label && strcmp(device->label, search) == 0) ||
-		    strcmp(device->name, search) == 0)
-			return 1;
-	}
-	return 0;
-}
-
 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
 {
 	char uuidbuf[37];
@@ -232,10 +211,10 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
 	else
 		printf("Label: none ");
 
-
 	total = device->total_devs;
-	printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
-	       (unsigned long long)total,
+
+	printf(" uuid: %s (unmounted)\n\tTotal devices %llu FS bytes used %s\n",
+		uuidbuf, (unsigned long long)total,
 	       pretty_size(device->super_bytes_used));
 
 	list_for_each(cur, &fs_devices->devices) {
@@ -305,23 +284,6 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fi,
 	return 0;
 }
 
-/* This function checks if the given input parameter is
- * an uuid or a path
- * return -1: some error in the given input
- * return 0: unknow input
- * return 1: given input is uuid
- * return 2: given input is path
- */
-static int check_arg_type(char *input, u8 *processed)
-{
-	int ret = 0;
-	if (!uuid_parse(input, processed))
-		ret = 1;
-	else if (realpath(input, (char *)processed))
-		ret = 2;
-	return ret;
-}
-
 static int btrfs_scan_kernel(void *input, int type)
 {
 	int ret = 0, fd;
@@ -371,7 +333,7 @@ static int btrfs_scan_kernel(void *input, int type)
 }
 
 static const char * const cmd_show_usage[] = {
-	"btrfs filesystem show [--all-devices|--mapper|--kernel|<uuid>]",
+	"btrfs filesystem show [--mounted]",
 	"Show the structure of a filesystem",
 	"If no argument is given, structure of all present filesystems is shown.",
 	NULL
@@ -382,65 +344,41 @@ static int cmd_show(int argc, char **argv)
 	struct list_head *all_uuids;
 	struct btrfs_fs_devices *fs_devices;
 	struct list_head *cur_uuid;
-	char *search = 0;
 	int ret = 0;
-	int where = BTRFS_SCAN_PROC;
-	int searchstart = 1;
-	u8 processed[PATH_MAX];
-
-	if( argc > 1 && !strcmp(argv[1], "--all-devices")){
-		where = BTRFS_SCAN_DEV;
-		searchstart += 1;
-	} else if (argc > 1 && !strcmp(argv[1], "--kernel")) {
-		where = 0;
-		searchstart += 1;
-	}
+	int where = 0;
 
-	if (!where) {
-		/* option --kernel*/
-		if (! (searchstart < argc))
-			ret = btrfs_scan_kernel(NULL, 0);
-
-		while (searchstart < argc) {
-			ret = check_arg_type(argv[searchstart], processed);
-			if (ret < 0) {
-				fprintf(stderr, "ERROR at input %s\n",
-							argv[searchstart]);
-				return 1;
-			}
-			if (!ret) {
-				fprintf(stderr, "ERROR unknown %s\n",
-					argv[searchstart]);
-				return 1;
-			}
+	if (check_argc_max(argc, 2))
+		usage(cmd_show_usage);
 
-			ret = btrfs_scan_kernel(processed, ret);
-			if (ret)
-				break;
-			searchstart++;
-		}
+	/* show only mounted btrfs disks */
+	if (argc > 1 && !strcmp(argv[1], "--mounted"))
+		where = BTRFS_SCAN_MOUNTED;
+
+	switch (where) {
+	case 0:
+		/* no option : show both mounted and unmounted
+		 */
+		/* mounted */
+		ret = btrfs_scan_kernel(NULL, 0);
 		if (ret)
 			fprintf(stderr, "ERROR: scan kernel failed, %d\n",
 				ret);
-		return ret;
-	}
 
-	ret = scan_for_btrfs(where, 0);
-	if (ret) {
-		fprintf(stderr, "ERROR: %d while scanning\n", ret);
-		return 1;
-	}
-	
-	if(searchstart < argc)
-		search = argv[searchstart];
-
-	all_uuids = btrfs_scanned_uuids();
-	list_for_each(cur_uuid, all_uuids) {
-		fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
+		/* unmounted */
+		scan_for_btrfs_v2(!BTRFS_UPDATE_KERNEL);
+		all_uuids = btrfs_scanned_uuids();
+		list_for_each(cur_uuid, all_uuids) {
+			fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
 					list);
-		if (search && uuid_search(fs_devices, search) == 0)
-			continue;
-		print_one_uuid(fs_devices);
+			print_one_uuid(fs_devices);
+		}
+		break;
+	case BTRFS_SCAN_MOUNTED:
+		ret = btrfs_scan_kernel(NULL, 0);
+		if (ret)
+			fprintf(stderr, "ERROR: scan kernel failed, %d\n",
+				ret);
+		break;
 	}
 	printf("%s\n", BTRFS_BUILD_VERSION);
 	return 0;
diff --git a/utils.c b/utils.c
index 86ee948..c26193e 100644
--- a/utils.c
+++ b/utils.c
@@ -680,9 +680,8 @@ int is_block_device(const char *path) {
  * Find the mount point for a mounted device.
  * On success, returns 0 with mountpoint in *mp.
  * On failure, returns -errno (not mounted yields -EINVAL)
- * Is noisy on failures, expects to be given a mounted device.
  */
-static int get_btrfs_mount(const char *dev, char *mp, size_t mp_size) {
+int get_btrfs_mount(const char *dev, char *mp, size_t mp_size) {
 	int ret;
 	int fd = -1;
 
@@ -707,7 +706,6 @@ static int get_btrfs_mount(const char *dev, char *mp, size_t mp_size) {
 
 	ret = check_mounted_where(fd, dev, mp, mp_size, NULL);
 	if (!ret) {
-		fprintf(stderr, "%s is not a mounted btrfs device\n", dev);
 		ret = -EINVAL;
 	} else { /* mounted, all good */
 		ret = 0;
@@ -1133,6 +1131,56 @@ fail:
 	return ret;
 }
 
+int test_skip_this_disk(char *path)
+{
+	int fd;
+	/* this will eliminate disks which are mounted (btrfs)
+	 * and non-dm disk path when dm is enabled
+	 */
+	fd = open(path, O_RDWR|O_EXCL);
+	if (fd < 0)
+		return 1;
+	close(fd);
+	return 0;
+}
+
+void scan_for_btrfs_v2(int update_kernel)
+{
+	int fd = -1;
+	u64 num_devices;
+	struct btrfs_fs_devices *tmp_devices;
+	blkid_dev_iterate iter = NULL;
+	blkid_dev dev = NULL;
+	blkid_cache cache = NULL;
+	char path[PATH_MAX];
+
+	if (blkid_get_cache(&cache, 0) < 0) {
+		printf("ERROR: lblkid cache get failed\n");
+		return;
+	}
+	blkid_probe_all(cache);
+	iter = blkid_dev_iterate_begin(cache);
+	blkid_dev_set_search(iter, "TYPE", "btrfs");
+	while (blkid_dev_next(iter, &dev) == 0) {
+		dev = blkid_verify(cache, dev);
+		if (!dev)
+			continue;
+		/* if we are here its definitly a btrfs disk*/
+		strcpy(path, blkid_dev_devname(dev));
+		if (test_skip_this_disk(path))
+			continue;
+
+		fd = open(path, O_RDONLY);
+		btrfs_scan_one_device(fd, path, &tmp_devices,
+				&num_devices, BTRFS_SUPER_INFO_OFFSET);
+		close(fd);
+		fd = -1;
+		if (update_kernel)
+			btrfs_register_one_device(path);
+	}
+	blkid_dev_iterate_end(iter);
+}
+
 int btrfs_scan_for_fsid(int run_ioctls)
 {
 	int ret;
diff --git a/utils.h b/utils.h
index ec06a82..4b449b8 100644
--- a/utils.h
+++ b/utils.h
@@ -25,8 +25,11 @@
 
 #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024)
 
-#define BTRFS_SCAN_PROC	1
+#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,7 +77,9 @@ 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 get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
 #endif
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
@ 2013-08-19 19:20   ` Zach Brown
  2013-08-20  6:17     ` Anand Jain
  2013-08-20  6:16   ` [PATCH] btrfs-progs: v3, " Anand Jain
  2013-08-30  2:01   ` [PATCH 1/3] btrfs-progs: " Wang Shilong
  2 siblings, 1 reply; 10+ messages in thread
From: Zach Brown @ 2013-08-19 19:20 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

> +static void print_df(struct btrfs_ioctl_space_args *sargs)
> +{
> +	char description[80];
> +	char *total_bytes;
> +	char *used_bytes;
> +	u64 flags;
> +	u64 i;
> +	int written;
> +	char g_str[64];
> +	int g_sz;
>  
> +	for (i = 0; i < sargs->total_spaces; i++) {
> +		flags = sargs->spaces[i].flags;
> +		written = 0;
>  		memset(description, 0, 80);
>  
> +		strcpy(g_str, group_type_str(flags));
> +		g_sz = strlen(g_str);
> +		snprintf(description, g_sz + 1, "%s", g_str);
> +		written += g_sz;

> +		strcpy(g_str, group_profile_str(flags));
> +		g_sz = strlen(g_str);
> +		snprintf(description+written, g_sz + 3, ", %s", g_str);
> +		written += g_sz + 2;

> +		total_bytes = pretty_size(sargs->spaces[i].total_bytes);
> +		used_bytes = pretty_size(sargs->spaces[i].used_bytes);
> +		printf("%s: total=%s, used=%s\n", description, total_bytes,
> +		       used_bytes);

This seems to be a whole lot of code for a simple iterating printf:

	struct space_type_thingy_whatver *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));
	}

- z

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] btrfs-progs: v3, move out print in cmd_df to another function
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
  2013-08-19 19:20   ` Zach Brown
@ 2013-08-20  6:16   ` Anand Jain
  2013-08-30  2:01   ` [PATCH 1/3] btrfs-progs: " Wang Shilong
  2 siblings, 0 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-20  6:16 UTC (permalink / raw)
  To: linux-btrfs

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

v3:
 accepts Zach review comments
v2:
combined the other patches as below and rebase
 btrfs-progs: get string for the group profile and type

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-filesystem.c |  181 ++++++++++++++++++++++++++++------------------------
 ctree.h           |   11 +++
 2 files changed, 108 insertions(+), 84 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index a4e30ea..d06de35 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -44,28 +44,51 @@ 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];
+	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 "mixed";
+	default:
+		return "unknown";
+	}
+}
 
-	fd = open_file_or_dir(path, &dirstream);
-	if (fd < 0) {
-		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
-		return 12;
+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";
 	}
+}
 
-	sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
+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;
 
@@ -75,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 12;
 	}
-out:
-	close_file_or_dir(fd, dirstream);
-	free(sargs);
+	ret = get_df(fd, &sargs);
 
-	return 0;
+	if (!ret && sargs) {
+		print_df(sargs);
+		free(sargs);
+	} else
+		fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
+
+	close_file_or_dir(fd, dirstream);
+	return ret;
 }
 
 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
diff --git a/ctree.h b/ctree.h
index 3c65d0a..3322c68 100644
--- a/ctree.h
+++ b/ctree.h
@@ -828,6 +828,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)
 
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function
  2013-08-19 19:20   ` Zach Brown
@ 2013-08-20  6:17     ` Anand Jain
  0 siblings, 0 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-20  6:17 UTC (permalink / raw)
  To: Zach Brown; +Cc: linux-btrfs



> This seems to be a whole lot of code for a simple iterating printf:
>
> 	struct space_type_thingy_whatver *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));
> 	}
>
> - z


  Thanks for the review. Sent v3 for this.

Anand

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function
  2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
  2013-08-19 19:20   ` Zach Brown
  2013-08-20  6:16   ` [PATCH] btrfs-progs: v3, " Anand Jain
@ 2013-08-30  2:01   ` Wang Shilong
  2013-08-30  2:28     ` Anand Jain
  2 siblings, 1 reply; 10+ messages in thread
From: Wang Shilong @ 2013-08-30  2:01 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On 08/16/2013 08:48 PM, Anand Jain wrote:

Hello Anand,

We'd appreciate you use checkpatch.pl to check coding
style before sending patches.

For this patch:

ERROR: "foo * bar" should be "foo *bar"
#35: FILE: cmds-filesystem.c:47:
+static char * group_type_str(u64 flag)

ERROR: "foo * bar" should be "foo *bar"
#67: FILE: cmds-filesystem.c:63:
+static char * group_profile_str(u64 flag)

total: 2 errors, 0 warnings, 245 lines checked


> This is a prepatory work for the following btrfs fi show command
> fixes. So that we have a function get_df to get the fs sizes
>
> v2:
> combined the other patches as below and rebase
>   btrfs-progs: get string for the group profile and type
>
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
>   cmds-filesystem.c |  194 +++++++++++++++++++++++++++++++----------------------
>   ctree.h           |   11 +++
>   2 files changed, 124 insertions(+), 81 deletions(-)
>
> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
> index a4e30ea..be8afde 100644
> --- a/cmds-filesystem.c
> +++ b/cmds-filesystem.c
> @@ -44,28 +44,51 @@ 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];
> +	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 "mixed";
> +	default:
> +		return "unknown";
> +	}
> +}
>   
> -	fd = open_file_or_dir(path, &dirstream);
> -	if (fd < 0) {
> -		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
> -		return 12;
> +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_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
> +	sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
>   	if (!sargs)
>   		return -ENOMEM;
>   
> @@ -75,89 +98,98 @@ 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;
> +static void print_df(struct btrfs_ioctl_space_args *sargs)
> +{
> +	char description[80];
> +	char *total_bytes;
> +	char *used_bytes;
> +	u64 flags;
> +	u64 i;
> +	int written;
> +	char g_str[64];
> +	int g_sz;
>   
> +	for (i = 0; i < sargs->total_spaces; i++) {
> +		flags = sargs->spaces[i].flags;
> +		written = 0;
>   		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;
> -		}
> +		strcpy(g_str, group_type_str(flags));
> +		g_sz = strlen(g_str);
> +		snprintf(description, g_sz + 1, "%s", g_str);
> +		written += g_sz;
>   
> -		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;
> -		}
> +		strcpy(g_str, group_profile_str(flags));
> +		g_sz = strlen(g_str);
> +		snprintf(description+written, g_sz + 3, ", %s", g_str);
> +		written += g_sz + 2;
>   
> -		printf("%s: total=%s, used=%s\n", description,
> -			pretty_size(sargs->spaces[i].total_bytes),
> -			pretty_size(sargs->spaces[i].used_bytes));
> +		total_bytes = pretty_size(sargs->spaces[i].total_bytes);
> +		used_bytes = pretty_size(sargs->spaces[i].used_bytes);
> +		printf("%s: total=%s, used=%s\n", description, total_bytes,
> +		       used_bytes);
>   	}
> -out:
> -	close_file_or_dir(fd, dirstream);
> -	free(sargs);
> +}
>   
> -	return 0;
> +static int cmd_df(int argc, char **argv)
> +{
> +	struct btrfs_ioctl_space_args *sargs = NULL;
> +	int ret;
> +	int fd;
> +	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 12;
> +	}
> +	ret = get_df(fd, &sargs);
> +
> +	if (!ret && sargs) {
> +		print_df(sargs);
> +		free(sargs);
> +	} else
> +		fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
> +
> +	close_file_or_dir(fd, dirstream);
> +	return ret;
>   }
>   
>   static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
> diff --git a/ctree.h b/ctree.h
> index 3c65d0a..3322c68 100644
> --- a/ctree.h
> +++ b/ctree.h
> @@ -828,6 +828,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)
>   


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted
  2013-08-16 12:48 ` [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted Anand Jain
@ 2013-08-30  2:07   ` Wang Shilong
  0 siblings, 0 replies; 10+ messages in thread
From: Wang Shilong @ 2013-08-30  2:07 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On 08/16/2013 08:48 PM, Anand Jain wrote:

ERROR: spaces required around that '?' (ctx:VxV)
#63: FILE: cmds-filesystem.c:279:
+        strlen(label)?label:"none", uuidbuf, path);
                       ^

ERROR: spaces required around that ':' (ctx:VxV)
#63: FILE: cmds-filesystem.c:279:
+        strlen(label)?label:"none", uuidbuf, path);
                             ^

WARNING: space prohibited between function name and open parenthesis '('
#119: FILE: cmds-filesystem.c:335:
+    if ((f = setmntent ("/proc/mounts", "r")) == NULL)

ERROR: do not use assignment in if condition
#119: FILE: cmds-filesystem.c:335:
+    if ((f = setmntent ("/proc/mounts", "r")) == NULL)

WARNING: space prohibited between function name and open parenthesis '('
#122: FILE: cmds-filesystem.c:338:
+    while ((mnt = getmntent (f)) != NULL) {

ERROR: space required before the open brace '{'
#174: FILE: cmds-filesystem.c:391:
+    if( argc > 1 && !strcmp(argv[1], "--all-devices")){

ERROR: space prohibited after that open parenthesis '('
#174: FILE: cmds-filesystem.c:391:
+    if( argc > 1 && !strcmp(argv[1], "--all-devices")){

ERROR: space required before the open parenthesis '('
#174: FILE: cmds-filesystem.c:391:
+    if( argc > 1 && !strcmp(argv[1], "--all-devices")){

ERROR: space prohibited after that '!' (ctx:BxW)
#186: FILE: cmds-filesystem.c:401:
+        if (! (searchstart < argc))
              ^

total: 7 errors, 2 warnings, 194 lines checked

> As of now btrfs filesystem show reads directly from
> disks. So sometimes output can be stale, mainly when
> user want to verify their last operation like,
> labeling or device delete or add... etc.
>
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
>   cmds-filesystem.c |  166 ++++++++++++++++++++++++++++++++++++++++++++++++++---
>   1 files changed, 157 insertions(+), 9 deletions(-)
>
> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
> index be8afde..508ba90 100644
> --- a/cmds-filesystem.c
> +++ b/cmds-filesystem.c
> @@ -22,6 +22,9 @@
>   #include <errno.h>
>   #include <uuid/uuid.h>
>   #include <ctype.h>
> +#include <mntent.h>
> +#include <fcntl.h>
> +#include <linux/limits.h>
>   
>   #include "kerncompat.h"
>   #include "ctree.h"
> @@ -251,8 +254,124 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
>   	printf("\n");
>   }
>   
> +/* adds up all the used spaces as reported by the space info ioctl
> + */
> +static u64 cal_used_bytes(struct btrfs_ioctl_space_args *si)
> +{
> +	u64 ret = 0;
> +	int i;
> +	for (i = 0; i < si->total_spaces; i++)
> +		ret += si->spaces[i].used_bytes;
> +	return ret;
> +}
> +
> +static int print_one_fs(struct btrfs_ioctl_fs_info_args *fi,
> +		struct btrfs_ioctl_dev_info_args *di_n,
> +		struct btrfs_ioctl_space_args *si_n, char *label, char *path)
> +{
> +	int i;
> +	char uuidbuf[37];
> +	struct btrfs_ioctl_dev_info_args *di = di_n;
> +	u64 flags;
> +
> +	uuid_unparse(fi->fsid, uuidbuf);
> +	printf("Label: %s  uuid: %s mounted: %s\n",
> +		strlen(label)?label:"none", uuidbuf, path);
> +	printf("\tGroup profile:");
> +	for (i = si_n->total_spaces - 1; i >= 0; i--) {
> +		flags = si_n->spaces[i].flags;
> +		if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
> +			continue;
> +		printf(" %s: %s", group_type_str(flags),
> +					group_profile_str(flags));
> +		printf(" ");
> +	}
> +	printf("\n");
> +
> +	printf("\tTotal devices %llu FS bytes used %s\n",
> +				fi->num_devices,
> +			pretty_size(cal_used_bytes(si_n)));
> +
> +	for (i = 0; i < fi->num_devices; i++) {
> +		di = (struct btrfs_ioctl_dev_info_args *)&di_n[i];
> +		printf("\tdevid    %llu size %s used %s path %s\n",
> +			di->devid,
> +			pretty_size(di->total_bytes),
> +			pretty_size(di->bytes_used),
> +			di->path);
> +	}
> +
> +	printf("\n");
> +	return 0;
> +}
> +
> +/* This function checks if the given input parameter is
> + * an uuid or a path
> + * return -1: some error in the given input
> + * return 0: unknow input
> + * return 1: given input is uuid
> + * return 2: given input is path
> + */
> +static int check_arg_type(char *input, u8 *processed)
> +{
> +	int ret = 0;
> +	if (!uuid_parse(input, processed))
> +		ret = 1;
> +	else if (realpath(input, (char *)processed))
> +		ret = 2;
> +	return ret;
> +}
> +
> +static int btrfs_scan_kernel(void *input, int type)
> +{
> +	int ret = 0, fd;
> +	FILE *f;
> +	struct mntent *mnt;
> +	struct btrfs_ioctl_fs_info_args fi;
> +	struct btrfs_ioctl_dev_info_args *di = NULL;
> +	struct btrfs_ioctl_space_args *si;
> +	char label[BTRFS_LABEL_SIZE];
> +
> +	if ((f = setmntent ("/proc/mounts", "r")) == NULL)
> +		return -errno;
> +
> +	while ((mnt = getmntent (f)) != NULL) {
> +		if (strcmp(mnt->mnt_type, "btrfs"))
> +			continue;
> +		ret = get_fs_info(mnt->mnt_dir, &fi, &di);
> +		if (ret)
> +			return ret;
> +
> +		switch (type) {
> +		case 0:
> +			break;
> +		case 1:
> +			if (uuid_compare(fi.fsid, (u8 *)input))
> +				continue;
> +			break;
> +		case 2:
> +			if (strcmp(input, mnt->mnt_dir))
> +				continue;
> +			break;
> +		default:
> +			break;
> +		}
> +
> +		fd = open(mnt->mnt_dir, O_RDONLY);
> +		if (fd > 0 && !get_df(fd, &si)) {
> +			get_label_mounted(mnt->mnt_dir, label);
> +			print_one_fs(&fi, di, si, label, mnt->mnt_dir);
> +			free(si);
> +		}
> +		if (fd > 0)
> +			close(fd);
> +		free(di);
> +	}
> +	return ret;
> +}
> +
>   static const char * const cmd_show_usage[] = {
> -	"btrfs filesystem show [--all-devices|<uuid>]",
> +	"btrfs filesystem show [--all-devices|--mapper|--kernel|<uuid>]",
>   	"Show the structure of a filesystem",
>   	"If no argument is given, structure of all present filesystems is shown.",
>   	NULL
> @@ -264,23 +383,52 @@ static int cmd_show(int argc, char **argv)
>   	struct btrfs_fs_devices *fs_devices;
>   	struct list_head *cur_uuid;
>   	char *search = 0;
> -	int ret;
> +	int ret = 0;
>   	int where = BTRFS_SCAN_PROC;
>   	int searchstart = 1;
> +	u8 processed[PATH_MAX];
>   
> -	if( argc > 1 && !strcmp(argv[1],"--all-devices")){
> +	if( argc > 1 && !strcmp(argv[1], "--all-devices")){
>   		where = BTRFS_SCAN_DEV;
>   		searchstart += 1;
> +	} else if (argc > 1 && !strcmp(argv[1], "--kernel")) {
> +		where = 0;
> +		searchstart += 1;
>   	}
>   
> -	if (check_argc_max(argc, searchstart + 1))
> -		usage(cmd_show_usage);
> +	if (!where) {
> +		/* option --kernel*/
> +		if (! (searchstart < argc))
> +			ret = btrfs_scan_kernel(NULL, 0);
> +
> +		while (searchstart < argc) {
> +			ret = check_arg_type(argv[searchstart], processed);
> +			if (ret < 0) {
> +				fprintf(stderr, "ERROR at input %s\n",
> +							argv[searchstart]);
> +				return 1;
> +			}
> +			if (!ret) {
> +				fprintf(stderr, "ERROR unknown %s\n",
> +					argv[searchstart]);
> +				return 1;
> +			}
>   
> -	ret = scan_for_btrfs(where, 0);
> +			ret = btrfs_scan_kernel(processed, ret);
> +			if (ret)
> +				break;
> +			searchstart++;
> +		}
> +		if (ret)
> +			fprintf(stderr, "ERROR: scan kernel failed, %d\n",
> +				ret);
> +		return ret;
> +	}
>   
> -	if (ret){
> -		fprintf(stderr, "ERROR: error %d while scanning\n", ret);
> -		return 18;
> +	ret = scan_for_btrfs(where, 0);
> +	if (ret) {
> +		fprintf(stderr, "ERROR: %d while scanning\n", ret);
> +		return 1;
>   	}
>   	
>   	if(searchstart < argc)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function
  2013-08-30  2:01   ` [PATCH 1/3] btrfs-progs: " Wang Shilong
@ 2013-08-30  2:28     ` Anand Jain
  0 siblings, 0 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-30  2:28 UTC (permalink / raw)
  To: Wang Shilong; +Cc: linux-btrfs


Hi Wang,

  apologies it didn't pass checkpatch.pl. will fix them.

Thanks, Anand


On 08/30/2013 10:01 AM, Wang Shilong wrote:
> On 08/16/2013 08:48 PM, Anand Jain wrote:
>
> Hello Anand,
>
> We'd appreciate you use checkpatch.pl to check coding
> style before sending patches.
>
> For this patch:
>
> ERROR: "foo * bar" should be "foo *bar"
> #35: FILE: cmds-filesystem.c:47:
> +static char * group_type_str(u64 flag)
>
> ERROR: "foo * bar" should be "foo *bar"
> #67: FILE: cmds-filesystem.c:63:
> +static char * group_profile_str(u64 flag)
>
> total: 2 errors, 0 warnings, 245 lines checked
>
>
>> This is a prepatory work for the following btrfs fi show command
>> fixes. So that we have a function get_df to get the fs sizes
>>
>> v2:
>> combined the other patches as below and rebase
>>   btrfs-progs: get string for the group profile and type
>>
>> Signed-off-by: Anand Jain <anand.jain@oracle.com>
>> ---
>>   cmds-filesystem.c |  194
>> +++++++++++++++++++++++++++++++----------------------
>>   ctree.h           |   11 +++
>>   2 files changed, 124 insertions(+), 81 deletions(-)
>>
>> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
>> index a4e30ea..be8afde 100644
>> --- a/cmds-filesystem.c
>> +++ b/cmds-filesystem.c
>> @@ -44,28 +44,51 @@ 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];
>> +    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 "mixed";
>> +    default:
>> +        return "unknown";
>> +    }
>> +}
>> -    fd = open_file_or_dir(path, &dirstream);
>> -    if (fd < 0) {
>> -        fprintf(stderr, "ERROR: can't access to '%s'\n", path);
>> -        return 12;
>> +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_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
>> +    sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
>>       if (!sargs)
>>           return -ENOMEM;
>> @@ -75,89 +98,98 @@ 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;
>> +static void print_df(struct btrfs_ioctl_space_args *sargs)
>> +{
>> +    char description[80];
>> +    char *total_bytes;
>> +    char *used_bytes;
>> +    u64 flags;
>> +    u64 i;
>> +    int written;
>> +    char g_str[64];
>> +    int g_sz;
>> +    for (i = 0; i < sargs->total_spaces; i++) {
>> +        flags = sargs->spaces[i].flags;
>> +        written = 0;
>>           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;
>> -        }
>> +        strcpy(g_str, group_type_str(flags));
>> +        g_sz = strlen(g_str);
>> +        snprintf(description, g_sz + 1, "%s", g_str);
>> +        written += g_sz;
>> -        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;
>> -        }
>> +        strcpy(g_str, group_profile_str(flags));
>> +        g_sz = strlen(g_str);
>> +        snprintf(description+written, g_sz + 3, ", %s", g_str);
>> +        written += g_sz + 2;
>> -        printf("%s: total=%s, used=%s\n", description,
>> -            pretty_size(sargs->spaces[i].total_bytes),
>> -            pretty_size(sargs->spaces[i].used_bytes));
>> +        total_bytes = pretty_size(sargs->spaces[i].total_bytes);
>> +        used_bytes = pretty_size(sargs->spaces[i].used_bytes);
>> +        printf("%s: total=%s, used=%s\n", description, total_bytes,
>> +               used_bytes);
>>       }
>> -out:
>> -    close_file_or_dir(fd, dirstream);
>> -    free(sargs);
>> +}
>> -    return 0;
>> +static int cmd_df(int argc, char **argv)
>> +{
>> +    struct btrfs_ioctl_space_args *sargs = NULL;
>> +    int ret;
>> +    int fd;
>> +    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 12;
>> +    }
>> +    ret = get_df(fd, &sargs);
>> +
>> +    if (!ret && sargs) {
>> +        print_df(sargs);
>> +        free(sargs);
>> +    } else
>> +        fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
>> +
>> +    close_file_or_dir(fd, dirstream);
>> +    return ret;
>>   }
>>   static int uuid_search(struct btrfs_fs_devices *fs_devices, char
>> *search)
>> diff --git a/ctree.h b/ctree.h
>> index 3c65d0a..3322c68 100644
>> --- a/ctree.h
>> +++ b/ctree.h
>> @@ -828,6 +828,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)
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-08-30  2:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-16 12:48 [PATCH 0/3] fi show, dev scan and lblkid Anand Jain
2013-08-16 12:48 ` [PATCH 1/3] btrfs-progs: move out print in cmd_df to another function Anand Jain
2013-08-19 19:20   ` Zach Brown
2013-08-20  6:17     ` Anand Jain
2013-08-20  6:16   ` [PATCH] btrfs-progs: v3, " Anand Jain
2013-08-30  2:01   ` [PATCH 1/3] btrfs-progs: " Wang Shilong
2013-08-30  2:28     ` Anand Jain
2013-08-16 12:48 ` [PATCH 2/3] btrfs-progs: read from the kernel when disk is mounted Anand Jain
2013-08-30  2:07   ` Wang Shilong
2013-08-16 12:48 ` [PATCH 3/3] btrfs-progs: use lblkid to scan and filesystem show improvements Anand Jain

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.