btrfs-progs: Avoid interpreting options after "--" when getting unit mode
[btrfs-progs-unstable/devel.git] / cmds-qgroup.c
blobc4a0c6b05ce8714105ff21ab0f97de7e46ccdbc2
1 /*
2 * Copyright (C) 2012 STRATO. All rights reserved.
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.
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.
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.
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21 #include <getopt.h>
23 #include "ctree.h"
24 #include "ioctl.h"
26 #include "commands.h"
27 #include "qgroup.h"
28 #include "utils.h"
30 static const char * const qgroup_cmd_group_usage[] = {
31 "btrfs qgroup <command> [options] <path>",
32 NULL
35 static int _cmd_qgroup_assign(int assign, int argc, char **argv)
37 int ret = 0;
38 int fd;
39 int rescan = 0;
40 char *path;
41 struct btrfs_ioctl_qgroup_assign_args args;
42 DIR *dirstream = NULL;
44 while (1) {
45 enum { GETOPT_VAL_RESCAN = 256 };
46 static const struct option long_options[] = {
47 { "rescan", no_argument, NULL, GETOPT_VAL_RESCAN },
48 { NULL, 0, NULL, 0 }
50 int c = getopt_long(argc, argv, "", long_options, NULL);
52 if (c < 0)
53 break;
54 switch (c) {
55 case GETOPT_VAL_RESCAN:
56 rescan = 1;
57 break;
58 default:
59 /* Usage printed by the caller */
60 return -1;
64 if (check_argc_exact(argc - optind, 3))
65 return -1;
67 memset(&args, 0, sizeof(args));
68 args.assign = assign;
69 args.src = parse_qgroupid(argv[optind]);
70 args.dst = parse_qgroupid(argv[optind + 1]);
72 path = argv[optind + 2];
75 * FIXME src should accept subvol path
77 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
78 error("bad relation requested: %s", path);
79 return 1;
81 fd = btrfs_open_dir(path, &dirstream, 1);
82 if (fd < 0)
83 return 1;
85 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
86 if (ret < 0) {
87 error("unable to assign quota group: %s", strerror(errno));
88 close_file_or_dir(fd, dirstream);
89 return 1;
93 * If ret > 0, it means assign caused qgroup data inconsistent state.
94 * Schedule a quota rescan if requested.
96 * The return value change only happens in newer kernel. But will not
97 * cause problem since old kernel has a bug that will never clear
98 * INCONSISTENT bit.
100 if (ret > 0) {
101 if (rescan) {
102 struct btrfs_ioctl_quota_rescan_args qargs;
104 printf("Quota data changed, rescan scheduled\n");
105 memset(&qargs, 0, sizeof(qargs));
106 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
107 if (ret < 0)
108 error("quota rescan failed: %s",
109 strerror(errno));
110 } else {
111 warning("quotas may be inconsistent, rescan needed");
114 close_file_or_dir(fd, dirstream);
115 return ret;
118 static int _cmd_qgroup_create(int create, int argc, char **argv)
120 int ret = 0;
121 int fd;
122 int e;
123 char *path;
124 struct btrfs_ioctl_qgroup_create_args args;
125 DIR *dirstream = NULL;
127 if (check_argc_exact(argc - optind, 3))
128 return -1;
130 memset(&args, 0, sizeof(args));
131 args.create = create;
132 args.qgroupid = parse_qgroupid(argv[optind]);
133 path = argv[optind + 1];
135 fd = btrfs_open_dir(path, &dirstream, 1);
136 if (fd < 0)
137 return 1;
139 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
140 e = errno;
141 close_file_or_dir(fd, dirstream);
142 if (ret < 0) {
143 error("unable to %s quota group: %s",
144 create ? "create":"destroy", strerror(e));
145 return 1;
147 return 0;
150 static int parse_limit(const char *p, unsigned long long *s)
152 char *endptr;
153 unsigned long long size;
154 unsigned long long CLEAR_VALUE = -1;
156 if (strcasecmp(p, "none") == 0) {
157 *s = CLEAR_VALUE;
158 return 1;
161 if (p[0] == '-')
162 return 0;
164 size = strtoull(p, &endptr, 10);
165 if (p == endptr)
166 return 0;
168 switch (*endptr) {
169 case 'T':
170 case 't':
171 size *= 1024;
172 /* fallthrough */
173 case 'G':
174 case 'g':
175 size *= 1024;
176 /* fallthrough */
177 case 'M':
178 case 'm':
179 size *= 1024;
180 /* fallthrough */
181 case 'K':
182 case 'k':
183 size *= 1024;
184 ++endptr;
185 break;
186 case 0:
187 break;
188 default:
189 return 0;
192 if (*endptr)
193 return 0;
195 *s = size;
197 return 1;
200 static const char * const cmd_qgroup_assign_usage[] = {
201 "btrfs qgroup assign [options] <src> <dst> <path>",
202 "Assign SRC as the child qgroup of DST",
204 "--rescan schedule qutoa rescan if needed",
205 "--no-rescan ",
206 NULL
209 static int cmd_qgroup_assign(int argc, char **argv)
211 int ret;
213 clean_args_no_options(argc, argv, cmd_qgroup_assign_usage);
215 ret = _cmd_qgroup_assign(1, argc, argv);
217 if (ret < 0)
218 usage(cmd_qgroup_assign_usage);
219 return ret;
222 static const char * const cmd_qgroup_remove_usage[] = {
223 "btrfs qgroup remove <src> <dst> <path>",
224 "Remove a child qgroup SRC from DST.",
225 NULL
228 static int cmd_qgroup_remove(int argc, char **argv)
230 int ret;
232 clean_args_no_options(argc, argv, cmd_qgroup_remove_usage);
234 ret = _cmd_qgroup_assign(0, argc, argv);
236 if (ret < 0)
237 usage(cmd_qgroup_remove_usage);
238 return ret;
241 static const char * const cmd_qgroup_create_usage[] = {
242 "btrfs qgroup create <qgroupid> <path>",
243 "Create a subvolume quota group.",
244 NULL
247 static int cmd_qgroup_create(int argc, char **argv)
249 int ret;
251 clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
253 ret = _cmd_qgroup_create(1, argc, argv);
255 if (ret < 0)
256 usage(cmd_qgroup_create_usage);
257 return ret;
260 static const char * const cmd_qgroup_destroy_usage[] = {
261 "btrfs qgroup destroy <qgroupid> <path>",
262 "Destroy a quota group.",
263 NULL
266 static int cmd_qgroup_destroy(int argc, char **argv)
268 int ret;
270 clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
272 ret = _cmd_qgroup_create(0, argc, argv);
274 if (ret < 0)
275 usage(cmd_qgroup_destroy_usage);
276 return ret;
279 static const char * const cmd_qgroup_show_usage[] = {
280 "btrfs qgroup show -pcreFf "
281 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
282 "Show subvolume quota groups.",
283 "-p print parent qgroup id",
284 "-c print child qgroup id",
285 "-r print limit of referenced size of qgroup",
286 "-e print limit of exclusive size of qgroup",
287 "-F list all qgroups which impact the given path",
288 " (including ancestral qgroups)",
289 "-f list all qgroups which impact the given path",
290 " (excluding ancestral qgroups)",
291 HELPINFO_UNITS_LONG,
292 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
293 " list qgroups sorted by specified items",
294 " you can use '+' or '-' in front of each item.",
295 " (+:ascending, -:descending, ascending default)",
296 NULL
299 static int cmd_qgroup_show(int argc, char **argv)
301 char *path;
302 int ret = 0;
303 int fd;
304 int e;
305 DIR *dirstream = NULL;
306 u64 qgroupid;
307 int filter_flag = 0;
308 unsigned unit_mode;
310 struct btrfs_qgroup_comparer_set *comparer_set;
311 struct btrfs_qgroup_filter_set *filter_set;
312 filter_set = btrfs_qgroup_alloc_filter_set();
313 comparer_set = btrfs_qgroup_alloc_comparer_set();
315 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
317 optind = 1;
318 while (1) {
319 int c;
320 static const struct option long_options[] = {
321 {"sort", required_argument, NULL, 'S'},
322 { NULL, 0, NULL, 0 }
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 'S':
352 ret = btrfs_qgroup_parse_sort_string(optarg,
353 &comparer_set);
354 if (ret)
355 usage(cmd_qgroup_show_usage);
356 break;
357 default:
358 usage(cmd_qgroup_show_usage);
361 btrfs_qgroup_setup_units(unit_mode);
363 if (check_argc_exact(argc - optind, 1))
364 usage(cmd_qgroup_show_usage);
366 path = argv[optind];
367 fd = btrfs_open_dir(path, &dirstream, 1);
368 if (fd < 0) {
369 btrfs_qgroup_free_filter_set(filter_set);
370 btrfs_qgroup_free_comparer_set(comparer_set);
371 return 1;
374 if (filter_flag) {
375 qgroupid = btrfs_get_path_rootid(fd);
376 if (filter_flag & 0x1)
377 btrfs_qgroup_setup_filter(&filter_set,
378 BTRFS_QGROUP_FILTER_ALL_PARENT,
379 qgroupid);
380 if (filter_flag & 0x2)
381 btrfs_qgroup_setup_filter(&filter_set,
382 BTRFS_QGROUP_FILTER_PARENT,
383 qgroupid);
385 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
386 e = errno;
387 close_file_or_dir(fd, dirstream);
388 if (ret < 0)
389 error("can't list qgroups: %s", strerror(e));
391 return !!ret;
394 static const char * const cmd_qgroup_limit_usage[] = {
395 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
396 "Set the limits a subvolume quota group.",
398 "-c limit amount of data after compression. This is the default,",
399 " it is currently not possible to turn off this option.",
400 "-e limit space exclusively assigned to this qgroup",
401 NULL
404 static int cmd_qgroup_limit(int argc, char **argv)
406 int ret = 0;
407 int fd;
408 int e;
409 char *path = NULL;
410 struct btrfs_ioctl_qgroup_limit_args args;
411 unsigned long long size;
412 int compressed = 0;
413 int exclusive = 0;
414 DIR *dirstream = NULL;
416 optind = 1;
417 while (1) {
418 int c = getopt(argc, argv, "ce");
419 if (c < 0)
420 break;
421 switch (c) {
422 case 'c':
423 compressed = 1;
424 break;
425 case 'e':
426 exclusive = 1;
427 break;
428 default:
429 usage(cmd_qgroup_limit_usage);
433 if (check_argc_min(argc - optind, 2))
434 usage(cmd_qgroup_limit_usage);
436 if (!parse_limit(argv[optind], &size)) {
437 error("invalid size argument: %s", argv[optind]);
438 return 1;
441 memset(&args, 0, sizeof(args));
442 if (compressed)
443 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
444 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
445 if (exclusive) {
446 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
447 args.lim.max_exclusive = size;
448 } else {
449 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
450 args.lim.max_referenced = size;
453 if (argc - optind == 2) {
454 args.qgroupid = 0;
455 path = argv[optind + 1];
456 ret = test_issubvolume(path);
457 if (ret < 0) {
458 error("cannot access '%s': %s", path, strerror(-ret));
459 return 1;
461 if (!ret) {
462 error("'%s' is not a subvolume", path);
463 return 1;
466 * keep qgroupid at 0, this indicates that the subvolume the
467 * fd refers to is to be limited
469 } else if (argc - optind == 3) {
470 args.qgroupid = parse_qgroupid(argv[optind + 1]);
471 path = argv[optind + 2];
472 } else
473 usage(cmd_qgroup_limit_usage);
475 fd = btrfs_open_dir(path, &dirstream, 1);
476 if (fd < 0)
477 return 1;
479 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
480 e = errno;
481 close_file_or_dir(fd, dirstream);
482 if (ret < 0) {
483 error("unable to limit requested quota group: %s", strerror(e));
484 return 1;
486 return 0;
489 static const char qgroup_cmd_group_info[] =
490 "manage quota groups";
492 const struct cmd_group qgroup_cmd_group = {
493 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
494 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
495 NULL, 0 },
496 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
497 NULL, 0 },
498 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
499 NULL, 0 },
500 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
501 NULL, 0 },
502 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
503 NULL, 0 },
504 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
505 NULL, 0 },
506 NULL_CMD_STRUCT
510 int cmd_qgroup(int argc, char **argv)
512 return handle_command_group(&qgroup_cmd_group, argc, argv);