xenbus.c 14 KB

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