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);
91 static int parse_range(const char *range, u64 *start, u64 *end)
95 dots = strstr(range, "..");
97 const char *rest = dots + 2;
106 if (parse_u64(rest, end))
113 if (parse_u64(range, start))
117 if (*start >= *end) {
118 fprintf(stderr, "Range %llu..%llu doesn't make "
119 "sense\n", (unsigned long long)*start,
120 (unsigned long long)*end);
131 static int parse_filters(char *filters, struct btrfs_balance_args *args)
135 char *save_ptr = NULL; /* Satisfy static checkers */
140 for (this_char = strtok_r(filters, ",", &save_ptr);
142 this_char = strtok_r(NULL, ",", &save_ptr)) {
143 if ((value = strchr(this_char, '=')) != NULL)
145 if (!strcmp(this_char, "profiles")) {
146 if (!value || !*value) {
147 fprintf(stderr, "the profiles filter requires "
151 if (parse_profiles(value, &args->profiles)) {
152 fprintf(stderr, "Invalid profiles argument\n");
155 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
156 } else if (!strcmp(this_char, "usage")) {
157 if (!value || !*value) {
158 fprintf(stderr, "the usage filter requires "
162 if (parse_u64(value, &args->usage) ||
164 fprintf(stderr, "Invalid usage argument: %s\n",
168 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
169 } else if (!strcmp(this_char, "devid")) {
170 if (!value || !*value) {
171 fprintf(stderr, "the devid filter requires "
175 if (parse_u64(value, &args->devid) ||
177 fprintf(stderr, "Invalid devid argument: %s\n",
181 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
182 } else if (!strcmp(this_char, "drange")) {
183 if (!value || !*value) {
184 fprintf(stderr, "the drange filter requires "
188 if (parse_range(value, &args->pstart, &args->pend)) {
189 fprintf(stderr, "Invalid drange argument\n");
192 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
193 } else if (!strcmp(this_char, "vrange")) {
194 if (!value || !*value) {
195 fprintf(stderr, "the vrange filter requires "
199 if (parse_range(value, &args->vstart, &args->vend)) {
200 fprintf(stderr, "Invalid vrange argument\n");
203 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
204 } else if (!strcmp(this_char, "convert")) {
205 if (!value || !*value) {
206 fprintf(stderr, "the convert option requires "
210 if (parse_one_profile(value, &args->target)) {
211 fprintf(stderr, "Invalid convert argument\n");
214 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
215 } else if (!strcmp(this_char, "soft")) {
216 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
217 } else if (!strcmp(this_char, "limit")) {
218 if (!value || !*value) {
220 "the limit filter requires an argument\n");
223 if (parse_u64(value, &args->limit)) {
224 fprintf(stderr, "Invalid limit argument: %s\n",
228 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
230 fprintf(stderr, "Unrecognized balance option '%s'\n",
239 static void dump_balance_args(struct btrfs_balance_args *args)
241 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
242 printf("converting, target=%llu, soft is %s",
243 (unsigned long long)args->target,
244 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
249 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
250 printf(", profiles=%llu", (unsigned long long)args->profiles);
251 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
252 printf(", usage=%llu", (unsigned long long)args->usage);
253 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
254 printf(", devid=%llu", (unsigned long long)args->devid);
255 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
256 printf(", drange=%llu..%llu",
257 (unsigned long long)args->pstart,
258 (unsigned long long)args->pend);
259 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
260 printf(", vrange=%llu..%llu",
261 (unsigned long long)args->vstart,
262 (unsigned long long)args->vend);
263 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
264 printf(", limit=%llu", (unsigned long long)args->limit);
269 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
271 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
272 (unsigned long long)args->flags, (unsigned long long)args->state,
273 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
274 if (args->flags & BTRFS_BALANCE_DATA) {
275 printf(" DATA (flags 0x%llx): ",
276 (unsigned long long)args->data.flags);
277 dump_balance_args(&args->data);
279 if (args->flags & BTRFS_BALANCE_METADATA) {
280 printf(" METADATA (flags 0x%llx): ",
281 (unsigned long long)args->meta.flags);
282 dump_balance_args(&args->meta);
284 if (args->flags & BTRFS_BALANCE_SYSTEM) {
285 printf(" SYSTEM (flags 0x%llx): ",
286 (unsigned long long)args->sys.flags);
287 dump_balance_args(&args->sys);
291 static int do_balance_v1(int fd)
293 struct btrfs_ioctl_vol_args args;
296 memset(&args, 0, sizeof(args));
297 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
301 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
307 DIR *dirstream = NULL;
309 fd = open_file_or_dir(path, &dirstream);
311 fprintf(stderr, "ERROR: can't access '%s'\n", path);
315 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
320 * older kernels don't have the new balance ioctl, try the
321 * old one. But, the old one doesn't know any filters, so
322 * don't fall back if they tried to use the fancy new things
324 if (e == ENOTTY && nofilters) {
325 ret = do_balance_v1(fd);
331 if (e == ECANCELED) {
332 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
333 fprintf(stderr, "balance paused by user\n");
334 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
335 fprintf(stderr, "balance canceled by user\n");
338 fprintf(stderr, "ERROR: error during balancing '%s' "
339 "- %s\n", path, strerror(e));
340 if (e != EINPROGRESS)
341 fprintf(stderr, "There may be more info in "
342 "syslog - try dmesg | tail\n");
346 printf("Done, had to relocate %llu out of %llu chunks\n",
347 (unsigned long long)args->stat.completed,
348 (unsigned long long)args->stat.considered);
353 close_file_or_dir(fd, dirstream);
357 static const char * const cmd_balance_start_usage[] = {
358 "btrfs balance start [options] <path>",
359 "Balance chunks across the devices",
360 "Balance and/or convert (change allocation profile of) chunks that",
361 "passed all filters in a comma-separated list of filters for a",
362 "particular chunk type. If filter list is not given balance all",
363 "chunks of that type. In case none of the -d, -m or -s options is",
364 "given balance all chunks in a filesystem.",
366 "-d[filters] act on data chunks",
367 "-m[filters] act on metadata chunks",
368 "-s[filters] act on system chunks (only under -f)",
370 "-f force reducing of metadata integrity",
374 static int cmd_balance_start(int argc, char **argv)
376 struct btrfs_ioctl_balance_args args;
377 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
384 memset(&args, 0, sizeof(args));
388 static const struct option longopts[] = {
389 { "data", optional_argument, NULL, 'd'},
390 { "metadata", optional_argument, NULL, 'm' },
391 { "system", optional_argument, NULL, 's' },
392 { "force", no_argument, NULL, 'f' },
393 { "verbose", no_argument, NULL, 'v' },
397 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
404 args.flags |= BTRFS_BALANCE_DATA;
406 if (parse_filters(optarg, &args.data))
411 args.flags |= BTRFS_BALANCE_SYSTEM;
413 if (parse_filters(optarg, &args.sys))
418 args.flags |= BTRFS_BALANCE_METADATA;
420 if (parse_filters(optarg, &args.meta))
430 usage(cmd_balance_start_usage);
434 if (check_argc_exact(argc - optind, 1))
435 usage(cmd_balance_start_usage);
438 * allow -s only under --force, otherwise do with system chunks
439 * the same thing we were ordered to do with meta chunks
441 if (args.flags & BTRFS_BALANCE_SYSTEM) {
444 "Refusing to explicitly operate on system chunks.\n"
445 "Pass --force if you really want to do that.\n");
448 } else if (args.flags & BTRFS_BALANCE_METADATA) {
449 args.flags |= BTRFS_BALANCE_SYSTEM;
450 memcpy(&args.sys, &args.meta,
451 sizeof(struct btrfs_balance_args));
455 /* relocate everything - no filters */
456 args.flags |= BTRFS_BALANCE_TYPE_MASK;
459 /* drange makes sense only when devid is set */
460 for (i = 0; ptrs[i]; i++) {
461 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
462 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
463 fprintf(stderr, "drange filter can be used only if "
464 "devid filter is used\n");
469 /* soft makes sense only when convert for corresponding type is set */
470 for (i = 0; ptrs[i]; i++) {
471 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
472 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
473 fprintf(stderr, "'soft' option can be used only if "
474 "changing profiles\n");
480 args.flags |= BTRFS_BALANCE_FORCE;
482 dump_ioctl_balance_args(&args);
484 return do_balance(argv[optind], &args, nofilters);
487 static const char * const cmd_balance_pause_usage[] = {
488 "btrfs balance pause <path>",
489 "Pause running balance",
493 static int cmd_balance_pause(int argc, char **argv)
499 DIR *dirstream = NULL;
501 if (check_argc_exact(argc, 2))
502 usage(cmd_balance_pause_usage);
506 fd = open_file_or_dir(path, &dirstream);
508 fprintf(stderr, "ERROR: can't access '%s'\n", path);
512 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
514 close_file_or_dir(fd, dirstream);
517 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
518 path, (e == ENOTCONN) ? "Not running" : strerror(e));
528 static const char * const cmd_balance_cancel_usage[] = {
529 "btrfs balance cancel <path>",
530 "Cancel running or paused balance",
534 static int cmd_balance_cancel(int argc, char **argv)
540 DIR *dirstream = NULL;
542 if (check_argc_exact(argc, 2))
543 usage(cmd_balance_cancel_usage);
547 fd = open_file_or_dir(path, &dirstream);
549 fprintf(stderr, "ERROR: can't access '%s'\n", path);
553 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
555 close_file_or_dir(fd, dirstream);
558 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
559 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
569 static const char * const cmd_balance_resume_usage[] = {
570 "btrfs balance resume <path>",
571 "Resume interrupted balance",
575 static int cmd_balance_resume(int argc, char **argv)
577 struct btrfs_ioctl_balance_args args;
579 DIR *dirstream = NULL;
584 if (check_argc_exact(argc, 2))
585 usage(cmd_balance_resume_usage);
589 fd = open_file_or_dir(path, &dirstream);
591 fprintf(stderr, "ERROR: can't access '%s'\n", path);
595 memset(&args, 0, sizeof(args));
596 args.flags |= BTRFS_BALANCE_RESUME;
598 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
600 close_file_or_dir(fd, dirstream);
603 if (e == ECANCELED) {
604 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
605 fprintf(stderr, "balance paused by user\n");
606 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
607 fprintf(stderr, "balance canceled by user\n");
608 } else if (e == ENOTCONN || e == EINPROGRESS) {
609 fprintf(stderr, "ERROR: balance resume on '%s' "
610 "failed - %s\n", path,
611 (e == ENOTCONN) ? "Not in progress" :
619 "ERROR: error during balancing '%s' - %s\n"
620 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
624 printf("Done, had to relocate %llu out of %llu chunks\n",
625 (unsigned long long)args.stat.completed,
626 (unsigned long long)args.stat.considered);
632 static const char * const cmd_balance_status_usage[] = {
633 "btrfs balance status [-v] <path>",
634 "Show status of running or paused balance",
640 /* Checks the status of the balance if any
642 * 2 : Error failed to know if there is any pending balance
643 * 1 : Successful to know status of a pending balance
644 * 0 : When there is no pending balance or completed
646 static int cmd_balance_status(int argc, char **argv)
648 struct btrfs_ioctl_balance_args args;
650 DIR *dirstream = NULL;
659 static const struct option longopts[] = {
660 { "verbose", no_argument, NULL, 'v' },
664 opt = getopt_long(argc, argv, "v", longopts, NULL);
673 usage(cmd_balance_status_usage);
677 if (check_argc_exact(argc - optind, 1))
678 usage(cmd_balance_status_usage);
682 fd = open_file_or_dir(path, &dirstream);
684 fprintf(stderr, "ERROR: can't access '%s'\n", path);
688 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
690 close_file_or_dir(fd, dirstream);
694 printf("No balance found on '%s'\n", path);
697 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
702 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
703 printf("Balance on '%s' is running", path);
704 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
705 printf(", cancel requested\n");
706 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
707 printf(", pause requested\n");
711 printf("Balance on '%s' is paused\n", path);
714 printf("%llu out of about %llu chunks balanced (%llu considered), "
715 "%3.f%% left\n", (unsigned long long)args.stat.completed,
716 (unsigned long long)args.stat.expected,
717 (unsigned long long)args.stat.considered,
718 100 * (1 - (float)args.stat.completed/args.stat.expected));
721 dump_ioctl_balance_args(&args);
726 static const char balance_cmd_group_info[] =
727 "balance data accross devices, or change block groups using filters";
729 const struct cmd_group balance_cmd_group = {
730 balance_cmd_group_usage, balance_cmd_group_info, {
731 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
732 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
733 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
734 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
735 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
740 int cmd_balance(int argc, char **argv)
743 /* old 'btrfs filesystem balance <path>' syntax */
744 struct btrfs_ioctl_balance_args args;
746 memset(&args, 0, sizeof(args));
747 args.flags |= BTRFS_BALANCE_TYPE_MASK;
749 return do_balance(argv[1], &args, 1);
752 return handle_command_group(&balance_cmd_group, argc, argv);