Btrfs-progs: move path modification to filters
[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 [-apurts] [-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         "-u           print the uuid of subvolumes (and snapshots)",
285         "-t           print the result as a table",
286         "-s           list snapshots only in the filesystem",
287         "-r           list readonly subvolumes (including snapshots)",
288         "-g [+|-]value",
289         "             filter the subvolumes by generation",
290         "             (+value: >= value; -value: <= value; value: = value)",
291         "-c [+|-]value",
292         "             filter the subvolumes by ogeneration",
293         "             (+value: >= value; -value: <= value; value: = value)",
294         "--sort=gen,ogen,rootid,path",
295         "             list the subvolume in order of gen, ogen, rootid or path",
296         "             you also can add '+' or '-' in front of each items.",
297         "             (+:ascending, -:descending, ascending default)",
298         NULL,
299 };
300
301 static int cmd_subvol_list(int argc, char **argv)
302 {
303         struct btrfs_list_filter_set *filter_set;
304         struct btrfs_list_comparer_set *comparer_set;
305         u64 flags = 0;
306         int fd;
307         u64 top_id;
308         int ret;
309         int c;
310         char *subvol;
311         int is_tab_result = 0;
312         int is_list_all = 0;
313         struct option long_options[] = {
314                 {"sort", 1, NULL, 'S'},
315                 {0, 0, 0, 0}
316         };
317
318         filter_set = btrfs_list_alloc_filter_set();
319         comparer_set = btrfs_list_alloc_comparer_set();
320
321         optind = 1;
322         while(1) {
323                 c = getopt_long(argc, argv,
324                                     "apsurg:c:t", long_options, NULL);
325                 if (c < 0)
326                         break;
327
328                 switch(c) {
329                 case 'p':
330                         btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
331                         break;
332                 case 'a':
333                         is_list_all = 1;
334                         break;
335                 case 't':
336                         is_tab_result = 1;
337                         break;
338                 case 's':
339                         btrfs_list_setup_filter(&filter_set,
340                                                 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
341                                                 0);
342                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
343                         btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
344
345                 case 'u':
346                         btrfs_list_setup_print_column(BTRFS_LIST_UUID);
347                         break;
348                 case 'r':
349                         flags |= BTRFS_ROOT_SUBVOL_RDONLY;
350                         break;
351                 case 'g':
352                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
353                         ret = btrfs_list_parse_filter_string(optarg,
354                                                         &filter_set,
355                                                         BTRFS_LIST_FILTER_GEN);
356                         if (ret)
357                                 usage(cmd_subvol_list_usage);
358                         break;
359
360                 case 'c':
361                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
362                         ret = btrfs_list_parse_filter_string(optarg,
363                                                         &filter_set,
364                                                         BTRFS_LIST_FILTER_CGEN);
365                         if (ret)
366                                 usage(cmd_subvol_list_usage);
367                         break;
368                 case 'S':
369                         ret = btrfs_list_parse_sort_string(optarg,
370                                                            &comparer_set);
371                         if (ret)
372                                 usage(cmd_subvol_list_usage);
373                         break;
374
375                 default:
376                         usage(cmd_subvol_list_usage);
377                 }
378         }
379
380         if (flags)
381                 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
382                                         flags);
383
384         if (check_argc_exact(argc - optind, 1))
385                 usage(cmd_subvol_list_usage);
386
387         subvol = argv[optind];
388
389         ret = test_issubvolume(subvol);
390         if (ret < 0) {
391                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
392                 return 12;
393         }
394         if (!ret) {
395                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
396                 return 13;
397         }
398
399         fd = open_file_or_dir(subvol);
400         if (fd < 0) {
401                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
402                 return 12;
403         }
404
405         top_id = btrfs_list_get_path_rootid(fd);
406
407         if (is_list_all)
408                 btrfs_list_setup_filter(&filter_set,
409                                         BTRFS_LIST_FILTER_FULL_PATH,
410                                         top_id);
411         else
412                 btrfs_list_setup_filter(&filter_set,
413                                         BTRFS_LIST_FILTER_TOPID_EQUAL,
414                                         top_id);
415
416         ret = btrfs_list_subvols(fd, filter_set, comparer_set,
417                                 is_tab_result);
418         if (ret)
419                 return 19;
420         return 0;
421 }
422
423 static const char * const cmd_snapshot_usage[] = {
424         "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
425         "Create a snapshot of the subvolume",
426         "Create a writable/readonly snapshot of the subvolume <source> with",
427         "the name <name> in the <dest> directory",
428         "",
429         "-r     create a readonly snapshot",
430         NULL
431 };
432
433 static int cmd_snapshot(int argc, char **argv)
434 {
435         char    *subvol, *dst;
436         int     res, fd, fddst, len, e, readonly = 0;
437         char    *newname;
438         char    *dstdir;
439         struct btrfs_ioctl_vol_args_v2  args;
440         struct btrfs_qgroup_inherit *inherit = NULL;
441
442         optind = 1;
443         memset(&args, 0, sizeof(args));
444         while (1) {
445                 int c = getopt(argc, argv, "c:i:r");
446                 if (c < 0)
447                         break;
448
449                 switch (c) {
450                 case 'c':
451                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
452                         if (res)
453                                 return res;
454                         break;
455                 case 'i':
456                         res = qgroup_inherit_add_group(&inherit, optarg);
457                         if (res)
458                                 return res;
459                         break;
460                 case 'r':
461                         readonly = 1;
462                         break;
463                 case 'x':
464                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
465                         if (res)
466                                 return res;
467                         break;
468                 default:
469                         usage(cmd_snapshot_usage);
470                 }
471         }
472
473         if (check_argc_exact(argc - optind, 2))
474                 usage(cmd_snapshot_usage);
475
476         subvol = argv[optind];
477         dst = argv[optind + 1];
478
479         res = test_issubvolume(subvol);
480         if(res<0){
481                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
482                 return 12;
483         }
484         if(!res){
485                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
486                 return 13;
487         }
488
489         res = test_isdir(dst);
490         if(res == 0 ){
491                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
492                 return 12;
493         }
494
495         if(res>0){
496                 newname = strdup(subvol);
497                 newname = basename(newname);
498                 dstdir = dst;
499         }else{
500                 newname = strdup(dst);
501                 newname = basename(newname);
502                 dstdir = strdup(dst);
503                 dstdir = dirname(dstdir);
504         }
505
506         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
507              strchr(newname, '/') ){
508                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
509                         newname);
510                 return 14;
511         }
512
513         len = strlen(newname);
514         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
515                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
516                         newname);
517                 return 14;
518         }
519
520         fddst = open_file_or_dir(dstdir);
521         if (fddst < 0) {
522                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
523                 return 12;
524         }
525
526         fd = open_file_or_dir(subvol);
527         if (fd < 0) {
528                 close(fddst);
529                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
530                 return 12;
531         }
532
533         if (readonly) {
534                 args.flags |= BTRFS_SUBVOL_RDONLY;
535                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
536                        subvol, dstdir, newname);
537         } else {
538                 printf("Create a snapshot of '%s' in '%s/%s'\n",
539                        subvol, dstdir, newname);
540         }
541
542         args.fd = fd;
543         if (inherit) {
544                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
545                 args.size = qgroup_inherit_size(inherit);
546                 args.qgroup_inherit = inherit;
547         }
548         strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
549         args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
550         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
551         e = errno;
552
553         close(fd);
554         close(fddst);
555
556         if(res < 0 ){
557                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
558                         subvol, strerror(e));
559                 return 11;
560         }
561         free(inherit);
562
563         return 0;
564 }
565
566 static const char * const cmd_subvol_get_default_usage[] = {
567         "btrfs subvolume get-default <path>",
568         "Get the default subvolume of a filesystem",
569         NULL
570 };
571
572 static int cmd_subvol_get_default(int argc, char **argv)
573 {
574         int fd;
575         int ret;
576         char *subvol;
577         struct btrfs_list_filter_set *filter_set;
578         u64 default_id;
579
580         if (check_argc_exact(argc, 2))
581                 usage(cmd_subvol_get_default_usage);
582
583         subvol = argv[1];
584
585         ret = test_issubvolume(subvol);
586         if (ret < 0) {
587                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
588                 return 12;
589         }
590         if (!ret) {
591                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
592                 return 13;
593         }
594
595         fd = open_file_or_dir(subvol);
596         if (fd < 0) {
597                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
598                 return 12;
599         }
600
601         ret = btrfs_list_get_default_subvolume(fd, &default_id);
602         if (ret) {
603                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
604                         strerror(errno));
605                 return ret;
606         }
607
608         if (default_id == 0) {
609                 fprintf(stderr, "ERROR: 'default' dir item not found\n");
610                 return ret;
611         }
612
613         /* no need to resolve roots if FS_TREE is default */
614         if (default_id == BTRFS_FS_TREE_OBJECTID) {
615                 printf("ID 5 (FS_TREE)\n");
616                 return ret;
617         }
618
619         filter_set = btrfs_list_alloc_filter_set();
620         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
621                                 default_id);
622
623         ret = btrfs_list_subvols(fd, filter_set, NULL, 0);
624         if (ret)
625                 return 19;
626         return 0;
627 }
628
629 static const char * const cmd_subvol_set_default_usage[] = {
630         "btrfs subvolume set-default <subvolid> <path>",
631         "Set the default subvolume of a filesystem",
632         NULL
633 };
634
635 static int cmd_subvol_set_default(int argc, char **argv)
636 {
637         int     ret=0, fd, e;
638         u64     objectid;
639         char    *path;
640         char    *subvolid;
641
642         if (check_argc_exact(argc, 3))
643                 usage(cmd_subvol_set_default_usage);
644
645         subvolid = argv[1];
646         path = argv[2];
647
648         fd = open_file_or_dir(path);
649         if (fd < 0) {
650                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
651                 return 12;
652         }
653
654         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
655         if (errno == ERANGE) {
656                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
657                 return 30;
658         }
659         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
660         e = errno;
661         close(fd);
662         if( ret < 0 ){
663                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
664                         strerror(e));
665                 return 30;
666         }
667         return 0;
668 }
669
670 static const char * const cmd_find_new_usage[] = {
671         "btrfs subvolume find-new <path> <lastgen>",
672         "List the recently modified files in a filesystem",
673         NULL
674 };
675
676 static int cmd_find_new(int argc, char **argv)
677 {
678         int fd;
679         int ret;
680         char *subvol;
681         u64 last_gen;
682
683         if (check_argc_exact(argc, 3))
684                 usage(cmd_find_new_usage);
685
686         subvol = argv[1];
687         last_gen = atoll(argv[2]);
688
689         ret = test_issubvolume(subvol);
690         if (ret < 0) {
691                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
692                 return 12;
693         }
694         if (!ret) {
695                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
696                 return 13;
697         }
698
699         fd = open_file_or_dir(subvol);
700         if (fd < 0) {
701                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
702                 return 12;
703         }
704         ret = btrfs_list_find_updated_files(fd, 0, last_gen);
705         if (ret)
706                 return 19;
707         return 0;
708 }
709
710 const struct cmd_group subvolume_cmd_group = {
711         subvolume_cmd_group_usage, NULL, {
712                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
713                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
714                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
715                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
716                 { "get-default", cmd_subvol_get_default,
717                         cmd_subvol_get_default_usage, NULL, 0 },
718                 { "set-default", cmd_subvol_set_default,
719                         cmd_subvol_set_default_usage, NULL, 0 },
720                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
721                 { 0, 0, 0, 0, 0 }
722         }
723 };
724
725 int cmd_subvolume(int argc, char **argv)
726 {
727         return handle_command_group(&subvolume_cmd_group, argc, argv);
728 }