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