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 };
48 static const struct option long_options[] = {
49 { "rescan", no_argument, NULL,
53 int c = getopt_long(argc, argv, "", long_options, NULL);
58 case GETOPT_VAL_RESCAN:
62 /* Usage printed by the caller */
67 clean_args_no_options(argc, argv, usage_str);
70 if (check_argc_exact(argc - optind, 3))
73 memset(&args, 0, sizeof(args));
75 args.src = parse_qgroupid(argv[optind]);
76 args.dst = parse_qgroupid(argv[optind + 1]);
78 path = argv[optind + 2];
81 * FIXME src should accept subvol path
83 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
84 error("bad relation requested: %s", path);
87 fd = btrfs_open_dir(path, &dirstream, 1);
91 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
93 error("unable to assign quota group: %s", strerror(errno));
94 close_file_or_dir(fd, dirstream);
99 * If ret > 0, it means assign caused qgroup data inconsistent state.
100 * Schedule a quota rescan if requested.
102 * The return value change only happens in newer kernel. But will not
103 * cause problem since old kernel has a bug that will never clear
108 struct btrfs_ioctl_quota_rescan_args qargs;
110 printf("Quota data changed, rescan scheduled\n");
111 memset(&qargs, 0, sizeof(qargs));
112 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
114 error("quota rescan failed: %s",
117 warning("quotas may be inconsistent, rescan needed");
120 close_file_or_dir(fd, dirstream);
124 static int _cmd_qgroup_create(int create, int argc, char **argv)
130 struct btrfs_ioctl_qgroup_create_args args;
131 DIR *dirstream = NULL;
133 if (check_argc_exact(argc - optind, 2))
136 memset(&args, 0, sizeof(args));
137 args.create = create;
138 args.qgroupid = parse_qgroupid(argv[optind]);
139 path = argv[optind + 1];
141 fd = btrfs_open_dir(path, &dirstream, 1);
145 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
147 close_file_or_dir(fd, dirstream);
149 error("unable to %s quota group: %s",
150 create ? "create":"destroy", strerror(e));
156 static int parse_limit(const char *p, unsigned long long *s)
159 unsigned long long size;
160 unsigned long long CLEAR_VALUE = -1;
162 if (strcasecmp(p, "none") == 0) {
170 size = strtoull(p, &endptr, 10);
206 static const char * const cmd_qgroup_assign_usage[] = {
207 "btrfs qgroup assign [options] <src> <dst> <path>",
208 "Assign SRC as the child qgroup of DST",
210 "--rescan schedule qutoa rescan if needed",
215 static int cmd_qgroup_assign(int argc, char **argv)
217 return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
220 static const char * const cmd_qgroup_remove_usage[] = {
221 "btrfs qgroup remove <src> <dst> <path>",
222 "Remove a child qgroup SRC from DST.",
226 static int cmd_qgroup_remove(int argc, char **argv)
228 return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
231 static const char * const cmd_qgroup_create_usage[] = {
232 "btrfs qgroup create <qgroupid> <path>",
233 "Create a subvolume quota group.",
237 static int cmd_qgroup_create(int argc, char **argv)
241 clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
243 ret = _cmd_qgroup_create(1, argc, argv);
246 usage(cmd_qgroup_create_usage);
250 static const char * const cmd_qgroup_destroy_usage[] = {
251 "btrfs qgroup destroy <qgroupid> <path>",
252 "Destroy a quota group.",
256 static int cmd_qgroup_destroy(int argc, char **argv)
260 clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
262 ret = _cmd_qgroup_create(0, argc, argv);
265 usage(cmd_qgroup_destroy_usage);
269 static const char * const cmd_qgroup_show_usage[] = {
270 "btrfs qgroup show -pcreFf "
271 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
272 "Show subvolume quota groups.",
273 "-p print parent qgroup id",
274 "-c print child qgroup id",
275 "-r print limit of referenced size of qgroup",
276 "-e print limit of exclusive size of qgroup",
277 "-F list all qgroups which impact the given path",
278 " (including ancestral qgroups)",
279 "-f list all qgroups which impact the given path",
280 " (excluding ancestral qgroups)",
282 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
283 " list qgroups sorted by specified items",
284 " you can use '+' or '-' in front of each item.",
285 " (+:ascending, -:descending, ascending default)",
289 static int cmd_qgroup_show(int argc, char **argv)
295 DIR *dirstream = NULL;
300 struct btrfs_qgroup_comparer_set *comparer_set;
301 struct btrfs_qgroup_filter_set *filter_set;
302 filter_set = btrfs_qgroup_alloc_filter_set();
303 comparer_set = btrfs_qgroup_alloc_comparer_set();
305 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
310 static const struct option long_options[] = {
311 {"sort", required_argument, NULL, 'S'},
315 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
320 btrfs_qgroup_setup_print_column(
321 BTRFS_QGROUP_PARENT);
324 btrfs_qgroup_setup_print_column(
328 btrfs_qgroup_setup_print_column(
329 BTRFS_QGROUP_MAX_RFER);
332 btrfs_qgroup_setup_print_column(
333 BTRFS_QGROUP_MAX_EXCL);
342 ret = btrfs_qgroup_parse_sort_string(optarg,
345 usage(cmd_qgroup_show_usage);
348 usage(cmd_qgroup_show_usage);
351 btrfs_qgroup_setup_units(unit_mode);
353 if (check_argc_exact(argc - optind, 1))
354 usage(cmd_qgroup_show_usage);
357 fd = btrfs_open_dir(path, &dirstream, 1);
359 btrfs_qgroup_free_filter_set(filter_set);
360 btrfs_qgroup_free_comparer_set(comparer_set);
365 qgroupid = btrfs_get_path_rootid(fd);
366 if (filter_flag & 0x1)
367 btrfs_qgroup_setup_filter(&filter_set,
368 BTRFS_QGROUP_FILTER_ALL_PARENT,
370 if (filter_flag & 0x2)
371 btrfs_qgroup_setup_filter(&filter_set,
372 BTRFS_QGROUP_FILTER_PARENT,
375 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
377 close_file_or_dir(fd, dirstream);
379 error("can't list qgroups: %s", strerror(e));
384 static const char * const cmd_qgroup_limit_usage[] = {
385 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
386 "Set the limits a subvolume quota group.",
388 "-c limit amount of data after compression. This is the default,",
389 " it is currently not possible to turn off this option.",
390 "-e limit space exclusively assigned to this qgroup",
394 static int cmd_qgroup_limit(int argc, char **argv)
400 struct btrfs_ioctl_qgroup_limit_args args;
401 unsigned long long size;
404 DIR *dirstream = NULL;
408 int c = getopt(argc, argv, "ce");
419 usage(cmd_qgroup_limit_usage);
423 if (check_argc_min(argc - optind, 2))
424 usage(cmd_qgroup_limit_usage);
426 if (!parse_limit(argv[optind], &size)) {
427 error("invalid size argument: %s", argv[optind]);
431 memset(&args, 0, sizeof(args));
433 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
434 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
436 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
437 args.lim.max_exclusive = size;
439 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
440 args.lim.max_referenced = size;
443 if (argc - optind == 2) {
445 path = argv[optind + 1];
446 ret = test_issubvolume(path);
448 error("cannot access '%s': %s", path, strerror(-ret));
452 error("'%s' is not a subvolume", path);
456 * keep qgroupid at 0, this indicates that the subvolume the
457 * fd refers to is to be limited
459 } else if (argc - optind == 3) {
460 args.qgroupid = parse_qgroupid(argv[optind + 1]);
461 path = argv[optind + 2];
463 usage(cmd_qgroup_limit_usage);
465 fd = btrfs_open_dir(path, &dirstream, 1);
469 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
471 close_file_or_dir(fd, dirstream);
473 error("unable to limit requested quota group: %s", strerror(e));
479 static const char qgroup_cmd_group_info[] =
480 "manage quota groups";
482 const struct cmd_group qgroup_cmd_group = {
483 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
484 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
486 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
488 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
490 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
492 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
494 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
500 int cmd_qgroup(int argc, char **argv)
502 return handle_command_group(&qgroup_cmd_group, argc, argv);