]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/xen/xenbus/xenbus_xs.c
xen: modify xenstore watch event interface
[mirror_ubuntu-artful-kernel.git] / drivers / xen / xenbus / xenbus_xs.c
CommitLineData
4bac07c9
JF
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
283c0972
JP
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
4bac07c9
JF
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>
4bac07c9 47#include <linux/mutex.h>
ecc635f9 48#include <asm/xen/hypervisor.h>
4bac07c9 49#include <xen/xenbus.h>
5b25d89e 50#include <xen/xen.h>
332f791d 51#include "xenbus.h"
4bac07c9
JF
52
53struct 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;
5584ea25
JG
67 const char *path;
68 const char *token;
4bac07c9
JF
69 } watch;
70 } u;
71};
72
73struct 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.
4c31a781
IC
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.
4bac07c9
JF
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. */
4c31a781
IC
99 struct mutex transaction_mutex;
100 atomic_t transaction_count;
101 wait_queue_head_t transaction_wq;
4bac07c9
JF
102
103 /* Protect watch (de)register against save/restore. */
104 struct rw_semaphore watch_mutex;
105};
106
107static struct xs_handle xs_state;
108
109/* List of registered watches, and a lock to protect it. */
110static LIST_HEAD(watches);
111static DEFINE_SPINLOCK(watches_lock);
112
113/* List of pending watch callback events, and a lock to protect it. */
114static LIST_HEAD(watch_events);
115static 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 */
123static pid_t xenwatch_pid;
124static DEFINE_MUTEX(xenwatch_mutex);
125static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
126
127static 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) {
283c0972
JP
133 pr_warn("xen store gave: unknown error %s\n",
134 errorstring);
4bac07c9
JF
135 return EINVAL;
136 }
137 }
138 return xsd_errors[i].errnum;
139}
140
027bd7e8
KRW
141static 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}
4bac07c9
JF
164static 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);
027bd7e8
KRW
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 }
4bac07c9
JF
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
4c31a781
IC
206static 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
213static void transaction_end(void)
214{
215 if (atomic_dec_and_test(&xs_state.transaction_count))
216 wake_up(&xs_state.transaction_wq);
217}
218
219static 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
226static void transaction_resume(void)
227{
228 mutex_unlock(&xs_state.transaction_mutex);
229}
230
4bac07c9
JF
231void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
232{
233 void *ret;
e5a79475 234 enum xsd_sockmsg_type type = msg->type;
4bac07c9
JF
235 int err;
236
e5a79475 237 if (type == XS_TRANSACTION_START)
4c31a781 238 transaction_start();
4bac07c9
JF
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) ||
e5a79475 252 ((type == XS_TRANSACTION_START) && (msg->type == XS_ERROR)))
4c31a781 253 transaction_end();
4bac07c9
JF
254
255 return ret;
256}
1107ba88 257EXPORT_SYMBOL(xenbus_dev_request_and_reply);
4bac07c9
JF
258
259/* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
260static 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) {
283c0972
JP
308 pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
309 msg.type, type);
4bac07c9
JF
310 kfree(ret);
311 return ERR_PTR(-EINVAL);
312 }
313 return ret;
314}
315
316/* Simplified version of xs_talkv: single message. */
317static 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. */
330static int xs_error(char *reply)
331{
332 if (IS_ERR(reply))
333 return PTR_ERR(reply);
334 kfree(reply);
335 return 0;
336}
337
338static 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. */
350static char *join(const char *dir, const char *name)
351{
352 char *buffer;
353
354 if (strlen(name) == 0)
a144ff09 355 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
4bac07c9 356 else
a144ff09 357 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
4bac07c9
JF
358 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
359}
360
361static 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. */
a144ff09 369 ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
4bac07c9
JF
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
384char **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}
401EXPORT_SYMBOL_GPL(xenbus_directory);
402
403/* Check if a path exists. Return 1 if it does. */
404int 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}
416EXPORT_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 */
422void *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}
436EXPORT_SYMBOL_GPL(xenbus_read);
437
438/* Write the value of a single file.
439 * Returns -err on failure.
440 */
441int 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}
461EXPORT_SYMBOL_GPL(xenbus_write);
462
463/* Create a new directory. */
464int 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}
478EXPORT_SYMBOL_GPL(xenbus_mkdir);
479
480/* Destroy a file or directory (directories must be empty). */
481int 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}
494EXPORT_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 */
499int xenbus_transaction_start(struct xenbus_transaction *t)
500{
501 char *id_str;
502
4c31a781 503 transaction_start();
4bac07c9
JF
504
505 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
506 if (IS_ERR(id_str)) {
4c31a781 507 transaction_end();
4bac07c9
JF
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}
515EXPORT_SYMBOL_GPL(xenbus_transaction_start);
516
517/* End a transaction.
518 * If abandon is true, transaction is discarded instead of committed.
519 */
520int 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
4c31a781 532 transaction_end();
4bac07c9
JF
533
534 return err;
535}
536EXPORT_SYMBOL_GPL(xenbus_transaction_end);
537
538/* Single read and scanf: returns -errno or num scanned. */
539int 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}
559EXPORT_SYMBOL_GPL(xenbus_scanf);
560
9c53a179
JG
561/* Read an (optional) unsigned value. */
562unsigned 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}
574EXPORT_SYMBOL_GPL(xenbus_read_unsigned);
575
4bac07c9
JF
576/* Single printf and write: returns -errno or 0. */
577int xenbus_printf(struct xenbus_transaction t,
578 const char *dir, const char *node, const char *fmt, ...)
579{
580 va_list ap;
581 int ret;
a800651e 582 char *buf;
4bac07c9
JF
583
584 va_start(ap, fmt);
a800651e 585 buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
4bac07c9
JF
586 va_end(ap);
587
a800651e
IC
588 if (!buf)
589 return -ENOMEM;
590
591 ret = xenbus_write(t, dir, node, buf);
4bac07c9 592
a800651e 593 kfree(buf);
4bac07c9
JF
594
595 return ret;
596}
597EXPORT_SYMBOL_GPL(xenbus_printf);
598
599/* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
600int 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}
627EXPORT_SYMBOL_GPL(xenbus_gather);
628
629static 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
642static 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
655static 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}
cb6b6df1
KRW
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 */
e9d1aa05 673static bool xen_strict_xenbus_quirk(void)
cb6b6df1 674{
7644bdac 675#ifdef CONFIG_X86
cb6b6df1
KRW
676 uint32_t eax, ebx, ecx, edx, base;
677
678 base = xen_cpuid_base();
679 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
4bac07c9 680
cb6b6df1
KRW
681 if ((eax >> 16) < 4)
682 return true;
7644bdac 683#endif
cb6b6df1
KRW
684 return false;
685
686}
254d1a3f
OH
687static void xs_reset_watches(void)
688{
999c9af9 689 int err;
254d1a3f 690
ecc635f9 691 if (!xen_hvm_domain() || xen_initial_domain())
254d1a3f
OH
692 return;
693
cb6b6df1
KRW
694 if (xen_strict_xenbus_quirk())
695 return;
696
999c9af9
JG
697 if (!xenbus_read_unsigned("control",
698 "platform-feature-xs_reset_watches", 0))
254d1a3f
OH
699 return;
700
701 err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
702 if (err && err != -EEXIST)
283c0972 703 pr_warn("xs_reset_watches failed: %d\n", err);
254d1a3f
OH
704}
705
4bac07c9
JF
706/* Register callback to watch this node. */
707int 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
c4c303c7 724 if (err) {
4bac07c9
JF
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}
734EXPORT_SYMBOL_GPL(register_xenbus_watch);
735
736void 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)
283c0972 753 pr_warn("Failed to release watch %s: %i\n", watch->node, err);
4bac07c9
JF
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);
5584ea25 768 kfree(msg->u.watch.path);
4bac07c9
JF
769 kfree(msg);
770 }
771 spin_unlock(&watch_events_lock);
772
773 if (current->pid != xenwatch_pid)
774 mutex_unlock(&xenwatch_mutex);
775}
776EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
777
778void xs_suspend(void)
779{
4c31a781 780 transaction_suspend();
4bac07c9
JF
781 down_write(&xs_state.watch_mutex);
782 mutex_lock(&xs_state.request_mutex);
783 mutex_lock(&xs_state.response_mutex);
784}
785
786void xs_resume(void)
787{
788 struct xenbus_watch *watch;
789 char token[sizeof(watch) * 2 + 1];
790
de5b31bd
IC
791 xb_init_comms();
792
4bac07c9
JF
793 mutex_unlock(&xs_state.response_mutex);
794 mutex_unlock(&xs_state.request_mutex);
4c31a781 795 transaction_resume();
4bac07c9
JF
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
806void 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);
4c31a781 811 mutex_unlock(&xs_state.transaction_mutex);
4bac07c9
JF
812}
813
814static 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);
5584ea25
JG
836 msg->u.watch.handle->callback(msg->u.watch.handle,
837 msg->u.watch.path,
838 msg->u.watch.token);
839 kfree(msg->u.watch.path);
4bac07c9
JF
840 kfree(msg);
841 }
842
843 mutex_unlock(&xenwatch_mutex);
844 }
845
846 return 0;
847}
848
849static int process_msg(void)
850{
851 struct xs_stored_msg *msg;
852 char *body;
853 int err;
854
855 /*
856 * We must disallow save/restore while reading a xenstore message.
857 * A partial read across s/r leaves us out of sync with xenstored.
858 */
859 for (;;) {
860 err = xb_wait_for_data_to_read();
861 if (err)
862 return err;
863 mutex_lock(&xs_state.response_mutex);
864 if (xb_data_to_read())
865 break;
866 /* We raced with save/restore: pending data 'disappeared'. */
867 mutex_unlock(&xs_state.response_mutex);
868 }
869
870
a144ff09 871 msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
4bac07c9
JF
872 if (msg == NULL) {
873 err = -ENOMEM;
874 goto out;
875 }
876
877 err = xb_read(&msg->hdr, sizeof(msg->hdr));
878 if (err) {
879 kfree(msg);
880 goto out;
881 }
882
9e7860ce
IC
883 if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
884 kfree(msg);
885 err = -EINVAL;
886 goto out;
887 }
888
a144ff09 889 body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
4bac07c9
JF
890 if (body == NULL) {
891 kfree(msg);
892 err = -ENOMEM;
893 goto out;
894 }
895
896 err = xb_read(body, msg->hdr.len);
897 if (err) {
898 kfree(body);
899 kfree(msg);
900 goto out;
901 }
902 body[msg->hdr.len] = '\0';
903
904 if (msg->hdr.type == XS_WATCH_EVENT) {
5584ea25
JG
905 if (count_strings(body, msg->hdr.len) != 2) {
906 err = -EINVAL;
98ac0e53 907 kfree(msg);
5584ea25 908 kfree(body);
4bac07c9
JF
909 goto out;
910 }
5584ea25
JG
911 msg->u.watch.path = (const char *)body;
912 msg->u.watch.token = (const char *)strchr(body, '\0') + 1;
4bac07c9
JF
913
914 spin_lock(&watches_lock);
5584ea25 915 msg->u.watch.handle = find_watch(msg->u.watch.token);
4bac07c9
JF
916 if (msg->u.watch.handle != NULL) {
917 spin_lock(&watch_events_lock);
918 list_add_tail(&msg->list, &watch_events);
919 wake_up(&watch_events_waitq);
920 spin_unlock(&watch_events_lock);
921 } else {
5584ea25 922 kfree(body);
4bac07c9
JF
923 kfree(msg);
924 }
925 spin_unlock(&watches_lock);
926 } else {
927 msg->u.reply.body = body;
928 spin_lock(&xs_state.reply_lock);
929 list_add_tail(&msg->list, &xs_state.reply_list);
930 spin_unlock(&xs_state.reply_lock);
931 wake_up(&xs_state.reply_waitq);
932 }
933
934 out:
935 mutex_unlock(&xs_state.response_mutex);
936 return err;
937}
938
939static int xenbus_thread(void *unused)
940{
941 int err;
942
943 for (;;) {
944 err = process_msg();
945 if (err)
283c0972 946 pr_warn("error %d while reading message\n", err);
4bac07c9
JF
947 if (kthread_should_stop())
948 break;
949 }
950
951 return 0;
952}
953
954int xs_init(void)
955{
956 int err;
957 struct task_struct *task;
958
959 INIT_LIST_HEAD(&xs_state.reply_list);
960 spin_lock_init(&xs_state.reply_lock);
961 init_waitqueue_head(&xs_state.reply_waitq);
962
963 mutex_init(&xs_state.request_mutex);
964 mutex_init(&xs_state.response_mutex);
4c31a781 965 mutex_init(&xs_state.transaction_mutex);
4bac07c9 966 init_rwsem(&xs_state.watch_mutex);
4c31a781
IC
967 atomic_set(&xs_state.transaction_count, 0);
968 init_waitqueue_head(&xs_state.transaction_wq);
4bac07c9
JF
969
970 /* Initialize the shared memory rings to talk to xenstored */
971 err = xb_init_comms();
972 if (err)
973 return err;
974
975 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
976 if (IS_ERR(task))
977 return PTR_ERR(task);
978 xenwatch_pid = task->pid;
979
980 task = kthread_run(xenbus_thread, NULL, "xenbus");
981 if (IS_ERR(task))
982 return PTR_ERR(task);
983
254d1a3f
OH
984 /* shutdown watches for kexec boot */
985 xs_reset_watches();
986
4bac07c9
JF
987 return 0;
988}