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 checkso
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);
420 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
426 DIR *dirstream = NULL;
428 fd = btrfs_open_dir(path, &dirstream, 1);
432 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
437 * older kernels don't have the new balance ioctl, try the
438 * old one. But, the old one doesn't know any filters, so
439 * don't fall back if they tried to use the fancy new things
441 if (e == ENOTTY && nofilters) {
442 ret = do_balance_v1(fd);
448 if (e == ECANCELED) {
449 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
450 fprintf(stderr, "balance paused by user\n");
451 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
452 fprintf(stderr, "balance canceled by user\n");
455 error("error during balancing '%s': %s", path, strerror(e));
456 if (e != EINPROGRESS)
458 "There may be more info in syslog - try dmesg | tail\n");
462 printf("Done, had to relocate %llu out of %llu chunks\n",
463 (unsigned long long)args->stat.completed,
464 (unsigned long long)args->stat.considered);
469 close_file_or_dir(fd, dirstream);
473 static const char * const cmd_balance_start_usage[] = {
474 "btrfs balance start [options] <path>",
475 "Balance chunks across the devices",
476 "Balance and/or convert (change allocation profile of) chunks that",
477 "passed all filters in a comma-separated list of filters for a",
478 "particular chunk type. If filter list is not given balance all",
479 "chunks of that type. In case none of the -d, -m or -s options is",
480 "given balance all chunks in a filesystem.",
482 "-d[filters] act on data chunks",
483 "-m[filters] act on metadata chunks",
484 "-s[filters] act on system chunks (only under -f)",
486 "-f force reducing of metadata integrity",
490 static int cmd_balance_start(int argc, char **argv)
492 struct btrfs_ioctl_balance_args args;
493 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
500 memset(&args, 0, sizeof(args));
504 static const struct option longopts[] = {
505 { "data", optional_argument, NULL, 'd'},
506 { "metadata", optional_argument, NULL, 'm' },
507 { "system", optional_argument, NULL, 's' },
508 { "force", no_argument, NULL, 'f' },
509 { "verbose", no_argument, NULL, 'v' },
513 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
520 args.flags |= BTRFS_BALANCE_DATA;
522 if (parse_filters(optarg, &args.data))
527 args.flags |= BTRFS_BALANCE_SYSTEM;
529 if (parse_filters(optarg, &args.sys))
534 args.flags |= BTRFS_BALANCE_METADATA;
536 if (parse_filters(optarg, &args.meta))
546 usage(cmd_balance_start_usage);
550 if (check_argc_exact(argc - optind, 1))
551 usage(cmd_balance_start_usage);
554 * allow -s only under --force, otherwise do with system chunks
555 * the same thing we were ordered to do with meta chunks
557 if (args.flags & BTRFS_BALANCE_SYSTEM) {
560 "Refusing to explicitly operate on system chunks.\n"
561 "Pass --force if you really want to do that.");
564 } else if (args.flags & BTRFS_BALANCE_METADATA) {
565 args.flags |= BTRFS_BALANCE_SYSTEM;
566 memcpy(&args.sys, &args.meta,
567 sizeof(struct btrfs_balance_args));
571 /* relocate everything - no filters */
572 args.flags |= BTRFS_BALANCE_TYPE_MASK;
575 /* drange makes sense only when devid is set */
576 for (i = 0; ptrs[i]; i++) {
577 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
578 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
579 error("drange filter must be used with devid filter");
584 /* soft makes sense only when convert for corresponding type is set */
585 for (i = 0; ptrs[i]; i++) {
586 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
587 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
588 error("'soft' option can be used only when converting profiles");
594 args.flags |= BTRFS_BALANCE_FORCE;
596 dump_ioctl_balance_args(&args);
598 return do_balance(argv[optind], &args, nofilters);
601 static const char * const cmd_balance_pause_usage[] = {
602 "btrfs balance pause <path>",
603 "Pause running balance",
607 static int cmd_balance_pause(int argc, char **argv)
613 DIR *dirstream = NULL;
615 clean_args_no_options(argc, argv, cmd_balance_pause_usage);
617 if (check_argc_exact(argc - optind, 1))
618 usage(cmd_balance_pause_usage);
622 fd = btrfs_open_dir(path, &dirstream, 1);
626 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
628 close_file_or_dir(fd, dirstream);
631 error("balance pause on '%s' failed: %s", path,
632 (e == ENOTCONN) ? "Not running" : strerror(e));
642 static const char * const cmd_balance_cancel_usage[] = {
643 "btrfs balance cancel <path>",
644 "Cancel running or paused balance",
648 static int cmd_balance_cancel(int argc, char **argv)
654 DIR *dirstream = NULL;
656 clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
658 if (check_argc_exact(argc - optind, 1))
659 usage(cmd_balance_cancel_usage);
663 fd = btrfs_open_dir(path, &dirstream, 1);
667 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
669 close_file_or_dir(fd, dirstream);
672 error("balance cancel on '%s' failed: %s", path,
673 (e == ENOTCONN) ? "Not in progress" : strerror(e));
683 static const char * const cmd_balance_resume_usage[] = {
684 "btrfs balance resume <path>",
685 "Resume interrupted balance",
689 static int cmd_balance_resume(int argc, char **argv)
691 struct btrfs_ioctl_balance_args args;
693 DIR *dirstream = NULL;
698 clean_args_no_options(argc, argv, cmd_balance_resume_usage);
700 if (check_argc_exact(argc - optind, 1))
701 usage(cmd_balance_resume_usage);
705 fd = btrfs_open_dir(path, &dirstream, 1);
709 memset(&args, 0, sizeof(args));
710 args.flags |= BTRFS_BALANCE_RESUME;
712 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
714 close_file_or_dir(fd, dirstream);
717 if (e == ECANCELED) {
718 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
719 fprintf(stderr, "balance paused by user\n");
720 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
721 fprintf(stderr, "balance canceled by user\n");
722 } else if (e == ENOTCONN || e == EINPROGRESS) {
723 error("balance resume on '%s' failed: %s", path,
724 (e == ENOTCONN) ? "Not in progress" :
731 error("error during balancing '%s': %s\n"
732 "There may be more info in syslog - try dmesg | tail",
737 printf("Done, had to relocate %llu out of %llu chunks\n",
738 (unsigned long long)args.stat.completed,
739 (unsigned long long)args.stat.considered);
745 static const char * const cmd_balance_status_usage[] = {
746 "btrfs balance status [-v] <path>",
747 "Show status of running or paused balance",
753 /* Checks the status of the balance if any
755 * 2 : Error failed to know if there is any pending balance
756 * 1 : Successful to know status of a pending balance
757 * 0 : When there is no pending balance or completed
759 static int cmd_balance_status(int argc, char **argv)
761 struct btrfs_ioctl_balance_args args;
763 DIR *dirstream = NULL;
772 static const struct option longopts[] = {
773 { "verbose", no_argument, NULL, 'v' },
777 opt = getopt_long(argc, argv, "v", longopts, NULL);
786 usage(cmd_balance_status_usage);
790 if (check_argc_exact(argc - optind, 1))
791 usage(cmd_balance_status_usage);
795 fd = btrfs_open_dir(path, &dirstream, 1);
799 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
801 close_file_or_dir(fd, dirstream);
805 printf("No balance found on '%s'\n", path);
808 error("balance status on '%s' failed: %s", path, strerror(e));
812 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
813 printf("Balance on '%s' is running", path);
814 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
815 printf(", cancel requested\n");
816 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
817 printf(", pause requested\n");
821 printf("Balance on '%s' is paused\n", path);
824 printf("%llu out of about %llu chunks balanced (%llu considered), "
825 "%3.f%% left\n", (unsigned long long)args.stat.completed,
826 (unsigned long long)args.stat.expected,
827 (unsigned long long)args.stat.considered,
828 100 * (1 - (float)args.stat.completed/args.stat.expected));
831 dump_ioctl_balance_args(&args);
836 static const char balance_cmd_group_info[] =
837 "balance data accross devices, or change block groups using filters";
839 const struct cmd_group balance_cmd_group = {
840 balance_cmd_group_usage, balance_cmd_group_info, {
841 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
842 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
843 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
844 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
845 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
850 int cmd_balance(int argc, char **argv)
853 /* old 'btrfs filesystem balance <path>' syntax */
854 struct btrfs_ioctl_balance_args args;
856 memset(&args, 0, sizeof(args));
857 args.flags |= BTRFS_BALANCE_TYPE_MASK;
859 return do_balance(argv[1], &args, 1);
862 return handle_command_group(&balance_cmd_group, argc, argv);