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/errno.h>
37 #include <linux/uio.h>
38 #include <linux/notifier.h>
39 #include <linux/wait.h>
41 #include <linux/poll.h>
43 #include "xenbus_comms.h"
45 #include <asm/uaccess.h>
46 #include <asm/hypervisor.h>
47 #include <xen/xenbus.h>
48 #include <xen/xen_proc.h>
49 #include <asm/hypervisor.h>
51 struct xenbus_dev_transaction {
52 struct list_head list;
53 struct xenbus_transaction handle;
56 struct xenbus_dev_data {
57 /* In-progress transaction. */
58 struct list_head transactions;
61 struct list_head watches;
63 /* Partial request. */
66 struct xsd_sockmsg msg;
67 char buffer[PAGE_SIZE];
71 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
72 char read_buffer[PAGE_SIZE];
73 unsigned int read_cons, read_prod;
74 wait_queue_head_t read_waitq;
76 struct mutex reply_mutex;
79 static struct proc_dir_entry *xenbus_dev_intf;
81 static ssize_t xenbus_dev_read(struct file *filp,
83 size_t len, loff_t *ppos)
85 struct xenbus_dev_data *u = filp->private_data;
88 if (wait_event_interruptible(u->read_waitq,
89 u->read_prod != u->read_cons))
92 for (i = 0; i < len; i++) {
93 if (u->read_cons == u->read_prod)
95 put_user(u->read_buffer[MASK_READ_IDX(u->read_cons)], ubuf+i);
102 static void queue_reply(struct xenbus_dev_data *u,
103 char *data, unsigned int len)
107 mutex_lock(&u->reply_mutex);
109 for (i = 0; i < len; i++, u->read_prod++)
110 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
112 BUG_ON((u->read_prod - u->read_cons) > sizeof(u->read_buffer));
114 mutex_unlock(&u->reply_mutex);
116 wake_up(&u->read_waitq);
121 struct list_head list;
122 struct xenbus_watch watch;
123 struct xenbus_dev_data *dev_data;
127 static void free_watch_adapter (struct watch_adapter *watch)
129 kfree(watch->watch.node);
134 static void watch_fired(struct xenbus_watch *watch,
138 struct watch_adapter *adap =
139 container_of(watch, struct watch_adapter, watch);
140 struct xsd_sockmsg hdr;
141 const char *path, *token;
142 int path_len, tok_len, body_len;
144 path = vec[XS_WATCH_PATH];
147 path_len = strlen(path) + 1;
148 tok_len = strlen(token) + 1;
149 body_len = path_len + tok_len;
151 hdr.type = XS_WATCH_EVENT;
154 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
155 queue_reply(adap->dev_data, (char *)path, path_len);
156 queue_reply(adap->dev_data, (char *)token, tok_len);
159 static LIST_HEAD(watch_list);
161 static ssize_t xenbus_dev_write(struct file *filp,
162 const char __user *ubuf,
163 size_t len, loff_t *ppos)
165 struct xenbus_dev_data *u = filp->private_data;
166 struct xenbus_dev_transaction *trans = NULL;
170 struct watch_adapter *watch, *tmp_watch;
173 if ((len + u->len) > sizeof(u->u.buffer))
176 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0)
180 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
183 msg_type = u->u.msg.type;
186 case XS_TRANSACTION_START:
187 case XS_TRANSACTION_END:
192 case XS_GET_DOMAIN_PATH:
197 if (msg_type == XS_TRANSACTION_START) {
198 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
203 reply = xenbus_dev_request_and_reply(&u->u.msg);
206 return PTR_ERR(reply);
209 if (msg_type == XS_TRANSACTION_START) {
210 trans->handle.id = simple_strtoul(reply, NULL, 0);
211 list_add(&trans->list, &u->transactions);
212 } else if (msg_type == XS_TRANSACTION_END) {
213 list_for_each_entry(trans, &u->transactions, list)
214 if (trans->handle.id == u->u.msg.tx_id)
216 BUG_ON(&trans->list == &u->transactions);
217 list_del(&trans->list);
220 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
221 queue_reply(u, (char *)reply, u->u.msg.len);
227 path = u->u.buffer + sizeof(u->u.msg);
228 token = memchr(path, 0, u->u.msg.len);
233 if (msg_type == XS_WATCH) {
234 static const char * XS_WATCH_RESP = "OK";
235 struct xsd_sockmsg hdr;
237 watch = kmalloc(sizeof(*watch), GFP_KERNEL);
238 watch->watch.node = kmalloc(strlen(path)+1,
240 strcpy((char *)watch->watch.node, path);
241 watch->watch.callback = watch_fired;
242 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
243 strcpy(watch->token, token);
246 err = register_xenbus_watch(&watch->watch);
248 free_watch_adapter(watch);
252 list_add(&watch->list, &u->watches);
255 hdr.len = strlen(XS_WATCH_RESP) + 1;
256 queue_reply(u, (char *)&hdr, sizeof(hdr));
257 queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
259 list_for_each_entry_safe(watch, tmp_watch,
261 if (!strcmp(watch->token, token) &&
262 !strcmp(watch->watch.node, path))
265 unregister_xenbus_watch(&watch->watch);
266 list_del(&watch->list);
267 free_watch_adapter(watch);
283 static int xenbus_dev_open(struct inode *inode, struct file *filp)
285 struct xenbus_dev_data *u;
287 if (xen_store_evtchn == 0)
290 nonseekable_open(inode, filp);
292 u = kzalloc(sizeof(*u), GFP_KERNEL);
296 INIT_LIST_HEAD(&u->transactions);
297 INIT_LIST_HEAD(&u->watches);
298 init_waitqueue_head(&u->read_waitq);
300 mutex_init(&u->reply_mutex);
302 filp->private_data = u;
307 static int xenbus_dev_release(struct inode *inode, struct file *filp)
309 struct xenbus_dev_data *u = filp->private_data;
310 struct xenbus_dev_transaction *trans, *tmp;
311 struct watch_adapter *watch, *tmp_watch;
313 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
314 xenbus_transaction_end(trans->handle, 1);
315 list_del(&trans->list);
319 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
320 unregister_xenbus_watch(&watch->watch);
321 list_del(&watch->list);
322 free_watch_adapter(watch);
330 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
332 struct xenbus_dev_data *u = file->private_data;
334 poll_wait(file, &u->read_waitq, wait);
335 if (u->read_cons != u->read_prod)
336 return POLLIN | POLLRDNORM;
340 static struct file_operations xenbus_dev_file_ops = {
341 .read = xenbus_dev_read,
342 .write = xenbus_dev_write,
343 .open = xenbus_dev_open,
344 .release = xenbus_dev_release,
345 .poll = xenbus_dev_poll,
349 xenbus_dev_init(void)
351 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
353 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;