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]);
53 * FIXME src should accept subvol path
55 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
56 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
59 fd = open_file_or_dir(path, &dirstream);
61 fprintf(stderr, "ERROR: can't access '%s'\n", path);
65 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
67 close_file_or_dir(fd, dirstream);
69 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
76 static int qgroup_create(int create, int argc, char **argv)
82 struct btrfs_ioctl_qgroup_create_args args;
83 DIR *dirstream = NULL;
85 if (check_argc_exact(argc, 3))
88 memset(&args, 0, sizeof(args));
90 args.qgroupid = parse_qgroupid(argv[1]);
92 fd = open_file_or_dir(path, &dirstream);
94 fprintf(stderr, "ERROR: can't access '%s'\n", path);
98 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
100 close_file_or_dir(fd, dirstream);
102 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
103 create ? "create":"destroy", strerror(e));
109 static int parse_limit(const char *p, unsigned long long *s)
112 unsigned long long size;
113 unsigned long long CLEAR_VALUE = -1;
115 if (strcasecmp(p, "none") == 0) {
119 size = strtoull(p, &endptr, 10);
152 static const char * const cmd_qgroup_assign_usage[] = {
153 "btrfs qgroup assign <src> <dst> <path>",
154 "Enable subvolume qgroup support for a filesystem.",
158 static int cmd_qgroup_assign(int argc, char **argv)
160 int ret = qgroup_assign(1, argc, argv);
162 usage(cmd_qgroup_assign_usage);
166 static const char * const cmd_qgroup_remove_usage[] = {
167 "btrfs qgroup remove <src> <dst> <path>",
168 "Remove a subvol from a quota group.",
172 static int cmd_qgroup_remove(int argc, char **argv)
174 int ret = qgroup_assign(0, argc, argv);
176 usage(cmd_qgroup_remove_usage);
180 static const char * const cmd_qgroup_create_usage[] = {
181 "btrfs qgroup create <qgroupid> <path>",
182 "Create a subvolume quota group.",
186 static int cmd_qgroup_create(int argc, char **argv)
188 int ret = qgroup_create(1, argc, argv);
190 usage(cmd_qgroup_create_usage);
194 static const char * const cmd_qgroup_destroy_usage[] = {
195 "btrfs qgroup destroy <qgroupid> <path>",
196 "Destroy a subvolume quota group.",
200 static int cmd_qgroup_destroy(int argc, char **argv)
202 int ret = qgroup_create(0, argc, argv);
204 usage(cmd_qgroup_destroy_usage);
208 static const char * const cmd_qgroup_show_usage[] = {
209 "btrfs qgroup show -pcreFf "
210 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
211 "Show subvolume quota groups.",
212 "-p print parent qgroup id",
213 "-c print child qgroup id",
214 "-r print limit of referenced size of qgroup",
215 "-e print limit of exclusive size of qgroup",
216 "-F list all qgroups which impact the given path",
217 " (including ancestral qgroups)",
218 "-f list all qgroups which impact the given path",
219 " (excluding ancestral qgroups)",
220 "--raw raw numbers in bytes",
222 " human firendly numbers in given base, 1024 by default",
223 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
224 "--si use 1000 as a base (kB, MB, GB, TB)",
225 "--kbytes show sizes in KiB, or kB with --si",
226 "--mbytes show sizes in MiB, or MB with --si",
227 "--gbytes show sizes in GiB, or GB with --si",
228 "--tbytes show sizes in TiB, or TB with --si",
229 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
230 " list qgroups sorted by specified items",
231 " you can use '+' or '-' in front of each item.",
232 " (+:ascending, -:descending, ascending default)",
236 static int cmd_qgroup_show(int argc, char **argv)
242 DIR *dirstream = NULL;
245 unsigned unit_mode = UNITS_DEFAULT;
247 struct btrfs_qgroup_comparer_set *comparer_set;
248 struct btrfs_qgroup_filter_set *filter_set;
249 filter_set = btrfs_qgroup_alloc_filter_set();
250 comparer_set = btrfs_qgroup_alloc_comparer_set();
255 static const struct option long_options[] = {
256 {"sort", required_argument, NULL, 'S'},
257 {"raw", no_argument, NULL, GETOPT_VAL_RAW},
258 {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
259 {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
260 {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
261 {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
262 {"si", no_argument, NULL, GETOPT_VAL_SI},
263 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
264 { "human-readable", no_argument, NULL,
265 GETOPT_VAL_HUMAN_READABLE},
269 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
274 btrfs_qgroup_setup_print_column(
275 BTRFS_QGROUP_PARENT);
278 btrfs_qgroup_setup_print_column(
282 btrfs_qgroup_setup_print_column(
283 BTRFS_QGROUP_MAX_RFER);
286 btrfs_qgroup_setup_print_column(
287 BTRFS_QGROUP_MAX_EXCL);
296 ret = btrfs_qgroup_parse_sort_string(optarg,
299 usage(cmd_qgroup_show_usage);
302 unit_mode = UNITS_RAW;
304 case GETOPT_VAL_KBYTES:
305 units_set_base(&unit_mode, UNITS_KBYTES);
307 case GETOPT_VAL_MBYTES:
308 units_set_base(&unit_mode, UNITS_MBYTES);
310 case GETOPT_VAL_GBYTES:
311 units_set_base(&unit_mode, UNITS_GBYTES);
313 case GETOPT_VAL_TBYTES:
314 units_set_base(&unit_mode, UNITS_TBYTES);
317 units_set_mode(&unit_mode, UNITS_DECIMAL);
320 units_set_mode(&unit_mode, UNITS_BINARY);
322 case GETOPT_VAL_HUMAN_READABLE:
323 unit_mode = UNITS_HUMAN_BINARY;
326 usage(cmd_qgroup_show_usage);
329 btrfs_qgroup_setup_units(unit_mode);
331 if (check_argc_exact(argc - optind, 1))
332 usage(cmd_qgroup_show_usage);
335 fd = open_file_or_dir(path, &dirstream);
337 fprintf(stderr, "ERROR: can't access '%s'\n", path);
342 qgroupid = btrfs_get_path_rootid(fd);
343 if (filter_flag & 0x1)
344 btrfs_qgroup_setup_filter(&filter_set,
345 BTRFS_QGROUP_FILTER_ALL_PARENT,
347 if (filter_flag & 0x2)
348 btrfs_qgroup_setup_filter(&filter_set,
349 BTRFS_QGROUP_FILTER_PARENT,
352 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
354 close_file_or_dir(fd, dirstream);
356 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
362 static const char * const cmd_qgroup_limit_usage[] = {
363 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
364 "Limit the size of a subvolume quota group.",
366 "-c limit amount of data after compression. This is the default,",
367 " it is currently not possible to turn off this option.",
368 "-e limit space exclusively assigned to this qgroup",
372 static int cmd_qgroup_limit(int argc, char **argv)
378 struct btrfs_ioctl_qgroup_limit_args args;
379 unsigned long long size;
382 DIR *dirstream = NULL;
386 int c = getopt(argc, argv, "ce");
397 usage(cmd_qgroup_limit_usage);
401 if (check_argc_min(argc - optind, 2))
402 usage(cmd_qgroup_limit_usage);
404 if (!parse_limit(argv[optind], &size)) {
405 fprintf(stderr, "Invalid size argument given\n");
409 memset(&args, 0, sizeof(args));
411 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
412 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
414 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
415 args.lim.max_exclusive = size;
417 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
418 args.lim.max_referenced = size;
421 if (argc - optind == 2) {
423 path = argv[optind + 1];
424 ret = test_issubvolume(path);
426 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
430 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
435 * keep qgroupid at 0, this indicates that the subvolume the
436 * fd refers to is to be limited
438 } else if (argc - optind == 3) {
439 args.qgroupid = parse_qgroupid(argv[optind + 1]);
440 path = argv[optind + 2];
442 usage(cmd_qgroup_limit_usage);
444 fd = open_file_or_dir(path, &dirstream);
446 fprintf(stderr, "ERROR: can't access '%s'\n", path);
450 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
452 close_file_or_dir(fd, dirstream);
454 fprintf(stderr, "ERROR: unable to limit requested quota group: "
455 "%s\n", strerror(e));
461 const struct cmd_group qgroup_cmd_group = {
462 qgroup_cmd_group_usage, NULL, {
463 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
465 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
467 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
469 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
471 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
473 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
479 int cmd_qgroup(int argc, char **argv)
481 return handle_command_group(&qgroup_cmd_group, argc, argv);