trans_virtio.c 19 KB

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