xref: /freebsd/sys/dev/xen/xenstore/xenstore_dev.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*
2  * xenstore_dev.c
3  *
4  * Driver giving user-space access to the kernel's connection to the
5  * XenStore service.
6  *
7  * Copyright (c) 2005, Christian Limpach
8  * Copyright (c) 2005, Rusty Russell, IBM Corporation
9  *
10  * This file may be distributed separately from the Linux kernel, or
11  * incorporated into other software packages, subject to the following license:
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this source file (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use, copy, modify,
16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/cdefs.h>
35 #include <sys/errno.h>
36 #include <sys/uio.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/conf.h>
43 #include <sys/module.h>
44 #include <sys/selinfo.h>
45 #include <sys/sysctl.h>
46 #include <sys/poll.h>
47 
48 #include <xen/xen-os.h>
49 
50 #include <xen/hypervisor.h>
51 #include <xen/xenstore/xenstorevar.h>
52 #include <xen/xenstore/xenstore_internal.h>
53 
54 static unsigned int max_pending_watches = 1000;
55 
56 struct xs_dev_transaction {
57 	LIST_ENTRY(xs_dev_transaction) list;
58 	struct xs_transaction handle;
59 };
60 
61 struct xs_dev_watch {
62 	LIST_ENTRY(xs_dev_watch) list;
63 	struct xs_watch watch;
64 	char *token;
65 	struct xs_dev_data *user;
66 };
67 
68 struct xs_dev_data {
69 	/* In-progress transaction. */
70 	LIST_HEAD(, xs_dev_transaction) transactions;
71 
72 	/* Active watches. */
73 	LIST_HEAD(, xs_dev_watch) watches;
74 
75 	/* Partial request. */
76 	unsigned int len;
77 	union {
78 		struct xsd_sockmsg msg;
79 		char buffer[PAGE_SIZE];
80 	} u;
81 
82 	/* Response queue. */
83 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
84 	char read_buffer[PAGE_SIZE];
85 	unsigned int read_cons, read_prod;
86 
87 	/* Serializes writes to the read buffer. */
88 	struct mtx lock;
89 
90 	/* Polling structure (for reads only ATM). */
91 	struct selinfo ev_rsel;
92 };
93 
94 static void
95 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
96 {
97 	unsigned int i;
98 
99 	for (i = 0; i < len; i++, u->read_prod++)
100 		u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
101 
102 	KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
103 	    ("xenstore reply too big"));
104 
105 	wakeup(u);
106 	selwakeup(&u->ev_rsel);
107 }
108 
109 static const char *
110 xs_dev_error_to_string(int error)
111 {
112 	unsigned int i;
113 
114 	for (i = 0; i < nitems(xsd_errors); i++)
115 		if (xsd_errors[i].errnum == error)
116 			return (xsd_errors[i].errstring);
117 
118 	return (NULL);
119 }
120 
121 static void
122 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
123 {
124 	struct xsd_sockmsg msg;
125 	const char *payload;
126 
127 	msg.type = XS_ERROR;
128 	msg.req_id = req_id;
129 	msg.tx_id = tx_id;
130 	payload = NULL;
131 
132 	payload = xs_dev_error_to_string(error);
133 	if (payload == NULL)
134 		payload = xs_dev_error_to_string(EINVAL);
135 	KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
136 
137 	msg.len = strlen(payload) + 1;
138 
139 	mtx_lock(&u->lock);
140 	xs_queue_reply(u, (char *)&msg, sizeof(msg));
141 	xs_queue_reply(u, payload, msg.len);
142 	mtx_unlock(&u->lock);
143 }
144 
145 static int
146 xs_dev_watch_message_parse_string(const char **p, const char *end,
147     const char **string_r)
148 {
149 	const char *nul;
150 
151 	nul = memchr(*p, 0, end - *p);
152 	if (!nul)
153 		return (EINVAL);
154 
155 	*string_r = *p;
156 	*p = nul+1;
157 
158 	return (0);
159 }
160 
161 static int
162 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
163     const char **token_r)
164 {
165 	const char *p, *end;
166 	int error;
167 
168 	p = (const char *)msg + sizeof(*msg);
169 	end = p + msg->len;
170 	KASSERT(p <= end, ("payload overflow"));
171 
172 	error = xs_dev_watch_message_parse_string(&p, end, path_r);
173 	if (error)
174 		return (error);
175 	error = xs_dev_watch_message_parse_string(&p, end, token_r);
176 	if (error)
177 		return (error);
178 
179 	return (0);
180 }
181 
182 static struct xs_dev_watch *
183 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
184 {
185 	struct xs_dev_watch *watch;
186 
187 	LIST_FOREACH(watch, &u->watches, list)
188 		if (strcmp(watch->token, token) == 0)
189 			return (watch);
190 
191 	return (NULL);
192 }
193 
194 static void
195 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
196 {
197 	struct xs_dev_watch *dwatch;
198 	struct xsd_sockmsg msg;
199 	char *payload;
200 
201 	dwatch = (struct xs_dev_watch *)watch->callback_data;
202 	msg.type = XS_WATCH_EVENT;
203 	msg.req_id = msg.tx_id = 0;
204 	msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
205 
206 	payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
207 	strcpy(payload, vec[XS_WATCH_PATH]);
208 	strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
209 	mtx_lock(&dwatch->user->lock);
210 	xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
211 	xs_queue_reply(dwatch->user, payload, msg.len);
212 	mtx_unlock(&dwatch->user->lock);
213 	free(payload, M_XENSTORE);
214 }
215 
216 static struct xs_dev_transaction *
217 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
218 {
219 	struct xs_dev_transaction *trans;
220 
221 	LIST_FOREACH(trans, &u->transactions, list)
222 		if (trans->handle.id == tx_id)
223 			return (trans);
224 
225 	return (NULL);
226 }
227 
228 static int
229 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
230 {
231 	int error;
232 	struct xs_dev_data *u;
233 
234 	error = devfs_get_cdevpriv((void **)&u);
235 	if (error != 0)
236 		return (error);
237 
238 	while (u->read_prod == u->read_cons) {
239 		error = tsleep(u, PCATCH, "xsdread", hz/10);
240 		if (error && error != EWOULDBLOCK)
241 			return (error);
242 	}
243 
244 	while (uio->uio_resid > 0) {
245 		if (u->read_cons == u->read_prod)
246 			break;
247 		error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
248 		    1, uio);
249 		if (error)
250 			return (error);
251 		u->read_cons++;
252 	}
253 	return (0);
254 }
255 
256 static int
257 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
258 {
259 	int error;
260 	const char *wpath, *wtoken;
261 	struct xs_dev_data *u;
262 	struct xs_dev_transaction *trans;
263 	struct xs_dev_watch *watch;
264 	void *reply;
265 	static const char *ok = "OK";
266 	int len = uio->uio_resid;
267 
268 	error = devfs_get_cdevpriv((void **)&u);
269 	if (error != 0)
270 		return (error);
271 
272 	if ((len + u->len) > sizeof(u->u.buffer))
273 		return (EINVAL);
274 
275 	error = uiomove(u->u.buffer + u->len, len, uio);
276 	if (error)
277 		return (error);
278 
279 	u->len += len;
280 	if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
281 		return (0);
282 
283 	switch (u->u.msg.type) {
284 	case XS_TRANSACTION_START:
285 	case XS_TRANSACTION_END:
286 	case XS_DIRECTORY:
287 	case XS_READ:
288 	case XS_GET_PERMS:
289 	case XS_RELEASE:
290 	case XS_GET_DOMAIN_PATH:
291 	case XS_WRITE:
292 	case XS_MKDIR:
293 	case XS_RM:
294 	case XS_SET_PERMS:
295 		/* Check that this transaction id is not hijacked. */
296 		if (u->u.msg.tx_id != 0 &&
297 		    xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
298 			error = EINVAL;
299 			break;
300 		}
301 		error = xs_dev_request_and_reply(&u->u.msg, &reply);
302 		if (!error) {
303 			if (u->u.msg.type == XS_TRANSACTION_START) {
304 				trans = malloc(sizeof(*trans), M_XENSTORE,
305 				    M_WAITOK);
306 				trans->handle.id = strtoul(reply, NULL, 0);
307 				LIST_INSERT_HEAD(&u->transactions, trans, list);
308 			} else if (u->u.msg.type == XS_TRANSACTION_END) {
309 				trans = xs_dev_find_transaction(u,
310 				    u->u.msg.tx_id);
311 				KASSERT(trans != NULL,
312 				    ("Unable to find transaction"));
313 				LIST_REMOVE(trans, list);
314 				free(trans, M_XENSTORE);
315 			}
316 			mtx_lock(&u->lock);
317 			xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
318 			xs_queue_reply(u, (char *)reply, u->u.msg.len);
319 			mtx_unlock(&u->lock);
320 			free(reply, M_XENSTORE);
321 		}
322 		break;
323 	case XS_WATCH:
324 		u->u.msg.tx_id = 0;
325 		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
326 		if (error)
327 			break;
328 		if (xs_dev_find_watch(u, wtoken) != NULL) {
329 			error = EINVAL;
330 			break;
331 		}
332 
333 		watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
334 		watch->watch.node = strdup(wpath, M_XENSTORE);
335 		watch->watch.callback = xs_dev_watch_cb;
336 		watch->watch.callback_data = (uintptr_t)watch;
337 		watch->watch.max_pending = max_pending_watches;
338 		watch->token = strdup(wtoken, M_XENSTORE);
339 		watch->user = u;
340 
341 		error = xs_register_watch(&watch->watch);
342 		if (error != 0) {
343 			free(watch->token, M_XENSTORE);
344 			free(watch->watch.node, M_XENSTORE);
345 			free(watch, M_XENSTORE);
346 			break;
347 		}
348 
349 		LIST_INSERT_HEAD(&u->watches, watch, list);
350 		u->u.msg.len = sizeof(ok);
351 		mtx_lock(&u->lock);
352 		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
353 		xs_queue_reply(u, ok, sizeof(ok));
354 		mtx_unlock(&u->lock);
355 		break;
356 	case XS_UNWATCH:
357 		u->u.msg.tx_id = 0;
358 		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
359 		if (error)
360 			break;
361 		watch = xs_dev_find_watch(u, wtoken);
362 		if (watch == NULL) {
363 			error = EINVAL;
364 			break;
365 		}
366 
367 		LIST_REMOVE(watch, list);
368 		xs_unregister_watch(&watch->watch);
369 		free(watch->watch.node, M_XENSTORE);
370 		free(watch->token, M_XENSTORE);
371 		free(watch, M_XENSTORE);
372 		u->u.msg.len = sizeof(ok);
373 		mtx_lock(&u->lock);
374 		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
375 		xs_queue_reply(u, ok, sizeof(ok));
376 		mtx_unlock(&u->lock);
377 		break;
378 	default:
379 		error = EINVAL;
380 		break;
381 	}
382 
383 	if (error != 0)
384 		xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
385 
386 	/* Reset the write buffer. */
387 	u->len = 0;
388 
389 	return (0);
390 }
391 
392 static int
393 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
394 {
395 	struct xs_dev_data *u;
396 	int error, mask;
397 
398 	error = devfs_get_cdevpriv((void **)&u);
399 	if (error != 0)
400 		return (POLLERR);
401 
402 	/* we can always write */
403 	mask = events & (POLLOUT | POLLWRNORM);
404 
405 	if (events & (POLLIN | POLLRDNORM)) {
406 		if (u->read_cons != u->read_prod) {
407 			mask |= events & (POLLIN | POLLRDNORM);
408 		} else {
409 			/* Record that someone is waiting */
410 			selrecord(td, &u->ev_rsel);
411 		}
412 	}
413 
414 	return (mask);
415 }
416 
417 static void
418 xs_dev_dtor(void *arg)
419 {
420 	struct xs_dev_data *u = arg;
421 	struct xs_dev_transaction *trans, *tmpt;
422 	struct xs_dev_watch *watch, *tmpw;
423 
424 	seldrain(&u->ev_rsel);
425 
426 	LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
427 		xs_transaction_end(trans->handle, 1);
428 		LIST_REMOVE(trans, list);
429 		free(trans, M_XENSTORE);
430 	}
431 
432 	LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
433 		LIST_REMOVE(watch, list);
434 		xs_unregister_watch(&watch->watch);
435 		free(watch->watch.node, M_XENSTORE);
436 		free(watch->token, M_XENSTORE);
437 		free(watch, M_XENSTORE);
438 	}
439 	mtx_destroy(&u->lock);
440 
441 	free(u, M_XENSTORE);
442 }
443 
444 static int
445 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
446 {
447 	struct xs_dev_data *u;
448 	int error;
449 
450 	u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
451 	mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
452 	LIST_INIT(&u->transactions);
453 	LIST_INIT(&u->watches);
454 	error = devfs_set_cdevpriv(u, xs_dev_dtor);
455 	if (error != 0)
456 		free(u, M_XENSTORE);
457 
458 	return (error);
459 }
460 
461 static struct cdevsw xs_dev_cdevsw = {
462 	.d_version = D_VERSION,
463 	.d_read = xs_dev_read,
464 	.d_write = xs_dev_write,
465 	.d_open = xs_dev_open,
466 	.d_poll = xs_dev_poll,
467 	.d_name = "xs_dev",
468 };
469 
470 /*------------------ Private Device Attachment Functions  --------------------*/
471 /**
472  * \brief Identify instances of this device type in the system.
473  *
474  * \param driver  The driver performing this identify action.
475  * \param parent  The NewBus parent device for any devices this method adds.
476  */
477 static void
478 xs_dev_identify(driver_t *driver __unused, device_t parent)
479 {
480 	/*
481 	 * A single device instance for our driver is always present
482 	 * in a system operating under Xen.
483 	 */
484 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
485 }
486 
487 /**
488  * \brief Probe for the existence of the Xenstore device
489  *
490  * \param dev  NewBus device_t for this instance.
491  *
492  * \return  Always returns 0 indicating success.
493  */
494 static int
495 xs_dev_probe(device_t dev)
496 {
497 
498 	device_set_desc(dev, "Xenstore user-space device");
499 	return (0);
500 }
501 
502 /**
503  * \brief Attach the Xenstore device.
504  *
505  * \param dev  NewBus device_t for this instance.
506  *
507  * \return  On success, 0. Otherwise an errno value indicating the
508  *          type of failure.
509  */
510 static int
511 xs_dev_attach(device_t dev)
512 {
513 	struct cdev *xs_cdev;
514 	struct sysctl_ctx_list *sysctl_ctx;
515 	struct sysctl_oid *sysctl_tree;
516 
517 	sysctl_ctx = device_get_sysctl_ctx(dev);
518 	sysctl_tree = device_get_sysctl_tree(dev);
519 	if (sysctl_ctx == NULL || sysctl_tree == NULL)
520 	    return (EINVAL);
521 
522 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
523 	    "max_pending_watch_events", CTLFLAG_RW, &max_pending_watches, 0,
524 	    "maximum amount of pending watch events to be delivered");
525 
526 	xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
527 	    UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
528 	if (xs_cdev == NULL)
529 		return (EINVAL);
530 
531 	return (0);
532 }
533 
534 /*-------------------- Private Device Attachment Data  -----------------------*/
535 static device_method_t xs_dev_methods[] = {
536 	/* Device interface */
537 	DEVMETHOD(device_identify,	xs_dev_identify),
538 	DEVMETHOD(device_probe,         xs_dev_probe),
539 	DEVMETHOD(device_attach,        xs_dev_attach),
540 
541 	DEVMETHOD_END
542 };
543 
544 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
545 
546 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, NULL, NULL);
547