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);
164 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);
247 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
252 printf("Delete subvolume '%s/%s'\n", dname, vname);
253 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
254 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
255 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
261 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
262 dname, vname, strerror(e));
277 * - uppercase for filters and sort options
278 * - lowercase for enabling specific items in the output
280 static const char * const cmd_subvol_list_usage[] = {
281 "btrfs subvolume list [-agopurts] [-G [+|-]value] [-C [+|-]value] "
282 "[--sort=gen,ogen,rootid,path] <path>",
283 "List subvolumes (and snapshots)",
285 "-p print parent ID",
286 "-a print all the subvolumes in the filesystem and",
287 " distinguish absolute and relative path with respect",
288 " to the given <path>",
289 "-c print the ogeneration of the subvolume",
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 "acgopqsurG:C:t", long_options, NULL);
340 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
346 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
349 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
358 btrfs_list_setup_filter(&filter_set,
359 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
361 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
362 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
365 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
368 btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
371 flags |= BTRFS_ROOT_SUBVOL_RDONLY;
374 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
375 ret = btrfs_list_parse_filter_string(optarg,
377 BTRFS_LIST_FILTER_GEN);
385 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
386 ret = btrfs_list_parse_filter_string(optarg,
388 BTRFS_LIST_FILTER_CGEN);
395 ret = btrfs_list_parse_sort_string(optarg,
410 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
413 if (check_argc_exact(argc - optind, 1)) {
418 subvol = argv[optind];
420 ret = test_issubvolume(subvol);
422 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
426 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
431 fd = open_file_or_dir(subvol);
434 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
438 top_id = btrfs_list_get_path_rootid(fd);
441 btrfs_list_setup_filter(&filter_set,
442 BTRFS_LIST_FILTER_FULL_PATH,
444 else if (is_only_in_path)
445 btrfs_list_setup_filter(&filter_set,
446 BTRFS_LIST_FILTER_TOPID_EQUAL,
449 /* by default we shall print the following columns*/
450 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
451 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
452 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
453 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
456 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
457 BTRFS_LIST_LAYOUT_TABLE,
458 !is_list_all && !is_only_in_path, NULL);
460 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
461 BTRFS_LIST_LAYOUT_DEFAULT,
462 !is_list_all && !is_only_in_path, NULL);
466 btrfs_list_free_filter_set(filter_set);
468 btrfs_list_free_comparer_set(comparer_set);
470 usage(cmd_subvol_list_usage);
475 static const char * const cmd_snapshot_usage[] = {
476 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
477 "Create a snapshot of the subvolume",
478 "Create a writable/readonly snapshot of the subvolume <source> with",
479 "the name <name> in the <dest> directory",
481 "-r create a readonly snapshot",
485 static int cmd_snapshot(int argc, char **argv)
488 int res, fd, fddst, len, e, readonly = 0;
491 struct btrfs_ioctl_vol_args_v2 args;
492 struct btrfs_qgroup_inherit *inherit = NULL;
495 memset(&args, 0, sizeof(args));
497 int c = getopt(argc, argv, "c:i:r");
503 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
508 res = qgroup_inherit_add_group(&inherit, optarg);
516 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
521 usage(cmd_snapshot_usage);
525 if (check_argc_exact(argc - optind, 2))
526 usage(cmd_snapshot_usage);
528 subvol = argv[optind];
529 dst = argv[optind + 1];
531 res = test_issubvolume(subvol);
533 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
537 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
541 res = test_isdir(dst);
543 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
548 newname = strdup(subvol);
549 newname = basename(newname);
552 newname = strdup(dst);
553 newname = basename(newname);
554 dstdir = strdup(dst);
555 dstdir = dirname(dstdir);
558 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
559 strchr(newname, '/') ){
560 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
565 len = strlen(newname);
566 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
567 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
572 fddst = open_file_or_dir(dstdir);
574 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
578 fd = open_file_or_dir(subvol);
581 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
586 args.flags |= BTRFS_SUBVOL_RDONLY;
587 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
588 subvol, dstdir, newname);
590 printf("Create a snapshot of '%s' in '%s/%s'\n",
591 subvol, dstdir, newname);
596 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
597 args.size = qgroup_inherit_size(inherit);
598 args.qgroup_inherit = inherit;
600 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
601 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
602 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
610 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
611 subvol, strerror(e));
618 static const char * const cmd_subvol_get_default_usage[] = {
619 "btrfs subvolume get-default <path>",
620 "Get the default subvolume of a filesystem",
624 static int cmd_subvol_get_default(int argc, char **argv)
629 struct btrfs_list_filter_set *filter_set;
632 if (check_argc_exact(argc, 2))
633 usage(cmd_subvol_get_default_usage);
637 ret = test_issubvolume(subvol);
639 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
643 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
647 fd = open_file_or_dir(subvol);
649 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
653 ret = btrfs_list_get_default_subvolume(fd, &default_id);
655 fprintf(stderr, "ERROR: can't perform the search - %s\n",
660 if (default_id == 0) {
661 fprintf(stderr, "ERROR: 'default' dir item not found\n");
665 /* no need to resolve roots if FS_TREE is default */
666 if (default_id == BTRFS_FS_TREE_OBJECTID) {
667 printf("ID 5 (FS_TREE)\n");
671 filter_set = btrfs_list_alloc_filter_set();
672 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
675 /* by default we shall print the following columns*/
676 btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
677 btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
678 btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
679 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
681 ret = btrfs_list_subvols_print(fd, filter_set, NULL,
682 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
685 btrfs_list_free_filter_set(filter_set);
691 static const char * const cmd_subvol_set_default_usage[] = {
692 "btrfs subvolume set-default <subvolid> <path>",
693 "Set the default subvolume of a filesystem",
697 static int cmd_subvol_set_default(int argc, char **argv)
704 if (check_argc_exact(argc, 3))
705 usage(cmd_subvol_set_default_usage);
710 fd = open_file_or_dir(path);
712 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
716 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
717 if (errno == ERANGE) {
718 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
721 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
725 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
732 static const char * const cmd_find_new_usage[] = {
733 "btrfs subvolume find-new <path> <lastgen>",
734 "List the recently modified files in a filesystem",
738 static int cmd_find_new(int argc, char **argv)
745 if (check_argc_exact(argc, 3))
746 usage(cmd_find_new_usage);
749 last_gen = atoll(argv[2]);
751 ret = test_issubvolume(subvol);
753 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
757 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
761 fd = open_file_or_dir(subvol);
763 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
766 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
773 static const char * const cmd_subvol_show_usage[] = {
774 "btrfs subvolume show <subvol-path>",
775 "Show more information of the subvolume",
779 static int cmd_subvol_show(int argc, char **argv)
781 struct root_info get_ri;
782 struct btrfs_list_filter_set *filter_set;
785 char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
786 char raw_prefix[] = "\t\t\t\t";
788 int fd = -1, mntfd = -1;
791 if (check_argc_exact(argc, 2))
792 usage(cmd_subvol_show_usage);
794 fullpath = realpath(argv[1], 0);
796 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
797 argv[1], strerror(errno));
801 ret = test_issubvolume(fullpath);
803 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
807 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
812 ret = find_mount_root(fullpath, &mnt);
814 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
815 "%s\n", fullpath, strerror(-ret));
819 svpath = get_subvol_name(mnt, fullpath);
821 fd = open_file_or_dir(fullpath);
823 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
827 sv_id = btrfs_list_get_path_rootid(fd);
829 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
834 mntfd = open_file_or_dir(mnt);
836 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
840 mntid = btrfs_list_get_path_rootid(mntfd);
842 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
846 if (sv_id == BTRFS_FS_TREE_OBJECTID) {
847 printf("%s is btrfs root\n", fullpath);
851 memset(&get_ri, 0, sizeof(get_ri));
852 get_ri.root_id = sv_id;
854 if (btrfs_get_subvol(mntfd, &get_ri)) {
855 fprintf(stderr, "ERROR: can't find '%s'\n",
862 printf("%s\n", fullpath);
863 printf("\tName: \t\t\t%s\n", get_ri.name);
865 if (uuid_is_null(get_ri.uuid))
866 strcpy(uuidparse, "-");
868 uuid_unparse(get_ri.uuid, uuidparse);
869 printf("\tuuid: \t\t\t%s\n", uuidparse);
871 if (uuid_is_null(get_ri.puuid))
872 strcpy(uuidparse, "-");
874 uuid_unparse(get_ri.puuid, uuidparse);
875 printf("\tParent uuid: \t\t%s\n", uuidparse);
878 strftime(tstr, 256, "%Y-%m-%d %X",
879 localtime(&get_ri.otime));
882 printf("\tCreation time: \t\t%s\n", tstr);
884 printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
885 printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
886 printf("\tGen at creation: \t%llu\n", get_ri.ogen);
887 printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
888 printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
890 if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
891 printf("\tFlags: \t\t\treadonly\n");
893 printf("\tFlags: \t\t\t-\n");
895 /* print the snapshots of the given subvol if any*/
896 printf("\tSnapshot(s):\n");
897 filter_set = btrfs_list_alloc_filter_set();
898 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
900 btrfs_list_setup_print_column(BTRFS_LIST_PATH);
901 btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
909 if (get_ri.full_path)
910 free(get_ri.full_path);
912 btrfs_list_free_filter_set(filter_set);
927 const struct cmd_group subvolume_cmd_group = {
928 subvolume_cmd_group_usage, NULL, {
929 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
930 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
931 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
932 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
933 { "get-default", cmd_subvol_get_default,
934 cmd_subvol_get_default_usage, NULL, 0 },
935 { "set-default", cmd_subvol_set_default,
936 cmd_subvol_set_default_usage, NULL, 0 },
937 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
938 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
943 int cmd_subvolume(int argc, char **argv)
945 return handle_command_group(&subvolume_cmd_group, argc, argv);