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
Cc: Ilya Dryomov <idryomov@gmail.com>
Subject: [PATCH 5/9] Btrfs-progs: bring 'subvol get-default' back in
Date: Fri,  3 Aug 2012 17:48:54 +0800	[thread overview]
Message-ID: <1343987338-10612-7-git-send-email-Anand.Jain@oracle.com> (raw)
In-Reply-To: <1343987338-10612-1-git-send-email-Anand.Jain@oracle.com>

From: Ilya Dryomov <idryomov@gmail.com>

Commit bab2c565 accidentally broke 'subvol get-default' command by
removing almost all of the underlying code.  Bring it back with some
fixes and improvements.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
---
 btrfs-list.c |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 ctree.h      |    2 +
 2 files changed, 82 insertions(+), 1 deletions(-)

diff --git a/btrfs-list.c b/btrfs-list.c
index 911b238..f2a6e19 100644
--- a/btrfs-list.c
+++ b/btrfs-list.c
@@ -554,6 +554,60 @@ build:
 	return full;
 }
 
+static int get_default_subvolid(int fd, u64 *default_id)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_key *sk = &args.key;
+	struct btrfs_ioctl_search_header *sh;
+	u64 found = 0;
+	int ret;
+
+	memset(&args, 0, sizeof(args));
+
+	/*
+	 * search for a dir item with a name 'default' in the tree of
+	 * tree roots, it should point us to a default root
+	 */
+	sk->tree_id = 1;
+
+	/* don't worry about ancient format and request only one item */
+	sk->nr_items = 1;
+
+	sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
+	sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
+	sk->max_type = BTRFS_DIR_ITEM_KEY;
+	sk->min_type = BTRFS_DIR_ITEM_KEY;
+	sk->max_offset = (u64)-1;
+	sk->max_transid = (u64)-1;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+	if (ret < 0)
+		return ret;
+
+	/* the ioctl returns the number of items it found in nr_items */
+	if (sk->nr_items == 0)
+		goto out;
+
+	sh = (struct btrfs_ioctl_search_header *)args.buf;
+
+	if (sh->type == BTRFS_DIR_ITEM_KEY) {
+		struct btrfs_dir_item *di;
+		int name_len;
+		char *name;
+
+		di = (struct btrfs_dir_item *)(sh + 1);
+		name_len = btrfs_stack_dir_name_len(di);
+		name = (char *)(di + 1);
+
+		if (!strncmp("default", name, name_len))
+			found = btrfs_disk_key_objectid(&di->location);
+	}
+
+out:
+	*default_id = found;
+	return 0;
+}
+
 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
 {
 	int ret;
@@ -667,12 +721,32 @@ static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
 	return 0;
 }
 
-int list_subvols(int fd, int print_parent)
+int list_subvols(int fd, int print_parent, int get_default)
 {
 	struct root_lookup root_lookup;
 	struct rb_node *n;
+	u64 default_id;
 	int ret;
 
+	if (get_default) {
+		ret = get_default_subvolid(fd, &default_id);
+		if (ret) {
+			fprintf(stderr, "ERROR: can't perform the search - %s\n",
+				strerror(errno));
+			return ret;
+		}
+		if (default_id == 0) {
+			fprintf(stderr, "ERROR: 'default' dir item not found\n");
+			return ret;
+		}
+
+		/* no need to resolve roots if FS_TREE is default */
+		if (default_id == BTRFS_FS_TREE_OBJECTID) {
+			printf("ID 5 (FS_TREE)\n");
+			return ret;
+		}
+	}
+
 	ret = __list_subvol_search(fd, &root_lookup);
 	if (ret) {
 		fprintf(stderr, "ERROR: can't perform the search - %s\n",
@@ -700,6 +774,11 @@ int list_subvols(int fd, int print_parent)
 		char *path;
 
 		entry = rb_entry(n, struct root_info, rb_node);
+		if (get_default && entry->root_id != default_id) {
+			n = rb_prev(n);
+			continue;
+		}
+
 		resolve_root(&root_lookup, entry, &parent_id, &level, &path);
 		if (print_parent) {
 			printf("ID %llu parent %llu top level %llu path %s\n",
diff --git a/ctree.h b/ctree.h
index 07691c7..32b591c 100644
--- a/ctree.h
+++ b/ctree.h
@@ -1459,6 +1459,8 @@ BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
 
+BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
+
 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
 				      struct btrfs_dir_item *item,
 				      struct btrfs_disk_key *key)
-- 
1.7.1


  parent reply	other threads:[~2012-08-03  9:51 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-01 12:01 [PATCH] add crtime to the snapshot list Anand jain
2012-08-01 12:01 ` [PATCH] Btrfs-progs: show crtime in " Anand jain
2012-08-01 12:14 ` [PATCH] add crtime to " Alexander Block
2012-08-01 15:56   ` anand jain
2012-08-01 12:43 ` Liu Bo
2012-08-03  9:48 ` [PATCH 0/9 V2] Include otime in " Anand jain
2012-08-03  9:48   ` [PATCH] Btrfs: introduce subvol uuids and times Anand jain
2012-08-03  9:48   ` [PATCH 1/9] Update ctree.h and ioctl.h for the new uuid+times for subvolumes Anand jain
2012-08-03  9:48   ` [PATCH 2/9] Btrfs-progs: search subvolumes with proper objectid Anand jain
2012-08-03  9:48   ` [PATCH 3/9] Btrfs-progs: refactor resolve_root() function a bit Anand jain
2012-08-03  9:48   ` [PATCH 4/9] Btrfs-progs: nuke redundant zeroing in __list_subvol_search() Anand jain
2012-08-03  9:48   ` Anand jain [this message]
2012-08-03  9:48   ` [PATCH 6/9] Btrfs-progs: show generation in command btrfs subvol list Anand jain
2012-08-03  9:48   ` [PATCH 7/9] Btrfs-progs: list snapshots by generation Anand jain
2012-08-03  9:48   ` [PATCH 8/9] Btrfs-progs: add otime to the snapshot list Anand jain
2012-08-03  9:48   ` [PATCH 9/9] Btrfs-progs: fix the btrfs subvol list path last char Anand jain
2012-08-14  6:04   ` [PATCH] get uuid of subvol and snapshot Anand jain
2012-08-14  6:04     ` [PATCH] add -u to show subvol uuid 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=1343987338-10612-7-git-send-email-Anand.Jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=idryomov@gmail.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).