Added patch headers.
[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[PAGE_SIZE];
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                 ret = wait_event_interruptible(u->read_waitq,
110                                                !list_empty(&u->read_buffers));
111                 if (ret)
112                         return ret;
113                 mutex_lock(&u->reply_mutex);
114         }
115
116         rb = list_entry(u->read_buffers.next, struct read_buffer, list);
117         for (i = 0; i < len;) {
118                 put_user(rb->msg[rb->cons], ubuf + i);
119                 i++;
120                 rb->cons++;
121                 if (rb->cons == rb->len) {
122                         list_del(&rb->list);
123                         kfree(rb);
124                         if (list_empty(&u->read_buffers))
125                                 break;
126                         rb = list_entry(u->read_buffers.next,
127                                         struct read_buffer, list);
128                 }
129         }
130         mutex_unlock(&u->reply_mutex);
131
132         return i;
133 }
134
135 static void queue_reply(struct xenbus_dev_data *u,
136                         char *data, unsigned int len)
137 {
138         struct read_buffer *rb;
139
140         if (len == 0)
141                 return;
142
143         rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
144         BUG_ON(rb == NULL);
145
146         rb->cons = 0;
147         rb->len = len;
148
149         memcpy(rb->msg, data, len);
150
151         list_add_tail(&rb->list, &u->read_buffers);
152
153         wake_up(&u->read_waitq);
154 }
155
156 struct watch_adapter
157 {
158         struct list_head list;
159         struct xenbus_watch watch;
160         struct xenbus_dev_data *dev_data;
161         char *token;
162 };
163
164 static void free_watch_adapter (struct watch_adapter *watch)
165 {
166         kfree(watch->watch.node);
167         kfree(watch->token);
168         kfree(watch);
169 }
170
171 static void watch_fired(struct xenbus_watch *watch,
172                         const char **vec,
173                         unsigned int len)
174 {
175         struct watch_adapter *adap =
176             container_of(watch, struct watch_adapter, watch);
177         struct xsd_sockmsg hdr;
178         const char *path, *token;
179         int path_len, tok_len, body_len, data_len = 0;
180
181         path = vec[XS_WATCH_PATH];
182         token = adap->token;
183
184         path_len = strlen(path) + 1;
185         tok_len = strlen(token) + 1;
186         if (len > 2)
187                 data_len = vec[len] - vec[2] + 1;
188         body_len = path_len + tok_len + data_len;
189
190         hdr.type = XS_WATCH_EVENT;
191         hdr.len = body_len;
192
193         mutex_lock(&adap->dev_data->reply_mutex);
194         queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
195         queue_reply(adap->dev_data, (char *)path, path_len);
196         queue_reply(adap->dev_data, (char *)token, tok_len);
197         if (len > 2)
198                 queue_reply(adap->dev_data, (char *)vec[2], data_len);
199         mutex_unlock(&adap->dev_data->reply_mutex);
200 }
201
202 static LIST_HEAD(watch_list);
203
204 static ssize_t xenbus_dev_write(struct file *filp,
205                                 const char __user *ubuf,
206                                 size_t len, loff_t *ppos)
207 {
208         struct xenbus_dev_data *u = filp->private_data;
209         struct xenbus_dev_transaction *trans = NULL;
210         uint32_t msg_type;
211         void *reply;
212         char *path, *token;
213         struct watch_adapter *watch, *tmp_watch;
214         int err, rc = len;
215
216         if (!is_xenstored_ready())
217                 return -ENODEV;
218
219         if ((len + u->len) > sizeof(u->u.buffer)) {
220                 rc = -EINVAL;
221                 goto out;
222         }
223
224         if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
225                 rc = -EFAULT;
226                 goto out;
227         }
228
229         u->len += len;
230         if ((u->len < sizeof(u->u.msg)) ||
231             (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
232                 return rc;
233
234         msg_type = u->u.msg.type;
235
236         switch (msg_type) {
237         case XS_WATCH:
238         case XS_UNWATCH: {
239                 static const char *XS_RESP = "OK";
240                 struct xsd_sockmsg hdr;
241
242                 path = u->u.buffer + sizeof(u->u.msg);
243                 token = memchr(path, 0, u->u.msg.len);
244                 if (token == NULL) {
245                         rc = -EILSEQ;
246                         goto out;
247                 }
248                 token++;
249
250                 if (msg_type == XS_WATCH) {
251                         watch = kzalloc(sizeof(*watch), GFP_KERNEL);
252                         watch->watch.node = kmalloc(strlen(path)+1,
253                                                     GFP_KERNEL);
254                         strcpy((char *)watch->watch.node, path);
255                         watch->watch.callback = watch_fired;
256                         watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
257                         strcpy(watch->token, token);
258                         watch->dev_data = u;
259
260                         err = register_xenbus_watch(&watch->watch);
261                         if (err) {
262                                 free_watch_adapter(watch);
263                                 rc = err;
264                                 goto out;
265                         }
266                         
267                         list_add(&watch->list, &u->watches);
268                 } else {
269                         list_for_each_entry_safe(watch, tmp_watch,
270                                                  &u->watches, list) {
271                                 if (!strcmp(watch->token, token) &&
272                                     !strcmp(watch->watch.node, path))
273                                 {
274                                         unregister_xenbus_watch(&watch->watch);
275                                         list_del(&watch->list);
276                                         free_watch_adapter(watch);
277                                         break;
278                                 }
279                         }
280                 }
281
282                 hdr.type = msg_type;
283                 hdr.len = strlen(XS_RESP) + 1;
284                 mutex_lock(&u->reply_mutex);
285                 queue_reply(u, (char *)&hdr, sizeof(hdr));
286                 queue_reply(u, (char *)XS_RESP, hdr.len);
287                 mutex_unlock(&u->reply_mutex);
288                 break;
289         }
290
291         default:
292                 if (msg_type == XS_TRANSACTION_START) {
293                         trans = kmalloc(sizeof(*trans), GFP_KERNEL);
294                         if (!trans) {
295                                 rc = -ENOMEM;
296                                 goto out;
297                         }
298                 }
299
300                 reply = xenbus_dev_request_and_reply(&u->u.msg);
301                 if (IS_ERR(reply)) {
302                         kfree(trans);
303                         rc = PTR_ERR(reply);
304                         goto out;
305                 }
306
307                 if (msg_type == XS_TRANSACTION_START) {
308                         trans->handle.id = simple_strtoul(reply, NULL, 0);
309                         list_add(&trans->list, &u->transactions);
310                 } else if (msg_type == XS_TRANSACTION_END) {
311                         list_for_each_entry(trans, &u->transactions, list)
312                                 if (trans->handle.id == u->u.msg.tx_id)
313                                         break;
314                         BUG_ON(&trans->list == &u->transactions);
315                         list_del(&trans->list);
316                         kfree(trans);
317                 }
318                 mutex_lock(&u->reply_mutex);
319                 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
320                 queue_reply(u, (char *)reply, u->u.msg.len);
321                 mutex_unlock(&u->reply_mutex);
322                 kfree(reply);
323                 break;
324         }
325
326  out:
327         u->len = 0;
328         return rc;
329 }
330
331 static int xenbus_dev_open(struct inode *inode, struct file *filp)
332 {
333         struct xenbus_dev_data *u;
334
335         if (xen_store_evtchn == 0)
336                 return -ENOENT;
337
338         nonseekable_open(inode, filp);
339
340         u = kzalloc(sizeof(*u), GFP_KERNEL);
341         if (u == NULL)
342                 return -ENOMEM;
343
344         INIT_LIST_HEAD(&u->transactions);
345         INIT_LIST_HEAD(&u->watches);
346         INIT_LIST_HEAD(&u->read_buffers);
347         init_waitqueue_head(&u->read_waitq);
348
349         mutex_init(&u->reply_mutex);
350
351         filp->private_data = u;
352
353         return 0;
354 }
355
356 static int xenbus_dev_release(struct inode *inode, struct file *filp)
357 {
358         struct xenbus_dev_data *u = filp->private_data;
359         struct xenbus_dev_transaction *trans, *tmp;
360         struct watch_adapter *watch, *tmp_watch;
361
362         list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
363                 xenbus_transaction_end(trans->handle, 1);
364                 list_del(&trans->list);
365                 kfree(trans);
366         }
367
368         list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
369                 unregister_xenbus_watch(&watch->watch);
370                 list_del(&watch->list);
371                 free_watch_adapter(watch);
372         }
373
374         kfree(u);
375
376         return 0;
377 }
378
379 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
380 {
381         struct xenbus_dev_data *u = file->private_data;
382
383         if (!is_xenstored_ready())
384                 return -ENODEV;
385
386         poll_wait(file, &u->read_waitq, wait);
387         if (!list_empty(&u->read_buffers))
388                 return POLLIN | POLLRDNORM;
389         return 0;
390 }
391
392 #ifdef HAVE_UNLOCKED_IOCTL
393 static long xenbus_dev_ioctl(struct file *file,
394                              unsigned int cmd, unsigned long data)
395 {
396         extern int xenbus_conn(domid_t remote_dom, int *grant_ref,
397                                evtchn_port_t *local_port);
398         void __user *udata = (void __user *) data;
399         int ret = -ENOTTY;
400         
401         if (!is_initial_xendomain())
402                 return -ENODEV;
403
404
405         switch (cmd) {
406         case IOCTL_XENBUS_ALLOC: {
407                 xenbus_alloc_t xa;
408                 int old;
409
410                 old = atomic_cmpxchg(&xenbus_xsd_state,
411                                      XENBUS_XSD_UNCOMMITTED,
412                                      XENBUS_XSD_FOREIGN_INIT);
413                 if (old != XENBUS_XSD_UNCOMMITTED)
414                         return -EBUSY;
415
416                 if (copy_from_user(&xa, udata, sizeof(xa))) {
417                         ret = -EFAULT;
418                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
419                         break;
420                 }
421
422                 ret = xenbus_conn(xa.dom, &xa.grant_ref, &xa.port);
423                 if (ret != 0) {
424                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
425                         break;
426                 }
427
428                 if (copy_to_user(udata, &xa, sizeof(xa))) {
429                         ret = -EFAULT;
430                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_UNCOMMITTED);
431                         break;
432                 }
433         }
434         break;
435
436         default:
437                 break;
438         }
439
440         return ret;
441 }
442 #endif
443
444 static const struct file_operations xenbus_dev_file_ops = {
445         .read = xenbus_dev_read,
446         .write = xenbus_dev_write,
447         .open = xenbus_dev_open,
448         .release = xenbus_dev_release,
449         .poll = xenbus_dev_poll,
450 #ifdef HAVE_UNLOCKED_IOCTL
451         .unlocked_ioctl = xenbus_dev_ioctl
452 #endif
453 };
454
455 int xenbus_dev_init(void)
456 {
457         xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
458         if (xenbus_dev_intf)
459                 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
460
461         return 0;
462 }