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"
32 #include "btrfs-list.h"
34 static const char * const subvolume_cmd_group_usage[] = {
35 "btrfs subvolume <command> <args>",
40 * test if path is a directory
41 * this function return
42 * 0-> path exists but it is not a directory
43 * 1-> path exists and it is a directory
44 * -1 -> path is unaccessible
46 static int test_isdir(char *path)
51 res = stat(path, &st);
55 return S_ISDIR(st.st_mode);
58 static const char * const cmd_subvol_create_usage[] = {
59 "btrfs subvolume create [<dest>/]<name>",
61 "Create a subvolume <name> in <dest>. If <dest> is not given",
62 "subvolume <name> will be created in the current directory.",
66 static int cmd_subvol_create(int argc, char **argv)
68 int res, fddst, len, e;
72 struct btrfs_qgroup_inherit *inherit = NULL;
76 int c = getopt(argc, argv, "c:i:r");
82 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
87 res = qgroup_inherit_add_group(&inherit, optarg);
92 usage(cmd_subvol_create_usage);
96 if (check_argc_exact(argc - optind, 1))
97 usage(cmd_subvol_create_usage);
101 res = test_isdir(dst);
103 fprintf(stderr, "ERROR: '%s' exists\n", dst);
107 newname = strdup(dst);
108 newname = basename(newname);
109 dstdir = strdup(dst);
110 dstdir = dirname(dstdir);
112 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
113 strchr(newname, '/') ){
114 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
119 len = strlen(newname);
120 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
121 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
126 fddst = open_file_or_dir(dstdir);
128 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
132 printf("Create subvolume '%s/%s'\n", dstdir, newname);
134 struct btrfs_ioctl_vol_args_v2 args;
136 memset(&args, 0, sizeof(args));
137 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
138 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
139 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
140 args.size = qgroup_inherit_size(inherit);
141 args.qgroup_inherit = inherit;
143 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
145 struct btrfs_ioctl_vol_args args;
147 memset(&args, 0, sizeof(args));
148 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
149 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
151 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
159 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
169 * test if path is a subvolume:
170 * this function return
171 * 0-> path exists but it is not a subvolume
172 * 1-> path exists and it is a subvolume
173 * -1 -> path is unaccessible
175 int test_issubvolume(char *path)
180 res = stat(path, &st);
184 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
187 static const char * const cmd_subvol_delete_usage[] = {
188 "btrfs subvolume delete <name>",
189 "Delete a subvolume",
193 static int cmd_subvol_delete(int argc, char **argv)
196 struct btrfs_ioctl_vol_args args;
197 char *dname, *vname, *cpath;
200 if (check_argc_exact(argc, 2))
201 usage(cmd_subvol_delete_usage);
205 res = test_issubvolume(path);
207 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
211 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
215 cpath = realpath(path, 0);
216 dname = strdup(cpath);
217 dname = dirname(dname);
218 vname = strdup(cpath);
219 vname = basename(vname);
222 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
223 strchr(vname, '/') ){
224 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
230 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
231 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
236 fd = open_file_or_dir(dname);
239 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
243 printf("Delete subvolume '%s/%s'\n", dname, vname);
244 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
245 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
246 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
252 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
253 dname, vname, strerror(e));
260 static const char * const cmd_subvol_list_usage[] = {
261 "btrfs subvolume list [-ps] <path>",
262 "List subvolumes (and snapshots)",
264 "-p print parent ID",
265 "-s value list snapshots with generation in ascending/descending order",
266 " (1: ascending, 0: descending)",
270 static int cmd_subvol_list(int argc, char **argv)
274 int print_parent = 0;
275 int print_snap_only = 0;
282 int c = getopt(argc, argv, "ps:u");
292 order = atoi(optarg);
298 usage(cmd_subvol_list_usage);
302 if (check_argc_exact(argc - optind, 1))
303 usage(cmd_subvol_list_usage);
305 subvol = argv[optind];
307 ret = test_issubvolume(subvol);
309 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
313 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
317 fd = open_file_or_dir(subvol);
319 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
322 if (!print_snap_only)
323 ret = list_subvols(fd, print_parent, 0, print_uuid);
325 ret = list_snapshots(fd, print_parent, order, print_uuid);
331 static const char * const cmd_snapshot_usage[] = {
332 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
333 "Create a snapshot of the subvolume",
334 "Create a writable/readonly snapshot of the subvolume <source> with",
335 "the name <name> in the <dest> directory",
337 "-r create a readonly snapshot",
341 static int cmd_snapshot(int argc, char **argv)
344 int res, fd, fddst, len, e, readonly = 0;
347 struct btrfs_ioctl_vol_args_v2 args;
348 struct btrfs_qgroup_inherit *inherit = NULL;
351 memset(&args, 0, sizeof(args));
353 int c = getopt(argc, argv, "c:i:r");
359 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
364 res = qgroup_inherit_add_group(&inherit, optarg);
372 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
377 usage(cmd_snapshot_usage);
381 if (check_argc_exact(argc - optind, 2))
382 usage(cmd_snapshot_usage);
384 subvol = argv[optind];
385 dst = argv[optind + 1];
387 res = test_issubvolume(subvol);
389 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
393 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
397 res = test_isdir(dst);
399 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
404 newname = strdup(subvol);
405 newname = basename(newname);
408 newname = strdup(dst);
409 newname = basename(newname);
410 dstdir = strdup(dst);
411 dstdir = dirname(dstdir);
414 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
415 strchr(newname, '/') ){
416 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
421 len = strlen(newname);
422 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
423 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
428 fddst = open_file_or_dir(dstdir);
430 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
434 fd = open_file_or_dir(subvol);
437 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
442 args.flags |= BTRFS_SUBVOL_RDONLY;
443 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
444 subvol, dstdir, newname);
446 printf("Create a snapshot of '%s' in '%s/%s'\n",
447 subvol, dstdir, newname);
452 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
453 args.size = qgroup_inherit_size(inherit);
454 args.qgroup_inherit = inherit;
456 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
457 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
458 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
465 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
466 subvol, strerror(e));
474 static const char * const cmd_subvol_get_default_usage[] = {
475 "btrfs subvolume get-default <path>",
476 "Get the default subvolume of a filesystem",
480 static int cmd_subvol_get_default(int argc, char **argv)
486 if (check_argc_exact(argc, 2))
487 usage(cmd_subvol_get_default_usage);
491 ret = test_issubvolume(subvol);
493 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
497 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
501 fd = open_file_or_dir(subvol);
503 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
506 ret = list_subvols(fd, 0, 1, 0);
512 static const char * const cmd_subvol_set_default_usage[] = {
513 "btrfs subvolume set-default <subvolid> <path>",
514 "Set the default subvolume of a filesystem",
518 static int cmd_subvol_set_default(int argc, char **argv)
525 if (check_argc_exact(argc, 3))
526 usage(cmd_subvol_set_default_usage);
531 fd = open_file_or_dir(path);
533 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
537 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
538 if (errno == ERANGE) {
539 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
542 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
546 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
553 static const char * const cmd_find_new_usage[] = {
554 "btrfs subvolume find-new <path> <lastgen>",
555 "List the recently modified files in a filesystem",
559 static int cmd_find_new(int argc, char **argv)
566 if (check_argc_exact(argc, 3))
567 usage(cmd_find_new_usage);
570 last_gen = atoll(argv[2]);
572 ret = test_issubvolume(subvol);
574 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
578 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
582 fd = open_file_or_dir(subvol);
584 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
587 ret = find_updated_files(fd, 0, last_gen);
593 const struct cmd_group subvolume_cmd_group = {
594 subvolume_cmd_group_usage, NULL, {
595 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
596 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
597 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
598 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
599 { "get-default", cmd_subvol_get_default,
600 cmd_subvol_get_default_usage, NULL, 0 },
601 { "set-default", cmd_subvol_set_default,
602 cmd_subvol_set_default_usage, NULL, 0 },
603 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
608 int cmd_subvolume(int argc, char **argv)
610 return handle_command_group(&subvolume_cmd_group, argc, argv);