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) {
123 size = strtoull(p, &endptr, 10);
159 static const char * const cmd_qgroup_assign_usage[] = {
160 "btrfs qgroup assign <src> <dst> <path>",
161 "Enable subvolume qgroup support for a filesystem.",
165 static int cmd_qgroup_assign(int argc, char **argv)
167 int ret = qgroup_assign(1, argc, argv);
169 usage(cmd_qgroup_assign_usage);
173 static const char * const cmd_qgroup_remove_usage[] = {
174 "btrfs qgroup remove <src> <dst> <path>",
175 "Remove a subvol from a quota group.",
179 static int cmd_qgroup_remove(int argc, char **argv)
181 int ret = qgroup_assign(0, argc, argv);
183 usage(cmd_qgroup_remove_usage);
187 static const char * const cmd_qgroup_create_usage[] = {
188 "btrfs qgroup create <qgroupid> <path>",
189 "Create a subvolume quota group.",
193 static int cmd_qgroup_create(int argc, char **argv)
195 int ret = qgroup_create(1, argc, argv);
197 usage(cmd_qgroup_create_usage);
201 static const char * const cmd_qgroup_destroy_usage[] = {
202 "btrfs qgroup destroy <qgroupid> <path>",
203 "Destroy a subvolume quota group.",
207 static int cmd_qgroup_destroy(int argc, char **argv)
209 int ret = qgroup_create(0, argc, argv);
211 usage(cmd_qgroup_destroy_usage);
215 static const char * const cmd_qgroup_show_usage[] = {
216 "btrfs qgroup show -pcreFf "
217 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
218 "Show subvolume quota groups.",
219 "-p print parent qgroup id",
220 "-c print child qgroup id",
221 "-r print limit of referenced size of qgroup",
222 "-e print limit of exclusive size of qgroup",
223 "-F list all qgroups which impact the given path",
224 " (including ancestral qgroups)",
225 "-f list all qgroups which impact the given path",
226 " (excluding ancestral qgroups)",
227 "--raw raw numbers in bytes",
229 " human firendly numbers in given base, 1024 by default",
230 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
231 "--si use 1000 as a base (kB, MB, GB, TB)",
232 "--kbytes show sizes in KiB, or kB with --si",
233 "--mbytes show sizes in MiB, or MB with --si",
234 "--gbytes show sizes in GiB, or GB with --si",
235 "--tbytes show sizes in TiB, or TB with --si",
236 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
237 " list qgroups sorted by specified items",
238 " you can use '+' or '-' in front of each item.",
239 " (+:ascending, -:descending, ascending default)",
243 static int cmd_qgroup_show(int argc, char **argv)
249 DIR *dirstream = NULL;
252 unsigned unit_mode = UNITS_DEFAULT;
254 struct btrfs_qgroup_comparer_set *comparer_set;
255 struct btrfs_qgroup_filter_set *filter_set;
256 filter_set = btrfs_qgroup_alloc_filter_set();
257 comparer_set = btrfs_qgroup_alloc_comparer_set();
262 static const struct option long_options[] = {
263 {"sort", required_argument, NULL, 'S'},
264 {"raw", no_argument, NULL, GETOPT_VAL_RAW},
265 {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
266 {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
267 {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
268 {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
269 {"si", no_argument, NULL, GETOPT_VAL_SI},
270 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
271 { "human-readable", no_argument, NULL,
272 GETOPT_VAL_HUMAN_READABLE},
276 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
281 btrfs_qgroup_setup_print_column(
282 BTRFS_QGROUP_PARENT);
285 btrfs_qgroup_setup_print_column(
289 btrfs_qgroup_setup_print_column(
290 BTRFS_QGROUP_MAX_RFER);
293 btrfs_qgroup_setup_print_column(
294 BTRFS_QGROUP_MAX_EXCL);
303 ret = btrfs_qgroup_parse_sort_string(optarg,
306 usage(cmd_qgroup_show_usage);
309 unit_mode = UNITS_RAW;
311 case GETOPT_VAL_KBYTES:
312 units_set_base(&unit_mode, UNITS_KBYTES);
314 case GETOPT_VAL_MBYTES:
315 units_set_base(&unit_mode, UNITS_MBYTES);
317 case GETOPT_VAL_GBYTES:
318 units_set_base(&unit_mode, UNITS_GBYTES);
320 case GETOPT_VAL_TBYTES:
321 units_set_base(&unit_mode, UNITS_TBYTES);
324 units_set_mode(&unit_mode, UNITS_DECIMAL);
327 units_set_mode(&unit_mode, UNITS_BINARY);
329 case GETOPT_VAL_HUMAN_READABLE:
330 unit_mode = UNITS_HUMAN_BINARY;
333 usage(cmd_qgroup_show_usage);
336 btrfs_qgroup_setup_units(unit_mode);
338 if (check_argc_exact(argc - optind, 1))
339 usage(cmd_qgroup_show_usage);
342 fd = open_file_or_dir(path, &dirstream);
344 fprintf(stderr, "ERROR: can't access '%s'\n", path);
349 qgroupid = btrfs_get_path_rootid(fd);
350 if (filter_flag & 0x1)
351 btrfs_qgroup_setup_filter(&filter_set,
352 BTRFS_QGROUP_FILTER_ALL_PARENT,
354 if (filter_flag & 0x2)
355 btrfs_qgroup_setup_filter(&filter_set,
356 BTRFS_QGROUP_FILTER_PARENT,
359 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
361 close_file_or_dir(fd, dirstream);
363 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
369 static const char * const cmd_qgroup_limit_usage[] = {
370 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
371 "Limit the size of a subvolume quota group.",
373 "-c limit amount of data after compression. This is the default,",
374 " it is currently not possible to turn off this option.",
375 "-e limit space exclusively assigned to this qgroup",
379 static int cmd_qgroup_limit(int argc, char **argv)
385 struct btrfs_ioctl_qgroup_limit_args args;
386 unsigned long long size;
389 DIR *dirstream = NULL;
393 int c = getopt(argc, argv, "ce");
404 usage(cmd_qgroup_limit_usage);
408 if (check_argc_min(argc - optind, 2))
409 usage(cmd_qgroup_limit_usage);
411 if (!parse_limit(argv[optind], &size)) {
412 fprintf(stderr, "Invalid size argument given\n");
416 memset(&args, 0, sizeof(args));
418 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
419 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
421 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
422 args.lim.max_exclusive = size;
424 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
425 args.lim.max_referenced = size;
428 if (argc - optind == 2) {
430 path = argv[optind + 1];
431 ret = test_issubvolume(path);
433 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
437 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
442 * keep qgroupid at 0, this indicates that the subvolume the
443 * fd refers to is to be limited
445 } else if (argc - optind == 3) {
446 args.qgroupid = parse_qgroupid(argv[optind + 1]);
447 path = argv[optind + 2];
449 usage(cmd_qgroup_limit_usage);
451 fd = open_file_or_dir(path, &dirstream);
453 fprintf(stderr, "ERROR: can't access '%s'\n", path);
457 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
459 close_file_or_dir(fd, dirstream);
461 fprintf(stderr, "ERROR: unable to limit requested quota group: "
462 "%s\n", strerror(e));
468 static const char qgroup_cmd_group_info[] =
469 "manage quota groups";
471 const struct cmd_group qgroup_cmd_group = {
472 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
473 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
475 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
477 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
479 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
481 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
483 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
489 int cmd_qgroup(int argc, char **argv)
491 return handle_command_group(&qgroup_cmd_group, argc, argv);