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>
31 static const char * const qgroup_cmd_group_usage[] = {
32 "btrfs qgroup <command> [options] <path>",
36 static int _cmd_qgroup_assign(int assign, int argc, char **argv,
37 const char * const *usage_str)
43 struct btrfs_ioctl_qgroup_assign_args args;
44 DIR *dirstream = NULL;
48 enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
49 static const struct option long_options[] = {
50 { "rescan", no_argument, NULL,
52 { "no-rescan", no_argument, NULL,
53 GETOPT_VAL_NO_RESCAN },
56 int c = getopt_long(argc, argv, "", long_options, NULL);
61 case GETOPT_VAL_RESCAN:
64 case GETOPT_VAL_NO_RESCAN:
68 /* Usage printed by the caller */
73 clean_args_no_options(argc, argv, usage_str);
76 if (check_argc_exact(argc - optind, 3))
79 memset(&args, 0, sizeof(args));
81 args.src = parse_qgroupid(argv[optind]);
82 args.dst = parse_qgroupid(argv[optind + 1]);
84 path = argv[optind + 2];
87 * FIXME src should accept subvol path
89 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
90 error("bad relation requested: %s", path);
93 fd = btrfs_open_dir(path, &dirstream, 1);
97 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
99 error("unable to assign quota group: %m");
100 close_file_or_dir(fd, dirstream);
105 * If ret > 0, it means assign caused qgroup data inconsistent state.
106 * Schedule a quota rescan if requested.
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
114 struct btrfs_ioctl_quota_rescan_args qargs;
116 printf("Quota data changed, rescan scheduled\n");
117 memset(&qargs, 0, sizeof(qargs));
118 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
120 error("quota rescan failed: %m");
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)
134 struct btrfs_ioctl_qgroup_create_args args;
135 DIR *dirstream = NULL;
137 if (check_argc_exact(argc - optind, 2))
140 memset(&args, 0, sizeof(args));
141 args.create = create;
142 args.qgroupid = parse_qgroupid(argv[optind]);
143 path = argv[optind + 1];
145 fd = btrfs_open_dir(path, &dirstream, 1);
149 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
150 close_file_or_dir(fd, dirstream);
152 error("unable to %s quota group: %m",
153 create ? "create":"destroy");
159 static int parse_limit(const char *p, unsigned long long *s)
162 unsigned long long size;
163 unsigned long long CLEAR_VALUE = -1;
165 if (strcasecmp(p, "none") == 0) {
173 size = strtoull(p, &endptr, 10);
209 static const char * const cmd_qgroup_assign_usage[] = {
210 "btrfs qgroup assign [options] <src> <dst> <path>",
211 "Assign SRC as the child qgroup of DST",
213 "--rescan schedule qutoa rescan if needed",
214 "--no-rescan don't schedule quota rescan",
218 static int cmd_qgroup_assign(int argc, char **argv)
220 return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
223 static const char * const cmd_qgroup_remove_usage[] = {
224 "btrfs qgroup remove <src> <dst> <path>",
225 "Remove a child qgroup SRC from DST.",
229 static int cmd_qgroup_remove(int argc, char **argv)
231 return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
234 static const char * const cmd_qgroup_create_usage[] = {
235 "btrfs qgroup create <qgroupid> <path>",
236 "Create a subvolume quota group.",
240 static int cmd_qgroup_create(int argc, char **argv)
244 clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
246 ret = _cmd_qgroup_create(1, argc, argv);
249 usage(cmd_qgroup_create_usage);
253 static const char * const cmd_qgroup_destroy_usage[] = {
254 "btrfs qgroup destroy <qgroupid> <path>",
255 "Destroy a quota group.",
259 static int cmd_qgroup_destroy(int argc, char **argv)
263 clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
265 ret = _cmd_qgroup_create(0, argc, argv);
268 usage(cmd_qgroup_destroy_usage);
272 static const char * const cmd_qgroup_show_usage[] = {
273 "btrfs qgroup show [options] <path>",
274 "Show subvolume quota groups.",
275 "-p print parent qgroup id",
276 "-c print child qgroup id",
277 "-r print limit of referenced size of qgroup",
278 "-e print limit of exclusive size of qgroup",
279 "-F list all qgroups which impact the given path",
280 " (including ancestral qgroups)",
281 "-f list all qgroups which impact the given path",
282 " (excluding ancestral qgroups)",
284 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
285 " list qgroups sorted by specified items",
286 " you can use '+' or '-' in front of each item.",
287 " (+:ascending, -:descending, ascending default)",
288 "--sync force sync of the filesystem before getting info",
292 static int cmd_qgroup_show(int argc, char **argv)
297 DIR *dirstream = NULL;
303 struct btrfs_qgroup_comparer_set *comparer_set;
304 struct btrfs_qgroup_filter_set *filter_set;
305 filter_set = btrfs_qgroup_alloc_filter_set();
306 comparer_set = btrfs_qgroup_alloc_comparer_set();
308 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
313 GETOPT_VAL_SORT = 256,
316 static const struct option long_options[] = {
317 {"sort", required_argument, NULL, GETOPT_VAL_SORT},
318 {"sync", no_argument, NULL, GETOPT_VAL_SYNC},
322 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
327 btrfs_qgroup_setup_print_column(
328 BTRFS_QGROUP_PARENT);
331 btrfs_qgroup_setup_print_column(
335 btrfs_qgroup_setup_print_column(
336 BTRFS_QGROUP_MAX_RFER);
339 btrfs_qgroup_setup_print_column(
340 BTRFS_QGROUP_MAX_EXCL);
348 case GETOPT_VAL_SORT:
349 ret = btrfs_qgroup_parse_sort_string(optarg,
352 usage(cmd_qgroup_show_usage);
354 case GETOPT_VAL_SYNC:
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);
367 fd = btrfs_open_dir(path, &dirstream, 1);
375 ret = ioctl(fd, BTRFS_IOC_SYNC);
377 warning("sync ioctl failed on '%s': %m", path);
381 ret = lookup_path_rootid(fd, &qgroupid);
383 error("cannot resolve rootid for %s: %s",
384 path, strerror(-ret));
385 close_file_or_dir(fd, dirstream);
388 if (filter_flag & 0x1)
389 btrfs_qgroup_setup_filter(&filter_set,
390 BTRFS_QGROUP_FILTER_ALL_PARENT,
392 if (filter_flag & 0x2)
393 btrfs_qgroup_setup_filter(&filter_set,
394 BTRFS_QGROUP_FILTER_PARENT,
397 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
398 close_file_or_dir(fd, dirstream);
404 static const char * const cmd_qgroup_limit_usage[] = {
405 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
406 "Set the limits a subvolume quota group.",
408 "-c limit amount of data after compression. This is the default,",
409 " it is currently not possible to turn off this option.",
410 "-e limit space exclusively assigned to this qgroup",
414 static int cmd_qgroup_limit(int argc, char **argv)
419 struct btrfs_ioctl_qgroup_limit_args args;
420 unsigned long long size;
423 DIR *dirstream = NULL;
426 int c = getopt(argc, argv, "ce");
437 usage(cmd_qgroup_limit_usage);
441 if (check_argc_min(argc - optind, 2))
442 usage(cmd_qgroup_limit_usage);
444 if (!parse_limit(argv[optind], &size)) {
445 error("invalid size argument: %s", argv[optind]);
449 memset(&args, 0, sizeof(args));
451 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
452 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
454 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
455 args.lim.max_exclusive = size;
457 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
458 args.lim.max_referenced = size;
461 if (argc - optind == 2) {
463 path = argv[optind + 1];
464 ret = test_issubvolume(path);
466 error("cannot access '%s': %s", path, strerror(-ret));
470 error("'%s' is not a subvolume", path);
474 * keep qgroupid at 0, this indicates that the subvolume the
475 * fd refers to is to be limited
477 } else if (argc - optind == 3) {
478 args.qgroupid = parse_qgroupid(argv[optind + 1]);
479 path = argv[optind + 2];
481 usage(cmd_qgroup_limit_usage);
483 fd = btrfs_open_dir(path, &dirstream, 1);
487 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
488 close_file_or_dir(fd, dirstream);
490 error("unable to limit requested quota group: %m");
496 static const char qgroup_cmd_group_info[] =
497 "manage quota groups";
499 const struct cmd_group qgroup_cmd_group = {
500 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
501 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
503 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
505 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
507 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
509 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
511 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
517 int cmd_qgroup(int argc, char **argv)
519 return handle_command_group(&qgroup_cmd_group, argc, argv);