Btrfs-progs: check return value of realpath(3)
[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 #include <uuid/uuid.h>
28
29 #include "kerncompat.h"
30 #include "ioctl.h"
31 #include "qgroup.h"
32
33 #include "ctree.h"
34 #include "commands.h"
35 #include "utils.h"
36 #include "btrfs-list.h"
37 #include "utils.h"
38
39 static const char * const subvolume_cmd_group_usage[] = {
40         "btrfs subvolume <command> <args>",
41         NULL
42 };
43
44 /*
45  * test if path is a directory
46  * this function return
47  * 0-> path exists but it is not a directory
48  * 1-> path exists and it is  a directory
49  * -1 -> path is unaccessible
50  */
51 static int test_isdir(char *path)
52 {
53         struct stat     st;
54         int             res;
55
56         res = stat(path, &st);
57         if(res < 0 )
58                 return -1;
59
60         return S_ISDIR(st.st_mode);
61 }
62
63 static const char * const cmd_subvol_create_usage[] = {
64         "btrfs subvolume create [-i <qgroupid>] [<dest>/]<name>",
65         "Create a subvolume",
66         "Create a subvolume <name> in <dest>.  If <dest> is not given",
67         "subvolume <name> will be created in the current directory.",
68         "",
69         "-i <qgroupid>  add the newly created subvolume to a qgroup. This",
70         "               option can be given multiple times.",
71         NULL
72 };
73
74 static int cmd_subvol_create(int argc, char **argv)
75 {
76         int     retval, res, len;
77         int     fddst = -1;
78         char    *dupname = NULL;
79         char    *dupdir = NULL;
80         char    *newname;
81         char    *dstdir;
82         char    *dst;
83         struct btrfs_qgroup_inherit *inherit = NULL;
84         DIR     *dirstream = NULL;
85
86         optind = 1;
87         while (1) {
88                 int c = getopt(argc, argv, "c:i:");
89                 if (c < 0)
90                         break;
91
92                 switch (c) {
93                 case 'c':
94                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
95                         if (res) {
96                                 retval = res;
97                                 goto out;
98                         }
99                         break;
100                 case 'i':
101                         res = qgroup_inherit_add_group(&inherit, optarg);
102                         if (res) {
103                                 retval = res;
104                                 goto out;
105                         }
106                         break;
107                 default:
108                         usage(cmd_subvol_create_usage);
109                 }
110         }
111
112         if (check_argc_exact(argc - optind, 1))
113                 usage(cmd_subvol_create_usage);
114
115         dst = argv[optind];
116
117         retval = 1;     /* failure */
118         res = test_isdir(dst);
119         if (res >= 0) {
120                 fprintf(stderr, "ERROR: '%s' exists\n", dst);
121                 goto out;
122         }
123
124         dupname = strdup(dst);
125         newname = basename(dupname);
126         dupdir = strdup(dst);
127         dstdir = dirname(dupdir);
128
129         if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
130              strchr(newname, '/') ){
131                 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
132                         newname);
133                 goto out;
134         }
135
136         len = strlen(newname);
137         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
138                 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
139                         newname);
140                 goto out;
141         }
142
143         fddst = open_file_or_dir(dstdir, &dirstream);
144         if (fddst < 0) {
145                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
146                 goto out;
147         }
148
149         printf("Create subvolume '%s/%s'\n", dstdir, newname);
150         if (inherit) {
151                 struct btrfs_ioctl_vol_args_v2  args;
152
153                 memset(&args, 0, sizeof(args));
154                 strncpy_null(args.name, newname);
155                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
156                 args.size = qgroup_inherit_size(inherit);
157                 args.qgroup_inherit = inherit;
158
159                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
160         } else {
161                 struct btrfs_ioctl_vol_args     args;
162
163                 memset(&args, 0, sizeof(args));
164                 strncpy_null(args.name, newname);
165
166                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
167         }
168
169         if (res < 0) {
170                 fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
171                         strerror(errno));
172                 goto out;
173         }
174
175         retval = 0;     /* success */
176 out:
177         close_file_or_dir(fddst, dirstream);
178         free(inherit);
179         free(dupname);
180         free(dupdir);
181
182         return retval;
183 }
184
185 /*
186  * test if path is a subvolume:
187  * this function return
188  * 0-> path exists but it is not a subvolume
189  * 1-> path exists and it is  a subvolume
190  * -1 -> path is unaccessible
191  */
192 int test_issubvolume(char *path)
193 {
194         struct stat     st;
195         int             res;
196
197         res = stat(path, &st);
198         if(res < 0 )
199                 return -1;
200
201         return (st.st_ino == 256) && S_ISDIR(st.st_mode);
202 }
203
204 static const char * const cmd_subvol_delete_usage[] = {
205         "btrfs subvolume delete <subvolume> [<subvolume>...]",
206         "Delete subvolume(s)",
207         NULL
208 };
209
210 static int cmd_subvol_delete(int argc, char **argv)
211 {
212         int     res, fd, len, e, cnt = 1, ret = 0;
213         struct btrfs_ioctl_vol_args     args;
214         char    *dname, *vname, *cpath;
215         char    *dupdname = NULL;
216         char    *dupvname = NULL;
217         char    *path;
218         DIR     *dirstream = NULL;
219
220         if (argc < 2)
221                 usage(cmd_subvol_delete_usage);
222
223 again:
224         path = argv[cnt];
225
226         res = test_issubvolume(path);
227         if (res < 0) {
228                 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
229                 ret = 1;
230                 goto out;
231         }
232         if (!res) {
233                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
234                 ret = 1;
235                 goto out;
236         }
237
238         cpath = realpath(path, NULL);
239         if (!cpath) {
240                 ret = errno;
241                 fprintf(stderr, "ERROR: finding real path for '%s': %s\n",
242                         path, strerror(errno));
243                 goto out;
244         }
245         dupdname = strdup(cpath);
246         dname = dirname(dupdname);
247         dupvname = strdup(cpath);
248         vname = basename(dupvname);
249         free(cpath);
250
251         if (!strcmp(vname, ".") || !strcmp(vname, "..") ||
252              strchr(vname, '/')) {
253                 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
254                         vname);
255                 ret = 1;
256                 goto out;
257         }
258
259         len = strlen(vname);
260         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
261                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
262                         vname);
263                 ret = 1;
264                 goto out;
265         }
266
267         fd = open_file_or_dir(dname, &dirstream);
268         if (fd < 0) {
269                 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
270                 ret = 1;
271                 goto out;
272         }
273
274         printf("Delete subvolume '%s/%s'\n", dname, vname);
275         strncpy_null(args.name, vname);
276         res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
277         e = errno;
278
279         close_file_or_dir(fd, dirstream);
280
281         if(res < 0 ){
282                 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
283                         dname, vname, strerror(e));
284                 ret = 1;
285                 goto out;
286         }
287
288 out:
289         free(dupdname);
290         free(dupvname);
291         cnt++;
292         if (cnt < argc)
293                 goto again;
294
295         return ret;
296 }
297
298 /*
299  * Naming of options:
300  * - uppercase for filters and sort options
301  * - lowercase for enabling specific items in the output
302  */
303 static const char * const cmd_subvol_list_usage[] = {
304         "btrfs subvolume list [options] [-G [+|-]value] [-C [+|-]value] "
305         "[--sort=gen,ogen,rootid,path] <path>",
306         "List subvolumes (and snapshots)",
307         "",
308         "-p           print parent ID",
309         "-a           print all the subvolumes in the filesystem and",
310         "             distinguish absolute and relative path with respect",
311         "             to the given <path>",
312         "-c           print the ogeneration of the subvolume",
313         "-g           print the generation of the subvolume",
314         "-o           print only subvolumes bellow specified path",
315         "-u           print the uuid of subvolumes (and snapshots)",
316         "-q           print the parent uuid of the snapshots",
317         "-t           print the result as a table",
318         "-s           list snapshots only in the filesystem",
319         "-r           list readonly subvolumes (including snapshots)",
320         "-G [+|-]value",
321         "             filter the subvolumes by generation",
322         "             (+value: >= value; -value: <= value; value: = value)",
323         "-C [+|-]value",
324         "             filter the subvolumes by ogeneration",
325         "             (+value: >= value; -value: <= value; value: = value)",
326         "--sort=gen,ogen,rootid,path",
327         "             list the subvolume in order of gen, ogen, rootid or path",
328         "             you also can add '+' or '-' in front of each items.",
329         "             (+:ascending, -:descending, ascending default)",
330         NULL,
331 };
332
333 static int cmd_subvol_list(int argc, char **argv)
334 {
335         struct btrfs_list_filter_set *filter_set;
336         struct btrfs_list_comparer_set *comparer_set;
337         u64 flags = 0;
338         int fd = -1;
339         u64 top_id;
340         int ret = -1, uerr = 0;
341         int c;
342         char *subvol;
343         int is_tab_result = 0;
344         int is_list_all = 0;
345         int is_only_in_path = 0;
346         struct option long_options[] = {
347                 {"sort", 1, NULL, 'S'},
348                 {NULL, 0, NULL, 0}
349         };
350         DIR *dirstream = NULL;
351
352         filter_set = btrfs_list_alloc_filter_set();
353         comparer_set = btrfs_list_alloc_comparer_set();
354
355         optind = 1;
356         while(1) {
357                 c = getopt_long(argc, argv,
358                                     "acgopqsurG:C:t", long_options, NULL);
359                 if (c < 0)
360                         break;
361
362                 switch(c) {
363                 case 'p':
364                         btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
365                         break;
366                 case 'a':
367                         is_list_all = 1;
368                         break;
369                 case 'c':
370                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
371                         break;
372                 case 'g':
373                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
374                         break;
375                 case 'o':
376                         is_only_in_path = 1;
377                         break;
378                 case 't':
379                         is_tab_result = 1;
380                         break;
381                 case 's':
382                         btrfs_list_setup_filter(&filter_set,
383                                                 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
384                                                 0);
385                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
386                         btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
387                         break;
388                 case 'u':
389                         btrfs_list_setup_print_column(BTRFS_LIST_UUID);
390                         break;
391                 case 'q':
392                         btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
393                         break;
394                 case 'r':
395                         flags |= BTRFS_ROOT_SUBVOL_RDONLY;
396                         break;
397                 case 'G':
398                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
399                         ret = btrfs_list_parse_filter_string(optarg,
400                                                         &filter_set,
401                                                         BTRFS_LIST_FILTER_GEN);
402                         if (ret) {
403                                 uerr = 1;
404                                 goto out;
405                         }
406                         break;
407
408                 case 'C':
409                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
410                         ret = btrfs_list_parse_filter_string(optarg,
411                                                         &filter_set,
412                                                         BTRFS_LIST_FILTER_CGEN);
413                         if (ret) {
414                                 uerr = 1;
415                                 goto out;
416                         }
417                         break;
418                 case 'S':
419                         ret = btrfs_list_parse_sort_string(optarg,
420                                                            &comparer_set);
421                         if (ret) {
422                                 uerr = 1;
423                                 goto out;
424                         }
425                         break;
426
427                 default:
428                         uerr = 1;
429                         goto out;
430                 }
431         }
432
433         if (flags)
434                 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
435                                         flags);
436
437         if (check_argc_exact(argc - optind, 1)) {
438                 uerr = 1;
439                 goto out;
440         }
441
442         subvol = argv[optind];
443         fd = open_file_or_dir(subvol, &dirstream);
444         if (fd < 0) {
445                 ret = -1;
446                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
447                 goto out;
448         }
449
450         ret = btrfs_list_get_path_rootid(fd, &top_id);
451         if (ret) {
452                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", subvol);
453                 goto out;
454         }
455
456         if (is_list_all)
457                 btrfs_list_setup_filter(&filter_set,
458                                         BTRFS_LIST_FILTER_FULL_PATH,
459                                         top_id);
460         else if (is_only_in_path)
461                 btrfs_list_setup_filter(&filter_set,
462                                         BTRFS_LIST_FILTER_TOPID_EQUAL,
463                                         top_id);
464
465         /* by default we shall print the following columns*/
466         btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
467         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
468         btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
469         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
470
471         if (is_tab_result)
472                 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
473                                 BTRFS_LIST_LAYOUT_TABLE,
474                                 !is_list_all && !is_only_in_path, NULL);
475         else
476                 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
477                                 BTRFS_LIST_LAYOUT_DEFAULT,
478                                 !is_list_all && !is_only_in_path, NULL);
479
480 out:
481         close_file_or_dir(fd, dirstream);
482         if (filter_set)
483                 btrfs_list_free_filter_set(filter_set);
484         if (comparer_set)
485                 btrfs_list_free_comparer_set(comparer_set);
486         if (uerr)
487                 usage(cmd_subvol_list_usage);
488         return !!ret;
489 }
490
491 static const char * const cmd_snapshot_usage[] = {
492         "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
493         "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
494         "Create a snapshot of the subvolume",
495         "Create a writable/readonly snapshot of the subvolume <source> with",
496         "the name <name> in the <dest> directory.  If only <dest> is given,",
497         "the subvolume will be named the basename of <source>.",
498         "",
499         "-r             create a readonly snapshot",
500         "-i <qgroupid>  add the newly created snapshot to a qgroup. This",
501         "               option can be given multiple times.",
502         NULL
503 };
504
505 static int cmd_snapshot(int argc, char **argv)
506 {
507         char    *subvol, *dst;
508         int     res, retval;
509         int     fd = -1, fddst = -1;
510         int     len, readonly = 0;
511         char    *dupname = NULL;
512         char    *dupdir = NULL;
513         char    *newname;
514         char    *dstdir;
515         struct btrfs_ioctl_vol_args_v2  args;
516         struct btrfs_qgroup_inherit *inherit = NULL;
517         DIR *dirstream1 = NULL, *dirstream2 = NULL;
518
519         optind = 1;
520         memset(&args, 0, sizeof(args));
521         while (1) {
522                 int c = getopt(argc, argv, "c:i:r");
523                 if (c < 0)
524                         break;
525
526                 switch (c) {
527                 case 'c':
528                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
529                         if (res) {
530                                 retval = res;
531                                 goto out;
532                         }
533                         break;
534                 case 'i':
535                         res = qgroup_inherit_add_group(&inherit, optarg);
536                         if (res) {
537                                 retval = res;
538                                 goto out;
539                         }
540                         break;
541                 case 'r':
542                         readonly = 1;
543                         break;
544                 case 'x':
545                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
546                         if (res) {
547                                 retval = res;
548                                 goto out;
549                         }
550                         break;
551                 default:
552                         usage(cmd_snapshot_usage);
553                 }
554         }
555
556         if (check_argc_exact(argc - optind, 2))
557                 usage(cmd_snapshot_usage);
558
559         subvol = argv[optind];
560         dst = argv[optind + 1];
561
562         retval = 1;     /* failure */
563         res = test_issubvolume(subvol);
564         if (res < 0) {
565                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
566                 goto out;
567         }
568         if (!res) {
569                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
570                 goto out;
571         }
572
573         res = test_isdir(dst);
574         if (res == 0) {
575                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
576                 goto out;
577         }
578
579         if (res > 0) {
580                 dupname = strdup(subvol);
581                 newname = basename(dupname);
582                 dstdir = dst;
583         } else {
584                 dupname = strdup(dst);
585                 newname = basename(dupname);
586                 dupdir = strdup(dst);
587                 dstdir = dirname(dupdir);
588         }
589
590         if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
591              strchr(newname, '/') ){
592                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
593                         newname);
594                 goto out;
595         }
596
597         len = strlen(newname);
598         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
599                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
600                         newname);
601                 goto out;
602         }
603
604         fddst = open_file_or_dir(dstdir, &dirstream1);
605         if (fddst < 0) {
606                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
607                 goto out;
608         }
609
610         fd = open_file_or_dir(subvol, &dirstream2);
611         if (fd < 0) {
612                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
613                 goto out;
614         }
615
616         if (readonly) {
617                 args.flags |= BTRFS_SUBVOL_RDONLY;
618                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
619                        subvol, dstdir, newname);
620         } else {
621                 printf("Create a snapshot of '%s' in '%s/%s'\n",
622                        subvol, dstdir, newname);
623         }
624
625         args.fd = fd;
626         if (inherit) {
627                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
628                 args.size = qgroup_inherit_size(inherit);
629                 args.qgroup_inherit = inherit;
630         }
631         strncpy_null(args.name, newname);
632
633         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
634
635         if (res < 0) {
636                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
637                         subvol, strerror(errno));
638                 goto out;
639         }
640
641         retval = 0;     /* success */
642
643 out:
644         close_file_or_dir(fddst, dirstream1);
645         close_file_or_dir(fd, dirstream2);
646         free(inherit);
647         free(dupname);
648         free(dupdir);
649
650         return retval;
651 }
652
653 static const char * const cmd_subvol_get_default_usage[] = {
654         "btrfs subvolume get-default <path>",
655         "Get the default subvolume of a filesystem",
656         NULL
657 };
658
659 static int cmd_subvol_get_default(int argc, char **argv)
660 {
661         int fd = -1;
662         int ret;
663         char *subvol;
664         struct btrfs_list_filter_set *filter_set;
665         u64 default_id;
666         DIR *dirstream = NULL;
667
668         if (check_argc_exact(argc, 2))
669                 usage(cmd_subvol_get_default_usage);
670
671         subvol = argv[1];
672         fd = open_file_or_dir(subvol, &dirstream);
673         if (fd < 0) {
674                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
675                 return 1;
676         }
677
678         ret = btrfs_list_get_default_subvolume(fd, &default_id);
679         if (ret) {
680                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
681                         strerror(errno));
682                 goto out;
683         }
684
685         ret = 1;
686         if (default_id == 0) {
687                 fprintf(stderr, "ERROR: 'default' dir item not found\n");
688                 goto out;
689         }
690
691         /* no need to resolve roots if FS_TREE is default */
692         if (default_id == BTRFS_FS_TREE_OBJECTID) {
693                 printf("ID 5 (FS_TREE)\n");
694                 goto out;
695         }
696
697         filter_set = btrfs_list_alloc_filter_set();
698         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
699                                 default_id);
700
701         /* by default we shall print the following columns*/
702         btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
703         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
704         btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
705         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
706
707         ret = btrfs_list_subvols_print(fd, filter_set, NULL,
708                 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
709
710         if (filter_set)
711                 btrfs_list_free_filter_set(filter_set);
712 out:
713         close_file_or_dir(fd, dirstream);
714         return !!ret;
715 }
716
717 static const char * const cmd_subvol_set_default_usage[] = {
718         "btrfs subvolume set-default <subvolid> <path>",
719         "Set the default subvolume of a filesystem",
720         NULL
721 };
722
723 static int cmd_subvol_set_default(int argc, char **argv)
724 {
725         int     ret=0, fd, e;
726         u64     objectid;
727         char    *path;
728         char    *subvolid;
729         DIR     *dirstream = NULL;
730
731         if (check_argc_exact(argc, 3))
732                 usage(cmd_subvol_set_default_usage);
733
734         subvolid = argv[1];
735         path = argv[2];
736
737         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
738         if (errno == ERANGE) {
739                 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
740                 return 1;
741         }
742
743         fd = open_file_or_dir(path, &dirstream);
744         if (fd < 0) {
745                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
746                 return 1;
747         }
748
749         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
750         e = errno;
751         close_file_or_dir(fd, dirstream);
752         if (ret < 0) {
753                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
754                         strerror(e));
755                 return 1;
756         }
757         return 0;
758 }
759
760 static const char * const cmd_find_new_usage[] = {
761         "btrfs subvolume find-new <path> <lastgen>",
762         "List the recently modified files in a filesystem",
763         NULL
764 };
765
766 static int cmd_find_new(int argc, char **argv)
767 {
768         int fd;
769         int ret;
770         char *subvol;
771         u64 last_gen;
772         DIR *dirstream = NULL;
773
774         if (check_argc_exact(argc, 3))
775                 usage(cmd_find_new_usage);
776
777         subvol = argv[1];
778         last_gen = atoll(argv[2]);
779
780         ret = test_issubvolume(subvol);
781         if (ret < 0) {
782                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
783                 return 1;
784         }
785         if (!ret) {
786                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
787                 return 1;
788         }
789
790         fd = open_file_or_dir(subvol, &dirstream);
791         if (fd < 0) {
792                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
793                 return 1;
794         }
795
796         ret = ioctl(fd, BTRFS_IOC_SYNC);
797         if (ret < 0) {
798                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
799                         subvol, strerror(errno));
800                 close_file_or_dir(fd, dirstream);
801                 return 1;
802         }
803
804         ret = btrfs_list_find_updated_files(fd, 0, last_gen);
805         close_file_or_dir(fd, dirstream);
806         return !!ret;
807 }
808
809 static const char * const cmd_subvol_show_usage[] = {
810         "btrfs subvolume show <subvol-path>",
811         "Show more information of the subvolume",
812         NULL
813 };
814
815 static int cmd_subvol_show(int argc, char **argv)
816 {
817         struct root_info get_ri;
818         struct btrfs_list_filter_set *filter_set;
819         char tstr[256];
820         char uuidparse[37];
821         char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
822         char raw_prefix[] = "\t\t\t\t";
823         u64 sv_id, mntid;
824         int fd = -1, mntfd = -1;
825         int ret = 1;
826         DIR *dirstream1 = NULL, *dirstream2 = NULL;
827
828         if (check_argc_exact(argc, 2))
829                 usage(cmd_subvol_show_usage);
830
831         fullpath = realpath(argv[1], NULL);
832         if (!fullpath) {
833                 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
834                         argv[1], strerror(errno));
835                 goto out;
836         }
837
838         ret = test_issubvolume(fullpath);
839         if (ret < 0) {
840                 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
841                 goto out;
842         }
843         if (!ret) {
844                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
845                 goto out;
846         }
847
848         ret = find_mount_root(fullpath, &mnt);
849         if (ret < 0) {
850                 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
851                                 "%s\n", fullpath, strerror(-ret));
852                 goto out;
853         }
854         ret = 1;
855         svpath = get_subvol_name(mnt, fullpath);
856
857         fd = open_file_or_dir(fullpath, &dirstream1);
858         if (fd < 0) {
859                 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
860                 goto out;
861         }
862
863         ret = btrfs_list_get_path_rootid(fd, &sv_id);
864         if (ret) {
865                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
866                         fullpath);
867                 goto out;
868         }
869
870         mntfd = open_file_or_dir(mnt, &dirstream2);
871         if (mntfd < 0) {
872                 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
873                 goto out;
874         }
875
876         ret = btrfs_list_get_path_rootid(mntfd, &mntid);
877         if (ret) {
878                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
879                 goto out;
880         }
881
882         if (sv_id == BTRFS_FS_TREE_OBJECTID) {
883                 printf("%s is btrfs root\n", fullpath);
884                 goto out;
885         }
886
887         memset(&get_ri, 0, sizeof(get_ri));
888         get_ri.root_id = sv_id;
889
890         if (btrfs_get_subvol(mntfd, &get_ri)) {
891                 fprintf(stderr, "ERROR: can't find '%s'\n",
892                         svpath);
893                 goto out;
894         }
895
896         ret = 0;
897         /* print the info */
898         printf("%s\n", fullpath);
899         printf("\tName: \t\t\t%s\n", get_ri.name);
900
901         if (uuid_is_null(get_ri.uuid))
902                 strcpy(uuidparse, "-");
903         else
904                 uuid_unparse(get_ri.uuid, uuidparse);
905         printf("\tuuid: \t\t\t%s\n", uuidparse);
906
907         if (uuid_is_null(get_ri.puuid))
908                 strcpy(uuidparse, "-");
909         else
910                 uuid_unparse(get_ri.puuid, uuidparse);
911         printf("\tParent uuid: \t\t%s\n", uuidparse);
912
913         if (get_ri.otime) {
914                 struct tm tm;
915
916                 localtime_r(&get_ri.otime, &tm);
917                 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
918         } else
919                 strcpy(tstr, "-");
920         printf("\tCreation time: \t\t%s\n", tstr);
921
922         printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
923         printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
924         printf("\tGen at creation: \t%llu\n", get_ri.ogen);
925         printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
926         printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
927
928         if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
929                 printf("\tFlags: \t\t\treadonly\n");
930         else
931                 printf("\tFlags: \t\t\t-\n");
932
933         /* print the snapshots of the given subvol if any*/
934         printf("\tSnapshot(s):\n");
935         filter_set = btrfs_list_alloc_filter_set();
936         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
937                                 (u64)(unsigned long)get_ri.uuid);
938         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
939         btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
940                         1, raw_prefix);
941
942         /* clean up */
943         if (get_ri.path)
944                 free(get_ri.path);
945         if (get_ri.name)
946                 free(get_ri.name);
947         if (get_ri.full_path)
948                 free(get_ri.full_path);
949         if (filter_set)
950                 btrfs_list_free_filter_set(filter_set);
951
952 out:
953         close_file_or_dir(fd, dirstream1);
954         close_file_or_dir(mntfd, dirstream2);
955         if (mnt)
956                 free(mnt);
957         if (fullpath)
958                 free(fullpath);
959         return !!ret;
960 }
961
962 const struct cmd_group subvolume_cmd_group = {
963         subvolume_cmd_group_usage, NULL, {
964                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
965                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
966                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
967                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
968                 { "get-default", cmd_subvol_get_default,
969                         cmd_subvol_get_default_usage, NULL, 0 },
970                 { "set-default", cmd_subvol_set_default,
971                         cmd_subvol_set_default_usage, NULL, 0 },
972                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
973                 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
974                 NULL_CMD_STRUCT
975         }
976 };
977
978 int cmd_subvolume(int argc, char **argv)
979 {
980         return handle_command_group(&subvolume_cmd_group, argc, argv);
981 }