xenbus_xs.c 21 KB

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