btrfs-progs: check: pass fs_info to check_fs_roots
[platform/upstream/btrfs-progs.git] / cmds-qgroup.c
1 /*
2  * Copyright (C) 2012 STRATO.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21 #include <getopt.h>
22
23 #include "ctree.h"
24 #include "ioctl.h"
25
26 #include "commands.h"
27 #include "qgroup.h"
28 #include "utils.h"
29 #include "help.h"
30
31 static const char * const qgroup_cmd_group_usage[] = {
32         "btrfs qgroup <command> [options] <path>",
33         NULL
34 };
35
36 static int _cmd_qgroup_assign(int assign, int argc, char **argv,
37                 const char * const *usage_str)
38 {
39         int ret = 0;
40         int fd;
41         int rescan = 0;
42         char *path;
43         struct btrfs_ioctl_qgroup_assign_args args;
44         DIR *dirstream = NULL;
45
46         if (assign) {
47                 while (1) {
48                         enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
49                         static const struct option long_options[] = {
50                                 { "rescan", no_argument, NULL,
51                                         GETOPT_VAL_RESCAN },
52                                 { "no-rescan", no_argument, NULL,
53                                         GETOPT_VAL_NO_RESCAN },
54                                 { NULL, 0, NULL, 0 }
55                         };
56                         int c = getopt_long(argc, argv, "", long_options, NULL);
57
58                         if (c < 0)
59                                 break;
60                         switch (c) {
61                         case GETOPT_VAL_RESCAN:
62                                 rescan = 1;
63                                 break;
64                         case GETOPT_VAL_NO_RESCAN:
65                                 rescan = 0;
66                                 break;
67                         default:
68                                 /* Usage printed by the caller */
69                                 return -1;
70                         }
71                 }
72         } else {
73                 clean_args_no_options(argc, argv, usage_str);
74         }
75
76         if (check_argc_exact(argc - optind, 3))
77                 usage(usage_str);
78
79         memset(&args, 0, sizeof(args));
80         args.assign = assign;
81         args.src = parse_qgroupid(argv[optind]);
82         args.dst = parse_qgroupid(argv[optind + 1]);
83
84         path = argv[optind + 2];
85
86         /*
87          * FIXME src should accept subvol path
88          */
89         if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
90                 error("bad relation requested: %s", path);
91                 return 1;
92         }
93         fd = btrfs_open_dir(path, &dirstream, 1);
94         if (fd < 0)
95                 return 1;
96
97         ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
98         if (ret < 0) {
99                 error("unable to assign quota group: %s", strerror(errno));
100                 close_file_or_dir(fd, dirstream);
101                 return 1;
102         }
103
104         /*
105          * If ret > 0, it means assign caused qgroup data inconsistent state.
106          * Schedule a quota rescan if requested.
107          *
108          * The return value change only happens in newer kernel. But will not
109          * cause problem since old kernel has a bug that will never clear
110          * INCONSISTENT bit.
111          */
112         if (ret > 0) {
113                 if (rescan) {
114                         struct btrfs_ioctl_quota_rescan_args qargs;
115
116                         printf("Quota data changed, rescan scheduled\n");
117                         memset(&qargs, 0, sizeof(qargs));
118                         ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
119                         if (ret < 0)
120                                 error("quota rescan failed: %s",
121                                         strerror(errno));
122                 } else {
123                         warning("quotas may be inconsistent, rescan needed");
124                 }
125         }
126         close_file_or_dir(fd, dirstream);
127         return ret;
128 }
129
130 static int _cmd_qgroup_create(int create, int argc, char **argv)
131 {
132         int ret = 0;
133         int fd;
134         int e;
135         char *path;
136         struct btrfs_ioctl_qgroup_create_args args;
137         DIR *dirstream = NULL;
138
139         if (check_argc_exact(argc - optind, 2))
140                 return -1;
141
142         memset(&args, 0, sizeof(args));
143         args.create = create;
144         args.qgroupid = parse_qgroupid(argv[optind]);
145         path = argv[optind + 1];
146
147         fd = btrfs_open_dir(path, &dirstream, 1);
148         if (fd < 0)
149                 return 1;
150
151         ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
152         e = errno;
153         close_file_or_dir(fd, dirstream);
154         if (ret < 0) {
155                 error("unable to %s quota group: %s",
156                         create ? "create":"destroy", strerror(e));
157                 return 1;
158         }
159         return 0;
160 }
161
162 static int parse_limit(const char *p, unsigned long long *s)
163 {
164         char *endptr;
165         unsigned long long size;
166         unsigned long long CLEAR_VALUE = -1;
167
168         if (strcasecmp(p, "none") == 0) {
169                 *s = CLEAR_VALUE;
170                 return 1;
171         }
172
173         if (p[0] == '-')
174                 return 0;
175
176         size = strtoull(p, &endptr, 10);
177         if (p == endptr)
178                 return 0;
179
180         switch (*endptr) {
181         case 'T':
182         case 't':
183                 size *= 1024;
184                 /* fallthrough */
185         case 'G':
186         case 'g':
187                 size *= 1024;
188                 /* fallthrough */
189         case 'M':
190         case 'm':
191                 size *= 1024;
192                 /* fallthrough */
193         case 'K':
194         case 'k':
195                 size *= 1024;
196                 ++endptr;
197                 break;
198         case 0:
199                 break;
200         default:
201                 return 0;
202         }
203
204         if (*endptr)
205                 return 0;
206
207         *s = size;
208
209         return 1;
210 }
211
212 static const char * const cmd_qgroup_assign_usage[] = {
213         "btrfs qgroup assign [options] <src> <dst> <path>",
214         "Assign SRC as the child qgroup of DST",
215         "",
216         "--rescan       schedule qutoa rescan if needed",
217         "--no-rescan    don't schedule quota rescan",
218         NULL
219 };
220
221 static int cmd_qgroup_assign(int argc, char **argv)
222 {
223         return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
224 }
225
226 static const char * const cmd_qgroup_remove_usage[] = {
227         "btrfs qgroup remove <src> <dst> <path>",
228         "Remove a child qgroup SRC from DST.",
229         NULL
230 };
231
232 static int cmd_qgroup_remove(int argc, char **argv)
233 {
234         return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
235 }
236
237 static const char * const cmd_qgroup_create_usage[] = {
238         "btrfs qgroup create <qgroupid> <path>",
239         "Create a subvolume quota group.",
240         NULL
241 };
242
243 static int cmd_qgroup_create(int argc, char **argv)
244 {
245         int ret;
246
247         clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
248
249         ret = _cmd_qgroup_create(1, argc, argv);
250
251         if (ret < 0)
252                 usage(cmd_qgroup_create_usage);
253         return ret;
254 }
255
256 static const char * const cmd_qgroup_destroy_usage[] = {
257         "btrfs qgroup destroy <qgroupid> <path>",
258         "Destroy a quota group.",
259         NULL
260 };
261
262 static int cmd_qgroup_destroy(int argc, char **argv)
263 {
264         int ret;
265
266         clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
267
268         ret = _cmd_qgroup_create(0, argc, argv);
269
270         if (ret < 0)
271                 usage(cmd_qgroup_destroy_usage);
272         return ret;
273 }
274
275 static const char * const cmd_qgroup_show_usage[] = {
276         "btrfs qgroup show [options] <path>",
277         "Show subvolume quota groups.",
278         "-p             print parent qgroup id",
279         "-c             print child qgroup id",
280         "-r             print limit of referenced size of qgroup",
281         "-e             print limit of exclusive size of qgroup",
282         "-F             list all qgroups which impact the given path",
283         "               (including ancestral qgroups)",
284         "-f             list all qgroups which impact the given path",
285         "               (excluding ancestral qgroups)",
286         HELPINFO_UNITS_LONG,
287         "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
288         "               list qgroups sorted by specified items",
289         "               you can use '+' or '-' in front of each item.",
290         "               (+:ascending, -:descending, ascending default)",
291         "--sync         force sync of the filesystem before getting info",
292         NULL
293 };
294
295 static int cmd_qgroup_show(int argc, char **argv)
296 {
297         char *path;
298         int ret = 0;
299         int fd;
300         DIR *dirstream = NULL;
301         u64 qgroupid;
302         int filter_flag = 0;
303         unsigned unit_mode;
304         int sync = 0;
305
306         struct btrfs_qgroup_comparer_set *comparer_set;
307         struct btrfs_qgroup_filter_set *filter_set;
308         filter_set = btrfs_qgroup_alloc_filter_set();
309         comparer_set = btrfs_qgroup_alloc_comparer_set();
310
311         unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
312
313         while (1) {
314                 int c;
315                 enum {
316                         GETOPT_VAL_SORT = 256,
317                         GETOPT_VAL_SYNC
318                 };
319                 static const struct option long_options[] = {
320                         {"sort", required_argument, NULL, GETOPT_VAL_SORT},
321                         {"sync", no_argument, NULL, GETOPT_VAL_SYNC},
322                         { NULL, 0, NULL, 0 }
323                 };
324
325                 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
326                 if (c < 0)
327                         break;
328                 switch (c) {
329                 case 'p':
330                         btrfs_qgroup_setup_print_column(
331                                 BTRFS_QGROUP_PARENT);
332                         break;
333                 case 'c':
334                         btrfs_qgroup_setup_print_column(
335                                 BTRFS_QGROUP_CHILD);
336                         break;
337                 case 'r':
338                         btrfs_qgroup_setup_print_column(
339                                 BTRFS_QGROUP_MAX_RFER);
340                         break;
341                 case 'e':
342                         btrfs_qgroup_setup_print_column(
343                                 BTRFS_QGROUP_MAX_EXCL);
344                         break;
345                 case 'F':
346                         filter_flag |= 0x1;
347                         break;
348                 case 'f':
349                         filter_flag |= 0x2;
350                         break;
351                 case GETOPT_VAL_SORT:
352                         ret = btrfs_qgroup_parse_sort_string(optarg,
353                                                              &comparer_set);
354                         if (ret)
355                                 usage(cmd_qgroup_show_usage);
356                         break;
357                 case GETOPT_VAL_SYNC:
358                         sync = 1;
359                         break;
360                 default:
361                         usage(cmd_qgroup_show_usage);
362                 }
363         }
364         btrfs_qgroup_setup_units(unit_mode);
365
366         if (check_argc_exact(argc - optind, 1))
367                 usage(cmd_qgroup_show_usage);
368
369         path = argv[optind];
370         fd = btrfs_open_dir(path, &dirstream, 1);
371         if (fd < 0) {
372                 free(filter_set);
373                 free(comparer_set);
374                 return 1;
375         }
376
377         if (sync) {
378                 ret = ioctl(fd, BTRFS_IOC_SYNC);
379                 if (ret < 0)
380                         warning("sync ioctl failed on '%s': %s", path,
381                               strerror(errno));
382         }
383
384         if (filter_flag) {
385                 ret = lookup_path_rootid(fd, &qgroupid);
386                 if (ret < 0) {
387                         error("cannot resolve rootid for %s: %s",
388                                         path, strerror(-ret));
389                         close_file_or_dir(fd, dirstream);
390                         goto out;
391                 }
392                 if (filter_flag & 0x1)
393                         btrfs_qgroup_setup_filter(&filter_set,
394                                         BTRFS_QGROUP_FILTER_ALL_PARENT,
395                                         qgroupid);
396                 if (filter_flag & 0x2)
397                         btrfs_qgroup_setup_filter(&filter_set,
398                                         BTRFS_QGROUP_FILTER_PARENT,
399                                         qgroupid);
400         }
401         ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
402         if (ret == -ENOENT)
403                 error("can't list qgroups: quotas not enabled");
404         else if (ret < 0)
405                 error("can't list qgroups: %s", strerror(-ret));
406         close_file_or_dir(fd, dirstream);
407
408 out:
409         return !!ret;
410 }
411
412 static const char * const cmd_qgroup_limit_usage[] = {
413         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
414         "Set the limits a subvolume quota group.",
415         "",
416         "-c   limit amount of data after compression. This is the default,",
417         "     it is currently not possible to turn off this option.",
418         "-e   limit space exclusively assigned to this qgroup",
419         NULL
420 };
421
422 static int cmd_qgroup_limit(int argc, char **argv)
423 {
424         int ret = 0;
425         int fd;
426         int e;
427         char *path = NULL;
428         struct btrfs_ioctl_qgroup_limit_args args;
429         unsigned long long size;
430         int compressed = 0;
431         int exclusive = 0;
432         DIR *dirstream = NULL;
433
434         while (1) {
435                 int c = getopt(argc, argv, "ce");
436                 if (c < 0)
437                         break;
438                 switch (c) {
439                 case 'c':
440                         compressed = 1;
441                         break;
442                 case 'e':
443                         exclusive = 1;
444                         break;
445                 default:
446                         usage(cmd_qgroup_limit_usage);
447                 }
448         }
449
450         if (check_argc_min(argc - optind, 2))
451                 usage(cmd_qgroup_limit_usage);
452
453         if (!parse_limit(argv[optind], &size)) {
454                 error("invalid size argument: %s", argv[optind]);
455                 return 1;
456         }
457
458         memset(&args, 0, sizeof(args));
459         if (compressed)
460                 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
461                                   BTRFS_QGROUP_LIMIT_EXCL_CMPR;
462         if (exclusive) {
463                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
464                 args.lim.max_exclusive = size;
465         } else {
466                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
467                 args.lim.max_referenced = size;
468         }
469
470         if (argc - optind == 2) {
471                 args.qgroupid = 0;
472                 path = argv[optind + 1];
473                 ret = test_issubvolume(path);
474                 if (ret < 0) {
475                         error("cannot access '%s': %s", path, strerror(-ret));
476                         return 1;
477                 }
478                 if (!ret) {
479                         error("'%s' is not a subvolume", path);
480                         return 1;
481                 }
482                 /*
483                  * keep qgroupid at 0, this indicates that the subvolume the
484                  * fd refers to is to be limited
485                  */
486         } else if (argc - optind == 3) {
487                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
488                 path = argv[optind + 2];
489         } else
490                 usage(cmd_qgroup_limit_usage);
491
492         fd = btrfs_open_dir(path, &dirstream, 1);
493         if (fd < 0)
494                 return 1;
495
496         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
497         e = errno;
498         close_file_or_dir(fd, dirstream);
499         if (ret < 0) {
500                 error("unable to limit requested quota group: %s", strerror(e));
501                 return 1;
502         }
503         return 0;
504 }
505
506 static const char qgroup_cmd_group_info[] =
507 "manage quota groups";
508
509 const struct cmd_group qgroup_cmd_group = {
510         qgroup_cmd_group_usage, qgroup_cmd_group_info, {
511                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
512                    NULL, 0 },
513                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
514                    NULL, 0 },
515                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
516                    NULL, 0 },
517                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
518                    NULL, 0 },
519                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
520                    NULL, 0 },
521                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
522                    NULL, 0 },
523                 NULL_CMD_STRUCT
524         }
525 };
526
527 int cmd_qgroup(int argc, char **argv)
528 {
529         return handle_command_group(&qgroup_cmd_group, argc, argv);
530 }