-
Notifications
You must be signed in to change notification settings - Fork 13
/
sysfs.c
316 lines (255 loc) · 6.45 KB
/
sysfs.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
/*
*
* 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/types.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include "blktap.h"
int blktap_debug_level = 1;
static struct class *class;
static ssize_t
blktap_sysfs_set_name(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
{
struct blktap *tap;
tap = dev_get_drvdata(dev);
if (!tap)
return 0;
if (size >= BLKTAP_NAME_MAX)
return -ENAMETOOLONG;
if (strnlen(buf, size) != size)
return -EINVAL;
strcpy(tap->name, buf);
return size;
}
static ssize_t
blktap_sysfs_get_name(struct device *dev, struct device_attribute *attr, char *buf)
{
struct blktap *tap;
ssize_t size;
tap = dev_get_drvdata(dev);
if (!tap)
return 0;
if (tap->name[0])
size = sprintf(buf, "%s\n", tap->name);
else
size = sprintf(buf, "%d\n", tap->minor);
return size;
}
static DEVICE_ATTR(name, S_IRUGO|S_IWUSR,
blktap_sysfs_get_name, blktap_sysfs_set_name);
static void
blktap_sysfs_remove_work(struct work_struct *work)
{
struct blktap *tap
= container_of(work, struct blktap, remove_work);
blktap_control_destroy_tap(tap);
}
static ssize_t
blktap_sysfs_remove_device(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct blktap *tap;
int err;
tap = dev_get_drvdata(dev);
if (!tap)
return size;
if (test_and_set_bit(BLKTAP_SHUTDOWN_REQUESTED, &tap->dev_inuse))
goto wait;
if (tap->ring.vma) {
struct blktap_sring *sring = tap->ring.ring.sring;
*BLKTAP_RING_MESSAGE(sring) = BLKTAP_RING_MESSAGE_CLOSE;
blktap_ring_kick_user(tap);
} else {
INIT_WORK(&tap->remove_work, blktap_sysfs_remove_work);
schedule_work(&tap->remove_work);
}
wait:
err = wait_event_interruptible(tap->remove_wait,
!dev_get_drvdata(dev));
if (err)
return err;
return size;
}
static DEVICE_ATTR(remove, S_IWUSR, NULL, blktap_sysfs_remove_device);
static ssize_t
blktap_sysfs_debug_device(struct device *dev, struct device_attribute *attr, char *buf)
{
struct blktap *tap;
char *s = buf, *end = buf + PAGE_SIZE;
tap = dev_get_drvdata(dev);
if (!tap)
return 0;
s += blktap_control_debug(tap, s, end - s);
s += blktap_request_debug(tap, s, end - s);
s += blktap_device_debug(tap, s, end - s);
s += blktap_ring_debug(tap, s, end - s);
return s - buf;
}
static DEVICE_ATTR(debug, S_IRUGO, blktap_sysfs_debug_device, NULL);
static ssize_t
blktap_sysfs_show_task(struct device *dev, struct device_attribute *attr, char *buf)
{
struct blktap *tap;
ssize_t rv = 0;
tap = dev_get_drvdata(dev);
if (!tap)
return 0;
if (tap->ring.task)
rv = sprintf(buf, "%d\n", tap->ring.task->pid);
return rv;
}
static DEVICE_ATTR(task, S_IRUGO, blktap_sysfs_show_task, NULL);
static ssize_t
blktap_sysfs_show_pool(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct blktap *tap = dev_get_drvdata(dev);
return sprintf(buf, "%s", kobject_name(&tap->pool->kobj));
}
static ssize_t
blktap_sysfs_store_pool(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct blktap *tap = dev_get_drvdata(dev);
struct blktap_page_pool *pool, *tmp = tap->pool;
if (tap->device.gd)
return -EBUSY;
pool = blktap_page_pool_get(buf);
if (IS_ERR(pool))
return PTR_ERR(pool);
tap->pool = pool;
kobject_put(&tmp->kobj);
return size;
}
DEVICE_ATTR(pool, S_IRUSR|S_IWUSR,
blktap_sysfs_show_pool, blktap_sysfs_store_pool);
int
blktap_sysfs_create(struct blktap *tap)
{
struct blktap_ring *ring = &tap->ring;
struct device *dev;
int err = 0;
init_waitqueue_head(&tap->remove_wait);
dev = device_create(class, NULL, ring->devno,
tap, "blktap%d", tap->minor);
if (IS_ERR(dev))
err = PTR_ERR(dev);
if (!err)
err = device_create_file(dev, &dev_attr_name);
if (!err)
err = device_create_file(dev, &dev_attr_remove);
if (!err)
err = device_create_file(dev, &dev_attr_debug);
if (!err)
err = device_create_file(dev, &dev_attr_task);
if (!err)
err = device_create_file(dev, &dev_attr_pool);
if (!err)
ring->dev = dev;
else
device_unregister(dev);
return err;
}
void
blktap_sysfs_destroy(struct blktap *tap)
{
struct blktap_ring *ring = &tap->ring;
struct device *dev;
dev = ring->dev;
if (!dev)
return;
dev_set_drvdata(dev, NULL);
wake_up(&tap->remove_wait);
device_unregister(dev);
ring->dev = NULL;
}
static ssize_t
blktap_sysfs_show_verbosity(struct class *class,
struct class_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", blktap_debug_level);
}
static ssize_t
blktap_sysfs_set_verbosity(struct class *class,
struct class_attribute *attr,
const char *buf, size_t size)
{
int level;
if (sscanf(buf, "%d", &level) == 1) {
blktap_debug_level = level;
return size;
}
return -EINVAL;
}
static CLASS_ATTR(verbosity, S_IRUGO|S_IWUSR,
blktap_sysfs_show_verbosity, blktap_sysfs_set_verbosity);
static ssize_t
blktap_sysfs_show_devices(struct class *class,
struct class_attribute *attr,
char *buf)
{
int i, ret;
struct blktap *tap;
mutex_lock(&blktap_lock);
ret = 0;
for (i = 0; i < blktap_max_minor; i++) {
tap = blktaps[i];
if (!tap)
continue;
if (!test_bit(BLKTAP_DEVICE, &tap->dev_inuse))
continue;
ret += sprintf(buf + ret, "%d %s\n", tap->minor, tap->name);
}
mutex_unlock(&blktap_lock);
return ret;
}
static CLASS_ATTR(devices, S_IRUGO, blktap_sysfs_show_devices, NULL);
void
blktap_sysfs_exit(void)
{
if (class)
class_destroy(class);
}
int __init
blktap_sysfs_init(void)
{
struct class *cls;
int err = 0;
cls = class_create(THIS_MODULE, "blktap2");
if (IS_ERR(cls))
err = PTR_ERR(cls);
if (!err)
err = class_create_file(cls, &class_attr_verbosity);
if (!err)
err = class_create_file(cls, &class_attr_devices);
if (!err)
class = cls;
else
class_destroy(cls);
return err;
}