trans_virtio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
  149. }
  150. }
  151. /**
  152. * pack_sg_list - pack a scatter gather list from a linear buffer
  153. * @sg: scatter/gather list to pack into
  154. * @start: which segment of the sg_list to start at
  155. * @limit: maximum segment to pack data to
  156. * @data: data to pack into scatter/gather list
  157. * @count: amount of data to pack into the scatter/gather list
  158. *
  159. * sg_lists have multiple segments of various sizes. This will pack
  160. * arbitrary data into an existing scatter gather list, segmenting the
  161. * data as necessary within constraints.
  162. *
  163. */
  164. static int pack_sg_list(struct scatterlist *sg, int start,
  165. int limit, char *data, int count)
  166. {
  167. int s;
  168. int index = start;
  169. while (count) {
  170. s = rest_of_page(data);
  171. if (s > count)
  172. s = count;
  173. BUG_ON(index > limit);
  174. /* Make sure we don't terminate early. */
  175. sg_unmark_end(&sg[index]);
  176. sg_set_buf(&sg[index++], data, s);
  177. count -= s;
  178. data += s;
  179. }
  180. if (index-start)
  181. sg_mark_end(&sg[index - 1]);
  182. return index-start;
  183. }
  184. /* We don't currently allow canceling of virtio requests */
  185. static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
  186. {
  187. return 1;
  188. }
  189. /**
  190. * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
  191. * this takes a list of pages.
  192. * @sg: scatter/gather list to pack into
  193. * @start: which segment of the sg_list to start at
  194. * @pdata: a list of pages to add into sg.
  195. * @nr_pages: number of pages to pack into the scatter/gather list
  196. * @data: data to pack into scatter/gather list
  197. * @count: amount of data to pack into the scatter/gather list
  198. */
  199. static int
  200. pack_sg_list_p(struct scatterlist *sg, int start, int limit,
  201. struct page **pdata, int nr_pages, char *data, int count)
  202. {
  203. int i = 0, s;
  204. int data_off;
  205. int index = start;
  206. BUG_ON(nr_pages > (limit - start));
  207. /*
  208. * if the first page doesn't start at
  209. * page boundary find the offset
  210. */
  211. data_off = offset_in_page(data);
  212. while (nr_pages) {
  213. s = rest_of_page(data);
  214. if (s > count)
  215. s = count;
  216. /* Make sure we don't terminate early. */
  217. sg_unmark_end(&sg[index]);
  218. sg_set_page(&sg[index++], pdata[i++], s, data_off);
  219. data_off = 0;
  220. data += s;
  221. count -= s;
  222. nr_pages--;
  223. }
  224. if (index-start)
  225. sg_mark_end(&sg[index - 1]);
  226. return index - start;
  227. }
  228. /**
  229. * p9_virtio_request - issue a request
  230. * @client: client instance issuing the request
  231. * @req: request to be issued
  232. *
  233. */
  234. static int
  235. p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
  236. {
  237. int err;
  238. int in, out, out_sgs, in_sgs;
  239. unsigned long flags;
  240. struct virtio_chan *chan = client->trans;
  241. struct scatterlist *sgs[2];
  242. p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
  243. req->status = REQ_STATUS_SENT;
  244. req_retry:
  245. spin_lock_irqsave(&chan->lock, flags);
  246. out_sgs = in_sgs = 0;
  247. /* Handle out VirtIO ring buffers */
  248. out = pack_sg_list(chan->sg, 0,
  249. VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
  250. if (out)
  251. sgs[out_sgs++] = chan->sg;
  252. in = pack_sg_list(chan->sg, out,
  253. VIRTQUEUE_NUM, req->rc->sdata, req->rc->capacity);
  254. if (in)
  255. sgs[out_sgs + in_sgs++] = chan->sg + out;
  256. err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
  257. GFP_ATOMIC);
  258. if (err < 0) {
  259. if (err == -ENOSPC) {
  260. chan->ring_bufs_avail = 0;
  261. spin_unlock_irqrestore(&chan->lock, flags);
  262. err = wait_event_interruptible(*chan->vc_wq,
  263. chan->ring_bufs_avail);
  264. if (err == -ERESTARTSYS)
  265. return err;
  266. p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
  267. goto req_retry;
  268. } else {
  269. spin_unlock_irqrestore(&chan->lock, flags);
  270. p9_debug(P9_DEBUG_TRANS,
  271. "virtio rpc add_sgs returned failure\n");
  272. return -EIO;
  273. }
  274. }
  275. virtqueue_kick(chan->vq);
  276. spin_unlock_irqrestore(&chan->lock, flags);
  277. p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
  278. return 0;
  279. }
  280. static int p9_get_mapped_pages(struct virtio_chan *chan,
  281. struct page **pages, char *data,
  282. int nr_pages, int write, int kern_buf)
  283. {
  284. int err;
  285. if (!kern_buf) {
  286. /*
  287. * We allow only p9_max_pages pinned. We wait for the
  288. * Other zc request to finish here
  289. */
  290. if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
  291. err = wait_event_interruptible(vp_wq,
  292. (atomic_read(&vp_pinned) < chan->p9_max_pages));
  293. if (err == -ERESTARTSYS)
  294. return err;
  295. }
  296. err = p9_payload_gup(data, &nr_pages, pages, write);
  297. if (err < 0)
  298. return err;
  299. atomic_add(nr_pages, &vp_pinned);
  300. } else {
  301. /* kernel buffer, no need to pin pages */
  302. int s, index = 0;
  303. int count = nr_pages;
  304. while (nr_pages) {
  305. s = rest_of_page(data);
  306. if (is_vmalloc_addr(data))
  307. pages[index++] = vmalloc_to_page(data);
  308. else
  309. pages[index++] = kmap_to_page(data);
  310. data += s;
  311. nr_pages--;
  312. }
  313. nr_pages = count;
  314. }
  315. return nr_pages;
  316. }
  317. /**
  318. * p9_virtio_zc_request - issue a zero copy request
  319. * @client: client instance issuing the request
  320. * @req: request to be issued
  321. * @uidata: user bffer that should be ued for zero copy read
  322. * @uodata: user buffer that shoud be user for zero copy write
  323. * @inlen: read buffer size
  324. * @olen: write buffer size
  325. * @hdrlen: reader header size, This is the size of response protocol data
  326. *
  327. */
  328. static int
  329. p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
  330. char *uidata, char *uodata, int inlen,
  331. int outlen, int in_hdr_len, int kern_buf)
  332. {
  333. int in, out, err, out_sgs, in_sgs;
  334. unsigned long flags;
  335. int in_nr_pages = 0, out_nr_pages = 0;
  336. struct page **in_pages = NULL, **out_pages = NULL;
  337. struct virtio_chan *chan = client->trans;
  338. struct scatterlist *sgs[4];
  339. p9_debug(P9_DEBUG_TRANS, "virtio request\n");
  340. if (uodata) {
  341. out_nr_pages = p9_nr_pages(uodata, outlen);
  342. out_pages = kmalloc(sizeof(struct page *) * out_nr_pages,
  343. GFP_NOFS);
  344. if (!out_pages) {
  345. err = -ENOMEM;
  346. goto err_out;
  347. }
  348. out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata,
  349. out_nr_pages, 0, kern_buf);
  350. if (out_nr_pages < 0) {
  351. err = out_nr_pages;
  352. kfree(out_pages);
  353. out_pages = NULL;
  354. goto err_out;
  355. }
  356. }
  357. if (uidata) {
  358. in_nr_pages = p9_nr_pages(uidata, inlen);
  359. in_pages = kmalloc(sizeof(struct page *) * in_nr_pages,
  360. GFP_NOFS);
  361. if (!in_pages) {
  362. err = -ENOMEM;
  363. goto err_out;
  364. }
  365. in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
  366. in_nr_pages, 1, kern_buf);
  367. if (in_nr_pages < 0) {
  368. err = in_nr_pages;
  369. kfree(in_pages);
  370. in_pages = NULL;
  371. goto err_out;
  372. }
  373. }
  374. req->status = REQ_STATUS_SENT;
  375. req_retry_pinned:
  376. spin_lock_irqsave(&chan->lock, flags);
  377. out_sgs = in_sgs = 0;
  378. /* out data */
  379. out = pack_sg_list(chan->sg, 0,
  380. VIRTQUEUE_NUM, req->tc->sdata, req->tc->size);
  381. if (out)
  382. sgs[out_sgs++] = chan->sg;
  383. if (out_pages) {
  384. sgs[out_sgs++] = chan->sg + out;
  385. out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
  386. out_pages, out_nr_pages, uodata, outlen);
  387. }
  388. /*
  389. * Take care of in data
  390. * For example TREAD have 11.
  391. * 11 is the read/write header = PDU Header(7) + IO Size (4).
  392. * Arrange in such a way that server places header in the
  393. * alloced memory and payload onto the user buffer.
  394. */
  395. in = pack_sg_list(chan->sg, out,
  396. VIRTQUEUE_NUM, req->rc->sdata, in_hdr_len);
  397. if (in)
  398. sgs[out_sgs + in_sgs++] = chan->sg + out;
  399. if (in_pages) {
  400. sgs[out_sgs + in_sgs++] = chan->sg + out + in;
  401. in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
  402. in_pages, in_nr_pages, uidata, inlen);
  403. }
  404. BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
  405. err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
  406. GFP_ATOMIC);
  407. if (err < 0) {
  408. if (err == -ENOSPC) {
  409. chan->ring_bufs_avail = 0;
  410. spin_unlock_irqrestore(&chan->lock, flags);
  411. err = wait_event_interruptible(*chan->vc_wq,
  412. chan->ring_bufs_avail);
  413. if (err == -ERESTARTSYS)
  414. goto err_out;
  415. p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
  416. goto req_retry_pinned;
  417. } else {
  418. spin_unlock_irqrestore(&chan->lock, flags);
  419. p9_debug(P9_DEBUG_TRANS,
  420. "virtio rpc add_sgs returned failure\n");
  421. err = -EIO;
  422. goto err_out;
  423. }
  424. }
  425. virtqueue_kick(chan->vq);
  426. spin_unlock_irqrestore(&chan->lock, flags);
  427. p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
  428. err = wait_event_interruptible(*req->wq,
  429. req->status >= REQ_STATUS_RCVD);
  430. /*
  431. * Non kernel buffers are pinned, unpin them
  432. */
  433. err_out:
  434. if (!kern_buf) {
  435. if (in_pages) {
  436. p9_release_pages(in_pages, in_nr_pages);
  437. atomic_sub(in_nr_pages, &vp_pinned);
  438. }
  439. if (out_pages) {
  440. p9_release_pages(out_pages, out_nr_pages);
  441. atomic_sub(out_nr_pages, &vp_pinned);
  442. }
  443. /* wakeup anybody waiting for slots to pin pages */
  444. wake_up(&vp_wq);
  445. }
  446. kfree(in_pages);
  447. kfree(out_pages);
  448. return err;
  449. }
  450. static ssize_t p9_mount_tag_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct virtio_chan *chan;
  454. struct virtio_device *vdev;
  455. vdev = dev_to_virtio(dev);
  456. chan = vdev->priv;
  457. return snprintf(buf, chan->tag_len + 1, "%s", chan->tag);
  458. }
  459. static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
  460. /**
  461. * p9_virtio_probe - probe for existence of 9P virtio channels
  462. * @vdev: virtio device to probe
  463. *
  464. * This probes for existing virtio channels.
  465. *
  466. */
  467. static int p9_virtio_probe(struct virtio_device *vdev)
  468. {
  469. __u16 tag_len;
  470. char *tag;
  471. int err;
  472. struct virtio_chan *chan;
  473. chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
  474. if (!chan) {
  475. pr_err("Failed to allocate virtio 9P channel\n");
  476. err = -ENOMEM;
  477. goto fail;
  478. }
  479. chan->vdev = vdev;
  480. /* We expect one virtqueue, for requests. */
  481. chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
  482. if (IS_ERR(chan->vq)) {
  483. err = PTR_ERR(chan->vq);
  484. goto out_free_vq;
  485. }
  486. chan->vq->vdev->priv = chan;
  487. spin_lock_init(&chan->lock);
  488. sg_init_table(chan->sg, VIRTQUEUE_NUM);
  489. chan->inuse = false;
  490. if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
  491. virtio_cread(vdev, struct virtio_9p_config, tag_len, &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. virtio_cread_bytes(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. /* Let udev rules use the new mount_tag attribute. */
  522. kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
  523. return 0;
  524. out_free_tag:
  525. kfree(tag);
  526. out_free_vq:
  527. vdev->config->del_vqs(vdev);
  528. kfree(chan);
  529. fail:
  530. return err;
  531. }
  532. /**
  533. * p9_virtio_create - allocate a new virtio channel
  534. * @client: client instance invoking this transport
  535. * @devname: string identifying the channel to connect to (unused)
  536. * @args: args passed from sys_mount() for per-transport options (unused)
  537. *
  538. * This sets up a transport channel for 9p communication. Right now
  539. * we only match the first available channel, but eventually we couldlook up
  540. * alternate channels by matching devname versus a virtio_config entry.
  541. * We use a simple reference count mechanism to ensure that only a single
  542. * mount has a channel open at a time.
  543. *
  544. */
  545. static int
  546. p9_virtio_create(struct p9_client *client, const char *devname, char *args)
  547. {
  548. struct virtio_chan *chan;
  549. int ret = -ENOENT;
  550. int found = 0;
  551. mutex_lock(&virtio_9p_lock);
  552. list_for_each_entry(chan, &virtio_chan_list, chan_list) {
  553. if (!strncmp(devname, chan->tag, chan->tag_len) &&
  554. strlen(devname) == chan->tag_len) {
  555. if (!chan->inuse) {
  556. chan->inuse = true;
  557. found = 1;
  558. break;
  559. }
  560. ret = -EBUSY;
  561. }
  562. }
  563. mutex_unlock(&virtio_9p_lock);
  564. if (!found) {
  565. pr_err("no channels available\n");
  566. return ret;
  567. }
  568. client->trans = (void *)chan;
  569. client->status = Connected;
  570. chan->client = client;
  571. return 0;
  572. }
  573. /**
  574. * p9_virtio_remove - clean up resources associated with a virtio device
  575. * @vdev: virtio device to remove
  576. *
  577. */
  578. static void p9_virtio_remove(struct virtio_device *vdev)
  579. {
  580. struct virtio_chan *chan = vdev->priv;
  581. if (chan->inuse)
  582. p9_virtio_close(chan->client);
  583. vdev->config->del_vqs(vdev);
  584. mutex_lock(&virtio_9p_lock);
  585. list_del(&chan->chan_list);
  586. mutex_unlock(&virtio_9p_lock);
  587. sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
  588. kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
  589. kfree(chan->tag);
  590. kfree(chan->vc_wq);
  591. kfree(chan);
  592. }
  593. static struct virtio_device_id id_table[] = {
  594. { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
  595. { 0 },
  596. };
  597. static unsigned int features[] = {
  598. VIRTIO_9P_MOUNT_TAG,
  599. };
  600. /* The standard "struct lguest_driver": */
  601. static struct virtio_driver p9_virtio_drv = {
  602. .feature_table = features,
  603. .feature_table_size = ARRAY_SIZE(features),
  604. .driver.name = KBUILD_MODNAME,
  605. .driver.owner = THIS_MODULE,
  606. .id_table = id_table,
  607. .probe = p9_virtio_probe,
  608. .remove = p9_virtio_remove,
  609. };
  610. static struct p9_trans_module p9_virtio_trans = {
  611. .name = "virtio",
  612. .create = p9_virtio_create,
  613. .close = p9_virtio_close,
  614. .request = p9_virtio_request,
  615. .zc_request = p9_virtio_zc_request,
  616. .cancel = p9_virtio_cancel,
  617. /*
  618. * We leave one entry for input and one entry for response
  619. * headers. We also skip one more entry to accomodate, address
  620. * that are not at page boundary, that can result in an extra
  621. * page in zero copy.
  622. */
  623. .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
  624. .def = 1,
  625. .owner = THIS_MODULE,
  626. };
  627. /* The standard init function */
  628. static int __init p9_virtio_init(void)
  629. {
  630. INIT_LIST_HEAD(&virtio_chan_list);
  631. v9fs_register_trans(&p9_virtio_trans);
  632. return register_virtio_driver(&p9_virtio_drv);
  633. }
  634. static void __exit p9_virtio_cleanup(void)
  635. {
  636. unregister_virtio_driver(&p9_virtio_drv);
  637. v9fs_unregister_trans(&p9_virtio_trans);
  638. }
  639. module_init(p9_virtio_init);
  640. module_exit(p9_virtio_cleanup);
  641. MODULE_DEVICE_TABLE(virtio, id_table);
  642. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  643. MODULE_DESCRIPTION("Virtio 9p Transport");
  644. MODULE_LICENSE("GPL");