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"
30 #include "btrfs_cmds.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);
37 * test if path is a directory
38 * this function return
39 * 0-> path exists but it is not a directory
40 * 1-> path exists and it is a directory
41 * -1 -> path is unaccessible
43 static int test_isdir(char *path)
48 res = stat(path, &st);
52 return S_ISDIR(st.st_mode);
55 int do_create_subvol(int argc, char **argv)
57 int res, fddst, len, e;
60 struct btrfs_ioctl_vol_args args;
63 res = test_isdir(dst);
65 fprintf(stderr, "ERROR: '%s' exists\n", dst);
69 newname = strdup(dst);
70 newname = basename(newname);
72 dstdir = dirname(dstdir);
74 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
75 strchr(newname, '/') ){
76 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
81 len = strlen(newname);
82 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
83 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
88 fddst = open_file_or_dir(dstdir);
90 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
94 printf("Create subvolume '%s/%s'\n", dstdir, newname);
95 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
96 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
102 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
112 * test if path is a subvolume:
113 * this function return
114 * 0-> path exists but it is not a subvolume
115 * 1-> path exists and it is a subvolume
116 * -1 -> path is unaccessible
118 static int test_issubvolume(char *path)
123 res = stat(path, &st);
127 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
130 int do_delete_subvolume(int argc, char **argv)
133 struct btrfs_ioctl_vol_args args;
134 char *dname, *vname, *cpath;
135 char *path = argv[1];
137 res = test_issubvolume(path);
139 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
143 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
147 cpath = realpath(path, 0);
148 dname = strdup(cpath);
149 dname = dirname(dname);
150 vname = strdup(cpath);
151 vname = basename(vname);
154 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
155 strchr(vname, '/') ){
156 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
162 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
163 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
168 fd = open_file_or_dir(dname);
171 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
175 printf("Delete subvolume '%s/%s'\n", dname, vname);
176 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
177 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
183 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
184 dname, vname, strerror(e));
192 int do_subvol_list(int argc, char **argv)
196 int print_parent = 0;
201 int c = getopt(argc, argv, "p");
211 if (argc - optind != 1) {
212 fprintf(stderr, "ERROR: invalid arguments for subvolume list\n");
216 subvol = argv[optind];
218 ret = test_issubvolume(subvol);
220 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
224 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
228 fd = open_file_or_dir(subvol);
230 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
233 ret = list_subvols(fd, print_parent, 0);
239 int do_clone(int argc, char **argv)
242 int res, fd, fddst, len, e, optind = 0, readonly = 0;
245 struct btrfs_ioctl_vol_args_v2 args;
247 memset(&args, 0, sizeof(args));
250 int c = getopt(argc, argv, "r");
261 "Invalid arguments for subvolume snapshot\n");
266 if (argc - optind != 3) {
267 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
272 subvol = argv[optind+1];
273 dst = argv[optind+2];
275 res = test_issubvolume(subvol);
277 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
281 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
285 res = test_isdir(dst);
287 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
292 newname = strdup(subvol);
293 newname = basename(newname);
296 newname = strdup(dst);
297 newname = basename(newname);
298 dstdir = strdup(dst);
299 dstdir = dirname(dstdir);
302 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
303 strchr(newname, '/') ){
304 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
309 len = strlen(newname);
310 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
311 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
316 fddst = open_file_or_dir(dstdir);
318 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
322 fd = open_file_or_dir(subvol);
325 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
330 args.flags |= BTRFS_SUBVOL_RDONLY;
331 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
332 subvol, dstdir, newname);
334 printf("Create a snapshot of '%s' in '%s/%s'\n",
335 subvol, dstdir, newname);
339 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
340 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
347 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
348 subvol, strerror(e));
356 int do_get_default_subvol(int nargs, char **argv)
364 ret = test_issubvolume(subvol);
366 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
370 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
374 fd = open_file_or_dir(subvol);
376 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
379 ret = list_subvols(fd, 0, 1);
385 int do_set_default_subvol(int nargs, char **argv)
389 char *path = argv[2];
390 char *subvolid = argv[1];
392 fd = open_file_or_dir(path);
394 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
398 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
399 if (errno == ERANGE) {
400 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
403 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
407 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
414 int do_find_newer(int argc, char **argv)
422 last_gen = atoll(argv[2]);
424 ret = test_issubvolume(subvol);
426 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
430 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
434 fd = open_file_or_dir(subvol);
436 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
439 ret = find_updated_files(fd, 0, last_gen);