ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_dev.c @ 877:f3a935eb30e0

PV-on-HVM: xenbus - check HAVE_UNLOCKED_IOCTL for old Linux kernels.

Signed-off-by: KUWAMURA Shin'ya <kuwa@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 26 09:53:55 2009 +0100 (2009-05-26)
parents bd7e30b58d12
children
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 #include <xen/public/xenbus.h>
58 struct xenbus_dev_transaction {
59 struct list_head list;
60 struct xenbus_transaction handle;
61 };
63 struct read_buffer {
64 struct list_head list;
65 unsigned int cons;
66 unsigned int len;
67 char msg[];
68 };
70 struct xenbus_dev_data {
71 /* In-progress transaction. */
72 struct list_head transactions;
74 /* Active watches. */
75 struct list_head watches;
77 /* Partial request. */
78 unsigned int len;
79 union {
80 struct xsd_sockmsg msg;
81 char buffer[PAGE_SIZE];
82 } u;
84 /* Response queue. */
85 struct list_head read_buffers;
86 wait_queue_head_t read_waitq;
88 struct mutex reply_mutex;
89 };
91 static struct proc_dir_entry *xenbus_dev_intf;
93 static ssize_t xenbus_dev_read(struct file *filp,
94 char __user *ubuf,
95 size_t len, loff_t *ppos)
96 {
97 struct xenbus_dev_data *u = filp->private_data;
98 struct read_buffer *rb;
99 int i, ret;
101 if (!is_xenstored_ready())
102 return -ENODEV;
104 mutex_lock(&u->reply_mutex);
105 while (list_empty(&u->read_buffers)) {
106 mutex_unlock(&u->reply_mutex);
107 ret = wait_event_interruptible(u->read_waitq,
108 !list_empty(&u->read_buffers));
109 if (ret)
110 return ret;
111 mutex_lock(&u->reply_mutex);
112 }
114 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
115 for (i = 0; i < len;) {
116 put_user(rb->msg[rb->cons], ubuf + i);
117 i++;
118 rb->cons++;
119 if (rb->cons == rb->len) {
120 list_del(&rb->list);
121 kfree(rb);
122 if (list_empty(&u->read_buffers))
123 break;
124 rb = list_entry(u->read_buffers.next,
125 struct read_buffer, list);
126 }
127 }
128 mutex_unlock(&u->reply_mutex);
130 return i;
131 }
133 static void queue_reply(struct xenbus_dev_data *u,
134 char *data, unsigned int len)
135 {
136 struct read_buffer *rb;
138 if (len == 0)
139 return;
141 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
142 BUG_ON(rb == NULL);
144 rb->cons = 0;
145 rb->len = len;
147 memcpy(rb->msg, data, len);
149 list_add_tail(&rb->list, &u->read_buffers);
151 wake_up(&u->read_waitq);
152 }
154 struct watch_adapter
155 {
156 struct list_head list;
157 struct xenbus_watch watch;
158 struct xenbus_dev_data *dev_data;
159 char *token;
160 };
162 static void free_watch_adapter (struct watch_adapter *watch)
163 {
164 kfree(watch->watch.node);
165 kfree(watch->token);
166 kfree(watch);
167 }
169 static void watch_fired(struct xenbus_watch *watch,
170 const char **vec,
171 unsigned int len)
172 {
173 struct watch_adapter *adap =
174 container_of(watch, struct watch_adapter, watch);
175 struct xsd_sockmsg hdr;
176 const char *path, *token;
177 int path_len, tok_len, body_len, data_len = 0;
179 path = vec[XS_WATCH_PATH];
180 token = adap->token;
182 path_len = strlen(path) + 1;
183 tok_len = strlen(token) + 1;
184 if (len > 2)
185 data_len = vec[len] - vec[2] + 1;
186 body_len = path_len + tok_len + data_len;
188 hdr.type = XS_WATCH_EVENT;
189 hdr.len = body_len;
191 mutex_lock(&adap->dev_data->reply_mutex);
192 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
193 queue_reply(adap->dev_data, (char *)path, path_len);
194 queue_reply(adap->dev_data, (char *)token, tok_len);
195 if (len > 2)
196 queue_reply(adap->dev_data, (char *)vec[2], data_len);
197 mutex_unlock(&adap->dev_data->reply_mutex);
198 }
200 static LIST_HEAD(watch_list);
202 static ssize_t xenbus_dev_write(struct file *filp,
203 const char __user *ubuf,
204 size_t len, loff_t *ppos)
205 {
206 struct xenbus_dev_data *u = filp->private_data;
207 struct xenbus_dev_transaction *trans = NULL;
208 uint32_t msg_type;
209 void *reply;
210 char *path, *token;
211 struct watch_adapter *watch, *tmp_watch;
212 int err, rc = len;
214 if (!is_xenstored_ready())
215 return -ENODEV;
217 if ((len + u->len) > sizeof(u->u.buffer)) {
218 rc = -EINVAL;
219 goto out;
220 }
222 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
223 rc = -EFAULT;
224 goto out;
225 }
227 u->len += len;
228 if ((u->len < sizeof(u->u.msg)) ||
229 (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
230 return rc;
232 msg_type = u->u.msg.type;
234 switch (msg_type) {
235 case XS_WATCH:
236 case XS_UNWATCH: {
237 static const char *XS_RESP = "OK";
238 struct xsd_sockmsg hdr;
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 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
250 watch->watch.node = kmalloc(strlen(path)+1,
251 GFP_KERNEL);
252 strcpy((char *)watch->watch.node, path);
253 watch->watch.callback = watch_fired;
254 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
255 strcpy(watch->token, token);
256 watch->dev_data = u;
258 err = register_xenbus_watch(&watch->watch);
259 if (err) {
260 free_watch_adapter(watch);
261 rc = err;
262 goto out;
263 }
265 list_add(&watch->list, &u->watches);
266 } else {
267 list_for_each_entry_safe(watch, tmp_watch,
268 &u->watches, list) {
269 if (!strcmp(watch->token, token) &&
270 !strcmp(watch->watch.node, path))
271 {
272 unregister_xenbus_watch(&watch->watch);
273 list_del(&watch->list);
274 free_watch_adapter(watch);
275 break;
276 }
277 }
278 }
280 hdr.type = msg_type;
281 hdr.len = strlen(XS_RESP) + 1;
282 mutex_lock(&u->reply_mutex);
283 queue_reply(u, (char *)&hdr, sizeof(hdr));
284 queue_reply(u, (char *)XS_RESP, hdr.len);
285 mutex_unlock(&u->reply_mutex);
286 break;
287 }
289 default:
290 if (msg_type == XS_TRANSACTION_START) {
291 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
292 if (!trans) {
293 rc = -ENOMEM;
294 goto out;
295 }
296 }
298 reply = xenbus_dev_request_and_reply(&u->u.msg);
299 if (IS_ERR(reply)) {
300 kfree(trans);
301 rc = PTR_ERR(reply);
302 goto out;
303 }
305 if (msg_type == XS_TRANSACTION_START) {
306 trans->handle.id = simple_strtoul(reply, NULL, 0);
307 list_add(&trans->list, &u->transactions);
308 } else if (msg_type == XS_TRANSACTION_END) {
309 list_for_each_entry(trans, &u->transactions, list)
310 if (trans->handle.id == u->u.msg.tx_id)
311 break;
312 BUG_ON(&trans->list == &u->transactions);
313 list_del(&trans->list);
314 kfree(trans);
315 }
316 mutex_lock(&u->reply_mutex);
317 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
318 queue_reply(u, (char *)reply, u->u.msg.len);
319 mutex_unlock(&u->reply_mutex);
320 kfree(reply);
321 break;
322 }
324 out:
325 u->len = 0;
326 return rc;
327 }
329 static int xenbus_dev_open(struct inode *inode, struct file *filp)
330 {
331 struct xenbus_dev_data *u;
333 if (xen_store_evtchn == 0)
334 return -ENOENT;
336 nonseekable_open(inode, filp);
338 u = kzalloc(sizeof(*u), GFP_KERNEL);
339 if (u == NULL)
340 return -ENOMEM;
342 INIT_LIST_HEAD(&u->transactions);
343 INIT_LIST_HEAD(&u->watches);
344 INIT_LIST_HEAD(&u->read_buffers);
345 init_waitqueue_head(&u->read_waitq);
347 mutex_init(&u->reply_mutex);
349 filp->private_data = u;
351 return 0;
352 }
354 static int xenbus_dev_release(struct inode *inode, struct file *filp)
355 {
356 struct xenbus_dev_data *u = filp->private_data;
357 struct xenbus_dev_transaction *trans, *tmp;
358 struct watch_adapter *watch, *tmp_watch;
360 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
361 xenbus_transaction_end(trans->handle, 1);
362 list_del(&trans->list);
363 kfree(trans);
364 }
366 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
367 unregister_xenbus_watch(&watch->watch);
368 list_del(&watch->list);
369 free_watch_adapter(watch);
370 }
372 kfree(u);
374 return 0;
375 }
377 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
378 {
379 struct xenbus_dev_data *u = file->private_data;
381 if (!is_xenstored_ready())
382 return -ENODEV;
384 poll_wait(file, &u->read_waitq, wait);
385 if (!list_empty(&u->read_buffers))
386 return POLLIN | POLLRDNORM;
387 return 0;
388 }
390 #ifdef HAVE_UNLOCKED_IOCTL
391 static long xenbus_dev_ioctl(struct file *file,
392 unsigned int cmd, unsigned long data)
393 {
394 extern int xenbus_conn(domid_t remote_dom, int *grant_ref,
395 evtchn_port_t *local_port);
396 void __user *udata = (void __user *) data;
397 int ret = -ENOTTY;
399 if (!is_initial_xendomain())
400 return -ENODEV;
403 switch (cmd) {
404 case IOCTL_XENBUS_ALLOC: {
405 xenbus_alloc_t xa;
406 int old;
408 old = atomic_cmpxchg(&xenbus_xsd_state,
409 XENBUS_XSD_UNCOMMITTED,
410 XENBUS_XSD_FOREIGN_INIT);
411 if (old != XENBUS_XSD_UNCOMMITTED)
412 return -EBUSY;
414 if (copy_from_user(&xa, udata, sizeof(xa))) {
415 ret = -EFAULT;
416 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
417 break;
418 }
420 ret = xenbus_conn(xa.dom, &xa.grant_ref, &xa.port);
421 if (ret != 0) {
422 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
423 break;
424 }
426 if (copy_to_user(udata, &xa, sizeof(xa))) {
427 ret = -EFAULT;
428 atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
429 break;
430 }
431 }
432 break;
434 default:
435 break;
436 }
438 return ret;
439 }
440 #endif
442 static const struct file_operations xenbus_dev_file_ops = {
443 .read = xenbus_dev_read,
444 .write = xenbus_dev_write,
445 .open = xenbus_dev_open,
446 .release = xenbus_dev_release,
447 .poll = xenbus_dev_poll,
448 #ifdef HAVE_UNLOCKED_IOCTL
449 .unlocked_ioctl = xenbus_dev_ioctl
450 #endif
451 };
453 int xenbus_dev_init(void)
454 {
455 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
456 if (xenbus_dev_intf)
457 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
459 return 0;
460 }