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/workqueue.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;
118 struct work_struct wq;
121 /* Read out any raw xenbus messages queued up. */
122 static ssize_t xenbus_file_read(struct file *filp,
124 size_t len, loff_t *ppos)
126 struct xenbus_file_priv *u = filp->private_data;
127 struct read_buffer *rb;
131 mutex_lock(&u->reply_mutex);
133 while (list_empty(&u->read_buffers)) {
134 mutex_unlock(&u->reply_mutex);
135 if (filp->f_flags & O_NONBLOCK)
138 ret = wait_event_interruptible(u->read_waitq,
139 !list_empty(&u->read_buffers));
142 mutex_lock(&u->reply_mutex);
145 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
148 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
150 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
153 rb->cons += sz - ret;
161 /* Clear out buffer if it has been consumed */
162 if (rb->cons == rb->len) {
165 if (list_empty(&u->read_buffers))
167 rb = list_entry(u->read_buffers.next,
168 struct read_buffer, list);
175 mutex_unlock(&u->reply_mutex);
180 * Add a buffer to the queue. Caller must hold the appropriate lock
181 * if the queue is not local. (Commonly the caller will build up
182 * multiple queued buffers on a temporary local list, and then add it
183 * to the appropriate list under lock once all the buffers have een
184 * successfully allocated.)
186 static int queue_reply(struct list_head *queue, const void *data, size_t len)
188 struct read_buffer *rb;
192 if (len > XENSTORE_PAYLOAD_MAX)
195 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
202 memcpy(rb->msg, data, len);
204 list_add_tail(&rb->list, queue);
209 * Free all the read_buffer s on a list.
210 * Caller must have sole reference to list.
212 static void queue_cleanup(struct list_head *list)
214 struct read_buffer *rb;
216 while (!list_empty(list)) {
217 rb = list_entry(list->next, struct read_buffer, list);
218 list_del(list->next);
223 struct watch_adapter {
224 struct list_head list;
225 struct xenbus_watch watch;
226 struct xenbus_file_priv *dev_data;
230 static void free_watch_adapter(struct watch_adapter *watch)
232 kfree(watch->watch.node);
237 static struct watch_adapter *alloc_watch_adapter(const char *path,
240 struct watch_adapter *watch;
242 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
246 watch->watch.node = kstrdup(path, GFP_KERNEL);
247 if (watch->watch.node == NULL)
250 watch->token = kstrdup(token, GFP_KERNEL);
251 if (watch->token == NULL)
257 free_watch_adapter(watch);
263 static void watch_fired(struct xenbus_watch *watch,
267 struct watch_adapter *adap;
268 struct xsd_sockmsg hdr;
269 const char *token_caller;
270 int path_len, tok_len, body_len;
272 LIST_HEAD(staging_q);
274 adap = container_of(watch, struct watch_adapter, watch);
276 token_caller = adap->token;
278 path_len = strlen(path) + 1;
279 tok_len = strlen(token_caller) + 1;
280 body_len = path_len + tok_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_caller, tok_len);
294 /* success: pass reply list onto watcher */
295 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
296 wake_up(&adap->dev_data->read_waitq);
298 queue_cleanup(&staging_q);
300 mutex_unlock(&adap->dev_data->reply_mutex);
303 static void xenbus_worker(struct work_struct *wq)
305 struct xenbus_file_priv *u;
306 struct xenbus_transaction_holder *trans, *tmp;
307 struct watch_adapter *watch, *tmp_watch;
308 struct read_buffer *rb, *tmp_rb;
310 u = container_of(wq, struct xenbus_file_priv, wq);
313 * No need for locking here because there are no other users,
317 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
318 xenbus_transaction_end(trans->handle, 1);
319 list_del(&trans->list);
323 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
324 unregister_xenbus_watch(&watch->watch);
325 list_del(&watch->list);
326 free_watch_adapter(watch);
329 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
336 static void xenbus_file_free(struct kref *kref)
338 struct xenbus_file_priv *u;
341 * We might be called in xenbus_thread().
342 * Use workqueue to avoid deadlock.
344 u = container_of(kref, struct xenbus_file_priv, kref);
345 schedule_work(&u->wq);
348 static struct xenbus_transaction_holder *xenbus_get_transaction(
349 struct xenbus_file_priv *u, uint32_t tx_id)
351 struct xenbus_transaction_holder *trans;
353 list_for_each_entry(trans, &u->transactions, list)
354 if (trans->handle.id == tx_id)
360 void xenbus_dev_queue_reply(struct xb_req_data *req)
362 struct xenbus_file_priv *u = req->par;
363 struct xenbus_transaction_holder *trans = NULL;
365 LIST_HEAD(staging_q);
367 xs_request_exit(req);
369 mutex_lock(&u->msgbuffer_mutex);
371 if (req->type == XS_TRANSACTION_START) {
372 trans = xenbus_get_transaction(u, 0);
375 if (req->msg.type == XS_ERROR) {
376 list_del(&trans->list);
379 rc = kstrtou32(req->body, 10, &trans->handle.id);
383 } else if (req->type == XS_TRANSACTION_END) {
384 trans = xenbus_get_transaction(u, req->msg.tx_id);
387 list_del(&trans->list);
391 mutex_unlock(&u->msgbuffer_mutex);
393 mutex_lock(&u->reply_mutex);
394 rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
396 rc = queue_reply(&staging_q, req->body, req->msg.len);
398 list_splice_tail(&staging_q, &u->read_buffers);
399 wake_up(&u->read_waitq);
401 queue_cleanup(&staging_q);
403 mutex_unlock(&u->reply_mutex);
408 kref_put(&u->kref, xenbus_file_free);
413 mutex_unlock(&u->msgbuffer_mutex);
416 static int xenbus_command_reply(struct xenbus_file_priv *u,
417 unsigned int msg_type, const char *reply)
420 struct xsd_sockmsg hdr;
426 msg.hdr.type = msg_type;
427 msg.hdr.len = strlen(reply) + 1;
428 if (msg.hdr.len > sizeof(msg.body))
430 memcpy(&msg.body, reply, msg.hdr.len);
432 mutex_lock(&u->reply_mutex);
433 rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
434 wake_up(&u->read_waitq);
435 mutex_unlock(&u->reply_mutex);
438 kref_put(&u->kref, xenbus_file_free);
443 static int xenbus_write_transaction(unsigned msg_type,
444 struct xenbus_file_priv *u)
447 struct xenbus_transaction_holder *trans = NULL;
449 if (msg_type == XS_TRANSACTION_START) {
450 trans = kzalloc(sizeof(*trans), GFP_KERNEL);
455 list_add(&trans->list, &u->transactions);
456 } else if (u->u.msg.tx_id != 0 &&
457 !xenbus_get_transaction(u, u->u.msg.tx_id))
458 return xenbus_command_reply(u, XS_ERROR, "ENOENT");
460 rc = xenbus_dev_request_and_reply(&u->u.msg, u);
462 list_del(&trans->list);
470 static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
472 struct watch_adapter *watch;
475 LIST_HEAD(staging_q);
477 path = u->u.buffer + sizeof(u->u.msg);
478 token = memchr(path, 0, u->u.msg.len);
480 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
484 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
485 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
489 if (msg_type == XS_WATCH) {
490 watch = alloc_watch_adapter(path, token);
496 watch->watch.callback = watch_fired;
499 err = register_xenbus_watch(&watch->watch);
501 free_watch_adapter(watch);
505 list_add(&watch->list, &u->watches);
507 list_for_each_entry(watch, &u->watches, list) {
508 if (!strcmp(watch->token, token) &&
509 !strcmp(watch->watch.node, path)) {
510 unregister_xenbus_watch(&watch->watch);
511 list_del(&watch->list);
512 free_watch_adapter(watch);
518 /* Success. Synthesize a reply to say all is OK. */
519 rc = xenbus_command_reply(u, msg_type, "OK");
525 static ssize_t xenbus_file_write(struct file *filp,
526 const char __user *ubuf,
527 size_t len, loff_t *ppos)
529 struct xenbus_file_priv *u = filp->private_data;
533 LIST_HEAD(staging_q);
536 * We're expecting usermode to be writing properly formed
537 * xenbus messages. If they write an incomplete message we
538 * buffer it up. Once it is complete, we act on it.
542 * Make sure concurrent writers can't stomp all over each
543 * other's messages and make a mess of our partial message
544 * buffer. We don't make any attemppt to stop multiple
545 * writers from making a mess of each other's incomplete
546 * messages; we're just trying to guarantee our own internal
547 * consistency and make sure that single writes are handled
550 mutex_lock(&u->msgbuffer_mutex);
552 /* Get this out of the way early to avoid confusion */
556 /* Can't write a xenbus message larger we can buffer */
557 if (len > sizeof(u->u.buffer) - u->len) {
558 /* On error, dump existing buffer */
564 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
571 /* Deal with a partial copy. */
577 /* Return if we haven't got a full message yet */
578 if (u->len < sizeof(u->u.msg))
579 goto out; /* not even the header yet */
581 /* If we're expecting a message that's larger than we can
582 possibly send, dump what we have and return an error. */
583 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
589 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
590 goto out; /* incomplete data portion */
593 * OK, now we have a complete message. Do something with it.
598 msg_type = u->u.msg.type;
603 /* (Un)Ask for some path to be watched for changes */
604 ret = xenbus_write_watch(msg_type, u);
608 /* Send out a transaction */
609 ret = xenbus_write_transaction(msg_type, u);
614 kref_put(&u->kref, xenbus_file_free);
617 /* Buffered message consumed */
621 mutex_unlock(&u->msgbuffer_mutex);
625 static int xenbus_file_open(struct inode *inode, struct file *filp)
627 struct xenbus_file_priv *u;
629 if (xen_store_evtchn == 0)
632 stream_open(inode, filp);
634 u = kzalloc(sizeof(*u), GFP_KERNEL);
640 INIT_LIST_HEAD(&u->transactions);
641 INIT_LIST_HEAD(&u->watches);
642 INIT_LIST_HEAD(&u->read_buffers);
643 init_waitqueue_head(&u->read_waitq);
644 INIT_WORK(&u->wq, xenbus_worker);
646 mutex_init(&u->reply_mutex);
647 mutex_init(&u->msgbuffer_mutex);
649 filp->private_data = u;
654 static int xenbus_file_release(struct inode *inode, struct file *filp)
656 struct xenbus_file_priv *u = filp->private_data;
658 kref_put(&u->kref, xenbus_file_free);
663 static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
665 struct xenbus_file_priv *u = file->private_data;
667 poll_wait(file, &u->read_waitq, wait);
668 if (!list_empty(&u->read_buffers))
669 return POLLIN | POLLRDNORM;
673 const struct file_operations xen_xenbus_fops = {
674 .read = xenbus_file_read,
675 .write = xenbus_file_write,
676 .open = xenbus_file_open,
677 .release = xenbus_file_release,
678 .poll = xenbus_file_poll,
681 EXPORT_SYMBOL_GPL(xen_xenbus_fops);
683 static struct miscdevice xenbus_dev = {
684 .minor = MISC_DYNAMIC_MINOR,
685 .name = "xen/xenbus",
686 .fops = &xen_xenbus_fops,
689 static int __init xenbus_init(void)
696 err = misc_register(&xenbus_dev);
698 pr_err("Could not register xenbus frontend device\n");
701 device_initcall(xenbus_init);