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