xenbus_xs.c 20 KB

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