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 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/uio.h>
41 #include <linux/notifier.h>
42 #include <linux/wait.h>
44 #include <linux/poll.h>
45 #include <linux/mutex.h>
46 #include <linux/spinlock.h>
47 #include <linux/mount.h>
48 #include <linux/pagemap.h>
49 #include <linux/uaccess.h>
50 #include <linux/init.h>
51 #include <linux/namei.h>
52 #include <linux/string.h>
55 #include "../xenbus/xenbus_comms.h"
57 #include <xen/xenbus.h>
58 #include <asm/xen/hypervisor.h>
61 * An element of a list of outstanding transactions, for which we're
62 * still waiting a reply.
64 struct xenbus_transaction_holder
{
65 struct list_head list
;
66 struct xenbus_transaction handle
;
70 * A buffer of data on the queue.
73 struct list_head list
;
79 struct xenbus_file_priv
{
81 * msgbuffer_mutex is held while partial requests are built up
82 * and complete requests are acted on. It therefore protects
83 * the "transactions" and "watches" lists, and the partial
84 * request length and buffer.
86 * reply_mutex protects the reply being built up to return to
87 * usermode. It nests inside msgbuffer_mutex but may be held
88 * alone during a watch callback.
90 struct mutex msgbuffer_mutex
;
92 /* In-progress transactions */
93 struct list_head transactions
;
96 struct list_head watches
;
98 /* Partial request. */
101 struct xsd_sockmsg msg
;
102 char buffer
[PAGE_SIZE
];
105 /* Response queue. */
106 struct mutex reply_mutex
;
107 struct list_head read_buffers
;
108 wait_queue_head_t read_waitq
;
112 /* Read out any raw xenbus messages queued up. */
113 static ssize_t
xenbus_file_read(struct file
*filp
,
115 size_t len
, loff_t
*ppos
)
117 struct xenbus_file_priv
*u
= filp
->private_data
;
118 struct read_buffer
*rb
;
122 mutex_lock(&u
->reply_mutex
);
123 while (list_empty(&u
->read_buffers
)) {
124 mutex_unlock(&u
->reply_mutex
);
125 ret
= wait_event_interruptible(u
->read_waitq
,
126 !list_empty(&u
->read_buffers
));
129 mutex_lock(&u
->reply_mutex
);
132 rb
= list_entry(u
->read_buffers
.next
, struct read_buffer
, list
);
135 unsigned sz
= min((unsigned)len
- i
, rb
->len
- rb
->cons
);
137 ret
= copy_to_user(ubuf
+ i
, &rb
->msg
[rb
->cons
], sz
);
140 rb
->cons
+= sz
- ret
;
148 /* Clear out buffer if it has been consumed */
149 if (rb
->cons
== rb
->len
) {
152 if (list_empty(&u
->read_buffers
))
154 rb
= list_entry(u
->read_buffers
.next
,
155 struct read_buffer
, list
);
160 mutex_unlock(&u
->reply_mutex
);
165 * Add a buffer to the queue. Caller must hold the appropriate lock
166 * if the queue is not local. (Commonly the caller will build up
167 * multiple queued buffers on a temporary local list, and then add it
168 * to the appropriate list under lock once all the buffers have een
169 * successfully allocated.)
171 static int queue_reply(struct list_head
*queue
, const void *data
, size_t len
)
173 struct read_buffer
*rb
;
178 rb
= kmalloc(sizeof(*rb
) + len
, GFP_KERNEL
);
185 memcpy(rb
->msg
, data
, len
);
187 list_add_tail(&rb
->list
, queue
);
192 * Free all the read_buffer s on a list.
193 * Caller must have sole reference to list.
195 static void queue_cleanup(struct list_head
*list
)
197 struct read_buffer
*rb
;
199 while (!list_empty(list
)) {
200 rb
= list_entry(list
->next
, struct read_buffer
, list
);
201 list_del(list
->next
);
206 struct watch_adapter
{
207 struct list_head list
;
208 struct xenbus_watch watch
;
209 struct xenbus_file_priv
*dev_data
;
213 static void free_watch_adapter(struct watch_adapter
*watch
)
215 kfree(watch
->watch
.node
);
220 static struct watch_adapter
*alloc_watch_adapter(const char *path
,
223 struct watch_adapter
*watch
;
225 watch
= kzalloc(sizeof(*watch
), GFP_KERNEL
);
229 watch
->watch
.node
= kstrdup(path
, GFP_KERNEL
);
230 if (watch
->watch
.node
== NULL
)
233 watch
->token
= kstrdup(token
, GFP_KERNEL
);
234 if (watch
->token
== NULL
)
240 free_watch_adapter(watch
);
246 static void watch_fired(struct xenbus_watch
*watch
,
250 struct watch_adapter
*adap
;
251 struct xsd_sockmsg hdr
;
252 const char *path
, *token
;
253 int path_len
, tok_len
, body_len
, data_len
= 0;
255 LIST_HEAD(staging_q
);
257 adap
= container_of(watch
, struct watch_adapter
, watch
);
259 path
= vec
[XS_WATCH_PATH
];
262 path_len
= strlen(path
) + 1;
263 tok_len
= strlen(token
) + 1;
265 data_len
= vec
[len
] - vec
[2] + 1;
266 body_len
= path_len
+ tok_len
+ data_len
;
268 hdr
.type
= XS_WATCH_EVENT
;
271 mutex_lock(&adap
->dev_data
->reply_mutex
);
273 ret
= queue_reply(&staging_q
, &hdr
, sizeof(hdr
));
275 ret
= queue_reply(&staging_q
, path
, path_len
);
277 ret
= queue_reply(&staging_q
, token
, tok_len
);
279 ret
= queue_reply(&staging_q
, vec
[2], data_len
);
282 /* success: pass reply list onto watcher */
283 list_splice_tail(&staging_q
, &adap
->dev_data
->read_buffers
);
284 wake_up(&adap
->dev_data
->read_waitq
);
286 queue_cleanup(&staging_q
);
288 mutex_unlock(&adap
->dev_data
->reply_mutex
);
291 static int xenbus_write_transaction(unsigned msg_type
,
292 struct xenbus_file_priv
*u
)
296 struct xenbus_transaction_holder
*trans
= NULL
;
297 LIST_HEAD(staging_q
);
299 if (msg_type
== XS_TRANSACTION_START
) {
300 trans
= kmalloc(sizeof(*trans
), GFP_KERNEL
);
307 reply
= xenbus_dev_request_and_reply(&u
->u
.msg
);
314 if (msg_type
== XS_TRANSACTION_START
) {
315 trans
->handle
.id
= simple_strtoul(reply
, NULL
, 0);
317 list_add(&trans
->list
, &u
->transactions
);
318 } else if (msg_type
== XS_TRANSACTION_END
) {
319 list_for_each_entry(trans
, &u
->transactions
, list
)
320 if (trans
->handle
.id
== u
->u
.msg
.tx_id
)
322 BUG_ON(&trans
->list
== &u
->transactions
);
323 list_del(&trans
->list
);
328 mutex_lock(&u
->reply_mutex
);
329 ret
= queue_reply(&staging_q
, &u
->u
.msg
, sizeof(u
->u
.msg
));
331 ret
= queue_reply(&staging_q
, reply
, u
->u
.msg
.len
);
333 list_splice_tail(&staging_q
, &u
->read_buffers
);
334 wake_up(&u
->read_waitq
);
336 queue_cleanup(&staging_q
);
339 mutex_unlock(&u
->reply_mutex
);
347 static int xenbus_write_watch(unsigned msg_type
, struct xenbus_file_priv
*u
)
349 struct watch_adapter
*watch
, *tmp_watch
;
352 LIST_HEAD(staging_q
);
354 path
= u
->u
.buffer
+ sizeof(u
->u
.msg
);
355 token
= memchr(path
, 0, u
->u
.msg
.len
);
362 if (msg_type
== XS_WATCH
) {
363 watch
= alloc_watch_adapter(path
, token
);
369 watch
->watch
.callback
= watch_fired
;
372 err
= register_xenbus_watch(&watch
->watch
);
374 free_watch_adapter(watch
);
378 list_add(&watch
->list
, &u
->watches
);
380 list_for_each_entry_safe(watch
, tmp_watch
, &u
->watches
, list
) {
381 if (!strcmp(watch
->token
, token
) &&
382 !strcmp(watch
->watch
.node
, path
)) {
383 unregister_xenbus_watch(&watch
->watch
);
384 list_del(&watch
->list
);
385 free_watch_adapter(watch
);
391 /* Success. Synthesize a reply to say all is OK. */
394 struct xsd_sockmsg hdr
;
399 .len
= sizeof(reply
.body
)
404 mutex_lock(&u
->reply_mutex
);
405 rc
= queue_reply(&u
->read_buffers
, &reply
, sizeof(reply
));
406 mutex_unlock(&u
->reply_mutex
);
413 static ssize_t
xenbus_file_write(struct file
*filp
,
414 const char __user
*ubuf
,
415 size_t len
, loff_t
*ppos
)
417 struct xenbus_file_priv
*u
= filp
->private_data
;
421 LIST_HEAD(staging_q
);
424 * We're expecting usermode to be writing properly formed
425 * xenbus messages. If they write an incomplete message we
426 * buffer it up. Once it is complete, we act on it.
430 * Make sure concurrent writers can't stomp all over each
431 * other's messages and make a mess of our partial message
432 * buffer. We don't make any attemppt to stop multiple
433 * writers from making a mess of each other's incomplete
434 * messages; we're just trying to guarantee our own internal
435 * consistency and make sure that single writes are handled
438 mutex_lock(&u
->msgbuffer_mutex
);
440 /* Get this out of the way early to avoid confusion */
444 /* Can't write a xenbus message larger we can buffer */
445 if ((len
+ u
->len
) > sizeof(u
->u
.buffer
)) {
446 /* On error, dump existing buffer */
452 ret
= copy_from_user(u
->u
.buffer
+ u
->len
, ubuf
, len
);
459 /* Deal with a partial copy. */
465 /* Return if we haven't got a full message yet */
466 if (u
->len
< sizeof(u
->u
.msg
))
467 goto out
; /* not even the header yet */
469 /* If we're expecting a message that's larger than we can
470 possibly send, dump what we have and return an error. */
471 if ((sizeof(u
->u
.msg
) + u
->u
.msg
.len
) > sizeof(u
->u
.buffer
)) {
477 if (u
->len
< (sizeof(u
->u
.msg
) + u
->u
.msg
.len
))
478 goto out
; /* incomplete data portion */
481 * OK, now we have a complete message. Do something with it.
484 msg_type
= u
->u
.msg
.type
;
487 case XS_TRANSACTION_START
:
488 case XS_TRANSACTION_END
:
493 case XS_GET_DOMAIN_PATH
:
498 /* Send out a transaction */
499 ret
= xenbus_write_transaction(msg_type
, u
);
504 /* (Un)Ask for some path to be watched for changes */
505 ret
= xenbus_write_watch(msg_type
, u
);
515 /* Buffered message consumed */
519 mutex_unlock(&u
->msgbuffer_mutex
);
523 static int xenbus_file_open(struct inode
*inode
, struct file
*filp
)
525 struct xenbus_file_priv
*u
;
527 if (xen_store_evtchn
== 0)
530 nonseekable_open(inode
, filp
);
532 u
= kzalloc(sizeof(*u
), GFP_KERNEL
);
536 INIT_LIST_HEAD(&u
->transactions
);
537 INIT_LIST_HEAD(&u
->watches
);
538 INIT_LIST_HEAD(&u
->read_buffers
);
539 init_waitqueue_head(&u
->read_waitq
);
541 mutex_init(&u
->reply_mutex
);
542 mutex_init(&u
->msgbuffer_mutex
);
544 filp
->private_data
= u
;
549 static int xenbus_file_release(struct inode
*inode
, struct file
*filp
)
551 struct xenbus_file_priv
*u
= filp
->private_data
;
552 struct xenbus_transaction_holder
*trans
, *tmp
;
553 struct watch_adapter
*watch
, *tmp_watch
;
556 * No need for locking here because there are no other users,
560 list_for_each_entry_safe(trans
, tmp
, &u
->transactions
, list
) {
561 xenbus_transaction_end(trans
->handle
, 1);
562 list_del(&trans
->list
);
566 list_for_each_entry_safe(watch
, tmp_watch
, &u
->watches
, list
) {
567 unregister_xenbus_watch(&watch
->watch
);
568 list_del(&watch
->list
);
569 free_watch_adapter(watch
);
577 static unsigned int xenbus_file_poll(struct file
*file
, poll_table
*wait
)
579 struct xenbus_file_priv
*u
= file
->private_data
;
581 poll_wait(file
, &u
->read_waitq
, wait
);
582 if (!list_empty(&u
->read_buffers
))
583 return POLLIN
| POLLRDNORM
;
587 const struct file_operations xenbus_file_ops
= {
588 .read
= xenbus_file_read
,
589 .write
= xenbus_file_write
,
590 .open
= xenbus_file_open
,
591 .release
= xenbus_file_release
,
592 .poll
= xenbus_file_poll
,