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/sched.h>
47 #include <linux/spinlock.h>
48 #include <linux/mount.h>
49 #include <linux/pagemap.h>
50 #include <linux/uaccess.h>
51 #include <linux/init.h>
52 #include <linux/namei.h>
53 #include <linux/string.h>
54 #include <linux/slab.h>
57 #include "../xenbus/xenbus_comms.h"
59 #include <xen/xenbus.h>
60 #include <asm/xen/hypervisor.h>
63 * An element of a list of outstanding transactions, for which we're
64 * still waiting a reply.
66 struct xenbus_transaction_holder
{
67 struct list_head list
;
68 struct xenbus_transaction handle
;
72 * A buffer of data on the queue.
75 struct list_head list
;
81 struct xenbus_file_priv
{
83 * msgbuffer_mutex is held while partial requests are built up
84 * and complete requests are acted on. It therefore protects
85 * the "transactions" and "watches" lists, and the partial
86 * request length and buffer.
88 * reply_mutex protects the reply being built up to return to
89 * usermode. It nests inside msgbuffer_mutex but may be held
90 * alone during a watch callback.
92 struct mutex msgbuffer_mutex
;
94 /* In-progress transactions */
95 struct list_head transactions
;
98 struct list_head watches
;
100 /* Partial request. */
103 struct xsd_sockmsg msg
;
104 char buffer
[PAGE_SIZE
];
107 /* Response queue. */
108 struct mutex reply_mutex
;
109 struct list_head read_buffers
;
110 wait_queue_head_t read_waitq
;
114 /* Read out any raw xenbus messages queued up. */
115 static ssize_t
xenbus_file_read(struct file
*filp
,
117 size_t len
, loff_t
*ppos
)
119 struct xenbus_file_priv
*u
= filp
->private_data
;
120 struct read_buffer
*rb
;
124 mutex_lock(&u
->reply_mutex
);
126 while (list_empty(&u
->read_buffers
)) {
127 mutex_unlock(&u
->reply_mutex
);
128 if (filp
->f_flags
& O_NONBLOCK
)
131 ret
= wait_event_interruptible(u
->read_waitq
,
132 !list_empty(&u
->read_buffers
));
135 mutex_lock(&u
->reply_mutex
);
138 rb
= list_entry(u
->read_buffers
.next
, struct read_buffer
, list
);
141 unsigned sz
= min((unsigned)len
- i
, rb
->len
- rb
->cons
);
143 ret
= copy_to_user(ubuf
+ i
, &rb
->msg
[rb
->cons
], sz
);
146 rb
->cons
+= sz
- ret
;
154 /* Clear out buffer if it has been consumed */
155 if (rb
->cons
== rb
->len
) {
158 if (list_empty(&u
->read_buffers
))
160 rb
= list_entry(u
->read_buffers
.next
,
161 struct read_buffer
, list
);
168 mutex_unlock(&u
->reply_mutex
);
173 * Add a buffer to the queue. Caller must hold the appropriate lock
174 * if the queue is not local. (Commonly the caller will build up
175 * multiple queued buffers on a temporary local list, and then add it
176 * to the appropriate list under lock once all the buffers have een
177 * successfully allocated.)
179 static int queue_reply(struct list_head
*queue
, const void *data
, size_t len
)
181 struct read_buffer
*rb
;
186 rb
= kmalloc(sizeof(*rb
) + len
, GFP_KERNEL
);
193 memcpy(rb
->msg
, data
, len
);
195 list_add_tail(&rb
->list
, queue
);
200 * Free all the read_buffer s on a list.
201 * Caller must have sole reference to list.
203 static void queue_cleanup(struct list_head
*list
)
205 struct read_buffer
*rb
;
207 while (!list_empty(list
)) {
208 rb
= list_entry(list
->next
, struct read_buffer
, list
);
209 list_del(list
->next
);
214 struct watch_adapter
{
215 struct list_head list
;
216 struct xenbus_watch watch
;
217 struct xenbus_file_priv
*dev_data
;
221 static void free_watch_adapter(struct watch_adapter
*watch
)
223 kfree(watch
->watch
.node
);
228 static struct watch_adapter
*alloc_watch_adapter(const char *path
,
231 struct watch_adapter
*watch
;
233 watch
= kzalloc(sizeof(*watch
), GFP_KERNEL
);
237 watch
->watch
.node
= kstrdup(path
, GFP_KERNEL
);
238 if (watch
->watch
.node
== NULL
)
241 watch
->token
= kstrdup(token
, GFP_KERNEL
);
242 if (watch
->token
== NULL
)
248 free_watch_adapter(watch
);
254 static void watch_fired(struct xenbus_watch
*watch
,
258 struct watch_adapter
*adap
;
259 struct xsd_sockmsg hdr
;
260 const char *path
, *token
;
261 int path_len
, tok_len
, body_len
, data_len
= 0;
263 LIST_HEAD(staging_q
);
265 adap
= container_of(watch
, struct watch_adapter
, watch
);
267 path
= vec
[XS_WATCH_PATH
];
270 path_len
= strlen(path
) + 1;
271 tok_len
= strlen(token
) + 1;
273 data_len
= vec
[len
] - vec
[2] + 1;
274 body_len
= path_len
+ tok_len
+ data_len
;
276 hdr
.type
= XS_WATCH_EVENT
;
279 mutex_lock(&adap
->dev_data
->reply_mutex
);
281 ret
= queue_reply(&staging_q
, &hdr
, sizeof(hdr
));
283 ret
= queue_reply(&staging_q
, path
, path_len
);
285 ret
= queue_reply(&staging_q
, token
, tok_len
);
287 ret
= queue_reply(&staging_q
, vec
[2], data_len
);
290 /* success: pass reply list onto watcher */
291 list_splice_tail(&staging_q
, &adap
->dev_data
->read_buffers
);
292 wake_up(&adap
->dev_data
->read_waitq
);
294 queue_cleanup(&staging_q
);
296 mutex_unlock(&adap
->dev_data
->reply_mutex
);
299 static int xenbus_write_transaction(unsigned msg_type
,
300 struct xenbus_file_priv
*u
)
304 struct xenbus_transaction_holder
*trans
= NULL
;
305 LIST_HEAD(staging_q
);
307 if (msg_type
== XS_TRANSACTION_START
) {
308 trans
= kmalloc(sizeof(*trans
), GFP_KERNEL
);
315 reply
= xenbus_dev_request_and_reply(&u
->u
.msg
);
322 if (msg_type
== XS_TRANSACTION_START
) {
323 trans
->handle
.id
= simple_strtoul(reply
, NULL
, 0);
325 list_add(&trans
->list
, &u
->transactions
);
326 } else if (msg_type
== XS_TRANSACTION_END
) {
327 list_for_each_entry(trans
, &u
->transactions
, list
)
328 if (trans
->handle
.id
== u
->u
.msg
.tx_id
)
330 BUG_ON(&trans
->list
== &u
->transactions
);
331 list_del(&trans
->list
);
336 mutex_lock(&u
->reply_mutex
);
337 rc
= queue_reply(&staging_q
, &u
->u
.msg
, sizeof(u
->u
.msg
));
339 rc
= queue_reply(&staging_q
, reply
, u
->u
.msg
.len
);
341 list_splice_tail(&staging_q
, &u
->read_buffers
);
342 wake_up(&u
->read_waitq
);
344 queue_cleanup(&staging_q
);
346 mutex_unlock(&u
->reply_mutex
);
354 static int xenbus_write_watch(unsigned msg_type
, struct xenbus_file_priv
*u
)
356 struct watch_adapter
*watch
, *tmp_watch
;
359 LIST_HEAD(staging_q
);
361 path
= u
->u
.buffer
+ sizeof(u
->u
.msg
);
362 token
= memchr(path
, 0, u
->u
.msg
.len
);
369 if (msg_type
== XS_WATCH
) {
370 watch
= alloc_watch_adapter(path
, token
);
376 watch
->watch
.callback
= watch_fired
;
379 err
= register_xenbus_watch(&watch
->watch
);
381 free_watch_adapter(watch
);
385 list_add(&watch
->list
, &u
->watches
);
387 list_for_each_entry_safe(watch
, tmp_watch
, &u
->watches
, list
) {
388 if (!strcmp(watch
->token
, token
) &&
389 !strcmp(watch
->watch
.node
, path
)) {
390 unregister_xenbus_watch(&watch
->watch
);
391 list_del(&watch
->list
);
392 free_watch_adapter(watch
);
398 /* Success. Synthesize a reply to say all is OK. */
401 struct xsd_sockmsg hdr
;
406 .len
= sizeof(reply
.body
)
411 mutex_lock(&u
->reply_mutex
);
412 rc
= queue_reply(&u
->read_buffers
, &reply
, sizeof(reply
));
413 wake_up(&u
->read_waitq
);
414 mutex_unlock(&u
->reply_mutex
);
421 static ssize_t
xenbus_file_write(struct file
*filp
,
422 const char __user
*ubuf
,
423 size_t len
, loff_t
*ppos
)
425 struct xenbus_file_priv
*u
= filp
->private_data
;
429 LIST_HEAD(staging_q
);
432 * We're expecting usermode to be writing properly formed
433 * xenbus messages. If they write an incomplete message we
434 * buffer it up. Once it is complete, we act on it.
438 * Make sure concurrent writers can't stomp all over each
439 * other's messages and make a mess of our partial message
440 * buffer. We don't make any attemppt to stop multiple
441 * writers from making a mess of each other's incomplete
442 * messages; we're just trying to guarantee our own internal
443 * consistency and make sure that single writes are handled
446 mutex_lock(&u
->msgbuffer_mutex
);
448 /* Get this out of the way early to avoid confusion */
452 /* Can't write a xenbus message larger we can buffer */
453 if ((len
+ u
->len
) > sizeof(u
->u
.buffer
)) {
454 /* On error, dump existing buffer */
460 ret
= copy_from_user(u
->u
.buffer
+ u
->len
, ubuf
, len
);
467 /* Deal with a partial copy. */
473 /* Return if we haven't got a full message yet */
474 if (u
->len
< sizeof(u
->u
.msg
))
475 goto out
; /* not even the header yet */
477 /* If we're expecting a message that's larger than we can
478 possibly send, dump what we have and return an error. */
479 if ((sizeof(u
->u
.msg
) + u
->u
.msg
.len
) > sizeof(u
->u
.buffer
)) {
485 if (u
->len
< (sizeof(u
->u
.msg
) + u
->u
.msg
.len
))
486 goto out
; /* incomplete data portion */
489 * OK, now we have a complete message. Do something with it.
492 msg_type
= u
->u
.msg
.type
;
497 /* (Un)Ask for some path to be watched for changes */
498 ret
= xenbus_write_watch(msg_type
, u
);
502 /* Send out a transaction */
503 ret
= xenbus_write_transaction(msg_type
, u
);
509 /* Buffered message consumed */
513 mutex_unlock(&u
->msgbuffer_mutex
);
517 static int xenbus_file_open(struct inode
*inode
, struct file
*filp
)
519 struct xenbus_file_priv
*u
;
521 if (xen_store_evtchn
== 0)
524 nonseekable_open(inode
, filp
);
526 u
= kzalloc(sizeof(*u
), GFP_KERNEL
);
530 INIT_LIST_HEAD(&u
->transactions
);
531 INIT_LIST_HEAD(&u
->watches
);
532 INIT_LIST_HEAD(&u
->read_buffers
);
533 init_waitqueue_head(&u
->read_waitq
);
535 mutex_init(&u
->reply_mutex
);
536 mutex_init(&u
->msgbuffer_mutex
);
538 filp
->private_data
= u
;
543 static int xenbus_file_release(struct inode
*inode
, struct file
*filp
)
545 struct xenbus_file_priv
*u
= filp
->private_data
;
546 struct xenbus_transaction_holder
*trans
, *tmp
;
547 struct watch_adapter
*watch
, *tmp_watch
;
548 struct read_buffer
*rb
, *tmp_rb
;
551 * No need for locking here because there are no other users,
555 list_for_each_entry_safe(trans
, tmp
, &u
->transactions
, list
) {
556 xenbus_transaction_end(trans
->handle
, 1);
557 list_del(&trans
->list
);
561 list_for_each_entry_safe(watch
, tmp_watch
, &u
->watches
, list
) {
562 unregister_xenbus_watch(&watch
->watch
);
563 list_del(&watch
->list
);
564 free_watch_adapter(watch
);
567 list_for_each_entry_safe(rb
, tmp_rb
, &u
->read_buffers
, list
) {
576 static unsigned int xenbus_file_poll(struct file
*file
, poll_table
*wait
)
578 struct xenbus_file_priv
*u
= file
->private_data
;
580 poll_wait(file
, &u
->read_waitq
, wait
);
581 if (!list_empty(&u
->read_buffers
))
582 return POLLIN
| POLLRDNORM
;
586 const struct file_operations xenbus_file_ops
= {
587 .read
= xenbus_file_read
,
588 .write
= xenbus_file_write
,
589 .open
= xenbus_file_open
,
590 .release
= xenbus_file_release
,
591 .poll
= xenbus_file_poll
,