trans_virtio.c 10 KB

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