xenbus_xs.c 20 KB

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