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>
27 #include <uuid/uuid.h>
29 #include "kerncompat.h"
36 #include "btrfs-list.h"
39 static const char * const subvolume_cmd_group_usage[] = {
40 "btrfs subvolume <command> <args>",
45 * test if path is a directory
46 * this function return
47 * 0-> path exists but it is not a directory
48 * 1-> path exists and it is a directory
49 * -1 -> path is unaccessible
51 static int test_isdir(char *path)
56 res = stat(path, &st);
60 return S_ISDIR(st.st_mode);
63 static const char * const cmd_subvol_create_usage[] = {
64 "btrfs subvolume create [<dest>/]<name>",
66 "Create a subvolume <name> in <dest>. If <dest> is not given",
67 "subvolume <name> will be created in the current directory.",
71 static int cmd_subvol_create(int argc, char **argv)
78 struct btrfs_qgroup_inherit *inherit = NULL;
82 int c = getopt(argc, argv, "c:i:r");
88 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
93 res = qgroup_inherit_add_group(&inherit, optarg);
98 usage(cmd_subvol_create_usage);
102 if (check_argc_exact(argc - optind, 1))
103 usage(cmd_subvol_create_usage);
107 retval = 1; /* failure */
108 res = test_isdir(dst);
110 fprintf(stderr, "ERROR: '%s' exists\n", dst);
114 newname = strdup(dst);
115 newname = basename(newname);
116 dstdir = strdup(dst);
117 dstdir = dirname(dstdir);
119 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
120 strchr(newname, '/') ){
121 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
126 len = strlen(newname);
127 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
128 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
133 fddst = open_file_or_dir(dstdir);
135 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
139 printf("Create subvolume '%s/%s'\n", dstdir, newname);
141 struct btrfs_ioctl_vol_args_v2 args;
143 memset(&args, 0, sizeof(args));
144 strncpy_null(args.name, newname);
145 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
146 args.size = qgroup_inherit_size(inherit);
147 args.qgroup_inherit = inherit;
149 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
151 struct btrfs_ioctl_vol_args args;
153 memset(&args, 0, sizeof(args));
154 strncpy_null(args.name, newname);
156 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
160 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
165 retval = 0; /* success */
175 * test if path is a subvolume:
176 * this function return
177 * 0-> path exists but it is not a subvolume
178 * 1-> path exists and it is a subvolume
179 * -1 -> path is unaccessible
181 int test_issubvolume(char *path)
186 res = stat(path, &st);
190 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
193 static const char * const cmd_subvol_delete_usage[] = {
194 "btrfs subvolume delete <subvolume> [<subvolume>...]",
195 "Delete subvolume(s)",
199 static int cmd_subvol_delete(int argc, char **argv)
201 int res, fd, len, e, cnt = 1, ret = 0;
202 struct btrfs_ioctl_vol_args args;
203 char *dname, *vname, *cpath;
207 usage(cmd_subvol_delete_usage);
212 res = test_issubvolume(path);
214 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
219 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
224 cpath = realpath(path, 0);
225 dname = strdup(cpath);
226 dname = dirname(dname);
227 vname = strdup(cpath);
228 vname = basename(vname);
231 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
232 strchr(vname, '/') ){
233 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
240 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
241 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
247 fd = open_file_or_dir(dname);
249 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
254 printf("Delete subvolume '%s/%s'\n", dname, vname);
255 strncpy_null(args.name, vname);
256 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
262 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
263 dname, vname, strerror(e));
278 * - uppercase for filters and sort options
279 * - lowercase for enabling specific items in the output
281 static const char * const cmd_subvol_list_usage[] = {
282 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
283 "[--sort=gen,ogen,rootid,path] <path>",
284 "List subvolumes (and snapshots)",
286 "-p print parent ID",
287 "-a print all the subvolumes in the filesystem and",
288 " distinguish absolute and relative path with respect",
289 " to the given <path>",
290 "-c print the ogeneration of the subvolume",
291 "-g print the generation of the subvolume",
292 "-o print only subvolumes bellow specified path",
293 "-u print the uuid of subvolumes (and snapshots)",
294 "-q print the parent uuid of the snapshots",
295 "-t print the result as a table",
296 "-s list snapshots only in the filesystem",
297 "-r list readonly subvolumes (including snapshots)",
299 " filter the subvolumes by generation",
300 " (+value: >= value; -value: <= value; value: = value)",
302 " filter the subvolumes by ogeneration",
303 " (+value: >= value; -value: <= value; value: = value)",
304 "--sort=gen,ogen,rootid,path",
305 " list the subvolume in order of gen, ogen, rootid or path",
306 " you also can add '+' or '-' in front of each items.",
307 " (+:ascending, -:descending, ascending default)",
311 static int cmd_subvol_list(int argc, char **argv)
313 struct btrfs_list_filter_set *filter_set;
314 struct btrfs_list_comparer_set *comparer_set;
318 int ret = -1, uerr = 0;
321 int is_tab_result = 0;
323 int is_only_in_path = 0;
324 struct option long_options[] = {
325 {"sort", 1, NULL, 'S'},
329 filter_set = btrfs_list_alloc_filter_set();
330 comparer_set = btrfs_list_alloc_comparer_set();
334 c = getopt_long(argc, argv,
335 "acgopqsurG:C:t", long_options, NULL);
341 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
347 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
350 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
359 btrfs_list_setup_filter(&filter_set,
360 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
362 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
363 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
366 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
369 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
372 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
375 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
376 ret = btrfs_list_parse_filter_string(optarg,
378 BTRFS_LIST_FILTER_GEN);
386 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
387 ret = btrfs_list_parse_filter_string(optarg,
389 BTRFS_LIST_FILTER_CGEN);
396 ret = btrfs_list_parse_sort_string(optarg,
411 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
414 if (check_argc_exact(argc - optind, 1)) {
419 subvol = argv[optind];
421 ret = test_issubvolume(subvol);
423 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
427 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
432 fd = open_file_or_dir(subvol);
435 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
439 ret = btrfs_list_get_path_rootid(fd, &top_id);
441 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
446 btrfs_list_setup_filter(&filter_set,
447 BTRFS_LIST_FILTER_FULL_PATH,
449 else if (is_only_in_path)
450 btrfs_list_setup_filter(&filter_set,
451 BTRFS_LIST_FILTER_TOPID_EQUAL,
454 /* by default we shall print the following columns*/
455 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
456 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
457 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
458 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
461 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
462 BTRFS_LIST_LAYOUT_TABLE,
463 !is_list_all && !is_only_in_path, NULL);
465 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
466 BTRFS_LIST_LAYOUT_DEFAULT,
467 !is_list_all && !is_only_in_path, NULL);
473 btrfs_list_free_filter_set(filter_set);
475 btrfs_list_free_comparer_set(comparer_set);
477 usage(cmd_subvol_list_usage);
482 static const char * const cmd_snapshot_usage[] = {
483 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
484 "Create a snapshot of the subvolume",
485 "Create a writable/readonly snapshot of the subvolume <source> with",
486 "the name <name> in the <dest> directory",
488 "-r create a readonly snapshot",
492 static int cmd_snapshot(int argc, char **argv)
496 int fd = -1, fddst = -1;
497 int len, readonly = 0;
500 struct btrfs_ioctl_vol_args_v2 args;
501 struct btrfs_qgroup_inherit *inherit = NULL;
504 memset(&args, 0, sizeof(args));
506 int c = getopt(argc, argv, "c:i:r");
512 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
517 res = qgroup_inherit_add_group(&inherit, optarg);
525 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
530 usage(cmd_snapshot_usage);
534 if (check_argc_exact(argc - optind, 2))
535 usage(cmd_snapshot_usage);
537 subvol = argv[optind];
538 dst = argv[optind + 1];
540 retval = 1; /* failure */
541 res = test_issubvolume(subvol);
543 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
547 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
551 res = test_isdir(dst);
553 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
558 newname = strdup(subvol);
559 newname = basename(newname);
562 newname = strdup(dst);
563 newname = basename(newname);
564 dstdir = strdup(dst);
565 dstdir = dirname(dstdir);
568 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
569 strchr(newname, '/') ){
570 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
575 len = strlen(newname);
576 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
577 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
582 fddst = open_file_or_dir(dstdir);
584 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
588 fd = open_file_or_dir(subvol);
590 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
595 args.flags |= BTRFS_SUBVOL_RDONLY;
596 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
597 subvol, dstdir, newname);
599 printf("Create a snapshot of '%s' in '%s/%s'\n",
600 subvol, dstdir, newname);
605 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
606 args.size = qgroup_inherit_size(inherit);
607 args.qgroup_inherit = inherit;
609 strncpy_null(args.name, newname);
611 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
614 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
615 subvol, strerror(errno));
619 retval = 0; /* success */
631 static const char * const cmd_subvol_get_default_usage[] = {
632 "btrfs subvolume get-default <path>",
633 "Get the default subvolume of a filesystem",
637 static int cmd_subvol_get_default(int argc, char **argv)
642 struct btrfs_list_filter_set *filter_set;
645 if (check_argc_exact(argc, 2))
646 usage(cmd_subvol_get_default_usage);
650 ret = test_issubvolume(subvol);
652 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
656 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
660 fd = open_file_or_dir(subvol);
662 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
666 ret = btrfs_list_get_default_subvolume(fd, &default_id);
668 fprintf(stderr, "ERROR: can't perform the search - %s\n",
674 if (default_id == 0) {
675 fprintf(stderr, "ERROR: 'default' dir item not found\n");
679 /* no need to resolve roots if FS_TREE is default */
680 if (default_id == BTRFS_FS_TREE_OBJECTID) {
681 printf("ID 5 (FS_TREE)\n");
685 filter_set = btrfs_list_alloc_filter_set();
686 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
689 /* by default we shall print the following columns*/
690 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
691 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
692 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
693 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
695 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
696 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
699 btrfs_list_free_filter_set(filter_set);
708 static const char * const cmd_subvol_set_default_usage[] = {
709 "btrfs subvolume set-default <subvolid> <path>",
710 "Set the default subvolume of a filesystem",
714 static int cmd_subvol_set_default(int argc, char **argv)
721 if (check_argc_exact(argc, 3))
722 usage(cmd_subvol_set_default_usage);
727 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
728 if (errno == ERANGE) {
729 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
733 fd = open_file_or_dir(path);
735 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
739 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
743 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
750 static const char * const cmd_find_new_usage[] = {
751 "btrfs subvolume find-new <path> <lastgen>",
752 "List the recently modified files in a filesystem",
756 static int cmd_find_new(int argc, char **argv)
763 if (check_argc_exact(argc, 3))
764 usage(cmd_find_new_usage);
767 last_gen = atoll(argv[2]);
769 ret = test_issubvolume(subvol);
771 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
775 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
779 fd = open_file_or_dir(subvol);
781 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
784 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
791 static const char * const cmd_subvol_show_usage[] = {
792 "btrfs subvolume show <subvol-path>",
793 "Show more information of the subvolume",
797 static int cmd_subvol_show(int argc, char **argv)
799 struct root_info get_ri;
800 struct btrfs_list_filter_set *filter_set;
803 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
804 char raw_prefix[] = "\t\t\t\t";
806 int fd = -1, mntfd = -1;
809 if (check_argc_exact(argc, 2))
810 usage(cmd_subvol_show_usage);
812 fullpath = realpath(argv[1], 0);
814 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
815 argv[1], strerror(errno));
819 ret = test_issubvolume(fullpath);
821 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
825 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
830 ret = find_mount_root(fullpath, &mnt);
832 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
833 "%s\n", fullpath, strerror(-ret));
837 svpath = get_subvol_name(mnt, fullpath);
839 fd = open_file_or_dir(fullpath);
841 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
845 ret = btrfs_list_get_path_rootid(fd, &sv_id);
847 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
852 mntfd = open_file_or_dir(mnt);
854 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
858 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
860 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
864 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
865 printf("%s is btrfs root\n", fullpath);
869 memset(&get_ri, 0, sizeof(get_ri));
870 get_ri.root_id = sv_id;
872 if (btrfs_get_subvol(mntfd, &get_ri)) {
873 fprintf(stderr, "ERROR: can't find '%s'\n",
880 printf("%s\n", fullpath);
881 printf("\tName: \t\t\t%s\n", get_ri.name);
883 if (uuid_is_null(get_ri.uuid))
884 strcpy(uuidparse, "-");
886 uuid_unparse(get_ri.uuid, uuidparse);
887 printf("\tuuid: \t\t\t%s\n", uuidparse);
889 if (uuid_is_null(get_ri.puuid))
890 strcpy(uuidparse, "-");
892 uuid_unparse(get_ri.puuid, uuidparse);
893 printf("\tParent uuid: \t\t%s\n", uuidparse);
896 strftime(tstr, 256, "%Y-%m-%d %X",
897 localtime(&get_ri.otime));
900 printf("\tCreation time: \t\t%s\n", tstr);
902 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
903 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
904 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
905 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
906 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
908 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
909 printf("\tFlags: \t\t\treadonly\n");
911 printf("\tFlags: \t\t\t-\n");
913 /* print the snapshots of the given subvol if any*/
914 printf("\tSnapshot(s):\n");
915 filter_set = btrfs_list_alloc_filter_set();
916 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
918 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
919 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
927 if (get_ri.full_path)
928 free(get_ri.full_path);
930 btrfs_list_free_filter_set(filter_set);
945 const struct cmd_group subvolume_cmd_group = {
946 subvolume_cmd_group_usage, NULL, {
947 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
948 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
949 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
950 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
951 { "get-default", cmd_subvol_get_default,
952 cmd_subvol_get_default_usage, NULL, 0 },
953 { "set-default", cmd_subvol_set_default,
954 cmd_subvol_set_default_usage, NULL, 0 },
955 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
956 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
961 int cmd_subvolume(int argc, char **argv)
963 return handle_command_group(&subvolume_cmd_group, argc, argv);