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 <linux/module.h>
46 #include <linux/mutex.h>
47 #include <xen/xenbus.h>
48 #include "xenbus_comms.h"
50 struct xs_stored_msg
{
51 struct list_head list
;
53 struct xsd_sockmsg hdr
;
61 /* Queued watch events. */
63 struct xenbus_watch
*handle
;
65 unsigned int vec_size
;
71 /* A list of replies. Currently only one will ever be outstanding. */
72 struct list_head reply_list
;
73 spinlock_t reply_lock
;
74 wait_queue_head_t reply_waitq
;
77 * Mutex ordering: transaction_mutex -> watch_mutex -> request_mutex.
78 * response_mutex is never taken simultaneously with the other three.
81 /* One request at a time. */
82 struct mutex request_mutex
;
84 /* Protect xenbus reader thread against save/restore. */
85 struct mutex response_mutex
;
87 /* Protect transactions against save/restore. */
88 struct rw_semaphore transaction_mutex
;
90 /* Protect watch (de)register against save/restore. */
91 struct rw_semaphore watch_mutex
;
94 static struct xs_handle xs_state
;
96 /* List of registered watches, and a lock to protect it. */
97 static LIST_HEAD(watches
);
98 static DEFINE_SPINLOCK(watches_lock
);
100 /* List of pending watch callback events, and a lock to protect it. */
101 static LIST_HEAD(watch_events
);
102 static DEFINE_SPINLOCK(watch_events_lock
);
105 * Details of the xenwatch callback kernel thread. The thread waits on the
106 * watch_events_waitq for work to do (queued on watch_events list). When it
107 * wakes up it acquires the xenwatch_mutex before reading the list and
110 static pid_t xenwatch_pid
;
111 static DEFINE_MUTEX(xenwatch_mutex
);
112 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq
);
114 static int get_error(const char *errorstring
)
118 for (i
= 0; strcmp(errorstring
, xsd_errors
[i
].errstring
) != 0; i
++) {
119 if (i
== ARRAY_SIZE(xsd_errors
) - 1) {
121 "XENBUS xen store gave: unknown error %s",
126 return xsd_errors
[i
].errnum
;
129 static void *read_reply(enum xsd_sockmsg_type
*type
, unsigned int *len
)
131 struct xs_stored_msg
*msg
;
134 spin_lock(&xs_state
.reply_lock
);
136 while (list_empty(&xs_state
.reply_list
)) {
137 spin_unlock(&xs_state
.reply_lock
);
138 /* XXX FIXME: Avoid synchronous wait for response here. */
139 wait_event(xs_state
.reply_waitq
,
140 !list_empty(&xs_state
.reply_list
));
141 spin_lock(&xs_state
.reply_lock
);
144 msg
= list_entry(xs_state
.reply_list
.next
,
145 struct xs_stored_msg
, list
);
146 list_del(&msg
->list
);
148 spin_unlock(&xs_state
.reply_lock
);
150 *type
= msg
->hdr
.type
;
153 body
= msg
->u
.reply
.body
;
160 void *xenbus_dev_request_and_reply(struct xsd_sockmsg
*msg
)
163 struct xsd_sockmsg req_msg
= *msg
;
166 if (req_msg
.type
== XS_TRANSACTION_START
)
167 down_read(&xs_state
.transaction_mutex
);
169 mutex_lock(&xs_state
.request_mutex
);
171 err
= xb_write(msg
, sizeof(*msg
) + msg
->len
);
173 msg
->type
= XS_ERROR
;
176 ret
= read_reply(&msg
->type
, &msg
->len
);
178 mutex_unlock(&xs_state
.request_mutex
);
180 if ((msg
->type
== XS_TRANSACTION_END
) ||
181 ((req_msg
.type
== XS_TRANSACTION_START
) &&
182 (msg
->type
== XS_ERROR
)))
183 up_read(&xs_state
.transaction_mutex
);
188 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
189 static void *xs_talkv(struct xenbus_transaction t
,
190 enum xsd_sockmsg_type type
,
191 const struct kvec
*iovec
,
192 unsigned int num_vecs
,
195 struct xsd_sockmsg msg
;
204 for (i
= 0; i
< num_vecs
; i
++)
205 msg
.len
+= iovec
[i
].iov_len
;
207 mutex_lock(&xs_state
.request_mutex
);
209 err
= xb_write(&msg
, sizeof(msg
));
211 mutex_unlock(&xs_state
.request_mutex
);
215 for (i
= 0; i
< num_vecs
; i
++) {
216 err
= xb_write(iovec
[i
].iov_base
, iovec
[i
].iov_len
);
218 mutex_unlock(&xs_state
.request_mutex
);
223 ret
= read_reply(&msg
.type
, len
);
225 mutex_unlock(&xs_state
.request_mutex
);
230 if (msg
.type
== XS_ERROR
) {
231 err
= get_error(ret
);
233 return ERR_PTR(-err
);
236 if (msg
.type
!= type
) {
237 if (printk_ratelimit())
239 "XENBUS unexpected type [%d], expected [%d]\n",
242 return ERR_PTR(-EINVAL
);
247 /* Simplified version of xs_talkv: single message. */
248 static void *xs_single(struct xenbus_transaction t
,
249 enum xsd_sockmsg_type type
,
255 iovec
.iov_base
= (void *)string
;
256 iovec
.iov_len
= strlen(string
) + 1;
257 return xs_talkv(t
, type
, &iovec
, 1, len
);
260 /* Many commands only need an ack, don't care what it says. */
261 static int xs_error(char *reply
)
264 return PTR_ERR(reply
);
269 static unsigned int count_strings(const char *strings
, unsigned int len
)
274 for (p
= strings
, num
= 0; p
< strings
+ len
; p
+= strlen(p
) + 1)
280 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
281 static char *join(const char *dir
, const char *name
)
285 if (strlen(name
) == 0)
286 buffer
= kasprintf(GFP_KERNEL
, "%s", dir
);
288 buffer
= kasprintf(GFP_KERNEL
, "%s/%s", dir
, name
);
289 return (!buffer
) ? ERR_PTR(-ENOMEM
) : buffer
;
292 static char **split(char *strings
, unsigned int len
, unsigned int *num
)
296 /* Count the strings. */
297 *num
= count_strings(strings
, len
);
299 /* Transfer to one big alloc for easy freeing. */
300 ret
= kmalloc(*num
* sizeof(char *) + len
, GFP_KERNEL
);
303 return ERR_PTR(-ENOMEM
);
305 memcpy(&ret
[*num
], strings
, len
);
308 strings
= (char *)&ret
[*num
];
309 for (p
= strings
, *num
= 0; p
< strings
+ len
; p
+= strlen(p
) + 1)
315 char **xenbus_directory(struct xenbus_transaction t
,
316 const char *dir
, const char *node
, unsigned int *num
)
318 char *strings
, *path
;
321 path
= join(dir
, node
);
323 return (char **)path
;
325 strings
= xs_single(t
, XS_DIRECTORY
, path
, &len
);
328 return (char **)strings
;
330 return split(strings
, len
, num
);
332 EXPORT_SYMBOL_GPL(xenbus_directory
);
334 /* Check if a path exists. Return 1 if it does. */
335 int xenbus_exists(struct xenbus_transaction t
,
336 const char *dir
, const char *node
)
341 d
= xenbus_directory(t
, dir
, node
, &dir_n
);
347 EXPORT_SYMBOL_GPL(xenbus_exists
);
349 /* Get the value of a single file.
350 * Returns a kmalloced value: call free() on it after use.
351 * len indicates length in bytes.
353 void *xenbus_read(struct xenbus_transaction t
,
354 const char *dir
, const char *node
, unsigned int *len
)
359 path
= join(dir
, node
);
363 ret
= xs_single(t
, XS_READ
, path
, len
);
367 EXPORT_SYMBOL_GPL(xenbus_read
);
369 /* Write the value of a single file.
370 * Returns -err on failure.
372 int xenbus_write(struct xenbus_transaction t
,
373 const char *dir
, const char *node
, const char *string
)
376 struct kvec iovec
[2];
379 path
= join(dir
, node
);
381 return PTR_ERR(path
);
383 iovec
[0].iov_base
= (void *)path
;
384 iovec
[0].iov_len
= strlen(path
) + 1;
385 iovec
[1].iov_base
= (void *)string
;
386 iovec
[1].iov_len
= strlen(string
);
388 ret
= xs_error(xs_talkv(t
, XS_WRITE
, iovec
, ARRAY_SIZE(iovec
), NULL
));
392 EXPORT_SYMBOL_GPL(xenbus_write
);
394 /* Create a new directory. */
395 int xenbus_mkdir(struct xenbus_transaction t
,
396 const char *dir
, const char *node
)
401 path
= join(dir
, node
);
403 return PTR_ERR(path
);
405 ret
= xs_error(xs_single(t
, XS_MKDIR
, path
, NULL
));
409 EXPORT_SYMBOL_GPL(xenbus_mkdir
);
411 /* Destroy a file or directory (directories must be empty). */
412 int xenbus_rm(struct xenbus_transaction t
, const char *dir
, const char *node
)
417 path
= join(dir
, node
);
419 return PTR_ERR(path
);
421 ret
= xs_error(xs_single(t
, XS_RM
, path
, NULL
));
425 EXPORT_SYMBOL_GPL(xenbus_rm
);
427 /* Start a transaction: changes by others will not be seen during this
428 * transaction, and changes will not be visible to others until end.
430 int xenbus_transaction_start(struct xenbus_transaction
*t
)
434 down_read(&xs_state
.transaction_mutex
);
436 id_str
= xs_single(XBT_NIL
, XS_TRANSACTION_START
, "", NULL
);
437 if (IS_ERR(id_str
)) {
438 up_read(&xs_state
.transaction_mutex
);
439 return PTR_ERR(id_str
);
442 t
->id
= simple_strtoul(id_str
, NULL
, 0);
446 EXPORT_SYMBOL_GPL(xenbus_transaction_start
);
448 /* End a transaction.
449 * If abandon is true, transaction is discarded instead of committed.
451 int xenbus_transaction_end(struct xenbus_transaction t
, int abort
)
457 strcpy(abortstr
, "F");
459 strcpy(abortstr
, "T");
461 err
= xs_error(xs_single(t
, XS_TRANSACTION_END
, abortstr
, NULL
));
463 up_read(&xs_state
.transaction_mutex
);
467 EXPORT_SYMBOL_GPL(xenbus_transaction_end
);
469 /* Single read and scanf: returns -errno or num scanned. */
470 int xenbus_scanf(struct xenbus_transaction t
,
471 const char *dir
, const char *node
, const char *fmt
, ...)
477 val
= xenbus_read(t
, dir
, node
, NULL
);
482 ret
= vsscanf(val
, fmt
, ap
);
485 /* Distinctive errno. */
490 EXPORT_SYMBOL_GPL(xenbus_scanf
);
492 /* Single printf and write: returns -errno or 0. */
493 int xenbus_printf(struct xenbus_transaction t
,
494 const char *dir
, const char *node
, const char *fmt
, ...)
498 #define PRINTF_BUFFER_SIZE 4096
501 printf_buffer
= kmalloc(PRINTF_BUFFER_SIZE
, GFP_KERNEL
);
502 if (printf_buffer
== NULL
)
506 ret
= vsnprintf(printf_buffer
, PRINTF_BUFFER_SIZE
, fmt
, ap
);
509 BUG_ON(ret
> PRINTF_BUFFER_SIZE
-1);
510 ret
= xenbus_write(t
, dir
, node
, printf_buffer
);
512 kfree(printf_buffer
);
516 EXPORT_SYMBOL_GPL(xenbus_printf
);
518 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
519 int xenbus_gather(struct xenbus_transaction t
, const char *dir
, ...)
526 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
527 const char *fmt
= va_arg(ap
, char *);
528 void *result
= va_arg(ap
, void *);
531 p
= xenbus_read(t
, dir
, name
, NULL
);
537 if (sscanf(p
, fmt
, result
) == 0)
541 *(char **)result
= p
;
546 EXPORT_SYMBOL_GPL(xenbus_gather
);
548 static int xs_watch(const char *path
, const char *token
)
552 iov
[0].iov_base
= (void *)path
;
553 iov
[0].iov_len
= strlen(path
) + 1;
554 iov
[1].iov_base
= (void *)token
;
555 iov
[1].iov_len
= strlen(token
) + 1;
557 return xs_error(xs_talkv(XBT_NIL
, XS_WATCH
, iov
,
558 ARRAY_SIZE(iov
), NULL
));
561 static int xs_unwatch(const char *path
, const char *token
)
565 iov
[0].iov_base
= (char *)path
;
566 iov
[0].iov_len
= strlen(path
) + 1;
567 iov
[1].iov_base
= (char *)token
;
568 iov
[1].iov_len
= strlen(token
) + 1;
570 return xs_error(xs_talkv(XBT_NIL
, XS_UNWATCH
, iov
,
571 ARRAY_SIZE(iov
), NULL
));
574 static struct xenbus_watch
*find_watch(const char *token
)
576 struct xenbus_watch
*i
, *cmp
;
578 cmp
= (void *)simple_strtoul(token
, NULL
, 16);
580 list_for_each_entry(i
, &watches
, list
)
587 /* Register callback to watch this node. */
588 int register_xenbus_watch(struct xenbus_watch
*watch
)
590 /* Pointer in ascii is the token. */
591 char token
[sizeof(watch
) * 2 + 1];
594 sprintf(token
, "%lX", (long)watch
);
596 down_read(&xs_state
.watch_mutex
);
598 spin_lock(&watches_lock
);
599 BUG_ON(find_watch(token
));
600 list_add(&watch
->list
, &watches
);
601 spin_unlock(&watches_lock
);
603 err
= xs_watch(watch
->node
, token
);
605 /* Ignore errors due to multiple registration. */
606 if ((err
!= 0) && (err
!= -EEXIST
)) {
607 spin_lock(&watches_lock
);
608 list_del(&watch
->list
);
609 spin_unlock(&watches_lock
);
612 up_read(&xs_state
.watch_mutex
);
616 EXPORT_SYMBOL_GPL(register_xenbus_watch
);
618 void unregister_xenbus_watch(struct xenbus_watch
*watch
)
620 struct xs_stored_msg
*msg
, *tmp
;
621 char token
[sizeof(watch
) * 2 + 1];
624 sprintf(token
, "%lX", (long)watch
);
626 down_read(&xs_state
.watch_mutex
);
628 spin_lock(&watches_lock
);
629 BUG_ON(!find_watch(token
));
630 list_del(&watch
->list
);
631 spin_unlock(&watches_lock
);
633 err
= xs_unwatch(watch
->node
, token
);
636 "XENBUS Failed to release watch %s: %i\n",
639 up_read(&xs_state
.watch_mutex
);
641 /* Make sure there are no callbacks running currently (unless
643 if (current
->pid
!= xenwatch_pid
)
644 mutex_lock(&xenwatch_mutex
);
646 /* Cancel pending watch events. */
647 spin_lock(&watch_events_lock
);
648 list_for_each_entry_safe(msg
, tmp
, &watch_events
, list
) {
649 if (msg
->u
.watch
.handle
!= watch
)
651 list_del(&msg
->list
);
652 kfree(msg
->u
.watch
.vec
);
655 spin_unlock(&watch_events_lock
);
657 if (current
->pid
!= xenwatch_pid
)
658 mutex_unlock(&xenwatch_mutex
);
660 EXPORT_SYMBOL_GPL(unregister_xenbus_watch
);
662 void xs_suspend(void)
664 down_write(&xs_state
.transaction_mutex
);
665 down_write(&xs_state
.watch_mutex
);
666 mutex_lock(&xs_state
.request_mutex
);
667 mutex_lock(&xs_state
.response_mutex
);
672 struct xenbus_watch
*watch
;
673 char token
[sizeof(watch
) * 2 + 1];
675 mutex_unlock(&xs_state
.response_mutex
);
676 mutex_unlock(&xs_state
.request_mutex
);
677 up_write(&xs_state
.transaction_mutex
);
679 /* No need for watches_lock: the watch_mutex is sufficient. */
680 list_for_each_entry(watch
, &watches
, list
) {
681 sprintf(token
, "%lX", (long)watch
);
682 xs_watch(watch
->node
, token
);
685 up_write(&xs_state
.watch_mutex
);
688 void xs_suspend_cancel(void)
690 mutex_unlock(&xs_state
.response_mutex
);
691 mutex_unlock(&xs_state
.request_mutex
);
692 up_write(&xs_state
.watch_mutex
);
693 up_write(&xs_state
.transaction_mutex
);
696 static int xenwatch_thread(void *unused
)
698 struct list_head
*ent
;
699 struct xs_stored_msg
*msg
;
702 wait_event_interruptible(watch_events_waitq
,
703 !list_empty(&watch_events
));
705 if (kthread_should_stop())
708 mutex_lock(&xenwatch_mutex
);
710 spin_lock(&watch_events_lock
);
711 ent
= watch_events
.next
;
712 if (ent
!= &watch_events
)
714 spin_unlock(&watch_events_lock
);
716 if (ent
!= &watch_events
) {
717 msg
= list_entry(ent
, struct xs_stored_msg
, list
);
718 msg
->u
.watch
.handle
->callback(
720 (const char **)msg
->u
.watch
.vec
,
721 msg
->u
.watch
.vec_size
);
722 kfree(msg
->u
.watch
.vec
);
726 mutex_unlock(&xenwatch_mutex
);
732 static int process_msg(void)
734 struct xs_stored_msg
*msg
;
739 * We must disallow save/restore while reading a xenstore message.
740 * A partial read across s/r leaves us out of sync with xenstored.
743 err
= xb_wait_for_data_to_read();
746 mutex_lock(&xs_state
.response_mutex
);
747 if (xb_data_to_read())
749 /* We raced with save/restore: pending data 'disappeared'. */
750 mutex_unlock(&xs_state
.response_mutex
);
754 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
760 err
= xb_read(&msg
->hdr
, sizeof(msg
->hdr
));
766 body
= kmalloc(msg
->hdr
.len
+ 1, GFP_KERNEL
);
773 err
= xb_read(body
, msg
->hdr
.len
);
779 body
[msg
->hdr
.len
] = '\0';
781 if (msg
->hdr
.type
== XS_WATCH_EVENT
) {
782 msg
->u
.watch
.vec
= split(body
, msg
->hdr
.len
,
783 &msg
->u
.watch
.vec_size
);
784 if (IS_ERR(msg
->u
.watch
.vec
)) {
785 err
= PTR_ERR(msg
->u
.watch
.vec
);
790 spin_lock(&watches_lock
);
791 msg
->u
.watch
.handle
= find_watch(
792 msg
->u
.watch
.vec
[XS_WATCH_TOKEN
]);
793 if (msg
->u
.watch
.handle
!= NULL
) {
794 spin_lock(&watch_events_lock
);
795 list_add_tail(&msg
->list
, &watch_events
);
796 wake_up(&watch_events_waitq
);
797 spin_unlock(&watch_events_lock
);
799 kfree(msg
->u
.watch
.vec
);
802 spin_unlock(&watches_lock
);
804 msg
->u
.reply
.body
= body
;
805 spin_lock(&xs_state
.reply_lock
);
806 list_add_tail(&msg
->list
, &xs_state
.reply_list
);
807 spin_unlock(&xs_state
.reply_lock
);
808 wake_up(&xs_state
.reply_waitq
);
812 mutex_unlock(&xs_state
.response_mutex
);
816 static int xenbus_thread(void *unused
)
823 printk(KERN_WARNING
"XENBUS error %d while reading "
825 if (kthread_should_stop())
835 struct task_struct
*task
;
837 INIT_LIST_HEAD(&xs_state
.reply_list
);
838 spin_lock_init(&xs_state
.reply_lock
);
839 init_waitqueue_head(&xs_state
.reply_waitq
);
841 mutex_init(&xs_state
.request_mutex
);
842 mutex_init(&xs_state
.response_mutex
);
843 init_rwsem(&xs_state
.transaction_mutex
);
844 init_rwsem(&xs_state
.watch_mutex
);
846 /* Initialize the shared memory rings to talk to xenstored */
847 err
= xb_init_comms();
851 task
= kthread_run(xenwatch_thread
, NULL
, "xenwatch");
853 return PTR_ERR(task
);
854 xenwatch_pid
= task
->pid
;
856 task
= kthread_run(xenbus_thread
, NULL
, "xenbus");
858 return PTR_ERR(task
);