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 [filesystem] balance <command> [options] <path>",
35 "btrfs [filesystem] balance <path>",
39 static const char balance_cmd_group_info[] =
40 "'btrfs filesystem balance' command is deprecated, please use\n"
41 "'btrfs balance start' command instead.";
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, "dup")) {
52 *flags |= BTRFS_BLOCK_GROUP_DUP;
53 } else if (!strcmp(profile, "single")) {
54 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
56 fprintf(stderr, "Unknown profile '%s'\n", profile);
63 static int parse_profiles(char *profiles, u64 *flags)
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);
91 static int parse_range(const char *range, u64 *start, u64 *end)
95 dots = strstr(range, "..");
97 const char *rest = dots + 2;
106 if (parse_u64(rest, end))
113 if (parse_u64(range, start))
117 if (*start >= *end) {
118 fprintf(stderr, "Range %llu..%llu doesn't make "
119 "sense\n", (unsigned long long)*start,
120 (unsigned long long)*end);
131 static int parse_filters(char *filters, struct btrfs_balance_args *args)
140 for (this_char = strtok_r(filters, ",", &save_ptr);
142 this_char = strtok_r(NULL, ",", &save_ptr)) {
143 if ((value = strchr(this_char, '=')) != NULL)
145 if (!strcmp(this_char, "profiles")) {
146 if (!value || !*value) {
147 fprintf(stderr, "the profiles filter requires "
151 if (parse_profiles(value, &args->profiles)) {
152 fprintf(stderr, "Invalid profiles argument\n");
155 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
156 } else if (!strcmp(this_char, "usage")) {
157 if (!value || !*value) {
158 fprintf(stderr, "the usage filter requires "
162 if (parse_u64(value, &args->usage) ||
163 args->usage < 1 || args->usage > 100) {
164 fprintf(stderr, "Invalid usage argument: %s\n",
168 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
169 } else if (!strcmp(this_char, "devid")) {
170 if (!value || !*value) {
171 fprintf(stderr, "the devid filter requires "
175 if (parse_u64(value, &args->devid) ||
177 fprintf(stderr, "Invalid devid argument: %s\n",
181 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
182 } else if (!strcmp(this_char, "drange")) {
183 if (!value || !*value) {
184 fprintf(stderr, "the drange filter requires "
188 if (parse_range(value, &args->pstart, &args->pend)) {
189 fprintf(stderr, "Invalid drange argument\n");
192 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
193 } else if (!strcmp(this_char, "vrange")) {
194 if (!value || !*value) {
195 fprintf(stderr, "the vrange filter requires "
199 if (parse_range(value, &args->vstart, &args->vend)) {
200 fprintf(stderr, "Invalid vrange argument\n");
203 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
204 } else if (!strcmp(this_char, "convert")) {
205 if (!value || !*value) {
206 fprintf(stderr, "the convert option requires "
210 if (parse_one_profile(value, &args->target)) {
211 fprintf(stderr, "Invalid convert argument\n");
214 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
215 } else if (!strcmp(this_char, "soft")) {
216 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
218 fprintf(stderr, "Unrecognized balance option '%s'\n",
227 static void dump_balance_args(struct btrfs_balance_args *args)
229 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
230 printf("converting, target=%llu, soft is %s",
231 (unsigned long long)args->target,
232 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
237 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
238 printf(", profiles=%llu", (unsigned long long)args->profiles);
239 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
240 printf(", usage=%llu", (unsigned long long)args->usage);
241 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
242 printf(", devid=%llu", (unsigned long long)args->devid);
243 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
244 printf(", drange=%llu..%llu",
245 (unsigned long long)args->pstart,
246 (unsigned long long)args->pend);
247 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
248 printf(", vrange=%llu..%llu",
249 (unsigned long long)args->vstart,
250 (unsigned long long)args->vend);
255 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
257 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
258 (unsigned long long)args->flags, (unsigned long long)args->state,
259 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
260 if (args->flags & BTRFS_BALANCE_DATA) {
261 printf(" DATA (flags 0x%llx): ",
262 (unsigned long long)args->data.flags);
263 dump_balance_args(&args->data);
265 if (args->flags & BTRFS_BALANCE_METADATA) {
266 printf(" METADATA (flags 0x%llx): ",
267 (unsigned long long)args->meta.flags);
268 dump_balance_args(&args->meta);
270 if (args->flags & BTRFS_BALANCE_SYSTEM) {
271 printf(" SYSTEM (flags 0x%llx): ",
272 (unsigned long long)args->sys.flags);
273 dump_balance_args(&args->sys);
277 static int do_balance_v1(int fd)
279 struct btrfs_ioctl_vol_args args;
282 memset(&args, 0, sizeof(args));
283 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
287 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
294 fd = open_file_or_dir(path);
296 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
300 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
305 * older kernels don't have the new balance ioctl, try the
306 * old one. But, the old one doesn't know any filters, so
307 * don't fall back if they tried to use the fancy new things
309 if (e == ENOTTY && nofilters) {
310 ret = do_balance_v1(fd);
316 if (e == ECANCELED) {
317 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
318 fprintf(stderr, "balance paused by user\n");
319 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
320 fprintf(stderr, "balance canceled by user\n");
323 fprintf(stderr, "ERROR: error during balancing '%s' "
324 "- %s\n", path, strerror(e));
325 if (e != EINPROGRESS)
326 fprintf(stderr, "There may be more info in "
327 "syslog - try dmesg | tail\n");
331 printf("Done, had to relocate %llu out of %llu chunks\n",
332 (unsigned long long)args->stat.completed,
333 (unsigned long long)args->stat.considered);
342 static const char * const cmd_balance_start_usage[] = {
343 "btrfs [filesystem] balance start [options] <path>",
344 "Balance chunks across the devices",
345 "Balance and/or convert (change allocation profile of) chunks that",
346 "passed all filters in a comma-separated list of filters for a",
347 "particular chunk type. If filter list is not given balance all",
348 "chunks of that type. In case none of the -d, -m or -s options is",
349 "given balance all chunks in a filesystem.",
351 "-d[filters] act on data chunks",
352 "-m[filters] act on metadata chunks",
353 "-s[filters] act on system chunks (only under -f)",
355 "-f force reducing of metadata integrity",
359 static int cmd_balance_start(int argc, char **argv)
361 struct btrfs_ioctl_balance_args args;
362 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
369 memset(&args, 0, sizeof(args));
374 static struct option longopts[] = {
375 { "data", optional_argument, NULL, 'd'},
376 { "metadata", optional_argument, NULL, 'm' },
377 { "system", optional_argument, NULL, 's' },
378 { "force", no_argument, NULL, 'f' },
379 { "verbose", no_argument, NULL, 'v' },
383 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
391 args.flags |= BTRFS_BALANCE_DATA;
393 if (parse_filters(optarg, &args.data))
398 args.flags |= BTRFS_BALANCE_SYSTEM;
400 if (parse_filters(optarg, &args.sys))
405 args.flags |= BTRFS_BALANCE_METADATA;
407 if (parse_filters(optarg, &args.meta))
417 usage(cmd_balance_start_usage);
421 if (check_argc_exact(argc - optind, 1))
422 usage(cmd_balance_start_usage);
425 * allow -s only under --force, otherwise do with system chunks
426 * the same thing we were ordered to do with meta chunks
428 if (args.flags & BTRFS_BALANCE_SYSTEM) {
431 "Refusing to explicitly operate on system chunks.\n"
432 "Pass --force if you really want to do that.\n");
435 } else if (args.flags & BTRFS_BALANCE_METADATA) {
436 args.flags |= BTRFS_BALANCE_SYSTEM;
437 memcpy(&args.sys, &args.meta,
438 sizeof(struct btrfs_balance_args));
442 /* relocate everything - no filters */
443 args.flags |= BTRFS_BALANCE_TYPE_MASK;
446 /* drange makes sense only when devid is set */
447 for (i = 0; ptrs[i]; i++) {
448 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
449 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
450 fprintf(stderr, "drange filter can be used only if "
451 "devid filter is used\n");
456 /* soft makes sense only when convert for corresponding type is set */
457 for (i = 0; ptrs[i]; i++) {
458 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
459 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
460 fprintf(stderr, "'soft' option can be used only if "
461 "changing profiles\n");
467 args.flags |= BTRFS_BALANCE_FORCE;
469 dump_ioctl_balance_args(&args);
471 return do_balance(argv[optind], &args, nofilters);
474 static const char * const cmd_balance_pause_usage[] = {
475 "btrfs [filesystem] balance pause <path>",
476 "Pause running balance",
480 static int cmd_balance_pause(int argc, char **argv)
487 if (check_argc_exact(argc, 2))
488 usage(cmd_balance_pause_usage);
492 fd = open_file_or_dir(path);
494 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
498 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
503 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
504 path, (e == ENOTCONN) ? "Not running" : strerror(e));
511 static const char * const cmd_balance_cancel_usage[] = {
512 "btrfs [filesystem] balance cancel <path>",
513 "Cancel running or paused balance",
517 static int cmd_balance_cancel(int argc, char **argv)
524 if (check_argc_exact(argc, 2))
525 usage(cmd_balance_cancel_usage);
529 fd = open_file_or_dir(path);
531 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
535 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
540 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
541 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
548 static const char * const cmd_balance_resume_usage[] = {
549 "btrfs [filesystem] balance resume <path>",
550 "Resume interrupted balance",
554 static int cmd_balance_resume(int argc, char **argv)
556 struct btrfs_ioctl_balance_args args;
562 if (check_argc_exact(argc, 2))
563 usage(cmd_balance_resume_usage);
567 fd = open_file_or_dir(path);
569 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
573 memset(&args, 0, sizeof(args));
574 args.flags |= BTRFS_BALANCE_RESUME;
576 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
581 if (e == ECANCELED) {
582 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
583 fprintf(stderr, "balance paused by user\n");
584 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
585 fprintf(stderr, "balance canceled by user\n");
586 } else if (e == ENOTCONN || e == EINPROGRESS) {
587 fprintf(stderr, "ERROR: balance resume on '%s' "
588 "failed - %s\n", path,
589 (e == ENOTCONN) ? "Not in progress" :
594 "ERROR: error during balancing '%s' - %s\n"
595 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
599 printf("Done, had to relocate %llu out of %llu chunks\n",
600 (unsigned long long)args.stat.completed,
601 (unsigned long long)args.stat.considered);
607 static const char * const cmd_balance_status_usage[] = {
608 "btrfs [filesystem] balance status [-v] <path>",
609 "Show status of running or paused balance",
615 static int cmd_balance_status(int argc, char **argv)
617 struct btrfs_ioctl_balance_args args;
627 static struct option longopts[] = {
628 { "verbose", no_argument, NULL, 'v' },
632 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
641 usage(cmd_balance_status_usage);
645 if (check_argc_exact(argc - optind, 1))
646 usage(cmd_balance_status_usage);
650 fd = open_file_or_dir(path);
652 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
656 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
661 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
662 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
666 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
667 printf("Balance on '%s' is running", path);
668 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
669 printf(", cancel requested\n");
670 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
671 printf(", pause requested\n");
675 printf("Balance on '%s' is paused\n", path);
678 printf("%llu out of about %llu chunks balanced (%llu considered), "
679 "%3.f%% left\n", (unsigned long long)args.stat.completed,
680 (unsigned long long)args.stat.expected,
681 (unsigned long long)args.stat.considered,
682 100 * (1 - (float)args.stat.completed/args.stat.expected));
685 dump_ioctl_balance_args(&args);
690 const struct cmd_group balance_cmd_group = {
691 balance_cmd_group_usage, balance_cmd_group_info, {
692 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
693 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
694 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
695 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
696 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
701 int cmd_balance(int argc, char **argv)
704 /* old 'btrfs filesystem balance <path>' syntax */
705 struct btrfs_ioctl_balance_args args;
707 memset(&args, 0, sizeof(args));
708 args.flags |= BTRFS_BALANCE_TYPE_MASK;
710 return do_balance(argv[1], &args, 1);
713 return handle_command_group(&balance_cmd_group, argc, argv);