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);
165 * Convert 64bit range to 32bit with boundary checkso
167 static int range_to_u32(u64 start, u64 end, u32 *start32, u32 *end32)
172 if (end != (u64)-1 && end > (u32)-1)
175 *start32 = (u32)start;
181 __attribute__ ((unused))
182 static int parse_range_u32(const char *range, u32 *start, u32 *end)
187 if (parse_range(range, &tmp_start, &tmp_end))
190 if (range_to_u32(tmp_start, tmp_end, start, end))
196 __attribute__ ((unused))
197 static void print_range(u64 start, u64 end)
200 printf("%llu", (unsigned long long)start);
203 printf("%llu", (unsigned long long)end);
206 __attribute__ ((unused))
207 static void print_range_u32(u32 start, u32 end)
216 static int parse_filters(char *filters, struct btrfs_balance_args *args)
220 char *save_ptr = NULL; /* Satisfy static checkers */
225 for (this_char = strtok_r(filters, ",", &save_ptr);
227 this_char = strtok_r(NULL, ",", &save_ptr)) {
228 if ((value = strchr(this_char, '=')) != NULL)
230 if (!strcmp(this_char, "profiles")) {
231 if (!value || !*value) {
232 fprintf(stderr, "the profiles filter requires "
236 if (parse_profiles(value, &args->profiles)) {
237 fprintf(stderr, "Invalid profiles argument\n");
240 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
241 } else if (!strcmp(this_char, "usage")) {
242 if (!value || !*value) {
243 fprintf(stderr, "the usage filter requires "
247 if (parse_u64(value, &args->usage)) {
248 if (parse_range_u32(value, &args->usage_min,
251 "Invalid usage argument: %s\n",
255 if (args->usage_max > 100) {
257 "Invalid usage argument: %s\n",
260 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
261 args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
263 if (args->usage > 100) {
265 "Invalid usage argument: %s\n",
269 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
270 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
272 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
273 } else if (!strcmp(this_char, "devid")) {
274 if (!value || !*value) {
275 fprintf(stderr, "the devid filter requires "
279 if (parse_u64(value, &args->devid) ||
281 fprintf(stderr, "Invalid devid argument: %s\n",
285 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
286 } else if (!strcmp(this_char, "drange")) {
287 if (!value || !*value) {
288 fprintf(stderr, "the drange filter requires "
292 if (parse_range_strict(value, &args->pstart, &args->pend)) {
293 fprintf(stderr, "Invalid drange argument\n");
296 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
297 } else if (!strcmp(this_char, "vrange")) {
298 if (!value || !*value) {
299 fprintf(stderr, "the vrange filter requires "
303 if (parse_range_strict(value, &args->vstart, &args->vend)) {
304 fprintf(stderr, "Invalid vrange argument\n");
307 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
308 } else if (!strcmp(this_char, "convert")) {
309 if (!value || !*value) {
310 fprintf(stderr, "the convert option requires "
314 if (parse_one_profile(value, &args->target)) {
315 fprintf(stderr, "Invalid convert argument\n");
318 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
319 } else if (!strcmp(this_char, "soft")) {
320 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
321 } else if (!strcmp(this_char, "limit")) {
322 if (!value || !*value) {
324 "the limit filter requires an argument\n");
327 if (parse_u64(value, &args->limit)) {
328 if (parse_range_u32(value, &args->limit_min,
331 "Invalid limit argument: %s\n",
335 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
336 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
338 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
339 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
341 } else if (!strcmp(this_char, "stripes")) {
342 if (!value || !*value) {
344 "the stripes filter requires an argument\n");
347 if (parse_range_u32(value, &args->stripes_min,
348 &args->stripes_max)) {
349 fprintf(stderr, "Invalid stripes argument\n");
352 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
354 fprintf(stderr, "Unrecognized balance option '%s'\n",
363 static void dump_balance_args(struct btrfs_balance_args *args)
365 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
366 printf("converting, target=%llu, soft is %s",
367 (unsigned long long)args->target,
368 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
373 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
374 printf(", profiles=%llu", (unsigned long long)args->profiles);
375 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
376 printf(", usage=%llu", (unsigned long long)args->usage);
377 if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
379 print_range_u32(args->usage_min, args->usage_max);
381 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
382 printf(", devid=%llu", (unsigned long long)args->devid);
383 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
384 printf(", drange=%llu..%llu",
385 (unsigned long long)args->pstart,
386 (unsigned long long)args->pend);
387 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
388 printf(", vrange=%llu..%llu",
389 (unsigned long long)args->vstart,
390 (unsigned long long)args->vend);
391 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
392 printf(", limit=%llu", (unsigned long long)args->limit);
393 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
395 print_range_u32(args->limit_min, args->limit_max);
397 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
398 printf(", stripes=");
399 print_range_u32(args->stripes_min, args->stripes_max);
405 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
407 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
408 (unsigned long long)args->flags, (unsigned long long)args->state,
409 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
410 if (args->flags & BTRFS_BALANCE_DATA) {
411 printf(" DATA (flags 0x%llx): ",
412 (unsigned long long)args->data.flags);
413 dump_balance_args(&args->data);
415 if (args->flags & BTRFS_BALANCE_METADATA) {
416 printf(" METADATA (flags 0x%llx): ",
417 (unsigned long long)args->meta.flags);
418 dump_balance_args(&args->meta);
420 if (args->flags & BTRFS_BALANCE_SYSTEM) {
421 printf(" SYSTEM (flags 0x%llx): ",
422 (unsigned long long)args->sys.flags);
423 dump_balance_args(&args->sys);
427 static int do_balance_v1(int fd)
429 struct btrfs_ioctl_vol_args args;
432 memset(&args, 0, sizeof(args));
433 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
437 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
443 DIR *dirstream = NULL;
445 fd = btrfs_open_dir(path, &dirstream, 1);
449 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
454 * older kernels don't have the new balance ioctl, try the
455 * old one. But, the old one doesn't know any filters, so
456 * don't fall back if they tried to use the fancy new things
458 if (e == ENOTTY && nofilters) {
459 ret = do_balance_v1(fd);
465 if (e == ECANCELED) {
466 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
467 fprintf(stderr, "balance paused by user\n");
468 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
469 fprintf(stderr, "balance canceled by user\n");
472 fprintf(stderr, "ERROR: error during balancing '%s' "
473 "- %s\n", path, strerror(e));
474 if (e != EINPROGRESS)
475 fprintf(stderr, "There may be more info in "
476 "syslog - try dmesg | tail\n");
480 printf("Done, had to relocate %llu out of %llu chunks\n",
481 (unsigned long long)args->stat.completed,
482 (unsigned long long)args->stat.considered);
487 close_file_or_dir(fd, dirstream);
491 static const char * const cmd_balance_start_usage[] = {
492 "btrfs balance start [options] <path>",
493 "Balance chunks across the devices",
494 "Balance and/or convert (change allocation profile of) chunks that",
495 "passed all filters in a comma-separated list of filters for a",
496 "particular chunk type. If filter list is not given balance all",
497 "chunks of that type. In case none of the -d, -m or -s options is",
498 "given balance all chunks in a filesystem.",
500 "-d[filters] act on data chunks",
501 "-m[filters] act on metadata chunks",
502 "-s[filters] act on system chunks (only under -f)",
504 "-f force reducing of metadata integrity",
508 static int cmd_balance_start(int argc, char **argv)
510 struct btrfs_ioctl_balance_args args;
511 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
518 memset(&args, 0, sizeof(args));
522 static const struct option longopts[] = {
523 { "data", optional_argument, NULL, 'd'},
524 { "metadata", optional_argument, NULL, 'm' },
525 { "system", optional_argument, NULL, 's' },
526 { "force", no_argument, NULL, 'f' },
527 { "verbose", no_argument, NULL, 'v' },
531 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
538 args.flags |= BTRFS_BALANCE_DATA;
540 if (parse_filters(optarg, &args.data))
545 args.flags |= BTRFS_BALANCE_SYSTEM;
547 if (parse_filters(optarg, &args.sys))
552 args.flags |= BTRFS_BALANCE_METADATA;
554 if (parse_filters(optarg, &args.meta))
564 usage(cmd_balance_start_usage);
568 if (check_argc_exact(argc - optind, 1))
569 usage(cmd_balance_start_usage);
572 * allow -s only under --force, otherwise do with system chunks
573 * the same thing we were ordered to do with meta chunks
575 if (args.flags & BTRFS_BALANCE_SYSTEM) {
578 "Refusing to explicitly operate on system chunks.\n"
579 "Pass --force if you really want to do that.\n");
582 } else if (args.flags & BTRFS_BALANCE_METADATA) {
583 args.flags |= BTRFS_BALANCE_SYSTEM;
584 memcpy(&args.sys, &args.meta,
585 sizeof(struct btrfs_balance_args));
589 /* relocate everything - no filters */
590 args.flags |= BTRFS_BALANCE_TYPE_MASK;
593 /* drange makes sense only when devid is set */
594 for (i = 0; ptrs[i]; i++) {
595 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
596 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
597 fprintf(stderr, "drange filter can be used only if "
598 "devid filter is used\n");
603 /* soft makes sense only when convert for corresponding type is set */
604 for (i = 0; ptrs[i]; i++) {
605 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
606 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
607 fprintf(stderr, "'soft' option can be used only if "
608 "changing profiles\n");
614 args.flags |= BTRFS_BALANCE_FORCE;
616 dump_ioctl_balance_args(&args);
618 return do_balance(argv[optind], &args, nofilters);
621 static const char * const cmd_balance_pause_usage[] = {
622 "btrfs balance pause <path>",
623 "Pause running balance",
627 static int cmd_balance_pause(int argc, char **argv)
633 DIR *dirstream = NULL;
635 if (check_argc_exact(argc, 2))
636 usage(cmd_balance_pause_usage);
640 fd = btrfs_open_dir(path, &dirstream, 1);
644 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
646 close_file_or_dir(fd, dirstream);
649 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
650 path, (e == ENOTCONN) ? "Not running" : strerror(e));
660 static const char * const cmd_balance_cancel_usage[] = {
661 "btrfs balance cancel <path>",
662 "Cancel running or paused balance",
666 static int cmd_balance_cancel(int argc, char **argv)
672 DIR *dirstream = NULL;
674 if (check_argc_exact(argc, 2))
675 usage(cmd_balance_cancel_usage);
679 fd = btrfs_open_dir(path, &dirstream, 1);
683 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
685 close_file_or_dir(fd, dirstream);
688 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
689 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
699 static const char * const cmd_balance_resume_usage[] = {
700 "btrfs balance resume <path>",
701 "Resume interrupted balance",
705 static int cmd_balance_resume(int argc, char **argv)
707 struct btrfs_ioctl_balance_args args;
709 DIR *dirstream = NULL;
714 if (check_argc_exact(argc, 2))
715 usage(cmd_balance_resume_usage);
719 fd = btrfs_open_dir(path, &dirstream, 1);
723 memset(&args, 0, sizeof(args));
724 args.flags |= BTRFS_BALANCE_RESUME;
726 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
728 close_file_or_dir(fd, dirstream);
731 if (e == ECANCELED) {
732 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
733 fprintf(stderr, "balance paused by user\n");
734 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
735 fprintf(stderr, "balance canceled by user\n");
736 } else if (e == ENOTCONN || e == EINPROGRESS) {
737 fprintf(stderr, "ERROR: balance resume on '%s' "
738 "failed - %s\n", path,
739 (e == ENOTCONN) ? "Not in progress" :
747 "ERROR: error during balancing '%s' - %s\n"
748 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
752 printf("Done, had to relocate %llu out of %llu chunks\n",
753 (unsigned long long)args.stat.completed,
754 (unsigned long long)args.stat.considered);
760 static const char * const cmd_balance_status_usage[] = {
761 "btrfs balance status [-v] <path>",
762 "Show status of running or paused balance",
768 /* Checks the status of the balance if any
770 * 2 : Error failed to know if there is any pending balance
771 * 1 : Successful to know status of a pending balance
772 * 0 : When there is no pending balance or completed
774 static int cmd_balance_status(int argc, char **argv)
776 struct btrfs_ioctl_balance_args args;
778 DIR *dirstream = NULL;
787 static const struct option longopts[] = {
788 { "verbose", no_argument, NULL, 'v' },
792 opt = getopt_long(argc, argv, "v", longopts, NULL);
801 usage(cmd_balance_status_usage);
805 if (check_argc_exact(argc - optind, 1))
806 usage(cmd_balance_status_usage);
810 fd = btrfs_open_dir(path, &dirstream, 1);
814 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
816 close_file_or_dir(fd, dirstream);
820 printf("No balance found on '%s'\n", path);
823 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
828 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
829 printf("Balance on '%s' is running", path);
830 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
831 printf(", cancel requested\n");
832 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
833 printf(", pause requested\n");
837 printf("Balance on '%s' is paused\n", path);
840 printf("%llu out of about %llu chunks balanced (%llu considered), "
841 "%3.f%% left\n", (unsigned long long)args.stat.completed,
842 (unsigned long long)args.stat.expected,
843 (unsigned long long)args.stat.considered,
844 100 * (1 - (float)args.stat.completed/args.stat.expected));
847 dump_ioctl_balance_args(&args);
852 static const char balance_cmd_group_info[] =
853 "balance data accross devices, or change block groups using filters";
855 const struct cmd_group balance_cmd_group = {
856 balance_cmd_group_usage, balance_cmd_group_info, {
857 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
858 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
859 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
860 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
861 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
866 int cmd_balance(int argc, char **argv)
869 /* old 'btrfs filesystem balance <path>' syntax */
870 struct btrfs_ioctl_balance_args args;
872 memset(&args, 0, sizeof(args));
873 args.flags |= BTRFS_BALANCE_TYPE_MASK;
875 return do_balance(argv[1], &args, 1);
878 return handle_command_group(&balance_cmd_group, argc, argv);