Btrfs-progs: fix 'subvol get-default' command
cover Btrfs-progs: fix 'subvol get-default' command
'btrfs subvol get-default' has been broken for a while, fix it. Patches 1 and 2 are straightforward cleanups in that area, patch 3 fixes the problem.
Thanks,
Ilya
Ilya Dryomov (3):
Btrfs-progs: nuke redundant zeroing in __list_subvol_search()
Btrfs-progs: refactor resolve_root() function a bit
Btrfs-progs: bring 'subvol get-default' back in
btrfs-list.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++--------- ctree.h | 2 + 2 files changed, 92 insertions(+), 16 deletions(-)
Signature · 1 line
1.7.9.1
PATCH 1/3 Btrfs-progs: nuke redundant zeroing in __list_subvol_search()
There's no need to zero out things twice.
--- btrfs-list.c | 4 ---- 1 files changed, 0 insertions(+), 4 deletions(-)
btrfs-list.c +0 -4
@@ -569,10 +569,6 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)root_lookup_init (root_lookup);memset (&args,0 ,sizeof (args)); -root_lookup_init (root_lookup); - -memset (&args,0 ,sizeof (args)); -/* search in the tree of tree roots */ sk->tree_id =1 ;
Signature · 1 line
1.7.9.1
PATCH 2/3 Btrfs-progs: refactor resolve_root() function a bit
In reply to Ilya DryomovDon't pass a pointer to root_id to resolve_root(). It's always the same as ri->root_id, passing a pointer hints that root_id can somehow change which is not true.
--- btrfs-list.c | 21 ++++++++++----------- 1 files changed, 10 insertions(+), 11 deletions(-)
btrfs-list.c +10 -11
@@ -200,7 +200,7 @@ static int add_root(struct root_lookup *root_lookup,* in by lookup_ino_path */ static int resolve_root (struct root_lookup *rl,struct root_info *ri, -u64 *root_id,u64 *parent_id,u64 *top_id,char **path) +u64 *parent_id,u64 *top_id,char **path) {char *full_path =NULL ;int len =0 ;
@@ -254,7 +254,6 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri, } } - *root_id = ri->root_id; *path = full_path;return 0 ;
@@ -692,23 +691,23 @@ int list_subvols(int fd, int print_parent) n =rb_last (&root_lookup.root);while (n) {struct root_info *entry; -u64 root_id;u64 level;u64 parent_id;char *path; + entry = rb_entry(n, struct root_info, rb_node); -resolve_root (&root_lookup, entry, &root_id, &parent_id, - &level, &path); +resolve_root (&root_lookup, entry, &parent_id, &level, &path);if (print_parent) {printf ("ID %llu parent %llu top level %llu path %s\n" , - (unsigned long long )root_id, + (unsigned long long )entry->root_id, (unsigned long long )parent_id, (unsigned long long )level, path); }else {printf ("ID %llu top level %llu path %s\n" , - (unsigned long long )root_id, + (unsigned long long )entry->root_id, (unsigned long long )level, path); } +free (path); n =rb_prev (n); }
@@ -914,17 +913,17 @@ char *path_for_root(int fd, u64 root) n =rb_last (&root_lookup.root);while (n) {struct root_info *entry; -u64 root_id;u64 parent_id;u64 level;char *path; + entry = rb_entry(n, struct root_info, rb_node); -resolve_root (&root_lookup, entry, &root_id, &parent_id, &level, - &path); -if (root_id == root) +resolve_root (&root_lookup, entry, &parent_id, &level, &path); +if (entry->root_id == root) ret_path = path;else free (path); + n =rb_prev (n); }
Signature · 1 line
1.7.9.1
PATCH 3/3 Btrfs-progs: bring 'subvol get-default' back in
In reply to Ilya DryomovCommit bab2c565 accidentally broke 'subvol get-default' command by removing almost all of the underlying code. Bring it back with some fixes and improvements.
--- btrfs-list.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- ctree.h | 2 + 2 files changed, 82 insertions(+), 1 deletions(-)
btrfs-list.c +80 -1
@@ -552,6 +552,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;
@@ -663,12 +717,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",
@@ -696,6 +770,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",
ctree.h +2 -0
@@ -1416,6 +1416,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)
Signature · 1 line
1.7.9.1