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/config.h>
36 #include <linux/kernel.h>
37 #include <linux/errno.h>
38 #include <linux/uio.h>
39 #include <linux/notifier.h>
40 #include <linux/wait.h>
42 #include <linux/poll.h>
44 #include "xenbus_comms.h"
46 #include <asm/uaccess.h>
47 #include <asm/hypervisor.h>
48 #include <xen/xenbus.h>
49 #include <xen/xen_proc.h>
50 #include <asm/hypervisor.h>
52 struct xenbus_dev_transaction {
53 struct list_head list;
54 struct xenbus_transaction handle;
57 struct xenbus_dev_data {
58 /* In-progress transaction. */
59 struct list_head transactions;
62 struct list_head watches;
64 /* Partial request. */
67 struct xsd_sockmsg msg;
68 char buffer[PAGE_SIZE];
72 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
73 char read_buffer[PAGE_SIZE];
74 unsigned int read_cons, read_prod;
75 wait_queue_head_t read_waitq;
77 struct mutex reply_mutex;
80 static struct proc_dir_entry *xenbus_dev_intf;
82 static ssize_t xenbus_dev_read(struct file *filp,
84 size_t len, loff_t *ppos)
86 struct xenbus_dev_data *u = filp->private_data;
89 if (wait_event_interruptible(u->read_waitq,
90 u->read_prod != u->read_cons))
93 for (i = 0; i < len; i++) {
94 if (u->read_cons == u->read_prod)
96 put_user(u->read_buffer[MASK_READ_IDX(u->read_cons)], ubuf+i);
103 static void queue_reply(struct xenbus_dev_data *u,
104 char *data, unsigned int len)
108 mutex_lock(&u->reply_mutex);
110 for (i = 0; i < len; i++, u->read_prod++)
111 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
113 BUG_ON((u->read_prod - u->read_cons) > sizeof(u->read_buffer));
115 mutex_unlock(&u->reply_mutex);
117 wake_up(&u->read_waitq);
122 struct list_head list;
123 struct xenbus_watch watch;
124 struct xenbus_dev_data *dev_data;
128 static void free_watch_adapter (struct watch_adapter *watch)
130 kfree(watch->watch.node);
135 static void watch_fired(struct xenbus_watch *watch,
139 struct watch_adapter *adap =
140 container_of(watch, struct watch_adapter, watch);
141 struct xsd_sockmsg hdr;
142 const char *path, *token;
143 int path_len, tok_len, body_len;
145 path = vec[XS_WATCH_PATH];
148 path_len = strlen(path) + 1;
149 tok_len = strlen(token) + 1;
150 body_len = path_len + tok_len;
152 hdr.type = XS_WATCH_EVENT;
155 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
156 queue_reply(adap->dev_data, (char *)path, path_len);
157 queue_reply(adap->dev_data, (char *)token, tok_len);
160 static LIST_HEAD(watch_list);
162 static ssize_t xenbus_dev_write(struct file *filp,
163 const char __user *ubuf,
164 size_t len, loff_t *ppos)
166 struct xenbus_dev_data *u = filp->private_data;
167 struct xenbus_dev_transaction *trans = NULL;
171 struct watch_adapter *watch, *tmp_watch;
174 if ((len + u->len) > sizeof(u->u.buffer))
177 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0)
181 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
184 msg_type = u->u.msg.type;
187 case XS_TRANSACTION_START:
188 case XS_TRANSACTION_END:
193 case XS_GET_DOMAIN_PATH:
198 if (msg_type == XS_TRANSACTION_START) {
199 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
204 reply = xenbus_dev_request_and_reply(&u->u.msg);
207 return PTR_ERR(reply);
210 if (msg_type == XS_TRANSACTION_START) {
211 trans->handle.id = simple_strtoul(reply, NULL, 0);
212 list_add(&trans->list, &u->transactions);
213 } else if (msg_type == XS_TRANSACTION_END) {
214 list_for_each_entry(trans, &u->transactions, list)
215 if (trans->handle.id == u->u.msg.tx_id)
217 BUG_ON(&trans->list == &u->transactions);
218 list_del(&trans->list);
221 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
222 queue_reply(u, (char *)reply, u->u.msg.len);
228 path = u->u.buffer + sizeof(u->u.msg);
229 token = memchr(path, 0, u->u.msg.len);
234 if (msg_type == XS_WATCH) {
235 static const char * XS_WATCH_RESP = "OK";
236 struct xsd_sockmsg hdr;
238 watch = kmalloc(sizeof(*watch), GFP_KERNEL);
239 watch->watch.node = kmalloc(strlen(path)+1,
241 strcpy((char *)watch->watch.node, path);
242 watch->watch.callback = watch_fired;
243 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
244 strcpy(watch->token, token);
247 err = register_xenbus_watch(&watch->watch);
249 free_watch_adapter(watch);
253 list_add(&watch->list, &u->watches);
256 hdr.len = strlen(XS_WATCH_RESP) + 1;
257 queue_reply(u, (char *)&hdr, sizeof(hdr));
258 queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
260 list_for_each_entry_safe(watch, tmp_watch,
262 if (!strcmp(watch->token, token) &&
263 !strcmp(watch->watch.node, path))
266 unregister_xenbus_watch(&watch->watch);
267 list_del(&watch->list);
268 free_watch_adapter(watch);
284 static int xenbus_dev_open(struct inode *inode, struct file *filp)
286 struct xenbus_dev_data *u;
288 if (xen_start_info->store_evtchn == 0)
291 nonseekable_open(inode, filp);
293 u = kzalloc(sizeof(*u), GFP_KERNEL);
297 INIT_LIST_HEAD(&u->transactions);
298 INIT_LIST_HEAD(&u->watches);
299 init_waitqueue_head(&u->read_waitq);
301 mutex_init(&u->reply_mutex);
303 filp->private_data = u;
308 static int xenbus_dev_release(struct inode *inode, struct file *filp)
310 struct xenbus_dev_data *u = filp->private_data;
311 struct xenbus_dev_transaction *trans, *tmp;
312 struct watch_adapter *watch, *tmp_watch;
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);
331 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
333 struct xenbus_dev_data *u = file->private_data;
335 poll_wait(file, &u->read_waitq, wait);
336 if (u->read_cons != u->read_prod)
337 return POLLIN | POLLRDNORM;
341 static struct file_operations xenbus_dev_file_ops = {
342 .read = xenbus_dev_read,
343 .write = xenbus_dev_write,
344 .open = xenbus_dev_open,
345 .release = xenbus_dev_release,
346 .poll = xenbus_dev_poll,
350 xenbus_dev_init(void)
352 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
354 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
359 __initcall(xenbus_dev_init);