This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
vhost-9p.c
349 lines (291 loc) · 7.39 KB
/
vhost-9p.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
339
340
341
342
343
344
345
346
347
348
349
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/compat.h>
#include <linux/eventfd.h>
#include <linux/vhost.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/namei.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/virtio_9p.h>
#include <net/9p/9p.h>
#include "vhost.h"
#include "vhost-9p.h"
/* Max number of bytes transferred before requeueing the job.
* Using this limit prevents one virtqueue from starving others.
*/
#define VHOST_9P_WEIGHT 0x80000
#define VHOST_SET_PATH 3
enum {
VHOST_9P_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_9P_MOUNT_TAG)
};
/* Expects to be always run from workqueue - which acts as
* read-size critical section for our kind of RCU.
*/
static void handle_vq(struct vhost_9p *n)
{
struct vhost_virtqueue *vq = &n->vqs[VHOST_9P_VQ];
unsigned int out, in;
int head;
size_t out_len, in_len, total_len = 0;
struct iov_iter req, resp;
mutex_lock(&vq->mutex);
vhost_disable_notify(&n->dev, vq);
for (;;) {
head = vhost_get_vq_desc(vq, vq->iov,
ARRAY_SIZE(vq->iov),
&out, &in,
NULL, NULL);
/* On error, stop handling until the next kick. */
if (unlikely(head < 0))
break;
/* Nothing new? Wait for eventfd to tell us they refilled. */
if (head == vq->num) {
if (unlikely(vhost_enable_notify(&n->dev, vq))) {
vhost_disable_notify(&n->dev, vq);
continue;
}
break;
}
out_len = iov_length(vq->iov, out);
in_len = iov_length(&vq->iov[out], in);
iov_iter_init(&req, WRITE, vq->iov, out, out_len);
iov_iter_init(&resp, READ, &vq->iov[out], in, in_len);
do_9p_request(n->server, &req, &resp);
vhost_add_used_and_signal(&n->dev, vq, head, in + out);
total_len += out_len;
if (unlikely(total_len >= VHOST_9P_WEIGHT)) {
vhost_poll_queue(&vq->poll);
break;
}
}
mutex_unlock(&vq->mutex);
}
static void handle_vq_kick(struct vhost_work *work)
{
struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
poll.work);
struct vhost_9p *n = container_of(vq->dev, struct vhost_9p, dev);
// printk("VHOST_9P_HANDLE_KICK\n");
handle_vq(n);
}
// TODO: execution flow review
static int vhost_9p_open(struct inode *inode, struct file *f)
{
struct vhost_dev *dev;
struct vhost_virtqueue **vqs;
struct vhost_9p *n = kmalloc(sizeof(*n), GFP_KERNEL);
pr_info("VHOST_9P_OPEN\n");
if (!n)
return -ENOMEM;
vqs = kmalloc_array(VHOST_9P_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
if (!vqs) {
kfree(n);
return -ENOMEM;
}
dev = &n->dev;
vqs[VHOST_9P_VQ] = &n->vqs[VHOST_9P_VQ];
n->vqs[VHOST_9P_VQ].handle_kick = handle_vq_kick;
vhost_dev_init(dev, vqs, VHOST_9P_VQ_MAX);
f->private_data = n;
return 0;
}
static void *vhost_9p_stop_vq(struct vhost_9p *n,
struct vhost_virtqueue *vq)
{
void *private;
mutex_lock(&vq->mutex);
private = vq->private_data;
vq->private_data = NULL;
mutex_unlock(&vq->mutex);
return private;
}
static void vhost_9p_stop(struct vhost_9p *n, void **privatep)
{
*privatep = vhost_9p_stop_vq(n, n->vqs + VHOST_9P_VQ);
}
static void vhost_9p_flush_vq(struct vhost_9p *n, int index)
{
vhost_poll_flush(&n->vqs[index].poll);
}
static void vhost_9p_flush(struct vhost_9p *n)
{
vhost_9p_flush_vq(n, VHOST_9P_VQ);
}
static int vhost_9p_release(struct inode *inode, struct file *f)
{
struct vhost_9p *n = f->private_data;
void *private;
pr_info("VHOST_9P_RELEASE\n");
vhost_9p_stop(n, &private);
vhost_9p_flush(n);
vhost_dev_cleanup(&n->dev, false);
/* We do an extra flush before freeing memory,
* since jobs can re-queue themselves.
*/
vhost_9p_flush(n);
kfree(n);
return 0;
}
static long vhost_9p_reset_owner(struct vhost_9p *n)
{
void *priv = NULL;
long err;
struct vhost_umem *umem;
mutex_lock(&n->dev.mutex);
err = vhost_dev_check_owner(&n->dev);
if (err)
goto done;
umem = vhost_dev_reset_owner_prepare();
if (!umem) {
err = -ENOMEM;
goto done;
}
vhost_9p_stop(n, &priv);
vhost_9p_flush(n);
vhost_dev_reset_owner(&n->dev, umem);
done:
mutex_unlock(&n->dev.mutex);
return err;
}
static int vhost_9p_set_features(struct vhost_9p *n, u64 features)
{
struct vhost_virtqueue *vq;
mutex_lock(&n->dev.mutex);
if ((features & (1 << VHOST_F_LOG_ALL)) &&
!vhost_log_access_ok(&n->dev)) {
mutex_unlock(&n->dev.mutex);
return -EFAULT;
}
vq = &n->vqs[VHOST_9P_VQ];
mutex_lock(&vq->mutex);
vq->acked_features = features;
mutex_unlock(&vq->mutex);
mutex_unlock(&n->dev.mutex);
return 0;
}
/*
if (root_dir[0] != '/')
return -EINVAL;
len = strlen(root_dir);
if (len > PATH_MAX - 1)
return -ENAMETOOLONG;
// Check if base is dir
err = lstat(root_dir, &st);
if (err)
return err;
if (!S_ISDIR(st.mode))
return -ENOTDIR;
strncpy(s->base, root_dir, len);
if (s->base[len - 1] == '/')
s->base[len - 1] = '\0';
*/
long vhost_9p_set_path(struct vhost_9p *n, void __user *argp)
{
char __user *src = argp;
char *dst = NULL;
size_t len;
int err;
unsigned int lookup_flags = LOOKUP_FOLLOW;
struct path root;
// TODO: improve srtlen(src)
dst = kmalloc(PATH_MAX, GFP_KERNEL);
if (copy_from_user(dst, src, strnlen_user(src, PATH_MAX))) {
err = -EFAULT;
goto out;
}
len = strlen(dst);
if (len > PATH_MAX - 1) {
err = -ENAMETOOLONG;
goto out;
}
retry:
err = kern_path(dst, lookup_flags, &root);
if (!err) {
n->server = p9_server_create(&root);
if (IS_ERR(n->server))
err = PTR_ERR(n->server);
}
if (retry_estale(err, lookup_flags)) {
lookup_flags |= LOOKUP_REVAL;
goto retry;
}
out:
kfree(dst);
return err;
}
static long vhost_9p_ioctl(struct file *f, unsigned int ioctl,
unsigned long arg)
{
struct vhost_9p *n = f->private_data;
void __user *argp = (void __user *)arg;
u64 __user *featurep = argp;
u64 features;
int r;
//printk("virtio-9p ioctl: %d, %lx\n", ioctl, arg);
switch (ioctl) {
case VHOST_GET_FEATURES:
features = VHOST_FEATURES;
if (copy_to_user(featurep, &features, sizeof(features)))
return -EFAULT;
return 0;
case VHOST_SET_FEATURES:
if (copy_from_user(&features, featurep, sizeof(features)))
return -EFAULT;
if (features & ~VHOST_9P_FEATURES)
return -EOPNOTSUPP;
return vhost_9p_set_features(n, features);
case VHOST_RESET_OWNER:
return vhost_9p_reset_owner(n);
case VHOST_SET_PATH:
return vhost_9p_set_path(n, argp);
default:
mutex_lock(&n->dev.mutex);
r = vhost_dev_ioctl(&n->dev, ioctl, argp);
if (r == -ENOIOCTLCMD)
r = vhost_vring_ioctl(&n->dev, ioctl, argp);
vhost_9p_flush(n);
mutex_unlock(&n->dev.mutex);
return r;
}
}
#ifdef CONFIG_COMPAT
static long vhost_9p_compat_ioctl(struct file *f, unsigned int ioctl,
unsigned long arg)
{
return vhost_9p_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
}
#endif
static const struct file_operations vhost_9p_fops = {
.owner = THIS_MODULE,
.release = vhost_9p_release,
.unlocked_ioctl = vhost_9p_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = vhost_9p_compat_ioctl,
#endif
.open = vhost_9p_open,
.llseek = noop_llseek,
};
static struct miscdevice vhost_9p_misc = {
MISC_DYNAMIC_MINOR,
"vhost-9p",
&vhost_9p_fops,
};
static int vhost_9p_init(void)
{
return misc_register(&vhost_9p_misc);
}
module_init(vhost_9p_init);
static void vhost_9p_exit(void)
{
misc_deregister(&vhost_9p_misc);
}
module_exit(vhost_9p_exit);
MODULE_VERSION("0.0.1");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Yuankai Guo");
MODULE_DESCRIPTION("In-kernel vhost 9P server");