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