2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
22 #include <sys/ioctl.h>
25 #include "kerncompat.h"
33 static const char * const balance_cmd_group_usage[] = {
34 "btrfs balance <command> [options] <path>",
35 "btrfs balance <path>",
39 static int parse_one_profile(const char *profile, u64 *flags)
41 if (!strcmp(profile, "raid0")) {
42 *flags |= BTRFS_BLOCK_GROUP_RAID0;
43 } else if (!strcmp(profile, "raid1")) {
44 *flags |= BTRFS_BLOCK_GROUP_RAID1;
45 } else if (!strcmp(profile, "raid10")) {
46 *flags |= BTRFS_BLOCK_GROUP_RAID10;
47 } else if (!strcmp(profile, "raid5")) {
48 *flags |= BTRFS_BLOCK_GROUP_RAID5;
49 } else if (!strcmp(profile, "raid6")) {
50 *flags |= BTRFS_BLOCK_GROUP_RAID6;
51 } else if (!strcmp(profile, "dup")) {
52 *flags |= BTRFS_BLOCK_GROUP_DUP;
53 } else if (!strcmp(profile, "single")) {
54 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
56 fprintf(stderr, "Unknown profile '%s'\n", profile);
63 static int parse_profiles(char *profiles, u64 *flags)
66 char *save_ptr = NULL; /* Satisfy static checkers */
68 for (this_char = strtok_r(profiles, "|", &save_ptr);
70 this_char = strtok_r(NULL, "|", &save_ptr)) {
71 if (parse_one_profile(this_char, flags))
78 static int parse_u64(const char *str, u64 *result)
83 val = strtoull(str, &endptr, 10);
92 * Parse range that's missing some part that can be implicit:
93 * a..b - exact range, a can be equal to b
94 * a.. - implicitly unbounded maximum (end == (u64)-1)
95 * ..b - implicitly starting at 0
96 * a - invalid; unclear semantics, use parse_u64 instead
98 * Returned values are u64, value validation and interpretation should be done
101 static int parse_range(const char *range, u64 *start, u64 *end)
108 dots = strstr(range, "..");
118 *end = strtoull(rest, &endptr, 10);
126 *end = strtoull(range, &endptr, 10);
127 if (*endptr != 0 && *endptr != '.')
133 "ERROR: range %llu..%llu doesn't make sense\n",
134 (unsigned long long)*start,
135 (unsigned long long)*end);
146 * Parse range and check if start < end
148 static int parse_range_strict(const char *range, u64 *start, u64 *end)
150 if (parse_range(range, start, end) == 0) {
151 if (*start >= *end) {
153 "ERROR: range %llu..%llu not allowed\n",
154 (unsigned long long)*start,
155 (unsigned long long)*end);
164 static int parse_filters(char *filters, struct btrfs_balance_args *args)
168 char *save_ptr = NULL; /* Satisfy static checkers */
173 for (this_char = strtok_r(filters, ",", &save_ptr);
175 this_char = strtok_r(NULL, ",", &save_ptr)) {
176 if ((value = strchr(this_char, '=')) != NULL)
178 if (!strcmp(this_char, "profiles")) {
179 if (!value || !*value) {
180 fprintf(stderr, "the profiles filter requires "
184 if (parse_profiles(value, &args->profiles)) {
185 fprintf(stderr, "Invalid profiles argument\n");
188 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
189 } else if (!strcmp(this_char, "usage")) {
190 if (!value || !*value) {
191 fprintf(stderr, "the usage filter requires "
195 if (parse_u64(value, &args->usage) ||
197 fprintf(stderr, "Invalid usage argument: %s\n",
201 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
202 } else if (!strcmp(this_char, "devid")) {
203 if (!value || !*value) {
204 fprintf(stderr, "the devid filter requires "
208 if (parse_u64(value, &args->devid) ||
210 fprintf(stderr, "Invalid devid argument: %s\n",
214 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
215 } else if (!strcmp(this_char, "drange")) {
216 if (!value || !*value) {
217 fprintf(stderr, "the drange filter requires "
221 if (parse_range_strict(value, &args->pstart, &args->pend)) {
222 fprintf(stderr, "Invalid drange argument\n");
225 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
226 } else if (!strcmp(this_char, "vrange")) {
227 if (!value || !*value) {
228 fprintf(stderr, "the vrange filter requires "
232 if (parse_range_strict(value, &args->vstart, &args->vend)) {
233 fprintf(stderr, "Invalid vrange argument\n");
236 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
237 } else if (!strcmp(this_char, "convert")) {
238 if (!value || !*value) {
239 fprintf(stderr, "the convert option requires "
243 if (parse_one_profile(value, &args->target)) {
244 fprintf(stderr, "Invalid convert argument\n");
247 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
248 } else if (!strcmp(this_char, "soft")) {
249 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
250 } else if (!strcmp(this_char, "limit")) {
251 if (!value || !*value) {
253 "the limit filter requires an argument\n");
256 if (parse_u64(value, &args->limit)) {
257 fprintf(stderr, "Invalid limit argument: %s\n",
261 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
263 fprintf(stderr, "Unrecognized balance option '%s'\n",
272 static void dump_balance_args(struct btrfs_balance_args *args)
274 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
275 printf("converting, target=%llu, soft is %s",
276 (unsigned long long)args->target,
277 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
282 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
283 printf(", profiles=%llu", (unsigned long long)args->profiles);
284 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
285 printf(", usage=%llu", (unsigned long long)args->usage);
286 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
287 printf(", devid=%llu", (unsigned long long)args->devid);
288 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
289 printf(", drange=%llu..%llu",
290 (unsigned long long)args->pstart,
291 (unsigned long long)args->pend);
292 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
293 printf(", vrange=%llu..%llu",
294 (unsigned long long)args->vstart,
295 (unsigned long long)args->vend);
296 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
297 printf(", limit=%llu", (unsigned long long)args->limit);
302 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
304 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
305 (unsigned long long)args->flags, (unsigned long long)args->state,
306 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
307 if (args->flags & BTRFS_BALANCE_DATA) {
308 printf(" DATA (flags 0x%llx): ",
309 (unsigned long long)args->data.flags);
310 dump_balance_args(&args->data);
312 if (args->flags & BTRFS_BALANCE_METADATA) {
313 printf(" METADATA (flags 0x%llx): ",
314 (unsigned long long)args->meta.flags);
315 dump_balance_args(&args->meta);
317 if (args->flags & BTRFS_BALANCE_SYSTEM) {
318 printf(" SYSTEM (flags 0x%llx): ",
319 (unsigned long long)args->sys.flags);
320 dump_balance_args(&args->sys);
324 static int do_balance_v1(int fd)
326 struct btrfs_ioctl_vol_args args;
329 memset(&args, 0, sizeof(args));
330 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
334 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
340 DIR *dirstream = NULL;
342 fd = btrfs_open_dir(path, &dirstream, 1);
346 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
351 * older kernels don't have the new balance ioctl, try the
352 * old one. But, the old one doesn't know any filters, so
353 * don't fall back if they tried to use the fancy new things
355 if (e == ENOTTY && nofilters) {
356 ret = do_balance_v1(fd);
362 if (e == ECANCELED) {
363 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
364 fprintf(stderr, "balance paused by user\n");
365 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
366 fprintf(stderr, "balance canceled by user\n");
369 fprintf(stderr, "ERROR: error during balancing '%s' "
370 "- %s\n", path, strerror(e));
371 if (e != EINPROGRESS)
372 fprintf(stderr, "There may be more info in "
373 "syslog - try dmesg | tail\n");
377 printf("Done, had to relocate %llu out of %llu chunks\n",
378 (unsigned long long)args->stat.completed,
379 (unsigned long long)args->stat.considered);
384 close_file_or_dir(fd, dirstream);
388 static const char * const cmd_balance_start_usage[] = {
389 "btrfs balance start [options] <path>",
390 "Balance chunks across the devices",
391 "Balance and/or convert (change allocation profile of) chunks that",
392 "passed all filters in a comma-separated list of filters for a",
393 "particular chunk type. If filter list is not given balance all",
394 "chunks of that type. In case none of the -d, -m or -s options is",
395 "given balance all chunks in a filesystem.",
397 "-d[filters] act on data chunks",
398 "-m[filters] act on metadata chunks",
399 "-s[filters] act on system chunks (only under -f)",
401 "-f force reducing of metadata integrity",
405 static int cmd_balance_start(int argc, char **argv)
407 struct btrfs_ioctl_balance_args args;
408 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
415 memset(&args, 0, sizeof(args));
419 static const struct option longopts[] = {
420 { "data", optional_argument, NULL, 'd'},
421 { "metadata", optional_argument, NULL, 'm' },
422 { "system", optional_argument, NULL, 's' },
423 { "force", no_argument, NULL, 'f' },
424 { "verbose", no_argument, NULL, 'v' },
428 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
435 args.flags |= BTRFS_BALANCE_DATA;
437 if (parse_filters(optarg, &args.data))
442 args.flags |= BTRFS_BALANCE_SYSTEM;
444 if (parse_filters(optarg, &args.sys))
449 args.flags |= BTRFS_BALANCE_METADATA;
451 if (parse_filters(optarg, &args.meta))
461 usage(cmd_balance_start_usage);
465 if (check_argc_exact(argc - optind, 1))
466 usage(cmd_balance_start_usage);
469 * allow -s only under --force, otherwise do with system chunks
470 * the same thing we were ordered to do with meta chunks
472 if (args.flags & BTRFS_BALANCE_SYSTEM) {
475 "Refusing to explicitly operate on system chunks.\n"
476 "Pass --force if you really want to do that.\n");
479 } else if (args.flags & BTRFS_BALANCE_METADATA) {
480 args.flags |= BTRFS_BALANCE_SYSTEM;
481 memcpy(&args.sys, &args.meta,
482 sizeof(struct btrfs_balance_args));
486 /* relocate everything - no filters */
487 args.flags |= BTRFS_BALANCE_TYPE_MASK;
490 /* drange makes sense only when devid is set */
491 for (i = 0; ptrs[i]; i++) {
492 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
493 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
494 fprintf(stderr, "drange filter can be used only if "
495 "devid filter is used\n");
500 /* soft makes sense only when convert for corresponding type is set */
501 for (i = 0; ptrs[i]; i++) {
502 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
503 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
504 fprintf(stderr, "'soft' option can be used only if "
505 "changing profiles\n");
511 args.flags |= BTRFS_BALANCE_FORCE;
513 dump_ioctl_balance_args(&args);
515 return do_balance(argv[optind], &args, nofilters);
518 static const char * const cmd_balance_pause_usage[] = {
519 "btrfs balance pause <path>",
520 "Pause running balance",
524 static int cmd_balance_pause(int argc, char **argv)
530 DIR *dirstream = NULL;
532 if (check_argc_exact(argc, 2))
533 usage(cmd_balance_pause_usage);
537 fd = btrfs_open_dir(path, &dirstream, 1);
541 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
543 close_file_or_dir(fd, dirstream);
546 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
547 path, (e == ENOTCONN) ? "Not running" : strerror(e));
557 static const char * const cmd_balance_cancel_usage[] = {
558 "btrfs balance cancel <path>",
559 "Cancel running or paused balance",
563 static int cmd_balance_cancel(int argc, char **argv)
569 DIR *dirstream = NULL;
571 if (check_argc_exact(argc, 2))
572 usage(cmd_balance_cancel_usage);
576 fd = btrfs_open_dir(path, &dirstream, 1);
580 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
582 close_file_or_dir(fd, dirstream);
585 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
586 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
596 static const char * const cmd_balance_resume_usage[] = {
597 "btrfs balance resume <path>",
598 "Resume interrupted balance",
602 static int cmd_balance_resume(int argc, char **argv)
604 struct btrfs_ioctl_balance_args args;
606 DIR *dirstream = NULL;
611 if (check_argc_exact(argc, 2))
612 usage(cmd_balance_resume_usage);
616 fd = btrfs_open_dir(path, &dirstream, 1);
620 memset(&args, 0, sizeof(args));
621 args.flags |= BTRFS_BALANCE_RESUME;
623 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
625 close_file_or_dir(fd, dirstream);
628 if (e == ECANCELED) {
629 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
630 fprintf(stderr, "balance paused by user\n");
631 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
632 fprintf(stderr, "balance canceled by user\n");
633 } else if (e == ENOTCONN || e == EINPROGRESS) {
634 fprintf(stderr, "ERROR: balance resume on '%s' "
635 "failed - %s\n", path,
636 (e == ENOTCONN) ? "Not in progress" :
644 "ERROR: error during balancing '%s' - %s\n"
645 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
649 printf("Done, had to relocate %llu out of %llu chunks\n",
650 (unsigned long long)args.stat.completed,
651 (unsigned long long)args.stat.considered);
657 static const char * const cmd_balance_status_usage[] = {
658 "btrfs balance status [-v] <path>",
659 "Show status of running or paused balance",
665 /* Checks the status of the balance if any
667 * 2 : Error failed to know if there is any pending balance
668 * 1 : Successful to know status of a pending balance
669 * 0 : When there is no pending balance or completed
671 static int cmd_balance_status(int argc, char **argv)
673 struct btrfs_ioctl_balance_args args;
675 DIR *dirstream = NULL;
684 static const struct option longopts[] = {
685 { "verbose", no_argument, NULL, 'v' },
689 opt = getopt_long(argc, argv, "v", longopts, NULL);
698 usage(cmd_balance_status_usage);
702 if (check_argc_exact(argc - optind, 1))
703 usage(cmd_balance_status_usage);
707 fd = btrfs_open_dir(path, &dirstream, 1);
711 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
713 close_file_or_dir(fd, dirstream);
717 printf("No balance found on '%s'\n", path);
720 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
725 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
726 printf("Balance on '%s' is running", path);
727 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
728 printf(", cancel requested\n");
729 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
730 printf(", pause requested\n");
734 printf("Balance on '%s' is paused\n", path);
737 printf("%llu out of about %llu chunks balanced (%llu considered), "
738 "%3.f%% left\n", (unsigned long long)args.stat.completed,
739 (unsigned long long)args.stat.expected,
740 (unsigned long long)args.stat.considered,
741 100 * (1 - (float)args.stat.completed/args.stat.expected));
744 dump_ioctl_balance_args(&args);
749 static const char balance_cmd_group_info[] =
750 "balance data accross devices, or change block groups using filters";
752 const struct cmd_group balance_cmd_group = {
753 balance_cmd_group_usage, balance_cmd_group_info, {
754 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
755 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
756 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
757 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
758 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
763 int cmd_balance(int argc, char **argv)
766 /* old 'btrfs filesystem balance <path>' syntax */
767 struct btrfs_ioctl_balance_args args;
769 memset(&args, 0, sizeof(args));
770 args.flags |= BTRFS_BALANCE_TYPE_MASK;
772 return do_balance(argv[1], &args, 1);
775 return handle_command_group(&balance_cmd_group, argc, argv);