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)
83 struct btrfs_qgroup_inherit *inherit = NULL;
84 DIR *dirstream = NULL;
88 int c = getopt(argc, argv, "c:i:");
94 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
101 res = qgroup_inherit_add_group(&inherit, optarg);
108 usage(cmd_subvol_create_usage);
112 if (check_argc_exact(argc - optind, 1))
113 usage(cmd_subvol_create_usage);
117 retval = 1; /* failure */
118 res = test_isdir(dst);
120 fprintf(stderr, "ERROR: '%s' exists\n", dst);
124 dupname = strdup(dst);
125 newname = basename(dupname);
126 dupdir = strdup(dst);
127 dstdir = dirname(dupdir);
129 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
130 strchr(newname, '/') ){
131 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
136 len = strlen(newname);
137 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
138 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
143 fddst = open_file_or_dir(dstdir, &dirstream);
145 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
149 printf("Create subvolume '%s/%s'\n", dstdir, newname);
151 struct btrfs_ioctl_vol_args_v2 args;
153 memset(&args, 0, sizeof(args));
154 strncpy_null(args.name, newname);
155 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
156 args.size = qgroup_inherit_size(inherit);
157 args.qgroup_inherit = inherit;
159 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
161 struct btrfs_ioctl_vol_args args;
163 memset(&args, 0, sizeof(args));
164 strncpy_null(args.name, newname);
166 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
170 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
175 retval = 0; /* success */
177 close_file_or_dir(fddst, dirstream);
186 * test if path is a subvolume:
187 * this function return
188 * 0-> path exists but it is not a subvolume
189 * 1-> path exists and it is a subvolume
190 * -1 -> path is unaccessible
192 int test_issubvolume(char *path)
197 res = stat(path, &st);
201 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
204 static const char * const cmd_subvol_delete_usage[] = {
205 "btrfs subvolume delete <subvolume> [<subvolume>...]",
206 "Delete subvolume(s)",
210 static int cmd_subvol_delete(int argc, char **argv)
212 int res, fd, len, e, cnt = 1, ret = 0;
213 struct btrfs_ioctl_vol_args args;
214 char *dname, *vname, *cpath;
215 char *dupdname = NULL;
216 char *dupvname = NULL;
218 DIR *dirstream = NULL;
221 usage(cmd_subvol_delete_usage);
226 res = test_issubvolume(path);
228 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
233 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
238 cpath = realpath(path, NULL);
241 fprintf(stderr, "ERROR: finding real path for '%s': %s\n",
242 path, strerror(errno));
245 dupdname = strdup(cpath);
246 dname = dirname(dupdname);
247 dupvname = strdup(cpath);
248 vname = basename(dupvname);
251 if (!strcmp(vname, ".") || !strcmp(vname, "..") ||
252 strchr(vname, '/')) {
253 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
260 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
261 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
267 fd = open_file_or_dir(dname, &dirstream);
269 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
274 printf("Delete subvolume '%s/%s'\n", dname, vname);
275 strncpy_null(args.name, vname);
276 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
279 close_file_or_dir(fd, dirstream);
282 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
283 dname, vname, strerror(e));
302 * - uppercase for filters and sort options
303 * - lowercase for enabling specific items in the output
305 static const char * const cmd_subvol_list_usage[] = {
306 "btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] "
307 "[--sort=gen,ogen,rootid,path] <path>",
308 "List subvolumes (and snapshots)",
310 "-p print parent ID",
311 "-a print all the subvolumes in the filesystem and",
312 " distinguish absolute and relative path with respect",
313 " to the given <path>",
314 "-c print the ogeneration of the subvolume",
315 "-g print the generation of the subvolume",
316 "-o print only subvolumes bellow specified path",
317 "-u print the uuid of subvolumes (and snapshots)",
318 "-q print the parent uuid of the snapshots",
319 "-t print the result as a table",
320 "-s list snapshots only in the filesystem",
321 "-r list readonly subvolumes (including snapshots)",
322 "-d list deleted subvolumes that are not yet cleaned",
324 " filter the subvolumes by generation",
325 " (+value: >= value; -value: <= value; value: = value)",
327 " filter the subvolumes by ogeneration",
328 " (+value: >= value; -value: <= value; value: = value)",
329 "--sort=gen,ogen,rootid,path",
330 " list the subvolume in order of gen, ogen, rootid or path",
331 " you also can add '+' or '-' in front of each items.",
332 " (+:ascending, -:descending, ascending default)",
336 static int cmd_subvol_list(int argc, char **argv)
338 struct btrfs_list_filter_set *filter_set;
339 struct btrfs_list_comparer_set *comparer_set;
343 int ret = -1, uerr = 0;
346 int is_tab_result = 0;
348 int is_only_in_path = 0;
349 struct option long_options[] = {
350 {"sort", 1, NULL, 'S'},
353 DIR *dirstream = NULL;
355 filter_set = btrfs_list_alloc_filter_set();
356 comparer_set = btrfs_list_alloc_comparer_set();
360 c = getopt_long(argc, argv,
361 "acdgopqsurG:C:t", long_options, NULL);
367 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
373 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
376 btrfs_list_setup_filter(&filter_set,
377 BTRFS_LIST_FILTER_DELETED,
381 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
390 btrfs_list_setup_filter(&filter_set,
391 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
393 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
394 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
397 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
400 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
403 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
406 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
407 ret = btrfs_list_parse_filter_string(optarg,
409 BTRFS_LIST_FILTER_GEN);
417 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
418 ret = btrfs_list_parse_filter_string(optarg,
420 BTRFS_LIST_FILTER_CGEN);
427 ret = btrfs_list_parse_sort_string(optarg,
442 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
445 if (check_argc_exact(argc - optind, 1)) {
450 subvol = argv[optind];
451 fd = open_file_or_dir(subvol, &dirstream);
454 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
458 ret = btrfs_list_get_path_rootid(fd, &top_id);
460 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
465 btrfs_list_setup_filter(&filter_set,
466 BTRFS_LIST_FILTER_FULL_PATH,
468 else if (is_only_in_path)
469 btrfs_list_setup_filter(&filter_set,
470 BTRFS_LIST_FILTER_TOPID_EQUAL,
473 /* by default we shall print the following columns*/
474 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
475 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
476 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
477 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
480 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
481 BTRFS_LIST_LAYOUT_TABLE,
482 !is_list_all && !is_only_in_path, NULL);
484 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
485 BTRFS_LIST_LAYOUT_DEFAULT,
486 !is_list_all && !is_only_in_path, NULL);
489 close_file_or_dir(fd, dirstream);
491 btrfs_list_free_filter_set(filter_set);
493 btrfs_list_free_comparer_set(comparer_set);
495 usage(cmd_subvol_list_usage);
499 static const char * const cmd_snapshot_usage[] = {
500 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
501 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
502 "Create a snapshot of the subvolume",
503 "Create a writable/readonly snapshot of the subvolume <source> with",
504 "the name <name> in the <dest> directory. If only <dest> is given,",
505 "the subvolume will be named the basename of <source>.",
507 "-r create a readonly snapshot",
508 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
509 " option can be given multiple times.",
513 static int cmd_snapshot(int argc, char **argv)
517 int fd = -1, fddst = -1;
518 int len, readonly = 0;
519 char *dupname = NULL;
523 struct btrfs_ioctl_vol_args_v2 args;
524 struct btrfs_qgroup_inherit *inherit = NULL;
525 DIR *dirstream1 = NULL, *dirstream2 = NULL;
528 memset(&args, 0, sizeof(args));
530 int c = getopt(argc, argv, "c:i:r");
536 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
543 res = qgroup_inherit_add_group(&inherit, optarg);
553 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
560 usage(cmd_snapshot_usage);
564 if (check_argc_exact(argc - optind, 2))
565 usage(cmd_snapshot_usage);
567 subvol = argv[optind];
568 dst = argv[optind + 1];
570 retval = 1; /* failure */
571 res = test_issubvolume(subvol);
573 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
577 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
581 res = test_isdir(dst);
583 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
588 dupname = strdup(subvol);
589 newname = basename(dupname);
592 dupname = strdup(dst);
593 newname = basename(dupname);
594 dupdir = strdup(dst);
595 dstdir = dirname(dupdir);
598 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
599 strchr(newname, '/') ){
600 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
605 len = strlen(newname);
606 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
607 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
612 fddst = open_file_or_dir(dstdir, &dirstream1);
614 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
618 fd = open_file_or_dir(subvol, &dirstream2);
620 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
625 args.flags |= BTRFS_SUBVOL_RDONLY;
626 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
627 subvol, dstdir, newname);
629 printf("Create a snapshot of '%s' in '%s/%s'\n",
630 subvol, dstdir, newname);
635 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
636 args.size = qgroup_inherit_size(inherit);
637 args.qgroup_inherit = inherit;
639 strncpy_null(args.name, newname);
641 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
644 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
645 subvol, strerror(errno));
649 retval = 0; /* success */
652 close_file_or_dir(fddst, dirstream1);
653 close_file_or_dir(fd, dirstream2);
661 static const char * const cmd_subvol_get_default_usage[] = {
662 "btrfs subvolume get-default <path>",
663 "Get the default subvolume of a filesystem",
667 static int cmd_subvol_get_default(int argc, char **argv)
672 struct btrfs_list_filter_set *filter_set;
674 DIR *dirstream = NULL;
676 if (check_argc_exact(argc, 2))
677 usage(cmd_subvol_get_default_usage);
680 fd = open_file_or_dir(subvol, &dirstream);
682 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
686 ret = btrfs_list_get_default_subvolume(fd, &default_id);
688 fprintf(stderr, "ERROR: can't perform the search - %s\n",
694 if (default_id == 0) {
695 fprintf(stderr, "ERROR: 'default' dir item not found\n");
699 /* no need to resolve roots if FS_TREE is default */
700 if (default_id == BTRFS_FS_TREE_OBJECTID) {
701 printf("ID 5 (FS_TREE)\n");
706 filter_set = btrfs_list_alloc_filter_set();
707 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
710 /* by default we shall print the following columns*/
711 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
712 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
713 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
714 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
716 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
717 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
720 btrfs_list_free_filter_set(filter_set);
722 close_file_or_dir(fd, dirstream);
726 static const char * const cmd_subvol_set_default_usage[] = {
727 "btrfs subvolume set-default <subvolid> <path>",
728 "Set the default subvolume of a filesystem",
732 static int cmd_subvol_set_default(int argc, char **argv)
738 DIR *dirstream = NULL;
740 if (check_argc_exact(argc, 3))
741 usage(cmd_subvol_set_default_usage);
746 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
747 if (errno == ERANGE) {
748 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
752 fd = open_file_or_dir(path, &dirstream);
754 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
758 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
760 close_file_or_dir(fd, dirstream);
762 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
769 static const char * const cmd_find_new_usage[] = {
770 "btrfs subvolume find-new <path> <lastgen>",
771 "List the recently modified files in a filesystem",
775 static int cmd_find_new(int argc, char **argv)
781 DIR *dirstream = NULL;
783 if (check_argc_exact(argc, 3))
784 usage(cmd_find_new_usage);
787 last_gen = atoll(argv[2]);
789 ret = test_issubvolume(subvol);
791 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
795 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
799 fd = open_file_or_dir(subvol, &dirstream);
801 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
805 ret = ioctl(fd, BTRFS_IOC_SYNC);
807 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
808 subvol, strerror(errno));
809 close_file_or_dir(fd, dirstream);
813 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
814 close_file_or_dir(fd, dirstream);
818 static const char * const cmd_subvol_show_usage[] = {
819 "btrfs subvolume show <subvol-path>",
820 "Show more information of the subvolume",
824 static int cmd_subvol_show(int argc, char **argv)
826 struct root_info get_ri;
827 struct btrfs_list_filter_set *filter_set;
829 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
830 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
831 char raw_prefix[] = "\t\t\t\t";
833 int fd = -1, mntfd = -1;
835 DIR *dirstream1 = NULL, *dirstream2 = NULL;
837 if (check_argc_exact(argc, 2))
838 usage(cmd_subvol_show_usage);
840 fullpath = realpath(argv[1], NULL);
842 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
843 argv[1], strerror(errno));
847 ret = test_issubvolume(fullpath);
849 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
853 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
857 ret = find_mount_root(fullpath, &mnt);
859 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
860 "%s\n", fullpath, strerror(-ret));
864 svpath = get_subvol_name(mnt, fullpath);
866 fd = open_file_or_dir(fullpath, &dirstream1);
868 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
872 ret = btrfs_list_get_path_rootid(fd, &sv_id);
874 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
879 mntfd = open_file_or_dir(mnt, &dirstream2);
881 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
885 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
887 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
891 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
892 printf("%s is btrfs root\n", fullpath);
896 memset(&get_ri, 0, sizeof(get_ri));
897 get_ri.root_id = sv_id;
899 if (btrfs_get_subvol(mntfd, &get_ri)) {
900 fprintf(stderr, "ERROR: can't find '%s'\n",
907 printf("%s\n", fullpath);
908 printf("\tName: \t\t\t%s\n", get_ri.name);
910 if (uuid_is_null(get_ri.uuid))
911 strcpy(uuidparse, "-");
913 uuid_unparse(get_ri.uuid, uuidparse);
914 printf("\tuuid: \t\t\t%s\n", uuidparse);
916 if (uuid_is_null(get_ri.puuid))
917 strcpy(uuidparse, "-");
919 uuid_unparse(get_ri.puuid, uuidparse);
920 printf("\tParent uuid: \t\t%s\n", uuidparse);
925 localtime_r(&get_ri.otime, &tm);
926 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
929 printf("\tCreation time: \t\t%s\n", tstr);
931 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
932 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
933 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
934 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
935 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
937 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
938 printf("\tFlags: \t\t\treadonly\n");
940 printf("\tFlags: \t\t\t-\n");
942 /* print the snapshots of the given subvol if any*/
943 printf("\tSnapshot(s):\n");
944 filter_set = btrfs_list_alloc_filter_set();
945 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
946 (u64)(unsigned long)get_ri.uuid);
947 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
948 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
954 free(get_ri.full_path);
955 btrfs_list_free_filter_set(filter_set);
958 close_file_or_dir(fd, dirstream1);
959 close_file_or_dir(mntfd, dirstream2);
965 const struct cmd_group subvolume_cmd_group = {
966 subvolume_cmd_group_usage, NULL, {
967 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
968 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
969 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
970 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
971 { "get-default", cmd_subvol_get_default,
972 cmd_subvol_get_default_usage, NULL, 0 },
973 { "set-default", cmd_subvol_set_default,
974 cmd_subvol_set_default_usage, NULL, 0 },
975 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
976 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
981 int cmd_subvolume(int argc, char **argv)
983 return handle_command_group(&subvolume_cmd_group, argc, argv);