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) ||
249 fprintf(stderr, "Invalid usage argument: %s\n",
253 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
254 } else if (!strcmp(this_char, "devid")) {
255 if (!value || !*value) {
256 fprintf(stderr, "the devid filter requires "
260 if (parse_u64(value, &args->devid) ||
262 fprintf(stderr, "Invalid devid argument: %s\n",
266 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
267 } else if (!strcmp(this_char, "drange")) {
268 if (!value || !*value) {
269 fprintf(stderr, "the drange filter requires "
273 if (parse_range_strict(value, &args->pstart, &args->pend)) {
274 fprintf(stderr, "Invalid drange argument\n");
277 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
278 } else if (!strcmp(this_char, "vrange")) {
279 if (!value || !*value) {
280 fprintf(stderr, "the vrange filter requires "
284 if (parse_range_strict(value, &args->vstart, &args->vend)) {
285 fprintf(stderr, "Invalid vrange argument\n");
288 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
289 } else if (!strcmp(this_char, "convert")) {
290 if (!value || !*value) {
291 fprintf(stderr, "the convert option requires "
295 if (parse_one_profile(value, &args->target)) {
296 fprintf(stderr, "Invalid convert argument\n");
299 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
300 } else if (!strcmp(this_char, "soft")) {
301 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
302 } else if (!strcmp(this_char, "limit")) {
303 if (!value || !*value) {
305 "the limit filter requires an argument\n");
308 if (parse_u64(value, &args->limit)) {
309 if (parse_range_u32(value, &args->limit_min,
312 "Invalid limit argument: %s\n",
316 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
317 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
319 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
320 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
322 } else if (!strcmp(this_char, "stripes")) {
323 if (!value || !*value) {
325 "the stripes filter requires an argument\n");
328 if (parse_range_u32(value, &args->stripes_min,
329 &args->stripes_max)) {
330 fprintf(stderr, "Invalid stripes argument\n");
333 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
335 fprintf(stderr, "Unrecognized balance option '%s'\n",
344 static void dump_balance_args(struct btrfs_balance_args *args)
346 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
347 printf("converting, target=%llu, soft is %s",
348 (unsigned long long)args->target,
349 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
354 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
355 printf(", profiles=%llu", (unsigned long long)args->profiles);
356 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
357 printf(", usage=%llu", (unsigned long long)args->usage);
358 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
359 printf(", devid=%llu", (unsigned long long)args->devid);
360 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
361 printf(", drange=%llu..%llu",
362 (unsigned long long)args->pstart,
363 (unsigned long long)args->pend);
364 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
365 printf(", vrange=%llu..%llu",
366 (unsigned long long)args->vstart,
367 (unsigned long long)args->vend);
368 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
369 printf(", limit=%llu", (unsigned long long)args->limit);
370 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
372 print_range_u32(args->limit_min, args->limit_max);
374 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
375 printf(", stripes=");
376 print_range_u32(args->stripes_min, args->stripes_max);
382 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
384 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
385 (unsigned long long)args->flags, (unsigned long long)args->state,
386 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
387 if (args->flags & BTRFS_BALANCE_DATA) {
388 printf(" DATA (flags 0x%llx): ",
389 (unsigned long long)args->data.flags);
390 dump_balance_args(&args->data);
392 if (args->flags & BTRFS_BALANCE_METADATA) {
393 printf(" METADATA (flags 0x%llx): ",
394 (unsigned long long)args->meta.flags);
395 dump_balance_args(&args->meta);
397 if (args->flags & BTRFS_BALANCE_SYSTEM) {
398 printf(" SYSTEM (flags 0x%llx): ",
399 (unsigned long long)args->sys.flags);
400 dump_balance_args(&args->sys);
404 static int do_balance_v1(int fd)
406 struct btrfs_ioctl_vol_args args;
409 memset(&args, 0, sizeof(args));
410 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
414 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
420 DIR *dirstream = NULL;
422 fd = btrfs_open_dir(path, &dirstream, 1);
426 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
431 * older kernels don't have the new balance ioctl, try the
432 * old one. But, the old one doesn't know any filters, so
433 * don't fall back if they tried to use the fancy new things
435 if (e == ENOTTY && nofilters) {
436 ret = do_balance_v1(fd);
442 if (e == ECANCELED) {
443 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
444 fprintf(stderr, "balance paused by user\n");
445 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
446 fprintf(stderr, "balance canceled by user\n");
449 fprintf(stderr, "ERROR: error during balancing '%s' "
450 "- %s\n", path, strerror(e));
451 if (e != EINPROGRESS)
452 fprintf(stderr, "There may be more info in "
453 "syslog - try dmesg | tail\n");
457 printf("Done, had to relocate %llu out of %llu chunks\n",
458 (unsigned long long)args->stat.completed,
459 (unsigned long long)args->stat.considered);
464 close_file_or_dir(fd, dirstream);
468 static const char * const cmd_balance_start_usage[] = {
469 "btrfs balance start [options] <path>",
470 "Balance chunks across the devices",
471 "Balance and/or convert (change allocation profile of) chunks that",
472 "passed all filters in a comma-separated list of filters for a",
473 "particular chunk type. If filter list is not given balance all",
474 "chunks of that type. In case none of the -d, -m or -s options is",
475 "given balance all chunks in a filesystem.",
477 "-d[filters] act on data chunks",
478 "-m[filters] act on metadata chunks",
479 "-s[filters] act on system chunks (only under -f)",
481 "-f force reducing of metadata integrity",
485 static int cmd_balance_start(int argc, char **argv)
487 struct btrfs_ioctl_balance_args args;
488 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
495 memset(&args, 0, sizeof(args));
499 static const struct option longopts[] = {
500 { "data", optional_argument, NULL, 'd'},
501 { "metadata", optional_argument, NULL, 'm' },
502 { "system", optional_argument, NULL, 's' },
503 { "force", no_argument, NULL, 'f' },
504 { "verbose", no_argument, NULL, 'v' },
508 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
515 args.flags |= BTRFS_BALANCE_DATA;
517 if (parse_filters(optarg, &args.data))
522 args.flags |= BTRFS_BALANCE_SYSTEM;
524 if (parse_filters(optarg, &args.sys))
529 args.flags |= BTRFS_BALANCE_METADATA;
531 if (parse_filters(optarg, &args.meta))
541 usage(cmd_balance_start_usage);
545 if (check_argc_exact(argc - optind, 1))
546 usage(cmd_balance_start_usage);
549 * allow -s only under --force, otherwise do with system chunks
550 * the same thing we were ordered to do with meta chunks
552 if (args.flags & BTRFS_BALANCE_SYSTEM) {
555 "Refusing to explicitly operate on system chunks.\n"
556 "Pass --force if you really want to do that.\n");
559 } else if (args.flags & BTRFS_BALANCE_METADATA) {
560 args.flags |= BTRFS_BALANCE_SYSTEM;
561 memcpy(&args.sys, &args.meta,
562 sizeof(struct btrfs_balance_args));
566 /* relocate everything - no filters */
567 args.flags |= BTRFS_BALANCE_TYPE_MASK;
570 /* drange makes sense only when devid is set */
571 for (i = 0; ptrs[i]; i++) {
572 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
573 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
574 fprintf(stderr, "drange filter can be used only if "
575 "devid filter is used\n");
580 /* soft makes sense only when convert for corresponding type is set */
581 for (i = 0; ptrs[i]; i++) {
582 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
583 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
584 fprintf(stderr, "'soft' option can be used only if "
585 "changing profiles\n");
591 args.flags |= BTRFS_BALANCE_FORCE;
593 dump_ioctl_balance_args(&args);
595 return do_balance(argv[optind], &args, nofilters);
598 static const char * const cmd_balance_pause_usage[] = {
599 "btrfs balance pause <path>",
600 "Pause running balance",
604 static int cmd_balance_pause(int argc, char **argv)
610 DIR *dirstream = NULL;
612 if (check_argc_exact(argc, 2))
613 usage(cmd_balance_pause_usage);
617 fd = btrfs_open_dir(path, &dirstream, 1);
621 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
623 close_file_or_dir(fd, dirstream);
626 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
627 path, (e == ENOTCONN) ? "Not running" : strerror(e));
637 static const char * const cmd_balance_cancel_usage[] = {
638 "btrfs balance cancel <path>",
639 "Cancel running or paused balance",
643 static int cmd_balance_cancel(int argc, char **argv)
649 DIR *dirstream = NULL;
651 if (check_argc_exact(argc, 2))
652 usage(cmd_balance_cancel_usage);
656 fd = btrfs_open_dir(path, &dirstream, 1);
660 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
662 close_file_or_dir(fd, dirstream);
665 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
666 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
676 static const char * const cmd_balance_resume_usage[] = {
677 "btrfs balance resume <path>",
678 "Resume interrupted balance",
682 static int cmd_balance_resume(int argc, char **argv)
684 struct btrfs_ioctl_balance_args args;
686 DIR *dirstream = NULL;
691 if (check_argc_exact(argc, 2))
692 usage(cmd_balance_resume_usage);
696 fd = btrfs_open_dir(path, &dirstream, 1);
700 memset(&args, 0, sizeof(args));
701 args.flags |= BTRFS_BALANCE_RESUME;
703 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
705 close_file_or_dir(fd, dirstream);
708 if (e == ECANCELED) {
709 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
710 fprintf(stderr, "balance paused by user\n");
711 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
712 fprintf(stderr, "balance canceled by user\n");
713 } else if (e == ENOTCONN || e == EINPROGRESS) {
714 fprintf(stderr, "ERROR: balance resume on '%s' "
715 "failed - %s\n", path,
716 (e == ENOTCONN) ? "Not in progress" :
724 "ERROR: error during balancing '%s' - %s\n"
725 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
729 printf("Done, had to relocate %llu out of %llu chunks\n",
730 (unsigned long long)args.stat.completed,
731 (unsigned long long)args.stat.considered);
737 static const char * const cmd_balance_status_usage[] = {
738 "btrfs balance status [-v] <path>",
739 "Show status of running or paused balance",
745 /* Checks the status of the balance if any
747 * 2 : Error failed to know if there is any pending balance
748 * 1 : Successful to know status of a pending balance
749 * 0 : When there is no pending balance or completed
751 static int cmd_balance_status(int argc, char **argv)
753 struct btrfs_ioctl_balance_args args;
755 DIR *dirstream = NULL;
764 static const struct option longopts[] = {
765 { "verbose", no_argument, NULL, 'v' },
769 opt = getopt_long(argc, argv, "v", longopts, NULL);
778 usage(cmd_balance_status_usage);
782 if (check_argc_exact(argc - optind, 1))
783 usage(cmd_balance_status_usage);
787 fd = btrfs_open_dir(path, &dirstream, 1);
791 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
793 close_file_or_dir(fd, dirstream);
797 printf("No balance found on '%s'\n", path);
800 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
805 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
806 printf("Balance on '%s' is running", path);
807 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
808 printf(", cancel requested\n");
809 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
810 printf(", pause requested\n");
814 printf("Balance on '%s' is paused\n", path);
817 printf("%llu out of about %llu chunks balanced (%llu considered), "
818 "%3.f%% left\n", (unsigned long long)args.stat.completed,
819 (unsigned long long)args.stat.expected,
820 (unsigned long long)args.stat.considered,
821 100 * (1 - (float)args.stat.completed/args.stat.expected));
824 dump_ioctl_balance_args(&args);
829 static const char balance_cmd_group_info[] =
830 "balance data accross devices, or change block groups using filters";
832 const struct cmd_group balance_cmd_group = {
833 balance_cmd_group_usage, balance_cmd_group_info, {
834 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
835 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
836 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
837 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
838 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
843 int cmd_balance(int argc, char **argv)
846 /* old 'btrfs filesystem balance <path>' syntax */
847 struct btrfs_ioctl_balance_args args;
849 memset(&args, 0, sizeof(args));
850 args.flags |= BTRFS_BALANCE_TYPE_MASK;
852 return do_balance(argv[1], &args, 1);
855 return handle_command_group(&balance_cmd_group, argc, argv);