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