2012-02-03 19:02:30 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2012-02-03 19:02:30 +00:00
|
|
|
#include <getopt.h>
|
2012-02-03 19:02:30 +00:00
|
|
|
#include <sys/ioctl.h>
|
2016-06-21 15:16:59 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-02-03 19:02:30 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "kerncompat.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2012-02-03 19:02:30 +00:00
|
|
|
#include "ioctl.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/volumes.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "cmds/commands.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/utils.h"
|
2021-09-06 13:57:27 +00:00
|
|
|
#include "common/parse-utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
static const char * const balance_cmd_group_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance <command> [options] <path>",
|
|
|
|
"btrfs balance <path>",
|
2012-02-08 15:45:54 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
static int parse_one_profile(const char *profile, u64 *flags)
|
|
|
|
{
|
2021-09-06 13:57:27 +00:00
|
|
|
int ret;
|
|
|
|
u64 tmp = 0;
|
2021-09-03 19:36:35 +00:00
|
|
|
|
2021-09-06 13:57:27 +00:00
|
|
|
ret = parse_bg_profile(profile, &tmp);
|
|
|
|
if (ret) {
|
|
|
|
error("unknown profile: %s", profile);
|
|
|
|
return 1;
|
2021-09-03 19:36:35 +00:00
|
|
|
}
|
2021-09-06 13:57:27 +00:00
|
|
|
if (tmp == 0)
|
|
|
|
tmp = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
|
|
|
|
*flags |= tmp;
|
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_profiles(char *profiles, u64 *flags)
|
|
|
|
{
|
|
|
|
char *this_char;
|
2013-02-26 21:50:22 +00:00
|
|
|
char *save_ptr = NULL; /* Satisfy static checkers */
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
for (this_char = strtok_r(profiles, "|", &save_ptr);
|
|
|
|
this_char != NULL;
|
|
|
|
this_char = strtok_r(NULL, "|", &save_ptr)) {
|
|
|
|
if (parse_one_profile(this_char, flags))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-19 14:31:14 +00:00
|
|
|
__attribute__ ((unused))
|
|
|
|
static void print_range(u64 start, u64 end)
|
|
|
|
{
|
|
|
|
if (start)
|
|
|
|
printf("%llu", (unsigned long long)start);
|
|
|
|
printf("..");
|
|
|
|
if (end != (u64)-1)
|
|
|
|
printf("%llu", (unsigned long long)end);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((unused))
|
|
|
|
static void print_range_u32(u32 start, u32 end)
|
|
|
|
{
|
|
|
|
if (start)
|
|
|
|
printf("%u", start);
|
|
|
|
printf("..");
|
|
|
|
if (end != (u32)-1)
|
|
|
|
printf("%u", end);
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
static int parse_filters(char *filters, struct btrfs_balance_args *args)
|
|
|
|
{
|
|
|
|
char *this_char;
|
|
|
|
char *value;
|
2013-02-26 21:50:22 +00:00
|
|
|
char *save_ptr = NULL; /* Satisfy static checkers */
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
if (!filters)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (this_char = strtok_r(filters, ",", &save_ptr);
|
|
|
|
this_char != NULL;
|
|
|
|
this_char = strtok_r(NULL, ",", &save_ptr)) {
|
|
|
|
if ((value = strchr(this_char, '=')) != NULL)
|
|
|
|
*value++ = 0;
|
|
|
|
if (!strcmp(this_char, "profiles")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the profiles filter requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parse_profiles(value, &args->profiles)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid profiles argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_PROFILES;
|
|
|
|
} else if (!strcmp(this_char, "usage")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the usage filter requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-22 08:26:10 +00:00
|
|
|
if (parse_u64(value, &args->usage)) {
|
|
|
|
if (parse_range_u32(value, &args->usage_min,
|
|
|
|
&args->usage_max)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid usage argument: %s",
|
2015-10-22 08:26:10 +00:00
|
|
|
value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (args->usage_max > 100) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid usage argument: %s",
|
2015-10-22 08:26:10 +00:00
|
|
|
value);
|
|
|
|
}
|
|
|
|
args->flags &= ~BTRFS_BALANCE_ARGS_USAGE;
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_USAGE_RANGE;
|
|
|
|
} else {
|
|
|
|
if (args->usage > 100) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid usage argument: %s",
|
2015-10-22 08:26:10 +00:00
|
|
|
value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags &= ~BTRFS_BALANCE_ARGS_USAGE_RANGE;
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_USAGE;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_USAGE;
|
|
|
|
} else if (!strcmp(this_char, "devid")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the devid filter requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-01-11 10:01:47 +00:00
|
|
|
if (parse_u64(value, &args->devid) || args->devid == 0) {
|
|
|
|
error("invalid devid argument: %s", value);
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_DEVID;
|
|
|
|
} else if (!strcmp(this_char, "drange")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the drange filter requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-11 22:29:31 +00:00
|
|
|
if (parse_range_strict(value, &args->pstart, &args->pend)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid drange argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_DRANGE;
|
|
|
|
} else if (!strcmp(this_char, "vrange")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the vrange filter requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-11 22:29:31 +00:00
|
|
|
if (parse_range_strict(value, &args->vstart, &args->vend)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid vrange argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_VRANGE;
|
|
|
|
} else if (!strcmp(this_char, "convert")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the convert option requires an argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parse_one_profile(value, &args->target)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid convert argument");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_CONVERT;
|
|
|
|
} else if (!strcmp(this_char, "soft")) {
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_SOFT;
|
2014-05-07 15:38:00 +00:00
|
|
|
} else if (!strcmp(this_char, "limit")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the limit filter requires an argument");
|
2014-05-07 15:38:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parse_u64(value, &args->limit)) {
|
2015-10-11 22:52:43 +00:00
|
|
|
if (parse_range_u32(value, &args->limit_min,
|
|
|
|
&args->limit_max)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("Invalid limit argument: %s",
|
2015-10-11 22:52:43 +00:00
|
|
|
value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT;
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_LIMIT_RANGE;
|
|
|
|
} else {
|
|
|
|
args->flags &= ~BTRFS_BALANCE_ARGS_LIMIT_RANGE;
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_LIMIT;
|
2014-05-07 15:38:00 +00:00
|
|
|
}
|
2015-09-28 22:33:04 +00:00
|
|
|
} else if (!strcmp(this_char, "stripes")) {
|
|
|
|
if (!value || !*value) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("the stripes filter requires an argument");
|
2015-09-28 22:33:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parse_range_u32(value, &args->stripes_min,
|
|
|
|
&args->stripes_max)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("invalid stripes argument");
|
2015-09-28 22:33:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
args->flags |= BTRFS_BALANCE_ARGS_STRIPES_RANGE;
|
2012-02-03 19:02:30 +00:00
|
|
|
} else {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("unrecognized balance option: %s", this_char);
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_balance_args(struct btrfs_balance_args *args)
|
|
|
|
{
|
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) {
|
|
|
|
printf("converting, target=%llu, soft is %s",
|
|
|
|
(unsigned long long)args->target,
|
|
|
|
(args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off");
|
|
|
|
} else {
|
|
|
|
printf("balancing");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_PROFILES)
|
|
|
|
printf(", profiles=%llu", (unsigned long long)args->profiles);
|
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_USAGE)
|
|
|
|
printf(", usage=%llu", (unsigned long long)args->usage);
|
2015-10-22 08:19:38 +00:00
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
|
|
|
|
printf(", usage=");
|
|
|
|
print_range_u32(args->usage_min, args->usage_max);
|
|
|
|
}
|
2012-02-03 19:02:30 +00:00
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_DEVID)
|
|
|
|
printf(", devid=%llu", (unsigned long long)args->devid);
|
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_DRANGE)
|
|
|
|
printf(", drange=%llu..%llu",
|
|
|
|
(unsigned long long)args->pstart,
|
|
|
|
(unsigned long long)args->pend);
|
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_VRANGE)
|
|
|
|
printf(", vrange=%llu..%llu",
|
|
|
|
(unsigned long long)args->vstart,
|
|
|
|
(unsigned long long)args->vend);
|
2014-05-07 15:38:00 +00:00
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_LIMIT)
|
|
|
|
printf(", limit=%llu", (unsigned long long)args->limit);
|
2015-10-11 22:34:58 +00:00
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
|
|
|
|
printf(", limit=");
|
|
|
|
print_range_u32(args->limit_min, args->limit_max);
|
|
|
|
}
|
2015-09-28 22:33:04 +00:00
|
|
|
if (args->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
|
|
|
|
printf(", stripes=");
|
|
|
|
print_range_u32(args->stripes_min, args->stripes_max);
|
|
|
|
}
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args)
|
|
|
|
{
|
|
|
|
printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n",
|
|
|
|
(unsigned long long)args->flags, (unsigned long long)args->state,
|
|
|
|
(args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off");
|
|
|
|
if (args->flags & BTRFS_BALANCE_DATA) {
|
|
|
|
printf(" DATA (flags 0x%llx): ",
|
|
|
|
(unsigned long long)args->data.flags);
|
|
|
|
dump_balance_args(&args->data);
|
|
|
|
}
|
|
|
|
if (args->flags & BTRFS_BALANCE_METADATA) {
|
|
|
|
printf(" METADATA (flags 0x%llx): ",
|
|
|
|
(unsigned long long)args->meta.flags);
|
|
|
|
dump_balance_args(&args->meta);
|
|
|
|
}
|
|
|
|
if (args->flags & BTRFS_BALANCE_SYSTEM) {
|
|
|
|
printf(" SYSTEM (flags 0x%llx): ",
|
|
|
|
(unsigned long long)args->sys.flags);
|
|
|
|
dump_balance_args(&args->sys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:42:02 +00:00
|
|
|
static int do_balance_v1(int fd)
|
|
|
|
{
|
|
|
|
struct btrfs_ioctl_vol_args args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE, &args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-02 08:26:29 +00:00
|
|
|
enum {
|
|
|
|
BALANCE_START_FILTERS = 1 << 0,
|
|
|
|
BALANCE_START_NOWARN = 1 << 1
|
|
|
|
};
|
|
|
|
|
2012-02-05 15:42:02 +00:00
|
|
|
static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
|
2020-11-05 02:38:00 +00:00
|
|
|
unsigned flags, bool enqueue)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2015-10-12 13:22:56 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-10 02:28:29 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_BALANCE, enqueue);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (ret < 0)
|
|
|
|
error("unable to check status of exclusive operation: %m");
|
2020-08-25 15:03:37 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
|
|
|
|
if (ret < 0) {
|
2012-02-05 15:42:02 +00:00
|
|
|
/*
|
|
|
|
* older kernels don't have the new balance ioctl, try the
|
|
|
|
* old one. But, the old one doesn't know any filters, so
|
|
|
|
* don't fall back if they tried to use the fancy new things
|
|
|
|
*/
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno == ENOTTY && !(flags & BALANCE_START_FILTERS)) {
|
2012-02-05 15:42:02 +00:00
|
|
|
ret = do_balance_v1(fd);
|
|
|
|
if (ret == 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno == ECANCELED) {
|
2012-02-03 19:02:30 +00:00
|
|
|
if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ)
|
|
|
|
fprintf(stderr, "balance paused by user\n");
|
|
|
|
if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ)
|
|
|
|
fprintf(stderr, "balance canceled by user\n");
|
2012-02-05 15:42:02 +00:00
|
|
|
ret = 0;
|
2012-02-03 19:02:30 +00:00
|
|
|
} else {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("error during balancing '%s': %m", path);
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno != EINPROGRESS)
|
2016-01-11 10:01:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"There may be more info in syslog - try dmesg | tail\n");
|
2013-09-10 02:28:29 +00:00
|
|
|
ret = 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-12-18 16:01:44 +00:00
|
|
|
} else if (ret > 0) {
|
|
|
|
error("balance: %s", btrfs_err_str(ret));
|
2012-02-03 19:02:30 +00:00
|
|
|
} else {
|
2020-06-11 17:41:19 +00:00
|
|
|
pr_verbose(MUST_LOG,
|
|
|
|
"Done, had to relocate %llu out of %llu chunks\n",
|
|
|
|
(unsigned long long)args->stat.completed,
|
|
|
|
(unsigned long long)args->stat.considered);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 15:42:02 +00:00
|
|
|
out:
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2012-02-05 15:42:02 +00:00
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
static const char * const cmd_balance_start_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance start [options] <path>",
|
2012-02-03 19:02:30 +00:00
|
|
|
"Balance chunks across the devices",
|
|
|
|
"Balance and/or convert (change allocation profile of) chunks that",
|
|
|
|
"passed all filters in a comma-separated list of filters for a",
|
|
|
|
"particular chunk type. If filter list is not given balance all",
|
|
|
|
"chunks of that type. In case none of the -d, -m or -s options is",
|
2016-05-02 08:26:29 +00:00
|
|
|
"given balance all chunks in a filesystem. This is potentially",
|
|
|
|
"long operation and the user is warned before this start, with",
|
|
|
|
"a delay to stop it.",
|
2012-02-03 19:02:30 +00:00
|
|
|
"",
|
|
|
|
"-d[filters] act on data chunks",
|
|
|
|
"-m[filters] act on metadata chunks",
|
2013-01-21 19:19:03 +00:00
|
|
|
"-s[filters] act on system chunks (only under -f)",
|
2021-03-03 15:40:53 +00:00
|
|
|
"-f force a reduction of metadata integrity, or",
|
|
|
|
" skip timeout when converting to RAID56 profiles",
|
2016-05-02 08:26:29 +00:00
|
|
|
"--full-balance do not print warning and do not delay start",
|
2016-07-26 17:30:39 +00:00
|
|
|
"--background|--bg",
|
|
|
|
" run the balance as a background process",
|
2020-11-05 02:38:00 +00:00
|
|
|
"--enqueue wait if there's another exclusive operation running,",
|
|
|
|
" otherwise continue",
|
2020-06-26 20:20:56 +00:00
|
|
|
"-v|--verbose deprecated, alias for global -v option",
|
2019-11-25 10:39:08 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_VERBOSE,
|
2020-06-11 17:41:19 +00:00
|
|
|
HELPINFO_INSERT_QUIET,
|
2012-02-03 19:02:30 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_start(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_balance_args args;
|
|
|
|
struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
|
|
|
|
&args.meta, NULL };
|
|
|
|
int force = 0;
|
2016-06-21 15:16:59 +00:00
|
|
|
int background = 0;
|
2020-11-05 02:38:00 +00:00
|
|
|
bool enqueue = false;
|
2016-05-02 08:26:29 +00:00
|
|
|
unsigned start_flags = 0;
|
2021-03-03 15:40:53 +00:00
|
|
|
bool raid56_warned = false;
|
2012-02-03 19:02:30 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2012-02-03 19:02:30 +00:00
|
|
|
while (1) {
|
2016-06-21 15:16:59 +00:00
|
|
|
enum { GETOPT_VAL_FULL_BALANCE = 256,
|
2020-11-05 02:38:00 +00:00
|
|
|
GETOPT_VAL_BACKGROUND = 257,
|
|
|
|
GETOPT_VAL_ENQUEUE };
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option longopts[] = {
|
2012-02-03 19:02:30 +00:00
|
|
|
{ "data", optional_argument, NULL, 'd'},
|
|
|
|
{ "metadata", optional_argument, NULL, 'm' },
|
|
|
|
{ "system", optional_argument, NULL, 's' },
|
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
2016-05-02 08:26:29 +00:00
|
|
|
{ "full-balance", no_argument, NULL,
|
|
|
|
GETOPT_VAL_FULL_BALANCE },
|
2016-06-21 15:16:59 +00:00
|
|
|
{ "background", no_argument, NULL,
|
|
|
|
GETOPT_VAL_BACKGROUND },
|
2016-07-26 17:30:39 +00:00
|
|
|
{ "bg", no_argument, NULL, GETOPT_VAL_BACKGROUND },
|
2020-11-05 02:38:00 +00:00
|
|
|
{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2012-02-03 19:02:30 +00:00
|
|
|
};
|
|
|
|
|
2015-04-08 15:33:55 +00:00
|
|
|
int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, NULL);
|
2012-02-03 19:02:30 +00:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
2016-05-02 08:26:29 +00:00
|
|
|
start_flags |= BALANCE_START_FILTERS;
|
2012-02-03 19:02:30 +00:00
|
|
|
args.flags |= BTRFS_BALANCE_DATA;
|
|
|
|
|
|
|
|
if (parse_filters(optarg, &args.data))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-05-02 08:26:29 +00:00
|
|
|
start_flags |= BALANCE_START_FILTERS;
|
2012-02-03 19:02:30 +00:00
|
|
|
args.flags |= BTRFS_BALANCE_SYSTEM;
|
|
|
|
|
|
|
|
if (parse_filters(optarg, &args.sys))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2016-05-02 08:26:29 +00:00
|
|
|
start_flags |= BALANCE_START_FILTERS;
|
2012-02-03 19:02:30 +00:00
|
|
|
args.flags |= BTRFS_BALANCE_METADATA;
|
|
|
|
|
|
|
|
if (parse_filters(optarg, &args.meta))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
2019-11-25 10:39:08 +00:00
|
|
|
bconf_be_verbose();
|
2012-02-03 19:02:30 +00:00
|
|
|
break;
|
2016-05-02 08:26:29 +00:00
|
|
|
case GETOPT_VAL_FULL_BALANCE:
|
|
|
|
start_flags |= BALANCE_START_NOWARN;
|
|
|
|
break;
|
2016-06-21 15:16:59 +00:00
|
|
|
case GETOPT_VAL_BACKGROUND:
|
|
|
|
background = 1;
|
|
|
|
break;
|
2020-11-05 02:38:00 +00:00
|
|
|
case GETOPT_VAL_ENQUEUE:
|
|
|
|
enqueue = true;
|
|
|
|
break;
|
2012-02-03 19:02:30 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* allow -s only under --force, otherwise do with system chunks
|
|
|
|
* the same thing we were ordered to do with meta chunks
|
|
|
|
*/
|
|
|
|
if (args.flags & BTRFS_BALANCE_SYSTEM) {
|
|
|
|
if (!force) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error(
|
|
|
|
"Refusing to explicitly operate on system chunks.\n"
|
|
|
|
"Pass --force if you really want to do that.");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (args.flags & BTRFS_BALANCE_METADATA) {
|
|
|
|
args.flags |= BTRFS_BALANCE_SYSTEM;
|
|
|
|
memcpy(&args.sys, &args.meta,
|
|
|
|
sizeof(struct btrfs_balance_args));
|
|
|
|
}
|
|
|
|
|
2016-05-02 08:26:29 +00:00
|
|
|
if (!(start_flags & BALANCE_START_FILTERS)) {
|
2012-02-03 19:02:30 +00:00
|
|
|
/* relocate everything - no filters */
|
|
|
|
args.flags |= BTRFS_BALANCE_TYPE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drange makes sense only when devid is set */
|
|
|
|
for (i = 0; ptrs[i]; i++) {
|
|
|
|
if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
|
|
|
|
!(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("drange filter must be used with devid filter");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* soft makes sense only when convert for corresponding type is set */
|
|
|
|
for (i = 0; ptrs[i]; i++) {
|
2020-08-25 17:13:29 +00:00
|
|
|
int delay = 10;
|
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) &&
|
|
|
|
!(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("'soft' option can be used only when converting profiles");
|
2012-02-03 19:02:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-08-25 17:13:29 +00:00
|
|
|
|
|
|
|
if (!(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT))
|
|
|
|
continue;
|
|
|
|
|
2021-09-03 19:41:13 +00:00
|
|
|
if (!(ptrs[i]->target & BTRFS_BLOCK_GROUP_RAID56_MASK))
|
2020-08-25 17:13:29 +00:00
|
|
|
continue;
|
|
|
|
|
2021-03-03 15:40:53 +00:00
|
|
|
if (raid56_warned)
|
2020-08-25 17:13:29 +00:00
|
|
|
continue;
|
|
|
|
|
2021-03-03 15:40:53 +00:00
|
|
|
raid56_warned = true;
|
2020-08-25 17:13:29 +00:00
|
|
|
printf("WARNING:\n\n");
|
|
|
|
printf("\tRAID5/6 support has known problems and is strongly discouraged\n");
|
|
|
|
printf("\tto be used besides testing or evaluation. It is recommended that\n");
|
|
|
|
printf("\tyou use one of the other RAID profiles.\n");
|
2021-03-03 15:40:53 +00:00
|
|
|
/*
|
|
|
|
* Override timeout by the --force option too, though it's
|
|
|
|
* otherwise used for allowing redundancy reduction.
|
|
|
|
*/
|
|
|
|
if (force) {
|
|
|
|
printf("\tSafety timeout skipped due to --force\n\n");
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-25 17:13:29 +00:00
|
|
|
printf("\tThe operation will continue in %d seconds.\n", delay);
|
|
|
|
printf("\tUse Ctrl-C to stop.\n");
|
|
|
|
while (delay) {
|
|
|
|
printf("%2d", delay--);
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
printf("\nStarting conversion to RAID5/6.\n");
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 23:14:34 +00:00
|
|
|
if (!(start_flags & BALANCE_START_FILTERS) && !(start_flags & BALANCE_START_NOWARN)) {
|
|
|
|
int delay = 10;
|
|
|
|
|
|
|
|
printf("WARNING:\n\n");
|
|
|
|
printf("\tFull balance without filters requested. This operation is very\n");
|
|
|
|
printf("\tintense and takes potentially very long. It is recommended to\n");
|
|
|
|
printf("\tuse the balance filters to narrow down the scope of balance.\n");
|
|
|
|
printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
|
|
|
|
printf("\twarning. The operation will start in %d seconds.\n", delay);
|
|
|
|
printf("\tUse Ctrl-C to stop it.\n");
|
|
|
|
while (delay) {
|
|
|
|
printf("%2d", delay--);
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
printf("\nStarting balance without any filters.\n");
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:02:30 +00:00
|
|
|
if (force)
|
|
|
|
args.flags |= BTRFS_BALANCE_FORCE;
|
2019-11-25 10:39:08 +00:00
|
|
|
if (bconf.verbose > BTRFS_BCONF_QUIET)
|
2012-02-03 19:02:30 +00:00
|
|
|
dump_ioctl_balance_args(&args);
|
2016-06-21 15:16:59 +00:00
|
|
|
if (background) {
|
|
|
|
switch (fork()) {
|
|
|
|
case (-1):
|
|
|
|
error("unable to fork to run balance in background");
|
|
|
|
return 1;
|
|
|
|
case (0):
|
|
|
|
setsid();
|
|
|
|
switch(fork()) {
|
|
|
|
case (-1):
|
|
|
|
error(
|
|
|
|
"unable to fork to run balance in background");
|
|
|
|
exit(1);
|
|
|
|
case (0):
|
2016-07-26 17:11:42 +00:00
|
|
|
/*
|
|
|
|
* Read the return value to silence compiler
|
|
|
|
* warning. Change to / should succeed and
|
|
|
|
* we're not in a security-sensitive context.
|
|
|
|
*/
|
|
|
|
i = chdir("/");
|
2016-06-21 15:16:59 +00:00
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
open("/dev/null", O_RDONLY);
|
|
|
|
open("/dev/null", O_WRONLY);
|
|
|
|
open("/dev/null", O_WRONLY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
return do_balance(argv[optind], &args, start_flags, enqueue);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(balance_start, "start");
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
static const char * const cmd_balance_pause_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance pause <path>",
|
2012-02-03 19:02:30 +00:00
|
|
|
"Pause running balance",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_pause(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
path = argv[optind];
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2015-10-12 13:22:56 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-10 02:28:29 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
|
|
|
|
if (ret < 0) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("balance pause on '%s' failed: %s", path,
|
2016-07-13 17:05:23 +00:00
|
|
|
(errno == ENOTCONN) ? "Not running" : strerror(errno));
|
|
|
|
if (errno == ENOTCONN)
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 2;
|
2013-09-10 02:28:29 +00:00
|
|
|
else
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fd);
|
2016-07-13 17:00:01 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(balance_pause, "pause");
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
static const char * const cmd_balance_cancel_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance cancel <path>",
|
2012-02-03 19:02:30 +00:00
|
|
|
"Cancel running or paused balance",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_cancel(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
path = argv[optind];
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2015-10-12 13:22:56 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-10 02:28:29 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
|
|
|
|
if (ret < 0) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("balance cancel on '%s' failed: %s", path,
|
2016-07-13 17:05:23 +00:00
|
|
|
(errno == ENOTCONN) ? "Not in progress" : strerror(errno));
|
|
|
|
if (errno == ENOTCONN)
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 2;
|
2013-09-10 02:28:29 +00:00
|
|
|
else
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 12:34:59 +00:00
|
|
|
btrfs_warn_multiple_profiles(fd);
|
2016-07-13 17:00:01 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(balance_cancel, "cancel");
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
static const char * const cmd_balance_resume_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance resume <path>",
|
2012-02-03 19:02:30 +00:00
|
|
|
"Resume interrupted balance",
|
2020-06-11 17:41:20 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_QUIET,
|
2012-02-03 19:02:30 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_resume(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_balance_args args;
|
|
|
|
const char *path;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:02:30 +00:00
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2016-03-01 15:02:08 +00:00
|
|
|
path = argv[optind];
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2015-10-12 13:22:56 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-10 02:28:29 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.flags |= BTRFS_BALANCE_RESUME;
|
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
|
|
|
|
if (ret < 0) {
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno == ECANCELED) {
|
2012-02-03 19:02:30 +00:00
|
|
|
if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
|
|
|
|
fprintf(stderr, "balance paused by user\n");
|
|
|
|
if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
|
|
|
|
fprintf(stderr, "balance canceled by user\n");
|
2016-07-13 17:05:23 +00:00
|
|
|
} else if (errno == ENOTCONN || errno == EINPROGRESS) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("balance resume on '%s' failed: %s", path,
|
2016-07-13 17:05:23 +00:00
|
|
|
(errno == ENOTCONN) ? "Not in progress" :
|
2012-02-03 19:02:30 +00:00
|
|
|
"Already running");
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno == ENOTCONN)
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 2;
|
2013-09-10 02:28:29 +00:00
|
|
|
else
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
} else {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("error during balancing '%s': %m\n"
|
2016-01-11 10:01:47 +00:00
|
|
|
"There may be more info in syslog - try dmesg | tail",
|
2018-01-07 21:54:21 +00:00
|
|
|
path);
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-11 17:41:20 +00:00
|
|
|
pr_verbose(MUST_LOG,
|
|
|
|
"Done, had to relocate %llu out of %llu chunks\n",
|
|
|
|
(unsigned long long)args.stat.completed,
|
|
|
|
(unsigned long long)args.stat.considered);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 17:00:01 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(balance_resume, "resume");
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
static const char * const cmd_balance_status_usage[] = {
|
2015-02-02 17:17:05 +00:00
|
|
|
"btrfs balance status [-v] <path>",
|
2012-02-03 19:02:30 +00:00
|
|
|
"Show status of running or paused balance",
|
|
|
|
"",
|
2020-06-26 20:20:56 +00:00
|
|
|
"-v|--verbose deprecated, alias for global -v option",
|
2019-11-25 10:39:09 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_VERBOSE,
|
2012-02-03 19:02:30 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-04-16 04:58:15 +00:00
|
|
|
/* Checks the status of the balance if any
|
|
|
|
* return codes:
|
|
|
|
* 2 : Error failed to know if there is any pending balance
|
|
|
|
* 1 : Successful to know status of a pending balance
|
|
|
|
* 0 : When there is no pending balance or completed
|
|
|
|
*/
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_status(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_balance_args args;
|
|
|
|
const char *path;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:02:30 +00:00
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2012-02-03 19:02:30 +00:00
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int opt;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option longopts[] = {
|
2012-02-03 19:02:30 +00:00
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2012-02-03 19:02:30 +00:00
|
|
|
};
|
|
|
|
|
2015-04-08 15:33:55 +00:00
|
|
|
opt = getopt_long(argc, argv, "v", longopts, NULL);
|
2012-02-03 19:02:30 +00:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'v':
|
2019-11-25 10:39:09 +00:00
|
|
|
bconf_be_verbose();
|
2012-02-03 19:02:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
path = argv[optind];
|
|
|
|
|
2015-10-12 13:22:56 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-04-16 04:58:15 +00:00
|
|
|
return 2;
|
2012-02-03 19:02:30 +00:00
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
|
|
|
|
if (ret < 0) {
|
2016-07-13 17:05:23 +00:00
|
|
|
if (errno == ENOTCONN) {
|
2013-04-16 04:58:15 +00:00
|
|
|
printf("No balance found on '%s'\n", path);
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2013-04-16 04:58:15 +00:00
|
|
|
}
|
2018-01-07 21:54:21 +00:00
|
|
|
error("balance status on '%s' failed: %m", path);
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 2;
|
|
|
|
goto out;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.state & BTRFS_BALANCE_STATE_RUNNING) {
|
|
|
|
printf("Balance on '%s' is running", path);
|
|
|
|
if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ)
|
|
|
|
printf(", cancel requested\n");
|
|
|
|
else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ)
|
|
|
|
printf(", pause requested\n");
|
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("Balance on '%s' is paused\n", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%llu out of about %llu chunks balanced (%llu considered), "
|
|
|
|
"%3.f%% left\n", (unsigned long long)args.stat.completed,
|
|
|
|
(unsigned long long)args.stat.expected,
|
|
|
|
(unsigned long long)args.stat.considered,
|
|
|
|
100 * (1 - (float)args.stat.completed/args.stat.expected));
|
|
|
|
|
2019-11-25 10:39:09 +00:00
|
|
|
if (bconf.verbose > BTRFS_BCONF_QUIET)
|
2012-02-03 19:02:30 +00:00
|
|
|
dump_ioctl_balance_args(&args);
|
|
|
|
|
2016-07-13 17:00:01 +00:00
|
|
|
ret = 1;
|
|
|
|
out:
|
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return ret;
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(balance_status, "status");
|
2012-02-03 19:02:30 +00:00
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_balance_full(const struct cmd_struct *cmd, int argc, char **argv)
|
2016-05-02 08:26:29 +00:00
|
|
|
{
|
|
|
|
struct btrfs_ioctl_balance_args args;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.flags |= BTRFS_BALANCE_TYPE_MASK;
|
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
/* No enqueueing supported for the obsolete syntax */
|
|
|
|
return do_balance(argv[1], &args, BALANCE_START_NOWARN, false);
|
2016-05-02 08:26:29 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_COMMAND(balance_full, "--full-balance", cmd_balance_full,
|
|
|
|
NULL, NULL, CMD_HIDDEN);
|
2016-05-02 08:26:29 +00:00
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char balance_cmd_group_info[] =
|
2016-05-11 23:50:36 +00:00
|
|
|
"balance data across devices, or change block groups using filters";
|
2015-06-09 12:22:05 +00:00
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const struct cmd_group balance_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
balance_cmd_group_usage, balance_cmd_group_info, {
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_balance_start,
|
|
|
|
&cmd_struct_balance_pause,
|
|
|
|
&cmd_struct_balance_cancel,
|
|
|
|
&cmd_struct_balance_resume,
|
|
|
|
&cmd_struct_balance_status,
|
|
|
|
&cmd_struct_balance_full,
|
|
|
|
NULL
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 04:27:53 +00:00
|
|
|
static int cmd_balance(const struct cmd_struct *cmd, int argc, char **argv)
|
2012-02-03 19:02:30 +00:00
|
|
|
{
|
2016-05-02 08:26:29 +00:00
|
|
|
if (argc == 2 && strcmp("start", argv[1]) != 0) {
|
2012-02-03 19:02:30 +00:00
|
|
|
/* old 'btrfs filesystem balance <path>' syntax */
|
|
|
|
struct btrfs_ioctl_balance_args args;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.flags |= BTRFS_BALANCE_TYPE_MASK;
|
|
|
|
|
2020-11-05 02:38:00 +00:00
|
|
|
/* No enqueueing supported for the obsolete syntax */
|
|
|
|
return do_balance(argv[1], &args, 0, false);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 04:27:53 +00:00
|
|
|
return handle_command_group(cmd, argc, argv);
|
2012-02-03 19:02:30 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
|
2018-03-07 04:27:53 +00:00
|
|
|
DEFINE_COMMAND(balance, "balance", cmd_balance, NULL, &balance_cmd_group, 0);
|