xenbus_xs.c 22 KB

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