4 * Driver giving user-space access to the kernel's xenbus connection
7 * Copyright (c) 2005, Christian Limpach
8 * Copyright (c) 2005, Rusty Russell, IBM Corporation
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/uio.h>
39 #include <linux/notifier.h>
40 #include <linux/sched.h>
41 #include <linux/wait.h>
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
46 #include "xenbus_comms.h"
48 #include <asm/uaccess.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <asm/hypervisor.h>
54 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
55 #include <xen/platform-compat.h>
58 #include <xen/public/xenbus.h>
60 struct xenbus_dev_transaction {
61 struct list_head list;
62 struct xenbus_transaction handle;
66 struct list_head list;
72 struct xenbus_dev_data {
73 /* In-progress transaction. */
74 struct list_head transactions;
77 struct list_head watches;
79 /* Partial request. */
82 struct xsd_sockmsg msg;
83 char buffer[PAGE_SIZE];
87 struct list_head read_buffers;
88 wait_queue_head_t read_waitq;
90 struct mutex reply_mutex;
93 static struct proc_dir_entry *xenbus_dev_intf;
95 static ssize_t xenbus_dev_read(struct file *filp,
97 size_t len, loff_t *ppos)
99 struct xenbus_dev_data *u = filp->private_data;
100 struct read_buffer *rb;
103 if (!is_xenstored_ready())
106 mutex_lock(&u->reply_mutex);
107 while (list_empty(&u->read_buffers)) {
108 mutex_unlock(&u->reply_mutex);
109 ret = wait_event_interruptible(u->read_waitq,
110 !list_empty(&u->read_buffers));
113 mutex_lock(&u->reply_mutex);
116 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
117 for (i = 0; i < len;) {
118 put_user(rb->msg[rb->cons], ubuf + i);
121 if (rb->cons == rb->len) {
124 if (list_empty(&u->read_buffers))
126 rb = list_entry(u->read_buffers.next,
127 struct read_buffer, list);
130 mutex_unlock(&u->reply_mutex);
135 static void queue_reply(struct xenbus_dev_data *u,
136 char *data, unsigned int len)
138 struct read_buffer *rb;
143 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
149 memcpy(rb->msg, data, len);
151 list_add_tail(&rb->list, &u->read_buffers);
153 wake_up(&u->read_waitq);
158 struct list_head list;
159 struct xenbus_watch watch;
160 struct xenbus_dev_data *dev_data;
164 static void free_watch_adapter (struct watch_adapter *watch)
166 kfree(watch->watch.node);
171 static void watch_fired(struct xenbus_watch *watch,
175 struct watch_adapter *adap =
176 container_of(watch, struct watch_adapter, watch);
177 struct xsd_sockmsg hdr;
178 const char *path, *token;
179 int path_len, tok_len, body_len, data_len = 0;
181 path = vec[XS_WATCH_PATH];
184 path_len = strlen(path) + 1;
185 tok_len = strlen(token) + 1;
187 data_len = vec[len] - vec[2] + 1;
188 body_len = path_len + tok_len + data_len;
190 hdr.type = XS_WATCH_EVENT;
193 mutex_lock(&adap->dev_data->reply_mutex);
194 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
195 queue_reply(adap->dev_data, (char *)path, path_len);
196 queue_reply(adap->dev_data, (char *)token, tok_len);
198 queue_reply(adap->dev_data, (char *)vec[2], data_len);
199 mutex_unlock(&adap->dev_data->reply_mutex);
202 static LIST_HEAD(watch_list);
204 static ssize_t xenbus_dev_write(struct file *filp,
205 const char __user *ubuf,
206 size_t len, loff_t *ppos)
208 struct xenbus_dev_data *u = filp->private_data;
209 struct xenbus_dev_transaction *trans = NULL;
213 struct watch_adapter *watch, *tmp_watch;
216 if (!is_xenstored_ready())
219 if ((len + u->len) > sizeof(u->u.buffer)) {
224 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
230 if ((u->len < sizeof(u->u.msg)) ||
231 (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
234 msg_type = u->u.msg.type;
239 static const char *XS_RESP = "OK";
240 struct xsd_sockmsg hdr;
242 path = u->u.buffer + sizeof(u->u.msg);
243 token = memchr(path, 0, u->u.msg.len);
250 if (msg_type == XS_WATCH) {
251 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
252 watch->watch.node = kmalloc(strlen(path)+1,
254 strcpy((char *)watch->watch.node, path);
255 watch->watch.callback = watch_fired;
256 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
257 strcpy(watch->token, token);
260 err = register_xenbus_watch(&watch->watch);
262 free_watch_adapter(watch);
267 list_add(&watch->list, &u->watches);
269 list_for_each_entry_safe(watch, tmp_watch,
271 if (!strcmp(watch->token, token) &&
272 !strcmp(watch->watch.node, path))
274 unregister_xenbus_watch(&watch->watch);
275 list_del(&watch->list);
276 free_watch_adapter(watch);
283 hdr.len = strlen(XS_RESP) + 1;
284 mutex_lock(&u->reply_mutex);
285 queue_reply(u, (char *)&hdr, sizeof(hdr));
286 queue_reply(u, (char *)XS_RESP, hdr.len);
287 mutex_unlock(&u->reply_mutex);
292 if (msg_type == XS_TRANSACTION_START) {
293 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
300 reply = xenbus_dev_request_and_reply(&u->u.msg);
307 if (msg_type == XS_TRANSACTION_START) {
308 trans->handle.id = simple_strtoul(reply, NULL, 0);
309 list_add(&trans->list, &u->transactions);
310 } else if (msg_type == XS_TRANSACTION_END) {
311 list_for_each_entry(trans, &u->transactions, list)
312 if (trans->handle.id == u->u.msg.tx_id)
314 BUG_ON(&trans->list == &u->transactions);
315 list_del(&trans->list);
318 mutex_lock(&u->reply_mutex);
319 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
320 queue_reply(u, (char *)reply, u->u.msg.len);
321 mutex_unlock(&u->reply_mutex);
331 static int xenbus_dev_open(struct inode *inode, struct file *filp)
333 struct xenbus_dev_data *u;
335 if (xen_store_evtchn == 0)
338 nonseekable_open(inode, filp);
340 u = kzalloc(sizeof(*u), GFP_KERNEL);
344 INIT_LIST_HEAD(&u->transactions);
345 INIT_LIST_HEAD(&u->watches);
346 INIT_LIST_HEAD(&u->read_buffers);
347 init_waitqueue_head(&u->read_waitq);
349 mutex_init(&u->reply_mutex);
351 filp->private_data = u;
356 static int xenbus_dev_release(struct inode *inode, struct file *filp)
358 struct xenbus_dev_data *u = filp->private_data;
359 struct xenbus_dev_transaction *trans, *tmp;
360 struct watch_adapter *watch, *tmp_watch;
362 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
363 xenbus_transaction_end(trans->handle, 1);
364 list_del(&trans->list);
368 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
369 unregister_xenbus_watch(&watch->watch);
370 list_del(&watch->list);
371 free_watch_adapter(watch);
379 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
381 struct xenbus_dev_data *u = file->private_data;
383 if (!is_xenstored_ready())
386 poll_wait(file, &u->read_waitq, wait);
387 if (!list_empty(&u->read_buffers))
388 return POLLIN | POLLRDNORM;
392 #ifdef HAVE_UNLOCKED_IOCTL
393 static long xenbus_dev_ioctl(struct file *file,
394 unsigned int cmd, unsigned long data)
396 extern int xenbus_conn(domid_t remote_dom, int *grant_ref,
397 evtchn_port_t *local_port);
398 void __user *udata = (void __user *) data;
401 if (!is_initial_xendomain())
406 case IOCTL_XENBUS_ALLOC: {
410 old = atomic_cmpxchg(&xenbus_xsd_state,
411 XENBUS_XSD_UNCOMMITTED,
412 XENBUS_XSD_FOREIGN_INIT);
413 if (old != XENBUS_XSD_UNCOMMITTED)
416 if (copy_from_user(&xa, udata, sizeof(xa))) {
418 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
422 ret = xenbus_conn(xa.dom, &xa.grant_ref, &xa.port);
424 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
428 if (copy_to_user(udata, &xa, sizeof(xa))) {
430 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
444 static const struct file_operations xenbus_dev_file_ops = {
445 .read = xenbus_dev_read,
446 .write = xenbus_dev_write,
447 .open = xenbus_dev_open,
448 .release = xenbus_dev_release,
449 .poll = xenbus_dev_poll,
450 #ifdef HAVE_UNLOCKED_IOCTL
451 .unlocked_ioctl = xenbus_dev_ioctl
455 int xenbus_dev_init(void)
457 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
459 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;