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