btrfs-progs: use calloc instead of malloc+memset for tree roots
[btrfs-progs-unstable/devel.git] / cmds-qgroup.c
blob48c173338d13dd35e41c4db3e778ad24375def8c
1 /*
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>
20 #include <unistd.h>
21 #include <getopt.h>
23 #include "ctree.h"
24 #include "ioctl.h"
26 #include "commands.h"
27 #include "qgroup.h"
28 #include "utils.h"
30 static const char * const qgroup_cmd_group_usage[] = {
31 "btrfs qgroup <command> [options] <path>",
32 NULL
35 static int qgroup_assign(int assign, int argc, char **argv)
37 int ret = 0;
38 int fd;
39 int e;
40 int rescan = 0;
41 char *path;
42 struct btrfs_ioctl_qgroup_assign_args args;
43 DIR *dirstream = NULL;
45 while (1) {
46 enum { GETOPT_VAL_RESCAN = 256 };
47 static const struct option long_options[] = {
48 { "rescan", no_argument, NULL, GETOPT_VAL_RESCAN },
49 { NULL, 0, NULL, 0 }
51 int c = getopt_long(argc, argv, "", long_options, NULL);
53 if (c < 0)
54 break;
55 switch (c) {
56 case GETOPT_VAL_RESCAN:
57 rescan = 1;
58 break;
59 default:
60 /* Usage printed by the caller */
61 return -1;
65 if (check_argc_exact(argc - optind, 3))
66 return -1;
68 memset(&args, 0, sizeof(args));
69 args.assign = assign;
70 args.src = parse_qgroupid(argv[optind]);
71 args.dst = parse_qgroupid(argv[optind + 1]);
73 path = argv[optind + 2];
76 * FIXME src should accept subvol path
78 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
79 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
80 return 1;
82 fd = open_file_or_dir(path, &dirstream);
83 if (fd < 0) {
84 fprintf(stderr, "ERROR: can't access '%s'\n", path);
85 return 1;
88 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
89 e = errno;
90 if (ret < 0) {
91 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
92 strerror(e));
93 close_file_or_dir(fd, dirstream);
94 return 1;
98 * If ret > 0, it means assign caused qgroup data inconsistent state.
99 * Schedule a quota rescan if requested.
101 * The return value change only happens in newer kernel. But will not
102 * cause problem since old kernel has a bug that will never clear
103 * INCONSISTENT bit.
105 if (ret > 0) {
106 if (rescan) {
107 struct btrfs_ioctl_quota_rescan_args args;
109 printf("Quota data changed, rescan scheduled\n");
110 memset(&args, 0, sizeof(args));
111 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
112 if (ret < 0)
113 fprintf(stderr,
114 "ERROR: quota rescan failed: %s\n",
115 strerror(errno));
116 } else {
117 printf("WARNING: quotas may be inconsistent, rescan needed\n");
120 close_file_or_dir(fd, dirstream);
121 return ret;
124 static int qgroup_create(int create, int argc, char **argv)
126 int ret = 0;
127 int fd;
128 int e;
129 char *path = argv[2];
130 struct btrfs_ioctl_qgroup_create_args args;
131 DIR *dirstream = NULL;
133 if (check_argc_exact(argc, 3))
134 return -1;
136 memset(&args, 0, sizeof(args));
137 args.create = create;
138 args.qgroupid = parse_qgroupid(argv[1]);
140 fd = open_file_or_dir(path, &dirstream);
141 if (fd < 0) {
142 fprintf(stderr, "ERROR: can't access '%s'\n", path);
143 return 1;
146 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
147 e = errno;
148 close_file_or_dir(fd, dirstream);
149 if (ret < 0) {
150 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
151 create ? "create":"destroy", strerror(e));
152 return 1;
154 return 0;
157 static int parse_limit(const char *p, unsigned long long *s)
159 char *endptr;
160 unsigned long long size;
161 unsigned long long CLEAR_VALUE = -1;
163 if (strcasecmp(p, "none") == 0) {
164 *s = CLEAR_VALUE;
165 return 1;
168 if (p[0] == '-')
169 return 0;
171 size = strtoull(p, &endptr, 10);
172 if (p == endptr)
173 return 0;
175 switch (*endptr) {
176 case 'T':
177 case 't':
178 size *= 1024;
179 /* fallthrough */
180 case 'G':
181 case 'g':
182 size *= 1024;
183 /* fallthrough */
184 case 'M':
185 case 'm':
186 size *= 1024;
187 /* fallthrough */
188 case 'K':
189 case 'k':
190 size *= 1024;
191 ++endptr;
192 break;
193 case 0:
194 break;
195 default:
196 return 0;
199 if (*endptr)
200 return 0;
202 *s = size;
204 return 1;
207 static const char * const cmd_qgroup_assign_usage[] = {
208 "btrfs qgroup assign [options] <src> <dst> <path>",
209 "Assign SRC as the child qgroup of DST",
211 "--rescan schedule qutoa rescan if needed",
212 "--no-rescan ",
213 NULL
216 static int cmd_qgroup_assign(int argc, char **argv)
218 int ret = qgroup_assign(1, argc, argv);
219 if (ret < 0)
220 usage(cmd_qgroup_assign_usage);
221 return ret;
224 static const char * const cmd_qgroup_remove_usage[] = {
225 "btrfs qgroup remove <src> <dst> <path>",
226 "Remove a child qgroup SRC from DST.",
227 NULL
230 static int cmd_qgroup_remove(int argc, char **argv)
232 int ret = qgroup_assign(0, argc, argv);
233 if (ret < 0)
234 usage(cmd_qgroup_remove_usage);
235 return ret;
238 static const char * const cmd_qgroup_create_usage[] = {
239 "btrfs qgroup create <qgroupid> <path>",
240 "Create a subvolume quota group.",
241 NULL
244 static int cmd_qgroup_create(int argc, char **argv)
246 int ret = qgroup_create(1, argc, argv);
247 if (ret < 0)
248 usage(cmd_qgroup_create_usage);
249 return ret;
252 static const char * const cmd_qgroup_destroy_usage[] = {
253 "btrfs qgroup destroy <qgroupid> <path>",
254 "Destroy a quota group.",
255 NULL
258 static int cmd_qgroup_destroy(int argc, char **argv)
260 int ret = qgroup_create(0, argc, argv);
261 if (ret < 0)
262 usage(cmd_qgroup_destroy_usage);
263 return ret;
266 static const char * const cmd_qgroup_show_usage[] = {
267 "btrfs qgroup show -pcreFf "
268 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
269 "Show subvolume quota groups.",
270 "-p print parent qgroup id",
271 "-c print child qgroup id",
272 "-r print limit of referenced size of qgroup",
273 "-e print limit of exclusive size of qgroup",
274 "-F list all qgroups which impact the given path",
275 " (including ancestral qgroups)",
276 "-f list all qgroups which impact the given path",
277 " (excluding ancestral qgroups)",
278 HELPINFO_OUTPUT_UNIT,
279 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
280 " list qgroups sorted by specified items",
281 " you can use '+' or '-' in front of each item.",
282 " (+:ascending, -:descending, ascending default)",
283 NULL
286 static int cmd_qgroup_show(int argc, char **argv)
288 char *path;
289 int ret = 0;
290 int fd;
291 int e;
292 DIR *dirstream = NULL;
293 u64 qgroupid;
294 int filter_flag = 0;
295 unsigned unit_mode;
297 struct btrfs_qgroup_comparer_set *comparer_set;
298 struct btrfs_qgroup_filter_set *filter_set;
299 filter_set = btrfs_qgroup_alloc_filter_set();
300 comparer_set = btrfs_qgroup_alloc_comparer_set();
302 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
304 optind = 1;
305 while (1) {
306 int c;
307 static const struct option long_options[] = {
308 {"sort", required_argument, NULL, 'S'},
309 { NULL, 0, NULL, 0 }
312 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
313 if (c < 0)
314 break;
315 switch (c) {
316 case 'p':
317 btrfs_qgroup_setup_print_column(
318 BTRFS_QGROUP_PARENT);
319 break;
320 case 'c':
321 btrfs_qgroup_setup_print_column(
322 BTRFS_QGROUP_CHILD);
323 break;
324 case 'r':
325 btrfs_qgroup_setup_print_column(
326 BTRFS_QGROUP_MAX_RFER);
327 break;
328 case 'e':
329 btrfs_qgroup_setup_print_column(
330 BTRFS_QGROUP_MAX_EXCL);
331 break;
332 case 'F':
333 filter_flag |= 0x1;
334 break;
335 case 'f':
336 filter_flag |= 0x2;
337 break;
338 case 'S':
339 ret = btrfs_qgroup_parse_sort_string(optarg,
340 &comparer_set);
341 if (ret)
342 usage(cmd_qgroup_show_usage);
343 break;
344 default:
345 usage(cmd_qgroup_show_usage);
348 btrfs_qgroup_setup_units(unit_mode);
350 if (check_argc_exact(argc - optind, 1))
351 usage(cmd_qgroup_show_usage);
353 path = argv[optind];
354 fd = open_file_or_dir(path, &dirstream);
355 if (fd < 0) {
356 fprintf(stderr, "ERROR: can't access '%s'\n", path);
357 return 1;
360 if (filter_flag) {
361 qgroupid = btrfs_get_path_rootid(fd);
362 if (filter_flag & 0x1)
363 btrfs_qgroup_setup_filter(&filter_set,
364 BTRFS_QGROUP_FILTER_ALL_PARENT,
365 qgroupid);
366 if (filter_flag & 0x2)
367 btrfs_qgroup_setup_filter(&filter_set,
368 BTRFS_QGROUP_FILTER_PARENT,
369 qgroupid);
371 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
372 e = errno;
373 close_file_or_dir(fd, dirstream);
374 if (ret < 0)
375 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
376 strerror(e));
378 return !!ret;
381 static const char * const cmd_qgroup_limit_usage[] = {
382 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
383 "Set the limits a subvolume quota group.",
385 "-c limit amount of data after compression. This is the default,",
386 " it is currently not possible to turn off this option.",
387 "-e limit space exclusively assigned to this qgroup",
388 NULL
391 static int cmd_qgroup_limit(int argc, char **argv)
393 int ret = 0;
394 int fd;
395 int e;
396 char *path = NULL;
397 struct btrfs_ioctl_qgroup_limit_args args;
398 unsigned long long size;
399 int compressed = 0;
400 int exclusive = 0;
401 DIR *dirstream = NULL;
403 optind = 1;
404 while (1) {
405 int c = getopt(argc, argv, "ce");
406 if (c < 0)
407 break;
408 switch (c) {
409 case 'c':
410 compressed = 1;
411 break;
412 case 'e':
413 exclusive = 1;
414 break;
415 default:
416 usage(cmd_qgroup_limit_usage);
420 if (check_argc_min(argc - optind, 2))
421 usage(cmd_qgroup_limit_usage);
423 if (!parse_limit(argv[optind], &size)) {
424 fprintf(stderr, "Invalid size argument given\n");
425 return 1;
428 memset(&args, 0, sizeof(args));
429 if (compressed)
430 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
431 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
432 if (exclusive) {
433 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
434 args.lim.max_exclusive = size;
435 } else {
436 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
437 args.lim.max_referenced = size;
440 if (argc - optind == 2) {
441 args.qgroupid = 0;
442 path = argv[optind + 1];
443 ret = test_issubvolume(path);
444 if (ret < 0) {
445 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
446 return 1;
448 if (!ret) {
449 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
450 path);
451 return 1;
454 * keep qgroupid at 0, this indicates that the subvolume the
455 * fd refers to is to be limited
457 } else if (argc - optind == 3) {
458 args.qgroupid = parse_qgroupid(argv[optind + 1]);
459 path = argv[optind + 2];
460 } else
461 usage(cmd_qgroup_limit_usage);
463 fd = open_file_or_dir(path, &dirstream);
464 if (fd < 0) {
465 fprintf(stderr, "ERROR: can't access '%s'\n", path);
466 return 1;
469 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
470 e = errno;
471 close_file_or_dir(fd, dirstream);
472 if (ret < 0) {
473 fprintf(stderr, "ERROR: unable to limit requested quota group: "
474 "%s\n", strerror(e));
475 return 1;
477 return 0;
480 static const char qgroup_cmd_group_info[] =
481 "manage quota groups";
483 const struct cmd_group qgroup_cmd_group = {
484 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
485 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
486 NULL, 0 },
487 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
488 NULL, 0 },
489 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
490 NULL, 0 },
491 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
492 NULL, 0 },
493 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
494 NULL, 0 },
495 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
496 NULL, 0 },
497 NULL_CMD_STRUCT
501 int cmd_qgroup(int argc, char **argv)
503 return handle_command_group(&qgroup_cmd_group, argc, argv);