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 int list_subvols(int fd, int print_parent, int get_default);
34 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
36 static const char subvolume_cmd_group_usage[] =
37 "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;
71 struct btrfs_ioctl_vol_args args;
74 if (check_argc_exact(argc, 2))
75 usage(cmd_subvol_create_usage);
79 res = test_isdir(dst);
81 fprintf(stderr, "ERROR: '%s' exists\n", dst);
85 newname = strdup(dst);
86 newname = basename(newname);
88 dstdir = dirname(dstdir);
90 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
91 strchr(newname, '/') ){
92 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
97 len = strlen(newname);
98 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
99 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
104 fddst = open_file_or_dir(dstdir);
106 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
110 printf("Create subvolume '%s/%s'\n", dstdir, newname);
111 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
112 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
118 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
127 * test if path is a subvolume:
128 * this function return
129 * 0-> path exists but it is not a subvolume
130 * 1-> path exists and it is a subvolume
131 * -1 -> path is unaccessible
133 static int test_issubvolume(char *path)
138 res = stat(path, &st);
142 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
145 static const char * const cmd_subvol_delete_usage[] = {
146 "btrfs subvolume delete <name>",
147 "Delete a subvolume",
151 static int cmd_subvol_delete(int argc, char **argv)
154 struct btrfs_ioctl_vol_args args;
155 char *dname, *vname, *cpath;
158 if (check_argc_exact(argc, 2))
159 usage(cmd_subvol_delete_usage);
163 res = test_issubvolume(path);
165 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
169 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
173 cpath = realpath(path, 0);
174 dname = strdup(cpath);
175 dname = dirname(dname);
176 vname = strdup(cpath);
177 vname = basename(vname);
180 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
181 strchr(vname, '/') ){
182 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
188 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
189 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
194 fd = open_file_or_dir(dname);
197 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
201 printf("Delete subvolume '%s/%s'\n", dname, vname);
202 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
203 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
209 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
210 dname, vname, strerror(e));
217 static const char * const cmd_subvol_list_usage[] = {
218 "btrfs subvolume list [-p] <path>",
219 "List subvolumes (and snapshots)",
221 "-p print parent ID",
225 static int cmd_subvol_list(int argc, char **argv)
229 int print_parent = 0;
234 int c = getopt(argc, argv, "p");
243 usage(cmd_subvol_list_usage);
247 if (check_argc_exact(argc - optind, 1))
248 usage(cmd_subvol_list_usage);
250 subvol = argv[optind];
252 ret = test_issubvolume(subvol);
254 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
258 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
262 fd = open_file_or_dir(subvol);
264 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
267 ret = list_subvols(fd, print_parent, 0);
273 static const char * const cmd_snapshot_usage[] = {
274 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
275 "Create a snapshot of the subvolume",
276 "Create a writable/readonly snapshot of the subvolume <source> with",
277 "the name <name> in the <dest> directory",
279 "-r create a readonly snapshot",
283 static int cmd_snapshot(int argc, char **argv)
286 int res, fd, fddst, len, e, readonly = 0;
289 struct btrfs_ioctl_vol_args_v2 args;
291 memset(&args, 0, sizeof(args));
295 int c = getopt(argc, argv, "r");
304 usage(cmd_snapshot_usage);
308 if (check_argc_exact(argc - optind, 2))
309 usage(cmd_snapshot_usage);
311 subvol = argv[optind];
312 dst = argv[optind + 1];
314 res = test_issubvolume(subvol);
316 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
320 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
324 res = test_isdir(dst);
326 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
331 newname = strdup(subvol);
332 newname = basename(newname);
335 newname = strdup(dst);
336 newname = basename(newname);
337 dstdir = strdup(dst);
338 dstdir = dirname(dstdir);
341 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
342 strchr(newname, '/') ){
343 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
348 len = strlen(newname);
349 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
350 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
355 fddst = open_file_or_dir(dstdir);
357 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
361 fd = open_file_or_dir(subvol);
364 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
369 args.flags |= BTRFS_SUBVOL_RDONLY;
370 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
371 subvol, dstdir, newname);
373 printf("Create a snapshot of '%s' in '%s/%s'\n",
374 subvol, dstdir, newname);
378 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
379 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
386 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
387 subvol, strerror(e));
394 static const char * const cmd_subvol_get_default_usage[] = {
395 "btrfs subvolume get-dafault <path>",
396 "Get the default subvolume of a filesystem",
400 static int cmd_subvol_get_default(int argc, char **argv)
406 if (check_argc_exact(argc, 2))
407 usage(cmd_subvol_get_default_usage);
411 ret = test_issubvolume(subvol);
413 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
417 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
421 fd = open_file_or_dir(subvol);
423 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
426 ret = list_subvols(fd, 0, 1);
432 static const char * const cmd_subvol_set_default_usage[] = {
433 "btrfs subvolume set-dafault <subvolid> <path>",
434 "Set the default subvolume of a filesystem",
438 static int cmd_subvol_set_default(int argc, char **argv)
445 if (check_argc_exact(argc, 3))
446 usage(cmd_subvol_set_default_usage);
451 fd = open_file_or_dir(path);
453 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
457 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
458 if (errno == ERANGE) {
459 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
462 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
466 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
473 static const char * const cmd_find_new_usage[] = {
474 "btrfs subvolume find-new <path> <lastgen>",
475 "List the recently modified files in a filesystem",
479 static int cmd_find_new(int argc, char **argv)
486 if (check_argc_exact(argc, 3))
487 usage(cmd_find_new_usage);
490 last_gen = atoll(argv[2]);
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 = find_updated_files(fd, 0, last_gen);
513 const struct cmd_group subvolume_cmd_group = {
514 subvolume_cmd_group_usage, NULL, {
515 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
516 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
517 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
518 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
519 { "get-default", cmd_subvol_get_default,
520 cmd_subvol_get_default_usage, NULL, 0 },
521 { "set-default", cmd_subvol_set_default,
522 cmd_subvol_set_default_usage, NULL, 0 },
523 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
528 int cmd_subvolume(int argc, char **argv)
530 return handle_command_group(&subvolume_cmd_group, argc, argv);