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 ((args.src >> 48) >= (args.dst >> 48)) {
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;
114 if (strcasecmp(p, "none") == 0) {
118 size = strtoull(p, &endptr, 10);
151 static const char * const cmd_qgroup_assign_usage[] = {
152 "btrfs qgroup assign <src> <dst> <path>",
153 "Enable subvolume qgroup support for a filesystem.",
157 static int cmd_qgroup_assign(int argc, char **argv)
159 int ret = qgroup_assign(1, argc, argv);
161 usage(cmd_qgroup_assign_usage);
165 static const char * const cmd_qgroup_remove_usage[] = {
166 "btrfs qgroup remove <src> <dst> <path>",
167 "Remove a subvol from a quota group.",
171 static int cmd_qgroup_remove(int argc, char **argv)
173 int ret = qgroup_assign(0, argc, argv);
175 usage(cmd_qgroup_remove_usage);
179 static const char * const cmd_qgroup_create_usage[] = {
180 "btrfs qgroup create <qgroupid> <path>",
181 "Create a subvolume quota group.",
185 static int cmd_qgroup_create(int argc, char **argv)
187 int ret = qgroup_create(1, argc, argv);
189 usage(cmd_qgroup_create_usage);
193 static const char * const cmd_qgroup_destroy_usage[] = {
194 "btrfs qgroup destroy <qgroupid> <path>",
195 "Destroy a subvolume quota group.",
199 static int cmd_qgroup_destroy(int argc, char **argv)
201 int ret = qgroup_create(0, argc, argv);
203 usage(cmd_qgroup_destroy_usage);
207 static const char * const cmd_qgroup_show_usage[] = {
208 "btrfs qgroup show -pcreFf "
209 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
210 "Show subvolume quota groups.",
211 "-p print parent qgroup id",
212 "-c print child qgroup id",
213 "-r print max referenced size of qgroup",
214 "-e print max exclusive size of qgroup",
215 "-F list all qgroups which impact the given path"
216 "(include ancestral qgroups)",
217 "-f list all qgroups which impact the given path"
218 "(exclude ancestral qgroups)",
219 "--raw raw numbers in bytes",
220 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
221 "--si use 1000 as a base (kB, MB, GB, TB)",
222 "--kbytes show sizes in KiB, or kB with --si",
223 "--mbytes show sizes in MiB, or MB with --si",
224 "--gbytes show sizes in GiB, or GB with --si",
225 "--tbytes show sizes in TiB, or TB with --si",
226 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
227 " list qgroups in order of qgroupid,"
228 "rfer,max_rfer or max_excl",
229 " you can use '+' or '-' in front of each item.",
230 " (+:ascending, -:descending, ascending default)",
234 static int cmd_qgroup_show(int argc, char **argv)
240 DIR *dirstream = NULL;
243 unsigned unit_mode = UNITS_DEFAULT;
245 struct btrfs_qgroup_comparer_set *comparer_set;
246 struct btrfs_qgroup_filter_set *filter_set;
247 filter_set = btrfs_qgroup_alloc_filter_set();
248 comparer_set = btrfs_qgroup_alloc_comparer_set();
253 int option_index = 0;
254 static const struct option long_options[] = {
255 {"sort", 1, NULL, 'S'},
256 {"raw", no_argument, NULL, 0},
257 {"kbytes", no_argument, NULL, 0},
258 {"mbytes", no_argument, NULL, 0},
259 {"gbytes", no_argument, NULL, 0},
260 {"tbytes", no_argument, NULL, 0},
261 {"si", no_argument, NULL, GETOPT_VAL_SI},
262 {"iec", no_argument, NULL, GETOPT_VAL_IEC},
265 c = getopt_long(argc, argv, "pcreFf",
266 long_options, &option_index);
272 btrfs_qgroup_setup_print_column(
273 BTRFS_QGROUP_PARENT);
276 btrfs_qgroup_setup_print_column(
280 btrfs_qgroup_setup_print_column(
281 BTRFS_QGROUP_MAX_RFER);
284 btrfs_qgroup_setup_print_column(
285 BTRFS_QGROUP_MAX_EXCL);
294 ret = btrfs_qgroup_parse_sort_string(optarg,
297 usage(cmd_qgroup_show_usage);
300 if (option_index == 1)
301 unit_mode = UNITS_RAW;
302 else if (option_index == 2)
303 units_set_base(&unit_mode, UNITS_KBYTES);
304 else if (option_index == 3)
305 units_set_base(&unit_mode, UNITS_MBYTES);
306 else if (option_index == 4)
307 units_set_base(&unit_mode, UNITS_GBYTES);
308 else if (option_index == 5)
309 units_set_base(&unit_mode, UNITS_TBYTES);
312 units_set_mode(&unit_mode, UNITS_DECIMAL);
315 units_set_mode(&unit_mode, UNITS_BINARY);
318 usage(cmd_qgroup_show_usage);
321 btrfs_qgroup_setup_units(unit_mode);
323 if (check_argc_exact(argc - optind, 1))
324 usage(cmd_qgroup_show_usage);
327 fd = open_file_or_dir(path, &dirstream);
329 fprintf(stderr, "ERROR: can't access '%s'\n", path);
334 qgroupid = btrfs_get_path_rootid(fd);
335 if (filter_flag & 0x1)
336 btrfs_qgroup_setup_filter(&filter_set,
337 BTRFS_QGROUP_FILTER_ALL_PARENT,
339 if (filter_flag & 0x2)
340 btrfs_qgroup_setup_filter(&filter_set,
341 BTRFS_QGROUP_FILTER_PARENT,
344 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
346 close_file_or_dir(fd, dirstream);
348 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
354 static const char * const cmd_qgroup_limit_usage[] = {
355 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
356 "Limit the size of a subvolume quota group.",
358 "-c limit amount of data after compression. This is the default,",
359 " it is currently not possible to turn off this option.",
360 "-e limit space exclusively assigned to this qgroup",
364 static int cmd_qgroup_limit(int argc, char **argv)
370 struct btrfs_ioctl_qgroup_limit_args args;
371 unsigned long long size;
374 DIR *dirstream = NULL;
378 int c = getopt(argc, argv, "ce");
389 usage(cmd_qgroup_limit_usage);
393 if (check_argc_min(argc - optind, 2))
394 usage(cmd_qgroup_limit_usage);
396 if (!parse_limit(argv[optind], &size)) {
397 fprintf(stderr, "Invalid size argument given\n");
401 memset(&args, 0, sizeof(args));
404 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
405 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
407 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
408 args.lim.max_exclusive = size;
410 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
411 args.lim.max_referenced = size;
415 if (argc - optind == 2) {
417 path = argv[optind + 1];
418 ret = test_issubvolume(path);
420 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
424 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
429 * keep qgroupid at 0, this indicates that the subvolume the
430 * fd refers to is to be limited
432 } else if (argc - optind == 3) {
433 args.qgroupid = parse_qgroupid(argv[optind + 1]);
434 path = argv[optind + 2];
436 usage(cmd_qgroup_limit_usage);
438 fd = open_file_or_dir(path, &dirstream);
440 fprintf(stderr, "ERROR: can't access '%s'\n", path);
444 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
446 close_file_or_dir(fd, dirstream);
448 fprintf(stderr, "ERROR: unable to limit requested quota group: "
449 "%s\n", strerror(e));
455 const struct cmd_group qgroup_cmd_group = {
456 qgroup_cmd_group_usage, NULL, {
457 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
459 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
461 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
463 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
465 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
467 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
473 int cmd_qgroup(int argc, char **argv)
475 return handle_command_group(&qgroup_cmd_group, argc, argv);