xenbus_xs.c 21 KB

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