xenbus_xs.c 19 KB

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