]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/xen/xenbus/xenbus_xs.c
4c49d870976577652ca2554cbdf1a6e21c339580
[karo-tx-linux.git] / drivers / xen / xenbus / xenbus_xs.c
1 /******************************************************************************
2  * xenbus_xs.c
3  *
4  * This is the kernel equivalent of the "xs" library.  We don't need everything
5  * and we use xenbus_comms for communication.
6  *
7  * Copyright (C) 2005 Rusty Russell, IBM Corporation
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation; or, when distributed
12  * separately from the Linux kernel or incorporated into other
13  * software packages, subject to the following license:
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this source file (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use, copy, modify,
18  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19  * and to permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31  * IN THE SOFTWARE.
32  */
33
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36 #include <linux/unistd.h>
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/uio.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/err.h>
43 #include <linux/slab.h>
44 #include <linux/fcntl.h>
45 #include <linux/kthread.h>
46 #include <linux/rwsem.h>
47 #include <linux/mutex.h>
48 #include <asm/xen/hypervisor.h>
49 #include <xen/xenbus.h>
50 #include <xen/xen.h>
51 #include "xenbus.h"
52
53 struct xs_stored_msg {
54         struct list_head list;
55
56         struct xsd_sockmsg hdr;
57
58         union {
59                 /* Queued replies. */
60                 struct {
61                         char *body;
62                 } reply;
63
64                 /* Queued watch events. */
65                 struct {
66                         struct xenbus_watch *handle;
67                         char **vec;
68                         unsigned int vec_size;
69                 } watch;
70         } u;
71 };
72
73 struct xs_handle {
74         /* A list of replies. Currently only one will ever be outstanding. */
75         struct list_head reply_list;
76         spinlock_t reply_lock;
77         wait_queue_head_t reply_waitq;
78
79         /*
80          * Mutex ordering: transaction_mutex -> watch_mutex -> request_mutex.
81          * response_mutex is never taken simultaneously with the other three.
82          *
83          * transaction_mutex must be held before incrementing
84          * transaction_count. The mutex is held when a suspend is in
85          * progress to prevent new transactions starting.
86          *
87          * When decrementing transaction_count to zero the wait queue
88          * should be woken up, the suspend code waits for count to
89          * reach zero.
90          */
91
92         /* One request at a time. */
93         struct mutex request_mutex;
94
95         /* Protect xenbus reader thread against save/restore. */
96         struct mutex response_mutex;
97
98         /* Protect transactions against save/restore. */
99         struct mutex transaction_mutex;
100         atomic_t transaction_count;
101         wait_queue_head_t transaction_wq;
102
103         /* Protect watch (de)register against save/restore. */
104         struct rw_semaphore watch_mutex;
105 };
106
107 static struct xs_handle xs_state;
108
109 /* List of registered watches, and a lock to protect it. */
110 static LIST_HEAD(watches);
111 static DEFINE_SPINLOCK(watches_lock);
112
113 /* List of pending watch callback events, and a lock to protect it. */
114 static LIST_HEAD(watch_events);
115 static DEFINE_SPINLOCK(watch_events_lock);
116
117 /*
118  * Details of the xenwatch callback kernel thread. The thread waits on the
119  * watch_events_waitq for work to do (queued on watch_events list). When it
120  * wakes up it acquires the xenwatch_mutex before reading the list and
121  * carrying out work.
122  */
123 static pid_t xenwatch_pid;
124 static DEFINE_MUTEX(xenwatch_mutex);
125 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
126
127 static int get_error(const char *errorstring)
128 {
129         unsigned int i;
130
131         for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
132                 if (i == ARRAY_SIZE(xsd_errors) - 1) {
133                         pr_warn("xen store gave: unknown error %s\n",
134                                 errorstring);
135                         return EINVAL;
136                 }
137         }
138         return xsd_errors[i].errnum;
139 }
140
141 static bool xenbus_ok(void)
142 {
143         switch (xen_store_domain_type) {
144         case XS_LOCAL:
145                 switch (system_state) {
146                 case SYSTEM_POWER_OFF:
147                 case SYSTEM_RESTART:
148                 case SYSTEM_HALT:
149                         return false;
150                 default:
151                         break;
152                 }
153                 return true;
154         case XS_PV:
155         case XS_HVM:
156                 /* FIXME: Could check that the remote domain is alive,
157                  * but it is normally initial domain. */
158                 return true;
159         default:
160                 break;
161         }
162         return false;
163 }
164 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
165 {
166         struct xs_stored_msg *msg;
167         char *body;
168
169         spin_lock(&xs_state.reply_lock);
170
171         while (list_empty(&xs_state.reply_list)) {
172                 spin_unlock(&xs_state.reply_lock);
173                 if (xenbus_ok())
174                         /* XXX FIXME: Avoid synchronous wait for response here. */
175                         wait_event_timeout(xs_state.reply_waitq,
176                                            !list_empty(&xs_state.reply_list),
177                                            msecs_to_jiffies(500));
178                 else {
179                         /*
180                          * If we are in the process of being shut-down there is
181                          * no point of trying to contact XenBus - it is either
182                          * killed (xenstored application) or the other domain
183                          * has been killed or is unreachable.
184                          */
185                         return ERR_PTR(-EIO);
186                 }
187                 spin_lock(&xs_state.reply_lock);
188         }
189
190         msg = list_entry(xs_state.reply_list.next,
191                          struct xs_stored_msg, list);
192         list_del(&msg->list);
193
194         spin_unlock(&xs_state.reply_lock);
195
196         *type = msg->hdr.type;
197         if (len)
198                 *len = msg->hdr.len;
199         body = msg->u.reply.body;
200
201         kfree(msg);
202
203         return body;
204 }
205
206 static void transaction_start(void)
207 {
208         mutex_lock(&xs_state.transaction_mutex);
209         atomic_inc(&xs_state.transaction_count);
210         mutex_unlock(&xs_state.transaction_mutex);
211 }
212
213 static void transaction_end(void)
214 {
215         if (atomic_dec_and_test(&xs_state.transaction_count))
216                 wake_up(&xs_state.transaction_wq);
217 }
218
219 static void transaction_suspend(void)
220 {
221         mutex_lock(&xs_state.transaction_mutex);
222         wait_event(xs_state.transaction_wq,
223                    atomic_read(&xs_state.transaction_count) == 0);
224 }
225
226 static void transaction_resume(void)
227 {
228         mutex_unlock(&xs_state.transaction_mutex);
229 }
230
231 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
232 {
233         void *ret;
234         enum xsd_sockmsg_type type = msg->type;
235         int err;
236
237         if (type == XS_TRANSACTION_START)
238                 transaction_start();
239
240         mutex_lock(&xs_state.request_mutex);
241
242         err = xb_write(msg, sizeof(*msg) + msg->len);
243         if (err) {
244                 msg->type = XS_ERROR;
245                 ret = ERR_PTR(err);
246         } else
247                 ret = read_reply(&msg->type, &msg->len);
248
249         mutex_unlock(&xs_state.request_mutex);
250
251         if ((msg->type == XS_TRANSACTION_END) ||
252             ((type == XS_TRANSACTION_START) && (msg->type == XS_ERROR)))
253                 transaction_end();
254
255         return ret;
256 }
257 EXPORT_SYMBOL(xenbus_dev_request_and_reply);
258
259 /* Send message to xs, get kmalloc'ed reply.  ERR_PTR() on error. */
260 static void *xs_talkv(struct xenbus_transaction t,
261                       enum xsd_sockmsg_type type,
262                       const struct kvec *iovec,
263                       unsigned int num_vecs,
264                       unsigned int *len)
265 {
266         struct xsd_sockmsg msg;
267         void *ret = NULL;
268         unsigned int i;
269         int err;
270
271         msg.tx_id = t.id;
272         msg.req_id = 0;
273         msg.type = type;
274         msg.len = 0;
275         for (i = 0; i < num_vecs; i++)
276                 msg.len += iovec[i].iov_len;
277
278         mutex_lock(&xs_state.request_mutex);
279
280         err = xb_write(&msg, sizeof(msg));
281         if (err) {
282                 mutex_unlock(&xs_state.request_mutex);
283                 return ERR_PTR(err);
284         }
285
286         for (i = 0; i < num_vecs; i++) {
287                 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);
288                 if (err) {
289                         mutex_unlock(&xs_state.request_mutex);
290                         return ERR_PTR(err);
291                 }
292         }
293
294         ret = read_reply(&msg.type, len);
295
296         mutex_unlock(&xs_state.request_mutex);
297
298         if (IS_ERR(ret))
299                 return ret;
300
301         if (msg.type == XS_ERROR) {
302                 err = get_error(ret);
303                 kfree(ret);
304                 return ERR_PTR(-err);
305         }
306
307         if (msg.type != type) {
308                 pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
309                                     msg.type, type);
310                 kfree(ret);
311                 return ERR_PTR(-EINVAL);
312         }
313         return ret;
314 }
315
316 /* Simplified version of xs_talkv: single message. */
317 static void *xs_single(struct xenbus_transaction t,
318                        enum xsd_sockmsg_type type,
319                        const char *string,
320                        unsigned int *len)
321 {
322         struct kvec iovec;
323
324         iovec.iov_base = (void *)string;
325         iovec.iov_len = strlen(string) + 1;
326         return xs_talkv(t, type, &iovec, 1, len);
327 }
328
329 /* Many commands only need an ack, don't care what it says. */
330 static int xs_error(char *reply)
331 {
332         if (IS_ERR(reply))
333                 return PTR_ERR(reply);
334         kfree(reply);
335         return 0;
336 }
337
338 static unsigned int count_strings(const char *strings, unsigned int len)
339 {
340         unsigned int num;
341         const char *p;
342
343         for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
344                 num++;
345
346         return num;
347 }
348
349 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
350 static char *join(const char *dir, const char *name)
351 {
352         char *buffer;
353
354         if (strlen(name) == 0)
355                 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
356         else
357                 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
358         return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
359 }
360
361 static char **split(char *strings, unsigned int len, unsigned int *num)
362 {
363         char *p, **ret;
364
365         /* Count the strings. */
366         *num = count_strings(strings, len);
367
368         /* Transfer to one big alloc for easy freeing. */
369         ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
370         if (!ret) {
371                 kfree(strings);
372                 return ERR_PTR(-ENOMEM);
373         }
374         memcpy(&ret[*num], strings, len);
375         kfree(strings);
376
377         strings = (char *)&ret[*num];
378         for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
379                 ret[(*num)++] = p;
380
381         return ret;
382 }
383
384 char **xenbus_directory(struct xenbus_transaction t,
385                         const char *dir, const char *node, unsigned int *num)
386 {
387         char *strings, *path;
388         unsigned int len;
389
390         path = join(dir, node);
391         if (IS_ERR(path))
392                 return (char **)path;
393
394         strings = xs_single(t, XS_DIRECTORY, path, &len);
395         kfree(path);
396         if (IS_ERR(strings))
397                 return (char **)strings;
398
399         return split(strings, len, num);
400 }
401 EXPORT_SYMBOL_GPL(xenbus_directory);
402
403 /* Check if a path exists. Return 1 if it does. */
404 int xenbus_exists(struct xenbus_transaction t,
405                   const char *dir, const char *node)
406 {
407         char **d;
408         int dir_n;
409
410         d = xenbus_directory(t, dir, node, &dir_n);
411         if (IS_ERR(d))
412                 return 0;
413         kfree(d);
414         return 1;
415 }
416 EXPORT_SYMBOL_GPL(xenbus_exists);
417
418 /* Get the value of a single file.
419  * Returns a kmalloced value: call free() on it after use.
420  * len indicates length in bytes.
421  */
422 void *xenbus_read(struct xenbus_transaction t,
423                   const char *dir, const char *node, unsigned int *len)
424 {
425         char *path;
426         void *ret;
427
428         path = join(dir, node);
429         if (IS_ERR(path))
430                 return (void *)path;
431
432         ret = xs_single(t, XS_READ, path, len);
433         kfree(path);
434         return ret;
435 }
436 EXPORT_SYMBOL_GPL(xenbus_read);
437
438 /* Write the value of a single file.
439  * Returns -err on failure.
440  */
441 int xenbus_write(struct xenbus_transaction t,
442                  const char *dir, const char *node, const char *string)
443 {
444         const char *path;
445         struct kvec iovec[2];
446         int ret;
447
448         path = join(dir, node);
449         if (IS_ERR(path))
450                 return PTR_ERR(path);
451
452         iovec[0].iov_base = (void *)path;
453         iovec[0].iov_len = strlen(path) + 1;
454         iovec[1].iov_base = (void *)string;
455         iovec[1].iov_len = strlen(string);
456
457         ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
458         kfree(path);
459         return ret;
460 }
461 EXPORT_SYMBOL_GPL(xenbus_write);
462
463 /* Create a new directory. */
464 int xenbus_mkdir(struct xenbus_transaction t,
465                  const char *dir, const char *node)
466 {
467         char *path;
468         int ret;
469
470         path = join(dir, node);
471         if (IS_ERR(path))
472                 return PTR_ERR(path);
473
474         ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
475         kfree(path);
476         return ret;
477 }
478 EXPORT_SYMBOL_GPL(xenbus_mkdir);
479
480 /* Destroy a file or directory (directories must be empty). */
481 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
482 {
483         char *path;
484         int ret;
485
486         path = join(dir, node);
487         if (IS_ERR(path))
488                 return PTR_ERR(path);
489
490         ret = xs_error(xs_single(t, XS_RM, path, NULL));
491         kfree(path);
492         return ret;
493 }
494 EXPORT_SYMBOL_GPL(xenbus_rm);
495
496 /* Start a transaction: changes by others will not be seen during this
497  * transaction, and changes will not be visible to others until end.
498  */
499 int xenbus_transaction_start(struct xenbus_transaction *t)
500 {
501         char *id_str;
502
503         transaction_start();
504
505         id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
506         if (IS_ERR(id_str)) {
507                 transaction_end();
508                 return PTR_ERR(id_str);
509         }
510
511         t->id = simple_strtoul(id_str, NULL, 0);
512         kfree(id_str);
513         return 0;
514 }
515 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
516
517 /* End a transaction.
518  * If abandon is true, transaction is discarded instead of committed.
519  */
520 int xenbus_transaction_end(struct xenbus_transaction t, int abort)
521 {
522         char abortstr[2];
523         int err;
524
525         if (abort)
526                 strcpy(abortstr, "F");
527         else
528                 strcpy(abortstr, "T");
529
530         err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
531
532         transaction_end();
533
534         return err;
535 }
536 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
537
538 /* Single read and scanf: returns -errno or num scanned. */
539 int xenbus_scanf(struct xenbus_transaction t,
540                  const char *dir, const char *node, const char *fmt, ...)
541 {
542         va_list ap;
543         int ret;
544         char *val;
545
546         val = xenbus_read(t, dir, node, NULL);
547         if (IS_ERR(val))
548                 return PTR_ERR(val);
549
550         va_start(ap, fmt);
551         ret = vsscanf(val, fmt, ap);
552         va_end(ap);
553         kfree(val);
554         /* Distinctive errno. */
555         if (ret == 0)
556                 return -ERANGE;
557         return ret;
558 }
559 EXPORT_SYMBOL_GPL(xenbus_scanf);
560
561 /* Read an (optional) unsigned value. */
562 unsigned int xenbus_read_unsigned(const char *dir, const char *node,
563                                   unsigned int default_val)
564 {
565         unsigned int val;
566         int ret;
567
568         ret = xenbus_scanf(XBT_NIL, dir, node, "%u", &val);
569         if (ret <= 0)
570                 val = default_val;
571
572         return val;
573 }
574 EXPORT_SYMBOL_GPL(xenbus_read_unsigned);
575
576 /* Single printf and write: returns -errno or 0. */
577 int xenbus_printf(struct xenbus_transaction t,
578                   const char *dir, const char *node, const char *fmt, ...)
579 {
580         va_list ap;
581         int ret;
582         char *buf;
583
584         va_start(ap, fmt);
585         buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
586         va_end(ap);
587
588         if (!buf)
589                 return -ENOMEM;
590
591         ret = xenbus_write(t, dir, node, buf);
592
593         kfree(buf);
594
595         return ret;
596 }
597 EXPORT_SYMBOL_GPL(xenbus_printf);
598
599 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
600 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
601 {
602         va_list ap;
603         const char *name;
604         int ret = 0;
605
606         va_start(ap, dir);
607         while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
608                 const char *fmt = va_arg(ap, char *);
609                 void *result = va_arg(ap, void *);
610                 char *p;
611
612                 p = xenbus_read(t, dir, name, NULL);
613                 if (IS_ERR(p)) {
614                         ret = PTR_ERR(p);
615                         break;
616                 }
617                 if (fmt) {
618                         if (sscanf(p, fmt, result) == 0)
619                                 ret = -EINVAL;
620                         kfree(p);
621                 } else
622                         *(char **)result = p;
623         }
624         va_end(ap);
625         return ret;
626 }
627 EXPORT_SYMBOL_GPL(xenbus_gather);
628
629 static int xs_watch(const char *path, const char *token)
630 {
631         struct kvec iov[2];
632
633         iov[0].iov_base = (void *)path;
634         iov[0].iov_len = strlen(path) + 1;
635         iov[1].iov_base = (void *)token;
636         iov[1].iov_len = strlen(token) + 1;
637
638         return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
639                                  ARRAY_SIZE(iov), NULL));
640 }
641
642 static int xs_unwatch(const char *path, const char *token)
643 {
644         struct kvec iov[2];
645
646         iov[0].iov_base = (char *)path;
647         iov[0].iov_len = strlen(path) + 1;
648         iov[1].iov_base = (char *)token;
649         iov[1].iov_len = strlen(token) + 1;
650
651         return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
652                                  ARRAY_SIZE(iov), NULL));
653 }
654
655 static struct xenbus_watch *find_watch(const char *token)
656 {
657         struct xenbus_watch *i, *cmp;
658
659         cmp = (void *)simple_strtoul(token, NULL, 16);
660
661         list_for_each_entry(i, &watches, list)
662                 if (i == cmp)
663                         return i;
664
665         return NULL;
666 }
667 /*
668  * Certain older XenBus toolstack cannot handle reading values that are
669  * not populated. Some Xen 3.4 installation are incapable of doing this
670  * so if we are running on anything older than 4 do not attempt to read
671  * control/platform-feature-xs_reset_watches.
672  */
673 static bool xen_strict_xenbus_quirk(void)
674 {
675 #ifdef CONFIG_X86
676         uint32_t eax, ebx, ecx, edx, base;
677
678         base = xen_cpuid_base();
679         cpuid(base + 1, &eax, &ebx, &ecx, &edx);
680
681         if ((eax >> 16) < 4)
682                 return true;
683 #endif
684         return false;
685
686 }
687 static void xs_reset_watches(void)
688 {
689         int err;
690
691         if (!xen_hvm_domain() || xen_initial_domain())
692                 return;
693
694         if (xen_strict_xenbus_quirk())
695                 return;
696
697         if (!xenbus_read_unsigned("control",
698                                   "platform-feature-xs_reset_watches", 0))
699                 return;
700
701         err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
702         if (err && err != -EEXIST)
703                 pr_warn("xs_reset_watches failed: %d\n", err);
704 }
705
706 /* Register callback to watch this node. */
707 int register_xenbus_watch(struct xenbus_watch *watch)
708 {
709         /* Pointer in ascii is the token. */
710         char token[sizeof(watch) * 2 + 1];
711         int err;
712
713         sprintf(token, "%lX", (long)watch);
714
715         down_read(&xs_state.watch_mutex);
716
717         spin_lock(&watches_lock);
718         BUG_ON(find_watch(token));
719         list_add(&watch->list, &watches);
720         spin_unlock(&watches_lock);
721
722         err = xs_watch(watch->node, token);
723
724         if (err) {
725                 spin_lock(&watches_lock);
726                 list_del(&watch->list);
727                 spin_unlock(&watches_lock);
728         }
729
730         up_read(&xs_state.watch_mutex);
731
732         return err;
733 }
734 EXPORT_SYMBOL_GPL(register_xenbus_watch);
735
736 void unregister_xenbus_watch(struct xenbus_watch *watch)
737 {
738         struct xs_stored_msg *msg, *tmp;
739         char token[sizeof(watch) * 2 + 1];
740         int err;
741
742         sprintf(token, "%lX", (long)watch);
743
744         down_read(&xs_state.watch_mutex);
745
746         spin_lock(&watches_lock);
747         BUG_ON(!find_watch(token));
748         list_del(&watch->list);
749         spin_unlock(&watches_lock);
750
751         err = xs_unwatch(watch->node, token);
752         if (err)
753                 pr_warn("Failed to release watch %s: %i\n", watch->node, err);
754
755         up_read(&xs_state.watch_mutex);
756
757         /* Make sure there are no callbacks running currently (unless
758            its us) */
759         if (current->pid != xenwatch_pid)
760                 mutex_lock(&xenwatch_mutex);
761
762         /* Cancel pending watch events. */
763         spin_lock(&watch_events_lock);
764         list_for_each_entry_safe(msg, tmp, &watch_events, list) {
765                 if (msg->u.watch.handle != watch)
766                         continue;
767                 list_del(&msg->list);
768                 kfree(msg->u.watch.vec);
769                 kfree(msg);
770         }
771         spin_unlock(&watch_events_lock);
772
773         if (current->pid != xenwatch_pid)
774                 mutex_unlock(&xenwatch_mutex);
775 }
776 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
777
778 void xs_suspend(void)
779 {
780         transaction_suspend();
781         down_write(&xs_state.watch_mutex);
782         mutex_lock(&xs_state.request_mutex);
783         mutex_lock(&xs_state.response_mutex);
784 }
785
786 void xs_resume(void)
787 {
788         struct xenbus_watch *watch;
789         char token[sizeof(watch) * 2 + 1];
790
791         xb_init_comms();
792
793         mutex_unlock(&xs_state.response_mutex);
794         mutex_unlock(&xs_state.request_mutex);
795         transaction_resume();
796
797         /* No need for watches_lock: the watch_mutex is sufficient. */
798         list_for_each_entry(watch, &watches, list) {
799                 sprintf(token, "%lX", (long)watch);
800                 xs_watch(watch->node, token);
801         }
802
803         up_write(&xs_state.watch_mutex);
804 }
805
806 void xs_suspend_cancel(void)
807 {
808         mutex_unlock(&xs_state.response_mutex);
809         mutex_unlock(&xs_state.request_mutex);
810         up_write(&xs_state.watch_mutex);
811         mutex_unlock(&xs_state.transaction_mutex);
812 }
813
814 static int xenwatch_thread(void *unused)
815 {
816         struct list_head *ent;
817         struct xs_stored_msg *msg;
818
819         for (;;) {
820                 wait_event_interruptible(watch_events_waitq,
821                                          !list_empty(&watch_events));
822
823                 if (kthread_should_stop())
824                         break;
825
826                 mutex_lock(&xenwatch_mutex);
827
828                 spin_lock(&watch_events_lock);
829                 ent = watch_events.next;
830                 if (ent != &watch_events)
831                         list_del(ent);
832                 spin_unlock(&watch_events_lock);
833
834                 if (ent != &watch_events) {
835                         msg = list_entry(ent, struct xs_stored_msg, list);
836                         msg->u.watch.handle->callback(
837                                 msg->u.watch.handle,
838                                 (const char **)msg->u.watch.vec,
839                                 msg->u.watch.vec_size);
840                         kfree(msg->u.watch.vec);
841                         kfree(msg);
842                 }
843
844                 mutex_unlock(&xenwatch_mutex);
845         }
846
847         return 0;
848 }
849
850 static int process_msg(void)
851 {
852         struct xs_stored_msg *msg;
853         char *body;
854         int err;
855
856         /*
857          * We must disallow save/restore while reading a xenstore message.
858          * A partial read across s/r leaves us out of sync with xenstored.
859          */
860         for (;;) {
861                 err = xb_wait_for_data_to_read();
862                 if (err)
863                         return err;
864                 mutex_lock(&xs_state.response_mutex);
865                 if (xb_data_to_read())
866                         break;
867                 /* We raced with save/restore: pending data 'disappeared'. */
868                 mutex_unlock(&xs_state.response_mutex);
869         }
870
871
872         msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
873         if (msg == NULL) {
874                 err = -ENOMEM;
875                 goto out;
876         }
877
878         err = xb_read(&msg->hdr, sizeof(msg->hdr));
879         if (err) {
880                 kfree(msg);
881                 goto out;
882         }
883
884         if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
885                 kfree(msg);
886                 err = -EINVAL;
887                 goto out;
888         }
889
890         body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
891         if (body == NULL) {
892                 kfree(msg);
893                 err = -ENOMEM;
894                 goto out;
895         }
896
897         err = xb_read(body, msg->hdr.len);
898         if (err) {
899                 kfree(body);
900                 kfree(msg);
901                 goto out;
902         }
903         body[msg->hdr.len] = '\0';
904
905         if (msg->hdr.type == XS_WATCH_EVENT) {
906                 msg->u.watch.vec = split(body, msg->hdr.len,
907                                          &msg->u.watch.vec_size);
908                 if (IS_ERR(msg->u.watch.vec)) {
909                         err = PTR_ERR(msg->u.watch.vec);
910                         kfree(msg);
911                         goto out;
912                 }
913
914                 spin_lock(&watches_lock);
915                 msg->u.watch.handle = find_watch(
916                         msg->u.watch.vec[XS_WATCH_TOKEN]);
917                 if (msg->u.watch.handle != NULL) {
918                         spin_lock(&watch_events_lock);
919                         list_add_tail(&msg->list, &watch_events);
920                         wake_up(&watch_events_waitq);
921                         spin_unlock(&watch_events_lock);
922                 } else {
923                         kfree(msg->u.watch.vec);
924                         kfree(msg);
925                 }
926                 spin_unlock(&watches_lock);
927         } else {
928                 msg->u.reply.body = body;
929                 spin_lock(&xs_state.reply_lock);
930                 list_add_tail(&msg->list, &xs_state.reply_list);
931                 spin_unlock(&xs_state.reply_lock);
932                 wake_up(&xs_state.reply_waitq);
933         }
934
935  out:
936         mutex_unlock(&xs_state.response_mutex);
937         return err;
938 }
939
940 static int xenbus_thread(void *unused)
941 {
942         int err;
943
944         for (;;) {
945                 err = process_msg();
946                 if (err)
947                         pr_warn("error %d while reading message\n", err);
948                 if (kthread_should_stop())
949                         break;
950         }
951
952         return 0;
953 }
954
955 int xs_init(void)
956 {
957         int err;
958         struct task_struct *task;
959
960         INIT_LIST_HEAD(&xs_state.reply_list);
961         spin_lock_init(&xs_state.reply_lock);
962         init_waitqueue_head(&xs_state.reply_waitq);
963
964         mutex_init(&xs_state.request_mutex);
965         mutex_init(&xs_state.response_mutex);
966         mutex_init(&xs_state.transaction_mutex);
967         init_rwsem(&xs_state.watch_mutex);
968         atomic_set(&xs_state.transaction_count, 0);
969         init_waitqueue_head(&xs_state.transaction_wq);
970
971         /* Initialize the shared memory rings to talk to xenstored */
972         err = xb_init_comms();
973         if (err)
974                 return err;
975
976         task = kthread_run(xenwatch_thread, NULL, "xenwatch");
977         if (IS_ERR(task))
978                 return PTR_ERR(task);
979         xenwatch_pid = task->pid;
980
981         task = kthread_run(xenbus_thread, NULL, "xenbus");
982         if (IS_ERR(task))
983                 return PTR_ERR(task);
984
985         /* shutdown watches for kexec boot */
986         xs_reset_watches();
987
988         return 0;
989 }