Btrfs-progs: add -u to show subvol uuid
[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
27 #include "kerncompat.h"
28 #include "ioctl.h"
29 #include "qgroup.h"
30
31 #include "commands.h"
32
33 /* btrfs-list.c */
34 int list_subvols(int fd, int print_parent, int print_uuid, int get_default);
35 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
36
37 static const char * const subvolume_cmd_group_usage[] = {
38         "btrfs subvolume <command> <args>",
39         NULL
40 };
41
42 /*
43  * test if path is a directory
44  * this function return
45  * 0-> path exists but it is not a directory
46  * 1-> path exists and it is  a directory
47  * -1 -> path is unaccessible
48  */
49 static int test_isdir(char *path)
50 {
51         struct stat     st;
52         int             res;
53
54         res = stat(path, &st);
55         if(res < 0 )
56                 return -1;
57
58         return S_ISDIR(st.st_mode);
59 }
60
61 static const char * const cmd_subvol_create_usage[] = {
62         "btrfs subvolume create [<dest>/]<name>",
63         "Create a subvolume",
64         "Create a subvolume <name> in <dest>.  If <dest> is not given",
65         "subvolume <name> will be created in the current directory.",
66         NULL
67 };
68
69 static int cmd_subvol_create(int argc, char **argv)
70 {
71         int     res, fddst, len, e;
72         char    *newname;
73         char    *dstdir;
74         char    *dst;
75         struct btrfs_qgroup_inherit *inherit = NULL;
76
77         optind = 1;
78         while (1) {
79                 int c = getopt(argc, argv, "c:i:r");
80                 if (c < 0)
81                         break;
82
83                 switch (c) {
84                 case 'c':
85                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
86                         if (res)
87                                 return res;
88                         break;
89                 case 'i':
90                         res = qgroup_inherit_add_group(&inherit, optarg);
91                         if (res)
92                                 return res;
93                         break;
94                 default:
95                         usage(cmd_subvol_create_usage);
96                 }
97         }
98
99         if (check_argc_exact(argc - optind, 1))
100                 usage(cmd_subvol_create_usage);
101
102         dst = argv[optind];
103
104         res = test_isdir(dst);
105         if(res >= 0 ){
106                 fprintf(stderr, "ERROR: '%s' exists\n", dst);
107                 return 12;
108         }
109
110         newname = strdup(dst);
111         newname = basename(newname);
112         dstdir = strdup(dst);
113         dstdir = dirname(dstdir);
114
115         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
116              strchr(newname, '/') ){
117                 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
118                         newname);
119                 return 14;
120         }
121
122         len = strlen(newname);
123         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
124                 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
125                         newname);
126                 return 14;
127         }
128
129         fddst = open_file_or_dir(dstdir);
130         if (fddst < 0) {
131                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
132                 return 12;
133         }
134
135         printf("Create subvolume '%s/%s'\n", dstdir, newname);
136         if (inherit) {
137                 struct btrfs_ioctl_vol_args_v2  args;
138
139                 memset(&args, 0, sizeof(args));
140                 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
141                 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
142                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
143                 args.size = qgroup_inherit_size(inherit);
144                 args.qgroup_inherit = inherit;
145
146                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
147         } else {
148                 struct btrfs_ioctl_vol_args     args;
149
150                 memset(&args, 0, sizeof(args));
151                 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
152                 args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
153
154                 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
155         }
156
157         e = errno;
158
159         close(fddst);
160
161         if(res < 0 ){
162                 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
163                         strerror(e));
164                 return 11;
165         }
166         free(inherit);
167
168         return 0;
169 }
170
171 /*
172  * test if path is a subvolume:
173  * this function return
174  * 0-> path exists but it is not a subvolume
175  * 1-> path exists and it is  a subvolume
176  * -1 -> path is unaccessible
177  */
178 int test_issubvolume(char *path)
179 {
180         struct stat     st;
181         int             res;
182
183         res = stat(path, &st);
184         if(res < 0 )
185                 return -1;
186
187         return (st.st_ino == 256) && S_ISDIR(st.st_mode);
188 }
189
190 static const char * const cmd_subvol_delete_usage[] = {
191         "btrfs subvolume delete <name>",
192         "Delete a subvolume",
193         NULL
194 };
195
196 static int cmd_subvol_delete(int argc, char **argv)
197 {
198         int     res, fd, len, e;
199         struct btrfs_ioctl_vol_args     args;
200         char    *dname, *vname, *cpath;
201         char    *path;
202
203         if (check_argc_exact(argc, 2))
204                 usage(cmd_subvol_delete_usage);
205
206         path = argv[1];
207
208         res = test_issubvolume(path);
209         if(res<0){
210                 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
211                 return 12;
212         }
213         if(!res){
214                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
215                 return 13;
216         }
217
218         cpath = realpath(path, 0);
219         dname = strdup(cpath);
220         dname = dirname(dname);
221         vname = strdup(cpath);
222         vname = basename(vname);
223         free(cpath);
224
225         if( !strcmp(vname,".") || !strcmp(vname,"..") ||
226              strchr(vname, '/') ){
227                 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
228                         vname);
229                 return 14;
230         }
231
232         len = strlen(vname);
233         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
234                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
235                         vname);
236                 return 14;
237         }
238
239         fd = open_file_or_dir(dname);
240         if (fd < 0) {
241                 close(fd);
242                 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
243                 return 12;
244         }
245
246         printf("Delete subvolume '%s/%s'\n", dname, vname);
247         strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
248         args.name[BTRFS_PATH_NAME_MAX-1] = 0;
249         res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
250         e = errno;
251
252         close(fd);
253
254         if(res < 0 ){
255                 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
256                         dname, vname, strerror(e));
257                 return 11;
258         }
259
260         return 0;
261 }
262
263 static const char * const cmd_subvol_list_usage[] = {
264         "btrfs subvolume list [-ps] <path>",
265         "List subvolumes (and snapshots)",
266         "",
267         "-p           print parent ID",
268         "-s value     list snapshots with generation in ascending/descending order",
269         "             (1: ascending, 0: descending)",
270         NULL
271 };
272
273 static int cmd_subvol_list(int argc, char **argv)
274 {
275         int fd;
276         int ret;
277         int print_parent = 0;
278         int print_snap_only = 0;
279         int order = 0;
280         char *subvol;
281         int print_uuid = 0;
282
283         optind = 1;
284         while(1) {
285                 int c = getopt(argc, argv, "ps:u");
286                 if (c < 0)
287                         break;
288
289                 switch(c) {
290                 case 'p':
291                         print_parent = 1;
292                         break;
293                 case 's':
294                         print_snap_only = 1;
295                         order = atoi(optarg);
296                         break;
297                 case 'u':
298                         print_uuid =1;
299                         break;
300                 default:
301                         usage(cmd_subvol_list_usage);
302                 }
303         }
304
305         if (check_argc_exact(argc - optind, 1))
306                 usage(cmd_subvol_list_usage);
307
308         subvol = argv[optind];
309
310         ret = test_issubvolume(subvol);
311         if (ret < 0) {
312                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
313                 return 12;
314         }
315         if (!ret) {
316                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
317                 return 13;
318         }
319
320         fd = open_file_or_dir(subvol);
321         if (fd < 0) {
322                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
323                 return 12;
324         }
325         if (!print_snap_only)
326                 ret = list_subvols(fd, print_parent, 0, print_uuid);
327         else
328                 ret = list_snapshots(fd, print_parent, order, print_uuid);
329         if (ret)
330                 return 19;
331         return 0;
332 }
333
334 static const char * const cmd_snapshot_usage[] = {
335         "btrfs subvolume snapshot [-r] <source> [<dest>/]<name>",
336         "Create a snapshot of the subvolume",
337         "Create a writable/readonly snapshot of the subvolume <source> with",
338         "the name <name> in the <dest> directory",
339         "",
340         "-r     create a readonly snapshot",
341         NULL
342 };
343
344 static int cmd_snapshot(int argc, char **argv)
345 {
346         char    *subvol, *dst;
347         int     res, fd, fddst, len, e, readonly = 0;
348         char    *newname;
349         char    *dstdir;
350         struct btrfs_ioctl_vol_args_v2  args;
351         struct btrfs_qgroup_inherit *inherit = NULL;
352
353         optind = 1;
354         memset(&args, 0, sizeof(args));
355         while (1) {
356                 int c = getopt(argc, argv, "c:i:r");
357                 if (c < 0)
358                         break;
359
360                 switch (c) {
361                 case 'c':
362                         res = qgroup_inherit_add_copy(&inherit, optarg, 0);
363                         if (res)
364                                 return res;
365                         break;
366                 case 'i':
367                         res = qgroup_inherit_add_group(&inherit, optarg);
368                         if (res)
369                                 return res;
370                         break;
371                 case 'r':
372                         readonly = 1;
373                         break;
374                 case 'x':
375                         res = qgroup_inherit_add_copy(&inherit, optarg, 1);
376                         if (res)
377                                 return res;
378                         break;
379                 default:
380                         usage(cmd_snapshot_usage);
381                 }
382         }
383
384         if (check_argc_exact(argc - optind, 2))
385                 usage(cmd_snapshot_usage);
386
387         subvol = argv[optind];
388         dst = argv[optind + 1];
389
390         res = test_issubvolume(subvol);
391         if(res<0){
392                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
393                 return 12;
394         }
395         if(!res){
396                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
397                 return 13;
398         }
399
400         res = test_isdir(dst);
401         if(res == 0 ){
402                 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
403                 return 12;
404         }
405
406         if(res>0){
407                 newname = strdup(subvol);
408                 newname = basename(newname);
409                 dstdir = dst;
410         }else{
411                 newname = strdup(dst);
412                 newname = basename(newname);
413                 dstdir = strdup(dst);
414                 dstdir = dirname(dstdir);
415         }
416
417         if( !strcmp(newname,".") || !strcmp(newname,"..") ||
418              strchr(newname, '/') ){
419                 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
420                         newname);
421                 return 14;
422         }
423
424         len = strlen(newname);
425         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
426                 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
427                         newname);
428                 return 14;
429         }
430
431         fddst = open_file_or_dir(dstdir);
432         if (fddst < 0) {
433                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
434                 return 12;
435         }
436
437         fd = open_file_or_dir(subvol);
438         if (fd < 0) {
439                 close(fddst);
440                 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
441                 return 12;
442         }
443
444         if (readonly) {
445                 args.flags |= BTRFS_SUBVOL_RDONLY;
446                 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
447                        subvol, dstdir, newname);
448         } else {
449                 printf("Create a snapshot of '%s' in '%s/%s'\n",
450                        subvol, dstdir, newname);
451         }
452
453         args.fd = fd;
454         if (inherit) {
455                 args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
456                 args.size = qgroup_inherit_size(inherit);
457                 args.qgroup_inherit = inherit;
458         }
459         strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
460         args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
461         res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
462         e = errno;
463
464         close(fd);
465         close(fddst);
466
467         if(res < 0 ){
468                 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
469                         subvol, strerror(e));
470                 return 11;
471         }
472         free(inherit);
473
474         return 0;
475 }
476
477 static const char * const cmd_subvol_get_default_usage[] = {
478         "btrfs subvolume get-default <path>",
479         "Get the default subvolume of a filesystem",
480         NULL
481 };
482
483 static int cmd_subvol_get_default(int argc, char **argv)
484 {
485         int fd;
486         int ret;
487         char *subvol;
488
489         if (check_argc_exact(argc, 2))
490                 usage(cmd_subvol_get_default_usage);
491
492         subvol = argv[1];
493
494         ret = test_issubvolume(subvol);
495         if (ret < 0) {
496                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
497                 return 12;
498         }
499         if (!ret) {
500                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
501                 return 13;
502         }
503
504         fd = open_file_or_dir(subvol);
505         if (fd < 0) {
506                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
507                 return 12;
508         }
509         ret = list_subvols(fd, 0, 1, 0);
510         if (ret)
511                 return 19;
512         return 0;
513 }
514
515 static const char * const cmd_subvol_set_default_usage[] = {
516         "btrfs subvolume set-default <subvolid> <path>",
517         "Set the default subvolume of a filesystem",
518         NULL
519 };
520
521 static int cmd_subvol_set_default(int argc, char **argv)
522 {
523         int     ret=0, fd, e;
524         u64     objectid;
525         char    *path;
526         char    *subvolid;
527
528         if (check_argc_exact(argc, 3))
529                 usage(cmd_subvol_set_default_usage);
530
531         subvolid = argv[1];
532         path = argv[2];
533
534         fd = open_file_or_dir(path);
535         if (fd < 0) {
536                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
537                 return 12;
538         }
539
540         objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
541         if (errno == ERANGE) {
542                 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
543                 return 30;
544         }
545         ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
546         e = errno;
547         close(fd);
548         if( ret < 0 ){
549                 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
550                         strerror(e));
551                 return 30;
552         }
553         return 0;
554 }
555
556 static const char * const cmd_find_new_usage[] = {
557         "btrfs subvolume find-new <path> <lastgen>",
558         "List the recently modified files in a filesystem",
559         NULL
560 };
561
562 static int cmd_find_new(int argc, char **argv)
563 {
564         int fd;
565         int ret;
566         char *subvol;
567         u64 last_gen;
568
569         if (check_argc_exact(argc, 3))
570                 usage(cmd_find_new_usage);
571
572         subvol = argv[1];
573         last_gen = atoll(argv[2]);
574
575         ret = test_issubvolume(subvol);
576         if (ret < 0) {
577                 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
578                 return 12;
579         }
580         if (!ret) {
581                 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
582                 return 13;
583         }
584
585         fd = open_file_or_dir(subvol);
586         if (fd < 0) {
587                 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
588                 return 12;
589         }
590         ret = find_updated_files(fd, 0, last_gen);
591         if (ret)
592                 return 19;
593         return 0;
594 }
595
596 const struct cmd_group subvolume_cmd_group = {
597         subvolume_cmd_group_usage, NULL, {
598                 { "create", cmd_subvol_create, cmd_subvol_create_usage, NULL, 0 },
599                 { "delete", cmd_subvol_delete, cmd_subvol_delete_usage, NULL, 0 },
600                 { "list", cmd_subvol_list, cmd_subvol_list_usage, NULL, 0 },
601                 { "snapshot", cmd_snapshot, cmd_snapshot_usage, NULL, 0 },
602                 { "get-default", cmd_subvol_get_default,
603                         cmd_subvol_get_default_usage, NULL, 0 },
604                 { "set-default", cmd_subvol_set_default,
605                         cmd_subvol_set_default_usage, NULL, 0 },
606                 { "find-new", cmd_find_new, cmd_find_new_usage, NULL, 0 },
607                 { 0, 0, 0, 0, 0 }
608         }
609 };
610
611 int cmd_subvolume(int argc, char **argv)
612 {
613         return handle_command_group(&subvolume_cmd_group, argc, argv);
614 }