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