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