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
59#include <xen/xenbus.h>
60#include <xen/xen.h>
61#include <asm/xen/hypervisor.h>
62
63#include "xenbus.h"
64
65
66
67
68
69struct xenbus_transaction_holder {
70 struct list_head list;
71 struct xenbus_transaction handle;
72};
73
74
75
76
77struct read_buffer {
78 struct list_head list;
79 unsigned int cons;
80 unsigned int len;
81 char msg[];
82};
83
84struct xenbus_file_priv {
85
86
87
88
89
90
91
92
93
94
95 struct mutex msgbuffer_mutex;
96
97
98 struct list_head transactions;
99
100
101 struct list_head watches;
102
103
104 unsigned int len;
105 union {
106 struct xsd_sockmsg msg;
107 char buffer[XENSTORE_PAYLOAD_MAX];
108 } u;
109
110
111 struct mutex reply_mutex;
112 struct list_head read_buffers;
113 wait_queue_head_t read_waitq;
114
115 struct kref kref;
116};
117
118
119static ssize_t xenbus_file_read(struct file *filp,
120 char __user *ubuf,
121 size_t len, loff_t *ppos)
122{
123 struct xenbus_file_priv *u = filp->private_data;
124 struct read_buffer *rb;
125 unsigned i;
126 int ret;
127
128 mutex_lock(&u->reply_mutex);
129again:
130 while (list_empty(&u->read_buffers)) {
131 mutex_unlock(&u->reply_mutex);
132 if (filp->f_flags & O_NONBLOCK)
133 return -EAGAIN;
134
135 ret = wait_event_interruptible(u->read_waitq,
136 !list_empty(&u->read_buffers));
137 if (ret)
138 return ret;
139 mutex_lock(&u->reply_mutex);
140 }
141
142 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
143 i = 0;
144 while (i < len) {
145 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
146
147 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
148
149 i += sz - ret;
150 rb->cons += sz - ret;
151
152 if (ret != 0) {
153 if (i == 0)
154 i = -EFAULT;
155 goto out;
156 }
157
158
159 if (rb->cons == rb->len) {
160 list_del(&rb->list);
161 kfree(rb);
162 if (list_empty(&u->read_buffers))
163 break;
164 rb = list_entry(u->read_buffers.next,
165 struct read_buffer, list);
166 }
167 }
168 if (i == 0)
169 goto again;
170
171out:
172 mutex_unlock(&u->reply_mutex);
173 return i;
174}
175
176
177
178
179
180
181
182
183static int queue_reply(struct list_head *queue, const void *data, size_t len)
184{
185 struct read_buffer *rb;
186
187 if (len == 0)
188 return 0;
189 if (len > XENSTORE_PAYLOAD_MAX)
190 return -EINVAL;
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 *path,
262 const char *token)
263{
264 struct watch_adapter *adap;
265 struct xsd_sockmsg hdr;
266 const char *token_caller;
267 int path_len, tok_len, body_len;
268 int ret;
269 LIST_HEAD(staging_q);
270
271 adap = container_of(watch, struct watch_adapter, watch);
272
273 token_caller = adap->token;
274
275 path_len = strlen(path) + 1;
276 tok_len = strlen(token_caller) + 1;
277 body_len = path_len + tok_len;
278
279 hdr.type = XS_WATCH_EVENT;
280 hdr.len = body_len;
281
282 mutex_lock(&adap->dev_data->reply_mutex);
283
284 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
285 if (!ret)
286 ret = queue_reply(&staging_q, path, path_len);
287 if (!ret)
288 ret = queue_reply(&staging_q, token_caller, tok_len);
289
290 if (!ret) {
291
292 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
293 wake_up(&adap->dev_data->read_waitq);
294 } else
295 queue_cleanup(&staging_q);
296
297 mutex_unlock(&adap->dev_data->reply_mutex);
298}
299
300static void xenbus_file_free(struct kref *kref)
301{
302 struct xenbus_file_priv *u;
303 struct xenbus_transaction_holder *trans, *tmp;
304 struct watch_adapter *watch, *tmp_watch;
305 struct read_buffer *rb, *tmp_rb;
306
307 u = container_of(kref, struct xenbus_file_priv, kref);
308
309
310
311
312
313
314 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
315 xenbus_transaction_end(trans->handle, 1);
316 list_del(&trans->list);
317 kfree(trans);
318 }
319
320 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
321 unregister_xenbus_watch(&watch->watch);
322 list_del(&watch->list);
323 free_watch_adapter(watch);
324 }
325
326 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
327 list_del(&rb->list);
328 kfree(rb);
329 }
330 kfree(u);
331}
332
333static struct xenbus_transaction_holder *xenbus_get_transaction(
334 struct xenbus_file_priv *u, uint32_t tx_id)
335{
336 struct xenbus_transaction_holder *trans;
337
338 list_for_each_entry(trans, &u->transactions, list)
339 if (trans->handle.id == tx_id)
340 return trans;
341
342 return NULL;
343}
344
345void xenbus_dev_queue_reply(struct xb_req_data *req)
346{
347 struct xenbus_file_priv *u = req->par;
348 struct xenbus_transaction_holder *trans = NULL;
349 int rc;
350 LIST_HEAD(staging_q);
351
352 xs_request_exit(req);
353
354 mutex_lock(&u->msgbuffer_mutex);
355
356 if (req->type == XS_TRANSACTION_START) {
357 trans = xenbus_get_transaction(u, 0);
358 if (WARN_ON(!trans))
359 goto out;
360 if (req->msg.type == XS_ERROR) {
361 list_del(&trans->list);
362 kfree(trans);
363 } else {
364 rc = kstrtou32(req->body, 10, &trans->handle.id);
365 if (WARN_ON(rc))
366 goto out;
367 }
368 } else if (req->msg.type == XS_TRANSACTION_END) {
369 trans = xenbus_get_transaction(u, req->msg.tx_id);
370 if (WARN_ON(!trans))
371 goto out;
372 list_del(&trans->list);
373 kfree(trans);
374 }
375
376 mutex_unlock(&u->msgbuffer_mutex);
377
378 mutex_lock(&u->reply_mutex);
379 rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
380 if (!rc)
381 rc = queue_reply(&staging_q, req->body, req->msg.len);
382 if (!rc) {
383 list_splice_tail(&staging_q, &u->read_buffers);
384 wake_up(&u->read_waitq);
385 } else {
386 queue_cleanup(&staging_q);
387 }
388 mutex_unlock(&u->reply_mutex);
389
390 kfree(req->body);
391 kfree(req);
392
393 kref_put(&u->kref, xenbus_file_free);
394
395 return;
396
397 out:
398 mutex_unlock(&u->msgbuffer_mutex);
399}
400
401static int xenbus_command_reply(struct xenbus_file_priv *u,
402 unsigned int msg_type, const char *reply)
403{
404 struct {
405 struct xsd_sockmsg hdr;
406 const char body[16];
407 } msg;
408 int rc;
409
410 msg.hdr = u->u.msg;
411 msg.hdr.type = msg_type;
412 msg.hdr.len = strlen(reply) + 1;
413 if (msg.hdr.len > sizeof(msg.body))
414 return -E2BIG;
415
416 mutex_lock(&u->reply_mutex);
417 rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
418 wake_up(&u->read_waitq);
419 mutex_unlock(&u->reply_mutex);
420
421 if (!rc)
422 kref_put(&u->kref, xenbus_file_free);
423
424 return rc;
425}
426
427static int xenbus_write_transaction(unsigned msg_type,
428 struct xenbus_file_priv *u)
429{
430 int rc;
431 struct xenbus_transaction_holder *trans = NULL;
432
433 if (msg_type == XS_TRANSACTION_START) {
434 trans = kzalloc(sizeof(*trans), GFP_KERNEL);
435 if (!trans) {
436 rc = -ENOMEM;
437 goto out;
438 }
439 list_add(&trans->list, &u->transactions);
440 } else if (u->u.msg.tx_id != 0 &&
441 !xenbus_get_transaction(u, u->u.msg.tx_id))
442 return xenbus_command_reply(u, XS_ERROR, "ENOENT");
443
444 rc = xenbus_dev_request_and_reply(&u->u.msg, u);
445 if (rc && trans) {
446 list_del(&trans->list);
447 kfree(trans);
448 }
449
450out:
451 return rc;
452}
453
454static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
455{
456 struct watch_adapter *watch;
457 char *path, *token;
458 int err, rc;
459 LIST_HEAD(staging_q);
460
461 path = u->u.buffer + sizeof(u->u.msg);
462 token = memchr(path, 0, u->u.msg.len);
463 if (token == NULL) {
464 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
465 goto out;
466 }
467 token++;
468 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
469 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
470 goto out;
471 }
472
473 if (msg_type == XS_WATCH) {
474 watch = alloc_watch_adapter(path, token);
475 if (watch == NULL) {
476 rc = -ENOMEM;
477 goto out;
478 }
479
480 watch->watch.callback = watch_fired;
481 watch->dev_data = u;
482
483 err = register_xenbus_watch(&watch->watch);
484 if (err) {
485 free_watch_adapter(watch);
486 rc = err;
487 goto out;
488 }
489 list_add(&watch->list, &u->watches);
490 } else {
491 list_for_each_entry(watch, &u->watches, list) {
492 if (!strcmp(watch->token, token) &&
493 !strcmp(watch->watch.node, path)) {
494 unregister_xenbus_watch(&watch->watch);
495 list_del(&watch->list);
496 free_watch_adapter(watch);
497 break;
498 }
499 }
500 }
501
502
503 rc = xenbus_command_reply(u, msg_type, "OK");
504
505out:
506 return rc;
507}
508
509static ssize_t xenbus_file_write(struct file *filp,
510 const char __user *ubuf,
511 size_t len, loff_t *ppos)
512{
513 struct xenbus_file_priv *u = filp->private_data;
514 uint32_t msg_type;
515 int rc = len;
516 int ret;
517 LIST_HEAD(staging_q);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 mutex_lock(&u->msgbuffer_mutex);
535
536
537 if (len == 0)
538 goto out;
539
540
541 if (len > sizeof(u->u.buffer) - u->len) {
542
543 u->len = 0;
544 rc = -EINVAL;
545 goto out;
546 }
547
548 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
549
550 if (ret != 0) {
551 rc = -EFAULT;
552 goto out;
553 }
554
555
556 len -= ret;
557 rc = len;
558
559 u->len += len;
560
561
562 if (u->len < sizeof(u->u.msg))
563 goto out;
564
565
566
567 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
568 rc = -E2BIG;
569 u->len = 0;
570 goto out;
571 }
572
573 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
574 goto out;
575
576
577
578
579
580 kref_get(&u->kref);
581
582 msg_type = u->u.msg.type;
583
584 switch (msg_type) {
585 case XS_WATCH:
586 case XS_UNWATCH:
587
588 ret = xenbus_write_watch(msg_type, u);
589 break;
590
591 default:
592
593 ret = xenbus_write_transaction(msg_type, u);
594 break;
595 }
596 if (ret != 0) {
597 rc = ret;
598 kref_put(&u->kref, xenbus_file_free);
599 }
600
601
602 u->len = 0;
603
604 out:
605 mutex_unlock(&u->msgbuffer_mutex);
606 return rc;
607}
608
609static int xenbus_file_open(struct inode *inode, struct file *filp)
610{
611 struct xenbus_file_priv *u;
612
613 if (xen_store_evtchn == 0)
614 return -ENOENT;
615
616 nonseekable_open(inode, filp);
617
618 filp->f_mode &= ~FMODE_ATOMIC_POS;
619
620 u = kzalloc(sizeof(*u), GFP_KERNEL);
621 if (u == NULL)
622 return -ENOMEM;
623
624 kref_init(&u->kref);
625
626 INIT_LIST_HEAD(&u->transactions);
627 INIT_LIST_HEAD(&u->watches);
628 INIT_LIST_HEAD(&u->read_buffers);
629 init_waitqueue_head(&u->read_waitq);
630
631 mutex_init(&u->reply_mutex);
632 mutex_init(&u->msgbuffer_mutex);
633
634 filp->private_data = u;
635
636 return 0;
637}
638
639static int xenbus_file_release(struct inode *inode, struct file *filp)
640{
641 struct xenbus_file_priv *u = filp->private_data;
642
643 kref_put(&u->kref, xenbus_file_free);
644
645 return 0;
646}
647
648static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
649{
650 struct xenbus_file_priv *u = file->private_data;
651
652 poll_wait(file, &u->read_waitq, wait);
653 if (!list_empty(&u->read_buffers))
654 return POLLIN | POLLRDNORM;
655 return 0;
656}
657
658const struct file_operations xen_xenbus_fops = {
659 .read = xenbus_file_read,
660 .write = xenbus_file_write,
661 .open = xenbus_file_open,
662 .release = xenbus_file_release,
663 .poll = xenbus_file_poll,
664 .llseek = no_llseek,
665};
666EXPORT_SYMBOL_GPL(xen_xenbus_fops);
667
668static struct miscdevice xenbus_dev = {
669 .minor = MISC_DYNAMIC_MINOR,
670 .name = "xen/xenbus",
671 .fops = &xen_xenbus_fops,
672};
673
674static int __init xenbus_init(void)
675{
676 int err;
677
678 if (!xen_domain())
679 return -ENODEV;
680
681 err = misc_register(&xenbus_dev);
682 if (err)
683 pr_err("Could not register xenbus frontend device\n");
684 return err;
685}
686device_initcall(xenbus_init);
687