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 const char balance_cmd_group_info[] =
40 "'btrfs filesystem balance' command is deprecated, please use\n"
41 "'btrfs balance start' command instead.";
43 static int parse_one_profile(const char *profile, u64 *flags)
45 if (!strcmp(profile, "raid0")) {
46 *flags |= BTRFS_BLOCK_GROUP_RAID0;
47 } else if (!strcmp(profile, "raid1")) {
48 *flags |= BTRFS_BLOCK_GROUP_RAID1;
49 } else if (!strcmp(profile, "raid10")) {
50 *flags |= BTRFS_BLOCK_GROUP_RAID10;
51 } else if (!strcmp(profile, "raid5")) {
52 *flags |= BTRFS_BLOCK_GROUP_RAID5;
53 } else if (!strcmp(profile, "raid6")) {
54 *flags |= BTRFS_BLOCK_GROUP_RAID6;
55 } else if (!strcmp(profile, "dup")) {
56 *flags |= BTRFS_BLOCK_GROUP_DUP;
57 } else if (!strcmp(profile, "single")) {
58 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
60 fprintf(stderr, "Unknown profile '%s'\n", profile);
67 static int parse_profiles(char *profiles, u64 *flags)
70 char *save_ptr = NULL; /* Satisfy static checkers */
72 for (this_char = strtok_r(profiles, "|", &save_ptr);
74 this_char = strtok_r(NULL, "|", &save_ptr)) {
75 if (parse_one_profile(this_char, flags))
82 static int parse_u64(const char *str, u64 *result)
87 val = strtoull(str, &endptr, 10);
95 static int parse_range(const char *range, u64 *start, u64 *end)
99 dots = strstr(range, "..");
101 const char *rest = dots + 2;
110 if (parse_u64(rest, end))
117 if (parse_u64(range, start))
121 if (*start >= *end) {
122 fprintf(stderr, "Range %llu..%llu doesn't make "
123 "sense\n", (unsigned long long)*start,
124 (unsigned long long)*end);
135 static int parse_filters(char *filters, struct btrfs_balance_args *args)
139 char *save_ptr = NULL; /* Satisfy static checkers */
144 for (this_char = strtok_r(filters, ",", &save_ptr);
146 this_char = strtok_r(NULL, ",", &save_ptr)) {
147 if ((value = strchr(this_char, '=')) != NULL)
149 if (!strcmp(this_char, "profiles")) {
150 if (!value || !*value) {
151 fprintf(stderr, "the profiles filter requires "
155 if (parse_profiles(value, &args->profiles)) {
156 fprintf(stderr, "Invalid profiles argument\n");
159 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
160 } else if (!strcmp(this_char, "usage")) {
161 if (!value || !*value) {
162 fprintf(stderr, "the usage filter requires "
166 if (parse_u64(value, &args->usage) ||
168 fprintf(stderr, "Invalid usage argument: %s\n",
172 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
173 } else if (!strcmp(this_char, "devid")) {
174 if (!value || !*value) {
175 fprintf(stderr, "the devid filter requires "
179 if (parse_u64(value, &args->devid) ||
181 fprintf(stderr, "Invalid devid argument: %s\n",
185 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
186 } else if (!strcmp(this_char, "drange")) {
187 if (!value || !*value) {
188 fprintf(stderr, "the drange filter requires "
192 if (parse_range(value, &args->pstart, &args->pend)) {
193 fprintf(stderr, "Invalid drange argument\n");
196 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
197 } else if (!strcmp(this_char, "vrange")) {
198 if (!value || !*value) {
199 fprintf(stderr, "the vrange filter requires "
203 if (parse_range(value, &args->vstart, &args->vend)) {
204 fprintf(stderr, "Invalid vrange argument\n");
207 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
208 } else if (!strcmp(this_char, "convert")) {
209 if (!value || !*value) {
210 fprintf(stderr, "the convert option requires "
214 if (parse_one_profile(value, &args->target)) {
215 fprintf(stderr, "Invalid convert argument\n");
218 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
219 } else if (!strcmp(this_char, "soft")) {
220 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
221 } else if (!strcmp(this_char, "limit")) {
222 if (!value || !*value) {
224 "the limit filter requires an argument\n");
227 if (parse_u64(value, &args->limit)) {
228 fprintf(stderr, "Invalid limit argument: %s\n",
232 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
234 fprintf(stderr, "Unrecognized balance option '%s'\n",
243 static void dump_balance_args(struct btrfs_balance_args *args)
245 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
246 printf("converting, target=%llu, soft is %s",
247 (unsigned long long)args->target,
248 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
253 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
254 printf(", profiles=%llu", (unsigned long long)args->profiles);
255 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
256 printf(", usage=%llu", (unsigned long long)args->usage);
257 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
258 printf(", devid=%llu", (unsigned long long)args->devid);
259 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
260 printf(", drange=%llu..%llu",
261 (unsigned long long)args->pstart,
262 (unsigned long long)args->pend);
263 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
264 printf(", vrange=%llu..%llu",
265 (unsigned long long)args->vstart,
266 (unsigned long long)args->vend);
267 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
268 printf(", limit=%llu", (unsigned long long)args->limit);
273 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
275 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
276 (unsigned long long)args->flags, (unsigned long long)args->state,
277 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
278 if (args->flags & BTRFS_BALANCE_DATA) {
279 printf(" DATA (flags 0x%llx): ",
280 (unsigned long long)args->data.flags);
281 dump_balance_args(&args->data);
283 if (args->flags & BTRFS_BALANCE_METADATA) {
284 printf(" METADATA (flags 0x%llx): ",
285 (unsigned long long)args->meta.flags);
286 dump_balance_args(&args->meta);
288 if (args->flags & BTRFS_BALANCE_SYSTEM) {
289 printf(" SYSTEM (flags 0x%llx): ",
290 (unsigned long long)args->sys.flags);
291 dump_balance_args(&args->sys);
295 static int do_balance_v1(int fd)
297 struct btrfs_ioctl_vol_args args;
300 memset(&args, 0, sizeof(args));
301 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
305 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
311 DIR *dirstream = NULL;
313 fd = open_file_or_dir(path, &dirstream);
315 fprintf(stderr, "ERROR: can't access '%s'\n", path);
319 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
324 * older kernels don't have the new balance ioctl, try the
325 * old one. But, the old one doesn't know any filters, so
326 * don't fall back if they tried to use the fancy new things
328 if (e == ENOTTY && nofilters) {
329 ret = do_balance_v1(fd);
335 if (e == ECANCELED) {
336 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
337 fprintf(stderr, "balance paused by user\n");
338 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
339 fprintf(stderr, "balance canceled by user\n");
342 fprintf(stderr, "ERROR: error during balancing '%s' "
343 "- %s\n", path, strerror(e));
344 if (e != EINPROGRESS)
345 fprintf(stderr, "There may be more info in "
346 "syslog - try dmesg | tail\n");
350 printf("Done, had to relocate %llu out of %llu chunks\n",
351 (unsigned long long)args->stat.completed,
352 (unsigned long long)args->stat.considered);
357 close_file_or_dir(fd, dirstream);
361 static const char * const cmd_balance_start_usage[] = {
362 "btrfs balance start [options] <path>",
363 "Balance chunks across the devices",
364 "Balance and/or convert (change allocation profile of) chunks that",
365 "passed all filters in a comma-separated list of filters for a",
366 "particular chunk type. If filter list is not given balance all",
367 "chunks of that type. In case none of the -d, -m or -s options is",
368 "given balance all chunks in a filesystem.",
370 "-d[filters] act on data chunks",
371 "-m[filters] act on metadata chunks",
372 "-s[filters] act on system chunks (only under -f)",
374 "-f force reducing of metadata integrity",
378 static int cmd_balance_start(int argc, char **argv)
380 struct btrfs_ioctl_balance_args args;
381 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
388 memset(&args, 0, sizeof(args));
392 static const struct option longopts[] = {
393 { "data", optional_argument, NULL, 'd'},
394 { "metadata", optional_argument, NULL, 'm' },
395 { "system", optional_argument, NULL, 's' },
396 { "force", no_argument, NULL, 'f' },
397 { "verbose", no_argument, NULL, 'v' },
401 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
408 args.flags |= BTRFS_BALANCE_DATA;
410 if (parse_filters(optarg, &args.data))
415 args.flags |= BTRFS_BALANCE_SYSTEM;
417 if (parse_filters(optarg, &args.sys))
422 args.flags |= BTRFS_BALANCE_METADATA;
424 if (parse_filters(optarg, &args.meta))
434 usage(cmd_balance_start_usage);
438 if (check_argc_exact(argc - optind, 1))
439 usage(cmd_balance_start_usage);
442 * allow -s only under --force, otherwise do with system chunks
443 * the same thing we were ordered to do with meta chunks
445 if (args.flags & BTRFS_BALANCE_SYSTEM) {
448 "Refusing to explicitly operate on system chunks.\n"
449 "Pass --force if you really want to do that.\n");
452 } else if (args.flags & BTRFS_BALANCE_METADATA) {
453 args.flags |= BTRFS_BALANCE_SYSTEM;
454 memcpy(&args.sys, &args.meta,
455 sizeof(struct btrfs_balance_args));
459 /* relocate everything - no filters */
460 args.flags |= BTRFS_BALANCE_TYPE_MASK;
463 /* drange makes sense only when devid is set */
464 for (i = 0; ptrs[i]; i++) {
465 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
466 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
467 fprintf(stderr, "drange filter can be used only if "
468 "devid filter is used\n");
473 /* soft makes sense only when convert for corresponding type is set */
474 for (i = 0; ptrs[i]; i++) {
475 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
476 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
477 fprintf(stderr, "'soft' option can be used only if "
478 "changing profiles\n");
484 args.flags |= BTRFS_BALANCE_FORCE;
486 dump_ioctl_balance_args(&args);
488 return do_balance(argv[optind], &args, nofilters);
491 static const char * const cmd_balance_pause_usage[] = {
492 "btrfs balance pause <path>",
493 "Pause running balance",
497 static int cmd_balance_pause(int argc, char **argv)
503 DIR *dirstream = NULL;
505 if (check_argc_exact(argc, 2))
506 usage(cmd_balance_pause_usage);
510 fd = open_file_or_dir(path, &dirstream);
512 fprintf(stderr, "ERROR: can't access '%s'\n", path);
516 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
518 close_file_or_dir(fd, dirstream);
521 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
522 path, (e == ENOTCONN) ? "Not running" : strerror(e));
532 static const char * const cmd_balance_cancel_usage[] = {
533 "btrfs balance cancel <path>",
534 "Cancel running or paused balance",
538 static int cmd_balance_cancel(int argc, char **argv)
544 DIR *dirstream = NULL;
546 if (check_argc_exact(argc, 2))
547 usage(cmd_balance_cancel_usage);
551 fd = open_file_or_dir(path, &dirstream);
553 fprintf(stderr, "ERROR: can't access '%s'\n", path);
557 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
559 close_file_or_dir(fd, dirstream);
562 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
563 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
573 static const char * const cmd_balance_resume_usage[] = {
574 "btrfs balance resume <path>",
575 "Resume interrupted balance",
579 static int cmd_balance_resume(int argc, char **argv)
581 struct btrfs_ioctl_balance_args args;
583 DIR *dirstream = NULL;
588 if (check_argc_exact(argc, 2))
589 usage(cmd_balance_resume_usage);
593 fd = open_file_or_dir(path, &dirstream);
595 fprintf(stderr, "ERROR: can't access '%s'\n", path);
599 memset(&args, 0, sizeof(args));
600 args.flags |= BTRFS_BALANCE_RESUME;
602 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
604 close_file_or_dir(fd, dirstream);
607 if (e == ECANCELED) {
608 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
609 fprintf(stderr, "balance paused by user\n");
610 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
611 fprintf(stderr, "balance canceled by user\n");
612 } else if (e == ENOTCONN || e == EINPROGRESS) {
613 fprintf(stderr, "ERROR: balance resume on '%s' "
614 "failed - %s\n", path,
615 (e == ENOTCONN) ? "Not in progress" :
623 "ERROR: error during balancing '%s' - %s\n"
624 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
628 printf("Done, had to relocate %llu out of %llu chunks\n",
629 (unsigned long long)args.stat.completed,
630 (unsigned long long)args.stat.considered);
636 static const char * const cmd_balance_status_usage[] = {
637 "btrfs balance status [-v] <path>",
638 "Show status of running or paused balance",
644 /* Checks the status of the balance if any
646 * 2 : Error failed to know if there is any pending balance
647 * 1 : Successful to know status of a pending balance
648 * 0 : When there is no pending balance or completed
650 static int cmd_balance_status(int argc, char **argv)
652 struct btrfs_ioctl_balance_args args;
654 DIR *dirstream = NULL;
663 static const struct option longopts[] = {
664 { "verbose", no_argument, NULL, 'v' },
668 opt = getopt_long(argc, argv, "v", longopts, NULL);
677 usage(cmd_balance_status_usage);
681 if (check_argc_exact(argc - optind, 1))
682 usage(cmd_balance_status_usage);
686 fd = open_file_or_dir(path, &dirstream);
688 fprintf(stderr, "ERROR: can't access '%s'\n", path);
692 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
694 close_file_or_dir(fd, dirstream);
698 printf("No balance found on '%s'\n", path);
701 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
706 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
707 printf("Balance on '%s' is running", path);
708 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
709 printf(", cancel requested\n");
710 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
711 printf(", pause requested\n");
715 printf("Balance on '%s' is paused\n", path);
718 printf("%llu out of about %llu chunks balanced (%llu considered), "
719 "%3.f%% left\n", (unsigned long long)args.stat.completed,
720 (unsigned long long)args.stat.expected,
721 (unsigned long long)args.stat.considered,
722 100 * (1 - (float)args.stat.completed/args.stat.expected));
725 dump_ioctl_balance_args(&args);
730 const struct cmd_group balance_cmd_group = {
731 balance_cmd_group_usage, balance_cmd_group_info, {
732 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
733 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
734 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
735 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
736 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
741 int cmd_balance(int argc, char **argv)
744 /* old 'btrfs filesystem balance <path>' syntax */
745 struct btrfs_ioctl_balance_args args;
747 memset(&args, 0, sizeof(args));
748 args.flags |= BTRFS_BALANCE_TYPE_MASK;
750 return do_balance(argv[1], &args, 1);
753 return handle_command_group(&balance_cmd_group, argc, argv);