xenbus_xs.c 21 KB

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