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"
37 static const char * const subvolume_cmd_group_usage[] = {
38 "btrfs subvolume <command> <args>",
43 * test if path is a directory
44 * this function return
45 * 0-> path exists but it is not a directory
46 * 1-> path exists and it is a directory
47 * -1 -> path is unaccessible
49 static int test_isdir(char *path)
54 res = stat(path, &st);
58 return S_ISDIR(st.st_mode);
61 static const char * const cmd_subvol_create_usage[] = {
62 "btrfs subvolume create [<dest>/]<name>",
64 "Create a subvolume <name> in <dest>. If <dest> is not given",
65 "subvolume <name> will be created in the current directory.",
69 static int cmd_subvol_create(int argc, char **argv)
71 int res, fddst, len, e;
75 struct btrfs_qgroup_inherit *inherit = NULL;
79 int c = getopt(argc, argv, "c:i:r");
85 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
90 res = qgroup_inherit_add_group(&inherit, optarg);
95 usage(cmd_subvol_create_usage);
99 if (check_argc_exact(argc - optind, 1))
100 usage(cmd_subvol_create_usage);
104 res = test_isdir(dst);
106 fprintf(stderr, "ERROR: '%s' exists\n", dst);
110 newname = strdup(dst);
111 newname = basename(newname);
112 dstdir = strdup(dst);
113 dstdir = dirname(dstdir);
115 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
116 strchr(newname, '/') ){
117 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
122 len = strlen(newname);
123 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
124 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
129 fddst = open_file_or_dir(dstdir);
131 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
135 printf("Create subvolume '%s/%s'\n", dstdir, newname);
137 struct btrfs_ioctl_vol_args_v2 args;
139 memset(&args, 0, sizeof(args));
140 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
141 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
142 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
143 args.size = qgroup_inherit_size(inherit);
144 args.qgroup_inherit = inherit;
146 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
148 struct btrfs_ioctl_vol_args args;
150 memset(&args, 0, sizeof(args));
151 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
152 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
154 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
162 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
172 * test if path is a subvolume:
173 * this function return
174 * 0-> path exists but it is not a subvolume
175 * 1-> path exists and it is a subvolume
176 * -1 -> path is unaccessible
178 int test_issubvolume(char *path)
183 res = stat(path, &st);
187 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
190 static const char * const cmd_subvol_delete_usage[] = {
191 "btrfs subvolume delete <subvolume> [<subvolume>...]",
192 "Delete subvolume(s)",
196 static int cmd_subvol_delete(int argc, char **argv)
198 int res, fd, len, e, cnt = 1, ret = 0;
199 struct btrfs_ioctl_vol_args args;
200 char *dname, *vname, *cpath;
204 usage(cmd_subvol_delete_usage);
209 res = test_issubvolume(path);
211 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
216 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
221 cpath = realpath(path, 0);
222 dname = strdup(cpath);
223 dname = dirname(dname);
224 vname = strdup(cpath);
225 vname = basename(vname);
228 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
229 strchr(vname, '/') ){
230 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
237 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
238 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
244 fd = open_file_or_dir(dname);
247 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
252 printf("Delete subvolume '%s/%s'\n", dname, vname);
253 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
254 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
255 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
261 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
262 dname, vname, strerror(e));
275 static const char * const cmd_subvol_list_usage[] = {
276 "btrfs subvolume list [-aopurts] [-g [+|-]value] [-c [+|-]value] "
277 "[--sort=gen,ogen,rootid,path] <path>",
278 "List subvolumes (and snapshots)",
280 "-p print parent ID",
281 "-a print all the subvolumes in the filesystem and",
282 " distinguish absolute and relative path with respect",
283 " to the given <path>",
284 "-o print only subvolumes bellow specified path",
285 "-u print the uuid of subvolumes (and snapshots)",
286 "-q print the parent uuid of the snapshots",
287 "-t print the result as a table",
288 "-s list snapshots only in the filesystem",
289 "-r list readonly subvolumes (including snapshots)",
291 " filter the subvolumes by generation",
292 " (+value: >= value; -value: <= value; value: = value)",
294 " filter the subvolumes by ogeneration",
295 " (+value: >= value; -value: <= value; value: = value)",
296 "--sort=gen,ogen,rootid,path",
297 " list the subvolume in order of gen, ogen, rootid or path",
298 " you also can add '+' or '-' in front of each items.",
299 " (+:ascending, -:descending, ascending default)",
303 static int cmd_subvol_list(int argc, char **argv)
305 struct btrfs_list_filter_set *filter_set;
306 struct btrfs_list_comparer_set *comparer_set;
313 int is_tab_result = 0;
315 int is_only_in_path = 0;
316 struct option long_options[] = {
317 {"sort", 1, NULL, 'S'},
321 filter_set = btrfs_list_alloc_filter_set();
322 comparer_set = btrfs_list_alloc_comparer_set();
326 c = getopt_long(argc, argv,
327 "aopqsurg:c:t", long_options, NULL);
333 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
345 btrfs_list_setup_filter(&filter_set,
346 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
348 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
349 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
352 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
355 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
358 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
361 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
362 ret = btrfs_list_parse_filter_string(optarg,
364 BTRFS_LIST_FILTER_GEN);
366 usage(cmd_subvol_list_usage);
370 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
371 ret = btrfs_list_parse_filter_string(optarg,
373 BTRFS_LIST_FILTER_CGEN);
375 usage(cmd_subvol_list_usage);
378 ret = btrfs_list_parse_sort_string(optarg,
381 usage(cmd_subvol_list_usage);
385 usage(cmd_subvol_list_usage);
390 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
393 if (check_argc_exact(argc - optind, 1))
394 usage(cmd_subvol_list_usage);
396 subvol = argv[optind];
398 ret = test_issubvolume(subvol);
400 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
404 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
408 fd = open_file_or_dir(subvol);
410 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
414 top_id = btrfs_list_get_path_rootid(fd);
417 btrfs_list_setup_filter(&filter_set,
418 BTRFS_LIST_FILTER_FULL_PATH,
420 else if (is_only_in_path)
421 btrfs_list_setup_filter(&filter_set,
422 BTRFS_LIST_FILTER_TOPID_EQUAL,
425 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
427 !is_list_all && !is_only_in_path);
433 static const char * const cmd_snapshot_usage[] = {
434 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
435 "Create a snapshot of the subvolume",
436 "Create a writable/readonly snapshot of the subvolume <source> with",
437 "the name <name> in the <dest> directory",
439 "-r create a readonly snapshot",
443 static int cmd_snapshot(int argc, char **argv)
446 int res, fd, fddst, len, e, readonly = 0;
449 struct btrfs_ioctl_vol_args_v2 args;
450 struct btrfs_qgroup_inherit *inherit = NULL;
453 memset(&args, 0, sizeof(args));
455 int c = getopt(argc, argv, "c:i:r");
461 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
466 res = qgroup_inherit_add_group(&inherit, optarg);
474 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
479 usage(cmd_snapshot_usage);
483 if (check_argc_exact(argc - optind, 2))
484 usage(cmd_snapshot_usage);
486 subvol = argv[optind];
487 dst = argv[optind + 1];
489 res = test_issubvolume(subvol);
491 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
495 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
499 res = test_isdir(dst);
501 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
506 newname = strdup(subvol);
507 newname = basename(newname);
510 newname = strdup(dst);
511 newname = basename(newname);
512 dstdir = strdup(dst);
513 dstdir = dirname(dstdir);
516 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
517 strchr(newname, '/') ){
518 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
523 len = strlen(newname);
524 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
525 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
530 fddst = open_file_or_dir(dstdir);
532 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
536 fd = open_file_or_dir(subvol);
539 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
544 args.flags |= BTRFS_SUBVOL_RDONLY;
545 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
546 subvol, dstdir, newname);
548 printf("Create a snapshot of '%s' in '%s/%s'\n",
549 subvol, dstdir, newname);
554 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
555 args.size = qgroup_inherit_size(inherit);
556 args.qgroup_inherit = inherit;
558 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
559 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
560 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
567 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
568 subvol, strerror(e));
576 static const char * const cmd_subvol_get_default_usage[] = {
577 "btrfs subvolume get-default <path>",
578 "Get the default subvolume of a filesystem",
582 static int cmd_subvol_get_default(int argc, char **argv)
587 struct btrfs_list_filter_set *filter_set;
590 if (check_argc_exact(argc, 2))
591 usage(cmd_subvol_get_default_usage);
595 ret = test_issubvolume(subvol);
597 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
601 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
605 fd = open_file_or_dir(subvol);
607 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
611 ret = btrfs_list_get_default_subvolume(fd, &default_id);
613 fprintf(stderr, "ERROR: can't perform the search - %s\n",
618 if (default_id == 0) {
619 fprintf(stderr, "ERROR: 'default' dir item not found\n");
623 /* no need to resolve roots if FS_TREE is default */
624 if (default_id == BTRFS_FS_TREE_OBJECTID) {
625 printf("ID 5 (FS_TREE)\n");
629 filter_set = btrfs_list_alloc_filter_set();
630 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
633 ret = btrfs_list_subvols_print(fd, filter_set, NULL, 0, 1);
639 static const char * const cmd_subvol_set_default_usage[] = {
640 "btrfs subvolume set-default <subvolid> <path>",
641 "Set the default subvolume of a filesystem",
645 static int cmd_subvol_set_default(int argc, char **argv)
652 if (check_argc_exact(argc, 3))
653 usage(cmd_subvol_set_default_usage);
658 fd = open_file_or_dir(path);
660 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
664 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
665 if (errno == ERANGE) {
666 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
669 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
673 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
680 static const char * const cmd_find_new_usage[] = {
681 "btrfs subvolume find-new <path> <lastgen>",
682 "List the recently modified files in a filesystem",
686 static int cmd_find_new(int argc, char **argv)
693 if (check_argc_exact(argc, 3))
694 usage(cmd_find_new_usage);
697 last_gen = atoll(argv[2]);
699 ret = test_issubvolume(subvol);
701 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
705 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
709 fd = open_file_or_dir(subvol);
711 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
714 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
720 const struct cmd_group subvolume_cmd_group = {
721 subvolume_cmd_group_usage, NULL, {
722 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
723 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
724 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
725 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
726 { "get-default", cmd_subvol_get_default,
727 cmd_subvol_get_default_usage, NULL, 0 },
728 { "set-default", cmd_subvol_set_default,
729 cmd_subvol_set_default_usage, NULL, 0 },
730 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
735 int cmd_subvolume(int argc, char **argv)
737 return handle_command_group(&subvolume_cmd_group, argc, argv);