linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 13/13 v3] btrfs-progs: introduce btrfs filesystem show --kernel
Date: Fri, 21 Jun 2013 15:58:05 +0800	[thread overview]
Message-ID: <1371801485-14571-6-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1371801485-14571-1-git-send-email-anand.jain@oracle.com>

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.

This patch adds --kernel option to the 'filesystem show'
subcli, which will read from the kernel instead of
the disks directly.

This btrfs-progs patch depends on the kernel patch
 btrfs: obtain used_bytes in BTRFS_IOC_FS_INFO ioctl

v2->v3:
	Do the stuffs without adding new ioctl
	new dependencies: this patch also depends on
	path 9/13 to 12/13 also sent here.
v1->v2:
	code optimized to remove redundancy

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-filesystem.c | 44 ++++++++++++++++++++++++++++++++++++++++----
 volumes.c         | 28 ++++++++++++++++++++++++++++
 volumes.h         |  2 ++
 3 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 5f8c258..10fdf41 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,8 @@
 #include <errno.h>
 #include <uuid/uuid.h>
 #include <ctype.h>
+#include <mntent.h>
+#include <fcntl.h>
 
 #include "kerncompat.h"
 #include "ctree.h"
@@ -256,8 +258,35 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
 	printf("\n");
 }
 
+int btrfs_scan_kernel()
+{
+	int ret = 0;
+	FILE *f;
+	struct mntent *mnt;
+
+	if ((f = setmntent ("/proc/mounts", "r")) == NULL)
+		return -errno;
+	while ((mnt = getmntent (f)) != NULL)  {
+		if (!strcmp(mnt->mnt_type, "btrfs")) {
+			struct btrfs_ioctl_fs_info_args fi;
+			struct btrfs_ioctl_dev_info_args *di = NULL;
+			char label[BTRFS_LABEL_SIZE];
+
+			get_label_mounted(mnt->mnt_dir, label);
+			ret = get_fs_info(mnt->mnt_dir, &fi, &di);
+			if (!ret) {
+				device_list_fini(fi.fsid);
+				device_list_add_from_kernel(&fi, di, label);
+				free(di);
+			}
+		}
+	}
+	return ret;
+}
+
+
 static const char * const cmd_show_usage[] = {
-	"btrfs filesystem show [--all-devices|--mapper|<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
@@ -279,15 +308,22 @@ static int cmd_show(int argc, char **argv)
 	} else if (argc > 1 && !strcmp(argv[1],"--mapper")) {
 		where = BTRFS_SCAN_MAPPER;
 		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);
 
-	ret = scan_for_btrfs(where, 0);
+	if (where)
+		ret = scan_for_btrfs(where, 0);
+	else {
+		ret = btrfs_scan_kernel();
+	}
 
-	if (ret){
-		fprintf(stderr, "ERROR: error %d while scanning\n", ret);
+	if (ret) {
+		fprintf(stderr, "ERROR: %d while scanning\n", ret);
 		return 18;
 	}
 	
diff --git a/volumes.c b/volumes.c
index 81904c6..01247bf 100644
--- a/volumes.c
+++ b/volumes.c
@@ -172,6 +172,34 @@ void device_list_fini(u8 *fsid)
 	}
 }
 
+int device_list_add_from_kernel(struct btrfs_ioctl_fs_info_args *fi,
+			struct btrfs_ioctl_dev_info_args *di_n, char *label)
+{
+	int ret = 0, i;
+	struct btrfs_super_block ds;
+	struct btrfs_fs_devices *fs_devices;
+	struct btrfs_ioctl_dev_info_args *di;
+
+	memcpy(ds.fsid, fi->fsid, BTRFS_FSID_SIZE);
+	memcpy(ds.label, label, BTRFS_LABEL_SIZE);
+	ds.num_devices = fi->num_devices; /*Todo Fix this */
+	ds.generation = 0;
+	ds.bytes_used = fi->used_bytes;
+
+	di = di_n;
+	for (i = 0; i < fi->num_devices; i++) {
+		memcpy(ds.dev_item.uuid, di->uuid, BTRFS_UUID_SIZE);
+		ds.dev_item.total_bytes = di->total_bytes;
+		ds.dev_item.bytes_used = di->bytes_used;
+
+		ret = device_list_add((char *)di->path, &ds, di->devid, &fs_devices);
+		if (ret)
+			break;
+		di++;
+	}
+	return ret;
+}
+
 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
 {
 	struct btrfs_fs_devices *seed_devices;
diff --git a/volumes.h b/volumes.h
index 6286d83..a64c8dd 100644
--- a/volumes.h
+++ b/volumes.h
@@ -193,4 +193,6 @@ struct btrfs_device *btrfs_find_device_by_devid(struct btrfs_root *root,
 int device_list_add(const char *path, struct btrfs_super_block *disk_super,
 			   u64 devid, struct btrfs_fs_devices **fs_devices_ret);
 void device_list_fini(u8 *fsid);
+int device_list_add_from_kernel(struct btrfs_ioctl_fs_info_args *fi,
+			struct btrfs_ioctl_dev_info_args *di, char *label);
 #endif
-- 
1.8.1.227.g44fe835


  parent reply	other threads:[~2013-06-21  7:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-21  7:58 [PATCH 00/13] Introduce show --kernel Anand Jain
2013-06-21  7:58 ` [PATCH 09/13] btrfs-progs: function to release a specific fsid from the list Anand Jain
2013-06-21  7:58 ` [PATCH 10/13] btrfs-progs: move out print in cmd_df to another function Anand Jain
2013-06-21  7:58 ` [PATCH 11/13] btrfs-progs: get string for the group profile and type Anand Jain
2013-06-21  7:58 ` [PATCH 12/13] btrfs-progs: obtain used_bytes in BTRFS_IOC_FS_INFO ioctl Anand Jain
2013-06-25  8:39   ` Anand Jain
2013-06-21  7:58 ` Anand Jain [this message]
2013-06-25  8:44   ` [PATCH 12/12 v4] btrfs-progs: introduce btrfs filesystem show --kernel Anand Jain
2013-06-25  8:50   ` Anand Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1371801485-14571-6-git-send-email-anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).