xenbus_xs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/unistd.h>
  35. #include <linux/errno.h>
  36. #include <linux/types.h>
  37. #include <linux/uio.h>
  38. #include <linux/kernel.h>
  39. #include <linux/string.h>
  40. #include <linux/err.h>
  41. #include <linux/slab.h>
  42. #include <linux/fcntl.h>
  43. #include <linux/kthread.h>
  44. #include <linux/reboot.h>
  45. #include <linux/rwsem.h>
  46. #include <linux/mutex.h>
  47. #include <asm/xen/hypervisor.h>
  48. #include <xen/xenbus.h>
  49. #include <xen/xen.h>
  50. #include "xenbus.h"
  51. /*
  52. * Framework to protect suspend/resume handling against normal Xenstore
  53. * message handling:
  54. * During suspend/resume there must be no open transaction and no pending
  55. * Xenstore request.
  56. * New watch events happening in this time can be ignored by firing all watches
  57. * after resume.
  58. */
  59. /* Lock protecting enter/exit critical region. */
  60. static DEFINE_SPINLOCK(xs_state_lock);
  61. /* Number of users in critical region (protected by xs_state_lock). */
  62. static unsigned int xs_state_users;
  63. /* Suspend handler waiting or already active (protected by xs_state_lock)? */
  64. static int xs_suspend_active;
  65. /* Unique Xenstore request id (protected by xs_state_lock). */
  66. static uint32_t xs_request_id;
  67. /* Wait queue for all callers waiting for critical region to become usable. */
  68. static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
  69. /* Wait queue for suspend handling waiting for critical region being empty. */
  70. static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
  71. /* List of registered watches, and a lock to protect it. */
  72. static LIST_HEAD(watches);
  73. static DEFINE_SPINLOCK(watches_lock);
  74. /* List of pending watch callback events, and a lock to protect it. */
  75. static LIST_HEAD(watch_events);
  76. static DEFINE_SPINLOCK(watch_events_lock);
  77. /* Protect watch (de)register against save/restore. */
  78. static DECLARE_RWSEM(xs_watch_rwsem);
  79. /*
  80. * Details of the xenwatch callback kernel thread. The thread waits on the
  81. * watch_events_waitq for work to do (queued on watch_events list). When it
  82. * wakes up it acquires the xenwatch_mutex before reading the list and
  83. * carrying out work.
  84. */
  85. static pid_t xenwatch_pid;
  86. static DEFINE_MUTEX(xenwatch_mutex);
  87. static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
  88. static void xs_suspend_enter(void)
  89. {
  90. spin_lock(&xs_state_lock);
  91. xs_suspend_active++;
  92. spin_unlock(&xs_state_lock);
  93. wait_event(xs_state_exit_wq, xs_state_users == 0);
  94. }
  95. static void xs_suspend_exit(void)
  96. {
  97. spin_lock(&xs_state_lock);
  98. xs_suspend_active--;
  99. spin_unlock(&xs_state_lock);
  100. wake_up_all(&xs_state_enter_wq);
  101. }
  102. static uint32_t xs_request_enter(struct xb_req_data *req)
  103. {
  104. uint32_t rq_id;
  105. req->type = req->msg.type;
  106. spin_lock(&xs_state_lock);
  107. while (!xs_state_users && xs_suspend_active) {
  108. spin_unlock(&xs_state_lock);
  109. wait_event(xs_state_enter_wq, xs_suspend_active == 0);
  110. spin_lock(&xs_state_lock);
  111. }
  112. if (req->type == XS_TRANSACTION_START)
  113. xs_state_users++;
  114. xs_state_users++;
  115. rq_id = xs_request_id++;
  116. spin_unlock(&xs_state_lock);
  117. return rq_id;
  118. }
  119. void xs_request_exit(struct xb_req_data *req)
  120. {
  121. spin_lock(&xs_state_lock);
  122. xs_state_users--;
  123. if ((req->type == XS_TRANSACTION_START && req->msg.type == XS_ERROR) ||
  124. req->type == XS_TRANSACTION_END)
  125. xs_state_users--;
  126. spin_unlock(&xs_state_lock);
  127. if (xs_suspend_active && !xs_state_users)
  128. wake_up(&xs_state_exit_wq);
  129. }
  130. static int get_error(const char *errorstring)
  131. {
  132. unsigned int i;
  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. static bool xenbus_ok(void)
  143. {
  144. switch (xen_store_domain_type) {
  145. case XS_LOCAL:
  146. switch (system_state) {
  147. case SYSTEM_POWER_OFF:
  148. case SYSTEM_RESTART:
  149. case SYSTEM_HALT:
  150. return false;
  151. default:
  152. break;
  153. }
  154. return true;
  155. case XS_PV:
  156. case XS_HVM:
  157. /* FIXME: Could check that the remote domain is alive,
  158. * but it is normally initial domain. */
  159. return true;
  160. default:
  161. break;
  162. }
  163. return false;
  164. }
  165. static bool test_reply(struct xb_req_data *req)
  166. {
  167. if (req->state == xb_req_state_got_reply || !xenbus_ok())
  168. return true;
  169. /* Make sure to reread req->state each time. */
  170. barrier();
  171. return false;
  172. }
  173. static void *read_reply(struct xb_req_data *req)
  174. {
  175. while (req->state != xb_req_state_got_reply) {
  176. wait_event(req->wq, test_reply(req));
  177. if (!xenbus_ok())
  178. /*
  179. * If we are in the process of being shut-down there is
  180. * no point of trying to contact XenBus - it is either
  181. * killed (xenstored application) or the other domain
  182. * has been killed or is unreachable.
  183. */
  184. return ERR_PTR(-EIO);
  185. if (req->err)
  186. return ERR_PTR(req->err);
  187. }
  188. return req->body;
  189. }
  190. static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
  191. {
  192. bool notify;
  193. req->msg = *msg;
  194. req->err = 0;
  195. req->state = xb_req_state_queued;
  196. init_waitqueue_head(&req->wq);
  197. req->msg.req_id = xs_request_enter(req);
  198. mutex_lock(&xb_write_mutex);
  199. list_add_tail(&req->list, &xb_write_list);
  200. notify = list_is_singular(&xb_write_list);
  201. mutex_unlock(&xb_write_mutex);
  202. if (notify)
  203. wake_up(&xb_waitq);
  204. }
  205. static void *xs_wait_for_reply(struct xb_req_data *req, struct xsd_sockmsg *msg)
  206. {
  207. void *ret;
  208. ret = read_reply(req);
  209. xs_request_exit(req);
  210. msg->type = req->msg.type;
  211. msg->len = req->msg.len;
  212. mutex_lock(&xb_write_mutex);
  213. if (req->state == xb_req_state_queued ||
  214. req->state == xb_req_state_wait_reply)
  215. req->state = xb_req_state_aborted;
  216. else
  217. kfree(req);
  218. mutex_unlock(&xb_write_mutex);
  219. return ret;
  220. }
  221. static void xs_wake_up(struct xb_req_data *req)
  222. {
  223. wake_up(&req->wq);
  224. }
  225. int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
  226. {
  227. struct xb_req_data *req;
  228. struct kvec *vec;
  229. req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);
  230. if (!req)
  231. return -ENOMEM;
  232. vec = (struct kvec *)(req + 1);
  233. vec->iov_len = msg->len;
  234. vec->iov_base = msg + 1;
  235. req->vec = vec;
  236. req->num_vecs = 1;
  237. req->cb = xenbus_dev_queue_reply;
  238. req->par = par;
  239. xs_send(req, msg);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(xenbus_dev_request_and_reply);
  243. /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
  244. static void *xs_talkv(struct xenbus_transaction t,
  245. enum xsd_sockmsg_type type,
  246. const struct kvec *iovec,
  247. unsigned int num_vecs,
  248. unsigned int *len)
  249. {
  250. struct xb_req_data *req;
  251. struct xsd_sockmsg msg;
  252. void *ret = NULL;
  253. unsigned int i;
  254. int err;
  255. req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
  256. if (!req)
  257. return ERR_PTR(-ENOMEM);
  258. req->vec = iovec;
  259. req->num_vecs = num_vecs;
  260. req->cb = xs_wake_up;
  261. msg.tx_id = t.id;
  262. msg.type = type;
  263. msg.len = 0;
  264. for (i = 0; i < num_vecs; i++)
  265. msg.len += iovec[i].iov_len;
  266. xs_send(req, &msg);
  267. ret = xs_wait_for_reply(req, &msg);
  268. if (len)
  269. *len = msg.len;
  270. if (IS_ERR(ret))
  271. return ret;
  272. if (msg.type == XS_ERROR) {
  273. err = get_error(ret);
  274. kfree(ret);
  275. return ERR_PTR(-err);
  276. }
  277. if (msg.type != type) {
  278. pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
  279. msg.type, type);
  280. kfree(ret);
  281. return ERR_PTR(-EINVAL);
  282. }
  283. return ret;
  284. }
  285. /* Simplified version of xs_talkv: single message. */
  286. static void *xs_single(struct xenbus_transaction t,
  287. enum xsd_sockmsg_type type,
  288. const char *string,
  289. unsigned int *len)
  290. {
  291. struct kvec iovec;
  292. iovec.iov_base = (void *)string;
  293. iovec.iov_len = strlen(string) + 1;
  294. return xs_talkv(t, type, &iovec, 1, len);
  295. }
  296. /* Many commands only need an ack, don't care what it says. */
  297. static int xs_error(char *reply)
  298. {
  299. if (IS_ERR(reply))
  300. return PTR_ERR(reply);
  301. kfree(reply);
  302. return 0;
  303. }
  304. static unsigned int count_strings(const char *strings, unsigned int len)
  305. {
  306. unsigned int num;
  307. const char *p;
  308. for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
  309. num++;
  310. return num;
  311. }
  312. /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
  313. static char *join(const char *dir, const char *name)
  314. {
  315. char *buffer;
  316. if (strlen(name) == 0)
  317. buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
  318. else
  319. buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
  320. return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
  321. }
  322. static char **split(char *strings, unsigned int len, unsigned int *num)
  323. {
  324. char *p, **ret;
  325. /* Count the strings. */
  326. *num = count_strings(strings, len);
  327. /* Transfer to one big alloc for easy freeing. */
  328. ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
  329. if (!ret) {
  330. kfree(strings);
  331. return ERR_PTR(-ENOMEM);
  332. }
  333. memcpy(&ret[*num], strings, len);
  334. kfree(strings);
  335. strings = (char *)&ret[*num];
  336. for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
  337. ret[(*num)++] = p;
  338. return ret;
  339. }
  340. char **xenbus_directory(struct xenbus_transaction t,
  341. const char *dir, const char *node, unsigned int *num)
  342. {
  343. char *strings, *path;
  344. unsigned int len;
  345. path = join(dir, node);
  346. if (IS_ERR(path))
  347. return (char **)path;
  348. strings = xs_single(t, XS_DIRECTORY, path, &len);
  349. kfree(path);
  350. if (IS_ERR(strings))
  351. return (char **)strings;
  352. return split(strings, len, num);
  353. }
  354. EXPORT_SYMBOL_GPL(xenbus_directory);
  355. /* Check if a path exists. Return 1 if it does. */
  356. int xenbus_exists(struct xenbus_transaction t,
  357. const char *dir, const char *node)
  358. {
  359. char **d;
  360. int dir_n;
  361. d = xenbus_directory(t, dir, node, &dir_n);
  362. if (IS_ERR(d))
  363. return 0;
  364. kfree(d);
  365. return 1;
  366. }
  367. EXPORT_SYMBOL_GPL(xenbus_exists);
  368. /* Get the value of a single file.
  369. * Returns a kmalloced value: call free() on it after use.
  370. * len indicates length in bytes.
  371. */
  372. void *xenbus_read(struct xenbus_transaction t,
  373. const char *dir, const char *node, unsigned int *len)
  374. {
  375. char *path;
  376. void *ret;
  377. path = join(dir, node);
  378. if (IS_ERR(path))
  379. return (void *)path;
  380. ret = xs_single(t, XS_READ, path, len);
  381. kfree(path);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL_GPL(xenbus_read);
  385. /* Write the value of a single file.
  386. * Returns -err on failure.
  387. */
  388. int xenbus_write(struct xenbus_transaction t,
  389. const char *dir, const char *node, const char *string)
  390. {
  391. const char *path;
  392. struct kvec iovec[2];
  393. int ret;
  394. path = join(dir, node);
  395. if (IS_ERR(path))
  396. return PTR_ERR(path);
  397. iovec[0].iov_base = (void *)path;
  398. iovec[0].iov_len = strlen(path) + 1;
  399. iovec[1].iov_base = (void *)string;
  400. iovec[1].iov_len = strlen(string);
  401. ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
  402. kfree(path);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL_GPL(xenbus_write);
  406. /* Create a new directory. */
  407. int xenbus_mkdir(struct xenbus_transaction t,
  408. const char *dir, const char *node)
  409. {
  410. char *path;
  411. int ret;
  412. path = join(dir, node);
  413. if (IS_ERR(path))
  414. return PTR_ERR(path);
  415. ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
  416. kfree(path);
  417. return ret;
  418. }
  419. EXPORT_SYMBOL_GPL(xenbus_mkdir);
  420. /* Destroy a file or directory (directories must be empty). */
  421. int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
  422. {
  423. char *path;
  424. int ret;
  425. path = join(dir, node);
  426. if (IS_ERR(path))
  427. return PTR_ERR(path);
  428. ret = xs_error(xs_single(t, XS_RM, path, NULL));
  429. kfree(path);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL_GPL(xenbus_rm);
  433. /* Start a transaction: changes by others will not be seen during this
  434. * transaction, and changes will not be visible to others until end.
  435. */
  436. int xenbus_transaction_start(struct xenbus_transaction *t)
  437. {
  438. char *id_str;
  439. id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
  440. if (IS_ERR(id_str))
  441. return PTR_ERR(id_str);
  442. t->id = simple_strtoul(id_str, NULL, 0);
  443. kfree(id_str);
  444. return 0;
  445. }
  446. EXPORT_SYMBOL_GPL(xenbus_transaction_start);
  447. /* End a transaction.
  448. * If abandon is true, transaction is discarded instead of committed.
  449. */
  450. int xenbus_transaction_end(struct xenbus_transaction t, int abort)
  451. {
  452. char abortstr[2];
  453. if (abort)
  454. strcpy(abortstr, "F");
  455. else
  456. strcpy(abortstr, "T");
  457. return xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
  458. }
  459. EXPORT_SYMBOL_GPL(xenbus_transaction_end);
  460. /* Single read and scanf: returns -errno or num scanned. */
  461. int xenbus_scanf(struct xenbus_transaction t,
  462. const char *dir, const char *node, const char *fmt, ...)
  463. {
  464. va_list ap;
  465. int ret;
  466. char *val;
  467. val = xenbus_read(t, dir, node, NULL);
  468. if (IS_ERR(val))
  469. return PTR_ERR(val);
  470. va_start(ap, fmt);
  471. ret = vsscanf(val, fmt, ap);
  472. va_end(ap);
  473. kfree(val);
  474. /* Distinctive errno. */
  475. if (ret == 0)
  476. return -ERANGE;
  477. return ret;
  478. }
  479. EXPORT_SYMBOL_GPL(xenbus_scanf);
  480. /* Read an (optional) unsigned value. */
  481. unsigned int xenbus_read_unsigned(const char *dir, const char *node,
  482. unsigned int default_val)
  483. {
  484. unsigned int val;
  485. int ret;
  486. ret = xenbus_scanf(XBT_NIL, dir, node, "%u", &val);
  487. if (ret <= 0)
  488. val = default_val;
  489. return val;
  490. }
  491. EXPORT_SYMBOL_GPL(xenbus_read_unsigned);
  492. /* Single printf and write: returns -errno or 0. */
  493. int xenbus_printf(struct xenbus_transaction t,
  494. const char *dir, const char *node, const char *fmt, ...)
  495. {
  496. va_list ap;
  497. int ret;
  498. char *buf;
  499. va_start(ap, fmt);
  500. buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
  501. va_end(ap);
  502. if (!buf)
  503. return -ENOMEM;
  504. ret = xenbus_write(t, dir, node, buf);
  505. kfree(buf);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL_GPL(xenbus_printf);
  509. /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
  510. int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
  511. {
  512. va_list ap;
  513. const char *name;
  514. int ret = 0;
  515. va_start(ap, dir);
  516. while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
  517. const char *fmt = va_arg(ap, char *);
  518. void *result = va_arg(ap, void *);
  519. char *p;
  520. p = xenbus_read(t, dir, name, NULL);
  521. if (IS_ERR(p)) {
  522. ret = PTR_ERR(p);
  523. break;
  524. }
  525. if (fmt) {
  526. if (sscanf(p, fmt, result) == 0)
  527. ret = -EINVAL;
  528. kfree(p);
  529. } else
  530. *(char **)result = p;
  531. }
  532. va_end(ap);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL_GPL(xenbus_gather);
  536. static int xs_watch(const char *path, const char *token)
  537. {
  538. struct kvec iov[2];
  539. iov[0].iov_base = (void *)path;
  540. iov[0].iov_len = strlen(path) + 1;
  541. iov[1].iov_base = (void *)token;
  542. iov[1].iov_len = strlen(token) + 1;
  543. return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
  544. ARRAY_SIZE(iov), NULL));
  545. }
  546. static int xs_unwatch(const char *path, const char *token)
  547. {
  548. struct kvec iov[2];
  549. iov[0].iov_base = (char *)path;
  550. iov[0].iov_len = strlen(path) + 1;
  551. iov[1].iov_base = (char *)token;
  552. iov[1].iov_len = strlen(token) + 1;
  553. return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
  554. ARRAY_SIZE(iov), NULL));
  555. }
  556. static struct xenbus_watch *find_watch(const char *token)
  557. {
  558. struct xenbus_watch *i, *cmp;
  559. cmp = (void *)simple_strtoul(token, NULL, 16);
  560. list_for_each_entry(i, &watches, list)
  561. if (i == cmp)
  562. return i;
  563. return NULL;
  564. }
  565. int xs_watch_msg(struct xs_watch_event *event)
  566. {
  567. if (count_strings(event->body, event->len) != 2) {
  568. kfree(event);
  569. return -EINVAL;
  570. }
  571. event->path = (const char *)event->body;
  572. event->token = (const char *)strchr(event->body, '\0') + 1;
  573. spin_lock(&watches_lock);
  574. event->handle = find_watch(event->token);
  575. if (event->handle != NULL) {
  576. spin_lock(&watch_events_lock);
  577. list_add_tail(&event->list, &watch_events);
  578. wake_up(&watch_events_waitq);
  579. spin_unlock(&watch_events_lock);
  580. } else
  581. kfree(event);
  582. spin_unlock(&watches_lock);
  583. return 0;
  584. }
  585. /*
  586. * Certain older XenBus toolstack cannot handle reading values that are
  587. * not populated. Some Xen 3.4 installation are incapable of doing this
  588. * so if we are running on anything older than 4 do not attempt to read
  589. * control/platform-feature-xs_reset_watches.
  590. */
  591. static bool xen_strict_xenbus_quirk(void)
  592. {
  593. #ifdef CONFIG_X86
  594. uint32_t eax, ebx, ecx, edx, base;
  595. base = xen_cpuid_base();
  596. cpuid(base + 1, &eax, &ebx, &ecx, &edx);
  597. if ((eax >> 16) < 4)
  598. return true;
  599. #endif
  600. return false;
  601. }
  602. static void xs_reset_watches(void)
  603. {
  604. int err;
  605. if (!xen_hvm_domain() || xen_initial_domain())
  606. return;
  607. if (xen_strict_xenbus_quirk())
  608. return;
  609. if (!xenbus_read_unsigned("control",
  610. "platform-feature-xs_reset_watches", 0))
  611. return;
  612. err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
  613. if (err && err != -EEXIST)
  614. pr_warn("xs_reset_watches failed: %d\n", err);
  615. }
  616. /* Register callback to watch this node. */
  617. int register_xenbus_watch(struct xenbus_watch *watch)
  618. {
  619. /* Pointer in ascii is the token. */
  620. char token[sizeof(watch) * 2 + 1];
  621. int err;
  622. sprintf(token, "%lX", (long)watch);
  623. down_read(&xs_watch_rwsem);
  624. spin_lock(&watches_lock);
  625. BUG_ON(find_watch(token));
  626. list_add(&watch->list, &watches);
  627. spin_unlock(&watches_lock);
  628. err = xs_watch(watch->node, token);
  629. if (err) {
  630. spin_lock(&watches_lock);
  631. list_del(&watch->list);
  632. spin_unlock(&watches_lock);
  633. }
  634. up_read(&xs_watch_rwsem);
  635. return err;
  636. }
  637. EXPORT_SYMBOL_GPL(register_xenbus_watch);
  638. void unregister_xenbus_watch(struct xenbus_watch *watch)
  639. {
  640. struct xs_watch_event *event, *tmp;
  641. char token[sizeof(watch) * 2 + 1];
  642. int err;
  643. sprintf(token, "%lX", (long)watch);
  644. down_read(&xs_watch_rwsem);
  645. spin_lock(&watches_lock);
  646. BUG_ON(!find_watch(token));
  647. list_del(&watch->list);
  648. spin_unlock(&watches_lock);
  649. err = xs_unwatch(watch->node, token);
  650. if (err)
  651. pr_warn("Failed to release watch %s: %i\n", watch->node, err);
  652. up_read(&xs_watch_rwsem);
  653. /* Make sure there are no callbacks running currently (unless
  654. its us) */
  655. if (current->pid != xenwatch_pid)
  656. mutex_lock(&xenwatch_mutex);
  657. /* Cancel pending watch events. */
  658. spin_lock(&watch_events_lock);
  659. list_for_each_entry_safe(event, tmp, &watch_events, list) {
  660. if (event->handle != watch)
  661. continue;
  662. list_del(&event->list);
  663. kfree(event);
  664. }
  665. spin_unlock(&watch_events_lock);
  666. if (current->pid != xenwatch_pid)
  667. mutex_unlock(&xenwatch_mutex);
  668. }
  669. EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
  670. void xs_suspend(void)
  671. {
  672. xs_suspend_enter();
  673. down_write(&xs_watch_rwsem);
  674. mutex_lock(&xs_response_mutex);
  675. }
  676. void xs_resume(void)
  677. {
  678. struct xenbus_watch *watch;
  679. char token[sizeof(watch) * 2 + 1];
  680. xb_init_comms();
  681. mutex_unlock(&xs_response_mutex);
  682. xs_suspend_exit();
  683. /* No need for watches_lock: the xs_watch_rwsem is sufficient. */
  684. list_for_each_entry(watch, &watches, list) {
  685. sprintf(token, "%lX", (long)watch);
  686. xs_watch(watch->node, token);
  687. }
  688. up_write(&xs_watch_rwsem);
  689. }
  690. void xs_suspend_cancel(void)
  691. {
  692. mutex_unlock(&xs_response_mutex);
  693. up_write(&xs_watch_rwsem);
  694. xs_suspend_exit();
  695. }
  696. static int xenwatch_thread(void *unused)
  697. {
  698. struct list_head *ent;
  699. struct xs_watch_event *event;
  700. for (;;) {
  701. wait_event_interruptible(watch_events_waitq,
  702. !list_empty(&watch_events));
  703. if (kthread_should_stop())
  704. break;
  705. mutex_lock(&xenwatch_mutex);
  706. spin_lock(&watch_events_lock);
  707. ent = watch_events.next;
  708. if (ent != &watch_events)
  709. list_del(ent);
  710. spin_unlock(&watch_events_lock);
  711. if (ent != &watch_events) {
  712. event = list_entry(ent, struct xs_watch_event, list);
  713. event->handle->callback(event->handle, event->path,
  714. event->token);
  715. kfree(event);
  716. }
  717. mutex_unlock(&xenwatch_mutex);
  718. }
  719. return 0;
  720. }
  721. /*
  722. * Wake up all threads waiting for a xenstore reply. In case of shutdown all
  723. * pending replies will be marked as "aborted" in order to let the waiters
  724. * return in spite of xenstore possibly no longer being able to reply. This
  725. * will avoid blocking shutdown by a thread waiting for xenstore but being
  726. * necessary for shutdown processing to proceed.
  727. */
  728. static int xs_reboot_notify(struct notifier_block *nb,
  729. unsigned long code, void *unused)
  730. {
  731. struct xb_req_data *req;
  732. mutex_lock(&xb_write_mutex);
  733. list_for_each_entry(req, &xs_reply_list, list)
  734. wake_up(&req->wq);
  735. list_for_each_entry(req, &xb_write_list, list)
  736. wake_up(&req->wq);
  737. mutex_unlock(&xb_write_mutex);
  738. return NOTIFY_DONE;
  739. }
  740. static struct notifier_block xs_reboot_nb = {
  741. .notifier_call = xs_reboot_notify,
  742. };
  743. int xs_init(void)
  744. {
  745. int err;
  746. struct task_struct *task;
  747. register_reboot_notifier(&xs_reboot_nb);
  748. /* Initialize the shared memory rings to talk to xenstored */
  749. err = xb_init_comms();
  750. if (err)
  751. return err;
  752. task = kthread_run(xenwatch_thread, NULL, "xenwatch");
  753. if (IS_ERR(task))
  754. return PTR_ERR(task);
  755. xenwatch_pid = task->pid;
  756. /* shutdown watches for kexec boot */
  757. xs_reset_watches();
  758. return 0;
  759. }