pandora: defconfig: update
[pandora-kernel.git] / drivers / xen / xenfs / xenbus.c
1 /*
2  * Driver giving user-space access to the kernel's xenbus connection
3  * to xenstore.
4  *
5  * Copyright (c) 2005, Christian Limpach
6  * Copyright (c) 2005, Rusty Russell, IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  *
32  * Changes:
33  * 2008-10-07  Alex Zeffertt    Replaced /proc/xen/xenbus with xenfs filesystem
34  *                              and /proc/xen compatibility mount point.
35  *                              Turned xenfs into a loadable module.
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/uio.h>
41 #include <linux/notifier.h>
42 #include <linux/wait.h>
43 #include <linux/fs.h>
44 #include <linux/poll.h>
45 #include <linux/mutex.h>
46 #include <linux/sched.h>
47 #include <linux/spinlock.h>
48 #include <linux/mount.h>
49 #include <linux/pagemap.h>
50 #include <linux/uaccess.h>
51 #include <linux/init.h>
52 #include <linux/namei.h>
53 #include <linux/string.h>
54 #include <linux/slab.h>
55
56 #include "xenfs.h"
57 #include "../xenbus/xenbus_comms.h"
58
59 #include <xen/xenbus.h>
60 #include <asm/xen/hypervisor.h>
61
62 /*
63  * An element of a list of outstanding transactions, for which we're
64  * still waiting a reply.
65  */
66 struct xenbus_transaction_holder {
67         struct list_head list;
68         struct xenbus_transaction handle;
69 };
70
71 /*
72  * A buffer of data on the queue.
73  */
74 struct read_buffer {
75         struct list_head list;
76         unsigned int cons;
77         unsigned int len;
78         char msg[];
79 };
80
81 struct xenbus_file_priv {
82         /*
83          * msgbuffer_mutex is held while partial requests are built up
84          * and complete requests are acted on.  It therefore protects
85          * the "transactions" and "watches" lists, and the partial
86          * request length and buffer.
87          *
88          * reply_mutex protects the reply being built up to return to
89          * usermode.  It nests inside msgbuffer_mutex but may be held
90          * alone during a watch callback.
91          */
92         struct mutex msgbuffer_mutex;
93
94         /* In-progress transactions */
95         struct list_head transactions;
96
97         /* Active watches. */
98         struct list_head watches;
99
100         /* Partial request. */
101         unsigned int len;
102         union {
103                 struct xsd_sockmsg msg;
104                 char buffer[PAGE_SIZE];
105         } u;
106
107         /* Response queue. */
108         struct mutex reply_mutex;
109         struct list_head read_buffers;
110         wait_queue_head_t read_waitq;
111
112 };
113
114 /* Read out any raw xenbus messages queued up. */
115 static ssize_t xenbus_file_read(struct file *filp,
116                                char __user *ubuf,
117                                size_t len, loff_t *ppos)
118 {
119         struct xenbus_file_priv *u = filp->private_data;
120         struct read_buffer *rb;
121         unsigned i;
122         int ret;
123
124         mutex_lock(&u->reply_mutex);
125 again:
126         while (list_empty(&u->read_buffers)) {
127                 mutex_unlock(&u->reply_mutex);
128                 if (filp->f_flags & O_NONBLOCK)
129                         return -EAGAIN;
130
131                 ret = wait_event_interruptible(u->read_waitq,
132                                                !list_empty(&u->read_buffers));
133                 if (ret)
134                         return ret;
135                 mutex_lock(&u->reply_mutex);
136         }
137
138         rb = list_entry(u->read_buffers.next, struct read_buffer, list);
139         i = 0;
140         while (i < len) {
141                 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
142
143                 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
144
145                 i += sz - ret;
146                 rb->cons += sz - ret;
147
148                 if (ret != 0) {
149                         if (i == 0)
150                                 i = -EFAULT;
151                         goto out;
152                 }
153
154                 /* Clear out buffer if it has been consumed */
155                 if (rb->cons == rb->len) {
156                         list_del(&rb->list);
157                         kfree(rb);
158                         if (list_empty(&u->read_buffers))
159                                 break;
160                         rb = list_entry(u->read_buffers.next,
161                                         struct read_buffer, list);
162                 }
163         }
164         if (i == 0)
165                 goto again;
166
167 out:
168         mutex_unlock(&u->reply_mutex);
169         return i;
170 }
171
172 /*
173  * Add a buffer to the queue.  Caller must hold the appropriate lock
174  * if the queue is not local.  (Commonly the caller will build up
175  * multiple queued buffers on a temporary local list, and then add it
176  * to the appropriate list under lock once all the buffers have een
177  * successfully allocated.)
178  */
179 static int queue_reply(struct list_head *queue, const void *data, size_t len)
180 {
181         struct read_buffer *rb;
182
183         if (len == 0)
184                 return 0;
185
186         rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
187         if (rb == NULL)
188                 return -ENOMEM;
189
190         rb->cons = 0;
191         rb->len = len;
192
193         memcpy(rb->msg, data, len);
194
195         list_add_tail(&rb->list, queue);
196         return 0;
197 }
198
199 /*
200  * Free all the read_buffer s on a list.
201  * Caller must have sole reference to list.
202  */
203 static void queue_cleanup(struct list_head *list)
204 {
205         struct read_buffer *rb;
206
207         while (!list_empty(list)) {
208                 rb = list_entry(list->next, struct read_buffer, list);
209                 list_del(list->next);
210                 kfree(rb);
211         }
212 }
213
214 struct watch_adapter {
215         struct list_head list;
216         struct xenbus_watch watch;
217         struct xenbus_file_priv *dev_data;
218         char *token;
219 };
220
221 static void free_watch_adapter(struct watch_adapter *watch)
222 {
223         kfree(watch->watch.node);
224         kfree(watch->token);
225         kfree(watch);
226 }
227
228 static struct watch_adapter *alloc_watch_adapter(const char *path,
229                                                  const char *token)
230 {
231         struct watch_adapter *watch;
232
233         watch = kzalloc(sizeof(*watch), GFP_KERNEL);
234         if (watch == NULL)
235                 goto out_fail;
236
237         watch->watch.node = kstrdup(path, GFP_KERNEL);
238         if (watch->watch.node == NULL)
239                 goto out_free;
240
241         watch->token = kstrdup(token, GFP_KERNEL);
242         if (watch->token == NULL)
243                 goto out_free;
244
245         return watch;
246
247 out_free:
248         free_watch_adapter(watch);
249
250 out_fail:
251         return NULL;
252 }
253
254 static void watch_fired(struct xenbus_watch *watch,
255                         const char **vec,
256                         unsigned int len)
257 {
258         struct watch_adapter *adap;
259         struct xsd_sockmsg hdr;
260         const char *path, *token;
261         int path_len, tok_len, body_len, data_len = 0;
262         int ret;
263         LIST_HEAD(staging_q);
264
265         adap = container_of(watch, struct watch_adapter, watch);
266
267         path = vec[XS_WATCH_PATH];
268         token = adap->token;
269
270         path_len = strlen(path) + 1;
271         tok_len = strlen(token) + 1;
272         if (len > 2)
273                 data_len = vec[len] - vec[2] + 1;
274         body_len = path_len + tok_len + data_len;
275
276         hdr.type = XS_WATCH_EVENT;
277         hdr.len = body_len;
278
279         mutex_lock(&adap->dev_data->reply_mutex);
280
281         ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
282         if (!ret)
283                 ret = queue_reply(&staging_q, path, path_len);
284         if (!ret)
285                 ret = queue_reply(&staging_q, token, tok_len);
286         if (!ret && len > 2)
287                 ret = queue_reply(&staging_q, vec[2], data_len);
288
289         if (!ret) {
290                 /* success: pass reply list onto watcher */
291                 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
292                 wake_up(&adap->dev_data->read_waitq);
293         } else
294                 queue_cleanup(&staging_q);
295
296         mutex_unlock(&adap->dev_data->reply_mutex);
297 }
298
299 static int xenbus_write_transaction(unsigned msg_type,
300                                     struct xenbus_file_priv *u)
301 {
302         int rc;
303         void *reply;
304         struct xenbus_transaction_holder *trans = NULL;
305         LIST_HEAD(staging_q);
306
307         if (msg_type == XS_TRANSACTION_START) {
308                 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
309                 if (!trans) {
310                         rc = -ENOMEM;
311                         goto out;
312                 }
313         } else if (msg_type == XS_TRANSACTION_END) {
314                 list_for_each_entry(trans, &u->transactions, list)
315                         if (trans->handle.id == u->u.msg.tx_id)
316                                 break;
317                 if (&trans->list == &u->transactions)
318                         return -ESRCH;
319         }
320
321         reply = xenbus_dev_request_and_reply(&u->u.msg);
322         if (IS_ERR(reply)) {
323                 if (msg_type == XS_TRANSACTION_START)
324                         kfree(trans);
325                 rc = PTR_ERR(reply);
326                 goto out;
327         }
328
329         if (msg_type == XS_TRANSACTION_START) {
330                 trans->handle.id = simple_strtoul(reply, NULL, 0);
331
332                 list_add(&trans->list, &u->transactions);
333         } else if (msg_type == XS_TRANSACTION_END) {
334                 list_del(&trans->list);
335                 kfree(trans);
336         }
337
338         mutex_lock(&u->reply_mutex);
339         rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
340         if (!rc)
341                 rc = queue_reply(&staging_q, reply, u->u.msg.len);
342         if (!rc) {
343                 list_splice_tail(&staging_q, &u->read_buffers);
344                 wake_up(&u->read_waitq);
345         } else {
346                 queue_cleanup(&staging_q);
347         }
348         mutex_unlock(&u->reply_mutex);
349
350         kfree(reply);
351
352 out:
353         return rc;
354 }
355
356 static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
357 {
358         struct watch_adapter *watch, *tmp_watch;
359         char *path, *token;
360         int err, rc;
361         LIST_HEAD(staging_q);
362
363         path = u->u.buffer + sizeof(u->u.msg);
364         token = memchr(path, 0, u->u.msg.len);
365         if (token == NULL) {
366                 rc = -EILSEQ;
367                 goto out;
368         }
369         token++;
370
371         if (msg_type == XS_WATCH) {
372                 watch = alloc_watch_adapter(path, token);
373                 if (watch == NULL) {
374                         rc = -ENOMEM;
375                         goto out;
376                 }
377
378                 watch->watch.callback = watch_fired;
379                 watch->dev_data = u;
380
381                 err = register_xenbus_watch(&watch->watch);
382                 if (err) {
383                         free_watch_adapter(watch);
384                         rc = err;
385                         goto out;
386                 }
387                 list_add(&watch->list, &u->watches);
388         } else {
389                 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
390                         if (!strcmp(watch->token, token) &&
391                             !strcmp(watch->watch.node, path)) {
392                                 unregister_xenbus_watch(&watch->watch);
393                                 list_del(&watch->list);
394                                 free_watch_adapter(watch);
395                                 break;
396                         }
397                 }
398         }
399
400         /* Success.  Synthesize a reply to say all is OK. */
401         {
402                 struct {
403                         struct xsd_sockmsg hdr;
404                         char body[3];
405                 } __packed reply = {
406                         {
407                                 .type = msg_type,
408                                 .len = sizeof(reply.body)
409                         },
410                         "OK"
411                 };
412
413                 mutex_lock(&u->reply_mutex);
414                 rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
415                 wake_up(&u->read_waitq);
416                 mutex_unlock(&u->reply_mutex);
417         }
418
419 out:
420         return rc;
421 }
422
423 static ssize_t xenbus_file_write(struct file *filp,
424                                 const char __user *ubuf,
425                                 size_t len, loff_t *ppos)
426 {
427         struct xenbus_file_priv *u = filp->private_data;
428         uint32_t msg_type;
429         int rc = len;
430         int ret;
431         LIST_HEAD(staging_q);
432
433         /*
434          * We're expecting usermode to be writing properly formed
435          * xenbus messages.  If they write an incomplete message we
436          * buffer it up.  Once it is complete, we act on it.
437          */
438
439         /*
440          * Make sure concurrent writers can't stomp all over each
441          * other's messages and make a mess of our partial message
442          * buffer.  We don't make any attemppt to stop multiple
443          * writers from making a mess of each other's incomplete
444          * messages; we're just trying to guarantee our own internal
445          * consistency and make sure that single writes are handled
446          * atomically.
447          */
448         mutex_lock(&u->msgbuffer_mutex);
449
450         /* Get this out of the way early to avoid confusion */
451         if (len == 0)
452                 goto out;
453
454         /* Can't write a xenbus message larger we can buffer */
455         if ((len + u->len) > sizeof(u->u.buffer)) {
456                 /* On error, dump existing buffer */
457                 u->len = 0;
458                 rc = -EINVAL;
459                 goto out;
460         }
461
462         ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
463
464         if (ret != 0) {
465                 rc = -EFAULT;
466                 goto out;
467         }
468
469         /* Deal with a partial copy. */
470         len -= ret;
471         rc = len;
472
473         u->len += len;
474
475         /* Return if we haven't got a full message yet */
476         if (u->len < sizeof(u->u.msg))
477                 goto out;       /* not even the header yet */
478
479         /* If we're expecting a message that's larger than we can
480            possibly send, dump what we have and return an error. */
481         if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
482                 rc = -E2BIG;
483                 u->len = 0;
484                 goto out;
485         }
486
487         if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
488                 goto out;       /* incomplete data portion */
489
490         /*
491          * OK, now we have a complete message.  Do something with it.
492          */
493
494         msg_type = u->u.msg.type;
495
496         switch (msg_type) {
497         case XS_WATCH:
498         case XS_UNWATCH:
499                 /* (Un)Ask for some path to be watched for changes */
500                 ret = xenbus_write_watch(msg_type, u);
501                 break;
502
503         default:
504                 /* Send out a transaction */
505                 ret = xenbus_write_transaction(msg_type, u);
506                 break;
507         }
508         if (ret != 0)
509                 rc = ret;
510
511         /* Buffered message consumed */
512         u->len = 0;
513
514  out:
515         mutex_unlock(&u->msgbuffer_mutex);
516         return rc;
517 }
518
519 static int xenbus_file_open(struct inode *inode, struct file *filp)
520 {
521         struct xenbus_file_priv *u;
522
523         if (xen_store_evtchn == 0)
524                 return -ENOENT;
525
526         nonseekable_open(inode, filp);
527
528         u = kzalloc(sizeof(*u), GFP_KERNEL);
529         if (u == NULL)
530                 return -ENOMEM;
531
532         INIT_LIST_HEAD(&u->transactions);
533         INIT_LIST_HEAD(&u->watches);
534         INIT_LIST_HEAD(&u->read_buffers);
535         init_waitqueue_head(&u->read_waitq);
536
537         mutex_init(&u->reply_mutex);
538         mutex_init(&u->msgbuffer_mutex);
539
540         filp->private_data = u;
541
542         return 0;
543 }
544
545 static int xenbus_file_release(struct inode *inode, struct file *filp)
546 {
547         struct xenbus_file_priv *u = filp->private_data;
548         struct xenbus_transaction_holder *trans, *tmp;
549         struct watch_adapter *watch, *tmp_watch;
550         struct read_buffer *rb, *tmp_rb;
551
552         /*
553          * No need for locking here because there are no other users,
554          * by definition.
555          */
556
557         list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
558                 xenbus_transaction_end(trans->handle, 1);
559                 list_del(&trans->list);
560                 kfree(trans);
561         }
562
563         list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
564                 unregister_xenbus_watch(&watch->watch);
565                 list_del(&watch->list);
566                 free_watch_adapter(watch);
567         }
568
569         list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
570                 list_del(&rb->list);
571                 kfree(rb);
572         }
573         kfree(u);
574
575         return 0;
576 }
577
578 static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
579 {
580         struct xenbus_file_priv *u = file->private_data;
581
582         poll_wait(file, &u->read_waitq, wait);
583         if (!list_empty(&u->read_buffers))
584                 return POLLIN | POLLRDNORM;
585         return 0;
586 }
587
588 const struct file_operations xenbus_file_ops = {
589         .read = xenbus_file_read,
590         .write = xenbus_file_write,
591         .open = xenbus_file_open,
592         .release = xenbus_file_release,
593         .poll = xenbus_file_poll,
594         .llseek = no_llseek,
595 };