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 create quota group: %s\n",
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);
148 static const char * const cmd_qgroup_assign_usage[] = {
149 "btrfs qgroup assign <src> <dst> <path>",
150 "Enable subvolume qgroup support for a filesystem.",
154 static int cmd_qgroup_assign(int argc, char **argv)
156 int ret = qgroup_assign(1, argc, argv);
158 usage(cmd_qgroup_assign_usage);
162 static const char * const cmd_qgroup_remove_usage[] = {
163 "btrfs qgroup remove <src> <dst> <path>",
164 "Remove a subvol from a quota group.",
168 static int cmd_qgroup_remove(int argc, char **argv)
170 int ret = qgroup_assign(0, argc, argv);
172 usage(cmd_qgroup_remove_usage);
176 static const char * const cmd_qgroup_create_usage[] = {
177 "btrfs qgroup create <qgroupid> <path>",
178 "Create a subvolume quota group.",
182 static int cmd_qgroup_create(int argc, char **argv)
184 int ret = qgroup_create(1, argc, argv);
186 usage(cmd_qgroup_create_usage);
190 static const char * const cmd_qgroup_destroy_usage[] = {
191 "btrfs qgroup destroy <qgroupid> <path>",
192 "Destroy a subvolume quota group.",
196 static int cmd_qgroup_destroy(int argc, char **argv)
198 int ret = qgroup_create(0, argc, argv);
200 usage(cmd_qgroup_destroy_usage);
204 static const char * const cmd_qgroup_show_usage[] = {
205 "btrfs qgroup show -pcreF <path>",
206 "Show subvolume quota groups.",
207 "-p print parent qgroup id",
208 "-c print child qgroup id",
209 "-r print max referenced size of qgroup",
210 "-e print max exclusive size of qgroup",
211 "-F list all qgroups which impact the given path"
212 "(include ancestral qgroups)",
216 static int cmd_qgroup_show(int argc, char **argv)
222 DIR *dirstream = NULL;
227 struct btrfs_qgroup_filter_set *filter_set;
228 filter_set = btrfs_qgroup_alloc_filter_set();
232 c = getopt(argc, argv, "pcreF");
237 btrfs_qgroup_setup_print_column(
238 BTRFS_QGROUP_PARENT);
241 btrfs_qgroup_setup_print_column(
245 btrfs_qgroup_setup_print_column(
246 BTRFS_QGROUP_MAX_RFER);
249 btrfs_qgroup_setup_print_column(
250 BTRFS_QGROUP_MAX_EXCL);
256 usage(cmd_qgroup_show_usage);
259 if (check_argc_exact(argc - optind, 1))
260 usage(cmd_qgroup_show_usage);
263 fd = open_file_or_dir(path, &dirstream);
265 fprintf(stderr, "ERROR: can't access '%s'\n", path);
270 qgroupid = btrfs_get_path_rootid(fd);
271 btrfs_qgroup_setup_filter(&filter_set,
272 BTRFS_QGROUP_FILTER_ALL_PARENT,
275 ret = btrfs_show_qgroups(fd, filter_set);
277 close_file_or_dir(fd, dirstream);
279 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
285 static const char * const cmd_qgroup_limit_usage[] = {
286 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
287 "Limit the size of a subvolume quota group.",
289 "-c limit amount of data after compression. This is the default,",
290 " it is currently not possible to turn off this option.",
291 "-e limit space exclusively assigned to this qgroup",
295 static int cmd_qgroup_limit(int argc, char **argv)
301 struct btrfs_ioctl_qgroup_limit_args args;
302 unsigned long long size;
305 DIR *dirstream = NULL;
309 int c = getopt(argc, argv, "ce");
320 usage(cmd_qgroup_limit_usage);
324 if (check_argc_min(argc - optind, 2))
325 usage(cmd_qgroup_limit_usage);
327 if (!parse_limit(argv[optind], &size)) {
328 fprintf(stderr, "Invalid size argument given\n");
332 memset(&args, 0, sizeof(args));
335 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
336 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
338 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
339 args.lim.max_exclusive = size;
341 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
342 args.lim.max_referenced = size;
346 if (argc - optind == 2) {
348 path = argv[optind + 1];
349 ret = test_issubvolume(path);
351 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
355 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
360 * keep qgroupid at 0, this indicates that the subvolume the
361 * fd refers to is to be limited
363 } else if (argc - optind == 3) {
364 args.qgroupid = parse_qgroupid(argv[optind + 1]);
365 path = argv[optind + 2];
367 usage(cmd_qgroup_limit_usage);
369 fd = open_file_or_dir(path, &dirstream);
371 fprintf(stderr, "ERROR: can't access '%s'\n", path);
375 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
377 close_file_or_dir(fd, dirstream);
379 fprintf(stderr, "ERROR: unable to limit requested quota group: "
380 "%s\n", strerror(e));
386 const struct cmd_group qgroup_cmd_group = {
387 qgroup_cmd_group_usage, NULL, {
388 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
390 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
392 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
394 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
396 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
398 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
404 int cmd_qgroup(int argc, char **argv)
406 return handle_command_group(&qgroup_cmd_group, argc, argv);