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)",
320 "-d list deleted subvolumes that are not yet cleaned",
322 " filter the subvolumes by generation",
323 " (+value: >= value; -value: <= value; value: = value)",
325 " filter the subvolumes by ogeneration",
326 " (+value: >= value; -value: <= value; value: = value)",
327 "--sort=gen,ogen,rootid,path",
328 " list the subvolume in order of gen, ogen, rootid or path",
329 " you also can add '+' or '-' in front of each items.",
330 " (+:ascending, -:descending, ascending default)",
334 static int cmd_subvol_list(int argc, char **argv)
336 struct btrfs_list_filter_set *filter_set;
337 struct btrfs_list_comparer_set *comparer_set;
341 int ret = -1, uerr = 0;
344 int is_tab_result = 0;
346 int is_only_in_path = 0;
347 struct option long_options[] = {
348 {"sort", 1, NULL, 'S'},
351 DIR *dirstream = NULL;
353 filter_set = btrfs_list_alloc_filter_set();
354 comparer_set = btrfs_list_alloc_comparer_set();
358 c = getopt_long(argc, argv,
359 "acdgopqsurG:C:t", long_options, NULL);
365 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
371 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
374 btrfs_list_setup_filter(&filter_set,
375 BTRFS_LIST_FILTER_DELETED,
379 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
388 btrfs_list_setup_filter(&filter_set,
389 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
391 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
392 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
395 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
398 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
401 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
404 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
405 ret = btrfs_list_parse_filter_string(optarg,
407 BTRFS_LIST_FILTER_GEN);
415 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
416 ret = btrfs_list_parse_filter_string(optarg,
418 BTRFS_LIST_FILTER_CGEN);
425 ret = btrfs_list_parse_sort_string(optarg,
440 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
443 if (check_argc_exact(argc - optind, 1)) {
448 subvol = argv[optind];
449 fd = open_file_or_dir(subvol, &dirstream);
452 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
456 ret = btrfs_list_get_path_rootid(fd, &top_id);
458 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
463 btrfs_list_setup_filter(&filter_set,
464 BTRFS_LIST_FILTER_FULL_PATH,
466 else if (is_only_in_path)
467 btrfs_list_setup_filter(&filter_set,
468 BTRFS_LIST_FILTER_TOPID_EQUAL,
471 /* by default we shall print the following columns*/
472 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
473 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
474 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
475 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
478 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
479 BTRFS_LIST_LAYOUT_TABLE,
480 !is_list_all && !is_only_in_path, NULL);
482 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
483 BTRFS_LIST_LAYOUT_DEFAULT,
484 !is_list_all && !is_only_in_path, NULL);
487 close_file_or_dir(fd, dirstream);
489 btrfs_list_free_filter_set(filter_set);
491 btrfs_list_free_comparer_set(comparer_set);
493 usage(cmd_subvol_list_usage);
497 static const char * const cmd_snapshot_usage[] = {
498 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
499 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
500 "Create a snapshot of the subvolume",
501 "Create a writable/readonly snapshot of the subvolume <source> with",
502 "the name <name> in the <dest> directory. If only <dest> is given,",
503 "the subvolume will be named the basename of <source>.",
505 "-r create a readonly snapshot",
506 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
507 " option can be given multiple times.",
511 static int cmd_snapshot(int argc, char **argv)
515 int fd = -1, fddst = -1;
516 int len, readonly = 0;
517 char *dupname = NULL;
521 struct btrfs_ioctl_vol_args_v2 args;
522 struct btrfs_qgroup_inherit *inherit = NULL;
523 DIR *dirstream1 = NULL, *dirstream2 = NULL;
526 memset(&args, 0, sizeof(args));
528 int c = getopt(argc, argv, "c:i:r");
534 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
541 res = qgroup_inherit_add_group(&inherit, optarg);
551 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
558 usage(cmd_snapshot_usage);
562 if (check_argc_exact(argc - optind, 2))
563 usage(cmd_snapshot_usage);
565 subvol = argv[optind];
566 dst = argv[optind + 1];
568 retval = 1; /* failure */
569 res = test_issubvolume(subvol);
571 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
575 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
579 res = test_isdir(dst);
581 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
586 dupname = strdup(subvol);
587 newname = basename(dupname);
590 dupname = strdup(dst);
591 newname = basename(dupname);
592 dupdir = strdup(dst);
593 dstdir = dirname(dupdir);
596 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
597 strchr(newname, '/') ){
598 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
603 len = strlen(newname);
604 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
605 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
610 fddst = open_file_or_dir(dstdir, &dirstream1);
612 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
616 fd = open_file_or_dir(subvol, &dirstream2);
618 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
623 args.flags |= BTRFS_SUBVOL_RDONLY;
624 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
625 subvol, dstdir, newname);
627 printf("Create a snapshot of '%s' in '%s/%s'\n",
628 subvol, dstdir, newname);
633 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
634 args.size = qgroup_inherit_size(inherit);
635 args.qgroup_inherit = inherit;
637 strncpy_null(args.name, newname);
639 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
642 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
643 subvol, strerror(errno));
647 retval = 0; /* success */
650 close_file_or_dir(fddst, dirstream1);
651 close_file_or_dir(fd, dirstream2);
659 static const char * const cmd_subvol_get_default_usage[] = {
660 "btrfs subvolume get-default <path>",
661 "Get the default subvolume of a filesystem",
665 static int cmd_subvol_get_default(int argc, char **argv)
670 struct btrfs_list_filter_set *filter_set;
672 DIR *dirstream = NULL;
674 if (check_argc_exact(argc, 2))
675 usage(cmd_subvol_get_default_usage);
678 fd = open_file_or_dir(subvol, &dirstream);
680 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
684 ret = btrfs_list_get_default_subvolume(fd, &default_id);
686 fprintf(stderr, "ERROR: can't perform the search - %s\n",
692 if (default_id == 0) {
693 fprintf(stderr, "ERROR: 'default' dir item not found\n");
697 /* no need to resolve roots if FS_TREE is default */
698 if (default_id == BTRFS_FS_TREE_OBJECTID) {
699 printf("ID 5 (FS_TREE)\n");
704 filter_set = btrfs_list_alloc_filter_set();
705 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
708 /* by default we shall print the following columns*/
709 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
710 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
711 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
712 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
714 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
715 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
718 btrfs_list_free_filter_set(filter_set);
720 close_file_or_dir(fd, dirstream);
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)
736 DIR *dirstream = NULL;
738 if (check_argc_exact(argc, 3))
739 usage(cmd_subvol_set_default_usage);
744 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
745 if (errno == ERANGE) {
746 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
750 fd = open_file_or_dir(path, &dirstream);
752 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
756 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
758 close_file_or_dir(fd, dirstream);
760 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
767 static const char * const cmd_find_new_usage[] = {
768 "btrfs subvolume find-new <path> <lastgen>",
769 "List the recently modified files in a filesystem",
773 static int cmd_find_new(int argc, char **argv)
779 DIR *dirstream = NULL;
781 if (check_argc_exact(argc, 3))
782 usage(cmd_find_new_usage);
785 last_gen = atoll(argv[2]);
787 ret = test_issubvolume(subvol);
789 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
793 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
797 fd = open_file_or_dir(subvol, &dirstream);
799 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
803 ret = ioctl(fd, BTRFS_IOC_SYNC);
805 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
806 subvol, strerror(errno));
807 close_file_or_dir(fd, dirstream);
811 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
812 close_file_or_dir(fd, dirstream);
816 static const char * const cmd_subvol_show_usage[] = {
817 "btrfs subvolume show <subvol-path>",
818 "Show more information of the subvolume",
822 static int cmd_subvol_show(int argc, char **argv)
824 struct root_info get_ri;
825 struct btrfs_list_filter_set *filter_set;
828 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
829 char raw_prefix[] = "\t\t\t\t";
831 int fd = -1, mntfd = -1;
833 DIR *dirstream1 = NULL, *dirstream2 = NULL;
835 if (check_argc_exact(argc, 2))
836 usage(cmd_subvol_show_usage);
838 fullpath = realpath(argv[1], NULL);
840 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
841 argv[1], strerror(errno));
845 ret = test_issubvolume(fullpath);
847 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
851 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
855 ret = find_mount_root(fullpath, &mnt);
857 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
858 "%s\n", fullpath, strerror(-ret));
862 svpath = get_subvol_name(mnt, fullpath);
864 fd = open_file_or_dir(fullpath, &dirstream1);
866 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
870 ret = btrfs_list_get_path_rootid(fd, &sv_id);
872 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
877 mntfd = open_file_or_dir(mnt, &dirstream2);
879 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
883 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
885 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
889 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
890 printf("%s is btrfs root\n", fullpath);
894 memset(&get_ri, 0, sizeof(get_ri));
895 get_ri.root_id = sv_id;
897 if (btrfs_get_subvol(mntfd, &get_ri)) {
898 fprintf(stderr, "ERROR: can't find '%s'\n",
905 printf("%s\n", fullpath);
906 printf("\tName: \t\t\t%s\n", get_ri.name);
908 if (uuid_is_null(get_ri.uuid))
909 strcpy(uuidparse, "-");
911 uuid_unparse(get_ri.uuid, uuidparse);
912 printf("\tuuid: \t\t\t%s\n", uuidparse);
914 if (uuid_is_null(get_ri.puuid))
915 strcpy(uuidparse, "-");
917 uuid_unparse(get_ri.puuid, uuidparse);
918 printf("\tParent uuid: \t\t%s\n", uuidparse);
923 localtime_r(&get_ri.otime, &tm);
924 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
927 printf("\tCreation time: \t\t%s\n", tstr);
929 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
930 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
931 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
932 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
933 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
935 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
936 printf("\tFlags: \t\t\treadonly\n");
938 printf("\tFlags: \t\t\t-\n");
940 /* print the snapshots of the given subvol if any*/
941 printf("\tSnapshot(s):\n");
942 filter_set = btrfs_list_alloc_filter_set();
943 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
944 (u64)(unsigned long)get_ri.uuid);
945 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
946 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
954 if (get_ri.full_path)
955 free(get_ri.full_path);
957 btrfs_list_free_filter_set(filter_set);
960 close_file_or_dir(fd, dirstream1);
961 close_file_or_dir(mntfd, dirstream2);
969 const struct cmd_group subvolume_cmd_group = {
970 subvolume_cmd_group_usage, NULL, {
971 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
972 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
973 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
974 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
975 { "get-default", cmd_subvol_get_default,
976 cmd_subvol_get_default_usage, NULL, 0 },
977 { "set-default", cmd_subvol_set_default,
978 cmd_subvol_set_default_usage, NULL, 0 },
979 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
980 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
985 int cmd_subvolume(int argc, char **argv)
987 return handle_command_group(&subvolume_cmd_group, argc, argv);