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;
222 fprintf(stderr, "Unrecognized balance option '%s'\n",
231 static void dump_balance_args(struct btrfs_balance_args *args)
233 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
234 printf("converting, target=%llu, soft is %s",
235 (unsigned long long)args->target,
236 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
241 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
242 printf(", profiles=%llu", (unsigned long long)args->profiles);
243 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
244 printf(", usage=%llu", (unsigned long long)args->usage);
245 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
246 printf(", devid=%llu", (unsigned long long)args->devid);
247 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
248 printf(", drange=%llu..%llu",
249 (unsigned long long)args->pstart,
250 (unsigned long long)args->pend);
251 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
252 printf(", vrange=%llu..%llu",
253 (unsigned long long)args->vstart,
254 (unsigned long long)args->vend);
259 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
261 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
262 (unsigned long long)args->flags, (unsigned long long)args->state,
263 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
264 if (args->flags & BTRFS_BALANCE_DATA) {
265 printf(" DATA (flags 0x%llx): ",
266 (unsigned long long)args->data.flags);
267 dump_balance_args(&args->data);
269 if (args->flags & BTRFS_BALANCE_METADATA) {
270 printf(" METADATA (flags 0x%llx): ",
271 (unsigned long long)args->meta.flags);
272 dump_balance_args(&args->meta);
274 if (args->flags & BTRFS_BALANCE_SYSTEM) {
275 printf(" SYSTEM (flags 0x%llx): ",
276 (unsigned long long)args->sys.flags);
277 dump_balance_args(&args->sys);
281 static int do_balance_v1(int fd)
283 struct btrfs_ioctl_vol_args args;
286 memset(&args, 0, sizeof(args));
287 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
291 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
298 fd = open_file_or_dir(path);
300 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
304 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
309 * older kernels don't have the new balance ioctl, try the
310 * old one. But, the old one doesn't know any filters, so
311 * don't fall back if they tried to use the fancy new things
313 if (e == ENOTTY && nofilters) {
314 ret = do_balance_v1(fd);
320 if (e == ECANCELED) {
321 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
322 fprintf(stderr, "balance paused by user\n");
323 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
324 fprintf(stderr, "balance canceled by user\n");
327 fprintf(stderr, "ERROR: error during balancing '%s' "
328 "- %s\n", path, strerror(e));
329 if (e != EINPROGRESS)
330 fprintf(stderr, "There may be more info in "
331 "syslog - try dmesg | tail\n");
335 printf("Done, had to relocate %llu out of %llu chunks\n",
336 (unsigned long long)args->stat.completed,
337 (unsigned long long)args->stat.considered);
346 static const char * const cmd_balance_start_usage[] = {
347 "btrfs [filesystem] balance start [options] <path>",
348 "Balance chunks across the devices",
349 "Balance and/or convert (change allocation profile of) chunks that",
350 "passed all filters in a comma-separated list of filters for a",
351 "particular chunk type. If filter list is not given balance all",
352 "chunks of that type. In case none of the -d, -m or -s options is",
353 "given balance all chunks in a filesystem.",
355 "-d[filters] act on data chunks",
356 "-m[filters] act on metadata chunks",
357 "-s[filters] act on system chunks (only under -f)",
359 "-f force reducing of metadata integrity",
363 static int cmd_balance_start(int argc, char **argv)
365 struct btrfs_ioctl_balance_args args;
366 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
373 memset(&args, 0, sizeof(args));
378 static struct option longopts[] = {
379 { "data", optional_argument, NULL, 'd'},
380 { "metadata", optional_argument, NULL, 'm' },
381 { "system", optional_argument, NULL, 's' },
382 { "force", no_argument, NULL, 'f' },
383 { "verbose", no_argument, NULL, 'v' },
387 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
395 args.flags |= BTRFS_BALANCE_DATA;
397 if (parse_filters(optarg, &args.data))
402 args.flags |= BTRFS_BALANCE_SYSTEM;
404 if (parse_filters(optarg, &args.sys))
409 args.flags |= BTRFS_BALANCE_METADATA;
411 if (parse_filters(optarg, &args.meta))
421 usage(cmd_balance_start_usage);
425 if (check_argc_exact(argc - optind, 1))
426 usage(cmd_balance_start_usage);
429 * allow -s only under --force, otherwise do with system chunks
430 * the same thing we were ordered to do with meta chunks
432 if (args.flags & BTRFS_BALANCE_SYSTEM) {
435 "Refusing to explicitly operate on system chunks.\n"
436 "Pass --force if you really want to do that.\n");
439 } else if (args.flags & BTRFS_BALANCE_METADATA) {
440 args.flags |= BTRFS_BALANCE_SYSTEM;
441 memcpy(&args.sys, &args.meta,
442 sizeof(struct btrfs_balance_args));
446 /* relocate everything - no filters */
447 args.flags |= BTRFS_BALANCE_TYPE_MASK;
450 /* drange makes sense only when devid is set */
451 for (i = 0; ptrs[i]; i++) {
452 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
453 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
454 fprintf(stderr, "drange filter can be used only if "
455 "devid filter is used\n");
460 /* soft makes sense only when convert for corresponding type is set */
461 for (i = 0; ptrs[i]; i++) {
462 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
463 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
464 fprintf(stderr, "'soft' option can be used only if "
465 "changing profiles\n");
471 args.flags |= BTRFS_BALANCE_FORCE;
473 dump_ioctl_balance_args(&args);
475 return do_balance(argv[optind], &args, nofilters);
478 static const char * const cmd_balance_pause_usage[] = {
479 "btrfs [filesystem] balance pause <path>",
480 "Pause running balance",
484 static int cmd_balance_pause(int argc, char **argv)
491 if (check_argc_exact(argc, 2))
492 usage(cmd_balance_pause_usage);
496 fd = open_file_or_dir(path);
498 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
502 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
507 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
508 path, (e == ENOTCONN) ? "Not running" : strerror(e));
515 static const char * const cmd_balance_cancel_usage[] = {
516 "btrfs [filesystem] balance cancel <path>",
517 "Cancel running or paused balance",
521 static int cmd_balance_cancel(int argc, char **argv)
528 if (check_argc_exact(argc, 2))
529 usage(cmd_balance_cancel_usage);
533 fd = open_file_or_dir(path);
535 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
539 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
544 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
545 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
552 static const char * const cmd_balance_resume_usage[] = {
553 "btrfs [filesystem] balance resume <path>",
554 "Resume interrupted balance",
558 static int cmd_balance_resume(int argc, char **argv)
560 struct btrfs_ioctl_balance_args args;
566 if (check_argc_exact(argc, 2))
567 usage(cmd_balance_resume_usage);
571 fd = open_file_or_dir(path);
573 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
577 memset(&args, 0, sizeof(args));
578 args.flags |= BTRFS_BALANCE_RESUME;
580 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
585 if (e == ECANCELED) {
586 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
587 fprintf(stderr, "balance paused by user\n");
588 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
589 fprintf(stderr, "balance canceled by user\n");
590 } else if (e == ENOTCONN || e == EINPROGRESS) {
591 fprintf(stderr, "ERROR: balance resume on '%s' "
592 "failed - %s\n", path,
593 (e == ENOTCONN) ? "Not in progress" :
598 "ERROR: error during balancing '%s' - %s\n"
599 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
603 printf("Done, had to relocate %llu out of %llu chunks\n",
604 (unsigned long long)args.stat.completed,
605 (unsigned long long)args.stat.considered);
611 static const char * const cmd_balance_status_usage[] = {
612 "btrfs [filesystem] balance status [-v] <path>",
613 "Show status of running or paused balance",
619 /* Checks the status of the balance if any
621 * 2 : Error failed to know if there is any pending balance
622 * 1 : Successful to know status of a pending balance
623 * 0 : When there is no pending balance or completed
625 static int cmd_balance_status(int argc, char **argv)
627 struct btrfs_ioctl_balance_args args;
637 static struct option longopts[] = {
638 { "verbose", no_argument, NULL, 'v' },
642 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
651 usage(cmd_balance_status_usage);
655 if (check_argc_exact(argc - optind, 1))
656 usage(cmd_balance_status_usage);
660 fd = open_file_or_dir(path);
662 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
666 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
672 printf("No balance found on '%s'\n", path);
675 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
680 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
681 printf("Balance on '%s' is running", path);
682 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
683 printf(", cancel requested\n");
684 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
685 printf(", pause requested\n");
689 printf("Balance on '%s' is paused\n", path);
692 printf("%llu out of about %llu chunks balanced (%llu considered), "
693 "%3.f%% left\n", (unsigned long long)args.stat.completed,
694 (unsigned long long)args.stat.expected,
695 (unsigned long long)args.stat.considered,
696 100 * (1 - (float)args.stat.completed/args.stat.expected));
699 dump_ioctl_balance_args(&args);
704 const struct cmd_group balance_cmd_group = {
705 balance_cmd_group_usage, balance_cmd_group_info, {
706 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
707 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
708 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
709 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
710 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
715 int cmd_balance(int argc, char **argv)
718 /* old 'btrfs filesystem balance <path>' syntax */
719 struct btrfs_ioctl_balance_args args;
721 memset(&args, 0, sizeof(args));
722 args.flags |= BTRFS_BALANCE_TYPE_MASK;
724 return do_balance(argv[1], &args, 1);
727 return handle_command_group(&balance_cmd_group, argc, argv);