2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
21 #include <sys/ioctl.h>
28 #include "kerncompat.h"
34 #include "btrfs-list.h"
36 static const char * const subvolume_cmd_group_usage
[] = {
37 "btrfs subvolume <command> <args>",
42 * test if path is a directory
43 * this function return
44 * 0-> path exists but it is not a directory
45 * 1-> path exists and it is a directory
46 * -1 -> path is unaccessible
48 static int test_isdir(char *path
)
53 res
= stat(path
, &st
);
57 return S_ISDIR(st
.st_mode
);
60 static const char * const cmd_subvol_create_usage
[] = {
61 "btrfs subvolume create [<dest>/]<name>",
63 "Create a subvolume <name> in <dest>. If <dest> is not given",
64 "subvolume <name> will be created in the current directory.",
68 static int cmd_subvol_create(int argc
, char **argv
)
70 int res
, fddst
, len
, e
;
74 struct btrfs_qgroup_inherit
*inherit
= NULL
;
78 int c
= getopt(argc
, argv
, "c:i:r");
84 res
= qgroup_inherit_add_copy(&inherit
, optarg
, 0);
89 res
= qgroup_inherit_add_group(&inherit
, optarg
);
94 usage(cmd_subvol_create_usage
);
98 if (check_argc_exact(argc
- optind
, 1))
99 usage(cmd_subvol_create_usage
);
103 res
= test_isdir(dst
);
105 fprintf(stderr
, "ERROR: '%s' exists\n", dst
);
109 newname
= strdup(dst
);
110 newname
= basename(newname
);
111 dstdir
= strdup(dst
);
112 dstdir
= dirname(dstdir
);
114 if( !strcmp(newname
,".") || !strcmp(newname
,"..") ||
115 strchr(newname
, '/') ){
116 fprintf(stderr
, "ERROR: uncorrect subvolume name ('%s')\n",
121 len
= strlen(newname
);
122 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
123 fprintf(stderr
, "ERROR: subvolume name too long ('%s)\n",
128 fddst
= open_file_or_dir(dstdir
);
130 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
134 printf("Create subvolume '%s/%s'\n", dstdir
, newname
);
136 struct btrfs_ioctl_vol_args_v2 args
;
138 memset(&args
, 0, sizeof(args
));
139 strncpy(args
.name
, newname
, BTRFS_SUBVOL_NAME_MAX
);
140 args
.name
[BTRFS_SUBVOL_NAME_MAX
-1] = 0;
141 args
.flags
|= BTRFS_SUBVOL_QGROUP_INHERIT
;
142 args
.size
= qgroup_inherit_size(inherit
);
143 args
.qgroup_inherit
= inherit
;
145 res
= ioctl(fddst
, BTRFS_IOC_SUBVOL_CREATE_V2
, &args
);
147 struct btrfs_ioctl_vol_args args
;
149 memset(&args
, 0, sizeof(args
));
150 strncpy(args
.name
, newname
, BTRFS_SUBVOL_NAME_MAX
);
151 args
.name
[BTRFS_SUBVOL_NAME_MAX
-1] = 0;
153 res
= ioctl(fddst
, BTRFS_IOC_SUBVOL_CREATE
, &args
);
161 fprintf( stderr
, "ERROR: cannot create subvolume - %s\n",
171 * test if path is a subvolume:
172 * this function return
173 * 0-> path exists but it is not a subvolume
174 * 1-> path exists and it is a subvolume
175 * -1 -> path is unaccessible
177 int test_issubvolume(char *path
)
182 res
= stat(path
, &st
);
186 return (st
.st_ino
== 256) && S_ISDIR(st
.st_mode
);
189 static const char * const cmd_subvol_delete_usage
[] = {
190 "btrfs subvolume delete <subvolume> [<subvolume>...]",
191 "Delete subvolume(s)",
195 static int cmd_subvol_delete(int argc
, char **argv
)
197 int res
, fd
, len
, e
, cnt
= 1, ret
= 0;
198 struct btrfs_ioctl_vol_args args
;
199 char *dname
, *vname
, *cpath
;
203 usage(cmd_subvol_delete_usage
);
208 res
= test_issubvolume(path
);
210 fprintf(stderr
, "ERROR: error accessing '%s'\n", path
);
215 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", path
);
220 cpath
= realpath(path
, 0);
221 dname
= strdup(cpath
);
222 dname
= dirname(dname
);
223 vname
= strdup(cpath
);
224 vname
= basename(vname
);
227 if( !strcmp(vname
,".") || !strcmp(vname
,"..") ||
228 strchr(vname
, '/') ){
229 fprintf(stderr
, "ERROR: incorrect subvolume name ('%s')\n",
236 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
237 fprintf(stderr
, "ERROR: snapshot name too long ('%s)\n",
243 fd
= open_file_or_dir(dname
);
246 fprintf(stderr
, "ERROR: can't access to '%s'\n", dname
);
251 printf("Delete subvolume '%s/%s'\n", dname
, vname
);
252 strncpy(args
.name
, vname
, BTRFS_PATH_NAME_MAX
);
253 args
.name
[BTRFS_PATH_NAME_MAX
-1] = 0;
254 res
= ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &args
);
260 fprintf( stderr
, "ERROR: cannot delete '%s/%s' - %s\n",
261 dname
, vname
, strerror(e
));
274 static const char * const cmd_subvol_list_usage
[] = {
275 "btrfs subvolume list [-apurts] [-g [+|-]value] [-c [+|-]value] "
276 "[--sort=gen,ogen,rootid,path] <path>",
277 "List subvolumes (and snapshots)",
279 "-p print parent ID",
280 "-a print all the subvolumes in the filesystem.",
281 "-u print the uuid of subvolumes (and snapshots)",
282 "-t print the result as a table",
283 "-s list snapshots only in the filesystem",
284 "-r list readonly subvolumes (including snapshots)",
286 " filter the subvolumes by generation",
287 " (+value: >= value; -value: <= value; value: = value)",
289 " filter the subvolumes by ogeneration",
290 " (+value: >= value; -value: <= value; value: = value)",
291 "--sort=gen,ogen,rootid,path",
292 " list the subvolume in order of gen, ogen, rootid or path",
293 " you also can add '+' or '-' in front of each items.",
294 " (+:ascending, -:descending, ascending default)",
298 static int cmd_subvol_list(int argc
, char **argv
)
300 struct btrfs_list_filter_set
*filter_set
;
301 struct btrfs_list_comparer_set
*comparer_set
;
308 int is_tab_result
= 0;
310 struct option long_options
[] = {
311 {"sort", 1, NULL
, 'S'},
315 filter_set
= btrfs_list_alloc_filter_set();
316 comparer_set
= btrfs_list_alloc_comparer_set();
320 c
= getopt_long(argc
, argv
,
321 "apsurg:c:t", long_options
, NULL
);
327 btrfs_list_setup_print_column(BTRFS_LIST_PARENT
);
336 btrfs_list_setup_filter(&filter_set
,
337 BTRFS_LIST_FILTER_SNAPSHOT_ONLY
,
339 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION
);
340 btrfs_list_setup_print_column(BTRFS_LIST_OTIME
);
343 btrfs_list_setup_print_column(BTRFS_LIST_UUID
);
346 flags
|= BTRFS_ROOT_SUBVOL_RDONLY
;
349 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION
);
350 ret
= btrfs_list_parse_filter_string(optarg
,
352 BTRFS_LIST_FILTER_GEN
);
354 usage(cmd_subvol_list_usage
);
358 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION
);
359 ret
= btrfs_list_parse_filter_string(optarg
,
361 BTRFS_LIST_FILTER_CGEN
);
363 usage(cmd_subvol_list_usage
);
366 ret
= btrfs_list_parse_sort_string(optarg
,
369 usage(cmd_subvol_list_usage
);
373 usage(cmd_subvol_list_usage
);
378 btrfs_list_setup_filter(&filter_set
, BTRFS_LIST_FILTER_FLAGS
,
381 if (check_argc_exact(argc
- optind
, 1))
382 usage(cmd_subvol_list_usage
);
384 subvol
= argv
[optind
];
386 ret
= test_issubvolume(subvol
);
388 fprintf(stderr
, "ERROR: error accessing '%s'\n", subvol
);
392 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", subvol
);
396 fd
= open_file_or_dir(subvol
);
398 fprintf(stderr
, "ERROR: can't access '%s'\n", subvol
);
402 top_id
= btrfs_list_get_path_rootid(fd
);
404 btrfs_list_setup_filter(&filter_set
,
405 BTRFS_LIST_FILTER_TOPID_EQUAL
,
408 ret
= btrfs_list_subvols(fd
, filter_set
, comparer_set
,
415 static const char * const cmd_snapshot_usage
[] = {
416 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
417 "Create a snapshot of the subvolume",
418 "Create a writable/readonly snapshot of the subvolume <source> with",
419 "the name <name> in the <dest> directory",
421 "-r create a readonly snapshot",
425 static int cmd_snapshot(int argc
, char **argv
)
428 int res
, fd
, fddst
, len
, e
, readonly
= 0;
431 struct btrfs_ioctl_vol_args_v2 args
;
432 struct btrfs_qgroup_inherit
*inherit
= NULL
;
435 memset(&args
, 0, sizeof(args
));
437 int c
= getopt(argc
, argv
, "c:i:r");
443 res
= qgroup_inherit_add_copy(&inherit
, optarg
, 0);
448 res
= qgroup_inherit_add_group(&inherit
, optarg
);
456 res
= qgroup_inherit_add_copy(&inherit
, optarg
, 1);
461 usage(cmd_snapshot_usage
);
465 if (check_argc_exact(argc
- optind
, 2))
466 usage(cmd_snapshot_usage
);
468 subvol
= argv
[optind
];
469 dst
= argv
[optind
+ 1];
471 res
= test_issubvolume(subvol
);
473 fprintf(stderr
, "ERROR: error accessing '%s'\n", subvol
);
477 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", subvol
);
481 res
= test_isdir(dst
);
483 fprintf(stderr
, "ERROR: '%s' exists and it is not a directory\n", dst
);
488 newname
= strdup(subvol
);
489 newname
= basename(newname
);
492 newname
= strdup(dst
);
493 newname
= basename(newname
);
494 dstdir
= strdup(dst
);
495 dstdir
= dirname(dstdir
);
498 if( !strcmp(newname
,".") || !strcmp(newname
,"..") ||
499 strchr(newname
, '/') ){
500 fprintf(stderr
, "ERROR: incorrect snapshot name ('%s')\n",
505 len
= strlen(newname
);
506 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
507 fprintf(stderr
, "ERROR: snapshot name too long ('%s)\n",
512 fddst
= open_file_or_dir(dstdir
);
514 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
518 fd
= open_file_or_dir(subvol
);
521 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
526 args
.flags
|= BTRFS_SUBVOL_RDONLY
;
527 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
528 subvol
, dstdir
, newname
);
530 printf("Create a snapshot of '%s' in '%s/%s'\n",
531 subvol
, dstdir
, newname
);
536 args
.flags
|= BTRFS_SUBVOL_QGROUP_INHERIT
;
537 args
.size
= qgroup_inherit_size(inherit
);
538 args
.qgroup_inherit
= inherit
;
540 strncpy(args
.name
, newname
, BTRFS_SUBVOL_NAME_MAX
);
541 args
.name
[BTRFS_SUBVOL_NAME_MAX
-1] = 0;
542 res
= ioctl(fddst
, BTRFS_IOC_SNAP_CREATE_V2
, &args
);
549 fprintf( stderr
, "ERROR: cannot snapshot '%s' - %s\n",
550 subvol
, strerror(e
));
558 static const char * const cmd_subvol_get_default_usage
[] = {
559 "btrfs subvolume get-default <path>",
560 "Get the default subvolume of a filesystem",
564 static int cmd_subvol_get_default(int argc
, char **argv
)
569 struct btrfs_list_filter_set
*filter_set
;
572 if (check_argc_exact(argc
, 2))
573 usage(cmd_subvol_get_default_usage
);
577 ret
= test_issubvolume(subvol
);
579 fprintf(stderr
, "ERROR: error accessing '%s'\n", subvol
);
583 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", subvol
);
587 fd
= open_file_or_dir(subvol
);
589 fprintf(stderr
, "ERROR: can't access '%s'\n", subvol
);
593 ret
= btrfs_list_get_default_subvolume(fd
, &default_id
);
595 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
600 if (default_id
== 0) {
601 fprintf(stderr
, "ERROR: 'default' dir item not found\n");
605 /* no need to resolve roots if FS_TREE is default */
606 if (default_id
== BTRFS_FS_TREE_OBJECTID
) {
607 printf("ID 5 (FS_TREE)\n");
611 filter_set
= btrfs_list_alloc_filter_set();
612 btrfs_list_setup_filter(&filter_set
, BTRFS_LIST_FILTER_ROOTID
,
615 ret
= btrfs_list_subvols(fd
, filter_set
, NULL
, 0);
621 static const char * const cmd_subvol_set_default_usage
[] = {
622 "btrfs subvolume set-default <subvolid> <path>",
623 "Set the default subvolume of a filesystem",
627 static int cmd_subvol_set_default(int argc
, char **argv
)
634 if (check_argc_exact(argc
, 3))
635 usage(cmd_subvol_set_default_usage
);
640 fd
= open_file_or_dir(path
);
642 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
646 objectid
= (unsigned long long)strtoll(subvolid
, NULL
, 0);
647 if (errno
== ERANGE
) {
648 fprintf(stderr
, "ERROR: invalid tree id (%s)\n",subvolid
);
651 ret
= ioctl(fd
, BTRFS_IOC_DEFAULT_SUBVOL
, &objectid
);
655 fprintf(stderr
, "ERROR: unable to set a new default subvolume - %s\n",
662 static const char * const cmd_find_new_usage
[] = {
663 "btrfs subvolume find-new <path> <lastgen>",
664 "List the recently modified files in a filesystem",
668 static int cmd_find_new(int argc
, char **argv
)
675 if (check_argc_exact(argc
, 3))
676 usage(cmd_find_new_usage
);
679 last_gen
= atoll(argv
[2]);
681 ret
= test_issubvolume(subvol
);
683 fprintf(stderr
, "ERROR: error accessing '%s'\n", subvol
);
687 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", subvol
);
691 fd
= open_file_or_dir(subvol
);
693 fprintf(stderr
, "ERROR: can't access '%s'\n", subvol
);
696 ret
= btrfs_list_find_updated_files(fd
, 0, last_gen
);
702 const struct cmd_group subvolume_cmd_group
= {
703 subvolume_cmd_group_usage
, NULL
, {
704 { "create", cmd_subvol_create
, cmd_subvol_create_usage
, NULL
, 0 },
705 { "delete", cmd_subvol_delete
, cmd_subvol_delete_usage
, NULL
, 0 },
706 { "list", cmd_subvol_list
, cmd_subvol_list_usage
, NULL
, 0 },
707 { "snapshot", cmd_snapshot
, cmd_snapshot_usage
, NULL
, 0 },
708 { "get-default", cmd_subvol_get_default
,
709 cmd_subvol_get_default_usage
, NULL
, 0 },
710 { "set-default", cmd_subvol_set_default
,
711 cmd_subvol_set_default_usage
, NULL
, 0 },
712 { "find-new", cmd_find_new
, cmd_find_new_usage
, NULL
, 0 },
717 int cmd_subvolume(int argc
, char **argv
)
719 return handle_command_group(&subvolume_cmd_group
, argc
, argv
);