xen-vtx-unstable

annotate linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c @ 6250:5a97aa8698d9

Add support to xenbus_gather to scan for strings and return them in allocated memory.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 18 19:24:42 2005 +0000 (2005-08-18)
parents 8203b7d536d3
children 509316987d65 66348ff38ec1 87dec3b9c546
rev   line source
cl349@5598 1 /******************************************************************************
cl349@5598 2 * xenbus_xs.c
cl349@5598 3 *
cl349@5598 4 * This is the kernel equivalent of the "xs" library. We don't need everything
cl349@5863 5 * and we use xenbus_comms for communication.
cl349@5598 6 *
cl349@5598 7 * Copyright (C) 2005 Rusty Russell, IBM Corporation
cl349@5598 8 *
cl349@5598 9 * This file may be distributed separately from the Linux kernel, or
cl349@5598 10 * incorporated into other software packages, subject to the following license:
cl349@5598 11 *
cl349@5598 12 * Permission is hereby granted, free of charge, to any person obtaining a copy
cl349@5598 13 * of this source file (the "Software"), to deal in the Software without
cl349@5598 14 * restriction, including without limitation the rights to use, copy, modify,
cl349@5598 15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
cl349@5598 16 * and to permit persons to whom the Software is furnished to do so, subject to
cl349@5598 17 * the following conditions:
cl349@5598 18 *
cl349@5598 19 * The above copyright notice and this permission notice shall be included in
cl349@5598 20 * all copies or substantial portions of the Software.
cl349@5598 21 *
cl349@5598 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cl349@5598 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cl349@5598 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cl349@5598 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cl349@5598 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
cl349@5598 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
cl349@5598 28 * IN THE SOFTWARE.
cl349@5598 29 */
cl349@5598 30
cl349@5598 31 #include <linux/errno.h>
cl349@5598 32 #include <linux/types.h>
cl349@5598 33 #include <linux/uio.h>
cl349@5598 34 #include <linux/kernel.h>
cl349@5598 35 #include <linux/string.h>
cl349@5598 36 #include <linux/err.h>
cl349@5598 37 #include <linux/slab.h>
cl349@5598 38 #include <linux/fcntl.h>
cl349@5598 39 #include <linux/kthread.h>
cl349@5598 40 #include <asm-xen/xenbus.h>
kaf24@6065 41 #include "xenstored.h"
cl349@5598 42 #include "xenbus_comms.h"
cl349@5598 43
cl349@5598 44 #define streq(a, b) (strcmp((a), (b)) == 0)
cl349@5598 45
cl349@5874 46 static char printf_buffer[4096];
cl349@5598 47 static LIST_HEAD(watches);
cl349@5863 48 DECLARE_MUTEX(xenbus_lock);
cl349@5598 49
cl349@5598 50 static int get_error(const char *errorstring)
cl349@5598 51 {
cl349@5598 52 unsigned int i;
cl349@5598 53
cl349@5598 54 for (i = 0; !streq(errorstring, xsd_errors[i].errstring); i++) {
cl349@5598 55 if (i == ARRAY_SIZE(xsd_errors) - 1) {
cl349@5670 56 printk(KERN_WARNING
cl349@5670 57 "XENBUS xen store gave: unknown error %s",
cl349@5598 58 errorstring);
cl349@5598 59 return EINVAL;
cl349@5598 60 }
cl349@5598 61 }
cl349@5598 62 return xsd_errors[i].errnum;
cl349@5598 63 }
cl349@5598 64
cl349@5598 65 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
cl349@5598 66 {
cl349@5598 67 struct xsd_sockmsg msg;
cl349@5598 68 void *ret;
cl349@5598 69 int err;
cl349@5598 70
cl349@5902 71 err = xb_read(&msg, sizeof(msg));
cl349@5598 72 if (err)
cl349@5598 73 return ERR_PTR(err);
cl349@5598 74
cl349@5598 75 ret = kmalloc(msg.len + 1, GFP_KERNEL);
cl349@5598 76 if (!ret)
cl349@5598 77 return ERR_PTR(-ENOMEM);
cl349@5598 78
cl349@5902 79 err = xb_read(ret, msg.len);
cl349@5598 80 if (err) {
cl349@5598 81 kfree(ret);
cl349@5598 82 return ERR_PTR(err);
cl349@5598 83 }
cl349@5598 84 ((char*)ret)[msg.len] = '\0';
cl349@5598 85
cl349@5598 86 *type = msg.type;
cl349@5598 87 if (len)
cl349@5598 88 *len = msg.len;
cl349@5598 89 return ret;
cl349@5598 90 }
cl349@5598 91
cl349@5747 92 /* Emergency write. */
cl349@5863 93 void xenbus_debug_write(const char *str, unsigned int count)
cl349@5747 94 {
cl349@5747 95 struct xsd_sockmsg msg;
cl349@5747 96
cl349@5747 97 msg.type = XS_DEBUG;
cl349@5747 98 msg.len = sizeof("print") + count + 1;
cl349@5747 99
cl349@5902 100 xb_write(&msg, sizeof(msg));
cl349@5902 101 xb_write("print", sizeof("print"));
cl349@5902 102 xb_write(str, count);
cl349@5902 103 xb_write("", 1);
cl349@5747 104 }
cl349@5747 105
cl349@5598 106 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
cl349@5598 107 static void *xs_talkv(enum xsd_sockmsg_type type,
cl349@5598 108 const struct kvec *iovec,
cl349@5598 109 unsigned int num_vecs,
cl349@5598 110 unsigned int *len)
cl349@5598 111 {
cl349@5598 112 struct xsd_sockmsg msg;
cl349@5598 113 void *ret = NULL;
cl349@5598 114 unsigned int i;
cl349@5598 115 int err;
cl349@5598 116
cl349@5863 117 WARN_ON(down_trylock(&xenbus_lock) == 0);
cl349@5598 118
cl349@5598 119 msg.type = type;
cl349@5598 120 msg.len = 0;
cl349@5598 121 for (i = 0; i < num_vecs; i++)
cl349@5598 122 msg.len += iovec[i].iov_len;
cl349@5598 123
cl349@5902 124 err = xb_write(&msg, sizeof(msg));
cl349@5598 125 if (err)
cl349@5598 126 return ERR_PTR(err);
cl349@5598 127
cl349@5598 128 for (i = 0; i < num_vecs; i++) {
cl349@5902 129 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);;
cl349@5598 130 if (err)
cl349@5598 131 return ERR_PTR(err);
cl349@5598 132 }
cl349@5598 133
cl349@5598 134 /* Watches can have fired before reply comes: daemon detects
cl349@5598 135 * and re-transmits, so we can ignore this. */
cl349@5598 136 do {
cl349@5598 137 kfree(ret);
cl349@5598 138 ret = read_reply(&msg.type, len);
cl349@5598 139 if (IS_ERR(ret))
cl349@5598 140 return ret;
cl349@5598 141 } while (msg.type == XS_WATCH_EVENT);
cl349@5598 142
cl349@5598 143 if (msg.type == XS_ERROR) {
cl349@5598 144 err = get_error(ret);
cl349@5598 145 kfree(ret);
cl349@5598 146 return ERR_PTR(-err);
cl349@5598 147 }
cl349@5598 148
cl349@5598 149 BUG_ON(msg.type != type);
cl349@5598 150 return ret;
cl349@5598 151 }
cl349@5598 152
cl349@5598 153 /* Simplified version of xs_talkv: single message. */
cl349@5598 154 static void *xs_single(enum xsd_sockmsg_type type,
cl349@5598 155 const char *string, unsigned int *len)
cl349@5598 156 {
cl349@5598 157 struct kvec iovec;
cl349@5598 158
cl349@5598 159 iovec.iov_base = (void *)string;
cl349@5598 160 iovec.iov_len = strlen(string) + 1;
cl349@5598 161 return xs_talkv(type, &iovec, 1, len);
cl349@5598 162 }
cl349@5598 163
cl349@5598 164 /* Many commands only need an ack, don't care what it says. */
cl349@5598 165 static int xs_error(char *reply)
cl349@5598 166 {
cl349@5598 167 if (IS_ERR(reply))
cl349@5598 168 return PTR_ERR(reply);
cl349@5598 169 kfree(reply);
cl349@5598 170 return 0;
cl349@5598 171 }
cl349@5598 172
cl349@5598 173 static unsigned int count_strings(const char *strings, unsigned int len)
cl349@5598 174 {
cl349@5598 175 unsigned int num;
cl349@5598 176 const char *p;
cl349@5598 177
cl349@5598 178 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
cl349@5598 179 num++;
cl349@5598 180
cl349@5598 181 return num;
cl349@5598 182 }
cl349@5598 183
cl349@5863 184 /* Return the path to dir with /name appended. */
cl349@5863 185 static char *join(const char *dir, const char *name)
cl349@5863 186 {
cl349@5863 187 static char buffer[4096];
cl349@5863 188
cl349@5863 189 BUG_ON(down_trylock(&xenbus_lock) == 0);
cl349@6250 190 /* XXX FIXME: might not be correct if name == "" */
cl349@5863 191 BUG_ON(strlen(dir) + strlen("/") + strlen(name) + 1 > sizeof(buffer));
cl349@5863 192
cl349@5863 193 strcpy(buffer, dir);
cl349@5863 194 if (!streq(name, "")) {
cl349@5863 195 strcat(buffer, "/");
cl349@5863 196 strcat(buffer, name);
cl349@5863 197 }
cl349@5863 198 return buffer;
cl349@5863 199 }
cl349@5863 200
cl349@5863 201 char **xenbus_directory(const char *dir, const char *node, unsigned int *num)
cl349@5598 202 {
cl349@5598 203 char *strings, *p, **ret;
cl349@5598 204 unsigned int len;
cl349@5598 205
cl349@5863 206 strings = xs_single(XS_DIRECTORY, join(dir, node), &len);
cl349@5598 207 if (IS_ERR(strings))
cl349@5598 208 return (char **)strings;
cl349@5598 209
cl349@5598 210 /* Count the strings. */
cl349@5598 211 *num = count_strings(strings, len);
cl349@5598 212
cl349@5598 213 /* Transfer to one big alloc for easy freeing. */
cl349@5598 214 ret = kmalloc(*num * sizeof(char *) + len, GFP_ATOMIC);
cl349@5598 215 if (!ret) {
cl349@5598 216 kfree(strings);
cl349@5598 217 return ERR_PTR(-ENOMEM);
cl349@5598 218 }
cl349@5598 219 memcpy(&ret[*num], strings, len);
cl349@5598 220 kfree(strings);
cl349@5598 221
cl349@5598 222 strings = (char *)&ret[*num];
cl349@5598 223 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
cl349@5598 224 ret[(*num)++] = p;
cl349@5598 225 return ret;
cl349@5598 226 }
cl349@5598 227
cl349@5598 228 /* Check if a path exists. Return 1 if it does. */
cl349@5863 229 int xenbus_exists(const char *dir, const char *node)
cl349@5598 230 {
cl349@5863 231 char **d;
cl349@5670 232 int dir_n;
cl349@5598 233
cl349@5867 234 d = xenbus_directory(dir, node, &dir_n);
cl349@5863 235 if (IS_ERR(d))
cl349@5670 236 return 0;
cl349@5863 237 kfree(d);
cl349@5670 238 return 1;
cl349@5598 239 }
cl349@5598 240
cl349@5598 241 /* Get the value of a single file.
cl349@5598 242 * Returns a kmalloced value: call free() on it after use.
cl349@5598 243 * len indicates length in bytes.
cl349@5598 244 */
cl349@5863 245 void *xenbus_read(const char *dir, const char *node, unsigned int *len)
cl349@5598 246 {
cl349@5863 247 return xs_single(XS_READ, join(dir, node), len);
cl349@5598 248 }
cl349@5598 249
cl349@5598 250 /* Write the value of a single file.
cl349@5598 251 * Returns -err on failure. createflags can be 0, O_CREAT, or O_CREAT|O_EXCL.
cl349@5598 252 */
cl349@5863 253 int xenbus_write(const char *dir, const char *node,
cl349@5863 254 const char *string, int createflags)
cl349@5598 255 {
cl349@5863 256 const char *flags, *path;
cl349@5598 257 struct kvec iovec[3];
cl349@5598 258
cl349@5863 259 path = join(dir, node);
cl349@5598 260 /* Format: Flags (as string), path, data. */
cl349@5598 261 if (createflags == 0)
cl349@5598 262 flags = XS_WRITE_NONE;
cl349@5598 263 else if (createflags == O_CREAT)
cl349@5598 264 flags = XS_WRITE_CREATE;
cl349@5598 265 else if (createflags == (O_CREAT|O_EXCL))
cl349@5598 266 flags = XS_WRITE_CREATE_EXCL;
cl349@5598 267 else
cl349@5598 268 return -EINVAL;
cl349@5598 269
cl349@5598 270 iovec[0].iov_base = (void *)path;
cl349@5598 271 iovec[0].iov_len = strlen(path) + 1;
cl349@5598 272 iovec[1].iov_base = (void *)flags;
cl349@5598 273 iovec[1].iov_len = strlen(flags) + 1;
cl349@5863 274 iovec[2].iov_base = (void *)string;
cl349@5863 275 iovec[2].iov_len = strlen(string);
cl349@5598 276
cl349@5598 277 return xs_error(xs_talkv(XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
cl349@5598 278 }
cl349@5598 279
cl349@5598 280 /* Create a new directory. */
cl349@5863 281 int xenbus_mkdir(const char *dir, const char *node)
cl349@5598 282 {
cl349@5863 283 return xs_error(xs_single(XS_MKDIR, join(dir, node), NULL));
cl349@5598 284 }
cl349@5598 285
cl349@5598 286 /* Destroy a file or directory (directories must be empty). */
cl349@5863 287 int xenbus_rm(const char *dir, const char *node)
cl349@5598 288 {
cl349@5863 289 return xs_error(xs_single(XS_RM, join(dir, node), NULL));
cl349@5598 290 }
cl349@5598 291
cl349@5598 292 /* Start a transaction: changes by others will not be seen during this
cl349@5598 293 * transaction, and changes will not be visible to others until end.
cl349@5598 294 * Transaction only applies to the given subtree.
cl349@5598 295 * You can only have one transaction at any time.
cl349@5598 296 */
cl349@5863 297 int xenbus_transaction_start(const char *subtree)
cl349@5598 298 {
cl349@5598 299 return xs_error(xs_single(XS_TRANSACTION_START, subtree, NULL));
cl349@5598 300 }
cl349@5598 301
cl349@5598 302 /* End a transaction.
cl349@5598 303 * If abandon is true, transaction is discarded instead of committed.
cl349@5598 304 */
cl349@5863 305 int xenbus_transaction_end(int abort)
cl349@5598 306 {
cl349@5598 307 char abortstr[2];
cl349@5598 308
cl349@5598 309 if (abort)
cl349@5598 310 strcpy(abortstr, "F");
cl349@5598 311 else
cl349@5598 312 strcpy(abortstr, "T");
cl349@5598 313 return xs_error(xs_single(XS_TRANSACTION_END, abortstr, NULL));
cl349@5598 314 }
cl349@5598 315
cl349@5863 316 /* Single read and scanf: returns -errno or num scanned. */
cl349@5863 317 int xenbus_scanf(const char *dir, const char *node, const char *fmt, ...)
cl349@5598 318 {
cl349@5863 319 va_list ap;
cl349@5863 320 int ret;
cl349@5863 321 char *val;
cl349@5863 322
cl349@5863 323 val = xenbus_read(dir, node, NULL);
cl349@5863 324 if (IS_ERR(val))
cl349@5863 325 return PTR_ERR(val);
cl349@5598 326
cl349@5863 327 va_start(ap, fmt);
cl349@5863 328 ret = vsscanf(val, fmt, ap);
cl349@5863 329 va_end(ap);
cl349@5863 330 kfree(val);
cl349@5863 331 /* Distinctive errno. */
cl349@5863 332 if (ret == 0)
cl349@5863 333 return -ERANGE;
cl349@5863 334 return ret;
cl349@5863 335 }
cl349@5863 336
cl349@5863 337 /* Single printf and write: returns -errno or 0. */
cl349@5863 338 int xenbus_printf(const char *dir, const char *node, const char *fmt, ...)
cl349@5598 339 {
cl349@5863 340 va_list ap;
cl349@5863 341 int ret;
cl349@5863 342
cl349@5863 343 BUG_ON(down_trylock(&xenbus_lock) == 0);
cl349@5863 344 va_start(ap, fmt);
cl349@5874 345 ret = vsnprintf(printf_buffer, sizeof(printf_buffer), fmt, ap);
cl349@5863 346 va_end(ap);
cl349@5863 347
cl349@5874 348 BUG_ON(ret > sizeof(printf_buffer)-1);
cl349@5874 349 return xenbus_write(dir, node, printf_buffer, O_CREAT);
cl349@5598 350 }
cl349@5598 351
cl349@5874 352 /* Report a (negative) errno into the store, with explanation. */
cl349@5874 353 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
cl349@5874 354 {
cl349@5874 355 va_list ap;
cl349@5874 356 int ret;
cl349@5874 357 unsigned int len;
cl349@5874 358
cl349@5874 359 BUG_ON(down_trylock(&xenbus_lock) == 0);
cl349@5874 360
cl349@5874 361 len = sprintf(printf_buffer, "%i ", -err);
cl349@5874 362 va_start(ap, fmt);
cl349@5874 363 ret = vsnprintf(printf_buffer+len, sizeof(printf_buffer)-len, fmt, ap);
cl349@5874 364 va_end(ap);
cl349@5874 365
cl349@5874 366 BUG_ON(len + ret > sizeof(printf_buffer)-1);
cl349@5874 367 dev->has_error = 1;
cl349@5874 368 if (xenbus_write(dev->nodename, "error", printf_buffer, O_CREAT) != 0)
cl349@5874 369 printk("xenbus: failed to write error node for %s (%s)\n",
cl349@5874 370 dev->nodename, printf_buffer);
cl349@5874 371 }
cl349@5874 372
cl349@5874 373 /* Clear any error. */
cl349@5874 374 void xenbus_dev_ok(struct xenbus_device *dev)
cl349@5598 375 {
cl349@5874 376 if (dev->has_error) {
cl349@5874 377 if (xenbus_rm(dev->nodename, "error") != 0)
cl349@5874 378 printk("xenbus: failed to clear error node for %s\n",
cl349@5874 379 dev->nodename);
cl349@5874 380 else
cl349@5874 381 dev->has_error = 0;
cl349@5874 382 }
cl349@5874 383 }
cl349@5874 384
cl349@5863 385 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
cl349@5863 386 int xenbus_gather(const char *dir, ...)
cl349@5863 387 {
cl349@5863 388 va_list ap;
cl349@5863 389 const char *name;
cl349@5863 390 int ret = 0;
cl349@5598 391
cl349@5863 392 va_start(ap, dir);
cl349@5863 393 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
cl349@5863 394 const char *fmt = va_arg(ap, char *);
cl349@5863 395 void *result = va_arg(ap, void *);
cl349@5863 396 char *p;
cl349@5863 397
cl349@5863 398 p = xenbus_read(dir, name, NULL);
cl349@5863 399 if (IS_ERR(p)) {
cl349@5863 400 ret = PTR_ERR(p);
cl349@5863 401 break;
cl349@5863 402 }
cl349@6250 403 if (fmt) {
cl349@6250 404 if (sscanf(p, fmt, result) == 0)
cl349@6250 405 ret = -EINVAL;
cl349@6250 406 kfree(p);
cl349@6250 407 } else
cl349@6250 408 *(char **)result = p;
cl349@5863 409 }
cl349@5863 410 va_end(ap);
cl349@5863 411 return ret;
cl349@5598 412 }
cl349@5598 413
cl349@5876 414 static int xs_watch(const char *path, const char *token)
cl349@5598 415 {
cl349@5876 416 struct kvec iov[2];
cl349@5598 417
cl349@5598 418 iov[0].iov_base = (void *)path;
cl349@5598 419 iov[0].iov_len = strlen(path) + 1;
cl349@5598 420 iov[1].iov_base = (void *)token;
cl349@5598 421 iov[1].iov_len = strlen(token) + 1;
cl349@5598 422
cl349@5598 423 return xs_error(xs_talkv(XS_WATCH, iov, ARRAY_SIZE(iov), NULL));
cl349@5598 424 }
cl349@5598 425
cl349@5598 426 static char *xs_read_watch(char **token)
cl349@5598 427 {
cl349@5598 428 enum xsd_sockmsg_type type;
cl349@5598 429 char *ret;
cl349@5598 430
cl349@5598 431 ret = read_reply(&type, NULL);
cl349@5598 432 if (IS_ERR(ret))
cl349@5598 433 return ret;
cl349@5598 434
cl349@5598 435 BUG_ON(type != XS_WATCH_EVENT);
cl349@5598 436 *token = ret + strlen(ret) + 1;
cl349@5598 437 return ret;
cl349@5598 438 }
cl349@5598 439
cl349@5598 440 static int xs_acknowledge_watch(const char *token)
cl349@5598 441 {
cl349@5598 442 return xs_error(xs_single(XS_WATCH_ACK, token, NULL));
cl349@5598 443 }
cl349@5598 444
cl349@5598 445 static int xs_unwatch(const char *path, const char *token)
cl349@5598 446 {
cl349@5598 447 struct kvec iov[2];
cl349@5598 448
cl349@5598 449 iov[0].iov_base = (char *)path;
cl349@5598 450 iov[0].iov_len = strlen(path) + 1;
cl349@5598 451 iov[1].iov_base = (char *)token;
cl349@5598 452 iov[1].iov_len = strlen(token) + 1;
cl349@5598 453
cl349@5598 454 return xs_error(xs_talkv(XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL));
cl349@5598 455 }
cl349@5598 456
cl349@5598 457 /* A little paranoia: we don't just trust token. */
cl349@5598 458 static struct xenbus_watch *find_watch(const char *token)
cl349@5598 459 {
cl349@5598 460 struct xenbus_watch *i, *cmp;
cl349@5598 461
cl349@5598 462 cmp = (void *)simple_strtoul(token, NULL, 16);
cl349@5598 463
cl349@5598 464 list_for_each_entry(i, &watches, list)
cl349@5598 465 if (i == cmp)
cl349@5598 466 return i;
cl349@5598 467 return NULL;
cl349@5598 468 }
cl349@5598 469
cl349@5598 470 /* Register callback to watch this node. */
cl349@5598 471 int register_xenbus_watch(struct xenbus_watch *watch)
cl349@5598 472 {
cl349@5598 473 /* Pointer in ascii is the token. */
cl349@5598 474 char token[sizeof(watch) * 2 + 1];
cl349@5598 475 int err;
cl349@5598 476
cl349@5598 477 sprintf(token, "%lX", (long)watch);
cl349@5598 478 BUG_ON(find_watch(token));
cl349@5598 479
cl349@5876 480 err = xs_watch(watch->node, token);
cl349@5598 481 if (!err)
cl349@5598 482 list_add(&watch->list, &watches);
cl349@5598 483 return err;
cl349@5598 484 }
cl349@5598 485
cl349@5598 486 void unregister_xenbus_watch(struct xenbus_watch *watch)
cl349@5598 487 {
cl349@5598 488 char token[sizeof(watch) * 2 + 1];
cl349@5598 489 int err;
cl349@5598 490
cl349@5598 491 sprintf(token, "%lX", (long)watch);
cl349@5598 492 BUG_ON(!find_watch(token));
cl349@5598 493
cl349@5598 494 err = xs_unwatch(watch->node, token);
cl349@5598 495 list_del(&watch->list);
cl349@5598 496
cl349@5598 497 if (err)
cl349@5903 498 printk(KERN_WARNING
cl349@5903 499 "XENBUS Failed to release watch %s: %i\n",
cl349@5598 500 watch->node, err);
cl349@5598 501 }
cl349@5598 502
cl349@6209 503 /* Re-register callbacks to all watches. */
cl349@6209 504 void reregister_xenbus_watches(void)
cl349@6209 505 {
cl349@6209 506 struct xenbus_watch *watch;
cl349@6209 507 char token[sizeof(watch) * 2 + 1];
cl349@6209 508
cl349@6209 509 list_for_each_entry(watch, &watches, list) {
cl349@6209 510 sprintf(token, "%lX", (long)watch);
cl349@6209 511 xs_watch(watch->node, token);
cl349@6209 512 }
cl349@6209 513 }
cl349@6209 514
cl349@5598 515 static int watch_thread(void *unused)
cl349@5598 516 {
cl349@5598 517 for (;;) {
cl349@5598 518 char *token;
cl349@5598 519 char *node = NULL;
cl349@5598 520
cl349@5902 521 wait_event(xb_waitq, xs_input_avail());
cl349@5598 522
cl349@5598 523 /* If this is a spurious wakeup caused by someone
cl349@5598 524 * doing an op, they'll hold the lock and the buffer
cl349@5598 525 * will be empty by the time we get there.
cl349@5598 526 */
cl349@5863 527 down(&xenbus_lock);
cl349@5902 528 if (xs_input_avail())
cl349@5598 529 node = xs_read_watch(&token);
cl349@5747 530
cl349@5598 531 if (node && !IS_ERR(node)) {
cl349@5598 532 struct xenbus_watch *w;
cl349@5598 533 int err;
cl349@5598 534
cl349@5903 535 err = xs_acknowledge_watch(token);
cl349@5903 536 if (err)
cl349@5903 537 printk(KERN_WARNING "XENBUS ack %s fail %i\n",
cl349@5903 538 node, err);
cl349@5598 539 w = find_watch(token);
cl349@5598 540 BUG_ON(!w);
cl349@5598 541 w->callback(w, node);
cl349@5598 542 kfree(node);
cl349@5598 543 } else
cl349@5598 544 printk(KERN_WARNING "XENBUS xs_read_watch: %li\n",
cl349@5598 545 PTR_ERR(node));
cl349@5863 546 up(&xenbus_lock);
cl349@5598 547 }
cl349@5598 548 }
cl349@5598 549
cl349@5598 550 int xs_init(void)
cl349@5598 551 {
cl349@5598 552 int err;
cl349@5598 553 struct task_struct *watcher;
cl349@5598 554
cl349@5902 555 err = xb_init_comms();
cl349@5598 556 if (err)
cl349@5598 557 return err;
cl349@5598 558
cl349@5598 559 watcher = kthread_run(watch_thread, NULL, "kxbwatch");
cl349@5598 560 if (IS_ERR(watcher))
cl349@5598 561 return PTR_ERR(watcher);
cl349@5598 562 return 0;
cl349@5598 563 }