1a62a8ef9SStefan Hajnoczi // SPDX-License-Identifier: GPL-2.0 2a62a8ef9SStefan Hajnoczi /* 3a62a8ef9SStefan Hajnoczi * virtio-fs: Virtio Filesystem 4a62a8ef9SStefan Hajnoczi * Copyright (C) 2018 Red Hat, Inc. 5a62a8ef9SStefan Hajnoczi */ 6a62a8ef9SStefan Hajnoczi 7a62a8ef9SStefan Hajnoczi #include <linux/fs.h> 822f3787eSStefan Hajnoczi #include <linux/dax.h> 922f3787eSStefan Hajnoczi #include <linux/pci.h> 1022f3787eSStefan Hajnoczi #include <linux/pfn_t.h> 11a62a8ef9SStefan Hajnoczi #include <linux/module.h> 12a62a8ef9SStefan Hajnoczi #include <linux/virtio.h> 13a62a8ef9SStefan Hajnoczi #include <linux/virtio_fs.h> 14a62a8ef9SStefan Hajnoczi #include <linux/delay.h> 15a62a8ef9SStefan Hajnoczi #include <linux/fs_context.h> 161dd53957SVivek Goyal #include <linux/fs_parser.h> 17a62a8ef9SStefan Hajnoczi #include <linux/highmem.h> 1822f3787eSStefan Hajnoczi #include <linux/uio.h> 19a62a8ef9SStefan Hajnoczi #include "fuse_i.h" 20a62a8ef9SStefan Hajnoczi 21a7f0d7aaSConnor Kuehl /* Used to help calculate the FUSE connection's max_pages limit for a request's 22a7f0d7aaSConnor Kuehl * size. Parts of the struct fuse_req are sliced into scattergather lists in 23a7f0d7aaSConnor Kuehl * addition to the pages used, so this can help account for that overhead. 24a7f0d7aaSConnor Kuehl */ 25a7f0d7aaSConnor Kuehl #define FUSE_HEADER_OVERHEAD 4 26a7f0d7aaSConnor Kuehl 27a62a8ef9SStefan Hajnoczi /* List of virtio-fs device instances and a lock for the list. Also provides 28a62a8ef9SStefan Hajnoczi * mutual exclusion in device removal and mounting path 29a62a8ef9SStefan Hajnoczi */ 30a62a8ef9SStefan Hajnoczi static DEFINE_MUTEX(virtio_fs_mutex); 31a62a8ef9SStefan Hajnoczi static LIST_HEAD(virtio_fs_instances); 32a62a8ef9SStefan Hajnoczi 33a62a8ef9SStefan Hajnoczi enum { 34a62a8ef9SStefan Hajnoczi VQ_HIPRIO, 35a62a8ef9SStefan Hajnoczi VQ_REQUEST 36a62a8ef9SStefan Hajnoczi }; 37a62a8ef9SStefan Hajnoczi 38b43b7e81SVivek Goyal #define VQ_NAME_LEN 24 39b43b7e81SVivek Goyal 40a62a8ef9SStefan Hajnoczi /* Per-virtqueue state */ 41a62a8ef9SStefan Hajnoczi struct virtio_fs_vq { 42a62a8ef9SStefan Hajnoczi spinlock_t lock; 43a62a8ef9SStefan Hajnoczi struct virtqueue *vq; /* protected by ->lock */ 44a62a8ef9SStefan Hajnoczi struct work_struct done_work; 45a62a8ef9SStefan Hajnoczi struct list_head queued_reqs; 4651fecdd2SVivek Goyal struct list_head end_reqs; /* End these requests */ 47a62a8ef9SStefan Hajnoczi struct delayed_work dispatch_work; 48a62a8ef9SStefan Hajnoczi struct fuse_dev *fud; 49a62a8ef9SStefan Hajnoczi bool connected; 50a62a8ef9SStefan Hajnoczi long in_flight; 51724c15a4SVivek Goyal struct completion in_flight_zero; /* No inflight requests */ 52b43b7e81SVivek Goyal char name[VQ_NAME_LEN]; 53a62a8ef9SStefan Hajnoczi } ____cacheline_aligned_in_smp; 54a62a8ef9SStefan Hajnoczi 55a62a8ef9SStefan Hajnoczi /* A virtio-fs device instance */ 56a62a8ef9SStefan Hajnoczi struct virtio_fs { 57a62a8ef9SStefan Hajnoczi struct kref refcount; 58a62a8ef9SStefan Hajnoczi struct list_head list; /* on virtio_fs_instances */ 59a62a8ef9SStefan Hajnoczi char *tag; 60a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *vqs; 61a62a8ef9SStefan Hajnoczi unsigned int nvqs; /* number of virtqueues */ 62a62a8ef9SStefan Hajnoczi unsigned int num_request_queues; /* number of request queues */ 6322f3787eSStefan Hajnoczi struct dax_device *dax_dev; 6422f3787eSStefan Hajnoczi 6522f3787eSStefan Hajnoczi /* DAX memory window where file contents are mapped */ 6622f3787eSStefan Hajnoczi void *window_kaddr; 6722f3787eSStefan Hajnoczi phys_addr_t window_phys_addr; 6822f3787eSStefan Hajnoczi size_t window_len; 69a62a8ef9SStefan Hajnoczi }; 70a62a8ef9SStefan Hajnoczi 711efcf39eSVivek Goyal struct virtio_fs_forget_req { 72a62a8ef9SStefan Hajnoczi struct fuse_in_header ih; 73a62a8ef9SStefan Hajnoczi struct fuse_forget_in arg; 741efcf39eSVivek Goyal }; 751efcf39eSVivek Goyal 761efcf39eSVivek Goyal struct virtio_fs_forget { 77a62a8ef9SStefan Hajnoczi /* This request can be temporarily queued on virt queue */ 78a62a8ef9SStefan Hajnoczi struct list_head list; 791efcf39eSVivek Goyal struct virtio_fs_forget_req req; 80a62a8ef9SStefan Hajnoczi }; 81a62a8ef9SStefan Hajnoczi 82bb737bbeSVivek Goyal struct virtio_fs_req_work { 83bb737bbeSVivek Goyal struct fuse_req *req; 84bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq; 85bb737bbeSVivek Goyal struct work_struct done_work; 86bb737bbeSVivek Goyal }; 87bb737bbeSVivek Goyal 88a9bfd9ddSVivek Goyal static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, 89a9bfd9ddSVivek Goyal struct fuse_req *req, bool in_flight); 90a9bfd9ddSVivek Goyal 911dd53957SVivek Goyal enum { 921dd53957SVivek Goyal OPT_DAX, 931dd53957SVivek Goyal }; 941dd53957SVivek Goyal 951dd53957SVivek Goyal static const struct fs_parameter_spec virtio_fs_parameters[] = { 961dd53957SVivek Goyal fsparam_flag("dax", OPT_DAX), 971dd53957SVivek Goyal {} 981dd53957SVivek Goyal }; 991dd53957SVivek Goyal 100*84c21507SMiklos Szeredi static int virtio_fs_parse_param(struct fs_context *fsc, 1011dd53957SVivek Goyal struct fs_parameter *param) 1021dd53957SVivek Goyal { 1031dd53957SVivek Goyal struct fs_parse_result result; 104*84c21507SMiklos Szeredi struct fuse_fs_context *ctx = fsc->fs_private; 1051dd53957SVivek Goyal int opt; 1061dd53957SVivek Goyal 107*84c21507SMiklos Szeredi opt = fs_parse(fsc, virtio_fs_parameters, param, &result); 1081dd53957SVivek Goyal if (opt < 0) 1091dd53957SVivek Goyal return opt; 1101dd53957SVivek Goyal 1111dd53957SVivek Goyal switch (opt) { 1121dd53957SVivek Goyal case OPT_DAX: 1131dd53957SVivek Goyal ctx->dax = 1; 1141dd53957SVivek Goyal break; 1151dd53957SVivek Goyal default: 1161dd53957SVivek Goyal return -EINVAL; 1171dd53957SVivek Goyal } 1181dd53957SVivek Goyal 1191dd53957SVivek Goyal return 0; 1201dd53957SVivek Goyal } 1211dd53957SVivek Goyal 122*84c21507SMiklos Szeredi static void virtio_fs_free_fsc(struct fs_context *fsc) 1231dd53957SVivek Goyal { 124*84c21507SMiklos Szeredi struct fuse_fs_context *ctx = fsc->fs_private; 1251dd53957SVivek Goyal 1261dd53957SVivek Goyal kfree(ctx); 1271dd53957SVivek Goyal } 1281dd53957SVivek Goyal 129a62a8ef9SStefan Hajnoczi static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq) 130a62a8ef9SStefan Hajnoczi { 131a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = vq->vdev->priv; 132a62a8ef9SStefan Hajnoczi 133a62a8ef9SStefan Hajnoczi return &fs->vqs[vq->index]; 134a62a8ef9SStefan Hajnoczi } 135a62a8ef9SStefan Hajnoczi 136c17ea009SVivek Goyal /* Should be called with fsvq->lock held. */ 137c17ea009SVivek Goyal static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq) 138c17ea009SVivek Goyal { 139c17ea009SVivek Goyal fsvq->in_flight++; 140c17ea009SVivek Goyal } 141c17ea009SVivek Goyal 142c17ea009SVivek Goyal /* Should be called with fsvq->lock held. */ 143c17ea009SVivek Goyal static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq) 144c17ea009SVivek Goyal { 145c17ea009SVivek Goyal WARN_ON(fsvq->in_flight <= 0); 146c17ea009SVivek Goyal fsvq->in_flight--; 147724c15a4SVivek Goyal if (!fsvq->in_flight) 148724c15a4SVivek Goyal complete(&fsvq->in_flight_zero); 149c17ea009SVivek Goyal } 150c17ea009SVivek Goyal 151a62a8ef9SStefan Hajnoczi static void release_virtio_fs_obj(struct kref *ref) 152a62a8ef9SStefan Hajnoczi { 153a62a8ef9SStefan Hajnoczi struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount); 154a62a8ef9SStefan Hajnoczi 155a62a8ef9SStefan Hajnoczi kfree(vfs->vqs); 156a62a8ef9SStefan Hajnoczi kfree(vfs); 157a62a8ef9SStefan Hajnoczi } 158a62a8ef9SStefan Hajnoczi 159a62a8ef9SStefan Hajnoczi /* Make sure virtiofs_mutex is held */ 160a62a8ef9SStefan Hajnoczi static void virtio_fs_put(struct virtio_fs *fs) 161a62a8ef9SStefan Hajnoczi { 162a62a8ef9SStefan Hajnoczi kref_put(&fs->refcount, release_virtio_fs_obj); 163a62a8ef9SStefan Hajnoczi } 164a62a8ef9SStefan Hajnoczi 165a62a8ef9SStefan Hajnoczi static void virtio_fs_fiq_release(struct fuse_iqueue *fiq) 166a62a8ef9SStefan Hajnoczi { 167a62a8ef9SStefan Hajnoczi struct virtio_fs *vfs = fiq->priv; 168a62a8ef9SStefan Hajnoczi 169a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex); 170a62a8ef9SStefan Hajnoczi virtio_fs_put(vfs); 171a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 172a62a8ef9SStefan Hajnoczi } 173a62a8ef9SStefan Hajnoczi 174a62a8ef9SStefan Hajnoczi static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq) 175a62a8ef9SStefan Hajnoczi { 176a62a8ef9SStefan Hajnoczi WARN_ON(fsvq->in_flight < 0); 177a62a8ef9SStefan Hajnoczi 178a62a8ef9SStefan Hajnoczi /* Wait for in flight requests to finish.*/ 179a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 180724c15a4SVivek Goyal if (fsvq->in_flight) { 181724c15a4SVivek Goyal /* We are holding virtio_fs_mutex. There should not be any 182724c15a4SVivek Goyal * waiters waiting for completion. 183724c15a4SVivek Goyal */ 184724c15a4SVivek Goyal reinit_completion(&fsvq->in_flight_zero); 185a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 186724c15a4SVivek Goyal wait_for_completion(&fsvq->in_flight_zero); 187724c15a4SVivek Goyal } else { 188a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 189a62a8ef9SStefan Hajnoczi } 190a62a8ef9SStefan Hajnoczi 191a62a8ef9SStefan Hajnoczi flush_work(&fsvq->done_work); 192a62a8ef9SStefan Hajnoczi flush_delayed_work(&fsvq->dispatch_work); 193a62a8ef9SStefan Hajnoczi } 194a62a8ef9SStefan Hajnoczi 195724c15a4SVivek Goyal static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs) 196a62a8ef9SStefan Hajnoczi { 197a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq; 198a62a8ef9SStefan Hajnoczi int i; 199a62a8ef9SStefan Hajnoczi 200a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) { 201a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i]; 202a62a8ef9SStefan Hajnoczi virtio_fs_drain_queue(fsvq); 203a62a8ef9SStefan Hajnoczi } 204a62a8ef9SStefan Hajnoczi } 205a62a8ef9SStefan Hajnoczi 206724c15a4SVivek Goyal static void virtio_fs_drain_all_queues(struct virtio_fs *fs) 207724c15a4SVivek Goyal { 208724c15a4SVivek Goyal /* Provides mutual exclusion between ->remove and ->kill_sb 209724c15a4SVivek Goyal * paths. We don't want both of these draining queue at the 210724c15a4SVivek Goyal * same time. Current completion logic reinits completion 211724c15a4SVivek Goyal * and that means there should not be any other thread 212724c15a4SVivek Goyal * doing reinit or waiting for completion already. 213724c15a4SVivek Goyal */ 214724c15a4SVivek Goyal mutex_lock(&virtio_fs_mutex); 215724c15a4SVivek Goyal virtio_fs_drain_all_queues_locked(fs); 216724c15a4SVivek Goyal mutex_unlock(&virtio_fs_mutex); 217724c15a4SVivek Goyal } 218724c15a4SVivek Goyal 219a62a8ef9SStefan Hajnoczi static void virtio_fs_start_all_queues(struct virtio_fs *fs) 220a62a8ef9SStefan Hajnoczi { 221a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq; 222a62a8ef9SStefan Hajnoczi int i; 223a62a8ef9SStefan Hajnoczi 224a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) { 225a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i]; 226a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 227a62a8ef9SStefan Hajnoczi fsvq->connected = true; 228a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 229a62a8ef9SStefan Hajnoczi } 230a62a8ef9SStefan Hajnoczi } 231a62a8ef9SStefan Hajnoczi 232a62a8ef9SStefan Hajnoczi /* Add a new instance to the list or return -EEXIST if tag name exists*/ 233a62a8ef9SStefan Hajnoczi static int virtio_fs_add_instance(struct virtio_fs *fs) 234a62a8ef9SStefan Hajnoczi { 235a62a8ef9SStefan Hajnoczi struct virtio_fs *fs2; 236a62a8ef9SStefan Hajnoczi bool duplicate = false; 237a62a8ef9SStefan Hajnoczi 238a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex); 239a62a8ef9SStefan Hajnoczi 240a62a8ef9SStefan Hajnoczi list_for_each_entry(fs2, &virtio_fs_instances, list) { 241a62a8ef9SStefan Hajnoczi if (strcmp(fs->tag, fs2->tag) == 0) 242a62a8ef9SStefan Hajnoczi duplicate = true; 243a62a8ef9SStefan Hajnoczi } 244a62a8ef9SStefan Hajnoczi 245a62a8ef9SStefan Hajnoczi if (!duplicate) 246a62a8ef9SStefan Hajnoczi list_add_tail(&fs->list, &virtio_fs_instances); 247a62a8ef9SStefan Hajnoczi 248a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 249a62a8ef9SStefan Hajnoczi 250a62a8ef9SStefan Hajnoczi if (duplicate) 251a62a8ef9SStefan Hajnoczi return -EEXIST; 252a62a8ef9SStefan Hajnoczi return 0; 253a62a8ef9SStefan Hajnoczi } 254a62a8ef9SStefan Hajnoczi 255a62a8ef9SStefan Hajnoczi /* Return the virtio_fs with a given tag, or NULL */ 256a62a8ef9SStefan Hajnoczi static struct virtio_fs *virtio_fs_find_instance(const char *tag) 257a62a8ef9SStefan Hajnoczi { 258a62a8ef9SStefan Hajnoczi struct virtio_fs *fs; 259a62a8ef9SStefan Hajnoczi 260a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex); 261a62a8ef9SStefan Hajnoczi 262a62a8ef9SStefan Hajnoczi list_for_each_entry(fs, &virtio_fs_instances, list) { 263a62a8ef9SStefan Hajnoczi if (strcmp(fs->tag, tag) == 0) { 264a62a8ef9SStefan Hajnoczi kref_get(&fs->refcount); 265a62a8ef9SStefan Hajnoczi goto found; 266a62a8ef9SStefan Hajnoczi } 267a62a8ef9SStefan Hajnoczi } 268a62a8ef9SStefan Hajnoczi 269a62a8ef9SStefan Hajnoczi fs = NULL; /* not found */ 270a62a8ef9SStefan Hajnoczi 271a62a8ef9SStefan Hajnoczi found: 272a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 273a62a8ef9SStefan Hajnoczi 274a62a8ef9SStefan Hajnoczi return fs; 275a62a8ef9SStefan Hajnoczi } 276a62a8ef9SStefan Hajnoczi 277a62a8ef9SStefan Hajnoczi static void virtio_fs_free_devs(struct virtio_fs *fs) 278a62a8ef9SStefan Hajnoczi { 279a62a8ef9SStefan Hajnoczi unsigned int i; 280a62a8ef9SStefan Hajnoczi 281a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) { 282a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i]; 283a62a8ef9SStefan Hajnoczi 284a62a8ef9SStefan Hajnoczi if (!fsvq->fud) 285a62a8ef9SStefan Hajnoczi continue; 286a62a8ef9SStefan Hajnoczi 287a62a8ef9SStefan Hajnoczi fuse_dev_free(fsvq->fud); 288a62a8ef9SStefan Hajnoczi fsvq->fud = NULL; 289a62a8ef9SStefan Hajnoczi } 290a62a8ef9SStefan Hajnoczi } 291a62a8ef9SStefan Hajnoczi 292a62a8ef9SStefan Hajnoczi /* Read filesystem name from virtio config into fs->tag (must kfree()). */ 293a62a8ef9SStefan Hajnoczi static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) 294a62a8ef9SStefan Hajnoczi { 295a62a8ef9SStefan Hajnoczi char tag_buf[sizeof_field(struct virtio_fs_config, tag)]; 296a62a8ef9SStefan Hajnoczi char *end; 297a62a8ef9SStefan Hajnoczi size_t len; 298a62a8ef9SStefan Hajnoczi 299a62a8ef9SStefan Hajnoczi virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag), 300a62a8ef9SStefan Hajnoczi &tag_buf, sizeof(tag_buf)); 301a62a8ef9SStefan Hajnoczi end = memchr(tag_buf, '\0', sizeof(tag_buf)); 302a62a8ef9SStefan Hajnoczi if (end == tag_buf) 303a62a8ef9SStefan Hajnoczi return -EINVAL; /* empty tag */ 304a62a8ef9SStefan Hajnoczi if (!end) 305a62a8ef9SStefan Hajnoczi end = &tag_buf[sizeof(tag_buf)]; 306a62a8ef9SStefan Hajnoczi 307a62a8ef9SStefan Hajnoczi len = end - tag_buf; 308a62a8ef9SStefan Hajnoczi fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL); 309a62a8ef9SStefan Hajnoczi if (!fs->tag) 310a62a8ef9SStefan Hajnoczi return -ENOMEM; 311a62a8ef9SStefan Hajnoczi memcpy(fs->tag, tag_buf, len); 312a62a8ef9SStefan Hajnoczi fs->tag[len] = '\0'; 313a62a8ef9SStefan Hajnoczi return 0; 314a62a8ef9SStefan Hajnoczi } 315a62a8ef9SStefan Hajnoczi 316a62a8ef9SStefan Hajnoczi /* Work function for hiprio completion */ 317a62a8ef9SStefan Hajnoczi static void virtio_fs_hiprio_done_work(struct work_struct *work) 318a62a8ef9SStefan Hajnoczi { 319a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 320a62a8ef9SStefan Hajnoczi done_work); 321a62a8ef9SStefan Hajnoczi struct virtqueue *vq = fsvq->vq; 322a62a8ef9SStefan Hajnoczi 323a62a8ef9SStefan Hajnoczi /* Free completed FUSE_FORGET requests */ 324a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 325a62a8ef9SStefan Hajnoczi do { 326a62a8ef9SStefan Hajnoczi unsigned int len; 327a62a8ef9SStefan Hajnoczi void *req; 328a62a8ef9SStefan Hajnoczi 329a62a8ef9SStefan Hajnoczi virtqueue_disable_cb(vq); 330a62a8ef9SStefan Hajnoczi 331a62a8ef9SStefan Hajnoczi while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 332a62a8ef9SStefan Hajnoczi kfree(req); 333c17ea009SVivek Goyal dec_in_flight_req(fsvq); 334a62a8ef9SStefan Hajnoczi } 335a62a8ef9SStefan Hajnoczi } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq))); 336a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 337a62a8ef9SStefan Hajnoczi } 338a62a8ef9SStefan Hajnoczi 33951fecdd2SVivek Goyal static void virtio_fs_request_dispatch_work(struct work_struct *work) 340a62a8ef9SStefan Hajnoczi { 34151fecdd2SVivek Goyal struct fuse_req *req; 34251fecdd2SVivek Goyal struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 34351fecdd2SVivek Goyal dispatch_work.work); 344a9bfd9ddSVivek Goyal int ret; 34551fecdd2SVivek Goyal 34651fecdd2SVivek Goyal pr_debug("virtio-fs: worker %s called.\n", __func__); 34751fecdd2SVivek Goyal while (1) { 34851fecdd2SVivek Goyal spin_lock(&fsvq->lock); 34951fecdd2SVivek Goyal req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req, 35051fecdd2SVivek Goyal list); 35151fecdd2SVivek Goyal if (!req) { 35251fecdd2SVivek Goyal spin_unlock(&fsvq->lock); 353a9bfd9ddSVivek Goyal break; 35451fecdd2SVivek Goyal } 35551fecdd2SVivek Goyal 35651fecdd2SVivek Goyal list_del_init(&req->list); 35751fecdd2SVivek Goyal spin_unlock(&fsvq->lock); 3588f622e94SMax Reitz fuse_request_end(req); 35951fecdd2SVivek Goyal } 360a9bfd9ddSVivek Goyal 361a9bfd9ddSVivek Goyal /* Dispatch pending requests */ 362a9bfd9ddSVivek Goyal while (1) { 363a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock); 364a9bfd9ddSVivek Goyal req = list_first_entry_or_null(&fsvq->queued_reqs, 365a9bfd9ddSVivek Goyal struct fuse_req, list); 366a9bfd9ddSVivek Goyal if (!req) { 367a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock); 368a9bfd9ddSVivek Goyal return; 369a9bfd9ddSVivek Goyal } 370a9bfd9ddSVivek Goyal list_del_init(&req->list); 371a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock); 372a9bfd9ddSVivek Goyal 373a9bfd9ddSVivek Goyal ret = virtio_fs_enqueue_req(fsvq, req, true); 374a9bfd9ddSVivek Goyal if (ret < 0) { 375a9bfd9ddSVivek Goyal if (ret == -ENOMEM || ret == -ENOSPC) { 376a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock); 377a9bfd9ddSVivek Goyal list_add_tail(&req->list, &fsvq->queued_reqs); 378a9bfd9ddSVivek Goyal schedule_delayed_work(&fsvq->dispatch_work, 379a9bfd9ddSVivek Goyal msecs_to_jiffies(1)); 380a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock); 381a9bfd9ddSVivek Goyal return; 382a9bfd9ddSVivek Goyal } 383a9bfd9ddSVivek Goyal req->out.h.error = ret; 384a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock); 385a9bfd9ddSVivek Goyal dec_in_flight_req(fsvq); 386a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock); 387a9bfd9ddSVivek Goyal pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", 388a9bfd9ddSVivek Goyal ret); 3898f622e94SMax Reitz fuse_request_end(req); 390a9bfd9ddSVivek Goyal } 391a9bfd9ddSVivek Goyal } 392a62a8ef9SStefan Hajnoczi } 393a62a8ef9SStefan Hajnoczi 39458ada94fSVivek Goyal /* 39558ada94fSVivek Goyal * Returns 1 if queue is full and sender should wait a bit before sending 39658ada94fSVivek Goyal * next request, 0 otherwise. 39758ada94fSVivek Goyal */ 39858ada94fSVivek Goyal static int send_forget_request(struct virtio_fs_vq *fsvq, 39958ada94fSVivek Goyal struct virtio_fs_forget *forget, 40058ada94fSVivek Goyal bool in_flight) 40158ada94fSVivek Goyal { 40258ada94fSVivek Goyal struct scatterlist sg; 40358ada94fSVivek Goyal struct virtqueue *vq; 40458ada94fSVivek Goyal int ret = 0; 40558ada94fSVivek Goyal bool notify; 4061efcf39eSVivek Goyal struct virtio_fs_forget_req *req = &forget->req; 40758ada94fSVivek Goyal 40858ada94fSVivek Goyal spin_lock(&fsvq->lock); 40958ada94fSVivek Goyal if (!fsvq->connected) { 41058ada94fSVivek Goyal if (in_flight) 41158ada94fSVivek Goyal dec_in_flight_req(fsvq); 41258ada94fSVivek Goyal kfree(forget); 41358ada94fSVivek Goyal goto out; 41458ada94fSVivek Goyal } 41558ada94fSVivek Goyal 4161efcf39eSVivek Goyal sg_init_one(&sg, req, sizeof(*req)); 41758ada94fSVivek Goyal vq = fsvq->vq; 41858ada94fSVivek Goyal dev_dbg(&vq->vdev->dev, "%s\n", __func__); 41958ada94fSVivek Goyal 42058ada94fSVivek Goyal ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC); 42158ada94fSVivek Goyal if (ret < 0) { 42258ada94fSVivek Goyal if (ret == -ENOMEM || ret == -ENOSPC) { 42358ada94fSVivek Goyal pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n", 42458ada94fSVivek Goyal ret); 42558ada94fSVivek Goyal list_add_tail(&forget->list, &fsvq->queued_reqs); 42658ada94fSVivek Goyal schedule_delayed_work(&fsvq->dispatch_work, 42758ada94fSVivek Goyal msecs_to_jiffies(1)); 42858ada94fSVivek Goyal if (!in_flight) 42958ada94fSVivek Goyal inc_in_flight_req(fsvq); 43058ada94fSVivek Goyal /* Queue is full */ 43158ada94fSVivek Goyal ret = 1; 43258ada94fSVivek Goyal } else { 43358ada94fSVivek Goyal pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n", 43458ada94fSVivek Goyal ret); 43558ada94fSVivek Goyal kfree(forget); 43658ada94fSVivek Goyal if (in_flight) 43758ada94fSVivek Goyal dec_in_flight_req(fsvq); 43858ada94fSVivek Goyal } 43958ada94fSVivek Goyal goto out; 44058ada94fSVivek Goyal } 44158ada94fSVivek Goyal 44258ada94fSVivek Goyal if (!in_flight) 44358ada94fSVivek Goyal inc_in_flight_req(fsvq); 44458ada94fSVivek Goyal notify = virtqueue_kick_prepare(vq); 44558ada94fSVivek Goyal spin_unlock(&fsvq->lock); 44658ada94fSVivek Goyal 44758ada94fSVivek Goyal if (notify) 44858ada94fSVivek Goyal virtqueue_notify(vq); 44958ada94fSVivek Goyal return ret; 45058ada94fSVivek Goyal out: 45158ada94fSVivek Goyal spin_unlock(&fsvq->lock); 45258ada94fSVivek Goyal return ret; 45358ada94fSVivek Goyal } 45458ada94fSVivek Goyal 455a62a8ef9SStefan Hajnoczi static void virtio_fs_hiprio_dispatch_work(struct work_struct *work) 456a62a8ef9SStefan Hajnoczi { 457a62a8ef9SStefan Hajnoczi struct virtio_fs_forget *forget; 458a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 459a62a8ef9SStefan Hajnoczi dispatch_work.work); 460a62a8ef9SStefan Hajnoczi pr_debug("virtio-fs: worker %s called.\n", __func__); 461a62a8ef9SStefan Hajnoczi while (1) { 462a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 463a62a8ef9SStefan Hajnoczi forget = list_first_entry_or_null(&fsvq->queued_reqs, 464a62a8ef9SStefan Hajnoczi struct virtio_fs_forget, list); 465a62a8ef9SStefan Hajnoczi if (!forget) { 466a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 467a62a8ef9SStefan Hajnoczi return; 468a62a8ef9SStefan Hajnoczi } 469a62a8ef9SStefan Hajnoczi 470a62a8ef9SStefan Hajnoczi list_del(&forget->list); 471a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 47258ada94fSVivek Goyal if (send_forget_request(fsvq, forget, true)) 473a62a8ef9SStefan Hajnoczi return; 474a62a8ef9SStefan Hajnoczi } 475a62a8ef9SStefan Hajnoczi } 476a62a8ef9SStefan Hajnoczi 477a62a8ef9SStefan Hajnoczi /* Allocate and copy args into req->argbuf */ 478a62a8ef9SStefan Hajnoczi static int copy_args_to_argbuf(struct fuse_req *req) 479a62a8ef9SStefan Hajnoczi { 480a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args; 481a62a8ef9SStefan Hajnoczi unsigned int offset = 0; 482a62a8ef9SStefan Hajnoczi unsigned int num_in; 483a62a8ef9SStefan Hajnoczi unsigned int num_out; 484a62a8ef9SStefan Hajnoczi unsigned int len; 485a62a8ef9SStefan Hajnoczi unsigned int i; 486a62a8ef9SStefan Hajnoczi 487a62a8ef9SStefan Hajnoczi num_in = args->in_numargs - args->in_pages; 488a62a8ef9SStefan Hajnoczi num_out = args->out_numargs - args->out_pages; 489a62a8ef9SStefan Hajnoczi len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) + 490a62a8ef9SStefan Hajnoczi fuse_len_args(num_out, args->out_args); 491a62a8ef9SStefan Hajnoczi 492a62a8ef9SStefan Hajnoczi req->argbuf = kmalloc(len, GFP_ATOMIC); 493a62a8ef9SStefan Hajnoczi if (!req->argbuf) 494a62a8ef9SStefan Hajnoczi return -ENOMEM; 495a62a8ef9SStefan Hajnoczi 496a62a8ef9SStefan Hajnoczi for (i = 0; i < num_in; i++) { 497a62a8ef9SStefan Hajnoczi memcpy(req->argbuf + offset, 498a62a8ef9SStefan Hajnoczi args->in_args[i].value, 499a62a8ef9SStefan Hajnoczi args->in_args[i].size); 500a62a8ef9SStefan Hajnoczi offset += args->in_args[i].size; 501a62a8ef9SStefan Hajnoczi } 502a62a8ef9SStefan Hajnoczi 503a62a8ef9SStefan Hajnoczi return 0; 504a62a8ef9SStefan Hajnoczi } 505a62a8ef9SStefan Hajnoczi 506a62a8ef9SStefan Hajnoczi /* Copy args out of and free req->argbuf */ 507a62a8ef9SStefan Hajnoczi static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req) 508a62a8ef9SStefan Hajnoczi { 509a62a8ef9SStefan Hajnoczi unsigned int remaining; 510a62a8ef9SStefan Hajnoczi unsigned int offset; 511a62a8ef9SStefan Hajnoczi unsigned int num_in; 512a62a8ef9SStefan Hajnoczi unsigned int num_out; 513a62a8ef9SStefan Hajnoczi unsigned int i; 514a62a8ef9SStefan Hajnoczi 515a62a8ef9SStefan Hajnoczi remaining = req->out.h.len - sizeof(req->out.h); 516a62a8ef9SStefan Hajnoczi num_in = args->in_numargs - args->in_pages; 517a62a8ef9SStefan Hajnoczi num_out = args->out_numargs - args->out_pages; 518a62a8ef9SStefan Hajnoczi offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args); 519a62a8ef9SStefan Hajnoczi 520a62a8ef9SStefan Hajnoczi for (i = 0; i < num_out; i++) { 521a62a8ef9SStefan Hajnoczi unsigned int argsize = args->out_args[i].size; 522a62a8ef9SStefan Hajnoczi 523a62a8ef9SStefan Hajnoczi if (args->out_argvar && 524a62a8ef9SStefan Hajnoczi i == args->out_numargs - 1 && 525a62a8ef9SStefan Hajnoczi argsize > remaining) { 526a62a8ef9SStefan Hajnoczi argsize = remaining; 527a62a8ef9SStefan Hajnoczi } 528a62a8ef9SStefan Hajnoczi 529a62a8ef9SStefan Hajnoczi memcpy(args->out_args[i].value, req->argbuf + offset, argsize); 530a62a8ef9SStefan Hajnoczi offset += argsize; 531a62a8ef9SStefan Hajnoczi 532a62a8ef9SStefan Hajnoczi if (i != args->out_numargs - 1) 533a62a8ef9SStefan Hajnoczi remaining -= argsize; 534a62a8ef9SStefan Hajnoczi } 535a62a8ef9SStefan Hajnoczi 536a62a8ef9SStefan Hajnoczi /* Store the actual size of the variable-length arg */ 537a62a8ef9SStefan Hajnoczi if (args->out_argvar) 538a62a8ef9SStefan Hajnoczi args->out_args[args->out_numargs - 1].size = remaining; 539a62a8ef9SStefan Hajnoczi 540a62a8ef9SStefan Hajnoczi kfree(req->argbuf); 541a62a8ef9SStefan Hajnoczi req->argbuf = NULL; 542a62a8ef9SStefan Hajnoczi } 543a62a8ef9SStefan Hajnoczi 544a62a8ef9SStefan Hajnoczi /* Work function for request completion */ 545bb737bbeSVivek Goyal static void virtio_fs_request_complete(struct fuse_req *req, 546bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq) 547a62a8ef9SStefan Hajnoczi { 548a62a8ef9SStefan Hajnoczi struct fuse_pqueue *fpq = &fsvq->fud->pq; 549a62a8ef9SStefan Hajnoczi struct fuse_args *args; 550bb737bbeSVivek Goyal struct fuse_args_pages *ap; 551a62a8ef9SStefan Hajnoczi unsigned int len, i, thislen; 552a62a8ef9SStefan Hajnoczi struct page *page; 553a62a8ef9SStefan Hajnoczi 554a62a8ef9SStefan Hajnoczi /* 555a62a8ef9SStefan Hajnoczi * TODO verify that server properly follows FUSE protocol 556a62a8ef9SStefan Hajnoczi * (oh.uniq, oh.len) 557a62a8ef9SStefan Hajnoczi */ 558a62a8ef9SStefan Hajnoczi args = req->args; 559a62a8ef9SStefan Hajnoczi copy_args_from_argbuf(args, req); 560a62a8ef9SStefan Hajnoczi 561a62a8ef9SStefan Hajnoczi if (args->out_pages && args->page_zeroing) { 562a62a8ef9SStefan Hajnoczi len = args->out_args[args->out_numargs - 1].size; 563a62a8ef9SStefan Hajnoczi ap = container_of(args, typeof(*ap), args); 564a62a8ef9SStefan Hajnoczi for (i = 0; i < ap->num_pages; i++) { 565a62a8ef9SStefan Hajnoczi thislen = ap->descs[i].length; 566a62a8ef9SStefan Hajnoczi if (len < thislen) { 567a62a8ef9SStefan Hajnoczi WARN_ON(ap->descs[i].offset); 568a62a8ef9SStefan Hajnoczi page = ap->pages[i]; 569a62a8ef9SStefan Hajnoczi zero_user_segment(page, len, thislen); 570a62a8ef9SStefan Hajnoczi len = 0; 571a62a8ef9SStefan Hajnoczi } else { 572a62a8ef9SStefan Hajnoczi len -= thislen; 573a62a8ef9SStefan Hajnoczi } 574a62a8ef9SStefan Hajnoczi } 575a62a8ef9SStefan Hajnoczi } 576a62a8ef9SStefan Hajnoczi 577a62a8ef9SStefan Hajnoczi spin_lock(&fpq->lock); 578a62a8ef9SStefan Hajnoczi clear_bit(FR_SENT, &req->flags); 579a62a8ef9SStefan Hajnoczi spin_unlock(&fpq->lock); 580a62a8ef9SStefan Hajnoczi 5818f622e94SMax Reitz fuse_request_end(req); 582a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 583c17ea009SVivek Goyal dec_in_flight_req(fsvq); 584a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 585a62a8ef9SStefan Hajnoczi } 586bb737bbeSVivek Goyal 587bb737bbeSVivek Goyal static void virtio_fs_complete_req_work(struct work_struct *work) 588bb737bbeSVivek Goyal { 589bb737bbeSVivek Goyal struct virtio_fs_req_work *w = 590bb737bbeSVivek Goyal container_of(work, typeof(*w), done_work); 591bb737bbeSVivek Goyal 592bb737bbeSVivek Goyal virtio_fs_request_complete(w->req, w->fsvq); 593bb737bbeSVivek Goyal kfree(w); 594bb737bbeSVivek Goyal } 595bb737bbeSVivek Goyal 596bb737bbeSVivek Goyal static void virtio_fs_requests_done_work(struct work_struct *work) 597bb737bbeSVivek Goyal { 598bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 599bb737bbeSVivek Goyal done_work); 600bb737bbeSVivek Goyal struct fuse_pqueue *fpq = &fsvq->fud->pq; 601bb737bbeSVivek Goyal struct virtqueue *vq = fsvq->vq; 602bb737bbeSVivek Goyal struct fuse_req *req; 603bb737bbeSVivek Goyal struct fuse_req *next; 604bb737bbeSVivek Goyal unsigned int len; 605bb737bbeSVivek Goyal LIST_HEAD(reqs); 606bb737bbeSVivek Goyal 607bb737bbeSVivek Goyal /* Collect completed requests off the virtqueue */ 608bb737bbeSVivek Goyal spin_lock(&fsvq->lock); 609bb737bbeSVivek Goyal do { 610bb737bbeSVivek Goyal virtqueue_disable_cb(vq); 611bb737bbeSVivek Goyal 612bb737bbeSVivek Goyal while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 613bb737bbeSVivek Goyal spin_lock(&fpq->lock); 614bb737bbeSVivek Goyal list_move_tail(&req->list, &reqs); 615bb737bbeSVivek Goyal spin_unlock(&fpq->lock); 616bb737bbeSVivek Goyal } 617bb737bbeSVivek Goyal } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq))); 618bb737bbeSVivek Goyal spin_unlock(&fsvq->lock); 619bb737bbeSVivek Goyal 620bb737bbeSVivek Goyal /* End requests */ 621bb737bbeSVivek Goyal list_for_each_entry_safe(req, next, &reqs, list) { 622bb737bbeSVivek Goyal list_del_init(&req->list); 623bb737bbeSVivek Goyal 624bb737bbeSVivek Goyal /* blocking async request completes in a worker context */ 625bb737bbeSVivek Goyal if (req->args->may_block) { 626bb737bbeSVivek Goyal struct virtio_fs_req_work *w; 627bb737bbeSVivek Goyal 628bb737bbeSVivek Goyal w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL); 629bb737bbeSVivek Goyal INIT_WORK(&w->done_work, virtio_fs_complete_req_work); 630bb737bbeSVivek Goyal w->fsvq = fsvq; 631bb737bbeSVivek Goyal w->req = req; 632bb737bbeSVivek Goyal schedule_work(&w->done_work); 633bb737bbeSVivek Goyal } else { 634bb737bbeSVivek Goyal virtio_fs_request_complete(req, fsvq); 635bb737bbeSVivek Goyal } 636bb737bbeSVivek Goyal } 637a62a8ef9SStefan Hajnoczi } 638a62a8ef9SStefan Hajnoczi 639a62a8ef9SStefan Hajnoczi /* Virtqueue interrupt handler */ 640a62a8ef9SStefan Hajnoczi static void virtio_fs_vq_done(struct virtqueue *vq) 641a62a8ef9SStefan Hajnoczi { 642a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = vq_to_fsvq(vq); 643a62a8ef9SStefan Hajnoczi 644a62a8ef9SStefan Hajnoczi dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name); 645a62a8ef9SStefan Hajnoczi 646a62a8ef9SStefan Hajnoczi schedule_work(&fsvq->done_work); 647a62a8ef9SStefan Hajnoczi } 648a62a8ef9SStefan Hajnoczi 649b43b7e81SVivek Goyal static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name, 650b43b7e81SVivek Goyal int vq_type) 651b43b7e81SVivek Goyal { 652b43b7e81SVivek Goyal strncpy(fsvq->name, name, VQ_NAME_LEN); 653b43b7e81SVivek Goyal spin_lock_init(&fsvq->lock); 654b43b7e81SVivek Goyal INIT_LIST_HEAD(&fsvq->queued_reqs); 655b43b7e81SVivek Goyal INIT_LIST_HEAD(&fsvq->end_reqs); 656b43b7e81SVivek Goyal init_completion(&fsvq->in_flight_zero); 657b43b7e81SVivek Goyal 658b43b7e81SVivek Goyal if (vq_type == VQ_REQUEST) { 659b43b7e81SVivek Goyal INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work); 660b43b7e81SVivek Goyal INIT_DELAYED_WORK(&fsvq->dispatch_work, 661b43b7e81SVivek Goyal virtio_fs_request_dispatch_work); 662b43b7e81SVivek Goyal } else { 663b43b7e81SVivek Goyal INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work); 664b43b7e81SVivek Goyal INIT_DELAYED_WORK(&fsvq->dispatch_work, 665b43b7e81SVivek Goyal virtio_fs_hiprio_dispatch_work); 666b43b7e81SVivek Goyal } 667b43b7e81SVivek Goyal } 668b43b7e81SVivek Goyal 669a62a8ef9SStefan Hajnoczi /* Initialize virtqueues */ 670a62a8ef9SStefan Hajnoczi static int virtio_fs_setup_vqs(struct virtio_device *vdev, 671a62a8ef9SStefan Hajnoczi struct virtio_fs *fs) 672a62a8ef9SStefan Hajnoczi { 673a62a8ef9SStefan Hajnoczi struct virtqueue **vqs; 674a62a8ef9SStefan Hajnoczi vq_callback_t **callbacks; 675a62a8ef9SStefan Hajnoczi const char **names; 676a62a8ef9SStefan Hajnoczi unsigned int i; 677a62a8ef9SStefan Hajnoczi int ret = 0; 678a62a8ef9SStefan Hajnoczi 6792c0349ecSMichael S. Tsirkin virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues, 680a62a8ef9SStefan Hajnoczi &fs->num_request_queues); 681a62a8ef9SStefan Hajnoczi if (fs->num_request_queues == 0) 682a62a8ef9SStefan Hajnoczi return -EINVAL; 683a62a8ef9SStefan Hajnoczi 684b43b7e81SVivek Goyal fs->nvqs = VQ_REQUEST + fs->num_request_queues; 685a62a8ef9SStefan Hajnoczi fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL); 686a62a8ef9SStefan Hajnoczi if (!fs->vqs) 687a62a8ef9SStefan Hajnoczi return -ENOMEM; 688a62a8ef9SStefan Hajnoczi 689a62a8ef9SStefan Hajnoczi vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL); 690a62a8ef9SStefan Hajnoczi callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]), 691a62a8ef9SStefan Hajnoczi GFP_KERNEL); 692a62a8ef9SStefan Hajnoczi names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL); 693a62a8ef9SStefan Hajnoczi if (!vqs || !callbacks || !names) { 694a62a8ef9SStefan Hajnoczi ret = -ENOMEM; 695a62a8ef9SStefan Hajnoczi goto out; 696a62a8ef9SStefan Hajnoczi } 697a62a8ef9SStefan Hajnoczi 698b43b7e81SVivek Goyal /* Initialize the hiprio/forget request virtqueue */ 699a62a8ef9SStefan Hajnoczi callbacks[VQ_HIPRIO] = virtio_fs_vq_done; 700b43b7e81SVivek Goyal virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO); 701a62a8ef9SStefan Hajnoczi names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name; 702a62a8ef9SStefan Hajnoczi 703a62a8ef9SStefan Hajnoczi /* Initialize the requests virtqueues */ 704a62a8ef9SStefan Hajnoczi for (i = VQ_REQUEST; i < fs->nvqs; i++) { 705b43b7e81SVivek Goyal char vq_name[VQ_NAME_LEN]; 706b43b7e81SVivek Goyal 707b43b7e81SVivek Goyal snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST); 708b43b7e81SVivek Goyal virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST); 709a62a8ef9SStefan Hajnoczi callbacks[i] = virtio_fs_vq_done; 710a62a8ef9SStefan Hajnoczi names[i] = fs->vqs[i].name; 711a62a8ef9SStefan Hajnoczi } 712a62a8ef9SStefan Hajnoczi 713a62a8ef9SStefan Hajnoczi ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL); 714a62a8ef9SStefan Hajnoczi if (ret < 0) 715a62a8ef9SStefan Hajnoczi goto out; 716a62a8ef9SStefan Hajnoczi 717a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) 718a62a8ef9SStefan Hajnoczi fs->vqs[i].vq = vqs[i]; 719a62a8ef9SStefan Hajnoczi 720a62a8ef9SStefan Hajnoczi virtio_fs_start_all_queues(fs); 721a62a8ef9SStefan Hajnoczi out: 722a62a8ef9SStefan Hajnoczi kfree(names); 723a62a8ef9SStefan Hajnoczi kfree(callbacks); 724a62a8ef9SStefan Hajnoczi kfree(vqs); 725a62a8ef9SStefan Hajnoczi if (ret) 726a62a8ef9SStefan Hajnoczi kfree(fs->vqs); 727a62a8ef9SStefan Hajnoczi return ret; 728a62a8ef9SStefan Hajnoczi } 729a62a8ef9SStefan Hajnoczi 730a62a8ef9SStefan Hajnoczi /* Free virtqueues (device must already be reset) */ 731a62a8ef9SStefan Hajnoczi static void virtio_fs_cleanup_vqs(struct virtio_device *vdev, 732a62a8ef9SStefan Hajnoczi struct virtio_fs *fs) 733a62a8ef9SStefan Hajnoczi { 734a62a8ef9SStefan Hajnoczi vdev->config->del_vqs(vdev); 735a62a8ef9SStefan Hajnoczi } 736a62a8ef9SStefan Hajnoczi 73722f3787eSStefan Hajnoczi /* Map a window offset to a page frame number. The window offset will have 73822f3787eSStefan Hajnoczi * been produced by .iomap_begin(), which maps a file offset to a window 73922f3787eSStefan Hajnoczi * offset. 74022f3787eSStefan Hajnoczi */ 74122f3787eSStefan Hajnoczi static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 74222f3787eSStefan Hajnoczi long nr_pages, void **kaddr, pfn_t *pfn) 74322f3787eSStefan Hajnoczi { 74422f3787eSStefan Hajnoczi struct virtio_fs *fs = dax_get_private(dax_dev); 74522f3787eSStefan Hajnoczi phys_addr_t offset = PFN_PHYS(pgoff); 74622f3787eSStefan Hajnoczi size_t max_nr_pages = fs->window_len/PAGE_SIZE - pgoff; 74722f3787eSStefan Hajnoczi 74822f3787eSStefan Hajnoczi if (kaddr) 74922f3787eSStefan Hajnoczi *kaddr = fs->window_kaddr + offset; 75022f3787eSStefan Hajnoczi if (pfn) 75122f3787eSStefan Hajnoczi *pfn = phys_to_pfn_t(fs->window_phys_addr + offset, 75222f3787eSStefan Hajnoczi PFN_DEV | PFN_MAP); 75322f3787eSStefan Hajnoczi return nr_pages > max_nr_pages ? max_nr_pages : nr_pages; 75422f3787eSStefan Hajnoczi } 75522f3787eSStefan Hajnoczi 75622f3787eSStefan Hajnoczi static size_t virtio_fs_copy_from_iter(struct dax_device *dax_dev, 75722f3787eSStefan Hajnoczi pgoff_t pgoff, void *addr, 75822f3787eSStefan Hajnoczi size_t bytes, struct iov_iter *i) 75922f3787eSStefan Hajnoczi { 76022f3787eSStefan Hajnoczi return copy_from_iter(addr, bytes, i); 76122f3787eSStefan Hajnoczi } 76222f3787eSStefan Hajnoczi 76322f3787eSStefan Hajnoczi static size_t virtio_fs_copy_to_iter(struct dax_device *dax_dev, 76422f3787eSStefan Hajnoczi pgoff_t pgoff, void *addr, 76522f3787eSStefan Hajnoczi size_t bytes, struct iov_iter *i) 76622f3787eSStefan Hajnoczi { 76722f3787eSStefan Hajnoczi return copy_to_iter(addr, bytes, i); 76822f3787eSStefan Hajnoczi } 76922f3787eSStefan Hajnoczi 77022f3787eSStefan Hajnoczi static int virtio_fs_zero_page_range(struct dax_device *dax_dev, 77122f3787eSStefan Hajnoczi pgoff_t pgoff, size_t nr_pages) 77222f3787eSStefan Hajnoczi { 77322f3787eSStefan Hajnoczi long rc; 77422f3787eSStefan Hajnoczi void *kaddr; 77522f3787eSStefan Hajnoczi 77622f3787eSStefan Hajnoczi rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL); 77722f3787eSStefan Hajnoczi if (rc < 0) 77822f3787eSStefan Hajnoczi return rc; 77922f3787eSStefan Hajnoczi memset(kaddr, 0, nr_pages << PAGE_SHIFT); 78022f3787eSStefan Hajnoczi dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT); 78122f3787eSStefan Hajnoczi return 0; 78222f3787eSStefan Hajnoczi } 78322f3787eSStefan Hajnoczi 78422f3787eSStefan Hajnoczi static const struct dax_operations virtio_fs_dax_ops = { 78522f3787eSStefan Hajnoczi .direct_access = virtio_fs_direct_access, 78622f3787eSStefan Hajnoczi .copy_from_iter = virtio_fs_copy_from_iter, 78722f3787eSStefan Hajnoczi .copy_to_iter = virtio_fs_copy_to_iter, 78822f3787eSStefan Hajnoczi .zero_page_range = virtio_fs_zero_page_range, 78922f3787eSStefan Hajnoczi }; 79022f3787eSStefan Hajnoczi 79122f3787eSStefan Hajnoczi static void virtio_fs_cleanup_dax(void *data) 79222f3787eSStefan Hajnoczi { 79322f3787eSStefan Hajnoczi struct dax_device *dax_dev = data; 79422f3787eSStefan Hajnoczi 79522f3787eSStefan Hajnoczi kill_dax(dax_dev); 79622f3787eSStefan Hajnoczi put_dax(dax_dev); 79722f3787eSStefan Hajnoczi } 79822f3787eSStefan Hajnoczi 79922f3787eSStefan Hajnoczi static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs) 80022f3787eSStefan Hajnoczi { 80122f3787eSStefan Hajnoczi struct virtio_shm_region cache_reg; 80222f3787eSStefan Hajnoczi struct dev_pagemap *pgmap; 80322f3787eSStefan Hajnoczi bool have_cache; 80422f3787eSStefan Hajnoczi 80522f3787eSStefan Hajnoczi if (!IS_ENABLED(CONFIG_FUSE_DAX)) 80622f3787eSStefan Hajnoczi return 0; 80722f3787eSStefan Hajnoczi 80822f3787eSStefan Hajnoczi /* Get cache region */ 80922f3787eSStefan Hajnoczi have_cache = virtio_get_shm_region(vdev, &cache_reg, 81022f3787eSStefan Hajnoczi (u8)VIRTIO_FS_SHMCAP_ID_CACHE); 81122f3787eSStefan Hajnoczi if (!have_cache) { 81222f3787eSStefan Hajnoczi dev_notice(&vdev->dev, "%s: No cache capability\n", __func__); 81322f3787eSStefan Hajnoczi return 0; 81422f3787eSStefan Hajnoczi } 81522f3787eSStefan Hajnoczi 81622f3787eSStefan Hajnoczi if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len, 81722f3787eSStefan Hajnoczi dev_name(&vdev->dev))) { 81822f3787eSStefan Hajnoczi dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n", 81922f3787eSStefan Hajnoczi cache_reg.addr, cache_reg.len); 82022f3787eSStefan Hajnoczi return -EBUSY; 82122f3787eSStefan Hajnoczi } 82222f3787eSStefan Hajnoczi 82322f3787eSStefan Hajnoczi dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len, 82422f3787eSStefan Hajnoczi cache_reg.addr); 82522f3787eSStefan Hajnoczi 82622f3787eSStefan Hajnoczi pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL); 82722f3787eSStefan Hajnoczi if (!pgmap) 82822f3787eSStefan Hajnoczi return -ENOMEM; 82922f3787eSStefan Hajnoczi 83022f3787eSStefan Hajnoczi pgmap->type = MEMORY_DEVICE_FS_DAX; 83122f3787eSStefan Hajnoczi 83222f3787eSStefan Hajnoczi /* Ideally we would directly use the PCI BAR resource but 83322f3787eSStefan Hajnoczi * devm_memremap_pages() wants its own copy in pgmap. So 83422f3787eSStefan Hajnoczi * initialize a struct resource from scratch (only the start 83522f3787eSStefan Hajnoczi * and end fields will be used). 83622f3787eSStefan Hajnoczi */ 83769456535SLinus Torvalds pgmap->range = (struct range) { 83822f3787eSStefan Hajnoczi .start = (phys_addr_t) cache_reg.addr, 83922f3787eSStefan Hajnoczi .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1, 84022f3787eSStefan Hajnoczi }; 84169456535SLinus Torvalds pgmap->nr_range = 1; 84222f3787eSStefan Hajnoczi 84322f3787eSStefan Hajnoczi fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap); 84422f3787eSStefan Hajnoczi if (IS_ERR(fs->window_kaddr)) 84522f3787eSStefan Hajnoczi return PTR_ERR(fs->window_kaddr); 84622f3787eSStefan Hajnoczi 84722f3787eSStefan Hajnoczi fs->window_phys_addr = (phys_addr_t) cache_reg.addr; 84822f3787eSStefan Hajnoczi fs->window_len = (phys_addr_t) cache_reg.len; 84922f3787eSStefan Hajnoczi 85022f3787eSStefan Hajnoczi dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n", 85122f3787eSStefan Hajnoczi __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len); 85222f3787eSStefan Hajnoczi 85322f3787eSStefan Hajnoczi fs->dax_dev = alloc_dax(fs, NULL, &virtio_fs_dax_ops, 0); 85422f3787eSStefan Hajnoczi if (IS_ERR(fs->dax_dev)) 85522f3787eSStefan Hajnoczi return PTR_ERR(fs->dax_dev); 85622f3787eSStefan Hajnoczi 85722f3787eSStefan Hajnoczi return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax, 85822f3787eSStefan Hajnoczi fs->dax_dev); 85922f3787eSStefan Hajnoczi } 86022f3787eSStefan Hajnoczi 861a62a8ef9SStefan Hajnoczi static int virtio_fs_probe(struct virtio_device *vdev) 862a62a8ef9SStefan Hajnoczi { 863a62a8ef9SStefan Hajnoczi struct virtio_fs *fs; 864a62a8ef9SStefan Hajnoczi int ret; 865a62a8ef9SStefan Hajnoczi 866a62a8ef9SStefan Hajnoczi fs = kzalloc(sizeof(*fs), GFP_KERNEL); 867a62a8ef9SStefan Hajnoczi if (!fs) 868a62a8ef9SStefan Hajnoczi return -ENOMEM; 869a62a8ef9SStefan Hajnoczi kref_init(&fs->refcount); 870a62a8ef9SStefan Hajnoczi vdev->priv = fs; 871a62a8ef9SStefan Hajnoczi 872a62a8ef9SStefan Hajnoczi ret = virtio_fs_read_tag(vdev, fs); 873a62a8ef9SStefan Hajnoczi if (ret < 0) 874a62a8ef9SStefan Hajnoczi goto out; 875a62a8ef9SStefan Hajnoczi 876a62a8ef9SStefan Hajnoczi ret = virtio_fs_setup_vqs(vdev, fs); 877a62a8ef9SStefan Hajnoczi if (ret < 0) 878a62a8ef9SStefan Hajnoczi goto out; 879a62a8ef9SStefan Hajnoczi 880a62a8ef9SStefan Hajnoczi /* TODO vq affinity */ 881a62a8ef9SStefan Hajnoczi 88222f3787eSStefan Hajnoczi ret = virtio_fs_setup_dax(vdev, fs); 88322f3787eSStefan Hajnoczi if (ret < 0) 88422f3787eSStefan Hajnoczi goto out_vqs; 88522f3787eSStefan Hajnoczi 886a62a8ef9SStefan Hajnoczi /* Bring the device online in case the filesystem is mounted and 887a62a8ef9SStefan Hajnoczi * requests need to be sent before we return. 888a62a8ef9SStefan Hajnoczi */ 889a62a8ef9SStefan Hajnoczi virtio_device_ready(vdev); 890a62a8ef9SStefan Hajnoczi 891a62a8ef9SStefan Hajnoczi ret = virtio_fs_add_instance(fs); 892a62a8ef9SStefan Hajnoczi if (ret < 0) 893a62a8ef9SStefan Hajnoczi goto out_vqs; 894a62a8ef9SStefan Hajnoczi 895a62a8ef9SStefan Hajnoczi return 0; 896a62a8ef9SStefan Hajnoczi 897a62a8ef9SStefan Hajnoczi out_vqs: 898a62a8ef9SStefan Hajnoczi vdev->config->reset(vdev); 899a62a8ef9SStefan Hajnoczi virtio_fs_cleanup_vqs(vdev, fs); 900c79c5e01SLuis Henriques kfree(fs->vqs); 901a62a8ef9SStefan Hajnoczi 902a62a8ef9SStefan Hajnoczi out: 903a62a8ef9SStefan Hajnoczi vdev->priv = NULL; 904a62a8ef9SStefan Hajnoczi kfree(fs); 905a62a8ef9SStefan Hajnoczi return ret; 906a62a8ef9SStefan Hajnoczi } 907a62a8ef9SStefan Hajnoczi 908a62a8ef9SStefan Hajnoczi static void virtio_fs_stop_all_queues(struct virtio_fs *fs) 909a62a8ef9SStefan Hajnoczi { 910a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq; 911a62a8ef9SStefan Hajnoczi int i; 912a62a8ef9SStefan Hajnoczi 913a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) { 914a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i]; 915a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 916a62a8ef9SStefan Hajnoczi fsvq->connected = false; 917a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 918a62a8ef9SStefan Hajnoczi } 919a62a8ef9SStefan Hajnoczi } 920a62a8ef9SStefan Hajnoczi 921a62a8ef9SStefan Hajnoczi static void virtio_fs_remove(struct virtio_device *vdev) 922a62a8ef9SStefan Hajnoczi { 923a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = vdev->priv; 924a62a8ef9SStefan Hajnoczi 925a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex); 926a62a8ef9SStefan Hajnoczi /* This device is going away. No one should get new reference */ 927a62a8ef9SStefan Hajnoczi list_del_init(&fs->list); 928a62a8ef9SStefan Hajnoczi virtio_fs_stop_all_queues(fs); 929724c15a4SVivek Goyal virtio_fs_drain_all_queues_locked(fs); 930a62a8ef9SStefan Hajnoczi vdev->config->reset(vdev); 931a62a8ef9SStefan Hajnoczi virtio_fs_cleanup_vqs(vdev, fs); 932a62a8ef9SStefan Hajnoczi 933a62a8ef9SStefan Hajnoczi vdev->priv = NULL; 934a62a8ef9SStefan Hajnoczi /* Put device reference on virtio_fs object */ 935a62a8ef9SStefan Hajnoczi virtio_fs_put(fs); 936a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 937a62a8ef9SStefan Hajnoczi } 938a62a8ef9SStefan Hajnoczi 939a62a8ef9SStefan Hajnoczi #ifdef CONFIG_PM_SLEEP 940a62a8ef9SStefan Hajnoczi static int virtio_fs_freeze(struct virtio_device *vdev) 941a62a8ef9SStefan Hajnoczi { 942a62a8ef9SStefan Hajnoczi /* TODO need to save state here */ 943a62a8ef9SStefan Hajnoczi pr_warn("virtio-fs: suspend/resume not yet supported\n"); 944a62a8ef9SStefan Hajnoczi return -EOPNOTSUPP; 945a62a8ef9SStefan Hajnoczi } 946a62a8ef9SStefan Hajnoczi 947a62a8ef9SStefan Hajnoczi static int virtio_fs_restore(struct virtio_device *vdev) 948a62a8ef9SStefan Hajnoczi { 949a62a8ef9SStefan Hajnoczi /* TODO need to restore state here */ 950a62a8ef9SStefan Hajnoczi return 0; 951a62a8ef9SStefan Hajnoczi } 952a62a8ef9SStefan Hajnoczi #endif /* CONFIG_PM_SLEEP */ 953a62a8ef9SStefan Hajnoczi 95400929447SYueHaibing static const struct virtio_device_id id_table[] = { 955a62a8ef9SStefan Hajnoczi { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID }, 956a62a8ef9SStefan Hajnoczi {}, 957a62a8ef9SStefan Hajnoczi }; 958a62a8ef9SStefan Hajnoczi 95900929447SYueHaibing static const unsigned int feature_table[] = {}; 960a62a8ef9SStefan Hajnoczi 961a62a8ef9SStefan Hajnoczi static struct virtio_driver virtio_fs_driver = { 962a62a8ef9SStefan Hajnoczi .driver.name = KBUILD_MODNAME, 963a62a8ef9SStefan Hajnoczi .driver.owner = THIS_MODULE, 964a62a8ef9SStefan Hajnoczi .id_table = id_table, 965a62a8ef9SStefan Hajnoczi .feature_table = feature_table, 966a62a8ef9SStefan Hajnoczi .feature_table_size = ARRAY_SIZE(feature_table), 967a62a8ef9SStefan Hajnoczi .probe = virtio_fs_probe, 968a62a8ef9SStefan Hajnoczi .remove = virtio_fs_remove, 969a62a8ef9SStefan Hajnoczi #ifdef CONFIG_PM_SLEEP 970a62a8ef9SStefan Hajnoczi .freeze = virtio_fs_freeze, 971a62a8ef9SStefan Hajnoczi .restore = virtio_fs_restore, 972a62a8ef9SStefan Hajnoczi #endif 973a62a8ef9SStefan Hajnoczi }; 974a62a8ef9SStefan Hajnoczi 975a62a8ef9SStefan Hajnoczi static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq) 976a62a8ef9SStefan Hajnoczi __releases(fiq->lock) 977a62a8ef9SStefan Hajnoczi { 978a62a8ef9SStefan Hajnoczi struct fuse_forget_link *link; 979a62a8ef9SStefan Hajnoczi struct virtio_fs_forget *forget; 9801efcf39eSVivek Goyal struct virtio_fs_forget_req *req; 981a62a8ef9SStefan Hajnoczi struct virtio_fs *fs; 982a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq; 983a62a8ef9SStefan Hajnoczi u64 unique; 984a62a8ef9SStefan Hajnoczi 985a62a8ef9SStefan Hajnoczi link = fuse_dequeue_forget(fiq, 1, NULL); 986a62a8ef9SStefan Hajnoczi unique = fuse_get_unique(fiq); 987a62a8ef9SStefan Hajnoczi 988a62a8ef9SStefan Hajnoczi fs = fiq->priv; 989a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[VQ_HIPRIO]; 990a62a8ef9SStefan Hajnoczi spin_unlock(&fiq->lock); 991a62a8ef9SStefan Hajnoczi 992a62a8ef9SStefan Hajnoczi /* Allocate a buffer for the request */ 993a62a8ef9SStefan Hajnoczi forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL); 9941efcf39eSVivek Goyal req = &forget->req; 995a62a8ef9SStefan Hajnoczi 9961efcf39eSVivek Goyal req->ih = (struct fuse_in_header){ 997a62a8ef9SStefan Hajnoczi .opcode = FUSE_FORGET, 998a62a8ef9SStefan Hajnoczi .nodeid = link->forget_one.nodeid, 999a62a8ef9SStefan Hajnoczi .unique = unique, 10001efcf39eSVivek Goyal .len = sizeof(*req), 1001a62a8ef9SStefan Hajnoczi }; 10021efcf39eSVivek Goyal req->arg = (struct fuse_forget_in){ 1003a62a8ef9SStefan Hajnoczi .nlookup = link->forget_one.nlookup, 1004a62a8ef9SStefan Hajnoczi }; 1005a62a8ef9SStefan Hajnoczi 100658ada94fSVivek Goyal send_forget_request(fsvq, forget, false); 1007a62a8ef9SStefan Hajnoczi kfree(link); 1008a62a8ef9SStefan Hajnoczi } 1009a62a8ef9SStefan Hajnoczi 1010a62a8ef9SStefan Hajnoczi static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq) 1011a62a8ef9SStefan Hajnoczi __releases(fiq->lock) 1012a62a8ef9SStefan Hajnoczi { 1013a62a8ef9SStefan Hajnoczi /* 1014a62a8ef9SStefan Hajnoczi * TODO interrupts. 1015a62a8ef9SStefan Hajnoczi * 1016a62a8ef9SStefan Hajnoczi * Normal fs operations on a local filesystems aren't interruptible. 1017a62a8ef9SStefan Hajnoczi * Exceptions are blocking lock operations; for example fcntl(F_SETLKW) 1018a62a8ef9SStefan Hajnoczi * with shared lock between host and guest. 1019a62a8ef9SStefan Hajnoczi */ 1020a62a8ef9SStefan Hajnoczi spin_unlock(&fiq->lock); 1021a62a8ef9SStefan Hajnoczi } 1022a62a8ef9SStefan Hajnoczi 102342d3e2d0SVivek Goyal /* Count number of scatter-gather elements required */ 102442d3e2d0SVivek Goyal static unsigned int sg_count_fuse_pages(struct fuse_page_desc *page_descs, 102542d3e2d0SVivek Goyal unsigned int num_pages, 102642d3e2d0SVivek Goyal unsigned int total_len) 102742d3e2d0SVivek Goyal { 102842d3e2d0SVivek Goyal unsigned int i; 102942d3e2d0SVivek Goyal unsigned int this_len; 103042d3e2d0SVivek Goyal 103142d3e2d0SVivek Goyal for (i = 0; i < num_pages && total_len; i++) { 103242d3e2d0SVivek Goyal this_len = min(page_descs[i].length, total_len); 103342d3e2d0SVivek Goyal total_len -= this_len; 103442d3e2d0SVivek Goyal } 103542d3e2d0SVivek Goyal 103642d3e2d0SVivek Goyal return i; 103742d3e2d0SVivek Goyal } 103842d3e2d0SVivek Goyal 1039a62a8ef9SStefan Hajnoczi /* Return the number of scatter-gather list elements required */ 1040a62a8ef9SStefan Hajnoczi static unsigned int sg_count_fuse_req(struct fuse_req *req) 1041a62a8ef9SStefan Hajnoczi { 1042a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args; 1043a62a8ef9SStefan Hajnoczi struct fuse_args_pages *ap = container_of(args, typeof(*ap), args); 104442d3e2d0SVivek Goyal unsigned int size, total_sgs = 1 /* fuse_in_header */; 1045a62a8ef9SStefan Hajnoczi 1046a62a8ef9SStefan Hajnoczi if (args->in_numargs - args->in_pages) 1047a62a8ef9SStefan Hajnoczi total_sgs += 1; 1048a62a8ef9SStefan Hajnoczi 104942d3e2d0SVivek Goyal if (args->in_pages) { 105042d3e2d0SVivek Goyal size = args->in_args[args->in_numargs - 1].size; 105142d3e2d0SVivek Goyal total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages, 105242d3e2d0SVivek Goyal size); 105342d3e2d0SVivek Goyal } 1054a62a8ef9SStefan Hajnoczi 1055a62a8ef9SStefan Hajnoczi if (!test_bit(FR_ISREPLY, &req->flags)) 1056a62a8ef9SStefan Hajnoczi return total_sgs; 1057a62a8ef9SStefan Hajnoczi 1058a62a8ef9SStefan Hajnoczi total_sgs += 1 /* fuse_out_header */; 1059a62a8ef9SStefan Hajnoczi 1060a62a8ef9SStefan Hajnoczi if (args->out_numargs - args->out_pages) 1061a62a8ef9SStefan Hajnoczi total_sgs += 1; 1062a62a8ef9SStefan Hajnoczi 106342d3e2d0SVivek Goyal if (args->out_pages) { 106442d3e2d0SVivek Goyal size = args->out_args[args->out_numargs - 1].size; 106542d3e2d0SVivek Goyal total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages, 106642d3e2d0SVivek Goyal size); 106742d3e2d0SVivek Goyal } 1068a62a8ef9SStefan Hajnoczi 1069a62a8ef9SStefan Hajnoczi return total_sgs; 1070a62a8ef9SStefan Hajnoczi } 1071a62a8ef9SStefan Hajnoczi 1072a62a8ef9SStefan Hajnoczi /* Add pages to scatter-gather list and return number of elements used */ 1073a62a8ef9SStefan Hajnoczi static unsigned int sg_init_fuse_pages(struct scatterlist *sg, 1074a62a8ef9SStefan Hajnoczi struct page **pages, 1075a62a8ef9SStefan Hajnoczi struct fuse_page_desc *page_descs, 1076a62a8ef9SStefan Hajnoczi unsigned int num_pages, 1077a62a8ef9SStefan Hajnoczi unsigned int total_len) 1078a62a8ef9SStefan Hajnoczi { 1079a62a8ef9SStefan Hajnoczi unsigned int i; 1080a62a8ef9SStefan Hajnoczi unsigned int this_len; 1081a62a8ef9SStefan Hajnoczi 1082a62a8ef9SStefan Hajnoczi for (i = 0; i < num_pages && total_len; i++) { 1083a62a8ef9SStefan Hajnoczi sg_init_table(&sg[i], 1); 1084a62a8ef9SStefan Hajnoczi this_len = min(page_descs[i].length, total_len); 1085a62a8ef9SStefan Hajnoczi sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset); 1086a62a8ef9SStefan Hajnoczi total_len -= this_len; 1087a62a8ef9SStefan Hajnoczi } 1088a62a8ef9SStefan Hajnoczi 1089a62a8ef9SStefan Hajnoczi return i; 1090a62a8ef9SStefan Hajnoczi } 1091a62a8ef9SStefan Hajnoczi 1092a62a8ef9SStefan Hajnoczi /* Add args to scatter-gather list and return number of elements used */ 1093a62a8ef9SStefan Hajnoczi static unsigned int sg_init_fuse_args(struct scatterlist *sg, 1094a62a8ef9SStefan Hajnoczi struct fuse_req *req, 1095a62a8ef9SStefan Hajnoczi struct fuse_arg *args, 1096a62a8ef9SStefan Hajnoczi unsigned int numargs, 1097a62a8ef9SStefan Hajnoczi bool argpages, 1098a62a8ef9SStefan Hajnoczi void *argbuf, 1099a62a8ef9SStefan Hajnoczi unsigned int *len_used) 1100a62a8ef9SStefan Hajnoczi { 1101a62a8ef9SStefan Hajnoczi struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); 1102a62a8ef9SStefan Hajnoczi unsigned int total_sgs = 0; 1103a62a8ef9SStefan Hajnoczi unsigned int len; 1104a62a8ef9SStefan Hajnoczi 1105a62a8ef9SStefan Hajnoczi len = fuse_len_args(numargs - argpages, args); 1106a62a8ef9SStefan Hajnoczi if (len) 1107a62a8ef9SStefan Hajnoczi sg_init_one(&sg[total_sgs++], argbuf, len); 1108a62a8ef9SStefan Hajnoczi 1109a62a8ef9SStefan Hajnoczi if (argpages) 1110a62a8ef9SStefan Hajnoczi total_sgs += sg_init_fuse_pages(&sg[total_sgs], 1111a62a8ef9SStefan Hajnoczi ap->pages, ap->descs, 1112a62a8ef9SStefan Hajnoczi ap->num_pages, 1113a62a8ef9SStefan Hajnoczi args[numargs - 1].size); 1114a62a8ef9SStefan Hajnoczi 1115a62a8ef9SStefan Hajnoczi if (len_used) 1116a62a8ef9SStefan Hajnoczi *len_used = len; 1117a62a8ef9SStefan Hajnoczi 1118a62a8ef9SStefan Hajnoczi return total_sgs; 1119a62a8ef9SStefan Hajnoczi } 1120a62a8ef9SStefan Hajnoczi 1121a62a8ef9SStefan Hajnoczi /* Add a request to a virtqueue and kick the device */ 1122a62a8ef9SStefan Hajnoczi static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, 1123a9bfd9ddSVivek Goyal struct fuse_req *req, bool in_flight) 1124a62a8ef9SStefan Hajnoczi { 1125a62a8ef9SStefan Hajnoczi /* requests need at least 4 elements */ 1126a62a8ef9SStefan Hajnoczi struct scatterlist *stack_sgs[6]; 1127a62a8ef9SStefan Hajnoczi struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)]; 1128a62a8ef9SStefan Hajnoczi struct scatterlist **sgs = stack_sgs; 1129a62a8ef9SStefan Hajnoczi struct scatterlist *sg = stack_sg; 1130a62a8ef9SStefan Hajnoczi struct virtqueue *vq; 1131a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args; 1132a62a8ef9SStefan Hajnoczi unsigned int argbuf_used = 0; 1133a62a8ef9SStefan Hajnoczi unsigned int out_sgs = 0; 1134a62a8ef9SStefan Hajnoczi unsigned int in_sgs = 0; 1135a62a8ef9SStefan Hajnoczi unsigned int total_sgs; 1136a62a8ef9SStefan Hajnoczi unsigned int i; 1137a62a8ef9SStefan Hajnoczi int ret; 1138a62a8ef9SStefan Hajnoczi bool notify; 11395dbe190fSVivek Goyal struct fuse_pqueue *fpq; 1140a62a8ef9SStefan Hajnoczi 1141a62a8ef9SStefan Hajnoczi /* Does the sglist fit on the stack? */ 1142a62a8ef9SStefan Hajnoczi total_sgs = sg_count_fuse_req(req); 1143a62a8ef9SStefan Hajnoczi if (total_sgs > ARRAY_SIZE(stack_sgs)) { 1144a62a8ef9SStefan Hajnoczi sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC); 1145a62a8ef9SStefan Hajnoczi sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC); 1146a62a8ef9SStefan Hajnoczi if (!sgs || !sg) { 1147a62a8ef9SStefan Hajnoczi ret = -ENOMEM; 1148a62a8ef9SStefan Hajnoczi goto out; 1149a62a8ef9SStefan Hajnoczi } 1150a62a8ef9SStefan Hajnoczi } 1151a62a8ef9SStefan Hajnoczi 1152a62a8ef9SStefan Hajnoczi /* Use a bounce buffer since stack args cannot be mapped */ 1153a62a8ef9SStefan Hajnoczi ret = copy_args_to_argbuf(req); 1154a62a8ef9SStefan Hajnoczi if (ret < 0) 1155a62a8ef9SStefan Hajnoczi goto out; 1156a62a8ef9SStefan Hajnoczi 1157a62a8ef9SStefan Hajnoczi /* Request elements */ 1158a62a8ef9SStefan Hajnoczi sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h)); 1159a62a8ef9SStefan Hajnoczi out_sgs += sg_init_fuse_args(&sg[out_sgs], req, 1160a62a8ef9SStefan Hajnoczi (struct fuse_arg *)args->in_args, 1161a62a8ef9SStefan Hajnoczi args->in_numargs, args->in_pages, 1162a62a8ef9SStefan Hajnoczi req->argbuf, &argbuf_used); 1163a62a8ef9SStefan Hajnoczi 1164a62a8ef9SStefan Hajnoczi /* Reply elements */ 1165a62a8ef9SStefan Hajnoczi if (test_bit(FR_ISREPLY, &req->flags)) { 1166a62a8ef9SStefan Hajnoczi sg_init_one(&sg[out_sgs + in_sgs++], 1167a62a8ef9SStefan Hajnoczi &req->out.h, sizeof(req->out.h)); 1168a62a8ef9SStefan Hajnoczi in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req, 1169a62a8ef9SStefan Hajnoczi args->out_args, args->out_numargs, 1170a62a8ef9SStefan Hajnoczi args->out_pages, 1171a62a8ef9SStefan Hajnoczi req->argbuf + argbuf_used, NULL); 1172a62a8ef9SStefan Hajnoczi } 1173a62a8ef9SStefan Hajnoczi 1174a62a8ef9SStefan Hajnoczi WARN_ON(out_sgs + in_sgs != total_sgs); 1175a62a8ef9SStefan Hajnoczi 1176a62a8ef9SStefan Hajnoczi for (i = 0; i < total_sgs; i++) 1177a62a8ef9SStefan Hajnoczi sgs[i] = &sg[i]; 1178a62a8ef9SStefan Hajnoczi 1179a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 1180a62a8ef9SStefan Hajnoczi 1181a62a8ef9SStefan Hajnoczi if (!fsvq->connected) { 1182a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 1183a62a8ef9SStefan Hajnoczi ret = -ENOTCONN; 1184a62a8ef9SStefan Hajnoczi goto out; 1185a62a8ef9SStefan Hajnoczi } 1186a62a8ef9SStefan Hajnoczi 1187a62a8ef9SStefan Hajnoczi vq = fsvq->vq; 1188a62a8ef9SStefan Hajnoczi ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC); 1189a62a8ef9SStefan Hajnoczi if (ret < 0) { 1190a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 1191a62a8ef9SStefan Hajnoczi goto out; 1192a62a8ef9SStefan Hajnoczi } 1193a62a8ef9SStefan Hajnoczi 11945dbe190fSVivek Goyal /* Request successfully sent. */ 11955dbe190fSVivek Goyal fpq = &fsvq->fud->pq; 11965dbe190fSVivek Goyal spin_lock(&fpq->lock); 11975dbe190fSVivek Goyal list_add_tail(&req->list, fpq->processing); 11985dbe190fSVivek Goyal spin_unlock(&fpq->lock); 11995dbe190fSVivek Goyal set_bit(FR_SENT, &req->flags); 12005dbe190fSVivek Goyal /* matches barrier in request_wait_answer() */ 12015dbe190fSVivek Goyal smp_mb__after_atomic(); 12025dbe190fSVivek Goyal 1203a9bfd9ddSVivek Goyal if (!in_flight) 1204c17ea009SVivek Goyal inc_in_flight_req(fsvq); 1205a62a8ef9SStefan Hajnoczi notify = virtqueue_kick_prepare(vq); 1206a62a8ef9SStefan Hajnoczi 1207a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 1208a62a8ef9SStefan Hajnoczi 1209a62a8ef9SStefan Hajnoczi if (notify) 1210a62a8ef9SStefan Hajnoczi virtqueue_notify(vq); 1211a62a8ef9SStefan Hajnoczi 1212a62a8ef9SStefan Hajnoczi out: 1213a62a8ef9SStefan Hajnoczi if (ret < 0 && req->argbuf) { 1214a62a8ef9SStefan Hajnoczi kfree(req->argbuf); 1215a62a8ef9SStefan Hajnoczi req->argbuf = NULL; 1216a62a8ef9SStefan Hajnoczi } 1217a62a8ef9SStefan Hajnoczi if (sgs != stack_sgs) { 1218a62a8ef9SStefan Hajnoczi kfree(sgs); 1219a62a8ef9SStefan Hajnoczi kfree(sg); 1220a62a8ef9SStefan Hajnoczi } 1221a62a8ef9SStefan Hajnoczi 1222a62a8ef9SStefan Hajnoczi return ret; 1223a62a8ef9SStefan Hajnoczi } 1224a62a8ef9SStefan Hajnoczi 1225a62a8ef9SStefan Hajnoczi static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq) 1226a62a8ef9SStefan Hajnoczi __releases(fiq->lock) 1227a62a8ef9SStefan Hajnoczi { 1228a62a8ef9SStefan Hajnoczi unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */ 1229a62a8ef9SStefan Hajnoczi struct virtio_fs *fs; 1230a62a8ef9SStefan Hajnoczi struct fuse_req *req; 123151fecdd2SVivek Goyal struct virtio_fs_vq *fsvq; 1232a62a8ef9SStefan Hajnoczi int ret; 1233a62a8ef9SStefan Hajnoczi 1234a62a8ef9SStefan Hajnoczi WARN_ON(list_empty(&fiq->pending)); 1235a62a8ef9SStefan Hajnoczi req = list_last_entry(&fiq->pending, struct fuse_req, list); 1236a62a8ef9SStefan Hajnoczi clear_bit(FR_PENDING, &req->flags); 1237a62a8ef9SStefan Hajnoczi list_del_init(&req->list); 1238a62a8ef9SStefan Hajnoczi WARN_ON(!list_empty(&fiq->pending)); 1239a62a8ef9SStefan Hajnoczi spin_unlock(&fiq->lock); 1240a62a8ef9SStefan Hajnoczi 1241a62a8ef9SStefan Hajnoczi fs = fiq->priv; 1242a62a8ef9SStefan Hajnoczi 1243a62a8ef9SStefan Hajnoczi pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n", 1244a62a8ef9SStefan Hajnoczi __func__, req->in.h.opcode, req->in.h.unique, 1245a62a8ef9SStefan Hajnoczi req->in.h.nodeid, req->in.h.len, 1246a62a8ef9SStefan Hajnoczi fuse_len_args(req->args->out_numargs, req->args->out_args)); 1247a62a8ef9SStefan Hajnoczi 124851fecdd2SVivek Goyal fsvq = &fs->vqs[queue_id]; 1249a9bfd9ddSVivek Goyal ret = virtio_fs_enqueue_req(fsvq, req, false); 1250a62a8ef9SStefan Hajnoczi if (ret < 0) { 1251a62a8ef9SStefan Hajnoczi if (ret == -ENOMEM || ret == -ENOSPC) { 1252a9bfd9ddSVivek Goyal /* 1253a9bfd9ddSVivek Goyal * Virtqueue full. Retry submission from worker 1254a9bfd9ddSVivek Goyal * context as we might be holding fc->bg_lock. 1255a9bfd9ddSVivek Goyal */ 1256a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock); 1257a9bfd9ddSVivek Goyal list_add_tail(&req->list, &fsvq->queued_reqs); 1258a9bfd9ddSVivek Goyal inc_in_flight_req(fsvq); 1259a9bfd9ddSVivek Goyal schedule_delayed_work(&fsvq->dispatch_work, 1260a9bfd9ddSVivek Goyal msecs_to_jiffies(1)); 1261a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock); 1262a9bfd9ddSVivek Goyal return; 1263a62a8ef9SStefan Hajnoczi } 1264a62a8ef9SStefan Hajnoczi req->out.h.error = ret; 1265a62a8ef9SStefan Hajnoczi pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret); 126651fecdd2SVivek Goyal 126751fecdd2SVivek Goyal /* Can't end request in submission context. Use a worker */ 126851fecdd2SVivek Goyal spin_lock(&fsvq->lock); 126951fecdd2SVivek Goyal list_add_tail(&req->list, &fsvq->end_reqs); 127051fecdd2SVivek Goyal schedule_delayed_work(&fsvq->dispatch_work, 0); 127151fecdd2SVivek Goyal spin_unlock(&fsvq->lock); 1272a62a8ef9SStefan Hajnoczi return; 1273a62a8ef9SStefan Hajnoczi } 1274a62a8ef9SStefan Hajnoczi } 1275a62a8ef9SStefan Hajnoczi 127600929447SYueHaibing static const struct fuse_iqueue_ops virtio_fs_fiq_ops = { 1277a62a8ef9SStefan Hajnoczi .wake_forget_and_unlock = virtio_fs_wake_forget_and_unlock, 1278a62a8ef9SStefan Hajnoczi .wake_interrupt_and_unlock = virtio_fs_wake_interrupt_and_unlock, 1279a62a8ef9SStefan Hajnoczi .wake_pending_and_unlock = virtio_fs_wake_pending_and_unlock, 1280a62a8ef9SStefan Hajnoczi .release = virtio_fs_fiq_release, 1281a62a8ef9SStefan Hajnoczi }; 1282a62a8ef9SStefan Hajnoczi 12831dd53957SVivek Goyal static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx) 1284a62a8ef9SStefan Hajnoczi { 12851dd53957SVivek Goyal ctx->rootmode = S_IFDIR; 12861dd53957SVivek Goyal ctx->default_permissions = 1; 12871dd53957SVivek Goyal ctx->allow_other = 1; 12881dd53957SVivek Goyal ctx->max_read = UINT_MAX; 12891dd53957SVivek Goyal ctx->blksize = 512; 12901dd53957SVivek Goyal ctx->destroy = true; 12911dd53957SVivek Goyal ctx->no_control = true; 12921dd53957SVivek Goyal ctx->no_force_umount = true; 12931dd53957SVivek Goyal } 12941dd53957SVivek Goyal 12951dd53957SVivek Goyal static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) 1296a62a8ef9SStefan Hajnoczi { 1297fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount_super(sb); 1298fcee216bSMax Reitz struct fuse_conn *fc = fm->fc; 1299a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = fc->iq.priv; 13001dd53957SVivek Goyal struct fuse_fs_context *ctx = fsc->fs_private; 1301a62a8ef9SStefan Hajnoczi unsigned int i; 1302a62a8ef9SStefan Hajnoczi int err; 1303a62a8ef9SStefan Hajnoczi 13041dd53957SVivek Goyal virtio_fs_ctx_set_defaults(ctx); 1305a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex); 1306a62a8ef9SStefan Hajnoczi 1307a62a8ef9SStefan Hajnoczi /* After holding mutex, make sure virtiofs device is still there. 1308a62a8ef9SStefan Hajnoczi * Though we are holding a reference to it, drive ->remove might 1309a62a8ef9SStefan Hajnoczi * still have cleaned up virtual queues. In that case bail out. 1310a62a8ef9SStefan Hajnoczi */ 1311a62a8ef9SStefan Hajnoczi err = -EINVAL; 1312a62a8ef9SStefan Hajnoczi if (list_empty(&fs->list)) { 1313a62a8ef9SStefan Hajnoczi pr_info("virtio-fs: tag <%s> not found\n", fs->tag); 1314a62a8ef9SStefan Hajnoczi goto err; 1315a62a8ef9SStefan Hajnoczi } 1316a62a8ef9SStefan Hajnoczi 1317a62a8ef9SStefan Hajnoczi err = -ENOMEM; 1318a62a8ef9SStefan Hajnoczi /* Allocate fuse_dev for hiprio and notification queues */ 13197fd3abfaSVivek Goyal for (i = 0; i < fs->nvqs; i++) { 1320a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1321a62a8ef9SStefan Hajnoczi 1322a62a8ef9SStefan Hajnoczi fsvq->fud = fuse_dev_alloc(); 1323a62a8ef9SStefan Hajnoczi if (!fsvq->fud) 1324a62a8ef9SStefan Hajnoczi goto err_free_fuse_devs; 1325a62a8ef9SStefan Hajnoczi } 1326a62a8ef9SStefan Hajnoczi 13277fd3abfaSVivek Goyal /* virtiofs allocates and installs its own fuse devices */ 13281dd53957SVivek Goyal ctx->fudptr = NULL; 13293f9b9efdSVivek Goyal if (ctx->dax) { 13303f9b9efdSVivek Goyal if (!fs->dax_dev) { 13313f9b9efdSVivek Goyal err = -EINVAL; 13323f9b9efdSVivek Goyal pr_err("virtio-fs: dax can't be enabled as filesystem" 13333f9b9efdSVivek Goyal " device does not support it.\n"); 13343f9b9efdSVivek Goyal goto err_free_fuse_devs; 13353f9b9efdSVivek Goyal } 13361dd53957SVivek Goyal ctx->dax_dev = fs->dax_dev; 13373f9b9efdSVivek Goyal } 13381dd53957SVivek Goyal err = fuse_fill_super_common(sb, ctx); 1339a62a8ef9SStefan Hajnoczi if (err < 0) 1340a62a8ef9SStefan Hajnoczi goto err_free_fuse_devs; 1341a62a8ef9SStefan Hajnoczi 1342a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) { 1343a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1344a62a8ef9SStefan Hajnoczi 1345a62a8ef9SStefan Hajnoczi fuse_dev_install(fsvq->fud, fc); 1346a62a8ef9SStefan Hajnoczi } 1347a62a8ef9SStefan Hajnoczi 1348a62a8ef9SStefan Hajnoczi /* Previous unmount will stop all queues. Start these again */ 1349a62a8ef9SStefan Hajnoczi virtio_fs_start_all_queues(fs); 1350fcee216bSMax Reitz fuse_send_init(fm); 1351a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 1352a62a8ef9SStefan Hajnoczi return 0; 1353a62a8ef9SStefan Hajnoczi 1354a62a8ef9SStefan Hajnoczi err_free_fuse_devs: 1355a62a8ef9SStefan Hajnoczi virtio_fs_free_devs(fs); 1356a62a8ef9SStefan Hajnoczi err: 1357a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex); 1358a62a8ef9SStefan Hajnoczi return err; 1359a62a8ef9SStefan Hajnoczi } 1360a62a8ef9SStefan Hajnoczi 1361fcee216bSMax Reitz static void virtio_fs_conn_destroy(struct fuse_mount *fm) 1362a62a8ef9SStefan Hajnoczi { 1363fcee216bSMax Reitz struct fuse_conn *fc = fm->fc; 1364fcee216bSMax Reitz struct virtio_fs *vfs = fc->iq.priv; 1365fcee216bSMax Reitz struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO]; 1366a62a8ef9SStefan Hajnoczi 1367fcee216bSMax Reitz /* Stop dax worker. Soon evict_inodes() will be called which 1368fcee216bSMax Reitz * will free all memory ranges belonging to all inodes. 13699a752d18SVivek Goyal */ 13709a752d18SVivek Goyal if (IS_ENABLED(CONFIG_FUSE_DAX)) 13719a752d18SVivek Goyal fuse_dax_cancel_work(fc); 1372a62a8ef9SStefan Hajnoczi 1373a62a8ef9SStefan Hajnoczi /* Stop forget queue. Soon destroy will be sent */ 1374a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock); 1375a62a8ef9SStefan Hajnoczi fsvq->connected = false; 1376a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock); 1377a62a8ef9SStefan Hajnoczi virtio_fs_drain_all_queues(vfs); 1378a62a8ef9SStefan Hajnoczi 1379fcee216bSMax Reitz fuse_conn_destroy(fm); 1380a62a8ef9SStefan Hajnoczi 1381fcee216bSMax Reitz /* fuse_conn_destroy() must have sent destroy. Stop all queues 1382a62a8ef9SStefan Hajnoczi * and drain one more time and free fuse devices. Freeing fuse 1383a62a8ef9SStefan Hajnoczi * devices will drop their reference on fuse_conn and that in 1384a62a8ef9SStefan Hajnoczi * turn will drop its reference on virtio_fs object. 1385a62a8ef9SStefan Hajnoczi */ 1386a62a8ef9SStefan Hajnoczi virtio_fs_stop_all_queues(vfs); 1387a62a8ef9SStefan Hajnoczi virtio_fs_drain_all_queues(vfs); 1388a62a8ef9SStefan Hajnoczi virtio_fs_free_devs(vfs); 1389a62a8ef9SStefan Hajnoczi } 1390a62a8ef9SStefan Hajnoczi 1391fcee216bSMax Reitz static void virtio_kill_sb(struct super_block *sb) 1392fcee216bSMax Reitz { 1393fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount_super(sb); 1394fcee216bSMax Reitz bool last; 1395fcee216bSMax Reitz 1396fcee216bSMax Reitz /* If mount failed, we can still be called without any fc */ 1397fcee216bSMax Reitz if (fm) { 1398fcee216bSMax Reitz last = fuse_mount_remove(fm); 1399fcee216bSMax Reitz if (last) 1400fcee216bSMax Reitz virtio_fs_conn_destroy(fm); 1401fcee216bSMax Reitz } 1402fcee216bSMax Reitz kill_anon_super(sb); 1403fcee216bSMax Reitz } 1404fcee216bSMax Reitz 1405a62a8ef9SStefan Hajnoczi static int virtio_fs_test_super(struct super_block *sb, 1406a62a8ef9SStefan Hajnoczi struct fs_context *fsc) 1407a62a8ef9SStefan Hajnoczi { 1408fcee216bSMax Reitz struct fuse_mount *fsc_fm = fsc->s_fs_info; 1409fcee216bSMax Reitz struct fuse_mount *sb_fm = get_fuse_mount_super(sb); 1410a62a8ef9SStefan Hajnoczi 1411fcee216bSMax Reitz return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv; 1412a62a8ef9SStefan Hajnoczi } 1413a62a8ef9SStefan Hajnoczi 1414a62a8ef9SStefan Hajnoczi static int virtio_fs_get_tree(struct fs_context *fsc) 1415a62a8ef9SStefan Hajnoczi { 1416a62a8ef9SStefan Hajnoczi struct virtio_fs *fs; 1417a62a8ef9SStefan Hajnoczi struct super_block *sb; 1418a7f0d7aaSConnor Kuehl struct fuse_conn *fc = NULL; 1419fcee216bSMax Reitz struct fuse_mount *fm; 1420a7f0d7aaSConnor Kuehl unsigned int virtqueue_size; 1421a7f0d7aaSConnor Kuehl int err = -EIO; 1422a62a8ef9SStefan Hajnoczi 1423a62a8ef9SStefan Hajnoczi /* This gets a reference on virtio_fs object. This ptr gets installed 1424a62a8ef9SStefan Hajnoczi * in fc->iq->priv. Once fuse_conn is going away, it calls ->put() 1425a62a8ef9SStefan Hajnoczi * to drop the reference to this object. 1426a62a8ef9SStefan Hajnoczi */ 1427a62a8ef9SStefan Hajnoczi fs = virtio_fs_find_instance(fsc->source); 1428a62a8ef9SStefan Hajnoczi if (!fs) { 1429a62a8ef9SStefan Hajnoczi pr_info("virtio-fs: tag <%s> not found\n", fsc->source); 1430a62a8ef9SStefan Hajnoczi return -EINVAL; 1431a62a8ef9SStefan Hajnoczi } 1432a62a8ef9SStefan Hajnoczi 1433a7f0d7aaSConnor Kuehl virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq); 1434a7f0d7aaSConnor Kuehl if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD)) 1435a7f0d7aaSConnor Kuehl goto out_err; 1436a7f0d7aaSConnor Kuehl 1437833c5a42SMiklos Szeredi err = -ENOMEM; 1438a62a8ef9SStefan Hajnoczi fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL); 1439833c5a42SMiklos Szeredi if (!fc) 1440833c5a42SMiklos Szeredi goto out_err; 1441a62a8ef9SStefan Hajnoczi 1442fcee216bSMax Reitz fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL); 1443833c5a42SMiklos Szeredi if (!fm) 1444833c5a42SMiklos Szeredi goto out_err; 1445fcee216bSMax Reitz 14460a7419c6SMiklos Szeredi fuse_conn_init(fc, fm, fsc->user_ns, &virtio_fs_fiq_ops, fs); 1447a62a8ef9SStefan Hajnoczi fc->release = fuse_free_conn; 1448a62a8ef9SStefan Hajnoczi fc->delete_stale = true; 1449bf109c64SMax Reitz fc->auto_submounts = true; 14502d82ab25SGreg Kurz fc->sync_fs = true; 1451a62a8ef9SStefan Hajnoczi 1452a7f0d7aaSConnor Kuehl /* Tell FUSE to split requests that exceed the virtqueue's size */ 1453a7f0d7aaSConnor Kuehl fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit, 1454a7f0d7aaSConnor Kuehl virtqueue_size - FUSE_HEADER_OVERHEAD); 1455a7f0d7aaSConnor Kuehl 1456fcee216bSMax Reitz fsc->s_fs_info = fm; 1457b19d3d00SMiklos Szeredi sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc); 1458514b5e3fSMiklos Szeredi if (fsc->s_fs_info) { 1459514b5e3fSMiklos Szeredi fuse_conn_put(fc); 1460514b5e3fSMiklos Szeredi kfree(fm); 1461514b5e3fSMiklos Szeredi } 1462a62a8ef9SStefan Hajnoczi if (IS_ERR(sb)) 1463a62a8ef9SStefan Hajnoczi return PTR_ERR(sb); 1464a62a8ef9SStefan Hajnoczi 1465a62a8ef9SStefan Hajnoczi if (!sb->s_root) { 14661dd53957SVivek Goyal err = virtio_fs_fill_super(sb, fsc); 1467a62a8ef9SStefan Hajnoczi if (err) { 1468514b5e3fSMiklos Szeredi fuse_conn_put(fc); 1469514b5e3fSMiklos Szeredi kfree(fm); 147066ab33bfSMiklos Szeredi sb->s_fs_info = NULL; 1471a62a8ef9SStefan Hajnoczi deactivate_locked_super(sb); 1472a62a8ef9SStefan Hajnoczi return err; 1473a62a8ef9SStefan Hajnoczi } 1474a62a8ef9SStefan Hajnoczi 1475a62a8ef9SStefan Hajnoczi sb->s_flags |= SB_ACTIVE; 1476a62a8ef9SStefan Hajnoczi } 1477a62a8ef9SStefan Hajnoczi 1478a62a8ef9SStefan Hajnoczi WARN_ON(fsc->root); 1479a62a8ef9SStefan Hajnoczi fsc->root = dget(sb->s_root); 1480a62a8ef9SStefan Hajnoczi return 0; 1481833c5a42SMiklos Szeredi 1482833c5a42SMiklos Szeredi out_err: 1483833c5a42SMiklos Szeredi kfree(fc); 1484833c5a42SMiklos Szeredi mutex_lock(&virtio_fs_mutex); 1485833c5a42SMiklos Szeredi virtio_fs_put(fs); 1486833c5a42SMiklos Szeredi mutex_unlock(&virtio_fs_mutex); 1487833c5a42SMiklos Szeredi return err; 1488a62a8ef9SStefan Hajnoczi } 1489a62a8ef9SStefan Hajnoczi 1490a62a8ef9SStefan Hajnoczi static const struct fs_context_operations virtio_fs_context_ops = { 1491*84c21507SMiklos Szeredi .free = virtio_fs_free_fsc, 14921dd53957SVivek Goyal .parse_param = virtio_fs_parse_param, 1493a62a8ef9SStefan Hajnoczi .get_tree = virtio_fs_get_tree, 1494a62a8ef9SStefan Hajnoczi }; 1495a62a8ef9SStefan Hajnoczi 1496a62a8ef9SStefan Hajnoczi static int virtio_fs_init_fs_context(struct fs_context *fsc) 1497a62a8ef9SStefan Hajnoczi { 14981dd53957SVivek Goyal struct fuse_fs_context *ctx; 14991dd53957SVivek Goyal 1500fe0a7bd8SGreg Kurz if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT) 1501fe0a7bd8SGreg Kurz return fuse_init_fs_context_submount(fsc); 1502fe0a7bd8SGreg Kurz 15031dd53957SVivek Goyal ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL); 15041dd53957SVivek Goyal if (!ctx) 15051dd53957SVivek Goyal return -ENOMEM; 15061dd53957SVivek Goyal fsc->fs_private = ctx; 1507a62a8ef9SStefan Hajnoczi fsc->ops = &virtio_fs_context_ops; 1508a62a8ef9SStefan Hajnoczi return 0; 1509a62a8ef9SStefan Hajnoczi } 1510a62a8ef9SStefan Hajnoczi 1511a62a8ef9SStefan Hajnoczi static struct file_system_type virtio_fs_type = { 1512a62a8ef9SStefan Hajnoczi .owner = THIS_MODULE, 1513a62a8ef9SStefan Hajnoczi .name = "virtiofs", 1514a62a8ef9SStefan Hajnoczi .init_fs_context = virtio_fs_init_fs_context, 1515a62a8ef9SStefan Hajnoczi .kill_sb = virtio_kill_sb, 1516a62a8ef9SStefan Hajnoczi }; 1517a62a8ef9SStefan Hajnoczi 1518a62a8ef9SStefan Hajnoczi static int __init virtio_fs_init(void) 1519a62a8ef9SStefan Hajnoczi { 1520a62a8ef9SStefan Hajnoczi int ret; 1521a62a8ef9SStefan Hajnoczi 1522a62a8ef9SStefan Hajnoczi ret = register_virtio_driver(&virtio_fs_driver); 1523a62a8ef9SStefan Hajnoczi if (ret < 0) 1524a62a8ef9SStefan Hajnoczi return ret; 1525a62a8ef9SStefan Hajnoczi 1526a62a8ef9SStefan Hajnoczi ret = register_filesystem(&virtio_fs_type); 1527a62a8ef9SStefan Hajnoczi if (ret < 0) { 1528a62a8ef9SStefan Hajnoczi unregister_virtio_driver(&virtio_fs_driver); 1529a62a8ef9SStefan Hajnoczi return ret; 1530a62a8ef9SStefan Hajnoczi } 1531a62a8ef9SStefan Hajnoczi 1532a62a8ef9SStefan Hajnoczi return 0; 1533a62a8ef9SStefan Hajnoczi } 1534a62a8ef9SStefan Hajnoczi module_init(virtio_fs_init); 1535a62a8ef9SStefan Hajnoczi 1536a62a8ef9SStefan Hajnoczi static void __exit virtio_fs_exit(void) 1537a62a8ef9SStefan Hajnoczi { 1538a62a8ef9SStefan Hajnoczi unregister_filesystem(&virtio_fs_type); 1539a62a8ef9SStefan Hajnoczi unregister_virtio_driver(&virtio_fs_driver); 1540a62a8ef9SStefan Hajnoczi } 1541a62a8ef9SStefan Hajnoczi module_exit(virtio_fs_exit); 1542a62a8ef9SStefan Hajnoczi 1543a62a8ef9SStefan Hajnoczi MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>"); 1544a62a8ef9SStefan Hajnoczi MODULE_DESCRIPTION("Virtio Filesystem"); 1545a62a8ef9SStefan Hajnoczi MODULE_LICENSE("GPL"); 1546a62a8ef9SStefan Hajnoczi MODULE_ALIAS_FS(KBUILD_MODNAME); 1547a62a8ef9SStefan Hajnoczi MODULE_DEVICE_TABLE(virtio, id_table); 1548