trans_virtio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * The Virtio 9p transport driver
  3. *
  4. * This is a block based transport driver based on the lguest block driver
  5. * code.
  6. *
  7. * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
  8. *
  9. * Based on virtio console driver
  10. * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to:
  23. * Free Software Foundation
  24. * 51 Franklin Street, Fifth Floor
  25. * Boston, MA 02111-1301 USA
  26. *
  27. */
  28. #include <linux/in.h>
  29. #include <linux/module.h>
  30. #include <linux/net.h>
  31. #include <linux/ipv6.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/un.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/inet.h>
  37. #include <linux/idr.h>
  38. #include <linux/file.h>
  39. #include <linux/slab.h>
  40. #include <net/9p/9p.h>
  41. #include <linux/parser.h>
  42. #include <net/9p/client.h>
  43. #include <net/9p/transport.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/virtio.h>
  46. #include <linux/virtio_9p.h>
  47. #include "trans_common.h"
  48. #define VIRTQUEUE_NUM 128
  49. /* a single mutex to manage channel initialization and attachment */
  50. static DEFINE_MUTEX(virtio_9p_lock);
  51. /**
  52. * struct virtio_chan - per-instance transport information
  53. * @initialized: whether the channel is initialized
  54. * @inuse: whether the channel is in use
  55. * @lock: protects multiple elements within this structure
  56. * @client: client instance
  57. * @vdev: virtio dev associated with this channel
  58. * @vq: virtio queue associated with this channel
  59. * @sg: scatter gather list which is used to pack a request (protected?)
  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. */
  66. struct virtio_chan {
  67. bool inuse;
  68. spinlock_t lock;
  69. struct p9_client *client;
  70. struct virtio_device *vdev;
  71. struct virtqueue *vq;
  72. int ring_bufs_avail;
  73. wait_queue_head_t *vc_wq;
  74. /* Scatterlist: can be too big for stack. */
  75. struct scatterlist sg[VIRTQUEUE_NUM];
  76. int tag_len;
  77. /*
  78. * tag name to identify a mount Non-null terminated
  79. */
  80. char *tag;
  81. struct list_head chan_list;
  82. };
  83. static struct list_head virtio_chan_list;
  84. /* How many bytes left in this page. */
  85. static unsigned int rest_of_page(void *data)
  86. {
  87. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  88. }
  89. /**
  90. * p9_virtio_close - reclaim resources of a channel
  91. * @client: client instance
  92. *
  93. * This reclaims a channel by freeing its resources and
  94. * reseting its inuse flag.
  95. *
  96. */
  97. static void p9_virtio_close(struct p9_client *client)
  98. {
  99. struct virtio_chan *chan = client->trans;
  100. mutex_lock(&virtio_9p_lock);
  101. if (chan)
  102. chan->inuse = false;
  103. mutex_unlock(&virtio_9p_lock);
  104. }
  105. /**
  106. * req_done - callback which signals activity from the server
  107. * @vq: virtio queue activity was received on
  108. *
  109. * This notifies us that the server has triggered some activity
  110. * on the virtio channel - most likely a response to request we
  111. * sent. Figure out which requests now have responses and wake up
  112. * those threads.
  113. *
  114. * Bugs: could do with some additional sanity checking, but appears to work.
  115. *
  116. */
  117. static void req_done(struct virtqueue *vq)
  118. {
  119. struct virtio_chan *chan = vq->vdev->priv;
  120. struct p9_fcall *rc;
  121. unsigned int len;
  122. struct p9_req_t *req;
  123. unsigned long flags;
  124. P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n");
  125. do {
  126. spin_lock_irqsave(&chan->lock, flags);
  127. rc = virtqueue_get_buf(chan->vq, &len);
  128. if (rc != NULL) {
  129. chan->ring_bufs_avail = 1;
  130. spin_unlock_irqrestore(&chan->lock, flags);
  131. /* Wakeup if anyone waiting for VirtIO ring space. */
  132. wake_up(chan->vc_wq);
  133. P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
  134. P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n",
  135. rc->tag);
  136. req = p9_tag_lookup(chan->client, rc->tag);
  137. req->status = REQ_STATUS_RCVD;
  138. if (req->tc->private) {
  139. struct trans_rpage_info *rp = req->tc->private;
  140. /*Release pages */
  141. p9_release_req_pages(rp);
  142. if (rp->rp_alloc)
  143. kfree(rp);
  144. req->tc->private = NULL;
  145. }
  146. p9_client_cb(chan->client, req);
  147. } else {
  148. spin_unlock_irqrestore(&chan->lock, flags);
  149. }
  150. } while (rc != NULL);
  151. }
  152. /**
  153. * pack_sg_list - pack a scatter gather list from a linear buffer
  154. * @sg: scatter/gather list to pack into
  155. * @start: which segment of the sg_list to start at
  156. * @limit: maximum segment to pack data to
  157. * @data: data to pack into scatter/gather list
  158. * @count: amount of data to pack into the scatter/gather list
  159. *
  160. * sg_lists have multiple segments of various sizes. This will pack
  161. * arbitrary data into an existing scatter gather list, segmenting the
  162. * data as necessary within constraints.
  163. *
  164. */
  165. static int
  166. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  167. int count)
  168. {
  169. int s;
  170. int index = start;
  171. while (count) {
  172. s = rest_of_page(data);
  173. if (s > count)
  174. s = count;
  175. sg_set_buf(&sg[index++], data, s);
  176. count -= s;
  177. data += s;
  178. BUG_ON(index > limit);
  179. }
  180. return index-start;
  181. }
  182. /* We don't currently allow canceling of virtio requests */
  183. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  184. {
  185. return 1;
  186. }
  187. /**
  188. * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
  189. * this takes a list of pages.
  190. * @sg: scatter/gather list to pack into
  191. * @start: which segment of the sg_list to start at
  192. * @pdata_off: Offset into the first page
  193. * @**pdata: a list of pages to add into sg.
  194. * @count: amount of data to pack into the scatter/gather list
  195. */
  196. static int
  197. pack_sg_list_p(struct scatterlist *sg, int start, int limit, size_t pdata_off,
  198. struct page **pdata, int count)
  199. {
  200. int s;
  201. int i = 0;
  202. int index = start;
  203. if (pdata_off) {
  204. s = min((int)(PAGE_SIZE - pdata_off), count);
  205. sg_set_page(&sg[index++], pdata[i++], s, pdata_off);
  206. count -= s;
  207. }
  208. while (count) {
  209. BUG_ON(index > limit);
  210. s = min((int)PAGE_SIZE, count);
  211. sg_set_page(&sg[index++], pdata[i++], s, 0);
  212. count -= s;
  213. }
  214. return index-start;
  215. }
  216. /**
  217. * p9_virtio_request - issue a request
  218. * @client: client instance issuing the request
  219. * @req: request to be issued
  220. *
  221. */
  222. static int
  223. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  224. {
  225. int in, out, inp, outp;
  226. struct virtio_chan *chan = client->trans;
  227. char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
  228. unsigned long flags;
  229. size_t pdata_off = 0;
  230. struct trans_rpage_info *rpinfo = NULL;
  231. int err, pdata_len = 0;
  232. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  233. req_retry:
  234. req->status = REQ_STATUS_SENT;
  235. if (req->tc->pbuf_size && (req->tc->pubuf && P9_IS_USER_CONTEXT)) {
  236. int nr_pages = p9_nr_pages(req);
  237. int rpinfo_size = sizeof(struct trans_rpage_info) +
  238. sizeof(struct page *) * nr_pages;
  239. if (rpinfo_size <= (req->tc->capacity - req->tc->size)) {
  240. /* We can use sdata */
  241. req->tc->private = req->tc->sdata + req->tc->size;
  242. rpinfo = (struct trans_rpage_info *)req->tc->private;
  243. rpinfo->rp_alloc = 0;
  244. } else {
  245. req->tc->private = kmalloc(rpinfo_size, GFP_NOFS);
  246. if (!req->tc->private) {
  247. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: "
  248. "private kmalloc returned NULL");
  249. return -ENOMEM;
  250. }
  251. rpinfo = (struct trans_rpage_info *)req->tc->private;
  252. rpinfo->rp_alloc = 1;
  253. }
  254. err = p9_payload_gup(req, &pdata_off, &pdata_len, nr_pages,
  255. req->tc->id == P9_TREAD ? 1 : 0);
  256. if (err < 0) {
  257. if (rpinfo->rp_alloc)
  258. kfree(rpinfo);
  259. return err;
  260. }
  261. }
  262. spin_lock_irqsave(&chan->lock, flags);
  263. /* Handle out VirtIO ring buffers */
  264. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
  265. req->tc->size);
  266. if (req->tc->pbuf_size && (req->tc->id == P9_TWRITE)) {
  267. /* We have additional write payload buffer to take care */
  268. if (req->tc->pubuf && P9_IS_USER_CONTEXT) {
  269. outp = pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
  270. pdata_off, rpinfo->rp_data, pdata_len);
  271. } else {
  272. char *pbuf = req->tc->pubuf ? req->tc->pubuf :
  273. req->tc->pkbuf;
  274. outp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, pbuf,
  275. req->tc->pbuf_size);
  276. }
  277. out += outp;
  278. }
  279. /* Handle in VirtIO ring buffers */
  280. if (req->tc->pbuf_size &&
  281. ((req->tc->id == P9_TREAD) || (req->tc->id == P9_TREADDIR))) {
  282. /*
  283. * Take care of additional Read payload.
  284. * 11 is the read/write header = PDU Header(7) + IO Size (4).
  285. * Arrange in such a way that server places header in the
  286. * alloced memory and payload onto the user buffer.
  287. */
  288. inp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata, 11);
  289. /*
  290. * Running executables in the filesystem may result in
  291. * a read request with kernel buffer as opposed to user buffer.
  292. */
  293. if (req->tc->pubuf && P9_IS_USER_CONTEXT) {
  294. in = pack_sg_list_p(chan->sg, out+inp, VIRTQUEUE_NUM,
  295. pdata_off, rpinfo->rp_data, pdata_len);
  296. } else {
  297. char *pbuf = req->tc->pubuf ? req->tc->pubuf :
  298. req->tc->pkbuf;
  299. in = pack_sg_list(chan->sg, out+inp, VIRTQUEUE_NUM,
  300. pbuf, req->tc->pbuf_size);
  301. }
  302. in += inp;
  303. } else {
  304. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata,
  305. client->msize);
  306. }
  307. err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc);
  308. if (err < 0) {
  309. if (err == -ENOSPC) {
  310. chan->ring_bufs_avail = 0;
  311. spin_unlock_irqrestore(&chan->lock, flags);
  312. err = wait_event_interruptible(*chan->vc_wq,
  313. chan->ring_bufs_avail);
  314. if (err == -ERESTARTSYS)
  315. return err;
  316. P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n");
  317. goto req_retry;
  318. } else {
  319. spin_unlock_irqrestore(&chan->lock, flags);
  320. P9_DPRINTK(P9_DEBUG_TRANS,
  321. "9p debug: "
  322. "virtio rpc add_buf returned failure");
  323. if (rpinfo && rpinfo->rp_alloc)
  324. kfree(rpinfo);
  325. return -EIO;
  326. }
  327. }
  328. virtqueue_kick(chan->vq);
  329. spin_unlock_irqrestore(&chan->lock, flags);
  330. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
  331. return 0;
  332. }
  333. static ssize_t p9_mount_tag_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct virtio_chan *chan;
  337. struct virtio_device *vdev;
  338. vdev = dev_to_virtio(dev);
  339. chan = vdev->priv;
  340. return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
  341. }
  342. static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
  343. /**
  344. * p9_virtio_probe - probe for existence of 9P virtio channels
  345. * @vdev: virtio device to probe
  346. *
  347. * This probes for existing virtio channels.
  348. *
  349. */
  350. static int p9_virtio_probe(struct virtio_device *vdev)
  351. {
  352. __u16 tag_len;
  353. char *tag;
  354. int err;
  355. struct virtio_chan *chan;
  356. chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
  357. if (!chan) {
  358. printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n");
  359. err = -ENOMEM;
  360. goto fail;
  361. }
  362. chan->vdev = vdev;
  363. /* We expect one virtqueue, for requests. */
  364. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  365. if (IS_ERR(chan->vq)) {
  366. err = PTR_ERR(chan->vq);
  367. goto out_free_vq;
  368. }
  369. chan->vq->vdev->priv = chan;
  370. spin_lock_init(&chan->lock);
  371. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  372. chan->inuse = false;
  373. if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
  374. vdev->config->get(vdev,
  375. offsetof(struct virtio_9p_config, tag_len),
  376. &tag_len, sizeof(tag_len));
  377. } else {
  378. err = -EINVAL;
  379. goto out_free_vq;
  380. }
  381. tag = kmalloc(tag_len, GFP_KERNEL);
  382. if (!tag) {
  383. err = -ENOMEM;
  384. goto out_free_vq;
  385. }
  386. vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
  387. tag, tag_len);
  388. chan->tag = tag;
  389. chan->tag_len = tag_len;
  390. err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  391. if (err) {
  392. goto out_free_tag;
  393. }
  394. chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
  395. if (!chan->vc_wq) {
  396. err = -ENOMEM;
  397. goto out_free_tag;
  398. }
  399. init_waitqueue_head(chan->vc_wq);
  400. chan->ring_bufs_avail = 1;
  401. mutex_lock(&virtio_9p_lock);
  402. list_add_tail(&chan->chan_list, &virtio_chan_list);
  403. mutex_unlock(&virtio_9p_lock);
  404. return 0;
  405. out_free_tag:
  406. kfree(tag);
  407. out_free_vq:
  408. vdev->config->del_vqs(vdev);
  409. kfree(chan);
  410. fail:
  411. return err;
  412. }
  413. /**
  414. * p9_virtio_create - allocate a new virtio channel
  415. * @client: client instance invoking this transport
  416. * @devname: string identifying the channel to connect to (unused)
  417. * @args: args passed from sys_mount() for per-transport options (unused)
  418. *
  419. * This sets up a transport channel for 9p communication. Right now
  420. * we only match the first available channel, but eventually we couldlook up
  421. * alternate channels by matching devname versus a virtio_config entry.
  422. * We use a simple reference count mechanism to ensure that only a single
  423. * mount has a channel open at a time.
  424. *
  425. */
  426. static int
  427. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  428. {
  429. struct virtio_chan *chan;
  430. int ret = -ENOENT;
  431. int found = 0;
  432. mutex_lock(&virtio_9p_lock);
  433. list_for_each_entry(chan, &virtio_chan_list, chan_list) {
  434. if (!strncmp(devname, chan->tag, chan->tag_len) &&
  435. strlen(devname) == chan->tag_len) {
  436. if (!chan->inuse) {
  437. chan->inuse = true;
  438. found = 1;
  439. break;
  440. }
  441. ret = -EBUSY;
  442. }
  443. }
  444. mutex_unlock(&virtio_9p_lock);
  445. if (!found) {
  446. printk(KERN_ERR "9p: no channels available\n");
  447. return ret;
  448. }
  449. client->trans = (void *)chan;
  450. client->status = Connected;
  451. chan->client = client;
  452. return 0;
  453. }
  454. /**
  455. * p9_virtio_remove - clean up resources associated with a virtio device
  456. * @vdev: virtio device to remove
  457. *
  458. */
  459. static void p9_virtio_remove(struct virtio_device *vdev)
  460. {
  461. struct virtio_chan *chan = vdev->priv;
  462. BUG_ON(chan->inuse);
  463. vdev->config->del_vqs(vdev);
  464. mutex_lock(&virtio_9p_lock);
  465. list_del(&chan->chan_list);
  466. mutex_unlock(&virtio_9p_lock);
  467. sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  468. kfree(chan->tag);
  469. kfree(chan->vc_wq);
  470. kfree(chan);
  471. }
  472. static struct virtio_device_id id_table[] = {
  473. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  474. { 0 },
  475. };
  476. static unsigned int features[] = {
  477. VIRTIO_9P_MOUNT_TAG,
  478. };
  479. /* The standard "struct lguest_driver": */
  480. static struct virtio_driver p9_virtio_drv = {
  481. .feature_table = features,
  482. .feature_table_size = ARRAY_SIZE(features),
  483. .driver.name = KBUILD_MODNAME,
  484. .driver.owner = THIS_MODULE,
  485. .id_table = id_table,
  486. .probe = p9_virtio_probe,
  487. .remove = p9_virtio_remove,
  488. };
  489. static struct p9_trans_module p9_virtio_trans = {
  490. .name = "virtio",
  491. .create = p9_virtio_create,
  492. .close = p9_virtio_close,
  493. .request = p9_virtio_request,
  494. .cancel = p9_virtio_cancel,
  495. .maxsize = PAGE_SIZE*16,
  496. .pref = P9_TRANS_PREF_PAYLOAD_SEP,
  497. .def = 0,
  498. .owner = THIS_MODULE,
  499. };
  500. /* The standard init function */
  501. static int __init p9_virtio_init(void)
  502. {
  503. INIT_LIST_HEAD(&virtio_chan_list);
  504. v9fs_register_trans(&p9_virtio_trans);
  505. return register_virtio_driver(&p9_virtio_drv);
  506. }
  507. static void __exit p9_virtio_cleanup(void)
  508. {
  509. unregister_virtio_driver(&p9_virtio_drv);
  510. v9fs_unregister_trans(&p9_virtio_trans);
  511. }
  512. module_init(p9_virtio_init);
  513. module_exit(p9_virtio_cleanup);
  514. MODULE_DEVICE_TABLE(virtio, id_table);
  515. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  516. MODULE_DESCRIPTION("Virtio 9p Transport");
  517. MODULE_LICENSE("GPL");