ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c @ 14223:36b923615369

linux: Fix xenbus device write function.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Mar 02 16:26:56 2007 +0000 (2007-03-02)
parents e47738923a05
children 4d447158e1dd
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 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)) ||
186 (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
187 return rc;
189 msg_type = u->u.msg.type;
191 switch (msg_type) {
192 case XS_TRANSACTION_START:
193 case XS_TRANSACTION_END:
194 case XS_DIRECTORY:
195 case XS_READ:
196 case XS_GET_PERMS:
197 case XS_RELEASE:
198 case XS_GET_DOMAIN_PATH:
199 case XS_WRITE:
200 case XS_MKDIR:
201 case XS_RM:
202 case XS_SET_PERMS:
203 if (msg_type == XS_TRANSACTION_START) {
204 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
205 if (!trans) {
206 rc = -ENOMEM;
207 goto out;
208 }
209 }
211 reply = xenbus_dev_request_and_reply(&u->u.msg);
212 if (IS_ERR(reply)) {
213 kfree(trans);
214 rc = PTR_ERR(reply);
215 goto out;
216 }
218 if (msg_type == XS_TRANSACTION_START) {
219 trans->handle.id = simple_strtoul(reply, NULL, 0);
220 list_add(&trans->list, &u->transactions);
221 } else if (msg_type == XS_TRANSACTION_END) {
222 list_for_each_entry(trans, &u->transactions, list)
223 if (trans->handle.id == u->u.msg.tx_id)
224 break;
225 BUG_ON(&trans->list == &u->transactions);
226 list_del(&trans->list);
227 kfree(trans);
228 }
229 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
230 queue_reply(u, (char *)reply, u->u.msg.len);
231 kfree(reply);
232 break;
234 case XS_WATCH:
235 case XS_UNWATCH:
236 path = u->u.buffer + sizeof(u->u.msg);
237 token = memchr(path, 0, u->u.msg.len);
238 if (token == NULL) {
239 rc = -EILSEQ;
240 goto out;
241 }
242 token++;
244 if (msg_type == XS_WATCH) {
245 static const char * XS_WATCH_RESP = "OK";
246 struct xsd_sockmsg hdr;
248 watch = kmalloc(sizeof(*watch), GFP_KERNEL);
249 watch->watch.node = kmalloc(strlen(path)+1,
250 GFP_KERNEL);
251 strcpy((char *)watch->watch.node, path);
252 watch->watch.callback = watch_fired;
253 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
254 strcpy(watch->token, token);
255 watch->dev_data = u;
257 err = register_xenbus_watch(&watch->watch);
258 if (err) {
259 free_watch_adapter(watch);
260 rc = err;
261 goto out;
262 }
264 list_add(&watch->list, &u->watches);
266 hdr.type = XS_WATCH;
267 hdr.len = strlen(XS_WATCH_RESP) + 1;
268 queue_reply(u, (char *)&hdr, sizeof(hdr));
269 queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
270 } else {
271 list_for_each_entry_safe(watch, tmp_watch,
272 &u->watches, list) {
273 if (!strcmp(watch->token, token) &&
274 !strcmp(watch->watch.node, path))
275 {
276 unregister_xenbus_watch(&watch->watch);
277 list_del(&watch->list);
278 free_watch_adapter(watch);
279 break;
280 }
281 }
282 }
284 break;
286 default:
287 rc = -EINVAL;
288 break;
289 }
291 out:
292 u->len = 0;
293 return rc;
294 }
296 static int xenbus_dev_open(struct inode *inode, struct file *filp)
297 {
298 struct xenbus_dev_data *u;
300 if (xen_store_evtchn == 0)
301 return -ENOENT;
303 nonseekable_open(inode, filp);
305 u = kzalloc(sizeof(*u), GFP_KERNEL);
306 if (u == NULL)
307 return -ENOMEM;
309 INIT_LIST_HEAD(&u->transactions);
310 INIT_LIST_HEAD(&u->watches);
311 init_waitqueue_head(&u->read_waitq);
313 mutex_init(&u->reply_mutex);
315 filp->private_data = u;
317 return 0;
318 }
320 static int xenbus_dev_release(struct inode *inode, struct file *filp)
321 {
322 struct xenbus_dev_data *u = filp->private_data;
323 struct xenbus_dev_transaction *trans, *tmp;
324 struct watch_adapter *watch, *tmp_watch;
326 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
327 xenbus_transaction_end(trans->handle, 1);
328 list_del(&trans->list);
329 kfree(trans);
330 }
332 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
333 unregister_xenbus_watch(&watch->watch);
334 list_del(&watch->list);
335 free_watch_adapter(watch);
336 }
338 kfree(u);
340 return 0;
341 }
343 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
344 {
345 struct xenbus_dev_data *u = file->private_data;
347 poll_wait(file, &u->read_waitq, wait);
348 if (u->read_cons != u->read_prod)
349 return POLLIN | POLLRDNORM;
350 return 0;
351 }
353 static struct file_operations xenbus_dev_file_ops = {
354 .read = xenbus_dev_read,
355 .write = xenbus_dev_write,
356 .open = xenbus_dev_open,
357 .release = xenbus_dev_release,
358 .poll = xenbus_dev_poll,
359 };
361 int __init
362 xenbus_dev_init(void)
363 {
364 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
365 if (xenbus_dev_intf)
366 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
368 return 0;
369 }