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>
30 static const char * const qgroup_cmd_group_usage[] = {
31 "btrfs qgroup <command> [options] <path>",
35 static int _cmd_qgroup_assign(int assign, int argc, char **argv,
36 const char * const *usage_str)
42 struct btrfs_ioctl_qgroup_assign_args args;
43 DIR *dirstream = NULL;
47 enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
48 static const struct option long_options[] = {
49 { "rescan", no_argument, NULL,
51 { "no-rescan", no_argument, NULL,
52 GETOPT_VAL_NO_RESCAN },
55 int c = getopt_long(argc, argv, "", long_options, NULL);
60 case GETOPT_VAL_RESCAN:
63 case GETOPT_VAL_NO_RESCAN:
67 /* Usage printed by the caller */
72 clean_args_no_options(argc, argv, usage_str);
75 if (check_argc_exact(argc - optind, 3))
78 memset(&args, 0, sizeof(args));
80 args.src = parse_qgroupid(argv[optind]);
81 args.dst = parse_qgroupid(argv[optind + 1]);
83 path = argv[optind + 2];
86 * FIXME src should accept subvol path
88 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
89 error("bad relation requested: %s", path);
92 fd = btrfs_open_dir(path, &dirstream, 1);
96 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
98 error("unable to assign quota group: %s", strerror(errno));
99 close_file_or_dir(fd, dirstream);
104 * If ret > 0, it means assign caused qgroup data inconsistent state.
105 * Schedule a quota rescan if requested.
107 * The return value change only happens in newer kernel. But will not
108 * cause problem since old kernel has a bug that will never clear
113 struct btrfs_ioctl_quota_rescan_args qargs;
115 printf("Quota data changed, rescan scheduled\n");
116 memset(&qargs, 0, sizeof(qargs));
117 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
119 error("quota rescan failed: %s",
122 warning("quotas may be inconsistent, rescan needed");
125 close_file_or_dir(fd, dirstream);
129 static int _cmd_qgroup_create(int create, int argc, char **argv)
135 struct btrfs_ioctl_qgroup_create_args args;
136 DIR *dirstream = NULL;
138 if (check_argc_exact(argc - optind, 2))
141 memset(&args, 0, sizeof(args));
142 args.create = create;
143 args.qgroupid = parse_qgroupid(argv[optind]);
144 path = argv[optind + 1];
146 fd = btrfs_open_dir(path, &dirstream, 1);
150 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
152 close_file_or_dir(fd, dirstream);
154 error("unable to %s quota group: %s",
155 create ? "create":"destroy", strerror(e));
161 static int parse_limit(const char *p, unsigned long long *s)
164 unsigned long long size;
165 unsigned long long CLEAR_VALUE = -1;
167 if (strcasecmp(p, "none") == 0) {
175 size = strtoull(p, &endptr, 10);
211 static const char * const cmd_qgroup_assign_usage[] = {
212 "btrfs qgroup assign [options] <src> <dst> <path>",
213 "Assign SRC as the child qgroup of DST",
215 "--rescan schedule qutoa rescan if needed",
216 "--no-rescan don't schedule quota rescan",
220 static int cmd_qgroup_assign(int argc, char **argv)
222 return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
225 static const char * const cmd_qgroup_remove_usage[] = {
226 "btrfs qgroup remove <src> <dst> <path>",
227 "Remove a child qgroup SRC from DST.",
231 static int cmd_qgroup_remove(int argc, char **argv)
233 return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
236 static const char * const cmd_qgroup_create_usage[] = {
237 "btrfs qgroup create <qgroupid> <path>",
238 "Create a subvolume quota group.",
242 static int cmd_qgroup_create(int argc, char **argv)
246 clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
248 ret = _cmd_qgroup_create(1, argc, argv);
251 usage(cmd_qgroup_create_usage);
255 static const char * const cmd_qgroup_destroy_usage[] = {
256 "btrfs qgroup destroy <qgroupid> <path>",
257 "Destroy a quota group.",
261 static int cmd_qgroup_destroy(int argc, char **argv)
265 clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
267 ret = _cmd_qgroup_create(0, argc, argv);
270 usage(cmd_qgroup_destroy_usage);
274 static const char * const cmd_qgroup_show_usage[] = {
275 "btrfs qgroup show -pcreFf "
276 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <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)",
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)",
294 static int cmd_qgroup_show(int argc, char **argv)
299 DIR *dirstream = NULL;
304 struct btrfs_qgroup_comparer_set *comparer_set;
305 struct btrfs_qgroup_filter_set *filter_set;
306 filter_set = btrfs_qgroup_alloc_filter_set();
307 comparer_set = btrfs_qgroup_alloc_comparer_set();
309 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
313 static const struct option long_options[] = {
314 {"sort", required_argument, NULL, 'S'},
318 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
323 btrfs_qgroup_setup_print_column(
324 BTRFS_QGROUP_PARENT);
327 btrfs_qgroup_setup_print_column(
331 btrfs_qgroup_setup_print_column(
332 BTRFS_QGROUP_MAX_RFER);
335 btrfs_qgroup_setup_print_column(
336 BTRFS_QGROUP_MAX_EXCL);
345 ret = btrfs_qgroup_parse_sort_string(optarg,
348 usage(cmd_qgroup_show_usage);
351 usage(cmd_qgroup_show_usage);
354 btrfs_qgroup_setup_units(unit_mode);
356 if (check_argc_exact(argc - optind, 1))
357 usage(cmd_qgroup_show_usage);
360 fd = btrfs_open_dir(path, &dirstream, 1);
368 ret = lookup_path_rootid(fd, &qgroupid);
370 error("cannot resolve rootid for %s: %s",
371 path, strerror(-ret));
372 close_file_or_dir(fd, dirstream);
375 if (filter_flag & 0x1)
376 btrfs_qgroup_setup_filter(&filter_set,
377 BTRFS_QGROUP_FILTER_ALL_PARENT,
379 if (filter_flag & 0x2)
380 btrfs_qgroup_setup_filter(&filter_set,
381 BTRFS_QGROUP_FILTER_PARENT,
384 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
386 error("can't list qgroups: quotas not enabled");
388 error("can't list qgroups: %s", strerror(-ret));
389 close_file_or_dir(fd, dirstream);
395 static const char * const cmd_qgroup_limit_usage[] = {
396 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
397 "Set the limits a subvolume quota group.",
399 "-c limit amount of data after compression. This is the default,",
400 " it is currently not possible to turn off this option.",
401 "-e limit space exclusively assigned to this qgroup",
405 static int cmd_qgroup_limit(int argc, char **argv)
411 struct btrfs_ioctl_qgroup_limit_args args;
412 unsigned long long size;
415 DIR *dirstream = NULL;
418 int c = getopt(argc, argv, "ce");
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]);
441 memset(&args, 0, sizeof(args));
443 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
444 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
446 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
447 args.lim.max_exclusive = size;
449 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
450 args.lim.max_referenced = size;
453 if (argc - optind == 2) {
455 path = argv[optind + 1];
456 ret = test_issubvolume(path);
458 error("cannot access '%s': %s", path, strerror(-ret));
462 error("'%s' is not a subvolume", path);
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];
473 usage(cmd_qgroup_limit_usage);
475 fd = btrfs_open_dir(path, &dirstream, 1);
479 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
481 close_file_or_dir(fd, dirstream);
483 error("unable to limit requested quota group: %s", strerror(e));
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,
496 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
498 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
500 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
502 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
504 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
510 int cmd_qgroup(int argc, char **argv)
512 return handle_command_group(&qgroup_cmd_group, argc, argv);