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];
427 fd = open_file_or_dir(subvol);
430 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
434 ret = btrfs_list_get_path_rootid(fd, &top_id);
436 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
441 btrfs_list_setup_filter(&filter_set,
442 BTRFS_LIST_FILTER_FULL_PATH,
444 else if (is_only_in_path)
445 btrfs_list_setup_filter(&filter_set,
446 BTRFS_LIST_FILTER_TOPID_EQUAL,
449 /* by default we shall print the following columns*/
450 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
451 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
452 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
453 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
456 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
457 BTRFS_LIST_LAYOUT_TABLE,
458 !is_list_all && !is_only_in_path, NULL);
460 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
461 BTRFS_LIST_LAYOUT_DEFAULT,
462 !is_list_all && !is_only_in_path, NULL);
468 btrfs_list_free_filter_set(filter_set);
470 btrfs_list_free_comparer_set(comparer_set);
472 usage(cmd_subvol_list_usage);
477 static const char * const cmd_snapshot_usage[] = {
478 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
479 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> [<dest>/]<name>",
480 "Create a snapshot of the subvolume",
481 "Create a writable/readonly snapshot of the subvolume <source> with",
482 "the name <name> in the <dest> directory",
484 "-r create a readonly snapshot",
485 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
486 " option can be given multiple times.",
490 static int cmd_snapshot(int argc, char **argv)
494 int fd = -1, fddst = -1;
495 int len, readonly = 0;
498 struct btrfs_ioctl_vol_args_v2 args;
499 struct btrfs_qgroup_inherit *inherit = NULL;
502 memset(&args, 0, sizeof(args));
504 int c = getopt(argc, argv, "c:i:r");
510 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
517 res = qgroup_inherit_add_group(&inherit, optarg);
527 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
534 usage(cmd_snapshot_usage);
538 if (check_argc_exact(argc - optind, 2))
539 usage(cmd_snapshot_usage);
541 subvol = argv[optind];
542 dst = argv[optind + 1];
544 retval = 1; /* failure */
545 res = test_issubvolume(subvol);
547 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
551 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
555 res = test_isdir(dst);
557 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
562 newname = strdup(subvol);
563 newname = basename(newname);
566 newname = strdup(dst);
567 newname = basename(newname);
568 dstdir = strdup(dst);
569 dstdir = dirname(dstdir);
572 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
573 strchr(newname, '/') ){
574 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
579 len = strlen(newname);
580 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
581 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
586 fddst = open_file_or_dir(dstdir);
588 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
592 fd = open_file_or_dir(subvol);
594 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
599 args.flags |= BTRFS_SUBVOL_RDONLY;
600 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
601 subvol, dstdir, newname);
603 printf("Create a snapshot of '%s' in '%s/%s'\n",
604 subvol, dstdir, newname);
609 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
610 args.size = qgroup_inherit_size(inherit);
611 args.qgroup_inherit = inherit;
613 strncpy_null(args.name, newname);
615 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
618 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
619 subvol, strerror(errno));
623 retval = 0; /* success */
635 static const char * const cmd_subvol_get_default_usage[] = {
636 "btrfs subvolume get-default <path>",
637 "Get the default subvolume of a filesystem",
641 static int cmd_subvol_get_default(int argc, char **argv)
646 struct btrfs_list_filter_set *filter_set;
649 if (check_argc_exact(argc, 2))
650 usage(cmd_subvol_get_default_usage);
653 fd = open_file_or_dir(subvol);
655 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
659 ret = btrfs_list_get_default_subvolume(fd, &default_id);
661 fprintf(stderr, "ERROR: can't perform the search - %s\n",
667 if (default_id == 0) {
668 fprintf(stderr, "ERROR: 'default' dir item not found\n");
672 /* no need to resolve roots if FS_TREE is default */
673 if (default_id == BTRFS_FS_TREE_OBJECTID) {
674 printf("ID 5 (FS_TREE)\n");
678 filter_set = btrfs_list_alloc_filter_set();
679 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
682 /* by default we shall print the following columns*/
683 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
684 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
685 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
686 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
688 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
689 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
692 btrfs_list_free_filter_set(filter_set);
701 static const char * const cmd_subvol_set_default_usage[] = {
702 "btrfs subvolume set-default <subvolid> <path>",
703 "Set the default subvolume of a filesystem",
707 static int cmd_subvol_set_default(int argc, char **argv)
714 if (check_argc_exact(argc, 3))
715 usage(cmd_subvol_set_default_usage);
720 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
721 if (errno == ERANGE) {
722 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
726 fd = open_file_or_dir(path);
728 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
732 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
736 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
743 static const char * const cmd_find_new_usage[] = {
744 "btrfs subvolume find-new <path> <lastgen>",
745 "List the recently modified files in a filesystem",
749 static int cmd_find_new(int argc, char **argv)
756 if (check_argc_exact(argc, 3))
757 usage(cmd_find_new_usage);
760 last_gen = atoll(argv[2]);
762 ret = test_issubvolume(subvol);
764 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
768 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
772 fd = open_file_or_dir(subvol);
774 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
777 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
784 static const char * const cmd_subvol_show_usage[] = {
785 "btrfs subvolume show <subvol-path>",
786 "Show more information of the subvolume",
790 static int cmd_subvol_show(int argc, char **argv)
792 struct root_info get_ri;
793 struct btrfs_list_filter_set *filter_set;
796 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
797 char raw_prefix[] = "\t\t\t\t";
799 int fd = -1, mntfd = -1;
802 if (check_argc_exact(argc, 2))
803 usage(cmd_subvol_show_usage);
805 fullpath = realpath(argv[1], 0);
807 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
808 argv[1], strerror(errno));
812 ret = test_issubvolume(fullpath);
814 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
818 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
823 ret = find_mount_root(fullpath, &mnt);
825 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
826 "%s\n", fullpath, strerror(-ret));
830 svpath = get_subvol_name(mnt, fullpath);
832 fd = open_file_or_dir(fullpath);
834 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
838 ret = btrfs_list_get_path_rootid(fd, &sv_id);
840 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
845 mntfd = open_file_or_dir(mnt);
847 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
851 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
853 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
857 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
858 printf("%s is btrfs root\n", fullpath);
862 memset(&get_ri, 0, sizeof(get_ri));
863 get_ri.root_id = sv_id;
865 if (btrfs_get_subvol(mntfd, &get_ri)) {
866 fprintf(stderr, "ERROR: can't find '%s'\n",
873 printf("%s\n", fullpath);
874 printf("\tName: \t\t\t%s\n", get_ri.name);
876 if (uuid_is_null(get_ri.uuid))
877 strcpy(uuidparse, "-");
879 uuid_unparse(get_ri.uuid, uuidparse);
880 printf("\tuuid: \t\t\t%s\n", uuidparse);
882 if (uuid_is_null(get_ri.puuid))
883 strcpy(uuidparse, "-");
885 uuid_unparse(get_ri.puuid, uuidparse);
886 printf("\tParent uuid: \t\t%s\n", uuidparse);
891 localtime_r(&get_ri.otime, &tm);
892 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
895 printf("\tCreation time: \t\t%s\n", tstr);
897 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
898 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
899 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
900 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
901 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
903 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
904 printf("\tFlags: \t\t\treadonly\n");
906 printf("\tFlags: \t\t\t-\n");
908 /* print the snapshots of the given subvol if any*/
909 printf("\tSnapshot(s):\n");
910 filter_set = btrfs_list_alloc_filter_set();
911 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
912 (u64)(unsigned long)get_ri.uuid);
913 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
914 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
922 if (get_ri.full_path)
923 free(get_ri.full_path);
925 btrfs_list_free_filter_set(filter_set);
940 const struct cmd_group subvolume_cmd_group = {
941 subvolume_cmd_group_usage, NULL, {
942 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
943 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
944 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
945 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
946 { "get-default", cmd_subvol_get_default,
947 cmd_subvol_get_default_usage, NULL, 0 },
948 { "set-default", cmd_subvol_set_default,
949 cmd_subvol_set_default_usage, NULL, 0 },
950 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
951 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
956 int cmd_subvolume(int argc, char **argv)
958 return handle_command_group(&subvolume_cmd_group, argc, argv);