ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c @ 14100:e47738923a05

[LINUX] Purge include <linux/config.h>. It has been obsolete for some time now.

Signed-off-by: Ian Campbell <ian.campbell@xensource.com>
author Ian Campbell <ian.campbell@xensource.com>
date Fri Feb 23 16:56:45 2007 +0000 (2007-02-23)
parents 206e20b7b54e
children 36b923615369
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/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/uio.h>
38 #include <linux/notifier.h>
39 #include <linux/wait.h>
40 #include <linux/fs.h>
41 #include <linux/poll.h>
42 #include <linux/mutex.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 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
53 #include <xen/platform-compat.h>
54 #endif
56 struct xenbus_dev_transaction {
57 struct list_head list;
58 struct xenbus_transaction handle;
59 };
61 struct xenbus_dev_data {
62 /* In-progress transaction. */
63 struct list_head transactions;
65 /* Active watches. */
66 struct list_head watches;
68 /* Partial request. */
69 unsigned int len;
70 union {
71 struct xsd_sockmsg msg;
72 char buffer[PAGE_SIZE];
73 } u;
75 /* Response queue. */
76 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
77 char read_buffer[PAGE_SIZE];
78 unsigned int read_cons, read_prod;
79 wait_queue_head_t read_waitq;
81 struct mutex reply_mutex;
82 };
84 static struct proc_dir_entry *xenbus_dev_intf;
86 static ssize_t xenbus_dev_read(struct file *filp,
87 char __user *ubuf,
88 size_t len, loff_t *ppos)
89 {
90 struct xenbus_dev_data *u = filp->private_data;
91 int i;
93 if (wait_event_interruptible(u->read_waitq,
94 u->read_prod != u->read_cons))
95 return -EINTR;
97 for (i = 0; i < len; i++) {
98 if (u->read_cons == u->read_prod)
99 break;
100 put_user(u->read_buffer[MASK_READ_IDX(u->read_cons)], ubuf+i);
101 u->read_cons++;
102 }
104 return i;
105 }
107 static void queue_reply(struct xenbus_dev_data *u,
108 char *data, unsigned int len)
109 {
110 int i;
112 mutex_lock(&u->reply_mutex);
114 for (i = 0; i < len; i++, u->read_prod++)
115 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
117 BUG_ON((u->read_prod - u->read_cons) > sizeof(u->read_buffer));
119 mutex_unlock(&u->reply_mutex);
121 wake_up(&u->read_waitq);
122 }
124 struct watch_adapter
125 {
126 struct list_head list;
127 struct xenbus_watch watch;
128 struct xenbus_dev_data *dev_data;
129 char *token;
130 };
132 static void free_watch_adapter (struct watch_adapter *watch)
133 {
134 kfree(watch->watch.node);
135 kfree(watch->token);
136 kfree(watch);
137 }
139 static void watch_fired(struct xenbus_watch *watch,
140 const char **vec,
141 unsigned int len)
142 {
143 struct watch_adapter *adap =
144 container_of(watch, struct watch_adapter, watch);
145 struct xsd_sockmsg hdr;
146 const char *path, *token;
147 int path_len, tok_len, body_len;
149 path = vec[XS_WATCH_PATH];
150 token = adap->token;
152 path_len = strlen(path) + 1;
153 tok_len = strlen(token) + 1;
154 body_len = path_len + tok_len;
156 hdr.type = XS_WATCH_EVENT;
157 hdr.len = body_len;
159 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
160 queue_reply(adap->dev_data, (char *)path, path_len);
161 queue_reply(adap->dev_data, (char *)token, tok_len);
162 }
164 static LIST_HEAD(watch_list);
166 static ssize_t xenbus_dev_write(struct file *filp,
167 const char __user *ubuf,
168 size_t len, loff_t *ppos)
169 {
170 struct xenbus_dev_data *u = filp->private_data;
171 struct xenbus_dev_transaction *trans = NULL;
172 uint32_t msg_type;
173 void *reply;
174 char *path, *token;
175 struct watch_adapter *watch, *tmp_watch;
176 int err;
178 if ((len + u->len) > sizeof(u->u.buffer))
179 return -EINVAL;
181 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0)
182 return -EFAULT;
184 u->len += len;
185 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
186 return len;
188 msg_type = u->u.msg.type;
190 switch (msg_type) {
191 case XS_TRANSACTION_START:
192 case XS_TRANSACTION_END:
193 case XS_DIRECTORY:
194 case XS_READ:
195 case XS_GET_PERMS:
196 case XS_RELEASE:
197 case XS_GET_DOMAIN_PATH:
198 case XS_WRITE:
199 case XS_MKDIR:
200 case XS_RM:
201 case XS_SET_PERMS:
202 if (msg_type == XS_TRANSACTION_START) {
203 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
204 if (!trans)
205 return -ENOMEM;
206 }
208 reply = xenbus_dev_request_and_reply(&u->u.msg);
209 if (IS_ERR(reply)) {
210 kfree(trans);
211 return PTR_ERR(reply);
212 }
214 if (msg_type == XS_TRANSACTION_START) {
215 trans->handle.id = simple_strtoul(reply, NULL, 0);
216 list_add(&trans->list, &u->transactions);
217 } else if (msg_type == XS_TRANSACTION_END) {
218 list_for_each_entry(trans, &u->transactions, list)
219 if (trans->handle.id == u->u.msg.tx_id)
220 break;
221 BUG_ON(&trans->list == &u->transactions);
222 list_del(&trans->list);
223 kfree(trans);
224 }
225 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
226 queue_reply(u, (char *)reply, u->u.msg.len);
227 kfree(reply);
228 break;
230 case XS_WATCH:
231 case XS_UNWATCH:
232 path = u->u.buffer + sizeof(u->u.msg);
233 token = memchr(path, 0, u->u.msg.len);
234 if (token == NULL)
235 return -EILSEQ;
236 token++;
238 if (msg_type == XS_WATCH) {
239 static const char * XS_WATCH_RESP = "OK";
240 struct xsd_sockmsg hdr;
242 watch = kmalloc(sizeof(*watch), GFP_KERNEL);
243 watch->watch.node = kmalloc(strlen(path)+1,
244 GFP_KERNEL);
245 strcpy((char *)watch->watch.node, path);
246 watch->watch.callback = watch_fired;
247 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
248 strcpy(watch->token, token);
249 watch->dev_data = u;
251 err = register_xenbus_watch(&watch->watch);
252 if (err) {
253 free_watch_adapter(watch);
254 return err;
255 }
257 list_add(&watch->list, &u->watches);
259 hdr.type = XS_WATCH;
260 hdr.len = strlen(XS_WATCH_RESP) + 1;
261 queue_reply(u, (char *)&hdr, sizeof(hdr));
262 queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
263 } else {
264 list_for_each_entry_safe(watch, tmp_watch,
265 &u->watches, list) {
266 if (!strcmp(watch->token, token) &&
267 !strcmp(watch->watch.node, path))
268 break;
269 {
270 unregister_xenbus_watch(&watch->watch);
271 list_del(&watch->list);
272 free_watch_adapter(watch);
273 break;
274 }
275 }
276 }
278 break;
280 default:
281 return -EINVAL;
282 }
284 u->len = 0;
285 return len;
286 }
288 static int xenbus_dev_open(struct inode *inode, struct file *filp)
289 {
290 struct xenbus_dev_data *u;
292 if (xen_store_evtchn == 0)
293 return -ENOENT;
295 nonseekable_open(inode, filp);
297 u = kzalloc(sizeof(*u), GFP_KERNEL);
298 if (u == NULL)
299 return -ENOMEM;
301 INIT_LIST_HEAD(&u->transactions);
302 INIT_LIST_HEAD(&u->watches);
303 init_waitqueue_head(&u->read_waitq);
305 mutex_init(&u->reply_mutex);
307 filp->private_data = u;
309 return 0;
310 }
312 static int xenbus_dev_release(struct inode *inode, struct file *filp)
313 {
314 struct xenbus_dev_data *u = filp->private_data;
315 struct xenbus_dev_transaction *trans, *tmp;
316 struct watch_adapter *watch, *tmp_watch;
318 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
319 xenbus_transaction_end(trans->handle, 1);
320 list_del(&trans->list);
321 kfree(trans);
322 }
324 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
325 unregister_xenbus_watch(&watch->watch);
326 list_del(&watch->list);
327 free_watch_adapter(watch);
328 }
330 kfree(u);
332 return 0;
333 }
335 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
336 {
337 struct xenbus_dev_data *u = file->private_data;
339 poll_wait(file, &u->read_waitq, wait);
340 if (u->read_cons != u->read_prod)
341 return POLLIN | POLLRDNORM;
342 return 0;
343 }
345 static struct file_operations xenbus_dev_file_ops = {
346 .read = xenbus_dev_read,
347 .write = xenbus_dev_write,
348 .open = xenbus_dev_open,
349 .release = xenbus_dev_release,
350 .poll = xenbus_dev_poll,
351 };
353 int __init
354 xenbus_dev_init(void)
355 {
356 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
357 if (xenbus_dev_intf)
358 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
360 return 0;
361 }