xenbus_xs.c 22 KB

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