Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / cmds-qgroup.c
blobb928edc7c4082bcf0081e44b5ea5c7e37a952143
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 <btrfsutil.h>
25 #include "ctree.h"
26 #include "ioctl.h"
28 #include "commands.h"
29 #include "qgroup.h"
30 #include "utils.h"
31 #include "help.h"
33 static const char * const qgroup_cmd_group_usage[] = {
34 "btrfs qgroup <command> [options] <path>",
35 NULL
38 static int _cmd_qgroup_assign(int assign, int argc, char **argv,
39 const char * const *usage_str)
41 int ret = 0;
42 int fd;
43 int rescan = 0;
44 char *path;
45 struct btrfs_ioctl_qgroup_assign_args args;
46 DIR *dirstream = NULL;
48 if (assign) {
49 optind = 0;
50 while (1) {
51 enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
52 static const struct option long_options[] = {
53 { "rescan", no_argument, NULL,
54 GETOPT_VAL_RESCAN },
55 { "no-rescan", no_argument, NULL,
56 GETOPT_VAL_NO_RESCAN },
57 { NULL, 0, NULL, 0 }
59 int c = getopt_long(argc, argv, "", long_options, NULL);
61 if (c < 0)
62 break;
63 switch (c) {
64 case GETOPT_VAL_RESCAN:
65 rescan = 1;
66 break;
67 case GETOPT_VAL_NO_RESCAN:
68 rescan = 0;
69 break;
70 default:
71 /* Usage printed by the caller */
72 return -1;
75 } else {
76 clean_args_no_options(argc, argv, usage_str);
79 if (check_argc_exact(argc - optind, 3))
80 usage(usage_str);
82 memset(&args, 0, sizeof(args));
83 args.assign = assign;
84 args.src = parse_qgroupid(argv[optind]);
85 args.dst = parse_qgroupid(argv[optind + 1]);
87 path = argv[optind + 2];
90 * FIXME src should accept subvol path
92 if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
93 error("bad relation requested: %s", path);
94 return 1;
96 fd = btrfs_open_dir(path, &dirstream, 1);
97 if (fd < 0)
98 return 1;
100 ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
101 if (ret < 0) {
102 error("unable to assign quota group: %m");
103 close_file_or_dir(fd, dirstream);
104 return 1;
108 * If ret > 0, it means assign caused qgroup data inconsistent state.
109 * Schedule a quota rescan if requested.
111 * The return value change only happens in newer kernel. But will not
112 * cause problem since old kernel has a bug that will never clear
113 * INCONSISTENT bit.
115 if (ret > 0) {
116 if (rescan) {
117 struct btrfs_ioctl_quota_rescan_args qargs;
119 printf("Quota data changed, rescan scheduled\n");
120 memset(&qargs, 0, sizeof(qargs));
121 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
122 if (ret < 0)
123 error("quota rescan failed: %m");
124 } else {
125 warning("quotas may be inconsistent, rescan needed");
128 close_file_or_dir(fd, dirstream);
129 return ret;
132 static int _cmd_qgroup_create(int create, int argc, char **argv)
134 int ret = 0;
135 int fd;
136 char *path;
137 struct btrfs_ioctl_qgroup_create_args args;
138 DIR *dirstream = NULL;
140 if (check_argc_exact(argc - optind, 2))
141 return -1;
143 memset(&args, 0, sizeof(args));
144 args.create = create;
145 args.qgroupid = parse_qgroupid(argv[optind]);
146 path = argv[optind + 1];
148 fd = btrfs_open_dir(path, &dirstream, 1);
149 if (fd < 0)
150 return 1;
152 ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
153 close_file_or_dir(fd, dirstream);
154 if (ret < 0) {
155 error("unable to %s quota group: %m",
156 create ? "create":"destroy");
157 return 1;
159 return 0;
162 static int parse_limit(const char *p, unsigned long long *s)
164 char *endptr;
165 unsigned long long size;
166 unsigned long long CLEAR_VALUE = -1;
168 if (strcasecmp(p, "none") == 0) {
169 *s = CLEAR_VALUE;
170 return 1;
173 if (p[0] == '-')
174 return 0;
176 size = strtoull(p, &endptr, 10);
177 if (p == endptr)
178 return 0;
180 switch (*endptr) {
181 case 'T':
182 case 't':
183 size *= 1024;
184 /* fallthrough */
185 case 'G':
186 case 'g':
187 size *= 1024;
188 /* fallthrough */
189 case 'M':
190 case 'm':
191 size *= 1024;
192 /* fallthrough */
193 case 'K':
194 case 'k':
195 size *= 1024;
196 ++endptr;
197 break;
198 case 0:
199 break;
200 default:
201 return 0;
204 if (*endptr)
205 return 0;
207 *s = size;
209 return 1;
212 static const char * const cmd_qgroup_assign_usage[] = {
213 "btrfs qgroup assign [options] <src> <dst> <path>",
214 "Assign SRC as the child qgroup of DST",
216 "--rescan schedule qutoa rescan if needed",
217 "--no-rescan don't schedule quota rescan",
218 NULL
221 static int cmd_qgroup_assign(int argc, char **argv)
223 return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
226 static const char * const cmd_qgroup_remove_usage[] = {
227 "btrfs qgroup remove <src> <dst> <path>",
228 "Remove a child qgroup SRC from DST.",
229 NULL
232 static int cmd_qgroup_remove(int argc, char **argv)
234 return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
237 static const char * const cmd_qgroup_create_usage[] = {
238 "btrfs qgroup create <qgroupid> <path>",
239 "Create a subvolume quota group.",
240 NULL
243 static int cmd_qgroup_create(int argc, char **argv)
245 int ret;
247 clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
249 ret = _cmd_qgroup_create(1, argc, argv);
251 if (ret < 0)
252 usage(cmd_qgroup_create_usage);
253 return ret;
256 static const char * const cmd_qgroup_destroy_usage[] = {
257 "btrfs qgroup destroy <qgroupid> <path>",
258 "Destroy a quota group.",
259 NULL
262 static int cmd_qgroup_destroy(int argc, char **argv)
264 int ret;
266 clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
268 ret = _cmd_qgroup_create(0, argc, argv);
270 if (ret < 0)
271 usage(cmd_qgroup_destroy_usage);
272 return ret;
275 static const char * const cmd_qgroup_show_usage[] = {
276 "btrfs qgroup show [options] <path>",
277 "Show subvolume quota groups.",
278 "-p print parent qgroup id",
279 "-c print child qgroup id",
280 "-r print limit of referenced size of qgroup",
281 "-e print limit of exclusive size of qgroup",
282 "-F list all qgroups which impact the given path",
283 " (including ancestral qgroups)",
284 "-f list all qgroups which impact the given path",
285 " (excluding ancestral qgroups)",
286 HELPINFO_UNITS_LONG,
287 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
288 " list qgroups sorted by specified items",
289 " you can use '+' or '-' in front of each item.",
290 " (+:ascending, -:descending, ascending default)",
291 "--sync force sync of the filesystem before getting info",
292 NULL
295 static int cmd_qgroup_show(int argc, char **argv)
297 char *path;
298 int ret = 0;
299 int fd;
300 DIR *dirstream = NULL;
301 u64 qgroupid;
302 int filter_flag = 0;
303 unsigned unit_mode;
304 int sync = 0;
305 enum btrfs_util_error err;
307 struct btrfs_qgroup_comparer_set *comparer_set;
308 struct btrfs_qgroup_filter_set *filter_set;
309 filter_set = btrfs_qgroup_alloc_filter_set();
310 comparer_set = btrfs_qgroup_alloc_comparer_set();
312 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
314 optind = 0;
315 while (1) {
316 int c;
317 enum {
318 GETOPT_VAL_SORT = 256,
319 GETOPT_VAL_SYNC
321 static const struct option long_options[] = {
322 {"sort", required_argument, NULL, GETOPT_VAL_SORT},
323 {"sync", no_argument, NULL, GETOPT_VAL_SYNC},
324 { NULL, 0, NULL, 0 }
327 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
328 if (c < 0)
329 break;
330 switch (c) {
331 case 'p':
332 btrfs_qgroup_setup_print_column(
333 BTRFS_QGROUP_PARENT);
334 break;
335 case 'c':
336 btrfs_qgroup_setup_print_column(
337 BTRFS_QGROUP_CHILD);
338 break;
339 case 'r':
340 btrfs_qgroup_setup_print_column(
341 BTRFS_QGROUP_MAX_RFER);
342 break;
343 case 'e':
344 btrfs_qgroup_setup_print_column(
345 BTRFS_QGROUP_MAX_EXCL);
346 break;
347 case 'F':
348 filter_flag |= 0x1;
349 break;
350 case 'f':
351 filter_flag |= 0x2;
352 break;
353 case GETOPT_VAL_SORT:
354 ret = btrfs_qgroup_parse_sort_string(optarg,
355 &comparer_set);
356 if (ret)
357 usage(cmd_qgroup_show_usage);
358 break;
359 case GETOPT_VAL_SYNC:
360 sync = 1;
361 break;
362 default:
363 usage(cmd_qgroup_show_usage);
366 btrfs_qgroup_setup_units(unit_mode);
368 if (check_argc_exact(argc - optind, 1))
369 usage(cmd_qgroup_show_usage);
371 path = argv[optind];
372 fd = btrfs_open_dir(path, &dirstream, 1);
373 if (fd < 0) {
374 free(filter_set);
375 free(comparer_set);
376 return 1;
379 if (sync) {
380 err = btrfs_util_sync_fd(fd);
381 if (err)
382 warning("sync ioctl failed on '%s': %s", path,
383 strerror(errno));
386 if (filter_flag) {
387 ret = lookup_path_rootid(fd, &qgroupid);
388 if (ret < 0) {
389 error("cannot resolve rootid for %s: %s",
390 path, strerror(-ret));
391 close_file_or_dir(fd, dirstream);
392 goto out;
394 if (filter_flag & 0x1)
395 btrfs_qgroup_setup_filter(&filter_set,
396 BTRFS_QGROUP_FILTER_ALL_PARENT,
397 qgroupid);
398 if (filter_flag & 0x2)
399 btrfs_qgroup_setup_filter(&filter_set,
400 BTRFS_QGROUP_FILTER_PARENT,
401 qgroupid);
403 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
404 close_file_or_dir(fd, dirstream);
405 free(filter_set);
406 free(comparer_set);
408 out:
409 return !!ret;
412 static const char * const cmd_qgroup_limit_usage[] = {
413 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
414 "Set the limits a subvolume quota group.",
416 "-c limit amount of data after compression. This is the default,",
417 " it is currently not possible to turn off this option.",
418 "-e limit space exclusively assigned to this qgroup",
419 NULL
422 static int cmd_qgroup_limit(int argc, char **argv)
424 int ret = 0;
425 int fd;
426 char *path = NULL;
427 struct btrfs_ioctl_qgroup_limit_args args;
428 unsigned long long size;
429 int compressed = 0;
430 int exclusive = 0;
431 DIR *dirstream = NULL;
432 enum btrfs_util_error err;
434 optind = 0;
435 while (1) {
436 int c = getopt(argc, argv, "ce");
437 if (c < 0)
438 break;
439 switch (c) {
440 case 'c':
441 compressed = 1;
442 break;
443 case 'e':
444 exclusive = 1;
445 break;
446 default:
447 usage(cmd_qgroup_limit_usage);
451 if (check_argc_min(argc - optind, 2))
452 usage(cmd_qgroup_limit_usage);
454 if (!parse_limit(argv[optind], &size)) {
455 error("invalid size argument: %s", argv[optind]);
456 return 1;
459 memset(&args, 0, sizeof(args));
460 if (compressed)
461 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
462 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
463 if (exclusive) {
464 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
465 args.lim.max_exclusive = size;
466 } else {
467 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
468 args.lim.max_referenced = size;
471 if (argc - optind == 2) {
472 args.qgroupid = 0;
473 path = argv[optind + 1];
474 err = btrfs_util_is_subvolume(path);
475 if (err) {
476 error_btrfs_util(err);
477 return 1;
480 * keep qgroupid at 0, this indicates that the subvolume the
481 * fd refers to is to be limited
483 } else if (argc - optind == 3) {
484 args.qgroupid = parse_qgroupid(argv[optind + 1]);
485 path = argv[optind + 2];
486 } else
487 usage(cmd_qgroup_limit_usage);
489 fd = btrfs_open_dir(path, &dirstream, 1);
490 if (fd < 0)
491 return 1;
493 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
494 close_file_or_dir(fd, dirstream);
495 if (ret < 0) {
496 error("unable to limit requested quota group: %m");
497 return 1;
499 return 0;
502 static const char qgroup_cmd_group_info[] =
503 "manage quota groups";
505 const struct cmd_group qgroup_cmd_group = {
506 qgroup_cmd_group_usage, qgroup_cmd_group_info, {
507 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
508 NULL, 0 },
509 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
510 NULL, 0 },
511 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
512 NULL, 0 },
513 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
514 NULL, 0 },
515 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
516 NULL, 0 },
517 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
518 NULL, 0 },
519 NULL_CMD_STRUCT
523 int cmd_qgroup(int argc, char **argv)
525 return handle_command_group(&qgroup_cmd_group, argc, argv);