d46064af2cc3eca22530cada18246a7041df8c02
[platform/upstream/btrfs-progs.git] / cmds-subvolume.c
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.
5  *
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.
10  *
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.
15  */
16
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>
26 #include <getopt.h>
27
28 #include "kerncompat.h"
29 #include "ioctl.h"
30 #include "qgroup.h"
31
32 #include "ctree.h"
33 #include "commands.h"
34 #include "btrfs-list.h"
35 #include "utils.h"
36
37 static const char * const subvolume_cmd_group_usage[] = {
38         "btrfs subvolume <command> <args>",
39         NULL
40 };
41
42 /*
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
48  */
49 static int test_isdir(char *path)
50 {
51         struct stat     st;
52         int             res;
53
54         res = stat(path, &st);
55         if(res < 0 )
56                 return -1;
57
58         return S_ISDIR(st.st_mode);
59 }
60
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
67 };
68
69 static int cmd_subvol_create(int argc, char **argv)
70 {
71         int     res, fddst, len, e;
72         char    *newname;
73         char    *dstdir;
74         char    *dst;
75         struct btrfs_qgroup_inherit *inherit = NULL;
76
77         optind = 1;
78         while (1) {
79                 int c = getopt(argc, argv, "c:i:r");
80                 if (c < 0)
81                         break;
82
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);
96                 }
97         }
98
99         if (check_argc_exact(argc - optind, 1))
100                 usage(cmd_subvol_create_usage);
101
102         dst = argv[optind];
103
104         res = test_isdir(dst);
105         if(res >= 0 ){
106                 fprintf(stderr, "ERROR: '%s' exists\n", dst);
107                 return 12;
108         }
109
110         newname = strdup(dst);
111         newname = basename(newname);
112         dstdir = strdup(dst);
113         dstdir = dirname(dstdir);
114
115         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
116              strchr(newname, '/') ){
117                 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
118                         newname);
119                 return 14;
120         }
121
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;
127         }
128
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;
133         }
134
135         printf("Create subvolume '%s/%s'\n", dstdir, newname);
136         if (inherit) {
137                 struct btrfs_ioctl_vol_args_v2  args;
138
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;
145
146                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
147         } else {
148                 struct btrfs_ioctl_vol_args     args;
149
150                 memset(&args, 0, sizeof(args));
151                 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
152                 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
153
154                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
155         }
156
157         e = errno;
158
159         close(fddst);
160
161         if(res < 0 ){
162                 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
163                         strerror(e));
164                 return 11;
165         }
166         free(inherit);
167
168         return 0;
169 }
170
171 /*
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
177  */
178 int test_issubvolume(char *path)
179 {
180         struct stat     st;
181         int             res;
182
183         res = stat(path, &st);
184         if(res < 0 )
185                 return -1;
186
187         return (st.st_ino == 256) && S_ISDIR(st.st_mode);
188 }
189
190 static const char * const cmd_subvol_delete_usage[] = {
191         "btrfs subvolume delete <subvolume> [<subvolume>...]",
192         "Delete subvolume(s)",
193         NULL
194 };
195
196 static int cmd_subvol_delete(int argc, char **argv)
197 {
198         int     res, fd, len, e, cnt = 1, ret = 0;
199         struct btrfs_ioctl_vol_args     args;
200         char    *dname, *vname, *cpath;
201         char    *path;
202
203         if (argc < 2)
204                 usage(cmd_subvol_delete_usage);
205
206 again:
207         path = argv[cnt];
208
209         res = test_issubvolume(path);
210         if(res<0){
211                 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
212                 ret = 12;
213                 goto out;
214         }
215         if(!res){
216                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
217                 ret = 13;
218                 goto out;
219         }
220
221         cpath = realpath(path, 0);
222         dname = strdup(cpath);
223         dname = dirname(dname);
224         vname = strdup(cpath);
225         vname = basename(vname);
226         free(cpath);
227
228         if( !strcmp(vname,".") || !strcmp(vname,"..") ||
229              strchr(vname, '/') ){
230                 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
231                         vname);
232                 ret = 14;
233                 goto out;
234         }
235
236         len = strlen(vname);
237         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
238                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
239                         vname);
240                 ret = 14;
241                 goto out;
242         }
243
244         fd = open_file_or_dir(dname);
245         if (fd < 0) {
246                 close(fd);
247                 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
248                 ret = 12;
249                 goto out;
250         }
251
252         printf("Delete subvolume '%s/%s'\n", dname, vname);
253         strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
254         args.name[BTRFS_PATH_NAME_MAX-1] = 0;
255         res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
256         e = errno;
257
258         close(fd);
259
260         if(res < 0 ){
261                 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
262                         dname, vname, strerror(e));
263                 ret = 11;
264                 goto out;
265         }
266
267 out:
268         cnt++;
269         if (cnt < argc)
270                 goto again;
271
272         return ret;
273 }
274
275 static const char * const cmd_subvol_list_usage[] = {
276         "btrfs subvolume list [-aopurts] [-g [+|-]value] [-c [+|-]value] "
277         "[--sort=gen,ogen,rootid,path] <path>",
278         "List subvolumes (and snapshots)",
279         "",
280         "-p           print parent ID",
281         "-a           print all the subvolumes in the filesystem and",
282         "             distinguish absolute and relative path with respect",
283         "             to the given <path>",
284         "-o           print only subvolumes bellow specified path",
285         "-u           print the uuid of subvolumes (and snapshots)",
286         "-q           print the parent uuid of the snapshots",
287         "-t           print the result as a table",
288         "-s           list snapshots only in the filesystem",
289         "-r           list readonly subvolumes (including snapshots)",
290         "-g [+|-]value",
291         "             filter the subvolumes by generation",
292         "             (+value: >= value; -value: <= value; value: = value)",
293         "-c [+|-]value",
294         "             filter the subvolumes by ogeneration",
295         "             (+value: >= value; -value: <= value; value: = value)",
296         "--sort=gen,ogen,rootid,path",
297         "             list the subvolume in order of gen, ogen, rootid or path",
298         "             you also can add '+' or '-' in front of each items.",
299         "             (+:ascending, -:descending, ascending default)",
300         NULL,
301 };
302
303 static int cmd_subvol_list(int argc, char **argv)
304 {
305         struct btrfs_list_filter_set *filter_set;
306         struct btrfs_list_comparer_set *comparer_set;
307         u64 flags = 0;
308         int fd;
309         u64 top_id;
310         int ret;
311         int c;
312         char *subvol;
313         int is_tab_result = 0;
314         int is_list_all = 0;
315         int is_only_in_path = 0;
316         struct option long_options[] = {
317                 {"sort", 1, NULL, 'S'},
318                 {0, 0, 0, 0}
319         };
320
321         filter_set = btrfs_list_alloc_filter_set();
322         comparer_set = btrfs_list_alloc_comparer_set();
323
324         optind = 1;
325         while(1) {
326                 c = getopt_long(argc, argv,
327                                     "aopqsurg:c:t", long_options, NULL);
328                 if (c < 0)
329                         break;
330
331                 switch(c) {
332                 case 'p':
333                         btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
334                         break;
335                 case 'a':
336                         is_list_all = 1;
337                         break;
338                 case 'o':
339                         is_only_in_path = 1;
340                         break;
341                 case 't':
342                         is_tab_result = 1;
343                         break;
344                 case 's':
345                         btrfs_list_setup_filter(&filter_set,
346                                                 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
347                                                 0);
348                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
349                         btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
350
351                 case 'u':
352                         btrfs_list_setup_print_column(BTRFS_LIST_UUID);
353                         break;
354                 case 'q':
355                         btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
356                         break;
357                 case 'r':
358                         flags |= BTRFS_ROOT_SUBVOL_RDONLY;
359                         break;
360                 case 'g':
361                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
362                         ret = btrfs_list_parse_filter_string(optarg,
363                                                         &filter_set,
364                                                         BTRFS_LIST_FILTER_GEN);
365                         if (ret)
366                                 usage(cmd_subvol_list_usage);
367                         break;
368
369                 case 'c':
370                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
371                         ret = btrfs_list_parse_filter_string(optarg,
372                                                         &filter_set,
373                                                         BTRFS_LIST_FILTER_CGEN);
374                         if (ret)
375                                 usage(cmd_subvol_list_usage);
376                         break;
377                 case 'S':
378                         ret = btrfs_list_parse_sort_string(optarg,
379                                                            &comparer_set);
380                         if (ret)
381                                 usage(cmd_subvol_list_usage);
382                         break;
383
384                 default:
385                         usage(cmd_subvol_list_usage);
386                 }
387         }
388
389         if (flags)
390                 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
391                                         flags);
392
393         if (check_argc_exact(argc - optind, 1))
394                 usage(cmd_subvol_list_usage);
395
396         subvol = argv[optind];
397
398         ret = test_issubvolume(subvol);
399         if (ret < 0) {
400                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
401                 return 12;
402         }
403         if (!ret) {
404                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
405                 return 13;
406         }
407
408         fd = open_file_or_dir(subvol);
409         if (fd < 0) {
410                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
411                 return 12;
412         }
413
414         top_id = btrfs_list_get_path_rootid(fd);
415
416         if (is_list_all)
417                 btrfs_list_setup_filter(&filter_set,
418                                         BTRFS_LIST_FILTER_FULL_PATH,
419                                         top_id);
420         else if (is_only_in_path)
421                 btrfs_list_setup_filter(&filter_set,
422                                         BTRFS_LIST_FILTER_TOPID_EQUAL,
423                                         top_id);
424
425         ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
426                                 is_tab_result,
427                                 !is_list_all && !is_only_in_path);
428         if (ret)
429                 return 19;
430         return 0;
431 }
432
433 static const char * const cmd_snapshot_usage[] = {
434         "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
435         "Create a snapshot of the subvolume",
436         "Create a writable/readonly snapshot of the subvolume <source> with",
437         "the name <name> in the <dest> directory",
438         "",
439         "-r     create a readonly snapshot",
440         NULL
441 };
442
443 static int cmd_snapshot(int argc, char **argv)
444 {
445         char    *subvol, *dst;
446         int     res, fd, fddst, len, e, readonly = 0;
447         char    *newname;
448         char    *dstdir;
449         struct btrfs_ioctl_vol_args_v2  args;
450         struct btrfs_qgroup_inherit *inherit = NULL;
451
452         optind = 1;
453         memset(&args, 0, sizeof(args));
454         while (1) {
455                 int c = getopt(argc, argv, "c:i:r");
456                 if (c < 0)
457                         break;
458
459                 switch (c) {
460                 case 'c':
461                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
462                         if (res)
463                                 return res;
464                         break;
465                 case 'i':
466                         res = qgroup_inherit_add_group(&inherit, optarg);
467                         if (res)
468                                 return res;
469                         break;
470                 case 'r':
471                         readonly = 1;
472                         break;
473                 case 'x':
474                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
475                         if (res)
476                                 return res;
477                         break;
478                 default:
479                         usage(cmd_snapshot_usage);
480                 }
481         }
482
483         if (check_argc_exact(argc - optind, 2))
484                 usage(cmd_snapshot_usage);
485
486         subvol = argv[optind];
487         dst = argv[optind + 1];
488
489         res = test_issubvolume(subvol);
490         if(res<0){
491                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
492                 return 12;
493         }
494         if(!res){
495                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
496                 return 13;
497         }
498
499         res = test_isdir(dst);
500         if(res == 0 ){
501                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
502                 return 12;
503         }
504
505         if(res>0){
506                 newname = strdup(subvol);
507                 newname = basename(newname);
508                 dstdir = dst;
509         }else{
510                 newname = strdup(dst);
511                 newname = basename(newname);
512                 dstdir = strdup(dst);
513                 dstdir = dirname(dstdir);
514         }
515
516         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
517              strchr(newname, '/') ){
518                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
519                         newname);
520                 return 14;
521         }
522
523         len = strlen(newname);
524         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
525                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
526                         newname);
527                 return 14;
528         }
529
530         fddst = open_file_or_dir(dstdir);
531         if (fddst < 0) {
532                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
533                 return 12;
534         }
535
536         fd = open_file_or_dir(subvol);
537         if (fd < 0) {
538                 close(fddst);
539                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
540                 return 12;
541         }
542
543         if (readonly) {
544                 args.flags |= BTRFS_SUBVOL_RDONLY;
545                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
546                        subvol, dstdir, newname);
547         } else {
548                 printf("Create a snapshot of '%s' in '%s/%s'\n",
549                        subvol, dstdir, newname);
550         }
551
552         args.fd = fd;
553         if (inherit) {
554                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
555                 args.size = qgroup_inherit_size(inherit);
556                 args.qgroup_inherit = inherit;
557         }
558         strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
559         args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
560         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
561         e = errno;
562
563         close(fd);
564         close(fddst);
565
566         if(res < 0 ){
567                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
568                         subvol, strerror(e));
569                 return 11;
570         }
571         free(inherit);
572
573         return 0;
574 }
575
576 static const char * const cmd_subvol_get_default_usage[] = {
577         "btrfs subvolume get-default <path>",
578         "Get the default subvolume of a filesystem",
579         NULL
580 };
581
582 static int cmd_subvol_get_default(int argc, char **argv)
583 {
584         int fd;
585         int ret;
586         char *subvol;
587         struct btrfs_list_filter_set *filter_set;
588         u64 default_id;
589
590         if (check_argc_exact(argc, 2))
591                 usage(cmd_subvol_get_default_usage);
592
593         subvol = argv[1];
594
595         ret = test_issubvolume(subvol);
596         if (ret < 0) {
597                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
598                 return 12;
599         }
600         if (!ret) {
601                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
602                 return 13;
603         }
604
605         fd = open_file_or_dir(subvol);
606         if (fd < 0) {
607                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
608                 return 12;
609         }
610
611         ret = btrfs_list_get_default_subvolume(fd, &default_id);
612         if (ret) {
613                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
614                         strerror(errno));
615                 return ret;
616         }
617
618         if (default_id == 0) {
619                 fprintf(stderr, "ERROR: 'default' dir item not found\n");
620                 return ret;
621         }
622
623         /* no need to resolve roots if FS_TREE is default */
624         if (default_id == BTRFS_FS_TREE_OBJECTID) {
625                 printf("ID 5 (FS_TREE)\n");
626                 return ret;
627         }
628
629         filter_set = btrfs_list_alloc_filter_set();
630         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
631                                 default_id);
632
633         ret = btrfs_list_subvols_print(fd, filter_set, NULL, 0, 1);
634         if (ret)
635                 return 19;
636         return 0;
637 }
638
639 static const char * const cmd_subvol_set_default_usage[] = {
640         "btrfs subvolume set-default <subvolid> <path>",
641         "Set the default subvolume of a filesystem",
642         NULL
643 };
644
645 static int cmd_subvol_set_default(int argc, char **argv)
646 {
647         int     ret=0, fd, e;
648         u64     objectid;
649         char    *path;
650         char    *subvolid;
651
652         if (check_argc_exact(argc, 3))
653                 usage(cmd_subvol_set_default_usage);
654
655         subvolid = argv[1];
656         path = argv[2];
657
658         fd = open_file_or_dir(path);
659         if (fd < 0) {
660                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
661                 return 12;
662         }
663
664         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
665         if (errno == ERANGE) {
666                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
667                 return 30;
668         }
669         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
670         e = errno;
671         close(fd);
672         if( ret < 0 ){
673                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
674                         strerror(e));
675                 return 30;
676         }
677         return 0;
678 }
679
680 static const char * const cmd_find_new_usage[] = {
681         "btrfs subvolume find-new <path> <lastgen>",
682         "List the recently modified files in a filesystem",
683         NULL
684 };
685
686 static int cmd_find_new(int argc, char **argv)
687 {
688         int fd;
689         int ret;
690         char *subvol;
691         u64 last_gen;
692
693         if (check_argc_exact(argc, 3))
694                 usage(cmd_find_new_usage);
695
696         subvol = argv[1];
697         last_gen = atoll(argv[2]);
698
699         ret = test_issubvolume(subvol);
700         if (ret < 0) {
701                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
702                 return 12;
703         }
704         if (!ret) {
705                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
706                 return 13;
707         }
708
709         fd = open_file_or_dir(subvol);
710         if (fd < 0) {
711                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
712                 return 12;
713         }
714         ret = btrfs_list_find_updated_files(fd, 0, last_gen);
715         if (ret)
716                 return 19;
717         return 0;
718 }
719
720 const struct cmd_group subvolume_cmd_group = {
721         subvolume_cmd_group_usage, NULL, {
722                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
723                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
724                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
725                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
726                 { "get-default", cmd_subvol_get_default,
727                         cmd_subvol_get_default_usage, NULL, 0 },
728                 { "set-default", cmd_subvol_set_default,
729                         cmd_subvol_set_default_usage, NULL, 0 },
730                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
731                 { 0, 0, 0, 0, 0 }
732         }
733 };
734
735 int cmd_subvolume(int argc, char **argv)
736 {
737         return handle_command_group(&subvolume_cmd_group, argc, argv);
738 }