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