trans_virtio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/in.h>
  30. #include <linux/module.h>
  31. #include <linux/net.h>
  32. #include <linux/ipv6.h>
  33. #include <linux/errno.h>
  34. #include <linux/kernel.h>
  35. #include <linux/un.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/inet.h>
  38. #include <linux/idr.h>
  39. #include <linux/file.h>
  40. #include <linux/highmem.h>
  41. #include <linux/slab.h>
  42. #include <net/9p/9p.h>
  43. #include <linux/parser.h>
  44. #include <net/9p/client.h>
  45. #include <net/9p/transport.h>
  46. #include <linux/scatterlist.h>
  47. #include <linux/swap.h>
  48. #include <linux/virtio.h>
  49. #include <linux/virtio_9p.h>
  50. #include "trans_common.h"
  51. #define VIRTQUEUE_NUM 128
  52. /* a single mutex to manage channel initialization and attachment */
  53. static DEFINE_MUTEX(virtio_9p_lock);
  54. static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
  55. static atomic_t vp_pinned = ATOMIC_INIT(0);
  56. /**
  57. * struct virtio_chan - per-instance transport information
  58. * @initialized: whether the channel is initialized
  59. * @inuse: whether the channel is in use
  60. * @lock: protects multiple elements within this structure
  61. * @client: client instance
  62. * @vdev: virtio dev associated with this channel
  63. * @vq: virtio queue associated with this channel
  64. * @sg: scatter gather list which is used to pack a request (protected?)
  65. *
  66. * We keep all per-channel information in a structure.
  67. * This structure is allocated within the devices dev->mem space.
  68. * A pointer to the structure will get put in the transport private.
  69. *
  70. */
  71. struct virtio_chan {
  72. bool inuse;
  73. spinlock_t lock;
  74. struct p9_client *client;
  75. struct virtio_device *vdev;
  76. struct virtqueue *vq;
  77. int ring_bufs_avail;
  78. wait_queue_head_t *vc_wq;
  79. /* This is global limit. Since we don't have a global structure,
  80. * will be placing it in each channel.
  81. */
  82. unsigned long p9_max_pages;
  83. /* Scatterlist: can be too big for stack. */
  84. struct scatterlist sg[VIRTQUEUE_NUM];
  85. int tag_len;
  86. /*
  87. * tag name to identify a mount Non-null terminated
  88. */
  89. char *tag;
  90. struct list_head chan_list;
  91. };
  92. static struct list_head virtio_chan_list;
  93. /* How many bytes left in this page. */
  94. static unsigned int rest_of_page(void *data)
  95. {
  96. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  97. }
  98. /**
  99. * p9_virtio_close - reclaim resources of a channel
  100. * @client: client instance
  101. *
  102. * This reclaims a channel by freeing its resources and
  103. * reseting its inuse flag.
  104. *
  105. */
  106. static void p9_virtio_close(struct p9_client *client)
  107. {
  108. struct virtio_chan *chan = client->trans;
  109. mutex_lock(&virtio_9p_lock);
  110. if (chan)
  111. chan->inuse = false;
  112. mutex_unlock(&virtio_9p_lock);
  113. }
  114. /**
  115. * req_done - callback which signals activity from the server
  116. * @vq: virtio queue activity was received on
  117. *
  118. * This notifies us that the server has triggered some activity
  119. * on the virtio channel - most likely a response to request we
  120. * sent. Figure out which requests now have responses and wake up
  121. * those threads.
  122. *
  123. * Bugs: could do with some additional sanity checking, but appears to work.
  124. *
  125. */
  126. static void req_done(struct virtqueue *vq)
  127. {
  128. struct virtio_chan *chan = vq->vdev->priv;
  129. struct p9_fcall *rc;
  130. unsigned int len;
  131. struct p9_req_t *req;
  132. unsigned long flags;
  133. p9_debug(P9_DEBUG_TRANS, ": request done\n");
  134. while (1) {
  135. spin_lock_irqsave(&chan->lock, flags);
  136. rc = virtqueue_get_buf(chan->vq, &len);
  137. if (rc == NULL) {
  138. spin_unlock_irqrestore(&chan->lock, flags);
  139. break;
  140. }
  141. chan->ring_bufs_avail = 1;
  142. spin_unlock_irqrestore(&chan->lock, flags);
  143. /* Wakeup if anyone waiting for VirtIO ring space. */
  144. wake_up(chan->vc_wq);
  145. p9_debug(P9_DEBUG_TRANS, ": rc %p\n", rc);
  146. p9_debug(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag);
  147. req = p9_tag_lookup(chan->client, rc->tag);
  148. req->status = REQ_STATUS_RCVD;
  149. p9_client_cb(chan->client, req);
  150. }
  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 pack_sg_list(struct scatterlist *sg, int start,
  166. int limit, char *data, int count)
  167. {
  168. int s;
  169. int index = start;
  170. while (count) {
  171. s = rest_of_page(data);
  172. if (s > count)
  173. s = count;
  174. BUG_ON(index > limit);
  175. /* Make sure we don't terminate early. */
  176. sg_unmark_end(&sg[index]);
  177. sg_set_buf(&sg[index++], data, s);
  178. count -= s;
  179. data += s;
  180. }
  181. if (index-start)
  182. sg_mark_end(&sg[index - 1]);
  183. return index-start;
  184. }
  185. /* We don't currently allow canceling of virtio requests */
  186. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  187. {
  188. return 1;
  189. }
  190. /**
  191. * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
  192. * this takes a list of pages.
  193. * @sg: scatter/gather list to pack into
  194. * @start: which segment of the sg_list to start at
  195. * @pdata: a list of pages to add into sg.
  196. * @nr_pages: number of pages to pack into the scatter/gather list
  197. * @data: data to pack into scatter/gather list
  198. * @count: amount of data to pack into the scatter/gather list
  199. */
  200. static int
  201. pack_sg_list_p(struct scatterlist *sg, int start, int limit,
  202. struct page **pdata, int nr_pages, char *data, int count)
  203. {
  204. int i = 0, s;
  205. int data_off;
  206. int index = start;
  207. BUG_ON(nr_pages > (limit - start));
  208. /*
  209. * if the first page doesn't start at
  210. * page boundary find the offset
  211. */
  212. data_off = offset_in_page(data);
  213. while (nr_pages) {
  214. s = rest_of_page(data);
  215. if (s > count)
  216. s = count;
  217. /* Make sure we don't terminate early. */
  218. sg_unmark_end(&sg[index]);
  219. sg_set_page(&sg[index++], pdata[i++], s, data_off);
  220. data_off = 0;
  221. data += s;
  222. count -= s;
  223. nr_pages--;
  224. }
  225. if (index-start)
  226. sg_mark_end(&sg[index - 1]);
  227. return index - start;
  228. }
  229. /**
  230. * p9_virtio_request - issue a request
  231. * @client: client instance issuing the request
  232. * @req: request to be issued
  233. *
  234. */
  235. static int
  236. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  237. {
  238. int err;
  239. int in, out, out_sgs, in_sgs;
  240. unsigned long flags;
  241. struct virtio_chan *chan = client->trans;
  242. struct scatterlist *sgs[2];
  243. p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  244. req->status = REQ_STATUS_SENT;
  245. req_retry:
  246. spin_lock_irqsave(&chan->lock, flags);
  247. out_sgs = in_sgs = 0;
  248. /* Handle out VirtIO ring buffers */
  249. out = pack_sg_list(chan->sg, 0,
  250. VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
  251. if (out)
  252. sgs[out_sgs++] = chan->sg;
  253. in = pack_sg_list(chan->sg, out,
  254. VIRTQUEUE_NUM, req->rc->sdata, req->rc->capacity);
  255. if (in)
  256. sgs[out_sgs + in_sgs++] = chan->sg + out;
  257. err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
  258. GFP_ATOMIC);
  259. if (err < 0) {
  260. if (err == -ENOSPC) {
  261. chan->ring_bufs_avail = 0;
  262. spin_unlock_irqrestore(&chan->lock, flags);
  263. err = wait_event_interruptible(*chan->vc_wq,
  264. chan->ring_bufs_avail);
  265. if (err == -ERESTARTSYS)
  266. return err;
  267. p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
  268. goto req_retry;
  269. } else {
  270. spin_unlock_irqrestore(&chan->lock, flags);
  271. p9_debug(P9_DEBUG_TRANS,
  272. "virtio rpc add_sgs returned failure\n");
  273. return -EIO;
  274. }
  275. }
  276. virtqueue_kick(chan->vq);
  277. spin_unlock_irqrestore(&chan->lock, flags);
  278. p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
  279. return 0;
  280. }
  281. static int p9_get_mapped_pages(struct virtio_chan *chan,
  282. struct page **pages, char *data,
  283. int nr_pages, int write, int kern_buf)
  284. {
  285. int err;
  286. if (!kern_buf) {
  287. /*
  288. * We allow only p9_max_pages pinned. We wait for the
  289. * Other zc request to finish here
  290. */
  291. if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
  292. err = wait_event_interruptible(vp_wq,
  293. (atomic_read(&vp_pinned) < chan->p9_max_pages));
  294. if (err == -ERESTARTSYS)
  295. return err;
  296. }
  297. err = p9_payload_gup(data, &nr_pages, pages, write);
  298. if (err < 0)
  299. return err;
  300. atomic_add(nr_pages, &vp_pinned);
  301. } else {
  302. /* kernel buffer, no need to pin pages */
  303. int s, index = 0;
  304. int count = nr_pages;
  305. while (nr_pages) {
  306. s = rest_of_page(data);
  307. pages[index++] = kmap_to_page(data);
  308. data += s;
  309. nr_pages--;
  310. }
  311. nr_pages = count;
  312. }
  313. return nr_pages;
  314. }
  315. /**
  316. * p9_virtio_zc_request - issue a zero copy request
  317. * @client: client instance issuing the request
  318. * @req: request to be issued
  319. * @uidata: user bffer that should be ued for zero copy read
  320. * @uodata: user buffer that shoud be user for zero copy write
  321. * @inlen: read buffer size
  322. * @olen: write buffer size
  323. * @hdrlen: reader header size, This is the size of response protocol data
  324. *
  325. */
  326. static int
  327. p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
  328. char *uidata, char *uodata, int inlen,
  329. int outlen, int in_hdr_len, int kern_buf)
  330. {
  331. int in, out, err, out_sgs, in_sgs;
  332. unsigned long flags;
  333. int in_nr_pages = 0, out_nr_pages = 0;
  334. struct page **in_pages = NULL, **out_pages = NULL;
  335. struct virtio_chan *chan = client->trans;
  336. struct scatterlist *sgs[4];
  337. p9_debug(P9_DEBUG_TRANS, "virtio request\n");
  338. if (uodata) {
  339. out_nr_pages = p9_nr_pages(uodata, outlen);
  340. out_pages = kmalloc(sizeof(struct page *) * out_nr_pages,
  341. GFP_NOFS);
  342. if (!out_pages) {
  343. err = -ENOMEM;
  344. goto err_out;
  345. }
  346. out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata,
  347. out_nr_pages, 0, kern_buf);
  348. if (out_nr_pages < 0) {
  349. err = out_nr_pages;
  350. kfree(out_pages);
  351. out_pages = NULL;
  352. goto err_out;
  353. }
  354. }
  355. if (uidata) {
  356. in_nr_pages = p9_nr_pages(uidata, inlen);
  357. in_pages = kmalloc(sizeof(struct page *) * in_nr_pages,
  358. GFP_NOFS);
  359. if (!in_pages) {
  360. err = -ENOMEM;
  361. goto err_out;
  362. }
  363. in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
  364. in_nr_pages, 1, kern_buf);
  365. if (in_nr_pages < 0) {
  366. err = in_nr_pages;
  367. kfree(in_pages);
  368. in_pages = NULL;
  369. goto err_out;
  370. }
  371. }
  372. req->status = REQ_STATUS_SENT;
  373. req_retry_pinned:
  374. spin_lock_irqsave(&chan->lock, flags);
  375. out_sgs = in_sgs = 0;
  376. /* out data */
  377. out = pack_sg_list(chan->sg, 0,
  378. VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
  379. if (out)
  380. sgs[out_sgs++] = chan->sg;
  381. if (out_pages) {
  382. sgs[out_sgs++] = chan->sg + out;
  383. out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
  384. out_pages, out_nr_pages, uodata, outlen);
  385. }
  386. /*
  387. * Take care of in data
  388. * For example TREAD have 11.
  389. * 11 is the read/write header = PDU Header(7) + IO Size (4).
  390. * Arrange in such a way that server places header in the
  391. * alloced memory and payload onto the user buffer.
  392. */
  393. in = pack_sg_list(chan->sg, out,
  394. VIRTQUEUE_NUM, req->rc->sdata, in_hdr_len);
  395. if (in)
  396. sgs[out_sgs + in_sgs++] = chan->sg + out;
  397. if (in_pages) {
  398. sgs[out_sgs + in_sgs++] = chan->sg + out + in;
  399. in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
  400. in_pages, in_nr_pages, uidata, inlen);
  401. }
  402. BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
  403. err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
  404. GFP_ATOMIC);
  405. if (err < 0) {
  406. if (err == -ENOSPC) {
  407. chan->ring_bufs_avail = 0;
  408. spin_unlock_irqrestore(&chan->lock, flags);
  409. err = wait_event_interruptible(*chan->vc_wq,
  410. chan->ring_bufs_avail);
  411. if (err == -ERESTARTSYS)
  412. goto err_out;
  413. p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
  414. goto req_retry_pinned;
  415. } else {
  416. spin_unlock_irqrestore(&chan->lock, flags);
  417. p9_debug(P9_DEBUG_TRANS,
  418. "virtio rpc add_sgs returned failure\n");
  419. err = -EIO;
  420. goto err_out;
  421. }
  422. }
  423. virtqueue_kick(chan->vq);
  424. spin_unlock_irqrestore(&chan->lock, flags);
  425. p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
  426. err = wait_event_interruptible(*req->wq,
  427. req->status >= REQ_STATUS_RCVD);
  428. /*
  429. * Non kernel buffers are pinned, unpin them
  430. */
  431. err_out:
  432. if (!kern_buf) {
  433. if (in_pages) {
  434. p9_release_pages(in_pages, in_nr_pages);
  435. atomic_sub(in_nr_pages, &vp_pinned);
  436. }
  437. if (out_pages) {
  438. p9_release_pages(out_pages, out_nr_pages);
  439. atomic_sub(out_nr_pages, &vp_pinned);
  440. }
  441. /* wakeup anybody waiting for slots to pin pages */
  442. wake_up(&vp_wq);
  443. }
  444. kfree(in_pages);
  445. kfree(out_pages);
  446. return err;
  447. }
  448. static ssize_t p9_mount_tag_show(struct device *dev,
  449. struct device_attribute *attr, char *buf)
  450. {
  451. struct virtio_chan *chan;
  452. struct virtio_device *vdev;
  453. vdev = dev_to_virtio(dev);
  454. chan = vdev->priv;
  455. return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
  456. }
  457. static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
  458. /**
  459. * p9_virtio_probe - probe for existence of 9P virtio channels
  460. * @vdev: virtio device to probe
  461. *
  462. * This probes for existing virtio channels.
  463. *
  464. */
  465. static int p9_virtio_probe(struct virtio_device *vdev)
  466. {
  467. __u16 tag_len;
  468. char *tag;
  469. int err;
  470. struct virtio_chan *chan;
  471. chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
  472. if (!chan) {
  473. pr_err("Failed to allocate virtio 9P channel\n");
  474. err = -ENOMEM;
  475. goto fail;
  476. }
  477. chan->vdev = vdev;
  478. /* We expect one virtqueue, for requests. */
  479. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  480. if (IS_ERR(chan->vq)) {
  481. err = PTR_ERR(chan->vq);
  482. goto out_free_vq;
  483. }
  484. chan->vq->vdev->priv = chan;
  485. spin_lock_init(&chan->lock);
  486. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  487. chan->inuse = false;
  488. if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
  489. vdev->config->get(vdev,
  490. offsetof(struct virtio_9p_config, tag_len),
  491. &tag_len, sizeof(tag_len));
  492. } else {
  493. err = -EINVAL;
  494. goto out_free_vq;
  495. }
  496. tag = kmalloc(tag_len, GFP_KERNEL);
  497. if (!tag) {
  498. err = -ENOMEM;
  499. goto out_free_vq;
  500. }
  501. vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag),
  502. tag, tag_len);
  503. chan->tag = tag;
  504. chan->tag_len = tag_len;
  505. err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  506. if (err) {
  507. goto out_free_tag;
  508. }
  509. chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
  510. if (!chan->vc_wq) {
  511. err = -ENOMEM;
  512. goto out_free_tag;
  513. }
  514. init_waitqueue_head(chan->vc_wq);
  515. chan->ring_bufs_avail = 1;
  516. /* Ceiling limit to avoid denial of service attacks */
  517. chan->p9_max_pages = nr_free_buffer_pages()/4;
  518. mutex_lock(&virtio_9p_lock);
  519. list_add_tail(&chan->chan_list, &virtio_chan_list);
  520. mutex_unlock(&virtio_9p_lock);
  521. return 0;
  522. out_free_tag:
  523. kfree(tag);
  524. out_free_vq:
  525. vdev->config->del_vqs(vdev);
  526. kfree(chan);
  527. fail:
  528. return err;
  529. }
  530. /**
  531. * p9_virtio_create - allocate a new virtio channel
  532. * @client: client instance invoking this transport
  533. * @devname: string identifying the channel to connect to (unused)
  534. * @args: args passed from sys_mount() for per-transport options (unused)
  535. *
  536. * This sets up a transport channel for 9p communication. Right now
  537. * we only match the first available channel, but eventually we couldlook up
  538. * alternate channels by matching devname versus a virtio_config entry.
  539. * We use a simple reference count mechanism to ensure that only a single
  540. * mount has a channel open at a time.
  541. *
  542. */
  543. static int
  544. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  545. {
  546. struct virtio_chan *chan;
  547. int ret = -ENOENT;
  548. int found = 0;
  549. mutex_lock(&virtio_9p_lock);
  550. list_for_each_entry(chan, &virtio_chan_list, chan_list) {
  551. if (!strncmp(devname, chan->tag, chan->tag_len) &&
  552. strlen(devname) == chan->tag_len) {
  553. if (!chan->inuse) {
  554. chan->inuse = true;
  555. found = 1;
  556. break;
  557. }
  558. ret = -EBUSY;
  559. }
  560. }
  561. mutex_unlock(&virtio_9p_lock);
  562. if (!found) {
  563. pr_err("no channels available\n");
  564. return ret;
  565. }
  566. client->trans = (void *)chan;
  567. client->status = Connected;
  568. chan->client = client;
  569. return 0;
  570. }
  571. /**
  572. * p9_virtio_remove - clean up resources associated with a virtio device
  573. * @vdev: virtio device to remove
  574. *
  575. */
  576. static void p9_virtio_remove(struct virtio_device *vdev)
  577. {
  578. struct virtio_chan *chan = vdev->priv;
  579. if (chan->inuse)
  580. p9_virtio_close(chan->client);
  581. vdev->config->del_vqs(vdev);
  582. mutex_lock(&virtio_9p_lock);
  583. list_del(&chan->chan_list);
  584. mutex_unlock(&virtio_9p_lock);
  585. sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  586. kfree(chan->tag);
  587. kfree(chan->vc_wq);
  588. kfree(chan);
  589. }
  590. static struct virtio_device_id id_table[] = {
  591. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  592. { 0 },
  593. };
  594. static unsigned int features[] = {
  595. VIRTIO_9P_MOUNT_TAG,
  596. };
  597. /* The standard "struct lguest_driver": */
  598. static struct virtio_driver p9_virtio_drv = {
  599. .feature_table = features,
  600. .feature_table_size = ARRAY_SIZE(features),
  601. .driver.name = KBUILD_MODNAME,
  602. .driver.owner = THIS_MODULE,
  603. .id_table = id_table,
  604. .probe = p9_virtio_probe,
  605. .remove = p9_virtio_remove,
  606. };
  607. static struct p9_trans_module p9_virtio_trans = {
  608. .name = "virtio",
  609. .create = p9_virtio_create,
  610. .close = p9_virtio_close,
  611. .request = p9_virtio_request,
  612. .zc_request = p9_virtio_zc_request,
  613. .cancel = p9_virtio_cancel,
  614. /*
  615. * We leave one entry for input and one entry for response
  616. * headers. We also skip one more entry to accomodate, address
  617. * that are not at page boundary, that can result in an extra
  618. * page in zero copy.
  619. */
  620. .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
  621. .def = 0,
  622. .owner = THIS_MODULE,
  623. };
  624. /* The standard init function */
  625. static int __init p9_virtio_init(void)
  626. {
  627. INIT_LIST_HEAD(&virtio_chan_list);
  628. v9fs_register_trans(&p9_virtio_trans);
  629. return register_virtio_driver(&p9_virtio_drv);
  630. }
  631. static void __exit p9_virtio_cleanup(void)
  632. {
  633. unregister_virtio_driver(&p9_virtio_drv);
  634. v9fs_unregister_trans(&p9_virtio_trans);
  635. }
  636. module_init(p9_virtio_init);
  637. module_exit(p9_virtio_cleanup);
  638. MODULE_DEVICE_TABLE(virtio, id_table);
  639. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  640. MODULE_DESCRIPTION("Virtio 9p Transport");
  641. MODULE_LICENSE("GPL");