1 // SPDX-License-Identifier: GPL-2.0
3 * virtio-fs: Virtio Filesystem
4 * Copyright (C) 2018 Red Hat, Inc.
8 #include <linux/module.h>
9 #include <linux/virtio.h>
10 #include <linux/virtio_fs.h>
11 #include <linux/delay.h>
12 #include <linux/fs_context.h>
13 #include <linux/highmem.h>
16 /* List of virtio-fs device instances and a lock for the list. Also provides
17 * mutual exclusion in device removal and mounting path
19 static DEFINE_MUTEX(virtio_fs_mutex);
20 static LIST_HEAD(virtio_fs_instances);
27 /* Per-virtqueue state */
30 struct virtqueue *vq; /* protected by ->lock */
31 struct work_struct done_work;
32 struct list_head queued_reqs;
33 struct list_head end_reqs; /* End these requests */
34 struct delayed_work dispatch_work;
39 } ____cacheline_aligned_in_smp;
41 /* A virtio-fs device instance */
44 struct list_head list; /* on virtio_fs_instances */
46 struct virtio_fs_vq *vqs;
47 unsigned int nvqs; /* number of virtqueues */
48 unsigned int num_request_queues; /* number of request queues */
51 struct virtio_fs_forget {
52 struct fuse_in_header ih;
53 struct fuse_forget_in arg;
54 /* This request can be temporarily queued on virt queue */
55 struct list_head list;
58 struct virtio_fs_req_work {
60 struct virtio_fs_vq *fsvq;
61 struct work_struct done_work;
64 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
65 struct fuse_req *req, bool in_flight);
67 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
69 struct virtio_fs *fs = vq->vdev->priv;
71 return &fs->vqs[vq->index];
74 static inline struct fuse_pqueue *vq_to_fpq(struct virtqueue *vq)
76 return &vq_to_fsvq(vq)->fud->pq;
79 /* Should be called with fsvq->lock held. */
80 static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq)
85 /* Should be called with fsvq->lock held. */
86 static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq)
88 WARN_ON(fsvq->in_flight <= 0);
92 static void release_virtio_fs_obj(struct kref *ref)
94 struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount);
100 /* Make sure virtiofs_mutex is held */
101 static void virtio_fs_put(struct virtio_fs *fs)
103 kref_put(&fs->refcount, release_virtio_fs_obj);
106 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
108 struct virtio_fs *vfs = fiq->priv;
110 mutex_lock(&virtio_fs_mutex);
112 mutex_unlock(&virtio_fs_mutex);
115 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
117 WARN_ON(fsvq->in_flight < 0);
119 /* Wait for in flight requests to finish.*/
121 spin_lock(&fsvq->lock);
122 if (!fsvq->in_flight) {
123 spin_unlock(&fsvq->lock);
126 spin_unlock(&fsvq->lock);
127 /* TODO use completion instead of timeout */
128 usleep_range(1000, 2000);
131 flush_work(&fsvq->done_work);
132 flush_delayed_work(&fsvq->dispatch_work);
135 static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
137 struct virtio_fs_vq *fsvq;
140 for (i = 0; i < fs->nvqs; i++) {
142 virtio_fs_drain_queue(fsvq);
146 static void virtio_fs_start_all_queues(struct virtio_fs *fs)
148 struct virtio_fs_vq *fsvq;
151 for (i = 0; i < fs->nvqs; i++) {
153 spin_lock(&fsvq->lock);
154 fsvq->connected = true;
155 spin_unlock(&fsvq->lock);
159 /* Add a new instance to the list or return -EEXIST if tag name exists*/
160 static int virtio_fs_add_instance(struct virtio_fs *fs)
162 struct virtio_fs *fs2;
163 bool duplicate = false;
165 mutex_lock(&virtio_fs_mutex);
167 list_for_each_entry(fs2, &virtio_fs_instances, list) {
168 if (strcmp(fs->tag, fs2->tag) == 0)
173 list_add_tail(&fs->list, &virtio_fs_instances);
175 mutex_unlock(&virtio_fs_mutex);
182 /* Return the virtio_fs with a given tag, or NULL */
183 static struct virtio_fs *virtio_fs_find_instance(const char *tag)
185 struct virtio_fs *fs;
187 mutex_lock(&virtio_fs_mutex);
189 list_for_each_entry(fs, &virtio_fs_instances, list) {
190 if (strcmp(fs->tag, tag) == 0) {
191 kref_get(&fs->refcount);
196 fs = NULL; /* not found */
199 mutex_unlock(&virtio_fs_mutex);
204 static void virtio_fs_free_devs(struct virtio_fs *fs)
208 for (i = 0; i < fs->nvqs; i++) {
209 struct virtio_fs_vq *fsvq = &fs->vqs[i];
214 fuse_dev_free(fsvq->fud);
219 /* Read filesystem name from virtio config into fs->tag (must kfree()). */
220 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
222 char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
226 virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
227 &tag_buf, sizeof(tag_buf));
228 end = memchr(tag_buf, '\0', sizeof(tag_buf));
230 return -EINVAL; /* empty tag */
232 end = &tag_buf[sizeof(tag_buf)];
235 fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
238 memcpy(fs->tag, tag_buf, len);
243 /* Work function for hiprio completion */
244 static void virtio_fs_hiprio_done_work(struct work_struct *work)
246 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
248 struct virtqueue *vq = fsvq->vq;
250 /* Free completed FUSE_FORGET requests */
251 spin_lock(&fsvq->lock);
256 virtqueue_disable_cb(vq);
258 while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
260 dec_in_flight_req(fsvq);
262 } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
263 spin_unlock(&fsvq->lock);
266 static void virtio_fs_request_dispatch_work(struct work_struct *work)
268 struct fuse_req *req;
269 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
271 struct fuse_conn *fc = fsvq->fud->fc;
274 pr_debug("virtio-fs: worker %s called.\n", __func__);
276 spin_lock(&fsvq->lock);
277 req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
280 spin_unlock(&fsvq->lock);
284 list_del_init(&req->list);
285 spin_unlock(&fsvq->lock);
286 fuse_request_end(fc, req);
289 /* Dispatch pending requests */
291 spin_lock(&fsvq->lock);
292 req = list_first_entry_or_null(&fsvq->queued_reqs,
293 struct fuse_req, list);
295 spin_unlock(&fsvq->lock);
298 list_del_init(&req->list);
299 spin_unlock(&fsvq->lock);
301 ret = virtio_fs_enqueue_req(fsvq, req, true);
303 if (ret == -ENOMEM || ret == -ENOSPC) {
304 spin_lock(&fsvq->lock);
305 list_add_tail(&req->list, &fsvq->queued_reqs);
306 schedule_delayed_work(&fsvq->dispatch_work,
307 msecs_to_jiffies(1));
308 spin_unlock(&fsvq->lock);
311 req->out.h.error = ret;
312 spin_lock(&fsvq->lock);
313 dec_in_flight_req(fsvq);
314 spin_unlock(&fsvq->lock);
315 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n",
317 fuse_request_end(fc, req);
322 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
324 struct virtio_fs_forget *forget;
325 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
327 struct virtqueue *vq = fsvq->vq;
328 struct scatterlist sg;
329 struct scatterlist *sgs[] = {&sg};
333 pr_debug("virtio-fs: worker %s called.\n", __func__);
335 spin_lock(&fsvq->lock);
336 forget = list_first_entry_or_null(&fsvq->queued_reqs,
337 struct virtio_fs_forget, list);
339 spin_unlock(&fsvq->lock);
343 list_del(&forget->list);
344 if (!fsvq->connected) {
345 dec_in_flight_req(fsvq);
346 spin_unlock(&fsvq->lock);
351 sg_init_one(&sg, forget, sizeof(*forget));
353 /* Enqueue the request */
354 dev_dbg(&vq->vdev->dev, "%s\n", __func__);
355 ret = virtqueue_add_sgs(vq, sgs, 1, 0, forget, GFP_ATOMIC);
357 if (ret == -ENOMEM || ret == -ENOSPC) {
358 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
360 list_add_tail(&forget->list,
362 schedule_delayed_work(&fsvq->dispatch_work,
363 msecs_to_jiffies(1));
365 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
367 dec_in_flight_req(fsvq);
370 spin_unlock(&fsvq->lock);
374 notify = virtqueue_kick_prepare(vq);
375 spin_unlock(&fsvq->lock);
378 virtqueue_notify(vq);
379 pr_debug("virtio-fs: worker %s dispatched one forget request.\n",
384 /* Allocate and copy args into req->argbuf */
385 static int copy_args_to_argbuf(struct fuse_req *req)
387 struct fuse_args *args = req->args;
388 unsigned int offset = 0;
390 unsigned int num_out;
394 num_in = args->in_numargs - args->in_pages;
395 num_out = args->out_numargs - args->out_pages;
396 len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
397 fuse_len_args(num_out, args->out_args);
399 req->argbuf = kmalloc(len, GFP_ATOMIC);
403 for (i = 0; i < num_in; i++) {
404 memcpy(req->argbuf + offset,
405 args->in_args[i].value,
406 args->in_args[i].size);
407 offset += args->in_args[i].size;
413 /* Copy args out of and free req->argbuf */
414 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
416 unsigned int remaining;
419 unsigned int num_out;
422 remaining = req->out.h.len - sizeof(req->out.h);
423 num_in = args->in_numargs - args->in_pages;
424 num_out = args->out_numargs - args->out_pages;
425 offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
427 for (i = 0; i < num_out; i++) {
428 unsigned int argsize = args->out_args[i].size;
430 if (args->out_argvar &&
431 i == args->out_numargs - 1 &&
432 argsize > remaining) {
436 memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
439 if (i != args->out_numargs - 1)
440 remaining -= argsize;
443 /* Store the actual size of the variable-length arg */
444 if (args->out_argvar)
445 args->out_args[args->out_numargs - 1].size = remaining;
451 /* Work function for request completion */
452 static void virtio_fs_request_complete(struct fuse_req *req,
453 struct virtio_fs_vq *fsvq)
455 struct fuse_pqueue *fpq = &fsvq->fud->pq;
456 struct fuse_conn *fc = fsvq->fud->fc;
457 struct fuse_args *args;
458 struct fuse_args_pages *ap;
459 unsigned int len, i, thislen;
463 * TODO verify that server properly follows FUSE protocol
467 copy_args_from_argbuf(args, req);
469 if (args->out_pages && args->page_zeroing) {
470 len = args->out_args[args->out_numargs - 1].size;
471 ap = container_of(args, typeof(*ap), args);
472 for (i = 0; i < ap->num_pages; i++) {
473 thislen = ap->descs[i].length;
475 WARN_ON(ap->descs[i].offset);
477 zero_user_segment(page, len, thislen);
485 spin_lock(&fpq->lock);
486 clear_bit(FR_SENT, &req->flags);
487 spin_unlock(&fpq->lock);
489 fuse_request_end(fc, req);
490 spin_lock(&fsvq->lock);
491 dec_in_flight_req(fsvq);
492 spin_unlock(&fsvq->lock);
495 static void virtio_fs_complete_req_work(struct work_struct *work)
497 struct virtio_fs_req_work *w =
498 container_of(work, typeof(*w), done_work);
500 virtio_fs_request_complete(w->req, w->fsvq);
504 static void virtio_fs_requests_done_work(struct work_struct *work)
506 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
508 struct fuse_pqueue *fpq = &fsvq->fud->pq;
509 struct virtqueue *vq = fsvq->vq;
510 struct fuse_req *req;
511 struct fuse_req *next;
515 /* Collect completed requests off the virtqueue */
516 spin_lock(&fsvq->lock);
518 virtqueue_disable_cb(vq);
520 while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
521 spin_lock(&fpq->lock);
522 list_move_tail(&req->list, &reqs);
523 spin_unlock(&fpq->lock);
525 } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
526 spin_unlock(&fsvq->lock);
529 list_for_each_entry_safe(req, next, &reqs, list) {
530 list_del_init(&req->list);
532 /* blocking async request completes in a worker context */
533 if (req->args->may_block) {
534 struct virtio_fs_req_work *w;
536 w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL);
537 INIT_WORK(&w->done_work, virtio_fs_complete_req_work);
540 schedule_work(&w->done_work);
542 virtio_fs_request_complete(req, fsvq);
547 /* Virtqueue interrupt handler */
548 static void virtio_fs_vq_done(struct virtqueue *vq)
550 struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
552 dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
554 schedule_work(&fsvq->done_work);
557 /* Initialize virtqueues */
558 static int virtio_fs_setup_vqs(struct virtio_device *vdev,
559 struct virtio_fs *fs)
561 struct virtqueue **vqs;
562 vq_callback_t **callbacks;
567 virtio_cread(vdev, struct virtio_fs_config, num_request_queues,
568 &fs->num_request_queues);
569 if (fs->num_request_queues == 0)
572 fs->nvqs = 1 + fs->num_request_queues;
573 fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
577 vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
578 callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
580 names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL);
581 if (!vqs || !callbacks || !names) {
586 callbacks[VQ_HIPRIO] = virtio_fs_vq_done;
587 snprintf(fs->vqs[VQ_HIPRIO].name, sizeof(fs->vqs[VQ_HIPRIO].name),
589 names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
590 INIT_WORK(&fs->vqs[VQ_HIPRIO].done_work, virtio_fs_hiprio_done_work);
591 INIT_LIST_HEAD(&fs->vqs[VQ_HIPRIO].queued_reqs);
592 INIT_LIST_HEAD(&fs->vqs[VQ_HIPRIO].end_reqs);
593 INIT_DELAYED_WORK(&fs->vqs[VQ_HIPRIO].dispatch_work,
594 virtio_fs_hiprio_dispatch_work);
595 spin_lock_init(&fs->vqs[VQ_HIPRIO].lock);
597 /* Initialize the requests virtqueues */
598 for (i = VQ_REQUEST; i < fs->nvqs; i++) {
599 spin_lock_init(&fs->vqs[i].lock);
600 INIT_WORK(&fs->vqs[i].done_work, virtio_fs_requests_done_work);
601 INIT_DELAYED_WORK(&fs->vqs[i].dispatch_work,
602 virtio_fs_request_dispatch_work);
603 INIT_LIST_HEAD(&fs->vqs[i].queued_reqs);
604 INIT_LIST_HEAD(&fs->vqs[i].end_reqs);
605 snprintf(fs->vqs[i].name, sizeof(fs->vqs[i].name),
606 "requests.%u", i - VQ_REQUEST);
607 callbacks[i] = virtio_fs_vq_done;
608 names[i] = fs->vqs[i].name;
611 ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
615 for (i = 0; i < fs->nvqs; i++)
616 fs->vqs[i].vq = vqs[i];
618 virtio_fs_start_all_queues(fs);
628 /* Free virtqueues (device must already be reset) */
629 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev,
630 struct virtio_fs *fs)
632 vdev->config->del_vqs(vdev);
635 static int virtio_fs_probe(struct virtio_device *vdev)
637 struct virtio_fs *fs;
640 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
643 kref_init(&fs->refcount);
646 ret = virtio_fs_read_tag(vdev, fs);
650 ret = virtio_fs_setup_vqs(vdev, fs);
654 /* TODO vq affinity */
656 /* Bring the device online in case the filesystem is mounted and
657 * requests need to be sent before we return.
659 virtio_device_ready(vdev);
661 ret = virtio_fs_add_instance(fs);
668 vdev->config->reset(vdev);
669 virtio_fs_cleanup_vqs(vdev, fs);
678 static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
680 struct virtio_fs_vq *fsvq;
683 for (i = 0; i < fs->nvqs; i++) {
685 spin_lock(&fsvq->lock);
686 fsvq->connected = false;
687 spin_unlock(&fsvq->lock);
691 static void virtio_fs_remove(struct virtio_device *vdev)
693 struct virtio_fs *fs = vdev->priv;
695 mutex_lock(&virtio_fs_mutex);
696 /* This device is going away. No one should get new reference */
697 list_del_init(&fs->list);
698 virtio_fs_stop_all_queues(fs);
699 virtio_fs_drain_all_queues(fs);
700 vdev->config->reset(vdev);
701 virtio_fs_cleanup_vqs(vdev, fs);
704 /* Put device reference on virtio_fs object */
706 mutex_unlock(&virtio_fs_mutex);
709 #ifdef CONFIG_PM_SLEEP
710 static int virtio_fs_freeze(struct virtio_device *vdev)
712 /* TODO need to save state here */
713 pr_warn("virtio-fs: suspend/resume not yet supported\n");
717 static int virtio_fs_restore(struct virtio_device *vdev)
719 /* TODO need to restore state here */
722 #endif /* CONFIG_PM_SLEEP */
724 const static struct virtio_device_id id_table[] = {
725 { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
729 const static unsigned int feature_table[] = {};
731 static struct virtio_driver virtio_fs_driver = {
732 .driver.name = KBUILD_MODNAME,
733 .driver.owner = THIS_MODULE,
734 .id_table = id_table,
735 .feature_table = feature_table,
736 .feature_table_size = ARRAY_SIZE(feature_table),
737 .probe = virtio_fs_probe,
738 .remove = virtio_fs_remove,
739 #ifdef CONFIG_PM_SLEEP
740 .freeze = virtio_fs_freeze,
741 .restore = virtio_fs_restore,
745 static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq)
746 __releases(fiq->lock)
748 struct fuse_forget_link *link;
749 struct virtio_fs_forget *forget;
750 struct scatterlist sg;
751 struct scatterlist *sgs[] = {&sg};
752 struct virtio_fs *fs;
753 struct virtqueue *vq;
754 struct virtio_fs_vq *fsvq;
759 link = fuse_dequeue_forget(fiq, 1, NULL);
760 unique = fuse_get_unique(fiq);
763 fsvq = &fs->vqs[VQ_HIPRIO];
764 spin_unlock(&fiq->lock);
766 /* Allocate a buffer for the request */
767 forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
769 forget->ih = (struct fuse_in_header){
770 .opcode = FUSE_FORGET,
771 .nodeid = link->forget_one.nodeid,
773 .len = sizeof(*forget),
775 forget->arg = (struct fuse_forget_in){
776 .nlookup = link->forget_one.nlookup,
779 sg_init_one(&sg, forget, sizeof(*forget));
781 /* Enqueue the request */
782 spin_lock(&fsvq->lock);
784 if (!fsvq->connected) {
786 spin_unlock(&fsvq->lock);
791 dev_dbg(&vq->vdev->dev, "%s\n", __func__);
793 ret = virtqueue_add_sgs(vq, sgs, 1, 0, forget, GFP_ATOMIC);
795 if (ret == -ENOMEM || ret == -ENOSPC) {
796 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later.\n",
798 list_add_tail(&forget->list, &fsvq->queued_reqs);
799 schedule_delayed_work(&fsvq->dispatch_work,
800 msecs_to_jiffies(1));
801 inc_in_flight_req(fsvq);
803 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
807 spin_unlock(&fsvq->lock);
811 inc_in_flight_req(fsvq);
812 notify = virtqueue_kick_prepare(vq);
814 spin_unlock(&fsvq->lock);
817 virtqueue_notify(vq);
822 static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq)
823 __releases(fiq->lock)
828 * Normal fs operations on a local filesystems aren't interruptible.
829 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
830 * with shared lock between host and guest.
832 spin_unlock(&fiq->lock);
835 /* Return the number of scatter-gather list elements required */
836 static unsigned int sg_count_fuse_req(struct fuse_req *req)
838 struct fuse_args *args = req->args;
839 struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
840 unsigned int total_sgs = 1 /* fuse_in_header */;
842 if (args->in_numargs - args->in_pages)
846 total_sgs += ap->num_pages;
848 if (!test_bit(FR_ISREPLY, &req->flags))
851 total_sgs += 1 /* fuse_out_header */;
853 if (args->out_numargs - args->out_pages)
857 total_sgs += ap->num_pages;
862 /* Add pages to scatter-gather list and return number of elements used */
863 static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
865 struct fuse_page_desc *page_descs,
866 unsigned int num_pages,
867 unsigned int total_len)
870 unsigned int this_len;
872 for (i = 0; i < num_pages && total_len; i++) {
873 sg_init_table(&sg[i], 1);
874 this_len = min(page_descs[i].length, total_len);
875 sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
876 total_len -= this_len;
882 /* Add args to scatter-gather list and return number of elements used */
883 static unsigned int sg_init_fuse_args(struct scatterlist *sg,
884 struct fuse_req *req,
885 struct fuse_arg *args,
886 unsigned int numargs,
889 unsigned int *len_used)
891 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
892 unsigned int total_sgs = 0;
895 len = fuse_len_args(numargs - argpages, args);
897 sg_init_one(&sg[total_sgs++], argbuf, len);
900 total_sgs += sg_init_fuse_pages(&sg[total_sgs],
901 ap->pages, ap->descs,
903 args[numargs - 1].size);
911 /* Add a request to a virtqueue and kick the device */
912 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
913 struct fuse_req *req, bool in_flight)
915 /* requests need at least 4 elements */
916 struct scatterlist *stack_sgs[6];
917 struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
918 struct scatterlist **sgs = stack_sgs;
919 struct scatterlist *sg = stack_sg;
920 struct virtqueue *vq;
921 struct fuse_args *args = req->args;
922 unsigned int argbuf_used = 0;
923 unsigned int out_sgs = 0;
924 unsigned int in_sgs = 0;
925 unsigned int total_sgs;
929 struct fuse_pqueue *fpq;
931 /* Does the sglist fit on the stack? */
932 total_sgs = sg_count_fuse_req(req);
933 if (total_sgs > ARRAY_SIZE(stack_sgs)) {
934 sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
935 sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
942 /* Use a bounce buffer since stack args cannot be mapped */
943 ret = copy_args_to_argbuf(req);
947 /* Request elements */
948 sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
949 out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
950 (struct fuse_arg *)args->in_args,
951 args->in_numargs, args->in_pages,
952 req->argbuf, &argbuf_used);
955 if (test_bit(FR_ISREPLY, &req->flags)) {
956 sg_init_one(&sg[out_sgs + in_sgs++],
957 &req->out.h, sizeof(req->out.h));
958 in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
959 args->out_args, args->out_numargs,
961 req->argbuf + argbuf_used, NULL);
964 WARN_ON(out_sgs + in_sgs != total_sgs);
966 for (i = 0; i < total_sgs; i++)
969 spin_lock(&fsvq->lock);
971 if (!fsvq->connected) {
972 spin_unlock(&fsvq->lock);
978 ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
980 spin_unlock(&fsvq->lock);
984 /* Request successfully sent. */
985 fpq = &fsvq->fud->pq;
986 spin_lock(&fpq->lock);
987 list_add_tail(&req->list, fpq->processing);
988 spin_unlock(&fpq->lock);
989 set_bit(FR_SENT, &req->flags);
990 /* matches barrier in request_wait_answer() */
991 smp_mb__after_atomic();
994 inc_in_flight_req(fsvq);
995 notify = virtqueue_kick_prepare(vq);
997 spin_unlock(&fsvq->lock);
1000 virtqueue_notify(vq);
1003 if (ret < 0 && req->argbuf) {
1007 if (sgs != stack_sgs) {
1015 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
1016 __releases(fiq->lock)
1018 unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
1019 struct virtio_fs *fs;
1020 struct fuse_req *req;
1021 struct virtio_fs_vq *fsvq;
1024 WARN_ON(list_empty(&fiq->pending));
1025 req = list_last_entry(&fiq->pending, struct fuse_req, list);
1026 clear_bit(FR_PENDING, &req->flags);
1027 list_del_init(&req->list);
1028 WARN_ON(!list_empty(&fiq->pending));
1029 spin_unlock(&fiq->lock);
1033 pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
1034 __func__, req->in.h.opcode, req->in.h.unique,
1035 req->in.h.nodeid, req->in.h.len,
1036 fuse_len_args(req->args->out_numargs, req->args->out_args));
1038 fsvq = &fs->vqs[queue_id];
1039 ret = virtio_fs_enqueue_req(fsvq, req, false);
1041 if (ret == -ENOMEM || ret == -ENOSPC) {
1043 * Virtqueue full. Retry submission from worker
1044 * context as we might be holding fc->bg_lock.
1046 spin_lock(&fsvq->lock);
1047 list_add_tail(&req->list, &fsvq->queued_reqs);
1048 inc_in_flight_req(fsvq);
1049 schedule_delayed_work(&fsvq->dispatch_work,
1050 msecs_to_jiffies(1));
1051 spin_unlock(&fsvq->lock);
1054 req->out.h.error = ret;
1055 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
1057 /* Can't end request in submission context. Use a worker */
1058 spin_lock(&fsvq->lock);
1059 list_add_tail(&req->list, &fsvq->end_reqs);
1060 schedule_delayed_work(&fsvq->dispatch_work, 0);
1061 spin_unlock(&fsvq->lock);
1066 const static struct fuse_iqueue_ops virtio_fs_fiq_ops = {
1067 .wake_forget_and_unlock = virtio_fs_wake_forget_and_unlock,
1068 .wake_interrupt_and_unlock = virtio_fs_wake_interrupt_and_unlock,
1069 .wake_pending_and_unlock = virtio_fs_wake_pending_and_unlock,
1070 .release = virtio_fs_fiq_release,
1073 static int virtio_fs_fill_super(struct super_block *sb)
1075 struct fuse_conn *fc = get_fuse_conn_super(sb);
1076 struct virtio_fs *fs = fc->iq.priv;
1079 struct fuse_fs_context ctx = {
1080 .rootmode = S_IFDIR,
1081 .default_permissions = 1,
1083 .max_read = UINT_MAX,
1087 .no_force_umount = true,
1088 .no_mount_options = true,
1091 mutex_lock(&virtio_fs_mutex);
1093 /* After holding mutex, make sure virtiofs device is still there.
1094 * Though we are holding a reference to it, drive ->remove might
1095 * still have cleaned up virtual queues. In that case bail out.
1098 if (list_empty(&fs->list)) {
1099 pr_info("virtio-fs: tag <%s> not found\n", fs->tag);
1104 /* Allocate fuse_dev for hiprio and notification queues */
1105 for (i = 0; i < VQ_REQUEST; i++) {
1106 struct virtio_fs_vq *fsvq = &fs->vqs[i];
1108 fsvq->fud = fuse_dev_alloc();
1110 goto err_free_fuse_devs;
1113 ctx.fudptr = (void **)&fs->vqs[VQ_REQUEST].fud;
1114 err = fuse_fill_super_common(sb, &ctx);
1116 goto err_free_fuse_devs;
1118 fc = fs->vqs[VQ_REQUEST].fud->fc;
1120 for (i = 0; i < fs->nvqs; i++) {
1121 struct virtio_fs_vq *fsvq = &fs->vqs[i];
1123 if (i == VQ_REQUEST)
1124 continue; /* already initialized */
1125 fuse_dev_install(fsvq->fud, fc);
1128 /* Previous unmount will stop all queues. Start these again */
1129 virtio_fs_start_all_queues(fs);
1131 mutex_unlock(&virtio_fs_mutex);
1135 virtio_fs_free_devs(fs);
1137 mutex_unlock(&virtio_fs_mutex);
1141 static void virtio_kill_sb(struct super_block *sb)
1143 struct fuse_conn *fc = get_fuse_conn_super(sb);
1144 struct virtio_fs *vfs;
1145 struct virtio_fs_vq *fsvq;
1147 /* If mount failed, we can still be called without any fc */
1149 return fuse_kill_sb_anon(sb);
1152 fsvq = &vfs->vqs[VQ_HIPRIO];
1154 /* Stop forget queue. Soon destroy will be sent */
1155 spin_lock(&fsvq->lock);
1156 fsvq->connected = false;
1157 spin_unlock(&fsvq->lock);
1158 virtio_fs_drain_all_queues(vfs);
1160 fuse_kill_sb_anon(sb);
1162 /* fuse_kill_sb_anon() must have sent destroy. Stop all queues
1163 * and drain one more time and free fuse devices. Freeing fuse
1164 * devices will drop their reference on fuse_conn and that in
1165 * turn will drop its reference on virtio_fs object.
1167 virtio_fs_stop_all_queues(vfs);
1168 virtio_fs_drain_all_queues(vfs);
1169 virtio_fs_free_devs(vfs);
1172 static int virtio_fs_test_super(struct super_block *sb,
1173 struct fs_context *fsc)
1175 struct fuse_conn *fc = fsc->s_fs_info;
1177 return fc->iq.priv == get_fuse_conn_super(sb)->iq.priv;
1180 static int virtio_fs_set_super(struct super_block *sb,
1181 struct fs_context *fsc)
1185 err = get_anon_bdev(&sb->s_dev);
1187 fuse_conn_get(fsc->s_fs_info);
1192 static int virtio_fs_get_tree(struct fs_context *fsc)
1194 struct virtio_fs *fs;
1195 struct super_block *sb;
1196 struct fuse_conn *fc;
1199 /* This gets a reference on virtio_fs object. This ptr gets installed
1200 * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1201 * to drop the reference to this object.
1203 fs = virtio_fs_find_instance(fsc->source);
1205 pr_info("virtio-fs: tag <%s> not found\n", fsc->source);
1209 fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
1211 mutex_lock(&virtio_fs_mutex);
1213 mutex_unlock(&virtio_fs_mutex);
1217 fuse_conn_init(fc, get_user_ns(current_user_ns()), &virtio_fs_fiq_ops,
1219 fc->release = fuse_free_conn;
1220 fc->delete_stale = true;
1222 fsc->s_fs_info = fc;
1223 sb = sget_fc(fsc, virtio_fs_test_super, virtio_fs_set_super);
1229 err = virtio_fs_fill_super(sb);
1231 deactivate_locked_super(sb);
1235 sb->s_flags |= SB_ACTIVE;
1239 fsc->root = dget(sb->s_root);
1243 static const struct fs_context_operations virtio_fs_context_ops = {
1244 .get_tree = virtio_fs_get_tree,
1247 static int virtio_fs_init_fs_context(struct fs_context *fsc)
1249 fsc->ops = &virtio_fs_context_ops;
1253 static struct file_system_type virtio_fs_type = {
1254 .owner = THIS_MODULE,
1256 .init_fs_context = virtio_fs_init_fs_context,
1257 .kill_sb = virtio_kill_sb,
1260 static int __init virtio_fs_init(void)
1264 ret = register_virtio_driver(&virtio_fs_driver);
1268 ret = register_filesystem(&virtio_fs_type);
1270 unregister_virtio_driver(&virtio_fs_driver);
1276 module_init(virtio_fs_init);
1278 static void __exit virtio_fs_exit(void)
1280 unregister_filesystem(&virtio_fs_type);
1281 unregister_virtio_driver(&virtio_fs_driver);
1283 module_exit(virtio_fs_exit);
1285 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1286 MODULE_DESCRIPTION("Virtio Filesystem");
1287 MODULE_LICENSE("GPL");
1288 MODULE_ALIAS_FS(KBUILD_MODNAME);
1289 MODULE_DEVICE_TABLE(virtio, id_table);