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
, "raid5")) {
52 *flags
|= BTRFS_BLOCK_GROUP_RAID5
;
53 } else if (!strcmp(profile
, "raid6")) {
54 *flags
|= BTRFS_BLOCK_GROUP_RAID6
;
55 } else if (!strcmp(profile
, "dup")) {
56 *flags
|= BTRFS_BLOCK_GROUP_DUP
;
57 } else if (!strcmp(profile
, "single")) {
58 *flags
|= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
60 fprintf(stderr
, "Unknown profile '%s'\n", profile
);
67 static int parse_profiles(char *profiles
, u64
*flags
)
70 char *save_ptr
= NULL
; /* Satisfy static checkers */
72 for (this_char
= strtok_r(profiles
, "|", &save_ptr
);
74 this_char
= strtok_r(NULL
, "|", &save_ptr
)) {
75 if (parse_one_profile(this_char
, flags
))
82 static int parse_u64(const char *str
, u64
*result
)
87 val
= strtoull(str
, &endptr
, 10);
95 static int parse_range(const char *range
, u64
*start
, u64
*end
)
99 dots
= strstr(range
, "..");
101 const char *rest
= dots
+ 2;
110 if (parse_u64(rest
, end
))
117 if (parse_u64(range
, start
))
121 if (*start
>= *end
) {
122 fprintf(stderr
, "Range %llu..%llu doesn't make "
123 "sense\n", (unsigned long long)*start
,
124 (unsigned long long)*end
);
135 static int parse_filters(char *filters
, struct btrfs_balance_args
*args
)
139 char *save_ptr
= NULL
; /* Satisfy static checkers */
144 for (this_char
= strtok_r(filters
, ",", &save_ptr
);
146 this_char
= strtok_r(NULL
, ",", &save_ptr
)) {
147 if ((value
= strchr(this_char
, '=')) != NULL
)
149 if (!strcmp(this_char
, "profiles")) {
150 if (!value
|| !*value
) {
151 fprintf(stderr
, "the profiles filter requires "
155 if (parse_profiles(value
, &args
->profiles
)) {
156 fprintf(stderr
, "Invalid profiles argument\n");
159 args
->flags
|= BTRFS_BALANCE_ARGS_PROFILES
;
160 } else if (!strcmp(this_char
, "usage")) {
161 if (!value
|| !*value
) {
162 fprintf(stderr
, "the usage filter requires "
166 if (parse_u64(value
, &args
->usage
) ||
168 fprintf(stderr
, "Invalid usage argument: %s\n",
172 args
->flags
|= BTRFS_BALANCE_ARGS_USAGE
;
173 } else if (!strcmp(this_char
, "devid")) {
174 if (!value
|| !*value
) {
175 fprintf(stderr
, "the devid filter requires "
179 if (parse_u64(value
, &args
->devid
) ||
181 fprintf(stderr
, "Invalid devid argument: %s\n",
185 args
->flags
|= BTRFS_BALANCE_ARGS_DEVID
;
186 } else if (!strcmp(this_char
, "drange")) {
187 if (!value
|| !*value
) {
188 fprintf(stderr
, "the drange filter requires "
192 if (parse_range(value
, &args
->pstart
, &args
->pend
)) {
193 fprintf(stderr
, "Invalid drange argument\n");
196 args
->flags
|= BTRFS_BALANCE_ARGS_DRANGE
;
197 } else if (!strcmp(this_char
, "vrange")) {
198 if (!value
|| !*value
) {
199 fprintf(stderr
, "the vrange filter requires "
203 if (parse_range(value
, &args
->vstart
, &args
->vend
)) {
204 fprintf(stderr
, "Invalid vrange argument\n");
207 args
->flags
|= BTRFS_BALANCE_ARGS_VRANGE
;
208 } else if (!strcmp(this_char
, "convert")) {
209 if (!value
|| !*value
) {
210 fprintf(stderr
, "the convert option requires "
214 if (parse_one_profile(value
, &args
->target
)) {
215 fprintf(stderr
, "Invalid convert argument\n");
218 args
->flags
|= BTRFS_BALANCE_ARGS_CONVERT
;
219 } else if (!strcmp(this_char
, "soft")) {
220 args
->flags
|= BTRFS_BALANCE_ARGS_SOFT
;
222 fprintf(stderr
, "Unrecognized balance option '%s'\n",
231 static void dump_balance_args(struct btrfs_balance_args
*args
)
233 if (args
->flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
234 printf("converting, target=%llu, soft is %s",
235 (unsigned long long)args
->target
,
236 (args
->flags
& BTRFS_BALANCE_ARGS_SOFT
) ? "on" : "off");
241 if (args
->flags
& BTRFS_BALANCE_ARGS_PROFILES
)
242 printf(", profiles=%llu", (unsigned long long)args
->profiles
);
243 if (args
->flags
& BTRFS_BALANCE_ARGS_USAGE
)
244 printf(", usage=%llu", (unsigned long long)args
->usage
);
245 if (args
->flags
& BTRFS_BALANCE_ARGS_DEVID
)
246 printf(", devid=%llu", (unsigned long long)args
->devid
);
247 if (args
->flags
& BTRFS_BALANCE_ARGS_DRANGE
)
248 printf(", drange=%llu..%llu",
249 (unsigned long long)args
->pstart
,
250 (unsigned long long)args
->pend
);
251 if (args
->flags
& BTRFS_BALANCE_ARGS_VRANGE
)
252 printf(", vrange=%llu..%llu",
253 (unsigned long long)args
->vstart
,
254 (unsigned long long)args
->vend
);
259 static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args
*args
)
261 printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
262 (unsigned long long)args
->flags
, (unsigned long long)args
->state
,
263 (args
->flags
& BTRFS_BALANCE_FORCE
) ? "on" : "off");
264 if (args
->flags
& BTRFS_BALANCE_DATA
) {
265 printf(" DATA (flags 0x%llx): ",
266 (unsigned long long)args
->data
.flags
);
267 dump_balance_args(&args
->data
);
269 if (args
->flags
& BTRFS_BALANCE_METADATA
) {
270 printf(" METADATA (flags 0x%llx): ",
271 (unsigned long long)args
->meta
.flags
);
272 dump_balance_args(&args
->meta
);
274 if (args
->flags
& BTRFS_BALANCE_SYSTEM
) {
275 printf(" SYSTEM (flags 0x%llx): ",
276 (unsigned long long)args
->sys
.flags
);
277 dump_balance_args(&args
->sys
);
281 static int do_balance_v1(int fd
)
283 struct btrfs_ioctl_vol_args args
;
286 memset(&args
, 0, sizeof(args
));
287 ret
= ioctl(fd
, BTRFS_IOC_BALANCE
, &args
);
291 static int do_balance(const char *path
, struct btrfs_ioctl_balance_args
*args
,
297 DIR *dirstream
= NULL
;
299 fd
= open_file_or_dir(path
, &dirstream
);
301 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
305 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_V2
, args
);
310 * older kernels don't have the new balance ioctl, try the
311 * old one. But, the old one doesn't know any filters, so
312 * don't fall back if they tried to use the fancy new things
314 if (e
== ENOTTY
&& nofilters
) {
315 ret
= do_balance_v1(fd
);
321 if (e
== ECANCELED
) {
322 if (args
->state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
323 fprintf(stderr
, "balance paused by user\n");
324 if (args
->state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
325 fprintf(stderr
, "balance canceled by user\n");
328 fprintf(stderr
, "ERROR: error during balancing '%s' "
329 "- %s\n", path
, strerror(e
));
330 if (e
!= EINPROGRESS
)
331 fprintf(stderr
, "There may be more info in "
332 "syslog - try dmesg | tail\n");
336 printf("Done, had to relocate %llu out of %llu chunks\n",
337 (unsigned long long)args
->stat
.completed
,
338 (unsigned long long)args
->stat
.considered
);
343 close_file_or_dir(fd
, dirstream
);
347 static const char * const cmd_balance_start_usage
[] = {
348 "btrfs [filesystem] balance start [options] <path>",
349 "Balance chunks across the devices",
350 "Balance and/or convert (change allocation profile of) chunks that",
351 "passed all filters in a comma-separated list of filters for a",
352 "particular chunk type. If filter list is not given balance all",
353 "chunks of that type. In case none of the -d, -m or -s options is",
354 "given balance all chunks in a filesystem.",
356 "-d[filters] act on data chunks",
357 "-m[filters] act on metadata chunks",
358 "-s[filters] act on system chunks (only under -f)",
360 "-f force reducing of metadata integrity",
364 static int cmd_balance_start(int argc
, char **argv
)
366 struct btrfs_ioctl_balance_args args
;
367 struct btrfs_balance_args
*ptrs
[] = { &args
.data
, &args
.sys
,
374 memset(&args
, 0, sizeof(args
));
379 static struct option longopts
[] = {
380 { "data", optional_argument
, NULL
, 'd'},
381 { "metadata", optional_argument
, NULL
, 'm' },
382 { "system", optional_argument
, NULL
, 's' },
383 { "force", no_argument
, NULL
, 'f' },
384 { "verbose", no_argument
, NULL
, 'v' },
385 { NULL
, no_argument
, NULL
, 0 },
388 int opt
= getopt_long(argc
, argv
, "d::s::m::fv", longopts
,
396 args
.flags
|= BTRFS_BALANCE_DATA
;
398 if (parse_filters(optarg
, &args
.data
))
403 args
.flags
|= BTRFS_BALANCE_SYSTEM
;
405 if (parse_filters(optarg
, &args
.sys
))
410 args
.flags
|= BTRFS_BALANCE_METADATA
;
412 if (parse_filters(optarg
, &args
.meta
))
422 usage(cmd_balance_start_usage
);
426 if (check_argc_exact(argc
- optind
, 1))
427 usage(cmd_balance_start_usage
);
430 * allow -s only under --force, otherwise do with system chunks
431 * the same thing we were ordered to do with meta chunks
433 if (args
.flags
& BTRFS_BALANCE_SYSTEM
) {
436 "Refusing to explicitly operate on system chunks.\n"
437 "Pass --force if you really want to do that.\n");
440 } else if (args
.flags
& BTRFS_BALANCE_METADATA
) {
441 args
.flags
|= BTRFS_BALANCE_SYSTEM
;
442 memcpy(&args
.sys
, &args
.meta
,
443 sizeof(struct btrfs_balance_args
));
447 /* relocate everything - no filters */
448 args
.flags
|= BTRFS_BALANCE_TYPE_MASK
;
451 /* drange makes sense only when devid is set */
452 for (i
= 0; ptrs
[i
]; i
++) {
453 if ((ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
454 !(ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_DEVID
)) {
455 fprintf(stderr
, "drange filter can be used only if "
456 "devid filter is used\n");
461 /* soft makes sense only when convert for corresponding type is set */
462 for (i
= 0; ptrs
[i
]; i
++) {
463 if ((ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
464 !(ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
465 fprintf(stderr
, "'soft' option can be used only if "
466 "changing profiles\n");
472 args
.flags
|= BTRFS_BALANCE_FORCE
;
474 dump_ioctl_balance_args(&args
);
476 return do_balance(argv
[optind
], &args
, nofilters
);
479 static const char * const cmd_balance_pause_usage
[] = {
480 "btrfs [filesystem] balance pause <path>",
481 "Pause running balance",
485 static int cmd_balance_pause(int argc
, char **argv
)
491 DIR *dirstream
= NULL
;
493 if (check_argc_exact(argc
, 2))
494 usage(cmd_balance_pause_usage
);
498 fd
= open_file_or_dir(path
, &dirstream
);
500 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
504 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_CTL
, BTRFS_BALANCE_CTL_PAUSE
);
506 close_file_or_dir(fd
, dirstream
);
509 fprintf(stderr
, "ERROR: balance pause on '%s' failed - %s\n",
510 path
, (e
== ENOTCONN
) ? "Not running" : strerror(e
));
520 static const char * const cmd_balance_cancel_usage
[] = {
521 "btrfs [filesystem] balance cancel <path>",
522 "Cancel running or paused balance",
526 static int cmd_balance_cancel(int argc
, char **argv
)
532 DIR *dirstream
= NULL
;
534 if (check_argc_exact(argc
, 2))
535 usage(cmd_balance_cancel_usage
);
539 fd
= open_file_or_dir(path
, &dirstream
);
541 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
545 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_CTL
, BTRFS_BALANCE_CTL_CANCEL
);
547 close_file_or_dir(fd
, dirstream
);
550 fprintf(stderr
, "ERROR: balance cancel on '%s' failed - %s\n",
551 path
, (e
== ENOTCONN
) ? "Not in progress" : strerror(e
));
561 static const char * const cmd_balance_resume_usage
[] = {
562 "btrfs [filesystem] balance resume <path>",
563 "Resume interrupted balance",
567 static int cmd_balance_resume(int argc
, char **argv
)
569 struct btrfs_ioctl_balance_args args
;
571 DIR *dirstream
= NULL
;
576 if (check_argc_exact(argc
, 2))
577 usage(cmd_balance_resume_usage
);
581 fd
= open_file_or_dir(path
, &dirstream
);
583 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
587 memset(&args
, 0, sizeof(args
));
588 args
.flags
|= BTRFS_BALANCE_RESUME
;
590 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_V2
, &args
);
592 close_file_or_dir(fd
, dirstream
);
595 if (e
== ECANCELED
) {
596 if (args
.state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
597 fprintf(stderr
, "balance paused by user\n");
598 if (args
.state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
599 fprintf(stderr
, "balance canceled by user\n");
600 } else if (e
== ENOTCONN
|| e
== EINPROGRESS
) {
601 fprintf(stderr
, "ERROR: balance resume on '%s' "
602 "failed - %s\n", path
,
603 (e
== ENOTCONN
) ? "Not in progress" :
611 "ERROR: error during balancing '%s' - %s\n"
612 "There may be more info in syslog - try dmesg | tail\n", path
, strerror(e
));
616 printf("Done, had to relocate %llu out of %llu chunks\n",
617 (unsigned long long)args
.stat
.completed
,
618 (unsigned long long)args
.stat
.considered
);
624 static const char * const cmd_balance_status_usage
[] = {
625 "btrfs [filesystem] balance status [-v] <path>",
626 "Show status of running or paused balance",
632 /* Checks the status of the balance if any
634 * 2 : Error failed to know if there is any pending balance
635 * 1 : Successful to know status of a pending balance
636 * 0 : When there is no pending balance or completed
638 static int cmd_balance_status(int argc
, char **argv
)
640 struct btrfs_ioctl_balance_args args
;
642 DIR *dirstream
= NULL
;
651 static struct option longopts
[] = {
652 { "verbose", no_argument
, NULL
, 'v' },
653 { NULL
, no_argument
, NULL
, 0}
656 int opt
= getopt_long(argc
, argv
, "v", longopts
, &longindex
);
665 usage(cmd_balance_status_usage
);
669 if (check_argc_exact(argc
- optind
, 1))
670 usage(cmd_balance_status_usage
);
674 fd
= open_file_or_dir(path
, &dirstream
);
676 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
680 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_PROGRESS
, &args
);
682 close_file_or_dir(fd
, dirstream
);
686 printf("No balance found on '%s'\n", path
);
689 fprintf(stderr
, "ERROR: balance status on '%s' failed - %s\n",
694 if (args
.state
& BTRFS_BALANCE_STATE_RUNNING
) {
695 printf("Balance on '%s' is running", path
);
696 if (args
.state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
697 printf(", cancel requested\n");
698 else if (args
.state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
699 printf(", pause requested\n");
703 printf("Balance on '%s' is paused\n", path
);
706 printf("%llu out of about %llu chunks balanced (%llu considered), "
707 "%3.f%% left\n", (unsigned long long)args
.stat
.completed
,
708 (unsigned long long)args
.stat
.expected
,
709 (unsigned long long)args
.stat
.considered
,
710 100 * (1 - (float)args
.stat
.completed
/args
.stat
.expected
));
713 dump_ioctl_balance_args(&args
);
718 const struct cmd_group balance_cmd_group
= {
719 balance_cmd_group_usage
, balance_cmd_group_info
, {
720 { "start", cmd_balance_start
, cmd_balance_start_usage
, NULL
, 0 },
721 { "pause", cmd_balance_pause
, cmd_balance_pause_usage
, NULL
, 0 },
722 { "cancel", cmd_balance_cancel
, cmd_balance_cancel_usage
, NULL
, 0 },
723 { "resume", cmd_balance_resume
, cmd_balance_resume_usage
, NULL
, 0 },
724 { "status", cmd_balance_status
, cmd_balance_status_usage
, NULL
, 0 },
729 int cmd_balance(int argc
, char **argv
)
732 /* old 'btrfs filesystem balance <path>' syntax */
733 struct btrfs_ioctl_balance_args args
;
735 memset(&args
, 0, sizeof(args
));
736 args
.flags
|= BTRFS_BALANCE_TYPE_MASK
;
738 return do_balance(argv
[1], &args
, 1);
741 return handle_command_group(&balance_cmd_group
, argc
, argv
);