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>
23 #include <sys/types.h>
28 #include "kerncompat.h"
36 static const char * const balance_cmd_group_usage[] = {
37 "btrfs balance <command> [options] <path>",
38 "btrfs balance <path>",
42 static int parse_one_profile(const char *profile, u64 *flags)
44 if (!strcmp(profile, "raid0")) {
45 *flags |= BTRFS_BLOCK_GROUP_RAID0;
46 } else if (!strcmp(profile, "raid1")) {
47 *flags |= BTRFS_BLOCK_GROUP_RAID1;
48 } else if (!strcmp(profile, "raid10")) {
49 *flags |= BTRFS_BLOCK_GROUP_RAID10;
50 } else if (!strcmp(profile, "raid5")) {
51 *flags |= BTRFS_BLOCK_GROUP_RAID5;
52 } else if (!strcmp(profile, "raid6")) {
53 *flags |= BTRFS_BLOCK_GROUP_RAID6;
54 } else if (!strcmp(profile, "dup")) {
55 *flags |= BTRFS_BLOCK_GROUP_DUP;
56 } else if (!strcmp(profile, "single")) {
57 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
59 error("unknown profile: %s", profile);
66 static int parse_profiles(char *profiles, u64 *flags)
69 char *save_ptr = NULL; /* Satisfy static checkers */
71 for (this_char = strtok_r(profiles, "|", &save_ptr);
73 this_char = strtok_r(NULL, "|", &save_ptr)) {
74 if (parse_one_profile(this_char, flags))
81 static int parse_u64(const char *str, u64 *result)
86 val = strtoull(str, &endptr, 10);
95 * Parse range that's missing some part that can be implicit:
96 * a..b - exact range, a can be equal to b
97 * a.. - implicitly unbounded maximum (end == (u64)-1)
98 * ..b - implicitly starting at 0
99 * a - invalid; unclear semantics, use parse_u64 instead
101 * Returned values are u64, value validation and interpretation should be done
104 static int parse_range(const char *range, u64 *start, u64 *end)
111 dots = strstr(range, "..");
121 *end = strtoull(rest, &endptr, 10);
129 *start = strtoull(range, &endptr, 10);
130 if (*endptr != 0 && *endptr != '.')
135 error("range %llu..%llu doesn't make sense",
136 (unsigned long long)*start,
137 (unsigned long long)*end);
148 * Parse range and check if start < end
150 static int parse_range_strict(const char *range, u64 *start, u64 *end)
152 if (parse_range(range, start, end) == 0) {
153 if (*start >= *end) {
154 error("range %llu..%llu not allowed",
155 (unsigned long long)*start,
156 (unsigned long long)*end);
166 * Convert 64bit range to 32bit with boundary checks
168 static int range_to_u32(u64 start, u64 end, u32 *start32, u32 *end32)
173 if (end != (u64)-1 && end > (u32)-1)
176 *start32 = (u32)start;
182 __attribute__ ((unused))
183 static int parse_range_u32(const char *range, u32 *start, u32 *end)
188 if (parse_range(range, &tmp_start, &tmp_end))
191 if (range_to_u32(tmp_start, tmp_end, start, end))
197 __attribute__ ((unused))
198 static void print_range(u64 start, u64 end)
201 printf("%llu", (unsigned long long)start);
204 printf("%llu", (unsigned long long)end);
207 __attribute__ ((unused))
208 static void print_range_u32(u32 start, u32 end)
217 static int parse_filters(char *filters, struct btrfs_balance_args *args)
221 char *save_ptr = NULL; /* Satisfy static checkers */
226 for (this_char = strtok_r(filters, ",", &save_ptr);
228 this_char = strtok_r(NULL, ",", &save_ptr)) {
229 if ((value = strchr(this_char, '=')) != NULL)
231 if (!strcmp(this_char, "profiles")) {
232 if (!value || !*value) {
233 error("the profiles filter requires an argument");
236 if (parse_profiles(value, &args->profiles)) {
237 error("invalid profiles argument");
240 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
241 } else if (!strcmp(this_char, "usage")) {
242 if (!value || !*value) {
243 error("the usage filter requires an argument");
246 if (parse_u64(value, &args->usage)) {
247 if (parse_range_u32(value, &args->usage_min,
249 error("invalid usage argument: %s",
253 if (args->usage_max > 100) {
254 error("invalid usage argument: %s",
257 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
258 args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
260 if (args->usage > 100) {
261 error("invalid usage argument: %s",
265 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
266 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
268 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
269 } else if (!strcmp(this_char, "devid")) {
270 if (!value || !*value) {
271 error("the devid filter requires an argument");
274 if (parse_u64(value, &args->devid) || args->devid == 0) {
275 error("invalid devid argument: %s", value);
278 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
279 } else if (!strcmp(this_char, "drange")) {
280 if (!value || !*value) {
281 error("the drange filter requires an argument");
284 if (parse_range_strict(value, &args->pstart, &args->pend)) {
285 error("invalid drange argument");
288 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
289 } else if (!strcmp(this_char, "vrange")) {
290 if (!value || !*value) {
291 error("the vrange filter requires an argument");
294 if (parse_range_strict(value, &args->vstart, &args->vend)) {
295 error("invalid vrange argument");
298 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
299 } else if (!strcmp(this_char, "convert")) {
300 if (!value || !*value) {
301 error("the convert option requires an argument");
304 if (parse_one_profile(value, &args->target)) {
305 error("invalid convert argument");
308 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
309 } else if (!strcmp(this_char, "soft")) {
310 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
311 } else if (!strcmp(this_char, "limit")) {
312 if (!value || !*value) {
313 error("the limit filter requires an argument");
316 if (parse_u64(value, &args->limit)) {
317 if (parse_range_u32(value, &args->limit_min,
319 error("Invalid limit argument: %s",
323 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
324 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
326 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
327 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
329 } else if (!strcmp(this_char, "stripes")) {
330 if (!value || !*value) {
331 error("the stripes filter requires an argument");
334 if (parse_range_u32(value, &args->stripes_min,
335 &args->stripes_max)) {
336 error("invalid stripes argument");
339 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
341 error("unrecognized balance option: %s", this_char);
349 static void dump_balance_args(struct btrfs_balance_args *args)
351 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
352 printf("converting, target=%llu, soft is %s",
353 (unsigned long long)args->target,
354 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
359 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
360 printf(", profiles=%llu", (unsigned long long)args->profiles);
361 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
362 printf(", usage=%llu", (unsigned long long)args->usage);
363 if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
365 print_range_u32(args->usage_min, args->usage_max);
367 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
368 printf(", devid=%llu", (unsigned long long)args->devid);
369 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
370 printf(", drange=%llu..%llu",
371 (unsigned long long)args->pstart,
372 (unsigned long long)args->pend);
373 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
374 printf(", vrange=%llu..%llu",
375 (unsigned long long)args->vstart,
376 (unsigned long long)args->vend);
377 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
378 printf(", limit=%llu", (unsigned long long)args->limit);
379 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
381 print_range_u32(args->limit_min, args->limit_max);
383 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
384 printf(", stripes=");
385 print_range_u32(args->stripes_min, args->stripes_max);
391 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
393 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
394 (unsigned long long)args->flags, (unsigned long long)args->state,
395 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
396 if (args->flags & BTRFS_BALANCE_DATA) {
397 printf(" DATA (flags 0x%llx): ",
398 (unsigned long long)args->data.flags);
399 dump_balance_args(&args->data);
401 if (args->flags & BTRFS_BALANCE_METADATA) {
402 printf(" METADATA (flags 0x%llx): ",
403 (unsigned long long)args->meta.flags);
404 dump_balance_args(&args->meta);
406 if (args->flags & BTRFS_BALANCE_SYSTEM) {
407 printf(" SYSTEM (flags 0x%llx): ",
408 (unsigned long long)args->sys.flags);
409 dump_balance_args(&args->sys);
413 static int do_balance_v1(int fd)
415 struct btrfs_ioctl_vol_args args;
418 memset(&args, 0, sizeof(args));
419 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
424 BALANCE_START_FILTERS = 1 << 0,
425 BALANCE_START_NOWARN = 1 << 1
428 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
433 DIR *dirstream = NULL;
435 fd = btrfs_open_dir(path, &dirstream, 1);
439 if (!(flags & BALANCE_START_FILTERS) && !(flags & BALANCE_START_NOWARN)) {
442 printf("WARNING:\n\n");
443 printf("\tFull balance without filters requested. This operation is very\n");
444 printf("\tintense and takes potentially very long. It is recommended to\n");
445 printf("\tuse the balance filters to narrow down the balanced data.\n");
446 printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
447 printf("\twarning. The operation will start in %d seconds.\n", delay);
448 printf("\tUse Ctrl-C to stop it.\n");
450 printf("%2d", delay--);
454 printf("\nStarting balance without any filters.\n");
457 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
460 * older kernels don't have the new balance ioctl, try the
461 * old one. But, the old one doesn't know any filters, so
462 * don't fall back if they tried to use the fancy new things
464 if (errno == ENOTTY && !(flags & BALANCE_START_FILTERS)) {
465 ret = do_balance_v1(fd);
470 if (errno == ECANCELED) {
471 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
472 fprintf(stderr, "balance paused by user\n");
473 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
474 fprintf(stderr, "balance canceled by user\n");
477 error("error during balancing '%s': %s", path,
479 if (errno != EINPROGRESS)
481 "There may be more info in syslog - try dmesg | tail\n");
485 printf("Done, had to relocate %llu out of %llu chunks\n",
486 (unsigned long long)args->stat.completed,
487 (unsigned long long)args->stat.considered);
492 close_file_or_dir(fd, dirstream);
496 static const char * const cmd_balance_start_usage[] = {
497 "btrfs balance start [options] <path>",
498 "Balance chunks across the devices",
499 "Balance and/or convert (change allocation profile of) chunks that",
500 "passed all filters in a comma-separated list of filters for a",
501 "particular chunk type. If filter list is not given balance all",
502 "chunks of that type. In case none of the -d, -m or -s options is",
503 "given balance all chunks in a filesystem. This is potentially",
504 "long operation and the user is warned before this start, with",
505 "a delay to stop it.",
507 "-d[filters] act on data chunks",
508 "-m[filters] act on metadata chunks",
509 "-s[filters] act on system chunks (only under -f)",
511 "-f force reducing of metadata integrity",
512 "--full-balance do not print warning and do not delay start",
513 "--background run the balance as a background process",
517 static int cmd_balance_start(int argc, char **argv)
519 struct btrfs_ioctl_balance_args args;
520 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
525 unsigned start_flags = 0;
528 memset(&args, 0, sizeof(args));
531 enum { GETOPT_VAL_FULL_BALANCE = 256,
532 GETOPT_VAL_BACKGROUND = 257 };
533 static const struct option longopts[] = {
534 { "data", optional_argument, NULL, 'd'},
535 { "metadata", optional_argument, NULL, 'm' },
536 { "system", optional_argument, NULL, 's' },
537 { "force", no_argument, NULL, 'f' },
538 { "verbose", no_argument, NULL, 'v' },
539 { "full-balance", no_argument, NULL,
540 GETOPT_VAL_FULL_BALANCE },
541 { "background", no_argument, NULL,
542 GETOPT_VAL_BACKGROUND },
546 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
552 start_flags |= BALANCE_START_FILTERS;
553 args.flags |= BTRFS_BALANCE_DATA;
555 if (parse_filters(optarg, &args.data))
559 start_flags |= BALANCE_START_FILTERS;
560 args.flags |= BTRFS_BALANCE_SYSTEM;
562 if (parse_filters(optarg, &args.sys))
566 start_flags |= BALANCE_START_FILTERS;
567 args.flags |= BTRFS_BALANCE_METADATA;
569 if (parse_filters(optarg, &args.meta))
578 case GETOPT_VAL_FULL_BALANCE:
579 start_flags |= BALANCE_START_NOWARN;
581 case GETOPT_VAL_BACKGROUND:
585 usage(cmd_balance_start_usage);
589 if (check_argc_exact(argc - optind, 1))
590 usage(cmd_balance_start_usage);
593 * allow -s only under --force, otherwise do with system chunks
594 * the same thing we were ordered to do with meta chunks
596 if (args.flags & BTRFS_BALANCE_SYSTEM) {
599 "Refusing to explicitly operate on system chunks.\n"
600 "Pass --force if you really want to do that.");
603 } else if (args.flags & BTRFS_BALANCE_METADATA) {
604 args.flags |= BTRFS_BALANCE_SYSTEM;
605 memcpy(&args.sys, &args.meta,
606 sizeof(struct btrfs_balance_args));
609 if (!(start_flags & BALANCE_START_FILTERS)) {
610 /* relocate everything - no filters */
611 args.flags |= BTRFS_BALANCE_TYPE_MASK;
614 /* drange makes sense only when devid is set */
615 for (i = 0; ptrs[i]; i++) {
616 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
617 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
618 error("drange filter must be used with devid filter");
623 /* soft makes sense only when convert for corresponding type is set */
624 for (i = 0; ptrs[i]; i++) {
625 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
626 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
627 error("'soft' option can be used only when converting profiles");
633 args.flags |= BTRFS_BALANCE_FORCE;
635 dump_ioctl_balance_args(&args);
639 error("unable to fork to run balance in background");
646 "unable to fork to run balance in background");
653 open("/dev/null", O_RDONLY);
654 open("/dev/null", O_WRONLY);
655 open("/dev/null", O_WRONLY);
666 return do_balance(argv[optind], &args, start_flags);
669 static const char * const cmd_balance_pause_usage[] = {
670 "btrfs balance pause <path>",
671 "Pause running balance",
675 static int cmd_balance_pause(int argc, char **argv)
680 DIR *dirstream = NULL;
682 clean_args_no_options(argc, argv, cmd_balance_pause_usage);
684 if (check_argc_exact(argc - optind, 1))
685 usage(cmd_balance_pause_usage);
689 fd = btrfs_open_dir(path, &dirstream, 1);
693 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
695 error("balance pause on '%s' failed: %s", path,
696 (errno == ENOTCONN) ? "Not running" : strerror(errno));
697 if (errno == ENOTCONN)
703 close_file_or_dir(fd, dirstream);
707 static const char * const cmd_balance_cancel_usage[] = {
708 "btrfs balance cancel <path>",
709 "Cancel running or paused balance",
713 static int cmd_balance_cancel(int argc, char **argv)
718 DIR *dirstream = NULL;
720 clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
722 if (check_argc_exact(argc - optind, 1))
723 usage(cmd_balance_cancel_usage);
727 fd = btrfs_open_dir(path, &dirstream, 1);
731 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
733 error("balance cancel on '%s' failed: %s", path,
734 (errno == ENOTCONN) ? "Not in progress" : strerror(errno));
735 if (errno == ENOTCONN)
741 close_file_or_dir(fd, dirstream);
745 static const char * const cmd_balance_resume_usage[] = {
746 "btrfs balance resume <path>",
747 "Resume interrupted balance",
751 static int cmd_balance_resume(int argc, char **argv)
753 struct btrfs_ioctl_balance_args args;
755 DIR *dirstream = NULL;
759 clean_args_no_options(argc, argv, cmd_balance_resume_usage);
761 if (check_argc_exact(argc - optind, 1))
762 usage(cmd_balance_resume_usage);
766 fd = btrfs_open_dir(path, &dirstream, 1);
770 memset(&args, 0, sizeof(args));
771 args.flags |= BTRFS_BALANCE_RESUME;
773 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
775 if (errno == ECANCELED) {
776 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
777 fprintf(stderr, "balance paused by user\n");
778 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
779 fprintf(stderr, "balance canceled by user\n");
780 } else if (errno == ENOTCONN || errno == EINPROGRESS) {
781 error("balance resume on '%s' failed: %s", path,
782 (errno == ENOTCONN) ? "Not in progress" :
784 if (errno == ENOTCONN)
789 error("error during balancing '%s': %s\n"
790 "There may be more info in syslog - try dmesg | tail",
791 path, strerror(errno));
795 printf("Done, had to relocate %llu out of %llu chunks\n",
796 (unsigned long long)args.stat.completed,
797 (unsigned long long)args.stat.considered);
800 close_file_or_dir(fd, dirstream);
804 static const char * const cmd_balance_status_usage[] = {
805 "btrfs balance status [-v] <path>",
806 "Show status of running or paused balance",
812 /* Checks the status of the balance if any
814 * 2 : Error failed to know if there is any pending balance
815 * 1 : Successful to know status of a pending balance
816 * 0 : When there is no pending balance or completed
818 static int cmd_balance_status(int argc, char **argv)
820 struct btrfs_ioctl_balance_args args;
822 DIR *dirstream = NULL;
829 static const struct option longopts[] = {
830 { "verbose", no_argument, NULL, 'v' },
834 opt = getopt_long(argc, argv, "v", longopts, NULL);
843 usage(cmd_balance_status_usage);
847 if (check_argc_exact(argc - optind, 1))
848 usage(cmd_balance_status_usage);
852 fd = btrfs_open_dir(path, &dirstream, 1);
856 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
858 if (errno == ENOTCONN) {
859 printf("No balance found on '%s'\n", path);
863 error("balance status on '%s' failed: %s", path, strerror(errno));
868 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
869 printf("Balance on '%s' is running", path);
870 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
871 printf(", cancel requested\n");
872 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
873 printf(", pause requested\n");
877 printf("Balance on '%s' is paused\n", path);
880 printf("%llu out of about %llu chunks balanced (%llu considered), "
881 "%3.f%% left\n", (unsigned long long)args.stat.completed,
882 (unsigned long long)args.stat.expected,
883 (unsigned long long)args.stat.considered,
884 100 * (1 - (float)args.stat.completed/args.stat.expected));
887 dump_ioctl_balance_args(&args);
891 close_file_or_dir(fd, dirstream);
895 static int cmd_balance_full(int argc, char **argv)
897 struct btrfs_ioctl_balance_args args;
899 memset(&args, 0, sizeof(args));
900 args.flags |= BTRFS_BALANCE_TYPE_MASK;
902 return do_balance(argv[1], &args, BALANCE_START_NOWARN);
905 static const char balance_cmd_group_info[] =
906 "balance data across devices, or change block groups using filters";
908 const struct cmd_group balance_cmd_group = {
909 balance_cmd_group_usage, balance_cmd_group_info, {
910 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
911 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
912 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
913 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
914 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
915 { "--full-balance", cmd_balance_full, NULL, NULL, 1 },
920 int cmd_balance(int argc, char **argv)
922 if (argc == 2 && strcmp("start", argv[1]) != 0) {
923 /* old 'btrfs filesystem balance <path>' syntax */
924 struct btrfs_ioctl_balance_args args;
926 memset(&args, 0, sizeof(args));
927 args.flags |= BTRFS_BALANCE_TYPE_MASK;
929 return do_balance(argv[1], &args, 0);
932 return handle_command_group(&balance_cmd_group, argc, argv);