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 * const balance_cmd_group_usage[] = {
33 "btrfs [filesystem] balance <command> [options] <path>",
34 "btrfs [filesystem] balance <path>",
38 static const char balance_cmd_group_info[] =
39 "'btrfs filesystem balance' command is deprecated, please use\n"
40 "'btrfs balance start' command instead.";
42 static int parse_one_profile(const char *profile, u64 *flags)
44 if (!strcmp(profile, "raid0")) {
45 *flags |= BTRFS_BLOCK_GROUP_RAID0;
46 } else if (!strcmp(profile, "raid1")) {
47 *flags |= BTRFS_BLOCK_GROUP_RAID1;
48 } else if (!strcmp(profile, "raid10")) {
49 *flags |= BTRFS_BLOCK_GROUP_RAID10;
50 } else if (!strcmp(profile, "dup")) {
51 *flags |= BTRFS_BLOCK_GROUP_DUP;
52 } else if (!strcmp(profile, "single")) {
53 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
55 fprintf(stderr, "Unknown profile '%s'\n", profile);
62 static int parse_profiles(char *profiles, u64 *flags)
67 for (this_char = strtok_r(profiles, "|", &save_ptr);
69 this_char = strtok_r(NULL, "|", &save_ptr)) {
70 if (parse_one_profile(this_char, flags))
77 static int parse_u64(const char *str, u64 *result)
82 val = strtoull(str, &endptr, 10);
90 static int parse_range(const char *range, u64 *start, u64 *end)
94 dots = strstr(range, "..");
96 const char *rest = dots + 2;
105 if (parse_u64(rest, end))
112 if (parse_u64(range, start))
116 if (*start >= *end) {
117 fprintf(stderr, "Range %llu..%llu doesn't make "
118 "sense\n", (unsigned long long)*start,
119 (unsigned long long)*end);
130 static int parse_filters(char *filters, struct btrfs_balance_args *args)
139 for (this_char = strtok_r(filters, ",", &save_ptr);
141 this_char = strtok_r(NULL, ",", &save_ptr)) {
142 if ((value = strchr(this_char, '=')) != NULL)
144 if (!strcmp(this_char, "profiles")) {
145 if (!value || !*value) {
146 fprintf(stderr, "the profiles filter requires "
150 if (parse_profiles(value, &args->profiles)) {
151 fprintf(stderr, "Invalid profiles argument\n");
154 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
155 } else if (!strcmp(this_char, "usage")) {
156 if (!value || !*value) {
157 fprintf(stderr, "the usage filter requires "
161 if (parse_u64(value, &args->usage) ||
162 args->usage < 1 || args->usage > 100) {
163 fprintf(stderr, "Invalid usage argument: %s\n",
167 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
168 } else if (!strcmp(this_char, "devid")) {
169 if (!value || !*value) {
170 fprintf(stderr, "the devid filter requires "
174 if (parse_u64(value, &args->devid) ||
176 fprintf(stderr, "Invalid devid argument: %s\n",
180 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
181 } else if (!strcmp(this_char, "drange")) {
182 if (!value || !*value) {
183 fprintf(stderr, "the drange filter requires "
187 if (parse_range(value, &args->pstart, &args->pend)) {
188 fprintf(stderr, "Invalid drange argument\n");
191 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
192 } else if (!strcmp(this_char, "vrange")) {
193 if (!value || !*value) {
194 fprintf(stderr, "the vrange filter requires "
198 if (parse_range(value, &args->vstart, &args->vend)) {
199 fprintf(stderr, "Invalid vrange argument\n");
202 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
203 } else if (!strcmp(this_char, "convert")) {
204 if (!value || !*value) {
205 fprintf(stderr, "the convert option requires "
209 if (parse_one_profile(value, &args->target)) {
210 fprintf(stderr, "Invalid convert argument\n");
213 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
214 } else if (!strcmp(this_char, "soft")) {
215 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
217 fprintf(stderr, "Unrecognized balance option '%s'\n",
226 static void dump_balance_args(struct btrfs_balance_args *args)
228 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
229 printf("converting, target=%llu, soft is %s",
230 (unsigned long long)args->target,
231 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
236 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
237 printf(", profiles=%llu", (unsigned long long)args->profiles);
238 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
239 printf(", usage=%llu", (unsigned long long)args->usage);
240 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
241 printf(", devid=%llu", (unsigned long long)args->devid);
242 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
243 printf(", drange=%llu..%llu",
244 (unsigned long long)args->pstart,
245 (unsigned long long)args->pend);
246 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
247 printf(", vrange=%llu..%llu",
248 (unsigned long long)args->vstart,
249 (unsigned long long)args->vend);
254 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
256 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
257 (unsigned long long)args->flags, (unsigned long long)args->state,
258 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
259 if (args->flags & BTRFS_BALANCE_DATA) {
260 printf(" DATA (flags 0x%llx): ",
261 (unsigned long long)args->data.flags);
262 dump_balance_args(&args->data);
264 if (args->flags & BTRFS_BALANCE_METADATA) {
265 printf(" METADATA (flags 0x%llx): ",
266 (unsigned long long)args->meta.flags);
267 dump_balance_args(&args->meta);
269 if (args->flags & BTRFS_BALANCE_SYSTEM) {
270 printf(" SYSTEM (flags 0x%llx): ",
271 (unsigned long long)args->sys.flags);
272 dump_balance_args(&args->sys);
276 static int do_balance_v1(int fd)
278 struct btrfs_ioctl_vol_args args;
281 memset(&args, 0, sizeof(args));
282 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
286 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
293 fd = open_file_or_dir(path);
295 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
299 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
304 * older kernels don't have the new balance ioctl, try the
305 * old one. But, the old one doesn't know any filters, so
306 * don't fall back if they tried to use the fancy new things
308 if (e == ENOTTY && nofilters) {
309 ret = do_balance_v1(fd);
315 if (e == ECANCELED) {
316 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
317 fprintf(stderr, "balance paused by user\n");
318 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
319 fprintf(stderr, "balance canceled by user\n");
322 fprintf(stderr, "ERROR: error during balancing '%s' "
323 "- %s\n", path, strerror(e));
324 if (e != EINPROGRESS)
325 fprintf(stderr, "There may be more info in "
326 "syslog - try dmesg | tail\n");
330 printf("Done, had to relocate %llu out of %llu chunks\n",
331 (unsigned long long)args->stat.completed,
332 (unsigned long long)args->stat.considered);
341 static const char * const cmd_balance_start_usage[] = {
342 "btrfs [filesystem] balance start [options] <path>",
343 "Balance chunks across the devices",
344 "Balance and/or convert (change allocation profile of) chunks that",
345 "passed all filters in a comma-separated list of filters for a",
346 "particular chunk type. If filter list is not given balance all",
347 "chunks of that type. In case none of the -d, -m or -s options is",
348 "given balance all chunks in a filesystem.",
350 "-d[filters] act on data chunks",
351 "-m[filters] act on metadata chunks",
352 "-s[filetrs] act on system chunks (only under -f)",
354 "-f force reducing of metadata integrity",
358 static int cmd_balance_start(int argc, char **argv)
360 struct btrfs_ioctl_balance_args args;
361 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
368 memset(&args, 0, sizeof(args));
373 static struct option longopts[] = {
374 { "data", optional_argument, NULL, 'd'},
375 { "metadata", optional_argument, NULL, 'm' },
376 { "system", optional_argument, NULL, 's' },
377 { "force", no_argument, NULL, 'f' },
378 { "verbose", no_argument, NULL, 'v' },
382 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
390 args.flags |= BTRFS_BALANCE_DATA;
392 if (parse_filters(optarg, &args.data))
397 args.flags |= BTRFS_BALANCE_SYSTEM;
399 if (parse_filters(optarg, &args.sys))
404 args.flags |= BTRFS_BALANCE_METADATA;
406 if (parse_filters(optarg, &args.meta))
416 usage(cmd_balance_start_usage);
420 if (check_argc_exact(argc - optind, 1))
421 usage(cmd_balance_start_usage);
424 * allow -s only under --force, otherwise do with system chunks
425 * the same thing we were ordered to do with meta chunks
427 if (args.flags & BTRFS_BALANCE_SYSTEM) {
430 "Refusing to explicitly operate on system chunks.\n"
431 "Pass --force if you really want to do that.\n");
434 } else if (args.flags & BTRFS_BALANCE_METADATA) {
435 args.flags |= BTRFS_BALANCE_SYSTEM;
436 memcpy(&args.sys, &args.meta,
437 sizeof(struct btrfs_balance_args));
441 /* relocate everything - no filters */
442 args.flags |= BTRFS_BALANCE_TYPE_MASK;
445 /* drange makes sense only when devid is set */
446 for (i = 0; ptrs[i]; i++) {
447 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
448 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
449 fprintf(stderr, "drange filter can be used only if "
450 "devid filter is used\n");
455 /* soft makes sense only when convert for corresponding type is set */
456 for (i = 0; ptrs[i]; i++) {
457 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
458 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
459 fprintf(stderr, "'soft' option can be used only if "
460 "changing profiles\n");
466 args.flags |= BTRFS_BALANCE_FORCE;
468 dump_ioctl_balance_args(&args);
470 return do_balance(argv[optind], &args, nofilters);
473 static const char * const cmd_balance_pause_usage[] = {
474 "btrfs [filesystem] balance pause <path>",
475 "Pause running balance",
479 static int cmd_balance_pause(int argc, char **argv)
486 if (check_argc_exact(argc, 2))
487 usage(cmd_balance_pause_usage);
491 fd = open_file_or_dir(path);
493 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
497 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
502 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
503 path, (e == ENOTCONN) ? "Not running" : strerror(e));
510 static const char * const cmd_balance_cancel_usage[] = {
511 "btrfs [filesystem] balance cancel <path>",
512 "Cancel running or paused balance",
516 static int cmd_balance_cancel(int argc, char **argv)
523 if (check_argc_exact(argc, 2))
524 usage(cmd_balance_cancel_usage);
528 fd = open_file_or_dir(path);
530 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
534 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
539 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
540 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
547 static const char * const cmd_balance_resume_usage[] = {
548 "btrfs [filesystem] balance resume <path>",
549 "Resume interrupted balance",
553 static int cmd_balance_resume(int argc, char **argv)
555 struct btrfs_ioctl_balance_args args;
561 if (check_argc_exact(argc, 2))
562 usage(cmd_balance_resume_usage);
566 fd = open_file_or_dir(path);
568 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
572 memset(&args, 0, sizeof(args));
573 args.flags |= BTRFS_BALANCE_RESUME;
575 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
580 if (e == ECANCELED) {
581 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
582 fprintf(stderr, "balance paused by user\n");
583 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
584 fprintf(stderr, "balance canceled by user\n");
585 } else if (e == ENOTCONN || e == EINPROGRESS) {
586 fprintf(stderr, "ERROR: balance resume on '%s' "
587 "failed - %s\n", path,
588 (e == ENOTCONN) ? "Not in progress" :
593 "ERROR: error during balancing '%s' - %s\n"
594 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
598 printf("Done, had to relocate %llu out of %llu chunks\n",
599 (unsigned long long)args.stat.completed,
600 (unsigned long long)args.stat.considered);
606 static const char * const cmd_balance_status_usage[] = {
607 "btrfs [filesystem] balance status [-v] <path>",
608 "Show status of running or paused balance",
614 static int cmd_balance_status(int argc, char **argv)
616 struct btrfs_ioctl_balance_args args;
626 static struct option longopts[] = {
627 { "verbose", no_argument, NULL, 'v' },
631 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
640 usage(cmd_balance_status_usage);
644 if (check_argc_exact(argc - optind, 1))
645 usage(cmd_balance_status_usage);
649 fd = open_file_or_dir(path);
651 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
655 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
660 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
661 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
665 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
666 printf("Balance on '%s' is running", path);
667 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
668 printf(", cancel requested\n");
669 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
670 printf(", pause requested\n");
674 printf("Balance on '%s' is paused\n", path);
677 printf("%llu out of about %llu chunks balanced (%llu considered), "
678 "%3.f%% left\n", (unsigned long long)args.stat.completed,
679 (unsigned long long)args.stat.expected,
680 (unsigned long long)args.stat.considered,
681 100 * (1 - (float)args.stat.completed/args.stat.expected));
684 dump_ioctl_balance_args(&args);
689 const struct cmd_group balance_cmd_group = {
690 balance_cmd_group_usage, balance_cmd_group_info, {
691 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
692 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
693 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
694 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
695 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
700 int cmd_balance(int argc, char **argv)
703 /* old 'btrfs filesystem balance <path>' syntax */
704 struct btrfs_ioctl_balance_args args;
706 memset(&args, 0, sizeof(args));
707 args.flags |= BTRFS_BALANCE_TYPE_MASK;
709 return do_balance(argv[1], &args, 1);
712 return handle_command_group(&balance_cmd_group, argc, argv);