1/****************************************************************************** 2 * xenbus_xs.c 3 * 4 * This is the kernel equivalent of the "xs" library. We don't need everything 5 * and we use xenbus_comms for communication. 6 * 7 * Copyright (C) 2005 Rusty Russell, IBM Corporation 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License version 2 11 * as published by the Free Software Foundation; or, when distributed 12 * separately from the Linux kernel or incorporated into other 13 * software packages, subject to the following license: 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a copy 16 * of this source file (the "Software"), to deal in the Software without 17 * restriction, including without limitation the rights to use, copy, modify, 18 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 19 * and to permit persons to whom the Software is furnished to do so, subject to 20 * the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be included in 23 * all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 31 * IN THE SOFTWARE. 32 */ 33 34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 35 36#include <linux/unistd.h> 37#include <linux/errno.h> 38#include <linux/types.h> 39#include <linux/uio.h> 40#include <linux/kernel.h> 41#include <linux/string.h> 42#include <linux/err.h> 43#include <linux/slab.h> 44#include <linux/fcntl.h> 45#include <linux/kthread.h> 46#include <linux/rwsem.h> 47#include <linux/module.h> 48#include <linux/mutex.h> 49#include <asm/xen/hypervisor.h> 50#include <xen/xenbus.h> 51#include <xen/xen.h> 52#include "xenbus_comms.h" 53#include "xenbus_probe.h" 54 55struct xs_stored_msg { 56 struct list_head list; 57 58 struct xsd_sockmsg hdr; 59 60 union { 61 /* Queued replies. */ 62 struct { 63 char *body; 64 } reply; 65 66 /* Queued watch events. */ 67 struct { 68 struct xenbus_watch *handle; 69 char **vec; 70 unsigned int vec_size; 71 } watch; 72 } u; 73}; 74 75struct xs_handle { 76 /* A list of replies. Currently only one will ever be outstanding. */ 77 struct list_head reply_list; 78 spinlock_t reply_lock; 79 wait_queue_head_t reply_waitq; 80 81 /* 82 * Mutex ordering: transaction_mutex -> watch_mutex -> request_mutex. 83 * response_mutex is never taken simultaneously with the other three. 84 * 85 * transaction_mutex must be held before incrementing 86 * transaction_count. The mutex is held when a suspend is in 87 * progress to prevent new transactions starting. 88 * 89 * When decrementing transaction_count to zero the wait queue 90 * should be woken up, the suspend code waits for count to 91 * reach zero. 92 */ 93 94 /* One request at a time. */ 95 struct mutex request_mutex; 96 97 /* Protect xenbus reader thread against save/restore. */ 98 struct mutex response_mutex; 99 100 /* Protect transactions against save/restore. */ 101 struct mutex transaction_mutex; 102 atomic_t transaction_count; 103 wait_queue_head_t transaction_wq; 104 105 /* Protect watch (de)register against save/restore. */ 106 struct rw_semaphore watch_mutex; 107}; 108 109static struct xs_handle xs_state; 110 111/* List of registered watches, and a lock to protect it. */ 112static LIST_HEAD(watches); 113static DEFINE_SPINLOCK(watches_lock); 114 115/* List of pending watch callback events, and a lock to protect it. */ 116static LIST_HEAD(watch_events); 117static DEFINE_SPINLOCK(watch_events_lock); 118 119/* 120 * Details of the xenwatch callback kernel thread. The thread waits on the 121 * watch_events_waitq for work to do (queued on watch_events list). When it 122 * wakes up it acquires the xenwatch_mutex before reading the list and 123 * carrying out work. 124 */ 125static pid_t xenwatch_pid; 126static DEFINE_MUTEX(xenwatch_mutex); 127static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq); 128 129static int get_error(const char *errorstring) 130{ 131 unsigned int i; 132 133 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) { 134 if (i == ARRAY_SIZE(xsd_errors) - 1) { 135 pr_warn("xen store gave: unknown error %s\n", 136 errorstring); 137 return EINVAL; 138 } 139 } 140 return xsd_errors[i].errnum; 141} 142 143static bool xenbus_ok(void) 144{ 145 switch (xen_store_domain_type) { 146 case XS_LOCAL: 147 switch (system_state) { 148 case SYSTEM_POWER_OFF: 149 case SYSTEM_RESTART: 150 case SYSTEM_HALT: 151 return false; 152 default: 153 break; 154 } 155 return true; 156 case XS_PV: 157 case XS_HVM: 158 /* FIXME: Could check that the remote domain is alive, 159 * but it is normally initial domain. */ 160 return true; 161 default: 162 break; 163 } 164 return false; 165} 166static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) 167{ 168 struct xs_stored_msg *msg; 169 char *body; 170 171 spin_lock(&xs_state.reply_lock); 172 173 while (list_empty(&xs_state.reply_list)) { 174 spin_unlock(&xs_state.reply_lock); 175 if (xenbus_ok()) 176 /* XXX FIXME: Avoid synchronous wait for response here. */ 177 wait_event_timeout(xs_state.reply_waitq, 178 !list_empty(&xs_state.reply_list), 179 msecs_to_jiffies(500)); 180 else { 181 /* 182 * If we are in the process of being shut-down there is 183 * no point of trying to contact XenBus - it is either 184 * killed (xenstored application) or the other domain 185 * has been killed or is unreachable. 186 */ 187 return ERR_PTR(-EIO); 188 } 189 spin_lock(&xs_state.reply_lock); 190 } 191 192 msg = list_entry(xs_state.reply_list.next, 193 struct xs_stored_msg, list); 194 list_del(&msg->list); 195 196 spin_unlock(&xs_state.reply_lock); 197 198 *type = msg->hdr.type; 199 if (len) 200 *len = msg->hdr.len; 201 body = msg->u.reply.body; 202 203 kfree(msg); 204 205 return body; 206} 207 208static void transaction_start(void) 209{ 210 mutex_lock(&xs_state.transaction_mutex); 211 atomic_inc(&xs_state.transaction_count); 212 mutex_unlock(&xs_state.transaction_mutex); 213} 214 215static void transaction_end(void) 216{ 217 if (atomic_dec_and_test(&xs_state.transaction_count)) 218 wake_up(&xs_state.transaction_wq); 219} 220 221static void transaction_suspend(void) 222{ 223 mutex_lock(&xs_state.transaction_mutex); 224 wait_event(xs_state.transaction_wq, 225 atomic_read(&xs_state.transaction_count) == 0); 226} 227 228static void transaction_resume(void) 229{ 230 mutex_unlock(&xs_state.transaction_mutex); 231} 232 233void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg) 234{ 235 void *ret; 236 struct xsd_sockmsg req_msg = *msg; 237 int err; 238 239 if (req_msg.type == XS_TRANSACTION_START) 240 transaction_start(); 241 242 mutex_lock(&xs_state.request_mutex); 243 244 err = xb_write(msg, sizeof(*msg) + msg->len); 245 if (err) { 246 msg->type = XS_ERROR; 247 ret = ERR_PTR(err); 248 } else 249 ret = read_reply(&msg->type, &msg->len); 250 251 mutex_unlock(&xs_state.request_mutex); 252 253 if (IS_ERR(ret)) 254 return ret; 255 256 if ((msg->type == XS_TRANSACTION_END) || 257 ((req_msg.type == XS_TRANSACTION_START) && 258 (msg->type == XS_ERROR))) 259 transaction_end(); 260 261 return ret; 262} 263EXPORT_SYMBOL(xenbus_dev_request_and_reply); 264 265/* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */ 266static void *xs_talkv(struct xenbus_transaction t, 267 enum xsd_sockmsg_type type, 268 const struct kvec *iovec, 269 unsigned int num_vecs, 270 unsigned int *len) 271{ 272 struct xsd_sockmsg msg; 273 void *ret = NULL; 274 unsigned int i; 275 int err; 276 277 msg.tx_id = t.id; 278 msg.req_id = 0; 279 msg.type = type; 280 msg.len = 0; 281 for (i = 0; i < num_vecs; i++) 282 msg.len += iovec[i].iov_len; 283 284 mutex_lock(&xs_state.request_mutex); 285 286 err = xb_write(&msg, sizeof(msg)); 287 if (err) { 288 mutex_unlock(&xs_state.request_mutex); 289 return ERR_PTR(err); 290 } 291 292 for (i = 0; i < num_vecs; i++) { 293 err = xb_write(iovec[i].iov_base, iovec[i].iov_len); 294 if (err) { 295 mutex_unlock(&xs_state.request_mutex); 296 return ERR_PTR(err); 297 } 298 } 299 300 ret = read_reply(&msg.type, len); 301 302 mutex_unlock(&xs_state.request_mutex); 303 304 if (IS_ERR(ret)) 305 return ret; 306 307 if (msg.type == XS_ERROR) { 308 err = get_error(ret); 309 kfree(ret); 310 return ERR_PTR(-err); 311 } 312 313 if (msg.type != type) { 314 pr_warn_ratelimited("unexpected type [%d], expected [%d]\n", 315 msg.type, type); 316 kfree(ret); 317 return ERR_PTR(-EINVAL); 318 } 319 return ret; 320} 321 322/* Simplified version of xs_talkv: single message. */ 323static void *xs_single(struct xenbus_transaction t, 324 enum xsd_sockmsg_type type, 325 const char *string, 326 unsigned int *len) 327{ 328 struct kvec iovec; 329 330 iovec.iov_base = (void *)string; 331 iovec.iov_len = strlen(string) + 1; 332 return xs_talkv(t, type, &iovec, 1, len); 333} 334 335/* Many commands only need an ack, don't care what it says. */ 336static int xs_error(char *reply) 337{ 338 if (IS_ERR(reply)) 339 return PTR_ERR(reply); 340 kfree(reply); 341 return 0; 342} 343 344static unsigned int count_strings(const char *strings, unsigned int len) 345{ 346 unsigned int num; 347 const char *p; 348 349 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) 350 num++; 351 352 return num; 353} 354 355/* Return the path to dir with /name appended. Buffer must be kfree()'ed. */ 356static char *join(const char *dir, const char *name) 357{ 358 char *buffer; 359 360 if (strlen(name) == 0) 361 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir); 362 else 363 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name); 364 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer; 365} 366 367static char **split(char *strings, unsigned int len, unsigned int *num) 368{ 369 char *p, **ret; 370 371 /* Count the strings. */ 372 *num = count_strings(strings, len); 373 374 /* Transfer to one big alloc for easy freeing. */ 375 ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH); 376 if (!ret) { 377 kfree(strings); 378 return ERR_PTR(-ENOMEM); 379 } 380 memcpy(&ret[*num], strings, len); 381 kfree(strings); 382 383 strings = (char *)&ret[*num]; 384 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1) 385 ret[(*num)++] = p; 386 387 return ret; 388} 389 390char **xenbus_directory(struct xenbus_transaction t, 391 const char *dir, const char *node, unsigned int *num) 392{ 393 char *strings, *path; 394 unsigned int len; 395 396 path = join(dir, node); 397 if (IS_ERR(path)) 398 return (char **)path; 399 400 strings = xs_single(t, XS_DIRECTORY, path, &len); 401 kfree(path); 402 if (IS_ERR(strings)) 403 return (char **)strings; 404 405 return split(strings, len, num); 406} 407EXPORT_SYMBOL_GPL(xenbus_directory); 408 409/* Check if a path exists. Return 1 if it does. */ 410int xenbus_exists(struct xenbus_transaction t, 411 const char *dir, const char *node) 412{ 413 char **d; 414 int dir_n; 415 416 d = xenbus_directory(t, dir, node, &dir_n); 417 if (IS_ERR(d)) 418 return 0; 419 kfree(d); 420 return 1; 421} 422EXPORT_SYMBOL_GPL(xenbus_exists); 423 424/* Get the value of a single file. 425 * Returns a kmalloced value: call free() on it after use. 426 * len indicates length in bytes. 427 */ 428void *xenbus_read(struct xenbus_transaction t, 429 const char *dir, const char *node, unsigned int *len) 430{ 431 char *path; 432 void *ret; 433 434 path = join(dir, node); 435 if (IS_ERR(path)) 436 return (void *)path; 437 438 ret = xs_single(t, XS_READ, path, len); 439 kfree(path); 440 return ret; 441} 442EXPORT_SYMBOL_GPL(xenbus_read); 443 444/* Write the value of a single file. 445 * Returns -err on failure. 446 */ 447int xenbus_write(struct xenbus_transaction t, 448 const char *dir, const char *node, const char *string) 449{ 450 const char *path; 451 struct kvec iovec[2]; 452 int ret; 453 454 path = join(dir, node); 455 if (IS_ERR(path)) 456 return PTR_ERR(path); 457 458 iovec[0].iov_base = (void *)path; 459 iovec[0].iov_len = strlen(path) + 1; 460 iovec[1].iov_base = (void *)string; 461 iovec[1].iov_len = strlen(string); 462 463 ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL)); 464 kfree(path); 465 return ret; 466} 467EXPORT_SYMBOL_GPL(xenbus_write); 468 469/* Create a new directory. */ 470int xenbus_mkdir(struct xenbus_transaction t, 471 const char *dir, const char *node) 472{ 473 char *path; 474 int ret; 475 476 path = join(dir, node); 477 if (IS_ERR(path)) 478 return PTR_ERR(path); 479 480 ret = xs_error(xs_single(t, XS_MKDIR, path, NULL)); 481 kfree(path); 482 return ret; 483} 484EXPORT_SYMBOL_GPL(xenbus_mkdir); 485 486/* Destroy a file or directory (directories must be empty). */ 487int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node) 488{ 489 char *path; 490 int ret; 491 492 path = join(dir, node); 493 if (IS_ERR(path)) 494 return PTR_ERR(path); 495 496 ret = xs_error(xs_single(t, XS_RM, path, NULL)); 497 kfree(path); 498 return ret; 499} 500EXPORT_SYMBOL_GPL(xenbus_rm); 501 502/* Start a transaction: changes by others will not be seen during this 503 * transaction, and changes will not be visible to others until end. 504 */ 505int xenbus_transaction_start(struct xenbus_transaction *t) 506{ 507 char *id_str; 508 509 transaction_start(); 510 511 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL); 512 if (IS_ERR(id_str)) { 513 transaction_end(); 514 return PTR_ERR(id_str); 515 } 516 517 t->id = simple_strtoul(id_str, NULL, 0); 518 kfree(id_str); 519 return 0; 520} 521EXPORT_SYMBOL_GPL(xenbus_transaction_start); 522 523/* End a transaction. 524 * If abandon is true, transaction is discarded instead of committed. 525 */ 526int xenbus_transaction_end(struct xenbus_transaction t, int abort) 527{ 528 char abortstr[2]; 529 int err; 530 531 if (abort) 532 strcpy(abortstr, "F"); 533 else 534 strcpy(abortstr, "T"); 535 536 err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL)); 537 538 transaction_end(); 539 540 return err; 541} 542EXPORT_SYMBOL_GPL(xenbus_transaction_end); 543 544/* Single read and scanf: returns -errno or num scanned. */ 545int xenbus_scanf(struct xenbus_transaction t, 546 const char *dir, const char *node, const char *fmt, ...) 547{ 548 va_list ap; 549 int ret; 550 char *val; 551 552 val = xenbus_read(t, dir, node, NULL); 553 if (IS_ERR(val)) 554 return PTR_ERR(val); 555 556 va_start(ap, fmt); 557 ret = vsscanf(val, fmt, ap); 558 va_end(ap); 559 kfree(val); 560 /* Distinctive errno. */ 561 if (ret == 0) 562 return -ERANGE; 563 return ret; 564} 565EXPORT_SYMBOL_GPL(xenbus_scanf); 566 567/* Single printf and write: returns -errno or 0. */ 568int xenbus_printf(struct xenbus_transaction t, 569 const char *dir, const char *node, const char *fmt, ...) 570{ 571 va_list ap; 572 int ret; 573 char *buf; 574 575 va_start(ap, fmt); 576 buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap); 577 va_end(ap); 578 579 if (!buf) 580 return -ENOMEM; 581 582 ret = xenbus_write(t, dir, node, buf); 583 584 kfree(buf); 585 586 return ret; 587} 588EXPORT_SYMBOL_GPL(xenbus_printf); 589 590/* Takes tuples of names, scanf-style args, and void **, NULL terminated. */ 591int xenbus_gather(struct xenbus_transaction t, const char *dir, ...) 592{ 593 va_list ap; 594 const char *name; 595 int ret = 0; 596 597 va_start(ap, dir); 598 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) { 599 const char *fmt = va_arg(ap, char *); 600 void *result = va_arg(ap, void *); 601 char *p; 602 603 p = xenbus_read(t, dir, name, NULL); 604 if (IS_ERR(p)) { 605 ret = PTR_ERR(p); 606 break; 607 } 608 if (fmt) { 609 if (sscanf(p, fmt, result) == 0) 610 ret = -EINVAL; 611 kfree(p); 612 } else 613 *(char **)result = p; 614 } 615 va_end(ap); 616 return ret; 617} 618EXPORT_SYMBOL_GPL(xenbus_gather); 619 620static int xs_watch(const char *path, const char *token) 621{ 622 struct kvec iov[2]; 623 624 iov[0].iov_base = (void *)path; 625 iov[0].iov_len = strlen(path) + 1; 626 iov[1].iov_base = (void *)token; 627 iov[1].iov_len = strlen(token) + 1; 628 629 return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov, 630 ARRAY_SIZE(iov), NULL)); 631} 632 633static int xs_unwatch(const char *path, const char *token) 634{ 635 struct kvec iov[2]; 636 637 iov[0].iov_base = (char *)path; 638 iov[0].iov_len = strlen(path) + 1; 639 iov[1].iov_base = (char *)token; 640 iov[1].iov_len = strlen(token) + 1; 641 642 return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov, 643 ARRAY_SIZE(iov), NULL)); 644} 645 646static struct xenbus_watch *find_watch(const char *token) 647{ 648 struct xenbus_watch *i, *cmp; 649 650 cmp = (void *)simple_strtoul(token, NULL, 16); 651 652 list_for_each_entry(i, &watches, list) 653 if (i == cmp) 654 return i; 655 656 return NULL; 657} 658/* 659 * Certain older XenBus toolstack cannot handle reading values that are 660 * not populated. Some Xen 3.4 installation are incapable of doing this 661 * so if we are running on anything older than 4 do not attempt to read 662 * control/platform-feature-xs_reset_watches. 663 */ 664static bool xen_strict_xenbus_quirk(void) 665{ 666#ifdef CONFIG_X86 667 uint32_t eax, ebx, ecx, edx, base; 668 669 base = xen_cpuid_base(); 670 cpuid(base + 1, &eax, &ebx, &ecx, &edx); 671 672 if ((eax >> 16) < 4) 673 return true; 674#endif 675 return false; 676 677} 678static void xs_reset_watches(void) 679{ 680 int err, supported = 0; 681 682 if (!xen_hvm_domain() || xen_initial_domain()) 683 return; 684 685 if (xen_strict_xenbus_quirk()) 686 return; 687 688 err = xenbus_scanf(XBT_NIL, "control", 689 "platform-feature-xs_reset_watches", "%d", &supported); 690 if (err != 1 || !supported) 691 return; 692 693 err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL)); 694 if (err && err != -EEXIST) 695 pr_warn("xs_reset_watches failed: %d\n", err); 696} 697 698/* Register callback to watch this node. */ 699int register_xenbus_watch(struct xenbus_watch *watch) 700{ 701 /* Pointer in ascii is the token. */ 702 char token[sizeof(watch) * 2 + 1]; 703 int err; 704 705 sprintf(token, "%lX", (long)watch); 706 707 down_read(&xs_state.watch_mutex); 708 709 spin_lock(&watches_lock); 710 BUG_ON(find_watch(token)); 711 list_add(&watch->list, &watches); 712 spin_unlock(&watches_lock); 713 714 err = xs_watch(watch->node, token); 715 716 if (err) { 717 spin_lock(&watches_lock); 718 list_del(&watch->list); 719 spin_unlock(&watches_lock); 720 } 721 722 up_read(&xs_state.watch_mutex); 723 724 return err; 725} 726EXPORT_SYMBOL_GPL(register_xenbus_watch); 727 728void unregister_xenbus_watch(struct xenbus_watch *watch) 729{ 730 struct xs_stored_msg *msg, *tmp; 731 char token[sizeof(watch) * 2 + 1]; 732 int err; 733 734 sprintf(token, "%lX", (long)watch); 735 736 down_read(&xs_state.watch_mutex); 737 738 spin_lock(&watches_lock); 739 BUG_ON(!find_watch(token)); 740 list_del(&watch->list); 741 spin_unlock(&watches_lock); 742 743 err = xs_unwatch(watch->node, token); 744 if (err) 745 pr_warn("Failed to release watch %s: %i\n", watch->node, err); 746 747 up_read(&xs_state.watch_mutex); 748 749 /* Make sure there are no callbacks running currently (unless 750 its us) */ 751 if (current->pid != xenwatch_pid) 752 mutex_lock(&xenwatch_mutex); 753 754 /* Cancel pending watch events. */ 755 spin_lock(&watch_events_lock); 756 list_for_each_entry_safe(msg, tmp, &watch_events, list) { 757 if (msg->u.watch.handle != watch) 758 continue; 759 list_del(&msg->list); 760 kfree(msg->u.watch.vec); 761 kfree(msg); 762 } 763 spin_unlock(&watch_events_lock); 764 765 if (current->pid != xenwatch_pid) 766 mutex_unlock(&xenwatch_mutex); 767} 768EXPORT_SYMBOL_GPL(unregister_xenbus_watch); 769 770void xs_suspend(void) 771{ 772 transaction_suspend(); 773 down_write(&xs_state.watch_mutex); 774 mutex_lock(&xs_state.request_mutex); 775 mutex_lock(&xs_state.response_mutex); 776} 777 778void xs_resume(void) 779{ 780 struct xenbus_watch *watch; 781 char token[sizeof(watch) * 2 + 1]; 782 783 xb_init_comms(); 784 785 mutex_unlock(&xs_state.response_mutex); 786 mutex_unlock(&xs_state.request_mutex); 787 transaction_resume(); 788 789 /* No need for watches_lock: the watch_mutex is sufficient. */ 790 list_for_each_entry(watch, &watches, list) { 791 sprintf(token, "%lX", (long)watch); 792 xs_watch(watch->node, token); 793 } 794 795 up_write(&xs_state.watch_mutex); 796} 797 798void xs_suspend_cancel(void) 799{ 800 mutex_unlock(&xs_state.response_mutex); 801 mutex_unlock(&xs_state.request_mutex); 802 up_write(&xs_state.watch_mutex); 803 mutex_unlock(&xs_state.transaction_mutex); 804} 805 806static int xenwatch_thread(void *unused) 807{ 808 struct list_head *ent; 809 struct xs_stored_msg *msg; 810 811 for (;;) { 812 wait_event_interruptible(watch_events_waitq, 813 !list_empty(&watch_events)); 814 815 if (kthread_should_stop()) 816 break; 817 818 mutex_lock(&xenwatch_mutex); 819 820 spin_lock(&watch_events_lock); 821 ent = watch_events.next; 822 if (ent != &watch_events) 823 list_del(ent); 824 spin_unlock(&watch_events_lock); 825 826 if (ent != &watch_events) { 827 msg = list_entry(ent, struct xs_stored_msg, list); 828 msg->u.watch.handle->callback( 829 msg->u.watch.handle, 830 (const char **)msg->u.watch.vec, 831 msg->u.watch.vec_size); 832 kfree(msg->u.watch.vec); 833 kfree(msg); 834 } 835 836 mutex_unlock(&xenwatch_mutex); 837 } 838 839 return 0; 840} 841 842static int process_msg(void) 843{ 844 struct xs_stored_msg *msg; 845 char *body; 846 int err; 847 848 /* 849 * We must disallow save/restore while reading a xenstore message. 850 * A partial read across s/r leaves us out of sync with xenstored. 851 */ 852 for (;;) { 853 err = xb_wait_for_data_to_read(); 854 if (err) 855 return err; 856 mutex_lock(&xs_state.response_mutex); 857 if (xb_data_to_read()) 858 break; 859 /* We raced with save/restore: pending data 'disappeared'. */ 860 mutex_unlock(&xs_state.response_mutex); 861 } 862 863 864 msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH); 865 if (msg == NULL) { 866 err = -ENOMEM; 867 goto out; 868 } 869 870 err = xb_read(&msg->hdr, sizeof(msg->hdr)); 871 if (err) { 872 kfree(msg); 873 goto out; 874 } 875 876 if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) { 877 kfree(msg); 878 err = -EINVAL; 879 goto out; 880 } 881 882 body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH); 883 if (body == NULL) { 884 kfree(msg); 885 err = -ENOMEM; 886 goto out; 887 } 888 889 err = xb_read(body, msg->hdr.len); 890 if (err) { 891 kfree(body); 892 kfree(msg); 893 goto out; 894 } 895 body[msg->hdr.len] = '\0'; 896 897 if (msg->hdr.type == XS_WATCH_EVENT) { 898 msg->u.watch.vec = split(body, msg->hdr.len, 899 &msg->u.watch.vec_size); 900 if (IS_ERR(msg->u.watch.vec)) { 901 err = PTR_ERR(msg->u.watch.vec); 902 kfree(msg); 903 goto out; 904 } 905 906 spin_lock(&watches_lock); 907 msg->u.watch.handle = find_watch( 908 msg->u.watch.vec[XS_WATCH_TOKEN]); 909 if (msg->u.watch.handle != NULL) { 910 spin_lock(&watch_events_lock); 911 list_add_tail(&msg->list, &watch_events); 912 wake_up(&watch_events_waitq); 913 spin_unlock(&watch_events_lock); 914 } else { 915 kfree(msg->u.watch.vec); 916 kfree(msg); 917 } 918 spin_unlock(&watches_lock); 919 } else { 920 msg->u.reply.body = body; 921 spin_lock(&xs_state.reply_lock); 922 list_add_tail(&msg->list, &xs_state.reply_list); 923 spin_unlock(&xs_state.reply_lock); 924 wake_up(&xs_state.reply_waitq); 925 } 926 927 out: 928 mutex_unlock(&xs_state.response_mutex); 929 return err; 930} 931 932static int xenbus_thread(void *unused) 933{ 934 int err; 935 936 for (;;) { 937 err = process_msg(); 938 if (err) 939 pr_warn("error %d while reading message\n", err); 940 if (kthread_should_stop()) 941 break; 942 } 943 944 return 0; 945} 946 947int xs_init(void) 948{ 949 int err; 950 struct task_struct *task; 951 952 INIT_LIST_HEAD(&xs_state.reply_list); 953 spin_lock_init(&xs_state.reply_lock); 954 init_waitqueue_head(&xs_state.reply_waitq); 955 956 mutex_init(&xs_state.request_mutex); 957 mutex_init(&xs_state.response_mutex); 958 mutex_init(&xs_state.transaction_mutex); 959 init_rwsem(&xs_state.watch_mutex); 960 atomic_set(&xs_state.transaction_count, 0); 961 init_waitqueue_head(&xs_state.transaction_wq); 962 963 /* Initialize the shared memory rings to talk to xenstored */ 964 err = xb_init_comms(); 965 if (err) 966 return err; 967 968 task = kthread_run(xenwatch_thread, NULL, "xenwatch"); 969 if (IS_ERR(task)) 970 return PTR_ERR(task); 971 xenwatch_pid = task->pid; 972 973 task = kthread_run(xenbus_thread, NULL, "xenbus"); 974 if (IS_ERR(task)) 975 return PTR_ERR(task); 976 977 /* shutdown watches for kexec boot */ 978 xs_reset_watches(); 979 980 return 0; 981} 982