xref: /openbmc/linux/fs/fuse/virtio_fs.c (revision 7ac5360c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * virtio-fs: Virtio Filesystem
4  * Copyright (C) 2018 Red Hat, Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/dax.h>
9 #include <linux/pci.h>
10 #include <linux/pfn_t.h>
11 #include <linux/module.h>
12 #include <linux/virtio.h>
13 #include <linux/virtio_fs.h>
14 #include <linux/delay.h>
15 #include <linux/fs_context.h>
16 #include <linux/fs_parser.h>
17 #include <linux/highmem.h>
18 #include <linux/uio.h>
19 #include "fuse_i.h"
20 
21 /* Used to help calculate the FUSE connection's max_pages limit for a request's
22  * size. Parts of the struct fuse_req are sliced into scattergather lists in
23  * addition to the pages used, so this can help account for that overhead.
24  */
25 #define FUSE_HEADER_OVERHEAD    4
26 
27 /* List of virtio-fs device instances and a lock for the list. Also provides
28  * mutual exclusion in device removal and mounting path
29  */
30 static DEFINE_MUTEX(virtio_fs_mutex);
31 static LIST_HEAD(virtio_fs_instances);
32 
33 enum {
34 	VQ_HIPRIO,
35 	VQ_REQUEST
36 };
37 
38 #define VQ_NAME_LEN	24
39 
40 /* Per-virtqueue state */
41 struct virtio_fs_vq {
42 	spinlock_t lock;
43 	struct virtqueue *vq;     /* protected by ->lock */
44 	struct work_struct done_work;
45 	struct list_head queued_reqs;
46 	struct list_head end_reqs;	/* End these requests */
47 	struct delayed_work dispatch_work;
48 	struct fuse_dev *fud;
49 	bool connected;
50 	long in_flight;
51 	struct completion in_flight_zero; /* No inflight requests */
52 	char name[VQ_NAME_LEN];
53 } ____cacheline_aligned_in_smp;
54 
55 /* A virtio-fs device instance */
56 struct virtio_fs {
57 	struct kref refcount;
58 	struct list_head list;    /* on virtio_fs_instances */
59 	char *tag;
60 	struct virtio_fs_vq *vqs;
61 	unsigned int nvqs;               /* number of virtqueues */
62 	unsigned int num_request_queues; /* number of request queues */
63 	struct dax_device *dax_dev;
64 
65 	/* DAX memory window where file contents are mapped */
66 	void *window_kaddr;
67 	phys_addr_t window_phys_addr;
68 	size_t window_len;
69 };
70 
71 struct virtio_fs_forget_req {
72 	struct fuse_in_header ih;
73 	struct fuse_forget_in arg;
74 };
75 
76 struct virtio_fs_forget {
77 	/* This request can be temporarily queued on virt queue */
78 	struct list_head list;
79 	struct virtio_fs_forget_req req;
80 };
81 
82 struct virtio_fs_req_work {
83 	struct fuse_req *req;
84 	struct virtio_fs_vq *fsvq;
85 	struct work_struct done_work;
86 };
87 
88 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
89 				 struct fuse_req *req, bool in_flight);
90 
91 enum {
92 	OPT_DAX,
93 };
94 
95 static const struct fs_parameter_spec virtio_fs_parameters[] = {
96 	fsparam_flag("dax", OPT_DAX),
97 	{}
98 };
99 
100 static int virtio_fs_parse_param(struct fs_context *fsc,
101 				 struct fs_parameter *param)
102 {
103 	struct fs_parse_result result;
104 	struct fuse_fs_context *ctx = fsc->fs_private;
105 	int opt;
106 
107 	opt = fs_parse(fsc, virtio_fs_parameters, param, &result);
108 	if (opt < 0)
109 		return opt;
110 
111 	switch (opt) {
112 	case OPT_DAX:
113 		ctx->dax = 1;
114 		break;
115 	default:
116 		return -EINVAL;
117 	}
118 
119 	return 0;
120 }
121 
122 static void virtio_fs_free_fsc(struct fs_context *fsc)
123 {
124 	struct fuse_fs_context *ctx = fsc->fs_private;
125 
126 	kfree(ctx);
127 }
128 
129 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
130 {
131 	struct virtio_fs *fs = vq->vdev->priv;
132 
133 	return &fs->vqs[vq->index];
134 }
135 
136 /* Should be called with fsvq->lock held. */
137 static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq)
138 {
139 	fsvq->in_flight++;
140 }
141 
142 /* Should be called with fsvq->lock held. */
143 static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq)
144 {
145 	WARN_ON(fsvq->in_flight <= 0);
146 	fsvq->in_flight--;
147 	if (!fsvq->in_flight)
148 		complete(&fsvq->in_flight_zero);
149 }
150 
151 static void release_virtio_fs_obj(struct kref *ref)
152 {
153 	struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount);
154 
155 	kfree(vfs->vqs);
156 	kfree(vfs);
157 }
158 
159 /* Make sure virtiofs_mutex is held */
160 static void virtio_fs_put(struct virtio_fs *fs)
161 {
162 	kref_put(&fs->refcount, release_virtio_fs_obj);
163 }
164 
165 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
166 {
167 	struct virtio_fs *vfs = fiq->priv;
168 
169 	mutex_lock(&virtio_fs_mutex);
170 	virtio_fs_put(vfs);
171 	mutex_unlock(&virtio_fs_mutex);
172 }
173 
174 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
175 {
176 	WARN_ON(fsvq->in_flight < 0);
177 
178 	/* Wait for in flight requests to finish.*/
179 	spin_lock(&fsvq->lock);
180 	if (fsvq->in_flight) {
181 		/* We are holding virtio_fs_mutex. There should not be any
182 		 * waiters waiting for completion.
183 		 */
184 		reinit_completion(&fsvq->in_flight_zero);
185 		spin_unlock(&fsvq->lock);
186 		wait_for_completion(&fsvq->in_flight_zero);
187 	} else {
188 		spin_unlock(&fsvq->lock);
189 	}
190 
191 	flush_work(&fsvq->done_work);
192 	flush_delayed_work(&fsvq->dispatch_work);
193 }
194 
195 static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs)
196 {
197 	struct virtio_fs_vq *fsvq;
198 	int i;
199 
200 	for (i = 0; i < fs->nvqs; i++) {
201 		fsvq = &fs->vqs[i];
202 		virtio_fs_drain_queue(fsvq);
203 	}
204 }
205 
206 static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
207 {
208 	/* Provides mutual exclusion between ->remove and ->kill_sb
209 	 * paths. We don't want both of these draining queue at the
210 	 * same time. Current completion logic reinits completion
211 	 * and that means there should not be any other thread
212 	 * doing reinit or waiting for completion already.
213 	 */
214 	mutex_lock(&virtio_fs_mutex);
215 	virtio_fs_drain_all_queues_locked(fs);
216 	mutex_unlock(&virtio_fs_mutex);
217 }
218 
219 static void virtio_fs_start_all_queues(struct virtio_fs *fs)
220 {
221 	struct virtio_fs_vq *fsvq;
222 	int i;
223 
224 	for (i = 0; i < fs->nvqs; i++) {
225 		fsvq = &fs->vqs[i];
226 		spin_lock(&fsvq->lock);
227 		fsvq->connected = true;
228 		spin_unlock(&fsvq->lock);
229 	}
230 }
231 
232 /* Add a new instance to the list or return -EEXIST if tag name exists*/
233 static int virtio_fs_add_instance(struct virtio_fs *fs)
234 {
235 	struct virtio_fs *fs2;
236 	bool duplicate = false;
237 
238 	mutex_lock(&virtio_fs_mutex);
239 
240 	list_for_each_entry(fs2, &virtio_fs_instances, list) {
241 		if (strcmp(fs->tag, fs2->tag) == 0)
242 			duplicate = true;
243 	}
244 
245 	if (!duplicate)
246 		list_add_tail(&fs->list, &virtio_fs_instances);
247 
248 	mutex_unlock(&virtio_fs_mutex);
249 
250 	if (duplicate)
251 		return -EEXIST;
252 	return 0;
253 }
254 
255 /* Return the virtio_fs with a given tag, or NULL */
256 static struct virtio_fs *virtio_fs_find_instance(const char *tag)
257 {
258 	struct virtio_fs *fs;
259 
260 	mutex_lock(&virtio_fs_mutex);
261 
262 	list_for_each_entry(fs, &virtio_fs_instances, list) {
263 		if (strcmp(fs->tag, tag) == 0) {
264 			kref_get(&fs->refcount);
265 			goto found;
266 		}
267 	}
268 
269 	fs = NULL; /* not found */
270 
271 found:
272 	mutex_unlock(&virtio_fs_mutex);
273 
274 	return fs;
275 }
276 
277 static void virtio_fs_free_devs(struct virtio_fs *fs)
278 {
279 	unsigned int i;
280 
281 	for (i = 0; i < fs->nvqs; i++) {
282 		struct virtio_fs_vq *fsvq = &fs->vqs[i];
283 
284 		if (!fsvq->fud)
285 			continue;
286 
287 		fuse_dev_free(fsvq->fud);
288 		fsvq->fud = NULL;
289 	}
290 }
291 
292 /* Read filesystem name from virtio config into fs->tag (must kfree()). */
293 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
294 {
295 	char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
296 	char *end;
297 	size_t len;
298 
299 	virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
300 			   &tag_buf, sizeof(tag_buf));
301 	end = memchr(tag_buf, '\0', sizeof(tag_buf));
302 	if (end == tag_buf)
303 		return -EINVAL; /* empty tag */
304 	if (!end)
305 		end = &tag_buf[sizeof(tag_buf)];
306 
307 	len = end - tag_buf;
308 	fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
309 	if (!fs->tag)
310 		return -ENOMEM;
311 	memcpy(fs->tag, tag_buf, len);
312 	fs->tag[len] = '\0';
313 	return 0;
314 }
315 
316 /* Work function for hiprio completion */
317 static void virtio_fs_hiprio_done_work(struct work_struct *work)
318 {
319 	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
320 						 done_work);
321 	struct virtqueue *vq = fsvq->vq;
322 
323 	/* Free completed FUSE_FORGET requests */
324 	spin_lock(&fsvq->lock);
325 	do {
326 		unsigned int len;
327 		void *req;
328 
329 		virtqueue_disable_cb(vq);
330 
331 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
332 			kfree(req);
333 			dec_in_flight_req(fsvq);
334 		}
335 	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
336 	spin_unlock(&fsvq->lock);
337 }
338 
339 static void virtio_fs_request_dispatch_work(struct work_struct *work)
340 {
341 	struct fuse_req *req;
342 	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
343 						 dispatch_work.work);
344 	int ret;
345 
346 	pr_debug("virtio-fs: worker %s called.\n", __func__);
347 	while (1) {
348 		spin_lock(&fsvq->lock);
349 		req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
350 					       list);
351 		if (!req) {
352 			spin_unlock(&fsvq->lock);
353 			break;
354 		}
355 
356 		list_del_init(&req->list);
357 		spin_unlock(&fsvq->lock);
358 		fuse_request_end(req);
359 	}
360 
361 	/* Dispatch pending requests */
362 	while (1) {
363 		spin_lock(&fsvq->lock);
364 		req = list_first_entry_or_null(&fsvq->queued_reqs,
365 					       struct fuse_req, list);
366 		if (!req) {
367 			spin_unlock(&fsvq->lock);
368 			return;
369 		}
370 		list_del_init(&req->list);
371 		spin_unlock(&fsvq->lock);
372 
373 		ret = virtio_fs_enqueue_req(fsvq, req, true);
374 		if (ret < 0) {
375 			if (ret == -ENOMEM || ret == -ENOSPC) {
376 				spin_lock(&fsvq->lock);
377 				list_add_tail(&req->list, &fsvq->queued_reqs);
378 				schedule_delayed_work(&fsvq->dispatch_work,
379 						      msecs_to_jiffies(1));
380 				spin_unlock(&fsvq->lock);
381 				return;
382 			}
383 			req->out.h.error = ret;
384 			spin_lock(&fsvq->lock);
385 			dec_in_flight_req(fsvq);
386 			spin_unlock(&fsvq->lock);
387 			pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n",
388 			       ret);
389 			fuse_request_end(req);
390 		}
391 	}
392 }
393 
394 /*
395  * Returns 1 if queue is full and sender should wait a bit before sending
396  * next request, 0 otherwise.
397  */
398 static int send_forget_request(struct virtio_fs_vq *fsvq,
399 			       struct virtio_fs_forget *forget,
400 			       bool in_flight)
401 {
402 	struct scatterlist sg;
403 	struct virtqueue *vq;
404 	int ret = 0;
405 	bool notify;
406 	struct virtio_fs_forget_req *req = &forget->req;
407 
408 	spin_lock(&fsvq->lock);
409 	if (!fsvq->connected) {
410 		if (in_flight)
411 			dec_in_flight_req(fsvq);
412 		kfree(forget);
413 		goto out;
414 	}
415 
416 	sg_init_one(&sg, req, sizeof(*req));
417 	vq = fsvq->vq;
418 	dev_dbg(&vq->vdev->dev, "%s\n", __func__);
419 
420 	ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC);
421 	if (ret < 0) {
422 		if (ret == -ENOMEM || ret == -ENOSPC) {
423 			pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
424 				 ret);
425 			list_add_tail(&forget->list, &fsvq->queued_reqs);
426 			schedule_delayed_work(&fsvq->dispatch_work,
427 					      msecs_to_jiffies(1));
428 			if (!in_flight)
429 				inc_in_flight_req(fsvq);
430 			/* Queue is full */
431 			ret = 1;
432 		} else {
433 			pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
434 				 ret);
435 			kfree(forget);
436 			if (in_flight)
437 				dec_in_flight_req(fsvq);
438 		}
439 		goto out;
440 	}
441 
442 	if (!in_flight)
443 		inc_in_flight_req(fsvq);
444 	notify = virtqueue_kick_prepare(vq);
445 	spin_unlock(&fsvq->lock);
446 
447 	if (notify)
448 		virtqueue_notify(vq);
449 	return ret;
450 out:
451 	spin_unlock(&fsvq->lock);
452 	return ret;
453 }
454 
455 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
456 {
457 	struct virtio_fs_forget *forget;
458 	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
459 						 dispatch_work.work);
460 	pr_debug("virtio-fs: worker %s called.\n", __func__);
461 	while (1) {
462 		spin_lock(&fsvq->lock);
463 		forget = list_first_entry_or_null(&fsvq->queued_reqs,
464 					struct virtio_fs_forget, list);
465 		if (!forget) {
466 			spin_unlock(&fsvq->lock);
467 			return;
468 		}
469 
470 		list_del(&forget->list);
471 		spin_unlock(&fsvq->lock);
472 		if (send_forget_request(fsvq, forget, true))
473 			return;
474 	}
475 }
476 
477 /* Allocate and copy args into req->argbuf */
478 static int copy_args_to_argbuf(struct fuse_req *req)
479 {
480 	struct fuse_args *args = req->args;
481 	unsigned int offset = 0;
482 	unsigned int num_in;
483 	unsigned int num_out;
484 	unsigned int len;
485 	unsigned int i;
486 
487 	num_in = args->in_numargs - args->in_pages;
488 	num_out = args->out_numargs - args->out_pages;
489 	len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
490 	      fuse_len_args(num_out, args->out_args);
491 
492 	req->argbuf = kmalloc(len, GFP_ATOMIC);
493 	if (!req->argbuf)
494 		return -ENOMEM;
495 
496 	for (i = 0; i < num_in; i++) {
497 		memcpy(req->argbuf + offset,
498 		       args->in_args[i].value,
499 		       args->in_args[i].size);
500 		offset += args->in_args[i].size;
501 	}
502 
503 	return 0;
504 }
505 
506 /* Copy args out of and free req->argbuf */
507 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
508 {
509 	unsigned int remaining;
510 	unsigned int offset;
511 	unsigned int num_in;
512 	unsigned int num_out;
513 	unsigned int i;
514 
515 	remaining = req->out.h.len - sizeof(req->out.h);
516 	num_in = args->in_numargs - args->in_pages;
517 	num_out = args->out_numargs - args->out_pages;
518 	offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
519 
520 	for (i = 0; i < num_out; i++) {
521 		unsigned int argsize = args->out_args[i].size;
522 
523 		if (args->out_argvar &&
524 		    i == args->out_numargs - 1 &&
525 		    argsize > remaining) {
526 			argsize = remaining;
527 		}
528 
529 		memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
530 		offset += argsize;
531 
532 		if (i != args->out_numargs - 1)
533 			remaining -= argsize;
534 	}
535 
536 	/* Store the actual size of the variable-length arg */
537 	if (args->out_argvar)
538 		args->out_args[args->out_numargs - 1].size = remaining;
539 
540 	kfree(req->argbuf);
541 	req->argbuf = NULL;
542 }
543 
544 /* Work function for request completion */
545 static void virtio_fs_request_complete(struct fuse_req *req,
546 				       struct virtio_fs_vq *fsvq)
547 {
548 	struct fuse_pqueue *fpq = &fsvq->fud->pq;
549 	struct fuse_args *args;
550 	struct fuse_args_pages *ap;
551 	unsigned int len, i, thislen;
552 	struct page *page;
553 
554 	/*
555 	 * TODO verify that server properly follows FUSE protocol
556 	 * (oh.uniq, oh.len)
557 	 */
558 	args = req->args;
559 	copy_args_from_argbuf(args, req);
560 
561 	if (args->out_pages && args->page_zeroing) {
562 		len = args->out_args[args->out_numargs - 1].size;
563 		ap = container_of(args, typeof(*ap), args);
564 		for (i = 0; i < ap->num_pages; i++) {
565 			thislen = ap->descs[i].length;
566 			if (len < thislen) {
567 				WARN_ON(ap->descs[i].offset);
568 				page = ap->pages[i];
569 				zero_user_segment(page, len, thislen);
570 				len = 0;
571 			} else {
572 				len -= thislen;
573 			}
574 		}
575 	}
576 
577 	spin_lock(&fpq->lock);
578 	clear_bit(FR_SENT, &req->flags);
579 	spin_unlock(&fpq->lock);
580 
581 	fuse_request_end(req);
582 	spin_lock(&fsvq->lock);
583 	dec_in_flight_req(fsvq);
584 	spin_unlock(&fsvq->lock);
585 }
586 
587 static void virtio_fs_complete_req_work(struct work_struct *work)
588 {
589 	struct virtio_fs_req_work *w =
590 		container_of(work, typeof(*w), done_work);
591 
592 	virtio_fs_request_complete(w->req, w->fsvq);
593 	kfree(w);
594 }
595 
596 static void virtio_fs_requests_done_work(struct work_struct *work)
597 {
598 	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
599 						 done_work);
600 	struct fuse_pqueue *fpq = &fsvq->fud->pq;
601 	struct virtqueue *vq = fsvq->vq;
602 	struct fuse_req *req;
603 	struct fuse_req *next;
604 	unsigned int len;
605 	LIST_HEAD(reqs);
606 
607 	/* Collect completed requests off the virtqueue */
608 	spin_lock(&fsvq->lock);
609 	do {
610 		virtqueue_disable_cb(vq);
611 
612 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
613 			spin_lock(&fpq->lock);
614 			list_move_tail(&req->list, &reqs);
615 			spin_unlock(&fpq->lock);
616 		}
617 	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
618 	spin_unlock(&fsvq->lock);
619 
620 	/* End requests */
621 	list_for_each_entry_safe(req, next, &reqs, list) {
622 		list_del_init(&req->list);
623 
624 		/* blocking async request completes in a worker context */
625 		if (req->args->may_block) {
626 			struct virtio_fs_req_work *w;
627 
628 			w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL);
629 			INIT_WORK(&w->done_work, virtio_fs_complete_req_work);
630 			w->fsvq = fsvq;
631 			w->req = req;
632 			schedule_work(&w->done_work);
633 		} else {
634 			virtio_fs_request_complete(req, fsvq);
635 		}
636 	}
637 }
638 
639 /* Virtqueue interrupt handler */
640 static void virtio_fs_vq_done(struct virtqueue *vq)
641 {
642 	struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
643 
644 	dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
645 
646 	schedule_work(&fsvq->done_work);
647 }
648 
649 static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name,
650 			      int vq_type)
651 {
652 	strscpy(fsvq->name, name, VQ_NAME_LEN);
653 	spin_lock_init(&fsvq->lock);
654 	INIT_LIST_HEAD(&fsvq->queued_reqs);
655 	INIT_LIST_HEAD(&fsvq->end_reqs);
656 	init_completion(&fsvq->in_flight_zero);
657 
658 	if (vq_type == VQ_REQUEST) {
659 		INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work);
660 		INIT_DELAYED_WORK(&fsvq->dispatch_work,
661 				  virtio_fs_request_dispatch_work);
662 	} else {
663 		INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work);
664 		INIT_DELAYED_WORK(&fsvq->dispatch_work,
665 				  virtio_fs_hiprio_dispatch_work);
666 	}
667 }
668 
669 /* Initialize virtqueues */
670 static int virtio_fs_setup_vqs(struct virtio_device *vdev,
671 			       struct virtio_fs *fs)
672 {
673 	struct virtqueue **vqs;
674 	vq_callback_t **callbacks;
675 	const char **names;
676 	unsigned int i;
677 	int ret = 0;
678 
679 	virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues,
680 			&fs->num_request_queues);
681 	if (fs->num_request_queues == 0)
682 		return -EINVAL;
683 
684 	fs->nvqs = VQ_REQUEST + fs->num_request_queues;
685 	fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
686 	if (!fs->vqs)
687 		return -ENOMEM;
688 
689 	vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
690 	callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
691 					GFP_KERNEL);
692 	names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL);
693 	if (!vqs || !callbacks || !names) {
694 		ret = -ENOMEM;
695 		goto out;
696 	}
697 
698 	/* Initialize the hiprio/forget request virtqueue */
699 	callbacks[VQ_HIPRIO] = virtio_fs_vq_done;
700 	virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO);
701 	names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
702 
703 	/* Initialize the requests virtqueues */
704 	for (i = VQ_REQUEST; i < fs->nvqs; i++) {
705 		char vq_name[VQ_NAME_LEN];
706 
707 		snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST);
708 		virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST);
709 		callbacks[i] = virtio_fs_vq_done;
710 		names[i] = fs->vqs[i].name;
711 	}
712 
713 	ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
714 	if (ret < 0)
715 		goto out;
716 
717 	for (i = 0; i < fs->nvqs; i++)
718 		fs->vqs[i].vq = vqs[i];
719 
720 	virtio_fs_start_all_queues(fs);
721 out:
722 	kfree(names);
723 	kfree(callbacks);
724 	kfree(vqs);
725 	if (ret)
726 		kfree(fs->vqs);
727 	return ret;
728 }
729 
730 /* Free virtqueues (device must already be reset) */
731 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev,
732 				  struct virtio_fs *fs)
733 {
734 	vdev->config->del_vqs(vdev);
735 }
736 
737 /* Map a window offset to a page frame number.  The window offset will have
738  * been produced by .iomap_begin(), which maps a file offset to a window
739  * offset.
740  */
741 static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
742 				    long nr_pages, void **kaddr, pfn_t *pfn)
743 {
744 	struct virtio_fs *fs = dax_get_private(dax_dev);
745 	phys_addr_t offset = PFN_PHYS(pgoff);
746 	size_t max_nr_pages = fs->window_len/PAGE_SIZE - pgoff;
747 
748 	if (kaddr)
749 		*kaddr = fs->window_kaddr + offset;
750 	if (pfn)
751 		*pfn = phys_to_pfn_t(fs->window_phys_addr + offset,
752 					PFN_DEV | PFN_MAP);
753 	return nr_pages > max_nr_pages ? max_nr_pages : nr_pages;
754 }
755 
756 static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
757 				     pgoff_t pgoff, size_t nr_pages)
758 {
759 	long rc;
760 	void *kaddr;
761 
762 	rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL);
763 	if (rc < 0)
764 		return rc;
765 	memset(kaddr, 0, nr_pages << PAGE_SHIFT);
766 	dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
767 	return 0;
768 }
769 
770 static const struct dax_operations virtio_fs_dax_ops = {
771 	.direct_access = virtio_fs_direct_access,
772 	.zero_page_range = virtio_fs_zero_page_range,
773 };
774 
775 static void virtio_fs_cleanup_dax(void *data)
776 {
777 	struct dax_device *dax_dev = data;
778 
779 	kill_dax(dax_dev);
780 	put_dax(dax_dev);
781 }
782 
783 static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs)
784 {
785 	struct virtio_shm_region cache_reg;
786 	struct dev_pagemap *pgmap;
787 	bool have_cache;
788 
789 	if (!IS_ENABLED(CONFIG_FUSE_DAX))
790 		return 0;
791 
792 	/* Get cache region */
793 	have_cache = virtio_get_shm_region(vdev, &cache_reg,
794 					   (u8)VIRTIO_FS_SHMCAP_ID_CACHE);
795 	if (!have_cache) {
796 		dev_notice(&vdev->dev, "%s: No cache capability\n", __func__);
797 		return 0;
798 	}
799 
800 	if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len,
801 				     dev_name(&vdev->dev))) {
802 		dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n",
803 			 cache_reg.addr, cache_reg.len);
804 		return -EBUSY;
805 	}
806 
807 	dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len,
808 		   cache_reg.addr);
809 
810 	pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL);
811 	if (!pgmap)
812 		return -ENOMEM;
813 
814 	pgmap->type = MEMORY_DEVICE_FS_DAX;
815 
816 	/* Ideally we would directly use the PCI BAR resource but
817 	 * devm_memremap_pages() wants its own copy in pgmap.  So
818 	 * initialize a struct resource from scratch (only the start
819 	 * and end fields will be used).
820 	 */
821 	pgmap->range = (struct range) {
822 		.start = (phys_addr_t) cache_reg.addr,
823 		.end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1,
824 	};
825 	pgmap->nr_range = 1;
826 
827 	fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap);
828 	if (IS_ERR(fs->window_kaddr))
829 		return PTR_ERR(fs->window_kaddr);
830 
831 	fs->window_phys_addr = (phys_addr_t) cache_reg.addr;
832 	fs->window_len = (phys_addr_t) cache_reg.len;
833 
834 	dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n",
835 		__func__, fs->window_kaddr, cache_reg.addr, cache_reg.len);
836 
837 	fs->dax_dev = alloc_dax(fs, &virtio_fs_dax_ops);
838 	if (IS_ERR(fs->dax_dev))
839 		return PTR_ERR(fs->dax_dev);
840 
841 	return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax,
842 					fs->dax_dev);
843 }
844 
845 static int virtio_fs_probe(struct virtio_device *vdev)
846 {
847 	struct virtio_fs *fs;
848 	int ret;
849 
850 	fs = kzalloc(sizeof(*fs), GFP_KERNEL);
851 	if (!fs)
852 		return -ENOMEM;
853 	kref_init(&fs->refcount);
854 	vdev->priv = fs;
855 
856 	ret = virtio_fs_read_tag(vdev, fs);
857 	if (ret < 0)
858 		goto out;
859 
860 	ret = virtio_fs_setup_vqs(vdev, fs);
861 	if (ret < 0)
862 		goto out;
863 
864 	/* TODO vq affinity */
865 
866 	ret = virtio_fs_setup_dax(vdev, fs);
867 	if (ret < 0)
868 		goto out_vqs;
869 
870 	/* Bring the device online in case the filesystem is mounted and
871 	 * requests need to be sent before we return.
872 	 */
873 	virtio_device_ready(vdev);
874 
875 	ret = virtio_fs_add_instance(fs);
876 	if (ret < 0)
877 		goto out_vqs;
878 
879 	return 0;
880 
881 out_vqs:
882 	vdev->config->reset(vdev);
883 	virtio_fs_cleanup_vqs(vdev, fs);
884 	kfree(fs->vqs);
885 
886 out:
887 	vdev->priv = NULL;
888 	kfree(fs);
889 	return ret;
890 }
891 
892 static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
893 {
894 	struct virtio_fs_vq *fsvq;
895 	int i;
896 
897 	for (i = 0; i < fs->nvqs; i++) {
898 		fsvq = &fs->vqs[i];
899 		spin_lock(&fsvq->lock);
900 		fsvq->connected = false;
901 		spin_unlock(&fsvq->lock);
902 	}
903 }
904 
905 static void virtio_fs_remove(struct virtio_device *vdev)
906 {
907 	struct virtio_fs *fs = vdev->priv;
908 
909 	mutex_lock(&virtio_fs_mutex);
910 	/* This device is going away. No one should get new reference */
911 	list_del_init(&fs->list);
912 	virtio_fs_stop_all_queues(fs);
913 	virtio_fs_drain_all_queues_locked(fs);
914 	vdev->config->reset(vdev);
915 	virtio_fs_cleanup_vqs(vdev, fs);
916 
917 	vdev->priv = NULL;
918 	/* Put device reference on virtio_fs object */
919 	virtio_fs_put(fs);
920 	mutex_unlock(&virtio_fs_mutex);
921 }
922 
923 #ifdef CONFIG_PM_SLEEP
924 static int virtio_fs_freeze(struct virtio_device *vdev)
925 {
926 	/* TODO need to save state here */
927 	pr_warn("virtio-fs: suspend/resume not yet supported\n");
928 	return -EOPNOTSUPP;
929 }
930 
931 static int virtio_fs_restore(struct virtio_device *vdev)
932 {
933 	 /* TODO need to restore state here */
934 	return 0;
935 }
936 #endif /* CONFIG_PM_SLEEP */
937 
938 static const struct virtio_device_id id_table[] = {
939 	{ VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
940 	{},
941 };
942 
943 static const unsigned int feature_table[] = {};
944 
945 static struct virtio_driver virtio_fs_driver = {
946 	.driver.name		= KBUILD_MODNAME,
947 	.driver.owner		= THIS_MODULE,
948 	.id_table		= id_table,
949 	.feature_table		= feature_table,
950 	.feature_table_size	= ARRAY_SIZE(feature_table),
951 	.probe			= virtio_fs_probe,
952 	.remove			= virtio_fs_remove,
953 #ifdef CONFIG_PM_SLEEP
954 	.freeze			= virtio_fs_freeze,
955 	.restore		= virtio_fs_restore,
956 #endif
957 };
958 
959 static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq)
960 __releases(fiq->lock)
961 {
962 	struct fuse_forget_link *link;
963 	struct virtio_fs_forget *forget;
964 	struct virtio_fs_forget_req *req;
965 	struct virtio_fs *fs;
966 	struct virtio_fs_vq *fsvq;
967 	u64 unique;
968 
969 	link = fuse_dequeue_forget(fiq, 1, NULL);
970 	unique = fuse_get_unique(fiq);
971 
972 	fs = fiq->priv;
973 	fsvq = &fs->vqs[VQ_HIPRIO];
974 	spin_unlock(&fiq->lock);
975 
976 	/* Allocate a buffer for the request */
977 	forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
978 	req = &forget->req;
979 
980 	req->ih = (struct fuse_in_header){
981 		.opcode = FUSE_FORGET,
982 		.nodeid = link->forget_one.nodeid,
983 		.unique = unique,
984 		.len = sizeof(*req),
985 	};
986 	req->arg = (struct fuse_forget_in){
987 		.nlookup = link->forget_one.nlookup,
988 	};
989 
990 	send_forget_request(fsvq, forget, false);
991 	kfree(link);
992 }
993 
994 static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq)
995 __releases(fiq->lock)
996 {
997 	/*
998 	 * TODO interrupts.
999 	 *
1000 	 * Normal fs operations on a local filesystems aren't interruptible.
1001 	 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
1002 	 * with shared lock between host and guest.
1003 	 */
1004 	spin_unlock(&fiq->lock);
1005 }
1006 
1007 /* Count number of scatter-gather elements required */
1008 static unsigned int sg_count_fuse_pages(struct fuse_page_desc *page_descs,
1009 				       unsigned int num_pages,
1010 				       unsigned int total_len)
1011 {
1012 	unsigned int i;
1013 	unsigned int this_len;
1014 
1015 	for (i = 0; i < num_pages && total_len; i++) {
1016 		this_len =  min(page_descs[i].length, total_len);
1017 		total_len -= this_len;
1018 	}
1019 
1020 	return i;
1021 }
1022 
1023 /* Return the number of scatter-gather list elements required */
1024 static unsigned int sg_count_fuse_req(struct fuse_req *req)
1025 {
1026 	struct fuse_args *args = req->args;
1027 	struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
1028 	unsigned int size, total_sgs = 1 /* fuse_in_header */;
1029 
1030 	if (args->in_numargs - args->in_pages)
1031 		total_sgs += 1;
1032 
1033 	if (args->in_pages) {
1034 		size = args->in_args[args->in_numargs - 1].size;
1035 		total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
1036 						 size);
1037 	}
1038 
1039 	if (!test_bit(FR_ISREPLY, &req->flags))
1040 		return total_sgs;
1041 
1042 	total_sgs += 1 /* fuse_out_header */;
1043 
1044 	if (args->out_numargs - args->out_pages)
1045 		total_sgs += 1;
1046 
1047 	if (args->out_pages) {
1048 		size = args->out_args[args->out_numargs - 1].size;
1049 		total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
1050 						 size);
1051 	}
1052 
1053 	return total_sgs;
1054 }
1055 
1056 /* Add pages to scatter-gather list and return number of elements used */
1057 static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
1058 				       struct page **pages,
1059 				       struct fuse_page_desc *page_descs,
1060 				       unsigned int num_pages,
1061 				       unsigned int total_len)
1062 {
1063 	unsigned int i;
1064 	unsigned int this_len;
1065 
1066 	for (i = 0; i < num_pages && total_len; i++) {
1067 		sg_init_table(&sg[i], 1);
1068 		this_len =  min(page_descs[i].length, total_len);
1069 		sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
1070 		total_len -= this_len;
1071 	}
1072 
1073 	return i;
1074 }
1075 
1076 /* Add args to scatter-gather list and return number of elements used */
1077 static unsigned int sg_init_fuse_args(struct scatterlist *sg,
1078 				      struct fuse_req *req,
1079 				      struct fuse_arg *args,
1080 				      unsigned int numargs,
1081 				      bool argpages,
1082 				      void *argbuf,
1083 				      unsigned int *len_used)
1084 {
1085 	struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
1086 	unsigned int total_sgs = 0;
1087 	unsigned int len;
1088 
1089 	len = fuse_len_args(numargs - argpages, args);
1090 	if (len)
1091 		sg_init_one(&sg[total_sgs++], argbuf, len);
1092 
1093 	if (argpages)
1094 		total_sgs += sg_init_fuse_pages(&sg[total_sgs],
1095 						ap->pages, ap->descs,
1096 						ap->num_pages,
1097 						args[numargs - 1].size);
1098 
1099 	if (len_used)
1100 		*len_used = len;
1101 
1102 	return total_sgs;
1103 }
1104 
1105 /* Add a request to a virtqueue and kick the device */
1106 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
1107 				 struct fuse_req *req, bool in_flight)
1108 {
1109 	/* requests need at least 4 elements */
1110 	struct scatterlist *stack_sgs[6];
1111 	struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
1112 	struct scatterlist **sgs = stack_sgs;
1113 	struct scatterlist *sg = stack_sg;
1114 	struct virtqueue *vq;
1115 	struct fuse_args *args = req->args;
1116 	unsigned int argbuf_used = 0;
1117 	unsigned int out_sgs = 0;
1118 	unsigned int in_sgs = 0;
1119 	unsigned int total_sgs;
1120 	unsigned int i;
1121 	int ret;
1122 	bool notify;
1123 	struct fuse_pqueue *fpq;
1124 
1125 	/* Does the sglist fit on the stack? */
1126 	total_sgs = sg_count_fuse_req(req);
1127 	if (total_sgs > ARRAY_SIZE(stack_sgs)) {
1128 		sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
1129 		sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
1130 		if (!sgs || !sg) {
1131 			ret = -ENOMEM;
1132 			goto out;
1133 		}
1134 	}
1135 
1136 	/* Use a bounce buffer since stack args cannot be mapped */
1137 	ret = copy_args_to_argbuf(req);
1138 	if (ret < 0)
1139 		goto out;
1140 
1141 	/* Request elements */
1142 	sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
1143 	out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
1144 				     (struct fuse_arg *)args->in_args,
1145 				     args->in_numargs, args->in_pages,
1146 				     req->argbuf, &argbuf_used);
1147 
1148 	/* Reply elements */
1149 	if (test_bit(FR_ISREPLY, &req->flags)) {
1150 		sg_init_one(&sg[out_sgs + in_sgs++],
1151 			    &req->out.h, sizeof(req->out.h));
1152 		in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
1153 					    args->out_args, args->out_numargs,
1154 					    args->out_pages,
1155 					    req->argbuf + argbuf_used, NULL);
1156 	}
1157 
1158 	WARN_ON(out_sgs + in_sgs != total_sgs);
1159 
1160 	for (i = 0; i < total_sgs; i++)
1161 		sgs[i] = &sg[i];
1162 
1163 	spin_lock(&fsvq->lock);
1164 
1165 	if (!fsvq->connected) {
1166 		spin_unlock(&fsvq->lock);
1167 		ret = -ENOTCONN;
1168 		goto out;
1169 	}
1170 
1171 	vq = fsvq->vq;
1172 	ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
1173 	if (ret < 0) {
1174 		spin_unlock(&fsvq->lock);
1175 		goto out;
1176 	}
1177 
1178 	/* Request successfully sent. */
1179 	fpq = &fsvq->fud->pq;
1180 	spin_lock(&fpq->lock);
1181 	list_add_tail(&req->list, fpq->processing);
1182 	spin_unlock(&fpq->lock);
1183 	set_bit(FR_SENT, &req->flags);
1184 	/* matches barrier in request_wait_answer() */
1185 	smp_mb__after_atomic();
1186 
1187 	if (!in_flight)
1188 		inc_in_flight_req(fsvq);
1189 	notify = virtqueue_kick_prepare(vq);
1190 
1191 	spin_unlock(&fsvq->lock);
1192 
1193 	if (notify)
1194 		virtqueue_notify(vq);
1195 
1196 out:
1197 	if (ret < 0 && req->argbuf) {
1198 		kfree(req->argbuf);
1199 		req->argbuf = NULL;
1200 	}
1201 	if (sgs != stack_sgs) {
1202 		kfree(sgs);
1203 		kfree(sg);
1204 	}
1205 
1206 	return ret;
1207 }
1208 
1209 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
1210 __releases(fiq->lock)
1211 {
1212 	unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
1213 	struct virtio_fs *fs;
1214 	struct fuse_req *req;
1215 	struct virtio_fs_vq *fsvq;
1216 	int ret;
1217 
1218 	WARN_ON(list_empty(&fiq->pending));
1219 	req = list_last_entry(&fiq->pending, struct fuse_req, list);
1220 	clear_bit(FR_PENDING, &req->flags);
1221 	list_del_init(&req->list);
1222 	WARN_ON(!list_empty(&fiq->pending));
1223 	spin_unlock(&fiq->lock);
1224 
1225 	fs = fiq->priv;
1226 
1227 	pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
1228 		  __func__, req->in.h.opcode, req->in.h.unique,
1229 		 req->in.h.nodeid, req->in.h.len,
1230 		 fuse_len_args(req->args->out_numargs, req->args->out_args));
1231 
1232 	fsvq = &fs->vqs[queue_id];
1233 	ret = virtio_fs_enqueue_req(fsvq, req, false);
1234 	if (ret < 0) {
1235 		if (ret == -ENOMEM || ret == -ENOSPC) {
1236 			/*
1237 			 * Virtqueue full. Retry submission from worker
1238 			 * context as we might be holding fc->bg_lock.
1239 			 */
1240 			spin_lock(&fsvq->lock);
1241 			list_add_tail(&req->list, &fsvq->queued_reqs);
1242 			inc_in_flight_req(fsvq);
1243 			schedule_delayed_work(&fsvq->dispatch_work,
1244 						msecs_to_jiffies(1));
1245 			spin_unlock(&fsvq->lock);
1246 			return;
1247 		}
1248 		req->out.h.error = ret;
1249 		pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
1250 
1251 		/* Can't end request in submission context. Use a worker */
1252 		spin_lock(&fsvq->lock);
1253 		list_add_tail(&req->list, &fsvq->end_reqs);
1254 		schedule_delayed_work(&fsvq->dispatch_work, 0);
1255 		spin_unlock(&fsvq->lock);
1256 		return;
1257 	}
1258 }
1259 
1260 static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
1261 	.wake_forget_and_unlock		= virtio_fs_wake_forget_and_unlock,
1262 	.wake_interrupt_and_unlock	= virtio_fs_wake_interrupt_and_unlock,
1263 	.wake_pending_and_unlock	= virtio_fs_wake_pending_and_unlock,
1264 	.release			= virtio_fs_fiq_release,
1265 };
1266 
1267 static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
1268 {
1269 	ctx->rootmode = S_IFDIR;
1270 	ctx->default_permissions = 1;
1271 	ctx->allow_other = 1;
1272 	ctx->max_read = UINT_MAX;
1273 	ctx->blksize = 512;
1274 	ctx->destroy = true;
1275 	ctx->no_control = true;
1276 	ctx->no_force_umount = true;
1277 }
1278 
1279 static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
1280 {
1281 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1282 	struct fuse_conn *fc = fm->fc;
1283 	struct virtio_fs *fs = fc->iq.priv;
1284 	struct fuse_fs_context *ctx = fsc->fs_private;
1285 	unsigned int i;
1286 	int err;
1287 
1288 	virtio_fs_ctx_set_defaults(ctx);
1289 	mutex_lock(&virtio_fs_mutex);
1290 
1291 	/* After holding mutex, make sure virtiofs device is still there.
1292 	 * Though we are holding a reference to it, drive ->remove might
1293 	 * still have cleaned up virtual queues. In that case bail out.
1294 	 */
1295 	err = -EINVAL;
1296 	if (list_empty(&fs->list)) {
1297 		pr_info("virtio-fs: tag <%s> not found\n", fs->tag);
1298 		goto err;
1299 	}
1300 
1301 	err = -ENOMEM;
1302 	/* Allocate fuse_dev for hiprio and notification queues */
1303 	for (i = 0; i < fs->nvqs; i++) {
1304 		struct virtio_fs_vq *fsvq = &fs->vqs[i];
1305 
1306 		fsvq->fud = fuse_dev_alloc();
1307 		if (!fsvq->fud)
1308 			goto err_free_fuse_devs;
1309 	}
1310 
1311 	/* virtiofs allocates and installs its own fuse devices */
1312 	ctx->fudptr = NULL;
1313 	if (ctx->dax) {
1314 		if (!fs->dax_dev) {
1315 			err = -EINVAL;
1316 			pr_err("virtio-fs: dax can't be enabled as filesystem"
1317 			       " device does not support it.\n");
1318 			goto err_free_fuse_devs;
1319 		}
1320 		ctx->dax_dev = fs->dax_dev;
1321 	}
1322 	err = fuse_fill_super_common(sb, ctx);
1323 	if (err < 0)
1324 		goto err_free_fuse_devs;
1325 
1326 	for (i = 0; i < fs->nvqs; i++) {
1327 		struct virtio_fs_vq *fsvq = &fs->vqs[i];
1328 
1329 		fuse_dev_install(fsvq->fud, fc);
1330 	}
1331 
1332 	/* Previous unmount will stop all queues. Start these again */
1333 	virtio_fs_start_all_queues(fs);
1334 	fuse_send_init(fm);
1335 	mutex_unlock(&virtio_fs_mutex);
1336 	return 0;
1337 
1338 err_free_fuse_devs:
1339 	virtio_fs_free_devs(fs);
1340 err:
1341 	mutex_unlock(&virtio_fs_mutex);
1342 	return err;
1343 }
1344 
1345 static void virtio_fs_conn_destroy(struct fuse_mount *fm)
1346 {
1347 	struct fuse_conn *fc = fm->fc;
1348 	struct virtio_fs *vfs = fc->iq.priv;
1349 	struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO];
1350 
1351 	/* Stop dax worker. Soon evict_inodes() will be called which
1352 	 * will free all memory ranges belonging to all inodes.
1353 	 */
1354 	if (IS_ENABLED(CONFIG_FUSE_DAX))
1355 		fuse_dax_cancel_work(fc);
1356 
1357 	/* Stop forget queue. Soon destroy will be sent */
1358 	spin_lock(&fsvq->lock);
1359 	fsvq->connected = false;
1360 	spin_unlock(&fsvq->lock);
1361 	virtio_fs_drain_all_queues(vfs);
1362 
1363 	fuse_conn_destroy(fm);
1364 
1365 	/* fuse_conn_destroy() must have sent destroy. Stop all queues
1366 	 * and drain one more time and free fuse devices. Freeing fuse
1367 	 * devices will drop their reference on fuse_conn and that in
1368 	 * turn will drop its reference on virtio_fs object.
1369 	 */
1370 	virtio_fs_stop_all_queues(vfs);
1371 	virtio_fs_drain_all_queues(vfs);
1372 	virtio_fs_free_devs(vfs);
1373 }
1374 
1375 static void virtio_kill_sb(struct super_block *sb)
1376 {
1377 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1378 	bool last;
1379 
1380 	/* If mount failed, we can still be called without any fc */
1381 	if (sb->s_root) {
1382 		last = fuse_mount_remove(fm);
1383 		if (last)
1384 			virtio_fs_conn_destroy(fm);
1385 	}
1386 	kill_anon_super(sb);
1387 	fuse_mount_destroy(fm);
1388 }
1389 
1390 static int virtio_fs_test_super(struct super_block *sb,
1391 				struct fs_context *fsc)
1392 {
1393 	struct fuse_mount *fsc_fm = fsc->s_fs_info;
1394 	struct fuse_mount *sb_fm = get_fuse_mount_super(sb);
1395 
1396 	return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv;
1397 }
1398 
1399 static int virtio_fs_get_tree(struct fs_context *fsc)
1400 {
1401 	struct virtio_fs *fs;
1402 	struct super_block *sb;
1403 	struct fuse_conn *fc = NULL;
1404 	struct fuse_mount *fm;
1405 	unsigned int virtqueue_size;
1406 	int err = -EIO;
1407 
1408 	/* This gets a reference on virtio_fs object. This ptr gets installed
1409 	 * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1410 	 * to drop the reference to this object.
1411 	 */
1412 	fs = virtio_fs_find_instance(fsc->source);
1413 	if (!fs) {
1414 		pr_info("virtio-fs: tag <%s> not found\n", fsc->source);
1415 		return -EINVAL;
1416 	}
1417 
1418 	virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq);
1419 	if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD))
1420 		goto out_err;
1421 
1422 	err = -ENOMEM;
1423 	fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
1424 	if (!fc)
1425 		goto out_err;
1426 
1427 	fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1428 	if (!fm)
1429 		goto out_err;
1430 
1431 	fuse_conn_init(fc, fm, fsc->user_ns, &virtio_fs_fiq_ops, fs);
1432 	fc->release = fuse_free_conn;
1433 	fc->delete_stale = true;
1434 	fc->auto_submounts = true;
1435 	fc->sync_fs = true;
1436 
1437 	/* Tell FUSE to split requests that exceed the virtqueue's size */
1438 	fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit,
1439 				    virtqueue_size - FUSE_HEADER_OVERHEAD);
1440 
1441 	fsc->s_fs_info = fm;
1442 	sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc);
1443 	if (fsc->s_fs_info)
1444 		fuse_mount_destroy(fm);
1445 	if (IS_ERR(sb))
1446 		return PTR_ERR(sb);
1447 
1448 	if (!sb->s_root) {
1449 		err = virtio_fs_fill_super(sb, fsc);
1450 		if (err) {
1451 			deactivate_locked_super(sb);
1452 			return err;
1453 		}
1454 
1455 		sb->s_flags |= SB_ACTIVE;
1456 	}
1457 
1458 	WARN_ON(fsc->root);
1459 	fsc->root = dget(sb->s_root);
1460 	return 0;
1461 
1462 out_err:
1463 	kfree(fc);
1464 	mutex_lock(&virtio_fs_mutex);
1465 	virtio_fs_put(fs);
1466 	mutex_unlock(&virtio_fs_mutex);
1467 	return err;
1468 }
1469 
1470 static const struct fs_context_operations virtio_fs_context_ops = {
1471 	.free		= virtio_fs_free_fsc,
1472 	.parse_param	= virtio_fs_parse_param,
1473 	.get_tree	= virtio_fs_get_tree,
1474 };
1475 
1476 static int virtio_fs_init_fs_context(struct fs_context *fsc)
1477 {
1478 	struct fuse_fs_context *ctx;
1479 
1480 	if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT)
1481 		return fuse_init_fs_context_submount(fsc);
1482 
1483 	ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1484 	if (!ctx)
1485 		return -ENOMEM;
1486 	fsc->fs_private = ctx;
1487 	fsc->ops = &virtio_fs_context_ops;
1488 	return 0;
1489 }
1490 
1491 static struct file_system_type virtio_fs_type = {
1492 	.owner		= THIS_MODULE,
1493 	.name		= "virtiofs",
1494 	.init_fs_context = virtio_fs_init_fs_context,
1495 	.kill_sb	= virtio_kill_sb,
1496 };
1497 
1498 static int __init virtio_fs_init(void)
1499 {
1500 	int ret;
1501 
1502 	ret = register_virtio_driver(&virtio_fs_driver);
1503 	if (ret < 0)
1504 		return ret;
1505 
1506 	ret = register_filesystem(&virtio_fs_type);
1507 	if (ret < 0) {
1508 		unregister_virtio_driver(&virtio_fs_driver);
1509 		return ret;
1510 	}
1511 
1512 	return 0;
1513 }
1514 module_init(virtio_fs_init);
1515 
1516 static void __exit virtio_fs_exit(void)
1517 {
1518 	unregister_filesystem(&virtio_fs_type);
1519 	unregister_virtio_driver(&virtio_fs_driver);
1520 }
1521 module_exit(virtio_fs_exit);
1522 
1523 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1524 MODULE_DESCRIPTION("Virtio Filesystem");
1525 MODULE_LICENSE("GPL");
1526 MODULE_ALIAS_FS(KBUILD_MODNAME);
1527 MODULE_DEVICE_TABLE(virtio, id_table);
1528