-
Notifications
You must be signed in to change notification settings - Fork 13
/
control.c
338 lines (262 loc) · 6.08 KB
/
control.c
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
/*
*
* Copyright (C) 2011 Citrix Systems Inc.
*
* This file is part of Blktap2.
*
* Blktap2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* Blktap2 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with Blktap2. If not, see
* <http://www.gnu.org/licenses/>.
*
*
*/
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/miscdevice.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include "blktap.h"
DEFINE_MUTEX(blktap_lock);
struct blktap **blktaps;
int blktap_max_minor;
static struct blktap_page_pool *default_pool;
static struct blktap *
blktap_control_get_minor(void)
{
int minor;
struct blktap *tap;
tap = kzalloc(sizeof(*tap), GFP_KERNEL);
if (unlikely(!tap))
return NULL;
mutex_lock(&blktap_lock);
for (minor = 0; minor < blktap_max_minor; minor++)
if (!blktaps[minor])
break;
if (minor == MAX_BLKTAP_DEVICE)
goto fail;
if (minor == blktap_max_minor) {
void *p;
int n;
n = min(2 * blktap_max_minor, MAX_BLKTAP_DEVICE);
p = krealloc(blktaps, n * sizeof(blktaps[0]), GFP_KERNEL);
if (!p)
goto fail;
blktaps = p;
minor = blktap_max_minor;
blktap_max_minor = n;
memset(&blktaps[minor], 0, (n - minor) * sizeof(blktaps[0]));
}
tap->minor = minor;
blktaps[minor] = tap;
__module_get(THIS_MODULE);
out:
mutex_unlock(&blktap_lock);
return tap;
fail:
mutex_unlock(&blktap_lock);
kfree(tap);
tap = NULL;
goto out;
}
static void
blktap_control_put_minor(struct blktap* tap)
{
blktaps[tap->minor] = NULL;
kfree(tap);
module_put(THIS_MODULE);
}
static struct blktap*
blktap_control_create_tap(void)
{
struct blktap *tap;
int err;
tap = blktap_control_get_minor();
if (!tap)
return NULL;
kobject_get(&default_pool->kobj);
tap->pool = default_pool;
err = blktap_ring_create(tap);
if (err)
goto fail_tap;
err = blktap_sysfs_create(tap);
if (err)
goto fail_ring;
return tap;
fail_ring:
blktap_ring_destroy(tap);
fail_tap:
blktap_control_put_minor(tap);
return NULL;
}
int
blktap_control_destroy_tap(struct blktap *tap)
{
int err;
err = blktap_ring_destroy(tap);
if (err)
return err;
kobject_put(&tap->pool->kobj);
blktap_sysfs_destroy(tap);
blktap_control_put_minor(tap);
return 0;
}
static long
blktap_control_ioctl(struct file *filp,
unsigned int cmd, unsigned long arg)
{
struct blktap *tap;
switch (cmd) {
case BLKTAP_IOCTL_ALLOC_TAP: {
struct blktap_info info;
void __user *ptr = (void __user*)arg;
tap = blktap_control_create_tap();
if (!tap)
return -ENOMEM;
info.ring_major = blktap_ring_major;
info.bdev_major = blktap_device_major;
info.ring_minor = tap->minor;
if (copy_to_user(ptr, &info, sizeof(info))) {
blktap_control_destroy_tap(tap);
return -EFAULT;
}
return 0;
}
case BLKTAP_IOCTL_FREE_TAP: {
int minor = arg;
if (minor > MAX_BLKTAP_DEVICE)
return -EINVAL;
tap = blktaps[minor];
if (!tap)
return -ENODEV;
return blktap_control_destroy_tap(tap);
}
}
return -ENOIOCTLCMD;
}
static struct file_operations blktap_control_file_operations = {
.owner = THIS_MODULE,
.unlocked_ioctl = blktap_control_ioctl,
};
static struct miscdevice blktap_control = {
.minor = MISC_DYNAMIC_MINOR,
.name = "blktap-control",
.fops = &blktap_control_file_operations,
};
static struct device *control_device;
static ssize_t
blktap_control_show_default_pool(struct device *device,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "%s", kobject_name(&default_pool->kobj));
}
static ssize_t
blktap_control_store_default_pool(struct device *device,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct blktap_page_pool *pool, *tmp = default_pool;
pool = blktap_page_pool_get(buf);
if (IS_ERR(pool))
return PTR_ERR(pool);
default_pool = pool;
kobject_put(&tmp->kobj);
return size;
}
static DEVICE_ATTR(default_pool, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
blktap_control_show_default_pool,
blktap_control_store_default_pool);
size_t
blktap_control_debug(struct blktap *tap, char *buf, size_t size)
{
char *s = buf, *end = buf + size;
s += snprintf(s, end - s,
"tap %u:%u name:'%s' flags:%#08lx\n",
MAJOR(tap->ring.devno), MINOR(tap->ring.devno),
tap->name, tap->dev_inuse);
return s - buf;
}
static int __init
blktap_control_init(void)
{
int err;
err = misc_register(&blktap_control);
if (err)
return err;
control_device = blktap_control.this_device;
blktap_max_minor = min(64, MAX_BLKTAP_DEVICE);
blktaps = kzalloc(blktap_max_minor * sizeof(blktaps[0]), GFP_KERNEL);
if (!blktaps) {
BTERR("failed to allocate blktap minor map");
return -ENOMEM;
}
err = blktap_page_pool_init(&control_device->kobj);
if (err)
return err;
default_pool = blktap_page_pool_get("default");
if (!default_pool)
return -ENOMEM;
err = device_create_file(control_device, &dev_attr_default_pool);
if (err)
return err;
return 0;
}
static void
blktap_control_exit(void)
{
if (default_pool) {
kobject_put(&default_pool->kobj);
default_pool = NULL;
}
blktap_page_pool_exit();
if (blktaps) {
kfree(blktaps);
blktaps = NULL;
}
if (control_device) {
misc_deregister(&blktap_control);
control_device = NULL;
}
}
static void
blktap_exit(void)
{
blktap_control_exit();
blktap_ring_exit();
blktap_sysfs_exit();
blktap_device_exit();
}
static int __init
blktap_init(void)
{
int err;
err = blktap_device_init();
if (err)
goto fail;
err = blktap_ring_init();
if (err)
goto fail;
err = blktap_sysfs_init();
if (err)
goto fail;
err = blktap_control_init();
if (err)
goto fail;
return 0;
fail:
blktap_exit();
return err;
}
module_init(blktap_init);
module_exit(blktap_exit);
MODULE_LICENSE("Dual BSD/GPL");