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)
42 struct btrfs_ioctl_qgroup_assign_args args;
43 DIR *dirstream = NULL;
46 enum { GETOPT_VAL_RESCAN = 256 };
47 static const struct option long_options[] = {
48 { "rescan", no_argument, NULL, GETOPT_VAL_RESCAN },
51 int c = getopt_long(argc, argv, "", long_options, NULL);
56 case GETOPT_VAL_RESCAN:
60 /* Usage printed by the caller */
65 if (check_argc_exact(argc - optind, 3))
68 memset(&args, 0, sizeof(args));
70 args.src = parse_qgroupid(argv[optind]);
71 args.dst = parse_qgroupid(argv[optind + 1]);
73 path = argv[optind + 2];
76 * FIXME src should accept subvol path
78 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
79 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
82 fd = open_file_or_dir(path, &dirstream);
84 fprintf(stderr, "ERROR: can't access '%s'\n", path);
88 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
91 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
93 close_file_or_dir(fd, dirstream);
98 * If ret > 0, it means assign caused qgroup data inconsistent state.
99 * Schedule a quota rescan if requested.
101 * The return value change only happens in newer kernel. But will not
102 * cause problem since old kernel has a bug that will never clear
107 struct btrfs_ioctl_quota_rescan_args args;
109 printf("Quota data changed, rescan scheduled\n");
110 memset(&args, 0, sizeof(args));
111 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
114 "ERROR: quota rescan failed: %s\n",
117 printf("WARNING: quotas may be inconsistent, rescan needed\n");
120 close_file_or_dir(fd, dirstream);
124 static int qgroup_create(int create, int argc, char **argv)
129 char *path = argv[2];
130 struct btrfs_ioctl_qgroup_create_args args;
131 DIR *dirstream = NULL;
133 if (check_argc_exact(argc, 3))
136 memset(&args, 0, sizeof(args));
137 args.create = create;
138 args.qgroupid = parse_qgroupid(argv[1]);
140 fd = open_file_or_dir(path, &dirstream);
142 fprintf(stderr, "ERROR: can't access '%s'\n", path);
146 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
148 close_file_or_dir(fd, dirstream);
150 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
151 create ? "create":"destroy", strerror(e));
157 static int parse_limit(const char *p, unsigned long long *s)
160 unsigned long long size;
161 unsigned long long CLEAR_VALUE = -1;
163 if (strcasecmp(p, "none") == 0) {
171 size = strtoull(p, &endptr, 10);
207 static const char * const cmd_qgroup_assign_usage[] = {
208 "btrfs qgroup assign [options] <src> <dst> <path>",
209 "Assign SRC as the child qgroup of DST",
211 "--rescan schedule qutoa rescan if needed",
216 static int cmd_qgroup_assign(int argc, char **argv)
218 int ret = qgroup_assign(1, argc, argv);
220 usage(cmd_qgroup_assign_usage);
224 static const char * const cmd_qgroup_remove_usage[] = {
225 "btrfs qgroup remove <src> <dst> <path>",
226 "Remove a child qgroup SRC from DST.",
230 static int cmd_qgroup_remove(int argc, char **argv)
232 int ret = qgroup_assign(0, argc, argv);
234 usage(cmd_qgroup_remove_usage);
238 static const char * const cmd_qgroup_create_usage[] = {
239 "btrfs qgroup create <qgroupid> <path>",
240 "Create a subvolume quota group.",
244 static int cmd_qgroup_create(int argc, char **argv)
246 int ret = qgroup_create(1, argc, argv);
248 usage(cmd_qgroup_create_usage);
252 static const char * const cmd_qgroup_destroy_usage[] = {
253 "btrfs qgroup destroy <qgroupid> <path>",
254 "Destroy a quota group.",
258 static int cmd_qgroup_destroy(int argc, char **argv)
260 int ret = qgroup_create(0, argc, argv);
262 usage(cmd_qgroup_destroy_usage);
266 static const char * const cmd_qgroup_show_usage[] = {
267 "btrfs qgroup show -pcreFf "
268 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
269 "Show subvolume quota groups.",
270 "-p print parent qgroup id",
271 "-c print child qgroup id",
272 "-r print limit of referenced size of qgroup",
273 "-e print limit of exclusive size of qgroup",
274 "-F list all qgroups which impact the given path",
275 " (including ancestral qgroups)",
276 "-f list all qgroups which impact the given path",
277 " (excluding ancestral qgroups)",
278 "--raw raw numbers in bytes",
280 " human firendly numbers in given base, 1024 by default",
281 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
282 "--si use 1000 as a base (kB, MB, GB, TB)",
283 "--kbytes show sizes in KiB, or kB with --si",
284 "--mbytes show sizes in MiB, or MB with --si",
285 "--gbytes show sizes in GiB, or GB with --si",
286 "--tbytes show sizes in TiB, or TB with --si",
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)
300 DIR *dirstream = NULL;
303 unsigned unit_mode = UNITS_DEFAULT;
305 struct btrfs_qgroup_comparer_set *comparer_set;
306 struct btrfs_qgroup_filter_set *filter_set;
307 filter_set = btrfs_qgroup_alloc_filter_set();
308 comparer_set = btrfs_qgroup_alloc_comparer_set();
313 static const struct option long_options[] = {
314 {"sort", required_argument, NULL, 'S'},
315 {"raw", no_argument, NULL, GETOPT_VAL_RAW},
316 {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
317 {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
318 {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
319 {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
320 {"si", no_argument, NULL, GETOPT_VAL_SI},
321 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
322 { "human-readable", no_argument, NULL,
323 GETOPT_VAL_HUMAN_READABLE},
327 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
332 btrfs_qgroup_setup_print_column(
333 BTRFS_QGROUP_PARENT);
336 btrfs_qgroup_setup_print_column(
340 btrfs_qgroup_setup_print_column(
341 BTRFS_QGROUP_MAX_RFER);
344 btrfs_qgroup_setup_print_column(
345 BTRFS_QGROUP_MAX_EXCL);
354 ret = btrfs_qgroup_parse_sort_string(optarg,
357 usage(cmd_qgroup_show_usage);
360 unit_mode = UNITS_RAW;
362 case GETOPT_VAL_KBYTES:
363 units_set_base(&unit_mode, UNITS_KBYTES);
365 case GETOPT_VAL_MBYTES:
366 units_set_base(&unit_mode, UNITS_MBYTES);
368 case GETOPT_VAL_GBYTES:
369 units_set_base(&unit_mode, UNITS_GBYTES);
371 case GETOPT_VAL_TBYTES:
372 units_set_base(&unit_mode, UNITS_TBYTES);
375 units_set_mode(&unit_mode, UNITS_DECIMAL);
378 units_set_mode(&unit_mode, UNITS_BINARY);
380 case GETOPT_VAL_HUMAN_READABLE:
381 unit_mode = UNITS_HUMAN_BINARY;
384 usage(cmd_qgroup_show_usage);
387 btrfs_qgroup_setup_units(unit_mode);
389 if (check_argc_exact(argc - optind, 1))
390 usage(cmd_qgroup_show_usage);
393 fd = open_file_or_dir(path, &dirstream);
395 fprintf(stderr, "ERROR: can't access '%s'\n", path);
400 qgroupid = btrfs_get_path_rootid(fd);
401 if (filter_flag & 0x1)
402 btrfs_qgroup_setup_filter(&filter_set,
403 BTRFS_QGROUP_FILTER_ALL_PARENT,
405 if (filter_flag & 0x2)
406 btrfs_qgroup_setup_filter(&filter_set,
407 BTRFS_QGROUP_FILTER_PARENT,
410 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
412 close_file_or_dir(fd, dirstream);
414 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
420 static const char * const cmd_qgroup_limit_usage[] = {
421 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
422 "Set the limits a subvolume quota group.",
424 "-c limit amount of data after compression. This is the default,",
425 " it is currently not possible to turn off this option.",
426 "-e limit space exclusively assigned to this qgroup",
430 static int cmd_qgroup_limit(int argc, char **argv)
436 struct btrfs_ioctl_qgroup_limit_args args;
437 unsigned long long size;
440 DIR *dirstream = NULL;
444 int c = getopt(argc, argv, "ce");
455 usage(cmd_qgroup_limit_usage);
459 if (check_argc_min(argc - optind, 2))
460 usage(cmd_qgroup_limit_usage);
462 if (!parse_limit(argv[optind], &size)) {
463 fprintf(stderr, "Invalid size argument given\n");
467 memset(&args, 0, sizeof(args));
469 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
470 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
472 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
473 args.lim.max_exclusive = size;
475 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
476 args.lim.max_referenced = size;
479 if (argc - optind == 2) {
481 path = argv[optind + 1];
482 ret = test_issubvolume(path);
484 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
488 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
493 * keep qgroupid at 0, this indicates that the subvolume the
494 * fd refers to is to be limited
496 } else if (argc - optind == 3) {
497 args.qgroupid = parse_qgroupid(argv[optind + 1]);
498 path = argv[optind + 2];
500 usage(cmd_qgroup_limit_usage);
502 fd = open_file_or_dir(path, &dirstream);
504 fprintf(stderr, "ERROR: can't access '%s'\n", path);
508 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
510 close_file_or_dir(fd, dirstream);
512 fprintf(stderr, "ERROR: unable to limit requested quota group: "
513 "%s\n", strerror(e));
519 static const char qgroup_cmd_group_info[] =
520 "manage quota groups";
522 const struct cmd_group qgroup_cmd_group = {
523 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
524 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
526 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
528 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
530 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
532 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
534 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
540 int cmd_qgroup(int argc, char **argv)
542 return handle_command_group(&qgroup_cmd_group, argc, argv);