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 "kerncompat.h"
33 #include "btrfs-list.h"
35 static const char * const subvolume_cmd_group_usage[] = {
36 "btrfs subvolume <command> <args>",
41 * test if path is a directory
42 * this function return
43 * 0-> path exists but it is not a directory
44 * 1-> path exists and it is a directory
45 * -1 -> path is unaccessible
47 static int test_isdir(char *path)
52 res = stat(path, &st);
56 return S_ISDIR(st.st_mode);
59 static const char * const cmd_subvol_create_usage[] = {
60 "btrfs subvolume create [<dest>/]<name>",
62 "Create a subvolume <name> in <dest>. If <dest> is not given",
63 "subvolume <name> will be created in the current directory.",
67 static int cmd_subvol_create(int argc, char **argv)
69 int res, fddst, len, e;
73 struct btrfs_qgroup_inherit *inherit = NULL;
77 int c = getopt(argc, argv, "c:i:r");
83 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
88 res = qgroup_inherit_add_group(&inherit, optarg);
93 usage(cmd_subvol_create_usage);
97 if (check_argc_exact(argc - optind, 1))
98 usage(cmd_subvol_create_usage);
102 res = test_isdir(dst);
104 fprintf(stderr, "ERROR: '%s' exists\n", dst);
108 newname = strdup(dst);
109 newname = basename(newname);
110 dstdir = strdup(dst);
111 dstdir = dirname(dstdir);
113 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
114 strchr(newname, '/') ){
115 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
120 len = strlen(newname);
121 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
122 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
127 fddst = open_file_or_dir(dstdir);
129 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
133 printf("Create subvolume '%s/%s'\n", dstdir, newname);
135 struct btrfs_ioctl_vol_args_v2 args;
137 memset(&args, 0, sizeof(args));
138 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
139 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
140 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
141 args.size = qgroup_inherit_size(inherit);
142 args.qgroup_inherit = inherit;
144 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
146 struct btrfs_ioctl_vol_args args;
148 memset(&args, 0, sizeof(args));
149 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
150 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
152 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
160 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
170 * test if path is a subvolume:
171 * this function return
172 * 0-> path exists but it is not a subvolume
173 * 1-> path exists and it is a subvolume
174 * -1 -> path is unaccessible
176 int test_issubvolume(char *path)
181 res = stat(path, &st);
185 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
188 static const char * const cmd_subvol_delete_usage[] = {
189 "btrfs subvolume delete <name>",
190 "Delete a subvolume",
194 static int cmd_subvol_delete(int argc, char **argv)
197 struct btrfs_ioctl_vol_args args;
198 char *dname, *vname, *cpath;
201 if (check_argc_exact(argc, 2))
202 usage(cmd_subvol_delete_usage);
206 res = test_issubvolume(path);
208 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
212 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
216 cpath = realpath(path, 0);
217 dname = strdup(cpath);
218 dname = dirname(dname);
219 vname = strdup(cpath);
220 vname = basename(vname);
223 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
224 strchr(vname, '/') ){
225 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
231 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
232 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
237 fd = open_file_or_dir(dname);
240 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
244 printf("Delete subvolume '%s/%s'\n", dname, vname);
245 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
246 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
247 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
253 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
254 dname, vname, strerror(e));
261 static const char * const cmd_subvol_list_usage[] = {
262 "btrfs subvolume list [-pu] [-s 0|1] <path>",
263 "List subvolumes (and snapshots)",
265 "-p print parent ID",
266 "-u print the uuid of subvolumes (and snapshots)",
267 "-s value list snapshots with generation in ascending/descending order",
268 " (1: ascending, 0: descending)",
272 static int cmd_subvol_list(int argc, char **argv)
274 struct btrfs_list_filter_set *filter_set;
275 struct btrfs_list_comparer_set *comparer_set;
281 filter_set = btrfs_list_alloc_filter_set();
282 comparer_set = btrfs_list_alloc_comparer_set();
286 int c = getopt(argc, argv, "ps:u");
292 btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
295 order = atoi(optarg);
296 btrfs_list_setup_filter(&filter_set,
297 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
299 btrfs_list_setup_comparer(&comparer_set,
300 BTRFS_LIST_COMP_OGEN,
302 btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
303 btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
306 btrfs_list_setup_print_column(BTRFS_LIST_UUID);
309 usage(cmd_subvol_list_usage);
313 if (check_argc_exact(argc - optind, 1))
314 usage(cmd_subvol_list_usage);
316 subvol = argv[optind];
318 ret = test_issubvolume(subvol);
320 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
324 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
328 fd = open_file_or_dir(subvol);
330 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
334 ret = btrfs_list_subvols(fd, filter_set, comparer_set);
340 static const char * const cmd_snapshot_usage[] = {
341 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
342 "Create a snapshot of the subvolume",
343 "Create a writable/readonly snapshot of the subvolume <source> with",
344 "the name <name> in the <dest> directory",
346 "-r create a readonly snapshot",
350 static int cmd_snapshot(int argc, char **argv)
353 int res, fd, fddst, len, e, readonly = 0;
356 struct btrfs_ioctl_vol_args_v2 args;
357 struct btrfs_qgroup_inherit *inherit = NULL;
360 memset(&args, 0, sizeof(args));
362 int c = getopt(argc, argv, "c:i:r");
368 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
373 res = qgroup_inherit_add_group(&inherit, optarg);
381 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
386 usage(cmd_snapshot_usage);
390 if (check_argc_exact(argc - optind, 2))
391 usage(cmd_snapshot_usage);
393 subvol = argv[optind];
394 dst = argv[optind + 1];
396 res = test_issubvolume(subvol);
398 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
402 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
406 res = test_isdir(dst);
408 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
413 newname = strdup(subvol);
414 newname = basename(newname);
417 newname = strdup(dst);
418 newname = basename(newname);
419 dstdir = strdup(dst);
420 dstdir = dirname(dstdir);
423 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
424 strchr(newname, '/') ){
425 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
430 len = strlen(newname);
431 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
432 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
437 fddst = open_file_or_dir(dstdir);
439 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
443 fd = open_file_or_dir(subvol);
446 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
451 args.flags |= BTRFS_SUBVOL_RDONLY;
452 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
453 subvol, dstdir, newname);
455 printf("Create a snapshot of '%s' in '%s/%s'\n",
456 subvol, dstdir, newname);
461 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
462 args.size = qgroup_inherit_size(inherit);
463 args.qgroup_inherit = inherit;
465 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
466 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
467 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
474 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
475 subvol, strerror(e));
483 static const char * const cmd_subvol_get_default_usage[] = {
484 "btrfs subvolume get-default <path>",
485 "Get the default subvolume of a filesystem",
489 static int cmd_subvol_get_default(int argc, char **argv)
494 struct btrfs_list_filter_set *filter_set;
497 if (check_argc_exact(argc, 2))
498 usage(cmd_subvol_get_default_usage);
502 ret = test_issubvolume(subvol);
504 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
508 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
512 fd = open_file_or_dir(subvol);
514 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
518 ret = btrfs_list_get_default_subvolume(fd, &default_id);
520 fprintf(stderr, "ERROR: can't perform the search - %s\n",
525 if (default_id == 0) {
526 fprintf(stderr, "ERROR: 'default' dir item not found\n");
530 /* no need to resolve roots if FS_TREE is default */
531 if (default_id == BTRFS_FS_TREE_OBJECTID) {
532 printf("ID 5 (FS_TREE)\n");
536 filter_set = btrfs_list_alloc_filter_set();
537 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
540 ret = btrfs_list_subvols(fd, filter_set, NULL);
546 static const char * const cmd_subvol_set_default_usage[] = {
547 "btrfs subvolume set-default <subvolid> <path>",
548 "Set the default subvolume of a filesystem",
552 static int cmd_subvol_set_default(int argc, char **argv)
559 if (check_argc_exact(argc, 3))
560 usage(cmd_subvol_set_default_usage);
565 fd = open_file_or_dir(path);
567 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
571 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
572 if (errno == ERANGE) {
573 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
576 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
580 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
587 static const char * const cmd_find_new_usage[] = {
588 "btrfs subvolume find-new <path> <lastgen>",
589 "List the recently modified files in a filesystem",
593 static int cmd_find_new(int argc, char **argv)
600 if (check_argc_exact(argc, 3))
601 usage(cmd_find_new_usage);
604 last_gen = atoll(argv[2]);
606 ret = test_issubvolume(subvol);
608 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
612 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
616 fd = open_file_or_dir(subvol);
618 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
621 ret = btrfs_list_find_updated_files(fd, 0, last_gen);
627 const struct cmd_group subvolume_cmd_group = {
628 subvolume_cmd_group_usage, NULL, {
629 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
630 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
631 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
632 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
633 { "get-default", cmd_subvol_get_default,
634 cmd_subvol_get_default_usage, NULL, 0 },
635 { "set-default", cmd_subvol_set_default,
636 cmd_subvol_set_default_usage, NULL, 0 },
637 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
642 int cmd_subvolume(int argc, char **argv)
644 return handle_command_group(&subvolume_cmd_group, argc, argv);