linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] btrfs-progs: restore: Have -l display subvolume name
@ 2020-10-29 23:33 Daniel Xu
  2020-11-11  1:36 ` Daniel Xu
  2020-11-13 21:08 ` Josef Bacik
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Xu @ 2020-10-29 23:33 UTC (permalink / raw)
  To: linux-btrfs, dsterba, josef; +Cc: Daniel Xu, kernel-team

This commit has `btrfs restore -l ...` display subvolume names if
applicable. Before, it only listed subvolume IDs which are not very
helpful for the user. A subvolume name is much more descriptive.

Before:
	$ btrfs restore ~/scratch/btrfs/fs -l
	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
	 tree key (256 ROOT_ITEM 0) 30818304 level 0
	 tree key (257 ROOT_ITEM 0) 30883840 level 0
	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0

After:
	$ ./btrfs restore ~/scratch/btrfs/fs -l
	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
	 tree key (256 ROOT_ITEM 0) 30818304 level 0 subvol1
	 tree key (257 ROOT_ITEM 0) 30883840 level 0 subvol2
	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0

Link: https://github.com/kdave/btrfs-progs/issues/289
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
v1 -> v2:
* moved get_subvol_name() to common/utils.c
* check return from get_subvol_name() for errors

 cmds/restore.c | 14 +++++++++++++-
 common/utils.c | 35 +++++++++++++++++++++++++++++++++++
 common/utils.h |  1 +
 3 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/cmds/restore.c b/cmds/restore.c
index 025e99e9..d3a25952 100644
--- a/cmds/restore.c
+++ b/cmds/restore.c
@@ -1206,6 +1206,7 @@ static int do_list_roots(struct btrfs_root *root)
 	struct extent_buffer *leaf;
 	struct btrfs_root_item ri;
 	unsigned long offset;
+	char *name;
 	int slot;
 	int ret;

@@ -1244,8 +1245,19 @@ static int do_list_roots(struct btrfs_root *root)
 		read_extent_buffer(leaf, &ri, offset, sizeof(ri));
 		printf(" tree ");
 		btrfs_print_key(&disk_key);
-		printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
+		printf(" %Lu level %d", btrfs_root_bytenr(&ri),
 		       btrfs_root_level(&ri));
+
+		name = get_subvol_name(root, found_key.objectid);
+		if (IS_ERR(name)) {
+			fprintf(stderr, "Failed to get subvol name: %s",
+					strerror(-PTR_ERR(name)));
+		} else if (name) {
+			printf(" %s", name);
+			free(name);
+		}
+
+		printf("\n");
 		path.slots[0]++;
 	}
 	btrfs_release_path(&path);
diff --git a/common/utils.c b/common/utils.c
index c47ce29b..b6cb578d 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -1590,6 +1590,41 @@ const char *subvol_strip_mountpoint(const char *mnt, const char *full_path)
 	return full_path + len;
 }

+char *get_subvol_name(struct btrfs_root *tree_root, u64 subvol_id)
+{
+	struct btrfs_root_ref *ref;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int namelen;
+	int ret;
+	char *name = NULL;
+
+	key.objectid = BTRFS_FS_TREE_OBJECTID;
+	key.type = BTRFS_ROOT_REF_KEY;
+	key.offset = subvol_id;
+
+	btrfs_init_path(&path);
+	ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
+	if (ret != 0)
+		goto out;
+
+	ref = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_root_ref);
+
+	namelen = btrfs_root_ref_name_len(path.nodes[0], ref);
+	name = malloc(sizeof(char) * namelen + 1);
+	if (!name) {
+		name = ERR_PTR(-ENOMEM);
+		goto out;
+	}
+
+	read_extent_buffer(path.nodes[0], name, (unsigned long)(ref + 1), namelen);
+	name[namelen] = 0;
+
+out:
+	btrfs_release_path(&path);
+	return name;
+}
+
 /* Set the seed manually */
 void init_rand_seed(u64 seed)
 {
diff --git a/common/utils.h b/common/utils.h
index 0413489d..6ca75fbf 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -99,6 +99,7 @@ int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret);
 int test_uuid_unique(char *fs_uuid);

 const char *subvol_strip_mountpoint(const char *mnt, const char *full_path);
+char *get_subvol_name(struct btrfs_root *tree_root, u64 subvol_id);
 int find_next_key(struct btrfs_path *path, struct btrfs_key *key);
 const char* btrfs_group_type_str(u64 flag);
 const char* btrfs_group_profile_str(u64 flag);
