Back to home page

OSCL-LXR

 
 

    


0001 /******************************************************************************
0002  * xenbus_xs.c
0003  *
0004  * This is the kernel equivalent of the "xs" library.  We don't need everything
0005  * and we use xenbus_comms for communication.
0006  *
0007  * Copyright (C) 2005 Rusty Russell, IBM Corporation
0008  *
0009  * This program is free software; you can redistribute it and/or
0010  * modify it under the terms of the GNU General Public License version 2
0011  * as published by the Free Software Foundation; or, when distributed
0012  * separately from the Linux kernel or incorporated into other
0013  * software packages, subject to the following license:
0014  *
0015  * Permission is hereby granted, free of charge, to any person obtaining a copy
0016  * of this source file (the "Software"), to deal in the Software without
0017  * restriction, including without limitation the rights to use, copy, modify,
0018  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
0019  * and to permit persons to whom the Software is furnished to do so, subject to
0020  * the following conditions:
0021  *
0022  * The above copyright notice and this permission notice shall be included in
0023  * all copies or substantial portions of the Software.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0026  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0027  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0028  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0029  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
0031  * IN THE SOFTWARE.
0032  */
0033 
0034 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0035 
0036 #include <linux/unistd.h>
0037 #include <linux/errno.h>
0038 #include <linux/types.h>
0039 #include <linux/uio.h>
0040 #include <linux/kernel.h>
0041 #include <linux/string.h>
0042 #include <linux/err.h>
0043 #include <linux/slab.h>
0044 #include <linux/fcntl.h>
0045 #include <linux/kthread.h>
0046 #include <linux/reboot.h>
0047 #include <linux/rwsem.h>
0048 #include <linux/mutex.h>
0049 #include <asm/xen/hypervisor.h>
0050 #include <xen/xenbus.h>
0051 #include <xen/xen.h>
0052 #include "xenbus.h"
0053 
0054 /*
0055  * Framework to protect suspend/resume handling against normal Xenstore
0056  * message handling:
0057  * During suspend/resume there must be no open transaction and no pending
0058  * Xenstore request.
0059  * New watch events happening in this time can be ignored by firing all watches
0060  * after resume.
0061  */
0062 
0063 /* Lock protecting enter/exit critical region. */
0064 static DEFINE_SPINLOCK(xs_state_lock);
0065 /* Number of users in critical region (protected by xs_state_lock). */
0066 static unsigned int xs_state_users;
0067 /* Suspend handler waiting or already active (protected by xs_state_lock)? */
0068 static int xs_suspend_active;
0069 /* Unique Xenstore request id (protected by xs_state_lock). */
0070 static uint32_t xs_request_id;
0071 
0072 /* Wait queue for all callers waiting for critical region to become usable. */
0073 static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
0074 /* Wait queue for suspend handling waiting for critical region being empty. */
0075 static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
0076 
0077 /* List of registered watches, and a lock to protect it. */
0078 static LIST_HEAD(watches);
0079 static DEFINE_SPINLOCK(watches_lock);
0080 
0081 /* List of pending watch callback events, and a lock to protect it. */
0082 static LIST_HEAD(watch_events);
0083 static DEFINE_SPINLOCK(watch_events_lock);
0084 
0085 /* Protect watch (de)register against save/restore. */
0086 static DECLARE_RWSEM(xs_watch_rwsem);
0087 
0088 /*
0089  * Details of the xenwatch callback kernel thread. The thread waits on the
0090  * watch_events_waitq for work to do (queued on watch_events list). When it
0091  * wakes up it acquires the xenwatch_mutex before reading the list and
0092  * carrying out work.
0093  */
0094 static pid_t xenwatch_pid;
0095 static DEFINE_MUTEX(xenwatch_mutex);
0096 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
0097 
0098 static void xs_suspend_enter(void)
0099 {
0100     spin_lock(&xs_state_lock);
0101     xs_suspend_active++;
0102     spin_unlock(&xs_state_lock);
0103     wait_event(xs_state_exit_wq, xs_state_users == 0);
0104 }
0105 
0106 static void xs_suspend_exit(void)
0107 {
0108     xb_dev_generation_id++;
0109     spin_lock(&xs_state_lock);
0110     xs_suspend_active--;
0111     spin_unlock(&xs_state_lock);
0112     wake_up_all(&xs_state_enter_wq);
0113 }
0114 
0115 static uint32_t xs_request_enter(struct xb_req_data *req)
0116 {
0117     uint32_t rq_id;
0118 
0119     req->type = req->msg.type;
0120 
0121     spin_lock(&xs_state_lock);
0122 
0123     while (!xs_state_users && xs_suspend_active) {
0124         spin_unlock(&xs_state_lock);
0125         wait_event(xs_state_enter_wq, xs_suspend_active == 0);
0126         spin_lock(&xs_state_lock);
0127     }
0128 
0129     if (req->type == XS_TRANSACTION_START && !req->user_req)
0130         xs_state_users++;
0131     xs_state_users++;
0132     rq_id = xs_request_id++;
0133 
0134     spin_unlock(&xs_state_lock);
0135 
0136     return rq_id;
0137 }
0138 
0139 void xs_request_exit(struct xb_req_data *req)
0140 {
0141     spin_lock(&xs_state_lock);
0142     xs_state_users--;
0143     if ((req->type == XS_TRANSACTION_START && req->msg.type == XS_ERROR) ||
0144         (req->type == XS_TRANSACTION_END && !req->user_req &&
0145          !WARN_ON_ONCE(req->msg.type == XS_ERROR &&
0146                !strcmp(req->body, "ENOENT"))))
0147         xs_state_users--;
0148     spin_unlock(&xs_state_lock);
0149 
0150     if (xs_suspend_active && !xs_state_users)
0151         wake_up(&xs_state_exit_wq);
0152 }
0153 
0154 static int get_error(const char *errorstring)
0155 {
0156     unsigned int i;
0157 
0158     for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
0159         if (i == ARRAY_SIZE(xsd_errors) - 1) {
0160             pr_warn("xen store gave: unknown error %s\n",
0161                 errorstring);
0162             return EINVAL;
0163         }
0164     }
0165     return xsd_errors[i].errnum;
0166 }
0167 
0168 static bool xenbus_ok(void)
0169 {
0170     switch (xen_store_domain_type) {
0171     case XS_LOCAL:
0172         switch (system_state) {
0173         case SYSTEM_POWER_OFF:
0174         case SYSTEM_RESTART:
0175         case SYSTEM_HALT:
0176             return false;
0177         default:
0178             break;
0179         }
0180         return true;
0181     case XS_PV:
0182     case XS_HVM:
0183         /* FIXME: Could check that the remote domain is alive,
0184          * but it is normally initial domain. */
0185         return true;
0186     default:
0187         break;
0188     }
0189     return false;
0190 }
0191 
0192 static bool test_reply(struct xb_req_data *req)
0193 {
0194     if (req->state == xb_req_state_got_reply || !xenbus_ok()) {
0195         /* read req->state before all other fields */
0196         virt_rmb();
0197         return true;
0198     }
0199 
0200     /* Make sure to reread req->state each time. */
0201     barrier();
0202 
0203     return false;
0204 }
0205 
0206 static void *read_reply(struct xb_req_data *req)
0207 {
0208     do {
0209         wait_event(req->wq, test_reply(req));
0210 
0211         if (!xenbus_ok())
0212             /*
0213              * If we are in the process of being shut-down there is
0214              * no point of trying to contact XenBus - it is either
0215              * killed (xenstored application) or the other domain
0216              * has been killed or is unreachable.
0217              */
0218             return ERR_PTR(-EIO);
0219         if (req->err)
0220             return ERR_PTR(req->err);
0221 
0222     } while (req->state != xb_req_state_got_reply);
0223 
0224     return req->body;
0225 }
0226 
0227 static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
0228 {
0229     bool notify;
0230 
0231     req->msg = *msg;
0232     req->err = 0;
0233     req->state = xb_req_state_queued;
0234     init_waitqueue_head(&req->wq);
0235 
0236     /* Save the caller req_id and restore it later in the reply */
0237     req->caller_req_id = req->msg.req_id;
0238     req->msg.req_id = xs_request_enter(req);
0239 
0240     mutex_lock(&xb_write_mutex);
0241     list_add_tail(&req->list, &xb_write_list);
0242     notify = list_is_singular(&xb_write_list);
0243     mutex_unlock(&xb_write_mutex);
0244 
0245     if (notify)
0246         wake_up(&xb_waitq);
0247 }
0248 
0249 static void *xs_wait_for_reply(struct xb_req_data *req, struct xsd_sockmsg *msg)
0250 {
0251     void *ret;
0252 
0253     ret = read_reply(req);
0254 
0255     xs_request_exit(req);
0256 
0257     msg->type = req->msg.type;
0258     msg->len = req->msg.len;
0259 
0260     mutex_lock(&xb_write_mutex);
0261     if (req->state == xb_req_state_queued ||
0262         req->state == xb_req_state_wait_reply)
0263         req->state = xb_req_state_aborted;
0264     else
0265         kfree(req);
0266     mutex_unlock(&xb_write_mutex);
0267 
0268     return ret;
0269 }
0270 
0271 static void xs_wake_up(struct xb_req_data *req)
0272 {
0273     wake_up(&req->wq);
0274 }
0275 
0276 int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
0277 {
0278     struct xb_req_data *req;
0279     struct kvec *vec;
0280 
0281     req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);
0282     if (!req)
0283         return -ENOMEM;
0284 
0285     vec = (struct kvec *)(req + 1);
0286     vec->iov_len = msg->len;
0287     vec->iov_base = msg + 1;
0288 
0289     req->vec = vec;
0290     req->num_vecs = 1;
0291     req->cb = xenbus_dev_queue_reply;
0292     req->par = par;
0293     req->user_req = true;
0294 
0295     xs_send(req, msg);
0296 
0297     return 0;
0298 }
0299 EXPORT_SYMBOL(xenbus_dev_request_and_reply);
0300 
0301 /* Send message to xs, get kmalloc'ed reply.  ERR_PTR() on error. */
0302 static void *xs_talkv(struct xenbus_transaction t,
0303               enum xsd_sockmsg_type type,
0304               const struct kvec *iovec,
0305               unsigned int num_vecs,
0306               unsigned int *len)
0307 {
0308     struct xb_req_data *req;
0309     struct xsd_sockmsg msg;
0310     void *ret = NULL;
0311     unsigned int i;
0312     int err;
0313 
0314     req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
0315     if (!req)
0316         return ERR_PTR(-ENOMEM);
0317 
0318     req->vec = iovec;
0319     req->num_vecs = num_vecs;
0320     req->cb = xs_wake_up;
0321     req->user_req = false;
0322 
0323     msg.req_id = 0;
0324     msg.tx_id = t.id;
0325     msg.type = type;
0326     msg.len = 0;
0327     for (i = 0; i < num_vecs; i++)
0328         msg.len += iovec[i].iov_len;
0329 
0330     xs_send(req, &msg);
0331 
0332     ret = xs_wait_for_reply(req, &msg);
0333     if (len)
0334         *len = msg.len;
0335 
0336     if (IS_ERR(ret))
0337         return ret;
0338 
0339     if (msg.type == XS_ERROR) {
0340         err = get_error(ret);
0341         kfree(ret);
0342         return ERR_PTR(-err);
0343     }
0344 
0345     if (msg.type != type) {
0346         pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
0347                     msg.type, type);
0348         kfree(ret);
0349         return ERR_PTR(-EINVAL);
0350     }
0351     return ret;
0352 }
0353 
0354 /* Simplified version of xs_talkv: single message. */
0355 static void *xs_single(struct xenbus_transaction t,
0356                enum xsd_sockmsg_type type,
0357                const char *string,
0358                unsigned int *len)
0359 {
0360     struct kvec iovec;
0361 
0362     iovec.iov_base = (void *)string;
0363     iovec.iov_len = strlen(string) + 1;
0364     return xs_talkv(t, type, &iovec, 1, len);
0365 }
0366 
0367 /* Many commands only need an ack, don't care what it says. */
0368 static int xs_error(char *reply)
0369 {
0370     if (IS_ERR(reply))
0371         return PTR_ERR(reply);
0372     kfree(reply);
0373     return 0;
0374 }
0375 
0376 static unsigned int count_strings(const char *strings, unsigned int len)
0377 {
0378     unsigned int num;
0379     const char *p;
0380 
0381     for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
0382         num++;
0383 
0384     return num;
0385 }
0386 
0387 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
0388 static char *join(const char *dir, const char *name)
0389 {
0390     char *buffer;
0391 
0392     if (strlen(name) == 0)
0393         buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
0394     else
0395         buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
0396     return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
0397 }
0398 
0399 static char **split(char *strings, unsigned int len, unsigned int *num)
0400 {
0401     char *p, **ret;
0402 
0403     /* Count the strings. */
0404     *num = count_strings(strings, len);
0405 
0406     /* Transfer to one big alloc for easy freeing. */
0407     ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
0408     if (!ret) {
0409         kfree(strings);
0410         return ERR_PTR(-ENOMEM);
0411     }
0412     memcpy(&ret[*num], strings, len);
0413     kfree(strings);
0414 
0415     strings = (char *)&ret[*num];
0416     for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
0417         ret[(*num)++] = p;
0418 
0419     return ret;
0420 }
0421 
0422 char **xenbus_directory(struct xenbus_transaction t,
0423             const char *dir, const char *node, unsigned int *num)
0424 {
0425     char *strings, *path;
0426     unsigned int len;
0427 
0428     path = join(dir, node);
0429     if (IS_ERR(path))
0430         return (char **)path;
0431 
0432     strings = xs_single(t, XS_DIRECTORY, path, &len);
0433     kfree(path);
0434     if (IS_ERR(strings))
0435         return (char **)strings;
0436 
0437     return split(strings, len, num);
0438 }
0439 EXPORT_SYMBOL_GPL(xenbus_directory);
0440 
0441 /* Check if a path exists. Return 1 if it does. */
0442 int xenbus_exists(struct xenbus_transaction t,
0443           const char *dir, const char *node)
0444 {
0445     char **d;
0446     int dir_n;
0447 
0448     d = xenbus_directory(t, dir, node, &dir_n);
0449     if (IS_ERR(d))
0450         return 0;
0451     kfree(d);
0452     return 1;
0453 }
0454 EXPORT_SYMBOL_GPL(xenbus_exists);
0455 
0456 /* Get the value of a single file.
0457  * Returns a kmalloced value: call free() on it after use.
0458  * len indicates length in bytes.
0459  */
0460 void *xenbus_read(struct xenbus_transaction t,
0461           const char *dir, const char *node, unsigned int *len)
0462 {
0463     char *path;
0464     void *ret;
0465 
0466     path = join(dir, node);
0467     if (IS_ERR(path))
0468         return (void *)path;
0469 
0470     ret = xs_single(t, XS_READ, path, len);
0471     kfree(path);
0472     return ret;
0473 }
0474 EXPORT_SYMBOL_GPL(xenbus_read);
0475 
0476 /* Write the value of a single file.
0477  * Returns -err on failure.
0478  */
0479 int xenbus_write(struct xenbus_transaction t,
0480          const char *dir, const char *node, const char *string)
0481 {
0482     const char *path;
0483     struct kvec iovec[2];
0484     int ret;
0485 
0486     path = join(dir, node);
0487     if (IS_ERR(path))
0488         return PTR_ERR(path);
0489 
0490     iovec[0].iov_base = (void *)path;
0491     iovec[0].iov_len = strlen(path) + 1;
0492     iovec[1].iov_base = (void *)string;
0493     iovec[1].iov_len = strlen(string);
0494 
0495     ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
0496     kfree(path);
0497     return ret;
0498 }
0499 EXPORT_SYMBOL_GPL(xenbus_write);
0500 
0501 /* Create a new directory. */
0502 int xenbus_mkdir(struct xenbus_transaction t,
0503          const char *dir, const char *node)
0504 {
0505     char *path;
0506     int ret;
0507 
0508     path = join(dir, node);
0509     if (IS_ERR(path))
0510         return PTR_ERR(path);
0511 
0512     ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
0513     kfree(path);
0514     return ret;
0515 }
0516 EXPORT_SYMBOL_GPL(xenbus_mkdir);
0517 
0518 /* Destroy a file or directory (directories must be empty). */
0519 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
0520 {
0521     char *path;
0522     int ret;
0523 
0524     path = join(dir, node);
0525     if (IS_ERR(path))
0526         return PTR_ERR(path);
0527 
0528     ret = xs_error(xs_single(t, XS_RM, path, NULL));
0529     kfree(path);
0530     return ret;
0531 }
0532 EXPORT_SYMBOL_GPL(xenbus_rm);
0533 
0534 /* Start a transaction: changes by others will not be seen during this
0535  * transaction, and changes will not be visible to others until end.
0536  */
0537 int xenbus_transaction_start(struct xenbus_transaction *t)
0538 {
0539     char *id_str;
0540 
0541     id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
0542     if (IS_ERR(id_str))
0543         return PTR_ERR(id_str);
0544 
0545     t->id = simple_strtoul(id_str, NULL, 0);
0546     kfree(id_str);
0547     return 0;
0548 }
0549 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
0550 
0551 /* End a transaction.
0552  * If abandon is true, transaction is discarded instead of committed.
0553  */
0554 int xenbus_transaction_end(struct xenbus_transaction t, int abort)
0555 {
0556     char abortstr[2];
0557 
0558     if (abort)
0559         strcpy(abortstr, "F");
0560     else
0561         strcpy(abortstr, "T");
0562 
0563     return xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
0564 }
0565 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
0566 
0567 /* Single read and scanf: returns -errno or num scanned. */
0568 int xenbus_scanf(struct xenbus_transaction t,
0569          const char *dir, const char *node, const char *fmt, ...)
0570 {
0571     va_list ap;
0572     int ret;
0573     char *val;
0574 
0575     val = xenbus_read(t, dir, node, NULL);
0576     if (IS_ERR(val))
0577         return PTR_ERR(val);
0578 
0579     va_start(ap, fmt);
0580     ret = vsscanf(val, fmt, ap);
0581     va_end(ap);
0582     kfree(val);
0583     /* Distinctive errno. */
0584     if (ret == 0)
0585         return -ERANGE;
0586     return ret;
0587 }
0588 EXPORT_SYMBOL_GPL(xenbus_scanf);
0589 
0590 /* Read an (optional) unsigned value. */
0591 unsigned int xenbus_read_unsigned(const char *dir, const char *node,
0592                   unsigned int default_val)
0593 {
0594     unsigned int val;
0595     int ret;
0596 
0597     ret = xenbus_scanf(XBT_NIL, dir, node, "%u", &val);
0598     if (ret <= 0)
0599         val = default_val;
0600 
0601     return val;
0602 }
0603 EXPORT_SYMBOL_GPL(xenbus_read_unsigned);
0604 
0605 /* Single printf and write: returns -errno or 0. */
0606 int xenbus_printf(struct xenbus_transaction t,
0607           const char *dir, const char *node, const char *fmt, ...)
0608 {
0609     va_list ap;
0610     int ret;
0611     char *buf;
0612 
0613     va_start(ap, fmt);
0614     buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
0615     va_end(ap);
0616 
0617     if (!buf)
0618         return -ENOMEM;
0619 
0620     ret = xenbus_write(t, dir, node, buf);
0621 
0622     kfree(buf);
0623 
0624     return ret;
0625 }
0626 EXPORT_SYMBOL_GPL(xenbus_printf);
0627 
0628 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
0629 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
0630 {
0631     va_list ap;
0632     const char *name;
0633     int ret = 0;
0634 
0635     va_start(ap, dir);
0636     while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
0637         const char *fmt = va_arg(ap, char *);
0638         void *result = va_arg(ap, void *);
0639         char *p;
0640 
0641         p = xenbus_read(t, dir, name, NULL);
0642         if (IS_ERR(p)) {
0643             ret = PTR_ERR(p);
0644             break;
0645         }
0646         if (fmt) {
0647             if (sscanf(p, fmt, result) == 0)
0648                 ret = -EINVAL;
0649             kfree(p);
0650         } else
0651             *(char **)result = p;
0652     }
0653     va_end(ap);
0654     return ret;
0655 }
0656 EXPORT_SYMBOL_GPL(xenbus_gather);
0657 
0658 static int xs_watch(const char *path, const char *token)
0659 {
0660     struct kvec iov[2];
0661 
0662     iov[0].iov_base = (void *)path;
0663     iov[0].iov_len = strlen(path) + 1;
0664     iov[1].iov_base = (void *)token;
0665     iov[1].iov_len = strlen(token) + 1;
0666 
0667     return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
0668                  ARRAY_SIZE(iov), NULL));
0669 }
0670 
0671 static int xs_unwatch(const char *path, const char *token)
0672 {
0673     struct kvec iov[2];
0674 
0675     iov[0].iov_base = (char *)path;
0676     iov[0].iov_len = strlen(path) + 1;
0677     iov[1].iov_base = (char *)token;
0678     iov[1].iov_len = strlen(token) + 1;
0679 
0680     return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
0681                  ARRAY_SIZE(iov), NULL));
0682 }
0683 
0684 static struct xenbus_watch *find_watch(const char *token)
0685 {
0686     struct xenbus_watch *i, *cmp;
0687 
0688     cmp = (void *)simple_strtoul(token, NULL, 16);
0689 
0690     list_for_each_entry(i, &watches, list)
0691         if (i == cmp)
0692             return i;
0693 
0694     return NULL;
0695 }
0696 
0697 int xs_watch_msg(struct xs_watch_event *event)
0698 {
0699     if (count_strings(event->body, event->len) != 2) {
0700         kfree(event);
0701         return -EINVAL;
0702     }
0703     event->path = (const char *)event->body;
0704     event->token = (const char *)strchr(event->body, '\0') + 1;
0705 
0706     spin_lock(&watches_lock);
0707     event->handle = find_watch(event->token);
0708     if (event->handle != NULL &&
0709             (!event->handle->will_handle ||
0710              event->handle->will_handle(event->handle,
0711                  event->path, event->token))) {
0712         spin_lock(&watch_events_lock);
0713         list_add_tail(&event->list, &watch_events);
0714         event->handle->nr_pending++;
0715         wake_up(&watch_events_waitq);
0716         spin_unlock(&watch_events_lock);
0717     } else
0718         kfree(event);
0719     spin_unlock(&watches_lock);
0720 
0721     return 0;
0722 }
0723 
0724 /*
0725  * Certain older XenBus toolstack cannot handle reading values that are
0726  * not populated. Some Xen 3.4 installation are incapable of doing this
0727  * so if we are running on anything older than 4 do not attempt to read
0728  * control/platform-feature-xs_reset_watches.
0729  */
0730 static bool xen_strict_xenbus_quirk(void)
0731 {
0732 #ifdef CONFIG_X86
0733     uint32_t eax, ebx, ecx, edx, base;
0734 
0735     base = xen_cpuid_base();
0736     cpuid(base + 1, &eax, &ebx, &ecx, &edx);
0737 
0738     if ((eax >> 16) < 4)
0739         return true;
0740 #endif
0741     return false;
0742 
0743 }
0744 static void xs_reset_watches(void)
0745 {
0746     int err;
0747 
0748     if (!xen_hvm_domain() || xen_initial_domain())
0749         return;
0750 
0751     if (xen_strict_xenbus_quirk())
0752         return;
0753 
0754     if (!xenbus_read_unsigned("control",
0755                   "platform-feature-xs_reset_watches", 0))
0756         return;
0757 
0758     err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
0759     if (err && err != -EEXIST)
0760         pr_warn("xs_reset_watches failed: %d\n", err);
0761 }
0762 
0763 /* Register callback to watch this node. */
0764 int register_xenbus_watch(struct xenbus_watch *watch)
0765 {
0766     /* Pointer in ascii is the token. */
0767     char token[sizeof(watch) * 2 + 1];
0768     int err;
0769 
0770     sprintf(token, "%lX", (long)watch);
0771 
0772     watch->nr_pending = 0;
0773 
0774     down_read(&xs_watch_rwsem);
0775 
0776     spin_lock(&watches_lock);
0777     BUG_ON(find_watch(token));
0778     list_add(&watch->list, &watches);
0779     spin_unlock(&watches_lock);
0780 
0781     err = xs_watch(watch->node, token);
0782 
0783     if (err) {
0784         spin_lock(&watches_lock);
0785         list_del(&watch->list);
0786         spin_unlock(&watches_lock);
0787     }
0788 
0789     up_read(&xs_watch_rwsem);
0790 
0791     return err;
0792 }
0793 EXPORT_SYMBOL_GPL(register_xenbus_watch);
0794 
0795 void unregister_xenbus_watch(struct xenbus_watch *watch)
0796 {
0797     struct xs_watch_event *event, *tmp;
0798     char token[sizeof(watch) * 2 + 1];
0799     int err;
0800 
0801     sprintf(token, "%lX", (long)watch);
0802 
0803     down_read(&xs_watch_rwsem);
0804 
0805     spin_lock(&watches_lock);
0806     BUG_ON(!find_watch(token));
0807     list_del(&watch->list);
0808     spin_unlock(&watches_lock);
0809 
0810     err = xs_unwatch(watch->node, token);
0811     if (err)
0812         pr_warn("Failed to release watch %s: %i\n", watch->node, err);
0813 
0814     up_read(&xs_watch_rwsem);
0815 
0816     /* Make sure there are no callbacks running currently (unless
0817        its us) */
0818     if (current->pid != xenwatch_pid)
0819         mutex_lock(&xenwatch_mutex);
0820 
0821     /* Cancel pending watch events. */
0822     spin_lock(&watch_events_lock);
0823     if (watch->nr_pending) {
0824         list_for_each_entry_safe(event, tmp, &watch_events, list) {
0825             if (event->handle != watch)
0826                 continue;
0827             list_del(&event->list);
0828             kfree(event);
0829         }
0830         watch->nr_pending = 0;
0831     }
0832     spin_unlock(&watch_events_lock);
0833 
0834     if (current->pid != xenwatch_pid)
0835         mutex_unlock(&xenwatch_mutex);
0836 }
0837 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
0838 
0839 void xs_suspend(void)
0840 {
0841     xs_suspend_enter();
0842 
0843     down_write(&xs_watch_rwsem);
0844     mutex_lock(&xs_response_mutex);
0845 }
0846 
0847 void xs_resume(void)
0848 {
0849     struct xenbus_watch *watch;
0850     char token[sizeof(watch) * 2 + 1];
0851 
0852     xb_init_comms();
0853 
0854     mutex_unlock(&xs_response_mutex);
0855 
0856     xs_suspend_exit();
0857 
0858     /* No need for watches_lock: the xs_watch_rwsem is sufficient. */
0859     list_for_each_entry(watch, &watches, list) {
0860         sprintf(token, "%lX", (long)watch);
0861         xs_watch(watch->node, token);
0862     }
0863 
0864     up_write(&xs_watch_rwsem);
0865 }
0866 
0867 void xs_suspend_cancel(void)
0868 {
0869     mutex_unlock(&xs_response_mutex);
0870     up_write(&xs_watch_rwsem);
0871 
0872     xs_suspend_exit();
0873 }
0874 
0875 static int xenwatch_thread(void *unused)
0876 {
0877     struct xs_watch_event *event;
0878 
0879     xenwatch_pid = current->pid;
0880 
0881     for (;;) {
0882         wait_event_interruptible(watch_events_waitq,
0883                      !list_empty(&watch_events));
0884 
0885         if (kthread_should_stop())
0886             break;
0887 
0888         mutex_lock(&xenwatch_mutex);
0889 
0890         spin_lock(&watch_events_lock);
0891         event = list_first_entry_or_null(&watch_events,
0892                 struct xs_watch_event, list);
0893         if (event) {
0894             list_del(&event->list);
0895             event->handle->nr_pending--;
0896         }
0897         spin_unlock(&watch_events_lock);
0898 
0899         if (event) {
0900             event->handle->callback(event->handle, event->path,
0901                         event->token);
0902             kfree(event);
0903         }
0904 
0905         mutex_unlock(&xenwatch_mutex);
0906     }
0907 
0908     return 0;
0909 }
0910 
0911 /*
0912  * Wake up all threads waiting for a xenstore reply. In case of shutdown all
0913  * pending replies will be marked as "aborted" in order to let the waiters
0914  * return in spite of xenstore possibly no longer being able to reply. This
0915  * will avoid blocking shutdown by a thread waiting for xenstore but being
0916  * necessary for shutdown processing to proceed.
0917  */
0918 static int xs_reboot_notify(struct notifier_block *nb,
0919                 unsigned long code, void *unused)
0920 {
0921     struct xb_req_data *req;
0922 
0923     mutex_lock(&xb_write_mutex);
0924     list_for_each_entry(req, &xs_reply_list, list)
0925         wake_up(&req->wq);
0926     list_for_each_entry(req, &xb_write_list, list)
0927         wake_up(&req->wq);
0928     mutex_unlock(&xb_write_mutex);
0929     return NOTIFY_DONE;
0930 }
0931 
0932 static struct notifier_block xs_reboot_nb = {
0933     .notifier_call = xs_reboot_notify,
0934 };
0935 
0936 int xs_init(void)
0937 {
0938     int err;
0939     struct task_struct *task;
0940 
0941     register_reboot_notifier(&xs_reboot_nb);
0942 
0943     /* Initialize the shared memory rings to talk to xenstored */
0944     err = xb_init_comms();
0945     if (err)
0946         return err;
0947 
0948     task = kthread_run(xenwatch_thread, NULL, "xenwatch");
0949     if (IS_ERR(task))
0950         return PTR_ERR(task);
0951 
0952     /* shutdown watches for kexec boot */
0953     xs_reset_watches();
0954 
0955     return 0;
0956 }