summaryrefslogtreecommitdiff
path: root/cmds-balance.c
blob: d141b461a0be3043bb5efacaf476e74c5fbe6c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * 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>
#include <sys/ioctl.h>
#include <errno.h>

#include "kerncompat.h"
#include "ctree.h"
#include "ioctl.h"
#include "volumes.h"

#include "commands.h"

static const char balance_cmd_group_usage[] =
	"btrfs [filesystem] balance [<command>] [options] <path>";

static const char balance_cmd_group_info[] =
	"'btrfs filesystem balance' command is deprecated, please use\n"
	"'btrfs balance start' command instead.";

static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args)
{
	int fd;
	int ret;
	int e;

	fd = open_file_or_dir(path);
	if (fd < 0) {
		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
		return 12;
	}

	ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
	e = errno;
	close(fd);

	if (ret < 0) {
		if (e == ECANCELED) {
			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");
		} else {
			fprintf(stderr, "ERROR: error during balancing '%s' "
				"- %s\n", path, strerror(e));
			if (e != EINPROGRESS)
				fprintf(stderr, "There may be more info in "
					"syslog - try dmesg | tail\n");
			return 19;
		}
	} else {
		printf("Done, had to relocate %llu out of %llu chunks\n",
		       (unsigned long long)args->stat.completed,
		       (unsigned long long)args->stat.considered);
	}

	return 0;
}

const struct cmd_group balance_cmd_group = {
	balance_cmd_group_usage, balance_cmd_group_info, {
		{ 0, 0, 0, 0, 0 }
	}
};

int cmd_balance(int argc, char **argv)
{
	if (argc == 2) {
		/* old 'btrfs filesystem balance <path>' syntax */
		struct btrfs_ioctl_balance_args args;

		memset(&args, 0, sizeof(args));
		args.flags |= BTRFS_BALANCE_TYPE_MASK;

		return do_balance(argv[1], &args);
	}

	return handle_command_group(&balance_cmd_group, argc, argv);
}