0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015
0016 #include <linux/in.h>
0017 #include <linux/module.h>
0018 #include <linux/net.h>
0019 #include <linux/ipv6.h>
0020 #include <linux/errno.h>
0021 #include <linux/kernel.h>
0022 #include <linux/un.h>
0023 #include <linux/uaccess.h>
0024 #include <linux/inet.h>
0025 #include <linux/idr.h>
0026 #include <linux/file.h>
0027 #include <linux/highmem.h>
0028 #include <linux/slab.h>
0029 #include <net/9p/9p.h>
0030 #include <linux/parser.h>
0031 #include <net/9p/client.h>
0032 #include <net/9p/transport.h>
0033 #include <linux/scatterlist.h>
0034 #include <linux/swap.h>
0035 #include <linux/virtio.h>
0036 #include <linux/virtio_9p.h>
0037 #include "trans_common.h"
0038
0039 #define VIRTQUEUE_NUM 128
0040
0041
0042 static DEFINE_MUTEX(virtio_9p_lock);
0043 static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
0044 static atomic_t vp_pinned = ATOMIC_INIT(0);
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 struct virtio_chan {
0066 bool inuse;
0067
0068 spinlock_t lock;
0069
0070 struct p9_client *client;
0071 struct virtio_device *vdev;
0072 struct virtqueue *vq;
0073 int ring_bufs_avail;
0074 wait_queue_head_t *vc_wq;
0075
0076
0077
0078 unsigned long p9_max_pages;
0079
0080 struct scatterlist sg[VIRTQUEUE_NUM];
0081
0082
0083
0084 char *tag;
0085
0086 struct list_head chan_list;
0087 };
0088
0089 static struct list_head virtio_chan_list;
0090
0091
0092 static unsigned int rest_of_page(void *data)
0093 {
0094 return PAGE_SIZE - offset_in_page(data);
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 static void p9_virtio_close(struct p9_client *client)
0107 {
0108 struct virtio_chan *chan = client->trans;
0109
0110 mutex_lock(&virtio_9p_lock);
0111 if (chan)
0112 chan->inuse = false;
0113 mutex_unlock(&virtio_9p_lock);
0114 }
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129 static void req_done(struct virtqueue *vq)
0130 {
0131 struct virtio_chan *chan = vq->vdev->priv;
0132 unsigned int len;
0133 struct p9_req_t *req;
0134 bool need_wakeup = false;
0135 unsigned long flags;
0136
0137 p9_debug(P9_DEBUG_TRANS, ": request done\n");
0138
0139 spin_lock_irqsave(&chan->lock, flags);
0140 while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
0141 if (!chan->ring_bufs_avail) {
0142 chan->ring_bufs_avail = 1;
0143 need_wakeup = true;
0144 }
0145
0146 if (len) {
0147 req->rc.size = len;
0148 p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
0149 }
0150 }
0151 spin_unlock_irqrestore(&chan->lock, flags);
0152
0153 if (need_wakeup)
0154 wake_up(chan->vc_wq);
0155 }
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 static int pack_sg_list(struct scatterlist *sg, int start,
0172 int limit, char *data, int count)
0173 {
0174 int s;
0175 int index = start;
0176
0177 while (count) {
0178 s = rest_of_page(data);
0179 if (s > count)
0180 s = count;
0181 BUG_ON(index >= limit);
0182
0183 sg_unmark_end(&sg[index]);
0184 sg_set_buf(&sg[index++], data, s);
0185 count -= s;
0186 data += s;
0187 }
0188 if (index-start)
0189 sg_mark_end(&sg[index - 1]);
0190 return index-start;
0191 }
0192
0193
0194 static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
0195 {
0196 return 1;
0197 }
0198
0199
0200 static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
0201 {
0202 p9_req_put(client, req);
0203 return 0;
0204 }
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 static int
0218 pack_sg_list_p(struct scatterlist *sg, int start, int limit,
0219 struct page **pdata, int nr_pages, size_t offs, int count)
0220 {
0221 int i = 0, s;
0222 int data_off = offs;
0223 int index = start;
0224
0225 BUG_ON(nr_pages > (limit - start));
0226
0227
0228
0229
0230 while (nr_pages) {
0231 s = PAGE_SIZE - data_off;
0232 if (s > count)
0233 s = count;
0234 BUG_ON(index >= limit);
0235
0236 sg_unmark_end(&sg[index]);
0237 sg_set_page(&sg[index++], pdata[i++], s, data_off);
0238 data_off = 0;
0239 count -= s;
0240 nr_pages--;
0241 }
0242
0243 if (index-start)
0244 sg_mark_end(&sg[index - 1]);
0245 return index - start;
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255 static int
0256 p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
0257 {
0258 int err;
0259 int in, out, out_sgs, in_sgs;
0260 unsigned long flags;
0261 struct virtio_chan *chan = client->trans;
0262 struct scatterlist *sgs[2];
0263
0264 p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
0265
0266 req->status = REQ_STATUS_SENT;
0267 req_retry:
0268 spin_lock_irqsave(&chan->lock, flags);
0269
0270 out_sgs = in_sgs = 0;
0271
0272 out = pack_sg_list(chan->sg, 0,
0273 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
0274 if (out)
0275 sgs[out_sgs++] = chan->sg;
0276
0277 in = pack_sg_list(chan->sg, out,
0278 VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
0279 if (in)
0280 sgs[out_sgs + in_sgs++] = chan->sg + out;
0281
0282 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
0283 GFP_ATOMIC);
0284 if (err < 0) {
0285 if (err == -ENOSPC) {
0286 chan->ring_bufs_avail = 0;
0287 spin_unlock_irqrestore(&chan->lock, flags);
0288 err = wait_event_killable(*chan->vc_wq,
0289 chan->ring_bufs_avail);
0290 if (err == -ERESTARTSYS)
0291 return err;
0292
0293 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
0294 goto req_retry;
0295 } else {
0296 spin_unlock_irqrestore(&chan->lock, flags);
0297 p9_debug(P9_DEBUG_TRANS,
0298 "virtio rpc add_sgs returned failure\n");
0299 return -EIO;
0300 }
0301 }
0302 virtqueue_kick(chan->vq);
0303 spin_unlock_irqrestore(&chan->lock, flags);
0304
0305 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
0306 return 0;
0307 }
0308
0309 static int p9_get_mapped_pages(struct virtio_chan *chan,
0310 struct page ***pages,
0311 struct iov_iter *data,
0312 int count,
0313 size_t *offs,
0314 int *need_drop)
0315 {
0316 int nr_pages;
0317 int err;
0318
0319 if (!iov_iter_count(data))
0320 return 0;
0321
0322 if (!iov_iter_is_kvec(data)) {
0323 int n;
0324
0325
0326
0327
0328 if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
0329 err = wait_event_killable(vp_wq,
0330 (atomic_read(&vp_pinned) < chan->p9_max_pages));
0331 if (err == -ERESTARTSYS)
0332 return err;
0333 }
0334 n = iov_iter_get_pages_alloc2(data, pages, count, offs);
0335 if (n < 0)
0336 return n;
0337 *need_drop = 1;
0338 nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
0339 atomic_add(nr_pages, &vp_pinned);
0340 return n;
0341 } else {
0342
0343 int index;
0344 size_t len;
0345 void *p;
0346
0347
0348 while (1) {
0349 len = iov_iter_single_seg_count(data);
0350 if (likely(len)) {
0351 p = data->kvec->iov_base + data->iov_offset;
0352 break;
0353 }
0354 iov_iter_advance(data, 0);
0355 }
0356 if (len > count)
0357 len = count;
0358
0359 nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
0360 (unsigned long)p / PAGE_SIZE;
0361
0362 *pages = kmalloc_array(nr_pages, sizeof(struct page *),
0363 GFP_NOFS);
0364 if (!*pages)
0365 return -ENOMEM;
0366
0367 *need_drop = 0;
0368 p -= (*offs = offset_in_page(p));
0369 for (index = 0; index < nr_pages; index++) {
0370 if (is_vmalloc_addr(p))
0371 (*pages)[index] = vmalloc_to_page(p);
0372 else
0373 (*pages)[index] = kmap_to_page(p);
0374 p += PAGE_SIZE;
0375 }
0376 iov_iter_advance(data, len);
0377 return len;
0378 }
0379 }
0380
0381 static void handle_rerror(struct p9_req_t *req, int in_hdr_len,
0382 size_t offs, struct page **pages)
0383 {
0384 unsigned size, n;
0385 void *to = req->rc.sdata + in_hdr_len;
0386
0387
0388 if (req->rc.size < in_hdr_len)
0389 return;
0390
0391
0392
0393
0394
0395 if (unlikely(req->rc.size > P9_ZC_HDR_SZ))
0396 req->rc.size = P9_ZC_HDR_SZ;
0397
0398
0399 size = req->rc.size - in_hdr_len;
0400 n = PAGE_SIZE - offs;
0401 if (size > n) {
0402 memcpy_from_page(to, *pages++, offs, n);
0403 offs = 0;
0404 to += n;
0405 size -= n;
0406 }
0407 memcpy_from_page(to, *pages, offs, size);
0408 }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421 static int
0422 p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
0423 struct iov_iter *uidata, struct iov_iter *uodata,
0424 int inlen, int outlen, int in_hdr_len)
0425 {
0426 int in, out, err, out_sgs, in_sgs;
0427 unsigned long flags;
0428 int in_nr_pages = 0, out_nr_pages = 0;
0429 struct page **in_pages = NULL, **out_pages = NULL;
0430 struct virtio_chan *chan = client->trans;
0431 struct scatterlist *sgs[4];
0432 size_t offs;
0433 int need_drop = 0;
0434 int kicked = 0;
0435
0436 p9_debug(P9_DEBUG_TRANS, "virtio request\n");
0437
0438 if (uodata) {
0439 __le32 sz;
0440 int n = p9_get_mapped_pages(chan, &out_pages, uodata,
0441 outlen, &offs, &need_drop);
0442 if (n < 0) {
0443 err = n;
0444 goto err_out;
0445 }
0446 out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
0447 if (n != outlen) {
0448 __le32 v = cpu_to_le32(n);
0449 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
0450 outlen = n;
0451 }
0452
0453
0454
0455
0456 sz = cpu_to_le32(req->tc.size + outlen);
0457 memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
0458 } else if (uidata) {
0459 int n = p9_get_mapped_pages(chan, &in_pages, uidata,
0460 inlen, &offs, &need_drop);
0461 if (n < 0) {
0462 err = n;
0463 goto err_out;
0464 }
0465 in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
0466 if (n != inlen) {
0467 __le32 v = cpu_to_le32(n);
0468 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
0469 inlen = n;
0470 }
0471 }
0472 req->status = REQ_STATUS_SENT;
0473 req_retry_pinned:
0474 spin_lock_irqsave(&chan->lock, flags);
0475
0476 out_sgs = in_sgs = 0;
0477
0478
0479 out = pack_sg_list(chan->sg, 0,
0480 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
0481
0482 if (out)
0483 sgs[out_sgs++] = chan->sg;
0484
0485 if (out_pages) {
0486 sgs[out_sgs++] = chan->sg + out;
0487 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
0488 out_pages, out_nr_pages, offs, outlen);
0489 }
0490
0491
0492
0493
0494
0495
0496
0497
0498 in = pack_sg_list(chan->sg, out,
0499 VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
0500 if (in)
0501 sgs[out_sgs + in_sgs++] = chan->sg + out;
0502
0503 if (in_pages) {
0504 sgs[out_sgs + in_sgs++] = chan->sg + out + in;
0505 in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
0506 in_pages, in_nr_pages, offs, inlen);
0507 }
0508
0509 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
0510 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
0511 GFP_ATOMIC);
0512 if (err < 0) {
0513 if (err == -ENOSPC) {
0514 chan->ring_bufs_avail = 0;
0515 spin_unlock_irqrestore(&chan->lock, flags);
0516 err = wait_event_killable(*chan->vc_wq,
0517 chan->ring_bufs_avail);
0518 if (err == -ERESTARTSYS)
0519 goto err_out;
0520
0521 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
0522 goto req_retry_pinned;
0523 } else {
0524 spin_unlock_irqrestore(&chan->lock, flags);
0525 p9_debug(P9_DEBUG_TRANS,
0526 "virtio rpc add_sgs returned failure\n");
0527 err = -EIO;
0528 goto err_out;
0529 }
0530 }
0531 virtqueue_kick(chan->vq);
0532 spin_unlock_irqrestore(&chan->lock, flags);
0533 kicked = 1;
0534 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
0535 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
0536
0537 if (req->status == REQ_STATUS_RCVD &&
0538 unlikely(req->rc.sdata[4] == P9_RERROR))
0539 handle_rerror(req, in_hdr_len, offs, in_pages);
0540
0541
0542
0543
0544 err_out:
0545 if (need_drop) {
0546 if (in_pages) {
0547 p9_release_pages(in_pages, in_nr_pages);
0548 atomic_sub(in_nr_pages, &vp_pinned);
0549 }
0550 if (out_pages) {
0551 p9_release_pages(out_pages, out_nr_pages);
0552 atomic_sub(out_nr_pages, &vp_pinned);
0553 }
0554
0555 wake_up(&vp_wq);
0556 }
0557 kvfree(in_pages);
0558 kvfree(out_pages);
0559 if (!kicked) {
0560
0561 p9_req_put(client, req);
0562 }
0563 return err;
0564 }
0565
0566 static ssize_t p9_mount_tag_show(struct device *dev,
0567 struct device_attribute *attr, char *buf)
0568 {
0569 struct virtio_chan *chan;
0570 struct virtio_device *vdev;
0571 int tag_len;
0572
0573 vdev = dev_to_virtio(dev);
0574 chan = vdev->priv;
0575 tag_len = strlen(chan->tag);
0576
0577 memcpy(buf, chan->tag, tag_len + 1);
0578
0579 return tag_len + 1;
0580 }
0581
0582 static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592 static int p9_virtio_probe(struct virtio_device *vdev)
0593 {
0594 __u16 tag_len;
0595 char *tag;
0596 int err;
0597 struct virtio_chan *chan;
0598
0599 if (!vdev->config->get) {
0600 dev_err(&vdev->dev, "%s failure: config access disabled\n",
0601 __func__);
0602 return -EINVAL;
0603 }
0604
0605 chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
0606 if (!chan) {
0607 pr_err("Failed to allocate virtio 9P channel\n");
0608 err = -ENOMEM;
0609 goto fail;
0610 }
0611
0612 chan->vdev = vdev;
0613
0614
0615 chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
0616 if (IS_ERR(chan->vq)) {
0617 err = PTR_ERR(chan->vq);
0618 goto out_free_chan;
0619 }
0620 chan->vq->vdev->priv = chan;
0621 spin_lock_init(&chan->lock);
0622
0623 sg_init_table(chan->sg, VIRTQUEUE_NUM);
0624
0625 chan->inuse = false;
0626 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
0627 virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
0628 } else {
0629 err = -EINVAL;
0630 goto out_free_vq;
0631 }
0632 tag = kzalloc(tag_len + 1, GFP_KERNEL);
0633 if (!tag) {
0634 err = -ENOMEM;
0635 goto out_free_vq;
0636 }
0637
0638 virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
0639 tag, tag_len);
0640 chan->tag = tag;
0641 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
0642 if (err) {
0643 goto out_free_tag;
0644 }
0645 chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
0646 if (!chan->vc_wq) {
0647 err = -ENOMEM;
0648 goto out_remove_file;
0649 }
0650 init_waitqueue_head(chan->vc_wq);
0651 chan->ring_bufs_avail = 1;
0652
0653 chan->p9_max_pages = nr_free_buffer_pages()/4;
0654
0655 virtio_device_ready(vdev);
0656
0657 mutex_lock(&virtio_9p_lock);
0658 list_add_tail(&chan->chan_list, &virtio_chan_list);
0659 mutex_unlock(&virtio_9p_lock);
0660
0661
0662 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
0663
0664 return 0;
0665
0666 out_remove_file:
0667 sysfs_remove_file(&vdev->dev.kobj, &dev_attr_mount_tag.attr);
0668 out_free_tag:
0669 kfree(tag);
0670 out_free_vq:
0671 vdev->config->del_vqs(vdev);
0672 out_free_chan:
0673 kfree(chan);
0674 fail:
0675 return err;
0676 }
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693 static int
0694 p9_virtio_create(struct p9_client *client, const char *devname, char *args)
0695 {
0696 struct virtio_chan *chan;
0697 int ret = -ENOENT;
0698 int found = 0;
0699
0700 if (devname == NULL)
0701 return -EINVAL;
0702
0703 mutex_lock(&virtio_9p_lock);
0704 list_for_each_entry(chan, &virtio_chan_list, chan_list) {
0705 if (!strcmp(devname, chan->tag)) {
0706 if (!chan->inuse) {
0707 chan->inuse = true;
0708 found = 1;
0709 break;
0710 }
0711 ret = -EBUSY;
0712 }
0713 }
0714 mutex_unlock(&virtio_9p_lock);
0715
0716 if (!found) {
0717 pr_err("no channels available for device %s\n", devname);
0718 return ret;
0719 }
0720
0721 client->trans = (void *)chan;
0722 client->status = Connected;
0723 chan->client = client;
0724
0725 return 0;
0726 }
0727
0728
0729
0730
0731
0732
0733
0734 static void p9_virtio_remove(struct virtio_device *vdev)
0735 {
0736 struct virtio_chan *chan = vdev->priv;
0737 unsigned long warning_time;
0738
0739 mutex_lock(&virtio_9p_lock);
0740
0741
0742 list_del(&chan->chan_list);
0743 warning_time = jiffies;
0744
0745
0746 while (chan->inuse) {
0747 mutex_unlock(&virtio_9p_lock);
0748 msleep(250);
0749 if (time_after(jiffies, warning_time + 10 * HZ)) {
0750 dev_emerg(&vdev->dev,
0751 "p9_virtio_remove: waiting for device in use.\n");
0752 warning_time = jiffies;
0753 }
0754 mutex_lock(&virtio_9p_lock);
0755 }
0756
0757 mutex_unlock(&virtio_9p_lock);
0758
0759 virtio_reset_device(vdev);
0760 vdev->config->del_vqs(vdev);
0761
0762 sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
0763 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
0764 kfree(chan->tag);
0765 kfree(chan->vc_wq);
0766 kfree(chan);
0767
0768 }
0769
0770 static struct virtio_device_id id_table[] = {
0771 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
0772 { 0 },
0773 };
0774
0775 static unsigned int features[] = {
0776 VIRTIO_9P_MOUNT_TAG,
0777 };
0778
0779
0780 static struct virtio_driver p9_virtio_drv = {
0781 .feature_table = features,
0782 .feature_table_size = ARRAY_SIZE(features),
0783 .driver.name = KBUILD_MODNAME,
0784 .driver.owner = THIS_MODULE,
0785 .id_table = id_table,
0786 .probe = p9_virtio_probe,
0787 .remove = p9_virtio_remove,
0788 };
0789
0790 static struct p9_trans_module p9_virtio_trans = {
0791 .name = "virtio",
0792 .create = p9_virtio_create,
0793 .close = p9_virtio_close,
0794 .request = p9_virtio_request,
0795 .zc_request = p9_virtio_zc_request,
0796 .cancel = p9_virtio_cancel,
0797 .cancelled = p9_virtio_cancelled,
0798
0799
0800
0801
0802
0803
0804 .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
0805 .def = 1,
0806 .owner = THIS_MODULE,
0807 };
0808
0809
0810 static int __init p9_virtio_init(void)
0811 {
0812 int rc;
0813
0814 INIT_LIST_HEAD(&virtio_chan_list);
0815
0816 v9fs_register_trans(&p9_virtio_trans);
0817 rc = register_virtio_driver(&p9_virtio_drv);
0818 if (rc)
0819 v9fs_unregister_trans(&p9_virtio_trans);
0820
0821 return rc;
0822 }
0823
0824 static void __exit p9_virtio_cleanup(void)
0825 {
0826 unregister_virtio_driver(&p9_virtio_drv);
0827 v9fs_unregister_trans(&p9_virtio_trans);
0828 }
0829
0830 module_init(p9_virtio_init);
0831 module_exit(p9_virtio_cleanup);
0832 MODULE_ALIAS_9P("virtio");
0833
0834 MODULE_DEVICE_TABLE(virtio, id_table);
0835 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
0836 MODULE_DESCRIPTION("Virtio 9p Transport");
0837 MODULE_LICENSE("GPL");