xenbus_xs.c 22 KB

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