1a6aedc5dSRoger Pau Monné /*
2a6aedc5dSRoger Pau Monné * xenstore_dev.c
3a6aedc5dSRoger Pau Monné *
4a6aedc5dSRoger Pau Monné * Driver giving user-space access to the kernel's connection to the
5a6aedc5dSRoger Pau Monné * XenStore service.
6a6aedc5dSRoger Pau Monné *
7a6aedc5dSRoger Pau Monné * Copyright (c) 2005, Christian Limpach
8a6aedc5dSRoger Pau Monné * Copyright (c) 2005, Rusty Russell, IBM Corporation
9a6aedc5dSRoger Pau Monné *
10a6aedc5dSRoger Pau Monné * This file may be distributed separately from the Linux kernel, or
11a6aedc5dSRoger Pau Monné * incorporated into other software packages, subject to the following license:
12a6aedc5dSRoger Pau Monné *
13a6aedc5dSRoger Pau Monné * Permission is hereby granted, free of charge, to any person obtaining a copy
14a6aedc5dSRoger Pau Monné * of this source file (the "Software"), to deal in the Software without
15a6aedc5dSRoger Pau Monné * restriction, including without limitation the rights to use, copy, modify,
16a6aedc5dSRoger Pau Monné * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17a6aedc5dSRoger Pau Monné * and to permit persons to whom the Software is furnished to do so, subject to
18a6aedc5dSRoger Pau Monné * the following conditions:
19a6aedc5dSRoger Pau Monné *
20a6aedc5dSRoger Pau Monné * The above copyright notice and this permission notice shall be included in
21a6aedc5dSRoger Pau Monné * all copies or substantial portions of the Software.
22a6aedc5dSRoger Pau Monné *
23a6aedc5dSRoger Pau Monné * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24a6aedc5dSRoger Pau Monné * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25a6aedc5dSRoger Pau Monné * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26a6aedc5dSRoger Pau Monné * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27a6aedc5dSRoger Pau Monné * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28a6aedc5dSRoger Pau Monné * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29a6aedc5dSRoger Pau Monné * IN THE SOFTWARE.
30a6aedc5dSRoger Pau Monné */
31a6aedc5dSRoger Pau Monné
32a6aedc5dSRoger Pau Monné #include <sys/types.h>
33a6aedc5dSRoger Pau Monné #include <sys/cdefs.h>
34a6aedc5dSRoger Pau Monné #include <sys/errno.h>
35a6aedc5dSRoger Pau Monné #include <sys/uio.h>
36a6aedc5dSRoger Pau Monné #include <sys/param.h>
37a6aedc5dSRoger Pau Monné #include <sys/systm.h>
38a6aedc5dSRoger Pau Monné #include <sys/proc.h>
39a6aedc5dSRoger Pau Monné #include <sys/kernel.h>
40a6aedc5dSRoger Pau Monné #include <sys/malloc.h>
41a6aedc5dSRoger Pau Monné #include <sys/conf.h>
4245ce037dSRoger Pau Monné #include <sys/module.h>
435d747694SRoger Pau Monné #include <sys/selinfo.h>
444e4e43dcSRoger Pau Monné #include <sys/sysctl.h>
455d747694SRoger Pau Monné #include <sys/poll.h>
46a6aedc5dSRoger Pau Monné
47a6aedc5dSRoger Pau Monné #include <xen/xen-os.h>
48a6aedc5dSRoger Pau Monné
49a6aedc5dSRoger Pau Monné #include <xen/hypervisor.h>
50a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstorevar.h>
51a6aedc5dSRoger Pau Monné #include <xen/xenstore/xenstore_internal.h>
52a6aedc5dSRoger Pau Monné
534e4e43dcSRoger Pau Monné static unsigned int max_pending_watches = 1000;
544e4e43dcSRoger Pau Monné
55a6aedc5dSRoger Pau Monné struct xs_dev_transaction {
56a6aedc5dSRoger Pau Monné LIST_ENTRY(xs_dev_transaction) list;
57a6aedc5dSRoger Pau Monné struct xs_transaction handle;
58a6aedc5dSRoger Pau Monné };
59a6aedc5dSRoger Pau Monné
605d747694SRoger Pau Monné struct xs_dev_watch {
615d747694SRoger Pau Monné LIST_ENTRY(xs_dev_watch) list;
625d747694SRoger Pau Monné struct xs_watch watch;
635d747694SRoger Pau Monné char *token;
645d747694SRoger Pau Monné struct xs_dev_data *user;
655d747694SRoger Pau Monné };
665d747694SRoger Pau Monné
67a6aedc5dSRoger Pau Monné struct xs_dev_data {
68a6aedc5dSRoger Pau Monné /* In-progress transaction. */
695d747694SRoger Pau Monné LIST_HEAD(, xs_dev_transaction) transactions;
705d747694SRoger Pau Monné
715d747694SRoger Pau Monné /* Active watches. */
725d747694SRoger Pau Monné LIST_HEAD(, xs_dev_watch) watches;
73a6aedc5dSRoger Pau Monné
74a6aedc5dSRoger Pau Monné /* Partial request. */
75a6aedc5dSRoger Pau Monné unsigned int len;
76a6aedc5dSRoger Pau Monné union {
77a6aedc5dSRoger Pau Monné struct xsd_sockmsg msg;
78a6aedc5dSRoger Pau Monné char buffer[PAGE_SIZE];
79a6aedc5dSRoger Pau Monné } u;
80a6aedc5dSRoger Pau Monné
81a6aedc5dSRoger Pau Monné /* Response queue. */
82a6aedc5dSRoger Pau Monné #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
83a6aedc5dSRoger Pau Monné char read_buffer[PAGE_SIZE];
84a6aedc5dSRoger Pau Monné unsigned int read_cons, read_prod;
855d747694SRoger Pau Monné
865d747694SRoger Pau Monné /* Serializes writes to the read buffer. */
875d747694SRoger Pau Monné struct mtx lock;
885d747694SRoger Pau Monné
895d747694SRoger Pau Monné /* Polling structure (for reads only ATM). */
905d747694SRoger Pau Monné struct selinfo ev_rsel;
91a6aedc5dSRoger Pau Monné };
92a6aedc5dSRoger Pau Monné
935d747694SRoger Pau Monné static void
xs_queue_reply(struct xs_dev_data * u,const char * data,unsigned int len)945d747694SRoger Pau Monné xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
955d747694SRoger Pau Monné {
965d747694SRoger Pau Monné unsigned int i;
975d747694SRoger Pau Monné
985d747694SRoger Pau Monné for (i = 0; i < len; i++, u->read_prod++)
995d747694SRoger Pau Monné u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
1005d747694SRoger Pau Monné
1015d747694SRoger Pau Monné KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
1025d747694SRoger Pau Monné ("xenstore reply too big"));
1035d747694SRoger Pau Monné
1045d747694SRoger Pau Monné wakeup(u);
1055d747694SRoger Pau Monné selwakeup(&u->ev_rsel);
1065d747694SRoger Pau Monné }
1075d747694SRoger Pau Monné
1085d747694SRoger Pau Monné static const char *
xs_dev_error_to_string(int error)1095d747694SRoger Pau Monné xs_dev_error_to_string(int error)
1105d747694SRoger Pau Monné {
1115d747694SRoger Pau Monné unsigned int i;
1125d747694SRoger Pau Monné
1135d747694SRoger Pau Monné for (i = 0; i < nitems(xsd_errors); i++)
1145d747694SRoger Pau Monné if (xsd_errors[i].errnum == error)
1155d747694SRoger Pau Monné return (xsd_errors[i].errstring);
1165d747694SRoger Pau Monné
1175d747694SRoger Pau Monné return (NULL);
1185d747694SRoger Pau Monné }
1195d747694SRoger Pau Monné
1205d747694SRoger Pau Monné static void
xs_dev_return_error(struct xs_dev_data * u,int error,int req_id,int tx_id)1215d747694SRoger Pau Monné xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
1225d747694SRoger Pau Monné {
1235d747694SRoger Pau Monné struct xsd_sockmsg msg;
1245d747694SRoger Pau Monné const char *payload;
1255d747694SRoger Pau Monné
1265d747694SRoger Pau Monné msg.type = XS_ERROR;
1275d747694SRoger Pau Monné msg.req_id = req_id;
1285d747694SRoger Pau Monné msg.tx_id = tx_id;
1295d747694SRoger Pau Monné payload = NULL;
1305d747694SRoger Pau Monné
1315d747694SRoger Pau Monné payload = xs_dev_error_to_string(error);
1325d747694SRoger Pau Monné if (payload == NULL)
1335d747694SRoger Pau Monné payload = xs_dev_error_to_string(EINVAL);
1345d747694SRoger Pau Monné KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
1355d747694SRoger Pau Monné
1365d747694SRoger Pau Monné msg.len = strlen(payload) + 1;
1375d747694SRoger Pau Monné
1385d747694SRoger Pau Monné mtx_lock(&u->lock);
1395d747694SRoger Pau Monné xs_queue_reply(u, (char *)&msg, sizeof(msg));
1405d747694SRoger Pau Monné xs_queue_reply(u, payload, msg.len);
1415d747694SRoger Pau Monné mtx_unlock(&u->lock);
1425d747694SRoger Pau Monné }
1435d747694SRoger Pau Monné
1445d747694SRoger Pau Monné static int
xs_dev_watch_message_parse_string(const char ** p,const char * end,const char ** string_r)1455d747694SRoger Pau Monné xs_dev_watch_message_parse_string(const char **p, const char *end,
1465d747694SRoger Pau Monné const char **string_r)
1475d747694SRoger Pau Monné {
1485d747694SRoger Pau Monné const char *nul;
1495d747694SRoger Pau Monné
1505d747694SRoger Pau Monné nul = memchr(*p, 0, end - *p);
1515d747694SRoger Pau Monné if (!nul)
1525d747694SRoger Pau Monné return (EINVAL);
1535d747694SRoger Pau Monné
1545d747694SRoger Pau Monné *string_r = *p;
1555d747694SRoger Pau Monné *p = nul+1;
1565d747694SRoger Pau Monné
1575d747694SRoger Pau Monné return (0);
1585d747694SRoger Pau Monné }
1595d747694SRoger Pau Monné
1605d747694SRoger Pau Monné static int
xs_dev_watch_message_parse(const struct xsd_sockmsg * msg,const char ** path_r,const char ** token_r)1615d747694SRoger Pau Monné xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
1625d747694SRoger Pau Monné const char **token_r)
1635d747694SRoger Pau Monné {
1645d747694SRoger Pau Monné const char *p, *end;
1655d747694SRoger Pau Monné int error;
1665d747694SRoger Pau Monné
1675d747694SRoger Pau Monné p = (const char *)msg + sizeof(*msg);
1685d747694SRoger Pau Monné end = p + msg->len;
1695d747694SRoger Pau Monné KASSERT(p <= end, ("payload overflow"));
1705d747694SRoger Pau Monné
1715d747694SRoger Pau Monné error = xs_dev_watch_message_parse_string(&p, end, path_r);
1725d747694SRoger Pau Monné if (error)
1735d747694SRoger Pau Monné return (error);
1745d747694SRoger Pau Monné error = xs_dev_watch_message_parse_string(&p, end, token_r);
1755d747694SRoger Pau Monné if (error)
1765d747694SRoger Pau Monné return (error);
1775d747694SRoger Pau Monné
1785d747694SRoger Pau Monné return (0);
1795d747694SRoger Pau Monné }
1805d747694SRoger Pau Monné
1815d747694SRoger Pau Monné static struct xs_dev_watch *
xs_dev_find_watch(struct xs_dev_data * u,const char * token)1825d747694SRoger Pau Monné xs_dev_find_watch(struct xs_dev_data *u, const char *token)
1835d747694SRoger Pau Monné {
1845d747694SRoger Pau Monné struct xs_dev_watch *watch;
1855d747694SRoger Pau Monné
1865d747694SRoger Pau Monné LIST_FOREACH(watch, &u->watches, list)
1875d747694SRoger Pau Monné if (strcmp(watch->token, token) == 0)
1885d747694SRoger Pau Monné return (watch);
1895d747694SRoger Pau Monné
1905d747694SRoger Pau Monné return (NULL);
1915d747694SRoger Pau Monné }
1925d747694SRoger Pau Monné
1935d747694SRoger Pau Monné static void
xs_dev_watch_cb(struct xs_watch * watch,const char ** vec,unsigned int len)1945d747694SRoger Pau Monné xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
1955d747694SRoger Pau Monné {
1965d747694SRoger Pau Monné struct xs_dev_watch *dwatch;
1975d747694SRoger Pau Monné struct xsd_sockmsg msg;
1985d747694SRoger Pau Monné char *payload;
1995d747694SRoger Pau Monné
2005d747694SRoger Pau Monné dwatch = (struct xs_dev_watch *)watch->callback_data;
2015d747694SRoger Pau Monné msg.type = XS_WATCH_EVENT;
2025d747694SRoger Pau Monné msg.req_id = msg.tx_id = 0;
2035d747694SRoger Pau Monné msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
2045d747694SRoger Pau Monné
2055d747694SRoger Pau Monné payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
2065d747694SRoger Pau Monné strcpy(payload, vec[XS_WATCH_PATH]);
2075d747694SRoger Pau Monné strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
2085d747694SRoger Pau Monné mtx_lock(&dwatch->user->lock);
2095d747694SRoger Pau Monné xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
2105d747694SRoger Pau Monné xs_queue_reply(dwatch->user, payload, msg.len);
2115d747694SRoger Pau Monné mtx_unlock(&dwatch->user->lock);
2125d747694SRoger Pau Monné free(payload, M_XENSTORE);
2135d747694SRoger Pau Monné }
2145d747694SRoger Pau Monné
215b3a5ba30SRoger Pau Monné static struct xs_dev_transaction *
xs_dev_find_transaction(struct xs_dev_data * u,uint32_t tx_id)216b3a5ba30SRoger Pau Monné xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
217b3a5ba30SRoger Pau Monné {
218b3a5ba30SRoger Pau Monné struct xs_dev_transaction *trans;
219b3a5ba30SRoger Pau Monné
220b3a5ba30SRoger Pau Monné LIST_FOREACH(trans, &u->transactions, list)
221b3a5ba30SRoger Pau Monné if (trans->handle.id == tx_id)
222b3a5ba30SRoger Pau Monné return (trans);
223b3a5ba30SRoger Pau Monné
224b3a5ba30SRoger Pau Monné return (NULL);
225b3a5ba30SRoger Pau Monné }
226b3a5ba30SRoger Pau Monné
227a6aedc5dSRoger Pau Monné static int
xs_dev_read(struct cdev * dev,struct uio * uio,int ioflag)228a6aedc5dSRoger Pau Monné xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
229a6aedc5dSRoger Pau Monné {
230a6aedc5dSRoger Pau Monné int error;
231f79cdf29SRoger Pau Monné struct xs_dev_data *u;
232f79cdf29SRoger Pau Monné
233f79cdf29SRoger Pau Monné error = devfs_get_cdevpriv((void **)&u);
234f79cdf29SRoger Pau Monné if (error != 0)
235f79cdf29SRoger Pau Monné return (error);
236a6aedc5dSRoger Pau Monné
237a6aedc5dSRoger Pau Monné while (u->read_prod == u->read_cons) {
238a6aedc5dSRoger Pau Monné error = tsleep(u, PCATCH, "xsdread", hz/10);
239a6aedc5dSRoger Pau Monné if (error && error != EWOULDBLOCK)
240a6aedc5dSRoger Pau Monné return (error);
241a6aedc5dSRoger Pau Monné }
242a6aedc5dSRoger Pau Monné
243a6aedc5dSRoger Pau Monné while (uio->uio_resid > 0) {
244a6aedc5dSRoger Pau Monné if (u->read_cons == u->read_prod)
245a6aedc5dSRoger Pau Monné break;
246a6aedc5dSRoger Pau Monné error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
247a6aedc5dSRoger Pau Monné 1, uio);
248a6aedc5dSRoger Pau Monné if (error)
249a6aedc5dSRoger Pau Monné return (error);
250a6aedc5dSRoger Pau Monné u->read_cons++;
251a6aedc5dSRoger Pau Monné }
252a6aedc5dSRoger Pau Monné return (0);
253a6aedc5dSRoger Pau Monné }
254a6aedc5dSRoger Pau Monné
255a6aedc5dSRoger Pau Monné static int
xs_dev_write(struct cdev * dev,struct uio * uio,int ioflag)256a6aedc5dSRoger Pau Monné xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
257a6aedc5dSRoger Pau Monné {
258a6aedc5dSRoger Pau Monné int error;
2595d747694SRoger Pau Monné const char *wpath, *wtoken;
260f79cdf29SRoger Pau Monné struct xs_dev_data *u;
261a6aedc5dSRoger Pau Monné struct xs_dev_transaction *trans;
2625d747694SRoger Pau Monné struct xs_dev_watch *watch;
263a6aedc5dSRoger Pau Monné void *reply;
2645d747694SRoger Pau Monné static const char *ok = "OK";
265a6aedc5dSRoger Pau Monné int len = uio->uio_resid;
266a6aedc5dSRoger Pau Monné
267f79cdf29SRoger Pau Monné error = devfs_get_cdevpriv((void **)&u);
268f79cdf29SRoger Pau Monné if (error != 0)
269f79cdf29SRoger Pau Monné return (error);
270f79cdf29SRoger Pau Monné
271a6aedc5dSRoger Pau Monné if ((len + u->len) > sizeof(u->u.buffer))
272a6aedc5dSRoger Pau Monné return (EINVAL);
273a6aedc5dSRoger Pau Monné
274a6aedc5dSRoger Pau Monné error = uiomove(u->u.buffer + u->len, len, uio);
275a6aedc5dSRoger Pau Monné if (error)
276a6aedc5dSRoger Pau Monné return (error);
277a6aedc5dSRoger Pau Monné
278a6aedc5dSRoger Pau Monné u->len += len;
279a6aedc5dSRoger Pau Monné if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
280a6aedc5dSRoger Pau Monné return (0);
281a6aedc5dSRoger Pau Monné
282a6aedc5dSRoger Pau Monné switch (u->u.msg.type) {
283a6aedc5dSRoger Pau Monné case XS_TRANSACTION_START:
284a6aedc5dSRoger Pau Monné case XS_TRANSACTION_END:
285a6aedc5dSRoger Pau Monné case XS_DIRECTORY:
286a6aedc5dSRoger Pau Monné case XS_READ:
287a6aedc5dSRoger Pau Monné case XS_GET_PERMS:
288a6aedc5dSRoger Pau Monné case XS_RELEASE:
289a6aedc5dSRoger Pau Monné case XS_GET_DOMAIN_PATH:
290a6aedc5dSRoger Pau Monné case XS_WRITE:
291a6aedc5dSRoger Pau Monné case XS_MKDIR:
292a6aedc5dSRoger Pau Monné case XS_RM:
293a6aedc5dSRoger Pau Monné case XS_SET_PERMS:
294b3a5ba30SRoger Pau Monné /* Check that this transaction id is not hijacked. */
295b3a5ba30SRoger Pau Monné if (u->u.msg.tx_id != 0 &&
296b3a5ba30SRoger Pau Monné xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
297b3a5ba30SRoger Pau Monné error = EINVAL;
298b3a5ba30SRoger Pau Monné break;
299b3a5ba30SRoger Pau Monné }
300a6aedc5dSRoger Pau Monné error = xs_dev_request_and_reply(&u->u.msg, &reply);
301a6aedc5dSRoger Pau Monné if (!error) {
302a6aedc5dSRoger Pau Monné if (u->u.msg.type == XS_TRANSACTION_START) {
303a6aedc5dSRoger Pau Monné trans = malloc(sizeof(*trans), M_XENSTORE,
304a6aedc5dSRoger Pau Monné M_WAITOK);
305a6aedc5dSRoger Pau Monné trans->handle.id = strtoul(reply, NULL, 0);
306a6aedc5dSRoger Pau Monné LIST_INSERT_HEAD(&u->transactions, trans, list);
307a6aedc5dSRoger Pau Monné } else if (u->u.msg.type == XS_TRANSACTION_END) {
308b3a5ba30SRoger Pau Monné trans = xs_dev_find_transaction(u,
309b3a5ba30SRoger Pau Monné u->u.msg.tx_id);
310b3a5ba30SRoger Pau Monné KASSERT(trans != NULL,
311b3a5ba30SRoger Pau Monné ("Unable to find transaction"));
312a6aedc5dSRoger Pau Monné LIST_REMOVE(trans, list);
313a6aedc5dSRoger Pau Monné free(trans, M_XENSTORE);
314a6aedc5dSRoger Pau Monné }
3155d747694SRoger Pau Monné mtx_lock(&u->lock);
316a6aedc5dSRoger Pau Monné xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
317a6aedc5dSRoger Pau Monné xs_queue_reply(u, (char *)reply, u->u.msg.len);
3185d747694SRoger Pau Monné mtx_unlock(&u->lock);
319a6aedc5dSRoger Pau Monné free(reply, M_XENSTORE);
320a6aedc5dSRoger Pau Monné }
321a6aedc5dSRoger Pau Monné break;
3225d747694SRoger Pau Monné case XS_WATCH:
3235d747694SRoger Pau Monné u->u.msg.tx_id = 0;
3245d747694SRoger Pau Monné error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
3255d747694SRoger Pau Monné if (error)
3265d747694SRoger Pau Monné break;
3275d747694SRoger Pau Monné if (xs_dev_find_watch(u, wtoken) != NULL) {
3285d747694SRoger Pau Monné error = EINVAL;
3295d747694SRoger Pau Monné break;
3305d747694SRoger Pau Monné }
331a6aedc5dSRoger Pau Monné
3325d747694SRoger Pau Monné watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
3335d747694SRoger Pau Monné watch->watch.node = strdup(wpath, M_XENSTORE);
3345d747694SRoger Pau Monné watch->watch.callback = xs_dev_watch_cb;
3355d747694SRoger Pau Monné watch->watch.callback_data = (uintptr_t)watch;
3364e4e43dcSRoger Pau Monné watch->watch.max_pending = max_pending_watches;
3375d747694SRoger Pau Monné watch->token = strdup(wtoken, M_XENSTORE);
3385d747694SRoger Pau Monné watch->user = u;
3395d747694SRoger Pau Monné
3405d747694SRoger Pau Monné error = xs_register_watch(&watch->watch);
3415d747694SRoger Pau Monné if (error != 0) {
3425d747694SRoger Pau Monné free(watch->token, M_XENSTORE);
3435d747694SRoger Pau Monné free(watch->watch.node, M_XENSTORE);
3445d747694SRoger Pau Monné free(watch, M_XENSTORE);
3455d747694SRoger Pau Monné break;
3465d747694SRoger Pau Monné }
3475d747694SRoger Pau Monné
3485d747694SRoger Pau Monné LIST_INSERT_HEAD(&u->watches, watch, list);
3495d747694SRoger Pau Monné u->u.msg.len = sizeof(ok);
3505d747694SRoger Pau Monné mtx_lock(&u->lock);
3515d747694SRoger Pau Monné xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
3525d747694SRoger Pau Monné xs_queue_reply(u, ok, sizeof(ok));
3535d747694SRoger Pau Monné mtx_unlock(&u->lock);
3545d747694SRoger Pau Monné break;
3555d747694SRoger Pau Monné case XS_UNWATCH:
3565d747694SRoger Pau Monné u->u.msg.tx_id = 0;
3575d747694SRoger Pau Monné error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
3585d747694SRoger Pau Monné if (error)
3595d747694SRoger Pau Monné break;
3605d747694SRoger Pau Monné watch = xs_dev_find_watch(u, wtoken);
3615d747694SRoger Pau Monné if (watch == NULL) {
3625d747694SRoger Pau Monné error = EINVAL;
3635d747694SRoger Pau Monné break;
3645d747694SRoger Pau Monné }
3655d747694SRoger Pau Monné
3665d747694SRoger Pau Monné LIST_REMOVE(watch, list);
3675d747694SRoger Pau Monné xs_unregister_watch(&watch->watch);
3685d747694SRoger Pau Monné free(watch->watch.node, M_XENSTORE);
3695d747694SRoger Pau Monné free(watch->token, M_XENSTORE);
3705d747694SRoger Pau Monné free(watch, M_XENSTORE);
3715d747694SRoger Pau Monné u->u.msg.len = sizeof(ok);
3725d747694SRoger Pau Monné mtx_lock(&u->lock);
3735d747694SRoger Pau Monné xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
3745d747694SRoger Pau Monné xs_queue_reply(u, ok, sizeof(ok));
3755d747694SRoger Pau Monné mtx_unlock(&u->lock);
3765d747694SRoger Pau Monné break;
377a6aedc5dSRoger Pau Monné default:
378a6aedc5dSRoger Pau Monné error = EINVAL;
379a6aedc5dSRoger Pau Monné break;
380a6aedc5dSRoger Pau Monné }
381a6aedc5dSRoger Pau Monné
3825d747694SRoger Pau Monné if (error != 0)
3835d747694SRoger Pau Monné xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
3845d747694SRoger Pau Monné
3855d747694SRoger Pau Monné /* Reset the write buffer. */
386a6aedc5dSRoger Pau Monné u->len = 0;
387a6aedc5dSRoger Pau Monné
3885d747694SRoger Pau Monné return (0);
3895d747694SRoger Pau Monné }
3905d747694SRoger Pau Monné
3915d747694SRoger Pau Monné static int
xs_dev_poll(struct cdev * dev,int events,struct thread * td)3925d747694SRoger Pau Monné xs_dev_poll(struct cdev *dev, int events, struct thread *td)
3935d747694SRoger Pau Monné {
3945d747694SRoger Pau Monné struct xs_dev_data *u;
3955d747694SRoger Pau Monné int error, mask;
3965d747694SRoger Pau Monné
3975d747694SRoger Pau Monné error = devfs_get_cdevpriv((void **)&u);
3985d747694SRoger Pau Monné if (error != 0)
3995d747694SRoger Pau Monné return (POLLERR);
4005d747694SRoger Pau Monné
4015d747694SRoger Pau Monné /* we can always write */
4025d747694SRoger Pau Monné mask = events & (POLLOUT | POLLWRNORM);
4035d747694SRoger Pau Monné
4045d747694SRoger Pau Monné if (events & (POLLIN | POLLRDNORM)) {
4055d747694SRoger Pau Monné if (u->read_cons != u->read_prod) {
4065d747694SRoger Pau Monné mask |= events & (POLLIN | POLLRDNORM);
4075d747694SRoger Pau Monné } else {
4085d747694SRoger Pau Monné /* Record that someone is waiting */
4095d747694SRoger Pau Monné selrecord(td, &u->ev_rsel);
4105d747694SRoger Pau Monné }
4115d747694SRoger Pau Monné }
4125d747694SRoger Pau Monné
4135d747694SRoger Pau Monné return (mask);
414a6aedc5dSRoger Pau Monné }
415a6aedc5dSRoger Pau Monné
416f79cdf29SRoger Pau Monné static void
xs_dev_dtor(void * arg)417f79cdf29SRoger Pau Monné xs_dev_dtor(void *arg)
418a6aedc5dSRoger Pau Monné {
419f79cdf29SRoger Pau Monné struct xs_dev_data *u = arg;
4205d747694SRoger Pau Monné struct xs_dev_transaction *trans, *tmpt;
4215d747694SRoger Pau Monné struct xs_dev_watch *watch, *tmpw;
422a6aedc5dSRoger Pau Monné
4235d747694SRoger Pau Monné seldrain(&u->ev_rsel);
4245d747694SRoger Pau Monné
4255d747694SRoger Pau Monné LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
426a6aedc5dSRoger Pau Monné xs_transaction_end(trans->handle, 1);
427a6aedc5dSRoger Pau Monné LIST_REMOVE(trans, list);
428a6aedc5dSRoger Pau Monné free(trans, M_XENSTORE);
429a6aedc5dSRoger Pau Monné }
430a6aedc5dSRoger Pau Monné
4315d747694SRoger Pau Monné LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
4325d747694SRoger Pau Monné LIST_REMOVE(watch, list);
4335d747694SRoger Pau Monné xs_unregister_watch(&watch->watch);
4345d747694SRoger Pau Monné free(watch->watch.node, M_XENSTORE);
4355d747694SRoger Pau Monné free(watch->token, M_XENSTORE);
4365d747694SRoger Pau Monné free(watch, M_XENSTORE);
4375d747694SRoger Pau Monné }
4385d747694SRoger Pau Monné mtx_destroy(&u->lock);
4395d747694SRoger Pau Monné
440a6aedc5dSRoger Pau Monné free(u, M_XENSTORE);
441f79cdf29SRoger Pau Monné }
442f79cdf29SRoger Pau Monné
443f79cdf29SRoger Pau Monné static int
xs_dev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)444f79cdf29SRoger Pau Monné xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
445f79cdf29SRoger Pau Monné {
446f79cdf29SRoger Pau Monné struct xs_dev_data *u;
447f79cdf29SRoger Pau Monné int error;
448f79cdf29SRoger Pau Monné
449f79cdf29SRoger Pau Monné u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
4505d747694SRoger Pau Monné mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
451f79cdf29SRoger Pau Monné LIST_INIT(&u->transactions);
4525d747694SRoger Pau Monné LIST_INIT(&u->watches);
453f79cdf29SRoger Pau Monné error = devfs_set_cdevpriv(u, xs_dev_dtor);
454f79cdf29SRoger Pau Monné if (error != 0)
455f79cdf29SRoger Pau Monné free(u, M_XENSTORE);
456f79cdf29SRoger Pau Monné
457f79cdf29SRoger Pau Monné return (error);
458a6aedc5dSRoger Pau Monné }
459a6aedc5dSRoger Pau Monné
460a6aedc5dSRoger Pau Monné static struct cdevsw xs_dev_cdevsw = {
461a6aedc5dSRoger Pau Monné .d_version = D_VERSION,
462a6aedc5dSRoger Pau Monné .d_read = xs_dev_read,
463a6aedc5dSRoger Pau Monné .d_write = xs_dev_write,
464a6aedc5dSRoger Pau Monné .d_open = xs_dev_open,
4655d747694SRoger Pau Monné .d_poll = xs_dev_poll,
466a6aedc5dSRoger Pau Monné .d_name = "xs_dev",
467a6aedc5dSRoger Pau Monné };
468a6aedc5dSRoger Pau Monné
46945ce037dSRoger Pau Monné /*------------------ Private Device Attachment Functions --------------------*/
47045ce037dSRoger Pau Monné /**
47145ce037dSRoger Pau Monné * \brief Identify instances of this device type in the system.
47245ce037dSRoger Pau Monné *
47345ce037dSRoger Pau Monné * \param driver The driver performing this identify action.
47445ce037dSRoger Pau Monné * \param parent The NewBus parent device for any devices this method adds.
47545ce037dSRoger Pau Monné */
47645ce037dSRoger Pau Monné static void
xs_dev_identify(driver_t * driver,device_t parent)477*d48760ffSElliott Mitchell xs_dev_identify(driver_t *driver, device_t parent)
478a6aedc5dSRoger Pau Monné {
47945ce037dSRoger Pau Monné /*
48045ce037dSRoger Pau Monné * A single device instance for our driver is always present
48145ce037dSRoger Pau Monné * in a system operating under Xen.
48245ce037dSRoger Pau Monné */
48345ce037dSRoger Pau Monné BUS_ADD_CHILD(parent, 0, driver->name, 0);
484a6aedc5dSRoger Pau Monné }
48545ce037dSRoger Pau Monné
48645ce037dSRoger Pau Monné /**
487453130d9SPedro F. Giffuni * \brief Probe for the existence of the Xenstore device
48845ce037dSRoger Pau Monné *
48945ce037dSRoger Pau Monné * \param dev NewBus device_t for this instance.
49045ce037dSRoger Pau Monné *
49145ce037dSRoger Pau Monné * \return Always returns 0 indicating success.
49245ce037dSRoger Pau Monné */
49345ce037dSRoger Pau Monné static int
xs_dev_probe(device_t dev)49445ce037dSRoger Pau Monné xs_dev_probe(device_t dev)
49545ce037dSRoger Pau Monné {
49645ce037dSRoger Pau Monné
49745ce037dSRoger Pau Monné device_set_desc(dev, "Xenstore user-space device");
49845ce037dSRoger Pau Monné return (0);
49945ce037dSRoger Pau Monné }
50045ce037dSRoger Pau Monné
50145ce037dSRoger Pau Monné /**
50245ce037dSRoger Pau Monné * \brief Attach the Xenstore device.
50345ce037dSRoger Pau Monné *
50445ce037dSRoger Pau Monné * \param dev NewBus device_t for this instance.
50545ce037dSRoger Pau Monné *
50645ce037dSRoger Pau Monné * \return On success, 0. Otherwise an errno value indicating the
50745ce037dSRoger Pau Monné * type of failure.
50845ce037dSRoger Pau Monné */
50945ce037dSRoger Pau Monné static int
xs_dev_attach(device_t dev)51045ce037dSRoger Pau Monné xs_dev_attach(device_t dev)
51145ce037dSRoger Pau Monné {
51245ce037dSRoger Pau Monné struct cdev *xs_cdev;
5134e4e43dcSRoger Pau Monné struct sysctl_ctx_list *sysctl_ctx;
5144e4e43dcSRoger Pau Monné struct sysctl_oid *sysctl_tree;
5154e4e43dcSRoger Pau Monné
5164e4e43dcSRoger Pau Monné sysctl_ctx = device_get_sysctl_ctx(dev);
5174e4e43dcSRoger Pau Monné sysctl_tree = device_get_sysctl_tree(dev);
5184e4e43dcSRoger Pau Monné if (sysctl_ctx == NULL || sysctl_tree == NULL)
5194e4e43dcSRoger Pau Monné return (EINVAL);
5204e4e43dcSRoger Pau Monné
5214e4e43dcSRoger Pau Monné SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
5224e4e43dcSRoger Pau Monné "max_pending_watch_events", CTLFLAG_RW, &max_pending_watches, 0,
5234e4e43dcSRoger Pau Monné "maximum amount of pending watch events to be delivered");
52445ce037dSRoger Pau Monné
525f79cdf29SRoger Pau Monné xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
526f79cdf29SRoger Pau Monné UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
52745ce037dSRoger Pau Monné if (xs_cdev == NULL)
52845ce037dSRoger Pau Monné return (EINVAL);
52945ce037dSRoger Pau Monné
53045ce037dSRoger Pau Monné return (0);
53145ce037dSRoger Pau Monné }
53245ce037dSRoger Pau Monné
53345ce037dSRoger Pau Monné /*-------------------- Private Device Attachment Data -----------------------*/
53445ce037dSRoger Pau Monné static device_method_t xs_dev_methods[] = {
53545ce037dSRoger Pau Monné /* Device interface */
53645ce037dSRoger Pau Monné DEVMETHOD(device_identify, xs_dev_identify),
53745ce037dSRoger Pau Monné DEVMETHOD(device_probe, xs_dev_probe),
53845ce037dSRoger Pau Monné DEVMETHOD(device_attach, xs_dev_attach),
53945ce037dSRoger Pau Monné
54045ce037dSRoger Pau Monné DEVMETHOD_END
54145ce037dSRoger Pau Monné };
54245ce037dSRoger Pau Monné
54345ce037dSRoger Pau Monné DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
54445ce037dSRoger Pau Monné
545f929eb1eSJohn Baldwin DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, NULL, NULL);
546