Btrfs-progs: only enforce a maximum size if we specify one
[btrfs-progs-unstable/devel.git] / cmds-subvolume.c
blobf4aa80ff42c3803750bdf5a8bdfd7ab7c2275968
1 /*
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <libgen.h>
25 #include <limits.h>
27 #include "kerncompat.h"
28 #include "ioctl.h"
29 #include "qgroup.h"
31 #include "commands.h"
33 /* btrfs-list.c */
34 int list_subvols(int fd, int print_parent, 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>",
39 NULL
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)
51 struct stat st;
52 int res;
54 res = stat(path, &st);
55 if(res < 0 )
56 return -1;
58 return S_ISDIR(st.st_mode);
61 static const char * const cmd_subvol_create_usage[] = {
62 "btrfs subvolume create [<dest>/]<name>",
63 "Create a subvolume",
64 "Create a subvolume <name> in <dest>. If <dest> is not given",
65 "subvolume <name> will be created in the current directory.",
66 NULL
69 static int cmd_subvol_create(int argc, char **argv)
71 int res, fddst, len, e;
72 char *newname;
73 char *dstdir;
74 char *dst;
75 struct btrfs_qgroup_inherit *inherit = NULL;
77 optind = 1;
78 while (1) {
79 int c = getopt(argc, argv, "c:i:r");
80 if (c < 0)
81 break;
83 switch (c) {
84 case 'c':
85 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
86 if (res)
87 return res;
88 break;
89 case 'i':
90 res = qgroup_inherit_add_group(&inherit, optarg);
91 if (res)
92 return res;
93 break;
94 default:
95 usage(cmd_subvol_create_usage);
99 if (check_argc_exact(argc - optind, 1))
100 usage(cmd_subvol_create_usage);
102 dst = argv[optind];
104 res = test_isdir(dst);
105 if(res >= 0 ){
106 fprintf(stderr, "ERROR: '%s' exists\n", dst);
107 return 12;
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",
118 newname);
119 return 14;
122 len = strlen(newname);
123 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
124 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
125 newname);
126 return 14;
129 fddst = open_file_or_dir(dstdir);
130 if (fddst < 0) {
131 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
132 return 12;
135 printf("Create subvolume '%s/%s'\n", dstdir, newname);
136 if (inherit) {
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);
147 } else {
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);
157 e = errno;
159 close(fddst);
161 if(res < 0 ){
162 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
163 strerror(e));
164 return 11;
166 free(inherit);
168 return 0;
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)
180 struct stat st;
181 int res;
183 res = stat(path, &st);
184 if(res < 0 )
185 return -1;
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",
193 NULL
196 static int cmd_subvol_delete(int argc, char **argv)
198 int res, fd, len, e;
199 struct btrfs_ioctl_vol_args args;
200 char *dname, *vname, *cpath;
201 char *path;
203 if (check_argc_exact(argc, 2))
204 usage(cmd_subvol_delete_usage);
206 path = argv[1];
208 res = test_issubvolume(path);
209 if(res<0){
210 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
211 return 12;
213 if(!res){
214 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
215 return 13;
218 cpath = realpath(path, 0);
219 dname = strdup(cpath);
220 dname = dirname(dname);
221 vname = strdup(cpath);
222 vname = basename(vname);
223 free(cpath);
225 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
226 strchr(vname, '/') ){
227 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
228 vname);
229 return 14;
232 len = strlen(vname);
233 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
234 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
235 vname);
236 return 14;
239 fd = open_file_or_dir(dname);
240 if (fd < 0) {
241 close(fd);
242 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
243 return 12;
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);
250 e = errno;
252 close(fd);
254 if(res < 0 ){
255 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
256 dname, vname, strerror(e));
257 return 11;
260 return 0;
263 static const char * const cmd_subvol_list_usage[] = {
264 "btrfs subvolume list [-p] <path>",
265 "List subvolumes (and snapshots)",
267 "-p print parent ID",
268 NULL
271 static int cmd_subvol_list(int argc, char **argv)
273 int fd;
274 int ret;
275 int print_parent = 0;
276 char *subvol;
278 optind = 1;
279 while(1) {
280 int c = getopt(argc, argv, "p");
281 if (c < 0)
282 break;
284 switch(c) {
285 case 'p':
286 print_parent = 1;
287 break;
288 default:
289 usage(cmd_subvol_list_usage);
293 if (check_argc_exact(argc - optind, 1))
294 usage(cmd_subvol_list_usage);
296 subvol = argv[optind];
298 ret = test_issubvolume(subvol);
299 if (ret < 0) {
300 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
301 return 12;
303 if (!ret) {
304 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
305 return 13;
308 fd = open_file_or_dir(subvol);
309 if (fd < 0) {
310 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
311 return 12;
313 ret = list_subvols(fd, print_parent, 0);
314 if (ret)
315 return 19;
316 return 0;
319 static const char * const cmd_snapshot_usage[] = {
320 "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
321 "Create a snapshot of the subvolume",
322 "Create a writable/readonly snapshot of the subvolume <source> with",
323 "the name <name> in the <dest> directory",
325 "-r create a readonly snapshot",
326 NULL
329 static int cmd_snapshot(int argc, char **argv)
331 char *subvol, *dst;
332 int res, fd, fddst, len, e, readonly = 0;
333 char *newname;
334 char *dstdir;
335 struct btrfs_ioctl_vol_args_v2 args;
336 struct btrfs_qgroup_inherit *inherit = NULL;
338 optind = 1;
339 memset(&args, 0, sizeof(args));
340 while (1) {
341 int c = getopt(argc, argv, "c:i:r");
342 if (c < 0)
343 break;
345 switch (c) {
346 case 'c':
347 res = qgroup_inherit_add_copy(&inherit, optarg, 0);
348 if (res)
349 return res;
350 break;
351 case 'i':
352 res = qgroup_inherit_add_group(&inherit, optarg);
353 if (res)
354 return res;
355 break;
356 case 'r':
357 readonly = 1;
358 break;
359 case 'x':
360 res = qgroup_inherit_add_copy(&inherit, optarg, 1);
361 if (res)
362 return res;
363 break;
364 default:
365 usage(cmd_snapshot_usage);
369 if (check_argc_exact(argc - optind, 2))
370 usage(cmd_snapshot_usage);
372 subvol = argv[optind];
373 dst = argv[optind + 1];
375 res = test_issubvolume(subvol);
376 if(res<0){
377 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
378 return 12;
380 if(!res){
381 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
382 return 13;
385 res = test_isdir(dst);
386 if(res == 0 ){
387 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
388 return 12;
391 if(res>0){
392 newname = strdup(subvol);
393 newname = basename(newname);
394 dstdir = dst;
395 }else{
396 newname = strdup(dst);
397 newname = basename(newname);
398 dstdir = strdup(dst);
399 dstdir = dirname(dstdir);
402 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
403 strchr(newname, '/') ){
404 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
405 newname);
406 return 14;
409 len = strlen(newname);
410 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
411 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
412 newname);
413 return 14;
416 fddst = open_file_or_dir(dstdir);
417 if (fddst < 0) {
418 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
419 return 12;
422 fd = open_file_or_dir(subvol);
423 if (fd < 0) {
424 close(fddst);
425 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
426 return 12;
429 if (readonly) {
430 args.flags |= BTRFS_SUBVOL_RDONLY;
431 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
432 subvol, dstdir, newname);
433 } else {
434 printf("Create a snapshot of '%s' in '%s/%s'\n",
435 subvol, dstdir, newname);
438 args.fd = fd;
439 if (inherit) {
440 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
441 args.size = qgroup_inherit_size(inherit);
442 args.qgroup_inherit = inherit;
444 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
445 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
446 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
447 e = errno;
449 close(fd);
450 close(fddst);
452 if(res < 0 ){
453 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
454 subvol, strerror(e));
455 return 11;
457 free(inherit);
459 return 0;
462 static const char * const cmd_subvol_get_default_usage[] = {
463 "btrfs subvolume get-default <path>",
464 "Get the default subvolume of a filesystem",
465 NULL
468 static int cmd_subvol_get_default(int argc, char **argv)
470 int fd;
471 int ret;
472 char *subvol;
474 if (check_argc_exact(argc, 2))
475 usage(cmd_subvol_get_default_usage);
477 subvol = argv[1];
479 ret = test_issubvolume(subvol);
480 if (ret < 0) {
481 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
482 return 12;
484 if (!ret) {
485 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
486 return 13;
489 fd = open_file_or_dir(subvol);
490 if (fd < 0) {
491 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
492 return 12;
494 ret = list_subvols(fd, 0, 1);
495 if (ret)
496 return 19;
497 return 0;
500 static const char * const cmd_subvol_set_default_usage[] = {
501 "btrfs subvolume set-default <subvolid> <path>",
502 "Set the default subvolume of a filesystem",
503 NULL
506 static int cmd_subvol_set_default(int argc, char **argv)
508 int ret=0, fd, e;
509 u64 objectid;
510 char *path;
511 char *subvolid;
513 if (check_argc_exact(argc, 3))
514 usage(cmd_subvol_set_default_usage);
516 subvolid = argv[1];
517 path = argv[2];
519 fd = open_file_or_dir(path);
520 if (fd < 0) {
521 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
522 return 12;
525 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
526 if (errno == ERANGE) {
527 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
528 return 30;
530 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
531 e = errno;
532 close(fd);
533 if( ret < 0 ){
534 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
535 strerror(e));
536 return 30;
538 return 0;
541 static const char * const cmd_find_new_usage[] = {
542 "btrfs subvolume find-new <path> <lastgen>",
543 "List the recently modified files in a filesystem",
544 NULL
547 static int cmd_find_new(int argc, char **argv)
549 int fd;
550 int ret;
551 char *subvol;
552 u64 last_gen;
554 if (check_argc_exact(argc, 3))
555 usage(cmd_find_new_usage);
557 subvol = argv[1];
558 last_gen = atoll(argv[2]);
560 ret = test_issubvolume(subvol);
561 if (ret < 0) {
562 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
563 return 12;
565 if (!ret) {
566 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
567 return 13;
570 fd = open_file_or_dir(subvol);
571 if (fd < 0) {
572 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
573 return 12;
575 ret = find_updated_files(fd, 0, last_gen);
576 if (ret)
577 return 19;
578 return 0;
581 const struct cmd_group subvolume_cmd_group = {
582 subvolume_cmd_group_usage, NULL, {
583 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
584 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
585 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
586 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
587 { "get-default", cmd_subvol_get_default,
588 cmd_subvol_get_default_usage, NULL, 0 },
589 { "set-default", cmd_subvol_set_default,
590 cmd_subvol_set_default_usage, NULL, 0 },
591 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
592 { 0, 0, 0, 0, 0 }
596 int cmd_subvolume(int argc, char **argv)
598 return handle_command_group(&subvolume_cmd_group, argc, argv);