Btrfs-progs: fix magic return value in cmds-subvolume.c
[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 = 1;
224                 goto out;
225         }
226         if (!res) {
227                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
228                 ret = 1;
229                 goto out;
230         }
231
232         cpath = realpath(path, NULL);
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 = 1;
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 = 1;
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 = 1;
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 = 1;
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                 {NULL, 0, NULL, 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         return !!ret;
475 }
476
477 static const char * const cmd_snapshot_usage[] = {
478         "btrfs subvolume snapshot [-r] <source> <dest>|[<dest>/]<name>",
479         "btrfs subvolume snapshot [-r] [-i <qgroupid>] <source> <dest>|[<dest>/]<name>",
480         "Create a snapshot of the subvolume",
481         "Create a writable/readonly snapshot of the subvolume <source> with",
482         "the name <name> in the <dest> directory.  If only <dest> is given,",
483         "the subvolume will be named the basename of <source>.",
484         "",
485         "-r             create a readonly snapshot",
486         "-i <qgroupid>  add the newly created snapshot to a qgroup. This",
487         "               option can be given multiple times.",
488         NULL
489 };
490
491 static int cmd_snapshot(int argc, char **argv)
492 {
493         char    *subvol, *dst;
494         int     res, retval;
495         int     fd = -1, fddst = -1;
496         int     len, readonly = 0;
497         char    *newname;
498         char    *dstdir;
499         struct btrfs_ioctl_vol_args_v2  args;
500         struct btrfs_qgroup_inherit *inherit = NULL;
501         DIR *dirstream1 = NULL, *dirstream2 = NULL;
502
503         optind = 1;
504         memset(&args, 0, sizeof(args));
505         while (1) {
506                 int c = getopt(argc, argv, "c:i:r");
507                 if (c < 0)
508                         break;
509
510                 switch (c) {
511                 case 'c':
512                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
513                         if (res) {
514                                 retval = res;
515                                 goto out;
516                         }
517                         break;
518                 case 'i':
519                         res = qgroup_inherit_add_group(&inherit, optarg);
520                         if (res) {
521                                 retval = res;
522                                 goto out;
523                         }
524                         break;
525                 case 'r':
526                         readonly = 1;
527                         break;
528                 case 'x':
529                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
530                         if (res) {
531                                 retval = res;
532                                 goto out;
533                         }
534                         break;
535                 default:
536                         usage(cmd_snapshot_usage);
537                 }
538         }
539
540         if (check_argc_exact(argc - optind, 2))
541                 usage(cmd_snapshot_usage);
542
543         subvol = argv[optind];
544         dst = argv[optind + 1];
545
546         retval = 1;     /* failure */
547         res = test_issubvolume(subvol);
548         if (res < 0) {
549                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
550                 goto out;
551         }
552         if (!res) {
553                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
554                 goto out;
555         }
556
557         res = test_isdir(dst);
558         if (res == 0) {
559                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
560                 goto out;
561         }
562
563         if (res > 0) {
564                 newname = strdup(subvol);
565                 newname = basename(newname);
566                 dstdir = dst;
567         } else {
568                 newname = strdup(dst);
569                 newname = basename(newname);
570                 dstdir = strdup(dst);
571                 dstdir = dirname(dstdir);
572         }
573
574         if (!strcmp(newname, ".") || !strcmp(newname, "..") ||
575              strchr(newname, '/') ){
576                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
577                         newname);
578                 goto out;
579         }
580
581         len = strlen(newname);
582         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
583                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
584                         newname);
585                 goto out;
586         }
587
588         fddst = open_file_or_dir(dstdir, &dirstream1);
589         if (fddst < 0) {
590                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
591                 goto out;
592         }
593
594         fd = open_file_or_dir(subvol, &dirstream2);
595         if (fd < 0) {
596                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
597                 goto out;
598         }
599
600         if (readonly) {
601                 args.flags |= BTRFS_SUBVOL_RDONLY;
602                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
603                        subvol, dstdir, newname);
604         } else {
605                 printf("Create a snapshot of '%s' in '%s/%s'\n",
606                        subvol, dstdir, newname);
607         }
608
609         args.fd = fd;
610         if (inherit) {
611                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
612                 args.size = qgroup_inherit_size(inherit);
613                 args.qgroup_inherit = inherit;
614         }
615         strncpy_null(args.name, newname);
616
617         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
618
619         if (res < 0) {
620                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
621                         subvol, strerror(errno));
622                 goto out;
623         }
624
625         retval = 0;     /* success */
626
627 out:
628         close_file_or_dir(fddst, dirstream1);
629         close_file_or_dir(fd, dirstream2);
630         free(inherit);
631
632         return retval;
633 }
634
635 static const char * const cmd_subvol_get_default_usage[] = {
636         "btrfs subvolume get-default <path>",
637         "Get the default subvolume of a filesystem",
638         NULL
639 };
640
641 static int cmd_subvol_get_default(int argc, char **argv)
642 {
643         int fd = -1;
644         int ret;
645         char *subvol;
646         struct btrfs_list_filter_set *filter_set;
647         u64 default_id;
648         DIR *dirstream = NULL;
649
650         if (check_argc_exact(argc, 2))
651                 usage(cmd_subvol_get_default_usage);
652
653         subvol = argv[1];
654         fd = open_file_or_dir(subvol, &dirstream);
655         if (fd < 0) {
656                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
657                 return 1;
658         }
659
660         ret = btrfs_list_get_default_subvolume(fd, &default_id);
661         if (ret) {
662                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
663                         strerror(errno));
664                 goto out;
665         }
666
667         ret = 1;
668         if (default_id == 0) {
669                 fprintf(stderr, "ERROR: 'default' dir item not found\n");
670                 goto out;
671         }
672
673         /* no need to resolve roots if FS_TREE is default */
674         if (default_id == BTRFS_FS_TREE_OBJECTID) {
675                 printf("ID 5 (FS_TREE)\n");
676                 goto out;
677         }
678
679         filter_set = btrfs_list_alloc_filter_set();
680         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
681                                 default_id);
682
683         /* by default we shall print the following columns*/
684         btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID);
685         btrfs_list_setup_print_column(BTRFS_LIST_GENERATION);
686         btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL);
687         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
688
689         ret = btrfs_list_subvols_print(fd, filter_set, NULL,
690                 BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL);
691
692         if (filter_set)
693                 btrfs_list_free_filter_set(filter_set);
694 out:
695         close_file_or_dir(fd, dirstream);
696         return !!ret;
697 }
698
699 static const char * const cmd_subvol_set_default_usage[] = {
700         "btrfs subvolume set-default <subvolid> <path>",
701         "Set the default subvolume of a filesystem",
702         NULL
703 };
704
705 static int cmd_subvol_set_default(int argc, char **argv)
706 {
707         int     ret=0, fd, e;
708         u64     objectid;
709         char    *path;
710         char    *subvolid;
711         DIR     *dirstream = NULL;
712
713         if (check_argc_exact(argc, 3))
714                 usage(cmd_subvol_set_default_usage);
715
716         subvolid = argv[1];
717         path = argv[2];
718
719         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
720         if (errno == ERANGE) {
721                 fprintf(stderr, "ERROR: invalid tree id (%s)\n", subvolid);
722                 return 1;
723         }
724
725         fd = open_file_or_dir(path, &dirstream);
726         if (fd < 0) {
727                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
728                 return 1;
729         }
730
731         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
732         e = errno;
733         close_file_or_dir(fd, dirstream);
734         if (ret < 0) {
735                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
736                         strerror(e));
737                 return 1;
738         }
739         return 0;
740 }
741
742 static const char * const cmd_find_new_usage[] = {
743         "btrfs subvolume find-new <path> <lastgen>",
744         "List the recently modified files in a filesystem",
745         NULL
746 };
747
748 static int cmd_find_new(int argc, char **argv)
749 {
750         int fd;
751         int ret;
752         char *subvol;
753         u64 last_gen;
754         DIR *dirstream = NULL;
755
756         if (check_argc_exact(argc, 3))
757                 usage(cmd_find_new_usage);
758
759         subvol = argv[1];
760         last_gen = atoll(argv[2]);
761
762         ret = test_issubvolume(subvol);
763         if (ret < 0) {
764                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
765                 return 1;
766         }
767         if (!ret) {
768                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
769                 return 1;
770         }
771
772         fd = open_file_or_dir(subvol, &dirstream);
773         if (fd < 0) {
774                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
775                 return 1;
776         }
777         ret = btrfs_list_find_updated_files(fd, 0, last_gen);
778         close_file_or_dir(fd, dirstream);
779         return !!ret;
780 }
781
782 static const char * const cmd_subvol_show_usage[] = {
783         "btrfs subvolume show <subvol-path>",
784         "Show more information of the subvolume",
785         NULL
786 };
787
788 static int cmd_subvol_show(int argc, char **argv)
789 {
790         struct root_info get_ri;
791         struct btrfs_list_filter_set *filter_set;
792         char tstr[256];
793         char uuidparse[37];
794         char *fullpath = NULL, *svpath = NULL, *mnt = NULL;
795         char raw_prefix[] = "\t\t\t\t";
796         u64 sv_id, mntid;
797         int fd = -1, mntfd = -1;
798         int ret = 1;
799         DIR *dirstream1 = NULL, *dirstream2 = NULL;
800
801         if (check_argc_exact(argc, 2))
802                 usage(cmd_subvol_show_usage);
803
804         fullpath = realpath(argv[1], NULL);
805         if (!fullpath) {
806                 fprintf(stderr, "ERROR: finding real path for '%s', %s\n",
807                         argv[1], strerror(errno));
808                 goto out;
809         }
810
811         ret = test_issubvolume(fullpath);
812         if (ret < 0) {
813                 fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath);
814                 goto out;
815         }
816         if (!ret) {
817                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath);
818                 goto out;
819         }
820
821         ret = find_mount_root(fullpath, &mnt);
822         if (ret < 0) {
823                 fprintf(stderr, "ERROR: find_mount_root failed on %s: "
824                                 "%s\n", fullpath, strerror(-ret));
825                 goto out;
826         }
827         ret = 1;
828         svpath = get_subvol_name(mnt, fullpath);
829
830         fd = open_file_or_dir(fullpath, &dirstream1);
831         if (fd < 0) {
832                 fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
833                 goto out;
834         }
835
836         ret = btrfs_list_get_path_rootid(fd, &sv_id);
837         if (ret) {
838                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n",
839                         fullpath);
840                 goto out;
841         }
842
843         mntfd = open_file_or_dir(mnt, &dirstream2);
844         if (mntfd < 0) {
845                 fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
846                 goto out;
847         }
848
849         ret = btrfs_list_get_path_rootid(mntfd, &mntid);
850         if (ret) {
851                 fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt);
852                 goto out;
853         }
854
855         if (sv_id == BTRFS_FS_TREE_OBJECTID) {
856                 printf("%s is btrfs root\n", fullpath);
857                 goto out;
858         }
859
860         memset(&get_ri, 0, sizeof(get_ri));
861         get_ri.root_id = sv_id;
862
863         if (btrfs_get_subvol(mntfd, &get_ri)) {
864                 fprintf(stderr, "ERROR: can't find '%s'\n",
865                         svpath);
866                 goto out;
867         }
868
869         ret = 0;
870         /* print the info */
871         printf("%s\n", fullpath);
872         printf("\tName: \t\t\t%s\n", get_ri.name);
873
874         if (uuid_is_null(get_ri.uuid))
875                 strcpy(uuidparse, "-");
876         else
877                 uuid_unparse(get_ri.uuid, uuidparse);
878         printf("\tuuid: \t\t\t%s\n", uuidparse);
879
880         if (uuid_is_null(get_ri.puuid))
881                 strcpy(uuidparse, "-");
882         else
883                 uuid_unparse(get_ri.puuid, uuidparse);
884         printf("\tParent uuid: \t\t%s\n", uuidparse);
885
886         if (get_ri.otime) {
887                 struct tm tm;
888
889                 localtime_r(&get_ri.otime, &tm);
890                 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
891         } else
892                 strcpy(tstr, "-");
893         printf("\tCreation time: \t\t%s\n", tstr);
894
895         printf("\tObject ID: \t\t%llu\n", get_ri.root_id);
896         printf("\tGeneration (Gen): \t%llu\n", get_ri.gen);
897         printf("\tGen at creation: \t%llu\n", get_ri.ogen);
898         printf("\tParent: \t\t%llu\n", get_ri.ref_tree);
899         printf("\tTop Level: \t\t%llu\n", get_ri.top_id);
900
901         if (get_ri.flags & BTRFS_ROOT_SUBVOL_RDONLY)
902                 printf("\tFlags: \t\t\treadonly\n");
903         else
904                 printf("\tFlags: \t\t\t-\n");
905
906         /* print the snapshots of the given subvol if any*/
907         printf("\tSnapshot(s):\n");
908         filter_set = btrfs_list_alloc_filter_set();
909         btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_BY_PARENT,
910                                 (u64)(unsigned long)get_ri.uuid);
911         btrfs_list_setup_print_column(BTRFS_LIST_PATH);
912         btrfs_list_subvols_print(fd, filter_set, NULL, BTRFS_LIST_LAYOUT_RAW,
913                         1, raw_prefix);
914
915         /* clean up */
916         if (get_ri.path)
917                 free(get_ri.path);
918         if (get_ri.name)
919                 free(get_ri.name);
920         if (get_ri.full_path)
921                 free(get_ri.full_path);
922         if (filter_set)
923                 btrfs_list_free_filter_set(filter_set);
924
925 out:
926         close_file_or_dir(fd, dirstream1);
927         close_file_or_dir(mntfd, dirstream2);
928         if (mnt)
929                 free(mnt);
930         if (fullpath)
931                 free(fullpath);
932         return !!ret;
933 }
934
935 const struct cmd_group subvolume_cmd_group = {
936         subvolume_cmd_group_usage, NULL, {
937                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
938                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
939                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
940                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
941                 { "get-default", cmd_subvol_get_default,
942                         cmd_subvol_get_default_usage, NULL, 0 },
943                 { "set-default", cmd_subvol_set_default,
944                         cmd_subvol_set_default_usage, NULL, 0 },
945                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
946                 { "show", cmd_subvol_show, cmd_subvol_show_usage, NULL, 0 },
947                 NULL_CMD_STRUCT
948         }
949 };
950
951 int cmd_subvolume(int argc, char **argv)
952 {
953         return handle_command_group(&subvolume_cmd_group, argc, argv);
954 }