trans_virtio.c 19 KB

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