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
);
187 EXPORT_SYMBOL(xenbus_dev_request_and_reply
);
189 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
190 static void *xs_talkv(struct xenbus_transaction t
,
191 enum xsd_sockmsg_type type
,
192 const struct kvec
*iovec
,
193 unsigned int num_vecs
,
196 struct xsd_sockmsg msg
;
205 for (i
= 0; i
< num_vecs
; i
++)
206 msg
.len
+= iovec
[i
].iov_len
;
208 mutex_lock(&xs_state
.request_mutex
);
210 err
= xb_write(&msg
, sizeof(msg
));
212 mutex_unlock(&xs_state
.request_mutex
);
216 for (i
= 0; i
< num_vecs
; i
++) {
217 err
= xb_write(iovec
[i
].iov_base
, iovec
[i
].iov_len
);
219 mutex_unlock(&xs_state
.request_mutex
);
224 ret
= read_reply(&msg
.type
, len
);
226 mutex_unlock(&xs_state
.request_mutex
);
231 if (msg
.type
== XS_ERROR
) {
232 err
= get_error(ret
);
234 return ERR_PTR(-err
);
237 if (msg
.type
!= type
) {
238 if (printk_ratelimit())
240 "XENBUS unexpected type [%d], expected [%d]\n",
243 return ERR_PTR(-EINVAL
);
248 /* Simplified version of xs_talkv: single message. */
249 static void *xs_single(struct xenbus_transaction t
,
250 enum xsd_sockmsg_type type
,
256 iovec
.iov_base
= (void *)string
;
257 iovec
.iov_len
= strlen(string
) + 1;
258 return xs_talkv(t
, type
, &iovec
, 1, len
);
261 /* Many commands only need an ack, don't care what it says. */
262 static int xs_error(char *reply
)
265 return PTR_ERR(reply
);
270 static unsigned int count_strings(const char *strings
, unsigned int len
)
275 for (p
= strings
, num
= 0; p
< strings
+ len
; p
+= strlen(p
) + 1)
281 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
282 static char *join(const char *dir
, const char *name
)
286 if (strlen(name
) == 0)
287 buffer
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "%s", dir
);
289 buffer
= kasprintf(GFP_NOIO
| __GFP_HIGH
, "%s/%s", dir
, name
);
290 return (!buffer
) ? ERR_PTR(-ENOMEM
) : buffer
;
293 static char **split(char *strings
, unsigned int len
, unsigned int *num
)
297 /* Count the strings. */
298 *num
= count_strings(strings
, len
);
300 /* Transfer to one big alloc for easy freeing. */
301 ret
= kmalloc(*num
* sizeof(char *) + len
, GFP_NOIO
| __GFP_HIGH
);
304 return ERR_PTR(-ENOMEM
);
306 memcpy(&ret
[*num
], strings
, len
);
309 strings
= (char *)&ret
[*num
];
310 for (p
= strings
, *num
= 0; p
< strings
+ len
; p
+= strlen(p
) + 1)
316 char **xenbus_directory(struct xenbus_transaction t
,
317 const char *dir
, const char *node
, unsigned int *num
)
319 char *strings
, *path
;
322 path
= join(dir
, node
);
324 return (char **)path
;
326 strings
= xs_single(t
, XS_DIRECTORY
, path
, &len
);
329 return (char **)strings
;
331 return split(strings
, len
, num
);
333 EXPORT_SYMBOL_GPL(xenbus_directory
);
335 /* Check if a path exists. Return 1 if it does. */
336 int xenbus_exists(struct xenbus_transaction t
,
337 const char *dir
, const char *node
)
342 d
= xenbus_directory(t
, dir
, node
, &dir_n
);
348 EXPORT_SYMBOL_GPL(xenbus_exists
);
350 /* Get the value of a single file.
351 * Returns a kmalloced value: call free() on it after use.
352 * len indicates length in bytes.
354 void *xenbus_read(struct xenbus_transaction t
,
355 const char *dir
, const char *node
, unsigned int *len
)
360 path
= join(dir
, node
);
364 ret
= xs_single(t
, XS_READ
, path
, len
);
368 EXPORT_SYMBOL_GPL(xenbus_read
);
370 /* Write the value of a single file.
371 * Returns -err on failure.
373 int xenbus_write(struct xenbus_transaction t
,
374 const char *dir
, const char *node
, const char *string
)
377 struct kvec iovec
[2];
380 path
= join(dir
, node
);
382 return PTR_ERR(path
);
384 iovec
[0].iov_base
= (void *)path
;
385 iovec
[0].iov_len
= strlen(path
) + 1;
386 iovec
[1].iov_base
= (void *)string
;
387 iovec
[1].iov_len
= strlen(string
);
389 ret
= xs_error(xs_talkv(t
, XS_WRITE
, iovec
, ARRAY_SIZE(iovec
), NULL
));
393 EXPORT_SYMBOL_GPL(xenbus_write
);
395 /* Create a new directory. */
396 int xenbus_mkdir(struct xenbus_transaction t
,
397 const char *dir
, const char *node
)
402 path
= join(dir
, node
);
404 return PTR_ERR(path
);
406 ret
= xs_error(xs_single(t
, XS_MKDIR
, path
, NULL
));
410 EXPORT_SYMBOL_GPL(xenbus_mkdir
);
412 /* Destroy a file or directory (directories must be empty). */
413 int xenbus_rm(struct xenbus_transaction t
, const char *dir
, const char *node
)
418 path
= join(dir
, node
);
420 return PTR_ERR(path
);
422 ret
= xs_error(xs_single(t
, XS_RM
, path
, NULL
));
426 EXPORT_SYMBOL_GPL(xenbus_rm
);
428 /* Start a transaction: changes by others will not be seen during this
429 * transaction, and changes will not be visible to others until end.
431 int xenbus_transaction_start(struct xenbus_transaction
*t
)
435 down_read(&xs_state
.transaction_mutex
);
437 id_str
= xs_single(XBT_NIL
, XS_TRANSACTION_START
, "", NULL
);
438 if (IS_ERR(id_str
)) {
439 up_read(&xs_state
.transaction_mutex
);
440 return PTR_ERR(id_str
);
443 t
->id
= simple_strtoul(id_str
, NULL
, 0);
447 EXPORT_SYMBOL_GPL(xenbus_transaction_start
);
449 /* End a transaction.
450 * If abandon is true, transaction is discarded instead of committed.
452 int xenbus_transaction_end(struct xenbus_transaction t
, int abort
)
458 strcpy(abortstr
, "F");
460 strcpy(abortstr
, "T");
462 err
= xs_error(xs_single(t
, XS_TRANSACTION_END
, abortstr
, NULL
));
464 up_read(&xs_state
.transaction_mutex
);
468 EXPORT_SYMBOL_GPL(xenbus_transaction_end
);
470 /* Single read and scanf: returns -errno or num scanned. */
471 int xenbus_scanf(struct xenbus_transaction t
,
472 const char *dir
, const char *node
, const char *fmt
, ...)
478 val
= xenbus_read(t
, dir
, node
, NULL
);
483 ret
= vsscanf(val
, fmt
, ap
);
486 /* Distinctive errno. */
491 EXPORT_SYMBOL_GPL(xenbus_scanf
);
493 /* Single printf and write: returns -errno or 0. */
494 int xenbus_printf(struct xenbus_transaction t
,
495 const char *dir
, const char *node
, const char *fmt
, ...)
499 #define PRINTF_BUFFER_SIZE 4096
502 printf_buffer
= kmalloc(PRINTF_BUFFER_SIZE
, GFP_KERNEL
);
503 if (printf_buffer
== NULL
)
507 ret
= vsnprintf(printf_buffer
, PRINTF_BUFFER_SIZE
, fmt
, ap
);
510 BUG_ON(ret
> PRINTF_BUFFER_SIZE
-1);
511 ret
= xenbus_write(t
, dir
, node
, printf_buffer
);
513 kfree(printf_buffer
);
517 EXPORT_SYMBOL_GPL(xenbus_printf
);
519 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
520 int xenbus_gather(struct xenbus_transaction t
, const char *dir
, ...)
527 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
528 const char *fmt
= va_arg(ap
, char *);
529 void *result
= va_arg(ap
, void *);
532 p
= xenbus_read(t
, dir
, name
, NULL
);
538 if (sscanf(p
, fmt
, result
) == 0)
542 *(char **)result
= p
;
547 EXPORT_SYMBOL_GPL(xenbus_gather
);
549 static int xs_watch(const char *path
, const char *token
)
553 iov
[0].iov_base
= (void *)path
;
554 iov
[0].iov_len
= strlen(path
) + 1;
555 iov
[1].iov_base
= (void *)token
;
556 iov
[1].iov_len
= strlen(token
) + 1;
558 return xs_error(xs_talkv(XBT_NIL
, XS_WATCH
, iov
,
559 ARRAY_SIZE(iov
), NULL
));
562 static int xs_unwatch(const char *path
, const char *token
)
566 iov
[0].iov_base
= (char *)path
;
567 iov
[0].iov_len
= strlen(path
) + 1;
568 iov
[1].iov_base
= (char *)token
;
569 iov
[1].iov_len
= strlen(token
) + 1;
571 return xs_error(xs_talkv(XBT_NIL
, XS_UNWATCH
, iov
,
572 ARRAY_SIZE(iov
), NULL
));
575 static struct xenbus_watch
*find_watch(const char *token
)
577 struct xenbus_watch
*i
, *cmp
;
579 cmp
= (void *)simple_strtoul(token
, NULL
, 16);
581 list_for_each_entry(i
, &watches
, list
)
588 /* Register callback to watch this node. */
589 int register_xenbus_watch(struct xenbus_watch
*watch
)
591 /* Pointer in ascii is the token. */
592 char token
[sizeof(watch
) * 2 + 1];
595 sprintf(token
, "%lX", (long)watch
);
597 down_read(&xs_state
.watch_mutex
);
599 spin_lock(&watches_lock
);
600 BUG_ON(find_watch(token
));
601 list_add(&watch
->list
, &watches
);
602 spin_unlock(&watches_lock
);
604 err
= xs_watch(watch
->node
, token
);
606 /* Ignore errors due to multiple registration. */
607 if ((err
!= 0) && (err
!= -EEXIST
)) {
608 spin_lock(&watches_lock
);
609 list_del(&watch
->list
);
610 spin_unlock(&watches_lock
);
613 up_read(&xs_state
.watch_mutex
);
617 EXPORT_SYMBOL_GPL(register_xenbus_watch
);
619 void unregister_xenbus_watch(struct xenbus_watch
*watch
)
621 struct xs_stored_msg
*msg
, *tmp
;
622 char token
[sizeof(watch
) * 2 + 1];
625 sprintf(token
, "%lX", (long)watch
);
627 down_read(&xs_state
.watch_mutex
);
629 spin_lock(&watches_lock
);
630 BUG_ON(!find_watch(token
));
631 list_del(&watch
->list
);
632 spin_unlock(&watches_lock
);
634 err
= xs_unwatch(watch
->node
, token
);
637 "XENBUS Failed to release watch %s: %i\n",
640 up_read(&xs_state
.watch_mutex
);
642 /* Make sure there are no callbacks running currently (unless
644 if (current
->pid
!= xenwatch_pid
)
645 mutex_lock(&xenwatch_mutex
);
647 /* Cancel pending watch events. */
648 spin_lock(&watch_events_lock
);
649 list_for_each_entry_safe(msg
, tmp
, &watch_events
, list
) {
650 if (msg
->u
.watch
.handle
!= watch
)
652 list_del(&msg
->list
);
653 kfree(msg
->u
.watch
.vec
);
656 spin_unlock(&watch_events_lock
);
658 if (current
->pid
!= xenwatch_pid
)
659 mutex_unlock(&xenwatch_mutex
);
661 EXPORT_SYMBOL_GPL(unregister_xenbus_watch
);
663 void xs_suspend(void)
665 down_write(&xs_state
.transaction_mutex
);
666 down_write(&xs_state
.watch_mutex
);
667 mutex_lock(&xs_state
.request_mutex
);
668 mutex_lock(&xs_state
.response_mutex
);
673 struct xenbus_watch
*watch
;
674 char token
[sizeof(watch
) * 2 + 1];
678 mutex_unlock(&xs_state
.response_mutex
);
679 mutex_unlock(&xs_state
.request_mutex
);
680 up_write(&xs_state
.transaction_mutex
);
682 /* No need for watches_lock: the watch_mutex is sufficient. */
683 list_for_each_entry(watch
, &watches
, list
) {
684 sprintf(token
, "%lX", (long)watch
);
685 xs_watch(watch
->node
, token
);
688 up_write(&xs_state
.watch_mutex
);
691 void xs_suspend_cancel(void)
693 mutex_unlock(&xs_state
.response_mutex
);
694 mutex_unlock(&xs_state
.request_mutex
);
695 up_write(&xs_state
.watch_mutex
);
696 up_write(&xs_state
.transaction_mutex
);
699 static int xenwatch_thread(void *unused
)
701 struct list_head
*ent
;
702 struct xs_stored_msg
*msg
;
705 wait_event_interruptible(watch_events_waitq
,
706 !list_empty(&watch_events
));
708 if (kthread_should_stop())
711 mutex_lock(&xenwatch_mutex
);
713 spin_lock(&watch_events_lock
);
714 ent
= watch_events
.next
;
715 if (ent
!= &watch_events
)
717 spin_unlock(&watch_events_lock
);
719 if (ent
!= &watch_events
) {
720 msg
= list_entry(ent
, struct xs_stored_msg
, list
);
721 msg
->u
.watch
.handle
->callback(
723 (const char **)msg
->u
.watch
.vec
,
724 msg
->u
.watch
.vec_size
);
725 kfree(msg
->u
.watch
.vec
);
729 mutex_unlock(&xenwatch_mutex
);
735 static int process_msg(void)
737 struct xs_stored_msg
*msg
;
742 * We must disallow save/restore while reading a xenstore message.
743 * A partial read across s/r leaves us out of sync with xenstored.
746 err
= xb_wait_for_data_to_read();
749 mutex_lock(&xs_state
.response_mutex
);
750 if (xb_data_to_read())
752 /* We raced with save/restore: pending data 'disappeared'. */
753 mutex_unlock(&xs_state
.response_mutex
);
757 msg
= kmalloc(sizeof(*msg
), GFP_NOIO
| __GFP_HIGH
);
763 err
= xb_read(&msg
->hdr
, sizeof(msg
->hdr
));
769 body
= kmalloc(msg
->hdr
.len
+ 1, GFP_NOIO
| __GFP_HIGH
);
776 err
= xb_read(body
, msg
->hdr
.len
);
782 body
[msg
->hdr
.len
] = '\0';
784 if (msg
->hdr
.type
== XS_WATCH_EVENT
) {
785 msg
->u
.watch
.vec
= split(body
, msg
->hdr
.len
,
786 &msg
->u
.watch
.vec_size
);
787 if (IS_ERR(msg
->u
.watch
.vec
)) {
788 err
= PTR_ERR(msg
->u
.watch
.vec
);
793 spin_lock(&watches_lock
);
794 msg
->u
.watch
.handle
= find_watch(
795 msg
->u
.watch
.vec
[XS_WATCH_TOKEN
]);
796 if (msg
->u
.watch
.handle
!= NULL
) {
797 spin_lock(&watch_events_lock
);
798 list_add_tail(&msg
->list
, &watch_events
);
799 wake_up(&watch_events_waitq
);
800 spin_unlock(&watch_events_lock
);
802 kfree(msg
->u
.watch
.vec
);
805 spin_unlock(&watches_lock
);
807 msg
->u
.reply
.body
= body
;
808 spin_lock(&xs_state
.reply_lock
);
809 list_add_tail(&msg
->list
, &xs_state
.reply_list
);
810 spin_unlock(&xs_state
.reply_lock
);
811 wake_up(&xs_state
.reply_waitq
);
815 mutex_unlock(&xs_state
.response_mutex
);
819 static int xenbus_thread(void *unused
)
826 printk(KERN_WARNING
"XENBUS error %d while reading "
828 if (kthread_should_stop())
838 struct task_struct
*task
;
840 INIT_LIST_HEAD(&xs_state
.reply_list
);
841 spin_lock_init(&xs_state
.reply_lock
);
842 init_waitqueue_head(&xs_state
.reply_waitq
);
844 mutex_init(&xs_state
.request_mutex
);
845 mutex_init(&xs_state
.response_mutex
);
846 init_rwsem(&xs_state
.transaction_mutex
);
847 init_rwsem(&xs_state
.watch_mutex
);
849 /* Initialize the shared memory rings to talk to xenstored */
850 err
= xb_init_comms();
854 task
= kthread_run(xenwatch_thread
, NULL
, "xenwatch");
856 return PTR_ERR(task
);
857 xenwatch_pid
= task
->pid
;
859 task
= kthread_run(xenbus_thread
, NULL
, "xenbus");
861 return PTR_ERR(task
);