btrfs-progs: add option c to show ogeneration
[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 [-agopurts] [-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         "-c           print the ogeneration of the subvolume",
291         "-g           print the generation of the subvolume",
292         "-o           print only subvolumes bellow specified path",
293         "-u           print the uuid of subvolumes (and snapshots)",
294         "-q           print the parent uuid of the snapshots",
295         "-t           print the result as a table",
296         "-s           list snapshots only in the filesystem",
297         "-r           list readonly subvolumes (including snapshots)",
298         "-G [+|-]value",
299         "             filter the subvolumes by generation",
300         "             (+value: >= value; -value: <= value; value: = value)",
301         "-C [+|-]value",
302         "             filter the subvolumes by ogeneration",
303         "             (+value: >= value; -value: <= value; value: = value)",
304         "--sort=gen,ogen,rootid,path",
305         "             list the subvolume in order of gen, ogen, rootid or path",
306         "             you also can add '+' or '-' in front of each items.",
307         "             (+:ascending, -:descending, ascending default)",
308         NULL,
309 };
310
311 static int cmd_subvol_list(int argc, char **argv)
312 {
313         struct btrfs_list_filter_set *filter_set;
314         struct btrfs_list_comparer_set *comparer_set;
315         u64 flags = 0;
316         int fd = -1;
317         u64 top_id;
318         int ret = -1, uerr = 0;
319         int c;
320         char *subvol;
321         int is_tab_result = 0;
322         int is_list_all = 0;
323         int is_only_in_path = 0;
324         struct option long_options[] = {
325                 {"sort", 1, NULL, 'S'},
326                 {0, 0, 0, 0}
327         };
328
329         filter_set = btrfs_list_alloc_filter_set();
330         comparer_set = btrfs_list_alloc_comparer_set();
331
332         optind = 1;
333         while(1) {
334                 c = getopt_long(argc, argv,
335                                     "acgopqsurG:C:t", long_options, NULL);
336                 if (c < 0)
337                         break;
338
339                 switch(c) {
340                 case 'p':
341                         btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
342                         break;
343                 case 'a':
344                         is_list_all = 1;
345                         break;
346                 case 'c':
347                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
348                         break;
349                 case 'g':
350                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
351                         break;
352                 case 'o':
353                         is_only_in_path = 1;
354                         break;
355                 case 't':
356                         is_tab_result = 1;
357                         break;
358                 case 's':
359                         btrfs_list_setup_filter(&filter_set,
360                                                 BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
361                                                 0);
362                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
363                         btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
364
365                 case 'u':
366                         btrfs_list_setup_print_column(BTRFS_LIST_UUID);
367                         break;
368                 case 'q':
369                         btrfs_list_setup_print_column(BTRFS_LIST_PUUID);
370                         break;
371                 case 'r':
372                         flags |= BTRFS_ROOT_SUBVOL_RDONLY;
373                         break;
374                 case 'G':
375                         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
376                         ret = btrfs_list_parse_filter_string(optarg,
377                                                         &filter_set,
378                                                         BTRFS_LIST_FILTER_GEN);
379                         if (ret) {
380                                 uerr = 1;
381                                 goto out;
382                         }
383                         break;
384
385                 case 'C':
386                         btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
387                         ret = btrfs_list_parse_filter_string(optarg,
388                                                         &filter_set,
389                                                         BTRFS_LIST_FILTER_CGEN);
390                         if (ret) {
391                                 uerr = 1;
392                                 goto out;
393                         }
394                         break;
395                 case 'S':
396                         ret = btrfs_list_parse_sort_string(optarg,
397                                                            &comparer_set);
398                         if (ret) {
399                                 uerr = 1;
400                                 goto out;
401                         }
402                         break;
403
404                 default:
405                         uerr = 1;
406                         goto out;
407                 }
408         }
409
410         if (flags)
411                 btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_FLAGS,
412                                         flags);
413
414         if (check_argc_exact(argc - optind, 1)) {
415                 uerr = 1;
416                 goto out;
417         }
418
419         subvol = argv[optind];
420
421         ret = test_issubvolume(subvol);
422         if (ret < 0) {
423                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
424                 goto out;
425         }
426         if (!ret) {
427                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
428                 ret = -1;
429                 goto out;
430         }
431
432         fd = open_file_or_dir(subvol);
433         if (fd < 0) {
434                 ret = -1;
435                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
436                 goto out;
437         }
438
439         top_id = btrfs_list_get_path_rootid(fd);
440
441         if (is_list_all)
442                 btrfs_list_setup_filter(&filter_set,
443                                         BTRFS_LIST_FILTER_FULL_PATH,
444                                         top_id);
445         else if (is_only_in_path)
446                 btrfs_list_setup_filter(&filter_set,
447                                         BTRFS_LIST_FILTER_TOPID_EQUAL,
448                                         top_id);
449
450         /* by default we shall print the following columns*/
451         btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
452         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
453         btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
454         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
455
456         if (is_tab_result)
457                 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
458                                 BTRFS_LIST_LAYOUT_TABLE,
459                                 !is_list_all && !is_only_in_path, NULL);
460         else
461                 ret = btrfs_list_subvols_print(fd, filter_set, comparer_set,
462                                 BTRFS_LIST_LAYOUT_DEFAULT,
463                                 !is_list_all && !is_only_in_path, NULL);
464
465 out:
466         if (filter_set)
467                 btrfs_list_free_filter_set(filter_set);
468         if (comparer_set)
469                 btrfs_list_free_comparer_set(comparer_set);
470         if (uerr)
471                 usage(cmd_subvol_list_usage);
472
473         return ret;
474 }
475
476 static const char * const cmd_snapshot_usage[] = {
477         "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
478         "Create a snapshot of the subvolume",
479         "Create a writable/readonly snapshot of the subvolume <source> with",
480         "the name <name> in the <dest> directory",
481         "",
482         "-r     create a readonly snapshot",
483         NULL
484 };
485
486 static int cmd_snapshot(int argc, char **argv)
487 {
488         char    *subvol, *dst;
489         int     res, fd, fddst, len, e, readonly = 0;
490         char    *newname;
491         char    *dstdir;
492         struct btrfs_ioctl_vol_args_v2  args;
493         struct btrfs_qgroup_inherit *inherit = NULL;
494
495         optind = 1;
496         memset(&args, 0, sizeof(args));
497         while (1) {
498                 int c = getopt(argc, argv, "c:i:r");
499                 if (c < 0)
500                         break;
501
502                 switch (c) {
503                 case 'c':
504                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
505                         if (res)
506                                 return res;
507                         break;
508                 case 'i':
509                         res = qgroup_inherit_add_group(&inherit, optarg);
510                         if (res)
511                                 return res;
512                         break;
513                 case 'r':
514                         readonly = 1;
515                         break;
516                 case 'x':
517                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
518                         if (res)
519                                 return res;
520                         break;
521                 default:
522                         usage(cmd_snapshot_usage);
523                 }
524         }
525
526         if (check_argc_exact(argc - optind, 2))
527                 usage(cmd_snapshot_usage);
528
529         subvol = argv[optind];
530         dst = argv[optind + 1];
531
532         res = test_issubvolume(subvol);
533         if(res<0){
534                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
535                 return 12;
536         }
537         if(!res){
538                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
539                 return 13;
540         }
541
542         res = test_isdir(dst);
543         if(res == 0 ){
544                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
545                 return 12;
546         }
547
548         if(res>0){
549                 newname = strdup(subvol);
550                 newname = basename(newname);
551                 dstdir = dst;
552         }else{
553                 newname = strdup(dst);
554                 newname = basename(newname);
555                 dstdir = strdup(dst);
556                 dstdir = dirname(dstdir);
557         }
558
559         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
560              strchr(newname, '/') ){
561                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
562                         newname);
563                 return 14;
564         }
565
566         len = strlen(newname);
567         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
568                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
569                         newname);
570                 return 14;
571         }
572
573         fddst = open_file_or_dir(dstdir);
574         if (fddst < 0) {
575                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
576                 return 12;
577         }
578
579         fd = open_file_or_dir(subvol);
580         if (fd < 0) {
581                 close(fddst);
582                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
583                 return 12;
584         }
585
586         if (readonly) {
587                 args.flags |= BTRFS_SUBVOL_RDONLY;
588                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
589                        subvol, dstdir, newname);
590         } else {
591                 printf("Create a snapshot of '%s' in '%s/%s'\n",
592                        subvol, dstdir, newname);
593         }
594
595         args.fd = fd;
596         if (inherit) {
597                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
598                 args.size = qgroup_inherit_size(inherit);
599                 args.qgroup_inherit = inherit;
600         }
601         strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
602         args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
603         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
604         e = errno;
605
606         close(fd);
607         close(fddst);
608
609         if(res < 0 ){
610                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
611                         subvol, strerror(e));
612                 return 11;
613         }
614         free(inherit);
615
616         return 0;
617 }
618
619 static const char * const cmd_subvol_get_default_usage[] = {
620         "btrfs subvolume get-default <path>",
621         "Get the default subvolume of a filesystem",
622         NULL
623 };
624
625 static int cmd_subvol_get_default(int argc, char **argv)
626 {
627         int fd;
628         int ret;
629         char *subvol;
630         struct btrfs_list_filter_set *filter_set;
631         u64 default_id;
632
633         if (check_argc_exact(argc, 2))
634                 usage(cmd_subvol_get_default_usage);
635
636         subvol = argv[1];
637
638         ret = test_issubvolume(subvol);
639         if (ret < 0) {
640                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
641                 return 12;
642         }
643         if (!ret) {
644                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
645                 return 13;
646         }
647
648         fd = open_file_or_dir(subvol);
649         if (fd < 0) {
650                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
651                 return 12;
652         }
653
654         ret = btrfs_list_get_default_subvolume(fd, &default_id);
655         if (ret) {
656                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
657                         strerror(errno));
658                 return ret;
659         }
660
661         if (default_id == 0) {
662                 fprintf(stderr, "ERROR: 'default' dir item not found\n");
663                 return ret;
664         }
665
666         /* no need to resolve roots if FS_TREE is default */
667         if (default_id == BTRFS_FS_TREE_OBJECTID) {
668                 printf("ID 5 (FS_TREE)\n");
669                 return ret;
670         }
671
672         filter_set = btrfs_list_alloc_filter_set();
673         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
674                                 default_id);
675
676         /* by default we shall print the following columns*/
677         btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
678         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
679         btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
680         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
681
682         ret = btrfs_list_subvols_print(fd, filter_set, NULL,
683                 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
684
685         if (filter_set)
686                 btrfs_list_free_filter_set(filter_set);
687         if (ret)
688                 return 19;
689         return 0;
690 }
691
692 static const char * const cmd_subvol_set_default_usage[] = {
693         "btrfs subvolume set-default <subvolid> <path>",
694         "Set the default subvolume of a filesystem",
695         NULL
696 };
697
698 static int cmd_subvol_set_default(int argc, char **argv)
699 {
700         int     ret=0, fd, e;
701         u64     objectid;
702         char    *path;
703         char    *subvolid;
704
705         if (check_argc_exact(argc, 3))
706                 usage(cmd_subvol_set_default_usage);
707
708         subvolid = argv[1];
709         path = argv[2];
710
711         fd = open_file_or_dir(path);
712         if (fd < 0) {
713                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
714                 return 12;
715         }
716
717         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
718         if (errno == ERANGE) {
719                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
720                 return 30;
721         }
722         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
723         e = errno;
724         close(fd);
725         if( ret < 0 ){
726                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
727                         strerror(e));
728                 return 30;
729         }
730         return 0;
731 }
732
733 static const char * const cmd_find_new_usage[] = {
734         "btrfs subvolume find-new <path> <lastgen>",
735         "List the recently modified files in a filesystem",
736         NULL
737 };
738
739 static int cmd_find_new(int argc, char **argv)
740 {
741         int fd;
742         int ret;
743         char *subvol;
744         u64 last_gen;
745
746         if (check_argc_exact(argc, 3))
747                 usage(cmd_find_new_usage);
748
749         subvol = argv[1];
750         last_gen = atoll(argv[2]);
751
752         ret = test_issubvolume(subvol);
753         if (ret < 0) {
754                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
755                 return 12;
756         }
757         if (!ret) {
758                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
759                 return 13;
760         }
761
762         fd = open_file_or_dir(subvol);
763         if (fd < 0) {
764                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
765                 return 12;
766         }
767         ret = btrfs_list_find_updated_files(fd, 0, last_gen);
768         if (ret)
769                 return 19;
770         return 0;
771 }
772
773 static const char * const cmd_subvol_show_usage[] = {
774         "btrfs subvolume show <subvol-path>",
775         "Show more information of the subvolume",
776         NULL
777 };
778
779 static int cmd_subvol_show(int argc, char **argv)
780 {
781         struct root_info get_ri;
782         struct btrfs_list_filter_set *filter_set;
783         char tstr[256];
784         char uuidparse[37];
785         char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
786         char raw_prefix[] = "\t\t\t\t";
787         u64 sv_id, mntid;
788         int fd = -1, mntfd = -1;
789         int ret = -1;
790
791         if (check_argc_exact(argc, 2))
792                 usage(cmd_subvol_show_usage);
793
794         fullpath = realpath(argv[1], 0);
795         if (!fullpath) {
796                 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
797                         argv[1], strerror(errno));
798                 goto out;
799         }
800
801         ret = test_issubvolume(fullpath);
802         if (ret < 0) {
803                 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
804                 goto out;
805         }
806         if (!ret) {
807                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
808                 ret = -1;
809                 goto out;
810         }
811
812         ret = find_mount_root(fullpath, &mnt);
813         if (ret < 0) {
814                 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
815                                 "%s\n", fullpath, strerror(-ret));
816                 goto out;
817         }
818         ret = -1;
819         svpath = get_subvol_name(mnt, fullpath);
820
821         fd = open_file_or_dir(fullpath);
822         if (fd < 0) {
823                 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
824                 goto out;
825         }
826
827         sv_id = btrfs_list_get_path_rootid(fd);
828         if (sv_id < 0) {
829                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
830                         fullpath);
831                 goto out;
832         }
833
834         mntfd = open_file_or_dir(mnt);
835         if (mntfd < 0) {
836                 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
837                 goto out;
838         }
839
840         mntid = btrfs_list_get_path_rootid(mntfd);
841         if (mntid < 0) {
842                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
843                 goto out;
844         }
845
846         if (sv_id == BTRFS_FS_TREE_OBJECTID) {
847                 printf("%s is btrfs root\n", fullpath);
848                 goto out;
849         }
850
851         memset(&get_ri, 0, sizeof(get_ri));
852         get_ri.root_id = sv_id;
853
854         if (btrfs_get_subvol(mntfd, &get_ri)) {
855                 fprintf(stderr, "ERROR: can't find '%s'\n",
856                         svpath);
857                 goto out;
858         }
859
860         ret = 0;
861         /* print the info */
862         printf("%s\n", fullpath);
863         printf("\tName: \t\t\t%s\n", get_ri.name);
864
865         if (uuid_is_null(get_ri.uuid))
866                 strcpy(uuidparse, "-");
867         else
868                 uuid_unparse(get_ri.uuid, uuidparse);
869         printf("\tuuid: \t\t\t%s\n", uuidparse);
870
871         if (uuid_is_null(get_ri.puuid))
872                 strcpy(uuidparse, "-");
873         else
874                 uuid_unparse(get_ri.puuid, uuidparse);
875         printf("\tParent uuid: \t\t%s\n", uuidparse);
876
877         if (get_ri.otime)
878                 strftime(tstr, 256, "%Y-%m-%d %X",
879                          localtime(&get_ri.otime));
880         else
881                 strcpy(tstr, "-");
882         printf("\tCreation time: \t\t%s\n", tstr);
883
884         printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
885         printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
886         printf("\tGen at creation: \t%llu\n", get_ri.ogen);
887         printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
888         printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
889
890         if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
891                 printf("\tFlags: \t\t\treadonly\n");
892         else
893                 printf("\tFlags: \t\t\t-\n");
894
895         /* print the snapshots of the given subvol if any*/
896         printf("\tSnapshot(s):\n");
897         filter_set = btrfs_list_alloc_filter_set();
898         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
899                                 (u64)get_ri.uuid);
900         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
901         btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
902                         1, raw_prefix);
903
904         /* clean up */
905         if (get_ri.path)
906                 free(get_ri.path);
907         if (get_ri.name)
908                 free(get_ri.name);
909         if (get_ri.full_path)
910                 free(get_ri.full_path);
911         if (filter_set)
912                 btrfs_list_free_filter_set(filter_set);
913
914 out:
915         if (mntfd >= 0)
916                 close(mntfd);
917         if (fd >= 0)
918                 close(fd);
919         if (mnt)
920                 free(mnt);
921         if (fullpath)
922                 free(fullpath);
923
924         return ret;
925 }
926
927 const struct cmd_group subvolume_cmd_group = {
928         subvolume_cmd_group_usage, NULL, {
929                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
930                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
931                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
932                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
933                 { "get-default", cmd_subvol_get_default,
934                         cmd_subvol_get_default_usage, NULL, 0 },
935                 { "set-default", cmd_subvol_set_default,
936                         cmd_subvol_set_default_usage, NULL, 0 },
937                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
938                 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
939                 { 0, 0, 0, 0, 0 }
940         }
941 };
942
943 int cmd_subvolume(int argc, char **argv)
944 {
945         return handle_command_group(&subvolume_cmd_group, argc, argv);
946 }