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"
35 #include "btrfs-list.h"
38 static const char * const subvolume_cmd_group_usage[] = {
39 "btrfs subvolume <command> <args>",
44 * test if path is a directory
45 * this function return
46 * 0-> path exists but it is not a directory
47 * 1-> path exists and it is a directory
48 * -1 -> path is unaccessible
50 static int test_isdir(char *path)
55 res = stat(path, &st);
59 return S_ISDIR(st.st_mode);
62 static const char * const cmd_subvol_create_usage[] = {
63 "btrfs subvolume create [<dest>/]<name>",
65 "Create a subvolume <name> in <dest>. If <dest> is not given",
66 "subvolume <name> will be created in the current directory.",
70 static int cmd_subvol_create(int argc, char **argv)
72 int res, fddst, len, e;
76 struct btrfs_qgroup_inherit *inherit = NULL;
80 int c = getopt(argc, argv, "c:i:r");
86 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
91 res = qgroup_inherit_add_group(&inherit, optarg);
96 usage(cmd_subvol_create_usage);
100 if (check_argc_exact(argc - optind, 1))
101 usage(cmd_subvol_create_usage);
105 res = test_isdir(dst);
107 fprintf(stderr, "ERROR: '%s' exists\n", dst);
111 newname = strdup(dst);
112 newname = basename(newname);
113 dstdir = strdup(dst);
114 dstdir = dirname(dstdir);
116 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
117 strchr(newname, '/') ){
118 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
123 len = strlen(newname);
124 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
125 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
130 fddst = open_file_or_dir(dstdir);
132 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
136 printf("Create subvolume '%s/%s'\n", dstdir, newname);
138 struct btrfs_ioctl_vol_args_v2 args;
140 memset(&args, 0, sizeof(args));
141 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
142 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
143 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
144 args.size = qgroup_inherit_size(inherit);
145 args.qgroup_inherit = inherit;
147 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
149 struct btrfs_ioctl_vol_args args;
151 memset(&args, 0, sizeof(args));
152 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
153 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
155 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
163 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
173 * test if path is a subvolume:
174 * this function return
175 * 0-> path exists but it is not a subvolume
176 * 1-> path exists and it is a subvolume
177 * -1 -> path is unaccessible
179 int test_issubvolume(char *path)
184 res = stat(path, &st);
188 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
191 static const char * const cmd_subvol_delete_usage[] = {
192 "btrfs subvolume delete <subvolume> [<subvolume>...]",
193 "Delete subvolume(s)",
197 static int cmd_subvol_delete(int argc, char **argv)
199 int res, fd, len, e, cnt = 1, ret = 0;
200 struct btrfs_ioctl_vol_args args;
201 char *dname, *vname, *cpath;
205 usage(cmd_subvol_delete_usage);
210 res = test_issubvolume(path);
212 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
217 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
222 cpath = realpath(path, 0);
223 dname = strdup(cpath);
224 dname = dirname(dname);
225 vname = strdup(cpath);
226 vname = basename(vname);
229 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
230 strchr(vname, '/') ){
231 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
238 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
239 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
245 fd = open_file_or_dir(dname);
248 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
253 printf("Delete subvolume '%s/%s'\n", dname, vname);
254 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
255 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
256 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
262 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
263 dname, vname, strerror(e));
278 * - uppercase for filters and sort options
279 * - lowercase for enabling specific items in the output
281 static const char * const cmd_subvol_list_usage[] = {
282 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
283 "[--sort=gen,ogen,rootid,path] <path>",
284 "List subvolumes (and snapshots)",
286 "-p print parent ID",
287 "-a print all the subvolumes in the filesystem and",
288 " distinguish absolute and relative path with respect",
289 " to the given <path>",
290 "-g print the generation of the subvolume",
291 "-o print only subvolumes bellow specified path",
292 "-u print the uuid of subvolumes (and snapshots)",
293 "-q print the parent uuid of the snapshots",
294 "-t print the result as a table",
295 "-s list snapshots only in the filesystem",
296 "-r list readonly subvolumes (including snapshots)",
298 " filter the subvolumes by generation",
299 " (+value: >= value; -value: <= value; value: = value)",
301 " filter the subvolumes by ogeneration",
302 " (+value: >= value; -value: <= value; value: = value)",
303 "--sort=gen,ogen,rootid,path",
304 " list the subvolume in order of gen, ogen, rootid or path",
305 " you also can add '+' or '-' in front of each items.",
306 " (+:ascending, -:descending, ascending default)",
310 static int cmd_subvol_list(int argc, char **argv)
312 struct btrfs_list_filter_set *filter_set;
313 struct btrfs_list_comparer_set *comparer_set;
317 int ret = -1, uerr = 0;
320 int is_tab_result = 0;
322 int is_only_in_path = 0;
323 struct option long_options[] = {
324 {"sort", 1, NULL, 'S'},
328 filter_set = btrfs_list_alloc_filter_set();
329 comparer_set = btrfs_list_alloc_comparer_set();
333 c = getopt_long(argc, argv,
334 "agopqsurG:C:t", long_options, NULL);
340 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
346 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
355 btrfs_list_setup_filter(&filter_set,
356 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
358 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
359 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
362 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
365 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
368 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
371 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
372 ret = btrfs_list_parse_filter_string(optarg,
374 BTRFS_LIST_FILTER_GEN);
382 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
383 ret = btrfs_list_parse_filter_string(optarg,
385 BTRFS_LIST_FILTER_CGEN);
392 ret = btrfs_list_parse_sort_string(optarg,
407 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
410 if (check_argc_exact(argc - optind, 1)) {
415 subvol = argv[optind];
417 ret = test_issubvolume(subvol);
419 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
423 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
428 fd = open_file_or_dir(subvol);
431 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
435 top_id = btrfs_list_get_path_rootid(fd);
438 btrfs_list_setup_filter(&filter_set,
439 BTRFS_LIST_FILTER_FULL_PATH,
441 else if (is_only_in_path)
442 btrfs_list_setup_filter(&filter_set,
443 BTRFS_LIST_FILTER_TOPID_EQUAL,
446 /* by default we shall print the following columns*/
447 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
448 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
449 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
450 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
453 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
454 BTRFS_LIST_LAYOUT_TABLE,
455 !is_list_all && !is_only_in_path, NULL);
457 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
458 BTRFS_LIST_LAYOUT_DEFAULT,
459 !is_list_all && !is_only_in_path, NULL);
463 btrfs_list_free_filter_set(filter_set);
465 btrfs_list_free_comparer_set(comparer_set);
467 usage(cmd_subvol_list_usage);
472 static const char * const cmd_snapshot_usage[] = {
473 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
474 "Create a snapshot of the subvolume",
475 "Create a writable/readonly snapshot of the subvolume <source> with",
476 "the name <name> in the <dest> directory",
478 "-r create a readonly snapshot",
482 static int cmd_snapshot(int argc, char **argv)
485 int res, fd, fddst, len, e, readonly = 0;
488 struct btrfs_ioctl_vol_args_v2 args;
489 struct btrfs_qgroup_inherit *inherit = NULL;
492 memset(&args, 0, sizeof(args));
494 int c = getopt(argc, argv, "c:i:r");
500 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
505 res = qgroup_inherit_add_group(&inherit, optarg);
513 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
518 usage(cmd_snapshot_usage);
522 if (check_argc_exact(argc - optind, 2))
523 usage(cmd_snapshot_usage);
525 subvol = argv[optind];
526 dst = argv[optind + 1];
528 res = test_issubvolume(subvol);
530 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
534 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
538 res = test_isdir(dst);
540 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
545 newname = strdup(subvol);
546 newname = basename(newname);
549 newname = strdup(dst);
550 newname = basename(newname);
551 dstdir = strdup(dst);
552 dstdir = dirname(dstdir);
555 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
556 strchr(newname, '/') ){
557 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
562 len = strlen(newname);
563 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
564 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
569 fddst = open_file_or_dir(dstdir);
571 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
575 fd = open_file_or_dir(subvol);
578 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
583 args.flags |= BTRFS_SUBVOL_RDONLY;
584 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
585 subvol, dstdir, newname);
587 printf("Create a snapshot of '%s' in '%s/%s'\n",
588 subvol, dstdir, newname);
593 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
594 args.size = qgroup_inherit_size(inherit);
595 args.qgroup_inherit = inherit;
597 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
598 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
599 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
606 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
607 subvol, strerror(e));
615 static const char * const cmd_subvol_get_default_usage[] = {
616 "btrfs subvolume get-default <path>",
617 "Get the default subvolume of a filesystem",
621 static int cmd_subvol_get_default(int argc, char **argv)
626 struct btrfs_list_filter_set *filter_set;
629 if (check_argc_exact(argc, 2))
630 usage(cmd_subvol_get_default_usage);
634 ret = test_issubvolume(subvol);
636 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
640 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
644 fd = open_file_or_dir(subvol);
646 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
650 ret = btrfs_list_get_default_subvolume(fd, &default_id);
652 fprintf(stderr, "ERROR: can't perform the search - %s\n",
657 if (default_id == 0) {
658 fprintf(stderr, "ERROR: 'default' dir item not found\n");
662 /* no need to resolve roots if FS_TREE is default */
663 if (default_id == BTRFS_FS_TREE_OBJECTID) {
664 printf("ID 5 (FS_TREE)\n");
668 filter_set = btrfs_list_alloc_filter_set();
669 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
672 /* by default we shall print the following columns*/
673 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
674 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
675 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
676 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
678 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
679 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
682 btrfs_list_free_filter_set(filter_set);
688 static const char * const cmd_subvol_set_default_usage[] = {
689 "btrfs subvolume set-default <subvolid> <path>",
690 "Set the default subvolume of a filesystem",
694 static int cmd_subvol_set_default(int argc, char **argv)
701 if (check_argc_exact(argc, 3))
702 usage(cmd_subvol_set_default_usage);
707 fd = open_file_or_dir(path);
709 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
713 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
714 if (errno == ERANGE) {
715 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
718 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
722 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
729 static const char * const cmd_find_new_usage[] = {
730 "btrfs subvolume find-new <path> <lastgen>",
731 "List the recently modified files in a filesystem",
735 static int cmd_find_new(int argc, char **argv)
742 if (check_argc_exact(argc, 3))
743 usage(cmd_find_new_usage);
746 last_gen = atoll(argv[2]);
748 ret = test_issubvolume(subvol);
750 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
754 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
758 fd = open_file_or_dir(subvol);
760 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
763 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
769 static const char * const cmd_subvol_show_usage[] = {
770 "btrfs subvolume show <subvol-path>",
771 "Show more information of the subvolume",
775 static int cmd_subvol_show(int argc, char **argv)
777 struct root_info get_ri;
778 struct btrfs_list_filter_set *filter_set;
781 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
782 char raw_prefix[] = "\t\t\t\t";
784 int fd = -1, mntfd = -1;
787 if (check_argc_exact(argc, 2))
788 usage(cmd_subvol_show_usage);
790 fullpath = realpath(argv[1], 0);
792 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
793 argv[1], strerror(errno));
797 ret = test_issubvolume(fullpath);
799 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
803 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
808 ret = find_mount_root(fullpath, &mnt);
810 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
811 "%s\n", fullpath, strerror(-ret));
815 svpath = get_subvol_name(mnt, fullpath);
817 fd = open_file_or_dir(fullpath);
819 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
823 sv_id = btrfs_list_get_path_rootid(fd);
825 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
830 mntfd = open_file_or_dir(mnt);
832 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
836 mntid = btrfs_list_get_path_rootid(mntfd);
838 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
842 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
843 printf("%s is btrfs root\n", fullpath);
847 memset(&get_ri, 0, sizeof(get_ri));
848 get_ri.root_id = sv_id;
850 if (btrfs_get_subvol(mntfd, &get_ri)) {
851 fprintf(stderr, "ERROR: can't find '%s'\n",
858 printf("%s\n", fullpath);
859 printf("\tName: \t\t\t%s\n", get_ri.name);
861 if (uuid_is_null(get_ri.uuid))
862 strcpy(uuidparse, "-");
864 uuid_unparse(get_ri.uuid, uuidparse);
865 printf("\tuuid: \t\t\t%s\n", uuidparse);
867 if (uuid_is_null(get_ri.puuid))
868 strcpy(uuidparse, "-");
870 uuid_unparse(get_ri.puuid, uuidparse);
871 printf("\tParent uuid: \t\t%s\n", uuidparse);
874 strftime(tstr, 256, "%Y-%m-%d %X",
875 localtime(&get_ri.otime));
878 printf("\tCreation time: \t\t%s\n", tstr);
880 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
881 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
882 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
883 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
884 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
886 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
887 printf("\tFlags: \t\t\treadonly\n");
889 printf("\tFlags: \t\t\t-\n");
891 /* print the snapshots of the given subvol if any*/
892 printf("\tSnapshot(s):\n");
893 filter_set = btrfs_list_alloc_filter_set();
894 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
896 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
897 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
905 if (get_ri.full_path)
906 free(get_ri.full_path);
908 btrfs_list_free_filter_set(filter_set);
923 const struct cmd_group subvolume_cmd_group = {
924 subvolume_cmd_group_usage, NULL, {
925 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
926 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
927 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
928 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
929 { "get-default", cmd_subvol_get_default,
930 cmd_subvol_get_default_usage, NULL, 0 },
931 { "set-default", cmd_subvol_set_default,
932 cmd_subvol_set_default_usage, NULL, 0 },
933 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
934 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
939 int cmd_subvolume(int argc, char **argv)
941 return handle_command_group(&subvolume_cmd_group, argc, argv);