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