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 qgroup_assign(int assign, int argc, char **argv)
41 struct btrfs_ioctl_qgroup_assign_args args;
42 DIR *dirstream = NULL;
45 enum { GETOPT_VAL_RESCAN = 256 };
46 static const struct option long_options[] = {
47 { "rescan", no_argument, NULL, GETOPT_VAL_RESCAN },
50 int c = getopt_long(argc, argv, "", long_options, NULL);
55 case GETOPT_VAL_RESCAN:
59 /* Usage printed by the caller */
64 if (check_argc_exact(argc - optind, 3))
67 memset(&args, 0, sizeof(args));
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);
81 fd = btrfs_open_dir(path, &dirstream, 1);
85 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
87 error("unable to assign quota group: %s", strerror(errno));
88 close_file_or_dir(fd, dirstream);
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
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);
108 error("quota rescan failed: %s",
111 warning("quotas may be inconsistent, rescan needed");
114 close_file_or_dir(fd, dirstream);
118 static int qgroup_create(int create, int argc, char **argv)
123 char *path = argv[2];
124 struct btrfs_ioctl_qgroup_create_args args;
125 DIR *dirstream = NULL;
127 if (check_argc_exact(argc, 3))
130 memset(&args, 0, sizeof(args));
131 args.create = create;
132 args.qgroupid = parse_qgroupid(argv[1]);
134 fd = btrfs_open_dir(path, &dirstream, 1);
138 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
140 close_file_or_dir(fd, dirstream);
142 error("unable to %s quota group: %s",
143 create ? "create":"destroy", strerror(e));
149 static int parse_limit(const char *p, unsigned long long *s)
152 unsigned long long size;
153 unsigned long long CLEAR_VALUE = -1;
155 if (strcasecmp(p, "none") == 0) {
163 size = strtoull(p, &endptr, 10);
199 static const char * const cmd_qgroup_assign_usage[] = {
200 "btrfs qgroup assign [options] <src> <dst> <path>",
201 "Assign SRC as the child qgroup of DST",
203 "--rescan schedule qutoa rescan if needed",
208 static int cmd_qgroup_assign(int argc, char **argv)
210 int ret = qgroup_assign(1, argc, argv);
212 usage(cmd_qgroup_assign_usage);
216 static const char * const cmd_qgroup_remove_usage[] = {
217 "btrfs qgroup remove <src> <dst> <path>",
218 "Remove a child qgroup SRC from DST.",
222 static int cmd_qgroup_remove(int argc, char **argv)
224 int ret = qgroup_assign(0, argc, argv);
226 usage(cmd_qgroup_remove_usage);
230 static const char * const cmd_qgroup_create_usage[] = {
231 "btrfs qgroup create <qgroupid> <path>",
232 "Create a subvolume quota group.",
236 static int cmd_qgroup_create(int argc, char **argv)
238 int ret = qgroup_create(1, argc, argv);
240 usage(cmd_qgroup_create_usage);
244 static const char * const cmd_qgroup_destroy_usage[] = {
245 "btrfs qgroup destroy <qgroupid> <path>",
246 "Destroy a quota group.",
250 static int cmd_qgroup_destroy(int argc, char **argv)
252 int ret = qgroup_create(0, argc, argv);
254 usage(cmd_qgroup_destroy_usage);
258 static const char * const cmd_qgroup_show_usage[] = {
259 "btrfs qgroup show -pcreFf "
260 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
261 "Show subvolume quota groups.",
262 "-p print parent qgroup id",
263 "-c print child qgroup id",
264 "-r print limit of referenced size of qgroup",
265 "-e print limit of exclusive size of qgroup",
266 "-F list all qgroups which impact the given path",
267 " (including ancestral qgroups)",
268 "-f list all qgroups which impact the given path",
269 " (excluding ancestral qgroups)",
271 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
272 " list qgroups sorted by specified items",
273 " you can use '+' or '-' in front of each item.",
274 " (+:ascending, -:descending, ascending default)",
278 static int cmd_qgroup_show(int argc, char **argv)
284 DIR *dirstream = NULL;
289 struct btrfs_qgroup_comparer_set *comparer_set;
290 struct btrfs_qgroup_filter_set *filter_set;
291 filter_set = btrfs_qgroup_alloc_filter_set();
292 comparer_set = btrfs_qgroup_alloc_comparer_set();
294 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
299 static const struct option long_options[] = {
300 {"sort", required_argument, NULL, 'S'},
304 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
309 btrfs_qgroup_setup_print_column(
310 BTRFS_QGROUP_PARENT);
313 btrfs_qgroup_setup_print_column(
317 btrfs_qgroup_setup_print_column(
318 BTRFS_QGROUP_MAX_RFER);
321 btrfs_qgroup_setup_print_column(
322 BTRFS_QGROUP_MAX_EXCL);
331 ret = btrfs_qgroup_parse_sort_string(optarg,
334 usage(cmd_qgroup_show_usage);
337 usage(cmd_qgroup_show_usage);
340 btrfs_qgroup_setup_units(unit_mode);
342 if (check_argc_exact(argc - optind, 1))
343 usage(cmd_qgroup_show_usage);
346 fd = btrfs_open_dir(path, &dirstream, 1);
348 btrfs_qgroup_free_filter_set(filter_set);
349 btrfs_qgroup_free_comparer_set(comparer_set);
354 qgroupid = btrfs_get_path_rootid(fd);
355 if (filter_flag & 0x1)
356 btrfs_qgroup_setup_filter(&filter_set,
357 BTRFS_QGROUP_FILTER_ALL_PARENT,
359 if (filter_flag & 0x2)
360 btrfs_qgroup_setup_filter(&filter_set,
361 BTRFS_QGROUP_FILTER_PARENT,
364 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
366 close_file_or_dir(fd, dirstream);
368 error("can't list qgroups: %s", strerror(e));
373 static const char * const cmd_qgroup_limit_usage[] = {
374 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
375 "Set the limits a subvolume quota group.",
377 "-c limit amount of data after compression. This is the default,",
378 " it is currently not possible to turn off this option.",
379 "-e limit space exclusively assigned to this qgroup",
383 static int cmd_qgroup_limit(int argc, char **argv)
389 struct btrfs_ioctl_qgroup_limit_args args;
390 unsigned long long size;
393 DIR *dirstream = NULL;
397 int c = getopt(argc, argv, "ce");
408 usage(cmd_qgroup_limit_usage);
412 if (check_argc_min(argc - optind, 2))
413 usage(cmd_qgroup_limit_usage);
415 if (!parse_limit(argv[optind], &size)) {
416 error("invalid size argument: %s", argv[optind]);
420 memset(&args, 0, sizeof(args));
422 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
423 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
425 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
426 args.lim.max_exclusive = size;
428 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
429 args.lim.max_referenced = size;
432 if (argc - optind == 2) {
434 path = argv[optind + 1];
435 ret = test_issubvolume(path);
437 error("cannot access '%s'", path);
441 error("'%s' is not a subvolume", path);
445 * keep qgroupid at 0, this indicates that the subvolume the
446 * fd refers to is to be limited
448 } else if (argc - optind == 3) {
449 args.qgroupid = parse_qgroupid(argv[optind + 1]);
450 path = argv[optind + 2];
452 usage(cmd_qgroup_limit_usage);
454 fd = btrfs_open_dir(path, &dirstream, 1);
458 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
460 close_file_or_dir(fd, dirstream);
462 error("unable to limit requested quota group: %s", strerror(e));
468 static const char qgroup_cmd_group_info[] =
469 "manage quota groups";
471 const struct cmd_group qgroup_cmd_group = {
472 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
473 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
475 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
477 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
479 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
481 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
483 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
489 int cmd_qgroup(int argc, char **argv)
491 return handle_command_group(&qgroup_cmd_group, argc, argv);