ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c @ 14224:4d447158e1dd

linux: xenbus dev write function resets packet data on *all* error conditions.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Mar 02 16:29:48 2007 +0000 (2007-03-02)
parents 36b923615369
children 2caed72258e8
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, rc = len;
178 if ((len + u->len) > sizeof(u->u.buffer)) {
179 rc = -EINVAL;
180 goto out;
181 }
183 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
184 rc = -EFAULT;
185 goto out;
186 }
188 u->len += len;
189 if ((u->len < sizeof(u->u.msg)) ||
190 (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
191 return rc;
193 msg_type = u->u.msg.type;
195 switch (msg_type) {
196 case XS_TRANSACTION_START:
197 case XS_TRANSACTION_END:
198 case XS_DIRECTORY:
199 case XS_READ:
200 case XS_GET_PERMS:
201 case XS_RELEASE:
202 case XS_GET_DOMAIN_PATH:
203 case XS_WRITE:
204 case XS_MKDIR:
205 case XS_RM:
206 case XS_SET_PERMS:
207 if (msg_type == XS_TRANSACTION_START) {
208 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
209 if (!trans) {
210 rc = -ENOMEM;
211 goto out;
212 }
213 }
215 reply = xenbus_dev_request_and_reply(&u->u.msg);
216 if (IS_ERR(reply)) {
217 kfree(trans);
218 rc = PTR_ERR(reply);
219 goto out;
220 }
222 if (msg_type == XS_TRANSACTION_START) {
223 trans->handle.id = simple_strtoul(reply, NULL, 0);
224 list_add(&trans->list, &u->transactions);
225 } else if (msg_type == XS_TRANSACTION_END) {
226 list_for_each_entry(trans, &u->transactions, list)
227 if (trans->handle.id == u->u.msg.tx_id)
228 break;
229 BUG_ON(&trans->list == &u->transactions);
230 list_del(&trans->list);
231 kfree(trans);
232 }
233 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
234 queue_reply(u, (char *)reply, u->u.msg.len);
235 kfree(reply);
236 break;
238 case XS_WATCH:
239 case XS_UNWATCH:
240 path = u->u.buffer + sizeof(u->u.msg);
241 token = memchr(path, 0, u->u.msg.len);
242 if (token == NULL) {
243 rc = -EILSEQ;
244 goto out;
245 }
246 token++;
248 if (msg_type == XS_WATCH) {
249 static const char * XS_WATCH_RESP = "OK";
250 struct xsd_sockmsg hdr;
252 watch = kmalloc(sizeof(*watch), GFP_KERNEL);
253 watch->watch.node = kmalloc(strlen(path)+1,
254 GFP_KERNEL);
255 strcpy((char *)watch->watch.node, path);
256 watch->watch.callback = watch_fired;
257 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
258 strcpy(watch->token, token);
259 watch->dev_data = u;
261 err = register_xenbus_watch(&watch->watch);
262 if (err) {
263 free_watch_adapter(watch);
264 rc = err;
265 goto out;
266 }
268 list_add(&watch->list, &u->watches);
270 hdr.type = XS_WATCH;
271 hdr.len = strlen(XS_WATCH_RESP) + 1;
272 queue_reply(u, (char *)&hdr, sizeof(hdr));
273 queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
274 } else {
275 list_for_each_entry_safe(watch, tmp_watch,
276 &u->watches, list) {
277 if (!strcmp(watch->token, token) &&
278 !strcmp(watch->watch.node, path))
279 {
280 unregister_xenbus_watch(&watch->watch);
281 list_del(&watch->list);
282 free_watch_adapter(watch);
283 break;
284 }
285 }
286 }
288 break;
290 default:
291 rc = -EINVAL;
292 break;
293 }
295 out:
296 u->len = 0;
297 return rc;
298 }
300 static int xenbus_dev_open(struct inode *inode, struct file *filp)
301 {
302 struct xenbus_dev_data *u;
304 if (xen_store_evtchn == 0)
305 return -ENOENT;
307 nonseekable_open(inode, filp);
309 u = kzalloc(sizeof(*u), GFP_KERNEL);
310 if (u == NULL)
311 return -ENOMEM;
313 INIT_LIST_HEAD(&u->transactions);
314 INIT_LIST_HEAD(&u->watches);
315 init_waitqueue_head(&u->read_waitq);
317 mutex_init(&u->reply_mutex);
319 filp->private_data = u;
321 return 0;
322 }
324 static int xenbus_dev_release(struct inode *inode, struct file *filp)
325 {
326 struct xenbus_dev_data *u = filp->private_data;
327 struct xenbus_dev_transaction *trans, *tmp;
328 struct watch_adapter *watch, *tmp_watch;
330 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
331 xenbus_transaction_end(trans->handle, 1);
332 list_del(&trans->list);
333 kfree(trans);
334 }
336 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
337 unregister_xenbus_watch(&watch->watch);
338 list_del(&watch->list);
339 free_watch_adapter(watch);
340 }
342 kfree(u);
344 return 0;
345 }
347 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
348 {
349 struct xenbus_dev_data *u = file->private_data;
351 poll_wait(file, &u->read_waitq, wait);
352 if (u->read_cons != u->read_prod)
353 return POLLIN | POLLRDNORM;
354 return 0;
355 }
357 static struct file_operations xenbus_dev_file_ops = {
358 .read = xenbus_dev_read,
359 .write = xenbus_dev_write,
360 .open = xenbus_dev_open,
361 .release = xenbus_dev_release,
362 .poll = xenbus_dev_poll,
363 };
365 int __init
366 xenbus_dev_init(void)
367 {
368 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
369 if (xenbus_dev_intf)
370 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
372 return 0;
373 }