xenbus_dev_frontend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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/sched.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/mount.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/init.h>
  51. #include <linux/namei.h>
  52. #include <linux/string.h>
  53. #include <linux/slab.h>
  54. #include <linux/miscdevice.h>
  55. #include <linux/module.h>
  56. #include "xenbus_comms.h"
  57. #include <xen/xenbus.h>
  58. #include <asm/xen/hypervisor.h>
  59. MODULE_LICENSE("GPL");
  60. /*
  61. * An element of a list of outstanding transactions, for which we're
  62. * still waiting a reply.
  63. */
  64. struct xenbus_transaction_holder {
  65. struct list_head list;
  66. struct xenbus_transaction handle;
  67. };
  68. /*
  69. * A buffer of data on the queue.
  70. */
  71. struct read_buffer {
  72. struct list_head list;
  73. unsigned int cons;
  74. unsigned int len;
  75. char msg[];
  76. };
  77. struct xenbus_file_priv {
  78. /*
  79. * msgbuffer_mutex is held while partial requests are built up
  80. * and complete requests are acted on. It therefore protects
  81. * the "transactions" and "watches" lists, and the partial
  82. * request length and buffer.
  83. *
  84. * reply_mutex protects the reply being built up to return to
  85. * usermode. It nests inside msgbuffer_mutex but may be held
  86. * alone during a watch callback.
  87. */
  88. struct mutex msgbuffer_mutex;
  89. /* In-progress transactions */
  90. struct list_head transactions;
  91. /* Active watches. */
  92. struct list_head watches;
  93. /* Partial request. */
  94. unsigned int len;
  95. union {
  96. struct xsd_sockmsg msg;
  97. char buffer[PAGE_SIZE];
  98. } u;
  99. /* Response queue. */
  100. struct mutex reply_mutex;
  101. struct list_head read_buffers;
  102. wait_queue_head_t read_waitq;
  103. };
  104. /* Read out any raw xenbus messages queued up. */
  105. static ssize_t xenbus_file_read(struct file *filp,
  106. char __user *ubuf,
  107. size_t len, loff_t *ppos)
  108. {
  109. struct xenbus_file_priv *u = filp->private_data;
  110. struct read_buffer *rb;
  111. unsigned i;
  112. int ret;
  113. mutex_lock(&u->reply_mutex);
  114. again:
  115. while (list_empty(&u->read_buffers)) {
  116. mutex_unlock(&u->reply_mutex);
  117. if (filp->f_flags & O_NONBLOCK)
  118. return -EAGAIN;
  119. ret = wait_event_interruptible(u->read_waitq,
  120. !list_empty(&u->read_buffers));
  121. if (ret)
  122. return ret;
  123. mutex_lock(&u->reply_mutex);
  124. }
  125. rb = list_entry(u->read_buffers.next, struct read_buffer, list);
  126. i = 0;
  127. while (i < len) {
  128. unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
  129. ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
  130. i += sz - ret;
  131. rb->cons += sz - ret;
  132. if (ret != 0) {
  133. if (i == 0)
  134. i = -EFAULT;
  135. goto out;
  136. }
  137. /* Clear out buffer if it has been consumed */
  138. if (rb->cons == rb->len) {
  139. list_del(&rb->list);
  140. kfree(rb);
  141. if (list_empty(&u->read_buffers))
  142. break;
  143. rb = list_entry(u->read_buffers.next,
  144. struct read_buffer, list);
  145. }
  146. }
  147. if (i == 0)
  148. goto again;
  149. out:
  150. mutex_unlock(&u->reply_mutex);
  151. return i;
  152. }
  153. /*
  154. * Add a buffer to the queue. Caller must hold the appropriate lock
  155. * if the queue is not local. (Commonly the caller will build up
  156. * multiple queued buffers on a temporary local list, and then add it
  157. * to the appropriate list under lock once all the buffers have een
  158. * successfully allocated.)
  159. */
  160. static int queue_reply(struct list_head *queue, const void *data, size_t len)
  161. {
  162. struct read_buffer *rb;
  163. if (len == 0)
  164. return 0;
  165. rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
  166. if (rb == NULL)
  167. return -ENOMEM;
  168. rb->cons = 0;
  169. rb->len = len;
  170. memcpy(rb->msg, data, len);
  171. list_add_tail(&rb->list, queue);
  172. return 0;
  173. }
  174. /*
  175. * Free all the read_buffer s on a list.
  176. * Caller must have sole reference to list.
  177. */
  178. static void queue_cleanup(struct list_head *list)
  179. {
  180. struct read_buffer *rb;
  181. while (!list_empty(list)) {
  182. rb = list_entry(list->next, struct read_buffer, list);
  183. list_del(list->next);
  184. kfree(rb);
  185. }
  186. }
  187. struct watch_adapter {
  188. struct list_head list;
  189. struct xenbus_watch watch;
  190. struct xenbus_file_priv *dev_data;
  191. char *token;
  192. };
  193. static void free_watch_adapter(struct watch_adapter *watch)
  194. {
  195. kfree(watch->watch.node);
  196. kfree(watch->token);
  197. kfree(watch);
  198. }
  199. static struct watch_adapter *alloc_watch_adapter(const char *path,
  200. const char *token)
  201. {
  202. struct watch_adapter *watch;
  203. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  204. if (watch == NULL)
  205. goto out_fail;
  206. watch->watch.node = kstrdup(path, GFP_KERNEL);
  207. if (watch->watch.node == NULL)
  208. goto out_free;
  209. watch->token = kstrdup(token, GFP_KERNEL);
  210. if (watch->token == NULL)
  211. goto out_free;
  212. return watch;
  213. out_free:
  214. free_watch_adapter(watch);
  215. out_fail:
  216. return NULL;
  217. }
  218. static void watch_fired(struct xenbus_watch *watch,
  219. const char **vec,
  220. unsigned int len)
  221. {
  222. struct watch_adapter *adap;
  223. struct xsd_sockmsg hdr;
  224. const char *path, *token;
  225. int path_len, tok_len, body_len, data_len = 0;
  226. int ret;
  227. LIST_HEAD(staging_q);
  228. adap = container_of(watch, struct watch_adapter, watch);
  229. path = vec[XS_WATCH_PATH];
  230. token = adap->token;
  231. path_len = strlen(path) + 1;
  232. tok_len = strlen(token) + 1;
  233. if (len > 2)
  234. data_len = vec[len] - vec[2] + 1;
  235. body_len = path_len + tok_len + data_len;
  236. hdr.type = XS_WATCH_EVENT;
  237. hdr.len = body_len;
  238. mutex_lock(&adap->dev_data->reply_mutex);
  239. ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
  240. if (!ret)
  241. ret = queue_reply(&staging_q, path, path_len);
  242. if (!ret)
  243. ret = queue_reply(&staging_q, token, tok_len);
  244. if (!ret && len > 2)
  245. ret = queue_reply(&staging_q, vec[2], data_len);
  246. if (!ret) {
  247. /* success: pass reply list onto watcher */
  248. list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
  249. wake_up(&adap->dev_data->read_waitq);
  250. } else
  251. queue_cleanup(&staging_q);
  252. mutex_unlock(&adap->dev_data->reply_mutex);
  253. }
  254. static int xenbus_write_transaction(unsigned msg_type,
  255. struct xenbus_file_priv *u)
  256. {
  257. int rc;
  258. void *reply;
  259. struct xenbus_transaction_holder *trans = NULL;
  260. LIST_HEAD(staging_q);
  261. if (msg_type == XS_TRANSACTION_START) {
  262. trans = kmalloc(sizeof(*trans), GFP_KERNEL);
  263. if (!trans) {
  264. rc = -ENOMEM;
  265. goto out;
  266. }
  267. }
  268. reply = xenbus_dev_request_and_reply(&u->u.msg);
  269. if (IS_ERR(reply)) {
  270. kfree(trans);
  271. rc = PTR_ERR(reply);
  272. goto out;
  273. }
  274. if (msg_type == XS_TRANSACTION_START) {
  275. trans->handle.id = simple_strtoul(reply, NULL, 0);
  276. list_add(&trans->list, &u->transactions);
  277. } else if (msg_type == XS_TRANSACTION_END) {
  278. list_for_each_entry(trans, &u->transactions, list)
  279. if (trans->handle.id == u->u.msg.tx_id)
  280. break;
  281. BUG_ON(&trans->list == &u->transactions);
  282. list_del(&trans->list);
  283. kfree(trans);
  284. }
  285. mutex_lock(&u->reply_mutex);
  286. rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
  287. if (!rc)
  288. rc = queue_reply(&staging_q, reply, u->u.msg.len);
  289. if (!rc) {
  290. list_splice_tail(&staging_q, &u->read_buffers);
  291. wake_up(&u->read_waitq);
  292. } else {
  293. queue_cleanup(&staging_q);
  294. }
  295. mutex_unlock(&u->reply_mutex);
  296. kfree(reply);
  297. out:
  298. return rc;
  299. }
  300. static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
  301. {
  302. struct watch_adapter *watch, *tmp_watch;
  303. char *path, *token;
  304. int err, rc;
  305. LIST_HEAD(staging_q);
  306. path = u->u.buffer + sizeof(u->u.msg);
  307. token = memchr(path, 0, u->u.msg.len);
  308. if (token == NULL) {
  309. rc = -EILSEQ;
  310. goto out;
  311. }
  312. token++;
  313. if (msg_type == XS_WATCH) {
  314. watch = alloc_watch_adapter(path, token);
  315. if (watch == NULL) {
  316. rc = -ENOMEM;
  317. goto out;
  318. }
  319. watch->watch.callback = watch_fired;
  320. watch->dev_data = u;
  321. err = register_xenbus_watch(&watch->watch);
  322. if (err) {
  323. free_watch_adapter(watch);
  324. rc = err;
  325. goto out;
  326. }
  327. list_add(&watch->list, &u->watches);
  328. } else {
  329. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  330. if (!strcmp(watch->token, token) &&
  331. !strcmp(watch->watch.node, path)) {
  332. unregister_xenbus_watch(&watch->watch);
  333. list_del(&watch->list);
  334. free_watch_adapter(watch);
  335. break;
  336. }
  337. }
  338. }
  339. /* Success. Synthesize a reply to say all is OK. */
  340. {
  341. struct {
  342. struct xsd_sockmsg hdr;
  343. char body[3];
  344. } __packed reply = {
  345. {
  346. .type = msg_type,
  347. .len = sizeof(reply.body)
  348. },
  349. "OK"
  350. };
  351. mutex_lock(&u->reply_mutex);
  352. rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
  353. wake_up(&u->read_waitq);
  354. mutex_unlock(&u->reply_mutex);
  355. }
  356. out:
  357. return rc;
  358. }
  359. static ssize_t xenbus_file_write(struct file *filp,
  360. const char __user *ubuf,
  361. size_t len, loff_t *ppos)
  362. {
  363. struct xenbus_file_priv *u = filp->private_data;
  364. uint32_t msg_type;
  365. int rc = len;
  366. int ret;
  367. LIST_HEAD(staging_q);
  368. /*
  369. * We're expecting usermode to be writing properly formed
  370. * xenbus messages. If they write an incomplete message we
  371. * buffer it up. Once it is complete, we act on it.
  372. */
  373. /*
  374. * Make sure concurrent writers can't stomp all over each
  375. * other's messages and make a mess of our partial message
  376. * buffer. We don't make any attemppt to stop multiple
  377. * writers from making a mess of each other's incomplete
  378. * messages; we're just trying to guarantee our own internal
  379. * consistency and make sure that single writes are handled
  380. * atomically.
  381. */
  382. mutex_lock(&u->msgbuffer_mutex);
  383. /* Get this out of the way early to avoid confusion */
  384. if (len == 0)
  385. goto out;
  386. /* Can't write a xenbus message larger we can buffer */
  387. if ((len + u->len) > sizeof(u->u.buffer)) {
  388. /* On error, dump existing buffer */
  389. u->len = 0;
  390. rc = -EINVAL;
  391. goto out;
  392. }
  393. ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
  394. if (ret != 0) {
  395. rc = -EFAULT;
  396. goto out;
  397. }
  398. /* Deal with a partial copy. */
  399. len -= ret;
  400. rc = len;
  401. u->len += len;
  402. /* Return if we haven't got a full message yet */
  403. if (u->len < sizeof(u->u.msg))
  404. goto out; /* not even the header yet */
  405. /* If we're expecting a message that's larger than we can
  406. possibly send, dump what we have and return an error. */
  407. if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
  408. rc = -E2BIG;
  409. u->len = 0;
  410. goto out;
  411. }
  412. if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
  413. goto out; /* incomplete data portion */
  414. /*
  415. * OK, now we have a complete message. Do something with it.
  416. */
  417. msg_type = u->u.msg.type;
  418. switch (msg_type) {
  419. case XS_WATCH:
  420. case XS_UNWATCH:
  421. /* (Un)Ask for some path to be watched for changes */
  422. ret = xenbus_write_watch(msg_type, u);
  423. break;
  424. default:
  425. /* Send out a transaction */
  426. ret = xenbus_write_transaction(msg_type, u);
  427. break;
  428. }
  429. if (ret != 0)
  430. rc = ret;
  431. /* Buffered message consumed */
  432. u->len = 0;
  433. out:
  434. mutex_unlock(&u->msgbuffer_mutex);
  435. return rc;
  436. }
  437. static int xenbus_file_open(struct inode *inode, struct file *filp)
  438. {
  439. struct xenbus_file_priv *u;
  440. if (xen_store_evtchn == 0)
  441. return -ENOENT;
  442. nonseekable_open(inode, filp);
  443. u = kzalloc(sizeof(*u), GFP_KERNEL);
  444. if (u == NULL)
  445. return -ENOMEM;
  446. INIT_LIST_HEAD(&u->transactions);
  447. INIT_LIST_HEAD(&u->watches);
  448. INIT_LIST_HEAD(&u->read_buffers);
  449. init_waitqueue_head(&u->read_waitq);
  450. mutex_init(&u->reply_mutex);
  451. mutex_init(&u->msgbuffer_mutex);
  452. filp->private_data = u;
  453. return 0;
  454. }
  455. static int xenbus_file_release(struct inode *inode, struct file *filp)
  456. {
  457. struct xenbus_file_priv *u = filp->private_data;
  458. struct xenbus_transaction_holder *trans, *tmp;
  459. struct watch_adapter *watch, *tmp_watch;
  460. struct read_buffer *rb, *tmp_rb;
  461. /*
  462. * No need for locking here because there are no other users,
  463. * by definition.
  464. */
  465. list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
  466. xenbus_transaction_end(trans->handle, 1);
  467. list_del(&trans->list);
  468. kfree(trans);
  469. }
  470. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  471. unregister_xenbus_watch(&watch->watch);
  472. list_del(&watch->list);
  473. free_watch_adapter(watch);
  474. }
  475. list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
  476. list_del(&rb->list);
  477. kfree(rb);
  478. }
  479. kfree(u);
  480. return 0;
  481. }
  482. static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
  483. {
  484. struct xenbus_file_priv *u = file->private_data;
  485. poll_wait(file, &u->read_waitq, wait);
  486. if (!list_empty(&u->read_buffers))
  487. return POLLIN | POLLRDNORM;
  488. return 0;
  489. }
  490. const struct file_operations xen_xenbus_fops = {
  491. .read = xenbus_file_read,
  492. .write = xenbus_file_write,
  493. .open = xenbus_file_open,
  494. .release = xenbus_file_release,
  495. .poll = xenbus_file_poll,
  496. .llseek = no_llseek,
  497. };
  498. EXPORT_SYMBOL_GPL(xen_xenbus_fops);
  499. static struct miscdevice xenbus_dev = {
  500. .minor = MISC_DYNAMIC_MINOR,
  501. .name = "xen/xenbus",
  502. .fops = &xen_xenbus_fops,
  503. };
  504. static int __init xenbus_init(void)
  505. {
  506. int err;
  507. if (!xen_domain())
  508. return -ENODEV;
  509. err = misc_register(&xenbus_dev);
  510. if (err)
  511. printk(KERN_ERR "Could not register xenbus frontend device\n");
  512. return err;
  513. }
  514. static void __exit xenbus_exit(void)
  515. {
  516. misc_deregister(&xenbus_dev);
  517. }
  518. module_init(xenbus_init);
  519. module_exit(xenbus_exit);