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