xenbus_xs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. char *buf;
  445. va_start(ap, fmt);
  446. buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
  447. va_end(ap);
  448. if (!buf)
  449. return -ENOMEM;
  450. ret = xenbus_write(t, dir, node, buf);
  451. kfree(buf);
  452. return ret;
  453. }
  454. EXPORT_SYMBOL_GPL(xenbus_printf);
  455. /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
  456. int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
  457. {
  458. va_list ap;
  459. const char *name;
  460. int ret = 0;
  461. va_start(ap, dir);
  462. while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
  463. const char *fmt = va_arg(ap, char *);
  464. void *result = va_arg(ap, void *);
  465. char *p;
  466. p = xenbus_read(t, dir, name, NULL);
  467. if (IS_ERR(p)) {
  468. ret = PTR_ERR(p);
  469. break;
  470. }
  471. if (fmt) {
  472. if (sscanf(p, fmt, result) == 0)
  473. ret = -EINVAL;
  474. kfree(p);
  475. } else
  476. *(char **)result = p;
  477. }
  478. va_end(ap);
  479. return ret;
  480. }
  481. EXPORT_SYMBOL_GPL(xenbus_gather);
  482. static int xs_watch(const char *path, const char *token)
  483. {
  484. struct kvec iov[2];
  485. iov[0].iov_base = (void *)path;
  486. iov[0].iov_len = strlen(path) + 1;
  487. iov[1].iov_base = (void *)token;
  488. iov[1].iov_len = strlen(token) + 1;
  489. return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
  490. ARRAY_SIZE(iov), NULL));
  491. }
  492. static int xs_unwatch(const char *path, const char *token)
  493. {
  494. struct kvec iov[2];
  495. iov[0].iov_base = (char *)path;
  496. iov[0].iov_len = strlen(path) + 1;
  497. iov[1].iov_base = (char *)token;
  498. iov[1].iov_len = strlen(token) + 1;
  499. return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
  500. ARRAY_SIZE(iov), NULL));
  501. }
  502. static struct xenbus_watch *find_watch(const char *token)
  503. {
  504. struct xenbus_watch *i, *cmp;
  505. cmp = (void *)simple_strtoul(token, NULL, 16);
  506. list_for_each_entry(i, &watches, list)
  507. if (i == cmp)
  508. return i;
  509. return NULL;
  510. }
  511. /* Register callback to watch this node. */
  512. int register_xenbus_watch(struct xenbus_watch *watch)
  513. {
  514. /* Pointer in ascii is the token. */
  515. char token[sizeof(watch) * 2 + 1];
  516. int err;
  517. sprintf(token, "%lX", (long)watch);
  518. down_read(&xs_state.watch_mutex);
  519. spin_lock(&watches_lock);
  520. BUG_ON(find_watch(token));
  521. list_add(&watch->list, &watches);
  522. spin_unlock(&watches_lock);
  523. err = xs_watch(watch->node, token);
  524. if (err) {
  525. spin_lock(&watches_lock);
  526. list_del(&watch->list);
  527. spin_unlock(&watches_lock);
  528. }
  529. up_read(&xs_state.watch_mutex);
  530. return err;
  531. }
  532. EXPORT_SYMBOL_GPL(register_xenbus_watch);
  533. void unregister_xenbus_watch(struct xenbus_watch *watch)
  534. {
  535. struct xs_stored_msg *msg, *tmp;
  536. char token[sizeof(watch) * 2 + 1];
  537. int err;
  538. sprintf(token, "%lX", (long)watch);
  539. down_read(&xs_state.watch_mutex);
  540. spin_lock(&watches_lock);
  541. BUG_ON(!find_watch(token));
  542. list_del(&watch->list);
  543. spin_unlock(&watches_lock);
  544. err = xs_unwatch(watch->node, token);
  545. if (err)
  546. printk(KERN_WARNING
  547. "XENBUS Failed to release watch %s: %i\n",
  548. watch->node, err);
  549. up_read(&xs_state.watch_mutex);
  550. /* Make sure there are no callbacks running currently (unless
  551. its us) */
  552. if (current->pid != xenwatch_pid)
  553. mutex_lock(&xenwatch_mutex);
  554. /* Cancel pending watch events. */
  555. spin_lock(&watch_events_lock);
  556. list_for_each_entry_safe(msg, tmp, &watch_events, list) {
  557. if (msg->u.watch.handle != watch)
  558. continue;
  559. list_del(&msg->list);
  560. kfree(msg->u.watch.vec);
  561. kfree(msg);
  562. }
  563. spin_unlock(&watch_events_lock);
  564. if (current->pid != xenwatch_pid)
  565. mutex_unlock(&xenwatch_mutex);
  566. }
  567. EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
  568. void xs_suspend(void)
  569. {
  570. transaction_suspend();
  571. down_write(&xs_state.watch_mutex);
  572. mutex_lock(&xs_state.request_mutex);
  573. mutex_lock(&xs_state.response_mutex);
  574. }
  575. void xs_resume(void)
  576. {
  577. struct xenbus_watch *watch;
  578. char token[sizeof(watch) * 2 + 1];
  579. xb_init_comms();
  580. mutex_unlock(&xs_state.response_mutex);
  581. mutex_unlock(&xs_state.request_mutex);
  582. transaction_resume();
  583. /* No need for watches_lock: the watch_mutex is sufficient. */
  584. list_for_each_entry(watch, &watches, list) {
  585. sprintf(token, "%lX", (long)watch);
  586. xs_watch(watch->node, token);
  587. }
  588. up_write(&xs_state.watch_mutex);
  589. }
  590. void xs_suspend_cancel(void)
  591. {
  592. mutex_unlock(&xs_state.response_mutex);
  593. mutex_unlock(&xs_state.request_mutex);
  594. up_write(&xs_state.watch_mutex);
  595. mutex_unlock(&xs_state.transaction_mutex);
  596. }
  597. static int xenwatch_thread(void *unused)
  598. {
  599. struct list_head *ent;
  600. struct xs_stored_msg *msg;
  601. for (;;) {
  602. wait_event_interruptible(watch_events_waitq,
  603. !list_empty(&watch_events));
  604. if (kthread_should_stop())
  605. break;
  606. mutex_lock(&xenwatch_mutex);
  607. spin_lock(&watch_events_lock);
  608. ent = watch_events.next;
  609. if (ent != &watch_events)
  610. list_del(ent);
  611. spin_unlock(&watch_events_lock);
  612. if (ent != &watch_events) {
  613. msg = list_entry(ent, struct xs_stored_msg, list);
  614. msg->u.watch.handle->callback(
  615. msg->u.watch.handle,
  616. (const char **)msg->u.watch.vec,
  617. msg->u.watch.vec_size);
  618. kfree(msg->u.watch.vec);
  619. kfree(msg);
  620. }
  621. mutex_unlock(&xenwatch_mutex);
  622. }
  623. return 0;
  624. }
  625. static int process_msg(void)
  626. {
  627. struct xs_stored_msg *msg;
  628. char *body;
  629. int err;
  630. /*
  631. * We must disallow save/restore while reading a xenstore message.
  632. * A partial read across s/r leaves us out of sync with xenstored.
  633. */
  634. for (;;) {
  635. err = xb_wait_for_data_to_read();
  636. if (err)
  637. return err;
  638. mutex_lock(&xs_state.response_mutex);
  639. if (xb_data_to_read())
  640. break;
  641. /* We raced with save/restore: pending data 'disappeared'. */
  642. mutex_unlock(&xs_state.response_mutex);
  643. }
  644. msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
  645. if (msg == NULL) {
  646. err = -ENOMEM;
  647. goto out;
  648. }
  649. err = xb_read(&msg->hdr, sizeof(msg->hdr));
  650. if (err) {
  651. kfree(msg);
  652. goto out;
  653. }
  654. if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
  655. kfree(msg);
  656. err = -EINVAL;
  657. goto out;
  658. }
  659. body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
  660. if (body == NULL) {
  661. kfree(msg);
  662. err = -ENOMEM;
  663. goto out;
  664. }
  665. err = xb_read(body, msg->hdr.len);
  666. if (err) {
  667. kfree(body);
  668. kfree(msg);
  669. goto out;
  670. }
  671. body[msg->hdr.len] = '\0';
  672. if (msg->hdr.type == XS_WATCH_EVENT) {
  673. msg->u.watch.vec = split(body, msg->hdr.len,
  674. &msg->u.watch.vec_size);
  675. if (IS_ERR(msg->u.watch.vec)) {
  676. err = PTR_ERR(msg->u.watch.vec);
  677. kfree(msg);
  678. goto out;
  679. }
  680. spin_lock(&watches_lock);
  681. msg->u.watch.handle = find_watch(
  682. msg->u.watch.vec[XS_WATCH_TOKEN]);
  683. if (msg->u.watch.handle != NULL) {
  684. spin_lock(&watch_events_lock);
  685. list_add_tail(&msg->list, &watch_events);
  686. wake_up(&watch_events_waitq);
  687. spin_unlock(&watch_events_lock);
  688. } else {
  689. kfree(msg->u.watch.vec);
  690. kfree(msg);
  691. }
  692. spin_unlock(&watches_lock);
  693. } else {
  694. msg->u.reply.body = body;
  695. spin_lock(&xs_state.reply_lock);
  696. list_add_tail(&msg->list, &xs_state.reply_list);
  697. spin_unlock(&xs_state.reply_lock);
  698. wake_up(&xs_state.reply_waitq);
  699. }
  700. out:
  701. mutex_unlock(&xs_state.response_mutex);
  702. return err;
  703. }
  704. static int xenbus_thread(void *unused)
  705. {
  706. int err;
  707. for (;;) {
  708. err = process_msg();
  709. if (err)
  710. printk(KERN_WARNING "XENBUS error %d while reading "
  711. "message\n", err);
  712. if (kthread_should_stop())
  713. break;
  714. }
  715. return 0;
  716. }
  717. int xs_init(void)
  718. {
  719. int err;
  720. struct task_struct *task;
  721. INIT_LIST_HEAD(&xs_state.reply_list);
  722. spin_lock_init(&xs_state.reply_lock);
  723. init_waitqueue_head(&xs_state.reply_waitq);
  724. mutex_init(&xs_state.request_mutex);
  725. mutex_init(&xs_state.response_mutex);
  726. mutex_init(&xs_state.transaction_mutex);
  727. init_rwsem(&xs_state.watch_mutex);
  728. atomic_set(&xs_state.transaction_count, 0);
  729. init_waitqueue_head(&xs_state.transaction_wq);
  730. /* Initialize the shared memory rings to talk to xenstored */
  731. err = xb_init_comms();
  732. if (err)
  733. return err;
  734. task = kthread_run(xenwatch_thread, NULL, "xenwatch");
  735. if (IS_ERR(task))
  736. return PTR_ERR(task);
  737. xenwatch_pid = task->pid;
  738. task = kthread_run(xenbus_thread, NULL, "xenbus");
  739. if (IS_ERR(task))
  740. return PTR_ERR(task);
  741. return 0;
  742. }