xenbus_dev_frontend.c 15 KB

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