--
2.26.2


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

* Re: [PATCH v2] btrfs-progs: restore: Have -l display subvolume name
  2020-10-29 23:33 [PATCH v2] btrfs-progs: restore: Have -l display subvolume name Daniel Xu
@ 2020-11-11  1:36 ` Daniel Xu
  2020-11-13 21:08 ` Josef Bacik
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Xu @ 2020-11-11  1:36 UTC (permalink / raw)
  To: linux-btrfs, dsterba, Josef Bacik; +Cc: Kernel Team

On Thu, Oct 29, 2020, at 4:33 PM, Daniel Xu wrote:
> This commit has `btrfs restore -l ...` display subvolume names if
> applicable. Before, it only listed subvolume IDs which are not very
> helpful for the user. A subvolume name is much more descriptive.
> 
> Before:
> 	$ btrfs restore ~/scratch/btrfs/fs -l
> 	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
> 	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
> 	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
> 	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
> 	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
> 	 tree key (256 ROOT_ITEM 0) 30818304 level 0
> 	 tree key (257 ROOT_ITEM 0) 30883840 level 0
> 	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0
> 
> After:
> 	$ ./btrfs restore ~/scratch/btrfs/fs -l
> 	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
> 	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
> 	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
> 	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
> 	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
> 	 tree key (256 ROOT_ITEM 0) 30818304 level 0 subvol1
> 	 tree key (257 ROOT_ITEM 0) 30883840 level 0 subvol2
> 	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0
> 
> Link: https://github.com/kdave/btrfs-progs/issues/289
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
> v1 -> v2:
> * moved get_subvol_name() to common/utils.c
> * check return from get_subvol_name() for errors
> 
>  cmds/restore.c | 14 +++++++++++++-
>  common/utils.c | 35 +++++++++++++++++++++++++++++++++++
>  common/utils.h |  1 +
>  3 files changed, 49 insertions(+), 1 deletion(-)
> 
[...]

Ping

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

* Re: [PATCH v2] btrfs-progs: restore: Have -l display subvolume name
  2020-10-29 23:33 [PATCH v2] btrfs-progs: restore: Have -l display subvolume name Daniel Xu
  2020-11-11  1:36 ` Daniel Xu
@ 2020-11-13 21:08 ` Josef Bacik
  1 sibling, 0 replies; 3+ messages in thread
From: Josef Bacik @ 2020-11-13 21:08 UTC (permalink / raw)
  To: Daniel Xu, linux-btrfs, dsterba; +Cc: kernel-team

On 10/29/20 7:33 PM, Daniel Xu wrote:
> This commit has `btrfs restore -l ...` display subvolume names if
> applicable. Before, it only listed subvolume IDs which are not very
> helpful for the user. A subvolume name is much more descriptive.
> 
> Before:
> 	$ btrfs restore ~/scratch/btrfs/fs -l
> 	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
> 	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
> 	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
> 	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
> 	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
> 	 tree key (256 ROOT_ITEM 0) 30818304 level 0
> 	 tree key (257 ROOT_ITEM 0) 30883840 level 0
> 	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0
> 
> After:
> 	$ ./btrfs restore ~/scratch/btrfs/fs -l
> 	 tree key (EXTENT_TREE ROOT_ITEM 0) 30425088 level 0
> 	 tree key (DEV_TREE ROOT_ITEM 0) 30441472 level 0
> 	 tree key (FS_TREE ROOT_ITEM 0) 30736384 level 0
> 	 tree key (CSUM_TREE ROOT_ITEM 0) 30474240 level 0
> 	 tree key (UUID_TREE ROOT_ITEM 0) 30785536 level 0
> 	 tree key (256 ROOT_ITEM 0) 30818304 level 0 subvol1
> 	 tree key (257 ROOT_ITEM 0) 30883840 level 0 subvol2
> 	 tree key (DATA_RELOC_TREE ROOT_ITEM 0) 30490624 level 0
> 
> Link: https://github.com/kdave/btrfs-progs/issues/289
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

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

end of thread, other threads:[~2020-11-13 21:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-29 23:33 [PATCH v2] btrfs-progs: restore: Have -l display subvolume name Daniel Xu
2020-11-11  1:36 ` Daniel Xu
2020-11-13 21:08 ` Josef Bacik

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).