trans_virtio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 DEFINE_MUTEX(virtio_9p_lock);
  50. /* global which tracks highest initialized channel */
  51. static int chan_index;
  52. #define P9_INIT_MAXTAG 16
  53. /**
  54. * enum p9_req_status_t - virtio request status
  55. * @REQ_STATUS_IDLE: request slot unused
  56. * @REQ_STATUS_SENT: request sent to server
  57. * @REQ_STATUS_RCVD: response received from server
  58. * @REQ_STATUS_FLSH: request has been flushed
  59. *
  60. * The @REQ_STATUS_IDLE state is used to mark a request slot as unused
  61. * but use is actually tracked by the idpool structure which handles tag
  62. * id allocation.
  63. *
  64. */
  65. enum p9_req_status_t {
  66. REQ_STATUS_IDLE,
  67. REQ_STATUS_SENT,
  68. REQ_STATUS_RCVD,
  69. REQ_STATUS_FLSH,
  70. };
  71. /**
  72. * struct p9_req_t - virtio request slots
  73. * @status: status of this request slot
  74. * @wq: wait_queue for the client to block on for this request
  75. *
  76. * The virtio transport uses an array to track outstanding requests
  77. * instead of a list. While this may incurr overhead during initial
  78. * allocation or expansion, it makes request lookup much easier as the
  79. * tag id is a index into an array. (We use tag+1 so that we can accomodate
  80. * the -1 tag for the T_VERSION request).
  81. * This also has the nice effect of only having to allocate wait_queues
  82. * once, instead of constantly allocating and freeing them. Its possible
  83. * other resources could benefit from this scheme as well.
  84. *
  85. */
  86. struct p9_req_t {
  87. int status;
  88. wait_queue_head_t *wq;
  89. };
  90. /**
  91. * struct virtio_chan - per-instance transport information
  92. * @initialized: whether the channel is initialized
  93. * @inuse: whether the channel is in use
  94. * @lock: protects multiple elements within this structure
  95. * @vdev: virtio dev associated with this channel
  96. * @vq: virtio queue associated with this channel
  97. * @tagpool: accounting for tag ids (and request slots)
  98. * @reqs: array of request slots
  99. * @max_tag: current number of request_slots allocated
  100. * @sg: scatter gather list which is used to pack a request (protected?)
  101. *
  102. * We keep all per-channel information in a structure.
  103. * This structure is allocated within the devices dev->mem space.
  104. * A pointer to the structure will get put in the transport private.
  105. *
  106. */
  107. static struct virtio_chan {
  108. bool initialized;
  109. bool inuse;
  110. spinlock_t lock;
  111. struct virtio_device *vdev;
  112. struct virtqueue *vq;
  113. struct p9_idpool *tagpool;
  114. struct p9_req_t *reqs;
  115. int max_tag;
  116. /* Scatterlist: can be too big for stack. */
  117. struct scatterlist sg[VIRTQUEUE_NUM];
  118. } channels[MAX_9P_CHAN];
  119. /**
  120. * p9_lookup_tag - Lookup requests by tag
  121. * @c: virtio channel to lookup tag within
  122. * @tag: numeric id for transaction
  123. *
  124. * this is a simple array lookup, but will grow the
  125. * request_slots as necessary to accomodate transaction
  126. * ids which did not previously have a slot.
  127. *
  128. * Bugs: there is currently no upper limit on request slots set
  129. * here, but that should be constrained by the id accounting.
  130. */
  131. static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
  132. {
  133. /* This looks up the original request by tag so we know which
  134. * buffer to read the data into */
  135. tag++;
  136. while (tag >= c->max_tag) {
  137. int old_max = c->max_tag;
  138. int count;
  139. if (c->max_tag)
  140. c->max_tag *= 2;
  141. else
  142. c->max_tag = P9_INIT_MAXTAG;
  143. c->reqs = krealloc(c->reqs, sizeof(struct p9_req_t)*c->max_tag,
  144. GFP_ATOMIC);
  145. if (!c->reqs) {
  146. printk(KERN_ERR "Couldn't grow tag array\n");
  147. BUG();
  148. }
  149. for (count = old_max; count < c->max_tag; count++) {
  150. c->reqs[count].status = REQ_STATUS_IDLE;
  151. c->reqs[count].wq = kmalloc(sizeof(wait_queue_head_t),
  152. GFP_ATOMIC);
  153. if (!c->reqs[count].wq) {
  154. printk(KERN_ERR "Couldn't grow tag array\n");
  155. BUG();
  156. }
  157. init_waitqueue_head(c->reqs[count].wq);
  158. }
  159. }
  160. return &c->reqs[tag];
  161. }
  162. /* How many bytes left in this page. */
  163. static unsigned int rest_of_page(void *data)
  164. {
  165. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  166. }
  167. /**
  168. * p9_virtio_close - reclaim resources of a channel
  169. * @trans: transport state
  170. *
  171. * This reclaims a channel by freeing its resources and
  172. * reseting its inuse flag.
  173. *
  174. */
  175. static void p9_virtio_close(struct p9_trans *trans)
  176. {
  177. struct virtio_chan *chan = trans->priv;
  178. int count;
  179. unsigned long flags;
  180. spin_lock_irqsave(&chan->lock, flags);
  181. p9_idpool_destroy(chan->tagpool);
  182. for (count = 0; count < chan->max_tag; count++)
  183. kfree(chan->reqs[count].wq);
  184. kfree(chan->reqs);
  185. chan->max_tag = 0;
  186. spin_unlock_irqrestore(&chan->lock, flags);
  187. mutex_lock(&virtio_9p_lock);
  188. chan->inuse = false;
  189. mutex_unlock(&virtio_9p_lock);
  190. kfree(trans);
  191. }
  192. /**
  193. * req_done - callback which signals activity from the server
  194. * @vq: virtio queue activity was received on
  195. *
  196. * This notifies us that the server has triggered some activity
  197. * on the virtio channel - most likely a response to request we
  198. * sent. Figure out which requests now have responses and wake up
  199. * those threads.
  200. *
  201. * Bugs: could do with some additional sanity checking, but appears to work.
  202. *
  203. */
  204. static void req_done(struct virtqueue *vq)
  205. {
  206. struct virtio_chan *chan = vq->vdev->priv;
  207. struct p9_fcall *rc;
  208. unsigned int len;
  209. unsigned long flags;
  210. struct p9_req_t *req;
  211. spin_lock_irqsave(&chan->lock, flags);
  212. while ((rc = chan->vq->vq_ops->get_buf(chan->vq, &len)) != NULL) {
  213. req = p9_lookup_tag(chan, rc->tag);
  214. req->status = REQ_STATUS_RCVD;
  215. wake_up(req->wq);
  216. }
  217. /* In case queue is stopped waiting for more buffers. */
  218. spin_unlock_irqrestore(&chan->lock, flags);
  219. }
  220. /**
  221. * pack_sg_list - pack a scatter gather list from a linear buffer
  222. * @sg: scatter/gather list to pack into
  223. * @start: which segment of the sg_list to start at
  224. * @limit: maximum segment to pack data to
  225. * @data: data to pack into scatter/gather list
  226. * @count: amount of data to pack into the scatter/gather list
  227. *
  228. * sg_lists have multiple segments of various sizes. This will pack
  229. * arbitrary data into an existing scatter gather list, segmenting the
  230. * data as necessary within constraints.
  231. *
  232. */
  233. static int
  234. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  235. int count)
  236. {
  237. int s;
  238. int index = start;
  239. while (count) {
  240. s = rest_of_page(data);
  241. if (s > count)
  242. s = count;
  243. sg_set_buf(&sg[index++], data, s);
  244. count -= s;
  245. data += s;
  246. BUG_ON(index > limit);
  247. }
  248. return index-start;
  249. }
  250. /**
  251. * p9_virtio_rpc - issue a request and wait for a response
  252. * @t: transport state
  253. * @tc: &p9_fcall request to transmit
  254. * @rc: &p9_fcall to put reponse into
  255. *
  256. */
  257. static int
  258. p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
  259. {
  260. int in, out;
  261. int n, err, size;
  262. struct virtio_chan *chan = t->priv;
  263. char *rdata;
  264. struct p9_req_t *req;
  265. unsigned long flags;
  266. if (*rc == NULL) {
  267. *rc = kmalloc(sizeof(struct p9_fcall) + t->msize, GFP_KERNEL);
  268. if (!*rc)
  269. return -ENOMEM;
  270. }
  271. rdata = (char *)*rc+sizeof(struct p9_fcall);
  272. n = P9_NOTAG;
  273. if (tc->id != P9_TVERSION) {
  274. n = p9_idpool_get(chan->tagpool);
  275. if (n < 0)
  276. return -ENOMEM;
  277. }
  278. spin_lock_irqsave(&chan->lock, flags);
  279. req = p9_lookup_tag(chan, n);
  280. spin_unlock_irqrestore(&chan->lock, flags);
  281. p9_set_tag(tc, n);
  282. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio rpc tag %d\n", n);
  283. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, tc->sdata, tc->size);
  284. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata, t->msize);
  285. req->status = REQ_STATUS_SENT;
  286. if (chan->vq->vq_ops->add_buf(chan->vq, chan->sg, out, in, tc)) {
  287. P9_DPRINTK(P9_DEBUG_TRANS,
  288. "9p debug: virtio rpc add_buf returned failure");
  289. return -EIO;
  290. }
  291. chan->vq->vq_ops->kick(chan->vq);
  292. wait_event(*req->wq, req->status == REQ_STATUS_RCVD);
  293. size = le32_to_cpu(*(__le32 *) rdata);
  294. err = p9_deserialize_fcall(rdata, size, *rc, t->extended);
  295. if (err < 0) {
  296. P9_DPRINTK(P9_DEBUG_TRANS,
  297. "9p debug: virtio rpc deserialize returned %d\n", err);
  298. return err;
  299. }
  300. #ifdef CONFIG_NET_9P_DEBUG
  301. if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
  302. char buf[150];
  303. p9_printfcall(buf, sizeof(buf), *rc, t->extended);
  304. printk(KERN_NOTICE ">>> %p %s\n", t, buf);
  305. }
  306. #endif
  307. if (n != P9_NOTAG && p9_idpool_check(n, chan->tagpool))
  308. p9_idpool_put(n, chan->tagpool);
  309. req->status = REQ_STATUS_IDLE;
  310. return 0;
  311. }
  312. /**
  313. * p9_virtio_probe - probe for existence of 9P virtio channels
  314. * @vdev: virtio device to probe
  315. *
  316. * This probes for existing virtio channels. At present only
  317. * a single channel is in use, so in the future more work may need
  318. * to be done here.
  319. *
  320. */
  321. static int p9_virtio_probe(struct virtio_device *vdev)
  322. {
  323. int err;
  324. struct virtio_chan *chan;
  325. int index;
  326. mutex_lock(&virtio_9p_lock);
  327. index = chan_index++;
  328. chan = &channels[index];
  329. mutex_unlock(&virtio_9p_lock);
  330. if (chan_index > MAX_9P_CHAN) {
  331. printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
  332. BUG();
  333. err = -ENOMEM;
  334. goto fail;
  335. }
  336. chan->vdev = vdev;
  337. /* We expect one virtqueue, for requests. */
  338. chan->vq = vdev->config->find_vq(vdev, 0, req_done);
  339. if (IS_ERR(chan->vq)) {
  340. err = PTR_ERR(chan->vq);
  341. goto out_free_vq;
  342. }
  343. chan->vq->vdev->priv = chan;
  344. spin_lock_init(&chan->lock);
  345. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  346. chan->inuse = false;
  347. chan->initialized = true;
  348. return 0;
  349. out_free_vq:
  350. vdev->config->del_vq(chan->vq);
  351. fail:
  352. mutex_lock(&virtio_9p_lock);
  353. chan_index--;
  354. mutex_unlock(&virtio_9p_lock);
  355. return err;
  356. }
  357. /**
  358. * p9_virtio_create - allocate a new virtio channel
  359. * @devname: string identifying the channel to connect to (unused)
  360. * @args: args passed from sys_mount() for per-transport options (unused)
  361. * @msize: requested maximum packet size
  362. * @extended: 9p2000.u enabled flag
  363. *
  364. * This sets up a transport channel for 9p communication. Right now
  365. * we only match the first available channel, but eventually we couldlook up
  366. * alternate channels by matching devname versus a virtio_config entry.
  367. * We use a simple reference count mechanism to ensure that only a single
  368. * mount has a channel open at a time.
  369. *
  370. * Bugs: doesn't allow identification of a specific channel
  371. * to allocate, channels are allocated sequentially. This was
  372. * a pragmatic decision to get things rolling, but ideally some
  373. * way of identifying the channel to attach to would be nice
  374. * if we are going to support multiple channels.
  375. *
  376. */
  377. static struct p9_trans *
  378. p9_virtio_create(const char *devname, char *args, int msize,
  379. unsigned char extended)
  380. {
  381. struct p9_trans *trans;
  382. struct virtio_chan *chan = channels;
  383. int index = 0;
  384. mutex_lock(&virtio_9p_lock);
  385. while (index < MAX_9P_CHAN) {
  386. if (chan->initialized && !chan->inuse) {
  387. chan->inuse = true;
  388. break;
  389. } else {
  390. index++;
  391. chan = &channels[index];
  392. }
  393. }
  394. mutex_unlock(&virtio_9p_lock);
  395. if (index >= MAX_9P_CHAN) {
  396. printk(KERN_ERR "9p: no channels available\n");
  397. return ERR_PTR(-ENODEV);
  398. }
  399. chan->tagpool = p9_idpool_create();
  400. if (IS_ERR(chan->tagpool)) {
  401. printk(KERN_ERR "9p: couldn't allocate tagpool\n");
  402. return ERR_PTR(-ENOMEM);
  403. }
  404. p9_idpool_get(chan->tagpool); /* reserve tag 0 */
  405. chan->max_tag = 0;
  406. chan->reqs = NULL;
  407. trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
  408. if (!trans) {
  409. printk(KERN_ERR "9p: couldn't allocate transport\n");
  410. return ERR_PTR(-ENOMEM);
  411. }
  412. trans->extended = extended;
  413. trans->msize = msize;
  414. trans->close = p9_virtio_close;
  415. trans->rpc = p9_virtio_rpc;
  416. trans->priv = chan;
  417. return trans;
  418. }
  419. /**
  420. * p9_virtio_remove - clean up resources associated with a virtio device
  421. * @vdev: virtio device to remove
  422. *
  423. */
  424. static void p9_virtio_remove(struct virtio_device *vdev)
  425. {
  426. struct virtio_chan *chan = vdev->priv;
  427. BUG_ON(chan->inuse);
  428. if (chan->initialized) {
  429. vdev->config->del_vq(chan->vq);
  430. chan->initialized = false;
  431. }
  432. }
  433. #define VIRTIO_ID_9P 9
  434. static struct virtio_device_id id_table[] = {
  435. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  436. { 0 },
  437. };
  438. /* The standard "struct lguest_driver": */
  439. static struct virtio_driver p9_virtio_drv = {
  440. .driver.name = KBUILD_MODNAME,
  441. .driver.owner = THIS_MODULE,
  442. .id_table = id_table,
  443. .probe = p9_virtio_probe,
  444. .remove = p9_virtio_remove,
  445. };
  446. static struct p9_trans_module p9_virtio_trans = {
  447. .name = "virtio",
  448. .create = p9_virtio_create,
  449. .maxsize = PAGE_SIZE*16,
  450. .def = 0,
  451. .owner = THIS_MODULE,
  452. };
  453. /* The standard init function */
  454. static int __init p9_virtio_init(void)
  455. {
  456. int count;
  457. for (count = 0; count < MAX_9P_CHAN; count++)
  458. channels[count].initialized = false;
  459. v9fs_register_trans(&p9_virtio_trans);
  460. return register_virtio_driver(&p9_virtio_drv);
  461. }
  462. static void __exit p9_virtio_cleanup(void)
  463. {
  464. unregister_virtio_driver(&p9_virtio_drv);
  465. v9fs_unregister_trans(&p9_virtio_trans);
  466. }
  467. module_init(p9_virtio_init);
  468. module_exit(p9_virtio_cleanup);
  469. MODULE_DEVICE_TABLE(virtio, id_table);
  470. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  471. MODULE_DESCRIPTION("Virtio 9p Transport");
  472. MODULE_LICENSE("GPL");