ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_xs.c @ 876:bd7e30b58d12

xenbus: Allow lazy init in case xenstored runs in a separate minios domain.

Here's an explanation of the states:

It starts out in XENBUS_XSD_UNCOMMITTED.

As the master xenbus (the one local to xenstored), it will receive an
mmap from xenstore, putting it in XENBUS_XSD_LOCAL_INIT. This enables
the wake_waiting IRQ, which will put it in XENBUS_XSD_LOCAL_READY.

Alternatively, as a slave xenbus, it will receive an ioctl from the
xenstore domain builder, putting it in XENBUS_XSD_FOREIGN_INIT. This
enables the wake_waiting IRQ, which will put it in
XENBUS_XSD_FOREIGN_READY.

DomU's are immediately initialized to XENBUS_XSD_FOREIGN_READY.

Signed-off-by: Diego Ongaro <diego.ongaro@citrix.com>
Signed-off-by: Alex Zeffertt <alex.zeffertt@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 19 14:45:50 2009 +0100 (2009-05-19)
parents 5db911a71eac
children
line source
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 */
34 #include <linux/unistd.h>
35 #include <linux/errno.h>
36 #include <linux/types.h>
37 #include <linux/uio.h>
38 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/err.h>
41 #include <linux/slab.h>
42 #include <linux/fcntl.h>
43 #include <linux/kthread.h>
44 #include <linux/rwsem.h>
45 #include <linux/module.h>
46 #include <linux/mutex.h>
47 #include <xen/xenbus.h>
48 #include "xenbus_comms.h"
50 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
51 #include <xen/platform-compat.h>
52 #endif
54 #ifndef PF_NOFREEZE /* Old kernel (pre-2.6.6). */
55 #define PF_NOFREEZE 0
56 #endif
58 struct xs_stored_msg {
59 struct list_head list;
61 struct xsd_sockmsg hdr;
63 union {
64 /* Queued replies. */
65 struct {
66 char *body;
67 } reply;
69 /* Queued watch events. */
70 struct {
71 struct xenbus_watch *handle;
72 char **vec;
73 unsigned int vec_size;
74 } watch;
75 } u;
76 };
78 struct xs_handle {
79 /* A list of replies. Currently only one will ever be outstanding. */
80 struct list_head reply_list;
81 spinlock_t reply_lock;
82 wait_queue_head_t reply_waitq;
84 /*
85 * Mutex ordering: transaction_mutex -> watch_mutex -> request_mutex.
86 * response_mutex is never taken simultaneously with the other three.
87 */
89 /* One request at a time. */
90 struct mutex request_mutex;
92 /* Protect xenbus reader thread against save/restore. */
93 struct mutex response_mutex;
95 /* Protect transactions against save/restore. */
96 struct rw_semaphore transaction_mutex;
98 /* Protect watch (de)register against save/restore. */
99 struct rw_semaphore watch_mutex;
100 };
102 static struct xs_handle xs_state;
104 /* List of registered watches, and a lock to protect it. */
105 static LIST_HEAD(watches);
106 static DEFINE_SPINLOCK(watches_lock);
108 /* List of pending watch callback events, and a lock to protect it. */
109 static LIST_HEAD(watch_events);
110 static DEFINE_SPINLOCK(watch_events_lock);
112 /*
113 * Details of the xenwatch callback kernel thread. The thread waits on the
114 * watch_events_waitq for work to do (queued on watch_events list). When it
115 * wakes up it acquires the xenwatch_mutex before reading the list and
116 * carrying out work.
117 */
118 static pid_t xenwatch_pid;
119 /* static */ DEFINE_MUTEX(xenwatch_mutex);
120 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
122 static int get_error(const char *errorstring)
123 {
124 unsigned int i;
126 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
127 if (i == ARRAY_SIZE(xsd_errors) - 1) {
128 printk(KERN_WARNING
129 "XENBUS xen store gave: unknown error %s",
130 errorstring);
131 return EINVAL;
132 }
133 }
134 return xsd_errors[i].errnum;
135 }
137 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
138 {
139 struct xs_stored_msg *msg;
140 char *body;
142 spin_lock(&xs_state.reply_lock);
144 while (list_empty(&xs_state.reply_list)) {
145 spin_unlock(&xs_state.reply_lock);
146 /* XXX FIXME: Avoid synchronous wait for response here. */
147 wait_event(xs_state.reply_waitq,
148 !list_empty(&xs_state.reply_list));
149 spin_lock(&xs_state.reply_lock);
150 }
152 msg = list_entry(xs_state.reply_list.next,
153 struct xs_stored_msg, list);
154 list_del(&msg->list);
156 spin_unlock(&xs_state.reply_lock);
158 *type = msg->hdr.type;
159 if (len)
160 *len = msg->hdr.len;
161 body = msg->u.reply.body;
163 kfree(msg);
165 return body;
166 }
168 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
169 {
170 void *ret;
171 struct xsd_sockmsg req_msg = *msg;
172 int err;
174 if (req_msg.type == XS_TRANSACTION_START)
175 down_read(&xs_state.transaction_mutex);
177 mutex_lock(&xs_state.request_mutex);
179 err = xb_write(msg, sizeof(*msg) + msg->len);
180 if (err) {
181 msg->type = XS_ERROR;
182 ret = ERR_PTR(err);
183 } else
184 ret = read_reply(&msg->type, &msg->len);
186 mutex_unlock(&xs_state.request_mutex);
188 if ((req_msg.type == XS_TRANSACTION_END) ||
189 ((req_msg.type == XS_TRANSACTION_START) &&
190 (msg->type == XS_ERROR)))
191 up_read(&xs_state.transaction_mutex);
193 return ret;
194 }
196 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
197 static void *xs_talkv(struct xenbus_transaction t,
198 enum xsd_sockmsg_type type,
199 const struct kvec *iovec,
200 unsigned int num_vecs,
201 unsigned int *len)
202 {
203 struct xsd_sockmsg msg;
204 void *ret = NULL;
205 unsigned int i;
206 int err;
208 msg.tx_id = t.id;
209 msg.req_id = 0;
210 msg.type = type;
211 msg.len = 0;
212 for (i = 0; i < num_vecs; i++)
213 msg.len += iovec[i].iov_len;
215 mutex_lock(&xs_state.request_mutex);
217 err = xb_write(&msg, sizeof(msg));
218 if (err) {
219 mutex_unlock(&xs_state.request_mutex);
220 return ERR_PTR(err);
221 }
223 for (i = 0; i < num_vecs; i++) {
224 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);;
225 if (err) {
226 mutex_unlock(&xs_state.request_mutex);
227 return ERR_PTR(err);
228 }
229 }
231 ret = read_reply(&msg.type, len);
233 mutex_unlock(&xs_state.request_mutex);
235 if (IS_ERR(ret))
236 return ret;
238 if (msg.type == XS_ERROR) {
239 err = get_error(ret);
240 kfree(ret);
241 return ERR_PTR(-err);
242 }
244 if (msg.type != type) {
245 if (printk_ratelimit())
246 printk(KERN_WARNING
247 "XENBUS unexpected type [%d], expected [%d]\n",
248 msg.type, type);
249 kfree(ret);
250 return ERR_PTR(-EINVAL);
251 }
252 return ret;
253 }
255 /* Simplified version of xs_talkv: single message. */
256 static void *xs_single(struct xenbus_transaction t,
257 enum xsd_sockmsg_type type,
258 const char *string,
259 unsigned int *len)
260 {
261 struct kvec iovec;
263 iovec.iov_base = (void *)string;
264 iovec.iov_len = strlen(string) + 1;
265 return xs_talkv(t, type, &iovec, 1, len);
266 }
268 /* Many commands only need an ack, don't care what it says. */
269 static int xs_error(char *reply)
270 {
271 if (IS_ERR(reply))
272 return PTR_ERR(reply);
273 kfree(reply);
274 return 0;
275 }
277 static unsigned int count_strings(const char *strings, unsigned int len)
278 {
279 unsigned int num;
280 const char *p;
282 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
283 num++;
285 return num;
286 }
288 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
289 static char *join(const char *dir, const char *name)
290 {
291 char *buffer;
293 if (strlen(name) == 0)
294 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
295 else
296 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
297 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
298 }
300 static char **split(char *strings, unsigned int len, unsigned int *num)
301 {
302 char *p, **ret;
304 /* Count the strings. */
305 *num = count_strings(strings, len) + 1;
307 /* Transfer to one big alloc for easy freeing. */
308 ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
309 if (!ret) {
310 kfree(strings);
311 return ERR_PTR(-ENOMEM);
312 }
313 memcpy(&ret[*num], strings, len);
314 kfree(strings);
316 strings = (char *)&ret[*num];
317 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
318 ret[(*num)++] = p;
319 ret[*num] = strings + len;
321 return ret;
322 }
324 char **xenbus_directory(struct xenbus_transaction t,
325 const char *dir, const char *node, unsigned int *num)
326 {
327 char *strings, *path;
328 unsigned int len;
330 path = join(dir, node);
331 if (IS_ERR(path))
332 return (char **)path;
334 strings = xs_single(t, XS_DIRECTORY, path, &len);
335 kfree(path);
336 if (IS_ERR(strings))
337 return (char **)strings;
339 return split(strings, len, num);
340 }
341 EXPORT_SYMBOL_GPL(xenbus_directory);
343 /* Check if a path exists. Return 1 if it does. */
344 int xenbus_exists(struct xenbus_transaction t,
345 const char *dir, const char *node)
346 {
347 char **d;
348 int dir_n;
350 d = xenbus_directory(t, dir, node, &dir_n);
351 if (IS_ERR(d))
352 return 0;
353 kfree(d);
354 return 1;
355 }
356 EXPORT_SYMBOL_GPL(xenbus_exists);
358 /* Get the value of a single file.
359 * Returns a kmalloced value: call free() on it after use.
360 * len indicates length in bytes.
361 */
362 void *xenbus_read(struct xenbus_transaction t,
363 const char *dir, const char *node, unsigned int *len)
364 {
365 char *path;
366 void *ret;
368 path = join(dir, node);
369 if (IS_ERR(path))
370 return (void *)path;
372 ret = xs_single(t, XS_READ, path, len);
373 kfree(path);
374 return ret;
375 }
376 EXPORT_SYMBOL_GPL(xenbus_read);
378 /* Write the value of a single file.
379 * Returns -err on failure.
380 */
381 int xenbus_write(struct xenbus_transaction t,
382 const char *dir, const char *node, const char *string)
383 {
384 const char *path;
385 struct kvec iovec[2];
386 int ret;
388 path = join(dir, node);
389 if (IS_ERR(path))
390 return PTR_ERR(path);
392 iovec[0].iov_base = (void *)path;
393 iovec[0].iov_len = strlen(path) + 1;
394 iovec[1].iov_base = (void *)string;
395 iovec[1].iov_len = strlen(string);
397 ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
398 kfree(path);
399 return ret;
400 }
401 EXPORT_SYMBOL_GPL(xenbus_write);
403 /* Create a new directory. */
404 int xenbus_mkdir(struct xenbus_transaction t,
405 const char *dir, const char *node)
406 {
407 char *path;
408 int ret;
410 path = join(dir, node);
411 if (IS_ERR(path))
412 return PTR_ERR(path);
414 ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
415 kfree(path);
416 return ret;
417 }
418 EXPORT_SYMBOL_GPL(xenbus_mkdir);
420 /* Destroy a file or directory (directories must be empty). */
421 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
422 {
423 char *path;
424 int ret;
426 path = join(dir, node);
427 if (IS_ERR(path))
428 return PTR_ERR(path);
430 ret = xs_error(xs_single(t, XS_RM, path, NULL));
431 kfree(path);
432 return ret;
433 }
434 EXPORT_SYMBOL_GPL(xenbus_rm);
436 /* Start a transaction: changes by others will not be seen during this
437 * transaction, and changes will not be visible to others until end.
438 */
439 int xenbus_transaction_start(struct xenbus_transaction *t)
440 {
441 char *id_str;
443 down_read(&xs_state.transaction_mutex);
445 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
446 if (IS_ERR(id_str)) {
447 up_read(&xs_state.transaction_mutex);
448 return PTR_ERR(id_str);
449 }
451 t->id = simple_strtoul(id_str, NULL, 0);
452 kfree(id_str);
453 return 0;
454 }
455 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
457 /* End a transaction.
458 * If abandon is true, transaction is discarded instead of committed.
459 */
460 int xenbus_transaction_end(struct xenbus_transaction t, int abort)
461 {
462 char abortstr[2];
463 int err;
465 if (abort)
466 strcpy(abortstr, "F");
467 else
468 strcpy(abortstr, "T");
470 err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
472 up_read(&xs_state.transaction_mutex);
474 return err;
475 }
476 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
478 /* Single read and scanf: returns -errno or num scanned. */
479 int xenbus_scanf(struct xenbus_transaction t,
480 const char *dir, const char *node, const char *fmt, ...)
481 {
482 va_list ap;
483 int ret;
484 char *val;
486 val = xenbus_read(t, dir, node, NULL);
487 if (IS_ERR(val))
488 return PTR_ERR(val);
490 va_start(ap, fmt);
491 ret = vsscanf(val, fmt, ap);
492 va_end(ap);
493 kfree(val);
494 /* Distinctive errno. */
495 if (ret == 0)
496 return -ERANGE;
497 return ret;
498 }
499 EXPORT_SYMBOL_GPL(xenbus_scanf);
501 /* Single printf and write: returns -errno or 0. */
502 int xenbus_printf(struct xenbus_transaction t,
503 const char *dir, const char *node, const char *fmt, ...)
504 {
505 va_list ap;
506 int ret;
507 #define PRINTF_BUFFER_SIZE 4096
508 char *printf_buffer;
510 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_NOIO | __GFP_HIGH);
511 if (printf_buffer == NULL)
512 return -ENOMEM;
514 va_start(ap, fmt);
515 ret = vsnprintf(printf_buffer, PRINTF_BUFFER_SIZE, fmt, ap);
516 va_end(ap);
518 BUG_ON(ret > PRINTF_BUFFER_SIZE-1);
519 ret = xenbus_write(t, dir, node, printf_buffer);
521 kfree(printf_buffer);
523 return ret;
524 }
525 EXPORT_SYMBOL_GPL(xenbus_printf);
527 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
528 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
529 {
530 va_list ap;
531 const char *name;
532 int ret = 0;
534 va_start(ap, dir);
535 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
536 const char *fmt = va_arg(ap, char *);
537 void *result = va_arg(ap, void *);
538 char *p;
540 p = xenbus_read(t, dir, name, NULL);
541 if (IS_ERR(p)) {
542 ret = PTR_ERR(p);
543 break;
544 }
545 if (fmt) {
546 if (sscanf(p, fmt, result) == 0)
547 ret = -EINVAL;
548 kfree(p);
549 } else
550 *(char **)result = p;
551 }
552 va_end(ap);
553 return ret;
554 }
555 EXPORT_SYMBOL_GPL(xenbus_gather);
557 static int xs_watch(const char *path, const char *token)
558 {
559 struct kvec iov[2];
561 iov[0].iov_base = (void *)path;
562 iov[0].iov_len = strlen(path) + 1;
563 iov[1].iov_base = (void *)token;
564 iov[1].iov_len = strlen(token) + 1;
566 return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
567 ARRAY_SIZE(iov), NULL));
568 }
570 static int xs_unwatch(const char *path, const char *token)
571 {
572 struct kvec iov[2];
574 iov[0].iov_base = (char *)path;
575 iov[0].iov_len = strlen(path) + 1;
576 iov[1].iov_base = (char *)token;
577 iov[1].iov_len = strlen(token) + 1;
579 return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
580 ARRAY_SIZE(iov), NULL));
581 }
583 static struct xenbus_watch *find_watch(const char *token)
584 {
585 struct xenbus_watch *i, *cmp;
587 cmp = (void *)simple_strtoul(token, NULL, 16);
589 list_for_each_entry(i, &watches, list)
590 if (i == cmp)
591 return i;
593 return NULL;
594 }
596 /* Register callback to watch this node. */
597 int register_xenbus_watch(struct xenbus_watch *watch)
598 {
599 /* Pointer in ascii is the token. */
600 char token[sizeof(watch) * 2 + 1];
601 int err;
603 sprintf(token, "%lX", (long)watch);
605 down_read(&xs_state.watch_mutex);
607 spin_lock(&watches_lock);
608 BUG_ON(find_watch(token));
609 list_add(&watch->list, &watches);
610 spin_unlock(&watches_lock);
612 err = xs_watch(watch->node, token);
614 /* Ignore errors due to multiple registration. */
615 if ((err != 0) && (err != -EEXIST)) {
616 spin_lock(&watches_lock);
617 list_del(&watch->list);
618 spin_unlock(&watches_lock);
619 }
621 up_read(&xs_state.watch_mutex);
623 return err;
624 }
625 EXPORT_SYMBOL_GPL(register_xenbus_watch);
627 void unregister_xenbus_watch(struct xenbus_watch *watch)
628 {
629 struct xs_stored_msg *msg, *tmp;
630 char token[sizeof(watch) * 2 + 1];
631 int err;
633 BUG_ON(watch->flags & XBWF_new_thread);
635 sprintf(token, "%lX", (long)watch);
637 down_read(&xs_state.watch_mutex);
639 spin_lock(&watches_lock);
640 BUG_ON(!find_watch(token));
641 list_del(&watch->list);
642 spin_unlock(&watches_lock);
644 err = xs_unwatch(watch->node, token);
645 if (err)
646 printk(KERN_WARNING
647 "XENBUS Failed to release watch %s: %i\n",
648 watch->node, err);
650 up_read(&xs_state.watch_mutex);
652 /* Cancel pending watch events. */
653 spin_lock(&watch_events_lock);
654 list_for_each_entry_safe(msg, tmp, &watch_events, list) {
655 if (msg->u.watch.handle != watch)
656 continue;
657 list_del(&msg->list);
658 kfree(msg->u.watch.vec);
659 kfree(msg);
660 }
661 spin_unlock(&watch_events_lock);
663 /* Flush any currently-executing callback, unless we are it. :-) */
664 if (current->pid != xenwatch_pid) {
665 mutex_lock(&xenwatch_mutex);
666 mutex_unlock(&xenwatch_mutex);
667 }
668 }
669 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
671 void xs_suspend(void)
672 {
673 down_write(&xs_state.transaction_mutex);
674 down_write(&xs_state.watch_mutex);
675 mutex_lock(&xs_state.request_mutex);
676 mutex_lock(&xs_state.response_mutex);
677 }
679 void xs_resume(void)
680 {
681 struct xenbus_watch *watch;
682 char token[sizeof(watch) * 2 + 1];
684 mutex_unlock(&xs_state.response_mutex);
685 mutex_unlock(&xs_state.request_mutex);
686 up_write(&xs_state.transaction_mutex);
688 /* No need for watches_lock: the watch_mutex is sufficient. */
689 list_for_each_entry(watch, &watches, list) {
690 sprintf(token, "%lX", (long)watch);
691 xs_watch(watch->node, token);
692 }
694 up_write(&xs_state.watch_mutex);
695 }
697 void xs_suspend_cancel(void)
698 {
699 mutex_unlock(&xs_state.response_mutex);
700 mutex_unlock(&xs_state.request_mutex);
701 up_write(&xs_state.watch_mutex);
702 up_write(&xs_state.transaction_mutex);
703 }
705 static int xenwatch_handle_callback(void *data)
706 {
707 struct xs_stored_msg *msg = data;
709 msg->u.watch.handle->callback(msg->u.watch.handle,
710 (const char **)msg->u.watch.vec,
711 msg->u.watch.vec_size);
713 kfree(msg->u.watch.vec);
714 kfree(msg);
716 /* Kill this kthread if we were spawned just for this callback. */
717 if (current->pid != xenwatch_pid)
718 do_exit(0);
720 return 0;
721 }
723 static int xenwatch_thread(void *unused)
724 {
725 struct list_head *ent;
726 struct xs_stored_msg *msg;
728 current->flags |= PF_NOFREEZE;
729 for (;;) {
730 wait_event_interruptible(watch_events_waitq,
731 !list_empty(&watch_events));
733 if (kthread_should_stop())
734 break;
736 mutex_lock(&xenwatch_mutex);
738 spin_lock(&watch_events_lock);
739 ent = watch_events.next;
740 if (ent != &watch_events)
741 list_del(ent);
742 spin_unlock(&watch_events_lock);
744 if (ent == &watch_events) {
745 mutex_unlock(&xenwatch_mutex);
746 continue;
747 }
749 msg = list_entry(ent, struct xs_stored_msg, list);
751 /*
752 * Unlock the mutex before running an XBWF_new_thread
753 * handler. kthread_run can block which can deadlock
754 * against unregister_xenbus_watch() if we need to
755 * unregister other watches in order to make
756 * progress. This can occur on resume before the swap
757 * device is attached.
758 */
759 if (msg->u.watch.handle->flags & XBWF_new_thread) {
760 mutex_unlock(&xenwatch_mutex);
761 kthread_run(xenwatch_handle_callback,
762 msg, "xenwatch_cb");
763 } else {
764 xenwatch_handle_callback(msg);
765 mutex_unlock(&xenwatch_mutex);
766 }
767 }
769 return 0;
770 }
772 static int process_msg(void)
773 {
774 struct xs_stored_msg *msg;
775 char *body;
776 int err;
778 /*
779 * We must disallow save/restore while reading a xenstore message.
780 * A partial read across s/r leaves us out of sync with xenstored.
781 */
782 for (;;) {
783 err = xb_wait_for_data_to_read();
784 if (err)
785 return err;
786 mutex_lock(&xs_state.response_mutex);
787 if (xb_data_to_read())
788 break;
789 /* We raced with save/restore: pending data 'disappeared'. */
790 mutex_unlock(&xs_state.response_mutex);
791 }
794 msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
795 if (msg == NULL) {
796 err = -ENOMEM;
797 goto out;
798 }
800 err = xb_read(&msg->hdr, sizeof(msg->hdr));
801 if (err) {
802 kfree(msg);
803 goto out;
804 }
806 body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
807 if (body == NULL) {
808 kfree(msg);
809 err = -ENOMEM;
810 goto out;
811 }
813 err = xb_read(body, msg->hdr.len);
814 if (err) {
815 kfree(body);
816 kfree(msg);
817 goto out;
818 }
819 body[msg->hdr.len] = '\0';
821 if (msg->hdr.type == XS_WATCH_EVENT) {
822 msg->u.watch.vec = split(body, msg->hdr.len,
823 &msg->u.watch.vec_size);
824 if (IS_ERR(msg->u.watch.vec)) {
825 err = PTR_ERR(msg->u.watch.vec);
826 kfree(msg);
827 goto out;
828 }
830 spin_lock(&watches_lock);
831 msg->u.watch.handle = find_watch(
832 msg->u.watch.vec[XS_WATCH_TOKEN]);
833 if (msg->u.watch.handle != NULL) {
834 spin_lock(&watch_events_lock);
835 list_add_tail(&msg->list, &watch_events);
836 wake_up(&watch_events_waitq);
837 spin_unlock(&watch_events_lock);
838 } else {
839 kfree(msg->u.watch.vec);
840 kfree(msg);
841 }
842 spin_unlock(&watches_lock);
843 } else {
844 msg->u.reply.body = body;
845 spin_lock(&xs_state.reply_lock);
846 list_add_tail(&msg->list, &xs_state.reply_list);
847 spin_unlock(&xs_state.reply_lock);
848 wake_up(&xs_state.reply_waitq);
849 }
851 out:
852 mutex_unlock(&xs_state.response_mutex);
853 return err;
854 }
856 static int xenbus_thread(void *unused)
857 {
858 int err;
860 current->flags |= PF_NOFREEZE;
861 for (;;) {
862 err = process_msg();
863 if (err)
864 printk(KERN_WARNING "XENBUS error %d while reading "
865 "message\n", err);
866 if (kthread_should_stop())
867 break;
868 }
870 return 0;
871 }
873 int xs_init(void)
874 {
875 struct task_struct *task;
877 INIT_LIST_HEAD(&xs_state.reply_list);
878 spin_lock_init(&xs_state.reply_lock);
879 init_waitqueue_head(&xs_state.reply_waitq);
881 mutex_init(&xs_state.request_mutex);
882 mutex_init(&xs_state.response_mutex);
883 init_rwsem(&xs_state.transaction_mutex);
884 init_rwsem(&xs_state.watch_mutex);
886 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
887 if (IS_ERR(task))
888 return PTR_ERR(task);
889 xenwatch_pid = task->pid;
891 task = kthread_run(xenbus_thread, NULL, "xenbus");
892 if (IS_ERR(task))
893 return PTR_ERR(task);
895 return 0;
896 }