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