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"
32 static const char balance_cmd_group_usage[] =
33 "btrfs [filesystem] balance [<command>] [options] <path>";
35 static const char balance_cmd_group_info[] =
36 "'btrfs filesystem balance' command is deprecated, please use\n"
37 "'btrfs balance start' command instead.";
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, "dup")) {
48 *flags |= BTRFS_BLOCK_GROUP_DUP;
49 } else if (!strcmp(profile, "single")) {
50 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
52 fprintf(stderr, "Unknown profile '%s'\n", profile);
59 static int parse_profiles(char *profiles, u64 *flags)
64 for (this_char = strtok_r(profiles, "|", &save_ptr);
66 this_char = strtok_r(NULL, "|", &save_ptr)) {
67 if (parse_one_profile(this_char, flags))
74 static int parse_u64(const char *str, u64 *result)
79 val = strtoull(str, &endptr, 10);
87 static int parse_range(const char *range, u64 *start, u64 *end)
91 dots = strstr(range, "..");
93 const char *rest = dots + 2;
102 if (parse_u64(rest, end))
109 if (parse_u64(range, start))
113 if (*start >= *end) {
114 fprintf(stderr, "Range %llu..%llu doesn't make "
115 "sense\n", (unsigned long long)*start,
116 (unsigned long long)*end);
127 static int parse_filters(char *filters, struct btrfs_balance_args *args)
136 for (this_char = strtok_r(filters, ",", &save_ptr);
138 this_char = strtok_r(NULL, ",", &save_ptr)) {
139 if ((value = strchr(this_char, '=')) != NULL)
141 if (!strcmp(this_char, "profiles")) {
142 if (!value || !*value) {
143 fprintf(stderr, "the profiles filter requires "
147 if (parse_profiles(value, &args->profiles)) {
148 fprintf(stderr, "Invalid profiles argument\n");
151 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
152 } else if (!strcmp(this_char, "usage")) {
153 if (!value || !*value) {
154 fprintf(stderr, "the usage filter requires "
158 if (parse_u64(value, &args->usage) ||
159 args->usage < 1 || args->usage > 100) {
160 fprintf(stderr, "Invalid usage argument: %s\n",
164 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
165 } else if (!strcmp(this_char, "devid")) {
166 if (!value || !*value) {
167 fprintf(stderr, "the devid filter requires "
171 if (parse_u64(value, &args->devid) ||
173 fprintf(stderr, "Invalid devid argument: %s\n",
177 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
178 } else if (!strcmp(this_char, "drange")) {
179 if (!value || !*value) {
180 fprintf(stderr, "the drange filter requires "
184 if (parse_range(value, &args->pstart, &args->pend)) {
185 fprintf(stderr, "Invalid drange argument\n");
188 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
189 } else if (!strcmp(this_char, "vrange")) {
190 if (!value || !*value) {
191 fprintf(stderr, "the vrange filter requires "
195 if (parse_range(value, &args->vstart, &args->vend)) {
196 fprintf(stderr, "Invalid vrange argument\n");
199 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
200 } else if (!strcmp(this_char, "convert")) {
201 if (!value || !*value) {
202 fprintf(stderr, "the convert option requires "
206 if (parse_one_profile(value, &args->target)) {
207 fprintf(stderr, "Invalid convert argument\n");
210 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
211 } else if (!strcmp(this_char, "soft")) {
212 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
214 fprintf(stderr, "Unrecognized balance option '%s'\n",
223 static void dump_balance_args(struct btrfs_balance_args *args)
225 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
226 printf("converting, target=%llu, soft is %s",
227 (unsigned long long)args->target,
228 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
233 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
234 printf(", profiles=%llu", (unsigned long long)args->profiles);
235 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
236 printf(", usage=%llu", (unsigned long long)args->usage);
237 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
238 printf(", devid=%llu", (unsigned long long)args->devid);
239 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
240 printf(", drange=%llu..%llu",
241 (unsigned long long)args->pstart,
242 (unsigned long long)args->pend);
243 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
244 printf(", vrange=%llu..%llu",
245 (unsigned long long)args->vstart,
246 (unsigned long long)args->vend);
251 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
253 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
254 (unsigned long long)args->flags, (unsigned long long)args->state,
255 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
256 if (args->flags & BTRFS_BALANCE_DATA) {
257 printf(" DATA (flags 0x%llx): ",
258 (unsigned long long)args->data.flags);
259 dump_balance_args(&args->data);
261 if (args->flags & BTRFS_BALANCE_METADATA) {
262 printf(" METADATA (flags 0x%llx): ",
263 (unsigned long long)args->meta.flags);
264 dump_balance_args(&args->meta);
266 if (args->flags & BTRFS_BALANCE_SYSTEM) {
267 printf(" SYSTEM (flags 0x%llx): ",
268 (unsigned long long)args->sys.flags);
269 dump_balance_args(&args->sys);
273 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args)
279 fd = open_file_or_dir(path);
281 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
285 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
290 if (e == ECANCELED) {
291 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
292 fprintf(stderr, "balance paused by user\n");
293 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
294 fprintf(stderr, "balance canceled by user\n");
296 fprintf(stderr, "ERROR: error during balancing '%s' "
297 "- %s\n", path, strerror(e));
298 if (e != EINPROGRESS)
299 fprintf(stderr, "There may be more info in "
300 "syslog - try dmesg | tail\n");
304 printf("Done, had to relocate %llu out of %llu chunks\n",
305 (unsigned long long)args->stat.completed,
306 (unsigned long long)args->stat.considered);
312 static const char * const cmd_balance_start_usage[] = {
313 "btrfs [filesystem] balance start [options] <path>",
314 "Balance chunks across the devices",
315 "Balance and/or convert (change allocation profile of) chunks that",
316 "passed all filters in a comma-separated list of filters for a",
317 "particular chunk type. If filter list is not given balance all",
318 "chunks of that type. In case none of the -d, -m or -s options is",
319 "given balance all chunks in a filesystem.",
321 "-d[filters] act on data chunks",
322 "-m[filters] act on metadata chunks",
323 "-s[filetrs] act on system chunks (only under -f)",
325 "-f force reducing of metadata integrity",
329 static int cmd_balance_start(int argc, char **argv)
331 struct btrfs_ioctl_balance_args args;
332 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
339 memset(&args, 0, sizeof(args));
344 static struct option longopts[] = {
345 { "data", optional_argument, NULL, 'd'},
346 { "metadata", optional_argument, NULL, 'm' },
347 { "system", optional_argument, NULL, 's' },
348 { "force", no_argument, NULL, 'f' },
349 { "verbose", no_argument, NULL, 'v' },
353 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
361 args.flags |= BTRFS_BALANCE_DATA;
363 if (parse_filters(optarg, &args.data))
368 args.flags |= BTRFS_BALANCE_SYSTEM;
370 if (parse_filters(optarg, &args.sys))
375 args.flags |= BTRFS_BALANCE_METADATA;
377 if (parse_filters(optarg, &args.meta))
387 usage(cmd_balance_start_usage);
391 if (check_argc_exact(argc - optind, 1))
392 usage(cmd_balance_start_usage);
395 * allow -s only under --force, otherwise do with system chunks
396 * the same thing we were ordered to do with meta chunks
398 if (args.flags & BTRFS_BALANCE_SYSTEM) {
401 "Refusing to explicitly operate on system chunks.\n"
402 "Pass --force if you really want to do that.\n");
405 } else if (args.flags & BTRFS_BALANCE_METADATA) {
406 args.flags |= BTRFS_BALANCE_SYSTEM;
407 memcpy(&args.sys, &args.meta,
408 sizeof(struct btrfs_balance_args));
412 /* relocate everything - no filters */
413 args.flags |= BTRFS_BALANCE_TYPE_MASK;
416 /* drange makes sense only when devid is set */
417 for (i = 0; ptrs[i]; i++) {
418 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
419 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
420 fprintf(stderr, "drange filter can be used only if "
421 "devid filter is used\n");
426 /* soft makes sense only when convert for corresponding type is set */
427 for (i = 0; ptrs[i]; i++) {
428 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
429 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
430 fprintf(stderr, "'soft' option can be used only if "
431 "changing profiles\n");
437 args.flags |= BTRFS_BALANCE_FORCE;
439 dump_ioctl_balance_args(&args);
441 return do_balance(argv[optind], &args);
444 static const char * const cmd_balance_pause_usage[] = {
445 "btrfs [filesystem] balance pause <path>",
446 "Pause running balance",
450 static int cmd_balance_pause(int argc, char **argv)
457 if (check_argc_exact(argc, 2))
458 usage(cmd_balance_pause_usage);
462 fd = open_file_or_dir(path);
464 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
468 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
473 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
474 path, (e == ENOTCONN) ? "Not running" : strerror(e));
481 static const char * const cmd_balance_cancel_usage[] = {
482 "btrfs [filesystem] balance cancel <path>",
483 "Cancel running or paused balance",
487 static int cmd_balance_cancel(int argc, char **argv)
494 if (check_argc_exact(argc, 2))
495 usage(cmd_balance_cancel_usage);
499 fd = open_file_or_dir(path);
501 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
505 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
510 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
511 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
518 static const char * const cmd_balance_resume_usage[] = {
519 "btrfs [filesystem] balance resume <path>",
520 "Resume interrupted balance",
524 static int cmd_balance_resume(int argc, char **argv)
526 struct btrfs_ioctl_balance_args args;
532 if (check_argc_exact(argc, 2))
533 usage(cmd_balance_resume_usage);
537 fd = open_file_or_dir(path);
539 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
543 memset(&args, 0, sizeof(args));
544 args.flags |= BTRFS_BALANCE_RESUME;
546 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
551 if (e == ECANCELED) {
552 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
553 fprintf(stderr, "balance paused by user\n");
554 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
555 fprintf(stderr, "balance canceled by user\n");
556 } else if (e == ENOTCONN || e == EINPROGRESS) {
557 fprintf(stderr, "ERROR: balance resume on '%s' "
558 "failed - %s\n", path,
559 (e == ENOTCONN) ? "Not in progress" :
564 "ERROR: error during balancing '%s' - %s\n"
565 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
569 printf("Done, had to relocate %llu out of %llu chunks\n",
570 (unsigned long long)args.stat.completed,
571 (unsigned long long)args.stat.considered);
577 static const char * const cmd_balance_status_usage[] = {
578 "btrfs [filesystem] balance status [-v] <path>",
579 "Show status of running or paused balance",
585 static int cmd_balance_status(int argc, char **argv)
587 struct btrfs_ioctl_balance_args args;
597 static struct option longopts[] = {
598 { "verbose", no_argument, NULL, 'v' },
602 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
611 usage(cmd_balance_status_usage);
615 if (check_argc_exact(argc - optind, 1))
616 usage(cmd_balance_status_usage);
620 fd = open_file_or_dir(path);
622 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
626 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
631 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
632 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
636 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
637 printf("Balance on '%s' is running", path);
638 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
639 printf(", cancel requested\n");
640 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
641 printf(", pause requested\n");
645 printf("Balance on '%s' is paused\n", path);
648 printf("%llu out of about %llu chunks balanced (%llu considered), "
649 "%3.f%% left\n", (unsigned long long)args.stat.completed,
650 (unsigned long long)args.stat.expected,
651 (unsigned long long)args.stat.considered,
652 100 * (1 - (float)args.stat.completed/args.stat.expected));
655 dump_ioctl_balance_args(&args);
660 const struct cmd_group balance_cmd_group = {
661 balance_cmd_group_usage, balance_cmd_group_info, {
662 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
663 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
664 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
665 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
666 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
671 int cmd_balance(int argc, char **argv)
674 /* old 'btrfs filesystem balance <path>' syntax */
675 struct btrfs_ioctl_balance_args args;
677 memset(&args, 0, sizeof(args));
678 args.flags |= BTRFS_BALANCE_TYPE_MASK;
680 return do_balance(argv[1], &args);
683 return handle_command_group(&balance_cmd_group, argc, argv);