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);
239 dupdname = strdup(cpath);
240 dname = dirname(dupdname);
241 dupvname = strdup(cpath);
242 vname = basename(dupvname);
245 if (!strcmp(vname, ".") || !strcmp(vname, "..") ||
246 strchr(vname, '/')) {
247 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
254 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
255 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
261 fd = open_file_or_dir(dname, &dirstream);
263 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
268 printf("Delete subvolume '%s/%s'\n", dname, vname);
269 strncpy_null(args.name, vname);
270 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
273 close_file_or_dir(fd, dirstream);
276 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
277 dname, vname, strerror(e));
294 * - uppercase for filters and sort options
295 * - lowercase for enabling specific items in the output
297 static const char * const cmd_subvol_list_usage[] = {
298 "btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] "
299 "[--sort=gen,ogen,rootid,path] <path>",
300 "List subvolumes (and snapshots)",
302 "-p print parent ID",
303 "-a print all the subvolumes in the filesystem and",
304 " distinguish absolute and relative path with respect",
305 " to the given <path>",
306 "-c print the ogeneration of the subvolume",
307 "-g print the generation of the subvolume",
308 "-o print only subvolumes bellow specified path",
309 "-u print the uuid of subvolumes (and snapshots)",
310 "-q print the parent uuid of the snapshots",
311 "-t print the result as a table",
312 "-s list snapshots only in the filesystem",
313 "-r list readonly subvolumes (including snapshots)",
315 " filter the subvolumes by generation",
316 " (+value: >= value; -value: <= value; value: = value)",
318 " filter the subvolumes by ogeneration",
319 " (+value: >= value; -value: <= value; value: = value)",
320 "--sort=gen,ogen,rootid,path",
321 " list the subvolume in order of gen, ogen, rootid or path",
322 " you also can add '+' or '-' in front of each items.",
323 " (+:ascending, -:descending, ascending default)",
327 static int cmd_subvol_list(int argc, char **argv)
329 struct btrfs_list_filter_set *filter_set;
330 struct btrfs_list_comparer_set *comparer_set;
334 int ret = -1, uerr = 0;
337 int is_tab_result = 0;
339 int is_only_in_path = 0;
340 struct option long_options[] = {
341 {"sort", 1, NULL, 'S'},
344 DIR *dirstream = NULL;
346 filter_set = btrfs_list_alloc_filter_set();
347 comparer_set = btrfs_list_alloc_comparer_set();
351 c = getopt_long(argc, argv,
352 "acgopqsurG:C:t", long_options, NULL);
358 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
364 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
367 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
376 btrfs_list_setup_filter(&filter_set,
377 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
379 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
380 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
383 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
386 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
389 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
392 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
393 ret = btrfs_list_parse_filter_string(optarg,
395 BTRFS_LIST_FILTER_GEN);
403 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
404 ret = btrfs_list_parse_filter_string(optarg,
406 BTRFS_LIST_FILTER_CGEN);
413 ret = btrfs_list_parse_sort_string(optarg,
428 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
431 if (check_argc_exact(argc - optind, 1)) {
436 subvol = argv[optind];
437 fd = open_file_or_dir(subvol, &dirstream);
440 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
444 ret = btrfs_list_get_path_rootid(fd, &top_id);
446 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
451 btrfs_list_setup_filter(&filter_set,
452 BTRFS_LIST_FILTER_FULL_PATH,
454 else if (is_only_in_path)
455 btrfs_list_setup_filter(&filter_set,
456 BTRFS_LIST_FILTER_TOPID_EQUAL,
459 /* by default we shall print the following columns*/
460 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
461 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
462 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
463 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
466 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
467 BTRFS_LIST_LAYOUT_TABLE,
468 !is_list_all && !is_only_in_path, NULL);
470 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
471 BTRFS_LIST_LAYOUT_DEFAULT,
472 !is_list_all && !is_only_in_path, NULL);
475 close_file_or_dir(fd, dirstream);
477 btrfs_list_free_filter_set(filter_set);
479 btrfs_list_free_comparer_set(comparer_set);
481 usage(cmd_subvol_list_usage);
485 static const char * const cmd_snapshot_usage[] = {
486 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
487 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
488 "Create a snapshot of the subvolume",
489 "Create a writable/readonly snapshot of the subvolume <source> with",
490 "the name <name> in the <dest> directory. If only <dest> is given,",
491 "the subvolume will be named the basename of <source>.",
493 "-r create a readonly snapshot",
494 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
495 " option can be given multiple times.",
499 static int cmd_snapshot(int argc, char **argv)
503 int fd = -1, fddst = -1;
504 int len, readonly = 0;
505 char *dupname = NULL;
509 struct btrfs_ioctl_vol_args_v2 args;
510 struct btrfs_qgroup_inherit *inherit = NULL;
511 DIR *dirstream1 = NULL, *dirstream2 = 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 dupname = strdup(subvol);
575 newname = basename(dupname);
578 dupname = strdup(dst);
579 newname = basename(dupname);
580 dupdir = strdup(dst);
581 dstdir = dirname(dupdir);
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, &dirstream1);
600 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
604 fd = open_file_or_dir(subvol, &dirstream2);
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 */
638 close_file_or_dir(fddst, dirstream1);
639 close_file_or_dir(fd, dirstream2);
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;
660 DIR *dirstream = NULL;
662 if (check_argc_exact(argc, 2))
663 usage(cmd_subvol_get_default_usage);
666 fd = open_file_or_dir(subvol, &dirstream);
668 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
672 ret = btrfs_list_get_default_subvolume(fd, &default_id);
674 fprintf(stderr, "ERROR: can't perform the search - %s\n",
680 if (default_id == 0) {
681 fprintf(stderr, "ERROR: 'default' dir item not found\n");
685 /* no need to resolve roots if FS_TREE is default */
686 if (default_id == BTRFS_FS_TREE_OBJECTID) {
687 printf("ID 5 (FS_TREE)\n");
691 filter_set = btrfs_list_alloc_filter_set();
692 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
695 /* by default we shall print the following columns*/
696 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
697 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
698 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
699 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
701 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
702 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
705 btrfs_list_free_filter_set(filter_set);
707 close_file_or_dir(fd, dirstream);
711 static const char * const cmd_subvol_set_default_usage[] = {
712 "btrfs subvolume set-default <subvolid> <path>",
713 "Set the default subvolume of a filesystem",
717 static int cmd_subvol_set_default(int argc, char **argv)
723 DIR *dirstream = NULL;
725 if (check_argc_exact(argc, 3))
726 usage(cmd_subvol_set_default_usage);
731 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
732 if (errno == ERANGE) {
733 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
737 fd = open_file_or_dir(path, &dirstream);
739 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
743 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
745 close_file_or_dir(fd, dirstream);
747 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
754 static const char * const cmd_find_new_usage[] = {
755 "btrfs subvolume find-new <path> <lastgen>",
756 "List the recently modified files in a filesystem",
760 static int cmd_find_new(int argc, char **argv)
766 DIR *dirstream = NULL;
768 if (check_argc_exact(argc, 3))
769 usage(cmd_find_new_usage);
772 last_gen = atoll(argv[2]);
774 ret = test_issubvolume(subvol);
776 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
780 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
784 fd = open_file_or_dir(subvol, &dirstream);
786 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
789 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
790 close_file_or_dir(fd, dirstream);
794 static const char * const cmd_subvol_show_usage[] = {
795 "btrfs subvolume show <subvol-path>",
796 "Show more information of the subvolume",
800 static int cmd_subvol_show(int argc, char **argv)
802 struct root_info get_ri;
803 struct btrfs_list_filter_set *filter_set;
806 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
807 char raw_prefix[] = "\t\t\t\t";
809 int fd = -1, mntfd = -1;
811 DIR *dirstream1 = NULL, *dirstream2 = NULL;
813 if (check_argc_exact(argc, 2))
814 usage(cmd_subvol_show_usage);
816 fullpath = realpath(argv[1], NULL);
818 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
819 argv[1], strerror(errno));
823 ret = test_issubvolume(fullpath);
825 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
829 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
833 ret = find_mount_root(fullpath, &mnt);
835 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
836 "%s\n", fullpath, strerror(-ret));
840 svpath = get_subvol_name(mnt, fullpath);
842 fd = open_file_or_dir(fullpath, &dirstream1);
844 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
848 ret = btrfs_list_get_path_rootid(fd, &sv_id);
850 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
855 mntfd = open_file_or_dir(mnt, &dirstream2);
857 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
861 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
863 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
867 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
868 printf("%s is btrfs root\n", fullpath);
872 memset(&get_ri, 0, sizeof(get_ri));
873 get_ri.root_id = sv_id;
875 if (btrfs_get_subvol(mntfd, &get_ri)) {
876 fprintf(stderr, "ERROR: can't find '%s'\n",
883 printf("%s\n", fullpath);
884 printf("\tName: \t\t\t%s\n", get_ri.name);
886 if (uuid_is_null(get_ri.uuid))
887 strcpy(uuidparse, "-");
889 uuid_unparse(get_ri.uuid, uuidparse);
890 printf("\tuuid: \t\t\t%s\n", uuidparse);
892 if (uuid_is_null(get_ri.puuid))
893 strcpy(uuidparse, "-");
895 uuid_unparse(get_ri.puuid, uuidparse);
896 printf("\tParent uuid: \t\t%s\n", uuidparse);
901 localtime_r(&get_ri.otime, &tm);
902 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
905 printf("\tCreation time: \t\t%s\n", tstr);
907 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
908 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
909 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
910 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
911 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
913 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
914 printf("\tFlags: \t\t\treadonly\n");
916 printf("\tFlags: \t\t\t-\n");
918 /* print the snapshots of the given subvol if any*/
919 printf("\tSnapshot(s):\n");
920 filter_set = btrfs_list_alloc_filter_set();
921 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
922 (u64)(unsigned long)get_ri.uuid);
923 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
924 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
932 if (get_ri.full_path)
933 free(get_ri.full_path);
935 btrfs_list_free_filter_set(filter_set);
938 close_file_or_dir(fd, dirstream1);
939 close_file_or_dir(mntfd, dirstream2);
947 const struct cmd_group subvolume_cmd_group = {
948 subvolume_cmd_group_usage, NULL, {
949 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
950 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
951 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
952 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
953 { "get-default", cmd_subvol_get_default,
954 cmd_subvol_get_default_usage, NULL, 0 },
955 { "set-default", cmd_subvol_set_default,
956 cmd_subvol_set_default_usage, NULL, 0 },
957 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
958 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
963 int cmd_subvolume(int argc, char **argv)
965 return handle_command_group(&subvolume_cmd_group, argc, argv);