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);
96 res = qgroup_inherit_add_group(&inherit, optarg);
101 usage(cmd_subvol_create_usage);
105 if (check_argc_exact(argc - optind, 1))
106 usage(cmd_subvol_create_usage);
110 retval = 1; /* failure */
111 res = test_isdir(dst);
113 fprintf(stderr, "ERROR: '%s' exists\n", dst);
117 newname = strdup(dst);
118 newname = basename(newname);
119 dstdir = strdup(dst);
120 dstdir = dirname(dstdir);
122 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
123 strchr(newname, '/') ){
124 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
129 len = strlen(newname);
130 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
131 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
136 fddst = open_file_or_dir(dstdir);
138 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
142 printf("Create subvolume '%s/%s'\n", dstdir, newname);
144 struct btrfs_ioctl_vol_args_v2 args;
146 memset(&args, 0, sizeof(args));
147 strncpy_null(args.name, newname);
148 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
149 args.size = qgroup_inherit_size(inherit);
150 args.qgroup_inherit = inherit;
152 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
154 struct btrfs_ioctl_vol_args args;
156 memset(&args, 0, sizeof(args));
157 strncpy_null(args.name, newname);
159 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
163 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
168 retval = 0; /* success */
178 * test if path is a subvolume:
179 * this function return
180 * 0-> path exists but it is not a subvolume
181 * 1-> path exists and it is a subvolume
182 * -1 -> path is unaccessible
184 int test_issubvolume(char *path)
189 res = stat(path, &st);
193 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
196 static const char * const cmd_subvol_delete_usage[] = {
197 "btrfs subvolume delete <subvolume> [<subvolume>...]",
198 "Delete subvolume(s)",
202 static int cmd_subvol_delete(int argc, char **argv)
204 int res, fd, len, e, cnt = 1, ret = 0;
205 struct btrfs_ioctl_vol_args args;
206 char *dname, *vname, *cpath;
210 usage(cmd_subvol_delete_usage);
215 res = test_issubvolume(path);
217 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
222 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
227 cpath = realpath(path, 0);
228 dname = strdup(cpath);
229 dname = dirname(dname);
230 vname = strdup(cpath);
231 vname = basename(vname);
234 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
235 strchr(vname, '/') ){
236 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
243 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
244 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
250 fd = open_file_or_dir(dname);
252 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
257 printf("Delete subvolume '%s/%s'\n", dname, vname);
258 strncpy_null(args.name, vname);
259 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
265 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
266 dname, vname, strerror(e));
281 * - uppercase for filters and sort options
282 * - lowercase for enabling specific items in the output
284 static const char * const cmd_subvol_list_usage[] = {
285 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
286 "[--sort=gen,ogen,rootid,path] <path>",
287 "List subvolumes (and snapshots)",
289 "-p print parent ID",
290 "-a print all the subvolumes in the filesystem and",
291 " distinguish absolute and relative path with respect",
292 " to the given <path>",
293 "-c print the ogeneration of the subvolume",
294 "-g print the generation of the subvolume",
295 "-o print only subvolumes bellow specified path",
296 "-u print the uuid of subvolumes (and snapshots)",
297 "-q print the parent uuid of the snapshots",
298 "-t print the result as a table",
299 "-s list snapshots only in the filesystem",
300 "-r list readonly subvolumes (including snapshots)",
302 " filter the subvolumes by generation",
303 " (+value: >= value; -value: <= value; value: = value)",
305 " filter the subvolumes by ogeneration",
306 " (+value: >= value; -value: <= value; value: = value)",
307 "--sort=gen,ogen,rootid,path",
308 " list the subvolume in order of gen, ogen, rootid or path",
309 " you also can add '+' or '-' in front of each items.",
310 " (+:ascending, -:descending, ascending default)",
314 static int cmd_subvol_list(int argc, char **argv)
316 struct btrfs_list_filter_set *filter_set;
317 struct btrfs_list_comparer_set *comparer_set;
321 int ret = -1, uerr = 0;
324 int is_tab_result = 0;
326 int is_only_in_path = 0;
327 struct option long_options[] = {
328 {"sort", 1, NULL, 'S'},
332 filter_set = btrfs_list_alloc_filter_set();
333 comparer_set = btrfs_list_alloc_comparer_set();
337 c = getopt_long(argc, argv,
338 "acgopqsurG:C:t", long_options, NULL);
344 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
350 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
353 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
362 btrfs_list_setup_filter(&filter_set,
363 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
365 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
366 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
369 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
372 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
375 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
378 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
379 ret = btrfs_list_parse_filter_string(optarg,
381 BTRFS_LIST_FILTER_GEN);
389 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
390 ret = btrfs_list_parse_filter_string(optarg,
392 BTRFS_LIST_FILTER_CGEN);
399 ret = btrfs_list_parse_sort_string(optarg,
414 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
417 if (check_argc_exact(argc - optind, 1)) {
422 subvol = argv[optind];
424 ret = test_issubvolume(subvol);
426 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
430 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
435 fd = open_file_or_dir(subvol);
438 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
442 ret = btrfs_list_get_path_rootid(fd, &top_id);
444 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
449 btrfs_list_setup_filter(&filter_set,
450 BTRFS_LIST_FILTER_FULL_PATH,
452 else if (is_only_in_path)
453 btrfs_list_setup_filter(&filter_set,
454 BTRFS_LIST_FILTER_TOPID_EQUAL,
457 /* by default we shall print the following columns*/
458 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
459 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
460 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
461 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
464 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
465 BTRFS_LIST_LAYOUT_TABLE,
466 !is_list_all && !is_only_in_path, NULL);
468 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
469 BTRFS_LIST_LAYOUT_DEFAULT,
470 !is_list_all && !is_only_in_path, NULL);
476 btrfs_list_free_filter_set(filter_set);
478 btrfs_list_free_comparer_set(comparer_set);
480 usage(cmd_subvol_list_usage);
485 static const char * const cmd_snapshot_usage[] = {
486 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
487 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> [<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",
492 "-r create a readonly snapshot",
493 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
494 " option can be given multiple times.",
498 static int cmd_snapshot(int argc, char **argv)
502 int fd = -1, fddst = -1;
503 int len, readonly = 0;
506 struct btrfs_ioctl_vol_args_v2 args;
507 struct btrfs_qgroup_inherit *inherit = NULL;
510 memset(&args, 0, sizeof(args));
512 int c = getopt(argc, argv, "c:i:r");
518 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
523 res = qgroup_inherit_add_group(&inherit, optarg);
531 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
536 usage(cmd_snapshot_usage);
540 if (check_argc_exact(argc - optind, 2))
541 usage(cmd_snapshot_usage);
543 subvol = argv[optind];
544 dst = argv[optind + 1];
546 retval = 1; /* failure */
547 res = test_issubvolume(subvol);
549 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
553 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
557 res = test_isdir(dst);
559 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
564 newname = strdup(subvol);
565 newname = basename(newname);
568 newname = strdup(dst);
569 newname = basename(newname);
570 dstdir = strdup(dst);
571 dstdir = dirname(dstdir);
574 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
575 strchr(newname, '/') ){
576 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
581 len = strlen(newname);
582 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
583 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
588 fddst = open_file_or_dir(dstdir);
590 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
594 fd = open_file_or_dir(subvol);
596 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
601 args.flags |= BTRFS_SUBVOL_RDONLY;
602 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
603 subvol, dstdir, newname);
605 printf("Create a snapshot of '%s' in '%s/%s'\n",
606 subvol, dstdir, newname);
611 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
612 args.size = qgroup_inherit_size(inherit);
613 args.qgroup_inherit = inherit;
615 strncpy_null(args.name, newname);
617 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
620 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
621 subvol, strerror(errno));
625 retval = 0; /* success */
637 static const char * const cmd_subvol_get_default_usage[] = {
638 "btrfs subvolume get-default <path>",
639 "Get the default subvolume of a filesystem",
643 static int cmd_subvol_get_default(int argc, char **argv)
648 struct btrfs_list_filter_set *filter_set;
651 if (check_argc_exact(argc, 2))
652 usage(cmd_subvol_get_default_usage);
656 ret = test_issubvolume(subvol);
658 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
662 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
666 fd = open_file_or_dir(subvol);
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);
714 static const char * const cmd_subvol_set_default_usage[] = {
715 "btrfs subvolume set-default <subvolid> <path>",
716 "Set the default subvolume of a filesystem",
720 static int cmd_subvol_set_default(int argc, char **argv)
727 if (check_argc_exact(argc, 3))
728 usage(cmd_subvol_set_default_usage);
733 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
734 if (errno == ERANGE) {
735 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
739 fd = open_file_or_dir(path);
741 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
745 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
749 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
756 static const char * const cmd_find_new_usage[] = {
757 "btrfs subvolume find-new <path> <lastgen>",
758 "List the recently modified files in a filesystem",
762 static int cmd_find_new(int argc, char **argv)
769 if (check_argc_exact(argc, 3))
770 usage(cmd_find_new_usage);
773 last_gen = atoll(argv[2]);
775 ret = test_issubvolume(subvol);
777 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
781 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
785 fd = open_file_or_dir(subvol);
787 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
790 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
797 static const char * const cmd_subvol_show_usage[] = {
798 "btrfs subvolume show <subvol-path>",
799 "Show more information of the subvolume",
803 static int cmd_subvol_show(int argc, char **argv)
805 struct root_info get_ri;
806 struct btrfs_list_filter_set *filter_set;
809 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
810 char raw_prefix[] = "\t\t\t\t";
812 int fd = -1, mntfd = -1;
815 if (check_argc_exact(argc, 2))
816 usage(cmd_subvol_show_usage);
818 fullpath = realpath(argv[1], 0);
820 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
821 argv[1], strerror(errno));
825 ret = test_issubvolume(fullpath);
827 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
831 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
836 ret = find_mount_root(fullpath, &mnt);
838 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
839 "%s\n", fullpath, strerror(-ret));
843 svpath = get_subvol_name(mnt, fullpath);
845 fd = open_file_or_dir(fullpath);
847 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
851 ret = btrfs_list_get_path_rootid(fd, &sv_id);
853 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
858 mntfd = open_file_or_dir(mnt);
860 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
864 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
866 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
870 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
871 printf("%s is btrfs root\n", fullpath);
875 memset(&get_ri, 0, sizeof(get_ri));
876 get_ri.root_id = sv_id;
878 if (btrfs_get_subvol(mntfd, &get_ri)) {
879 fprintf(stderr, "ERROR: can't find '%s'\n",
886 printf("%s\n", fullpath);
887 printf("\tName: \t\t\t%s\n", get_ri.name);
889 if (uuid_is_null(get_ri.uuid))
890 strcpy(uuidparse, "-");
892 uuid_unparse(get_ri.uuid, uuidparse);
893 printf("\tuuid: \t\t\t%s\n", uuidparse);
895 if (uuid_is_null(get_ri.puuid))
896 strcpy(uuidparse, "-");
898 uuid_unparse(get_ri.puuid, uuidparse);
899 printf("\tParent uuid: \t\t%s\n", uuidparse);
902 strftime(tstr, 256, "%Y-%m-%d %X",
903 localtime(&get_ri.otime));
906 printf("\tCreation time: \t\t%s\n", tstr);
908 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
909 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
910 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
911 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
912 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
914 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
915 printf("\tFlags: \t\t\treadonly\n");
917 printf("\tFlags: \t\t\t-\n");
919 /* print the snapshots of the given subvol if any*/
920 printf("\tSnapshot(s):\n");
921 filter_set = btrfs_list_alloc_filter_set();
922 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
923 (u64)(unsigned long)get_ri.uuid);
924 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
925 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
933 if (get_ri.full_path)
934 free(get_ri.full_path);
936 btrfs_list_free_filter_set(filter_set);
951 const struct cmd_group subvolume_cmd_group = {
952 subvolume_cmd_group_usage, NULL, {
953 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
954 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
955 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
956 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
957 { "get-default", cmd_subvol_get_default,
958 cmd_subvol_get_default_usage, NULL, 0 },
959 { "set-default", cmd_subvol_set_default,
960 cmd_subvol_set_default_usage, NULL, 0 },
961 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
962 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
967 int cmd_subvolume(int argc, char **argv)
969 return handle_command_group(&subvolume_cmd_group, argc, argv);