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);
478 static const char * const cmd_snapshot_usage[] = {
479 "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
480 "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
481 "Create a snapshot of the subvolume",
482 "Create a writable/readonly snapshot of the subvolume <source> with",
483 "the name <name> in the <dest> directory. If only <dest> is given,",
484 "the subvolume will be named the basename of <source>.",
486 "-r create a readonly snapshot",
487 "-i <qgroupid> add the newly created snapshot to a qgroup. This",
488 " option can be given multiple times.",
492 static int cmd_snapshot(int argc, char **argv)
496 int fd = -1, fddst = -1;
497 int len, readonly = 0;
500 struct btrfs_ioctl_vol_args_v2 args;
501 struct btrfs_qgroup_inherit *inherit = NULL;
502 DIR *dirstream1 = NULL, *dirstream2 = NULL;
505 memset(&args, 0, sizeof(args));
507 int c = getopt(argc, argv, "c:i:r");
513 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
520 res = qgroup_inherit_add_group(&inherit, optarg);
530 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
537 usage(cmd_snapshot_usage);
541 if (check_argc_exact(argc - optind, 2))
542 usage(cmd_snapshot_usage);
544 subvol = argv[optind];
545 dst = argv[optind + 1];
547 retval = 1; /* failure */
548 res = test_issubvolume(subvol);
550 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
554 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
558 res = test_isdir(dst);
560 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
565 newname = strdup(subvol);
566 newname = basename(newname);
569 newname = strdup(dst);
570 newname = basename(newname);
571 dstdir = strdup(dst);
572 dstdir = dirname(dstdir);
575 if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
576 strchr(newname, '/') ){
577 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
582 len = strlen(newname);
583 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
584 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
589 fddst = open_file_or_dir(dstdir, &dirstream1);
591 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
595 fd = open_file_or_dir(subvol, &dirstream2);
597 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
602 args.flags |= BTRFS_SUBVOL_RDONLY;
603 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
604 subvol, dstdir, newname);
606 printf("Create a snapshot of '%s' in '%s/%s'\n",
607 subvol, dstdir, newname);
612 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
613 args.size = qgroup_inherit_size(inherit);
614 args.qgroup_inherit = inherit;
616 strncpy_null(args.name, newname);
618 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
621 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
622 subvol, strerror(errno));
626 retval = 0; /* success */
629 close_file_or_dir(fddst, dirstream1);
630 close_file_or_dir(fd, dirstream2);
636 static const char * const cmd_subvol_get_default_usage[] = {
637 "btrfs subvolume get-default <path>",
638 "Get the default subvolume of a filesystem",
642 static int cmd_subvol_get_default(int argc, char **argv)
647 struct btrfs_list_filter_set *filter_set;
649 DIR *dirstream = NULL;
651 if (check_argc_exact(argc, 2))
652 usage(cmd_subvol_get_default_usage);
655 fd = open_file_or_dir(subvol, &dirstream);
657 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
661 ret = btrfs_list_get_default_subvolume(fd, &default_id);
663 fprintf(stderr, "ERROR: can't perform the search - %s\n",
669 if (default_id == 0) {
670 fprintf(stderr, "ERROR: 'default' dir item not found\n");
674 /* no need to resolve roots if FS_TREE is default */
675 if (default_id == BTRFS_FS_TREE_OBJECTID) {
676 printf("ID 5 (FS_TREE)\n");
680 filter_set = btrfs_list_alloc_filter_set();
681 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
684 /* by default we shall print the following columns*/
685 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
686 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
687 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
688 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
690 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
691 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
694 btrfs_list_free_filter_set(filter_set);
696 close_file_or_dir(fd, dirstream);
702 static const char * const cmd_subvol_set_default_usage[] = {
703 "btrfs subvolume set-default <subvolid> <path>",
704 "Set the default subvolume of a filesystem",
708 static int cmd_subvol_set_default(int argc, char **argv)
714 DIR *dirstream = NULL;
716 if (check_argc_exact(argc, 3))
717 usage(cmd_subvol_set_default_usage);
722 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
723 if (errno == ERANGE) {
724 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
728 fd = open_file_or_dir(path, &dirstream);
730 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
734 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
736 close_file_or_dir(fd, dirstream);
738 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
745 static const char * const cmd_find_new_usage[] = {
746 "btrfs subvolume find-new <path> <lastgen>",
747 "List the recently modified files in a filesystem",
751 static int cmd_find_new(int argc, char **argv)
757 DIR *dirstream = NULL;
759 if (check_argc_exact(argc, 3))
760 usage(cmd_find_new_usage);
763 last_gen = atoll(argv[2]);
765 ret = test_issubvolume(subvol);
767 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
771 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
775 fd = open_file_or_dir(subvol, &dirstream);
777 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
780 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
781 close_file_or_dir(fd, dirstream);
787 static const char * const cmd_subvol_show_usage[] = {
788 "btrfs subvolume show <subvol-path>",
789 "Show more information of the subvolume",
793 static int cmd_subvol_show(int argc, char **argv)
795 struct root_info get_ri;
796 struct btrfs_list_filter_set *filter_set;
799 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
800 char raw_prefix[] = "\t\t\t\t";
802 int fd = -1, mntfd = -1;
804 DIR *dirstream1 = NULL, *dirstream2 = NULL;
806 if (check_argc_exact(argc, 2))
807 usage(cmd_subvol_show_usage);
809 fullpath = realpath(argv[1], NULL);
811 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
812 argv[1], strerror(errno));
816 ret = test_issubvolume(fullpath);
818 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
822 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
827 ret = find_mount_root(fullpath, &mnt);
829 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
830 "%s\n", fullpath, strerror(-ret));
834 svpath = get_subvol_name(mnt, fullpath);
836 fd = open_file_or_dir(fullpath, &dirstream1);
838 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
842 ret = btrfs_list_get_path_rootid(fd, &sv_id);
844 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
849 mntfd = open_file_or_dir(mnt, &dirstream2);
851 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
855 ret = btrfs_list_get_path_rootid(mntfd, &mntid);
857 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
861 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
862 printf("%s is btrfs root\n", fullpath);
866 memset(&get_ri, 0, sizeof(get_ri));
867 get_ri.root_id = sv_id;
869 if (btrfs_get_subvol(mntfd, &get_ri)) {
870 fprintf(stderr, "ERROR: can't find '%s'\n",
877 printf("%s\n", fullpath);
878 printf("\tName: \t\t\t%s\n", get_ri.name);
880 if (uuid_is_null(get_ri.uuid))
881 strcpy(uuidparse, "-");
883 uuid_unparse(get_ri.uuid, uuidparse);
884 printf("\tuuid: \t\t\t%s\n", uuidparse);
886 if (uuid_is_null(get_ri.puuid))
887 strcpy(uuidparse, "-");
889 uuid_unparse(get_ri.puuid, uuidparse);
890 printf("\tParent uuid: \t\t%s\n", uuidparse);
895 localtime_r(&get_ri.otime, &tm);
896 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
899 printf("\tCreation time: \t\t%s\n", tstr);
901 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
902 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
903 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
904 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
905 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
907 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
908 printf("\tFlags: \t\t\treadonly\n");
910 printf("\tFlags: \t\t\t-\n");
912 /* print the snapshots of the given subvol if any*/
913 printf("\tSnapshot(s):\n");
914 filter_set = btrfs_list_alloc_filter_set();
915 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
916 (u64)(unsigned long)get_ri.uuid);
917 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
918 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
926 if (get_ri.full_path)
927 free(get_ri.full_path);
929 btrfs_list_free_filter_set(filter_set);
932 close_file_or_dir(fd, dirstream1);
933 close_file_or_dir(mntfd, dirstream2);
942 const struct cmd_group subvolume_cmd_group = {
943 subvolume_cmd_group_usage, NULL, {
944 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
945 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
946 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
947 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
948 { "get-default", cmd_subvol_get_default,
949 cmd_subvol_get_default_usage, NULL, 0 },
950 { "set-default", cmd_subvol_set_default,
951 cmd_subvol_set_default_usage, NULL, 0 },
952 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
953 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
958 int cmd_subvolume(int argc, char **argv)
960 return handle_command_group(&subvolume_cmd_group, argc, argv);