xenbus.c 14 KB

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