summaryrefslogtreecommitdiff
path: root/bcacheadm-run.c
blob: 8f995a1dfb3a71d80c756ddbe0a583aa04f37e65 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <nih/command.h>
#include <nih/option.h>

#include <linux/bcache-ioctl.h>

#include <uuid/uuid.h>

#include "bcache.h"
#include "bcacheadm-run.h"

static bool force_data = false;
static bool force_metadata = false;

NihOption opts_run[] = {
	NIH_OPTION_LAST
};

int cmd_run(NihCommand *command, char *const *args)
{
	return 0;
}

NihOption opts_stop[] = {
	NIH_OPTION_LAST
};

int cmd_stop(NihCommand *command, char *const *args)
{
	int bcachefd = open("/dev/bcache_extent0", O_RDWR);
	if (bcachefd < 0)
		die("Can't open bcache device");

	int ret = ioctl(bcachefd, BCH_IOCTL_STOP);
	if (ret < 0)
		die("BCH_IOCTL_STOP error: %s", strerror(errno));

	close(bcachefd);
	return 0;
}

NihOption opts_add[] = {
	NIH_OPTION_LAST
};

int cmd_add(NihCommand *command, char *const *args)
{
	if (nr_args(args) != 1)
		die("Please supply exactly one device");

	int ret, bcachefd;

	bcachefd = open("/dev/bcache_extent0", O_RDWR);
	if (bcachefd < 0)
		die("Can't open bcache device: %s", strerror(errno));

	struct bch_ioctl_disk_add ia = {
		.dev = (__u64) args[0],
	};

	ret = ioctl(bcachefd, BCH_IOCTL_DISK_ADD, &ia);
	if (ret < 0)
		die("BCH_IOCTL_DISK_ADD error: %s", strerror(ret));

	close(bcachefd);
	return 0;
}

NihOption opts_readd[] = {
	NIH_OPTION_LAST
};

int cmd_readd(NihCommand *command, char *const *args)
{
	if (nr_args(args) != 1)
		die("Please supply exactly one device");

	return 0;
}

NihOption opts_remove[] = {
	{
		'f', "force", N_("force if data present"),
		NULL, NULL, &force_data, NULL
	},
	{
		'\0', "force-metadata", N_("force if metadata present"),
		NULL, NULL, &force_metadata, NULL},
	NIH_OPTION_LAST
};

int cmd_remove(NihCommand *command, char *const *args)
{
	if (nr_args(args) != 1)
		die("Please supply exactly one device");

	int bcachefd = open("/dev/bcache_extent0", O_RDWR);
	if (bcachefd < 0)
		die("Can't open bcache device");

	struct bch_ioctl_disk_remove ir = {
		.dev = (__u64) args[0],
	};

	if (force_data)
		ir.flags |= BCH_FORCE_IF_DATA_MISSING;
	if (force_metadata)
		ir.flags |= BCH_FORCE_IF_METADATA_MISSING;

	int ret = ioctl(bcachefd, BCH_IOCTL_DISK_REMOVE, &ir);
	if (ret < 0)
		die("BCH_IOCTL_DISK_REMOVE error: %s\n", strerror(errno));

	close(bcachefd);
	return 0;
}

static const char *dev_failed_uuid = NULL;

NihOption opts_fail[] = {
	{'d', "dev", N_("dev UUID"), NULL, "UUID", &dev_failed_uuid, NULL},
	NIH_OPTION_LAST
};

int cmd_fail(NihCommand *command, char *const *args)
{
	if (nr_args(args) != 1)
		die("Please supply exactly one device");

	int bcachefd = open("/dev/bcache_extent0", O_RDWR);
	if (bcachefd < 0)
		die("Can't open bcache device");

	struct bch_ioctl_disk_fail df = {
		.dev = (__u64) args[0],
	};

	int ret = ioctl(bcachefd, BCH_IOCTL_DISK_FAIL, &df);
	if (ret < 0)
		die("BCH_IOCTL_DISK_FAIL error: %s\n", strerror(errno));

	return 0;
}