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 != '.')
131 if (*start >= *end) {
132 fprintf(stderr, "Range %llu..%llu doesn't make "
133 "sense\n", (unsigned long long)*start,
134 (unsigned long long)*end);
144 static int parse_filters(char *filters, struct btrfs_balance_args *args)
148 char *save_ptr = NULL; /* Satisfy static checkers */
153 for (this_char = strtok_r(filters, ",", &save_ptr);
155 this_char = strtok_r(NULL, ",", &save_ptr)) {
156 if ((value = strchr(this_char, '=')) != NULL)
158 if (!strcmp(this_char, "profiles")) {
159 if (!value || !*value) {
160 fprintf(stderr, "the profiles filter requires "
164 if (parse_profiles(value, &args->profiles)) {
165 fprintf(stderr, "Invalid profiles argument\n");
168 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
169 } else if (!strcmp(this_char, "usage")) {
170 if (!value || !*value) {
171 fprintf(stderr, "the usage filter requires "
175 if (parse_u64(value, &args->usage) ||
177 fprintf(stderr, "Invalid usage argument: %s\n",
181 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
182 } else if (!strcmp(this_char, "devid")) {
183 if (!value || !*value) {
184 fprintf(stderr, "the devid filter requires "
188 if (parse_u64(value, &args->devid) ||
190 fprintf(stderr, "Invalid devid argument: %s\n",
194 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
195 } else if (!strcmp(this_char, "drange")) {
196 if (!value || !*value) {
197 fprintf(stderr, "the drange filter requires "
201 if (parse_range(value, &args->pstart, &args->pend)) {
202 fprintf(stderr, "Invalid drange argument\n");
205 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
206 } else if (!strcmp(this_char, "vrange")) {
207 if (!value || !*value) {
208 fprintf(stderr, "the vrange filter requires "
212 if (parse_range(value, &args->vstart, &args->vend)) {
213 fprintf(stderr, "Invalid vrange argument\n");
216 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
217 } else if (!strcmp(this_char, "convert")) {
218 if (!value || !*value) {
219 fprintf(stderr, "the convert option requires "
223 if (parse_one_profile(value, &args->target)) {
224 fprintf(stderr, "Invalid convert argument\n");
227 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
228 } else if (!strcmp(this_char, "soft")) {
229 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
230 } else if (!strcmp(this_char, "limit")) {
231 if (!value || !*value) {
233 "the limit filter requires an argument\n");
236 if (parse_u64(value, &args->limit)) {
237 fprintf(stderr, "Invalid limit argument: %s\n",
241 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
243 fprintf(stderr, "Unrecognized balance option '%s'\n",
252 static void dump_balance_args(struct btrfs_balance_args *args)
254 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
255 printf("converting, target=%llu, soft is %s",
256 (unsigned long long)args->target,
257 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
262 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
263 printf(", profiles=%llu", (unsigned long long)args->profiles);
264 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
265 printf(", usage=%llu", (unsigned long long)args->usage);
266 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
267 printf(", devid=%llu", (unsigned long long)args->devid);
268 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
269 printf(", drange=%llu..%llu",
270 (unsigned long long)args->pstart,
271 (unsigned long long)args->pend);
272 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
273 printf(", vrange=%llu..%llu",
274 (unsigned long long)args->vstart,
275 (unsigned long long)args->vend);
276 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
277 printf(", limit=%llu", (unsigned long long)args->limit);
282 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
284 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
285 (unsigned long long)args->flags, (unsigned long long)args->state,
286 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
287 if (args->flags & BTRFS_BALANCE_DATA) {
288 printf(" DATA (flags 0x%llx): ",
289 (unsigned long long)args->data.flags);
290 dump_balance_args(&args->data);
292 if (args->flags & BTRFS_BALANCE_METADATA) {
293 printf(" METADATA (flags 0x%llx): ",
294 (unsigned long long)args->meta.flags);
295 dump_balance_args(&args->meta);
297 if (args->flags & BTRFS_BALANCE_SYSTEM) {
298 printf(" SYSTEM (flags 0x%llx): ",
299 (unsigned long long)args->sys.flags);
300 dump_balance_args(&args->sys);
304 static int do_balance_v1(int fd)
306 struct btrfs_ioctl_vol_args args;
309 memset(&args, 0, sizeof(args));
310 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
314 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
320 DIR *dirstream = NULL;
322 fd = btrfs_open_dir(path, &dirstream, 1);
326 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
331 * older kernels don't have the new balance ioctl, try the
332 * old one. But, the old one doesn't know any filters, so
333 * don't fall back if they tried to use the fancy new things
335 if (e == ENOTTY && nofilters) {
336 ret = do_balance_v1(fd);
342 if (e == ECANCELED) {
343 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
344 fprintf(stderr, "balance paused by user\n");
345 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
346 fprintf(stderr, "balance canceled by user\n");
349 fprintf(stderr, "ERROR: error during balancing '%s' "
350 "- %s\n", path, strerror(e));
351 if (e != EINPROGRESS)
352 fprintf(stderr, "There may be more info in "
353 "syslog - try dmesg | tail\n");
357 printf("Done, had to relocate %llu out of %llu chunks\n",
358 (unsigned long long)args->stat.completed,
359 (unsigned long long)args->stat.considered);
364 close_file_or_dir(fd, dirstream);
368 static const char * const cmd_balance_start_usage[] = {
369 "btrfs balance start [options] <path>",
370 "Balance chunks across the devices",
371 "Balance and/or convert (change allocation profile of) chunks that",
372 "passed all filters in a comma-separated list of filters for a",
373 "particular chunk type. If filter list is not given balance all",
374 "chunks of that type. In case none of the -d, -m or -s options is",
375 "given balance all chunks in a filesystem.",
377 "-d[filters] act on data chunks",
378 "-m[filters] act on metadata chunks",
379 "-s[filters] act on system chunks (only under -f)",
381 "-f force reducing of metadata integrity",
385 static int cmd_balance_start(int argc, char **argv)
387 struct btrfs_ioctl_balance_args args;
388 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
395 memset(&args, 0, sizeof(args));
399 static const struct option longopts[] = {
400 { "data", optional_argument, NULL, 'd'},
401 { "metadata", optional_argument, NULL, 'm' },
402 { "system", optional_argument, NULL, 's' },
403 { "force", no_argument, NULL, 'f' },
404 { "verbose", no_argument, NULL, 'v' },
408 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
415 args.flags |= BTRFS_BALANCE_DATA;
417 if (parse_filters(optarg, &args.data))
422 args.flags |= BTRFS_BALANCE_SYSTEM;
424 if (parse_filters(optarg, &args.sys))
429 args.flags |= BTRFS_BALANCE_METADATA;
431 if (parse_filters(optarg, &args.meta))
441 usage(cmd_balance_start_usage);
445 if (check_argc_exact(argc - optind, 1))
446 usage(cmd_balance_start_usage);
449 * allow -s only under --force, otherwise do with system chunks
450 * the same thing we were ordered to do with meta chunks
452 if (args.flags & BTRFS_BALANCE_SYSTEM) {
455 "Refusing to explicitly operate on system chunks.\n"
456 "Pass --force if you really want to do that.\n");
459 } else if (args.flags & BTRFS_BALANCE_METADATA) {
460 args.flags |= BTRFS_BALANCE_SYSTEM;
461 memcpy(&args.sys, &args.meta,
462 sizeof(struct btrfs_balance_args));
466 /* relocate everything - no filters */
467 args.flags |= BTRFS_BALANCE_TYPE_MASK;
470 /* drange makes sense only when devid is set */
471 for (i = 0; ptrs[i]; i++) {
472 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
473 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
474 fprintf(stderr, "drange filter can be used only if "
475 "devid filter is used\n");
480 /* soft makes sense only when convert for corresponding type is set */
481 for (i = 0; ptrs[i]; i++) {
482 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
483 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
484 fprintf(stderr, "'soft' option can be used only if "
485 "changing profiles\n");
491 args.flags |= BTRFS_BALANCE_FORCE;
493 dump_ioctl_balance_args(&args);
495 return do_balance(argv[optind], &args, nofilters);
498 static const char * const cmd_balance_pause_usage[] = {
499 "btrfs balance pause <path>",
500 "Pause running balance",
504 static int cmd_balance_pause(int argc, char **argv)
510 DIR *dirstream = NULL;
512 if (check_argc_exact(argc, 2))
513 usage(cmd_balance_pause_usage);
517 fd = btrfs_open_dir(path, &dirstream, 1);
521 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
523 close_file_or_dir(fd, dirstream);
526 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
527 path, (e == ENOTCONN) ? "Not running" : strerror(e));
537 static const char * const cmd_balance_cancel_usage[] = {
538 "btrfs balance cancel <path>",
539 "Cancel running or paused balance",
543 static int cmd_balance_cancel(int argc, char **argv)
549 DIR *dirstream = NULL;
551 if (check_argc_exact(argc, 2))
552 usage(cmd_balance_cancel_usage);
556 fd = btrfs_open_dir(path, &dirstream, 1);
560 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
562 close_file_or_dir(fd, dirstream);
565 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
566 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
576 static const char * const cmd_balance_resume_usage[] = {
577 "btrfs balance resume <path>",
578 "Resume interrupted balance",
582 static int cmd_balance_resume(int argc, char **argv)
584 struct btrfs_ioctl_balance_args args;
586 DIR *dirstream = NULL;
591 if (check_argc_exact(argc, 2))
592 usage(cmd_balance_resume_usage);
596 fd = btrfs_open_dir(path, &dirstream, 1);
600 memset(&args, 0, sizeof(args));
601 args.flags |= BTRFS_BALANCE_RESUME;
603 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
605 close_file_or_dir(fd, dirstream);
608 if (e == ECANCELED) {
609 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
610 fprintf(stderr, "balance paused by user\n");
611 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
612 fprintf(stderr, "balance canceled by user\n");
613 } else if (e == ENOTCONN || e == EINPROGRESS) {
614 fprintf(stderr, "ERROR: balance resume on '%s' "
615 "failed - %s\n", path,
616 (e == ENOTCONN) ? "Not in progress" :
624 "ERROR: error during balancing '%s' - %s\n"
625 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
629 printf("Done, had to relocate %llu out of %llu chunks\n",
630 (unsigned long long)args.stat.completed,
631 (unsigned long long)args.stat.considered);
637 static const char * const cmd_balance_status_usage[] = {
638 "btrfs balance status [-v] <path>",
639 "Show status of running or paused balance",
645 /* Checks the status of the balance if any
647 * 2 : Error failed to know if there is any pending balance
648 * 1 : Successful to know status of a pending balance
649 * 0 : When there is no pending balance or completed
651 static int cmd_balance_status(int argc, char **argv)
653 struct btrfs_ioctl_balance_args args;
655 DIR *dirstream = NULL;
664 static const struct option longopts[] = {
665 { "verbose", no_argument, NULL, 'v' },
669 opt = getopt_long(argc, argv, "v", longopts, NULL);
678 usage(cmd_balance_status_usage);
682 if (check_argc_exact(argc - optind, 1))
683 usage(cmd_balance_status_usage);
687 fd = btrfs_open_dir(path, &dirstream, 1);
691 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
693 close_file_or_dir(fd, dirstream);
697 printf("No balance found on '%s'\n", path);
700 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
705 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
706 printf("Balance on '%s' is running", path);
707 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
708 printf(", cancel requested\n");
709 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
710 printf(", pause requested\n");
714 printf("Balance on '%s' is paused\n", path);
717 printf("%llu out of about %llu chunks balanced (%llu considered), "
718 "%3.f%% left\n", (unsigned long long)args.stat.completed,
719 (unsigned long long)args.stat.expected,
720 (unsigned long long)args.stat.considered,
721 100 * (1 - (float)args.stat.completed/args.stat.expected));
724 dump_ioctl_balance_args(&args);
729 static const char balance_cmd_group_info[] =
730 "balance data accross devices, or change block groups using filters";
732 const struct cmd_group balance_cmd_group = {
733 balance_cmd_group_usage, balance_cmd_group_info, {
734 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
735 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
736 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
737 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
738 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
743 int cmd_balance(int argc, char **argv)
746 /* old 'btrfs filesystem balance <path>' syntax */
747 struct btrfs_ioctl_balance_args args;
749 memset(&args, 0, sizeof(args));
750 args.flags |= BTRFS_BALANCE_TYPE_MASK;
752 return do_balance(argv[1], &args, 1);
755 return handle_command_group(&balance_cmd_group, argc, argv);