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 [-i <qgroupid>] [<dest>/]<name>",
66 "Create a subvolume <name> in <dest>. If <dest> is not given",
67 "subvolume <name> will be created in the current directory.",
69 "-i <qgroupid> add the newly created subvolume to a qgroup. This",
70 " option can be given multiple times.",
74 static int cmd_subvol_create(int argc, char **argv)
81 struct btrfs_qgroup_inherit *inherit = NULL;
85 int c = getopt(argc, argv, "c:i:r");
91 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
98 res = qgroup_inherit_add_group(&inherit, optarg);
105 usage(cmd_subvol_create_usage);
109 if (check_argc_exact(argc - optind, 1))
110 usage(cmd_subvol_create_usage);
114 retval = 1; /* failure */
115 res = test_isdir(dst);
117 fprintf(stderr, "ERROR: '%s' exists\n", dst);
121 newname = strdup(dst);
122 newname = basename(newname);
123 dstdir = strdup(dst);
124 dstdir = dirname(dstdir);
126 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
127 strchr(newname, '/') ){
128 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
133 len = strlen(newname);
134 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
135 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
140 fddst = open_file_or_dir(dstdir);
142 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
146 printf("Create subvolume '%s/%s'\n", dstdir, newname);
148 struct btrfs_ioctl_vol_args_v2 args;
150 memset(&args, 0, sizeof(args));
151 strncpy_null(args.name, newname);
152 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
153 args.size = qgroup_inherit_size(inherit);
154 args.qgroup_inherit = inherit;
156 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
158 struct btrfs_ioctl_vol_args args;
160 memset(&args, 0, sizeof(args));
161 strncpy_null(args.name, newname);
163 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
167 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
172 retval = 0; /* success */
182 * test if path is a subvolume:
183 * this function return
184 * 0-> path exists but it is not a subvolume
185 * 1-> path exists and it is a subvolume
186 * -1 -> path is unaccessible
188 int test_issubvolume(char *path)
193 res = stat(path, &st);
197 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
200 static const char * const cmd_subvol_delete_usage[] = {
201 "btrfs subvolume delete <subvolume> [<subvolume>...]",
202 "Delete subvolume(s)",
206 static int cmd_subvol_delete(int argc, char **argv)
208 int res, fd, len, e, cnt = 1, ret = 0;
209 struct btrfs_ioctl_vol_args args;
210 char *dname, *vname, *cpath;
214 usage(cmd_subvol_delete_usage);
219 res = test_issubvolume(path);
221 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
226 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
231 cpath = realpath(path, 0);
232 dname = strdup(cpath);
233 dname = dirname(dname);
234 vname = strdup(cpath);
235 vname = basename(vname);
238 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
239 strchr(vname, '/') ){
240 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
247 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
248 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
254 fd = open_file_or_dir(dname);
256 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
261 printf("Delete subvolume '%s/%s'\n", dname, vname);
262 strncpy_null(args.name, vname);
263 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
269 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
270 dname, vname, strerror(e));
285 * - uppercase for filters and sort options
286 * - lowercase for enabling specific items in the output
288 static const char * const cmd_subvol_list_usage[] = {
289 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
290 "[--sort=gen,ogen,rootid,path] <path>",
291 "List subvolumes (and snapshots)",
293 "-p print parent ID",
294 "-a print all the subvolumes in the filesystem and",
295 " distinguish absolute and relative path with respect",
296 " to the given <path>",
297 "-c print the ogeneration of the subvolume",
298 "-g print the generation of the subvolume",
299 "-o print only subvolumes bellow specified path",
300 "-u print the uuid of subvolumes (and snapshots)",
301 "-q print the parent uuid of the snapshots",
302 "-t print the result as a table",
303 "-s list snapshots only in the filesystem",
304 "-r list readonly subvolumes (including snapshots)",
306 " filter the subvolumes by generation",
307 " (+value: >= value; -value: <= value; value: = value)",
309 " filter the subvolumes by ogeneration",
310 " (+value: >= value; -value: <= value; value: = value)",
311 "--sort=gen,ogen,rootid,path",
312 " list the subvolume in order of gen, ogen, rootid or path",
313 " you also can add '+' or '-' in front of each items.",
314 " (+:ascending, -:descending, ascending default)",
318 static int cmd_subvol_list(int argc, char **argv)
320 struct btrfs_list_filter_set *filter_set;
321 struct btrfs_list_comparer_set *comparer_set;
325 int ret = -1, uerr = 0;
328 int is_tab_result = 0;
330 int is_only_in_path = 0;
331 struct option long_options[] = {
332 {"sort", 1, NULL, 'S'},
336 filter_set = btrfs_list_alloc_filter_set();
337 comparer_set = btrfs_list_alloc_comparer_set();
341 c = getopt_long(argc, argv,
342 "acgopqsurG:C:t", long_options, NULL);
348 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
354 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
357 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
366 btrfs_list_setup_filter(&filter_set,
367 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
369 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
370 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
373 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
376 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
379 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
382 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
383 ret = btrfs_list_parse_filter_string(optarg,
385 BTRFS_LIST_FILTER_GEN);
393 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
394 ret = btrfs_list_parse_filter_string(optarg,
396 BTRFS_LIST_FILTER_CGEN);
403 ret = btrfs_list_parse_sort_string(optarg,
418 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
421 if (check_argc_exact(argc - optind, 1)) {
426 subvol = argv[optind];
428 ret = test_issubvolume(subvol);
430 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
434 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
439 fd = open_file_or_dir(subvol);
442 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
446 ret = btrfs_list_get_path_rootid(fd, &top_id);
448 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
453 btrfs_list_setup_filter(&filter_set,
454 BTRFS_LIST_FILTER_FULL_PATH,
456 else if (is_only_in_path)
457 btrfs_list_setup_filter(&filter_set,
458 BTRFS_LIST_FILTER_TOPID_EQUAL,
461 /* by default we shall print the following columns*/
462 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
463 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
464 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
465 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
468 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
469 BTRFS_LIST_LAYOUT_TABLE,
470 !is_list_all && !is_only_in_path, NULL);
472 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
473 BTRFS_LIST_LAYOUT_DEFAULT,
474 !is_list_all && !is_only_in_path, NULL);
480 btrfs_list_free_filter_set(filter_set);
482 btrfs_list_free_comparer_set(comparer_set);
484 usage(cmd_subvol_list_usage);
489 static const char * const cmd_snapshot_usage[] = {
490 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
491 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> [<dest>/]<name>",
492 "Create a snapshot of the subvolume",
493 "Create a writable/readonly snapshot of the subvolume <source> with",
494 "the name <name> in the <dest> directory",
496 "-r create a readonly snapshot",
497 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
498 " option can be given multiple times.",
502 static int cmd_snapshot(int argc, char **argv)
506 int fd = -1, fddst = -1;
507 int len, readonly = 0;
510 struct btrfs_ioctl_vol_args_v2 args;
511 struct btrfs_qgroup_inherit *inherit = NULL;
514 memset(&args, 0, sizeof(args));
516 int c = getopt(argc, argv, "c:i:r");
522 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
529 res = qgroup_inherit_add_group(&inherit, optarg);
539 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
546 usage(cmd_snapshot_usage);
550 if (check_argc_exact(argc - optind, 2))
551 usage(cmd_snapshot_usage);
553 subvol = argv[optind];
554 dst = argv[optind + 1];
556 retval = 1; /* failure */
557 res = test_issubvolume(subvol);
559 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
563 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
567 res = test_isdir(dst);
569 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
574 newname = strdup(subvol);
575 newname = basename(newname);
578 newname = strdup(dst);
579 newname = basename(newname);
580 dstdir = strdup(dst);
581 dstdir = dirname(dstdir);
584 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
585 strchr(newname, '/') ){
586 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
591 len = strlen(newname);
592 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
593 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
598 fddst = open_file_or_dir(dstdir);
600 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
604 fd = open_file_or_dir(subvol);
606 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
611 args.flags |= BTRFS_SUBVOL_RDONLY;
612 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
613 subvol, dstdir, newname);
615 printf("Create a snapshot of '%s' in '%s/%s'\n",
616 subvol, dstdir, newname);
621 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
622 args.size = qgroup_inherit_size(inherit);
623 args.qgroup_inherit = inherit;
625 strncpy_null(args.name, newname);
627 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
630 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
631 subvol, strerror(errno));
635 retval = 0; /* success */
647 static const char * const cmd_subvol_get_default_usage[] = {
648 "btrfs subvolume get-default <path>",
649 "Get the default subvolume of a filesystem",
653 static int cmd_subvol_get_default(int argc, char **argv)
658 struct btrfs_list_filter_set *filter_set;
661 if (check_argc_exact(argc, 2))
662 usage(cmd_subvol_get_default_usage);
666 ret = test_issubvolume(subvol);
668 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
672 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
676 fd = open_file_or_dir(subvol);
678 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
682 ret = btrfs_list_get_default_subvolume(fd, &default_id);
684 fprintf(stderr, "ERROR: can't perform the search - %s\n",
690 if (default_id == 0) {
691 fprintf(stderr, "ERROR: 'default' dir item not found\n");
695 /* no need to resolve roots if FS_TREE is default */
696 if (default_id == BTRFS_FS_TREE_OBJECTID) {
697 printf("ID 5 (FS_TREE)\n");
701 filter_set = btrfs_list_alloc_filter_set();
702 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
705 /* by default we shall print the following columns*/
706 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
707 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
708 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
709 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
711 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
712 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
715 btrfs_list_free_filter_set(filter_set);
724 static const char * const cmd_subvol_set_default_usage[] = {
725 "btrfs subvolume set-default <subvolid> <path>",
726 "Set the default subvolume of a filesystem",
730 static int cmd_subvol_set_default(int argc, char **argv)
737 if (check_argc_exact(argc, 3))
738 usage(cmd_subvol_set_default_usage);
743 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
744 if (errno == ERANGE) {
745 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
749 fd = open_file_or_dir(path);
751 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
755 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
759 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
766 static const char * const cmd_find_new_usage[] = {
767 "btrfs subvolume find-new <path> <lastgen>",
768 "List the recently modified files in a filesystem",
772 static int cmd_find_new(int argc, char **argv)
779 if (check_argc_exact(argc, 3))
780 usage(cmd_find_new_usage);
783 last_gen = atoll(argv[2]);
785 ret = test_issubvolume(subvol);
787 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
791 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
795 fd = open_file_or_dir(subvol);
797 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
800 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
807 static const char * const cmd_subvol_show_usage[] = {
808 "btrfs subvolume show <subvol-path>",
809 "Show more information of the subvolume",
813 static int cmd_subvol_show(int argc, char **argv)
815 struct root_info get_ri;
816 struct btrfs_list_filter_set *filter_set;
819 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
820 char raw_prefix[] = "\t\t\t\t";
822 int fd = -1, mntfd = -1;
825 if (check_argc_exact(argc, 2))
826 usage(cmd_subvol_show_usage);
828 fullpath = realpath(argv[1], 0);
830 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
831 argv[1], strerror(errno));
835 ret = test_issubvolume(fullpath);
837 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
841 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
846 ret = find_mount_root(fullpath, &mnt);
848 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
849 "%s\n", fullpath, strerror(-ret));
853 svpath = get_subvol_name(mnt, fullpath);
855 fd = open_file_or_dir(fullpath);
857 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
861 ret = btrfs_list_get_path_rootid(fd, &sv_id);
863 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
868 mntfd = open_file_or_dir(mnt);
870 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
874 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
876 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
880 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
881 printf("%s is btrfs root\n", fullpath);
885 memset(&get_ri, 0, sizeof(get_ri));
886 get_ri.root_id = sv_id;
888 if (btrfs_get_subvol(mntfd, &get_ri)) {
889 fprintf(stderr, "ERROR: can't find '%s'\n",
896 printf("%s\n", fullpath);
897 printf("\tName: \t\t\t%s\n", get_ri.name);
899 if (uuid_is_null(get_ri.uuid))
900 strcpy(uuidparse, "-");
902 uuid_unparse(get_ri.uuid, uuidparse);
903 printf("\tuuid: \t\t\t%s\n", uuidparse);
905 if (uuid_is_null(get_ri.puuid))
906 strcpy(uuidparse, "-");
908 uuid_unparse(get_ri.puuid, uuidparse);
909 printf("\tParent uuid: \t\t%s\n", uuidparse);
914 localtime_r(&get_ri.otime, &tm);
915 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
918 printf("\tCreation time: \t\t%s\n", tstr);
920 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
921 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
922 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
923 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
924 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
926 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
927 printf("\tFlags: \t\t\treadonly\n");
929 printf("\tFlags: \t\t\t-\n");
931 /* print the snapshots of the given subvol if any*/
932 printf("\tSnapshot(s):\n");
933 filter_set = btrfs_list_alloc_filter_set();
934 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
935 (u64)(unsigned long)get_ri.uuid);
936 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
937 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
945 if (get_ri.full_path)
946 free(get_ri.full_path);
948 btrfs_list_free_filter_set(filter_set);
963 const struct cmd_group subvolume_cmd_group = {
964 subvolume_cmd_group_usage, NULL, {
965 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
966 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
967 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
968 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
969 { "get-default", cmd_subvol_get_default,
970 cmd_subvol_get_default_usage, NULL, 0 },
971 { "set-default", cmd_subvol_set_default,
972 cmd_subvol_set_default_usage, NULL, 0 },
973 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
974 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
979 int cmd_subvolume(int argc, char **argv)
981 return handle_command_group(&subvolume_cmd_group, argc, argv);