trans_virtio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_head_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. BUG_ON(index > limit);
  161. }
  162. return index-start;
  163. }
  164. static int
  165. p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
  166. {
  167. int in, out;
  168. int n, err, size;
  169. struct virtio_chan *chan = t->priv;
  170. char *rdata;
  171. struct p9_req_t *req;
  172. unsigned long flags;
  173. if (*rc == NULL) {
  174. *rc = kmalloc(sizeof(struct p9_fcall) + t->msize, GFP_KERNEL);
  175. if (!*rc)
  176. return -ENOMEM;
  177. }
  178. rdata = (char *)*rc+sizeof(struct p9_fcall);
  179. n = P9_NOTAG;
  180. if (tc->id != P9_TVERSION) {
  181. n = p9_idpool_get(chan->tagpool);
  182. if (n < 0)
  183. return -ENOMEM;
  184. }
  185. spin_lock_irqsave(&chan->lock, flags);
  186. req = p9_lookup_tag(chan, n);
  187. spin_unlock_irqrestore(&chan->lock, flags);
  188. p9_set_tag(tc, n);
  189. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
  190. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
  191. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, t->msize);
  192. req->status = REQ_STATUS_SENT;
  193. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
  194. P9_DPRINTK(P9_DEBUG_TRANS,
  195. "9p debug: virtio rpc add_buf returned failure");
  196. return -EIO;
  197. }
  198. chan->vq->vq_ops->kick(chan->vq);
  199. wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
  200. size = le32_to_cpu(*(__le32 *) rdata);
  201. err = p9_deserialize_fcall(rdata, size, *rc, t->extended);
  202. if (err < 0) {
  203. P9_DPRINTK(P9_DEBUG_TRANS,
  204. "9p debug: virtio rpc deserialize returned %d\n", err);
  205. return err;
  206. }
  207. #ifdef CONFIG_NET_9P_DEBUG
  208. if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
  209. char buf[150];
  210. p9_printfcall(buf, sizeof(buf), *rc, t->extended);
  211. printk(KERN_NOTICE ">>> %p %s\n", t, buf);
  212. }
  213. #endif
  214. if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
  215. p9_idpool_put(n, chan->tagpool);
  216. req->status = REQ_STATUS_IDLE;
  217. return 0;
  218. }
  219. static int p9_virtio_probe(struct virtio_device *vdev)
  220. {
  221. int err;
  222. struct virtio_chan *chan;
  223. int index;
  224. down(&virtio_9p_lock);
  225. index = chan_index++;
  226. chan = &channels[index];
  227. up(&virtio_9p_lock);
  228. if (chan_index > MAX_9P_CHAN) {
  229. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  230. BUG();
  231. err = -ENOMEM;
  232. goto fail;
  233. }
  234. chan->vdev = vdev;
  235. /* We expect one virtqueue, for requests. */
  236. chan->vq = vdev->config->find_vq(vdev, 0, req_done);
  237. if (IS_ERR(chan->vq)) {
  238. err = PTR_ERR(chan->vq);
  239. goto out_free_vq;
  240. }
  241. chan->vq->vdev->priv = chan;
  242. spin_lock_init(&chan->lock);
  243. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  244. chan->inuse = false;
  245. chan->initialized = true;
  246. return 0;
  247. out_free_vq:
  248. vdev->config->del_vq(chan->vq);
  249. fail:
  250. down(&virtio_9p_lock);
  251. chan_index--;
  252. up(&virtio_9p_lock);
  253. return err;
  254. }
  255. /* This sets up a transport channel for 9p communication. Right now
  256. * we only match the first available channel, but eventually we couldlook up
  257. * alternate channels by matching devname versus a virtio_config entry.
  258. * We use a simple reference count mechanism to ensure that only a single
  259. * mount has a channel open at a time. */
  260. static struct p9_trans *
  261. p9_virtio_create(const char *devname, char *args, int msize,
  262. unsigned char extended)
  263. {
  264. struct p9_trans *trans;
  265. struct virtio_chan *chan = channels;
  266. int index = 0;
  267. down(&virtio_9p_lock);
  268. while (index < MAX_9P_CHAN) {
  269. if (chan->initialized && !chan->inuse) {
  270. chan->inuse = true;
  271. break;
  272. } else {
  273. index++;
  274. chan = &channels[index];
  275. }
  276. }
  277. up(&virtio_9p_lock);
  278. if (index >= MAX_9P_CHAN) {
  279. printk(KERN_ERR "9p: no channels available\n");
  280. return ERR_PTR(-ENODEV);
  281. }
  282. chan->tagpool = p9_idpool_create();
  283. if (IS_ERR(chan->tagpool)) {
  284. printk(KERN_ERR "9p: couldn't allocate tagpool\n");
  285. return ERR_PTR(-ENOMEM);
  286. }
  287. p9_idpool_get(chan->tagpool); /* reserve tag 0 */
  288. chan->max_tag = 0;
  289. chan->reqs = NULL;
  290. trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
  291. if (!trans) {
  292. printk(KERN_ERR "9p: couldn't allocate transport\n");
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. trans->extended = extended;
  296. trans->msize = msize;
  297. trans->close = p9_virtio_close;
  298. trans->rpc = p9_virtio_rpc;
  299. trans->priv = chan;
  300. return trans;
  301. }
  302. static void p9_virtio_remove(struct virtio_device *vdev)
  303. {
  304. struct virtio_chan *chan = vdev->priv;
  305. BUG_ON(chan->inuse);
  306. if (chan->initialized) {
  307. vdev->config->del_vq(chan->vq);
  308. chan->initialized = false;
  309. }
  310. }
  311. #define VIRTIO_ID_9P 9
  312. static struct virtio_device_id id_table[] = {
  313. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  314. { 0 },
  315. };
  316. /* The standard "struct lguest_driver": */
  317. static struct virtio_driver p9_virtio_drv = {
  318. .driver.name = KBUILD_MODNAME,
  319. .driver.owner = THIS_MODULE,
  320. .id_table = id_table,
  321. .probe = p9_virtio_probe,
  322. .remove = p9_virtio_remove,
  323. };
  324. static struct p9_trans_module p9_virtio_trans = {
  325. .name = "virtio",
  326. .create = p9_virtio_create,
  327. .maxsize = PAGE_SIZE*16,
  328. .def = 0,
  329. };
  330. /* The standard init function */
  331. static int __init p9_virtio_init(void)
  332. {
  333. int count;
  334. for (count = 0; count < MAX_9P_CHAN; count++)
  335. channels[count].initialized = false;
  336. v9fs_register_trans(&p9_virtio_trans);
  337. return register_virtio_driver(&p9_virtio_drv);
  338. }
  339. static void __exit p9_virtio_cleanup(void)
  340. {
  341. unregister_virtio_driver(&p9_virtio_drv);
  342. }
  343. module_init(p9_virtio_init);
  344. module_exit(p9_virtio_cleanup);
  345. MODULE_DEVICE_TABLE(virtio, id_table);
  346. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  347. MODULE_DESCRIPTION("Virtio 9p Transport");
  348. MODULE_LICENSE("GPL");