xenbus_dev_frontend.c 14 KB

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