1 /******************************************************************************
4 * This is the kernel equivalent of the "xs" library. We don't need everything
5 * and we use xenbus_comms for communication.
7 * Copyright (C) 2005 Rusty Russell, IBM Corporation
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:
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:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
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
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 <xen/xenbus.h>
46 #include "xenbus_comms.h"
48 struct xs_stored_msg {
49 struct list_head list;
51 struct xsd_sockmsg hdr;
59 /* Queued watch events. */
61 struct xenbus_watch *handle;
63 unsigned int vec_size;
69 /* A list of replies. Currently only one will ever be outstanding. */
70 struct list_head reply_list;
71 spinlock_t reply_lock;
72 wait_queue_head_t reply_waitq;
74 /* One request at a time. */
75 struct mutex request_mutex;
77 /* Protect transactions against save/restore. */
78 struct rw_semaphore suspend_mutex;
81 static struct xs_handle xs_state;
83 /* List of registered watches, and a lock to protect it. */
84 static LIST_HEAD(watches);
85 static DEFINE_SPINLOCK(watches_lock);
87 /* List of pending watch callback events, and a lock to protect it. */
88 static LIST_HEAD(watch_events);
89 static DEFINE_SPINLOCK(watch_events_lock);
92 * Details of the xenwatch callback kernel thread. The thread waits on the
93 * watch_events_waitq for work to do (queued on watch_events list). When it
94 * wakes up it acquires the xenwatch_mutex before reading the list and
97 static pid_t xenwatch_pid;
98 /* static */ DEFINE_MUTEX(xenwatch_mutex);
99 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
101 static int get_error(const char *errorstring)
105 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
106 if (i == ARRAY_SIZE(xsd_errors) - 1) {
108 "XENBUS xen store gave: unknown error %s",
113 return xsd_errors[i].errnum;
116 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
118 struct xs_stored_msg *msg;
121 spin_lock(&xs_state.reply_lock);
123 while (list_empty(&xs_state.reply_list)) {
124 spin_unlock(&xs_state.reply_lock);
125 /* XXX FIXME: Avoid synchronous wait for response here. */
126 wait_event(xs_state.reply_waitq,
127 !list_empty(&xs_state.reply_list));
128 spin_lock(&xs_state.reply_lock);
131 msg = list_entry(xs_state.reply_list.next,
132 struct xs_stored_msg, list);
133 list_del(&msg->list);
135 spin_unlock(&xs_state.reply_lock);
137 *type = msg->hdr.type;
140 body = msg->u.reply.body;
147 /* Emergency write. */
148 void xenbus_debug_write(const char *str, unsigned int count)
150 struct xsd_sockmsg msg = { 0 };
153 msg.len = sizeof("print") + count + 1;
155 mutex_lock(&xs_state.request_mutex);
156 xb_write(&msg, sizeof(msg));
157 xb_write("print", sizeof("print"));
158 xb_write(str, count);
160 mutex_unlock(&xs_state.request_mutex);
163 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
166 struct xsd_sockmsg req_msg = *msg;
169 if (req_msg.type == XS_TRANSACTION_START)
170 down_read(&xs_state.suspend_mutex);
172 mutex_lock(&xs_state.request_mutex);
174 err = xb_write(msg, sizeof(*msg) + msg->len);
176 msg->type = XS_ERROR;
179 ret = read_reply(&msg->type, &msg->len);
181 mutex_unlock(&xs_state.request_mutex);
183 if ((req_msg.type == XS_TRANSACTION_END) ||
184 ((req_msg.type == XS_TRANSACTION_START) &&
185 (msg->type == XS_ERROR)))
186 up_read(&xs_state.suspend_mutex);
191 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
192 static void *xs_talkv(struct xenbus_transaction t,
193 enum xsd_sockmsg_type type,
194 const struct kvec *iovec,
195 unsigned int num_vecs,
198 struct xsd_sockmsg msg;
207 for (i = 0; i < num_vecs; i++)
208 msg.len += iovec[i].iov_len;
210 mutex_lock(&xs_state.request_mutex);
212 err = xb_write(&msg, sizeof(msg));
214 mutex_unlock(&xs_state.request_mutex);
218 for (i = 0; i < num_vecs; i++) {
219 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);;
221 mutex_unlock(&xs_state.request_mutex);
226 ret = read_reply(&msg.type, len);
228 mutex_unlock(&xs_state.request_mutex);
233 if (msg.type == XS_ERROR) {
234 err = get_error(ret);
236 return ERR_PTR(-err);
239 if (msg.type != type) {
240 if (printk_ratelimit())
242 "XENBUS unexpected type [%d], expected [%d]\n",
245 return ERR_PTR(-EINVAL);
250 /* Simplified version of xs_talkv: single message. */
251 static void *xs_single(struct xenbus_transaction t,
252 enum xsd_sockmsg_type type,
258 iovec.iov_base = (void *)string;
259 iovec.iov_len = strlen(string) + 1;
260 return xs_talkv(t, type, &iovec, 1, len);
263 /* Many commands only need an ack, don't care what it says. */
264 static int xs_error(char *reply)
267 return PTR_ERR(reply);
272 static unsigned int count_strings(const char *strings, unsigned int len)
277 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
283 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
284 static char *join(const char *dir, const char *name)
288 if (strlen(name) == 0)
289 buffer = kasprintf(GFP_KERNEL, "%s", dir);
291 buffer = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
292 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
295 static char **split(char *strings, unsigned int len, unsigned int *num)
299 /* Count the strings. */
300 *num = count_strings(strings, len);
302 /* Transfer to one big alloc for easy freeing. */
303 ret = kmalloc(*num * sizeof(char *) + len, GFP_KERNEL);
306 return ERR_PTR(-ENOMEM);
308 memcpy(&ret[*num], strings, len);
311 strings = (char *)&ret[*num];
312 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
318 char **xenbus_directory(struct xenbus_transaction t,
319 const char *dir, const char *node, unsigned int *num)
321 char *strings, *path;
324 path = join(dir, node);
326 return (char **)path;
328 strings = xs_single(t, XS_DIRECTORY, path, &len);
331 return (char **)strings;
333 return split(strings, len, num);
335 EXPORT_SYMBOL_GPL(xenbus_directory);
337 /* Check if a path exists. Return 1 if it does. */
338 int xenbus_exists(struct xenbus_transaction t,
339 const char *dir, const char *node)
344 d = xenbus_directory(t, dir, node, &dir_n);
350 EXPORT_SYMBOL_GPL(xenbus_exists);
352 /* Get the value of a single file.
353 * Returns a kmalloced value: call free() on it after use.
354 * len indicates length in bytes.
356 void *xenbus_read(struct xenbus_transaction t,
357 const char *dir, const char *node, unsigned int *len)
362 path = join(dir, node);
366 ret = xs_single(t, XS_READ, path, len);
370 EXPORT_SYMBOL_GPL(xenbus_read);
372 /* Write the value of a single file.
373 * Returns -err on failure.
375 int xenbus_write(struct xenbus_transaction t,
376 const char *dir, const char *node, const char *string)
379 struct kvec iovec[2];
382 path = join(dir, node);
384 return PTR_ERR(path);
386 iovec[0].iov_base = (void *)path;
387 iovec[0].iov_len = strlen(path) + 1;
388 iovec[1].iov_base = (void *)string;
389 iovec[1].iov_len = strlen(string);
391 ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
395 EXPORT_SYMBOL_GPL(xenbus_write);
397 /* Create a new directory. */
398 int xenbus_mkdir(struct xenbus_transaction t,
399 const char *dir, const char *node)
404 path = join(dir, node);
406 return PTR_ERR(path);
408 ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
412 EXPORT_SYMBOL_GPL(xenbus_mkdir);
414 /* Destroy a file or directory (directories must be empty). */
415 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
420 path = join(dir, node);
422 return PTR_ERR(path);
424 ret = xs_error(xs_single(t, XS_RM, path, NULL));
428 EXPORT_SYMBOL_GPL(xenbus_rm);
430 /* Start a transaction: changes by others will not be seen during this
431 * transaction, and changes will not be visible to others until end.
433 int xenbus_transaction_start(struct xenbus_transaction *t)
437 down_read(&xs_state.suspend_mutex);
439 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
440 if (IS_ERR(id_str)) {
441 up_read(&xs_state.suspend_mutex);
442 return PTR_ERR(id_str);
445 t->id = simple_strtoul(id_str, NULL, 0);
449 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
451 /* End a transaction.
452 * If abandon is true, transaction is discarded instead of committed.
454 int xenbus_transaction_end(struct xenbus_transaction t, int abort)
460 strcpy(abortstr, "F");
462 strcpy(abortstr, "T");
464 err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
466 up_read(&xs_state.suspend_mutex);
470 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
472 /* Single read and scanf: returns -errno or num scanned. */
473 int xenbus_scanf(struct xenbus_transaction t,
474 const char *dir, const char *node, const char *fmt, ...)
480 val = xenbus_read(t, dir, node, NULL);
485 ret = vsscanf(val, fmt, ap);
488 /* Distinctive errno. */
493 EXPORT_SYMBOL_GPL(xenbus_scanf);
495 /* Single printf and write: returns -errno or 0. */
496 int xenbus_printf(struct xenbus_transaction t,
497 const char *dir, const char *node, const char *fmt, ...)
501 #define PRINTF_BUFFER_SIZE 4096
504 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
505 if (printf_buffer == NULL)
509 ret = vsnprintf(printf_buffer, PRINTF_BUFFER_SIZE, fmt, ap);
512 BUG_ON(ret > PRINTF_BUFFER_SIZE-1);
513 ret = xenbus_write(t, dir, node, printf_buffer);
515 kfree(printf_buffer);
519 EXPORT_SYMBOL_GPL(xenbus_printf);
521 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
522 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
529 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
530 const char *fmt = va_arg(ap, char *);
531 void *result = va_arg(ap, void *);
534 p = xenbus_read(t, dir, name, NULL);
540 if (sscanf(p, fmt, result) == 0)
544 *(char **)result = p;
549 EXPORT_SYMBOL_GPL(xenbus_gather);
551 static int xs_watch(const char *path, const char *token)
555 iov[0].iov_base = (void *)path;
556 iov[0].iov_len = strlen(path) + 1;
557 iov[1].iov_base = (void *)token;
558 iov[1].iov_len = strlen(token) + 1;
560 return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
561 ARRAY_SIZE(iov), NULL));
564 static int xs_unwatch(const char *path, const char *token)
568 iov[0].iov_base = (char *)path;
569 iov[0].iov_len = strlen(path) + 1;
570 iov[1].iov_base = (char *)token;
571 iov[1].iov_len = strlen(token) + 1;
573 return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
574 ARRAY_SIZE(iov), NULL));
577 static struct xenbus_watch *find_watch(const char *token)
579 struct xenbus_watch *i, *cmp;
581 cmp = (void *)simple_strtoul(token, NULL, 16);
583 list_for_each_entry(i, &watches, list)
590 /* Register callback to watch this node. */
591 int register_xenbus_watch(struct xenbus_watch *watch)
593 /* Pointer in ascii is the token. */
594 char token[sizeof(watch) * 2 + 1];
597 sprintf(token, "%lX", (long)watch);
599 down_read(&xs_state.suspend_mutex);
601 spin_lock(&watches_lock);
602 BUG_ON(find_watch(token));
603 list_add(&watch->list, &watches);
604 spin_unlock(&watches_lock);
606 err = xs_watch(watch->node, token);
608 /* Ignore errors due to multiple registration. */
609 if ((err != 0) && (err != -EEXIST)) {
610 spin_lock(&watches_lock);
611 list_del(&watch->list);
612 spin_unlock(&watches_lock);
615 up_read(&xs_state.suspend_mutex);
619 EXPORT_SYMBOL_GPL(register_xenbus_watch);
621 void unregister_xenbus_watch(struct xenbus_watch *watch)
623 struct xs_stored_msg *msg, *tmp;
624 char token[sizeof(watch) * 2 + 1];
627 sprintf(token, "%lX", (long)watch);
629 down_read(&xs_state.suspend_mutex);
631 spin_lock(&watches_lock);
632 BUG_ON(!find_watch(token));
633 list_del(&watch->list);
634 spin_unlock(&watches_lock);
636 err = xs_unwatch(watch->node, token);
639 "XENBUS Failed to release watch %s: %i\n",
642 up_read(&xs_state.suspend_mutex);
644 /* Cancel pending watch events. */
645 spin_lock(&watch_events_lock);
646 list_for_each_entry_safe(msg, tmp, &watch_events, list) {
647 if (msg->u.watch.handle != watch)
649 list_del(&msg->list);
650 kfree(msg->u.watch.vec);
653 spin_unlock(&watch_events_lock);
655 /* Flush any currently-executing callback, unless we are it. :-) */
656 if (current->pid != xenwatch_pid) {
657 mutex_lock(&xenwatch_mutex);
658 mutex_unlock(&xenwatch_mutex);
661 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
663 void xs_suspend(void)
665 struct xenbus_watch *watch;
666 char token[sizeof(watch) * 2 + 1];
668 down_write(&xs_state.suspend_mutex);
670 /* No need for watches_lock: the suspend_mutex is sufficient. */
671 list_for_each_entry(watch, &watches, list) {
672 sprintf(token, "%lX", (long)watch);
673 xs_unwatch(watch->node, token);
676 mutex_lock(&xs_state.request_mutex);
681 struct xenbus_watch *watch;
682 char token[sizeof(watch) * 2 + 1];
684 mutex_unlock(&xs_state.request_mutex);
686 /* No need for watches_lock: the suspend_mutex is sufficient. */
687 list_for_each_entry(watch, &watches, list) {
688 sprintf(token, "%lX", (long)watch);
689 xs_watch(watch->node, token);
692 up_write(&xs_state.suspend_mutex);
695 static int xenwatch_handle_callback(void *data)
697 struct xs_stored_msg *msg = data;
699 msg->u.watch.handle->callback(msg->u.watch.handle,
700 (const char **)msg->u.watch.vec,
701 msg->u.watch.vec_size);
703 kfree(msg->u.watch.vec);
706 /* Kill this kthread if we were spawned just for this callback. */
707 if (current->pid != xenwatch_pid)
713 static int xenwatch_thread(void *unused)
715 struct list_head *ent;
716 struct xs_stored_msg *msg;
719 wait_event_interruptible(watch_events_waitq,
720 !list_empty(&watch_events));
722 if (kthread_should_stop())
725 mutex_lock(&xenwatch_mutex);
727 spin_lock(&watch_events_lock);
728 ent = watch_events.next;
729 if (ent != &watch_events)
731 spin_unlock(&watch_events_lock);
733 if (ent != &watch_events) {
734 msg = list_entry(ent, struct xs_stored_msg, list);
735 if (msg->u.watch.handle->flags & XBWF_new_thread)
736 kthread_run(xenwatch_handle_callback,
739 xenwatch_handle_callback(msg);
742 mutex_unlock(&xenwatch_mutex);
748 static int process_msg(void)
750 struct xs_stored_msg *msg;
754 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
758 err = xb_read(&msg->hdr, sizeof(msg->hdr));
764 body = kmalloc(msg->hdr.len + 1, GFP_KERNEL);
770 err = xb_read(body, msg->hdr.len);
776 body[msg->hdr.len] = '\0';
778 if (msg->hdr.type == XS_WATCH_EVENT) {
779 msg->u.watch.vec = split(body, msg->hdr.len,
780 &msg->u.watch.vec_size);
781 if (IS_ERR(msg->u.watch.vec)) {
783 return PTR_ERR(msg->u.watch.vec);
786 spin_lock(&watches_lock);
787 msg->u.watch.handle = find_watch(
788 msg->u.watch.vec[XS_WATCH_TOKEN]);
789 if (msg->u.watch.handle != NULL) {
790 spin_lock(&watch_events_lock);
791 list_add_tail(&msg->list, &watch_events);
792 wake_up(&watch_events_waitq);
793 spin_unlock(&watch_events_lock);
795 kfree(msg->u.watch.vec);
798 spin_unlock(&watches_lock);
800 msg->u.reply.body = body;
801 spin_lock(&xs_state.reply_lock);
802 list_add_tail(&msg->list, &xs_state.reply_list);
803 spin_unlock(&xs_state.reply_lock);
804 wake_up(&xs_state.reply_waitq);
810 static int xenbus_thread(void *unused)
817 printk(KERN_WARNING "XENBUS error %d while reading "
819 if (kthread_should_stop())
829 struct task_struct *task;
831 INIT_LIST_HEAD(&xs_state.reply_list);
832 spin_lock_init(&xs_state.reply_lock);
833 init_waitqueue_head(&xs_state.reply_waitq);
835 mutex_init(&xs_state.request_mutex);
836 init_rwsem(&xs_state.suspend_mutex);
838 /* Initialize the shared memory rings to talk to xenstored */
839 err = xb_init_comms();
843 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
845 return PTR_ERR(task);
846 xenwatch_pid = task->pid;
848 task = kthread_run(xenbus_thread, NULL, "xenbus");
850 return PTR_ERR(task);