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>
59 #include <xen/xenbus.h>
61 #include <asm/xen/hypervisor.h>
66 * An element of a list of outstanding transactions, for which we're
67 * still waiting a reply.
69 struct xenbus_transaction_holder
{
70 struct list_head list
;
71 struct xenbus_transaction handle
;
75 * A buffer of data on the queue.
78 struct list_head list
;
84 struct xenbus_file_priv
{
86 * msgbuffer_mutex is held while partial requests are built up
87 * and complete requests are acted on. It therefore protects
88 * the "transactions" and "watches" lists, and the partial
89 * request length and buffer.
91 * reply_mutex protects the reply being built up to return to
92 * usermode. It nests inside msgbuffer_mutex but may be held
93 * alone during a watch callback.
95 struct mutex msgbuffer_mutex
;
97 /* In-progress transactions */
98 struct list_head transactions
;
100 /* Active watches. */
101 struct list_head watches
;
103 /* Partial request. */
106 struct xsd_sockmsg msg
;
107 char buffer
[XENSTORE_PAYLOAD_MAX
];
110 /* Response queue. */
111 struct mutex reply_mutex
;
112 struct list_head read_buffers
;
113 wait_queue_head_t read_waitq
;
118 /* Read out any raw xenbus messages queued up. */
119 static ssize_t
xenbus_file_read(struct file
*filp
,
121 size_t len
, loff_t
*ppos
)
123 struct xenbus_file_priv
*u
= filp
->private_data
;
124 struct read_buffer
*rb
;
128 mutex_lock(&u
->reply_mutex
);
130 while (list_empty(&u
->read_buffers
)) {
131 mutex_unlock(&u
->reply_mutex
);
132 if (filp
->f_flags
& O_NONBLOCK
)
135 ret
= wait_event_interruptible(u
->read_waitq
,
136 !list_empty(&u
->read_buffers
));
139 mutex_lock(&u
->reply_mutex
);
142 rb
= list_entry(u
->read_buffers
.next
, struct read_buffer
, list
);
145 unsigned sz
= min((unsigned)len
- i
, rb
->len
- rb
->cons
);
147 ret
= copy_to_user(ubuf
+ i
, &rb
->msg
[rb
->cons
], sz
);
150 rb
->cons
+= sz
- ret
;
158 /* Clear out buffer if it has been consumed */
159 if (rb
->cons
== rb
->len
) {
162 if (list_empty(&u
->read_buffers
))
164 rb
= list_entry(u
->read_buffers
.next
,
165 struct read_buffer
, list
);
172 mutex_unlock(&u
->reply_mutex
);
177 * Add a buffer to the queue. Caller must hold the appropriate lock
178 * if the queue is not local. (Commonly the caller will build up
179 * multiple queued buffers on a temporary local list, and then add it
180 * to the appropriate list under lock once all the buffers have een
181 * successfully allocated.)
183 static int queue_reply(struct list_head
*queue
, const void *data
, size_t len
)
185 struct read_buffer
*rb
;
189 if (len
> XENSTORE_PAYLOAD_MAX
)
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 *token_caller
;
267 int path_len
, tok_len
, body_len
;
269 LIST_HEAD(staging_q
);
271 adap
= container_of(watch
, struct watch_adapter
, watch
);
273 token_caller
= adap
->token
;
275 path_len
= strlen(path
) + 1;
276 tok_len
= strlen(token_caller
) + 1;
277 body_len
= path_len
+ tok_len
;
279 hdr
.type
= XS_WATCH_EVENT
;
282 mutex_lock(&adap
->dev_data
->reply_mutex
);
284 ret
= queue_reply(&staging_q
, &hdr
, sizeof(hdr
));
286 ret
= queue_reply(&staging_q
, path
, path_len
);
288 ret
= queue_reply(&staging_q
, token_caller
, tok_len
);
291 /* success: pass reply list onto watcher */
292 list_splice_tail(&staging_q
, &adap
->dev_data
->read_buffers
);
293 wake_up(&adap
->dev_data
->read_waitq
);
295 queue_cleanup(&staging_q
);
297 mutex_unlock(&adap
->dev_data
->reply_mutex
);
300 static void xenbus_file_free(struct kref
*kref
)
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
;
307 u
= container_of(kref
, struct xenbus_file_priv
, kref
);
310 * No need for locking here because there are no other users,
314 list_for_each_entry_safe(trans
, tmp
, &u
->transactions
, list
) {
315 xenbus_transaction_end(trans
->handle
, 1);
316 list_del(&trans
->list
);
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
);
326 list_for_each_entry_safe(rb
, tmp_rb
, &u
->read_buffers
, list
) {
333 static struct xenbus_transaction_holder
*xenbus_get_transaction(
334 struct xenbus_file_priv
*u
, uint32_t tx_id
)
336 struct xenbus_transaction_holder
*trans
;
338 list_for_each_entry(trans
, &u
->transactions
, list
)
339 if (trans
->handle
.id
== tx_id
)
345 void xenbus_dev_queue_reply(struct xb_req_data
*req
)
347 struct xenbus_file_priv
*u
= req
->par
;
348 struct xenbus_transaction_holder
*trans
= NULL
;
350 LIST_HEAD(staging_q
);
352 xs_request_exit(req
);
354 mutex_lock(&u
->msgbuffer_mutex
);
356 if (req
->type
== XS_TRANSACTION_START
) {
357 trans
= xenbus_get_transaction(u
, 0);
360 if (req
->msg
.type
== XS_ERROR
) {
361 list_del(&trans
->list
);
364 rc
= kstrtou32(req
->body
, 10, &trans
->handle
.id
);
368 } else if (req
->type
== XS_TRANSACTION_END
) {
369 trans
= xenbus_get_transaction(u
, req
->msg
.tx_id
);
372 list_del(&trans
->list
);
376 mutex_unlock(&u
->msgbuffer_mutex
);
378 mutex_lock(&u
->reply_mutex
);
379 rc
= queue_reply(&staging_q
, &req
->msg
, sizeof(req
->msg
));
381 rc
= queue_reply(&staging_q
, req
->body
, req
->msg
.len
);
383 list_splice_tail(&staging_q
, &u
->read_buffers
);
384 wake_up(&u
->read_waitq
);
386 queue_cleanup(&staging_q
);
388 mutex_unlock(&u
->reply_mutex
);
393 kref_put(&u
->kref
, xenbus_file_free
);
398 mutex_unlock(&u
->msgbuffer_mutex
);
401 static int xenbus_command_reply(struct xenbus_file_priv
*u
,
402 unsigned int msg_type
, const char *reply
)
405 struct xsd_sockmsg hdr
;
411 msg
.hdr
.type
= msg_type
;
412 msg
.hdr
.len
= strlen(reply
) + 1;
413 if (msg
.hdr
.len
> sizeof(msg
.body
))
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
);
422 kref_put(&u
->kref
, xenbus_file_free
);
427 static int xenbus_write_transaction(unsigned msg_type
,
428 struct xenbus_file_priv
*u
)
431 struct xenbus_transaction_holder
*trans
= NULL
;
433 if (msg_type
== XS_TRANSACTION_START
) {
434 trans
= kzalloc(sizeof(*trans
), GFP_KERNEL
);
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");
444 rc
= xenbus_dev_request_and_reply(&u
->u
.msg
, u
);
446 list_del(&trans
->list
);
454 static int xenbus_write_watch(unsigned msg_type
, struct xenbus_file_priv
*u
)
456 struct watch_adapter
*watch
;
459 LIST_HEAD(staging_q
);
461 path
= u
->u
.buffer
+ sizeof(u
->u
.msg
);
462 token
= memchr(path
, 0, u
->u
.msg
.len
);
464 rc
= xenbus_command_reply(u
, XS_ERROR
, "EINVAL");
468 if (memchr(token
, 0, u
->u
.msg
.len
- (token
- path
)) == NULL
) {
469 rc
= xenbus_command_reply(u
, XS_ERROR
, "EINVAL");
473 if (msg_type
== XS_WATCH
) {
474 watch
= alloc_watch_adapter(path
, token
);
480 watch
->watch
.callback
= watch_fired
;
483 err
= register_xenbus_watch(&watch
->watch
);
485 free_watch_adapter(watch
);
489 list_add(&watch
->list
, &u
->watches
);
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
);
502 /* Success. Synthesize a reply to say all is OK. */
503 rc
= xenbus_command_reply(u
, msg_type
, "OK");
509 static ssize_t
xenbus_file_write(struct file
*filp
,
510 const char __user
*ubuf
,
511 size_t len
, loff_t
*ppos
)
513 struct xenbus_file_priv
*u
= filp
->private_data
;
517 LIST_HEAD(staging_q
);
520 * We're expecting usermode to be writing properly formed
521 * xenbus messages. If they write an incomplete message we
522 * buffer it up. Once it is complete, we act on it.
526 * Make sure concurrent writers can't stomp all over each
527 * other's messages and make a mess of our partial message
528 * buffer. We don't make any attemppt to stop multiple
529 * writers from making a mess of each other's incomplete
530 * messages; we're just trying to guarantee our own internal
531 * consistency and make sure that single writes are handled
534 mutex_lock(&u
->msgbuffer_mutex
);
536 /* Get this out of the way early to avoid confusion */
540 /* Can't write a xenbus message larger we can buffer */
541 if (len
> sizeof(u
->u
.buffer
) - u
->len
) {
542 /* On error, dump existing buffer */
548 ret
= copy_from_user(u
->u
.buffer
+ u
->len
, ubuf
, len
);
555 /* Deal with a partial copy. */
561 /* Return if we haven't got a full message yet */
562 if (u
->len
< sizeof(u
->u
.msg
))
563 goto out
; /* not even the header yet */
565 /* If we're expecting a message that's larger than we can
566 possibly send, dump what we have and return an error. */
567 if ((sizeof(u
->u
.msg
) + u
->u
.msg
.len
) > sizeof(u
->u
.buffer
)) {
573 if (u
->len
< (sizeof(u
->u
.msg
) + u
->u
.msg
.len
))
574 goto out
; /* incomplete data portion */
577 * OK, now we have a complete message. Do something with it.
582 msg_type
= u
->u
.msg
.type
;
587 /* (Un)Ask for some path to be watched for changes */
588 ret
= xenbus_write_watch(msg_type
, u
);
592 /* Send out a transaction */
593 ret
= xenbus_write_transaction(msg_type
, u
);
598 kref_put(&u
->kref
, xenbus_file_free
);
601 /* Buffered message consumed */
605 mutex_unlock(&u
->msgbuffer_mutex
);
609 static int xenbus_file_open(struct inode
*inode
, struct file
*filp
)
611 struct xenbus_file_priv
*u
;
613 if (xen_store_evtchn
== 0)
616 nonseekable_open(inode
, filp
);
618 filp
->f_mode
&= ~FMODE_ATOMIC_POS
; /* cdev-style semantics */
620 u
= kzalloc(sizeof(*u
), GFP_KERNEL
);
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
);
631 mutex_init(&u
->reply_mutex
);
632 mutex_init(&u
->msgbuffer_mutex
);
634 filp
->private_data
= u
;
639 static int xenbus_file_release(struct inode
*inode
, struct file
*filp
)
641 struct xenbus_file_priv
*u
= filp
->private_data
;
643 kref_put(&u
->kref
, xenbus_file_free
);
648 static __poll_t
xenbus_file_poll(struct file
*file
, poll_table
*wait
)
650 struct xenbus_file_priv
*u
= file
->private_data
;
652 poll_wait(file
, &u
->read_waitq
, wait
);
653 if (!list_empty(&u
->read_buffers
))
654 return EPOLLIN
| EPOLLRDNORM
;
658 const 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
,
666 EXPORT_SYMBOL_GPL(xen_xenbus_fops
);
668 static struct miscdevice xenbus_dev
= {
669 .minor
= MISC_DYNAMIC_MINOR
,
670 .name
= "xen/xenbus",
671 .fops
= &xen_xenbus_fops
,
674 static int __init
xenbus_init(void)
681 err
= misc_register(&xenbus_dev
);
683 pr_err("Could not register xenbus frontend device\n");
686 device_initcall(xenbus_init
);