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/init.h>
60 #include <xen/xenbus.h>
62 #include <asm/xen/hypervisor.h>
67 * An element of a list of outstanding transactions, for which we're
68 * still waiting a reply.
70 struct xenbus_transaction_holder {
71 struct list_head list;
72 struct xenbus_transaction handle;
76 * A buffer of data on the queue.
79 struct list_head list;
85 struct xenbus_file_priv {
87 * msgbuffer_mutex is held while partial requests are built up
88 * and complete requests are acted on. It therefore protects
89 * the "transactions" and "watches" lists, and the partial
90 * request length and buffer.
92 * reply_mutex protects the reply being built up to return to
93 * usermode. It nests inside msgbuffer_mutex but may be held
94 * alone during a watch callback.
96 struct mutex msgbuffer_mutex;
98 /* In-progress transactions */
99 struct list_head transactions;
101 /* Active watches. */
102 struct list_head watches;
104 /* Partial request. */
107 struct xsd_sockmsg msg;
108 char buffer[XENSTORE_PAYLOAD_MAX];
111 /* Response queue. */
112 struct mutex reply_mutex;
113 struct list_head read_buffers;
114 wait_queue_head_t read_waitq;
119 /* Read out any raw xenbus messages queued up. */
120 static ssize_t xenbus_file_read(struct file *filp,
122 size_t len, loff_t *ppos)
124 struct xenbus_file_priv *u = filp->private_data;
125 struct read_buffer *rb;
129 mutex_lock(&u->reply_mutex);
131 while (list_empty(&u->read_buffers)) {
132 mutex_unlock(&u->reply_mutex);
133 if (filp->f_flags & O_NONBLOCK)
136 ret = wait_event_interruptible(u->read_waitq,
137 !list_empty(&u->read_buffers));
140 mutex_lock(&u->reply_mutex);
143 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
146 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
148 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
151 rb->cons += sz - ret;
159 /* Clear out buffer if it has been consumed */
160 if (rb->cons == rb->len) {
163 if (list_empty(&u->read_buffers))
165 rb = list_entry(u->read_buffers.next,
166 struct read_buffer, list);
173 mutex_unlock(&u->reply_mutex);
178 * Add a buffer to the queue. Caller must hold the appropriate lock
179 * if the queue is not local. (Commonly the caller will build up
180 * multiple queued buffers on a temporary local list, and then add it
181 * to the appropriate list under lock once all the buffers have een
182 * successfully allocated.)
184 static int queue_reply(struct list_head *queue, const void *data, size_t len)
186 struct read_buffer *rb;
190 if (len > XENSTORE_PAYLOAD_MAX)
193 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
200 memcpy(rb->msg, data, len);
202 list_add_tail(&rb->list, queue);
207 * Free all the read_buffer s on a list.
208 * Caller must have sole reference to list.
210 static void queue_cleanup(struct list_head *list)
212 struct read_buffer *rb;
214 while (!list_empty(list)) {
215 rb = list_entry(list->next, struct read_buffer, list);
216 list_del(list->next);
221 struct watch_adapter {
222 struct list_head list;
223 struct xenbus_watch watch;
224 struct xenbus_file_priv *dev_data;
228 static void free_watch_adapter(struct watch_adapter *watch)
230 kfree(watch->watch.node);
235 static struct watch_adapter *alloc_watch_adapter(const char *path,
238 struct watch_adapter *watch;
240 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
244 watch->watch.node = kstrdup(path, GFP_KERNEL);
245 if (watch->watch.node == NULL)
248 watch->token = kstrdup(token, GFP_KERNEL);
249 if (watch->token == NULL)
255 free_watch_adapter(watch);
261 static void watch_fired(struct xenbus_watch *watch,
265 struct watch_adapter *adap;
266 struct xsd_sockmsg hdr;
267 const char *token_caller;
268 int path_len, tok_len, body_len;
270 LIST_HEAD(staging_q);
272 adap = container_of(watch, struct watch_adapter, watch);
274 token_caller = adap->token;
276 path_len = strlen(path) + 1;
277 tok_len = strlen(token_caller) + 1;
278 body_len = path_len + tok_len;
280 hdr.type = XS_WATCH_EVENT;
283 mutex_lock(&adap->dev_data->reply_mutex);
285 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
287 ret = queue_reply(&staging_q, path, path_len);
289 ret = queue_reply(&staging_q, token_caller, tok_len);
292 /* success: pass reply list onto watcher */
293 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
294 wake_up(&adap->dev_data->read_waitq);
296 queue_cleanup(&staging_q);
298 mutex_unlock(&adap->dev_data->reply_mutex);
301 static void xenbus_file_free(struct kref *kref)
303 struct xenbus_file_priv *u;
304 struct xenbus_transaction_holder *trans, *tmp;
305 struct watch_adapter *watch, *tmp_watch;
306 struct read_buffer *rb, *tmp_rb;
308 u = container_of(kref, struct xenbus_file_priv, kref);
311 * No need for locking here because there are no other users,
315 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
316 xenbus_transaction_end(trans->handle, 1);
317 list_del(&trans->list);
321 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
322 unregister_xenbus_watch(&watch->watch);
323 list_del(&watch->list);
324 free_watch_adapter(watch);
327 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
334 static struct xenbus_transaction_holder *xenbus_get_transaction(
335 struct xenbus_file_priv *u, uint32_t tx_id)
337 struct xenbus_transaction_holder *trans;
339 list_for_each_entry(trans, &u->transactions, list)
340 if (trans->handle.id == tx_id)
346 void xenbus_dev_queue_reply(struct xb_req_data *req)
348 struct xenbus_file_priv *u = req->par;
349 struct xenbus_transaction_holder *trans = NULL;
351 LIST_HEAD(staging_q);
353 xs_request_exit(req);
355 mutex_lock(&u->msgbuffer_mutex);
357 if (req->type == XS_TRANSACTION_START) {
358 trans = xenbus_get_transaction(u, 0);
361 if (req->msg.type == XS_ERROR) {
362 list_del(&trans->list);
365 rc = kstrtou32(req->body, 10, &trans->handle.id);
369 } else if (req->msg.type == XS_TRANSACTION_END) {
370 trans = xenbus_get_transaction(u, req->msg.tx_id);
373 list_del(&trans->list);
377 mutex_unlock(&u->msgbuffer_mutex);
379 mutex_lock(&u->reply_mutex);
380 rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
382 rc = queue_reply(&staging_q, req->body, req->msg.len);
384 list_splice_tail(&staging_q, &u->read_buffers);
385 wake_up(&u->read_waitq);
387 queue_cleanup(&staging_q);
389 mutex_unlock(&u->reply_mutex);
394 kref_put(&u->kref, xenbus_file_free);
399 mutex_unlock(&u->msgbuffer_mutex);
402 static int xenbus_command_reply(struct xenbus_file_priv *u,
403 unsigned int msg_type, const char *reply)
406 struct xsd_sockmsg hdr;
412 msg.hdr.type = msg_type;
413 msg.hdr.len = strlen(reply) + 1;
414 if (msg.hdr.len > sizeof(msg.body))
417 mutex_lock(&u->reply_mutex);
418 rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
419 wake_up(&u->read_waitq);
420 mutex_unlock(&u->reply_mutex);
423 kref_put(&u->kref, xenbus_file_free);
428 static int xenbus_write_transaction(unsigned msg_type,
429 struct xenbus_file_priv *u)
432 struct xenbus_transaction_holder *trans = NULL;
434 if (msg_type == XS_TRANSACTION_START) {
435 trans = kzalloc(sizeof(*trans), GFP_KERNEL);
440 list_add(&trans->list, &u->transactions);
441 } else if (u->u.msg.tx_id != 0 &&
442 !xenbus_get_transaction(u, u->u.msg.tx_id))
443 return xenbus_command_reply(u, XS_ERROR, "ENOENT");
445 rc = xenbus_dev_request_and_reply(&u->u.msg, u);
453 static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
455 struct watch_adapter *watch;
458 LIST_HEAD(staging_q);
460 path = u->u.buffer + sizeof(u->u.msg);
461 token = memchr(path, 0, u->u.msg.len);
463 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
467 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
468 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
472 if (msg_type == XS_WATCH) {
473 watch = alloc_watch_adapter(path, token);
479 watch->watch.callback = watch_fired;
482 err = register_xenbus_watch(&watch->watch);
484 free_watch_adapter(watch);
488 list_add(&watch->list, &u->watches);
490 list_for_each_entry(watch, &u->watches, list) {
491 if (!strcmp(watch->token, token) &&
492 !strcmp(watch->watch.node, path)) {
493 unregister_xenbus_watch(&watch->watch);
494 list_del(&watch->list);
495 free_watch_adapter(watch);
501 /* Success. Synthesize a reply to say all is OK. */
502 rc = xenbus_command_reply(u, msg_type, "OK");
508 static ssize_t xenbus_file_write(struct file *filp,
509 const char __user *ubuf,
510 size_t len, loff_t *ppos)
512 struct xenbus_file_priv *u = filp->private_data;
516 LIST_HEAD(staging_q);
519 * We're expecting usermode to be writing properly formed
520 * xenbus messages. If they write an incomplete message we
521 * buffer it up. Once it is complete, we act on it.
525 * Make sure concurrent writers can't stomp all over each
526 * other's messages and make a mess of our partial message
527 * buffer. We don't make any attemppt to stop multiple
528 * writers from making a mess of each other's incomplete
529 * messages; we're just trying to guarantee our own internal
530 * consistency and make sure that single writes are handled
533 mutex_lock(&u->msgbuffer_mutex);
535 /* Get this out of the way early to avoid confusion */
539 /* Can't write a xenbus message larger we can buffer */
540 if (len > sizeof(u->u.buffer) - u->len) {
541 /* On error, dump existing buffer */
547 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
554 /* Deal with a partial copy. */
560 /* Return if we haven't got a full message yet */
561 if (u->len < sizeof(u->u.msg))
562 goto out; /* not even the header yet */
564 /* If we're expecting a message that's larger than we can
565 possibly send, dump what we have and return an error. */
566 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
572 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
573 goto out; /* incomplete data portion */
576 * OK, now we have a complete message. Do something with it.
581 msg_type = u->u.msg.type;
586 /* (Un)Ask for some path to be watched for changes */
587 ret = xenbus_write_watch(msg_type, u);
591 /* Send out a transaction */
592 ret = xenbus_write_transaction(msg_type, u);
597 kref_put(&u->kref, xenbus_file_free);
600 /* Buffered message consumed */
604 mutex_unlock(&u->msgbuffer_mutex);
608 static int xenbus_file_open(struct inode *inode, struct file *filp)
610 struct xenbus_file_priv *u;
612 if (xen_store_evtchn == 0)
615 nonseekable_open(inode, filp);
617 filp->f_mode &= ~FMODE_ATOMIC_POS; /* cdev-style semantics */
619 u = kzalloc(sizeof(*u), GFP_KERNEL);
625 INIT_LIST_HEAD(&u->transactions);
626 INIT_LIST_HEAD(&u->watches);
627 INIT_LIST_HEAD(&u->read_buffers);
628 init_waitqueue_head(&u->read_waitq);
630 mutex_init(&u->reply_mutex);
631 mutex_init(&u->msgbuffer_mutex);
633 filp->private_data = u;
638 static int xenbus_file_release(struct inode *inode, struct file *filp)
640 struct xenbus_file_priv *u = filp->private_data;
642 kref_put(&u->kref, xenbus_file_free);
647 static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
649 struct xenbus_file_priv *u = file->private_data;
651 poll_wait(file, &u->read_waitq, wait);
652 if (!list_empty(&u->read_buffers))
653 return POLLIN | POLLRDNORM;
657 const struct file_operations xen_xenbus_fops = {
658 .read = xenbus_file_read,
659 .write = xenbus_file_write,
660 .open = xenbus_file_open,
661 .release = xenbus_file_release,
662 .poll = xenbus_file_poll,
665 EXPORT_SYMBOL_GPL(xen_xenbus_fops);
667 static struct miscdevice xenbus_dev = {
668 .minor = MISC_DYNAMIC_MINOR,
669 .name = "xen/xenbus",
670 .fops = &xen_xenbus_fops,
673 static int __init xenbus_init(void)
680 err = misc_register(&xenbus_dev);
682 pr_err("Could not register xenbus frontend device\n");
685 device_initcall(xenbus_init);