trans_virtio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. if (!chan->ring_bufs_avail) {
  130. chan->ring_bufs_avail = 1;
  131. wake_up(chan->vc_wq);
  132. }
  133. spin_unlock_irqrestore(&chan->lock, flags);
  134. P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc);
  135. P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n",
  136. rc->tag);
  137. req = p9_tag_lookup(chan->client, rc->tag);
  138. req->status = REQ_STATUS_RCVD;
  139. if (req->tc->private) {
  140. struct trans_rpage_info *rp = req->tc->private;
  141. /*Release pages */
  142. p9_release_req_pages(rp);
  143. if (rp->rp_alloc)
  144. kfree(rp);
  145. req->tc->private = NULL;
  146. }
  147. p9_client_cb(chan->client, req);
  148. } else {
  149. spin_unlock_irqrestore(&chan->lock, flags);
  150. }
  151. } while (rc != NULL);
  152. }
  153. /**
  154. * pack_sg_list - pack a scatter gather list from a linear buffer
  155. * @sg: scatter/gather list to pack into
  156. * @start: which segment of the sg_list to start at
  157. * @limit: maximum segment to pack data to
  158. * @data: data to pack into scatter/gather list
  159. * @count: amount of data to pack into the scatter/gather list
  160. *
  161. * sg_lists have multiple segments of various sizes. This will pack
  162. * arbitrary data into an existing scatter gather list, segmenting the
  163. * data as necessary within constraints.
  164. *
  165. */
  166. static int
  167. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  168. int count)
  169. {
  170. int s;
  171. int index = start;
  172. while (count) {
  173. s = rest_of_page(data);
  174. if (s > count)
  175. s = count;
  176. sg_set_buf(&sg[index++], data, s);
  177. count -= s;
  178. data += s;
  179. BUG_ON(index > limit);
  180. }
  181. return index-start;
  182. }
  183. /* We don't currently allow canceling of virtio requests */
  184. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  185. {
  186. return 1;
  187. }
  188. /**
  189. * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
  190. * this takes a list of pages.
  191. * @sg: scatter/gather list to pack into
  192. * @start: which segment of the sg_list to start at
  193. * @pdata_off: Offset into the first page
  194. * @**pdata: a list of pages to add into sg.
  195. * @count: amount of data to pack into the scatter/gather list
  196. */
  197. static int
  198. pack_sg_list_p(struct scatterlist *sg, int start, int limit, size_t pdata_off,
  199. struct page **pdata, int count)
  200. {
  201. int s;
  202. int i = 0;
  203. int index = start;
  204. if (pdata_off) {
  205. s = min((int)(PAGE_SIZE - pdata_off), count);
  206. sg_set_page(&sg[index++], pdata[i++], s, pdata_off);
  207. count -= s;
  208. }
  209. while (count) {
  210. BUG_ON(index > limit);
  211. s = min((int)PAGE_SIZE, count);
  212. sg_set_page(&sg[index++], pdata[i++], s, 0);
  213. count -= s;
  214. }
  215. return index-start;
  216. }
  217. /**
  218. * p9_virtio_request - issue a request
  219. * @client: client instance issuing the request
  220. * @req: request to be issued
  221. *
  222. */
  223. static int
  224. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  225. {
  226. int in, out, inp, outp;
  227. struct virtio_chan *chan = client->trans;
  228. char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
  229. unsigned long flags;
  230. size_t pdata_off = 0;
  231. struct trans_rpage_info *rpinfo = NULL;
  232. int err, pdata_len = 0;
  233. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  234. req_retry:
  235. req->status = REQ_STATUS_SENT;
  236. if (req->tc->pbuf_size && (req->tc->pubuf && P9_IS_USER_CONTEXT)) {
  237. int nr_pages = p9_nr_pages(req);
  238. int rpinfo_size = sizeof(struct trans_rpage_info) +
  239. sizeof(struct page *) * nr_pages;
  240. if (rpinfo_size <= (req->tc->capacity - req->tc->size)) {
  241. /* We can use sdata */
  242. req->tc->private = req->tc->sdata + req->tc->size;
  243. rpinfo = (struct trans_rpage_info *)req->tc->private;
  244. rpinfo->rp_alloc = 0;
  245. } else {
  246. req->tc->private = kmalloc(rpinfo_size, GFP_NOFS);
  247. if (!req->tc->private) {
  248. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: "
  249. "private kmalloc returned NULL");
  250. return -ENOMEM;
  251. }
  252. rpinfo = (struct trans_rpage_info *)req->tc->private;
  253. rpinfo->rp_alloc = 1;
  254. }
  255. err = p9_payload_gup(req, &pdata_off, &pdata_len, nr_pages,
  256. req->tc->id == P9_TREAD ? 1 : 0);
  257. if (err < 0) {
  258. if (rpinfo->rp_alloc)
  259. kfree(rpinfo);
  260. return err;
  261. }
  262. }
  263. spin_lock_irqsave(&chan->lock, flags);
  264. /* Handle out VirtIO ring buffers */
  265. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
  266. req->tc->size);
  267. if (req->tc->pbuf_size && (req->tc->id == P9_TWRITE)) {
  268. /* We have additional write payload buffer to take care */
  269. if (req->tc->pubuf && P9_IS_USER_CONTEXT) {
  270. outp = pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
  271. pdata_off, rpinfo->rp_data, pdata_len);
  272. } else {
  273. char *pbuf = req->tc->pubuf ? req->tc->pubuf :
  274. req->tc->pkbuf;
  275. outp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, pbuf,
  276. req->tc->pbuf_size);
  277. }
  278. out += outp;
  279. }
  280. /* Handle in VirtIO ring buffers */
  281. if (req->tc->pbuf_size &&
  282. ((req->tc->id == P9_TREAD) || (req->tc->id == P9_TREADDIR))) {
  283. /*
  284. * Take care of additional Read payload.
  285. * 11 is the read/write header = PDU Header(7) + IO Size (4).
  286. * Arrange in such a way that server places header in the
  287. * alloced memory and payload onto the user buffer.
  288. */
  289. inp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata, 11);
  290. /*
  291. * Running executables in the filesystem may result in
  292. * a read request with kernel buffer as opposed to user buffer.
  293. */
  294. if (req->tc->pubuf && P9_IS_USER_CONTEXT) {
  295. in = pack_sg_list_p(chan->sg, out+inp, VIRTQUEUE_NUM,
  296. pdata_off, rpinfo->rp_data, pdata_len);
  297. } else {
  298. char *pbuf = req->tc->pubuf ? req->tc->pubuf :
  299. req->tc->pkbuf;
  300. in = pack_sg_list(chan->sg, out+inp, VIRTQUEUE_NUM,
  301. pbuf, req->tc->pbuf_size);
  302. }
  303. in += inp;
  304. } else {
  305. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata,
  306. client->msize);
  307. }
  308. err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc);
  309. if (err < 0) {
  310. if (err == -ENOSPC) {
  311. chan->ring_bufs_avail = 0;
  312. spin_unlock_irqrestore(&chan->lock, flags);
  313. err = wait_event_interruptible(*chan->vc_wq,
  314. chan->ring_bufs_avail);
  315. if (err == -ERESTARTSYS)
  316. return err;
  317. P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n");
  318. goto req_retry;
  319. } else {
  320. spin_unlock_irqrestore(&chan->lock, flags);
  321. P9_DPRINTK(P9_DEBUG_TRANS,
  322. "9p debug: "
  323. "virtio rpc add_buf returned failure");
  324. if (rpinfo && rpinfo->rp_alloc)
  325. kfree(rpinfo);
  326. return -EIO;
  327. }
  328. }
  329. virtqueue_kick(chan->vq);
  330. spin_unlock_irqrestore(&chan->lock, flags);
  331. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
  332. return 0;
  333. }
  334. static ssize_t p9_mount_tag_show(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct virtio_chan *chan;
  338. struct virtio_device *vdev;
  339. vdev = dev_to_virtio(dev);
  340. chan = vdev->priv;
  341. return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
  342. }
  343. static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
  344. /**
  345. * p9_virtio_probe - probe for existence of 9P virtio channels
  346. * @vdev: virtio device to probe
  347. *
  348. * This probes for existing virtio channels.
  349. *
  350. */
  351. static int p9_virtio_probe(struct virtio_device *vdev)
  352. {
  353. __u16 tag_len;
  354. char *tag;
  355. int err;
  356. struct virtio_chan *chan;
  357. chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
  358. if (!chan) {
  359. printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n");
  360. err = -ENOMEM;
  361. goto fail;
  362. }
  363. chan->vdev = vdev;
  364. /* We expect one virtqueue, for requests. */
  365. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  366. if (IS_ERR(chan->vq)) {
  367. err = PTR_ERR(chan->vq);
  368. goto out_free_vq;
  369. }
  370. chan->vq->vdev->priv = chan;
  371. spin_lock_init(&chan->lock);
  372. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  373. chan->inuse = false;
  374. if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
  375. vdev->config->get(vdev,
  376. offsetof(struct virtio_9p_config, tag_len),
  377. &tag_len, sizeof(tag_len));
  378. } else {
  379. err = -EINVAL;
  380. goto out_free_vq;
  381. }
  382. tag = kmalloc(tag_len, GFP_KERNEL);
  383. if (!tag) {
  384. err = -ENOMEM;
  385. goto out_free_vq;
  386. }
  387. vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
  388. tag, tag_len);
  389. chan->tag = tag;
  390. chan->tag_len = tag_len;
  391. err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  392. if (err) {
  393. goto out_free_tag;
  394. }
  395. chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
  396. if (!chan->vc_wq) {
  397. err = -ENOMEM;
  398. goto out_free_tag;
  399. }
  400. init_waitqueue_head(chan->vc_wq);
  401. chan->ring_bufs_avail = 1;
  402. mutex_lock(&virtio_9p_lock);
  403. list_add_tail(&chan->chan_list, &virtio_chan_list);
  404. mutex_unlock(&virtio_9p_lock);
  405. return 0;
  406. out_free_tag:
  407. kfree(tag);
  408. out_free_vq:
  409. vdev->config->del_vqs(vdev);
  410. kfree(chan);
  411. fail:
  412. return err;
  413. }
  414. /**
  415. * p9_virtio_create - allocate a new virtio channel
  416. * @client: client instance invoking this transport
  417. * @devname: string identifying the channel to connect to (unused)
  418. * @args: args passed from sys_mount() for per-transport options (unused)
  419. *
  420. * This sets up a transport channel for 9p communication. Right now
  421. * we only match the first available channel, but eventually we couldlook up
  422. * alternate channels by matching devname versus a virtio_config entry.
  423. * We use a simple reference count mechanism to ensure that only a single
  424. * mount has a channel open at a time.
  425. *
  426. */
  427. static int
  428. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  429. {
  430. struct virtio_chan *chan;
  431. int ret = -ENOENT;
  432. int found = 0;
  433. mutex_lock(&virtio_9p_lock);
  434. list_for_each_entry(chan, &virtio_chan_list, chan_list) {
  435. if (!strncmp(devname, chan->tag, chan->tag_len) &&
  436. strlen(devname) == chan->tag_len) {
  437. if (!chan->inuse) {
  438. chan->inuse = true;
  439. found = 1;
  440. break;
  441. }
  442. ret = -EBUSY;
  443. }
  444. }
  445. mutex_unlock(&virtio_9p_lock);
  446. if (!found) {
  447. printk(KERN_ERR "9p: no channels available\n");
  448. return ret;
  449. }
  450. client->trans = (void *)chan;
  451. client->status = Connected;
  452. chan->client = client;
  453. return 0;
  454. }
  455. /**
  456. * p9_virtio_remove - clean up resources associated with a virtio device
  457. * @vdev: virtio device to remove
  458. *
  459. */
  460. static void p9_virtio_remove(struct virtio_device *vdev)
  461. {
  462. struct virtio_chan *chan = vdev->priv;
  463. BUG_ON(chan->inuse);
  464. vdev->config->del_vqs(vdev);
  465. mutex_lock(&virtio_9p_lock);
  466. list_del(&chan->chan_list);
  467. mutex_unlock(&virtio_9p_lock);
  468. sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  469. kfree(chan->tag);
  470. kfree(chan->vc_wq);
  471. kfree(chan);
  472. }
  473. static struct virtio_device_id id_table[] = {
  474. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  475. { 0 },
  476. };
  477. static unsigned int features[] = {
  478. VIRTIO_9P_MOUNT_TAG,
  479. };
  480. /* The standard "struct lguest_driver": */
  481. static struct virtio_driver p9_virtio_drv = {
  482. .feature_table = features,
  483. .feature_table_size = ARRAY_SIZE(features),
  484. .driver.name = KBUILD_MODNAME,
  485. .driver.owner = THIS_MODULE,
  486. .id_table = id_table,
  487. .probe = p9_virtio_probe,
  488. .remove = p9_virtio_remove,
  489. };
  490. static struct p9_trans_module p9_virtio_trans = {
  491. .name = "virtio",
  492. .create = p9_virtio_create,
  493. .close = p9_virtio_close,
  494. .request = p9_virtio_request,
  495. .cancel = p9_virtio_cancel,
  496. .maxsize = PAGE_SIZE*16,
  497. .pref = P9_TRANS_PREF_PAYLOAD_SEP,
  498. .def = 0,
  499. .owner = THIS_MODULE,
  500. };
  501. /* The standard init function */
  502. static int __init p9_virtio_init(void)
  503. {
  504. INIT_LIST_HEAD(&virtio_chan_list);
  505. v9fs_register_trans(&p9_virtio_trans);
  506. return register_virtio_driver(&p9_virtio_drv);
  507. }
  508. static void __exit p9_virtio_cleanup(void)
  509. {
  510. unregister_virtio_driver(&p9_virtio_drv);
  511. v9fs_unregister_trans(&p9_virtio_trans);
  512. }
  513. module_init(p9_virtio_init);
  514. module_exit(p9_virtio_cleanup);
  515. MODULE_DEVICE_TABLE(virtio, id_table);
  516. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  517. MODULE_DESCRIPTION("Virtio 9p Transport");
  518. MODULE_LICENSE("GPL");