trans_virtio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #define VIRTQUEUE_NUM 128
  48. /* a single mutex to manage channel initialization and attachment */
  49. static DEFINE_MUTEX(virtio_9p_lock);
  50. /**
  51. * struct virtio_chan - per-instance transport information
  52. * @initialized: whether the channel is initialized
  53. * @inuse: whether the channel is in use
  54. * @lock: protects multiple elements within this structure
  55. * @client: client instance
  56. * @vdev: virtio dev associated with this channel
  57. * @vq: virtio queue associated with this channel
  58. * @sg: scatter gather list which is used to pack a request (protected?)
  59. *
  60. * We keep all per-channel information in a structure.
  61. * This structure is allocated within the devices dev->mem space.
  62. * A pointer to the structure will get put in the transport private.
  63. *
  64. */
  65. struct virtio_chan {
  66. bool inuse;
  67. spinlock_t lock;
  68. struct p9_client *client;
  69. struct virtio_device *vdev;
  70. struct virtqueue *vq;
  71. int ring_bufs_avail;
  72. wait_queue_head_t *vc_wq;
  73. /* Scatterlist: can be too big for stack. */
  74. struct scatterlist sg[VIRTQUEUE_NUM];
  75. int tag_len;
  76. /*
  77. * tag name to identify a mount Non-null terminated
  78. */
  79. char *tag;
  80. struct list_head chan_list;
  81. };
  82. static struct list_head virtio_chan_list;
  83. /* How many bytes left in this page. */
  84. static unsigned int rest_of_page(void *data)
  85. {
  86. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  87. }
  88. /**
  89. * p9_virtio_close - reclaim resources of a channel
  90. * @client: client instance
  91. *
  92. * This reclaims a channel by freeing its resources and
  93. * reseting its inuse flag.
  94. *
  95. */
  96. static void p9_virtio_close(struct p9_client *client)
  97. {
  98. struct virtio_chan *chan = client->trans;
  99. mutex_lock(&virtio_9p_lock);
  100. if (chan)
  101. chan->inuse = false;
  102. mutex_unlock(&virtio_9p_lock);
  103. }
  104. /**
  105. * req_done - callback which signals activity from the server
  106. * @vq: virtio queue activity was received on
  107. *
  108. * This notifies us that the server has triggered some activity
  109. * on the virtio channel - most likely a response to request we
  110. * sent. Figure out which requests now have responses and wake up
  111. * those threads.
  112. *
  113. * Bugs: could do with some additional sanity checking, but appears to work.
  114. *
  115. */
  116. static void req_done(struct virtqueue *vq)
  117. {
  118. struct virtio_chan *chan = vq->vdev->priv;
  119. struct p9_fcall *rc;
  120. unsigned int len;
  121. struct p9_req_t *req;
  122. unsigned long flags;
  123. P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n");
  124. do {
  125. spin_lock_irqsave(&chan->lock, flags);
  126. rc = virtqueue_get_buf(chan->vq, &len);
  127. if (rc != NULL) {
  128. if (!chan->ring_bufs_avail) {
  129. chan->ring_bufs_avail = 1;
  130. wake_up(chan->vc_wq);
  131. }
  132. spin_unlock_irqrestore(&chan->lock, flags);
  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. p9_client_cb(chan->client, req);
  139. } else {
  140. spin_unlock_irqrestore(&chan->lock, flags);
  141. }
  142. } while (rc != NULL);
  143. }
  144. /**
  145. * pack_sg_list - pack a scatter gather list from a linear buffer
  146. * @sg: scatter/gather list to pack into
  147. * @start: which segment of the sg_list to start at
  148. * @limit: maximum segment to pack data to
  149. * @data: data to pack into scatter/gather list
  150. * @count: amount of data to pack into the scatter/gather list
  151. *
  152. * sg_lists have multiple segments of various sizes. This will pack
  153. * arbitrary data into an existing scatter gather list, segmenting the
  154. * data as necessary within constraints.
  155. *
  156. */
  157. static int
  158. pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
  159. int count)
  160. {
  161. int s;
  162. int index = start;
  163. while (count) {
  164. s = rest_of_page(data);
  165. if (s > count)
  166. s = count;
  167. sg_set_buf(&sg[index++], data, s);
  168. count -= s;
  169. data += s;
  170. BUG_ON(index > limit);
  171. }
  172. return index-start;
  173. }
  174. /* We don't currently allow canceling of virtio requests */
  175. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  176. {
  177. return 1;
  178. }
  179. /**
  180. * p9_virtio_request - issue a request
  181. * @client: client instance issuing the request
  182. * @req: request to be issued
  183. *
  184. */
  185. static int
  186. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  187. {
  188. int in, out;
  189. struct virtio_chan *chan = client->trans;
  190. char *rdata = (char *)req->rc+sizeof(struct p9_fcall);
  191. unsigned long flags;
  192. int err;
  193. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  194. req_retry:
  195. req->status = REQ_STATUS_SENT;
  196. spin_lock_irqsave(&chan->lock, flags);
  197. out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata,
  198. req->tc->size);
  199. in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM-out, rdata,
  200. client->msize);
  201. err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc);
  202. if (err < 0) {
  203. if (err == -ENOSPC) {
  204. chan->ring_bufs_avail = 0;
  205. spin_unlock_irqrestore(&chan->lock, flags);
  206. err = wait_event_interruptible(*chan->vc_wq,
  207. chan->ring_bufs_avail);
  208. if (err == -ERESTARTSYS)
  209. return err;
  210. P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n");
  211. goto req_retry;
  212. } else {
  213. spin_unlock_irqrestore(&chan->lock, flags);
  214. P9_DPRINTK(P9_DEBUG_TRANS,
  215. "9p debug: "
  216. "virtio rpc add_buf returned failure");
  217. return -EIO;
  218. }
  219. }
  220. virtqueue_kick(chan->vq);
  221. spin_unlock_irqrestore(&chan->lock, flags);
  222. P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n");
  223. return 0;
  224. }
  225. static ssize_t p9_mount_tag_show(struct device *dev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct virtio_chan *chan;
  229. struct virtio_device *vdev;
  230. vdev = dev_to_virtio(dev);
  231. chan = vdev->priv;
  232. return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
  233. }
  234. static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
  235. /**
  236. * p9_virtio_probe - probe for existence of 9P virtio channels
  237. * @vdev: virtio device to probe
  238. *
  239. * This probes for existing virtio channels.
  240. *
  241. */
  242. static int p9_virtio_probe(struct virtio_device *vdev)
  243. {
  244. __u16 tag_len;
  245. char *tag;
  246. int err;
  247. struct virtio_chan *chan;
  248. chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
  249. if (!chan) {
  250. printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n");
  251. err = -ENOMEM;
  252. goto fail;
  253. }
  254. chan->vdev = vdev;
  255. /* We expect one virtqueue, for requests. */
  256. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  257. if (IS_ERR(chan->vq)) {
  258. err = PTR_ERR(chan->vq);
  259. goto out_free_vq;
  260. }
  261. chan->vq->vdev->priv = chan;
  262. spin_lock_init(&chan->lock);
  263. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  264. chan->inuse = false;
  265. if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
  266. vdev->config->get(vdev,
  267. offsetof(struct virtio_9p_config, tag_len),
  268. &tag_len, sizeof(tag_len));
  269. } else {
  270. err = -EINVAL;
  271. goto out_free_vq;
  272. }
  273. tag = kmalloc(tag_len, GFP_KERNEL);
  274. if (!tag) {
  275. err = -ENOMEM;
  276. goto out_free_vq;
  277. }
  278. vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
  279. tag, tag_len);
  280. chan->tag = tag;
  281. chan->tag_len = tag_len;
  282. err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  283. if (err) {
  284. goto out_free_tag;
  285. }
  286. chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
  287. if (!chan->vc_wq) {
  288. err = -ENOMEM;
  289. goto out_free_tag;
  290. }
  291. init_waitqueue_head(chan->vc_wq);
  292. chan->ring_bufs_avail = 1;
  293. mutex_lock(&virtio_9p_lock);
  294. list_add_tail(&chan->chan_list, &virtio_chan_list);
  295. mutex_unlock(&virtio_9p_lock);
  296. return 0;
  297. out_free_tag:
  298. kfree(tag);
  299. out_free_vq:
  300. vdev->config->del_vqs(vdev);
  301. kfree(chan);
  302. fail:
  303. return err;
  304. }
  305. /**
  306. * p9_virtio_create - allocate a new virtio channel
  307. * @client: client instance invoking this transport
  308. * @devname: string identifying the channel to connect to (unused)
  309. * @args: args passed from sys_mount() for per-transport options (unused)
  310. *
  311. * This sets up a transport channel for 9p communication. Right now
  312. * we only match the first available channel, but eventually we couldlook up
  313. * alternate channels by matching devname versus a virtio_config entry.
  314. * We use a simple reference count mechanism to ensure that only a single
  315. * mount has a channel open at a time.
  316. *
  317. */
  318. static int
  319. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  320. {
  321. struct virtio_chan *chan;
  322. int ret = -ENOENT;
  323. int found = 0;
  324. mutex_lock(&virtio_9p_lock);
  325. list_for_each_entry(chan, &virtio_chan_list, chan_list) {
  326. if (!strncmp(devname, chan->tag, chan->tag_len) &&
  327. strlen(devname) == chan->tag_len) {
  328. if (!chan->inuse) {
  329. chan->inuse = true;
  330. found = 1;
  331. break;
  332. }
  333. ret = -EBUSY;
  334. }
  335. }
  336. mutex_unlock(&virtio_9p_lock);
  337. if (!found) {
  338. printk(KERN_ERR "9p: no channels available\n");
  339. return ret;
  340. }
  341. client->trans = (void *)chan;
  342. client->status = Connected;
  343. chan->client = client;
  344. return 0;
  345. }
  346. /**
  347. * p9_virtio_remove - clean up resources associated with a virtio device
  348. * @vdev: virtio device to remove
  349. *
  350. */
  351. static void p9_virtio_remove(struct virtio_device *vdev)
  352. {
  353. struct virtio_chan *chan = vdev->priv;
  354. BUG_ON(chan->inuse);
  355. vdev->config->del_vqs(vdev);
  356. mutex_lock(&virtio_9p_lock);
  357. list_del(&chan->chan_list);
  358. mutex_unlock(&virtio_9p_lock);
  359. sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  360. kfree(chan->tag);
  361. kfree(chan->vc_wq);
  362. kfree(chan);
  363. }
  364. static struct virtio_device_id id_table[] = {
  365. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  366. { 0 },
  367. };
  368. static unsigned int features[] = {
  369. VIRTIO_9P_MOUNT_TAG,
  370. };
  371. /* The standard "struct lguest_driver": */
  372. static struct virtio_driver p9_virtio_drv = {
  373. .feature_table = features,
  374. .feature_table_size = ARRAY_SIZE(features),
  375. .driver.name = KBUILD_MODNAME,
  376. .driver.owner = THIS_MODULE,
  377. .id_table = id_table,
  378. .probe = p9_virtio_probe,
  379. .remove = p9_virtio_remove,
  380. };
  381. static struct p9_trans_module p9_virtio_trans = {
  382. .name = "virtio",
  383. .create = p9_virtio_create,
  384. .close = p9_virtio_close,
  385. .request = p9_virtio_request,
  386. .cancel = p9_virtio_cancel,
  387. .maxsize = PAGE_SIZE*16,
  388. .def = 0,
  389. .owner = THIS_MODULE,
  390. };
  391. /* The standard init function */
  392. static int __init p9_virtio_init(void)
  393. {
  394. INIT_LIST_HEAD(&virtio_chan_list);
  395. v9fs_register_trans(&p9_virtio_trans);
  396. return register_virtio_driver(&p9_virtio_drv);
  397. }
  398. static void __exit p9_virtio_cleanup(void)
  399. {
  400. unregister_virtio_driver(&p9_virtio_drv);
  401. v9fs_unregister_trans(&p9_virtio_trans);
  402. }
  403. module_init(p9_virtio_init);
  404. module_exit(p9_virtio_cleanup);
  405. MODULE_DEVICE_TABLE(virtio, id_table);
  406. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  407. MODULE_DESCRIPTION("Virtio 9p Transport");
  408. MODULE_LICENSE("GPL");