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 delayed_work dispatch_work;
38 } ____cacheline_aligned_in_smp;
40 /* A virtio-fs device instance */
43 struct list_head list; /* on virtio_fs_instances */
45 struct virtio_fs_vq *vqs;
46 unsigned int nvqs; /* number of virtqueues */
47 unsigned int num_request_queues; /* number of request queues */
50 struct virtio_fs_forget {
51 struct fuse_in_header ih;
52 struct fuse_forget_in arg;
53 /* This request can be temporarily queued on virt queue */
54 struct list_head list;
57 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
59 struct virtio_fs *fs = vq->vdev->priv;
61 return &fs->vqs[vq->index];
64 static inline struct fuse_pqueue *vq_to_fpq(struct virtqueue *vq)
66 return &vq_to_fsvq(vq)->fud->pq;
69 static void release_virtio_fs_obj(struct kref *ref)
71 struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount);
77 /* Make sure virtiofs_mutex is held */
78 static void virtio_fs_put(struct virtio_fs *fs)
80 kref_put(&fs->refcount, release_virtio_fs_obj);
83 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
85 struct virtio_fs *vfs = fiq->priv;
87 mutex_lock(&virtio_fs_mutex);
89 mutex_unlock(&virtio_fs_mutex);
92 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
94 WARN_ON(fsvq->in_flight < 0);
96 /* Wait for in flight requests to finish.*/
98 spin_lock(&fsvq->lock);
99 if (!fsvq->in_flight) {
100 spin_unlock(&fsvq->lock);
103 spin_unlock(&fsvq->lock);
104 /* TODO use completion instead of timeout */
105 usleep_range(1000, 2000);
108 flush_work(&fsvq->done_work);
109 flush_delayed_work(&fsvq->dispatch_work);
112 static inline void drain_hiprio_queued_reqs(struct virtio_fs_vq *fsvq)
114 struct virtio_fs_forget *forget;
116 spin_lock(&fsvq->lock);
118 forget = list_first_entry_or_null(&fsvq->queued_reqs,
119 struct virtio_fs_forget, list);
122 list_del(&forget->list);
125 spin_unlock(&fsvq->lock);
128 static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
130 struct virtio_fs_vq *fsvq;
133 for (i = 0; i < fs->nvqs; i++) {
136 drain_hiprio_queued_reqs(fsvq);
138 virtio_fs_drain_queue(fsvq);
142 static void virtio_fs_start_all_queues(struct virtio_fs *fs)
144 struct virtio_fs_vq *fsvq;
147 for (i = 0; i < fs->nvqs; i++) {
149 spin_lock(&fsvq->lock);
150 fsvq->connected = true;
151 spin_unlock(&fsvq->lock);
155 /* Add a new instance to the list or return -EEXIST if tag name exists*/
156 static int virtio_fs_add_instance(struct virtio_fs *fs)
158 struct virtio_fs *fs2;
159 bool duplicate = false;
161 mutex_lock(&virtio_fs_mutex);
163 list_for_each_entry(fs2, &virtio_fs_instances, list) {
164 if (strcmp(fs->tag, fs2->tag) == 0)
169 list_add_tail(&fs->list, &virtio_fs_instances);
171 mutex_unlock(&virtio_fs_mutex);
178 /* Return the virtio_fs with a given tag, or NULL */
179 static struct virtio_fs *virtio_fs_find_instance(const char *tag)
181 struct virtio_fs *fs;
183 mutex_lock(&virtio_fs_mutex);
185 list_for_each_entry(fs, &virtio_fs_instances, list) {
186 if (strcmp(fs->tag, tag) == 0) {
187 kref_get(&fs->refcount);
192 fs = NULL; /* not found */
195 mutex_unlock(&virtio_fs_mutex);
200 static void virtio_fs_free_devs(struct virtio_fs *fs)
204 for (i = 0; i < fs->nvqs; i++) {
205 struct virtio_fs_vq *fsvq = &fs->vqs[i];
210 fuse_dev_free(fsvq->fud);
215 /* Read filesystem name from virtio config into fs->tag (must kfree()). */
216 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
218 char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
222 virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
223 &tag_buf, sizeof(tag_buf));
224 end = memchr(tag_buf, '\0', sizeof(tag_buf));
226 return -EINVAL; /* empty tag */
228 end = &tag_buf[sizeof(tag_buf)];
231 fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
234 memcpy(fs->tag, tag_buf, len);
239 /* Work function for hiprio completion */
240 static void virtio_fs_hiprio_done_work(struct work_struct *work)
242 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
244 struct virtqueue *vq = fsvq->vq;
246 /* Free completed FUSE_FORGET requests */
247 spin_lock(&fsvq->lock);
252 virtqueue_disable_cb(vq);
254 while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
258 } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
259 spin_unlock(&fsvq->lock);
262 static void virtio_fs_dummy_dispatch_work(struct work_struct *work)
266 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
268 struct virtio_fs_forget *forget;
269 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
271 struct virtqueue *vq = fsvq->vq;
272 struct scatterlist sg;
273 struct scatterlist *sgs[] = {&sg};
277 pr_debug("virtio-fs: worker %s called.\n", __func__);
279 spin_lock(&fsvq->lock);
280 forget = list_first_entry_or_null(&fsvq->queued_reqs,
281 struct virtio_fs_forget, list);
283 spin_unlock(&fsvq->lock);
287 list_del(&forget->list);
288 if (!fsvq->connected) {
289 spin_unlock(&fsvq->lock);
294 sg_init_one(&sg, forget, sizeof(*forget));
296 /* Enqueue the request */
297 dev_dbg(&vq->vdev->dev, "%s\n", __func__);
298 ret = virtqueue_add_sgs(vq, sgs, 1, 0, forget, GFP_ATOMIC);
300 if (ret == -ENOMEM || ret == -ENOSPC) {
301 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
303 list_add_tail(&forget->list,
305 schedule_delayed_work(&fsvq->dispatch_work,
306 msecs_to_jiffies(1));
308 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
312 spin_unlock(&fsvq->lock);
317 notify = virtqueue_kick_prepare(vq);
318 spin_unlock(&fsvq->lock);
321 virtqueue_notify(vq);
322 pr_debug("virtio-fs: worker %s dispatched one forget request.\n",
327 /* Allocate and copy args into req->argbuf */
328 static int copy_args_to_argbuf(struct fuse_req *req)
330 struct fuse_args *args = req->args;
331 unsigned int offset = 0;
333 unsigned int num_out;
337 num_in = args->in_numargs - args->in_pages;
338 num_out = args->out_numargs - args->out_pages;
339 len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
340 fuse_len_args(num_out, args->out_args);
342 req->argbuf = kmalloc(len, GFP_ATOMIC);
346 for (i = 0; i < num_in; i++) {
347 memcpy(req->argbuf + offset,
348 args->in_args[i].value,
349 args->in_args[i].size);
350 offset += args->in_args[i].size;
356 /* Copy args out of and free req->argbuf */
357 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
359 unsigned int remaining;
362 unsigned int num_out;
365 remaining = req->out.h.len - sizeof(req->out.h);
366 num_in = args->in_numargs - args->in_pages;
367 num_out = args->out_numargs - args->out_pages;
368 offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
370 for (i = 0; i < num_out; i++) {
371 unsigned int argsize = args->out_args[i].size;
373 if (args->out_argvar &&
374 i == args->out_numargs - 1 &&
375 argsize > remaining) {
379 memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
382 if (i != args->out_numargs - 1)
383 remaining -= argsize;
386 /* Store the actual size of the variable-length arg */
387 if (args->out_argvar)
388 args->out_args[args->out_numargs - 1].size = remaining;
394 /* Work function for request completion */
395 static void virtio_fs_requests_done_work(struct work_struct *work)
397 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
399 struct fuse_pqueue *fpq = &fsvq->fud->pq;
400 struct fuse_conn *fc = fsvq->fud->fc;
401 struct virtqueue *vq = fsvq->vq;
402 struct fuse_req *req;
403 struct fuse_args_pages *ap;
404 struct fuse_req *next;
405 struct fuse_args *args;
406 unsigned int len, i, thislen;
410 /* Collect completed requests off the virtqueue */
411 spin_lock(&fsvq->lock);
413 virtqueue_disable_cb(vq);
415 while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
416 spin_lock(&fpq->lock);
417 list_move_tail(&req->list, &reqs);
418 spin_unlock(&fpq->lock);
420 } while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
421 spin_unlock(&fsvq->lock);
424 list_for_each_entry_safe(req, next, &reqs, list) {
426 * TODO verify that server properly follows FUSE protocol
430 copy_args_from_argbuf(args, req);
432 if (args->out_pages && args->page_zeroing) {
433 len = args->out_args[args->out_numargs - 1].size;
434 ap = container_of(args, typeof(*ap), args);
435 for (i = 0; i < ap->num_pages; i++) {
436 thislen = ap->descs[i].length;
438 WARN_ON(ap->descs[i].offset);
440 zero_user_segment(page, len, thislen);
448 spin_lock(&fpq->lock);
449 clear_bit(FR_SENT, &req->flags);
450 list_del_init(&req->list);
451 spin_unlock(&fpq->lock);
453 fuse_request_end(fc, req);
454 spin_lock(&fsvq->lock);
456 spin_unlock(&fsvq->lock);
460 /* Virtqueue interrupt handler */
461 static void virtio_fs_vq_done(struct virtqueue *vq)
463 struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
465 dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
467 schedule_work(&fsvq->done_work);
470 /* Initialize virtqueues */
471 static int virtio_fs_setup_vqs(struct virtio_device *vdev,
472 struct virtio_fs *fs)
474 struct virtqueue **vqs;
475 vq_callback_t **callbacks;
480 virtio_cread(vdev, struct virtio_fs_config, num_request_queues,
481 &fs->num_request_queues);
482 if (fs->num_request_queues == 0)
485 fs->nvqs = 1 + fs->num_request_queues;
486 fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
490 vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
491 callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
493 names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL);
494 if (!vqs || !callbacks || !names) {
499 callbacks[VQ_HIPRIO] = virtio_fs_vq_done;
500 snprintf(fs->vqs[VQ_HIPRIO].name, sizeof(fs->vqs[VQ_HIPRIO].name),
502 names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
503 INIT_WORK(&fs->vqs[VQ_HIPRIO].done_work, virtio_fs_hiprio_done_work);
504 INIT_LIST_HEAD(&fs->vqs[VQ_HIPRIO].queued_reqs);
505 INIT_DELAYED_WORK(&fs->vqs[VQ_HIPRIO].dispatch_work,
506 virtio_fs_hiprio_dispatch_work);
507 spin_lock_init(&fs->vqs[VQ_HIPRIO].lock);
509 /* Initialize the requests virtqueues */
510 for (i = VQ_REQUEST; i < fs->nvqs; i++) {
511 spin_lock_init(&fs->vqs[i].lock);
512 INIT_WORK(&fs->vqs[i].done_work, virtio_fs_requests_done_work);
513 INIT_DELAYED_WORK(&fs->vqs[i].dispatch_work,
514 virtio_fs_dummy_dispatch_work);
515 INIT_LIST_HEAD(&fs->vqs[i].queued_reqs);
516 snprintf(fs->vqs[i].name, sizeof(fs->vqs[i].name),
517 "requests.%u", i - VQ_REQUEST);
518 callbacks[i] = virtio_fs_vq_done;
519 names[i] = fs->vqs[i].name;
522 ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
526 for (i = 0; i < fs->nvqs; i++)
527 fs->vqs[i].vq = vqs[i];
529 virtio_fs_start_all_queues(fs);
539 /* Free virtqueues (device must already be reset) */
540 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev,
541 struct virtio_fs *fs)
543 vdev->config->del_vqs(vdev);
546 static int virtio_fs_probe(struct virtio_device *vdev)
548 struct virtio_fs *fs;
551 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
554 kref_init(&fs->refcount);
557 ret = virtio_fs_read_tag(vdev, fs);
561 ret = virtio_fs_setup_vqs(vdev, fs);
565 /* TODO vq affinity */
567 /* Bring the device online in case the filesystem is mounted and
568 * requests need to be sent before we return.
570 virtio_device_ready(vdev);
572 ret = virtio_fs_add_instance(fs);
579 vdev->config->reset(vdev);
580 virtio_fs_cleanup_vqs(vdev, fs);
588 static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
590 struct virtio_fs_vq *fsvq;
593 for (i = 0; i < fs->nvqs; i++) {
595 spin_lock(&fsvq->lock);
596 fsvq->connected = false;
597 spin_unlock(&fsvq->lock);
601 static void virtio_fs_remove(struct virtio_device *vdev)
603 struct virtio_fs *fs = vdev->priv;
605 mutex_lock(&virtio_fs_mutex);
606 /* This device is going away. No one should get new reference */
607 list_del_init(&fs->list);
608 virtio_fs_stop_all_queues(fs);
609 virtio_fs_drain_all_queues(fs);
610 vdev->config->reset(vdev);
611 virtio_fs_cleanup_vqs(vdev, fs);
614 /* Put device reference on virtio_fs object */
616 mutex_unlock(&virtio_fs_mutex);
619 #ifdef CONFIG_PM_SLEEP
620 static int virtio_fs_freeze(struct virtio_device *vdev)
622 /* TODO need to save state here */
623 pr_warn("virtio-fs: suspend/resume not yet supported\n");
627 static int virtio_fs_restore(struct virtio_device *vdev)
629 /* TODO need to restore state here */
632 #endif /* CONFIG_PM_SLEEP */
634 const static struct virtio_device_id id_table[] = {
635 { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
639 const static unsigned int feature_table[] = {};
641 static struct virtio_driver virtio_fs_driver = {
642 .driver.name = KBUILD_MODNAME,
643 .driver.owner = THIS_MODULE,
644 .id_table = id_table,
645 .feature_table = feature_table,
646 .feature_table_size = ARRAY_SIZE(feature_table),
647 .probe = virtio_fs_probe,
648 .remove = virtio_fs_remove,
649 #ifdef CONFIG_PM_SLEEP
650 .freeze = virtio_fs_freeze,
651 .restore = virtio_fs_restore,
655 static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq)
656 __releases(fiq->lock)
658 struct fuse_forget_link *link;
659 struct virtio_fs_forget *forget;
660 struct scatterlist sg;
661 struct scatterlist *sgs[] = {&sg};
662 struct virtio_fs *fs;
663 struct virtqueue *vq;
664 struct virtio_fs_vq *fsvq;
669 link = fuse_dequeue_forget(fiq, 1, NULL);
670 unique = fuse_get_unique(fiq);
673 fsvq = &fs->vqs[VQ_HIPRIO];
674 spin_unlock(&fiq->lock);
676 /* Allocate a buffer for the request */
677 forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
679 forget->ih = (struct fuse_in_header){
680 .opcode = FUSE_FORGET,
681 .nodeid = link->forget_one.nodeid,
683 .len = sizeof(*forget),
685 forget->arg = (struct fuse_forget_in){
686 .nlookup = link->forget_one.nlookup,
689 sg_init_one(&sg, forget, sizeof(*forget));
691 /* Enqueue the request */
692 spin_lock(&fsvq->lock);
694 if (!fsvq->connected) {
696 spin_unlock(&fsvq->lock);
701 dev_dbg(&vq->vdev->dev, "%s\n", __func__);
703 ret = virtqueue_add_sgs(vq, sgs, 1, 0, forget, GFP_ATOMIC);
705 if (ret == -ENOMEM || ret == -ENOSPC) {
706 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later.\n",
708 list_add_tail(&forget->list, &fsvq->queued_reqs);
709 schedule_delayed_work(&fsvq->dispatch_work,
710 msecs_to_jiffies(1));
712 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
716 spin_unlock(&fsvq->lock);
721 notify = virtqueue_kick_prepare(vq);
723 spin_unlock(&fsvq->lock);
726 virtqueue_notify(vq);
731 static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq)
732 __releases(fiq->lock)
737 * Normal fs operations on a local filesystems aren't interruptible.
738 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
739 * with shared lock between host and guest.
741 spin_unlock(&fiq->lock);
744 /* Return the number of scatter-gather list elements required */
745 static unsigned int sg_count_fuse_req(struct fuse_req *req)
747 struct fuse_args *args = req->args;
748 struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
749 unsigned int total_sgs = 1 /* fuse_in_header */;
751 if (args->in_numargs - args->in_pages)
755 total_sgs += ap->num_pages;
757 if (!test_bit(FR_ISREPLY, &req->flags))
760 total_sgs += 1 /* fuse_out_header */;
762 if (args->out_numargs - args->out_pages)
766 total_sgs += ap->num_pages;
771 /* Add pages to scatter-gather list and return number of elements used */
772 static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
774 struct fuse_page_desc *page_descs,
775 unsigned int num_pages,
776 unsigned int total_len)
779 unsigned int this_len;
781 for (i = 0; i < num_pages && total_len; i++) {
782 sg_init_table(&sg[i], 1);
783 this_len = min(page_descs[i].length, total_len);
784 sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
785 total_len -= this_len;
791 /* Add args to scatter-gather list and return number of elements used */
792 static unsigned int sg_init_fuse_args(struct scatterlist *sg,
793 struct fuse_req *req,
794 struct fuse_arg *args,
795 unsigned int numargs,
798 unsigned int *len_used)
800 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
801 unsigned int total_sgs = 0;
804 len = fuse_len_args(numargs - argpages, args);
806 sg_init_one(&sg[total_sgs++], argbuf, len);
809 total_sgs += sg_init_fuse_pages(&sg[total_sgs],
810 ap->pages, ap->descs,
812 args[numargs - 1].size);
820 /* Add a request to a virtqueue and kick the device */
821 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
822 struct fuse_req *req)
824 /* requests need at least 4 elements */
825 struct scatterlist *stack_sgs[6];
826 struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
827 struct scatterlist **sgs = stack_sgs;
828 struct scatterlist *sg = stack_sg;
829 struct virtqueue *vq;
830 struct fuse_args *args = req->args;
831 unsigned int argbuf_used = 0;
832 unsigned int out_sgs = 0;
833 unsigned int in_sgs = 0;
834 unsigned int total_sgs;
839 /* Does the sglist fit on the stack? */
840 total_sgs = sg_count_fuse_req(req);
841 if (total_sgs > ARRAY_SIZE(stack_sgs)) {
842 sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
843 sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
850 /* Use a bounce buffer since stack args cannot be mapped */
851 ret = copy_args_to_argbuf(req);
855 /* Request elements */
856 sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
857 out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
858 (struct fuse_arg *)args->in_args,
859 args->in_numargs, args->in_pages,
860 req->argbuf, &argbuf_used);
863 if (test_bit(FR_ISREPLY, &req->flags)) {
864 sg_init_one(&sg[out_sgs + in_sgs++],
865 &req->out.h, sizeof(req->out.h));
866 in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
867 args->out_args, args->out_numargs,
869 req->argbuf + argbuf_used, NULL);
872 WARN_ON(out_sgs + in_sgs != total_sgs);
874 for (i = 0; i < total_sgs; i++)
877 spin_lock(&fsvq->lock);
879 if (!fsvq->connected) {
880 spin_unlock(&fsvq->lock);
886 ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
888 spin_unlock(&fsvq->lock);
893 notify = virtqueue_kick_prepare(vq);
895 spin_unlock(&fsvq->lock);
898 virtqueue_notify(vq);
901 if (ret < 0 && req->argbuf) {
905 if (sgs != stack_sgs) {
913 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
914 __releases(fiq->lock)
916 unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
917 struct virtio_fs *fs;
918 struct fuse_conn *fc;
919 struct fuse_req *req;
920 struct fuse_pqueue *fpq;
923 WARN_ON(list_empty(&fiq->pending));
924 req = list_last_entry(&fiq->pending, struct fuse_req, list);
925 clear_bit(FR_PENDING, &req->flags);
926 list_del_init(&req->list);
927 WARN_ON(!list_empty(&fiq->pending));
928 spin_unlock(&fiq->lock);
931 fc = fs->vqs[queue_id].fud->fc;
933 pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
934 __func__, req->in.h.opcode, req->in.h.unique,
935 req->in.h.nodeid, req->in.h.len,
936 fuse_len_args(req->args->out_numargs, req->args->out_args));
938 fpq = &fs->vqs[queue_id].fud->pq;
939 spin_lock(&fpq->lock);
940 if (!fpq->connected) {
941 spin_unlock(&fpq->lock);
942 req->out.h.error = -ENODEV;
943 pr_err("virtio-fs: %s disconnected\n", __func__);
944 fuse_request_end(fc, req);
947 list_add_tail(&req->list, fpq->processing);
948 spin_unlock(&fpq->lock);
949 set_bit(FR_SENT, &req->flags);
950 /* matches barrier in request_wait_answer() */
951 smp_mb__after_atomic();
954 ret = virtio_fs_enqueue_req(&fs->vqs[queue_id], req);
956 if (ret == -ENOMEM || ret == -ENOSPC) {
957 /* Virtqueue full. Retry submission */
958 /* TODO use completion instead of timeout */
959 usleep_range(20, 30);
962 req->out.h.error = ret;
963 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
964 spin_lock(&fpq->lock);
965 clear_bit(FR_SENT, &req->flags);
966 list_del_init(&req->list);
967 spin_unlock(&fpq->lock);
968 fuse_request_end(fc, req);
973 const static struct fuse_iqueue_ops virtio_fs_fiq_ops = {
974 .wake_forget_and_unlock = virtio_fs_wake_forget_and_unlock,
975 .wake_interrupt_and_unlock = virtio_fs_wake_interrupt_and_unlock,
976 .wake_pending_and_unlock = virtio_fs_wake_pending_and_unlock,
977 .release = virtio_fs_fiq_release,
980 static int virtio_fs_fill_super(struct super_block *sb)
982 struct fuse_conn *fc = get_fuse_conn_super(sb);
983 struct virtio_fs *fs = fc->iq.priv;
986 struct fuse_fs_context ctx = {
988 .default_permissions = 1,
990 .max_read = UINT_MAX,
994 .no_force_umount = true,
997 mutex_lock(&virtio_fs_mutex);
999 /* After holding mutex, make sure virtiofs device is still there.
1000 * Though we are holding a reference to it, drive ->remove might
1001 * still have cleaned up virtual queues. In that case bail out.
1004 if (list_empty(&fs->list)) {
1005 pr_info("virtio-fs: tag <%s> not found\n", fs->tag);
1010 /* Allocate fuse_dev for hiprio and notification queues */
1011 for (i = 0; i < VQ_REQUEST; i++) {
1012 struct virtio_fs_vq *fsvq = &fs->vqs[i];
1014 fsvq->fud = fuse_dev_alloc();
1016 goto err_free_fuse_devs;
1019 ctx.fudptr = (void **)&fs->vqs[VQ_REQUEST].fud;
1020 err = fuse_fill_super_common(sb, &ctx);
1022 goto err_free_fuse_devs;
1024 fc = fs->vqs[VQ_REQUEST].fud->fc;
1026 for (i = 0; i < fs->nvqs; i++) {
1027 struct virtio_fs_vq *fsvq = &fs->vqs[i];
1029 if (i == VQ_REQUEST)
1030 continue; /* already initialized */
1031 fuse_dev_install(fsvq->fud, fc);
1034 /* Previous unmount will stop all queues. Start these again */
1035 virtio_fs_start_all_queues(fs);
1037 mutex_unlock(&virtio_fs_mutex);
1041 virtio_fs_free_devs(fs);
1043 mutex_unlock(&virtio_fs_mutex);
1047 static void virtio_kill_sb(struct super_block *sb)
1049 struct fuse_conn *fc = get_fuse_conn_super(sb);
1050 struct virtio_fs *vfs;
1051 struct virtio_fs_vq *fsvq;
1053 /* If mount failed, we can still be called without any fc */
1055 return fuse_kill_sb_anon(sb);
1058 fsvq = &vfs->vqs[VQ_HIPRIO];
1060 /* Stop forget queue. Soon destroy will be sent */
1061 spin_lock(&fsvq->lock);
1062 fsvq->connected = false;
1063 spin_unlock(&fsvq->lock);
1064 virtio_fs_drain_all_queues(vfs);
1066 fuse_kill_sb_anon(sb);
1068 /* fuse_kill_sb_anon() must have sent destroy. Stop all queues
1069 * and drain one more time and free fuse devices. Freeing fuse
1070 * devices will drop their reference on fuse_conn and that in
1071 * turn will drop its reference on virtio_fs object.
1073 virtio_fs_stop_all_queues(vfs);
1074 virtio_fs_drain_all_queues(vfs);
1075 virtio_fs_free_devs(vfs);
1078 static int virtio_fs_test_super(struct super_block *sb,
1079 struct fs_context *fsc)
1081 struct fuse_conn *fc = fsc->s_fs_info;
1083 return fc->iq.priv == get_fuse_conn_super(sb)->iq.priv;
1086 static int virtio_fs_set_super(struct super_block *sb,
1087 struct fs_context *fsc)
1091 err = get_anon_bdev(&sb->s_dev);
1093 fuse_conn_get(fsc->s_fs_info);
1098 static int virtio_fs_get_tree(struct fs_context *fsc)
1100 struct virtio_fs *fs;
1101 struct super_block *sb;
1102 struct fuse_conn *fc;
1105 /* This gets a reference on virtio_fs object. This ptr gets installed
1106 * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1107 * to drop the reference to this object.
1109 fs = virtio_fs_find_instance(fsc->source);
1111 pr_info("virtio-fs: tag <%s> not found\n", fsc->source);
1115 fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
1117 mutex_lock(&virtio_fs_mutex);
1119 mutex_unlock(&virtio_fs_mutex);
1123 fuse_conn_init(fc, get_user_ns(current_user_ns()), &virtio_fs_fiq_ops,
1125 fc->release = fuse_free_conn;
1126 fc->delete_stale = true;
1128 fsc->s_fs_info = fc;
1129 sb = sget_fc(fsc, virtio_fs_test_super, virtio_fs_set_super);
1135 err = virtio_fs_fill_super(sb);
1137 deactivate_locked_super(sb);
1141 sb->s_flags |= SB_ACTIVE;
1145 fsc->root = dget(sb->s_root);
1149 static const struct fs_context_operations virtio_fs_context_ops = {
1150 .get_tree = virtio_fs_get_tree,
1153 static int virtio_fs_init_fs_context(struct fs_context *fsc)
1155 fsc->ops = &virtio_fs_context_ops;
1159 static struct file_system_type virtio_fs_type = {
1160 .owner = THIS_MODULE,
1162 .init_fs_context = virtio_fs_init_fs_context,
1163 .kill_sb = virtio_kill_sb,
1166 static int __init virtio_fs_init(void)
1170 ret = register_virtio_driver(&virtio_fs_driver);
1174 ret = register_filesystem(&virtio_fs_type);
1176 unregister_virtio_driver(&virtio_fs_driver);
1182 module_init(virtio_fs_init);
1184 static void __exit virtio_fs_exit(void)
1186 unregister_filesystem(&virtio_fs_type);
1187 unregister_virtio_driver(&virtio_fs_driver);
1189 module_exit(virtio_fs_exit);
1191 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1192 MODULE_DESCRIPTION("Virtio Filesystem");
1193 MODULE_LICENSE("GPL");
1194 MODULE_ALIAS_FS(KBUILD_MODNAME);
1195 MODULE_DEVICE_TABLE(virtio, id_table);