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 error("bad relation requested: %s", path);
82 fd = btrfs_open_dir(path, &dirstream, 1);
86 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
89 error("unable to assign quota group: %s", strerror(e));
90 close_file_or_dir(fd, dirstream);
95 * If ret > 0, it means assign caused qgroup data inconsistent state.
96 * Schedule a quota rescan if requested.
98 * The return value change only happens in newer kernel. But will not
99 * cause problem since old kernel has a bug that will never clear
104 struct btrfs_ioctl_quota_rescan_args args;
106 printf("Quota data changed, rescan scheduled\n");
107 memset(&args, 0, sizeof(args));
108 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
110 error("quota rescan failed: %s",
113 warning("quotas may be inconsistent, rescan needed");
116 close_file_or_dir(fd, dirstream);
120 static int qgroup_create(int create, int argc, char **argv)
125 char *path = argv[2];
126 struct btrfs_ioctl_qgroup_create_args args;
127 DIR *dirstream = NULL;
129 if (check_argc_exact(argc, 3))
132 memset(&args, 0, sizeof(args));
133 args.create = create;
134 args.qgroupid = parse_qgroupid(argv[1]);
136 fd = btrfs_open_dir(path, &dirstream, 1);
140 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
142 close_file_or_dir(fd, dirstream);
144 error("unable to %s quota group: %s",
145 create ? "create":"destroy", strerror(e));
151 static int parse_limit(const char *p, unsigned long long *s)
154 unsigned long long size;
155 unsigned long long CLEAR_VALUE = -1;
157 if (strcasecmp(p, "none") == 0) {
165 size = strtoull(p, &endptr, 10);
201 static const char * const cmd_qgroup_assign_usage[] = {
202 "btrfs qgroup assign [options] <src> <dst> <path>",
203 "Assign SRC as the child qgroup of DST",
205 "--rescan schedule qutoa rescan if needed",
210 static int cmd_qgroup_assign(int argc, char **argv)
212 int ret = qgroup_assign(1, argc, argv);
214 usage(cmd_qgroup_assign_usage);
218 static const char * const cmd_qgroup_remove_usage[] = {
219 "btrfs qgroup remove <src> <dst> <path>",
220 "Remove a child qgroup SRC from DST.",
224 static int cmd_qgroup_remove(int argc, char **argv)
226 int ret = qgroup_assign(0, argc, argv);
228 usage(cmd_qgroup_remove_usage);
232 static const char * const cmd_qgroup_create_usage[] = {
233 "btrfs qgroup create <qgroupid> <path>",
234 "Create a subvolume quota group.",
238 static int cmd_qgroup_create(int argc, char **argv)
240 int ret = qgroup_create(1, argc, argv);
242 usage(cmd_qgroup_create_usage);
246 static const char * const cmd_qgroup_destroy_usage[] = {
247 "btrfs qgroup destroy <qgroupid> <path>",
248 "Destroy a quota group.",
252 static int cmd_qgroup_destroy(int argc, char **argv)
254 int ret = qgroup_create(0, argc, argv);
256 usage(cmd_qgroup_destroy_usage);
260 static const char * const cmd_qgroup_show_usage[] = {
261 "btrfs qgroup show -pcreFf "
262 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
263 "Show subvolume quota groups.",
264 "-p print parent qgroup id",
265 "-c print child qgroup id",
266 "-r print limit of referenced size of qgroup",
267 "-e print limit of exclusive size of qgroup",
268 "-F list all qgroups which impact the given path",
269 " (including ancestral qgroups)",
270 "-f list all qgroups which impact the given path",
271 " (excluding ancestral qgroups)",
273 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
274 " list qgroups sorted by specified items",
275 " you can use '+' or '-' in front of each item.",
276 " (+:ascending, -:descending, ascending default)",
280 static int cmd_qgroup_show(int argc, char **argv)
286 DIR *dirstream = NULL;
291 struct btrfs_qgroup_comparer_set *comparer_set;
292 struct btrfs_qgroup_filter_set *filter_set;
293 filter_set = btrfs_qgroup_alloc_filter_set();
294 comparer_set = btrfs_qgroup_alloc_comparer_set();
296 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
301 static const struct option long_options[] = {
302 {"sort", required_argument, NULL, 'S'},
306 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
311 btrfs_qgroup_setup_print_column(
312 BTRFS_QGROUP_PARENT);
315 btrfs_qgroup_setup_print_column(
319 btrfs_qgroup_setup_print_column(
320 BTRFS_QGROUP_MAX_RFER);
323 btrfs_qgroup_setup_print_column(
324 BTRFS_QGROUP_MAX_EXCL);
333 ret = btrfs_qgroup_parse_sort_string(optarg,
336 usage(cmd_qgroup_show_usage);
339 usage(cmd_qgroup_show_usage);
342 btrfs_qgroup_setup_units(unit_mode);
344 if (check_argc_exact(argc - optind, 1))
345 usage(cmd_qgroup_show_usage);
348 fd = btrfs_open_dir(path, &dirstream, 1);
350 btrfs_qgroup_free_filter_set(filter_set);
351 btrfs_qgroup_free_comparer_set(comparer_set);
356 qgroupid = btrfs_get_path_rootid(fd);
357 if (filter_flag & 0x1)
358 btrfs_qgroup_setup_filter(&filter_set,
359 BTRFS_QGROUP_FILTER_ALL_PARENT,
361 if (filter_flag & 0x2)
362 btrfs_qgroup_setup_filter(&filter_set,
363 BTRFS_QGROUP_FILTER_PARENT,
366 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
368 close_file_or_dir(fd, dirstream);
370 error("can't list qgroups: %s", strerror(e));
375 static const char * const cmd_qgroup_limit_usage[] = {
376 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
377 "Set the limits a subvolume quota group.",
379 "-c limit amount of data after compression. This is the default,",
380 " it is currently not possible to turn off this option.",
381 "-e limit space exclusively assigned to this qgroup",
385 static int cmd_qgroup_limit(int argc, char **argv)
391 struct btrfs_ioctl_qgroup_limit_args args;
392 unsigned long long size;
395 DIR *dirstream = NULL;
399 int c = getopt(argc, argv, "ce");
410 usage(cmd_qgroup_limit_usage);
414 if (check_argc_min(argc - optind, 2))
415 usage(cmd_qgroup_limit_usage);
417 if (!parse_limit(argv[optind], &size)) {
418 error("invalid size argument: %s", argv[optind]);
422 memset(&args, 0, sizeof(args));
424 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
425 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
427 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
428 args.lim.max_exclusive = size;
430 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
431 args.lim.max_referenced = size;
434 if (argc - optind == 2) {
436 path = argv[optind + 1];
437 ret = test_issubvolume(path);
439 error("cannot access '%s'", path);
443 error("'%s' is not a subvolume", path);
447 * keep qgroupid at 0, this indicates that the subvolume the
448 * fd refers to is to be limited
450 } else if (argc - optind == 3) {
451 args.qgroupid = parse_qgroupid(argv[optind + 1]);
452 path = argv[optind + 2];
454 usage(cmd_qgroup_limit_usage);
456 fd = btrfs_open_dir(path, &dirstream, 1);
460 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
462 close_file_or_dir(fd, dirstream);
464 error("unable to limit requested quota group: %s", strerror(e));
470 static const char qgroup_cmd_group_info[] =
471 "manage quota groups";
473 const struct cmd_group qgroup_cmd_group = {
474 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
475 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
477 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
479 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
481 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
483 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
485 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
491 int cmd_qgroup(int argc, char **argv)
493 return handle_command_group(&qgroup_cmd_group, argc, argv);