xenbus_xs.c 22 KB

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