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