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