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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/uio.h>
43#include <linux/notifier.h>
44#include <linux/wait.h>
45#include <linux/fs.h>
46#include <linux/poll.h>
47#include <linux/mutex.h>
48#include <linux/sched.h>
49#include <linux/spinlock.h>
50#include <linux/mount.h>
51#include <linux/pagemap.h>
52#include <linux/uaccess.h>
53#include <linux/init.h>
54#include <linux/namei.h>
55#include <linux/string.h>
56#include <linux/slab.h>
57#include <linux/miscdevice.h>
58#include <linux/module.h>
59
60#include "xenbus_comms.h"
61
62#include <xen/xenbus.h>
63#include <xen/xen.h>
64#include <asm/xen/hypervisor.h>
65
66MODULE_LICENSE("GPL");
67
68
69
70
71
72struct xenbus_transaction_holder {
73 struct list_head list;
74 struct xenbus_transaction handle;
75};
76
77
78
79
80struct read_buffer {
81 struct list_head list;
82 unsigned int cons;
83 unsigned int len;
84 char msg[];
85};
86
87struct xenbus_file_priv {
88
89
90
91
92
93
94
95
96
97
98 struct mutex msgbuffer_mutex;
99
100
101 struct list_head transactions;
102
103
104 struct list_head watches;
105
106
107 unsigned int len;
108 union {
109 struct xsd_sockmsg msg;
110 char buffer[XENSTORE_PAYLOAD_MAX];
111 } u;
112
113
114 struct mutex reply_mutex;
115 struct list_head read_buffers;
116 wait_queue_head_t read_waitq;
117
118};
119
120
121static ssize_t xenbus_file_read(struct file *filp,
122 char __user *ubuf,
123 size_t len, loff_t *ppos)
124{
125 struct xenbus_file_priv *u = filp->private_data;
126 struct read_buffer *rb;
127 unsigned i;
128 int ret;
129
130 mutex_lock(&u->reply_mutex);
131again:
132 while (list_empty(&u->read_buffers)) {
133 mutex_unlock(&u->reply_mutex);
134 if (filp->f_flags & O_NONBLOCK)
135 return -EAGAIN;
136
137 ret = wait_event_interruptible(u->read_waitq,
138 !list_empty(&u->read_buffers));
139 if (ret)
140 return ret;
141 mutex_lock(&u->reply_mutex);
142 }
143
144 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
145 i = 0;
146 while (i < len) {
147 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
148
149 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
150
151 i += sz - ret;
152 rb->cons += sz - ret;
153
154 if (ret != 0) {
155 if (i == 0)
156 i = -EFAULT;
157 goto out;
158 }
159
160
161 if (rb->cons == rb->len) {
162 list_del(&rb->list);
163 kfree(rb);
164 if (list_empty(&u->read_buffers))
165 break;
166 rb = list_entry(u->read_buffers.next,
167 struct read_buffer, list);
168 }
169 }
170 if (i == 0)
171 goto again;
172
173out:
174 mutex_unlock(&u->reply_mutex);
175 return i;
176}
177
178
179
180
181
182
183
184
185static int queue_reply(struct list_head *queue, const void *data, size_t len)
186{
187 struct read_buffer *rb;
188
189 if (len == 0)
190 return 0;
191
192 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
193 if (rb == NULL)
194 return -ENOMEM;
195
196 rb->cons = 0;
197 rb->len = len;
198
199 memcpy(rb->msg, data, len);
200
201 list_add_tail(&rb->list, queue);
202 return 0;
203}
204
205
206
207
208
209static void queue_cleanup(struct list_head *list)
210{
211 struct read_buffer *rb;
212
213 while (!list_empty(list)) {
214 rb = list_entry(list->next, struct read_buffer, list);
215 list_del(list->next);
216 kfree(rb);
217 }
218}
219
220struct watch_adapter {
221 struct list_head list;
222 struct xenbus_watch watch;
223 struct xenbus_file_priv *dev_data;
224 char *token;
225};
226
227static void free_watch_adapter(struct watch_adapter *watch)
228{
229 kfree(watch->watch.node);
230 kfree(watch->token);
231 kfree(watch);
232}
233
234static struct watch_adapter *alloc_watch_adapter(const char *path,
235 const char *token)
236{
237 struct watch_adapter *watch;
238
239 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
240 if (watch == NULL)
241 goto out_fail;
242
243 watch->watch.node = kstrdup(path, GFP_KERNEL);
244 if (watch->watch.node == NULL)
245 goto out_free;
246
247 watch->token = kstrdup(token, GFP_KERNEL);
248 if (watch->token == NULL)
249 goto out_free;
250
251 return watch;
252
253out_free:
254 free_watch_adapter(watch);
255
256out_fail:
257 return NULL;
258}
259
260static void watch_fired(struct xenbus_watch *watch,
261 const char **vec,
262 unsigned int len)
263{
264 struct watch_adapter *adap;
265 struct xsd_sockmsg hdr;
266 const char *path, *token;
267 int path_len, tok_len, body_len, data_len = 0;
268 int ret;
269 LIST_HEAD(staging_q);
270
271 adap = container_of(watch, struct watch_adapter, watch);
272
273 path = vec[XS_WATCH_PATH];
274 token = adap->token;
275
276 path_len = strlen(path) + 1;
277 tok_len = strlen(token) + 1;
278 if (len > 2)
279 data_len = vec[len] - vec[2] + 1;
280 body_len = path_len + tok_len + data_len;
281
282 hdr.type = XS_WATCH_EVENT;
283 hdr.len = body_len;
284
285 mutex_lock(&adap->dev_data->reply_mutex);
286
287 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
288 if (!ret)
289 ret = queue_reply(&staging_q, path, path_len);
290 if (!ret)
291 ret = queue_reply(&staging_q, token, tok_len);
292 if (!ret && len > 2)
293 ret = queue_reply(&staging_q, vec[2], data_len);
294
295 if (!ret) {
296
297 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
298 wake_up(&adap->dev_data->read_waitq);
299 } else
300 queue_cleanup(&staging_q);
301
302 mutex_unlock(&adap->dev_data->reply_mutex);
303}
304
305static int xenbus_write_transaction(unsigned msg_type,
306 struct xenbus_file_priv *u)
307{
308 int rc;
309 void *reply;
310 struct xenbus_transaction_holder *trans = NULL;
311 LIST_HEAD(staging_q);
312
313 if (msg_type == XS_TRANSACTION_START) {
314 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
315 if (!trans) {
316 rc = -ENOMEM;
317 goto out;
318 }
319 }
320
321 reply = xenbus_dev_request_and_reply(&u->u.msg);
322 if (IS_ERR(reply)) {
323 kfree(trans);
324 rc = PTR_ERR(reply);
325 goto out;
326 }
327
328 if (msg_type == XS_TRANSACTION_START) {
329 trans->handle.id = simple_strtoul(reply, NULL, 0);
330
331 list_add(&trans->list, &u->transactions);
332 } else if (msg_type == XS_TRANSACTION_END) {
333 list_for_each_entry(trans, &u->transactions, list)
334 if (trans->handle.id == u->u.msg.tx_id)
335 break;
336 BUG_ON(&trans->list == &u->transactions);
337 list_del(&trans->list);
338
339 kfree(trans);
340 }
341
342 mutex_lock(&u->reply_mutex);
343 rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
344 if (!rc)
345 rc = queue_reply(&staging_q, reply, u->u.msg.len);
346 if (!rc) {
347 list_splice_tail(&staging_q, &u->read_buffers);
348 wake_up(&u->read_waitq);
349 } else {
350 queue_cleanup(&staging_q);
351 }
352 mutex_unlock(&u->reply_mutex);
353
354 kfree(reply);
355
356out:
357 return rc;
358}
359
360static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
361{
362 struct watch_adapter *watch, *tmp_watch;
363 char *path, *token;
364 int err, rc;
365 LIST_HEAD(staging_q);
366
367 path = u->u.buffer + sizeof(u->u.msg);
368 token = memchr(path, 0, u->u.msg.len);
369 if (token == NULL) {
370 rc = -EILSEQ;
371 goto out;
372 }
373 token++;
374 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
375 rc = -EILSEQ;
376 goto out;
377 }
378
379 if (msg_type == XS_WATCH) {
380 watch = alloc_watch_adapter(path, token);
381 if (watch == NULL) {
382 rc = -ENOMEM;
383 goto out;
384 }
385
386 watch->watch.callback = watch_fired;
387 watch->dev_data = u;
388
389 err = register_xenbus_watch(&watch->watch);
390 if (err) {
391 free_watch_adapter(watch);
392 rc = err;
393 goto out;
394 }
395 list_add(&watch->list, &u->watches);
396 } else {
397 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
398 if (!strcmp(watch->token, token) &&
399 !strcmp(watch->watch.node, path)) {
400 unregister_xenbus_watch(&watch->watch);
401 list_del(&watch->list);
402 free_watch_adapter(watch);
403 break;
404 }
405 }
406 }
407
408
409 {
410 struct {
411 struct xsd_sockmsg hdr;
412 char body[3];
413 } __packed reply = {
414 {
415 .type = msg_type,
416 .len = sizeof(reply.body)
417 },
418 "OK"
419 };
420
421 mutex_lock(&u->reply_mutex);
422 rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
423 wake_up(&u->read_waitq);
424 mutex_unlock(&u->reply_mutex);
425 }
426
427out:
428 return rc;
429}
430
431static ssize_t xenbus_file_write(struct file *filp,
432 const char __user *ubuf,
433 size_t len, loff_t *ppos)
434{
435 struct xenbus_file_priv *u = filp->private_data;
436 uint32_t msg_type;
437 int rc = len;
438 int ret;
439 LIST_HEAD(staging_q);
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 mutex_lock(&u->msgbuffer_mutex);
457
458
459 if (len == 0)
460 goto out;
461
462
463 if (len > sizeof(u->u.buffer) - u->len) {
464
465 u->len = 0;
466 rc = -EINVAL;
467 goto out;
468 }
469
470 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
471
472 if (ret != 0) {
473 rc = -EFAULT;
474 goto out;
475 }
476
477
478 len -= ret;
479 rc = len;
480
481 u->len += len;
482
483
484 if (u->len < sizeof(u->u.msg))
485 goto out;
486
487
488
489 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
490 rc = -E2BIG;
491 u->len = 0;
492 goto out;
493 }
494
495 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
496 goto out;
497
498
499
500
501
502 msg_type = u->u.msg.type;
503
504 switch (msg_type) {
505 case XS_WATCH:
506 case XS_UNWATCH:
507
508 ret = xenbus_write_watch(msg_type, u);
509 break;
510
511 default:
512
513 ret = xenbus_write_transaction(msg_type, u);
514 break;
515 }
516 if (ret != 0)
517 rc = ret;
518
519
520 u->len = 0;
521
522 out:
523 mutex_unlock(&u->msgbuffer_mutex);
524 return rc;
525}
526
527static int xenbus_file_open(struct inode *inode, struct file *filp)
528{
529 struct xenbus_file_priv *u;
530
531 if (xen_store_evtchn == 0)
532 return -ENOENT;
533
534 nonseekable_open(inode, filp);
535
536 u = kzalloc(sizeof(*u), GFP_KERNEL);
537 if (u == NULL)
538 return -ENOMEM;
539
540 INIT_LIST_HEAD(&u->transactions);
541 INIT_LIST_HEAD(&u->watches);
542 INIT_LIST_HEAD(&u->read_buffers);
543 init_waitqueue_head(&u->read_waitq);
544
545 mutex_init(&u->reply_mutex);
546 mutex_init(&u->msgbuffer_mutex);
547
548 filp->private_data = u;
549
550 return 0;
551}
552
553static int xenbus_file_release(struct inode *inode, struct file *filp)
554{
555 struct xenbus_file_priv *u = filp->private_data;
556 struct xenbus_transaction_holder *trans, *tmp;
557 struct watch_adapter *watch, *tmp_watch;
558 struct read_buffer *rb, *tmp_rb;
559
560
561
562
563
564
565 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
566 xenbus_transaction_end(trans->handle, 1);
567 list_del(&trans->list);
568 kfree(trans);
569 }
570
571 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
572 unregister_xenbus_watch(&watch->watch);
573 list_del(&watch->list);
574 free_watch_adapter(watch);
575 }
576
577 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
578 list_del(&rb->list);
579 kfree(rb);
580 }
581 kfree(u);
582
583 return 0;
584}
585
586static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
587{
588 struct xenbus_file_priv *u = file->private_data;
589
590 poll_wait(file, &u->read_waitq, wait);
591 if (!list_empty(&u->read_buffers))
592 return POLLIN | POLLRDNORM;
593 return 0;
594}
595
596const struct file_operations xen_xenbus_fops = {
597 .read = xenbus_file_read,
598 .write = xenbus_file_write,
599 .open = xenbus_file_open,
600 .release = xenbus_file_release,
601 .poll = xenbus_file_poll,
602 .llseek = no_llseek,
603};
604EXPORT_SYMBOL_GPL(xen_xenbus_fops);
605
606static struct miscdevice xenbus_dev = {
607 .minor = MISC_DYNAMIC_MINOR,
608 .name = "xen/xenbus",
609 .fops = &xen_xenbus_fops,
610};
611
612static int __init xenbus_init(void)
613{
614 int err;
615
616 if (!xen_domain())
617 return -ENODEV;
618
619 err = misc_register(&xenbus_dev);
620 if (err)
621 pr_err("Could not register xenbus frontend device\n");
622 return err;
623}
624
625static void __exit xenbus_exit(void)
626{
627 misc_deregister(&xenbus_dev);
628}
629
630module_init(xenbus_init);
631module_exit(xenbus_exit);
632