ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c @ 11190:487fe5006064

[LINUX] Allow xenbus to be built as a module.
Signed-off-by: Steven Smith <ssmith@xensource.com>
author kfraser@localhost.localdomain
date Thu Aug 17 16:28:47 2006 +0100 (2006-08-17)
parents e061f14161b0
children e715360e82f8
line source
1 /*
2 * xenbus_dev.c
3 *
4 * Driver giving user-space access to the kernel's xenbus connection
5 * to xenstore.
6 *
7 * Copyright (c) 2005, Christian Limpach
8 * Copyright (c) 2005, Rusty Russell, IBM Corporation
9 *
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:
15 *
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:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
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
32 * IN THE SOFTWARE.
33 */
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>
41 #include <linux/fs.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;
55 };
57 struct xenbus_dev_data {
58 /* In-progress transaction. */
59 struct list_head transactions;
61 /* Active watches. */
62 struct list_head watches;
64 /* Partial request. */
65 unsigned int len;
66 union {
67 struct xsd_sockmsg msg;
68 char buffer[PAGE_SIZE];
69 } u;
71 /* Response queue. */
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;
78 };
80 static struct proc_dir_entry *xenbus_dev_intf;
82 static ssize_t xenbus_dev_read(struct file *filp,
83 char __user *ubuf,
84 size_t len, loff_t *ppos)
85 {
86 struct xenbus_dev_data *u = filp->private_data;
87 int i;
89 if (wait_event_interruptible(u->read_waitq,
90 u->read_prod != u->read_cons))
91 return -EINTR;
93 for (i = 0; i < len; i++) {
94 if (u->read_cons == u->read_prod)
95 break;
96 put_user(u->read_buffer[MASK_READ_IDX(u->read_cons)], ubuf+i);
97 u->read_cons++;
98 }
100 return i;
101 }
103 static void queue_reply(struct xenbus_dev_data *u,
104 char *data, unsigned int len)
105 {
106 int i;
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);
118 }
120 struct watch_adapter
121 {
122 struct list_head list;
123 struct xenbus_watch watch;
124 struct xenbus_dev_data *dev_data;
125 char *token;
126 };
128 static void free_watch_adapter (struct watch_adapter *watch)
129 {
130 kfree(watch->watch.node);
131 kfree(watch->token);
132 kfree(watch);
133 }
135 static void watch_fired(struct xenbus_watch *watch,
136 const char **vec,
137 unsigned int len)
138 {
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];
146 token = adap->token;
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;
153 hdr.len = body_len;
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);
158 }
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)
165 {
166 struct xenbus_dev_data *u = filp->private_data;
167 struct xenbus_dev_transaction *trans = NULL;
168 uint32_t msg_type;
169 void *reply;
170 char *path, *token;
171 struct watch_adapter *watch, *tmp_watch;
172 int err;
174 if ((len + u->len) > sizeof(u->u.buffer))
175 return -EINVAL;
177 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0)
178 return -EFAULT;
180 u->len += len;
181 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
182 return len;
184 msg_type = u->u.msg.type;
186 switch (msg_type) {
187 case XS_TRANSACTION_START:
188 case XS_TRANSACTION_END:
189 case XS_DIRECTORY:
190 case XS_READ:
191 case XS_GET_PERMS:
192 case XS_RELEASE:
193 case XS_GET_DOMAIN_PATH:
194 case XS_WRITE:
195 case XS_MKDIR:
196 case XS_RM:
197 case XS_SET_PERMS:
198 if (msg_type == XS_TRANSACTION_START) {
199 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
200 if (!trans)
201 return -ENOMEM;
202 }
204 reply = xenbus_dev_request_and_reply(&u->u.msg);
205 if (IS_ERR(reply)) {
206 kfree(trans);
207 return PTR_ERR(reply);
208 }
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)
216 break;
217 BUG_ON(&trans->list == &u->transactions);
218 list_del(&trans->list);
219 kfree(trans);
220 }
221 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
222 queue_reply(u, (char *)reply, u->u.msg.len);
223 kfree(reply);
224 break;
226 case XS_WATCH:
227 case XS_UNWATCH:
228 path = u->u.buffer + sizeof(u->u.msg);
229 token = memchr(path, 0, u->u.msg.len);
230 if (token == NULL)
231 return -EILSEQ;
232 token++;
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,
240 GFP_KERNEL);
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);
245 watch->dev_data = u;
247 err = register_xenbus_watch(&watch->watch);
248 if (err) {
249 free_watch_adapter(watch);
250 return err;
251 }
253 list_add(&watch->list, &u->watches);
255 hdr.type = XS_WATCH;
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);
259 } else {
260 list_for_each_entry_safe(watch, tmp_watch,
261 &u->watches, list) {
262 if (!strcmp(watch->token, token) &&
263 !strcmp(watch->watch.node, path))
264 break;
265 {
266 unregister_xenbus_watch(&watch->watch);
267 list_del(&watch->list);
268 free_watch_adapter(watch);
269 break;
270 }
271 }
272 }
274 break;
276 default:
277 return -EINVAL;
278 }
280 u->len = 0;
281 return len;
282 }
284 static int xenbus_dev_open(struct inode *inode, struct file *filp)
285 {
286 struct xenbus_dev_data *u;
288 if (xen_store_evtchn == 0)
289 return -ENOENT;
291 nonseekable_open(inode, filp);
293 u = kzalloc(sizeof(*u), GFP_KERNEL);
294 if (u == NULL)
295 return -ENOMEM;
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;
305 return 0;
306 }
308 static int xenbus_dev_release(struct inode *inode, struct file *filp)
309 {
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);
317 kfree(trans);
318 }
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);
324 }
326 kfree(u);
328 return 0;
329 }
331 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
332 {
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;
338 return 0;
339 }
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,
347 };
349 int __init
350 xenbus_dev_init(void)
351 {
352 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
353 if (xenbus_dev_intf)
354 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
356 return 0;
357 }