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;
44 if (check_argc_exact(argc, 4))
47 memset(&args, 0, sizeof(args));
49 args.src = parse_qgroupid(argv[1]);
50 args.dst = parse_qgroupid(argv[2]);
55 * FIXME src should accept subvol path
57 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
58 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
61 fd = open_file_or_dir(path, &dirstream);
63 fprintf(stderr, "ERROR: can't access '%s'\n", path);
67 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
70 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
72 close_file_or_dir(fd, dirstream);
77 * If ret > 0, it means assign caused qgroup data inconsistent state.
78 * Schedule a quota rescan if requested.
80 * The return value change only happens in newer kernel. But will not
81 * cause problem since old kernel has a bug that will never clear
85 struct btrfs_ioctl_quota_rescan_args args;
87 printf("Quota data changed, quota rescan scheduled\n");
88 memset(&args, 0, sizeof(args));
89 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
91 fprintf(stderr, "ERROR: quota rescan failed: %s\n",
94 close_file_or_dir(fd, dirstream);
98 static int qgroup_create(int create, int argc, char **argv)
103 char *path = argv[2];
104 struct btrfs_ioctl_qgroup_create_args args;
105 DIR *dirstream = NULL;
107 if (check_argc_exact(argc, 3))
110 memset(&args, 0, sizeof(args));
111 args.create = create;
112 args.qgroupid = parse_qgroupid(argv[1]);
114 fd = open_file_or_dir(path, &dirstream);
116 fprintf(stderr, "ERROR: can't access '%s'\n", path);
120 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
122 close_file_or_dir(fd, dirstream);
124 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
125 create ? "create":"destroy", strerror(e));
131 static int parse_limit(const char *p, unsigned long long *s)
134 unsigned long long size;
135 unsigned long long CLEAR_VALUE = -1;
137 if (strcasecmp(p, "none") == 0) {
145 size = strtoull(p, &endptr, 10);
181 static const char * const cmd_qgroup_assign_usage[] = {
182 "btrfs qgroup assign <src> <dst> <path>",
183 "Enable subvolume qgroup support for a filesystem.",
187 static int cmd_qgroup_assign(int argc, char **argv)
189 int ret = qgroup_assign(1, argc, argv);
191 usage(cmd_qgroup_assign_usage);
195 static const char * const cmd_qgroup_remove_usage[] = {
196 "btrfs qgroup remove <src> <dst> <path>",
197 "Remove a subvol from a quota group.",
201 static int cmd_qgroup_remove(int argc, char **argv)
203 int ret = qgroup_assign(0, argc, argv);
205 usage(cmd_qgroup_remove_usage);
209 static const char * const cmd_qgroup_create_usage[] = {
210 "btrfs qgroup create <qgroupid> <path>",
211 "Create a subvolume quota group.",
215 static int cmd_qgroup_create(int argc, char **argv)
217 int ret = qgroup_create(1, argc, argv);
219 usage(cmd_qgroup_create_usage);
223 static const char * const cmd_qgroup_destroy_usage[] = {
224 "btrfs qgroup destroy <qgroupid> <path>",
225 "Destroy a subvolume quota group.",
229 static int cmd_qgroup_destroy(int argc, char **argv)
231 int ret = qgroup_create(0, argc, argv);
233 usage(cmd_qgroup_destroy_usage);
237 static const char * const cmd_qgroup_show_usage[] = {
238 "btrfs qgroup show -pcreFf "
239 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
240 "Show subvolume quota groups.",
241 "-p print parent qgroup id",
242 "-c print child qgroup id",
243 "-r print limit of referenced size of qgroup",
244 "-e print limit of exclusive size of qgroup",
245 "-F list all qgroups which impact the given path",
246 " (including ancestral qgroups)",
247 "-f list all qgroups which impact the given path",
248 " (excluding ancestral qgroups)",
249 "--raw raw numbers in bytes",
251 " human firendly numbers in given base, 1024 by default",
252 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
253 "--si use 1000 as a base (kB, MB, GB, TB)",
254 "--kbytes show sizes in KiB, or kB with --si",
255 "--mbytes show sizes in MiB, or MB with --si",
256 "--gbytes show sizes in GiB, or GB with --si",
257 "--tbytes show sizes in TiB, or TB with --si",
258 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
259 " list qgroups sorted by specified items",
260 " you can use '+' or '-' in front of each item.",
261 " (+:ascending, -:descending, ascending default)",
265 static int cmd_qgroup_show(int argc, char **argv)
271 DIR *dirstream = NULL;
274 unsigned unit_mode = UNITS_DEFAULT;
276 struct btrfs_qgroup_comparer_set *comparer_set;
277 struct btrfs_qgroup_filter_set *filter_set;
278 filter_set = btrfs_qgroup_alloc_filter_set();
279 comparer_set = btrfs_qgroup_alloc_comparer_set();
284 static const struct option long_options[] = {
285 {"sort", required_argument, NULL, 'S'},
286 {"raw", no_argument, NULL, GETOPT_VAL_RAW},
287 {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
288 {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
289 {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
290 {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
291 {"si", no_argument, NULL, GETOPT_VAL_SI},
292 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
293 { "human-readable", no_argument, NULL,
294 GETOPT_VAL_HUMAN_READABLE},
298 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
303 btrfs_qgroup_setup_print_column(
304 BTRFS_QGROUP_PARENT);
307 btrfs_qgroup_setup_print_column(
311 btrfs_qgroup_setup_print_column(
312 BTRFS_QGROUP_MAX_RFER);
315 btrfs_qgroup_setup_print_column(
316 BTRFS_QGROUP_MAX_EXCL);
325 ret = btrfs_qgroup_parse_sort_string(optarg,
328 usage(cmd_qgroup_show_usage);
331 unit_mode = UNITS_RAW;
333 case GETOPT_VAL_KBYTES:
334 units_set_base(&unit_mode, UNITS_KBYTES);
336 case GETOPT_VAL_MBYTES:
337 units_set_base(&unit_mode, UNITS_MBYTES);
339 case GETOPT_VAL_GBYTES:
340 units_set_base(&unit_mode, UNITS_GBYTES);
342 case GETOPT_VAL_TBYTES:
343 units_set_base(&unit_mode, UNITS_TBYTES);
346 units_set_mode(&unit_mode, UNITS_DECIMAL);
349 units_set_mode(&unit_mode, UNITS_BINARY);
351 case GETOPT_VAL_HUMAN_READABLE:
352 unit_mode = UNITS_HUMAN_BINARY;
355 usage(cmd_qgroup_show_usage);
358 btrfs_qgroup_setup_units(unit_mode);
360 if (check_argc_exact(argc - optind, 1))
361 usage(cmd_qgroup_show_usage);
364 fd = open_file_or_dir(path, &dirstream);
366 fprintf(stderr, "ERROR: can't access '%s'\n", path);
371 qgroupid = btrfs_get_path_rootid(fd);
372 if (filter_flag & 0x1)
373 btrfs_qgroup_setup_filter(&filter_set,
374 BTRFS_QGROUP_FILTER_ALL_PARENT,
376 if (filter_flag & 0x2)
377 btrfs_qgroup_setup_filter(&filter_set,
378 BTRFS_QGROUP_FILTER_PARENT,
381 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
383 close_file_or_dir(fd, dirstream);
385 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
391 static const char * const cmd_qgroup_limit_usage[] = {
392 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
393 "Limit the size of a subvolume quota group.",
395 "-c limit amount of data after compression. This is the default,",
396 " it is currently not possible to turn off this option.",
397 "-e limit space exclusively assigned to this qgroup",
401 static int cmd_qgroup_limit(int argc, char **argv)
407 struct btrfs_ioctl_qgroup_limit_args args;
408 unsigned long long size;
411 DIR *dirstream = NULL;
415 int c = getopt(argc, argv, "ce");
426 usage(cmd_qgroup_limit_usage);
430 if (check_argc_min(argc - optind, 2))
431 usage(cmd_qgroup_limit_usage);
433 if (!parse_limit(argv[optind], &size)) {
434 fprintf(stderr, "Invalid size argument given\n");
438 memset(&args, 0, sizeof(args));
440 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
441 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
443 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
444 args.lim.max_exclusive = size;
446 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
447 args.lim.max_referenced = size;
450 if (argc - optind == 2) {
452 path = argv[optind + 1];
453 ret = test_issubvolume(path);
455 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
459 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
464 * keep qgroupid at 0, this indicates that the subvolume the
465 * fd refers to is to be limited
467 } else if (argc - optind == 3) {
468 args.qgroupid = parse_qgroupid(argv[optind + 1]);
469 path = argv[optind + 2];
471 usage(cmd_qgroup_limit_usage);
473 fd = open_file_or_dir(path, &dirstream);
475 fprintf(stderr, "ERROR: can't access '%s'\n", path);
479 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
481 close_file_or_dir(fd, dirstream);
483 fprintf(stderr, "ERROR: unable to limit requested quota group: "
484 "%s\n", strerror(e));
490 static const char qgroup_cmd_group_info[] =
491 "manage quota groups";
493 const struct cmd_group qgroup_cmd_group = {
494 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
495 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
497 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
499 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
501 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
503 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
505 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
511 int cmd_qgroup(int argc, char **argv)
513 return handle_command_group(&qgroup_cmd_group, argc, argv);