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"
34 int list_subvols(int fd, int print_parent, int print_uuid, int get_default);
35 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
37 static const char * const subvolume_cmd_group_usage[] = {
38 "btrfs subvolume <command> <args>",
43 * test if path is a directory
44 * this function return
45 * 0-> path exists but it is not a directory
46 * 1-> path exists and it is a directory
47 * -1 -> path is unaccessible
49 static int test_isdir(char *path)
54 res = stat(path, &st);
58 return S_ISDIR(st.st_mode);
61 static const char * const cmd_subvol_create_usage[] = {
62 "btrfs subvolume create [<dest>/]<name>",
64 "Create a subvolume <name> in <dest>. If <dest> is not given",
65 "subvolume <name> will be created in the current directory.",
69 static int cmd_subvol_create(int argc, char **argv)
71 int res, fddst, len, e;
75 struct btrfs_qgroup_inherit *inherit = NULL;
79 int c = getopt(argc, argv, "c:i:r");
85 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
90 res = qgroup_inherit_add_group(&inherit, optarg);
95 usage(cmd_subvol_create_usage);
99 if (check_argc_exact(argc - optind, 1))
100 usage(cmd_subvol_create_usage);
104 res = test_isdir(dst);
106 fprintf(stderr, "ERROR: '%s' exists\n", dst);
110 newname = strdup(dst);
111 newname = basename(newname);
112 dstdir = strdup(dst);
113 dstdir = dirname(dstdir);
115 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
116 strchr(newname, '/') ){
117 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
122 len = strlen(newname);
123 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
124 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
129 fddst = open_file_or_dir(dstdir);
131 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
135 printf("Create subvolume '%s/%s'\n", dstdir, newname);
137 struct btrfs_ioctl_vol_args_v2 args;
139 memset(&args, 0, sizeof(args));
140 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
141 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
142 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
143 args.size = qgroup_inherit_size(inherit);
144 args.qgroup_inherit = inherit;
146 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
148 struct btrfs_ioctl_vol_args args;
150 memset(&args, 0, sizeof(args));
151 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
152 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
154 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
162 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 <name>",
192 "Delete a subvolume",
196 static int cmd_subvol_delete(int argc, char **argv)
199 struct btrfs_ioctl_vol_args args;
200 char *dname, *vname, *cpath;
203 if (check_argc_exact(argc, 2))
204 usage(cmd_subvol_delete_usage);
208 res = test_issubvolume(path);
210 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
214 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
218 cpath = realpath(path, 0);
219 dname = strdup(cpath);
220 dname = dirname(dname);
221 vname = strdup(cpath);
222 vname = basename(vname);
225 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
226 strchr(vname, '/') ){
227 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
233 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
234 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
239 fd = open_file_or_dir(dname);
242 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
246 printf("Delete subvolume '%s/%s'\n", dname, vname);
247 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
248 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
249 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
255 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
256 dname, vname, strerror(e));
263 static const char * const cmd_subvol_list_usage[] = {
264 "btrfs subvolume list [-ps] <path>",
265 "List subvolumes (and snapshots)",
267 "-p print parent ID",
268 "-s value list snapshots with generation in ascending/descending order",
269 " (1: ascending, 0: descending)",
273 static int cmd_subvol_list(int argc, char **argv)
277 int print_parent = 0;
278 int print_snap_only = 0;
285 int c = getopt(argc, argv, "ps:u");
295 order = atoi(optarg);
301 usage(cmd_subvol_list_usage);
305 if (check_argc_exact(argc - optind, 1))
306 usage(cmd_subvol_list_usage);
308 subvol = argv[optind];
310 ret = test_issubvolume(subvol);
312 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
316 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
320 fd = open_file_or_dir(subvol);
322 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
325 if (!print_snap_only)
326 ret = list_subvols(fd, print_parent, 0, print_uuid);
328 ret = list_snapshots(fd, print_parent, order, print_uuid);
334 static const char * const cmd_snapshot_usage[] = {
335 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
336 "Create a snapshot of the subvolume",
337 "Create a writable/readonly snapshot of the subvolume <source> with",
338 "the name <name> in the <dest> directory",
340 "-r create a readonly snapshot",
344 static int cmd_snapshot(int argc, char **argv)
347 int res, fd, fddst, len, e, readonly = 0;
350 struct btrfs_ioctl_vol_args_v2 args;
351 struct btrfs_qgroup_inherit *inherit = NULL;
354 memset(&args, 0, sizeof(args));
356 int c = getopt(argc, argv, "c:i:r");
362 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
367 res = qgroup_inherit_add_group(&inherit, optarg);
375 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
380 usage(cmd_snapshot_usage);
384 if (check_argc_exact(argc - optind, 2))
385 usage(cmd_snapshot_usage);
387 subvol = argv[optind];
388 dst = argv[optind + 1];
390 res = test_issubvolume(subvol);
392 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
396 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
400 res = test_isdir(dst);
402 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
407 newname = strdup(subvol);
408 newname = basename(newname);
411 newname = strdup(dst);
412 newname = basename(newname);
413 dstdir = strdup(dst);
414 dstdir = dirname(dstdir);
417 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
418 strchr(newname, '/') ){
419 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
424 len = strlen(newname);
425 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
426 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
431 fddst = open_file_or_dir(dstdir);
433 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
437 fd = open_file_or_dir(subvol);
440 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
445 args.flags |= BTRFS_SUBVOL_RDONLY;
446 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
447 subvol, dstdir, newname);
449 printf("Create a snapshot of '%s' in '%s/%s'\n",
450 subvol, dstdir, newname);
455 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
456 args.size = qgroup_inherit_size(inherit);
457 args.qgroup_inherit = inherit;
459 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
460 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
461 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
468 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
469 subvol, strerror(e));
477 static const char * const cmd_subvol_get_default_usage[] = {
478 "btrfs subvolume get-default <path>",
479 "Get the default subvolume of a filesystem",
483 static int cmd_subvol_get_default(int argc, char **argv)
489 if (check_argc_exact(argc, 2))
490 usage(cmd_subvol_get_default_usage);
494 ret = test_issubvolume(subvol);
496 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
500 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
504 fd = open_file_or_dir(subvol);
506 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
509 ret = list_subvols(fd, 0, 1, 0);
515 static const char * const cmd_subvol_set_default_usage[] = {
516 "btrfs subvolume set-default <subvolid> <path>",
517 "Set the default subvolume of a filesystem",
521 static int cmd_subvol_set_default(int argc, char **argv)
528 if (check_argc_exact(argc, 3))
529 usage(cmd_subvol_set_default_usage);
534 fd = open_file_or_dir(path);
536 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
540 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
541 if (errno == ERANGE) {
542 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
545 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
549 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
556 static const char * const cmd_find_new_usage[] = {
557 "btrfs subvolume find-new <path> <lastgen>",
558 "List the recently modified files in a filesystem",
562 static int cmd_find_new(int argc, char **argv)
569 if (check_argc_exact(argc, 3))
570 usage(cmd_find_new_usage);
573 last_gen = atoll(argv[2]);
575 ret = test_issubvolume(subvol);
577 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
581 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
585 fd = open_file_or_dir(subvol);
587 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
590 ret = find_updated_files(fd, 0, last_gen);
596 const struct cmd_group subvolume_cmd_group = {
597 subvolume_cmd_group_usage, NULL, {
598 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
599 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
600 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
601 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
602 { "get-default", cmd_subvol_get_default,
603 cmd_subvol_get_default_usage, NULL, 0 },
604 { "set-default", cmd_subvol_set_default,
605 cmd_subvol_set_default_usage, NULL, 0 },
606 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
611 int cmd_subvolume(int argc, char **argv)
613 return handle_command_group(&subvolume_cmd_group, argc, argv);