xenbus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Driver giving user-space access to the kernel's xenbus connection
  3. * to xenstore.
  4. *
  5. * Copyright (c) 2005, Christian Limpach
  6. * Copyright (c) 2005, Rusty Russell, IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. *
  32. * Changes:
  33. * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
  34. * and /proc/xen compatibility mount point.
  35. * Turned xenfs into a loadable module.
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/errno.h>
  39. #include <linux/uio.h>
  40. #include <linux/notifier.h>
  41. #include <linux/wait.h>
  42. #include <linux/fs.h>
  43. #include <linux/poll.h>
  44. #include <linux/mutex.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/mount.h>
  47. #include <linux/pagemap.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/init.h>
  50. #include <linux/namei.h>
  51. #include <linux/string.h>
  52. #include "xenfs.h"
  53. #include "../xenbus/xenbus_comms.h"
  54. #include <xen/xenbus.h>
  55. #include <asm/xen/hypervisor.h>
  56. /*
  57. * An element of a list of outstanding transactions, for which we're
  58. * still waiting a reply.
  59. */
  60. struct xenbus_transaction_holder {
  61. struct list_head list;
  62. struct xenbus_transaction handle;
  63. };
  64. /*
  65. * A buffer of data on the queue.
  66. */
  67. struct read_buffer {
  68. struct list_head list;
  69. unsigned int cons;
  70. unsigned int len;
  71. char msg[];
  72. };
  73. struct xenbus_file_priv {
  74. /*
  75. * msgbuffer_mutex is held while partial requests are built up
  76. * and complete requests are acted on. It therefore protects
  77. * the "transactions" and "watches" lists, and the partial
  78. * request length and buffer.
  79. *
  80. * reply_mutex protects the reply being built up to return to
  81. * usermode. It nests inside msgbuffer_mutex but may be held
  82. * alone during a watch callback.
  83. */
  84. struct mutex msgbuffer_mutex;
  85. /* In-progress transactions */
  86. struct list_head transactions;
  87. /* Active watches. */
  88. struct list_head watches;
  89. /* Partial request. */
  90. unsigned int len;
  91. union {
  92. struct xsd_sockmsg msg;
  93. char buffer[PAGE_SIZE];
  94. } u;
  95. /* Response queue. */
  96. struct mutex reply_mutex;
  97. struct list_head read_buffers;
  98. wait_queue_head_t read_waitq;
  99. };
  100. /* Read out any raw xenbus messages queued up. */
  101. static ssize_t xenbus_file_read(struct file *filp,
  102. char __user *ubuf,
  103. size_t len, loff_t *ppos)
  104. {
  105. struct xenbus_file_priv *u = filp->private_data;
  106. struct read_buffer *rb;
  107. unsigned i;
  108. int ret;
  109. mutex_lock(&u->reply_mutex);
  110. again:
  111. while (list_empty(&u->read_buffers)) {
  112. mutex_unlock(&u->reply_mutex);
  113. if (filp->f_flags & O_NONBLOCK)
  114. return -EAGAIN;
  115. ret = wait_event_interruptible(u->read_waitq,
  116. !list_empty(&u->read_buffers));
  117. if (ret)
  118. return ret;
  119. mutex_lock(&u->reply_mutex);
  120. }
  121. rb = list_entry(u->read_buffers.next, struct read_buffer, list);
  122. i = 0;
  123. while (i < len) {
  124. unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
  125. ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
  126. i += sz - ret;
  127. rb->cons += sz - ret;
  128. if (ret != 0) {
  129. if (i == 0)
  130. i = -EFAULT;
  131. goto out;
  132. }
  133. /* Clear out buffer if it has been consumed */
  134. if (rb->cons == rb->len) {
  135. list_del(&rb->list);
  136. kfree(rb);
  137. if (list_empty(&u->read_buffers))
  138. break;
  139. rb = list_entry(u->read_buffers.next,
  140. struct read_buffer, list);
  141. }
  142. }
  143. if (i == 0)
  144. goto again;
  145. out:
  146. mutex_unlock(&u->reply_mutex);
  147. return i;
  148. }
  149. /*
  150. * Add a buffer to the queue. Caller must hold the appropriate lock
  151. * if the queue is not local. (Commonly the caller will build up
  152. * multiple queued buffers on a temporary local list, and then add it
  153. * to the appropriate list under lock once all the buffers have een
  154. * successfully allocated.)
  155. */
  156. static int queue_reply(struct list_head *queue, const void *data, size_t len)
  157. {
  158. struct read_buffer *rb;
  159. if (len == 0)
  160. return 0;
  161. rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
  162. if (rb == NULL)
  163. return -ENOMEM;
  164. rb->cons = 0;
  165. rb->len = len;
  166. memcpy(rb->msg, data, len);
  167. list_add_tail(&rb->list, queue);
  168. return 0;
  169. }
  170. /*
  171. * Free all the read_buffer s on a list.
  172. * Caller must have sole reference to list.
  173. */
  174. static void queue_cleanup(struct list_head *list)
  175. {
  176. struct read_buffer *rb;
  177. while (!list_empty(list)) {
  178. rb = list_entry(list->next, struct read_buffer, list);
  179. list_del(list->next);
  180. kfree(rb);
  181. }
  182. }
  183. struct watch_adapter {
  184. struct list_head list;
  185. struct xenbus_watch watch;
  186. struct xenbus_file_priv *dev_data;
  187. char *token;
  188. };
  189. static void free_watch_adapter(struct watch_adapter *watch)
  190. {
  191. kfree(watch->watch.node);
  192. kfree(watch->token);
  193. kfree(watch);
  194. }
  195. static struct watch_adapter *alloc_watch_adapter(const char *path,
  196. const char *token)
  197. {
  198. struct watch_adapter *watch;
  199. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  200. if (watch == NULL)
  201. goto out_fail;
  202. watch->watch.node = kstrdup(path, GFP_KERNEL);
  203. if (watch->watch.node == NULL)
  204. goto out_free;
  205. watch->token = kstrdup(token, GFP_KERNEL);
  206. if (watch->token == NULL)
  207. goto out_free;
  208. return watch;
  209. out_free:
  210. free_watch_adapter(watch);
  211. out_fail:
  212. return NULL;
  213. }
  214. static void watch_fired(struct xenbus_watch *watch,
  215. const char **vec,
  216. unsigned int len)
  217. {
  218. struct watch_adapter *adap;
  219. struct xsd_sockmsg hdr;
  220. const char *path, *token;
  221. int path_len, tok_len, body_len, data_len = 0;
  222. int ret;
  223. LIST_HEAD(staging_q);
  224. adap = container_of(watch, struct watch_adapter, watch);
  225. path = vec[XS_WATCH_PATH];
  226. token = adap->token;
  227. path_len = strlen(path) + 1;
  228. tok_len = strlen(token) + 1;
  229. if (len > 2)
  230. data_len = vec[len] - vec[2] + 1;
  231. body_len = path_len + tok_len + data_len;
  232. hdr.type = XS_WATCH_EVENT;
  233. hdr.len = body_len;
  234. mutex_lock(&adap->dev_data->reply_mutex);
  235. ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
  236. if (!ret)
  237. ret = queue_reply(&staging_q, path, path_len);
  238. if (!ret)
  239. ret = queue_reply(&staging_q, token, tok_len);
  240. if (!ret && len > 2)
  241. ret = queue_reply(&staging_q, vec[2], data_len);
  242. if (!ret) {
  243. /* success: pass reply list onto watcher */
  244. list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
  245. wake_up(&adap->dev_data->read_waitq);
  246. } else
  247. queue_cleanup(&staging_q);
  248. mutex_unlock(&adap->dev_data->reply_mutex);
  249. }
  250. static int xenbus_write_transaction(unsigned msg_type,
  251. struct xenbus_file_priv *u)
  252. {
  253. int rc;
  254. void *reply;
  255. struct xenbus_transaction_holder *trans = NULL;
  256. LIST_HEAD(staging_q);
  257. if (msg_type == XS_TRANSACTION_START) {
  258. trans = kmalloc(sizeof(*trans), GFP_KERNEL);
  259. if (!trans) {
  260. rc = -ENOMEM;
  261. goto out;
  262. }
  263. }
  264. reply = xenbus_dev_request_and_reply(&u->u.msg);
  265. if (IS_ERR(reply)) {
  266. kfree(trans);
  267. rc = PTR_ERR(reply);
  268. goto out;
  269. }
  270. if (msg_type == XS_TRANSACTION_START) {
  271. trans->handle.id = simple_strtoul(reply, NULL, 0);
  272. list_add(&trans->list, &u->transactions);
  273. } else if (msg_type == XS_TRANSACTION_END) {
  274. list_for_each_entry(trans, &u->transactions, list)
  275. if (trans->handle.id == u->u.msg.tx_id)
  276. break;
  277. BUG_ON(&trans->list == &u->transactions);
  278. list_del(&trans->list);
  279. kfree(trans);
  280. }
  281. mutex_lock(&u->reply_mutex);
  282. rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
  283. if (!rc)
  284. rc = queue_reply(&staging_q, reply, u->u.msg.len);
  285. if (!rc) {
  286. list_splice_tail(&staging_q, &u->read_buffers);
  287. wake_up(&u->read_waitq);
  288. } else {
  289. queue_cleanup(&staging_q);
  290. }
  291. mutex_unlock(&u->reply_mutex);
  292. kfree(reply);
  293. out:
  294. return rc;
  295. }
  296. static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
  297. {
  298. struct watch_adapter *watch, *tmp_watch;
  299. char *path, *token;
  300. int err, rc;
  301. LIST_HEAD(staging_q);
  302. path = u->u.buffer + sizeof(u->u.msg);
  303. token = memchr(path, 0, u->u.msg.len);
  304. if (token == NULL) {
  305. rc = -EILSEQ;
  306. goto out;
  307. }
  308. token++;
  309. if (msg_type == XS_WATCH) {
  310. watch = alloc_watch_adapter(path, token);
  311. if (watch == NULL) {
  312. rc = -ENOMEM;
  313. goto out;
  314. }
  315. watch->watch.callback = watch_fired;
  316. watch->dev_data = u;
  317. err = register_xenbus_watch(&watch->watch);
  318. if (err) {
  319. free_watch_adapter(watch);
  320. rc = err;
  321. goto out;
  322. }
  323. list_add(&watch->list, &u->watches);
  324. } else {
  325. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  326. if (!strcmp(watch->token, token) &&
  327. !strcmp(watch->watch.node, path)) {
  328. unregister_xenbus_watch(&watch->watch);
  329. list_del(&watch->list);
  330. free_watch_adapter(watch);
  331. break;
  332. }
  333. }
  334. }
  335. /* Success. Synthesize a reply to say all is OK. */
  336. {
  337. struct {
  338. struct xsd_sockmsg hdr;
  339. char body[3];
  340. } __packed reply = {
  341. {
  342. .type = msg_type,
  343. .len = sizeof(reply.body)
  344. },
  345. "OK"
  346. };
  347. mutex_lock(&u->reply_mutex);
  348. rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
  349. wake_up(&u->read_waitq);
  350. mutex_unlock(&u->reply_mutex);
  351. }
  352. out:
  353. return rc;
  354. }
  355. static ssize_t xenbus_file_write(struct file *filp,
  356. const char __user *ubuf,
  357. size_t len, loff_t *ppos)
  358. {
  359. struct xenbus_file_priv *u = filp->private_data;
  360. uint32_t msg_type;
  361. int rc = len;
  362. int ret;
  363. LIST_HEAD(staging_q);
  364. /*
  365. * We're expecting usermode to be writing properly formed
  366. * xenbus messages. If they write an incomplete message we
  367. * buffer it up. Once it is complete, we act on it.
  368. */
  369. /*
  370. * Make sure concurrent writers can't stomp all over each
  371. * other's messages and make a mess of our partial message
  372. * buffer. We don't make any attemppt to stop multiple
  373. * writers from making a mess of each other's incomplete
  374. * messages; we're just trying to guarantee our own internal
  375. * consistency and make sure that single writes are handled
  376. * atomically.
  377. */
  378. mutex_lock(&u->msgbuffer_mutex);
  379. /* Get this out of the way early to avoid confusion */
  380. if (len == 0)
  381. goto out;
  382. /* Can't write a xenbus message larger we can buffer */
  383. if ((len + u->len) > sizeof(u->u.buffer)) {
  384. /* On error, dump existing buffer */
  385. u->len = 0;
  386. rc = -EINVAL;
  387. goto out;
  388. }
  389. ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
  390. if (ret != 0) {
  391. rc = -EFAULT;
  392. goto out;
  393. }
  394. /* Deal with a partial copy. */
  395. len -= ret;
  396. rc = len;
  397. u->len += len;
  398. /* Return if we haven't got a full message yet */
  399. if (u->len < sizeof(u->u.msg))
  400. goto out; /* not even the header yet */
  401. /* If we're expecting a message that's larger than we can
  402. possibly send, dump what we have and return an error. */
  403. if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
  404. rc = -E2BIG;
  405. u->len = 0;
  406. goto out;
  407. }
  408. if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
  409. goto out; /* incomplete data portion */
  410. /*
  411. * OK, now we have a complete message. Do something with it.
  412. */
  413. msg_type = u->u.msg.type;
  414. switch (msg_type) {
  415. case XS_WATCH:
  416. case XS_UNWATCH:
  417. /* (Un)Ask for some path to be watched for changes */
  418. ret = xenbus_write_watch(msg_type, u);
  419. break;
  420. default:
  421. /* Send out a transaction */
  422. ret = xenbus_write_transaction(msg_type, u);
  423. break;
  424. }
  425. if (ret != 0)
  426. rc = ret;
  427. /* Buffered message consumed */
  428. u->len = 0;
  429. out:
  430. mutex_unlock(&u->msgbuffer_mutex);
  431. return rc;
  432. }
  433. static int xenbus_file_open(struct inode *inode, struct file *filp)
  434. {
  435. struct xenbus_file_priv *u;
  436. if (xen_store_evtchn == 0)
  437. return -ENOENT;
  438. nonseekable_open(inode, filp);
  439. u = kzalloc(sizeof(*u), GFP_KERNEL);
  440. if (u == NULL)
  441. return -ENOMEM;
  442. INIT_LIST_HEAD(&u->transactions);
  443. INIT_LIST_HEAD(&u->watches);
  444. INIT_LIST_HEAD(&u->read_buffers);
  445. init_waitqueue_head(&u->read_waitq);
  446. mutex_init(&u->reply_mutex);
  447. mutex_init(&u->msgbuffer_mutex);
  448. filp->private_data = u;
  449. return 0;
  450. }
  451. static int xenbus_file_release(struct inode *inode, struct file *filp)
  452. {
  453. struct xenbus_file_priv *u = filp->private_data;
  454. struct xenbus_transaction_holder *trans, *tmp;
  455. struct watch_adapter *watch, *tmp_watch;
  456. struct read_buffer *rb, *tmp_rb;
  457. /*
  458. * No need for locking here because there are no other users,
  459. * by definition.
  460. */
  461. list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
  462. xenbus_transaction_end(trans->handle, 1);
  463. list_del(&trans->list);
  464. kfree(trans);
  465. }
  466. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  467. unregister_xenbus_watch(&watch->watch);
  468. list_del(&watch->list);
  469. free_watch_adapter(watch);
  470. }
  471. list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
  472. list_del(&rb->list);
  473. kfree(rb);
  474. }
  475. kfree(u);
  476. return 0;
  477. }
  478. static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
  479. {
  480. struct xenbus_file_priv *u = file->private_data;
  481. poll_wait(file, &u->read_waitq, wait);
  482. if (!list_empty(&u->read_buffers))
  483. return POLLIN | POLLRDNORM;
  484. return 0;
  485. }
  486. const struct file_operations xenbus_file_ops = {
  487. .read = xenbus_file_read,
  488. .write = xenbus_file_write,
  489. .open = xenbus_file_open,
  490. .release = xenbus_file_release,
  491. .poll = xenbus_file_poll,
  492. };