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 [<dest>/]<name>",
66 "Create a subvolume <name> in <dest>. If <dest> is not given",
67 "subvolume <name> will be created in the current directory.",
71 static int cmd_subvol_create(int argc, char **argv)
73 int res, fddst, len, e;
77 struct btrfs_qgroup_inherit *inherit = NULL;
81 int c = getopt(argc, argv, "c:i:r");
87 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
92 res = qgroup_inherit_add_group(&inherit, optarg);
97 usage(cmd_subvol_create_usage);
101 if (check_argc_exact(argc - optind, 1))
102 usage(cmd_subvol_create_usage);
106 res = test_isdir(dst);
108 fprintf(stderr, "ERROR: '%s' exists\n", dst);
112 newname = strdup(dst);
113 newname = basename(newname);
114 dstdir = strdup(dst);
115 dstdir = dirname(dstdir);
117 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
118 strchr(newname, '/') ){
119 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
124 len = strlen(newname);
125 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
126 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
131 fddst = open_file_or_dir(dstdir);
133 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
137 printf("Create subvolume '%s/%s'\n", dstdir, newname);
139 struct btrfs_ioctl_vol_args_v2 args;
141 memset(&args, 0, sizeof(args));
142 strncpy_null(args.name, newname);
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_null(args.name, newname);
154 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
163 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
172 * test if path is a subvolume:
173 * this function return
174 * 0-> path exists but it is not a subvolume
175 * 1-> path exists and it is a subvolume
176 * -1 -> path is unaccessible
178 int test_issubvolume(char *path)
183 res = stat(path, &st);
187 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
190 static const char * const cmd_subvol_delete_usage[] = {
191 "btrfs subvolume delete <subvolume> [<subvolume>...]",
192 "Delete subvolume(s)",
196 static int cmd_subvol_delete(int argc, char **argv)
198 int res, fd, len, e, cnt = 1, ret = 0;
199 struct btrfs_ioctl_vol_args args;
200 char *dname, *vname, *cpath;
204 usage(cmd_subvol_delete_usage);
209 res = test_issubvolume(path);
211 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
216 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
221 cpath = realpath(path, 0);
222 dname = strdup(cpath);
223 dname = dirname(dname);
224 vname = strdup(cpath);
225 vname = basename(vname);
228 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
229 strchr(vname, '/') ){
230 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
237 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
238 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
244 fd = open_file_or_dir(dname);
246 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
251 printf("Delete subvolume '%s/%s'\n", dname, vname);
252 strncpy_null(args.name, vname);
253 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
259 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
260 dname, vname, strerror(e));
275 * - uppercase for filters and sort options
276 * - lowercase for enabling specific items in the output
278 static const char * const cmd_subvol_list_usage[] = {
279 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
280 "[--sort=gen,ogen,rootid,path] <path>",
281 "List subvolumes (and snapshots)",
283 "-p print parent ID",
284 "-a print all the subvolumes in the filesystem and",
285 " distinguish absolute and relative path with respect",
286 " to the given <path>",
287 "-c print the ogeneration of the subvolume",
288 "-g print the generation of the subvolume",
289 "-o print only subvolumes bellow specified path",
290 "-u print the uuid of subvolumes (and snapshots)",
291 "-q print the parent uuid of the snapshots",
292 "-t print the result as a table",
293 "-s list snapshots only in the filesystem",
294 "-r list readonly subvolumes (including snapshots)",
296 " filter the subvolumes by generation",
297 " (+value: >= value; -value: <= value; value: = value)",
299 " filter the subvolumes by ogeneration",
300 " (+value: >= value; -value: <= value; value: = value)",
301 "--sort=gen,ogen,rootid,path",
302 " list the subvolume in order of gen, ogen, rootid or path",
303 " you also can add '+' or '-' in front of each items.",
304 " (+:ascending, -:descending, ascending default)",
308 static int cmd_subvol_list(int argc, char **argv)
310 struct btrfs_list_filter_set *filter_set;
311 struct btrfs_list_comparer_set *comparer_set;
315 int ret = -1, uerr = 0;
318 int is_tab_result = 0;
320 int is_only_in_path = 0;
321 struct option long_options[] = {
322 {"sort", 1, NULL, 'S'},
326 filter_set = btrfs_list_alloc_filter_set();
327 comparer_set = btrfs_list_alloc_comparer_set();
331 c = getopt_long(argc, argv,
332 "acgopqsurG:C:t", long_options, NULL);
338 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
344 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
347 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
356 btrfs_list_setup_filter(&filter_set,
357 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
359 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
360 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
363 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
366 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
369 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
372 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
373 ret = btrfs_list_parse_filter_string(optarg,
375 BTRFS_LIST_FILTER_GEN);
383 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
384 ret = btrfs_list_parse_filter_string(optarg,
386 BTRFS_LIST_FILTER_CGEN);
393 ret = btrfs_list_parse_sort_string(optarg,
408 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
411 if (check_argc_exact(argc - optind, 1)) {
416 subvol = argv[optind];
418 ret = test_issubvolume(subvol);
420 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
424 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
429 fd = open_file_or_dir(subvol);
432 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
436 ret = btrfs_list_get_path_rootid(fd, &top_id);
438 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
443 btrfs_list_setup_filter(&filter_set,
444 BTRFS_LIST_FILTER_FULL_PATH,
446 else if (is_only_in_path)
447 btrfs_list_setup_filter(&filter_set,
448 BTRFS_LIST_FILTER_TOPID_EQUAL,
451 /* by default we shall print the following columns*/
452 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
453 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
454 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
455 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
458 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
459 BTRFS_LIST_LAYOUT_TABLE,
460 !is_list_all && !is_only_in_path, NULL);
462 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
463 BTRFS_LIST_LAYOUT_DEFAULT,
464 !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 "Create a snapshot of the subvolume",
480 "Create a writable/readonly snapshot of the subvolume <source> with",
481 "the name <name> in the <dest> directory",
483 "-r create a readonly snapshot",
487 static int cmd_snapshot(int argc, char **argv)
490 int res, fd, fddst, len, e, readonly = 0;
493 struct btrfs_ioctl_vol_args_v2 args;
494 struct btrfs_qgroup_inherit *inherit = NULL;
497 memset(&args, 0, sizeof(args));
499 int c = getopt(argc, argv, "c:i:r");
505 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
510 res = qgroup_inherit_add_group(&inherit, optarg);
518 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
523 usage(cmd_snapshot_usage);
527 if (check_argc_exact(argc - optind, 2))
528 usage(cmd_snapshot_usage);
530 subvol = argv[optind];
531 dst = argv[optind + 1];
533 res = test_issubvolume(subvol);
535 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
539 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
543 res = test_isdir(dst);
545 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
550 newname = strdup(subvol);
551 newname = basename(newname);
554 newname = strdup(dst);
555 newname = basename(newname);
556 dstdir = strdup(dst);
557 dstdir = dirname(dstdir);
560 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
561 strchr(newname, '/') ){
562 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
567 len = strlen(newname);
568 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
569 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
574 fddst = open_file_or_dir(dstdir);
576 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
580 fd = open_file_or_dir(subvol);
583 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
588 args.flags |= BTRFS_SUBVOL_RDONLY;
589 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
590 subvol, dstdir, newname);
592 printf("Create a snapshot of '%s' in '%s/%s'\n",
593 subvol, dstdir, newname);
598 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
599 args.size = qgroup_inherit_size(inherit);
600 args.qgroup_inherit = inherit;
602 strncpy_null(args.name, newname);
603 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
611 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
612 subvol, strerror(e));
619 static const char * const cmd_subvol_get_default_usage[] = {
620 "btrfs subvolume get-default <path>",
621 "Get the default subvolume of a filesystem",
625 static int cmd_subvol_get_default(int argc, char **argv)
630 struct btrfs_list_filter_set *filter_set;
633 if (check_argc_exact(argc, 2))
634 usage(cmd_subvol_get_default_usage);
638 ret = test_issubvolume(subvol);
640 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
644 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
648 fd = open_file_or_dir(subvol);
650 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
654 ret = btrfs_list_get_default_subvolume(fd, &default_id);
656 fprintf(stderr, "ERROR: can't perform the search - %s\n",
661 if (default_id == 0) {
662 fprintf(stderr, "ERROR: 'default' dir item not found\n");
666 /* no need to resolve roots if FS_TREE is default */
667 if (default_id == BTRFS_FS_TREE_OBJECTID) {
668 printf("ID 5 (FS_TREE)\n");
672 filter_set = btrfs_list_alloc_filter_set();
673 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
676 /* by default we shall print the following columns*/
677 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
678 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
679 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
680 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
682 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
683 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
686 btrfs_list_free_filter_set(filter_set);
692 static const char * const cmd_subvol_set_default_usage[] = {
693 "btrfs subvolume set-default <subvolid> <path>",
694 "Set the default subvolume of a filesystem",
698 static int cmd_subvol_set_default(int argc, char **argv)
705 if (check_argc_exact(argc, 3))
706 usage(cmd_subvol_set_default_usage);
711 fd = open_file_or_dir(path);
713 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
717 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
718 if (errno == ERANGE) {
719 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
722 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
726 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
733 static const char * const cmd_find_new_usage[] = {
734 "btrfs subvolume find-new <path> <lastgen>",
735 "List the recently modified files in a filesystem",
739 static int cmd_find_new(int argc, char **argv)
746 if (check_argc_exact(argc, 3))
747 usage(cmd_find_new_usage);
750 last_gen = atoll(argv[2]);
752 ret = test_issubvolume(subvol);
754 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
758 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
762 fd = open_file_or_dir(subvol);
764 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
767 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
774 static const char * const cmd_subvol_show_usage[] = {
775 "btrfs subvolume show <subvol-path>",
776 "Show more information of the subvolume",
780 static int cmd_subvol_show(int argc, char **argv)
782 struct root_info get_ri;
783 struct btrfs_list_filter_set *filter_set;
786 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
787 char raw_prefix[] = "\t\t\t\t";
789 int fd = -1, mntfd = -1;
792 if (check_argc_exact(argc, 2))
793 usage(cmd_subvol_show_usage);
795 fullpath = realpath(argv[1], 0);
797 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
798 argv[1], strerror(errno));
802 ret = test_issubvolume(fullpath);
804 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
808 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
813 ret = find_mount_root(fullpath, &mnt);
815 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
816 "%s\n", fullpath, strerror(-ret));
820 svpath = get_subvol_name(mnt, fullpath);
822 fd = open_file_or_dir(fullpath);
824 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
828 ret = btrfs_list_get_path_rootid(fd, &sv_id);
830 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
835 mntfd = open_file_or_dir(mnt);
837 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
841 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
843 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
847 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
848 printf("%s is btrfs root\n", fullpath);
852 memset(&get_ri, 0, sizeof(get_ri));
853 get_ri.root_id = sv_id;
855 if (btrfs_get_subvol(mntfd, &get_ri)) {
856 fprintf(stderr, "ERROR: can't find '%s'\n",
863 printf("%s\n", fullpath);
864 printf("\tName: \t\t\t%s\n", get_ri.name);
866 if (uuid_is_null(get_ri.uuid))
867 strcpy(uuidparse, "-");
869 uuid_unparse(get_ri.uuid, uuidparse);
870 printf("\tuuid: \t\t\t%s\n", uuidparse);
872 if (uuid_is_null(get_ri.puuid))
873 strcpy(uuidparse, "-");
875 uuid_unparse(get_ri.puuid, uuidparse);
876 printf("\tParent uuid: \t\t%s\n", uuidparse);
879 strftime(tstr, 256, "%Y-%m-%d %X",
880 localtime(&get_ri.otime));
883 printf("\tCreation time: \t\t%s\n", tstr);
885 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
886 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
887 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
888 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
889 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
891 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
892 printf("\tFlags: \t\t\treadonly\n");
894 printf("\tFlags: \t\t\t-\n");
896 /* print the snapshots of the given subvol if any*/
897 printf("\tSnapshot(s):\n");
898 filter_set = btrfs_list_alloc_filter_set();
899 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
901 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
902 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
910 if (get_ri.full_path)
911 free(get_ri.full_path);
913 btrfs_list_free_filter_set(filter_set);
928 const struct cmd_group subvolume_cmd_group = {
929 subvolume_cmd_group_usage, NULL, {
930 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
931 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
932 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
933 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
934 { "get-default", cmd_subvol_get_default,
935 cmd_subvol_get_default_usage, NULL, 0 },
936 { "set-default", cmd_subvol_set_default,
937 cmd_subvol_set_default_usage, NULL, 0 },
938 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
939 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
944 int cmd_subvolume(int argc, char **argv)
946 return handle_command_group(&subvolume_cmd_group, argc, argv);