Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_dev.c
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  */
34
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/uio.h>
39 #include <linux/notifier.h>
40 #include <linux/sched.h>
41 #include <linux/wait.h>
42 #include <linux/fs.h>
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45
46 #include "xenbus_comms.h"
47
48 #include <asm/uaccess.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <asm/hypervisor.h>
53
54 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
55 #include <xen/platform-compat.h>
56 #endif
57
58 #include <xen/public/xenbus.h>
59
60 struct xenbus_dev_transaction {
61         struct list_head list;
62         struct xenbus_transaction handle;
63 };
64
65 struct read_buffer {
66         struct list_head list;
67         unsigned int cons;
68         unsigned int len;
69         char msg[];
70 };
71
72 struct xenbus_dev_data {
73         /* In-progress transaction. */
74         struct list_head transactions;
75
76         /* Active watches. */
77         struct list_head watches;
78
79         /* Partial request. */
80         unsigned int len;
81         union {
82                 struct xsd_sockmsg msg;
83                 char buffer[XENSTORE_PAYLOAD_MAX];
84         } u;
85
86         /* Response queue. */
87         struct list_head read_buffers;
88         wait_queue_head_t read_waitq;
89
90         struct mutex reply_mutex;
91 };
92
93 static struct proc_dir_entry *xenbus_dev_intf;
94
95 static ssize_t xenbus_dev_read(struct file *filp,
96                                char __user *ubuf,
97                                size_t len, loff_t *ppos)
98 {
99         struct xenbus_dev_data *u = filp->private_data;
100         struct read_buffer *rb;
101         int i, ret;
102
103         if (!is_xenstored_ready())
104                 return -ENODEV;
105
106         mutex_lock(&u->reply_mutex);
107         while (list_empty(&u->read_buffers)) {
108                 mutex_unlock(&u->reply_mutex);
109                 if (filp->f_flags & O_NONBLOCK)
110                         return -EAGAIN;
111
112                 ret = wait_event_interruptible(u->read_waitq,
113                                                !list_empty(&u->read_buffers));
114                 if (ret)
115                         return ret;
116                 mutex_lock(&u->reply_mutex);
117         }
118
119         rb = list_entry(u->read_buffers.next, struct read_buffer, list);
120         for (i = 0; i < len;) {
121                 put_user(rb->msg[rb->cons], ubuf + i);
122                 i++;
123                 rb->cons++;
124                 if (rb->cons == rb->len) {
125                         list_del(&rb->list);
126                         kfree(rb);
127                         if (list_empty(&u->read_buffers))
128                                 break;
129                         rb = list_entry(u->read_buffers.next,
130                                         struct read_buffer, list);
131                 }
132         }
133         mutex_unlock(&u->reply_mutex);
134
135         return i;
136 }
137
138 static int queue_reply(struct list_head *queue,
139                        const void *data, unsigned int len)
140 {
141         struct read_buffer *rb;
142
143         if (len == 0)
144                 return 0;
145
146         rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
147         if (!rb)
148                 return -ENOMEM;
149
150         rb->cons = 0;
151         rb->len = len;
152
153         memcpy(rb->msg, data, len);
154
155         list_add_tail(&rb->list, queue);
156         return 0;
157 }
158
159 static void queue_flush(struct xenbus_dev_data *u, struct list_head *queue,
160                         int err)
161 {
162         if (!err) {
163                 list_splice_tail(queue, &u->read_buffers);
164                 wake_up(&u->read_waitq);
165         } else
166                 while (!list_empty(queue)) {
167                         struct read_buffer *rb = list_entry(queue->next,
168                                 struct read_buffer, list);
169
170                         list_del(queue->next);
171                         kfree(rb);
172                 }
173 }
174
175 struct watch_adapter
176 {
177         struct list_head list;
178         struct xenbus_watch watch;
179         struct xenbus_dev_data *dev_data;
180         char *token;
181 };
182
183 static void free_watch_adapter (struct watch_adapter *watch)
184 {
185         kfree(watch->watch.node);
186         kfree(watch->token);
187         kfree(watch);
188 }
189
190 static void watch_fired(struct xenbus_watch *watch,
191                         const char **vec,
192                         unsigned int len)
193 {
194         struct watch_adapter *adap =
195             container_of(watch, struct watch_adapter, watch);
196         struct xsd_sockmsg hdr;
197         const char *path, *token;
198         int err, path_len, tok_len, body_len, data_len = 0;
199         LIST_HEAD(queue);
200
201         path = vec[XS_WATCH_PATH];
202         token = adap->token;
203
204         path_len = strlen(path) + 1;
205         tok_len = strlen(token) + 1;
206         if (len > 2)
207                 data_len = vec[len] - vec[2] + 1;
208         body_len = path_len + tok_len + data_len;
209
210         hdr.type = XS_WATCH_EVENT;
211         hdr.len = body_len;
212
213         mutex_lock(&adap->dev_data->reply_mutex);
214         err = queue_reply(&queue, &hdr, sizeof(hdr));
215         if (!err)
216                 err = queue_reply(&queue, path, path_len);
217         if (!err)
218                 err = queue_reply(&queue, token, tok_len);
219         if (!err && len > 2)
220                 err = queue_reply(&queue, vec[2], data_len);
221         queue_flush(adap->dev_data, &queue, err);
222         mutex_unlock(&adap->dev_data->reply_mutex);
223 }
224
225 static LIST_HEAD(watch_list);
226
227 static ssize_t xenbus_dev_write(struct file *filp,
228                                 const char __user *ubuf,
229                                 size_t len, loff_t *ppos)
230 {
231         struct xenbus_dev_data *u = filp->private_data;
232         struct xenbus_dev_transaction *trans = NULL;
233         uint32_t msg_type;
234         void *reply = NULL;
235         LIST_HEAD(queue);
236         char *path, *token;
237         struct watch_adapter *watch;
238         int err, rc = len;
239
240         if (!is_xenstored_ready())
241                 return -ENODEV;
242
243         if ((len + u->len) > sizeof(u->u.buffer)) {
244                 rc = -EINVAL;
245                 goto out;
246         }
247
248         if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
249                 rc = -EFAULT;
250                 goto out;
251         }
252
253         u->len += len;
254         if ((u->len < sizeof(u->u.msg)) ||
255             (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
256                 return rc;
257
258         msg_type = u->u.msg.type;
259
260         switch (msg_type) {
261         case XS_WATCH:
262         case XS_UNWATCH: {
263                 static const char XS_RESP[] = "OK";
264                 struct xsd_sockmsg hdr;
265
266                 path = u->u.buffer + sizeof(u->u.msg);
267                 token = memchr(path, 0, u->u.msg.len);
268                 if (token == NULL) {
269                         rc = -EILSEQ;
270                         goto out;
271                 }
272                 token++;
273                 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
274                         rc = -EILSEQ;
275                         goto out;
276                 }
277
278                 if (msg_type == XS_WATCH) {
279                         watch = kzalloc(sizeof(*watch), GFP_KERNEL);
280                         if (watch == NULL) {
281                                 rc = -ENOMEM;
282                                 goto out;
283                         }
284                         watch->watch.node = kstrdup(path, GFP_KERNEL);
285                         watch->watch.callback = watch_fired;
286                         watch->token = kstrdup(token, GFP_KERNEL);
287                         watch->dev_data = u;
288
289                         err = watch->watch.node && watch->token
290                               ? register_xenbus_watch(&watch->watch) : -ENOMEM;
291                         if (err) {
292                                 free_watch_adapter(watch);
293                                 rc = err;
294                                 goto out;
295                         }
296                         
297                         list_add(&watch->list, &u->watches);
298                 } else {
299                         list_for_each_entry(watch, &u->watches, list) {
300                                 if (!strcmp(watch->token, token) &&
301                                     !strcmp(watch->watch.node, path))
302                                 {
303                                         unregister_xenbus_watch(&watch->watch);
304                                         list_del(&watch->list);
305                                         free_watch_adapter(watch);
306                                         break;
307                                 }
308                         }
309                 }
310
311                 hdr.type = msg_type;
312                 hdr.len = sizeof(XS_RESP);
313                 mutex_lock(&u->reply_mutex);
314                 err = queue_reply(&queue, &hdr, sizeof(hdr))
315                       ?: queue_reply(&queue, XS_RESP, hdr.len);
316                 break;
317         }
318
319         case XS_TRANSACTION_START:
320                 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
321                 if (!trans) {
322                         rc = -ENOMEM;
323                         goto out;
324                 }
325                 goto common;
326
327         case XS_TRANSACTION_END:
328                 list_for_each_entry(trans, &u->transactions, list)
329                         if (trans->handle.id == u->u.msg.tx_id)
330                                 break;
331                 if (&trans->list == &u->transactions) {
332                         rc = -ESRCH;
333                         goto out;
334                 }
335                 /* fall through */
336         common:
337         default:
338                 reply = xenbus_dev_request_and_reply(&u->u.msg);
339                 if (IS_ERR(reply)) {
340                         if (msg_type == XS_TRANSACTION_START)
341                                 kfree(trans);
342                         rc = PTR_ERR(reply);
343                         goto out;
344                 }
345
346                 if (msg_type == XS_TRANSACTION_START) {
347                         trans->handle.id = simple_strtoul(reply, NULL, 0);
348                         list_add(&trans->list, &u->transactions);
349                 } else if (msg_type == XS_TRANSACTION_END) {
350                         list_del(&trans->list);
351                         kfree(trans);
352                 }
353                 mutex_lock(&u->reply_mutex);
354                 err = queue_reply(&queue, &u->u.msg, sizeof(u->u.msg))
355                       ?: queue_reply(&queue, reply, u->u.msg.len);
356                 break;
357         }
358
359         queue_flush(u, &queue, err);
360         mutex_unlock(&u->reply_mutex);
361         kfree(reply);
362         if (err)
363                 rc = err;
364
365  out:
366         u->len = 0;
367         return rc;
368 }
369
370 static int xenbus_dev_open(struct inode *inode, struct file *filp)
371 {
372         struct xenbus_dev_data *u;
373
374         if (xen_store_evtchn == 0)
375                 return -ENOENT;
376
377         nonseekable_open(inode, filp);
378
379         u = kzalloc(sizeof(*u), GFP_KERNEL);
380         if (u == NULL)
381                 return -ENOMEM;
382
383         INIT_LIST_HEAD(&u->transactions);
384         INIT_LIST_HEAD(&u->watches);
385         INIT_LIST_HEAD(&u->read_buffers);
386         init_waitqueue_head(&u->read_waitq);
387
388         mutex_init(&u->reply_mutex);
389
390         filp->private_data = u;
391
392         return 0;
393 }
394
395 static int xenbus_dev_release(struct inode *inode, struct file *filp)
396 {
397         struct xenbus_dev_data *u = filp->private_data;
398         struct xenbus_dev_transaction *trans, *tmp;
399         struct watch_adapter *watch, *tmp_watch;
400         struct read_buffer *rb, *tmp_rb;
401
402         list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
403                 xenbus_transaction_end(trans->handle, 1);
404                 list_del(&trans->list);
405                 kfree(trans);
406         }
407
408         list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
409                 unregister_xenbus_watch(&watch->watch);
410                 list_del(&watch->list);
411                 free_watch_adapter(watch);
412         }
413
414         list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
415                 list_del(&rb->list);
416                 kfree(rb);
417         }
418         kfree(u);
419
420         return 0;
421 }
422
423 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
424 {
425         struct xenbus_dev_data *u = file->private_data;
426
427         if (!is_xenstored_ready())
428                 return -ENODEV;
429
430         poll_wait(file, &u->read_waitq, wait);
431         if (!list_empty(&u->read_buffers))
432                 return POLLIN | POLLRDNORM;
433         return 0;
434 }
435
436 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
437 static long xenbus_dev_ioctl(struct file *file,
438                              unsigned int cmd, unsigned long data)
439 {
440         void __user *udata = (void __user *) data;
441         int ret = -ENOTTY;
442         
443         if (!is_initial_xendomain())
444                 return -ENODEV;
445
446
447         switch (cmd) {
448         case IOCTL_XENBUS_ALLOC: {
449                 xenbus_alloc_t xa;
450                 int old;
451
452                 old = atomic_cmpxchg(&xenbus_xsd_state,
453                                      XENBUS_XSD_UNCOMMITTED,
454                                      XENBUS_XSD_FOREIGN_INIT);
455                 if (old != XENBUS_XSD_UNCOMMITTED)
456                         return -EBUSY;
457
458                 if (copy_from_user(&xa, udata, sizeof(xa))) {
459                         ret = -EFAULT;
460                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
461                         break;
462                 }
463
464                 ret = xenbus_conn(xa.dom, &xa.grant_ref, &xa.port);
465                 if (ret != 0) {
466                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
467                         break;
468                 }
469
470                 if (copy_to_user(udata, &xa, sizeof(xa))) {
471                         ret = -EFAULT;
472                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
473                         break;
474                 }
475         }
476         break;
477
478         default:
479                 break;
480         }
481
482         return ret;
483 }
484 #endif
485
486 static const struct file_operations xenbus_dev_file_ops = {
487         .read = xenbus_dev_read,
488         .write = xenbus_dev_write,
489         .open = xenbus_dev_open,
490         .release = xenbus_dev_release,
491         .llseek = no_llseek,
492         .poll = xenbus_dev_poll,
493 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
494         .unlocked_ioctl = xenbus_dev_ioctl
495 #endif
496 };
497
498 int
499 #ifndef MODULE
500 __init
501 #else
502 __devinit
503 #endif
504 xenbus_dev_init(void)
505 {
506         xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
507         if (xenbus_dev_intf)
508                 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
509
510         return 0;
511 }