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 = btrfs_open_dir(path, &dirstream, 1);
86 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
89 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
91 close_file_or_dir(fd, dirstream);
96 * If ret > 0, it means assign caused qgroup data inconsistent state.
97 * Schedule a quota rescan if requested.
99 * The return value change only happens in newer kernel. But will not
100 * cause problem since old kernel has a bug that will never clear
105 struct btrfs_ioctl_quota_rescan_args args;
107 printf("Quota data changed, rescan scheduled\n");
108 memset(&args, 0, sizeof(args));
109 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
112 "ERROR: quota rescan failed: %s\n",
115 printf("WARNING: quotas may be inconsistent, rescan needed\n");
118 close_file_or_dir(fd, dirstream);
122 static int qgroup_create(int create, int argc, char **argv)
127 char *path = argv[2];
128 struct btrfs_ioctl_qgroup_create_args args;
129 DIR *dirstream = NULL;
131 if (check_argc_exact(argc, 3))
134 memset(&args, 0, sizeof(args));
135 args.create = create;
136 args.qgroupid = parse_qgroupid(argv[1]);
138 fd = btrfs_open_dir(path, &dirstream, 1);
142 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
144 close_file_or_dir(fd, dirstream);
146 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
147 create ? "create":"destroy", strerror(e));
153 static int parse_limit(const char *p, unsigned long long *s)
156 unsigned long long size;
157 unsigned long long CLEAR_VALUE = -1;
159 if (strcasecmp(p, "none") == 0) {
167 size = strtoull(p, &endptr, 10);
203 static const char * const cmd_qgroup_assign_usage[] = {
204 "btrfs qgroup assign [options] <src> <dst> <path>",
205 "Assign SRC as the child qgroup of DST",
207 "--rescan schedule qutoa rescan if needed",
212 static int cmd_qgroup_assign(int argc, char **argv)
214 int ret = qgroup_assign(1, argc, argv);
216 usage(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 int ret = qgroup_assign(0, argc, argv);
230 usage(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)
242 int ret = qgroup_create(1, argc, argv);
244 usage(cmd_qgroup_create_usage);
248 static const char * const cmd_qgroup_destroy_usage[] = {
249 "btrfs qgroup destroy <qgroupid> <path>",
250 "Destroy a quota group.",
254 static int cmd_qgroup_destroy(int argc, char **argv)
256 int ret = qgroup_create(0, argc, argv);
258 usage(cmd_qgroup_destroy_usage);
262 static const char * const cmd_qgroup_show_usage[] = {
263 "btrfs qgroup show -pcreFf "
264 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
265 "Show subvolume quota groups.",
266 "-p print parent qgroup id",
267 "-c print child qgroup id",
268 "-r print limit of referenced size of qgroup",
269 "-e print limit of exclusive size of qgroup",
270 "-F list all qgroups which impact the given path",
271 " (including ancestral qgroups)",
272 "-f list all qgroups which impact the given path",
273 " (excluding ancestral qgroups)",
275 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
276 " list qgroups sorted by specified items",
277 " you can use '+' or '-' in front of each item.",
278 " (+:ascending, -:descending, ascending default)",
282 static int cmd_qgroup_show(int argc, char **argv)
288 DIR *dirstream = NULL;
293 struct btrfs_qgroup_comparer_set *comparer_set;
294 struct btrfs_qgroup_filter_set *filter_set;
295 filter_set = btrfs_qgroup_alloc_filter_set();
296 comparer_set = btrfs_qgroup_alloc_comparer_set();
298 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
303 static const struct option long_options[] = {
304 {"sort", required_argument, NULL, 'S'},
308 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
313 btrfs_qgroup_setup_print_column(
314 BTRFS_QGROUP_PARENT);
317 btrfs_qgroup_setup_print_column(
321 btrfs_qgroup_setup_print_column(
322 BTRFS_QGROUP_MAX_RFER);
325 btrfs_qgroup_setup_print_column(
326 BTRFS_QGROUP_MAX_EXCL);
335 ret = btrfs_qgroup_parse_sort_string(optarg,
338 usage(cmd_qgroup_show_usage);
341 usage(cmd_qgroup_show_usage);
344 btrfs_qgroup_setup_units(unit_mode);
346 if (check_argc_exact(argc - optind, 1))
347 usage(cmd_qgroup_show_usage);
350 fd = btrfs_open_dir(path, &dirstream, 1);
352 btrfs_qgroup_free_filter_set(filter_set);
353 btrfs_qgroup_free_comparer_set(comparer_set);
358 qgroupid = btrfs_get_path_rootid(fd);
359 if (filter_flag & 0x1)
360 btrfs_qgroup_setup_filter(&filter_set,
361 BTRFS_QGROUP_FILTER_ALL_PARENT,
363 if (filter_flag & 0x2)
364 btrfs_qgroup_setup_filter(&filter_set,
365 BTRFS_QGROUP_FILTER_PARENT,
368 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
370 close_file_or_dir(fd, dirstream);
372 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
378 static const char * const cmd_qgroup_limit_usage[] = {
379 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
380 "Set the limits a subvolume quota group.",
382 "-c limit amount of data after compression. This is the default,",
383 " it is currently not possible to turn off this option.",
384 "-e limit space exclusively assigned to this qgroup",
388 static int cmd_qgroup_limit(int argc, char **argv)
394 struct btrfs_ioctl_qgroup_limit_args args;
395 unsigned long long size;
398 DIR *dirstream = NULL;
402 int c = getopt(argc, argv, "ce");
413 usage(cmd_qgroup_limit_usage);
417 if (check_argc_min(argc - optind, 2))
418 usage(cmd_qgroup_limit_usage);
420 if (!parse_limit(argv[optind], &size)) {
421 fprintf(stderr, "Invalid size argument given\n");
425 memset(&args, 0, sizeof(args));
427 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
428 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
430 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
431 args.lim.max_exclusive = size;
433 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
434 args.lim.max_referenced = size;
437 if (argc - optind == 2) {
439 path = argv[optind + 1];
440 ret = test_issubvolume(path);
442 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
446 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
451 * keep qgroupid at 0, this indicates that the subvolume the
452 * fd refers to is to be limited
454 } else if (argc - optind == 3) {
455 args.qgroupid = parse_qgroupid(argv[optind + 1]);
456 path = argv[optind + 2];
458 usage(cmd_qgroup_limit_usage);
460 fd = btrfs_open_dir(path, &dirstream, 1);
464 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
466 close_file_or_dir(fd, dirstream);
468 fprintf(stderr, "ERROR: unable to limit requested quota group: "
469 "%s\n", strerror(e));
475 static const char qgroup_cmd_group_info[] =
476 "manage quota groups";
478 const struct cmd_group qgroup_cmd_group = {
479 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
480 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
482 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
484 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
486 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
488 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
490 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
496 int cmd_qgroup(int argc, char **argv)
498 return handle_command_group(&qgroup_cmd_group, argc, argv);