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