trans_virtio.c 21 KB

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