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"
37 static const char * const balance_cmd_group_usage[] = {
38 "btrfs balance <command> [options] <path>",
39 "btrfs balance <path>",
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 error("unknown profile: %s", 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);
96 * Parse range that's missing some part that can be implicit:
97 * a..b - exact range, a can be equal to b
98 * a.. - implicitly unbounded maximum (end == (u64)-1)
99 * ..b - implicitly starting at 0
100 * a - invalid; unclear semantics, use parse_u64 instead
102 * Returned values are u64, value validation and interpretation should be done
105 static int parse_range(const char *range, u64 *start, u64 *end)
112 dots = strstr(range, "..");
122 *end = strtoull(rest, &endptr, 10);
130 *start = strtoull(range, &endptr, 10);
131 if (*endptr != 0 && *endptr != '.')
136 error("range %llu..%llu doesn't make sense",
137 (unsigned long long)*start,
138 (unsigned long long)*end);
149 * Parse range and check if start < end
151 static int parse_range_strict(const char *range, u64 *start, u64 *end)
153 if (parse_range(range, start, end) == 0) {
154 if (*start >= *end) {
155 error("range %llu..%llu not allowed",
156 (unsigned long long)*start,
157 (unsigned long long)*end);
167 * Convert 64bit range to 32bit with boundary checks
169 static int range_to_u32(u64 start, u64 end, u32 *start32, u32 *end32)
174 if (end != (u64)-1 && end > (u32)-1)
177 *start32 = (u32)start;
183 __attribute__ ((unused))
184 static int parse_range_u32(const char *range, u32 *start, u32 *end)
189 if (parse_range(range, &tmp_start, &tmp_end))
192 if (range_to_u32(tmp_start, tmp_end, start, end))
198 __attribute__ ((unused))
199 static void print_range(u64 start, u64 end)
202 printf("%llu", (unsigned long long)start);
205 printf("%llu", (unsigned long long)end);
208 __attribute__ ((unused))
209 static void print_range_u32(u32 start, u32 end)
218 static int parse_filters(char *filters, struct btrfs_balance_args *args)
222 char *save_ptr = NULL; /* Satisfy static checkers */
227 for (this_char = strtok_r(filters, ",", &save_ptr);
229 this_char = strtok_r(NULL, ",", &save_ptr)) {
230 if ((value = strchr(this_char, '=')) != NULL)
232 if (!strcmp(this_char, "profiles")) {
233 if (!value || !*value) {
234 error("the profiles filter requires an argument");
237 if (parse_profiles(value, &args->profiles)) {
238 error("invalid profiles argument");
241 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
242 } else if (!strcmp(this_char, "usage")) {
243 if (!value || !*value) {
244 error("the usage filter requires an argument");
247 if (parse_u64(value, &args->usage)) {
248 if (parse_range_u32(value, &args->usage_min,
250 error("invalid usage argument: %s",
254 if (args->usage_max > 100) {
255 error("invalid usage argument: %s",
258 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
259 args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
261 if (args->usage > 100) {
262 error("invalid usage argument: %s",
266 args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
267 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
269 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
270 } else if (!strcmp(this_char, "devid")) {
271 if (!value || !*value) {
272 error("the devid filter requires an argument");
275 if (parse_u64(value, &args->devid) || args->devid == 0) {
276 error("invalid devid argument: %s", value);
279 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
280 } else if (!strcmp(this_char, "drange")) {
281 if (!value || !*value) {
282 error("the drange filter requires an argument");
285 if (parse_range_strict(value, &args->pstart, &args->pend)) {
286 error("invalid drange argument");
289 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
290 } else if (!strcmp(this_char, "vrange")) {
291 if (!value || !*value) {
292 error("the vrange filter requires an argument");
295 if (parse_range_strict(value, &args->vstart, &args->vend)) {
296 error("invalid vrange argument");
299 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
300 } else if (!strcmp(this_char, "convert")) {
301 if (!value || !*value) {
302 error("the convert option requires an argument");
305 if (parse_one_profile(value, &args->target)) {
306 error("invalid convert argument");
309 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
310 } else if (!strcmp(this_char, "soft")) {
311 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
312 } else if (!strcmp(this_char, "limit")) {
313 if (!value || !*value) {
314 error("the limit filter requires an argument");
317 if (parse_u64(value, &args->limit)) {
318 if (parse_range_u32(value, &args->limit_min,
320 error("Invalid limit argument: %s",
324 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
325 args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
327 args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
328 args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
330 } else if (!strcmp(this_char, "stripes")) {
331 if (!value || !*value) {
332 error("the stripes filter requires an argument");
335 if (parse_range_u32(value, &args->stripes_min,
336 &args->stripes_max)) {
337 error("invalid stripes argument");
340 args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
342 error("unrecognized balance option: %s", this_char);
350 static void dump_balance_args(struct btrfs_balance_args *args)
352 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
353 printf("converting, target=%llu, soft is %s",
354 (unsigned long long)args->target,
355 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
360 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
361 printf(", profiles=%llu", (unsigned long long)args->profiles);
362 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
363 printf(", usage=%llu", (unsigned long long)args->usage);
364 if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
366 print_range_u32(args->usage_min, args->usage_max);
368 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
369 printf(", devid=%llu", (unsigned long long)args->devid);
370 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
371 printf(", drange=%llu..%llu",
372 (unsigned long long)args->pstart,
373 (unsigned long long)args->pend);
374 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
375 printf(", vrange=%llu..%llu",
376 (unsigned long long)args->vstart,
377 (unsigned long long)args->vend);
378 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
379 printf(", limit=%llu", (unsigned long long)args->limit);
380 if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
382 print_range_u32(args->limit_min, args->limit_max);
384 if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
385 printf(", stripes=");
386 print_range_u32(args->stripes_min, args->stripes_max);
392 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
394 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
395 (unsigned long long)args->flags, (unsigned long long)args->state,
396 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
397 if (args->flags & BTRFS_BALANCE_DATA) {
398 printf(" DATA (flags 0x%llx): ",
399 (unsigned long long)args->data.flags);
400 dump_balance_args(&args->data);
402 if (args->flags & BTRFS_BALANCE_METADATA) {
403 printf(" METADATA (flags 0x%llx): ",
404 (unsigned long long)args->meta.flags);
405 dump_balance_args(&args->meta);
407 if (args->flags & BTRFS_BALANCE_SYSTEM) {
408 printf(" SYSTEM (flags 0x%llx): ",
409 (unsigned long long)args->sys.flags);
410 dump_balance_args(&args->sys);
414 static int do_balance_v1(int fd)
416 struct btrfs_ioctl_vol_args args;
419 memset(&args, 0, sizeof(args));
420 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
425 BALANCE_START_FILTERS = 1 << 0,
426 BALANCE_START_NOWARN = 1 << 1
429 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
434 DIR *dirstream = NULL;
436 fd = btrfs_open_dir(path, &dirstream, 1);
440 if (!(flags & BALANCE_START_FILTERS) && !(flags & BALANCE_START_NOWARN)) {
443 printf("WARNING:\n\n");
444 printf("\tFull balance without filters requested. This operation is very\n");
445 printf("\tintense and takes potentially very long. It is recommended to\n");
446 printf("\tuse the balance filters to narrow down the scope of balance.\n");
447 printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
448 printf("\twarning. The operation will start in %d seconds.\n", delay);
449 printf("\tUse Ctrl-C to stop it.\n");
451 printf("%2d", delay--);
455 printf("\nStarting balance without any filters.\n");
458 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
461 * older kernels don't have the new balance ioctl, try the
462 * old one. But, the old one doesn't know any filters, so
463 * don't fall back if they tried to use the fancy new things
465 if (errno == ENOTTY && !(flags & BALANCE_START_FILTERS)) {
466 ret = do_balance_v1(fd);
471 if (errno == 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': %m", 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",
514 " run the balance as a background process",
518 static int cmd_balance_start(int argc, char **argv)
520 struct btrfs_ioctl_balance_args args;
521 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
526 unsigned start_flags = 0;
529 memset(&args, 0, sizeof(args));
532 enum { GETOPT_VAL_FULL_BALANCE = 256,
533 GETOPT_VAL_BACKGROUND = 257 };
534 static const struct option longopts[] = {
535 { "data", optional_argument, NULL, 'd'},
536 { "metadata", optional_argument, NULL, 'm' },
537 { "system", optional_argument, NULL, 's' },
538 { "force", no_argument, NULL, 'f' },
539 { "verbose", no_argument, NULL, 'v' },
540 { "full-balance", no_argument, NULL,
541 GETOPT_VAL_FULL_BALANCE },
542 { "background", no_argument, NULL,
543 GETOPT_VAL_BACKGROUND },
544 { "bg", no_argument, NULL, GETOPT_VAL_BACKGROUND },
548 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
554 start_flags |= BALANCE_START_FILTERS;
555 args.flags |= BTRFS_BALANCE_DATA;
557 if (parse_filters(optarg, &args.data))
561 start_flags |= BALANCE_START_FILTERS;
562 args.flags |= BTRFS_BALANCE_SYSTEM;
564 if (parse_filters(optarg, &args.sys))
568 start_flags |= BALANCE_START_FILTERS;
569 args.flags |= BTRFS_BALANCE_METADATA;
571 if (parse_filters(optarg, &args.meta))
580 case GETOPT_VAL_FULL_BALANCE:
581 start_flags |= BALANCE_START_NOWARN;
583 case GETOPT_VAL_BACKGROUND:
587 usage(cmd_balance_start_usage);
591 if (check_argc_exact(argc - optind, 1))
592 usage(cmd_balance_start_usage);
595 * allow -s only under --force, otherwise do with system chunks
596 * the same thing we were ordered to do with meta chunks
598 if (args.flags & BTRFS_BALANCE_SYSTEM) {
601 "Refusing to explicitly operate on system chunks.\n"
602 "Pass --force if you really want to do that.");
605 } else if (args.flags & BTRFS_BALANCE_METADATA) {
606 args.flags |= BTRFS_BALANCE_SYSTEM;
607 memcpy(&args.sys, &args.meta,
608 sizeof(struct btrfs_balance_args));
611 if (!(start_flags & BALANCE_START_FILTERS)) {
612 /* relocate everything - no filters */
613 args.flags |= BTRFS_BALANCE_TYPE_MASK;
616 /* drange makes sense only when devid is set */
617 for (i = 0; ptrs[i]; i++) {
618 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
619 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
620 error("drange filter must be used with devid filter");
625 /* soft makes sense only when convert for corresponding type is set */
626 for (i = 0; ptrs[i]; i++) {
627 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
628 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
629 error("'soft' option can be used only when converting profiles");
635 args.flags |= BTRFS_BALANCE_FORCE;
637 dump_ioctl_balance_args(&args);
641 error("unable to fork to run balance in background");
648 "unable to fork to run balance in background");
652 * Read the return value to silence compiler
653 * warning. Change to / should succeed and
654 * we're not in a security-sensitive context.
660 open("/dev/null", O_RDONLY);
661 open("/dev/null", O_WRONLY);
662 open("/dev/null", O_WRONLY);
673 return do_balance(argv[optind], &args, start_flags);
676 static const char * const cmd_balance_pause_usage[] = {
677 "btrfs balance pause <path>",
678 "Pause running balance",
682 static int cmd_balance_pause(int argc, char **argv)
687 DIR *dirstream = NULL;
689 clean_args_no_options(argc, argv, cmd_balance_pause_usage);
691 if (check_argc_exact(argc - optind, 1))
692 usage(cmd_balance_pause_usage);
696 fd = btrfs_open_dir(path, &dirstream, 1);
700 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
702 error("balance pause on '%s' failed: %s", path,
703 (errno == ENOTCONN) ? "Not running" : strerror(errno));
704 if (errno == ENOTCONN)
710 close_file_or_dir(fd, dirstream);
714 static const char * const cmd_balance_cancel_usage[] = {
715 "btrfs balance cancel <path>",
716 "Cancel running or paused balance",
720 static int cmd_balance_cancel(int argc, char **argv)
725 DIR *dirstream = NULL;
727 clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
729 if (check_argc_exact(argc - optind, 1))
730 usage(cmd_balance_cancel_usage);
734 fd = btrfs_open_dir(path, &dirstream, 1);
738 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
740 error("balance cancel on '%s' failed: %s", path,
741 (errno == ENOTCONN) ? "Not in progress" : strerror(errno));
742 if (errno == ENOTCONN)
748 close_file_or_dir(fd, dirstream);
752 static const char * const cmd_balance_resume_usage[] = {
753 "btrfs balance resume <path>",
754 "Resume interrupted balance",
758 static int cmd_balance_resume(int argc, char **argv)
760 struct btrfs_ioctl_balance_args args;
762 DIR *dirstream = NULL;
766 clean_args_no_options(argc, argv, cmd_balance_resume_usage);
768 if (check_argc_exact(argc - optind, 1))
769 usage(cmd_balance_resume_usage);
773 fd = btrfs_open_dir(path, &dirstream, 1);
777 memset(&args, 0, sizeof(args));
778 args.flags |= BTRFS_BALANCE_RESUME;
780 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
782 if (errno == ECANCELED) {
783 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
784 fprintf(stderr, "balance paused by user\n");
785 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
786 fprintf(stderr, "balance canceled by user\n");
787 } else if (errno == ENOTCONN || errno == EINPROGRESS) {
788 error("balance resume on '%s' failed: %s", path,
789 (errno == ENOTCONN) ? "Not in progress" :
791 if (errno == ENOTCONN)
796 error("error during balancing '%s': %m\n"
797 "There may be more info in syslog - try dmesg | tail",
802 printf("Done, had to relocate %llu out of %llu chunks\n",
803 (unsigned long long)args.stat.completed,
804 (unsigned long long)args.stat.considered);
807 close_file_or_dir(fd, dirstream);
811 static const char * const cmd_balance_status_usage[] = {
812 "btrfs balance status [-v] <path>",
813 "Show status of running or paused balance",
819 /* Checks the status of the balance if any
821 * 2 : Error failed to know if there is any pending balance
822 * 1 : Successful to know status of a pending balance
823 * 0 : When there is no pending balance or completed
825 static int cmd_balance_status(int argc, char **argv)
827 struct btrfs_ioctl_balance_args args;
829 DIR *dirstream = NULL;
836 static const struct option longopts[] = {
837 { "verbose", no_argument, NULL, 'v' },
841 opt = getopt_long(argc, argv, "v", longopts, NULL);
850 usage(cmd_balance_status_usage);
854 if (check_argc_exact(argc - optind, 1))
855 usage(cmd_balance_status_usage);
859 fd = btrfs_open_dir(path, &dirstream, 1);
863 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
865 if (errno == ENOTCONN) {
866 printf("No balance found on '%s'\n", path);
870 error("balance status on '%s' failed: %m", path);
875 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
876 printf("Balance on '%s' is running", path);
877 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
878 printf(", cancel requested\n");
879 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
880 printf(", pause requested\n");
884 printf("Balance on '%s' is paused\n", path);
887 printf("%llu out of about %llu chunks balanced (%llu considered), "
888 "%3.f%% left\n", (unsigned long long)args.stat.completed,
889 (unsigned long long)args.stat.expected,
890 (unsigned long long)args.stat.considered,
891 100 * (1 - (float)args.stat.completed/args.stat.expected));
894 dump_ioctl_balance_args(&args);
898 close_file_or_dir(fd, dirstream);
902 static int cmd_balance_full(int argc, char **argv)
904 struct btrfs_ioctl_balance_args args;
906 memset(&args, 0, sizeof(args));
907 args.flags |= BTRFS_BALANCE_TYPE_MASK;
909 return do_balance(argv[1], &args, BALANCE_START_NOWARN);
912 static const char balance_cmd_group_info[] =
913 "balance data across devices, or change block groups using filters";
915 const struct cmd_group balance_cmd_group = {
916 balance_cmd_group_usage, balance_cmd_group_info, {
917 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
918 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
919 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
920 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
921 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
922 { "--full-balance", cmd_balance_full, NULL, NULL, 1 },
927 int cmd_balance(int argc, char **argv)
929 if (argc == 2 && strcmp("start", argv[1]) != 0) {
930 /* old 'btrfs filesystem balance <path>' syntax */
931 struct btrfs_ioctl_balance_args args;
933 memset(&args, 0, sizeof(args));
934 args.flags |= BTRFS_BALANCE_TYPE_MASK;
936 return do_balance(argv[1], &args, 0);
939 return handle_command_group(&balance_cmd_group, argc, argv);