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));
300 * - uppercase for filters and sort options
301 * - lowercase for enabling specific items in the output
303 static const char * const cmd_subvol_list_usage[] = {
304 "btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] "
305 "[--sort=gen,ogen,rootid,path] <path>",
306 "List subvolumes (and snapshots)",
308 "-p print parent ID",
309 "-a print all the subvolumes in the filesystem and",
310 " distinguish absolute and relative path with respect",
311 " to the given <path>",
312 "-c print the ogeneration of the subvolume",
313 "-g print the generation of the subvolume",
314 "-o print only subvolumes bellow specified path",
315 "-u print the uuid of subvolumes (and snapshots)",
316 "-q print the parent uuid of the snapshots",
317 "-t print the result as a table",
318 "-s list snapshots only in the filesystem",
319 "-r list readonly subvolumes (including snapshots)",
321 " filter the subvolumes by generation",
322 " (+value: >= value; -value: <= value; value: = value)",
324 " filter the subvolumes by ogeneration",
325 " (+value: >= value; -value: <= value; value: = value)",
326 "--sort=gen,ogen,rootid,path",
327 " list the subvolume in order of gen, ogen, rootid or path",
328 " you also can add '+' or '-' in front of each items.",
329 " (+:ascending, -:descending, ascending default)",
333 static int cmd_subvol_list(int argc, char **argv)
335 struct btrfs_list_filter_set *filter_set;
336 struct btrfs_list_comparer_set *comparer_set;
340 int ret = -1, uerr = 0;
343 int is_tab_result = 0;
345 int is_only_in_path = 0;
346 struct option long_options[] = {
347 {"sort", 1, NULL, 'S'},
350 DIR *dirstream = NULL;
352 filter_set = btrfs_list_alloc_filter_set();
353 comparer_set = btrfs_list_alloc_comparer_set();
357 c = getopt_long(argc, argv,
358 "acgopqsurG:C:t", long_options, NULL);
364 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
370 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
373 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
382 btrfs_list_setup_filter(&filter_set,
383 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
385 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
386 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
389 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
392 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
395 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
398 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
399 ret = btrfs_list_parse_filter_string(optarg,
401 BTRFS_LIST_FILTER_GEN);
409 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
410 ret = btrfs_list_parse_filter_string(optarg,
412 BTRFS_LIST_FILTER_CGEN);
419 ret = btrfs_list_parse_sort_string(optarg,
434 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
437 if (check_argc_exact(argc - optind, 1)) {
442 subvol = argv[optind];
443 fd = open_file_or_dir(subvol, &dirstream);
446 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
450 ret = btrfs_list_get_path_rootid(fd, &top_id);
452 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
457 btrfs_list_setup_filter(&filter_set,
458 BTRFS_LIST_FILTER_FULL_PATH,
460 else if (is_only_in_path)
461 btrfs_list_setup_filter(&filter_set,
462 BTRFS_LIST_FILTER_TOPID_EQUAL,
465 /* by default we shall print the following columns*/
466 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
467 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
468 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
469 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
472 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
473 BTRFS_LIST_LAYOUT_TABLE,
474 !is_list_all && !is_only_in_path, NULL);
476 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
477 BTRFS_LIST_LAYOUT_DEFAULT,
478 !is_list_all && !is_only_in_path, NULL);
481 close_file_or_dir(fd, dirstream);
483 btrfs_list_free_filter_set(filter_set);
485 btrfs_list_free_comparer_set(comparer_set);
487 usage(cmd_subvol_list_usage);
491 static const char * const cmd_snapshot_usage[] = {
492 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
493 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
494 "Create a snapshot of the subvolume",
495 "Create a writable/readonly snapshot of the subvolume <source> with",
496 "the name <name> in the <dest> directory. If only <dest> is given,",
497 "the subvolume will be named the basename of <source>.",
499 "-r create a readonly snapshot",
500 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
501 " option can be given multiple times.",
505 static int cmd_snapshot(int argc, char **argv)
509 int fd = -1, fddst = -1;
510 int len, readonly = 0;
511 char *dupname = NULL;
515 struct btrfs_ioctl_vol_args_v2 args;
516 struct btrfs_qgroup_inherit *inherit = NULL;
517 DIR *dirstream1 = NULL, *dirstream2 = NULL;
520 memset(&args, 0, sizeof(args));
522 int c = getopt(argc, argv, "c:i:r");
528 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
535 res = qgroup_inherit_add_group(&inherit, optarg);
545 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
552 usage(cmd_snapshot_usage);
556 if (check_argc_exact(argc - optind, 2))
557 usage(cmd_snapshot_usage);
559 subvol = argv[optind];
560 dst = argv[optind + 1];
562 retval = 1; /* failure */
563 res = test_issubvolume(subvol);
565 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
569 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
573 res = test_isdir(dst);
575 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
580 dupname = strdup(subvol);
581 newname = basename(dupname);
584 dupname = strdup(dst);
585 newname = basename(dupname);
586 dupdir = strdup(dst);
587 dstdir = dirname(dupdir);
590 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
591 strchr(newname, '/') ){
592 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
597 len = strlen(newname);
598 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
599 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
604 fddst = open_file_or_dir(dstdir, &dirstream1);
606 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
610 fd = open_file_or_dir(subvol, &dirstream2);
612 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
617 args.flags |= BTRFS_SUBVOL_RDONLY;
618 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
619 subvol, dstdir, newname);
621 printf("Create a snapshot of '%s' in '%s/%s'\n",
622 subvol, dstdir, newname);
627 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
628 args.size = qgroup_inherit_size(inherit);
629 args.qgroup_inherit = inherit;
631 strncpy_null(args.name, newname);
633 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
636 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
637 subvol, strerror(errno));
641 retval = 0; /* success */
644 close_file_or_dir(fddst, dirstream1);
645 close_file_or_dir(fd, dirstream2);
653 static const char * const cmd_subvol_get_default_usage[] = {
654 "btrfs subvolume get-default <path>",
655 "Get the default subvolume of a filesystem",
659 static int cmd_subvol_get_default(int argc, char **argv)
664 struct btrfs_list_filter_set *filter_set;
666 DIR *dirstream = NULL;
668 if (check_argc_exact(argc, 2))
669 usage(cmd_subvol_get_default_usage);
672 fd = open_file_or_dir(subvol, &dirstream);
674 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
678 ret = btrfs_list_get_default_subvolume(fd, &default_id);
680 fprintf(stderr, "ERROR: can't perform the search - %s\n",
686 if (default_id == 0) {
687 fprintf(stderr, "ERROR: 'default' dir item not found\n");
691 /* no need to resolve roots if FS_TREE is default */
692 if (default_id == BTRFS_FS_TREE_OBJECTID) {
693 printf("ID 5 (FS_TREE)\n");
697 filter_set = btrfs_list_alloc_filter_set();
698 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
701 /* by default we shall print the following columns*/
702 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
703 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
704 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
705 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
707 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
708 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
711 btrfs_list_free_filter_set(filter_set);
713 close_file_or_dir(fd, dirstream);
717 static const char * const cmd_subvol_set_default_usage[] = {
718 "btrfs subvolume set-default <subvolid> <path>",
719 "Set the default subvolume of a filesystem",
723 static int cmd_subvol_set_default(int argc, char **argv)
729 DIR *dirstream = NULL;
731 if (check_argc_exact(argc, 3))
732 usage(cmd_subvol_set_default_usage);
737 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
738 if (errno == ERANGE) {
739 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
743 fd = open_file_or_dir(path, &dirstream);
745 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
749 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
751 close_file_or_dir(fd, dirstream);
753 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
760 static const char * const cmd_find_new_usage[] = {
761 "btrfs subvolume find-new <path> <lastgen>",
762 "List the recently modified files in a filesystem",
766 static int cmd_find_new(int argc, char **argv)
772 DIR *dirstream = NULL;
774 if (check_argc_exact(argc, 3))
775 usage(cmd_find_new_usage);
778 last_gen = atoll(argv[2]);
780 ret = test_issubvolume(subvol);
782 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
786 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
790 fd = open_file_or_dir(subvol, &dirstream);
792 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
796 ret = ioctl(fd, BTRFS_IOC_SYNC);
798 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
799 subvol, strerror(errno));
800 close_file_or_dir(fd, dirstream);
804 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
805 close_file_or_dir(fd, dirstream);
809 static const char * const cmd_subvol_show_usage[] = {
810 "btrfs subvolume show <subvol-path>",
811 "Show more information of the subvolume",
815 static int cmd_subvol_show(int argc, char **argv)
817 struct root_info get_ri;
818 struct btrfs_list_filter_set *filter_set;
821 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
822 char raw_prefix[] = "\t\t\t\t";
824 int fd = -1, mntfd = -1;
826 DIR *dirstream1 = NULL, *dirstream2 = NULL;
828 if (check_argc_exact(argc, 2))
829 usage(cmd_subvol_show_usage);
831 fullpath = realpath(argv[1], NULL);
833 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
834 argv[1], strerror(errno));
838 ret = test_issubvolume(fullpath);
840 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
844 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
848 ret = find_mount_root(fullpath, &mnt);
850 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
851 "%s\n", fullpath, strerror(-ret));
855 svpath = get_subvol_name(mnt, fullpath);
857 fd = open_file_or_dir(fullpath, &dirstream1);
859 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
863 ret = btrfs_list_get_path_rootid(fd, &sv_id);
865 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
870 mntfd = open_file_or_dir(mnt, &dirstream2);
872 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
876 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
878 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
882 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
883 printf("%s is btrfs root\n", fullpath);
887 memset(&get_ri, 0, sizeof(get_ri));
888 get_ri.root_id = sv_id;
890 if (btrfs_get_subvol(mntfd, &get_ri)) {
891 fprintf(stderr, "ERROR: can't find '%s'\n",
898 printf("%s\n", fullpath);
899 printf("\tName: \t\t\t%s\n", get_ri.name);
901 if (uuid_is_null(get_ri.uuid))
902 strcpy(uuidparse, "-");
904 uuid_unparse(get_ri.uuid, uuidparse);
905 printf("\tuuid: \t\t\t%s\n", uuidparse);
907 if (uuid_is_null(get_ri.puuid))
908 strcpy(uuidparse, "-");
910 uuid_unparse(get_ri.puuid, uuidparse);
911 printf("\tParent uuid: \t\t%s\n", uuidparse);
916 localtime_r(&get_ri.otime, &tm);
917 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
920 printf("\tCreation time: \t\t%s\n", tstr);
922 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
923 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
924 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
925 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
926 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
928 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
929 printf("\tFlags: \t\t\treadonly\n");
931 printf("\tFlags: \t\t\t-\n");
933 /* print the snapshots of the given subvol if any*/
934 printf("\tSnapshot(s):\n");
935 filter_set = btrfs_list_alloc_filter_set();
936 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
937 (u64)(unsigned long)get_ri.uuid);
938 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
939 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
947 if (get_ri.full_path)
948 free(get_ri.full_path);
950 btrfs_list_free_filter_set(filter_set);
953 close_file_or_dir(fd, dirstream1);
954 close_file_or_dir(mntfd, dirstream2);
962 const struct cmd_group subvolume_cmd_group = {
963 subvolume_cmd_group_usage, NULL, {
964 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
965 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
966 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
967 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
968 { "get-default", cmd_subvol_get_default,
969 cmd_subvol_get_default_usage, NULL, 0 },
970 { "set-default", cmd_subvol_set_default,
971 cmd_subvol_set_default_usage, NULL, 0 },
972 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
973 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
978 int cmd_subvolume(int argc, char **argv)
980 return handle_command_group(&subvolume_cmd_group, argc, argv);