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 [-pu] [-s 0|1] <path>",
262 "List subvolumes (and snapshots)",
264 "-p print parent ID",
265 "-u print the uuid of subvolumes (and snapshots)",
266 "-s value list snapshots with generation in ascending/descending order",
267 " (1: ascending, 0: descending)",
271 static int cmd_subvol_list(int argc, char **argv)
275 int print_parent = 0;
276 int print_snap_only = 0;
283 int c = getopt(argc, argv, "ps:u");
293 order = atoi(optarg);
299 usage(cmd_subvol_list_usage);
303 if (check_argc_exact(argc - optind, 1))
304 usage(cmd_subvol_list_usage);
306 subvol = argv[optind];
308 ret = test_issubvolume(subvol);
310 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
314 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
318 fd = open_file_or_dir(subvol);
320 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
323 if (!print_snap_only)
324 ret = list_subvols(fd, print_parent, 0, print_uuid);
326 ret = list_snapshots(fd, print_parent, order, print_uuid);
332 static const char * const cmd_snapshot_usage[] = {
333 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
334 "Create a snapshot of the subvolume",
335 "Create a writable/readonly snapshot of the subvolume <source> with",
336 "the name <name> in the <dest> directory",
338 "-r create a readonly snapshot",
342 static int cmd_snapshot(int argc, char **argv)
345 int res, fd, fddst, len, e, readonly = 0;
348 struct btrfs_ioctl_vol_args_v2 args;
349 struct btrfs_qgroup_inherit *inherit = NULL;
352 memset(&args, 0, sizeof(args));
354 int c = getopt(argc, argv, "c:i:r");
360 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
365 res = qgroup_inherit_add_group(&inherit, optarg);
373 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
378 usage(cmd_snapshot_usage);
382 if (check_argc_exact(argc - optind, 2))
383 usage(cmd_snapshot_usage);
385 subvol = argv[optind];
386 dst = argv[optind + 1];
388 res = test_issubvolume(subvol);
390 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
394 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
398 res = test_isdir(dst);
400 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
405 newname = strdup(subvol);
406 newname = basename(newname);
409 newname = strdup(dst);
410 newname = basename(newname);
411 dstdir = strdup(dst);
412 dstdir = dirname(dstdir);
415 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
416 strchr(newname, '/') ){
417 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
422 len = strlen(newname);
423 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
424 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
429 fddst = open_file_or_dir(dstdir);
431 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
435 fd = open_file_or_dir(subvol);
438 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
443 args.flags |= BTRFS_SUBVOL_RDONLY;
444 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
445 subvol, dstdir, newname);
447 printf("Create a snapshot of '%s' in '%s/%s'\n",
448 subvol, dstdir, newname);
453 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
454 args.size = qgroup_inherit_size(inherit);
455 args.qgroup_inherit = inherit;
457 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
458 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
459 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
466 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
467 subvol, strerror(e));
475 static const char * const cmd_subvol_get_default_usage[] = {
476 "btrfs subvolume get-default <path>",
477 "Get the default subvolume of a filesystem",
481 static int cmd_subvol_get_default(int argc, char **argv)
487 if (check_argc_exact(argc, 2))
488 usage(cmd_subvol_get_default_usage);
492 ret = test_issubvolume(subvol);
494 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
498 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
502 fd = open_file_or_dir(subvol);
504 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
507 ret = list_subvols(fd, 0, 1, 0);
513 static const char * const cmd_subvol_set_default_usage[] = {
514 "btrfs subvolume set-default <subvolid> <path>",
515 "Set the default subvolume of a filesystem",
519 static int cmd_subvol_set_default(int argc, char **argv)
526 if (check_argc_exact(argc, 3))
527 usage(cmd_subvol_set_default_usage);
532 fd = open_file_or_dir(path);
534 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
538 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
539 if (errno == ERANGE) {
540 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
543 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
547 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
554 static const char * const cmd_find_new_usage[] = {
555 "btrfs subvolume find-new <path> <lastgen>",
556 "List the recently modified files in a filesystem",
560 static int cmd_find_new(int argc, char **argv)
567 if (check_argc_exact(argc, 3))
568 usage(cmd_find_new_usage);
571 last_gen = atoll(argv[2]);
573 ret = test_issubvolume(subvol);
575 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
579 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
583 fd = open_file_or_dir(subvol);
585 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
588 ret = find_updated_files(fd, 0, last_gen);
594 const struct cmd_group subvolume_cmd_group = {
595 subvolume_cmd_group_usage, NULL, {
596 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
597 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
598 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
599 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
600 { "get-default", cmd_subvol_get_default,
601 cmd_subvol_get_default_usage, NULL, 0 },
602 { "set-default", cmd_subvol_set_default,
603 cmd_subvol_set_default_usage, NULL, 0 },
604 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
609 int cmd_subvolume(int argc, char **argv)
611 return handle_command_group(&subvolume_cmd_group, argc, argv);