xenbus_dev_frontend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. }
  270. reply = xenbus_dev_request_and_reply(&u->u.msg);
  271. if (IS_ERR(reply)) {
  272. kfree(trans);
  273. rc = PTR_ERR(reply);
  274. goto out;
  275. }
  276. if (msg_type == XS_TRANSACTION_START) {
  277. if (u->u.msg.type == XS_ERROR)
  278. kfree(trans);
  279. else {
  280. trans->handle.id = simple_strtoul(reply, NULL, 0);
  281. list_add(&trans->list, &u->transactions);
  282. }
  283. } else if (u->u.msg.type == XS_TRANSACTION_END) {
  284. list_for_each_entry(trans, &u->transactions, list)
  285. if (trans->handle.id == u->u.msg.tx_id)
  286. break;
  287. BUG_ON(&trans->list == &u->transactions);
  288. list_del(&trans->list);
  289. kfree(trans);
  290. }
  291. mutex_lock(&u->reply_mutex);
  292. rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
  293. if (!rc)
  294. rc = queue_reply(&staging_q, reply, u->u.msg.len);
  295. if (!rc) {
  296. list_splice_tail(&staging_q, &u->read_buffers);
  297. wake_up(&u->read_waitq);
  298. } else {
  299. queue_cleanup(&staging_q);
  300. }
  301. mutex_unlock(&u->reply_mutex);
  302. kfree(reply);
  303. out:
  304. return rc;
  305. }
  306. static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
  307. {
  308. struct watch_adapter *watch, *tmp_watch;
  309. char *path, *token;
  310. int err, rc;
  311. LIST_HEAD(staging_q);
  312. path = u->u.buffer + sizeof(u->u.msg);
  313. token = memchr(path, 0, u->u.msg.len);
  314. if (token == NULL) {
  315. rc = -EILSEQ;
  316. goto out;
  317. }
  318. token++;
  319. if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
  320. rc = -EILSEQ;
  321. goto out;
  322. }
  323. if (msg_type == XS_WATCH) {
  324. watch = alloc_watch_adapter(path, token);
  325. if (watch == NULL) {
  326. rc = -ENOMEM;
  327. goto out;
  328. }
  329. watch->watch.callback = watch_fired;
  330. watch->dev_data = u;
  331. err = register_xenbus_watch(&watch->watch);
  332. if (err) {
  333. free_watch_adapter(watch);
  334. rc = err;
  335. goto out;
  336. }
  337. list_add(&watch->list, &u->watches);
  338. } else {
  339. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  340. if (!strcmp(watch->token, token) &&
  341. !strcmp(watch->watch.node, path)) {
  342. unregister_xenbus_watch(&watch->watch);
  343. list_del(&watch->list);
  344. free_watch_adapter(watch);
  345. break;
  346. }
  347. }
  348. }
  349. /* Success. Synthesize a reply to say all is OK. */
  350. {
  351. struct {
  352. struct xsd_sockmsg hdr;
  353. char body[3];
  354. } __packed reply = {
  355. {
  356. .type = msg_type,
  357. .len = sizeof(reply.body)
  358. },
  359. "OK"
  360. };
  361. mutex_lock(&u->reply_mutex);
  362. rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
  363. wake_up(&u->read_waitq);
  364. mutex_unlock(&u->reply_mutex);
  365. }
  366. out:
  367. return rc;
  368. }
  369. static ssize_t xenbus_file_write(struct file *filp,
  370. const char __user *ubuf,
  371. size_t len, loff_t *ppos)
  372. {
  373. struct xenbus_file_priv *u = filp->private_data;
  374. uint32_t msg_type;
  375. int rc = len;
  376. int ret;
  377. LIST_HEAD(staging_q);
  378. /*
  379. * We're expecting usermode to be writing properly formed
  380. * xenbus messages. If they write an incomplete message we
  381. * buffer it up. Once it is complete, we act on it.
  382. */
  383. /*
  384. * Make sure concurrent writers can't stomp all over each
  385. * other's messages and make a mess of our partial message
  386. * buffer. We don't make any attemppt to stop multiple
  387. * writers from making a mess of each other's incomplete
  388. * messages; we're just trying to guarantee our own internal
  389. * consistency and make sure that single writes are handled
  390. * atomically.
  391. */
  392. mutex_lock(&u->msgbuffer_mutex);
  393. /* Get this out of the way early to avoid confusion */
  394. if (len == 0)
  395. goto out;
  396. /* Can't write a xenbus message larger we can buffer */
  397. if (len > sizeof(u->u.buffer) - u->len) {
  398. /* On error, dump existing buffer */
  399. u->len = 0;
  400. rc = -EINVAL;
  401. goto out;
  402. }
  403. ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
  404. if (ret != 0) {
  405. rc = -EFAULT;
  406. goto out;
  407. }
  408. /* Deal with a partial copy. */
  409. len -= ret;
  410. rc = len;
  411. u->len += len;
  412. /* Return if we haven't got a full message yet */
  413. if (u->len < sizeof(u->u.msg))
  414. goto out; /* not even the header yet */
  415. /* If we're expecting a message that's larger than we can
  416. possibly send, dump what we have and return an error. */
  417. if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
  418. rc = -E2BIG;
  419. u->len = 0;
  420. goto out;
  421. }
  422. if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
  423. goto out; /* incomplete data portion */
  424. /*
  425. * OK, now we have a complete message. Do something with it.
  426. */
  427. msg_type = u->u.msg.type;
  428. switch (msg_type) {
  429. case XS_WATCH:
  430. case XS_UNWATCH:
  431. /* (Un)Ask for some path to be watched for changes */
  432. ret = xenbus_write_watch(msg_type, u);
  433. break;
  434. default:
  435. /* Send out a transaction */
  436. ret = xenbus_write_transaction(msg_type, u);
  437. break;
  438. }
  439. if (ret != 0)
  440. rc = ret;
  441. /* Buffered message consumed */
  442. u->len = 0;
  443. out:
  444. mutex_unlock(&u->msgbuffer_mutex);
  445. return rc;
  446. }
  447. static int xenbus_file_open(struct inode *inode, struct file *filp)
  448. {
  449. struct xenbus_file_priv *u;
  450. if (xen_store_evtchn == 0)
  451. return -ENOENT;
  452. nonseekable_open(inode, filp);
  453. u = kzalloc(sizeof(*u), GFP_KERNEL);
  454. if (u == NULL)
  455. return -ENOMEM;
  456. INIT_LIST_HEAD(&u->transactions);
  457. INIT_LIST_HEAD(&u->watches);
  458. INIT_LIST_HEAD(&u->read_buffers);
  459. init_waitqueue_head(&u->read_waitq);
  460. mutex_init(&u->reply_mutex);
  461. mutex_init(&u->msgbuffer_mutex);
  462. filp->private_data = u;
  463. return 0;
  464. }
  465. static int xenbus_file_release(struct inode *inode, struct file *filp)
  466. {
  467. struct xenbus_file_priv *u = filp->private_data;
  468. struct xenbus_transaction_holder *trans, *tmp;
  469. struct watch_adapter *watch, *tmp_watch;
  470. struct read_buffer *rb, *tmp_rb;
  471. /*
  472. * No need for locking here because there are no other users,
  473. * by definition.
  474. */
  475. list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
  476. xenbus_transaction_end(trans->handle, 1);
  477. list_del(&trans->list);
  478. kfree(trans);
  479. }
  480. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  481. unregister_xenbus_watch(&watch->watch);
  482. list_del(&watch->list);
  483. free_watch_adapter(watch);
  484. }
  485. list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
  486. list_del(&rb->list);
  487. kfree(rb);
  488. }
  489. kfree(u);
  490. return 0;
  491. }
  492. static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
  493. {
  494. struct xenbus_file_priv *u = file->private_data;
  495. poll_wait(file, &u->read_waitq, wait);
  496. if (!list_empty(&u->read_buffers))
  497. return POLLIN | POLLRDNORM;
  498. return 0;
  499. }
  500. const struct file_operations xen_xenbus_fops = {
  501. .read = xenbus_file_read,
  502. .write = xenbus_file_write,
  503. .open = xenbus_file_open,
  504. .release = xenbus_file_release,
  505. .poll = xenbus_file_poll,
  506. .llseek = no_llseek,
  507. };
  508. EXPORT_SYMBOL_GPL(xen_xenbus_fops);
  509. static struct miscdevice xenbus_dev = {
  510. .minor = MISC_DYNAMIC_MINOR,
  511. .name = "xen/xenbus",
  512. .fops = &xen_xenbus_fops,
  513. };
  514. static int __init xenbus_init(void)
  515. {
  516. int err;
  517. if (!xen_domain())
  518. return -ENODEV;
  519. err = misc_register(&xenbus_dev);
  520. if (err)
  521. pr_err("Could not register xenbus frontend device\n");
  522. return err;
  523. }
  524. static void __exit xenbus_exit(void)
  525. {
  526. misc_deregister(&xenbus_dev);
  527. }
  528. module_init(xenbus_init);
  529. module_exit(xenbus_exit);