trans_virtio.c 20 KB

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