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);
156 static const char * const cmd_qgroup_assign_usage[] = {
157 "btrfs qgroup assign <src> <dst> <path>",
158 "Enable subvolume qgroup support for a filesystem.",
162 static int cmd_qgroup_assign(int argc, char **argv)
164 int ret = qgroup_assign(1, argc, argv);
166 usage(cmd_qgroup_assign_usage);
170 static const char * const cmd_qgroup_remove_usage[] = {
171 "btrfs qgroup remove <src> <dst> <path>",
172 "Remove a subvol from a quota group.",
176 static int cmd_qgroup_remove(int argc, char **argv)
178 int ret = qgroup_assign(0, argc, argv);
180 usage(cmd_qgroup_remove_usage);
184 static const char * const cmd_qgroup_create_usage[] = {
185 "btrfs qgroup create <qgroupid> <path>",
186 "Create a subvolume quota group.",
190 static int cmd_qgroup_create(int argc, char **argv)
192 int ret = qgroup_create(1, argc, argv);
194 usage(cmd_qgroup_create_usage);
198 static const char * const cmd_qgroup_destroy_usage[] = {
199 "btrfs qgroup destroy <qgroupid> <path>",
200 "Destroy a subvolume quota group.",
204 static int cmd_qgroup_destroy(int argc, char **argv)
206 int ret = qgroup_create(0, argc, argv);
208 usage(cmd_qgroup_destroy_usage);
212 static const char * const cmd_qgroup_show_usage[] = {
213 "btrfs qgroup show -pcreFf "
214 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
215 "Show subvolume quota groups.",
216 "-p print parent qgroup id",
217 "-c print child qgroup id",
218 "-r print limit of referenced size of qgroup",
219 "-e print limit of exclusive size of qgroup",
220 "-F list all qgroups which impact the given path",
221 " (including ancestral qgroups)",
222 "-f list all qgroups which impact the given path",
223 " (excluding ancestral qgroups)",
224 "--raw raw numbers in bytes",
226 " human firendly numbers in given base, 1024 by default",
227 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
228 "--si use 1000 as a base (kB, MB, GB, TB)",
229 "--kbytes show sizes in KiB, or kB with --si",
230 "--mbytes show sizes in MiB, or MB with --si",
231 "--gbytes show sizes in GiB, or GB with --si",
232 "--tbytes show sizes in TiB, or TB with --si",
233 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
234 " list qgroups sorted by specified items",
235 " you can use '+' or '-' in front of each item.",
236 " (+:ascending, -:descending, ascending default)",
240 static int cmd_qgroup_show(int argc, char **argv)
246 DIR *dirstream = NULL;
249 unsigned unit_mode = UNITS_DEFAULT;
251 struct btrfs_qgroup_comparer_set *comparer_set;
252 struct btrfs_qgroup_filter_set *filter_set;
253 filter_set = btrfs_qgroup_alloc_filter_set();
254 comparer_set = btrfs_qgroup_alloc_comparer_set();
259 static const struct option long_options[] = {
260 {"sort", required_argument, NULL, 'S'},
261 {"raw", no_argument, NULL, GETOPT_VAL_RAW},
262 {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
263 {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
264 {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
265 {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
266 {"si", no_argument, NULL, GETOPT_VAL_SI},
267 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
268 { "human-readable", no_argument, NULL,
269 GETOPT_VAL_HUMAN_READABLE},
273 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
278 btrfs_qgroup_setup_print_column(
279 BTRFS_QGROUP_PARENT);
282 btrfs_qgroup_setup_print_column(
286 btrfs_qgroup_setup_print_column(
287 BTRFS_QGROUP_MAX_RFER);
290 btrfs_qgroup_setup_print_column(
291 BTRFS_QGROUP_MAX_EXCL);
300 ret = btrfs_qgroup_parse_sort_string(optarg,
303 usage(cmd_qgroup_show_usage);
306 unit_mode = UNITS_RAW;
308 case GETOPT_VAL_KBYTES:
309 units_set_base(&unit_mode, UNITS_KBYTES);
311 case GETOPT_VAL_MBYTES:
312 units_set_base(&unit_mode, UNITS_MBYTES);
314 case GETOPT_VAL_GBYTES:
315 units_set_base(&unit_mode, UNITS_GBYTES);
317 case GETOPT_VAL_TBYTES:
318 units_set_base(&unit_mode, UNITS_TBYTES);
321 units_set_mode(&unit_mode, UNITS_DECIMAL);
324 units_set_mode(&unit_mode, UNITS_BINARY);
326 case GETOPT_VAL_HUMAN_READABLE:
327 unit_mode = UNITS_HUMAN_BINARY;
330 usage(cmd_qgroup_show_usage);
333 btrfs_qgroup_setup_units(unit_mode);
335 if (check_argc_exact(argc - optind, 1))
336 usage(cmd_qgroup_show_usage);
339 fd = open_file_or_dir(path, &dirstream);
341 fprintf(stderr, "ERROR: can't access '%s'\n", path);
346 qgroupid = btrfs_get_path_rootid(fd);
347 if (filter_flag & 0x1)
348 btrfs_qgroup_setup_filter(&filter_set,
349 BTRFS_QGROUP_FILTER_ALL_PARENT,
351 if (filter_flag & 0x2)
352 btrfs_qgroup_setup_filter(&filter_set,
353 BTRFS_QGROUP_FILTER_PARENT,
356 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
358 close_file_or_dir(fd, dirstream);
360 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
366 static const char * const cmd_qgroup_limit_usage[] = {
367 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
368 "Limit the size of a subvolume quota group.",
370 "-c limit amount of data after compression. This is the default,",
371 " it is currently not possible to turn off this option.",
372 "-e limit space exclusively assigned to this qgroup",
376 static int cmd_qgroup_limit(int argc, char **argv)
382 struct btrfs_ioctl_qgroup_limit_args args;
383 unsigned long long size;
386 DIR *dirstream = NULL;
390 int c = getopt(argc, argv, "ce");
401 usage(cmd_qgroup_limit_usage);
405 if (check_argc_min(argc - optind, 2))
406 usage(cmd_qgroup_limit_usage);
408 if (!parse_limit(argv[optind], &size)) {
409 fprintf(stderr, "Invalid size argument given\n");
413 memset(&args, 0, sizeof(args));
415 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
416 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
418 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
419 args.lim.max_exclusive = size;
421 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
422 args.lim.max_referenced = size;
425 if (argc - optind == 2) {
427 path = argv[optind + 1];
428 ret = test_issubvolume(path);
430 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
434 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
439 * keep qgroupid at 0, this indicates that the subvolume the
440 * fd refers to is to be limited
442 } else if (argc - optind == 3) {
443 args.qgroupid = parse_qgroupid(argv[optind + 1]);
444 path = argv[optind + 2];
446 usage(cmd_qgroup_limit_usage);
448 fd = open_file_or_dir(path, &dirstream);
450 fprintf(stderr, "ERROR: can't access '%s'\n", path);
454 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
456 close_file_or_dir(fd, dirstream);
458 fprintf(stderr, "ERROR: unable to limit requested quota group: "
459 "%s\n", strerror(e));
465 const struct cmd_group qgroup_cmd_group = {
466 qgroup_cmd_group_usage, NULL, {
467 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
469 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
471 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
473 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
475 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
477 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
483 int cmd_qgroup(int argc, char **argv)
485 return handle_command_group(&qgroup_cmd_group, argc, argv);