xenbus_dev_frontend.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 <xen/xenbus.h>
  57. #include <xen/xen.h>
  58. #include <asm/xen/hypervisor.h>
  59. #include "xenbus.h"
  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[XENSTORE_PAYLOAD_MAX];
  98. } u;
  99. /* Response queue. */
  100. struct mutex reply_mutex;
  101. struct list_head read_buffers;
  102. wait_queue_head_t read_waitq;
  103. struct kref kref;
  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. if (len > XENSTORE_PAYLOAD_MAX)
  167. return -EINVAL;
  168. rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
  169. if (rb == NULL)
  170. return -ENOMEM;
  171. rb->cons = 0;
  172. rb->len = len;
  173. memcpy(rb->msg, data, len);
  174. list_add_tail(&rb->list, queue);
  175. return 0;
  176. }
  177. /*
  178. * Free all the read_buffer s on a list.
  179. * Caller must have sole reference to list.
  180. */
  181. static void queue_cleanup(struct list_head *list)
  182. {
  183. struct read_buffer *rb;
  184. while (!list_empty(list)) {
  185. rb = list_entry(list->next, struct read_buffer, list);
  186. list_del(list->next);
  187. kfree(rb);
  188. }
  189. }
  190. struct watch_adapter {
  191. struct list_head list;
  192. struct xenbus_watch watch;
  193. struct xenbus_file_priv *dev_data;
  194. char *token;
  195. };
  196. static void free_watch_adapter(struct watch_adapter *watch)
  197. {
  198. kfree(watch->watch.node);
  199. kfree(watch->token);
  200. kfree(watch);
  201. }
  202. static struct watch_adapter *alloc_watch_adapter(const char *path,
  203. const char *token)
  204. {
  205. struct watch_adapter *watch;
  206. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  207. if (watch == NULL)
  208. goto out_fail;
  209. watch->watch.node = kstrdup(path, GFP_KERNEL);
  210. if (watch->watch.node == NULL)
  211. goto out_free;
  212. watch->token = kstrdup(token, GFP_KERNEL);
  213. if (watch->token == NULL)
  214. goto out_free;
  215. return watch;
  216. out_free:
  217. free_watch_adapter(watch);
  218. out_fail:
  219. return NULL;
  220. }
  221. static void watch_fired(struct xenbus_watch *watch,
  222. const char *path,
  223. const char *token)
  224. {
  225. struct watch_adapter *adap;
  226. struct xsd_sockmsg hdr;
  227. const char *token_caller;
  228. int path_len, tok_len, body_len;
  229. int ret;
  230. LIST_HEAD(staging_q);
  231. adap = container_of(watch, struct watch_adapter, watch);
  232. token_caller = adap->token;
  233. path_len = strlen(path) + 1;
  234. tok_len = strlen(token_caller) + 1;
  235. body_len = path_len + tok_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_caller, tok_len);
  244. if (!ret) {
  245. /* success: pass reply list onto watcher */
  246. list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
  247. wake_up(&adap->dev_data->read_waitq);
  248. } else
  249. queue_cleanup(&staging_q);
  250. mutex_unlock(&adap->dev_data->reply_mutex);
  251. }
  252. static void xenbus_file_free(struct kref *kref)
  253. {
  254. struct xenbus_file_priv *u;
  255. struct xenbus_transaction_holder *trans, *tmp;
  256. struct watch_adapter *watch, *tmp_watch;
  257. struct read_buffer *rb, *tmp_rb;
  258. u = container_of(kref, struct xenbus_file_priv, kref);
  259. /*
  260. * No need for locking here because there are no other users,
  261. * by definition.
  262. */
  263. list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
  264. xenbus_transaction_end(trans->handle, 1);
  265. list_del(&trans->list);
  266. kfree(trans);
  267. }
  268. list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
  269. unregister_xenbus_watch(&watch->watch);
  270. list_del(&watch->list);
  271. free_watch_adapter(watch);
  272. }
  273. list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
  274. list_del(&rb->list);
  275. kfree(rb);
  276. }
  277. kfree(u);
  278. }
  279. static struct xenbus_transaction_holder *xenbus_get_transaction(
  280. struct xenbus_file_priv *u, uint32_t tx_id)
  281. {
  282. struct xenbus_transaction_holder *trans;
  283. list_for_each_entry(trans, &u->transactions, list)
  284. if (trans->handle.id == tx_id)
  285. return trans;
  286. return NULL;
  287. }
  288. void xenbus_dev_queue_reply(struct xb_req_data *req)
  289. {
  290. struct xenbus_file_priv *u = req->par;
  291. struct xenbus_transaction_holder *trans = NULL;
  292. int rc;
  293. LIST_HEAD(staging_q);
  294. xs_request_exit(req);
  295. mutex_lock(&u->msgbuffer_mutex);
  296. if (req->type == XS_TRANSACTION_START) {
  297. trans = xenbus_get_transaction(u, 0);
  298. if (WARN_ON(!trans))
  299. goto out;
  300. if (req->msg.type == XS_ERROR) {
  301. list_del(&trans->list);
  302. kfree(trans);
  303. } else {
  304. rc = kstrtou32(req->body, 10, &trans->handle.id);
  305. if (WARN_ON(rc))
  306. goto out;
  307. }
  308. } else if (req->type == XS_TRANSACTION_END) {
  309. trans = xenbus_get_transaction(u, req->msg.tx_id);
  310. if (WARN_ON(!trans))
  311. goto out;
  312. list_del(&trans->list);
  313. kfree(trans);
  314. }
  315. mutex_unlock(&u->msgbuffer_mutex);
  316. mutex_lock(&u->reply_mutex);
  317. rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
  318. if (!rc)
  319. rc = queue_reply(&staging_q, req->body, req->msg.len);
  320. if (!rc) {
  321. list_splice_tail(&staging_q, &u->read_buffers);
  322. wake_up(&u->read_waitq);
  323. } else {
  324. queue_cleanup(&staging_q);
  325. }
  326. mutex_unlock(&u->reply_mutex);
  327. kfree(req->body);
  328. kfree(req);
  329. kref_put(&u->kref, xenbus_file_free);
  330. return;
  331. out:
  332. mutex_unlock(&u->msgbuffer_mutex);
  333. }
  334. static int xenbus_command_reply(struct xenbus_file_priv *u,
  335. unsigned int msg_type, const char *reply)
  336. {
  337. struct {
  338. struct xsd_sockmsg hdr;
  339. char body[16];
  340. } msg;
  341. int rc;
  342. msg.hdr = u->u.msg;
  343. msg.hdr.type = msg_type;
  344. msg.hdr.len = strlen(reply) + 1;
  345. if (msg.hdr.len > sizeof(msg.body))
  346. return -E2BIG;
  347. memcpy(&msg.body, reply, msg.hdr.len);
  348. mutex_lock(&u->reply_mutex);
  349. rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
  350. wake_up(&u->read_waitq);
  351. mutex_unlock(&u->reply_mutex);
  352. if (!rc)
  353. kref_put(&u->kref, xenbus_file_free);
  354. return rc;
  355. }
  356. static int xenbus_write_transaction(unsigned msg_type,
  357. struct xenbus_file_priv *u)
  358. {
  359. int rc;
  360. struct xenbus_transaction_holder *trans = NULL;
  361. struct {
  362. struct xsd_sockmsg hdr;
  363. char body[];
  364. } *msg = (void *)u->u.buffer;
  365. if (msg_type == XS_TRANSACTION_START) {
  366. trans = kzalloc(sizeof(*trans), GFP_KERNEL);
  367. if (!trans) {
  368. rc = -ENOMEM;
  369. goto out;
  370. }
  371. list_add(&trans->list, &u->transactions);
  372. } else if (msg->hdr.tx_id != 0 &&
  373. !xenbus_get_transaction(u, msg->hdr.tx_id))
  374. return xenbus_command_reply(u, XS_ERROR, "ENOENT");
  375. else if (msg_type == XS_TRANSACTION_END &&
  376. !(msg->hdr.len == 2 &&
  377. (!strcmp(msg->body, "T") || !strcmp(msg->body, "F"))))
  378. return xenbus_command_reply(u, XS_ERROR, "EINVAL");
  379. rc = xenbus_dev_request_and_reply(&msg->hdr, u);
  380. if (rc && trans) {
  381. list_del(&trans->list);
  382. kfree(trans);
  383. }
  384. out:
  385. return rc;
  386. }
  387. static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
  388. {
  389. struct watch_adapter *watch;
  390. char *path, *token;
  391. int err, rc;
  392. LIST_HEAD(staging_q);
  393. path = u->u.buffer + sizeof(u->u.msg);
  394. token = memchr(path, 0, u->u.msg.len);
  395. if (token == NULL) {
  396. rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
  397. goto out;
  398. }
  399. token++;
  400. if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
  401. rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
  402. goto out;
  403. }
  404. if (msg_type == XS_WATCH) {
  405. watch = alloc_watch_adapter(path, token);
  406. if (watch == NULL) {
  407. rc = -ENOMEM;
  408. goto out;
  409. }
  410. watch->watch.callback = watch_fired;
  411. watch->dev_data = u;
  412. err = register_xenbus_watch(&watch->watch);
  413. if (err) {
  414. free_watch_adapter(watch);
  415. rc = err;
  416. goto out;
  417. }
  418. list_add(&watch->list, &u->watches);
  419. } else {
  420. list_for_each_entry(watch, &u->watches, list) {
  421. if (!strcmp(watch->token, token) &&
  422. !strcmp(watch->watch.node, path)) {
  423. unregister_xenbus_watch(&watch->watch);
  424. list_del(&watch->list);
  425. free_watch_adapter(watch);
  426. break;
  427. }
  428. }
  429. }
  430. /* Success. Synthesize a reply to say all is OK. */
  431. rc = xenbus_command_reply(u, msg_type, "OK");
  432. out:
  433. return rc;
  434. }
  435. static ssize_t xenbus_file_write(struct file *filp,
  436. const char __user *ubuf,
  437. size_t len, loff_t *ppos)
  438. {
  439. struct xenbus_file_priv *u = filp->private_data;
  440. uint32_t msg_type;
  441. int rc = len;
  442. int ret;
  443. LIST_HEAD(staging_q);
  444. /*
  445. * We're expecting usermode to be writing properly formed
  446. * xenbus messages. If they write an incomplete message we
  447. * buffer it up. Once it is complete, we act on it.
  448. */
  449. /*
  450. * Make sure concurrent writers can't stomp all over each
  451. * other's messages and make a mess of our partial message
  452. * buffer. We don't make any attemppt to stop multiple
  453. * writers from making a mess of each other's incomplete
  454. * messages; we're just trying to guarantee our own internal
  455. * consistency and make sure that single writes are handled
  456. * atomically.
  457. */
  458. mutex_lock(&u->msgbuffer_mutex);
  459. /* Get this out of the way early to avoid confusion */
  460. if (len == 0)
  461. goto out;
  462. /* Can't write a xenbus message larger we can buffer */
  463. if (len > sizeof(u->u.buffer) - u->len) {
  464. /* On error, dump existing buffer */
  465. u->len = 0;
  466. rc = -EINVAL;
  467. goto out;
  468. }
  469. ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
  470. if (ret != 0) {
  471. rc = -EFAULT;
  472. goto out;
  473. }
  474. /* Deal with a partial copy. */
  475. len -= ret;
  476. rc = len;
  477. u->len += len;
  478. /* Return if we haven't got a full message yet */
  479. if (u->len < sizeof(u->u.msg))
  480. goto out; /* not even the header yet */
  481. /* If we're expecting a message that's larger than we can
  482. possibly send, dump what we have and return an error. */
  483. if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
  484. rc = -E2BIG;
  485. u->len = 0;
  486. goto out;
  487. }
  488. if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
  489. goto out; /* incomplete data portion */
  490. /*
  491. * OK, now we have a complete message. Do something with it.
  492. */
  493. kref_get(&u->kref);
  494. msg_type = u->u.msg.type;
  495. switch (msg_type) {
  496. case XS_WATCH:
  497. case XS_UNWATCH:
  498. /* (Un)Ask for some path to be watched for changes */
  499. ret = xenbus_write_watch(msg_type, u);
  500. break;
  501. default:
  502. /* Send out a transaction */
  503. ret = xenbus_write_transaction(msg_type, u);
  504. break;
  505. }
  506. if (ret != 0) {
  507. rc = ret;
  508. kref_put(&u->kref, xenbus_file_free);
  509. }
  510. /* Buffered message consumed */
  511. u->len = 0;
  512. out:
  513. mutex_unlock(&u->msgbuffer_mutex);
  514. return rc;
  515. }
  516. static int xenbus_file_open(struct inode *inode, struct file *filp)
  517. {
  518. struct xenbus_file_priv *u;
  519. if (xen_store_evtchn == 0)
  520. return -ENOENT;
  521. nonseekable_open(inode, filp);
  522. filp->f_mode &= ~FMODE_ATOMIC_POS; /* cdev-style semantics */
  523. u = kzalloc(sizeof(*u), GFP_KERNEL);
  524. if (u == NULL)
  525. return -ENOMEM;
  526. kref_init(&u->kref);
  527. INIT_LIST_HEAD(&u->transactions);
  528. INIT_LIST_HEAD(&u->watches);
  529. INIT_LIST_HEAD(&u->read_buffers);
  530. init_waitqueue_head(&u->read_waitq);
  531. mutex_init(&u->reply_mutex);
  532. mutex_init(&u->msgbuffer_mutex);
  533. filp->private_data = u;
  534. return 0;
  535. }
  536. static int xenbus_file_release(struct inode *inode, struct file *filp)
  537. {
  538. struct xenbus_file_priv *u = filp->private_data;
  539. kref_put(&u->kref, xenbus_file_free);
  540. return 0;
  541. }
  542. static __poll_t xenbus_file_poll(struct file *file, poll_table *wait)
  543. {
  544. struct xenbus_file_priv *u = file->private_data;
  545. poll_wait(file, &u->read_waitq, wait);
  546. if (!list_empty(&u->read_buffers))
  547. return EPOLLIN | EPOLLRDNORM;
  548. return 0;
  549. }
  550. const struct file_operations xen_xenbus_fops = {
  551. .read = xenbus_file_read,
  552. .write = xenbus_file_write,
  553. .open = xenbus_file_open,
  554. .release = xenbus_file_release,
  555. .poll = xenbus_file_poll,
  556. .llseek = no_llseek,
  557. };
  558. EXPORT_SYMBOL_GPL(xen_xenbus_fops);
  559. static struct miscdevice xenbus_dev = {
  560. .minor = MISC_DYNAMIC_MINOR,
  561. .name = "xen/xenbus",
  562. .fops = &xen_xenbus_fops,
  563. };
  564. static int __init xenbus_init(void)
  565. {
  566. int err;
  567. if (!xen_domain())
  568. return -ENODEV;
  569. err = misc_register(&xenbus_dev);
  570. if (err)
  571. pr_err("Could not register xenbus frontend device\n");
  572. return err;
  573. }
  574. device_initcall(xenbus_init);