xref: /freebsd/sys/dev/xen/xenstore/xenstore.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1a6aedc5dSRoger Pau Monné /******************************************************************************
2a6aedc5dSRoger Pau Monné  * xenstore.c
3a6aedc5dSRoger Pau Monné  *
4a6aedc5dSRoger Pau Monné  * Low-level kernel interface to the XenStore.
5a6aedc5dSRoger Pau Monné  *
6a6aedc5dSRoger Pau Monné  * Copyright (C) 2005 Rusty Russell, IBM Corporation
7a6aedc5dSRoger Pau Monné  * Copyright (C) 2009,2010 Spectra Logic Corporation
8a6aedc5dSRoger Pau Monné  *
9a6aedc5dSRoger Pau Monné  * This file may be distributed separately from the Linux kernel, or
10a6aedc5dSRoger Pau Monné  * incorporated into other software packages, subject to the following license:
11a6aedc5dSRoger Pau Monné  *
12a6aedc5dSRoger Pau Monné  * Permission is hereby granted, free of charge, to any person obtaining a copy
13a6aedc5dSRoger Pau Monné  * of this source file (the "Software"), to deal in the Software without
14a6aedc5dSRoger Pau Monné  * restriction, including without limitation the rights to use, copy, modify,
15a6aedc5dSRoger Pau Monné  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16a6aedc5dSRoger Pau Monné  * and to permit persons to whom the Software is furnished to do so, subject to
17a6aedc5dSRoger Pau Monné  * the following conditions:
18a6aedc5dSRoger Pau Monné  *
19a6aedc5dSRoger Pau Monné  * The above copyright notice and this permission notice shall be included in
20a6aedc5dSRoger Pau Monné  * all copies or substantial portions of the Software.
21a6aedc5dSRoger Pau Monné  *
22a6aedc5dSRoger Pau Monné  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a6aedc5dSRoger Pau Monné  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a6aedc5dSRoger Pau Monné  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25a6aedc5dSRoger Pau Monné  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26a6aedc5dSRoger Pau Monné  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27a6aedc5dSRoger Pau Monné  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28a6aedc5dSRoger Pau Monné  * IN THE SOFTWARE.
29a6aedc5dSRoger Pau Monné  */
30a6aedc5dSRoger Pau Monné 
31a6aedc5dSRoger Pau Monné #include <sys/param.h>
32a6aedc5dSRoger Pau Monné #include <sys/bus.h>
33a6aedc5dSRoger Pau Monné #include <sys/kernel.h>
34a6aedc5dSRoger Pau Monné #include <sys/lock.h>
35a6aedc5dSRoger Pau Monné #include <sys/module.h>
36a6aedc5dSRoger Pau Monné #include <sys/mutex.h>
37a6aedc5dSRoger Pau Monné #include <sys/sx.h>
38a6aedc5dSRoger Pau Monné #include <sys/syslog.h>
39a6aedc5dSRoger Pau Monné #include <sys/malloc.h>
40a6aedc5dSRoger Pau Monné #include <sys/systm.h>
41a6aedc5dSRoger Pau Monné #include <sys/proc.h>
42a6aedc5dSRoger Pau Monné #include <sys/kthread.h>
43a6aedc5dSRoger Pau Monné #include <sys/sbuf.h>
44a6aedc5dSRoger Pau Monné #include <sys/sysctl.h>
45a6aedc5dSRoger Pau Monné #include <sys/uio.h>
46a6aedc5dSRoger Pau Monné #include <sys/unistd.h>
471d84e2b3SRoger Pau Monné #include <sys/queue.h>
481d84e2b3SRoger Pau Monné #include <sys/taskqueue.h>
49a6aedc5dSRoger Pau Monné 
50a6aedc5dSRoger Pau Monné #include <machine/stdarg.h>
51a6aedc5dSRoger Pau Monné 
52a6aedc5dSRoger Pau Monné #include <xen/xen-os.h>
53a6aedc5dSRoger Pau Monné #include <xen/hypervisor.h>
54a6aedc5dSRoger Pau Monné #include <xen/xen_intr.h>
55a6aedc5dSRoger Pau Monné 
56ad7dd514SElliott Mitchell #include <contrib/xen/hvm/params.h>
57a6aedc5dSRoger Pau Monné #include <xen/hvm.h>
58a6aedc5dSRoger Pau Monné 
59a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstorevar.h>
60a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstore_internal.h>
61a6aedc5dSRoger Pau Monné 
62a6aedc5dSRoger Pau Monné #include <vm/vm.h>
63a6aedc5dSRoger Pau Monné #include <vm/pmap.h>
64a6aedc5dSRoger Pau Monné 
65a6aedc5dSRoger Pau Monné /**
66a6aedc5dSRoger Pau Monné  * \file xenstore.c
67a6aedc5dSRoger Pau Monné  * \brief XenStore interface
68a6aedc5dSRoger Pau Monné  *
69a6aedc5dSRoger Pau Monné  * The XenStore interface is a simple storage system that is a means of
70a6aedc5dSRoger Pau Monné  * communicating state and configuration data between the Xen Domain 0
71a6aedc5dSRoger Pau Monné  * and the various guest domains.  All configuration data other than
72a6aedc5dSRoger Pau Monné  * a small amount of essential information required during the early
73a6aedc5dSRoger Pau Monné  * boot process of launching a Xen aware guest, is managed using the
74a6aedc5dSRoger Pau Monné  * XenStore.
75a6aedc5dSRoger Pau Monné  *
76a6aedc5dSRoger Pau Monné  * The XenStore is ASCII string based, and has a structure and semantics
77a6aedc5dSRoger Pau Monné  * similar to a filesystem.  There are files and directories, the directories
78453130d9SPedro F. Giffuni  * able to contain files or other directories.  The depth of the hierarchy
79a6aedc5dSRoger Pau Monné  * is only limited by the XenStore's maximum path length.
80a6aedc5dSRoger Pau Monné  *
81a6aedc5dSRoger Pau Monné  * The communication channel between the XenStore service and other
82a6aedc5dSRoger Pau Monné  * domains is via two, guest specific, ring buffers in a shared memory
83a6aedc5dSRoger Pau Monné  * area.  One ring buffer is used for communicating in each direction.
84a6aedc5dSRoger Pau Monné  * The grant table references for this shared memory are given to the
85a6aedc5dSRoger Pau Monné  * guest either via the xen_start_info structure for a fully para-
86a6aedc5dSRoger Pau Monné  * virtualized guest, or via HVM hypercalls for a hardware virtualized
87a6aedc5dSRoger Pau Monné  * guest.
88a6aedc5dSRoger Pau Monné  *
89a6aedc5dSRoger Pau Monné  * The XenStore communication relies on an event channel and thus
90a6aedc5dSRoger Pau Monné  * interrupts.  For this reason, the attachment of the XenStore
91a6aedc5dSRoger Pau Monné  * relies on an interrupt driven configuration hook to hold off
92a6aedc5dSRoger Pau Monné  * boot processing until communication with the XenStore service
93a6aedc5dSRoger Pau Monné  * can be established.
94a6aedc5dSRoger Pau Monné  *
95a6aedc5dSRoger Pau Monné  * Several Xen services depend on the XenStore, most notably the
96a6aedc5dSRoger Pau Monné  * XenBus used to discover and manage Xen devices.  These services
97a6aedc5dSRoger Pau Monné  * are implemented as NewBus child attachments to a bus exported
98a6aedc5dSRoger Pau Monné  * by this XenStore driver.
99a6aedc5dSRoger Pau Monné  */
100a6aedc5dSRoger Pau Monné 
101a6aedc5dSRoger Pau Monné static struct xs_watch *find_watch(const char *token);
102a6aedc5dSRoger Pau Monné 
103a6aedc5dSRoger Pau Monné MALLOC_DEFINE(M_XENSTORE, "xenstore", "XenStore data and results");
104a6aedc5dSRoger Pau Monné 
105a6aedc5dSRoger Pau Monné /**
106a6aedc5dSRoger Pau Monné  * Pointer to shared memory communication structures allowing us
107a6aedc5dSRoger Pau Monné  * to communicate with the XenStore service.
108a6aedc5dSRoger Pau Monné  *
109a6aedc5dSRoger Pau Monné  * When operating in full PV mode, this pointer is set early in kernel
110a6aedc5dSRoger Pau Monné  * startup from within xen_machdep.c.  In HVM mode, we use hypercalls
111a6aedc5dSRoger Pau Monné  * to get the guest frame number for the shared page and then map it
112a6aedc5dSRoger Pau Monné  * into kva.  See xs_init() for details.
113a6aedc5dSRoger Pau Monné  */
114cfa0b7b8SRoger Pau Monné static struct xenstore_domain_interface *xen_store;
115a6aedc5dSRoger Pau Monné 
116a6aedc5dSRoger Pau Monné /*-------------------------- Private Data Structures ------------------------*/
117a6aedc5dSRoger Pau Monné 
118a6aedc5dSRoger Pau Monné /**
119a6aedc5dSRoger Pau Monné  * Structure capturing messages received from the XenStore service.
120a6aedc5dSRoger Pau Monné  */
121a6aedc5dSRoger Pau Monné struct xs_stored_msg {
122a6aedc5dSRoger Pau Monné 	TAILQ_ENTRY(xs_stored_msg) list;
123a6aedc5dSRoger Pau Monné 
124a6aedc5dSRoger Pau Monné 	struct xsd_sockmsg hdr;
125a6aedc5dSRoger Pau Monné 
126a6aedc5dSRoger Pau Monné 	union {
127a6aedc5dSRoger Pau Monné 		/* Queued replies. */
128a6aedc5dSRoger Pau Monné 		struct {
129a6aedc5dSRoger Pau Monné 			char *body;
130a6aedc5dSRoger Pau Monné 		} reply;
131a6aedc5dSRoger Pau Monné 
132a6aedc5dSRoger Pau Monné 		/* Queued watch events. */
133a6aedc5dSRoger Pau Monné 		struct {
134a6aedc5dSRoger Pau Monné 			struct xs_watch *handle;
135a6aedc5dSRoger Pau Monné 			const char **vec;
136a6aedc5dSRoger Pau Monné 			u_int vec_size;
137a6aedc5dSRoger Pau Monné 		} watch;
138a6aedc5dSRoger Pau Monné 	} u;
139a6aedc5dSRoger Pau Monné };
140a6aedc5dSRoger Pau Monné TAILQ_HEAD(xs_stored_msg_list, xs_stored_msg);
141a6aedc5dSRoger Pau Monné 
142a6aedc5dSRoger Pau Monné /**
143a6aedc5dSRoger Pau Monné  * Container for all XenStore related state.
144a6aedc5dSRoger Pau Monné  */
145a6aedc5dSRoger Pau Monné struct xs_softc {
146a6aedc5dSRoger Pau Monné 	/** Newbus device for the XenStore. */
147a6aedc5dSRoger Pau Monné 	device_t xs_dev;
148a6aedc5dSRoger Pau Monné 
149a6aedc5dSRoger Pau Monné 	/**
150a6aedc5dSRoger Pau Monné 	 * Lock serializing access to ring producer/consumer
151a6aedc5dSRoger Pau Monné 	 * indexes.  Use of this lock guarantees that wakeups
152a6aedc5dSRoger Pau Monné 	 * of blocking readers/writers are not missed due to
153a6aedc5dSRoger Pau Monné 	 * races with the XenStore service.
154a6aedc5dSRoger Pau Monné 	 */
155a6aedc5dSRoger Pau Monné 	struct mtx ring_lock;
156a6aedc5dSRoger Pau Monné 
157a6aedc5dSRoger Pau Monné 	/*
158a6aedc5dSRoger Pau Monné 	 * Mutex used to insure exclusive access to the outgoing
159a6aedc5dSRoger Pau Monné 	 * communication ring.  We use a lock type that can be
160a6aedc5dSRoger Pau Monné 	 * held while sleeping so that xs_write() can block waiting
161a6aedc5dSRoger Pau Monné 	 * for space in the ring to free up, without allowing another
162a6aedc5dSRoger Pau Monné 	 * writer to come in and corrupt a partial message write.
163a6aedc5dSRoger Pau Monné 	 */
164a6aedc5dSRoger Pau Monné 	struct sx request_mutex;
165a6aedc5dSRoger Pau Monné 
166a6aedc5dSRoger Pau Monné 	/**
167a6aedc5dSRoger Pau Monné 	 * A list of replies to our requests.
168a6aedc5dSRoger Pau Monné 	 *
169a6aedc5dSRoger Pau Monné 	 * The reply list is filled by xs_rcv_thread().  It
170a6aedc5dSRoger Pau Monné 	 * is consumed by the context that issued the request
171a6aedc5dSRoger Pau Monné 	 * to which a reply is made.  The requester blocks in
172a6aedc5dSRoger Pau Monné 	 * xs_read_reply().
173a6aedc5dSRoger Pau Monné 	 *
174a6aedc5dSRoger Pau Monné 	 * /note Only one requesting context can be active at a time.
175a6aedc5dSRoger Pau Monné 	 *       This is guaranteed by the request_mutex and insures
176a6aedc5dSRoger Pau Monné 	 *	 that the requester sees replies matching the order
177a6aedc5dSRoger Pau Monné 	 *	 of its requests.
178a6aedc5dSRoger Pau Monné 	 */
179a6aedc5dSRoger Pau Monné 	struct xs_stored_msg_list reply_list;
180a6aedc5dSRoger Pau Monné 
181a6aedc5dSRoger Pau Monné 	/** Lock protecting the reply list. */
182a6aedc5dSRoger Pau Monné 	struct mtx reply_lock;
183a6aedc5dSRoger Pau Monné 
184a6aedc5dSRoger Pau Monné 	/**
185a6aedc5dSRoger Pau Monné 	 * List of registered watches.
186a6aedc5dSRoger Pau Monné 	 */
187a6aedc5dSRoger Pau Monné 	struct xs_watch_list  registered_watches;
188a6aedc5dSRoger Pau Monné 
189a6aedc5dSRoger Pau Monné 	/** Lock protecting the registered watches list. */
190a6aedc5dSRoger Pau Monné 	struct mtx registered_watches_lock;
191a6aedc5dSRoger Pau Monné 
192a6aedc5dSRoger Pau Monné 	/**
193a6aedc5dSRoger Pau Monné 	 * List of pending watch callback events.
194a6aedc5dSRoger Pau Monné 	 */
195a6aedc5dSRoger Pau Monné 	struct xs_stored_msg_list watch_events;
196a6aedc5dSRoger Pau Monné 
197a6aedc5dSRoger Pau Monné 	/** Lock protecting the watch calback list. */
198a6aedc5dSRoger Pau Monné 	struct mtx watch_events_lock;
199a6aedc5dSRoger Pau Monné 
200a6aedc5dSRoger Pau Monné 	/**
201a6aedc5dSRoger Pau Monné 	 * The processid of the xenwatch thread.
202a6aedc5dSRoger Pau Monné 	 */
203a6aedc5dSRoger Pau Monné 	pid_t xenwatch_pid;
204a6aedc5dSRoger Pau Monné 
205a6aedc5dSRoger Pau Monné 	/**
206a6aedc5dSRoger Pau Monné 	 * Sleepable mutex used to gate the execution of XenStore
207a6aedc5dSRoger Pau Monné 	 * watch event callbacks.
208a6aedc5dSRoger Pau Monné 	 *
209a6aedc5dSRoger Pau Monné 	 * xenwatch_thread holds an exclusive lock on this mutex
210a6aedc5dSRoger Pau Monné 	 * while delivering event callbacks, and xenstore_unregister_watch()
211a6aedc5dSRoger Pau Monné 	 * uses an exclusive lock of this mutex to guarantee that no
212a6aedc5dSRoger Pau Monné 	 * callbacks of the just unregistered watch are pending
213a6aedc5dSRoger Pau Monné 	 * before returning to its caller.
214a6aedc5dSRoger Pau Monné 	 */
215a6aedc5dSRoger Pau Monné 	struct sx xenwatch_mutex;
216a6aedc5dSRoger Pau Monné 
217a6aedc5dSRoger Pau Monné 	/**
218a6aedc5dSRoger Pau Monné 	 * The HVM guest pseudo-physical frame number.  This is Xen's mapping
219a6aedc5dSRoger Pau Monné 	 * of the true machine frame number into our "physical address space".
220a6aedc5dSRoger Pau Monné 	 */
221a6aedc5dSRoger Pau Monné 	unsigned long gpfn;
222a6aedc5dSRoger Pau Monné 
223a6aedc5dSRoger Pau Monné 	/**
224a6aedc5dSRoger Pau Monné 	 * The event channel for communicating with the
225a6aedc5dSRoger Pau Monné 	 * XenStore service.
226a6aedc5dSRoger Pau Monné 	 */
227a6aedc5dSRoger Pau Monné 	int evtchn;
228a6aedc5dSRoger Pau Monné 
229a6aedc5dSRoger Pau Monné 	/** Handle for XenStore interrupts. */
230a6aedc5dSRoger Pau Monné 	xen_intr_handle_t xen_intr_handle;
231a6aedc5dSRoger Pau Monné 
232a6aedc5dSRoger Pau Monné 	/**
233a6aedc5dSRoger Pau Monné 	 * Interrupt driven config hook allowing us to defer
234a6aedc5dSRoger Pau Monné 	 * attaching children until interrupts (and thus communication
235a6aedc5dSRoger Pau Monné 	 * with the XenStore service) are available.
236a6aedc5dSRoger Pau Monné 	 */
237a6aedc5dSRoger Pau Monné 	struct intr_config_hook xs_attachcb;
2381d84e2b3SRoger Pau Monné 
2391d84e2b3SRoger Pau Monné 	/**
2401d84e2b3SRoger Pau Monné 	 * Xenstore is a user-space process that usually runs in Dom0,
2411d84e2b3SRoger Pau Monné 	 * so if this domain is booting as Dom0, xenstore wont we accessible,
2421d84e2b3SRoger Pau Monné 	 * and we have to defer the initialization of xenstore related
2431d84e2b3SRoger Pau Monné 	 * devices to later (when xenstore is started).
2441d84e2b3SRoger Pau Monné 	 */
2451d84e2b3SRoger Pau Monné 	bool initialized;
2461d84e2b3SRoger Pau Monné 
2471d84e2b3SRoger Pau Monné 	/**
2481d84e2b3SRoger Pau Monné 	 * Task to run when xenstore is initialized (Dom0 only), will
2491d84e2b3SRoger Pau Monné 	 * take care of attaching xenstore related devices.
2501d84e2b3SRoger Pau Monné 	 */
2511d84e2b3SRoger Pau Monné 	struct task xs_late_init;
252a6aedc5dSRoger Pau Monné };
253a6aedc5dSRoger Pau Monné 
254a6aedc5dSRoger Pau Monné /*-------------------------------- Global Data ------------------------------*/
255a6aedc5dSRoger Pau Monné static struct xs_softc xs;
256a6aedc5dSRoger Pau Monné 
257a6aedc5dSRoger Pau Monné /*------------------------- Private Utility Functions -----------------------*/
258a6aedc5dSRoger Pau Monné 
259a6aedc5dSRoger Pau Monné /**
260a6aedc5dSRoger Pau Monné  * Count and optionally record pointers to a number of NUL terminated
261a6aedc5dSRoger Pau Monné  * strings in a buffer.
262a6aedc5dSRoger Pau Monné  *
263a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
264a6aedc5dSRoger Pau Monné  * \param dest	   An array to store pointers to each string found in strings.
265a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
266a6aedc5dSRoger Pau Monné  *
267a6aedc5dSRoger Pau Monné  * \return  A count of the number of strings found.
268a6aedc5dSRoger Pau Monné  */
269a6aedc5dSRoger Pau Monné static u_int
extract_strings(const char * strings,const char ** dest,u_int len)270a6aedc5dSRoger Pau Monné extract_strings(const char *strings, const char **dest, u_int len)
271a6aedc5dSRoger Pau Monné {
272a6aedc5dSRoger Pau Monné 	u_int num;
273a6aedc5dSRoger Pau Monné 	const char *p;
274a6aedc5dSRoger Pau Monné 
275a6aedc5dSRoger Pau Monné 	for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) {
276a6aedc5dSRoger Pau Monné 		if (dest != NULL)
277a6aedc5dSRoger Pau Monné 			*dest++ = p;
278a6aedc5dSRoger Pau Monné 		num++;
279a6aedc5dSRoger Pau Monné 	}
280a6aedc5dSRoger Pau Monné 
281a6aedc5dSRoger Pau Monné 	return (num);
282a6aedc5dSRoger Pau Monné }
283a6aedc5dSRoger Pau Monné 
284a6aedc5dSRoger Pau Monné /**
285a6aedc5dSRoger Pau Monné  * Convert a contiguous buffer containing a series of NUL terminated
286a6aedc5dSRoger Pau Monné  * strings into an array of pointers to strings.
287a6aedc5dSRoger Pau Monné  *
288a6aedc5dSRoger Pau Monné  * The returned pointer references the array of string pointers which
289a6aedc5dSRoger Pau Monné  * is followed by the storage for the string data.  It is the client's
290a6aedc5dSRoger Pau Monné  * responsibility to free this storage.
291a6aedc5dSRoger Pau Monné  *
292a6aedc5dSRoger Pau Monné  * The storage addressed by strings is free'd prior to split returning.
293a6aedc5dSRoger Pau Monné  *
294a6aedc5dSRoger Pau Monné  * \param strings  A pointer to a contiguous buffer of NUL terminated strings.
295a6aedc5dSRoger Pau Monné  * \param len	   The length of the buffer pointed to by strings.
296a6aedc5dSRoger Pau Monné  * \param num	   The number of strings found and returned in the strings
297a6aedc5dSRoger Pau Monné  *                 array.
298a6aedc5dSRoger Pau Monné  *
299a6aedc5dSRoger Pau Monné  * \return  An array of pointers to the strings found in the input buffer.
300a6aedc5dSRoger Pau Monné  */
301a6aedc5dSRoger Pau Monné static const char **
split(char * strings,u_int len,u_int * num)302a6aedc5dSRoger Pau Monné split(char *strings, u_int len, u_int *num)
303a6aedc5dSRoger Pau Monné {
304a6aedc5dSRoger Pau Monné 	const char **ret;
305a6aedc5dSRoger Pau Monné 
306a6aedc5dSRoger Pau Monné 	/* Protect against unterminated buffers. */
307a6aedc5dSRoger Pau Monné 	if (len > 0)
308a6aedc5dSRoger Pau Monné 		strings[len - 1] = '\0';
309a6aedc5dSRoger Pau Monné 
310a6aedc5dSRoger Pau Monné 	/* Count the strings. */
311a6aedc5dSRoger Pau Monné 	*num = extract_strings(strings, /*dest*/NULL, len);
312a6aedc5dSRoger Pau Monné 
313a6aedc5dSRoger Pau Monné 	/* Transfer to one big alloc for easy freeing by the caller. */
314a6aedc5dSRoger Pau Monné 	ret = malloc(*num * sizeof(char *) + len, M_XENSTORE, M_WAITOK);
315a6aedc5dSRoger Pau Monné 	memcpy(&ret[*num], strings, len);
316a6aedc5dSRoger Pau Monné 	free(strings, M_XENSTORE);
317a6aedc5dSRoger Pau Monné 
318a6aedc5dSRoger Pau Monné 	/* Extract pointers to newly allocated array. */
319a6aedc5dSRoger Pau Monné 	strings = (char *)&ret[*num];
320a6aedc5dSRoger Pau Monné 	(void)extract_strings(strings, /*dest*/ret, len);
321a6aedc5dSRoger Pau Monné 
322a6aedc5dSRoger Pau Monné 	return (ret);
323a6aedc5dSRoger Pau Monné }
324a6aedc5dSRoger Pau Monné 
325a6aedc5dSRoger Pau Monné /*------------------------- Public Utility Functions -------------------------*/
326a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
327a6aedc5dSRoger Pau Monné struct sbuf *
xs_join(const char * dir,const char * name)328a6aedc5dSRoger Pau Monné xs_join(const char *dir, const char *name)
329a6aedc5dSRoger Pau Monné {
330a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
331a6aedc5dSRoger Pau Monné 
332a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
333a6aedc5dSRoger Pau Monné 	sbuf_cat(sb, dir);
334a6aedc5dSRoger Pau Monné 	if (name[0] != '\0') {
335a6aedc5dSRoger Pau Monné 		sbuf_putc(sb, '/');
336a6aedc5dSRoger Pau Monné 		sbuf_cat(sb, name);
337a6aedc5dSRoger Pau Monné 	}
338a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
339a6aedc5dSRoger Pau Monné 
340a6aedc5dSRoger Pau Monné 	return (sb);
341a6aedc5dSRoger Pau Monné }
342a6aedc5dSRoger Pau Monné 
343a6aedc5dSRoger Pau Monné /*-------------------- Low Level Communication Management --------------------*/
344a6aedc5dSRoger Pau Monné /**
345a6aedc5dSRoger Pau Monné  * Interrupt handler for the XenStore event channel.
346a6aedc5dSRoger Pau Monné  *
347a6aedc5dSRoger Pau Monné  * XenStore reads and writes block on "xen_store" for buffer
348a6aedc5dSRoger Pau Monné  * space.  Wakeup any blocking operations when the XenStore
349a6aedc5dSRoger Pau Monné  * service has modified the queues.
350a6aedc5dSRoger Pau Monné  */
351a6aedc5dSRoger Pau Monné static void
xs_intr(void * arg __unused)352a6aedc5dSRoger Pau Monné xs_intr(void * arg __unused /*__attribute__((unused))*/)
353a6aedc5dSRoger Pau Monné {
354a6aedc5dSRoger Pau Monné 
3551d84e2b3SRoger Pau Monné 	/* If xenstore has not been initialized, initialize it now */
3561d84e2b3SRoger Pau Monné 	if (!xs.initialized) {
3571d84e2b3SRoger Pau Monné 		xs.initialized = true;
3581d84e2b3SRoger Pau Monné 		/*
3591d84e2b3SRoger Pau Monné 		 * Since this task is probing and attaching devices we
3601d84e2b3SRoger Pau Monné 		 * have to hold the Giant lock.
3611d84e2b3SRoger Pau Monné 		 */
3621d84e2b3SRoger Pau Monné 		taskqueue_enqueue(taskqueue_swi_giant, &xs.xs_late_init);
3631d84e2b3SRoger Pau Monné 	}
3641d84e2b3SRoger Pau Monné 
365a6aedc5dSRoger Pau Monné 	/*
366a6aedc5dSRoger Pau Monné 	 * Hold ring lock across wakeup so that clients
367a6aedc5dSRoger Pau Monné 	 * cannot miss a wakeup.
368a6aedc5dSRoger Pau Monné 	 */
369a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.ring_lock);
370a6aedc5dSRoger Pau Monné 	wakeup(xen_store);
371a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.ring_lock);
372a6aedc5dSRoger Pau Monné }
373a6aedc5dSRoger Pau Monné 
374a6aedc5dSRoger Pau Monné /**
375a6aedc5dSRoger Pau Monné  * Verify that the indexes for a ring are valid.
376a6aedc5dSRoger Pau Monné  *
377a6aedc5dSRoger Pau Monné  * The difference between the producer and consumer cannot
378a6aedc5dSRoger Pau Monné  * exceed the size of the ring.
379a6aedc5dSRoger Pau Monné  *
380a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring to test.
381a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring to test.
382a6aedc5dSRoger Pau Monné  *
383a6aedc5dSRoger Pau Monné  * \retval 1  If indexes are in range.
384a6aedc5dSRoger Pau Monné  * \retval 0  If the indexes are out of range.
385a6aedc5dSRoger Pau Monné  */
386a6aedc5dSRoger Pau Monné static int
xs_check_indexes(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod)387a6aedc5dSRoger Pau Monné xs_check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
388a6aedc5dSRoger Pau Monné {
389a6aedc5dSRoger Pau Monné 
390a6aedc5dSRoger Pau Monné 	return ((prod - cons) <= XENSTORE_RING_SIZE);
391a6aedc5dSRoger Pau Monné }
392a6aedc5dSRoger Pau Monné 
393a6aedc5dSRoger Pau Monné /**
394a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
395a6aedc5dSRoger Pau Monné  * free region available for output in a ring buffer.
396a6aedc5dSRoger Pau Monné  *
397a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
398a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
399a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
400a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous storage available.
401a6aedc5dSRoger Pau Monné  *
402a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the free region.
403a6aedc5dSRoger Pau Monné  */
404a6aedc5dSRoger Pau Monné static void *
xs_get_output_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,char * buf,uint32_t * len)405a6aedc5dSRoger Pau Monné xs_get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
406a6aedc5dSRoger Pau Monné     char *buf, uint32_t *len)
407a6aedc5dSRoger Pau Monné {
408a6aedc5dSRoger Pau Monné 
409a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
410a6aedc5dSRoger Pau Monné 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
411a6aedc5dSRoger Pau Monné 		*len = XENSTORE_RING_SIZE - (prod - cons);
412a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(prod));
413a6aedc5dSRoger Pau Monné }
414a6aedc5dSRoger Pau Monné 
415a6aedc5dSRoger Pau Monné /**
416a6aedc5dSRoger Pau Monné  * Return a pointer to, and the length of, the contiguous
417a6aedc5dSRoger Pau Monné  * data available to read from a ring buffer.
418a6aedc5dSRoger Pau Monné  *
419a6aedc5dSRoger Pau Monné  * \param cons  The consumer index for the ring.
420a6aedc5dSRoger Pau Monné  * \param prod  The producer index for the ring.
421a6aedc5dSRoger Pau Monné  * \param buf   The base address of the ring's storage.
422a6aedc5dSRoger Pau Monné  * \param len   The amount of contiguous data available to read.
423a6aedc5dSRoger Pau Monné  *
424a6aedc5dSRoger Pau Monné  * \return  A pointer to the start location of the available data.
425a6aedc5dSRoger Pau Monné  */
426a6aedc5dSRoger Pau Monné static const void *
xs_get_input_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,const char * buf,uint32_t * len)427a6aedc5dSRoger Pau Monné xs_get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
428a6aedc5dSRoger Pau Monné     const char *buf, uint32_t *len)
429a6aedc5dSRoger Pau Monné {
430a6aedc5dSRoger Pau Monné 
431a6aedc5dSRoger Pau Monné 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
432a6aedc5dSRoger Pau Monné 	if ((prod - cons) < *len)
433a6aedc5dSRoger Pau Monné 		*len = prod - cons;
434a6aedc5dSRoger Pau Monné 	return (buf + MASK_XENSTORE_IDX(cons));
435a6aedc5dSRoger Pau Monné }
436a6aedc5dSRoger Pau Monné 
437a6aedc5dSRoger Pau Monné /**
438a6aedc5dSRoger Pau Monné  * Transmit data to the XenStore service.
439a6aedc5dSRoger Pau Monné  *
440a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous data to send.
441a6aedc5dSRoger Pau Monné  * \param len    The amount of data to send.
442a6aedc5dSRoger Pau Monné  *
443a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
444a6aedc5dSRoger Pau Monné  *          cause of failure.
445a6aedc5dSRoger Pau Monné  *
446a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
447a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
448a6aedc5dSRoger Pau Monné  *             in length.
449a6aedc5dSRoger Pau Monné  * \invariant  xs.request_mutex exclusively locked.
450a6aedc5dSRoger Pau Monné  */
451a6aedc5dSRoger Pau Monné static int
xs_write_store(const void * tdata,unsigned len)452a6aedc5dSRoger Pau Monné xs_write_store(const void *tdata, unsigned len)
453a6aedc5dSRoger Pau Monné {
454a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
455a6aedc5dSRoger Pau Monné 	const char *data = (const char *)tdata;
456a6aedc5dSRoger Pau Monné 	int error;
457a6aedc5dSRoger Pau Monné 
458a6aedc5dSRoger Pau Monné 	sx_assert(&xs.request_mutex, SX_XLOCKED);
459a6aedc5dSRoger Pau Monné 	while (len != 0) {
460a6aedc5dSRoger Pau Monné 		void *dst;
461a6aedc5dSRoger Pau Monné 		u_int avail;
462a6aedc5dSRoger Pau Monné 
463a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
464a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
465a6aedc5dSRoger Pau Monné 		cons = xen_store->req_cons;
466a6aedc5dSRoger Pau Monné 		prod = xen_store->req_prod;
467a6aedc5dSRoger Pau Monné 		if ((prod - cons) == XENSTORE_RING_SIZE) {
468a6aedc5dSRoger Pau Monné 			/*
469a6aedc5dSRoger Pau Monné 			 * Output ring is full. Wait for a ring event.
470a6aedc5dSRoger Pau Monné 			 *
471a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
472a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
473a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
474a6aedc5dSRoger Pau Monné 			 * ring.
475a6aedc5dSRoger Pau Monné 			 *
476a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
477a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
478a6aedc5dSRoger Pau Monné 			 * when msleep returns.
479a6aedc5dSRoger Pau Monné 			 */
480a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
481a6aedc5dSRoger Pau Monné 			     "xbwrite", /*timeout*/0);
482a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
483a6aedc5dSRoger Pau Monné 				return (error);
484a6aedc5dSRoger Pau Monné 
485a6aedc5dSRoger Pau Monné 			/* Try again. */
486a6aedc5dSRoger Pau Monné 			continue;
487a6aedc5dSRoger Pau Monné 		}
488a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
489a6aedc5dSRoger Pau Monné 
490a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
491a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
492a6aedc5dSRoger Pau Monné 			xen_store->req_cons = xen_store->req_prod = 0;
493a6aedc5dSRoger Pau Monné 			return (EIO);
494a6aedc5dSRoger Pau Monné 		}
495a6aedc5dSRoger Pau Monné 
496a6aedc5dSRoger Pau Monné 		dst = xs_get_output_chunk(cons, prod, xen_store->req, &avail);
497a6aedc5dSRoger Pau Monné 		if (avail > len)
498a6aedc5dSRoger Pau Monné 			avail = len;
499a6aedc5dSRoger Pau Monné 
500a6aedc5dSRoger Pau Monné 		memcpy(dst, data, avail);
501a6aedc5dSRoger Pau Monné 		data += avail;
502a6aedc5dSRoger Pau Monné 		len -= avail;
503a6aedc5dSRoger Pau Monné 
504a6aedc5dSRoger Pau Monné 		/*
505a6aedc5dSRoger Pau Monné 		 * The store to the producer index, which indicates
506a6aedc5dSRoger Pau Monné 		 * to the other side that new data has arrived, must
507a6aedc5dSRoger Pau Monné 		 * be visible only after our copy of the data into the
508a6aedc5dSRoger Pau Monné 		 * ring has completed.
509a6aedc5dSRoger Pau Monné 		 */
510a6aedc5dSRoger Pau Monné 		wmb();
511a6aedc5dSRoger Pau Monné 		xen_store->req_prod += avail;
512a6aedc5dSRoger Pau Monné 
513a6aedc5dSRoger Pau Monné 		/*
514a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The other side will see
515a6aedc5dSRoger Pau Monné 		 * the change to req_prod at the time of the interrupt.
516a6aedc5dSRoger Pau Monné 		 */
517a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
518a6aedc5dSRoger Pau Monné 	}
519a6aedc5dSRoger Pau Monné 
520a6aedc5dSRoger Pau Monné 	return (0);
521a6aedc5dSRoger Pau Monné }
522a6aedc5dSRoger Pau Monné 
523a6aedc5dSRoger Pau Monné /**
524a6aedc5dSRoger Pau Monné  * Receive data from the XenStore service.
525a6aedc5dSRoger Pau Monné  *
526a6aedc5dSRoger Pau Monné  * \param tdata  A pointer to the contiguous buffer to receive the data.
527a6aedc5dSRoger Pau Monné  * \param len    The amount of data to receive.
528a6aedc5dSRoger Pau Monné  *
529a6aedc5dSRoger Pau Monné  * \return  On success 0, otherwise an errno value indicating the
530a6aedc5dSRoger Pau Monné  *          cause of failure.
531a6aedc5dSRoger Pau Monné  *
532a6aedc5dSRoger Pau Monné  * \invariant  Called from thread context.
533a6aedc5dSRoger Pau Monné  * \invariant  The buffer pointed to by tdata is at least len bytes
534a6aedc5dSRoger Pau Monné  *             in length.
535a6aedc5dSRoger Pau Monné  *
536a6aedc5dSRoger Pau Monné  * \note xs_read does not perform any internal locking to guarantee
537a6aedc5dSRoger Pau Monné  *       serial access to the incoming ring buffer.  However, there
538a6aedc5dSRoger Pau Monné  *	 is only one context processing reads: xs_rcv_thread().
539a6aedc5dSRoger Pau Monné  */
540a6aedc5dSRoger Pau Monné static int
xs_read_store(void * tdata,unsigned len)541a6aedc5dSRoger Pau Monné xs_read_store(void *tdata, unsigned len)
542a6aedc5dSRoger Pau Monné {
543a6aedc5dSRoger Pau Monné 	XENSTORE_RING_IDX cons, prod;
544a6aedc5dSRoger Pau Monné 	char *data = (char *)tdata;
545a6aedc5dSRoger Pau Monné 	int error;
546a6aedc5dSRoger Pau Monné 
547a6aedc5dSRoger Pau Monné 	while (len != 0) {
548a6aedc5dSRoger Pau Monné 		u_int avail;
549a6aedc5dSRoger Pau Monné 		const char *src;
550a6aedc5dSRoger Pau Monné 
551a6aedc5dSRoger Pau Monné 		/* Hold lock so we can't miss wakeups should we block. */
552a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.ring_lock);
553a6aedc5dSRoger Pau Monné 		cons = xen_store->rsp_cons;
554a6aedc5dSRoger Pau Monné 		prod = xen_store->rsp_prod;
555a6aedc5dSRoger Pau Monné 		if (cons == prod) {
556a6aedc5dSRoger Pau Monné 			/*
557a6aedc5dSRoger Pau Monné 			 * Nothing to read. Wait for a ring event.
558a6aedc5dSRoger Pau Monné 			 *
559a6aedc5dSRoger Pau Monné 			 * Note that the events from both queues
560a6aedc5dSRoger Pau Monné 			 * are combined, so being woken does not
561a6aedc5dSRoger Pau Monné 			 * guarantee that data exist in the read
562a6aedc5dSRoger Pau Monné 			 * ring.
563a6aedc5dSRoger Pau Monné 			 *
564a6aedc5dSRoger Pau Monné 			 * To simplify error recovery and the retry,
565a6aedc5dSRoger Pau Monné 			 * we specify PDROP so our lock is *not* held
566a6aedc5dSRoger Pau Monné 			 * when msleep returns.
567a6aedc5dSRoger Pau Monné 			 */
568a6aedc5dSRoger Pau Monné 			error = msleep(xen_store, &xs.ring_lock, PCATCH|PDROP,
569a6aedc5dSRoger Pau Monné 			    "xbread", /*timeout*/0);
570a6aedc5dSRoger Pau Monné 			if (error && error != EWOULDBLOCK)
571a6aedc5dSRoger Pau Monné 				return (error);
572a6aedc5dSRoger Pau Monné 			continue;
573a6aedc5dSRoger Pau Monné 		}
574a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.ring_lock);
575a6aedc5dSRoger Pau Monné 
576a6aedc5dSRoger Pau Monné 		/* Verify queue sanity. */
577a6aedc5dSRoger Pau Monné 		if (!xs_check_indexes(cons, prod)) {
578a6aedc5dSRoger Pau Monné 			xen_store->rsp_cons = xen_store->rsp_prod = 0;
579a6aedc5dSRoger Pau Monné 			return (EIO);
580a6aedc5dSRoger Pau Monné 		}
581a6aedc5dSRoger Pau Monné 
582a6aedc5dSRoger Pau Monné 		src = xs_get_input_chunk(cons, prod, xen_store->rsp, &avail);
583a6aedc5dSRoger Pau Monné 		if (avail > len)
584a6aedc5dSRoger Pau Monné 			avail = len;
585a6aedc5dSRoger Pau Monné 
586a6aedc5dSRoger Pau Monné 		/*
587a6aedc5dSRoger Pau Monné 		 * Insure the data we read is related to the indexes
588a6aedc5dSRoger Pau Monné 		 * we read above.
589a6aedc5dSRoger Pau Monné 		 */
590a6aedc5dSRoger Pau Monné 		rmb();
591a6aedc5dSRoger Pau Monné 
592a6aedc5dSRoger Pau Monné 		memcpy(data, src, avail);
593a6aedc5dSRoger Pau Monné 		data += avail;
594a6aedc5dSRoger Pau Monné 		len -= avail;
595a6aedc5dSRoger Pau Monné 
596a6aedc5dSRoger Pau Monné 		/*
597a6aedc5dSRoger Pau Monné 		 * Insure that the producer of this ring does not see
598a6aedc5dSRoger Pau Monné 		 * the ring space as free until after we have copied it
599a6aedc5dSRoger Pau Monné 		 * out.
600a6aedc5dSRoger Pau Monné 		 */
601a6aedc5dSRoger Pau Monné 		mb();
602a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons += avail;
603a6aedc5dSRoger Pau Monné 
604a6aedc5dSRoger Pau Monné 		/*
605a6aedc5dSRoger Pau Monné 		 * xen_intr_signal() implies mb(). The producer will see
606a6aedc5dSRoger Pau Monné 		 * the updated consumer index when the event is delivered.
607a6aedc5dSRoger Pau Monné 		 */
608a6aedc5dSRoger Pau Monné 		xen_intr_signal(xs.xen_intr_handle);
609a6aedc5dSRoger Pau Monné 	}
610a6aedc5dSRoger Pau Monné 
611a6aedc5dSRoger Pau Monné 	return (0);
612a6aedc5dSRoger Pau Monné }
613a6aedc5dSRoger Pau Monné 
614a6aedc5dSRoger Pau Monné /*----------------------- Received Message Processing ------------------------*/
615a6aedc5dSRoger Pau Monné /**
616a6aedc5dSRoger Pau Monné  * Block reading the next message from the XenStore service and
617a6aedc5dSRoger Pau Monné  * process the result.
618a6aedc5dSRoger Pau Monné  *
619a6aedc5dSRoger Pau Monné  * \param type  The returned type of the XenStore message received.
620a6aedc5dSRoger Pau Monné  *
621a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno value indicating the
622a6aedc5dSRoger Pau Monné  *          type of failure encountered.
623a6aedc5dSRoger Pau Monné  */
624a6aedc5dSRoger Pau Monné static int
xs_process_msg(enum xsd_sockmsg_type * type)625a6aedc5dSRoger Pau Monné xs_process_msg(enum xsd_sockmsg_type *type)
626a6aedc5dSRoger Pau Monné {
627a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
628a6aedc5dSRoger Pau Monné 	char *body;
629a6aedc5dSRoger Pau Monné 	int error;
630a6aedc5dSRoger Pau Monné 
631a6aedc5dSRoger Pau Monné 	msg = malloc(sizeof(*msg), M_XENSTORE, M_WAITOK);
632a6aedc5dSRoger Pau Monné 	error = xs_read_store(&msg->hdr, sizeof(msg->hdr));
633a6aedc5dSRoger Pau Monné 	if (error) {
634a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
635a6aedc5dSRoger Pau Monné 		return (error);
636a6aedc5dSRoger Pau Monné 	}
637a6aedc5dSRoger Pau Monné 
638a6aedc5dSRoger Pau Monné 	body = malloc(msg->hdr.len + 1, M_XENSTORE, M_WAITOK);
639a6aedc5dSRoger Pau Monné 	error = xs_read_store(body, msg->hdr.len);
640a6aedc5dSRoger Pau Monné 	if (error) {
641a6aedc5dSRoger Pau Monné 		free(body, M_XENSTORE);
642a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
643a6aedc5dSRoger Pau Monné 		return (error);
644a6aedc5dSRoger Pau Monné 	}
645a6aedc5dSRoger Pau Monné 	body[msg->hdr.len] = '\0';
646a6aedc5dSRoger Pau Monné 
647a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
648a6aedc5dSRoger Pau Monné 	if (msg->hdr.type == XS_WATCH_EVENT) {
649a6aedc5dSRoger Pau Monné 		msg->u.watch.vec = split(body, msg->hdr.len,
650a6aedc5dSRoger Pau Monné 		    &msg->u.watch.vec_size);
651a6aedc5dSRoger Pau Monné 
652a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
653a6aedc5dSRoger Pau Monné 		msg->u.watch.handle = find_watch(
654a6aedc5dSRoger Pau Monné 		    msg->u.watch.vec[XS_WATCH_TOKEN]);
655a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
6564e4e43dcSRoger Pau Monné 		if (msg->u.watch.handle != NULL &&
6574e4e43dcSRoger Pau Monné 		    (!msg->u.watch.handle->max_pending ||
6584e4e43dcSRoger Pau Monné 		    msg->u.watch.handle->pending <
6594e4e43dcSRoger Pau Monné 		    msg->u.watch.handle->max_pending)) {
6604e4e43dcSRoger Pau Monné 			msg->u.watch.handle->pending++;
661a6aedc5dSRoger Pau Monné 			TAILQ_INSERT_TAIL(&xs.watch_events, msg, list);
662a6aedc5dSRoger Pau Monné 			wakeup(&xs.watch_events);
663a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.watch_events_lock);
664a6aedc5dSRoger Pau Monné 		} else {
6654e4e43dcSRoger Pau Monné 			mtx_unlock(&xs.watch_events_lock);
666a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
667a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
668a6aedc5dSRoger Pau Monné 		}
669a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
670a6aedc5dSRoger Pau Monné 	} else {
671a6aedc5dSRoger Pau Monné 		msg->u.reply.body = body;
672a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.reply_lock);
673a6aedc5dSRoger Pau Monné 		TAILQ_INSERT_TAIL(&xs.reply_list, msg, list);
674a6aedc5dSRoger Pau Monné 		wakeup(&xs.reply_list);
675a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.reply_lock);
676a6aedc5dSRoger Pau Monné 	}
677a6aedc5dSRoger Pau Monné 
678a6aedc5dSRoger Pau Monné 	return (0);
679a6aedc5dSRoger Pau Monné }
680a6aedc5dSRoger Pau Monné 
681a6aedc5dSRoger Pau Monné /**
682a6aedc5dSRoger Pau Monné  * Thread body of the XenStore receive thread.
683a6aedc5dSRoger Pau Monné  *
684a6aedc5dSRoger Pau Monné  * This thread blocks waiting for data from the XenStore service
685a6aedc5dSRoger Pau Monné  * and processes and received messages.
686a6aedc5dSRoger Pau Monné  */
687a6aedc5dSRoger Pau Monné static void
xs_rcv_thread(void * arg __unused)688a6aedc5dSRoger Pau Monné xs_rcv_thread(void *arg __unused)
689a6aedc5dSRoger Pau Monné {
690a6aedc5dSRoger Pau Monné 	int error;
691a6aedc5dSRoger Pau Monné 	enum xsd_sockmsg_type type;
692a6aedc5dSRoger Pau Monné 
693a6aedc5dSRoger Pau Monné 	for (;;) {
694a6aedc5dSRoger Pau Monné 		error = xs_process_msg(&type);
695a6aedc5dSRoger Pau Monné 		if (error)
696a6aedc5dSRoger Pau Monné 			printf("XENSTORE error %d while reading message\n",
697a6aedc5dSRoger Pau Monné 			    error);
698a6aedc5dSRoger Pau Monné 	}
699a6aedc5dSRoger Pau Monné }
700a6aedc5dSRoger Pau Monné 
701a6aedc5dSRoger Pau Monné /*---------------- XenStore Message Request/Reply Processing -----------------*/
702a6aedc5dSRoger Pau Monné #define xsd_error_count	(sizeof(xsd_errors) / sizeof(xsd_errors[0]))
703a6aedc5dSRoger Pau Monné 
704a6aedc5dSRoger Pau Monné /**
705a6aedc5dSRoger Pau Monné  * Convert a XenStore error string into an errno number.
706a6aedc5dSRoger Pau Monné  *
707a6aedc5dSRoger Pau Monné  * \param errorstring  The error string to convert.
708a6aedc5dSRoger Pau Monné  *
709a6aedc5dSRoger Pau Monné  * \return  The errno best matching the input string.
710a6aedc5dSRoger Pau Monné  *
711a6aedc5dSRoger Pau Monné  * \note Unknown error strings are converted to EINVAL.
712a6aedc5dSRoger Pau Monné  */
713a6aedc5dSRoger Pau Monné static int
xs_get_error(const char * errorstring)714a6aedc5dSRoger Pau Monné xs_get_error(const char *errorstring)
715a6aedc5dSRoger Pau Monné {
716a6aedc5dSRoger Pau Monné 	u_int i;
717a6aedc5dSRoger Pau Monné 
718a6aedc5dSRoger Pau Monné 	for (i = 0; i < xsd_error_count; i++) {
719a6aedc5dSRoger Pau Monné 		if (!strcmp(errorstring, xsd_errors[i].errstring))
720a6aedc5dSRoger Pau Monné 			return (xsd_errors[i].errnum);
721a6aedc5dSRoger Pau Monné 	}
722a6aedc5dSRoger Pau Monné 	log(LOG_WARNING, "XENSTORE xen store gave: unknown error %s",
723a6aedc5dSRoger Pau Monné 	    errorstring);
724a6aedc5dSRoger Pau Monné 	return (EINVAL);
725a6aedc5dSRoger Pau Monné }
726a6aedc5dSRoger Pau Monné 
727a6aedc5dSRoger Pau Monné /**
728a6aedc5dSRoger Pau Monné  * Block waiting for a reply to a message request.
729a6aedc5dSRoger Pau Monné  *
730a6aedc5dSRoger Pau Monné  * \param type	  The returned type of the reply.
731a6aedc5dSRoger Pau Monné  * \param len	  The returned body length of the reply.
732a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
733a6aedc5dSRoger Pau Monné  *
734a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
735a6aedc5dSRoger Pau Monné  *          cause of failure.
736a6aedc5dSRoger Pau Monné  */
737a6aedc5dSRoger Pau Monné static int
xs_read_reply(enum xsd_sockmsg_type * type,u_int * len,void ** result)738a6aedc5dSRoger Pau Monné xs_read_reply(enum xsd_sockmsg_type *type, u_int *len, void **result)
739a6aedc5dSRoger Pau Monné {
740a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
741a6aedc5dSRoger Pau Monné 	char *body;
742a6aedc5dSRoger Pau Monné 	int error;
743a6aedc5dSRoger Pau Monné 
744a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.reply_lock);
745a6aedc5dSRoger Pau Monné 	while (TAILQ_EMPTY(&xs.reply_list)) {
7467c743c89SRoger Pau Monné 		error = mtx_sleep(&xs.reply_list, &xs.reply_lock, 0, "xswait",
7477c743c89SRoger Pau Monné 		    hz/10);
748a6aedc5dSRoger Pau Monné 		if (error && error != EWOULDBLOCK) {
749a6aedc5dSRoger Pau Monné 			mtx_unlock(&xs.reply_lock);
750a6aedc5dSRoger Pau Monné 			return (error);
751a6aedc5dSRoger Pau Monné 		}
752a6aedc5dSRoger Pau Monné 	}
753a6aedc5dSRoger Pau Monné 	msg = TAILQ_FIRST(&xs.reply_list);
754a6aedc5dSRoger Pau Monné 	TAILQ_REMOVE(&xs.reply_list, msg, list);
755a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.reply_lock);
756a6aedc5dSRoger Pau Monné 
757a6aedc5dSRoger Pau Monné 	*type = msg->hdr.type;
758a6aedc5dSRoger Pau Monné 	if (len)
759a6aedc5dSRoger Pau Monné 		*len = msg->hdr.len;
760a6aedc5dSRoger Pau Monné 	body = msg->u.reply.body;
761a6aedc5dSRoger Pau Monné 
762a6aedc5dSRoger Pau Monné 	free(msg, M_XENSTORE);
763a6aedc5dSRoger Pau Monné 	*result = body;
764a6aedc5dSRoger Pau Monné 	return (0);
765a6aedc5dSRoger Pau Monné }
766a6aedc5dSRoger Pau Monné 
767a6aedc5dSRoger Pau Monné /**
768a6aedc5dSRoger Pau Monné  * Pass-thru interface for XenStore access by userland processes
769a6aedc5dSRoger Pau Monné  * via the XenStore device.
770a6aedc5dSRoger Pau Monné  *
771a6aedc5dSRoger Pau Monné  * Reply type and length data are returned by overwriting these
772a6aedc5dSRoger Pau Monné  * fields in the passed in request message.
773a6aedc5dSRoger Pau Monné  *
774a6aedc5dSRoger Pau Monné  * \param msg	  A properly formatted message to transmit to
775a6aedc5dSRoger Pau Monné  *		  the XenStore service.
776a6aedc5dSRoger Pau Monné  * \param result  The returned body of the reply.
777a6aedc5dSRoger Pau Monné  *
778a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the cause
779a6aedc5dSRoger Pau Monné  *          of failure.
780a6aedc5dSRoger Pau Monné  *
781a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
782a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(result, M_XENSTORE);
783a6aedc5dSRoger Pau Monné  */
784a6aedc5dSRoger Pau Monné int
xs_dev_request_and_reply(struct xsd_sockmsg * msg,void ** result)785a6aedc5dSRoger Pau Monné xs_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
786a6aedc5dSRoger Pau Monné {
787a6aedc5dSRoger Pau Monné 	int error;
788a6aedc5dSRoger Pau Monné 
789a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
790a6aedc5dSRoger Pau Monné 	if ((error = xs_write_store(msg, sizeof(*msg) + msg->len)) == 0)
791a6aedc5dSRoger Pau Monné 		error = xs_read_reply(&msg->type, &msg->len, result);
792a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
793a6aedc5dSRoger Pau Monné 
794a6aedc5dSRoger Pau Monné 	return (error);
795a6aedc5dSRoger Pau Monné }
796a6aedc5dSRoger Pau Monné 
797a6aedc5dSRoger Pau Monné /**
798a6aedc5dSRoger Pau Monné  * Send a message with an optionally muti-part body to the XenStore service.
799a6aedc5dSRoger Pau Monné  *
800a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
801a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
802a6aedc5dSRoger Pau Monné  * \param iovec          Pointers to the body sections of the request.
803a6aedc5dSRoger Pau Monné  * \param num_vecs       The number of body sections in the request.
804a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
805a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
806a6aedc5dSRoger Pau Monné  *
807a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
808a6aedc5dSRoger Pau Monné  *          the cause of failure.
809a6aedc5dSRoger Pau Monné  *
810a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
811a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
812a6aedc5dSRoger Pau Monné  */
813a6aedc5dSRoger Pau Monné static int
xs_talkv(struct xs_transaction t,enum xsd_sockmsg_type request_type,const struct iovec * iovec,u_int num_vecs,u_int * len,void ** result)814a6aedc5dSRoger Pau Monné xs_talkv(struct xs_transaction t, enum xsd_sockmsg_type request_type,
815a6aedc5dSRoger Pau Monné     const struct iovec *iovec, u_int num_vecs, u_int *len, void **result)
816a6aedc5dSRoger Pau Monné {
817a6aedc5dSRoger Pau Monné 	struct xsd_sockmsg msg;
818a6aedc5dSRoger Pau Monné 	void *ret = NULL;
819a6aedc5dSRoger Pau Monné 	u_int i;
820a6aedc5dSRoger Pau Monné 	int error;
821a6aedc5dSRoger Pau Monné 
822a6aedc5dSRoger Pau Monné 	msg.tx_id = t.id;
823a6aedc5dSRoger Pau Monné 	msg.req_id = 0;
824a6aedc5dSRoger Pau Monné 	msg.type = request_type;
825a6aedc5dSRoger Pau Monné 	msg.len = 0;
826a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++)
827a6aedc5dSRoger Pau Monné 		msg.len += iovec[i].iov_len;
828a6aedc5dSRoger Pau Monné 
829a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
830a6aedc5dSRoger Pau Monné 	error = xs_write_store(&msg, sizeof(msg));
831a6aedc5dSRoger Pau Monné 	if (error) {
832a6aedc5dSRoger Pau Monné 		printf("xs_talkv failed %d\n", error);
833a6aedc5dSRoger Pau Monné 		goto error_lock_held;
834a6aedc5dSRoger Pau Monné 	}
835a6aedc5dSRoger Pau Monné 
836a6aedc5dSRoger Pau Monné 	for (i = 0; i < num_vecs; i++) {
837a6aedc5dSRoger Pau Monné 		error = xs_write_store(iovec[i].iov_base, iovec[i].iov_len);
838a6aedc5dSRoger Pau Monné 		if (error) {
839a6aedc5dSRoger Pau Monné 			printf("xs_talkv failed %d\n", error);
840a6aedc5dSRoger Pau Monné 			goto error_lock_held;
841a6aedc5dSRoger Pau Monné 		}
842a6aedc5dSRoger Pau Monné 	}
843a6aedc5dSRoger Pau Monné 
844a6aedc5dSRoger Pau Monné 	error = xs_read_reply(&msg.type, len, &ret);
845a6aedc5dSRoger Pau Monné 
846a6aedc5dSRoger Pau Monné error_lock_held:
847a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
848a6aedc5dSRoger Pau Monné 	if (error)
849a6aedc5dSRoger Pau Monné 		return (error);
850a6aedc5dSRoger Pau Monné 
851a6aedc5dSRoger Pau Monné 	if (msg.type == XS_ERROR) {
852a6aedc5dSRoger Pau Monné 		error = xs_get_error(ret);
853a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
854a6aedc5dSRoger Pau Monné 		return (error);
855a6aedc5dSRoger Pau Monné 	}
856a6aedc5dSRoger Pau Monné 
857a6aedc5dSRoger Pau Monné 	/* Reply is either error or an echo of our request message type. */
858a6aedc5dSRoger Pau Monné 	KASSERT(msg.type == request_type, ("bad xenstore message type"));
859a6aedc5dSRoger Pau Monné 
860a6aedc5dSRoger Pau Monné 	if (result)
861a6aedc5dSRoger Pau Monné 		*result = ret;
862a6aedc5dSRoger Pau Monné 	else
863a6aedc5dSRoger Pau Monné 		free(ret, M_XENSTORE);
864a6aedc5dSRoger Pau Monné 
865a6aedc5dSRoger Pau Monné 	return (0);
866a6aedc5dSRoger Pau Monné }
867a6aedc5dSRoger Pau Monné 
868a6aedc5dSRoger Pau Monné /**
869a6aedc5dSRoger Pau Monné  * Wrapper for xs_talkv allowing easy transmission of a message with
870a6aedc5dSRoger Pau Monné  * a single, contiguous, message body.
871a6aedc5dSRoger Pau Monné  *
872a6aedc5dSRoger Pau Monné  * \param t              The transaction to use for this request.
873a6aedc5dSRoger Pau Monné  * \param request_type   The type of message to send.
874a6aedc5dSRoger Pau Monné  * \param body           The body of the request.
875a6aedc5dSRoger Pau Monné  * \param len            The returned length of the reply.
876a6aedc5dSRoger Pau Monné  * \param result         The returned body of the reply.
877a6aedc5dSRoger Pau Monné  *
878a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating
879a6aedc5dSRoger Pau Monné  *          the cause of failure.
880a6aedc5dSRoger Pau Monné  *
881a6aedc5dSRoger Pau Monné  * \note The returned result is provided in malloced storage and thus
882a6aedc5dSRoger Pau Monné  *       must be free'd by the caller with 'free(*result, M_XENSTORE);
883a6aedc5dSRoger Pau Monné  */
884a6aedc5dSRoger Pau Monné static int
xs_single(struct xs_transaction t,enum xsd_sockmsg_type request_type,const char * body,u_int * len,void ** result)885a6aedc5dSRoger Pau Monné xs_single(struct xs_transaction t, enum xsd_sockmsg_type request_type,
886a6aedc5dSRoger Pau Monné     const char *body, u_int *len, void **result)
887a6aedc5dSRoger Pau Monné {
888a6aedc5dSRoger Pau Monné 	struct iovec iovec;
889a6aedc5dSRoger Pau Monné 
890a6aedc5dSRoger Pau Monné 	iovec.iov_base = (void *)(uintptr_t)body;
891a6aedc5dSRoger Pau Monné 	iovec.iov_len = strlen(body) + 1;
892a6aedc5dSRoger Pau Monné 
893a6aedc5dSRoger Pau Monné 	return (xs_talkv(t, request_type, &iovec, 1, len, result));
894a6aedc5dSRoger Pau Monné }
895a6aedc5dSRoger Pau Monné 
896a6aedc5dSRoger Pau Monné /*------------------------- XenStore Watch Support ---------------------------*/
897a6aedc5dSRoger Pau Monné /**
898a6aedc5dSRoger Pau Monné  * Transmit a watch request to the XenStore service.
899a6aedc5dSRoger Pau Monné  *
900a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
901a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
902a6aedc5dSRoger Pau Monné  *
903a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
904a6aedc5dSRoger Pau Monné  *          cause of failure.
905a6aedc5dSRoger Pau Monné  */
906a6aedc5dSRoger Pau Monné static int
xs_watch(const char * path,const char * token)907a6aedc5dSRoger Pau Monné xs_watch(const char *path, const char *token)
908a6aedc5dSRoger Pau Monné {
909a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
910a6aedc5dSRoger Pau Monné 
911a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
912a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
913a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
914a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
915a6aedc5dSRoger Pau Monné 
916a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_WATCH, iov, 2, NULL, NULL));
917a6aedc5dSRoger Pau Monné }
918a6aedc5dSRoger Pau Monné 
919a6aedc5dSRoger Pau Monné /**
920a6aedc5dSRoger Pau Monné  * Transmit an uwatch request to the XenStore service.
921a6aedc5dSRoger Pau Monné  *
922a6aedc5dSRoger Pau Monné  * \param path    The path in the XenStore to watch.
923a6aedc5dSRoger Pau Monné  * \param tocken  A unique identifier for this watch.
924a6aedc5dSRoger Pau Monné  *
925a6aedc5dSRoger Pau Monné  * \return  0 on success.  Otherwise an errno indicating the
926a6aedc5dSRoger Pau Monné  *          cause of failure.
927a6aedc5dSRoger Pau Monné  */
928a6aedc5dSRoger Pau Monné static int
xs_unwatch(const char * path,const char * token)929a6aedc5dSRoger Pau Monné xs_unwatch(const char *path, const char *token)
930a6aedc5dSRoger Pau Monné {
931a6aedc5dSRoger Pau Monné 	struct iovec iov[2];
932a6aedc5dSRoger Pau Monné 
933a6aedc5dSRoger Pau Monné 	iov[0].iov_base = (void *)(uintptr_t) path;
934a6aedc5dSRoger Pau Monné 	iov[0].iov_len = strlen(path) + 1;
935a6aedc5dSRoger Pau Monné 	iov[1].iov_base = (void *)(uintptr_t) token;
936a6aedc5dSRoger Pau Monné 	iov[1].iov_len = strlen(token) + 1;
937a6aedc5dSRoger Pau Monné 
938a6aedc5dSRoger Pau Monné 	return (xs_talkv(XST_NIL, XS_UNWATCH, iov, 2, NULL, NULL));
939a6aedc5dSRoger Pau Monné }
940a6aedc5dSRoger Pau Monné 
941a6aedc5dSRoger Pau Monné /**
942a6aedc5dSRoger Pau Monné  * Convert from watch token (unique identifier) to the associated
943a6aedc5dSRoger Pau Monné  * internal tracking structure for this watch.
944a6aedc5dSRoger Pau Monné  *
945a6aedc5dSRoger Pau Monné  * \param tocken  The unique identifier for the watch to find.
946a6aedc5dSRoger Pau Monné  *
947a6aedc5dSRoger Pau Monné  * \return  A pointer to the found watch structure or NULL.
948a6aedc5dSRoger Pau Monné  */
949a6aedc5dSRoger Pau Monné static struct xs_watch *
find_watch(const char * token)950a6aedc5dSRoger Pau Monné find_watch(const char *token)
951a6aedc5dSRoger Pau Monné {
952a6aedc5dSRoger Pau Monné 	struct xs_watch *i, *cmp;
953a6aedc5dSRoger Pau Monné 
954a6aedc5dSRoger Pau Monné 	cmp = (void *)strtoul(token, NULL, 16);
955a6aedc5dSRoger Pau Monné 
956a6aedc5dSRoger Pau Monné 	LIST_FOREACH(i, &xs.registered_watches, list)
957a6aedc5dSRoger Pau Monné 		if (i == cmp)
958a6aedc5dSRoger Pau Monné 			return (i);
959a6aedc5dSRoger Pau Monné 
960a6aedc5dSRoger Pau Monné 	return (NULL);
961a6aedc5dSRoger Pau Monné }
962a6aedc5dSRoger Pau Monné 
963a6aedc5dSRoger Pau Monné /**
964a6aedc5dSRoger Pau Monné  * Thread body of the XenStore watch event dispatch thread.
965a6aedc5dSRoger Pau Monné  */
966a6aedc5dSRoger Pau Monné static void
xenwatch_thread(void * unused)967a6aedc5dSRoger Pau Monné xenwatch_thread(void *unused)
968a6aedc5dSRoger Pau Monné {
969a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg;
970a6aedc5dSRoger Pau Monné 
971a6aedc5dSRoger Pau Monné 	for (;;) {
972a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
973a6aedc5dSRoger Pau Monné 		while (TAILQ_EMPTY(&xs.watch_events))
974a6aedc5dSRoger Pau Monné 			mtx_sleep(&xs.watch_events,
975a6aedc5dSRoger Pau Monné 			    &xs.watch_events_lock,
976a6aedc5dSRoger Pau Monné 			    PWAIT | PCATCH, "waitev", hz/10);
977a6aedc5dSRoger Pau Monné 
978a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
979a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
980a6aedc5dSRoger Pau Monné 
981a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.watch_events_lock);
982a6aedc5dSRoger Pau Monné 		msg = TAILQ_FIRST(&xs.watch_events);
9834e4e43dcSRoger Pau Monné 		if (msg) {
984a6aedc5dSRoger Pau Monné 			TAILQ_REMOVE(&xs.watch_events, msg, list);
9854e4e43dcSRoger Pau Monné 			msg->u.watch.handle->pending--;
9864e4e43dcSRoger Pau Monné 		}
987a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.watch_events_lock);
988a6aedc5dSRoger Pau Monné 
989a6aedc5dSRoger Pau Monné 		if (msg != NULL) {
990a6aedc5dSRoger Pau Monné 			/*
991a6aedc5dSRoger Pau Monné 			 * XXX There are messages coming in with a NULL
992a6aedc5dSRoger Pau Monné 			 * XXX callback.  This deserves further investigation;
993a6aedc5dSRoger Pau Monné 			 * XXX the workaround here simply prevents the kernel
994a6aedc5dSRoger Pau Monné 			 * XXX from panic'ing on startup.
995a6aedc5dSRoger Pau Monné 			 */
996a6aedc5dSRoger Pau Monné 			if (msg->u.watch.handle->callback != NULL)
997a6aedc5dSRoger Pau Monné 				msg->u.watch.handle->callback(
998a6aedc5dSRoger Pau Monné 					msg->u.watch.handle,
999a6aedc5dSRoger Pau Monné 					(const char **)msg->u.watch.vec,
1000a6aedc5dSRoger Pau Monné 					msg->u.watch.vec_size);
1001a6aedc5dSRoger Pau Monné 			free(msg->u.watch.vec, M_XENSTORE);
1002a6aedc5dSRoger Pau Monné 			free(msg, M_XENSTORE);
1003a6aedc5dSRoger Pau Monné 		}
1004a6aedc5dSRoger Pau Monné 
1005a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1006a6aedc5dSRoger Pau Monné 	}
1007a6aedc5dSRoger Pau Monné }
1008a6aedc5dSRoger Pau Monné 
1009a6aedc5dSRoger Pau Monné /*----------- XenStore Configuration, Initialization, and Control ------------*/
1010a6aedc5dSRoger Pau Monné /**
1011a6aedc5dSRoger Pau Monné  * Setup communication channels with the XenStore service.
1012a6aedc5dSRoger Pau Monné  *
1013a6aedc5dSRoger Pau Monné  * \return  On success, 0. Otherwise an errno value indicating the
1014a6aedc5dSRoger Pau Monné  *          type of failure.
1015a6aedc5dSRoger Pau Monné  */
1016a6aedc5dSRoger Pau Monné static int
xs_init_comms(void)1017a6aedc5dSRoger Pau Monné xs_init_comms(void)
1018a6aedc5dSRoger Pau Monné {
1019a6aedc5dSRoger Pau Monné 	int error;
1020a6aedc5dSRoger Pau Monné 
1021a6aedc5dSRoger Pau Monné 	if (xen_store->rsp_prod != xen_store->rsp_cons) {
1022a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE response ring is not quiescent "
1023a6aedc5dSRoger Pau Monné 		    "(%08x:%08x): fixing up\n",
1024a6aedc5dSRoger Pau Monné 		    xen_store->rsp_cons, xen_store->rsp_prod);
1025a6aedc5dSRoger Pau Monné 		xen_store->rsp_cons = xen_store->rsp_prod;
1026a6aedc5dSRoger Pau Monné 	}
1027a6aedc5dSRoger Pau Monné 
1028a6aedc5dSRoger Pau Monné 	xen_intr_unbind(&xs.xen_intr_handle);
1029a6aedc5dSRoger Pau Monné 
1030a6aedc5dSRoger Pau Monné 	error = xen_intr_bind_local_port(xs.xs_dev, xs.evtchn,
1031a6aedc5dSRoger Pau Monné 	    /*filter*/NULL, xs_intr, /*arg*/NULL, INTR_TYPE_NET|INTR_MPSAFE,
1032a6aedc5dSRoger Pau Monné 	    &xs.xen_intr_handle);
1033a6aedc5dSRoger Pau Monné 	if (error) {
1034a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE request irq failed %i\n", error);
1035a6aedc5dSRoger Pau Monné 		return (error);
1036a6aedc5dSRoger Pau Monné 	}
1037a6aedc5dSRoger Pau Monné 
1038a6aedc5dSRoger Pau Monné 	return (0);
1039a6aedc5dSRoger Pau Monné }
1040a6aedc5dSRoger Pau Monné 
1041a6aedc5dSRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
1042a6aedc5dSRoger Pau Monné static void
xs_identify(driver_t * driver,device_t parent)1043a6aedc5dSRoger Pau Monné xs_identify(driver_t *driver, device_t parent)
1044a6aedc5dSRoger Pau Monné {
1045a6aedc5dSRoger Pau Monné 
1046a6aedc5dSRoger Pau Monné 	BUS_ADD_CHILD(parent, 0, "xenstore", 0);
1047a6aedc5dSRoger Pau Monné }
1048a6aedc5dSRoger Pau Monné 
1049a6aedc5dSRoger Pau Monné /**
1050453130d9SPedro F. Giffuni  * Probe for the existence of the XenStore.
1051a6aedc5dSRoger Pau Monné  *
1052a6aedc5dSRoger Pau Monné  * \param dev
1053a6aedc5dSRoger Pau Monné  */
1054a6aedc5dSRoger Pau Monné static int
xs_probe(device_t dev)1055a6aedc5dSRoger Pau Monné xs_probe(device_t dev)
1056a6aedc5dSRoger Pau Monné {
1057a6aedc5dSRoger Pau Monné 	/*
1058a6aedc5dSRoger Pau Monné 	 * We are either operating within a PV kernel or being probed
1059a6aedc5dSRoger Pau Monné 	 * as the child of the successfully attached xenpci device.
1060a6aedc5dSRoger Pau Monné 	 * Thus we are in a Xen environment and there will be a XenStore.
1061a6aedc5dSRoger Pau Monné 	 * Unconditionally return success.
1062a6aedc5dSRoger Pau Monné 	 */
1063a6aedc5dSRoger Pau Monné 	device_set_desc(dev, "XenStore");
10642f9ec994SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
1065a6aedc5dSRoger Pau Monné }
1066a6aedc5dSRoger Pau Monné 
1067a6aedc5dSRoger Pau Monné static void
xs_attach_deferred(void * arg)1068a6aedc5dSRoger Pau Monné xs_attach_deferred(void *arg)
1069a6aedc5dSRoger Pau Monné {
1070a6aedc5dSRoger Pau Monné 
1071a6aedc5dSRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
1072a6aedc5dSRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
1073a6aedc5dSRoger Pau Monné 
1074a6aedc5dSRoger Pau Monné 	config_intrhook_disestablish(&xs.xs_attachcb);
1075a6aedc5dSRoger Pau Monné }
1076a6aedc5dSRoger Pau Monné 
10771d84e2b3SRoger Pau Monné static void
xs_attach_late(void * arg,int pending)10781d84e2b3SRoger Pau Monné xs_attach_late(void *arg, int pending)
10791d84e2b3SRoger Pau Monné {
10801d84e2b3SRoger Pau Monné 
10811d84e2b3SRoger Pau Monné 	KASSERT((pending == 1), ("xs late attach queued several times"));
10821d84e2b3SRoger Pau Monné 	bus_generic_probe(xs.xs_dev);
10831d84e2b3SRoger Pau Monné 	bus_generic_attach(xs.xs_dev);
10841d84e2b3SRoger Pau Monné }
10851d84e2b3SRoger Pau Monné 
1086a6aedc5dSRoger Pau Monné /**
1087a6aedc5dSRoger Pau Monné  * Attach to the XenStore.
1088a6aedc5dSRoger Pau Monné  *
1089a6aedc5dSRoger Pau Monné  * This routine also prepares for the probe/attach of drivers that rely
1090a6aedc5dSRoger Pau Monné  * on the XenStore.
1091a6aedc5dSRoger Pau Monné  */
1092a6aedc5dSRoger Pau Monné static int
xs_attach(device_t dev)1093a6aedc5dSRoger Pau Monné xs_attach(device_t dev)
1094a6aedc5dSRoger Pau Monné {
1095a6aedc5dSRoger Pau Monné 	int error;
1096a6aedc5dSRoger Pau Monné 
1097a6aedc5dSRoger Pau Monné 	/* Allow us to get device_t from softc and vice-versa. */
1098a6aedc5dSRoger Pau Monné 	xs.xs_dev = dev;
1099a6aedc5dSRoger Pau Monné 	device_set_softc(dev, &xs);
1100a6aedc5dSRoger Pau Monné 
1101a6aedc5dSRoger Pau Monné 	/* Initialize the interface to xenstore. */
1102a6aedc5dSRoger Pau Monné 	struct proc *p;
1103a6aedc5dSRoger Pau Monné 
11041d84e2b3SRoger Pau Monné 	xs.initialized = false;
1105cfa0b7b8SRoger Pau Monné 	xs.evtchn = xen_get_xenstore_evtchn();
1106cfa0b7b8SRoger Pau Monné 	if (xs.evtchn == 0) {
11071d84e2b3SRoger Pau Monné 		struct evtchn_alloc_unbound alloc_unbound;
11081d84e2b3SRoger Pau Monné 
11091d84e2b3SRoger Pau Monné 		/* Allocate a local event channel for xenstore */
11101d84e2b3SRoger Pau Monné 		alloc_unbound.dom = DOMID_SELF;
11111d84e2b3SRoger Pau Monné 		alloc_unbound.remote_dom = DOMID_SELF;
11121d84e2b3SRoger Pau Monné 		error = HYPERVISOR_event_channel_op(
11131d84e2b3SRoger Pau Monné 		    EVTCHNOP_alloc_unbound, &alloc_unbound);
11141d84e2b3SRoger Pau Monné 		if (error != 0)
11151d84e2b3SRoger Pau Monné 			panic(
11161d84e2b3SRoger Pau Monné 			   "unable to alloc event channel for Dom0: %d",
11171d84e2b3SRoger Pau Monné 			    error);
11181d84e2b3SRoger Pau Monné 
11191d84e2b3SRoger Pau Monné 		xs.evtchn = alloc_unbound.port;
11201d84e2b3SRoger Pau Monné 
11211d84e2b3SRoger Pau Monné 		/* Allocate memory for the xs shared ring */
1122cfa0b7b8SRoger Pau Monné 		xen_store = malloc(PAGE_SIZE, M_XENSTORE, M_WAITOK | M_ZERO);
1123cfa0b7b8SRoger Pau Monné 		xs.gpfn = atop(pmap_kextract((vm_offset_t)xen_store));
11241d84e2b3SRoger Pau Monné 	} else {
1125cfa0b7b8SRoger Pau Monné 		xs.gpfn = xen_get_xenstore_mfn();
1126cfa0b7b8SRoger Pau Monné 		xen_store = pmap_mapdev_attr(ptoa(xs.gpfn), PAGE_SIZE,
1127b6ff9345SElliott Mitchell 		    VM_MEMATTR_XEN);
11281d84e2b3SRoger Pau Monné 		xs.initialized = true;
11291d84e2b3SRoger Pau Monné 	}
1130a6aedc5dSRoger Pau Monné 
1131a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.reply_list);
1132a6aedc5dSRoger Pau Monné 	TAILQ_INIT(&xs.watch_events);
1133a6aedc5dSRoger Pau Monné 
1134a6aedc5dSRoger Pau Monné 	mtx_init(&xs.ring_lock, "ring lock", NULL, MTX_DEF);
1135a6aedc5dSRoger Pau Monné 	mtx_init(&xs.reply_lock, "reply lock", NULL, MTX_DEF);
1136a6aedc5dSRoger Pau Monné 	sx_init(&xs.xenwatch_mutex, "xenwatch");
1137a6aedc5dSRoger Pau Monné 	sx_init(&xs.request_mutex, "xenstore request");
1138a6aedc5dSRoger Pau Monné 	mtx_init(&xs.registered_watches_lock, "watches", NULL, MTX_DEF);
1139a6aedc5dSRoger Pau Monné 	mtx_init(&xs.watch_events_lock, "watch events", NULL, MTX_DEF);
1140a6aedc5dSRoger Pau Monné 
1141a6aedc5dSRoger Pau Monné 	/* Initialize the shared memory rings to talk to xenstored */
1142a6aedc5dSRoger Pau Monné 	error = xs_init_comms();
1143a6aedc5dSRoger Pau Monné 	if (error)
1144a6aedc5dSRoger Pau Monné 		return (error);
1145a6aedc5dSRoger Pau Monné 
1146a6aedc5dSRoger Pau Monné 	error = kproc_create(xenwatch_thread, NULL, &p, RFHIGHPID,
1147a6aedc5dSRoger Pau Monné 	    0, "xenwatch");
1148a6aedc5dSRoger Pau Monné 	if (error)
1149a6aedc5dSRoger Pau Monné 		return (error);
1150a6aedc5dSRoger Pau Monné 	xs.xenwatch_pid = p->p_pid;
1151a6aedc5dSRoger Pau Monné 
1152a6aedc5dSRoger Pau Monné 	error = kproc_create(xs_rcv_thread, NULL, NULL,
1153a6aedc5dSRoger Pau Monné 	    RFHIGHPID, 0, "xenstore_rcv");
1154a6aedc5dSRoger Pau Monné 
1155a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_func = xs_attach_deferred;
1156a6aedc5dSRoger Pau Monné 	xs.xs_attachcb.ich_arg = NULL;
11571d84e2b3SRoger Pau Monné 	if (xs.initialized) {
1158a6aedc5dSRoger Pau Monné 		config_intrhook_establish(&xs.xs_attachcb);
11591d84e2b3SRoger Pau Monné 	} else {
11601d84e2b3SRoger Pau Monné 		TASK_INIT(&xs.xs_late_init, 0, xs_attach_late, NULL);
11611d84e2b3SRoger Pau Monné 	}
1162a6aedc5dSRoger Pau Monné 
1163a6aedc5dSRoger Pau Monné 	return (error);
1164a6aedc5dSRoger Pau Monné }
1165a6aedc5dSRoger Pau Monné 
1166a6aedc5dSRoger Pau Monné /**
1167a6aedc5dSRoger Pau Monné  * Prepare for suspension of this VM by halting XenStore access after
1168a6aedc5dSRoger Pau Monné  * all transactions and individual requests have completed.
1169a6aedc5dSRoger Pau Monné  */
1170a6aedc5dSRoger Pau Monné static int
xs_suspend(device_t dev)1171a6aedc5dSRoger Pau Monné xs_suspend(device_t dev)
1172a6aedc5dSRoger Pau Monné {
1173a6aedc5dSRoger Pau Monné 	int error;
1174a6aedc5dSRoger Pau Monné 
1175a6aedc5dSRoger Pau Monné 	/* Suspend child Xen devices. */
1176a6aedc5dSRoger Pau Monné 	error = bus_generic_suspend(dev);
1177a6aedc5dSRoger Pau Monné 	if (error != 0)
1178a6aedc5dSRoger Pau Monné 		return (error);
1179a6aedc5dSRoger Pau Monné 
1180a6aedc5dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
1181a6aedc5dSRoger Pau Monné 
1182a6aedc5dSRoger Pau Monné 	return (0);
1183a6aedc5dSRoger Pau Monné }
1184a6aedc5dSRoger Pau Monné 
1185a6aedc5dSRoger Pau Monné /**
1186a6aedc5dSRoger Pau Monné  * Resume XenStore operations after this VM is resumed.
1187a6aedc5dSRoger Pau Monné  */
1188a6aedc5dSRoger Pau Monné static int
xs_resume(device_t dev __unused)1189a6aedc5dSRoger Pau Monné xs_resume(device_t dev __unused)
1190a6aedc5dSRoger Pau Monné {
1191a6aedc5dSRoger Pau Monné 	struct xs_watch *watch;
1192a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1193a6aedc5dSRoger Pau Monné 
1194a6aedc5dSRoger Pau Monné 	xs_init_comms();
1195a6aedc5dSRoger Pau Monné 
1196a6aedc5dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
1197a6aedc5dSRoger Pau Monné 
1198a6aedc5dSRoger Pau Monné 	/*
11995f8f6646SRoger Pau Monné 	 * NB: since xenstore childs have not been resumed yet, there's
12005f8f6646SRoger Pau Monné 	 * no need to hold any watch mutex. Having clients try to add or
12015f8f6646SRoger Pau Monné 	 * remove watches at this point (before xenstore is resumed) is
12025f8f6646SRoger Pau Monné 	 * clearly a violantion of the resume order.
1203a6aedc5dSRoger Pau Monné 	 */
1204a6aedc5dSRoger Pau Monné 	LIST_FOREACH(watch, &xs.registered_watches, list) {
1205a6aedc5dSRoger Pau Monné 		sprintf(token, "%lX", (long)watch);
1206a6aedc5dSRoger Pau Monné 		xs_watch(watch->node, token);
1207a6aedc5dSRoger Pau Monné 	}
1208a6aedc5dSRoger Pau Monné 
1209a6aedc5dSRoger Pau Monné 	/* Resume child Xen devices. */
1210a6aedc5dSRoger Pau Monné 	bus_generic_resume(dev);
1211a6aedc5dSRoger Pau Monné 
1212a6aedc5dSRoger Pau Monné 	return (0);
1213a6aedc5dSRoger Pau Monné }
1214a6aedc5dSRoger Pau Monné 
1215a6aedc5dSRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
1216a6aedc5dSRoger Pau Monné static device_method_t xenstore_methods[] = {
1217a6aedc5dSRoger Pau Monné 	/* Device interface */
1218a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_identify,	xs_identify),
1219a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_probe,         xs_probe),
1220a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_attach,        xs_attach),
1221a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_detach,        bus_generic_detach),
1222a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1223a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_suspend,       xs_suspend),
1224a6aedc5dSRoger Pau Monné 	DEVMETHOD(device_resume,        xs_resume),
1225a6aedc5dSRoger Pau Monné 
1226a6aedc5dSRoger Pau Monné 	/* Bus interface */
1227a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_add_child,        bus_generic_add_child),
1228a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
1229a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1230a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1231a6aedc5dSRoger Pau Monné 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1232a6aedc5dSRoger Pau Monné 
1233a6aedc5dSRoger Pau Monné 	DEVMETHOD_END
1234a6aedc5dSRoger Pau Monné };
1235a6aedc5dSRoger Pau Monné 
1236a6aedc5dSRoger Pau Monné DEFINE_CLASS_0(xenstore, xenstore_driver, xenstore_methods, 0);
1237a6aedc5dSRoger Pau Monné 
1238*f929eb1eSJohn Baldwin DRIVER_MODULE(xenstore, xenpv, xenstore_driver, 0, 0);
1239a6aedc5dSRoger Pau Monné 
1240a6aedc5dSRoger Pau Monné /*------------------------------- Sysctl Data --------------------------------*/
1241a6aedc5dSRoger Pau Monné /* XXX Shouldn't the node be somewhere else? */
124241fc1ce1SPawel Biernacki SYSCTL_NODE(_dev, OID_AUTO, xen, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
124341fc1ce1SPawel Biernacki     "Xen");
1244a6aedc5dSRoger Pau Monné SYSCTL_INT(_dev_xen, OID_AUTO, xsd_port, CTLFLAG_RD, &xs.evtchn, 0, "");
1245a6aedc5dSRoger Pau Monné SYSCTL_ULONG(_dev_xen, OID_AUTO, xsd_kva, CTLFLAG_RD, (u_long *) &xen_store, 0, "");
1246a6aedc5dSRoger Pau Monné 
1247a6aedc5dSRoger Pau Monné /*-------------------------------- Public API --------------------------------*/
1248a6aedc5dSRoger Pau Monné /*------- API comments for these methods can be found in xenstorevar.h -------*/
1249cfa0b7b8SRoger Pau Monné bool
xs_initialized(void)1250cfa0b7b8SRoger Pau Monné xs_initialized(void)
1251cfa0b7b8SRoger Pau Monné {
1252cfa0b7b8SRoger Pau Monné 
1253cfa0b7b8SRoger Pau Monné 	return (xs.initialized);
1254cfa0b7b8SRoger Pau Monné }
1255cfa0b7b8SRoger Pau Monné 
1256cfa0b7b8SRoger Pau Monné evtchn_port_t
xs_evtchn(void)1257cfa0b7b8SRoger Pau Monné xs_evtchn(void)
1258cfa0b7b8SRoger Pau Monné {
1259cfa0b7b8SRoger Pau Monné 
1260cfa0b7b8SRoger Pau Monné     return (xs.evtchn);
1261cfa0b7b8SRoger Pau Monné }
1262cfa0b7b8SRoger Pau Monné 
1263cfa0b7b8SRoger Pau Monné vm_paddr_t
xs_address(void)1264cfa0b7b8SRoger Pau Monné xs_address(void)
1265cfa0b7b8SRoger Pau Monné {
1266cfa0b7b8SRoger Pau Monné 
1267cfa0b7b8SRoger Pau Monné     return (ptoa(xs.gpfn));
1268cfa0b7b8SRoger Pau Monné }
1269cfa0b7b8SRoger Pau Monné 
1270a6aedc5dSRoger Pau Monné int
xs_directory(struct xs_transaction t,const char * dir,const char * node,u_int * num,const char *** result)1271a6aedc5dSRoger Pau Monné xs_directory(struct xs_transaction t, const char *dir, const char *node,
1272a6aedc5dSRoger Pau Monné     u_int *num, const char ***result)
1273a6aedc5dSRoger Pau Monné {
1274a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1275a6aedc5dSRoger Pau Monné 	char *strings;
1276a6aedc5dSRoger Pau Monné 	u_int len = 0;
1277a6aedc5dSRoger Pau Monné 	int error;
1278a6aedc5dSRoger Pau Monné 
1279a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1280a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_DIRECTORY, sbuf_data(path), &len,
1281a6aedc5dSRoger Pau Monné 	    (void **)&strings);
1282a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1283a6aedc5dSRoger Pau Monné 	if (error)
1284a6aedc5dSRoger Pau Monné 		return (error);
1285a6aedc5dSRoger Pau Monné 
1286a6aedc5dSRoger Pau Monné 	*result = split(strings, len, num);
1287a6aedc5dSRoger Pau Monné 
1288a6aedc5dSRoger Pau Monné 	return (0);
1289a6aedc5dSRoger Pau Monné }
1290a6aedc5dSRoger Pau Monné 
1291a6aedc5dSRoger Pau Monné int
xs_exists(struct xs_transaction t,const char * dir,const char * node)1292a6aedc5dSRoger Pau Monné xs_exists(struct xs_transaction t, const char *dir, const char *node)
1293a6aedc5dSRoger Pau Monné {
1294a6aedc5dSRoger Pau Monné 	const char **d;
1295a6aedc5dSRoger Pau Monné 	int error, dir_n;
1296a6aedc5dSRoger Pau Monné 
1297a6aedc5dSRoger Pau Monné 	error = xs_directory(t, dir, node, &dir_n, &d);
1298a6aedc5dSRoger Pau Monné 	if (error)
1299a6aedc5dSRoger Pau Monné 		return (0);
1300a6aedc5dSRoger Pau Monné 	free(d, M_XENSTORE);
1301a6aedc5dSRoger Pau Monné 	return (1);
1302a6aedc5dSRoger Pau Monné }
1303a6aedc5dSRoger Pau Monné 
1304a6aedc5dSRoger Pau Monné int
xs_read(struct xs_transaction t,const char * dir,const char * node,u_int * len,void ** result)1305a6aedc5dSRoger Pau Monné xs_read(struct xs_transaction t, const char *dir, const char *node,
1306a6aedc5dSRoger Pau Monné     u_int *len, void **result)
1307a6aedc5dSRoger Pau Monné {
1308a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1309a6aedc5dSRoger Pau Monné 	void *ret;
1310a6aedc5dSRoger Pau Monné 	int error;
1311a6aedc5dSRoger Pau Monné 
1312a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1313a6aedc5dSRoger Pau Monné 	error = xs_single(t, XS_READ, sbuf_data(path), len, &ret);
1314a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1315a6aedc5dSRoger Pau Monné 	if (error)
1316a6aedc5dSRoger Pau Monné 		return (error);
1317a6aedc5dSRoger Pau Monné 	*result = ret;
1318a6aedc5dSRoger Pau Monné 	return (0);
1319a6aedc5dSRoger Pau Monné }
1320a6aedc5dSRoger Pau Monné 
1321a6aedc5dSRoger Pau Monné int
xs_write(struct xs_transaction t,const char * dir,const char * node,const char * string)1322a6aedc5dSRoger Pau Monné xs_write(struct xs_transaction t, const char *dir, const char *node,
1323a6aedc5dSRoger Pau Monné     const char *string)
1324a6aedc5dSRoger Pau Monné {
1325a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1326a6aedc5dSRoger Pau Monné 	struct iovec iovec[2];
1327a6aedc5dSRoger Pau Monné 	int error;
1328a6aedc5dSRoger Pau Monné 
1329a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1330a6aedc5dSRoger Pau Monné 
1331a6aedc5dSRoger Pau Monné 	iovec[0].iov_base = (void *)(uintptr_t) sbuf_data(path);
1332a6aedc5dSRoger Pau Monné 	iovec[0].iov_len = sbuf_len(path) + 1;
1333a6aedc5dSRoger Pau Monné 	iovec[1].iov_base = (void *)(uintptr_t) string;
1334a6aedc5dSRoger Pau Monné 	iovec[1].iov_len = strlen(string);
1335a6aedc5dSRoger Pau Monné 
1336a6aedc5dSRoger Pau Monné 	error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL);
1337a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1338a6aedc5dSRoger Pau Monné 
1339a6aedc5dSRoger Pau Monné 	return (error);
1340a6aedc5dSRoger Pau Monné }
1341a6aedc5dSRoger Pau Monné 
1342a6aedc5dSRoger Pau Monné int
xs_mkdir(struct xs_transaction t,const char * dir,const char * node)1343a6aedc5dSRoger Pau Monné xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
1344a6aedc5dSRoger Pau Monné {
1345a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1346a6aedc5dSRoger Pau Monné 	int ret;
1347a6aedc5dSRoger Pau Monné 
1348a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1349a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_MKDIR, sbuf_data(path), NULL, NULL);
1350a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1351a6aedc5dSRoger Pau Monné 
1352a6aedc5dSRoger Pau Monné 	return (ret);
1353a6aedc5dSRoger Pau Monné }
1354a6aedc5dSRoger Pau Monné 
1355a6aedc5dSRoger Pau Monné int
xs_rm(struct xs_transaction t,const char * dir,const char * node)1356a6aedc5dSRoger Pau Monné xs_rm(struct xs_transaction t, const char *dir, const char *node)
1357a6aedc5dSRoger Pau Monné {
1358a6aedc5dSRoger Pau Monné 	struct sbuf *path;
1359a6aedc5dSRoger Pau Monné 	int ret;
1360a6aedc5dSRoger Pau Monné 
1361a6aedc5dSRoger Pau Monné 	path = xs_join(dir, node);
1362a6aedc5dSRoger Pau Monné 	ret = xs_single(t, XS_RM, sbuf_data(path), NULL, NULL);
1363a6aedc5dSRoger Pau Monné 	sbuf_delete(path);
1364a6aedc5dSRoger Pau Monné 
1365a6aedc5dSRoger Pau Monné 	return (ret);
1366a6aedc5dSRoger Pau Monné }
1367a6aedc5dSRoger Pau Monné 
1368a6aedc5dSRoger Pau Monné int
xs_rm_tree(struct xs_transaction xbt,const char * base,const char * node)1369a6aedc5dSRoger Pau Monné xs_rm_tree(struct xs_transaction xbt, const char *base, const char *node)
1370a6aedc5dSRoger Pau Monné {
1371a6aedc5dSRoger Pau Monné 	struct xs_transaction local_xbt;
1372a6aedc5dSRoger Pau Monné 	struct sbuf *root_path_sbuf;
1373a6aedc5dSRoger Pau Monné 	struct sbuf *cur_path_sbuf;
1374a6aedc5dSRoger Pau Monné 	char *root_path;
1375a6aedc5dSRoger Pau Monné 	char *cur_path;
1376a6aedc5dSRoger Pau Monné 	const char **dir;
1377a6aedc5dSRoger Pau Monné 	int error;
1378a6aedc5dSRoger Pau Monné 
1379a6aedc5dSRoger Pau Monné retry:
1380a6aedc5dSRoger Pau Monné 	root_path_sbuf = xs_join(base, node);
1381a6aedc5dSRoger Pau Monné 	cur_path_sbuf  = xs_join(base, node);
1382a6aedc5dSRoger Pau Monné 	root_path      = sbuf_data(root_path_sbuf);
1383a6aedc5dSRoger Pau Monné 	cur_path       = sbuf_data(cur_path_sbuf);
1384a6aedc5dSRoger Pau Monné 	dir            = NULL;
1385a6aedc5dSRoger Pau Monné 	local_xbt.id   = 0;
1386a6aedc5dSRoger Pau Monné 
1387a6aedc5dSRoger Pau Monné 	if (xbt.id == 0) {
1388a6aedc5dSRoger Pau Monné 		error = xs_transaction_start(&local_xbt);
1389a6aedc5dSRoger Pau Monné 		if (error != 0)
1390a6aedc5dSRoger Pau Monné 			goto out;
1391a6aedc5dSRoger Pau Monné 		xbt = local_xbt;
1392a6aedc5dSRoger Pau Monné 	}
1393a6aedc5dSRoger Pau Monné 
1394a6aedc5dSRoger Pau Monné 	while (1) {
1395a6aedc5dSRoger Pau Monné 		u_int count;
1396a6aedc5dSRoger Pau Monné 		u_int i;
1397a6aedc5dSRoger Pau Monné 
1398a6aedc5dSRoger Pau Monné 		error = xs_directory(xbt, cur_path, "", &count, &dir);
1399a6aedc5dSRoger Pau Monné 		if (error)
1400a6aedc5dSRoger Pau Monné 			goto out;
1401a6aedc5dSRoger Pau Monné 
1402a6aedc5dSRoger Pau Monné 		for (i = 0; i < count; i++) {
1403a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, dir[i]);
1404a6aedc5dSRoger Pau Monné 			if (error == ENOTEMPTY) {
1405a6aedc5dSRoger Pau Monné 				struct sbuf *push_dir;
1406a6aedc5dSRoger Pau Monné 
1407a6aedc5dSRoger Pau Monné 				/*
1408a6aedc5dSRoger Pau Monné 				 * Descend to clear out this sub directory.
1409a6aedc5dSRoger Pau Monné 				 * We'll return to cur_dir once push_dir
1410a6aedc5dSRoger Pau Monné 				 * is empty.
1411a6aedc5dSRoger Pau Monné 				 */
1412a6aedc5dSRoger Pau Monné 				push_dir = xs_join(cur_path, dir[i]);
1413a6aedc5dSRoger Pau Monné 				sbuf_delete(cur_path_sbuf);
1414a6aedc5dSRoger Pau Monné 				cur_path_sbuf = push_dir;
1415a6aedc5dSRoger Pau Monné 				cur_path = sbuf_data(cur_path_sbuf);
1416a6aedc5dSRoger Pau Monné 				break;
1417a6aedc5dSRoger Pau Monné 			} else if (error != 0) {
1418a6aedc5dSRoger Pau Monné 				goto out;
1419a6aedc5dSRoger Pau Monné 			}
1420a6aedc5dSRoger Pau Monné 		}
1421a6aedc5dSRoger Pau Monné 
1422a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1423a6aedc5dSRoger Pau Monné 		dir = NULL;
1424a6aedc5dSRoger Pau Monné 
1425a6aedc5dSRoger Pau Monné 		if (i == count) {
1426a6aedc5dSRoger Pau Monné 			char *last_slash;
1427a6aedc5dSRoger Pau Monné 
1428a6aedc5dSRoger Pau Monné 			/* Directory is empty.  It is now safe to remove. */
1429a6aedc5dSRoger Pau Monné 			error = xs_rm(xbt, cur_path, "");
1430a6aedc5dSRoger Pau Monné 			if (error != 0)
1431a6aedc5dSRoger Pau Monné 				goto out;
1432a6aedc5dSRoger Pau Monné 
1433a6aedc5dSRoger Pau Monné 			if (!strcmp(cur_path, root_path))
1434a6aedc5dSRoger Pau Monné 				break;
1435a6aedc5dSRoger Pau Monné 
1436a6aedc5dSRoger Pau Monné 			/* Return to processing the parent directory. */
1437a6aedc5dSRoger Pau Monné 			last_slash = strrchr(cur_path, '/');
1438a6aedc5dSRoger Pau Monné 			KASSERT(last_slash != NULL,
1439a6aedc5dSRoger Pau Monné 				("xs_rm_tree: mangled path %s", cur_path));
1440a6aedc5dSRoger Pau Monné 			*last_slash = '\0';
1441a6aedc5dSRoger Pau Monné 		}
1442a6aedc5dSRoger Pau Monné 	}
1443a6aedc5dSRoger Pau Monné 
1444a6aedc5dSRoger Pau Monné out:
1445a6aedc5dSRoger Pau Monné 	sbuf_delete(cur_path_sbuf);
1446a6aedc5dSRoger Pau Monné 	sbuf_delete(root_path_sbuf);
1447a6aedc5dSRoger Pau Monné 	if (dir != NULL)
1448a6aedc5dSRoger Pau Monné 		free(dir, M_XENSTORE);
1449a6aedc5dSRoger Pau Monné 
1450a6aedc5dSRoger Pau Monné 	if (local_xbt.id != 0) {
1451a6aedc5dSRoger Pau Monné 		int terror;
1452a6aedc5dSRoger Pau Monné 
1453a6aedc5dSRoger Pau Monné 		terror = xs_transaction_end(local_xbt, /*abort*/error != 0);
1454a6aedc5dSRoger Pau Monné 		xbt.id = 0;
1455a6aedc5dSRoger Pau Monné 		if (terror == EAGAIN && error == 0)
1456a6aedc5dSRoger Pau Monné 			goto retry;
1457a6aedc5dSRoger Pau Monné 	}
1458a6aedc5dSRoger Pau Monné 	return (error);
1459a6aedc5dSRoger Pau Monné }
1460a6aedc5dSRoger Pau Monné 
1461a6aedc5dSRoger Pau Monné int
xs_transaction_start(struct xs_transaction * t)1462a6aedc5dSRoger Pau Monné xs_transaction_start(struct xs_transaction *t)
1463a6aedc5dSRoger Pau Monné {
1464a6aedc5dSRoger Pau Monné 	char *id_str;
1465a6aedc5dSRoger Pau Monné 	int error;
1466a6aedc5dSRoger Pau Monné 
1467a6aedc5dSRoger Pau Monné 	error = xs_single(XST_NIL, XS_TRANSACTION_START, "", NULL,
1468a6aedc5dSRoger Pau Monné 	    (void **)&id_str);
1469a6aedc5dSRoger Pau Monné 	if (error == 0) {
1470a6aedc5dSRoger Pau Monné 		t->id = strtoul(id_str, NULL, 0);
1471a6aedc5dSRoger Pau Monné 		free(id_str, M_XENSTORE);
1472a6aedc5dSRoger Pau Monné 	}
1473a6aedc5dSRoger Pau Monné 	return (error);
1474a6aedc5dSRoger Pau Monné }
1475a6aedc5dSRoger Pau Monné 
1476a6aedc5dSRoger Pau Monné int
xs_transaction_end(struct xs_transaction t,int abort)1477a6aedc5dSRoger Pau Monné xs_transaction_end(struct xs_transaction t, int abort)
1478a6aedc5dSRoger Pau Monné {
1479a6aedc5dSRoger Pau Monné 	char abortstr[2];
1480a6aedc5dSRoger Pau Monné 
1481a6aedc5dSRoger Pau Monné 	if (abort)
1482a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "F");
1483a6aedc5dSRoger Pau Monné 	else
1484a6aedc5dSRoger Pau Monné 		strcpy(abortstr, "T");
1485a6aedc5dSRoger Pau Monné 
1486a6aedc5dSRoger Pau Monné 	return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL));
1487a6aedc5dSRoger Pau Monné }
1488a6aedc5dSRoger Pau Monné 
1489a6aedc5dSRoger Pau Monné int
xs_scanf(struct xs_transaction t,const char * dir,const char * node,int * scancountp,const char * fmt,...)1490a6aedc5dSRoger Pau Monné xs_scanf(struct xs_transaction t, const char *dir, const char *node,
1491a6aedc5dSRoger Pau Monné      int *scancountp, const char *fmt, ...)
1492a6aedc5dSRoger Pau Monné {
1493a6aedc5dSRoger Pau Monné 	va_list ap;
1494a6aedc5dSRoger Pau Monné 	int error, ns;
1495a6aedc5dSRoger Pau Monné 	char *val;
1496a6aedc5dSRoger Pau Monné 
1497a6aedc5dSRoger Pau Monné 	error = xs_read(t, dir, node, NULL, (void **) &val);
1498a6aedc5dSRoger Pau Monné 	if (error)
1499a6aedc5dSRoger Pau Monné 		return (error);
1500a6aedc5dSRoger Pau Monné 
1501a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1502a6aedc5dSRoger Pau Monné 	ns = vsscanf(val, fmt, ap);
1503a6aedc5dSRoger Pau Monné 	va_end(ap);
1504a6aedc5dSRoger Pau Monné 	free(val, M_XENSTORE);
1505a6aedc5dSRoger Pau Monné 	/* Distinctive errno. */
1506a6aedc5dSRoger Pau Monné 	if (ns == 0)
1507a6aedc5dSRoger Pau Monné 		return (ERANGE);
1508a6aedc5dSRoger Pau Monné 	if (scancountp)
1509a6aedc5dSRoger Pau Monné 		*scancountp = ns;
1510a6aedc5dSRoger Pau Monné 	return (0);
1511a6aedc5dSRoger Pau Monné }
1512a6aedc5dSRoger Pau Monné 
1513a6aedc5dSRoger Pau Monné int
xs_vprintf(struct xs_transaction t,const char * dir,const char * node,const char * fmt,va_list ap)1514a6aedc5dSRoger Pau Monné xs_vprintf(struct xs_transaction t,
1515a6aedc5dSRoger Pau Monné     const char *dir, const char *node, const char *fmt, va_list ap)
1516a6aedc5dSRoger Pau Monné {
1517a6aedc5dSRoger Pau Monné 	struct sbuf *sb;
1518a6aedc5dSRoger Pau Monné 	int error;
1519a6aedc5dSRoger Pau Monné 
1520a6aedc5dSRoger Pau Monné 	sb = sbuf_new_auto();
1521a6aedc5dSRoger Pau Monné 	sbuf_vprintf(sb, fmt, ap);
1522a6aedc5dSRoger Pau Monné 	sbuf_finish(sb);
1523a6aedc5dSRoger Pau Monné 	error = xs_write(t, dir, node, sbuf_data(sb));
1524a6aedc5dSRoger Pau Monné 	sbuf_delete(sb);
1525a6aedc5dSRoger Pau Monné 
1526a6aedc5dSRoger Pau Monné 	return (error);
1527a6aedc5dSRoger Pau Monné }
1528a6aedc5dSRoger Pau Monné 
1529a6aedc5dSRoger Pau Monné int
xs_printf(struct xs_transaction t,const char * dir,const char * node,const char * fmt,...)1530a6aedc5dSRoger Pau Monné xs_printf(struct xs_transaction t, const char *dir, const char *node,
1531a6aedc5dSRoger Pau Monné      const char *fmt, ...)
1532a6aedc5dSRoger Pau Monné {
1533a6aedc5dSRoger Pau Monné 	va_list ap;
1534a6aedc5dSRoger Pau Monné 	int error;
1535a6aedc5dSRoger Pau Monné 
1536a6aedc5dSRoger Pau Monné 	va_start(ap, fmt);
1537a6aedc5dSRoger Pau Monné 	error = xs_vprintf(t, dir, node, fmt, ap);
1538a6aedc5dSRoger Pau Monné 	va_end(ap);
1539a6aedc5dSRoger Pau Monné 
1540a6aedc5dSRoger Pau Monné 	return (error);
1541a6aedc5dSRoger Pau Monné }
1542a6aedc5dSRoger Pau Monné 
1543a6aedc5dSRoger Pau Monné int
xs_gather(struct xs_transaction t,const char * dir,...)1544a6aedc5dSRoger Pau Monné xs_gather(struct xs_transaction t, const char *dir, ...)
1545a6aedc5dSRoger Pau Monné {
1546a6aedc5dSRoger Pau Monné 	va_list ap;
1547a6aedc5dSRoger Pau Monné 	const char *name;
1548a6aedc5dSRoger Pau Monné 	int error;
1549a6aedc5dSRoger Pau Monné 
1550a6aedc5dSRoger Pau Monné 	va_start(ap, dir);
1551a6aedc5dSRoger Pau Monné 	error = 0;
1552a6aedc5dSRoger Pau Monné 	while (error == 0 && (name = va_arg(ap, char *)) != NULL) {
1553a6aedc5dSRoger Pau Monné 		const char *fmt = va_arg(ap, char *);
1554a6aedc5dSRoger Pau Monné 		void *result = va_arg(ap, void *);
1555a6aedc5dSRoger Pau Monné 		char *p;
1556a6aedc5dSRoger Pau Monné 
1557a6aedc5dSRoger Pau Monné 		error = xs_read(t, dir, name, NULL, (void **) &p);
1558a6aedc5dSRoger Pau Monné 		if (error)
1559a6aedc5dSRoger Pau Monné 			break;
1560a6aedc5dSRoger Pau Monné 
1561a6aedc5dSRoger Pau Monné 		if (fmt) {
1562a6aedc5dSRoger Pau Monné 			if (sscanf(p, fmt, result) == 0)
1563a6aedc5dSRoger Pau Monné 				error = EINVAL;
1564a6aedc5dSRoger Pau Monné 			free(p, M_XENSTORE);
1565a6aedc5dSRoger Pau Monné 		} else
1566a6aedc5dSRoger Pau Monné 			*(char **)result = p;
1567a6aedc5dSRoger Pau Monné 	}
1568a6aedc5dSRoger Pau Monné 	va_end(ap);
1569a6aedc5dSRoger Pau Monné 
1570a6aedc5dSRoger Pau Monné 	return (error);
1571a6aedc5dSRoger Pau Monné }
1572a6aedc5dSRoger Pau Monné 
1573a6aedc5dSRoger Pau Monné int
xs_register_watch(struct xs_watch * watch)1574a6aedc5dSRoger Pau Monné xs_register_watch(struct xs_watch *watch)
1575a6aedc5dSRoger Pau Monné {
1576a6aedc5dSRoger Pau Monné 	/* Pointer in ascii is the token. */
1577a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1578a6aedc5dSRoger Pau Monné 	int error;
1579a6aedc5dSRoger Pau Monné 
15804e4e43dcSRoger Pau Monné 	watch->pending = 0;
1581a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1582a6aedc5dSRoger Pau Monné 
1583a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1584a6aedc5dSRoger Pau Monné 	KASSERT(find_watch(token) == NULL, ("watch already registered"));
1585a6aedc5dSRoger Pau Monné 	LIST_INSERT_HEAD(&xs.registered_watches, watch, list);
1586a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1587a6aedc5dSRoger Pau Monné 
1588a6aedc5dSRoger Pau Monné 	error = xs_watch(watch->node, token);
1589a6aedc5dSRoger Pau Monné 
1590a6aedc5dSRoger Pau Monné 	/* Ignore errors due to multiple registration. */
1591a6aedc5dSRoger Pau Monné 	if (error == EEXIST)
1592a6aedc5dSRoger Pau Monné 		error = 0;
1593a6aedc5dSRoger Pau Monné 
1594a6aedc5dSRoger Pau Monné 	if (error != 0) {
1595a6aedc5dSRoger Pau Monné 		mtx_lock(&xs.registered_watches_lock);
1596a6aedc5dSRoger Pau Monné 		LIST_REMOVE(watch, list);
1597a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1598a6aedc5dSRoger Pau Monné 	}
1599a6aedc5dSRoger Pau Monné 
1600a6aedc5dSRoger Pau Monné 	return (error);
1601a6aedc5dSRoger Pau Monné }
1602a6aedc5dSRoger Pau Monné 
1603a6aedc5dSRoger Pau Monné void
xs_unregister_watch(struct xs_watch * watch)1604a6aedc5dSRoger Pau Monné xs_unregister_watch(struct xs_watch *watch)
1605a6aedc5dSRoger Pau Monné {
1606a6aedc5dSRoger Pau Monné 	struct xs_stored_msg *msg, *tmp;
1607a6aedc5dSRoger Pau Monné 	char token[sizeof(watch) * 2 + 1];
1608a6aedc5dSRoger Pau Monné 	int error;
1609a6aedc5dSRoger Pau Monné 
1610a6aedc5dSRoger Pau Monné 	sprintf(token, "%lX", (long)watch);
1611a6aedc5dSRoger Pau Monné 
1612a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.registered_watches_lock);
1613a6aedc5dSRoger Pau Monné 	if (find_watch(token) == NULL) {
1614a6aedc5dSRoger Pau Monné 		mtx_unlock(&xs.registered_watches_lock);
1615a6aedc5dSRoger Pau Monné 		return;
1616a6aedc5dSRoger Pau Monné 	}
1617a6aedc5dSRoger Pau Monné 	LIST_REMOVE(watch, list);
1618a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.registered_watches_lock);
1619a6aedc5dSRoger Pau Monné 
1620a6aedc5dSRoger Pau Monné 	error = xs_unwatch(watch->node, token);
1621a6aedc5dSRoger Pau Monné 	if (error)
1622a6aedc5dSRoger Pau Monné 		log(LOG_WARNING, "XENSTORE Failed to release watch %s: %i\n",
1623a6aedc5dSRoger Pau Monné 		    watch->node, error);
1624a6aedc5dSRoger Pau Monné 
1625a6aedc5dSRoger Pau Monné 	/* Cancel pending watch events. */
1626a6aedc5dSRoger Pau Monné 	mtx_lock(&xs.watch_events_lock);
1627a6aedc5dSRoger Pau Monné 	TAILQ_FOREACH_SAFE(msg, &xs.watch_events, list, tmp) {
1628a6aedc5dSRoger Pau Monné 		if (msg->u.watch.handle != watch)
1629a6aedc5dSRoger Pau Monné 			continue;
1630a6aedc5dSRoger Pau Monné 		TAILQ_REMOVE(&xs.watch_events, msg, list);
1631a6aedc5dSRoger Pau Monné 		free(msg->u.watch.vec, M_XENSTORE);
1632a6aedc5dSRoger Pau Monné 		free(msg, M_XENSTORE);
1633a6aedc5dSRoger Pau Monné 	}
1634a6aedc5dSRoger Pau Monné 	mtx_unlock(&xs.watch_events_lock);
1635a6aedc5dSRoger Pau Monné 
1636a6aedc5dSRoger Pau Monné 	/* Flush any currently-executing callback, unless we are it. :-) */
1637a6aedc5dSRoger Pau Monné 	if (curproc->p_pid != xs.xenwatch_pid) {
1638a6aedc5dSRoger Pau Monné 		sx_xlock(&xs.xenwatch_mutex);
1639a6aedc5dSRoger Pau Monné 		sx_xunlock(&xs.xenwatch_mutex);
1640a6aedc5dSRoger Pau Monné 	}
1641a6aedc5dSRoger Pau Monné }
164241716b8dSRoger Pau Monné 
164341716b8dSRoger Pau Monné void
xs_lock(void)164441716b8dSRoger Pau Monné xs_lock(void)
164541716b8dSRoger Pau Monné {
164641716b8dSRoger Pau Monné 
164741716b8dSRoger Pau Monné 	sx_xlock(&xs.request_mutex);
164841716b8dSRoger Pau Monné 	return;
164941716b8dSRoger Pau Monné }
165041716b8dSRoger Pau Monné 
165141716b8dSRoger Pau Monné void
xs_unlock(void)165241716b8dSRoger Pau Monné xs_unlock(void)
165341716b8dSRoger Pau Monné {
165441716b8dSRoger Pau Monné 
165541716b8dSRoger Pau Monné 	sx_xunlock(&xs.request_mutex);
165641716b8dSRoger Pau Monné 	return;
165741716b8dSRoger Pau Monné }
1658