xenbus_xs.c 21 KB

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