Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * virtio-fs: Virtio Filesystem
0004  * Copyright (C) 2018 Red Hat, Inc.
0005  */
0006 
0007 #include <linux/fs.h>
0008 #include <linux/dax.h>
0009 #include <linux/pci.h>
0010 #include <linux/pfn_t.h>
0011 #include <linux/memremap.h>
0012 #include <linux/module.h>
0013 #include <linux/virtio.h>
0014 #include <linux/virtio_fs.h>
0015 #include <linux/delay.h>
0016 #include <linux/fs_context.h>
0017 #include <linux/fs_parser.h>
0018 #include <linux/highmem.h>
0019 #include <linux/uio.h>
0020 #include "fuse_i.h"
0021 
0022 /* Used to help calculate the FUSE connection's max_pages limit for a request's
0023  * size. Parts of the struct fuse_req are sliced into scattergather lists in
0024  * addition to the pages used, so this can help account for that overhead.
0025  */
0026 #define FUSE_HEADER_OVERHEAD    4
0027 
0028 /* List of virtio-fs device instances and a lock for the list. Also provides
0029  * mutual exclusion in device removal and mounting path
0030  */
0031 static DEFINE_MUTEX(virtio_fs_mutex);
0032 static LIST_HEAD(virtio_fs_instances);
0033 
0034 enum {
0035     VQ_HIPRIO,
0036     VQ_REQUEST
0037 };
0038 
0039 #define VQ_NAME_LEN 24
0040 
0041 /* Per-virtqueue state */
0042 struct virtio_fs_vq {
0043     spinlock_t lock;
0044     struct virtqueue *vq;     /* protected by ->lock */
0045     struct work_struct done_work;
0046     struct list_head queued_reqs;
0047     struct list_head end_reqs;  /* End these requests */
0048     struct delayed_work dispatch_work;
0049     struct fuse_dev *fud;
0050     bool connected;
0051     long in_flight;
0052     struct completion in_flight_zero; /* No inflight requests */
0053     char name[VQ_NAME_LEN];
0054 } ____cacheline_aligned_in_smp;
0055 
0056 /* A virtio-fs device instance */
0057 struct virtio_fs {
0058     struct kref refcount;
0059     struct list_head list;    /* on virtio_fs_instances */
0060     char *tag;
0061     struct virtio_fs_vq *vqs;
0062     unsigned int nvqs;               /* number of virtqueues */
0063     unsigned int num_request_queues; /* number of request queues */
0064     struct dax_device *dax_dev;
0065 
0066     /* DAX memory window where file contents are mapped */
0067     void *window_kaddr;
0068     phys_addr_t window_phys_addr;
0069     size_t window_len;
0070 };
0071 
0072 struct virtio_fs_forget_req {
0073     struct fuse_in_header ih;
0074     struct fuse_forget_in arg;
0075 };
0076 
0077 struct virtio_fs_forget {
0078     /* This request can be temporarily queued on virt queue */
0079     struct list_head list;
0080     struct virtio_fs_forget_req req;
0081 };
0082 
0083 struct virtio_fs_req_work {
0084     struct fuse_req *req;
0085     struct virtio_fs_vq *fsvq;
0086     struct work_struct done_work;
0087 };
0088 
0089 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
0090                  struct fuse_req *req, bool in_flight);
0091 
0092 static const struct constant_table dax_param_enums[] = {
0093     {"always",  FUSE_DAX_ALWAYS },
0094     {"never",   FUSE_DAX_NEVER },
0095     {"inode",   FUSE_DAX_INODE_USER },
0096     {}
0097 };
0098 
0099 enum {
0100     OPT_DAX,
0101     OPT_DAX_ENUM,
0102 };
0103 
0104 static const struct fs_parameter_spec virtio_fs_parameters[] = {
0105     fsparam_flag("dax", OPT_DAX),
0106     fsparam_enum("dax", OPT_DAX_ENUM, dax_param_enums),
0107     {}
0108 };
0109 
0110 static int virtio_fs_parse_param(struct fs_context *fsc,
0111                  struct fs_parameter *param)
0112 {
0113     struct fs_parse_result result;
0114     struct fuse_fs_context *ctx = fsc->fs_private;
0115     int opt;
0116 
0117     opt = fs_parse(fsc, virtio_fs_parameters, param, &result);
0118     if (opt < 0)
0119         return opt;
0120 
0121     switch (opt) {
0122     case OPT_DAX:
0123         ctx->dax_mode = FUSE_DAX_ALWAYS;
0124         break;
0125     case OPT_DAX_ENUM:
0126         ctx->dax_mode = result.uint_32;
0127         break;
0128     default:
0129         return -EINVAL;
0130     }
0131 
0132     return 0;
0133 }
0134 
0135 static void virtio_fs_free_fsc(struct fs_context *fsc)
0136 {
0137     struct fuse_fs_context *ctx = fsc->fs_private;
0138 
0139     kfree(ctx);
0140 }
0141 
0142 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
0143 {
0144     struct virtio_fs *fs = vq->vdev->priv;
0145 
0146     return &fs->vqs[vq->index];
0147 }
0148 
0149 /* Should be called with fsvq->lock held. */
0150 static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq)
0151 {
0152     fsvq->in_flight++;
0153 }
0154 
0155 /* Should be called with fsvq->lock held. */
0156 static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq)
0157 {
0158     WARN_ON(fsvq->in_flight <= 0);
0159     fsvq->in_flight--;
0160     if (!fsvq->in_flight)
0161         complete(&fsvq->in_flight_zero);
0162 }
0163 
0164 static void release_virtio_fs_obj(struct kref *ref)
0165 {
0166     struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount);
0167 
0168     kfree(vfs->vqs);
0169     kfree(vfs);
0170 }
0171 
0172 /* Make sure virtiofs_mutex is held */
0173 static void virtio_fs_put(struct virtio_fs *fs)
0174 {
0175     kref_put(&fs->refcount, release_virtio_fs_obj);
0176 }
0177 
0178 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
0179 {
0180     struct virtio_fs *vfs = fiq->priv;
0181 
0182     mutex_lock(&virtio_fs_mutex);
0183     virtio_fs_put(vfs);
0184     mutex_unlock(&virtio_fs_mutex);
0185 }
0186 
0187 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
0188 {
0189     WARN_ON(fsvq->in_flight < 0);
0190 
0191     /* Wait for in flight requests to finish.*/
0192     spin_lock(&fsvq->lock);
0193     if (fsvq->in_flight) {
0194         /* We are holding virtio_fs_mutex. There should not be any
0195          * waiters waiting for completion.
0196          */
0197         reinit_completion(&fsvq->in_flight_zero);
0198         spin_unlock(&fsvq->lock);
0199         wait_for_completion(&fsvq->in_flight_zero);
0200     } else {
0201         spin_unlock(&fsvq->lock);
0202     }
0203 
0204     flush_work(&fsvq->done_work);
0205     flush_delayed_work(&fsvq->dispatch_work);
0206 }
0207 
0208 static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs)
0209 {
0210     struct virtio_fs_vq *fsvq;
0211     int i;
0212 
0213     for (i = 0; i < fs->nvqs; i++) {
0214         fsvq = &fs->vqs[i];
0215         virtio_fs_drain_queue(fsvq);
0216     }
0217 }
0218 
0219 static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
0220 {
0221     /* Provides mutual exclusion between ->remove and ->kill_sb
0222      * paths. We don't want both of these draining queue at the
0223      * same time. Current completion logic reinits completion
0224      * and that means there should not be any other thread
0225      * doing reinit or waiting for completion already.
0226      */
0227     mutex_lock(&virtio_fs_mutex);
0228     virtio_fs_drain_all_queues_locked(fs);
0229     mutex_unlock(&virtio_fs_mutex);
0230 }
0231 
0232 static void virtio_fs_start_all_queues(struct virtio_fs *fs)
0233 {
0234     struct virtio_fs_vq *fsvq;
0235     int i;
0236 
0237     for (i = 0; i < fs->nvqs; i++) {
0238         fsvq = &fs->vqs[i];
0239         spin_lock(&fsvq->lock);
0240         fsvq->connected = true;
0241         spin_unlock(&fsvq->lock);
0242     }
0243 }
0244 
0245 /* Add a new instance to the list or return -EEXIST if tag name exists*/
0246 static int virtio_fs_add_instance(struct virtio_fs *fs)
0247 {
0248     struct virtio_fs *fs2;
0249     bool duplicate = false;
0250 
0251     mutex_lock(&virtio_fs_mutex);
0252 
0253     list_for_each_entry(fs2, &virtio_fs_instances, list) {
0254         if (strcmp(fs->tag, fs2->tag) == 0)
0255             duplicate = true;
0256     }
0257 
0258     if (!duplicate)
0259         list_add_tail(&fs->list, &virtio_fs_instances);
0260 
0261     mutex_unlock(&virtio_fs_mutex);
0262 
0263     if (duplicate)
0264         return -EEXIST;
0265     return 0;
0266 }
0267 
0268 /* Return the virtio_fs with a given tag, or NULL */
0269 static struct virtio_fs *virtio_fs_find_instance(const char *tag)
0270 {
0271     struct virtio_fs *fs;
0272 
0273     mutex_lock(&virtio_fs_mutex);
0274 
0275     list_for_each_entry(fs, &virtio_fs_instances, list) {
0276         if (strcmp(fs->tag, tag) == 0) {
0277             kref_get(&fs->refcount);
0278             goto found;
0279         }
0280     }
0281 
0282     fs = NULL; /* not found */
0283 
0284 found:
0285     mutex_unlock(&virtio_fs_mutex);
0286 
0287     return fs;
0288 }
0289 
0290 static void virtio_fs_free_devs(struct virtio_fs *fs)
0291 {
0292     unsigned int i;
0293 
0294     for (i = 0; i < fs->nvqs; i++) {
0295         struct virtio_fs_vq *fsvq = &fs->vqs[i];
0296 
0297         if (!fsvq->fud)
0298             continue;
0299 
0300         fuse_dev_free(fsvq->fud);
0301         fsvq->fud = NULL;
0302     }
0303 }
0304 
0305 /* Read filesystem name from virtio config into fs->tag (must kfree()). */
0306 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
0307 {
0308     char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
0309     char *end;
0310     size_t len;
0311 
0312     virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
0313                &tag_buf, sizeof(tag_buf));
0314     end = memchr(tag_buf, '\0', sizeof(tag_buf));
0315     if (end == tag_buf)
0316         return -EINVAL; /* empty tag */
0317     if (!end)
0318         end = &tag_buf[sizeof(tag_buf)];
0319 
0320     len = end - tag_buf;
0321     fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
0322     if (!fs->tag)
0323         return -ENOMEM;
0324     memcpy(fs->tag, tag_buf, len);
0325     fs->tag[len] = '\0';
0326     return 0;
0327 }
0328 
0329 /* Work function for hiprio completion */
0330 static void virtio_fs_hiprio_done_work(struct work_struct *work)
0331 {
0332     struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
0333                          done_work);
0334     struct virtqueue *vq = fsvq->vq;
0335 
0336     /* Free completed FUSE_FORGET requests */
0337     spin_lock(&fsvq->lock);
0338     do {
0339         unsigned int len;
0340         void *req;
0341 
0342         virtqueue_disable_cb(vq);
0343 
0344         while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
0345             kfree(req);
0346             dec_in_flight_req(fsvq);
0347         }
0348     } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
0349     spin_unlock(&fsvq->lock);
0350 }
0351 
0352 static void virtio_fs_request_dispatch_work(struct work_struct *work)
0353 {
0354     struct fuse_req *req;
0355     struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
0356                          dispatch_work.work);
0357     int ret;
0358 
0359     pr_debug("virtio-fs: worker %s called.\n", __func__);
0360     while (1) {
0361         spin_lock(&fsvq->lock);
0362         req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
0363                            list);
0364         if (!req) {
0365             spin_unlock(&fsvq->lock);
0366             break;
0367         }
0368 
0369         list_del_init(&req->list);
0370         spin_unlock(&fsvq->lock);
0371         fuse_request_end(req);
0372     }
0373 
0374     /* Dispatch pending requests */
0375     while (1) {
0376         spin_lock(&fsvq->lock);
0377         req = list_first_entry_or_null(&fsvq->queued_reqs,
0378                            struct fuse_req, list);
0379         if (!req) {
0380             spin_unlock(&fsvq->lock);
0381             return;
0382         }
0383         list_del_init(&req->list);
0384         spin_unlock(&fsvq->lock);
0385 
0386         ret = virtio_fs_enqueue_req(fsvq, req, true);
0387         if (ret < 0) {
0388             if (ret == -ENOMEM || ret == -ENOSPC) {
0389                 spin_lock(&fsvq->lock);
0390                 list_add_tail(&req->list, &fsvq->queued_reqs);
0391                 schedule_delayed_work(&fsvq->dispatch_work,
0392                               msecs_to_jiffies(1));
0393                 spin_unlock(&fsvq->lock);
0394                 return;
0395             }
0396             req->out.h.error = ret;
0397             spin_lock(&fsvq->lock);
0398             dec_in_flight_req(fsvq);
0399             spin_unlock(&fsvq->lock);
0400             pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n",
0401                    ret);
0402             fuse_request_end(req);
0403         }
0404     }
0405 }
0406 
0407 /*
0408  * Returns 1 if queue is full and sender should wait a bit before sending
0409  * next request, 0 otherwise.
0410  */
0411 static int send_forget_request(struct virtio_fs_vq *fsvq,
0412                    struct virtio_fs_forget *forget,
0413                    bool in_flight)
0414 {
0415     struct scatterlist sg;
0416     struct virtqueue *vq;
0417     int ret = 0;
0418     bool notify;
0419     struct virtio_fs_forget_req *req = &forget->req;
0420 
0421     spin_lock(&fsvq->lock);
0422     if (!fsvq->connected) {
0423         if (in_flight)
0424             dec_in_flight_req(fsvq);
0425         kfree(forget);
0426         goto out;
0427     }
0428 
0429     sg_init_one(&sg, req, sizeof(*req));
0430     vq = fsvq->vq;
0431     dev_dbg(&vq->vdev->dev, "%s\n", __func__);
0432 
0433     ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC);
0434     if (ret < 0) {
0435         if (ret == -ENOMEM || ret == -ENOSPC) {
0436             pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
0437                  ret);
0438             list_add_tail(&forget->list, &fsvq->queued_reqs);
0439             schedule_delayed_work(&fsvq->dispatch_work,
0440                           msecs_to_jiffies(1));
0441             if (!in_flight)
0442                 inc_in_flight_req(fsvq);
0443             /* Queue is full */
0444             ret = 1;
0445         } else {
0446             pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
0447                  ret);
0448             kfree(forget);
0449             if (in_flight)
0450                 dec_in_flight_req(fsvq);
0451         }
0452         goto out;
0453     }
0454 
0455     if (!in_flight)
0456         inc_in_flight_req(fsvq);
0457     notify = virtqueue_kick_prepare(vq);
0458     spin_unlock(&fsvq->lock);
0459 
0460     if (notify)
0461         virtqueue_notify(vq);
0462     return ret;
0463 out:
0464     spin_unlock(&fsvq->lock);
0465     return ret;
0466 }
0467 
0468 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
0469 {
0470     struct virtio_fs_forget *forget;
0471     struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
0472                          dispatch_work.work);
0473     pr_debug("virtio-fs: worker %s called.\n", __func__);
0474     while (1) {
0475         spin_lock(&fsvq->lock);
0476         forget = list_first_entry_or_null(&fsvq->queued_reqs,
0477                     struct virtio_fs_forget, list);
0478         if (!forget) {
0479             spin_unlock(&fsvq->lock);
0480             return;
0481         }
0482 
0483         list_del(&forget->list);
0484         spin_unlock(&fsvq->lock);
0485         if (send_forget_request(fsvq, forget, true))
0486             return;
0487     }
0488 }
0489 
0490 /* Allocate and copy args into req->argbuf */
0491 static int copy_args_to_argbuf(struct fuse_req *req)
0492 {
0493     struct fuse_args *args = req->args;
0494     unsigned int offset = 0;
0495     unsigned int num_in;
0496     unsigned int num_out;
0497     unsigned int len;
0498     unsigned int i;
0499 
0500     num_in = args->in_numargs - args->in_pages;
0501     num_out = args->out_numargs - args->out_pages;
0502     len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
0503           fuse_len_args(num_out, args->out_args);
0504 
0505     req->argbuf = kmalloc(len, GFP_ATOMIC);
0506     if (!req->argbuf)
0507         return -ENOMEM;
0508 
0509     for (i = 0; i < num_in; i++) {
0510         memcpy(req->argbuf + offset,
0511                args->in_args[i].value,
0512                args->in_args[i].size);
0513         offset += args->in_args[i].size;
0514     }
0515 
0516     return 0;
0517 }
0518 
0519 /* Copy args out of and free req->argbuf */
0520 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
0521 {
0522     unsigned int remaining;
0523     unsigned int offset;
0524     unsigned int num_in;
0525     unsigned int num_out;
0526     unsigned int i;
0527 
0528     remaining = req->out.h.len - sizeof(req->out.h);
0529     num_in = args->in_numargs - args->in_pages;
0530     num_out = args->out_numargs - args->out_pages;
0531     offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
0532 
0533     for (i = 0; i < num_out; i++) {
0534         unsigned int argsize = args->out_args[i].size;
0535 
0536         if (args->out_argvar &&
0537             i == args->out_numargs - 1 &&
0538             argsize > remaining) {
0539             argsize = remaining;
0540         }
0541 
0542         memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
0543         offset += argsize;
0544 
0545         if (i != args->out_numargs - 1)
0546             remaining -= argsize;
0547     }
0548 
0549     /* Store the actual size of the variable-length arg */
0550     if (args->out_argvar)
0551         args->out_args[args->out_numargs - 1].size = remaining;
0552 
0553     kfree(req->argbuf);
0554     req->argbuf = NULL;
0555 }
0556 
0557 /* Work function for request completion */
0558 static void virtio_fs_request_complete(struct fuse_req *req,
0559                        struct virtio_fs_vq *fsvq)
0560 {
0561     struct fuse_pqueue *fpq = &fsvq->fud->pq;
0562     struct fuse_args *args;
0563     struct fuse_args_pages *ap;
0564     unsigned int len, i, thislen;
0565     struct page *page;
0566 
0567     /*
0568      * TODO verify that server properly follows FUSE protocol
0569      * (oh.uniq, oh.len)
0570      */
0571     args = req->args;
0572     copy_args_from_argbuf(args, req);
0573 
0574     if (args->out_pages && args->page_zeroing) {
0575         len = args->out_args[args->out_numargs - 1].size;
0576         ap = container_of(args, typeof(*ap), args);
0577         for (i = 0; i < ap->num_pages; i++) {
0578             thislen = ap->descs[i].length;
0579             if (len < thislen) {
0580                 WARN_ON(ap->descs[i].offset);
0581                 page = ap->pages[i];
0582                 zero_user_segment(page, len, thislen);
0583                 len = 0;
0584             } else {
0585                 len -= thislen;
0586             }
0587         }
0588     }
0589 
0590     spin_lock(&fpq->lock);
0591     clear_bit(FR_SENT, &req->flags);
0592     spin_unlock(&fpq->lock);
0593 
0594     fuse_request_end(req);
0595     spin_lock(&fsvq->lock);
0596     dec_in_flight_req(fsvq);
0597     spin_unlock(&fsvq->lock);
0598 }
0599 
0600 static void virtio_fs_complete_req_work(struct work_struct *work)
0601 {
0602     struct virtio_fs_req_work *w =
0603         container_of(work, typeof(*w), done_work);
0604 
0605     virtio_fs_request_complete(w->req, w->fsvq);
0606     kfree(w);
0607 }
0608 
0609 static void virtio_fs_requests_done_work(struct work_struct *work)
0610 {
0611     struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
0612                          done_work);
0613     struct fuse_pqueue *fpq = &fsvq->fud->pq;
0614     struct virtqueue *vq = fsvq->vq;
0615     struct fuse_req *req;
0616     struct fuse_req *next;
0617     unsigned int len;
0618     LIST_HEAD(reqs);
0619 
0620     /* Collect completed requests off the virtqueue */
0621     spin_lock(&fsvq->lock);
0622     do {
0623         virtqueue_disable_cb(vq);
0624 
0625         while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
0626             spin_lock(&fpq->lock);
0627             list_move_tail(&req->list, &reqs);
0628             spin_unlock(&fpq->lock);
0629         }
0630     } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
0631     spin_unlock(&fsvq->lock);
0632 
0633     /* End requests */
0634     list_for_each_entry_safe(req, next, &reqs, list) {
0635         list_del_init(&req->list);
0636 
0637         /* blocking async request completes in a worker context */
0638         if (req->args->may_block) {
0639             struct virtio_fs_req_work *w;
0640 
0641             w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL);
0642             INIT_WORK(&w->done_work, virtio_fs_complete_req_work);
0643             w->fsvq = fsvq;
0644             w->req = req;
0645             schedule_work(&w->done_work);
0646         } else {
0647             virtio_fs_request_complete(req, fsvq);
0648         }
0649     }
0650 }
0651 
0652 /* Virtqueue interrupt handler */
0653 static void virtio_fs_vq_done(struct virtqueue *vq)
0654 {
0655     struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
0656 
0657     dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
0658 
0659     schedule_work(&fsvq->done_work);
0660 }
0661 
0662 static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name,
0663                   int vq_type)
0664 {
0665     strscpy(fsvq->name, name, VQ_NAME_LEN);
0666     spin_lock_init(&fsvq->lock);
0667     INIT_LIST_HEAD(&fsvq->queued_reqs);
0668     INIT_LIST_HEAD(&fsvq->end_reqs);
0669     init_completion(&fsvq->in_flight_zero);
0670 
0671     if (vq_type == VQ_REQUEST) {
0672         INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work);
0673         INIT_DELAYED_WORK(&fsvq->dispatch_work,
0674                   virtio_fs_request_dispatch_work);
0675     } else {
0676         INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work);
0677         INIT_DELAYED_WORK(&fsvq->dispatch_work,
0678                   virtio_fs_hiprio_dispatch_work);
0679     }
0680 }
0681 
0682 /* Initialize virtqueues */
0683 static int virtio_fs_setup_vqs(struct virtio_device *vdev,
0684                    struct virtio_fs *fs)
0685 {
0686     struct virtqueue **vqs;
0687     vq_callback_t **callbacks;
0688     const char **names;
0689     unsigned int i;
0690     int ret = 0;
0691 
0692     virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues,
0693             &fs->num_request_queues);
0694     if (fs->num_request_queues == 0)
0695         return -EINVAL;
0696 
0697     fs->nvqs = VQ_REQUEST + fs->num_request_queues;
0698     fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
0699     if (!fs->vqs)
0700         return -ENOMEM;
0701 
0702     vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
0703     callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
0704                     GFP_KERNEL);
0705     names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL);
0706     if (!vqs || !callbacks || !names) {
0707         ret = -ENOMEM;
0708         goto out;
0709     }
0710 
0711     /* Initialize the hiprio/forget request virtqueue */
0712     callbacks[VQ_HIPRIO] = virtio_fs_vq_done;
0713     virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO);
0714     names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
0715 
0716     /* Initialize the requests virtqueues */
0717     for (i = VQ_REQUEST; i < fs->nvqs; i++) {
0718         char vq_name[VQ_NAME_LEN];
0719 
0720         snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST);
0721         virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST);
0722         callbacks[i] = virtio_fs_vq_done;
0723         names[i] = fs->vqs[i].name;
0724     }
0725 
0726     ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
0727     if (ret < 0)
0728         goto out;
0729 
0730     for (i = 0; i < fs->nvqs; i++)
0731         fs->vqs[i].vq = vqs[i];
0732 
0733     virtio_fs_start_all_queues(fs);
0734 out:
0735     kfree(names);
0736     kfree(callbacks);
0737     kfree(vqs);
0738     if (ret)
0739         kfree(fs->vqs);
0740     return ret;
0741 }
0742 
0743 /* Free virtqueues (device must already be reset) */
0744 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev)
0745 {
0746     vdev->config->del_vqs(vdev);
0747 }
0748 
0749 /* Map a window offset to a page frame number.  The window offset will have
0750  * been produced by .iomap_begin(), which maps a file offset to a window
0751  * offset.
0752  */
0753 static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
0754                     long nr_pages, enum dax_access_mode mode,
0755                     void **kaddr, pfn_t *pfn)
0756 {
0757     struct virtio_fs *fs = dax_get_private(dax_dev);
0758     phys_addr_t offset = PFN_PHYS(pgoff);
0759     size_t max_nr_pages = fs->window_len / PAGE_SIZE - pgoff;
0760 
0761     if (kaddr)
0762         *kaddr = fs->window_kaddr + offset;
0763     if (pfn)
0764         *pfn = phys_to_pfn_t(fs->window_phys_addr + offset,
0765                     PFN_DEV | PFN_MAP);
0766     return nr_pages > max_nr_pages ? max_nr_pages : nr_pages;
0767 }
0768 
0769 static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
0770                      pgoff_t pgoff, size_t nr_pages)
0771 {
0772     long rc;
0773     void *kaddr;
0774 
0775     rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS, &kaddr,
0776                    NULL);
0777     if (rc < 0)
0778         return rc;
0779     memset(kaddr, 0, nr_pages << PAGE_SHIFT);
0780     dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
0781     return 0;
0782 }
0783 
0784 static const struct dax_operations virtio_fs_dax_ops = {
0785     .direct_access = virtio_fs_direct_access,
0786     .zero_page_range = virtio_fs_zero_page_range,
0787 };
0788 
0789 static void virtio_fs_cleanup_dax(void *data)
0790 {
0791     struct dax_device *dax_dev = data;
0792 
0793     kill_dax(dax_dev);
0794     put_dax(dax_dev);
0795 }
0796 
0797 static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs)
0798 {
0799     struct virtio_shm_region cache_reg;
0800     struct dev_pagemap *pgmap;
0801     bool have_cache;
0802 
0803     if (!IS_ENABLED(CONFIG_FUSE_DAX))
0804         return 0;
0805 
0806     /* Get cache region */
0807     have_cache = virtio_get_shm_region(vdev, &cache_reg,
0808                        (u8)VIRTIO_FS_SHMCAP_ID_CACHE);
0809     if (!have_cache) {
0810         dev_notice(&vdev->dev, "%s: No cache capability\n", __func__);
0811         return 0;
0812     }
0813 
0814     if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len,
0815                      dev_name(&vdev->dev))) {
0816         dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n",
0817              cache_reg.addr, cache_reg.len);
0818         return -EBUSY;
0819     }
0820 
0821     dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len,
0822            cache_reg.addr);
0823 
0824     pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL);
0825     if (!pgmap)
0826         return -ENOMEM;
0827 
0828     pgmap->type = MEMORY_DEVICE_FS_DAX;
0829 
0830     /* Ideally we would directly use the PCI BAR resource but
0831      * devm_memremap_pages() wants its own copy in pgmap.  So
0832      * initialize a struct resource from scratch (only the start
0833      * and end fields will be used).
0834      */
0835     pgmap->range = (struct range) {
0836         .start = (phys_addr_t) cache_reg.addr,
0837         .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1,
0838     };
0839     pgmap->nr_range = 1;
0840 
0841     fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap);
0842     if (IS_ERR(fs->window_kaddr))
0843         return PTR_ERR(fs->window_kaddr);
0844 
0845     fs->window_phys_addr = (phys_addr_t) cache_reg.addr;
0846     fs->window_len = (phys_addr_t) cache_reg.len;
0847 
0848     dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n",
0849         __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len);
0850 
0851     fs->dax_dev = alloc_dax(fs, &virtio_fs_dax_ops);
0852     if (IS_ERR(fs->dax_dev))
0853         return PTR_ERR(fs->dax_dev);
0854 
0855     return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax,
0856                     fs->dax_dev);
0857 }
0858 
0859 static int virtio_fs_probe(struct virtio_device *vdev)
0860 {
0861     struct virtio_fs *fs;
0862     int ret;
0863 
0864     fs = kzalloc(sizeof(*fs), GFP_KERNEL);
0865     if (!fs)
0866         return -ENOMEM;
0867     kref_init(&fs->refcount);
0868     vdev->priv = fs;
0869 
0870     ret = virtio_fs_read_tag(vdev, fs);
0871     if (ret < 0)
0872         goto out;
0873 
0874     ret = virtio_fs_setup_vqs(vdev, fs);
0875     if (ret < 0)
0876         goto out;
0877 
0878     /* TODO vq affinity */
0879 
0880     ret = virtio_fs_setup_dax(vdev, fs);
0881     if (ret < 0)
0882         goto out_vqs;
0883 
0884     /* Bring the device online in case the filesystem is mounted and
0885      * requests need to be sent before we return.
0886      */
0887     virtio_device_ready(vdev);
0888 
0889     ret = virtio_fs_add_instance(fs);
0890     if (ret < 0)
0891         goto out_vqs;
0892 
0893     return 0;
0894 
0895 out_vqs:
0896     virtio_reset_device(vdev);
0897     virtio_fs_cleanup_vqs(vdev);
0898     kfree(fs->vqs);
0899 
0900 out:
0901     vdev->priv = NULL;
0902     kfree(fs);
0903     return ret;
0904 }
0905 
0906 static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
0907 {
0908     struct virtio_fs_vq *fsvq;
0909     int i;
0910 
0911     for (i = 0; i < fs->nvqs; i++) {
0912         fsvq = &fs->vqs[i];
0913         spin_lock(&fsvq->lock);
0914         fsvq->connected = false;
0915         spin_unlock(&fsvq->lock);
0916     }
0917 }
0918 
0919 static void virtio_fs_remove(struct virtio_device *vdev)
0920 {
0921     struct virtio_fs *fs = vdev->priv;
0922 
0923     mutex_lock(&virtio_fs_mutex);
0924     /* This device is going away. No one should get new reference */
0925     list_del_init(&fs->list);
0926     virtio_fs_stop_all_queues(fs);
0927     virtio_fs_drain_all_queues_locked(fs);
0928     virtio_reset_device(vdev);
0929     virtio_fs_cleanup_vqs(vdev);
0930 
0931     vdev->priv = NULL;
0932     /* Put device reference on virtio_fs object */
0933     virtio_fs_put(fs);
0934     mutex_unlock(&virtio_fs_mutex);
0935 }
0936 
0937 #ifdef CONFIG_PM_SLEEP
0938 static int virtio_fs_freeze(struct virtio_device *vdev)
0939 {
0940     /* TODO need to save state here */
0941     pr_warn("virtio-fs: suspend/resume not yet supported\n");
0942     return -EOPNOTSUPP;
0943 }
0944 
0945 static int virtio_fs_restore(struct virtio_device *vdev)
0946 {
0947      /* TODO need to restore state here */
0948     return 0;
0949 }
0950 #endif /* CONFIG_PM_SLEEP */
0951 
0952 static const struct virtio_device_id id_table[] = {
0953     { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
0954     {},
0955 };
0956 
0957 static const unsigned int feature_table[] = {};
0958 
0959 static struct virtio_driver virtio_fs_driver = {
0960     .driver.name        = KBUILD_MODNAME,
0961     .driver.owner       = THIS_MODULE,
0962     .id_table       = id_table,
0963     .feature_table      = feature_table,
0964     .feature_table_size = ARRAY_SIZE(feature_table),
0965     .probe          = virtio_fs_probe,
0966     .remove         = virtio_fs_remove,
0967 #ifdef CONFIG_PM_SLEEP
0968     .freeze         = virtio_fs_freeze,
0969     .restore        = virtio_fs_restore,
0970 #endif
0971 };
0972 
0973 static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq)
0974 __releases(fiq->lock)
0975 {
0976     struct fuse_forget_link *link;
0977     struct virtio_fs_forget *forget;
0978     struct virtio_fs_forget_req *req;
0979     struct virtio_fs *fs;
0980     struct virtio_fs_vq *fsvq;
0981     u64 unique;
0982 
0983     link = fuse_dequeue_forget(fiq, 1, NULL);
0984     unique = fuse_get_unique(fiq);
0985 
0986     fs = fiq->priv;
0987     fsvq = &fs->vqs[VQ_HIPRIO];
0988     spin_unlock(&fiq->lock);
0989 
0990     /* Allocate a buffer for the request */
0991     forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
0992     req = &forget->req;
0993 
0994     req->ih = (struct fuse_in_header){
0995         .opcode = FUSE_FORGET,
0996         .nodeid = link->forget_one.nodeid,
0997         .unique = unique,
0998         .len = sizeof(*req),
0999     };
1000     req->arg = (struct fuse_forget_in){
1001         .nlookup = link->forget_one.nlookup,
1002     };
1003 
1004     send_forget_request(fsvq, forget, false);
1005     kfree(link);
1006 }
1007 
1008 static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq)
1009 __releases(fiq->lock)
1010 {
1011     /*
1012      * TODO interrupts.
1013      *
1014      * Normal fs operations on a local filesystems aren't interruptible.
1015      * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
1016      * with shared lock between host and guest.
1017      */
1018     spin_unlock(&fiq->lock);
1019 }
1020 
1021 /* Count number of scatter-gather elements required */
1022 static unsigned int sg_count_fuse_pages(struct fuse_page_desc *page_descs,
1023                        unsigned int num_pages,
1024                        unsigned int total_len)
1025 {
1026     unsigned int i;
1027     unsigned int this_len;
1028 
1029     for (i = 0; i < num_pages && total_len; i++) {
1030         this_len =  min(page_descs[i].length, total_len);
1031         total_len -= this_len;
1032     }
1033 
1034     return i;
1035 }
1036 
1037 /* Return the number of scatter-gather list elements required */
1038 static unsigned int sg_count_fuse_req(struct fuse_req *req)
1039 {
1040     struct fuse_args *args = req->args;
1041     struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
1042     unsigned int size, total_sgs = 1 /* fuse_in_header */;
1043 
1044     if (args->in_numargs - args->in_pages)
1045         total_sgs += 1;
1046 
1047     if (args->in_pages) {
1048         size = args->in_args[args->in_numargs - 1].size;
1049         total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
1050                          size);
1051     }
1052 
1053     if (!test_bit(FR_ISREPLY, &req->flags))
1054         return total_sgs;
1055 
1056     total_sgs += 1 /* fuse_out_header */;
1057 
1058     if (args->out_numargs - args->out_pages)
1059         total_sgs += 1;
1060 
1061     if (args->out_pages) {
1062         size = args->out_args[args->out_numargs - 1].size;
1063         total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
1064                          size);
1065     }
1066 
1067     return total_sgs;
1068 }
1069 
1070 /* Add pages to scatter-gather list and return number of elements used */
1071 static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
1072                        struct page **pages,
1073                        struct fuse_page_desc *page_descs,
1074                        unsigned int num_pages,
1075                        unsigned int total_len)
1076 {
1077     unsigned int i;
1078     unsigned int this_len;
1079 
1080     for (i = 0; i < num_pages && total_len; i++) {
1081         sg_init_table(&sg[i], 1);
1082         this_len =  min(page_descs[i].length, total_len);
1083         sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
1084         total_len -= this_len;
1085     }
1086 
1087     return i;
1088 }
1089 
1090 /* Add args to scatter-gather list and return number of elements used */
1091 static unsigned int sg_init_fuse_args(struct scatterlist *sg,
1092                       struct fuse_req *req,
1093                       struct fuse_arg *args,
1094                       unsigned int numargs,
1095                       bool argpages,
1096                       void *argbuf,
1097                       unsigned int *len_used)
1098 {
1099     struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
1100     unsigned int total_sgs = 0;
1101     unsigned int len;
1102 
1103     len = fuse_len_args(numargs - argpages, args);
1104     if (len)
1105         sg_init_one(&sg[total_sgs++], argbuf, len);
1106 
1107     if (argpages)
1108         total_sgs += sg_init_fuse_pages(&sg[total_sgs],
1109                         ap->pages, ap->descs,
1110                         ap->num_pages,
1111                         args[numargs - 1].size);
1112 
1113     if (len_used)
1114         *len_used = len;
1115 
1116     return total_sgs;
1117 }
1118 
1119 /* Add a request to a virtqueue and kick the device */
1120 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
1121                  struct fuse_req *req, bool in_flight)
1122 {
1123     /* requests need at least 4 elements */
1124     struct scatterlist *stack_sgs[6];
1125     struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
1126     struct scatterlist **sgs = stack_sgs;
1127     struct scatterlist *sg = stack_sg;
1128     struct virtqueue *vq;
1129     struct fuse_args *args = req->args;
1130     unsigned int argbuf_used = 0;
1131     unsigned int out_sgs = 0;
1132     unsigned int in_sgs = 0;
1133     unsigned int total_sgs;
1134     unsigned int i;
1135     int ret;
1136     bool notify;
1137     struct fuse_pqueue *fpq;
1138 
1139     /* Does the sglist fit on the stack? */
1140     total_sgs = sg_count_fuse_req(req);
1141     if (total_sgs > ARRAY_SIZE(stack_sgs)) {
1142         sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
1143         sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
1144         if (!sgs || !sg) {
1145             ret = -ENOMEM;
1146             goto out;
1147         }
1148     }
1149 
1150     /* Use a bounce buffer since stack args cannot be mapped */
1151     ret = copy_args_to_argbuf(req);
1152     if (ret < 0)
1153         goto out;
1154 
1155     /* Request elements */
1156     sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
1157     out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
1158                      (struct fuse_arg *)args->in_args,
1159                      args->in_numargs, args->in_pages,
1160                      req->argbuf, &argbuf_used);
1161 
1162     /* Reply elements */
1163     if (test_bit(FR_ISREPLY, &req->flags)) {
1164         sg_init_one(&sg[out_sgs + in_sgs++],
1165                 &req->out.h, sizeof(req->out.h));
1166         in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
1167                         args->out_args, args->out_numargs,
1168                         args->out_pages,
1169                         req->argbuf + argbuf_used, NULL);
1170     }
1171 
1172     WARN_ON(out_sgs + in_sgs != total_sgs);
1173 
1174     for (i = 0; i < total_sgs; i++)
1175         sgs[i] = &sg[i];
1176 
1177     spin_lock(&fsvq->lock);
1178 
1179     if (!fsvq->connected) {
1180         spin_unlock(&fsvq->lock);
1181         ret = -ENOTCONN;
1182         goto out;
1183     }
1184 
1185     vq = fsvq->vq;
1186     ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
1187     if (ret < 0) {
1188         spin_unlock(&fsvq->lock);
1189         goto out;
1190     }
1191 
1192     /* Request successfully sent. */
1193     fpq = &fsvq->fud->pq;
1194     spin_lock(&fpq->lock);
1195     list_add_tail(&req->list, fpq->processing);
1196     spin_unlock(&fpq->lock);
1197     set_bit(FR_SENT, &req->flags);
1198     /* matches barrier in request_wait_answer() */
1199     smp_mb__after_atomic();
1200 
1201     if (!in_flight)
1202         inc_in_flight_req(fsvq);
1203     notify = virtqueue_kick_prepare(vq);
1204 
1205     spin_unlock(&fsvq->lock);
1206 
1207     if (notify)
1208         virtqueue_notify(vq);
1209 
1210 out:
1211     if (ret < 0 && req->argbuf) {
1212         kfree(req->argbuf);
1213         req->argbuf = NULL;
1214     }
1215     if (sgs != stack_sgs) {
1216         kfree(sgs);
1217         kfree(sg);
1218     }
1219 
1220     return ret;
1221 }
1222 
1223 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
1224 __releases(fiq->lock)
1225 {
1226     unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
1227     struct virtio_fs *fs;
1228     struct fuse_req *req;
1229     struct virtio_fs_vq *fsvq;
1230     int ret;
1231 
1232     WARN_ON(list_empty(&fiq->pending));
1233     req = list_last_entry(&fiq->pending, struct fuse_req, list);
1234     clear_bit(FR_PENDING, &req->flags);
1235     list_del_init(&req->list);
1236     WARN_ON(!list_empty(&fiq->pending));
1237     spin_unlock(&fiq->lock);
1238 
1239     fs = fiq->priv;
1240 
1241     pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
1242           __func__, req->in.h.opcode, req->in.h.unique,
1243          req->in.h.nodeid, req->in.h.len,
1244          fuse_len_args(req->args->out_numargs, req->args->out_args));
1245 
1246     fsvq = &fs->vqs[queue_id];
1247     ret = virtio_fs_enqueue_req(fsvq, req, false);
1248     if (ret < 0) {
1249         if (ret == -ENOMEM || ret == -ENOSPC) {
1250             /*
1251              * Virtqueue full. Retry submission from worker
1252              * context as we might be holding fc->bg_lock.
1253              */
1254             spin_lock(&fsvq->lock);
1255             list_add_tail(&req->list, &fsvq->queued_reqs);
1256             inc_in_flight_req(fsvq);
1257             schedule_delayed_work(&fsvq->dispatch_work,
1258                         msecs_to_jiffies(1));
1259             spin_unlock(&fsvq->lock);
1260             return;
1261         }
1262         req->out.h.error = ret;
1263         pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
1264 
1265         /* Can't end request in submission context. Use a worker */
1266         spin_lock(&fsvq->lock);
1267         list_add_tail(&req->list, &fsvq->end_reqs);
1268         schedule_delayed_work(&fsvq->dispatch_work, 0);
1269         spin_unlock(&fsvq->lock);
1270         return;
1271     }
1272 }
1273 
1274 static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
1275     .wake_forget_and_unlock     = virtio_fs_wake_forget_and_unlock,
1276     .wake_interrupt_and_unlock  = virtio_fs_wake_interrupt_and_unlock,
1277     .wake_pending_and_unlock    = virtio_fs_wake_pending_and_unlock,
1278     .release            = virtio_fs_fiq_release,
1279 };
1280 
1281 static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
1282 {
1283     ctx->rootmode = S_IFDIR;
1284     ctx->default_permissions = 1;
1285     ctx->allow_other = 1;
1286     ctx->max_read = UINT_MAX;
1287     ctx->blksize = 512;
1288     ctx->destroy = true;
1289     ctx->no_control = true;
1290     ctx->no_force_umount = true;
1291 }
1292 
1293 static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
1294 {
1295     struct fuse_mount *fm = get_fuse_mount_super(sb);
1296     struct fuse_conn *fc = fm->fc;
1297     struct virtio_fs *fs = fc->iq.priv;
1298     struct fuse_fs_context *ctx = fsc->fs_private;
1299     unsigned int i;
1300     int err;
1301 
1302     virtio_fs_ctx_set_defaults(ctx);
1303     mutex_lock(&virtio_fs_mutex);
1304 
1305     /* After holding mutex, make sure virtiofs device is still there.
1306      * Though we are holding a reference to it, drive ->remove might
1307      * still have cleaned up virtual queues. In that case bail out.
1308      */
1309     err = -EINVAL;
1310     if (list_empty(&fs->list)) {
1311         pr_info("virtio-fs: tag <%s> not found\n", fs->tag);
1312         goto err;
1313     }
1314 
1315     err = -ENOMEM;
1316     /* Allocate fuse_dev for hiprio and notification queues */
1317     for (i = 0; i < fs->nvqs; i++) {
1318         struct virtio_fs_vq *fsvq = &fs->vqs[i];
1319 
1320         fsvq->fud = fuse_dev_alloc();
1321         if (!fsvq->fud)
1322             goto err_free_fuse_devs;
1323     }
1324 
1325     /* virtiofs allocates and installs its own fuse devices */
1326     ctx->fudptr = NULL;
1327     if (ctx->dax_mode != FUSE_DAX_NEVER) {
1328         if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) {
1329             err = -EINVAL;
1330             pr_err("virtio-fs: dax can't be enabled as filesystem"
1331                    " device does not support it.\n");
1332             goto err_free_fuse_devs;
1333         }
1334         ctx->dax_dev = fs->dax_dev;
1335     }
1336     err = fuse_fill_super_common(sb, ctx);
1337     if (err < 0)
1338         goto err_free_fuse_devs;
1339 
1340     for (i = 0; i < fs->nvqs; i++) {
1341         struct virtio_fs_vq *fsvq = &fs->vqs[i];
1342 
1343         fuse_dev_install(fsvq->fud, fc);
1344     }
1345 
1346     /* Previous unmount will stop all queues. Start these again */
1347     virtio_fs_start_all_queues(fs);
1348     fuse_send_init(fm);
1349     mutex_unlock(&virtio_fs_mutex);
1350     return 0;
1351 
1352 err_free_fuse_devs:
1353     virtio_fs_free_devs(fs);
1354 err:
1355     mutex_unlock(&virtio_fs_mutex);
1356     return err;
1357 }
1358 
1359 static void virtio_fs_conn_destroy(struct fuse_mount *fm)
1360 {
1361     struct fuse_conn *fc = fm->fc;
1362     struct virtio_fs *vfs = fc->iq.priv;
1363     struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO];
1364 
1365     /* Stop dax worker. Soon evict_inodes() will be called which
1366      * will free all memory ranges belonging to all inodes.
1367      */
1368     if (IS_ENABLED(CONFIG_FUSE_DAX))
1369         fuse_dax_cancel_work(fc);
1370 
1371     /* Stop forget queue. Soon destroy will be sent */
1372     spin_lock(&fsvq->lock);
1373     fsvq->connected = false;
1374     spin_unlock(&fsvq->lock);
1375     virtio_fs_drain_all_queues(vfs);
1376 
1377     fuse_conn_destroy(fm);
1378 
1379     /* fuse_conn_destroy() must have sent destroy. Stop all queues
1380      * and drain one more time and free fuse devices. Freeing fuse
1381      * devices will drop their reference on fuse_conn and that in
1382      * turn will drop its reference on virtio_fs object.
1383      */
1384     virtio_fs_stop_all_queues(vfs);
1385     virtio_fs_drain_all_queues(vfs);
1386     virtio_fs_free_devs(vfs);
1387 }
1388 
1389 static void virtio_kill_sb(struct super_block *sb)
1390 {
1391     struct fuse_mount *fm = get_fuse_mount_super(sb);
1392     bool last;
1393 
1394     /* If mount failed, we can still be called without any fc */
1395     if (sb->s_root) {
1396         last = fuse_mount_remove(fm);
1397         if (last)
1398             virtio_fs_conn_destroy(fm);
1399     }
1400     kill_anon_super(sb);
1401     fuse_mount_destroy(fm);
1402 }
1403 
1404 static int virtio_fs_test_super(struct super_block *sb,
1405                 struct fs_context *fsc)
1406 {
1407     struct fuse_mount *fsc_fm = fsc->s_fs_info;
1408     struct fuse_mount *sb_fm = get_fuse_mount_super(sb);
1409 
1410     return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv;
1411 }
1412 
1413 static int virtio_fs_get_tree(struct fs_context *fsc)
1414 {
1415     struct virtio_fs *fs;
1416     struct super_block *sb;
1417     struct fuse_conn *fc = NULL;
1418     struct fuse_mount *fm;
1419     unsigned int virtqueue_size;
1420     int err = -EIO;
1421 
1422     /* This gets a reference on virtio_fs object. This ptr gets installed
1423      * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1424      * to drop the reference to this object.
1425      */
1426     fs = virtio_fs_find_instance(fsc->source);
1427     if (!fs) {
1428         pr_info("virtio-fs: tag <%s> not found\n", fsc->source);
1429         return -EINVAL;
1430     }
1431 
1432     virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq);
1433     if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD))
1434         goto out_err;
1435 
1436     err = -ENOMEM;
1437     fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
1438     if (!fc)
1439         goto out_err;
1440 
1441     fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1442     if (!fm)
1443         goto out_err;
1444 
1445     fuse_conn_init(fc, fm, fsc->user_ns, &virtio_fs_fiq_ops, fs);
1446     fc->release = fuse_free_conn;
1447     fc->delete_stale = true;
1448     fc->auto_submounts = true;
1449     fc->sync_fs = true;
1450 
1451     /* Tell FUSE to split requests that exceed the virtqueue's size */
1452     fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit,
1453                     virtqueue_size - FUSE_HEADER_OVERHEAD);
1454 
1455     fsc->s_fs_info = fm;
1456     sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc);
1457     if (fsc->s_fs_info)
1458         fuse_mount_destroy(fm);
1459     if (IS_ERR(sb))
1460         return PTR_ERR(sb);
1461 
1462     if (!sb->s_root) {
1463         err = virtio_fs_fill_super(sb, fsc);
1464         if (err) {
1465             deactivate_locked_super(sb);
1466             return err;
1467         }
1468 
1469         sb->s_flags |= SB_ACTIVE;
1470     }
1471 
1472     WARN_ON(fsc->root);
1473     fsc->root = dget(sb->s_root);
1474     return 0;
1475 
1476 out_err:
1477     kfree(fc);
1478     mutex_lock(&virtio_fs_mutex);
1479     virtio_fs_put(fs);
1480     mutex_unlock(&virtio_fs_mutex);
1481     return err;
1482 }
1483 
1484 static const struct fs_context_operations virtio_fs_context_ops = {
1485     .free       = virtio_fs_free_fsc,
1486     .parse_param    = virtio_fs_parse_param,
1487     .get_tree   = virtio_fs_get_tree,
1488 };
1489 
1490 static int virtio_fs_init_fs_context(struct fs_context *fsc)
1491 {
1492     struct fuse_fs_context *ctx;
1493 
1494     if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT)
1495         return fuse_init_fs_context_submount(fsc);
1496 
1497     ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1498     if (!ctx)
1499         return -ENOMEM;
1500     fsc->fs_private = ctx;
1501     fsc->ops = &virtio_fs_context_ops;
1502     return 0;
1503 }
1504 
1505 static struct file_system_type virtio_fs_type = {
1506     .owner      = THIS_MODULE,
1507     .name       = "virtiofs",
1508     .init_fs_context = virtio_fs_init_fs_context,
1509     .kill_sb    = virtio_kill_sb,
1510 };
1511 
1512 static int __init virtio_fs_init(void)
1513 {
1514     int ret;
1515 
1516     ret = register_virtio_driver(&virtio_fs_driver);
1517     if (ret < 0)
1518         return ret;
1519 
1520     ret = register_filesystem(&virtio_fs_type);
1521     if (ret < 0) {
1522         unregister_virtio_driver(&virtio_fs_driver);
1523         return ret;
1524     }
1525 
1526     return 0;
1527 }
1528 module_init(virtio_fs_init);
1529 
1530 static void __exit virtio_fs_exit(void)
1531 {
1532     unregister_filesystem(&virtio_fs_type);
1533     unregister_virtio_driver(&virtio_fs_driver);
1534 }
1535 module_exit(virtio_fs_exit);
1536 
1537 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1538 MODULE_DESCRIPTION("Virtio Filesystem");
1539 MODULE_LICENSE("GPL");
1540 MODULE_ALIAS_FS(KBUILD_MODNAME);
1541 MODULE_DEVICE_TABLE(virtio, id_table);