trans_virtio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * The Guest 9p transport driver
  3. *
  4. * This is a block based transport driver based on the lguest block driver
  5. * code.
  6. *
  7. */
  8. /*
  9. * Copyright (C) 2007 Eric Van Hensbergen, IBM Corporation
  10. *
  11. * Based on virtio console driver
  12. * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to:
  25. * Free Software Foundation
  26. * 51 Franklin Street, Fifth Floor
  27. * Boston, MA 02111-1301 USA
  28. *
  29. */
  30. #include <linux/in.h>
  31. #include <linux/module.h>
  32. #include <linux/net.h>
  33. #include <linux/ipv6.h>
  34. #include <linux/errno.h>
  35. #include <linux/kernel.h>
  36. #include <linux/un.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/inet.h>
  39. #include <linux/idr.h>
  40. #include <linux/file.h>
  41. #include <net/9p/9p.h>
  42. #include <linux/parser.h>
  43. #include <net/9p/transport.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/virtio.h>
  46. #include <linux/virtio_9p.h>
  47. #define VIRTQUEUE_NUM 128
  48. /* a single mutex to manage channel initialization and attachment */
  49. static DECLARE_MUTEX(virtio_9p_lock);
  50. /* global which tracks highest initialized channel */
  51. static int chan_index;
  52. #define P9_INIT_MAXTAG 16
  53. #define REQ_STATUS_IDLE 0
  54. #define REQ_STATUS_SENT 1
  55. #define REQ_STATUS_RCVD 2
  56. #define REQ_STATUS_FLSH 3
  57. struct p9_req_t {
  58. int status;
  59. wait_queue_head_t *wq;
  60. };
  61. /* We keep all per-channel information in a structure.
  62. * This structure is allocated within the devices dev->mem space.
  63. * A pointer to the structure will get put in the transport private.
  64. */
  65. static struct virtio_chan {
  66. bool initialized; /* channel is initialized */
  67. bool inuse; /* channel is in use */
  68. spinlock_t lock;
  69. struct virtio_device *vdev;
  70. struct virtqueue *vq;
  71. struct p9_idpool *tagpool;
  72. struct p9_req_t *reqs;
  73. int max_tag;
  74. /* Scatterlist: can be too big for stack. */
  75. struct scatterlist sg[VIRTQUEUE_NUM];
  76. } channels[MAX_9P_CHAN];
  77. /* Lookup requests by tag */
  78. static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
  79. {
  80. /* This looks up the original request by tag so we know which
  81. * buffer to read the data into */
  82. tag++;
  83. while (tag >= c->max_tag) {
  84. int old_max = c->max_tag;
  85. int count;
  86. if (c->max_tag)
  87. c->max_tag *= 2;
  88. else
  89. c->max_tag = P9_INIT_MAXTAG;
  90. c->reqs = krealloc(c->reqs, sizeof(struct p9_req_t)*c->max_tag,
  91. GFP_ATOMIC);
  92. if (!c->reqs) {
  93. printk(KERN_ERR "Couldn't grow tag array\n");
  94. BUG();
  95. }
  96. for (count = old_max; count < c->max_tag; count++) {
  97. c->reqs[count].status = REQ_STATUS_IDLE;
  98. c->reqs[count].wq = kmalloc(sizeof(wait_queue_t),
  99. GFP_ATOMIC);
  100. if (!c->reqs[count].wq) {
  101. printk(KERN_ERR "Couldn't grow tag array\n");
  102. BUG();
  103. }
  104. init_waitqueue_head(c->reqs[count].wq);
  105. }
  106. }
  107. return &c->reqs[tag];
  108. }
  109. /* How many bytes left in this page. */
  110. static unsigned int rest_of_page(void *data)
  111. {
  112. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  113. }
  114. static void p9_virtio_close(struct p9_trans *trans)
  115. {
  116. struct virtio_chan *chan = trans->priv;
  117. int count;
  118. unsigned int flags;
  119. spin_lock_irqsave(&chan->lock, flags);
  120. p9_idpool_destroy(chan->tagpool);
  121. for (count = 0; count < chan->max_tag; count++)
  122. kfree(chan->reqs[count].wq);
  123. kfree(chan->reqs);
  124. chan->max_tag = 0;
  125. spin_unlock_irqrestore(&chan->lock, flags);
  126. down(&virtio_9p_lock);
  127. chan->inuse = false;
  128. up(&virtio_9p_lock);
  129. kfree(trans);
  130. }
  131. static void req_done(struct virtqueue *vq)
  132. {
  133. struct virtio_chan *chan = vq->vdev->priv;
  134. struct p9_fcall *rc;
  135. unsigned int len;
  136. unsigned long flags;
  137. struct p9_req_t *req;
  138. spin_lock_irqsave(&chan->lock, flags);
  139. while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
  140. req = p9_lookup_tag(chan, rc->tag);
  141. req->status = REQ_STATUS_RCVD;
  142. wake_up(req->wq);
  143. }
  144. /* In case queue is stopped waiting for more buffers. */
  145. spin_unlock_irqrestore(&chan->lock, flags);
  146. }
  147. static int
  148. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  149. int count)
  150. {
  151. int s;
  152. int index = start;
  153. while (count) {
  154. s = rest_of_page(data);
  155. if (s > count)
  156. s = count;
  157. sg_set_buf(&sg[index++], data, s);
  158. count -= s;
  159. data += s;
  160. if (index > limit)
  161. BUG();
  162. }
  163. return index-start;
  164. }
  165. static int
  166. p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
  167. {
  168. int in, out;
  169. int n, err, size;
  170. struct virtio_chan *chan = t->priv;
  171. char *rdata;
  172. struct p9_req_t *req;
  173. unsigned long flags;
  174. if (*rc == NULL) {
  175. *rc = kmalloc(sizeof(struct p9_fcall) + t->msize, GFP_KERNEL);
  176. if (!*rc)
  177. return -ENOMEM;
  178. }
  179. rdata = (char *)*rc+sizeof(struct p9_fcall);
  180. n = P9_NOTAG;
  181. if (tc->id != P9_TVERSION) {
  182. n = p9_idpool_get(chan->tagpool);
  183. if (n < 0)
  184. return -ENOMEM;
  185. }
  186. spin_lock_irqsave(&chan->lock, flags);
  187. req = p9_lookup_tag(chan, n);
  188. spin_unlock_irqrestore(&chan->lock, flags);
  189. p9_set_tag(tc, n);
  190. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
  191. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
  192. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, t->msize);
  193. req->status = REQ_STATUS_SENT;
  194. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
  195. P9_DPRINTK(P9_DEBUG_TRANS,
  196. "9p debug: virtio rpc add_buf returned failure");
  197. return -EIO;
  198. }
  199. chan->vq->vq_ops->kick(chan->vq);
  200. wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
  201. size = le32_to_cpu(*(__le32 *) rdata);
  202. err = p9_deserialize_fcall(rdata, size, *rc, t->extended);
  203. if (err < 0) {
  204. P9_DPRINTK(P9_DEBUG_TRANS,
  205. "9p debug: virtio rpc deserialize returned %d\n", err);
  206. return err;
  207. }
  208. #ifdef CONFIG_NET_9P_DEBUG
  209. if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
  210. char buf[150];
  211. p9_printfcall(buf, sizeof(buf), *rc, t->extended);
  212. printk(KERN_NOTICE ">>> %p %s\n", t, buf);
  213. }
  214. #endif
  215. if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
  216. p9_idpool_put(n, chan->tagpool);
  217. req->status = REQ_STATUS_IDLE;
  218. return 0;
  219. }
  220. static int p9_virtio_probe(struct virtio_device *vdev)
  221. {
  222. int err;
  223. struct virtio_chan *chan;
  224. int index;
  225. down(&virtio_9p_lock);
  226. index = chan_index++;
  227. chan = &channels[index];
  228. up(&virtio_9p_lock);
  229. if (chan_index > MAX_9P_CHAN) {
  230. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  231. BUG();
  232. err = -ENOMEM;
  233. goto fail;
  234. }
  235. chan->vdev = vdev;
  236. /* We expect one virtqueue, for requests. */
  237. chan->vq = vdev->config->find_vq(vdev, 0, req_done);
  238. if (IS_ERR(chan->vq)) {
  239. err = PTR_ERR(chan->vq);
  240. goto out_free_vq;
  241. }
  242. chan->vq->vdev->priv = chan;
  243. spin_lock_init(&chan->lock);
  244. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  245. chan->inuse = false;
  246. chan->initialized = true;
  247. return 0;
  248. out_free_vq:
  249. vdev->config->del_vq(chan->vq);
  250. fail:
  251. down(&virtio_9p_lock);
  252. chan_index--;
  253. up(&virtio_9p_lock);
  254. return err;
  255. }
  256. /* This sets up a transport channel for 9p communication. Right now
  257. * we only match the first available channel, but eventually we couldlook up
  258. * alternate channels by matching devname versus a virtio_config entry.
  259. * We use a simple reference count mechanism to ensure that only a single
  260. * mount has a channel open at a time. */
  261. static struct p9_trans *
  262. p9_virtio_create(const char *devname, char *args, int msize,
  263. unsigned char extended)
  264. {
  265. struct p9_trans *trans;
  266. struct virtio_chan *chan = channels;
  267. int index = 0;
  268. down(&virtio_9p_lock);
  269. while (index < MAX_9P_CHAN) {
  270. if (chan->initialized && !chan->inuse) {
  271. chan->inuse = true;
  272. break;
  273. } else {
  274. index++;
  275. chan = &channels[index];
  276. }
  277. }
  278. up(&virtio_9p_lock);
  279. if (index >= MAX_9P_CHAN) {
  280. printk(KERN_ERR "9p: no channels available\n");
  281. return ERR_PTR(-ENODEV);
  282. }
  283. chan->tagpool = p9_idpool_create();
  284. if (IS_ERR(chan->tagpool)) {
  285. printk(KERN_ERR "9p: couldn't allocate tagpool\n");
  286. return ERR_PTR(-ENOMEM);
  287. }
  288. p9_idpool_get(chan->tagpool); /* reserve tag 0 */
  289. chan->max_tag = 0;
  290. chan->reqs = NULL;
  291. trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
  292. if (!trans) {
  293. printk(KERN_ERR "9p: couldn't allocate transport\n");
  294. return ERR_PTR(-ENOMEM);
  295. }
  296. trans->extended = extended;
  297. trans->msize = msize;
  298. trans->close = p9_virtio_close;
  299. trans->rpc = p9_virtio_rpc;
  300. trans->priv = chan;
  301. return trans;
  302. }
  303. static void p9_virtio_remove(struct virtio_device *vdev)
  304. {
  305. struct virtio_chan *chan = vdev->priv;
  306. BUG_ON(chan->inuse);
  307. if (chan->initialized) {
  308. vdev->config->del_vq(chan->vq);
  309. chan->initialized = false;
  310. }
  311. }
  312. #define VIRTIO_ID_9P 9
  313. static struct virtio_device_id id_table[] = {
  314. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  315. { 0 },
  316. };
  317. /* The standard "struct lguest_driver": */
  318. static struct virtio_driver p9_virtio_drv = {
  319. .driver.name = KBUILD_MODNAME,
  320. .driver.owner = THIS_MODULE,
  321. .id_table = id_table,
  322. .probe = p9_virtio_probe,
  323. .remove = p9_virtio_remove,
  324. };
  325. static struct p9_trans_module p9_virtio_trans = {
  326. .name = "virtio",
  327. .create = p9_virtio_create,
  328. .maxsize = PAGE_SIZE*16,
  329. .def = 0,
  330. };
  331. /* The standard init function */
  332. static int __init p9_virtio_init(void)
  333. {
  334. int count;
  335. for (count = 0; count < MAX_9P_CHAN; count++)
  336. channels[count].initialized = false;
  337. v9fs_register_trans(&p9_virtio_trans);
  338. return register_virtio_driver(&p9_virtio_drv);
  339. }
  340. static void __exit p9_virtio_cleanup(void)
  341. {
  342. unregister_virtio_driver(&p9_virtio_drv);
  343. }
  344. module_init(p9_virtio_init);
  345. module_exit(p9_virtio_cleanup);
  346. MODULE_DEVICE_TABLE(virtio, id_table);
  347. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  348. MODULE_DESCRIPTION("Virtio 9p Transport");
  349. MODULE_LICENSE("GPL");