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