summaryrefslogtreecommitdiff
path: root/libbcache/chardev.c
blob: b142d7b27a291eb622b919aef7fc9a7691dfc25d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * This file adds support for a character device /dev/bcache that is used to
 * atomically register a list of devices, remove a device from a cache_set
 * and add a device to a cache set.
 *
 * Copyright (c) 2014 Datera, Inc.
 *
 */

#include "bcache.h"
#include "super.h"
#include "super-io.h"

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/bcache-ioctl.h>

static long bch_ioctl_assemble(struct bch_ioctl_assemble __user *user_arg)
{
	struct bch_ioctl_assemble arg;
	const char *err;
	u64 *user_devs = NULL;
	char **devs = NULL;
	unsigned i;
	int ret = -EFAULT;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	user_devs = kmalloc_array(arg.nr_devs, sizeof(u64), GFP_KERNEL);
	if (!devs)
		return -ENOMEM;

	devs = kcalloc(arg.nr_devs, sizeof(char *), GFP_KERNEL);

	if (copy_from_user(user_devs, user_arg->devs,
			   sizeof(u64) * arg.nr_devs))
		goto err;

	for (i = 0; i < arg.nr_devs; i++) {
		devs[i] = strndup_user((const char __user *)(unsigned long)
				       user_devs[i],
				       PATH_MAX);
		if (!devs[i]) {
			ret = -ENOMEM;
			goto err;
		}
	}

	err = bch_fs_open(devs, arg.nr_devs, bch_opts_empty(), NULL);
	if (err) {
		pr_err("Could not register cache set: %s", err);
		ret = -EINVAL;
		goto err;
	}

	ret = 0;
err:
	if (devs)
		for (i = 0; i < arg.nr_devs; i++)
			kfree(devs[i]);
	kfree(devs);
	return ret;
}

static long bch_ioctl_incremental(struct bch_ioctl_incremental __user *user_arg)
{
	struct bch_ioctl_incremental arg;
	const char *err;
	char *path;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
	if (!path)
		return -ENOMEM;

	err = bch_fs_open_incremental(path);
	kfree(path);

	if (err) {
		pr_err("Could not register bcache devices: %s", err);
		return -EINVAL;
	}

	return 0;
}

static long bch_global_ioctl(unsigned cmd, void __user *arg)
{
	switch (cmd) {
	case BCH_IOCTL_ASSEMBLE:
		return bch_ioctl_assemble(arg);
	case BCH_IOCTL_INCREMENTAL:
		return bch_ioctl_incremental(arg);
	default:
		return -ENOTTY;
	}
}

static long bch_ioctl_stop(struct cache_set *c)
{
	bch_fs_stop(c);
	return 0;
}

static long bch_ioctl_disk_add(struct cache_set *c,
			       struct bch_ioctl_disk_add __user *user_arg)
{
	struct bch_ioctl_disk_add arg;
	char *path;
	int ret;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
	if (!path)
		return -ENOMEM;

	ret = bch_dev_add(c, path);
	kfree(path);

	return ret;
}

/* returns with ref on ca->ref */
static struct cache *bch_device_lookup(struct cache_set *c,
				       const char __user *dev)
{
	struct block_device *bdev;
	struct cache *ca;
	char *path;
	unsigned i;

	path = strndup_user(dev, PATH_MAX);
	if (!path)
		return ERR_PTR(-ENOMEM);

	bdev = lookup_bdev(strim(path));
	kfree(path);
	if (IS_ERR(bdev))
		return ERR_CAST(bdev);

	for_each_cache(ca, c, i)
		if (ca->disk_sb.bdev == bdev)
			goto found;

	ca = NULL;
found:
	bdput(bdev);
	return ca;
}

static long bch_ioctl_disk_remove(struct cache_set *c,
				  struct bch_ioctl_disk_remove __user *user_arg)
{
	struct bch_ioctl_disk_remove arg;
	struct cache *ca;
	int ret;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	ca = bch_device_lookup(c, (const char __user *)(unsigned long) arg.dev);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	ret = bch_dev_remove(ca, arg.flags & BCH_FORCE_IF_DATA_MISSING)
		? 0 : -EBUSY;

	percpu_ref_put(&ca->ref);
	return ret;
}

static long bch_ioctl_disk_fail(struct cache_set *c,
				struct bch_ioctl_disk_fail __user *user_arg)
{
	struct bch_ioctl_disk_fail arg;
	struct cache *ca;
	int ret;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	ca = bch_device_lookup(c, (const char __user *)(unsigned long) arg.dev);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	/* XXX: failed not actually implemented yet */
	ret = bch_dev_remove(ca, true);

	percpu_ref_put(&ca->ref);
	return ret;
}

static struct bch_member *bch_uuid_lookup(struct cache_set *c, uuid_le uuid)
{
	struct bch_sb_field_members *mi = bch_sb_get_members(c->disk_sb);
	unsigned i;

	lockdep_assert_held(&c->sb_lock);

	for (i = 0; i < c->disk_sb->nr_devices; i++)
		if (!memcmp(&mi->members[i].uuid, &uuid, sizeof(uuid)))
			return &mi->members[i];

