2 * Driver giving user-space access to the kernel's xenbus connection
5 * Copyright (c) 2005, Christian Limpach
6 * Copyright (c) 2005, Rusty Russell, IBM Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
34 * and /proc/xen compatibility mount point.
35 * Turned xenfs into a loadable module.
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
60 #include "xenbus_comms.h"
62 #include <xen/xenbus.h>
64 #include <asm/xen/hypervisor.h>
66 MODULE_LICENSE("GPL");
69 * An element of a list of outstanding transactions, for which we're
70 * still waiting a reply.
72 struct xenbus_transaction_holder {
73 struct list_head list;
74 struct xenbus_transaction handle;
78 * A buffer of data on the queue.
81 struct list_head list;
87 struct xenbus_file_priv {
89 * msgbuffer_mutex is held while partial requests are built up
90 * and complete requests are acted on. It therefore protects
91 * the "transactions" and "watches" lists, and the partial
92 * request length and buffer.
94 * reply_mutex protects the reply being built up to return to
95 * usermode. It nests inside msgbuffer_mutex but may be held
96 * alone during a watch callback.
98 struct mutex msgbuffer_mutex;
100 /* In-progress transactions */
101 struct list_head transactions;
103 /* Active watches. */
104 struct list_head watches;
106 /* Partial request. */
109 struct xsd_sockmsg msg;
110 char buffer[XENSTORE_PAYLOAD_MAX];
113 /* Response queue. */
114 struct mutex reply_mutex;
115 struct list_head read_buffers;
116 wait_queue_head_t read_waitq;
120 /* Read out any raw xenbus messages queued up. */
121 static ssize_t xenbus_file_read(struct file *filp,
123 size_t len, loff_t *ppos)
125 struct xenbus_file_priv *u = filp->private_data;
126 struct read_buffer *rb;
130 mutex_lock(&u->reply_mutex);
132 while (list_empty(&u->read_buffers)) {
133 mutex_unlock(&u->reply_mutex);
134 if (filp->f_flags & O_NONBLOCK)
137 ret = wait_event_interruptible(u->read_waitq,
138 !list_empty(&u->read_buffers));
141 mutex_lock(&u->reply_mutex);
144 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
147 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
149 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
152 rb->cons += sz - ret;
160 /* Clear out buffer if it has been consumed */
161 if (rb->cons == rb->len) {
164 if (list_empty(&u->read_buffers))
166 rb = list_entry(u->read_buffers.next,
167 struct read_buffer, list);
174 mutex_unlock(&u->reply_mutex);
179 * Add a buffer to the queue. Caller must hold the appropriate lock
180 * if the queue is not local. (Commonly the caller will build up
181 * multiple queued buffers on a temporary local list, and then add it
182 * to the appropriate list under lock once all the buffers have een
183 * successfully allocated.)
185 static int queue_reply(struct list_head *queue, const void *data, size_t len)
187 struct read_buffer *rb;
192 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
199 memcpy(rb->msg, data, len);
201 list_add_tail(&rb->list, queue);
206 * Free all the read_buffer s on a list.
207 * Caller must have sole reference to list.
209 static void queue_cleanup(struct list_head *list)
211 struct read_buffer *rb;
213 while (!list_empty(list)) {
214 rb = list_entry(list->next, struct read_buffer, list);
215 list_del(list->next);
220 struct watch_adapter {
221 struct list_head list;
222 struct xenbus_watch watch;
223 struct xenbus_file_priv *dev_data;
227 static void free_watch_adapter(struct watch_adapter *watch)
229 kfree(watch->watch.node);
234 static struct watch_adapter *alloc_watch_adapter(const char *path,
237 struct watch_adapter *watch;
239 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
243 watch->watch.node = kstrdup(path, GFP_KERNEL);
244 if (watch->watch.node == NULL)
247 watch->token = kstrdup(token, GFP_KERNEL);
248 if (watch->token == NULL)
254 free_watch_adapter(watch);
260 static void watch_fired(struct xenbus_watch *watch,
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;
269 LIST_HEAD(staging_q);
271 adap = container_of(watch, struct watch_adapter, watch);
273 path = vec[XS_WATCH_PATH];
276 path_len = strlen(path) + 1;
277 tok_len = strlen(token) + 1;
279 data_len = vec[len] - vec[2] + 1;
280 body_len = path_len + tok_len + data_len;
282 hdr.type = XS_WATCH_EVENT;
285 mutex_lock(&adap->dev_data->reply_mutex);
287 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
289 ret = queue_reply(&staging_q, path, path_len);
291 ret = queue_reply(&staging_q, token, tok_len);
293 ret = queue_reply(&staging_q, vec[2], data_len);
296 /* success: pass reply list onto watcher */
297 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
298 wake_up(&adap->dev_data->read_waitq);
300 queue_cleanup(&staging_q);
302 mutex_unlock(&adap->dev_data->reply_mutex);
305 static int xenbus_write_transaction(unsigned msg_type,
306 struct xenbus_file_priv *u)
310 struct xenbus_transaction_holder *trans = NULL;
311 LIST_HEAD(staging_q);
313 if (msg_type == XS_TRANSACTION_START) {
314 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
321 reply = xenbus_dev_request_and_reply(&u->u.msg);
328 if (msg_type == XS_TRANSACTION_START) {
329 if (u->u.msg.type == XS_ERROR)
332 trans->handle.id = simple_strtoul(reply, NULL, 0);
333 list_add(&trans->list, &u->transactions);
335 } else if (u->u.msg.type == XS_TRANSACTION_END) {
336 list_for_each_entry(trans, &u->transactions, list)
337 if (trans->handle.id == u->u.msg.tx_id)
339 BUG_ON(&trans->list == &u->transactions);
340 list_del(&trans->list);
345 mutex_lock(&u->reply_mutex);
346 rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
348 rc = queue_reply(&staging_q, reply, u->u.msg.len);
350 list_splice_tail(&staging_q, &u->read_buffers);
351 wake_up(&u->read_waitq);
353 queue_cleanup(&staging_q);
355 mutex_unlock(&u->reply_mutex);
363 static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
365 struct watch_adapter *watch, *tmp_watch;
368 LIST_HEAD(staging_q);
370 path = u->u.buffer + sizeof(u->u.msg);
371 token = memchr(path, 0, u->u.msg.len);
377 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
382 if (msg_type == XS_WATCH) {
383 watch = alloc_watch_adapter(path, token);
389 watch->watch.callback = watch_fired;
392 err = register_xenbus_watch(&watch->watch);
394 free_watch_adapter(watch);
398 list_add(&watch->list, &u->watches);
400 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
401 if (!strcmp(watch->token, token) &&
402 !strcmp(watch->watch.node, path)) {
403 unregister_xenbus_watch(&watch->watch);
404 list_del(&watch->list);
405 free_watch_adapter(watch);
411 /* Success. Synthesize a reply to say all is OK. */
414 struct xsd_sockmsg hdr;
419 .len = sizeof(reply.body)
424 mutex_lock(&u->reply_mutex);
425 rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
426 wake_up(&u->read_waitq);
427 mutex_unlock(&u->reply_mutex);
434 static ssize_t xenbus_file_write(struct file *filp,
435 const char __user *ubuf,
436 size_t len, loff_t *ppos)
438 struct xenbus_file_priv *u = filp->private_data;
442 LIST_HEAD(staging_q);
445 * We're expecting usermode to be writing properly formed
446 * xenbus messages. If they write an incomplete message we
447 * buffer it up. Once it is complete, we act on it.
451 * Make sure concurrent writers can't stomp all over each
452 * other's messages and make a mess of our partial message
453 * buffer. We don't make any attemppt to stop multiple
454 * writers from making a mess of each other's incomplete
455 * messages; we're just trying to guarantee our own internal
456 * consistency and make sure that single writes are handled
459 mutex_lock(&u->msgbuffer_mutex);
461 /* Get this out of the way early to avoid confusion */
465 /* Can't write a xenbus message larger we can buffer */
466 if (len > sizeof(u->u.buffer) - u->len) {
467 /* On error, dump existing buffer */
473 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
480 /* Deal with a partial copy. */
486 /* Return if we haven't got a full message yet */
487 if (u->len < sizeof(u->u.msg))
488 goto out; /* not even the header yet */
490 /* If we're expecting a message that's larger than we can
491 possibly send, dump what we have and return an error. */
492 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
498 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
499 goto out; /* incomplete data portion */
502 * OK, now we have a complete message. Do something with it.
505 msg_type = u->u.msg.type;
510 /* (Un)Ask for some path to be watched for changes */
511 ret = xenbus_write_watch(msg_type, u);
515 /* Send out a transaction */
516 ret = xenbus_write_transaction(msg_type, u);
522 /* Buffered message consumed */
526 mutex_unlock(&u->msgbuffer_mutex);
530 static int xenbus_file_open(struct inode *inode, struct file *filp)
532 struct xenbus_file_priv *u;
534 if (xen_store_evtchn == 0)
537 nonseekable_open(inode, filp);
539 u = kzalloc(sizeof(*u), GFP_KERNEL);
543 INIT_LIST_HEAD(&u->transactions);
544 INIT_LIST_HEAD(&u->watches);
545 INIT_LIST_HEAD(&u->read_buffers);
546 init_waitqueue_head(&u->read_waitq);
548 mutex_init(&u->reply_mutex);
549 mutex_init(&u->msgbuffer_mutex);
551 filp->private_data = u;
556 static int xenbus_file_release(struct inode *inode, struct file *filp)
558 struct xenbus_file_priv *u = filp->private_data;
559 struct xenbus_transaction_holder *trans, *tmp;
560 struct watch_adapter *watch, *tmp_watch;
561 struct read_buffer *rb, *tmp_rb;
564 * No need for locking here because there are no other users,
568 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
569 xenbus_transaction_end(trans->handle, 1);
570 list_del(&trans->list);
574 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
575 unregister_xenbus_watch(&watch->watch);
576 list_del(&watch->list);
577 free_watch_adapter(watch);
580 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
589 static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
591 struct xenbus_file_priv *u = file->private_data;
593 poll_wait(file, &u->read_waitq, wait);
594 if (!list_empty(&u->read_buffers))
595 return POLLIN | POLLRDNORM;
599 const struct file_operations xen_xenbus_fops = {
600 .read = xenbus_file_read,
601 .write = xenbus_file_write,
602 .open = xenbus_file_open,
603 .release = xenbus_file_release,
604 .poll = xenbus_file_poll,
607 EXPORT_SYMBOL_GPL(xen_xenbus_fops);
609 static struct miscdevice xenbus_dev = {
610 .minor = MISC_DYNAMIC_MINOR,
611 .name = "xen/xenbus",
612 .fops = &xen_xenbus_fops,
615 static int __init xenbus_init(void)
622 err = misc_register(&xenbus_dev);
624 pr_err("Could not register xenbus frontend device\n");
628 static void __exit xenbus_exit(void)
630 misc_deregister(&xenbus_dev);
633 module_init(xenbus_init);
634 module_exit(xenbus_exit);