trans_virtio.c 21 KB

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