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;
82 DIR *dirstream = NULL;
86 int c = getopt(argc, argv, "c:i:");
92 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
99 res = qgroup_inherit_add_group(&inherit, optarg);
106 usage(cmd_subvol_create_usage);
110 if (check_argc_exact(argc - optind, 1))
111 usage(cmd_subvol_create_usage);
115 retval = 1; /* failure */
116 res = test_isdir(dst);
118 fprintf(stderr, "ERROR: '%s' exists\n", dst);
122 newname = strdup(dst);
123 newname = basename(newname);
124 dstdir = strdup(dst);
125 dstdir = dirname(dstdir);
127 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
128 strchr(newname, '/') ){
129 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
134 len = strlen(newname);
135 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
136 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
141 fddst = open_file_or_dir(dstdir, &dirstream);
143 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
147 printf("Create subvolume '%s/%s'\n", dstdir, newname);
149 struct btrfs_ioctl_vol_args_v2 args;
151 memset(&args, 0, sizeof(args));
152 strncpy_null(args.name, newname);
153 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
154 args.size = qgroup_inherit_size(inherit);
155 args.qgroup_inherit = inherit;
157 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
159 struct btrfs_ioctl_vol_args args;
161 memset(&args, 0, sizeof(args));
162 strncpy_null(args.name, newname);
164 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
168 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
173 retval = 0; /* success */
175 close_file_or_dir(fddst, dirstream);
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;
212 DIR *dirstream = NULL;
215 usage(cmd_subvol_delete_usage);
220 res = test_issubvolume(path);
222 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
227 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
232 cpath = realpath(path, NULL);
233 dname = strdup(cpath);
234 dname = dirname(dname);
235 vname = strdup(cpath);
236 vname = basename(vname);
239 if (!strcmp(vname, ".") || !strcmp(vname, "..") ||
240 strchr(vname, '/')) {
241 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
248 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
249 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
255 fd = open_file_or_dir(dname, &dirstream);
257 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
262 printf("Delete subvolume '%s/%s'\n", dname, vname);
263 strncpy_null(args.name, vname);
264 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
267 close_file_or_dir(fd, dirstream);
270 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
271 dname, vname, strerror(e));
286 * - uppercase for filters and sort options
287 * - lowercase for enabling specific items in the output
289 static const char * const cmd_subvol_list_usage[] = {
290 "btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] "
291 "[--sort=gen,ogen,rootid,path] <path>",
292 "List subvolumes (and snapshots)",
294 "-p print parent ID",
295 "-a print all the subvolumes in the filesystem and",
296 " distinguish absolute and relative path with respect",
297 " to the given <path>",
298 "-c print the ogeneration of the subvolume",
299 "-g print the generation of the subvolume",
300 "-o print only subvolumes bellow specified path",
301 "-u print the uuid of subvolumes (and snapshots)",
302 "-q print the parent uuid of the snapshots",
303 "-t print the result as a table",
304 "-s list snapshots only in the filesystem",
305 "-r list readonly subvolumes (including snapshots)",
307 " filter the subvolumes by generation",
308 " (+value: >= value; -value: <= value; value: = value)",
310 " filter the subvolumes by ogeneration",
311 " (+value: >= value; -value: <= value; value: = value)",
312 "--sort=gen,ogen,rootid,path",
313 " list the subvolume in order of gen, ogen, rootid or path",
314 " you also can add '+' or '-' in front of each items.",
315 " (+:ascending, -:descending, ascending default)",
319 static int cmd_subvol_list(int argc, char **argv)
321 struct btrfs_list_filter_set *filter_set;
322 struct btrfs_list_comparer_set *comparer_set;
326 int ret = -1, uerr = 0;
329 int is_tab_result = 0;
331 int is_only_in_path = 0;
332 struct option long_options[] = {
333 {"sort", 1, NULL, 'S'},
336 DIR *dirstream = NULL;
338 filter_set = btrfs_list_alloc_filter_set();
339 comparer_set = btrfs_list_alloc_comparer_set();
343 c = getopt_long(argc, argv,
344 "acgopqsurG:C:t", long_options, NULL);
350 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
356 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
359 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
368 btrfs_list_setup_filter(&filter_set,
369 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
371 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
372 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
375 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
378 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
381 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
384 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
385 ret = btrfs_list_parse_filter_string(optarg,
387 BTRFS_LIST_FILTER_GEN);
395 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
396 ret = btrfs_list_parse_filter_string(optarg,
398 BTRFS_LIST_FILTER_CGEN);
405 ret = btrfs_list_parse_sort_string(optarg,
420 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
423 if (check_argc_exact(argc - optind, 1)) {
428 subvol = argv[optind];
429 fd = open_file_or_dir(subvol, &dirstream);
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);
467 close_file_or_dir(fd, dirstream);
469 btrfs_list_free_filter_set(filter_set);
471 btrfs_list_free_comparer_set(comparer_set);
473 usage(cmd_subvol_list_usage);
477 static const char * const cmd_snapshot_usage[] = {
478 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
479 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<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. If only <dest> is given,",
483 "the subvolume will be named the basename of <source>.",
485 "-r create a readonly snapshot",
486 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
487 " option can be given multiple times.",
491 static int cmd_snapshot(int argc, char **argv)
495 int fd = -1, fddst = -1;
496 int len, readonly = 0;
499 struct btrfs_ioctl_vol_args_v2 args;
500 struct btrfs_qgroup_inherit *inherit = NULL;
501 DIR *dirstream1 = NULL, *dirstream2 = NULL;
504 memset(&args, 0, sizeof(args));
506 int c = getopt(argc, argv, "c:i:r");
512 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
519 res = qgroup_inherit_add_group(&inherit, optarg);
529 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, &dirstream1);
590 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
594 fd = open_file_or_dir(subvol, &dirstream2);
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 */
628 close_file_or_dir(fddst, dirstream1);
629 close_file_or_dir(fd, dirstream2);
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;
648 DIR *dirstream = NULL;
650 if (check_argc_exact(argc, 2))
651 usage(cmd_subvol_get_default_usage);
654 fd = open_file_or_dir(subvol, &dirstream);
656 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
660 ret = btrfs_list_get_default_subvolume(fd, &default_id);
662 fprintf(stderr, "ERROR: can't perform the search - %s\n",
668 if (default_id == 0) {
669 fprintf(stderr, "ERROR: 'default' dir item not found\n");
673 /* no need to resolve roots if FS_TREE is default */
674 if (default_id == BTRFS_FS_TREE_OBJECTID) {
675 printf("ID 5 (FS_TREE)\n");
679 filter_set = btrfs_list_alloc_filter_set();
680 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
683 /* by default we shall print the following columns*/
684 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
685 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
686 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
687 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
689 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
690 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
693 btrfs_list_free_filter_set(filter_set);
695 close_file_or_dir(fd, dirstream);
699 static const char * const cmd_subvol_set_default_usage[] = {
700 "btrfs subvolume set-default <subvolid> <path>",
701 "Set the default subvolume of a filesystem",
705 static int cmd_subvol_set_default(int argc, char **argv)
711 DIR *dirstream = NULL;
713 if (check_argc_exact(argc, 3))
714 usage(cmd_subvol_set_default_usage);
719 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
720 if (errno == ERANGE) {
721 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
725 fd = open_file_or_dir(path, &dirstream);
727 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
731 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
733 close_file_or_dir(fd, dirstream);
735 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
742 static const char * const cmd_find_new_usage[] = {
743 "btrfs subvolume find-new <path> <lastgen>",
744 "List the recently modified files in a filesystem",
748 static int cmd_find_new(int argc, char **argv)
754 DIR *dirstream = NULL;
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, &dirstream);
774 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
777 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
778 close_file_or_dir(fd, dirstream);
782 static const char * const cmd_subvol_show_usage[] = {
783 "btrfs subvolume show <subvol-path>",
784 "Show more information of the subvolume",
788 static int cmd_subvol_show(int argc, char **argv)
790 struct root_info get_ri;
791 struct btrfs_list_filter_set *filter_set;
794 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
795 char raw_prefix[] = "\t\t\t\t";
797 int fd = -1, mntfd = -1;
799 DIR *dirstream1 = NULL, *dirstream2 = NULL;
801 if (check_argc_exact(argc, 2))
802 usage(cmd_subvol_show_usage);
804 fullpath = realpath(argv[1], NULL);
806 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
807 argv[1], strerror(errno));
811 ret = test_issubvolume(fullpath);
813 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
817 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
821 ret = find_mount_root(fullpath, &mnt);
823 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
824 "%s\n", fullpath, strerror(-ret));
828 svpath = get_subvol_name(mnt, fullpath);
830 fd = open_file_or_dir(fullpath, &dirstream1);
832 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
836 ret = btrfs_list_get_path_rootid(fd, &sv_id);
838 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
843 mntfd = open_file_or_dir(mnt, &dirstream2);
845 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
849 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
851 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
855 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
856 printf("%s is btrfs root\n", fullpath);
860 memset(&get_ri, 0, sizeof(get_ri));
861 get_ri.root_id = sv_id;
863 if (btrfs_get_subvol(mntfd, &get_ri)) {
864 fprintf(stderr, "ERROR: can't find '%s'\n",
871 printf("%s\n", fullpath);
872 printf("\tName: \t\t\t%s\n", get_ri.name);
874 if (uuid_is_null(get_ri.uuid))
875 strcpy(uuidparse, "-");
877 uuid_unparse(get_ri.uuid, uuidparse);
878 printf("\tuuid: \t\t\t%s\n", uuidparse);
880 if (uuid_is_null(get_ri.puuid))
881 strcpy(uuidparse, "-");
883 uuid_unparse(get_ri.puuid, uuidparse);
884 printf("\tParent uuid: \t\t%s\n", uuidparse);
889 localtime_r(&get_ri.otime, &tm);
890 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
893 printf("\tCreation time: \t\t%s\n", tstr);
895 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
896 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
897 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
898 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
899 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
901 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
902 printf("\tFlags: \t\t\treadonly\n");
904 printf("\tFlags: \t\t\t-\n");
906 /* print the snapshots of the given subvol if any*/
907 printf("\tSnapshot(s):\n");
908 filter_set = btrfs_list_alloc_filter_set();
909 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
910 (u64)(unsigned long)get_ri.uuid);
911 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
912 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
920 if (get_ri.full_path)
921 free(get_ri.full_path);
923 btrfs_list_free_filter_set(filter_set);
926 close_file_or_dir(fd, dirstream1);
927 close_file_or_dir(mntfd, dirstream2);
935 const struct cmd_group subvolume_cmd_group = {
936 subvolume_cmd_group_usage, NULL, {
937 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
938 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
939 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
940 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
941 { "get-default", cmd_subvol_get_default,
942 cmd_subvol_get_default_usage, NULL, 0 },
943 { "set-default", cmd_subvol_set_default,
944 cmd_subvol_set_default_usage, NULL, 0 },
945 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
946 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
951 int cmd_subvolume(int argc, char **argv)
953 return handle_command_group(&subvolume_cmd_group, argc, argv);