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 error("unknown profile: %s", 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);
92 * Parse range that's missing some part that can be implicit:
93 * a..b - exact range, a can be equal to b
94 * a.. - implicitly unbounded maximum (end == (u64)-1)
95 * ..b - implicitly starting at 0
96 * a - invalid; unclear semantics, use parse_u64 instead
98 * Returned values are u64, value validation and interpretation should be done
101 static int parse_range(const char *range, u64 *start, u64 *end)
108 dots = strstr(range, "..");
118 *end = strtoull(rest, &endptr, 10);
126 *start = strtoull(range, &endptr, 10);
127 if (*endptr != 0 && *endptr != '.')
132 error("range %llu..%llu doesn't make sense",
133 (unsigned long long)*start,
134 (unsigned long long)*end);
145 * Parse range and check if start < end
147 static int parse_range_strict(const char *range, u64 *start, u64 *end)
149 if (parse_range(range, start, end) == 0) {
150 if (*start >= *end) {
151 error("range %llu..%llu not allowed",
152 (unsigned long long)*start,
153 (unsigned long long)*end);
163 * Convert 64bit range to 32bit with boundary checks
165 static int range_to_u32(u64 start, u64 end, u32 *start32, u32 *end32)
170 if (end != (u64)-1 && end > (u32)-1)
173 *start32 = (u32)start;
179 __attribute__ ((unused))
180 static int parse_range_u32(const char *range, u32 *start, u32 *end)
185 if (parse_range(range, &tmp_start, &tmp_end))
188 if (range_to_u32(tmp_start, tmp_end, start, end))
194 __attribute__ ((unused))
195 static void print_range(u64 start, u64 end)
198 printf("%llu", (unsigned long long)start);
201 printf("%llu", (unsigned long long)end);
204 __attribute__ ((unused))
205 static void print_range_u32(u32 start, u32 end)
214 static int parse_filters(char *filters, struct btrfs_balance_args *args)
218 char *save_ptr = NULL; /* Satisfy static checkers */
223 for (this_char = strtok_r(filters, ",", &save_ptr);
225 this_char = strtok_r(NULL, ",", &save_ptr)) {
226 if ((value = strchr(this_char, '=')) != NULL)
228 if (!strcmp(this_char, "profiles")) {
229 if (!value || !*value) {
230 error("the profiles filter requires an argument");
233 if (parse_profiles(value, &args->profiles)) {
234 error("invalid profiles argument");
237 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
238 } else if (!strcmp(this_char, "usage")) {
239 if (!value || !*value) {
240 error("the usage filter requires an argument");
243 if (parse_u64(value, &args->usage)) {
244 if (parse_range_u32(value, &args->usage_min,
246 error("invalid usage argument: %s",
250 if (args->usage_max > 100) {
251 error("invalid usage argument: %s",
254 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
255 args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
257 if (args->usage > 100) {
258 error("invalid usage argument: %s",
262 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
263 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
265 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
266 } else if (!strcmp(this_char, "devid")) {
267 if (!value || !*value) {
268 error("the devid filter requires an argument");
271 if (parse_u64(value, &args->devid) || args->devid == 0) {
272 error("invalid devid argument: %s", value);
275 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
276 } else if (!strcmp(this_char, "drange")) {
277 if (!value || !*value) {
278 error("the drange filter requires an argument");
281 if (parse_range_strict(value, &args->pstart, &args->pend)) {
282 error("invalid drange argument");
285 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
286 } else if (!strcmp(this_char, "vrange")) {
287 if (!value || !*value) {
288 error("the vrange filter requires an argument");
291 if (parse_range_strict(value, &args->vstart, &args->vend)) {
292 error("invalid vrange argument");
295 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
296 } else if (!strcmp(this_char, "convert")) {
297 if (!value || !*value) {
298 error("the convert option requires an argument");
301 if (parse_one_profile(value, &args->target)) {
302 error("invalid convert argument");
305 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
306 } else if (!strcmp(this_char, "soft")) {
307 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
308 } else if (!strcmp(this_char, "limit")) {
309 if (!value || !*value) {
310 error("the limit filter requires an argument");
313 if (parse_u64(value, &args->limit)) {
314 if (parse_range_u32(value, &args->limit_min,
316 error("Invalid limit argument: %s",
320 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
321 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
323 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
324 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
326 } else if (!strcmp(this_char, "stripes")) {
327 if (!value || !*value) {
328 error("the stripes filter requires an argument");
331 if (parse_range_u32(value, &args->stripes_min,
332 &args->stripes_max)) {
333 error("invalid stripes argument");
336 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
338 error("unrecognized balance option: %s", this_char);
346 static void dump_balance_args(struct btrfs_balance_args *args)
348 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
349 printf("converting, target=%llu, soft is %s",
350 (unsigned long long)args->target,
351 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
356 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
357 printf(", profiles=%llu", (unsigned long long)args->profiles);
358 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
359 printf(", usage=%llu", (unsigned long long)args->usage);
360 if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
362 print_range_u32(args->usage_min, args->usage_max);
364 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
365 printf(", devid=%llu", (unsigned long long)args->devid);
366 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
367 printf(", drange=%llu..%llu",
368 (unsigned long long)args->pstart,
369 (unsigned long long)args->pend);
370 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
371 printf(", vrange=%llu..%llu",
372 (unsigned long long)args->vstart,
373 (unsigned long long)args->vend);
374 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
375 printf(", limit=%llu", (unsigned long long)args->limit);
376 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
378 print_range_u32(args->limit_min, args->limit_max);
380 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
381 printf(", stripes=");
382 print_range_u32(args->stripes_min, args->stripes_max);
388 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
390 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
391 (unsigned long long)args->flags, (unsigned long long)args->state,
392 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
393 if (args->flags & BTRFS_BALANCE_DATA) {
394 printf(" DATA (flags 0x%llx): ",
395 (unsigned long long)args->data.flags);
396 dump_balance_args(&args->data);
398 if (args->flags & BTRFS_BALANCE_METADATA) {
399 printf(" METADATA (flags 0x%llx): ",
400 (unsigned long long)args->meta.flags);
401 dump_balance_args(&args->meta);
403 if (args->flags & BTRFS_BALANCE_SYSTEM) {
404 printf(" SYSTEM (flags 0x%llx): ",
405 (unsigned long long)args->sys.flags);
406 dump_balance_args(&args->sys);
410 static int do_balance_v1(int fd)
412 struct btrfs_ioctl_vol_args args;
415 memset(&args, 0, sizeof(args));
416 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
421 BALANCE_START_FILTERS = 1 << 0,
422 BALANCE_START_NOWARN = 1 << 1
425 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
431 DIR *dirstream = NULL;
433 fd = btrfs_open_dir(path, &dirstream, 1);
437 if (!(flags & BALANCE_START_FILTERS) && !(flags & BALANCE_START_NOWARN)) {
440 printf("WARNING:\n\n");
441 printf("\tFull balance without filters requested. This operation is very\n");
442 printf("\tintense and takes potentially very long. It is recommended to\n");
443 printf("\tuse the balance filters to narrow down the balanced data.\n");
444 printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
445 printf("\twarning. The operation will start in %d seconds.\n", delay);
446 printf("\tUse Ctrl-C to stop it.\n");
448 printf("%2d", delay--);
452 printf("\nStarting balance without any filters.\n");
455 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 (e == ENOTTY && !(flags & BALANCE_START_FILTERS)) {
465 ret = do_balance_v1(fd);
471 if (e == ECANCELED) {
472 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
473 fprintf(stderr, "balance paused by user\n");
474 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
475 fprintf(stderr, "balance canceled by user\n");
478 error("error during balancing '%s': %s", path, strerror(e));
479 if (e != 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",
516 static int cmd_balance_start(int argc, char **argv)
518 struct btrfs_ioctl_balance_args args;
519 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
523 unsigned start_flags = 0;
526 memset(&args, 0, sizeof(args));
530 enum { GETOPT_VAL_FULL_BALANCE = 256 };
531 static const struct option longopts[] = {
532 { "data", optional_argument, NULL, 'd'},
533 { "metadata", optional_argument, NULL, 'm' },
534 { "system", optional_argument, NULL, 's' },
535 { "force", no_argument, NULL, 'f' },
536 { "verbose", no_argument, NULL, 'v' },
537 { "full-balance", no_argument, NULL,
538 GETOPT_VAL_FULL_BALANCE },
542 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
548 start_flags |= BALANCE_START_FILTERS;
549 args.flags |= BTRFS_BALANCE_DATA;
551 if (parse_filters(optarg, &args.data))
555 start_flags |= BALANCE_START_FILTERS;
556 args.flags |= BTRFS_BALANCE_SYSTEM;
558 if (parse_filters(optarg, &args.sys))
562 start_flags |= BALANCE_START_FILTERS;
563 args.flags |= BTRFS_BALANCE_METADATA;
565 if (parse_filters(optarg, &args.meta))
574 case GETOPT_VAL_FULL_BALANCE:
575 start_flags |= BALANCE_START_NOWARN;
578 usage(cmd_balance_start_usage);
582 if (check_argc_exact(argc - optind, 1))
583 usage(cmd_balance_start_usage);
586 * allow -s only under --force, otherwise do with system chunks
587 * the same thing we were ordered to do with meta chunks
589 if (args.flags & BTRFS_BALANCE_SYSTEM) {
592 "Refusing to explicitly operate on system chunks.\n"
593 "Pass --force if you really want to do that.");
596 } else if (args.flags & BTRFS_BALANCE_METADATA) {
597 args.flags |= BTRFS_BALANCE_SYSTEM;
598 memcpy(&args.sys, &args.meta,
599 sizeof(struct btrfs_balance_args));
602 if (!(start_flags & BALANCE_START_FILTERS)) {
603 /* relocate everything - no filters */
604 args.flags |= BTRFS_BALANCE_TYPE_MASK;
607 /* drange makes sense only when devid is set */
608 for (i = 0; ptrs[i]; i++) {
609 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
610 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
611 error("drange filter must be used with devid filter");
616 /* soft makes sense only when convert for corresponding type is set */
617 for (i = 0; ptrs[i]; i++) {
618 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
619 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
620 error("'soft' option can be used only when converting profiles");
626 args.flags |= BTRFS_BALANCE_FORCE;
628 dump_ioctl_balance_args(&args);
630 return do_balance(argv[optind], &args, start_flags);
633 static const char * const cmd_balance_pause_usage[] = {
634 "btrfs balance pause <path>",
635 "Pause running balance",
639 static int cmd_balance_pause(int argc, char **argv)
645 DIR *dirstream = NULL;
647 clean_args_no_options(argc, argv, cmd_balance_pause_usage);
649 if (check_argc_exact(argc - optind, 1))
650 usage(cmd_balance_pause_usage);
654 fd = btrfs_open_dir(path, &dirstream, 1);
658 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
660 close_file_or_dir(fd, dirstream);
663 error("balance pause on '%s' failed: %s", path,
664 (e == ENOTCONN) ? "Not running" : strerror(e));
674 static const char * const cmd_balance_cancel_usage[] = {
675 "btrfs balance cancel <path>",
676 "Cancel running or paused balance",
680 static int cmd_balance_cancel(int argc, char **argv)
686 DIR *dirstream = NULL;
688 clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
690 if (check_argc_exact(argc - optind, 1))
691 usage(cmd_balance_cancel_usage);
695 fd = btrfs_open_dir(path, &dirstream, 1);
699 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
701 close_file_or_dir(fd, dirstream);
704 error("balance cancel on '%s' failed: %s", path,
705 (e == ENOTCONN) ? "Not in progress" : strerror(e));
715 static const char * const cmd_balance_resume_usage[] = {
716 "btrfs balance resume <path>",
717 "Resume interrupted balance",
721 static int cmd_balance_resume(int argc, char **argv)
723 struct btrfs_ioctl_balance_args args;
725 DIR *dirstream = NULL;
730 clean_args_no_options(argc, argv, cmd_balance_resume_usage);
732 if (check_argc_exact(argc - optind, 1))
733 usage(cmd_balance_resume_usage);
737 fd = btrfs_open_dir(path, &dirstream, 1);
741 memset(&args, 0, sizeof(args));
742 args.flags |= BTRFS_BALANCE_RESUME;
744 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
746 close_file_or_dir(fd, dirstream);
749 if (e == ECANCELED) {
750 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
751 fprintf(stderr, "balance paused by user\n");
752 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
753 fprintf(stderr, "balance canceled by user\n");
754 } else if (e == ENOTCONN || e == EINPROGRESS) {
755 error("balance resume on '%s' failed: %s", path,
756 (e == ENOTCONN) ? "Not in progress" :
763 error("error during balancing '%s': %s\n"
764 "There may be more info in syslog - try dmesg | tail",
769 printf("Done, had to relocate %llu out of %llu chunks\n",
770 (unsigned long long)args.stat.completed,
771 (unsigned long long)args.stat.considered);
777 static const char * const cmd_balance_status_usage[] = {
778 "btrfs balance status [-v] <path>",
779 "Show status of running or paused balance",
785 /* Checks the status of the balance if any
787 * 2 : Error failed to know if there is any pending balance
788 * 1 : Successful to know status of a pending balance
789 * 0 : When there is no pending balance or completed
791 static int cmd_balance_status(int argc, char **argv)
793 struct btrfs_ioctl_balance_args args;
795 DIR *dirstream = NULL;
804 static const struct option longopts[] = {
805 { "verbose", no_argument, NULL, 'v' },
809 opt = getopt_long(argc, argv, "v", longopts, NULL);
818 usage(cmd_balance_status_usage);
822 if (check_argc_exact(argc - optind, 1))
823 usage(cmd_balance_status_usage);
827 fd = btrfs_open_dir(path, &dirstream, 1);
831 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
833 close_file_or_dir(fd, dirstream);
837 printf("No balance found on '%s'\n", path);
840 error("balance status on '%s' failed: %s", path, strerror(e));
844 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
845 printf("Balance on '%s' is running", path);
846 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
847 printf(", cancel requested\n");
848 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
849 printf(", pause requested\n");
853 printf("Balance on '%s' is paused\n", path);
856 printf("%llu out of about %llu chunks balanced (%llu considered), "
857 "%3.f%% left\n", (unsigned long long)args.stat.completed,
858 (unsigned long long)args.stat.expected,
859 (unsigned long long)args.stat.considered,
860 100 * (1 - (float)args.stat.completed/args.stat.expected));
863 dump_ioctl_balance_args(&args);
868 static int cmd_balance_full(int argc, char **argv)
870 struct btrfs_ioctl_balance_args args;
872 memset(&args, 0, sizeof(args));
873 args.flags |= BTRFS_BALANCE_TYPE_MASK;
875 return do_balance(argv[1], &args, BALANCE_START_NOWARN);
878 static const char balance_cmd_group_info[] =
879 "balance data across devices, or change block groups using filters";
881 const struct cmd_group balance_cmd_group = {
882 balance_cmd_group_usage, balance_cmd_group_info, {
883 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
884 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
885 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
886 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
887 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
888 { "--full-balance", cmd_balance_full, NULL, NULL, 1 },
893 int cmd_balance(int argc, char **argv)
895 if (argc == 2 && strcmp("start", argv[1]) != 0) {
896 /* old 'btrfs filesystem balance <path>' syntax */
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, 0);
905 return handle_command_group(&balance_cmd_group, argc, argv);