trans_virtio.c 14 KB

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