Btrfs-progs: enhance btrfs subvol list only to show read-only snapshots
v2 cover Btrfs-progs: enhance btrfs subvol list only to show read-only snapshots
From: Zhou Bo <[email protected]>
We want 'btrfs subvolume list' only to list readonly subvolumes, this patch set introduces a new option 'r' to implement it.
You can use the command like that:
btrfs subvolume list -r <path>
v1->v2: address the comments from Goffredo Baroncelli
- change the changelog of the patches and make them more elaborate.
- move the function declarations to a new head file.
- add the introduction of the new option 'r' into the man page
--- btrfs-list.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++------ btrfs-list.h | 21 +++++++++++++++ cmds-inspect.c | 4 +-- cmds-subvolume.c | 18 +++++++------ ctree.h | 2 + man/btrfs.8.in | 5 ++- 6 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 btrfs-list.h
Signature · 1 line
1.6.0.6
PATCH v2 1/3 Btrfs-progs: move the function declarations to a new head file
From: Zhou Bo <[email protected]>
Move the function declarations to a new head file.
--- btrfs-list.c | 1 + btrfs-list.h | 21 +++++++++++++++++++++ cmds-inspect.c | 4 +--- cmds-subvolume.c | 5 +---- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 btrfs-list.h
btrfs-list.c +1 -0
@@ -34,6 +34,7 @@#include "ctree.h" #include "transaction.h" #include "utils.h" +#include "btrfs-list.h" /* we store all the roots we find in an rbtree so that we can * search for them later.
btrfs-list.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Copyright (C) 2012 Fujitsu. All rights reserved. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public +* License v2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public +* License along with this program; if not, write to the +* Free Software Foundation, Inc., 59 Temple Place - Suite 330, +* Boston, MA 021110-1307, USA. +*/ + +int list_subvols (int fd,int print_parent,int get_default,int list_ro); +int find_updated_files (int fd,u64 root_id,u64 oldest_gen); +char *path_for_root (int fd,u64 root);
cmds-inspect.c +1 -3
@@ -24,9 +24,7 @@#include "ioctl.h" #include "commands.h" - -/* btrfs-list.c */ -char *path_for_root (int fd,u64 root); +#include "btrfs-list.h" static const char *const inspect_cmd_group_usage[] = {"btrfs inspect-internal <command> <args>" ,
cmds-subvolume.c +1 -4
@@ -28,10 +28,7 @@#include "ioctl.h" #include "commands.h" - -/* btrfs-list.c */ -int list_subvols (int fd,int print_parent,int get_default); -int find_updated_files (int fd,u64 root_id,u64 oldest_gen); +#include "btrfs-list.h" static const char *const subvolume_cmd_group_usage[] = {"btrfs subvolume <command> <args>" ,
Signature · 1 line
1.6.0.6
PATCH v2 2/3 Btrfs-progs: enhance btrfs subvol list only to show read-only snapshots
In reply to zhouboFrom: Zhou Bo <[email protected]>
We want 'btrfs subvolume list' only to list readonly subvolumes, this patch set introduces a new option 'r' to implement it.
You can use the command like that:
btrfs subvolume list -r <path>
--- btrfs-list.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++------ cmds-subvolume.c | 13 +++++++--- ctree.h | 2 + 3 files changed, 71 insertions(+), 12 deletions(-)
btrfs-list.c +60 -8
@@ -608,7 +608,46 @@ out:return 0 ; } -static int __list_subvol_search (int fd,struct root_lookup *root_lookup) +static int is_readonly_subvol (int fd,u64 objectid) +{ +int ret; +struct btrfs_ioctl_search_args args; +struct btrfs_ioctl_search_key *sk = &args.key; +struct btrfs_ioctl_search_header *sh; +unsigned long off =0 ; +int found =0 ; +struct btrfs_root_item *item; + +memset (&args,0 ,sizeof (args)); + +/* search in the tree of tree roots */ + sk->tree_id =BTRFS_ROOT_TREE_OBJECTID ; + sk->min_objectid = objectid; + sk->max_objectid = objectid; + sk->max_type =BTRFS_ROOT_ITEM_KEY ; + sk->min_type =BTRFS_ROOT_ITEM_KEY ; + sk->max_offset = (u64)-1 ; + sk->max_transid = (u64)-1 ; + sk->nr_items =1 ; + ret =ioctl (fd,BTRFS_IOC_TREE_SEARCH , &args); +if (ret <0 ) +return ret; +if (sk->nr_items ==0 ) { + errno = -ENOENT ; + found =-1 ; + goto out; + } + sh = (struct btrfs_ioctl_search_header *)args.buf; + off +=sizeof (*sh); + item = (struct btrfs_root_item *)(args.buf + off); +if (item->flags &cpu_to_le64 (BTRFS_ROOT_SUBVOL_RDONLY )) + found =1 ; +out: +return found; +} + +static int __list_subvol_search (int fd,struct root_lookup *root_lookup, +int list_ro) {int ret;struct btrfs_ioctl_search_args args;
@@ -668,9 +707,23 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup) name_len =btrfs_stack_root_ref_name_len (ref); name = (char *)(ref +1 ); dir_id =btrfs_stack_root_ref_dirid (ref); - -add_root (root_lookup, sh->objectid, sh->offset, - dir_id, name, name_len); +if (list_ro) { +int subvol_readonly = +is_readonly_subvol (fd, + sh->objectid); +if (subvol_readonly <0 ) { +return subvol_readonly; + }else if (subvol_readonly) { +add_root (root_lookup, + sh->objectid, + sh->offset, dir_id, + name, name_len); + } + }else { +add_root (root_lookup, sh->objectid, + sh->offset, dir_id, + name, name_len); + } } off += sh->len;
@@ -719,7 +772,7 @@ static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)return 0 ; } -int list_subvols (int fd,int print_parent,int get_default) +int list_subvols (int fd,int print_parent,int get_default,int list_ro) {struct root_lookup root_lookup;struct rb_node *n;
@@ -745,7 +798,7 @@ int list_subvols(int fd, int print_parent, int get_default) } } - ret =__list_subvol_search (fd, &root_lookup); + ret =__list_subvol_search (fd, &root_lookup, list_ro);if (ret) {fprintf (stderr,"ERROR: can't perform the search - %s\n" ,strerror (errno));
@@ -788,7 +841,6 @@ int list_subvols(int fd, int print_parent, int get_default) (unsigned long long )entry->root_id, (unsigned long long)level, path); } -free (path); n =rb_prev (n); }
@@ -983,7 +1035,7 @@ char *path_for_root(int fd, u64 root)char *ret_path =NULL ;int ret; - ret =__list_subvol_search (fd, &root_lookup); + ret =__list_subvol_search (fd, &root_lookup,0 );if (ret <0 )return ERR_PTR (ret);
cmds-subvolume.c +9 -4
@@ -219,10 +219,11 @@ static int cmd_subvol_delete(int argc, char **argv) }static const char *const cmd_subvol_list_usage[] = { -"btrfs subvolume list [-p] <path>" , +"btrfs subvolume list [-pr] <path>" ,"List subvolumes (and snapshots)" ,"" ,"-p print parent ID" , +"-r only list readonly snapshots" ,NULL };
@@ -231,15 +232,19 @@ static int cmd_subvol_list(int argc, char **argv)int fd;int ret;int print_parent =0 ; +int list_ro =0 ;char *subvol; optind =1 ;while (1 ) { -int c =getopt (argc, argv,"p" ); +int c =getopt (argc, argv,"pr" );if (c <0 )break ;switch (c) { +case 'r' : + list_ro =1 ; +break ;case 'p' : print_parent =1 ;break ;
@@ -268,7 +273,7 @@ static int cmd_subvol_list(int argc, char **argv)fprintf (stderr,"ERROR: can't access '%s'\n" , subvol);return 12 ; } - ret =list_subvols (fd, print_parent,0 ); + ret =list_subvols (fd, print_parent,0 , list_ro);if (ret)return 19 ;return 0 ;
@@ -428,7 +433,7 @@ static int cmd_subvol_get_default(int argc, char **argv)fprintf (stderr,"ERROR: can't access '%s'\n" , subvol);return 12 ; } - ret =list_subvols (fd,0 ,1 ); + ret =list_subvols (fd,0 ,1 ,0 );if (ret)return 19 ;return 0 ;
ctree.h +2 -0
@@ -138,6 +138,8 @@ static int btrfs_csum_sizes[] = { 4, 0 };#define BTRFS_FT_XATTR 8#define BTRFS_FT_MAX 9 +#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0) +/* * the key defines the order in the tree, and so it also defines (optimal) * block layout. objectid corresonds to the inode number. The flags
Signature · 1 line
1.6.0.6
PATCH v2 3/3 Btrfs-progs: update the manpage entries for the btrfs subvolume list
In reply to zhouboFrom: Zhou Bo <[email protected]>
This patch adds the introduction of the new option '-r' into the man page of 'btrfs subvolume list' command.
--- man/btrfs.8.in | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
man/btrfs.8.in +3 -2
@@ -11,7 +11,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBsubvolume create\fP\fI [<dest>/]<name>\fP .PP -\fBbtrfs\fP \fBsubvolume list\fP\fI [-p] <path>\fP +\fBbtrfs\fP \fBsubvolume list\fP\fI [-pr] <path>\fP .PP \fBbtrfs\fP \fBsubvolume set-default\fP\fI <id> <path>\fP .PP
@@ -108,7 +108,7 @@ Create a subvolume in \fI<dest>\fR (or in the current directory if \fI<dest>\fR is omitted). .TP -\fBsubvolume list\fR\fI [-p] <path>\fR +\fBsubvolume list\fR\fI [-pr] <path>\fR List the subvolumes present in the filesystem \fI<path>\fR. For every subvolume the following information is shown by default. ID <ID> top level <ID> path <path>
@@ -119,6 +119,7 @@ at mount time via the \fIsubvol=\fR option. If \fI-p\fR is given, then \fIparent <ID>\fR is added to the output between ID and top level. The parent's ID may be used at mount time via the \fIsubvolrootid=\fR option. +If \fI-r\fR is given, only readonly subvolumes in the filesystem will be listed. .TP \fBsubvolume set-default\fR\fI <id> <path>\fR
Signature · 1 line
1.6.0.6