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_v1(int fd
)
275 struct btrfs_ioctl_vol_args args
;
278 memset(&args
, 0, sizeof(args
));
279 ret
= ioctl(fd
, BTRFS_IOC_BALANCE
, &args
);
283 static int do_balance(const char *path
, struct btrfs_ioctl_balance_args
*args
,
290 fd
= open_file_or_dir(path
);
292 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
296 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_V2
, args
);
301 * older kernels don't have the new balance ioctl, try the
302 * old one. But, the old one doesn't know any filters, so
303 * don't fall back if they tried to use the fancy new things
305 if (e
== ENOTTY
&& nofilters
) {
306 ret
= do_balance_v1(fd
);
312 if (e
== ECANCELED
) {
313 if (args
->state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
314 fprintf(stderr
, "balance paused by user\n");
315 if (args
->state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
316 fprintf(stderr
, "balance canceled by user\n");
319 fprintf(stderr
, "ERROR: error during balancing '%s' "
320 "- %s\n", path
, strerror(e
));
321 if (e
!= EINPROGRESS
)
322 fprintf(stderr
, "There may be more info in "
323 "syslog - try dmesg | tail\n");
327 printf("Done, had to relocate %llu out of %llu chunks\n",
328 (unsigned long long)args
->stat
.completed
,
329 (unsigned long long)args
->stat
.considered
);
338 static const char * const cmd_balance_start_usage
[] = {
339 "btrfs [filesystem] balance start [options] <path>",
340 "Balance chunks across the devices",
341 "Balance and/or convert (change allocation profile of) chunks that",
342 "passed all filters in a comma-separated list of filters for a",
343 "particular chunk type. If filter list is not given balance all",
344 "chunks of that type. In case none of the -d, -m or -s options is",
345 "given balance all chunks in a filesystem.",
347 "-d[filters] act on data chunks",
348 "-m[filters] act on metadata chunks",
349 "-s[filetrs] act on system chunks (only under -f)",
351 "-f force reducing of metadata integrity",
355 static int cmd_balance_start(int argc
, char **argv
)
357 struct btrfs_ioctl_balance_args args
;
358 struct btrfs_balance_args
*ptrs
[] = { &args
.data
, &args
.sys
,
365 memset(&args
, 0, sizeof(args
));
370 static struct option longopts
[] = {
371 { "data", optional_argument
, NULL
, 'd'},
372 { "metadata", optional_argument
, NULL
, 'm' },
373 { "system", optional_argument
, NULL
, 's' },
374 { "force", no_argument
, NULL
, 'f' },
375 { "verbose", no_argument
, NULL
, 'v' },
379 int opt
= getopt_long(argc
, argv
, "d::s::m::fv", longopts
,
387 args
.flags
|= BTRFS_BALANCE_DATA
;
389 if (parse_filters(optarg
, &args
.data
))
394 args
.flags
|= BTRFS_BALANCE_SYSTEM
;
396 if (parse_filters(optarg
, &args
.sys
))
401 args
.flags
|= BTRFS_BALANCE_METADATA
;
403 if (parse_filters(optarg
, &args
.meta
))
413 usage(cmd_balance_start_usage
);
417 if (check_argc_exact(argc
- optind
, 1))
418 usage(cmd_balance_start_usage
);
421 * allow -s only under --force, otherwise do with system chunks
422 * the same thing we were ordered to do with meta chunks
424 if (args
.flags
& BTRFS_BALANCE_SYSTEM
) {
427 "Refusing to explicitly operate on system chunks.\n"
428 "Pass --force if you really want to do that.\n");
431 } else if (args
.flags
& BTRFS_BALANCE_METADATA
) {
432 args
.flags
|= BTRFS_BALANCE_SYSTEM
;
433 memcpy(&args
.sys
, &args
.meta
,
434 sizeof(struct btrfs_balance_args
));
438 /* relocate everything - no filters */
439 args
.flags
|= BTRFS_BALANCE_TYPE_MASK
;
442 /* drange makes sense only when devid is set */
443 for (i
= 0; ptrs
[i
]; i
++) {
444 if ((ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
445 !(ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_DEVID
)) {
446 fprintf(stderr
, "drange filter can be used only if "
447 "devid filter is used\n");
452 /* soft makes sense only when convert for corresponding type is set */
453 for (i
= 0; ptrs
[i
]; i
++) {
454 if ((ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
455 !(ptrs
[i
]->flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
456 fprintf(stderr
, "'soft' option can be used only if "
457 "changing profiles\n");
463 args
.flags
|= BTRFS_BALANCE_FORCE
;
465 dump_ioctl_balance_args(&args
);
467 return do_balance(argv
[optind
], &args
, nofilters
);
470 static const char * const cmd_balance_pause_usage
[] = {
471 "btrfs [filesystem] balance pause <path>",
472 "Pause running balance",
476 static int cmd_balance_pause(int argc
, char **argv
)
483 if (check_argc_exact(argc
, 2))
484 usage(cmd_balance_pause_usage
);
488 fd
= open_file_or_dir(path
);
490 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
494 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_CTL
, BTRFS_BALANCE_CTL_PAUSE
);
499 fprintf(stderr
, "ERROR: balance pause on '%s' failed - %s\n",
500 path
, (e
== ENOTCONN
) ? "Not running" : strerror(e
));
507 static const char * const cmd_balance_cancel_usage
[] = {
508 "btrfs [filesystem] balance cancel <path>",
509 "Cancel running or paused balance",
513 static int cmd_balance_cancel(int argc
, char **argv
)
520 if (check_argc_exact(argc
, 2))
521 usage(cmd_balance_cancel_usage
);
525 fd
= open_file_or_dir(path
);
527 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
531 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_CTL
, BTRFS_BALANCE_CTL_CANCEL
);
536 fprintf(stderr
, "ERROR: balance cancel on '%s' failed - %s\n",
537 path
, (e
== ENOTCONN
) ? "Not in progress" : strerror(e
));
544 static const char * const cmd_balance_resume_usage
[] = {
545 "btrfs [filesystem] balance resume <path>",
546 "Resume interrupted balance",
550 static int cmd_balance_resume(int argc
, char **argv
)
552 struct btrfs_ioctl_balance_args args
;
558 if (check_argc_exact(argc
, 2))
559 usage(cmd_balance_resume_usage
);
563 fd
= open_file_or_dir(path
);
565 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
569 memset(&args
, 0, sizeof(args
));
570 args
.flags
|= BTRFS_BALANCE_RESUME
;
572 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_V2
, &args
);
577 if (e
== ECANCELED
) {
578 if (args
.state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
579 fprintf(stderr
, "balance paused by user\n");
580 if (args
.state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
581 fprintf(stderr
, "balance canceled by user\n");
582 } else if (e
== ENOTCONN
|| e
== EINPROGRESS
) {
583 fprintf(stderr
, "ERROR: balance resume on '%s' "
584 "failed - %s\n", path
,
585 (e
== ENOTCONN
) ? "Not in progress" :
590 "ERROR: error during balancing '%s' - %s\n"
591 "There may be more info in syslog - try dmesg | tail\n", path
, strerror(e
));
595 printf("Done, had to relocate %llu out of %llu chunks\n",
596 (unsigned long long)args
.stat
.completed
,
597 (unsigned long long)args
.stat
.considered
);
603 static const char * const cmd_balance_status_usage
[] = {
604 "btrfs [filesystem] balance status [-v] <path>",
605 "Show status of running or paused balance",
611 static int cmd_balance_status(int argc
, char **argv
)
613 struct btrfs_ioctl_balance_args args
;
623 static struct option longopts
[] = {
624 { "verbose", no_argument
, NULL
, 'v' },
628 int opt
= getopt_long(argc
, argv
, "v", longopts
, &longindex
);
637 usage(cmd_balance_status_usage
);
641 if (check_argc_exact(argc
- optind
, 1))
642 usage(cmd_balance_status_usage
);
646 fd
= open_file_or_dir(path
);
648 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
652 ret
= ioctl(fd
, BTRFS_IOC_BALANCE_PROGRESS
, &args
);
657 fprintf(stderr
, "ERROR: balance status on '%s' failed - %s\n",
658 path
, (e
== ENOTCONN
) ? "Not in progress" : strerror(e
));
662 if (args
.state
& BTRFS_BALANCE_STATE_RUNNING
) {
663 printf("Balance on '%s' is running", path
);
664 if (args
.state
& BTRFS_BALANCE_STATE_CANCEL_REQ
)
665 printf(", cancel requested\n");
666 else if (args
.state
& BTRFS_BALANCE_STATE_PAUSE_REQ
)
667 printf(", pause requested\n");
671 printf("Balance on '%s' is paused\n", path
);
674 printf("%llu out of about %llu chunks balanced (%llu considered), "
675 "%3.f%% left\n", (unsigned long long)args
.stat
.completed
,
676 (unsigned long long)args
.stat
.expected
,
677 (unsigned long long)args
.stat
.considered
,
678 100 * (1 - (float)args
.stat
.completed
/args
.stat
.expected
));
681 dump_ioctl_balance_args(&args
);
686 const struct cmd_group balance_cmd_group
= {
687 balance_cmd_group_usage
, balance_cmd_group_info
, {
688 { "start", cmd_balance_start
, cmd_balance_start_usage
, NULL
, 0 },
689 { "pause", cmd_balance_pause
, cmd_balance_pause_usage
, NULL
, 0 },
690 { "cancel", cmd_balance_cancel
, cmd_balance_cancel_usage
, NULL
, 0 },
691 { "resume", cmd_balance_resume
, cmd_balance_resume_usage
, NULL
, 0 },
692 { "status", cmd_balance_status
, cmd_balance_status_usage
, NULL
, 0 },
697 int cmd_balance(int argc
, char **argv
)
700 /* old 'btrfs filesystem balance <path>' syntax */
701 struct btrfs_ioctl_balance_args args
;
703 memset(&args
, 0, sizeof(args
));
704 args
.flags
|= BTRFS_BALANCE_TYPE_MASK
;
706 return do_balance(argv
[1], &args
, 1);
709 return handle_command_group(&balance_cmd_group
, argc
, argv
);