xenbus_xs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/unistd.h>
  35. #include <linux/errno.h>
  36. #include <linux/types.h>
  37. #include <linux/uio.h>
  38. #include <linux/kernel.h>
  39. #include <linux/string.h>
  40. #include <linux/err.h>
  41. #include <linux/slab.h>
  42. #include <linux/fcntl.h>
  43. #include <linux/kthread.h>
  44. #include <linux/rwsem.h>
  45. #include <linux/module.h>
  46. #include <linux/mutex.h>
  47. #include <asm/xen/hypervisor.h>
  48. #include <xen/xenbus.h>
  49. #include <xen/xen.h>
  50. #include "xenbus_comms.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. pr_warn("xen store gave: unknown error %s\n",
  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. pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
  230. msg.type, type);
  231. kfree(ret);
  232. return ERR_PTR(-EINVAL);
  233. }
  234. return ret;
  235. }
  236. /* Simplified version of xs_talkv: single message. */
  237. static void *xs_single(struct xenbus_transaction t,
  238. enum xsd_sockmsg_type type,
  239. const char *string,
  240. unsigned int *len)
  241. {
  242. struct kvec iovec;
  243. iovec.iov_base = (void *)string;
  244. iovec.iov_len = strlen(string) + 1;
  245. return xs_talkv(t, type, &iovec, 1, len);
  246. }
  247. /* Many commands only need an ack, don't care what it says. */
  248. static int xs_error(char *reply)
  249. {
  250. if (IS_ERR(reply))
  251. return PTR_ERR(reply);
  252. kfree(reply);
  253. return 0;
  254. }
  255. static unsigned int count_strings(const char *strings, unsigned int len)
  256. {
  257. unsigned int num;
  258. const char *p;
  259. for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
  260. num++;
  261. return num;
  262. }
  263. /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
  264. static char *join(const char *dir, const char *name)
  265. {
  266. char *buffer;
  267. if (strlen(name) == 0)
  268. buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
  269. else
  270. buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
  271. return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
  272. }
  273. static char **split(char *strings, unsigned int len, unsigned int *num)
  274. {
  275. char *p, **ret;
  276. /* Count the strings. */
  277. *num = count_strings(strings, len);
  278. /* Transfer to one big alloc for easy freeing. */
  279. ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
  280. if (!ret) {
  281. kfree(strings);
  282. return ERR_PTR(-ENOMEM);
  283. }
  284. memcpy(&ret[*num], strings, len);
  285. kfree(strings);
  286. strings = (char *)&ret[*num];
  287. for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
  288. ret[(*num)++] = p;
  289. return ret;
  290. }
  291. char **xenbus_directory(struct xenbus_transaction t,
  292. const char *dir, const char *node, unsigned int *num)
  293. {
  294. char *strings, *path;
  295. unsigned int len;
  296. path = join(dir, node);
  297. if (IS_ERR(path))
  298. return (char **)path;
  299. strings = xs_single(t, XS_DIRECTORY, path, &len);
  300. kfree(path);
  301. if (IS_ERR(strings))
  302. return (char **)strings;
  303. return split(strings, len, num);
  304. }
  305. EXPORT_SYMBOL_GPL(xenbus_directory);
  306. /* Check if a path exists. Return 1 if it does. */
  307. int xenbus_exists(struct xenbus_transaction t,
  308. const char *dir, const char *node)
  309. {
  310. char **d;
  311. int dir_n;
  312. d = xenbus_directory(t, dir, node, &dir_n);
  313. if (IS_ERR(d))
  314. return 0;
  315. kfree(d);
  316. return 1;
  317. }
  318. EXPORT_SYMBOL_GPL(xenbus_exists);
  319. /* Get the value of a single file.
  320. * Returns a kmalloced value: call free() on it after use.
  321. * len indicates length in bytes.
  322. */
  323. void *xenbus_read(struct xenbus_transaction t,
  324. const char *dir, const char *node, unsigned int *len)
  325. {
  326. char *path;
  327. void *ret;
  328. path = join(dir, node);
  329. if (IS_ERR(path))
  330. return (void *)path;
  331. ret = xs_single(t, XS_READ, path, len);
  332. kfree(path);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(xenbus_read);
  336. /* Write the value of a single file.
  337. * Returns -err on failure.
  338. */
  339. int xenbus_write(struct xenbus_transaction t,
  340. const char *dir, const char *node, const char *string)
  341. {
  342. const char *path;
  343. struct kvec iovec[2];
  344. int ret;
  345. path = join(dir, node);
  346. if (IS_ERR(path))
  347. return PTR_ERR(path);
  348. iovec[0].iov_base = (void *)path;
  349. iovec[0].iov_len = strlen(path) + 1;
  350. iovec[1].iov_base = (void *)string;
  351. iovec[1].iov_len = strlen(string);
  352. ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
  353. kfree(path);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL_GPL(xenbus_write);
  357. /* Create a new directory. */
  358. int xenbus_mkdir(struct xenbus_transaction t,
  359. const char *dir, const char *node)
  360. {
  361. char *path;
  362. int ret;
  363. path = join(dir, node);
  364. if (IS_ERR(path))
  365. return PTR_ERR(path);
  366. ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
  367. kfree(path);
  368. return ret;
  369. }
  370. EXPORT_SYMBOL_GPL(xenbus_mkdir);
  371. /* Destroy a file or directory (directories must be empty). */
  372. int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
  373. {
  374. char *path;
  375. int ret;
  376. path = join(dir, node);
  377. if (IS_ERR(path))
  378. return PTR_ERR(path);
  379. ret = xs_error(xs_single(t, XS_RM, path, NULL));
  380. kfree(path);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(xenbus_rm);
  384. /* Start a transaction: changes by others will not be seen during this
  385. * transaction, and changes will not be visible to others until end.
  386. */
  387. int xenbus_transaction_start(struct xenbus_transaction *t)
  388. {
  389. char *id_str;
  390. transaction_start();
  391. id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
  392. if (IS_ERR(id_str)) {
  393. transaction_end();
  394. return PTR_ERR(id_str);
  395. }
  396. t->id = simple_strtoul(id_str, NULL, 0);
  397. kfree(id_str);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(xenbus_transaction_start);
  401. /* End a transaction.
  402. * If abandon is true, transaction is discarded instead of committed.
  403. */
  404. int xenbus_transaction_end(struct xenbus_transaction t, int abort)
  405. {
  406. char abortstr[2];
  407. int err;
  408. if (abort)
  409. strcpy(abortstr, "F");
  410. else
  411. strcpy(abortstr, "T");
  412. err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
  413. transaction_end();
  414. return err;
  415. }
  416. EXPORT_SYMBOL_GPL(xenbus_transaction_end);
  417. /* Single read and scanf: returns -errno or num scanned. */
  418. int xenbus_scanf(struct xenbus_transaction t,
  419. const char *dir, const char *node, const char *fmt, ...)
  420. {
  421. va_list ap;
  422. int ret;
  423. char *val;
  424. val = xenbus_read(t, dir, node, NULL);
  425. if (IS_ERR(val))
  426. return PTR_ERR(val);
  427. va_start(ap, fmt);
  428. ret = vsscanf(val, fmt, ap);
  429. va_end(ap);
  430. kfree(val);
  431. /* Distinctive errno. */
  432. if (ret == 0)
  433. return -ERANGE;
  434. return ret;
  435. }
  436. EXPORT_SYMBOL_GPL(xenbus_scanf);
  437. /* Single printf and write: returns -errno or 0. */
  438. int xenbus_printf(struct xenbus_transaction t,
  439. const char *dir, const char *node, const char *fmt, ...)
  440. {
  441. va_list ap;
  442. int ret;
  443. char *buf;
  444. va_start(ap, fmt);
  445. buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
  446. va_end(ap);
  447. if (!buf)
  448. return -ENOMEM;
  449. ret = xenbus_write(t, dir, node, buf);
  450. kfree(buf);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL_GPL(xenbus_printf);
  454. /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
  455. int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
  456. {
  457. va_list ap;
  458. const char *name;
  459. int ret = 0;
  460. va_start(ap, dir);
  461. while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
  462. const char *fmt = va_arg(ap, char *);
  463. void *result = va_arg(ap, void *);
  464. char *p;
  465. p = xenbus_read(t, dir, name, NULL);
  466. if (IS_ERR(p)) {
  467. ret = PTR_ERR(p);
  468. break;
  469. }
  470. if (fmt) {
  471. if (sscanf(p, fmt, result) == 0)
  472. ret = -EINVAL;
  473. kfree(p);
  474. } else
  475. *(char **)result = p;
  476. }
  477. va_end(ap);
  478. return ret;
  479. }
  480. EXPORT_SYMBOL_GPL(xenbus_gather);
  481. static int xs_watch(const char *path, const char *token)
  482. {
  483. struct kvec iov[2];
  484. iov[0].iov_base = (void *)path;
  485. iov[0].iov_len = strlen(path) + 1;
  486. iov[1].iov_base = (void *)token;
  487. iov[1].iov_len = strlen(token) + 1;
  488. return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
  489. ARRAY_SIZE(iov), NULL));
  490. }
  491. static int xs_unwatch(const char *path, const char *token)
  492. {
  493. struct kvec iov[2];
  494. iov[0].iov_base = (char *)path;
  495. iov[0].iov_len = strlen(path) + 1;
  496. iov[1].iov_base = (char *)token;
  497. iov[1].iov_len = strlen(token) + 1;
  498. return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
  499. ARRAY_SIZE(iov), NULL));
  500. }
  501. static struct xenbus_watch *find_watch(const char *token)
  502. {
  503. struct xenbus_watch *i, *cmp;
  504. cmp = (void *)simple_strtoul(token, NULL, 16);
  505. list_for_each_entry(i, &watches, list)
  506. if (i == cmp)
  507. return i;
  508. return NULL;
  509. }
  510. /*
  511. * Certain older XenBus toolstack cannot handle reading values that are
  512. * not populated. Some Xen 3.4 installation are incapable of doing this
  513. * so if we are running on anything older than 4 do not attempt to read
  514. * control/platform-feature-xs_reset_watches.
  515. */
  516. static bool xen_strict_xenbus_quirk(void)
  517. {
  518. #ifdef CONFIG_X86
  519. uint32_t eax, ebx, ecx, edx, base;
  520. base = xen_cpuid_base();
  521. cpuid(base + 1, &eax, &ebx, &ecx, &edx);
  522. if ((eax >> 16) < 4)
  523. return true;
  524. #endif
  525. return false;
  526. }
  527. static void xs_reset_watches(void)
  528. {
  529. int err, supported = 0;
  530. if (!xen_hvm_domain() || xen_initial_domain())
  531. return;
  532. if (xen_strict_xenbus_quirk())
  533. return;
  534. err = xenbus_scanf(XBT_NIL, "control",
  535. "platform-feature-xs_reset_watches", "%d", &supported);
  536. if (err != 1 || !supported)
  537. return;
  538. err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
  539. if (err && err != -EEXIST)
  540. pr_warn("xs_reset_watches failed: %d\n", err);
  541. }
  542. /* Register callback to watch this node. */
  543. int register_xenbus_watch(struct xenbus_watch *watch)
  544. {
  545. /* Pointer in ascii is the token. */
  546. char token[sizeof(watch) * 2 + 1];
  547. int err;
  548. sprintf(token, "%lX", (long)watch);
  549. down_read(&xs_state.watch_mutex);
  550. spin_lock(&watches_lock);
  551. BUG_ON(find_watch(token));
  552. list_add(&watch->list, &watches);
  553. spin_unlock(&watches_lock);
  554. err = xs_watch(watch->node, token);
  555. if (err) {
  556. spin_lock(&watches_lock);
  557. list_del(&watch->list);
  558. spin_unlock(&watches_lock);
  559. }
  560. up_read(&xs_state.watch_mutex);
  561. return err;
  562. }
  563. EXPORT_SYMBOL_GPL(register_xenbus_watch);
  564. void unregister_xenbus_watch(struct xenbus_watch *watch)
  565. {
  566. struct xs_stored_msg *msg, *tmp;
  567. char token[sizeof(watch) * 2 + 1];
  568. int err;
  569. sprintf(token, "%lX", (long)watch);
  570. down_read(&xs_state.watch_mutex);
  571. spin_lock(&watches_lock);
  572. BUG_ON(!find_watch(token));
  573. list_del(&watch->list);
  574. spin_unlock(&watches_lock);
  575. err = xs_unwatch(watch->node, token);
  576. if (err)
  577. pr_warn("Failed to release watch %s: %i\n", watch->node, err);
  578. up_read(&xs_state.watch_mutex);
  579. /* Make sure there are no callbacks running currently (unless
  580. its us) */
  581. if (current->pid != xenwatch_pid)
  582. mutex_lock(&xenwatch_mutex);
  583. /* Cancel pending watch events. */
  584. spin_lock(&watch_events_lock);
  585. list_for_each_entry_safe(msg, tmp, &watch_events, list) {
  586. if (msg->u.watch.handle != watch)
  587. continue;
  588. list_del(&msg->list);
  589. kfree(msg->u.watch.vec);
  590. kfree(msg);
  591. }
  592. spin_unlock(&watch_events_lock);
  593. if (current->pid != xenwatch_pid)
  594. mutex_unlock(&xenwatch_mutex);
  595. }
  596. EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
  597. void xs_suspend(void)
  598. {
  599. transaction_suspend();
  600. down_write(&xs_state.watch_mutex);
  601. mutex_lock(&xs_state.request_mutex);
  602. mutex_lock(&xs_state.response_mutex);
  603. }
  604. void xs_resume(void)
  605. {
  606. struct xenbus_watch *watch;
  607. char token[sizeof(watch) * 2 + 1];
  608. xb_init_comms();
  609. mutex_unlock(&xs_state.response_mutex);
  610. mutex_unlock(&xs_state.request_mutex);
  611. transaction_resume();
  612. /* No need for watches_lock: the watch_mutex is sufficient. */
  613. list_for_each_entry(watch, &watches, list) {
  614. sprintf(token, "%lX", (long)watch);
  615. xs_watch(watch->node, token);
  616. }
  617. up_write(&xs_state.watch_mutex);
  618. }
  619. void xs_suspend_cancel(void)
  620. {
  621. mutex_unlock(&xs_state.response_mutex);
  622. mutex_unlock(&xs_state.request_mutex);
  623. up_write(&xs_state.watch_mutex);
  624. mutex_unlock(&xs_state.transaction_mutex);
  625. }
  626. static int xenwatch_thread(void *unused)
  627. {
  628. struct list_head *ent;
  629. struct xs_stored_msg *msg;
  630. for (;;) {
  631. wait_event_interruptible(watch_events_waitq,
  632. !list_empty(&watch_events));
  633. if (kthread_should_stop())
  634. break;
  635. mutex_lock(&xenwatch_mutex);
  636. spin_lock(&watch_events_lock);
  637. ent = watch_events.next;
  638. if (ent != &watch_events)
  639. list_del(ent);
  640. spin_unlock(&watch_events_lock);
  641. if (ent != &watch_events) {
  642. msg = list_entry(ent, struct xs_stored_msg, list);
  643. msg->u.watch.handle->callback(
  644. msg->u.watch.handle,
  645. (const char **)msg->u.watch.vec,
  646. msg->u.watch.vec_size);
  647. kfree(msg->u.watch.vec);
  648. kfree(msg);
  649. }
  650. mutex_unlock(&xenwatch_mutex);
  651. }
  652. return 0;
  653. }
  654. static int process_msg(void)
  655. {
  656. struct xs_stored_msg *msg;
  657. char *body;
  658. int err;
  659. /*
  660. * We must disallow save/restore while reading a xenstore message.
  661. * A partial read across s/r leaves us out of sync with xenstored.
  662. */
  663. for (;;) {
  664. err = xb_wait_for_data_to_read();
  665. if (err)
  666. return err;
  667. mutex_lock(&xs_state.response_mutex);
  668. if (xb_data_to_read())
  669. break;
  670. /* We raced with save/restore: pending data 'disappeared'. */
  671. mutex_unlock(&xs_state.response_mutex);
  672. }
  673. msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
  674. if (msg == NULL) {
  675. err = -ENOMEM;
  676. goto out;
  677. }
  678. err = xb_read(&msg->hdr, sizeof(msg->hdr));
  679. if (err) {
  680. kfree(msg);
  681. goto out;
  682. }
  683. if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
  684. kfree(msg);
  685. err = -EINVAL;
  686. goto out;
  687. }
  688. body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
  689. if (body == NULL) {
  690. kfree(msg);
  691. err = -ENOMEM;
  692. goto out;
  693. }
  694. err = xb_read(body, msg->hdr.len);
  695. if (err) {
  696. kfree(body);
  697. kfree(msg);
  698. goto out;
  699. }
  700. body[msg->hdr.len] = '\0';
  701. if (msg->hdr.type == XS_WATCH_EVENT) {
  702. msg->u.watch.vec = split(body, msg->hdr.len,
  703. &msg->u.watch.vec_size);
  704. if (IS_ERR(msg->u.watch.vec)) {
  705. err = PTR_ERR(msg->u.watch.vec);
  706. kfree(msg);
  707. goto out;
  708. }
  709. spin_lock(&watches_lock);
  710. msg->u.watch.handle = find_watch(
  711. msg->u.watch.vec[XS_WATCH_TOKEN]);
  712. if (msg->u.watch.handle != NULL) {
  713. spin_lock(&watch_events_lock);
  714. list_add_tail(&msg->list, &watch_events);
  715. wake_up(&watch_events_waitq);
  716. spin_unlock(&watch_events_lock);
  717. } else {
  718. kfree(msg->u.watch.vec);
  719. kfree(msg);
  720. }
  721. spin_unlock(&watches_lock);
  722. } else {
  723. msg->u.reply.body = body;
  724. spin_lock(&xs_state.reply_lock);
  725. list_add_tail(&msg->list, &xs_state.reply_list);
  726. spin_unlock(&xs_state.reply_lock);
  727. wake_up(&xs_state.reply_waitq);
  728. }
  729. out:
  730. mutex_unlock(&xs_state.response_mutex);
  731. return err;
  732. }
  733. static int xenbus_thread(void *unused)
  734. {
  735. int err;
  736. for (;;) {
  737. err = process_msg();
  738. if (err)
  739. pr_warn("error %d while reading message\n", err);
  740. if (kthread_should_stop())
  741. break;
  742. }
  743. return 0;
  744. }
  745. int xs_init(void)
  746. {
  747. int err;
  748. struct task_struct *task;
  749. INIT_LIST_HEAD(&xs_state.reply_list);
  750. spin_lock_init(&xs_state.reply_lock);
  751. init_waitqueue_head(&xs_state.reply_waitq);
  752. mutex_init(&xs_state.request_mutex);
  753. mutex_init(&xs_state.response_mutex);
  754. mutex_init(&xs_state.transaction_mutex);
  755. init_rwsem(&xs_state.watch_mutex);
  756. atomic_set(&xs_state.transaction_count, 0);
  757. init_waitqueue_head(&xs_state.transaction_wq);
  758. /* Initialize the shared memory rings to talk to xenstored */
  759. err = xb_init_comms();
  760. if (err)
  761. return err;
  762. task = kthread_run(xenwatch_thread, NULL, "xenwatch");
  763. if (IS_ERR(task))
  764. return PTR_ERR(task);
  765. xenwatch_pid = task->pid;
  766. task = kthread_run(xenbus_thread, NULL, "xenbus");
  767. if (IS_ERR(task))
  768. return PTR_ERR(task);
  769. /* shutdown watches for kexec boot */
  770. xs_reset_watches();
  771. return 0;
  772. }