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