	return NULL;
}

static long bch_ioctl_disk_remove_by_uuid(struct cache_set *c,
			struct bch_ioctl_disk_remove_by_uuid __user *user_arg)
{
	struct bch_ioctl_disk_fail_by_uuid arg;
	struct bch_member *m;
	int ret = -ENOENT;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	mutex_lock(&c->sb_lock);
	if ((m = bch_uuid_lookup(c, arg.dev))) {
		/* XXX: */
		SET_BCH_MEMBER_STATE(m, BCH_MEMBER_STATE_FAILED);
		bch_write_super(c);
		ret = 0;
	}
	mutex_unlock(&c->sb_lock);

	return ret;
}

static long bch_ioctl_disk_fail_by_uuid(struct cache_set *c,
			struct bch_ioctl_disk_fail_by_uuid __user *user_arg)
{
	struct bch_ioctl_disk_fail_by_uuid arg;
	struct bch_member *m;
	int ret = -ENOENT;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	mutex_lock(&c->sb_lock);
	if ((m = bch_uuid_lookup(c, arg.dev))) {
		SET_BCH_MEMBER_STATE(m, BCH_MEMBER_STATE_FAILED);
		bch_write_super(c);
		ret = 0;
	}
	mutex_unlock(&c->sb_lock);

	return ret;
}

static long bch_ioctl_query_uuid(struct cache_set *c,
			struct bch_ioctl_query_uuid __user *user_arg)
{
	return copy_to_user(&user_arg->uuid,
			    &c->sb.user_uuid,
			    sizeof(c->sb.user_uuid));
}

long bch_fs_ioctl(struct cache_set *c, unsigned cmd, void __user *arg)
{
	/* ioctls that don't require admin cap: */
	switch (cmd) {
	case BCH_IOCTL_QUERY_UUID:
		return bch_ioctl_query_uuid(c, arg);
	}

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	/* ioctls that do require admin cap: */
	switch (cmd) {
	case BCH_IOCTL_RUN:
		return -ENOTTY;
	case BCH_IOCTL_STOP:
		return bch_ioctl_stop(c);

	case BCH_IOCTL_DISK_ADD:
		return bch_ioctl_disk_add(c, arg);
	case BCH_IOCTL_DISK_REMOVE:
		return bch_ioctl_disk_remove(c, arg);
	case BCH_IOCTL_DISK_FAIL:
		return bch_ioctl_disk_fail(c, arg);

	case BCH_IOCTL_DISK_REMOVE_BY_UUID:
		return bch_ioctl_disk_remove_by_uuid(c, arg);
	case BCH_IOCTL_DISK_FAIL_BY_UUID:
		return bch_ioctl_disk_fail_by_uuid(c, arg);

	default:
		return -ENOTTY;
	}
}

static long bch_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
{
	struct cache_set *c = filp->private_data;
	void __user *arg = (void __user *) v;

	return c
		? bch_fs_ioctl(c, cmd, arg)
		: bch_global_ioctl(cmd, arg);
}

static const struct file_operations bch_chardev_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl = bch_chardev_ioctl,
	.open		= nonseekable_open,
};

static int bch_chardev_major;
static struct class *bch_chardev_class;
static struct device *bch_chardev;
static DEFINE_IDR(bch_chardev_minor);

void bch_fs_chardev_exit(struct cache_set *c)
{
	if (!IS_ERR_OR_NULL(c->chardev))
		device_unregister(c->chardev);
	if (c->minor >= 0)
		idr_remove(&bch_chardev_minor, c->minor);
}

int bch_fs_chardev_init(struct cache_set *c)
{
	c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
	if (c->minor < 0)
		return c->minor;

	c->chardev = device_create(bch_chardev_class, NULL,
				   MKDEV(bch_chardev_major, c->minor), NULL,
				   "bcache%u-ctl", c->minor);
	if (IS_ERR(c->chardev))
		return PTR_ERR(c->chardev);

	return 0;
}

void bch_chardev_exit(void)
{
	if (!IS_ERR_OR_NULL(bch_chardev_class))
		device_destroy(bch_chardev_class,
			       MKDEV(bch_chardev_major, 0));
	if (!IS_ERR_OR_NULL(bch_chardev_class))
		class_destroy(bch_chardev_class);
	if (bch_chardev_major > 0)
		unregister_chrdev(bch_chardev_major, "bcache");

}

int __init bch_chardev_init(void)
{
	bch_chardev_major = register_chrdev(0, "bcache-ctl", &bch_chardev_fops);
	if (bch_chardev_major < 0)
		return bch_chardev_major;

	bch_chardev_class = class_create(THIS_MODULE, "bcache");
	if (IS_ERR(bch_chardev_class))
		return PTR_ERR(bch_chardev_class);

	bch_chardev = device_create(bch_chardev_class, NULL,
				    MKDEV(bch_chardev_major, 255),
				    NULL, "bcache-ctl");
	if (IS_ERR(bch_chardev))
		return PTR_ERR(bch_chardev);

	return 0;
}