ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c @ 13665:140afd7a5462

merge with xen-unstable.hg
author awilliam@xenbuild2.aw
date Sat Jan 27 14:07:19 2007 -0700 (2007-01-27)
parents b4a8000e76db
children b67c253d1cdb
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 struct xs_stored_msg {
55 struct list_head list;
57 struct xsd_sockmsg hdr;
59 union {
60 /* Queued replies. */
61 struct {
62 char *body;
63 } reply;
65 /* Queued watch events. */
66 struct {
67 struct xenbus_watch *handle;
68 char **vec;
69 unsigned int vec_size;
70 } watch;
71 } u;
72 };
74 struct xs_handle {
75 /* A list of replies. Currently only one will ever be outstanding. */
76 struct list_head reply_list;
77 spinlock_t reply_lock;
78 wait_queue_head_t reply_waitq;
80 /* One request at a time. */
81 struct mutex request_mutex;
83 /* Protect transactions against save/restore. */
84 struct rw_semaphore suspend_mutex;
85 };
87 static struct xs_handle xs_state;
89 /* List of registered watches, and a lock to protect it. */
90 static LIST_HEAD(watches);
91 static DEFINE_SPINLOCK(watches_lock);
93 /* List of pending watch callback events, and a lock to protect it. */
94 static LIST_HEAD(watch_events);
95 static DEFINE_SPINLOCK(watch_events_lock);
97 /*
98 * Details of the xenwatch callback kernel thread. The thread waits on the
99 * watch_events_waitq for work to do (queued on watch_events list). When it
100 * wakes up it acquires the xenwatch_mutex before reading the list and
101 * carrying out work.
102 */
103 static pid_t xenwatch_pid;
104 /* static */ DEFINE_MUTEX(xenwatch_mutex);
105 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
107 static int get_error(const char *errorstring)
108 {
109 unsigned int i;
111 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
112 if (i == ARRAY_SIZE(xsd_errors) - 1) {
113 printk(KERN_WARNING
114 "XENBUS xen store gave: unknown error %s",
115 errorstring);
116 return EINVAL;
117 }
118 }
119 return xsd_errors[i].errnum;
120 }
122 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
123 {
124 struct xs_stored_msg *msg;
125 char *body;
127 spin_lock(&xs_state.reply_lock);
129 while (list_empty(&xs_state.reply_list)) {
130 spin_unlock(&xs_state.reply_lock);
131 /* XXX FIXME: Avoid synchronous wait for response here. */
132 wait_event(xs_state.reply_waitq,
133 !list_empty(&xs_state.reply_list));
134 spin_lock(&xs_state.reply_lock);
135 }
137 msg = list_entry(xs_state.reply_list.next,
138 struct xs_stored_msg, list);
139 list_del(&msg->list);
141 spin_unlock(&xs_state.reply_lock);
143 *type = msg->hdr.type;
144 if (len)
145 *len = msg->hdr.len;
146 body = msg->u.reply.body;
148 kfree(msg);
150 return body;
151 }
153 /* Emergency write. */
154 void xenbus_debug_write(const char *str, unsigned int count)
155 {
156 struct xsd_sockmsg msg = { 0 };
158 msg.type = XS_DEBUG;
159 msg.len = sizeof("print") + count + 1;
161 mutex_lock(&xs_state.request_mutex);
162 xb_write(&msg, sizeof(msg));
163 xb_write("print", sizeof("print"));
164 xb_write(str, count);
165 xb_write("", 1);
166 mutex_unlock(&xs_state.request_mutex);
167 }
169 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
170 {
171 void *ret;
172 struct xsd_sockmsg req_msg = *msg;
173 int err;
175 if (req_msg.type == XS_TRANSACTION_START)
176 down_read(&xs_state.suspend_mutex);
178 mutex_lock(&xs_state.request_mutex);
180 err = xb_write(msg, sizeof(*msg) + msg->len);
181 if (err) {
182 msg->type = XS_ERROR;
183 ret = ERR_PTR(err);
184 } else
185 ret = read_reply(&msg->type, &msg->len);
187 mutex_unlock(&xs_state.request_mutex);
189 if ((req_msg.type == XS_TRANSACTION_END) ||
190 ((req_msg.type == XS_TRANSACTION_START) &&
191 (msg->type == XS_ERROR)))
192 up_read(&xs_state.suspend_mutex);
194 return ret;
195 }
197 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
198 static void *xs_talkv(struct xenbus_transaction t,
199 enum xsd_sockmsg_type type,
200 const struct kvec *iovec,
201 unsigned int num_vecs,
202 unsigned int *len)
203 {
204 struct xsd_sockmsg msg;
205 void *ret = NULL;
206 unsigned int i;
207 int err;
209 msg.tx_id = t.id;
210 msg.req_id = 0;
211 msg.type = type;
212 msg.len = 0;
213 for (i = 0; i < num_vecs; i++)
214 msg.len += iovec[i].iov_len;
216 mutex_lock(&xs_state.request_mutex);
218 err = xb_write(&msg, sizeof(msg));
219 if (err) {
220 mutex_unlock(&xs_state.request_mutex);
221 return ERR_PTR(err);
222 }
224 for (i = 0; i < num_vecs; i++) {
225 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);;
226 if (err) {
227 mutex_unlock(&xs_state.request_mutex);
228 return ERR_PTR(err);
229 }
230 }
232 ret = read_reply(&msg.type, len);
234 mutex_unlock(&xs_state.request_mutex);
236 if (IS_ERR(ret))
237 return ret;
239 if (msg.type == XS_ERROR) {
240 err = get_error(ret);
241 kfree(ret);
242 return ERR_PTR(-err);
243 }
245 if (msg.type != type) {
246 if (printk_ratelimit())
247 printk(KERN_WARNING
248 "XENBUS unexpected type [%d], expected [%d]\n",
249 msg.type, type);
250 kfree(ret);
251 return ERR_PTR(-EINVAL);
252 }
253 return ret;
254 }
256 /* Simplified version of xs_talkv: single message. */
257 static void *xs_single(struct xenbus_transaction t,
258 enum xsd_sockmsg_type type,
259 const char *string,
260 unsigned int *len)
261 {
262 struct kvec iovec;
264 iovec.iov_base = (void *)string;
265 iovec.iov_len = strlen(string) + 1;
266 return xs_talkv(t, type, &iovec, 1, len);
267 }
269 /* Many commands only need an ack, don't care what it says. */
270 static int xs_error(char *reply)
271 {
272 if (IS_ERR(reply))
273 return PTR_ERR(reply);
274 kfree(reply);
275 return 0;
276 }
278 static unsigned int count_strings(const char *strings, unsigned int len)
279 {
280 unsigned int num;
281 const char *p;
283 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
284 num++;
286 return num;
287 }
289 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
290 static char *join(const char *dir, const char *name)
291 {
292 char *buffer;
294 if (strlen(name) == 0)
295 buffer = kasprintf(GFP_KERNEL, "%s", dir);
296 else
297 buffer = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
298 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
299 }
301 static char **split(char *strings, unsigned int len, unsigned int *num)
302 {
303 char *p, **ret;
305 /* Count the strings. */
306 *num = count_strings(strings, len);
308 /* Transfer to one big alloc for easy freeing. */
309 ret = kmalloc(*num * sizeof(char *) + len, GFP_KERNEL);
310 if (!ret) {
311 kfree(strings);
312 return ERR_PTR(-ENOMEM);
313 }
314 memcpy(&ret[*num], strings, len);
315 kfree(strings);
317 strings = (char *)&ret[*num];
318 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
319 ret[(*num)++] = p;
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.suspend_mutex);
445 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
446 if (IS_ERR(id_str)) {
447 up_read(&xs_state.suspend_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.suspend_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_KERNEL);
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.suspend_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.suspend_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 sprintf(token, "%lX", (long)watch);
635 down_read(&xs_state.suspend_mutex);
637 spin_lock(&watches_lock);
638 BUG_ON(!find_watch(token));
639 list_del(&watch->list);
640 spin_unlock(&watches_lock);
642 err = xs_unwatch(watch->node, token);
643 if (err)
644 printk(KERN_WARNING
645 "XENBUS Failed to release watch %s: %i\n",
646 watch->node, err);
648 up_read(&xs_state.suspend_mutex);
650 /* Cancel pending watch events. */
651 spin_lock(&watch_events_lock);
652 list_for_each_entry_safe(msg, tmp, &watch_events, list) {
653 if (msg->u.watch.handle != watch)
654 continue;
655 list_del(&msg->list);
656 kfree(msg->u.watch.vec);
657 kfree(msg);
658 }
659 spin_unlock(&watch_events_lock);
661 /* Flush any currently-executing callback, unless we are it. :-) */
662 if (current->pid != xenwatch_pid) {
663 mutex_lock(&xenwatch_mutex);
664 mutex_unlock(&xenwatch_mutex);
665 }
666 }
667 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
669 void xs_suspend(void)
670 {
671 down_write(&xs_state.suspend_mutex);
672 mutex_lock(&xs_state.request_mutex);
673 }
675 void xs_resume(void)
676 {
677 struct xenbus_watch *watch;
678 char token[sizeof(watch) * 2 + 1];
680 mutex_unlock(&xs_state.request_mutex);
682 /* No need for watches_lock: the suspend_mutex is sufficient. */
683 list_for_each_entry(watch, &watches, list) {
684 sprintf(token, "%lX", (long)watch);
685 xs_watch(watch->node, token);
686 }
688 up_write(&xs_state.suspend_mutex);
689 }
691 void xs_suspend_cancel(void)
692 {
693 mutex_unlock(&xs_state.request_mutex);
694 up_write(&xs_state.suspend_mutex);
695 }
697 static int xenwatch_handle_callback(void *data)
698 {
699 struct xs_stored_msg *msg = data;
701 msg->u.watch.handle->callback(msg->u.watch.handle,
702 (const char **)msg->u.watch.vec,
703 msg->u.watch.vec_size);
705 kfree(msg->u.watch.vec);
706 kfree(msg);
708 /* Kill this kthread if we were spawned just for this callback. */
709 if (current->pid != xenwatch_pid)
710 do_exit(0);
712 return 0;
713 }
715 static int xenwatch_thread(void *unused)
716 {
717 struct list_head *ent;
718 struct xs_stored_msg *msg;
720 for (;;) {
721 wait_event_interruptible(watch_events_waitq,
722 !list_empty(&watch_events));
724 if (kthread_should_stop())
725 break;
727 mutex_lock(&xenwatch_mutex);
729 spin_lock(&watch_events_lock);
730 ent = watch_events.next;
731 if (ent != &watch_events)
732 list_del(ent);
733 spin_unlock(&watch_events_lock);
735 if (ent != &watch_events) {
736 msg = list_entry(ent, struct xs_stored_msg, list);
737 if (msg->u.watch.handle->flags & XBWF_new_thread)
738 kthread_run(xenwatch_handle_callback,
739 msg, "xenwatch_cb");
740 else
741 xenwatch_handle_callback(msg);
742 }
744 mutex_unlock(&xenwatch_mutex);
745 }
747 return 0;
748 }
750 static int process_msg(void)
751 {
752 struct xs_stored_msg *msg;
753 char *body;
754 int err;
756 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
757 if (msg == NULL)
758 return -ENOMEM;
760 err = xb_read(&msg->hdr, sizeof(msg->hdr));
761 if (err) {
762 kfree(msg);
763 return err;
764 }
766 body = kmalloc(msg->hdr.len + 1, GFP_KERNEL);
767 if (body == NULL) {
768 kfree(msg);
769 return -ENOMEM;
770 }
772 err = xb_read(body, msg->hdr.len);
773 if (err) {
774 kfree(body);
775 kfree(msg);
776 return err;
777 }
778 body[msg->hdr.len] = '\0';
780 if (msg->hdr.type == XS_WATCH_EVENT) {
781 msg->u.watch.vec = split(body, msg->hdr.len,
782 &msg->u.watch.vec_size);
783 if (IS_ERR(msg->u.watch.vec)) {
784 kfree(msg);
785 return PTR_ERR(msg->u.watch.vec);
786 }
788 spin_lock(&watches_lock);
789 msg->u.watch.handle = find_watch(
790 msg->u.watch.vec[XS_WATCH_TOKEN]);
791 if (msg->u.watch.handle != NULL) {
792 spin_lock(&watch_events_lock);
793 list_add_tail(&msg->list, &watch_events);
794 wake_up(&watch_events_waitq);
795 spin_unlock(&watch_events_lock);
796 } else {
797 kfree(msg->u.watch.vec);
798 kfree(msg);
799 }
800 spin_unlock(&watches_lock);
801 } else {
802 msg->u.reply.body = body;
803 spin_lock(&xs_state.reply_lock);
804 list_add_tail(&msg->list, &xs_state.reply_list);
805 spin_unlock(&xs_state.reply_lock);
806 wake_up(&xs_state.reply_waitq);
807 }
809 return 0;
810 }
812 static int xenbus_thread(void *unused)
813 {
814 int err;
816 for (;;) {
817 err = process_msg();
818 if (err)
819 printk(KERN_WARNING "XENBUS error %d while reading "
820 "message\n", err);
821 if (kthread_should_stop())
822 break;
823 }
825 return 0;
826 }
828 int xs_init(void)
829 {
830 int err;
831 struct task_struct *task;
833 INIT_LIST_HEAD(&xs_state.reply_list);
834 spin_lock_init(&xs_state.reply_lock);
835 init_waitqueue_head(&xs_state.reply_waitq);
837 mutex_init(&xs_state.request_mutex);
838 init_rwsem(&xs_state.suspend_mutex);
840 /* Initialize the shared memory rings to talk to xenstored */
841 err = xb_init_comms();
842 if (err)
843 return err;
845 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
846 if (IS_ERR(task))
847 return PTR_ERR(task);
848 xenwatch_pid = task->pid;
850 task = kthread_run(xenbus_thread, NULL, "xenbus");
851 if (IS_ERR(task))
852 return PTR_ERR(task);
854 return 0;
855 }