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 [filesystem] balance <command> [options] <path>",
35 "btrfs [filesystem] 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 [filesystem] 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));
393 static struct option longopts[] = {
394 { "data", optional_argument, NULL, 'd'},
395 { "metadata", optional_argument, NULL, 'm' },
396 { "system", optional_argument, NULL, 's' },
397 { "force", no_argument, NULL, 'f' },
398 { "verbose", no_argument, NULL, 'v' },
399 { NULL, no_argument, NULL, 0 },
402 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
410 args.flags |= BTRFS_BALANCE_DATA;
412 if (parse_filters(optarg, &args.data))
417 args.flags |= BTRFS_BALANCE_SYSTEM;
419 if (parse_filters(optarg, &args.sys))
424 args.flags |= BTRFS_BALANCE_METADATA;
426 if (parse_filters(optarg, &args.meta))
436 usage(cmd_balance_start_usage);
440 if (check_argc_exact(argc - optind, 1))
441 usage(cmd_balance_start_usage);
444 * allow -s only under --force, otherwise do with system chunks
445 * the same thing we were ordered to do with meta chunks
447 if (args.flags & BTRFS_BALANCE_SYSTEM) {
450 "Refusing to explicitly operate on system chunks.\n"
451 "Pass --force if you really want to do that.\n");
454 } else if (args.flags & BTRFS_BALANCE_METADATA) {
455 args.flags |= BTRFS_BALANCE_SYSTEM;
456 memcpy(&args.sys, &args.meta,
457 sizeof(struct btrfs_balance_args));
461 /* relocate everything - no filters */
462 args.flags |= BTRFS_BALANCE_TYPE_MASK;
465 /* drange makes sense only when devid is set */
466 for (i = 0; ptrs[i]; i++) {
467 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
468 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
469 fprintf(stderr, "drange filter can be used only if "
470 "devid filter is used\n");
475 /* soft makes sense only when convert for corresponding type is set */
476 for (i = 0; ptrs[i]; i++) {
477 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
478 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
479 fprintf(stderr, "'soft' option can be used only if "
480 "changing profiles\n");
486 args.flags |= BTRFS_BALANCE_FORCE;
488 dump_ioctl_balance_args(&args);
490 return do_balance(argv[optind], &args, nofilters);
493 static const char * const cmd_balance_pause_usage[] = {
494 "btrfs [filesystem] balance pause <path>",
495 "Pause running balance",
499 static int cmd_balance_pause(int argc, char **argv)
505 DIR *dirstream = NULL;
507 if (check_argc_exact(argc, 2))
508 usage(cmd_balance_pause_usage);
512 fd = open_file_or_dir(path, &dirstream);
514 fprintf(stderr, "ERROR: can't access '%s'\n", path);
518 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
520 close_file_or_dir(fd, dirstream);
523 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
524 path, (e == ENOTCONN) ? "Not running" : strerror(e));
534 static const char * const cmd_balance_cancel_usage[] = {
535 "btrfs [filesystem] balance cancel <path>",
536 "Cancel running or paused balance",
540 static int cmd_balance_cancel(int argc, char **argv)
546 DIR *dirstream = NULL;
548 if (check_argc_exact(argc, 2))
549 usage(cmd_balance_cancel_usage);
553 fd = open_file_or_dir(path, &dirstream);
555 fprintf(stderr, "ERROR: can't access '%s'\n", path);
559 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
561 close_file_or_dir(fd, dirstream);
564 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
565 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
575 static const char * const cmd_balance_resume_usage[] = {
576 "btrfs [filesystem] balance resume <path>",
577 "Resume interrupted balance",
581 static int cmd_balance_resume(int argc, char **argv)
583 struct btrfs_ioctl_balance_args args;
585 DIR *dirstream = NULL;
590 if (check_argc_exact(argc, 2))
591 usage(cmd_balance_resume_usage);
595 fd = open_file_or_dir(path, &dirstream);
597 fprintf(stderr, "ERROR: can't access '%s'\n", path);
601 memset(&args, 0, sizeof(args));
602 args.flags |= BTRFS_BALANCE_RESUME;
604 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
606 close_file_or_dir(fd, dirstream);
609 if (e == ECANCELED) {
610 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
611 fprintf(stderr, "balance paused by user\n");
612 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
613 fprintf(stderr, "balance canceled by user\n");
614 } else if (e == ENOTCONN || e == EINPROGRESS) {
615 fprintf(stderr, "ERROR: balance resume on '%s' "
616 "failed - %s\n", path,
617 (e == ENOTCONN) ? "Not in progress" :
625 "ERROR: error during balancing '%s' - %s\n"
626 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
630 printf("Done, had to relocate %llu out of %llu chunks\n",
631 (unsigned long long)args.stat.completed,
632 (unsigned long long)args.stat.considered);
638 static const char * const cmd_balance_status_usage[] = {
639 "btrfs [filesystem] balance status [-v] <path>",
640 "Show status of running or paused balance",
646 /* Checks the status of the balance if any
648 * 2 : Error failed to know if there is any pending balance
649 * 1 : Successful to know status of a pending balance
650 * 0 : When there is no pending balance or completed
652 static int cmd_balance_status(int argc, char **argv)
654 struct btrfs_ioctl_balance_args args;
656 DIR *dirstream = NULL;
665 static struct option longopts[] = {
666 { "verbose", no_argument, NULL, 'v' },
667 { NULL, no_argument, NULL, 0}
670 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
679 usage(cmd_balance_status_usage);
683 if (check_argc_exact(argc - optind, 1))
684 usage(cmd_balance_status_usage);
688 fd = open_file_or_dir(path, &dirstream);
690 fprintf(stderr, "ERROR: can't access '%s'\n", path);
694 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
696 close_file_or_dir(fd, dirstream);
700 printf("No balance found on '%s'\n", path);
703 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
708 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
709 printf("Balance on '%s' is running", path);
710 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
711 printf(", cancel requested\n");
712 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
713 printf(", pause requested\n");
717 printf("Balance on '%s' is paused\n", path);
720 printf("%llu out of about %llu chunks balanced (%llu considered), "
721 "%3.f%% left\n", (unsigned long long)args.stat.completed,
722 (unsigned long long)args.stat.expected,
723 (unsigned long long)args.stat.considered,
724 100 * (1 - (float)args.stat.completed/args.stat.expected));
727 dump_ioctl_balance_args(&args);
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);