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, "raid5")) {
51 *flags |= BTRFS_BLOCK_GROUP_RAID5;
52 } else if (!strcmp(profile, "raid6")) {
53 *flags |= BTRFS_BLOCK_GROUP_RAID6;
54 } else if (!strcmp(profile, "dup")) {
55 *flags |= BTRFS_BLOCK_GROUP_DUP;
56 } else if (!strcmp(profile, "single")) {
57 *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE;
59 fprintf(stderr, "Unknown profile '%s'\n", profile);
66 static int parse_profiles(char *profiles, u64 *flags)
71 for (this_char = strtok_r(profiles, "|", &save_ptr);
73 this_char = strtok_r(NULL, "|", &save_ptr)) {
74 if (parse_one_profile(this_char, flags))
81 static int parse_u64(const char *str, u64 *result)
86 val = strtoull(str, &endptr, 10);
94 static int parse_range(const char *range, u64 *start, u64 *end)
98 dots = strstr(range, "..");
100 const char *rest = dots + 2;
109 if (parse_u64(rest, end))
116 if (parse_u64(range, start))
120 if (*start >= *end) {
121 fprintf(stderr, "Range %llu..%llu doesn't make "
122 "sense\n", (unsigned long long)*start,
123 (unsigned long long)*end);
134 static int parse_filters(char *filters, struct btrfs_balance_args *args)
143 for (this_char = strtok_r(filters, ",", &save_ptr);
145 this_char = strtok_r(NULL, ",", &save_ptr)) {
146 if ((value = strchr(this_char, '=')) != NULL)
148 if (!strcmp(this_char, "profiles")) {
149 if (!value || !*value) {
150 fprintf(stderr, "the profiles filter requires "
154 if (parse_profiles(value, &args->profiles)) {
155 fprintf(stderr, "Invalid profiles argument\n");
158 args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
159 } else if (!strcmp(this_char, "usage")) {
160 if (!value || !*value) {
161 fprintf(stderr, "the usage filter requires "
165 if (parse_u64(value, &args->usage) ||
166 args->usage < 1 || args->usage > 100) {
167 fprintf(stderr, "Invalid usage argument: %s\n",
171 args->flags |= BTRFS_BALANCE_ARGS_USAGE;
172 } else if (!strcmp(this_char, "devid")) {
173 if (!value || !*value) {
174 fprintf(stderr, "the devid filter requires "
178 if (parse_u64(value, &args->devid) ||
180 fprintf(stderr, "Invalid devid argument: %s\n",
184 args->flags |= BTRFS_BALANCE_ARGS_DEVID;
185 } else if (!strcmp(this_char, "drange")) {
186 if (!value || !*value) {
187 fprintf(stderr, "the drange filter requires "
191 if (parse_range(value, &args->pstart, &args->pend)) {
192 fprintf(stderr, "Invalid drange argument\n");
195 args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
196 } else if (!strcmp(this_char, "vrange")) {
197 if (!value || !*value) {
198 fprintf(stderr, "the vrange filter requires "
202 if (parse_range(value, &args->vstart, &args->vend)) {
203 fprintf(stderr, "Invalid vrange argument\n");
206 args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
207 } else if (!strcmp(this_char, "convert")) {
208 if (!value || !*value) {
209 fprintf(stderr, "the convert option requires "
213 if (parse_one_profile(value, &args->target)) {
214 fprintf(stderr, "Invalid convert argument\n");
217 args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
218 } else if (!strcmp(this_char, "soft")) {
219 args->flags |= BTRFS_BALANCE_ARGS_SOFT;
221 fprintf(stderr, "Unrecognized balance option '%s'\n",
230 static void dump_balance_args(struct btrfs_balance_args *args)
232 if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
233 printf("converting, target=%llu, soft is %s",
234 (unsigned long long)args->target,
235 (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
240 if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
241 printf(", profiles=%llu", (unsigned long long)args->profiles);
242 if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
243 printf(", usage=%llu", (unsigned long long)args->usage);
244 if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
245 printf(", devid=%llu", (unsigned long long)args->devid);
246 if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
247 printf(", drange=%llu..%llu",
248 (unsigned long long)args->pstart,
249 (unsigned long long)args->pend);
250 if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
251 printf(", vrange=%llu..%llu",
252 (unsigned long long)args->vstart,
253 (unsigned long long)args->vend);
258 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
260 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
261 (unsigned long long)args->flags, (unsigned long long)args->state,
262 (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
263 if (args->flags & BTRFS_BALANCE_DATA) {
264 printf(" DATA (flags 0x%llx): ",
265 (unsigned long long)args->data.flags);
266 dump_balance_args(&args->data);
268 if (args->flags & BTRFS_BALANCE_METADATA) {
269 printf(" METADATA (flags 0x%llx): ",
270 (unsigned long long)args->meta.flags);
271 dump_balance_args(&args->meta);
273 if (args->flags & BTRFS_BALANCE_SYSTEM) {
274 printf(" SYSTEM (flags 0x%llx): ",
275 (unsigned long long)args->sys.flags);
276 dump_balance_args(&args->sys);
280 static int do_balance_v1(int fd)
282 struct btrfs_ioctl_vol_args args;
285 memset(&args, 0, sizeof(args));
286 ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
290 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
297 fd = open_file_or_dir(path);
299 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
303 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
308 * older kernels don't have the new balance ioctl, try the
309 * old one. But, the old one doesn't know any filters, so
310 * don't fall back if they tried to use the fancy new things
312 if (e == ENOTTY && nofilters) {
313 ret = do_balance_v1(fd);
319 if (e == ECANCELED) {
320 if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
321 fprintf(stderr, "balance paused by user\n");
322 if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
323 fprintf(stderr, "balance canceled by user\n");
326 fprintf(stderr, "ERROR: error during balancing '%s' "
327 "- %s\n", path, strerror(e));
328 if (e != EINPROGRESS)
329 fprintf(stderr, "There may be more info in "
330 "syslog - try dmesg | tail\n");
334 printf("Done, had to relocate %llu out of %llu chunks\n",
335 (unsigned long long)args->stat.completed,
336 (unsigned long long)args->stat.considered);
345 static const char * const cmd_balance_start_usage[] = {
346 "btrfs [filesystem] balance start [options] <path>",
347 "Balance chunks across the devices",
348 "Balance and/or convert (change allocation profile of) chunks that",
349 "passed all filters in a comma-separated list of filters for a",
350 "particular chunk type. If filter list is not given balance all",
351 "chunks of that type. In case none of the -d, -m or -s options is",
352 "given balance all chunks in a filesystem.",
354 "-d[filters] act on data chunks",
355 "-m[filters] act on metadata chunks",
356 "-s[filetrs] act on system chunks (only under -f)",
358 "-f force reducing of metadata integrity",
362 static int cmd_balance_start(int argc, char **argv)
364 struct btrfs_ioctl_balance_args args;
365 struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
372 memset(&args, 0, sizeof(args));
377 static struct option longopts[] = {
378 { "data", optional_argument, NULL, 'd'},
379 { "metadata", optional_argument, NULL, 'm' },
380 { "system", optional_argument, NULL, 's' },
381 { "force", no_argument, NULL, 'f' },
382 { "verbose", no_argument, NULL, 'v' },
386 int opt = getopt_long(argc, argv, "d::s::m::fv", longopts,
394 args.flags |= BTRFS_BALANCE_DATA;
396 if (parse_filters(optarg, &args.data))
401 args.flags |= BTRFS_BALANCE_SYSTEM;
403 if (parse_filters(optarg, &args.sys))
408 args.flags |= BTRFS_BALANCE_METADATA;
410 if (parse_filters(optarg, &args.meta))
420 usage(cmd_balance_start_usage);
424 if (check_argc_exact(argc - optind, 1))
425 usage(cmd_balance_start_usage);
428 * allow -s only under --force, otherwise do with system chunks
429 * the same thing we were ordered to do with meta chunks
431 if (args.flags & BTRFS_BALANCE_SYSTEM) {
434 "Refusing to explicitly operate on system chunks.\n"
435 "Pass --force if you really want to do that.\n");
438 } else if (args.flags & BTRFS_BALANCE_METADATA) {
439 args.flags |= BTRFS_BALANCE_SYSTEM;
440 memcpy(&args.sys, &args.meta,
441 sizeof(struct btrfs_balance_args));
445 /* relocate everything - no filters */
446 args.flags |= BTRFS_BALANCE_TYPE_MASK;
449 /* drange makes sense only when devid is set */
450 for (i = 0; ptrs[i]; i++) {
451 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
452 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
453 fprintf(stderr, "drange filter can be used only if "
454 "devid filter is used\n");
459 /* soft makes sense only when convert for corresponding type is set */
460 for (i = 0; ptrs[i]; i++) {
461 if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
462 !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
463 fprintf(stderr, "'soft' option can be used only if "
464 "changing profiles\n");
470 args.flags |= BTRFS_BALANCE_FORCE;
472 dump_ioctl_balance_args(&args);
474 return do_balance(argv[optind], &args, nofilters);
477 static const char * const cmd_balance_pause_usage[] = {
478 "btrfs [filesystem] balance pause <path>",
479 "Pause running balance",
483 static int cmd_balance_pause(int argc, char **argv)
490 if (check_argc_exact(argc, 2))
491 usage(cmd_balance_pause_usage);
495 fd = open_file_or_dir(path);
497 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
501 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
506 fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
507 path, (e == ENOTCONN) ? "Not running" : strerror(e));
514 static const char * const cmd_balance_cancel_usage[] = {
515 "btrfs [filesystem] balance cancel <path>",
516 "Cancel running or paused balance",
520 static int cmd_balance_cancel(int argc, char **argv)
527 if (check_argc_exact(argc, 2))
528 usage(cmd_balance_cancel_usage);
532 fd = open_file_or_dir(path);
534 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
538 ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
543 fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
544 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
551 static const char * const cmd_balance_resume_usage[] = {
552 "btrfs [filesystem] balance resume <path>",
553 "Resume interrupted balance",
557 static int cmd_balance_resume(int argc, char **argv)
559 struct btrfs_ioctl_balance_args args;
565 if (check_argc_exact(argc, 2))
566 usage(cmd_balance_resume_usage);
570 fd = open_file_or_dir(path);
572 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
576 memset(&args, 0, sizeof(args));
577 args.flags |= BTRFS_BALANCE_RESUME;
579 ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
584 if (e == ECANCELED) {
585 if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
586 fprintf(stderr, "balance paused by user\n");
587 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
588 fprintf(stderr, "balance canceled by user\n");
589 } else if (e == ENOTCONN || e == EINPROGRESS) {
590 fprintf(stderr, "ERROR: balance resume on '%s' "
591 "failed - %s\n", path,
592 (e == ENOTCONN) ? "Not in progress" :
597 "ERROR: error during balancing '%s' - %s\n"
598 "There may be more info in syslog - try dmesg | tail\n", path, strerror(e));
602 printf("Done, had to relocate %llu out of %llu chunks\n",
603 (unsigned long long)args.stat.completed,
604 (unsigned long long)args.stat.considered);
610 static const char * const cmd_balance_status_usage[] = {
611 "btrfs [filesystem] balance status [-v] <path>",
612 "Show status of running or paused balance",
618 static int cmd_balance_status(int argc, char **argv)
620 struct btrfs_ioctl_balance_args args;
630 static struct option longopts[] = {
631 { "verbose", no_argument, NULL, 'v' },
635 int opt = getopt_long(argc, argv, "v", longopts, &longindex);
644 usage(cmd_balance_status_usage);
648 if (check_argc_exact(argc - optind, 1))
649 usage(cmd_balance_status_usage);
653 fd = open_file_or_dir(path);
655 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
659 ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
664 fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n",
665 path, (e == ENOTCONN) ? "Not in progress" : strerror(e));
669 if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
670 printf("Balance on '%s' is running", path);
671 if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
672 printf(", cancel requested\n");
673 else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
674 printf(", pause requested\n");
678 printf("Balance on '%s' is paused\n", path);
681 printf("%llu out of about %llu chunks balanced (%llu considered), "
682 "%3.f%% left\n", (unsigned long long)args.stat.completed,
683 (unsigned long long)args.stat.expected,
684 (unsigned long long)args.stat.considered,
685 100 * (1 - (float)args.stat.completed/args.stat.expected));
688 dump_ioctl_balance_args(&args);
693 const struct cmd_group balance_cmd_group = {
694 balance_cmd_group_usage, balance_cmd_group_info, {
695 { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 },
696 { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 },
697 { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 },
698 { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 },
699 { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 },
704 int cmd_balance(int argc, char **argv)
707 /* old 'btrfs filesystem balance <path>' syntax */
708 struct btrfs_ioctl_balance_args args;
710 memset(&args, 0, sizeof(args));
711 args.flags |= BTRFS_BALANCE_TYPE_MASK;
713 return do_balance(argv[1], &args, 1);
716 return handle_command_group(&balance_cmd_group, argc, argv);