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
;
38 struct completion in_flight_zero
; /* No inflight requests */
40 } ____cacheline_aligned_in_smp
;
42 /* A virtio-fs device instance */
45 struct list_head list
; /* on virtio_fs_instances */
47 struct virtio_fs_vq
*vqs
;
48 unsigned int nvqs
; /* number of virtqueues */
49 unsigned int num_request_queues
; /* number of request queues */
52 struct virtio_fs_forget_req
{
53 struct fuse_in_header ih
;
54 struct fuse_forget_in arg
;
57 struct virtio_fs_forget
{
58 /* This request can be temporarily queued on virt queue */
59 struct list_head list
;
60 struct virtio_fs_forget_req req
;
63 struct virtio_fs_req_work
{
65 struct virtio_fs_vq
*fsvq
;
66 struct work_struct done_work
;
69 static int virtio_fs_enqueue_req(struct virtio_fs_vq
*fsvq
,
70 struct fuse_req
*req
, bool in_flight
);
72 static inline struct virtio_fs_vq
*vq_to_fsvq(struct virtqueue
*vq
)
74 struct virtio_fs
*fs
= vq
->vdev
->priv
;
76 return &fs
->vqs
[vq
->index
];
79 static inline struct fuse_pqueue
*vq_to_fpq(struct virtqueue
*vq
)
81 return &vq_to_fsvq(vq
)->fud
->pq
;
84 /* Should be called with fsvq->lock held. */
85 static inline void inc_in_flight_req(struct virtio_fs_vq
*fsvq
)
90 /* Should be called with fsvq->lock held. */
91 static inline void dec_in_flight_req(struct virtio_fs_vq
*fsvq
)
93 WARN_ON(fsvq
->in_flight
<= 0);
96 complete(&fsvq
->in_flight_zero
);
99 static void release_virtio_fs_obj(struct kref
*ref
)
101 struct virtio_fs
*vfs
= container_of(ref
, struct virtio_fs
, refcount
);
107 /* Make sure virtiofs_mutex is held */
108 static void virtio_fs_put(struct virtio_fs
*fs
)
110 kref_put(&fs
->refcount
, release_virtio_fs_obj
);
113 static void virtio_fs_fiq_release(struct fuse_iqueue
*fiq
)
115 struct virtio_fs
*vfs
= fiq
->priv
;
117 mutex_lock(&virtio_fs_mutex
);
119 mutex_unlock(&virtio_fs_mutex
);
122 static void virtio_fs_drain_queue(struct virtio_fs_vq
*fsvq
)
124 WARN_ON(fsvq
->in_flight
< 0);
126 /* Wait for in flight requests to finish.*/
127 spin_lock(&fsvq
->lock
);
128 if (fsvq
->in_flight
) {
129 /* We are holding virtio_fs_mutex. There should not be any
130 * waiters waiting for completion.
132 reinit_completion(&fsvq
->in_flight_zero
);
133 spin_unlock(&fsvq
->lock
);
134 wait_for_completion(&fsvq
->in_flight_zero
);
136 spin_unlock(&fsvq
->lock
);
139 flush_work(&fsvq
->done_work
);
140 flush_delayed_work(&fsvq
->dispatch_work
);
143 static void virtio_fs_drain_all_queues_locked(struct virtio_fs
*fs
)
145 struct virtio_fs_vq
*fsvq
;
148 for (i
= 0; i
< fs
->nvqs
; i
++) {
150 virtio_fs_drain_queue(fsvq
);
154 static void virtio_fs_drain_all_queues(struct virtio_fs
*fs
)
156 /* Provides mutual exclusion between ->remove and ->kill_sb
157 * paths. We don't want both of these draining queue at the
158 * same time. Current completion logic reinits completion
159 * and that means there should not be any other thread
160 * doing reinit or waiting for completion already.
162 mutex_lock(&virtio_fs_mutex
);
163 virtio_fs_drain_all_queues_locked(fs
);
164 mutex_unlock(&virtio_fs_mutex
);
167 static void virtio_fs_start_all_queues(struct virtio_fs
*fs
)
169 struct virtio_fs_vq
*fsvq
;
172 for (i
= 0; i
< fs
->nvqs
; i
++) {
174 spin_lock(&fsvq
->lock
);
175 fsvq
->connected
= true;
176 spin_unlock(&fsvq
->lock
);
180 /* Add a new instance to the list or return -EEXIST if tag name exists*/
181 static int virtio_fs_add_instance(struct virtio_fs
*fs
)
183 struct virtio_fs
*fs2
;
184 bool duplicate
= false;
186 mutex_lock(&virtio_fs_mutex
);
188 list_for_each_entry(fs2
, &virtio_fs_instances
, list
) {
189 if (strcmp(fs
->tag
, fs2
->tag
) == 0)
194 list_add_tail(&fs
->list
, &virtio_fs_instances
);
196 mutex_unlock(&virtio_fs_mutex
);
203 /* Return the virtio_fs with a given tag, or NULL */
204 static struct virtio_fs
*virtio_fs_find_instance(const char *tag
)
206 struct virtio_fs
*fs
;
208 mutex_lock(&virtio_fs_mutex
);
210 list_for_each_entry(fs
, &virtio_fs_instances
, list
) {
211 if (strcmp(fs
->tag
, tag
) == 0) {
212 kref_get(&fs
->refcount
);
217 fs
= NULL
; /* not found */
220 mutex_unlock(&virtio_fs_mutex
);
225 static void virtio_fs_free_devs(struct virtio_fs
*fs
)
229 for (i
= 0; i
< fs
->nvqs
; i
++) {
230 struct virtio_fs_vq
*fsvq
= &fs
->vqs
[i
];
235 fuse_dev_free(fsvq
->fud
);
240 /* Read filesystem name from virtio config into fs->tag (must kfree()). */
241 static int virtio_fs_read_tag(struct virtio_device
*vdev
, struct virtio_fs
*fs
)
243 char tag_buf
[sizeof_field(struct virtio_fs_config
, tag
)];
247 virtio_cread_bytes(vdev
, offsetof(struct virtio_fs_config
, tag
),
248 &tag_buf
, sizeof(tag_buf
));
249 end
= memchr(tag_buf
, '\0', sizeof(tag_buf
));
251 return -EINVAL
; /* empty tag */
253 end
= &tag_buf
[sizeof(tag_buf
)];
256 fs
->tag
= devm_kmalloc(&vdev
->dev
, len
+ 1, GFP_KERNEL
);
259 memcpy(fs
->tag
, tag_buf
, len
);
264 /* Work function for hiprio completion */
265 static void virtio_fs_hiprio_done_work(struct work_struct
*work
)
267 struct virtio_fs_vq
*fsvq
= container_of(work
, struct virtio_fs_vq
,
269 struct virtqueue
*vq
= fsvq
->vq
;
271 /* Free completed FUSE_FORGET requests */
272 spin_lock(&fsvq
->lock
);
277 virtqueue_disable_cb(vq
);
279 while ((req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
281 dec_in_flight_req(fsvq
);
283 } while (!virtqueue_enable_cb(vq
) && likely(!virtqueue_is_broken(vq
)));
284 spin_unlock(&fsvq
->lock
);
287 static void virtio_fs_request_dispatch_work(struct work_struct
*work
)
289 struct fuse_req
*req
;
290 struct virtio_fs_vq
*fsvq
= container_of(work
, struct virtio_fs_vq
,
292 struct fuse_conn
*fc
= fsvq
->fud
->fc
;
295 pr_debug("virtio-fs: worker %s called.\n", __func__
);
297 spin_lock(&fsvq
->lock
);
298 req
= list_first_entry_or_null(&fsvq
->end_reqs
, struct fuse_req
,
301 spin_unlock(&fsvq
->lock
);
305 list_del_init(&req
->list
);
306 spin_unlock(&fsvq
->lock
);
307 fuse_request_end(fc
, req
);
310 /* Dispatch pending requests */
312 spin_lock(&fsvq
->lock
);
313 req
= list_first_entry_or_null(&fsvq
->queued_reqs
,
314 struct fuse_req
, list
);
316 spin_unlock(&fsvq
->lock
);
319 list_del_init(&req
->list
);
320 spin_unlock(&fsvq
->lock
);
322 ret
= virtio_fs_enqueue_req(fsvq
, req
, true);
324 if (ret
== -ENOMEM
|| ret
== -ENOSPC
) {
325 spin_lock(&fsvq
->lock
);
326 list_add_tail(&req
->list
, &fsvq
->queued_reqs
);
327 schedule_delayed_work(&fsvq
->dispatch_work
,
328 msecs_to_jiffies(1));
329 spin_unlock(&fsvq
->lock
);
332 req
->out
.h
.error
= ret
;
333 spin_lock(&fsvq
->lock
);
334 dec_in_flight_req(fsvq
);
335 spin_unlock(&fsvq
->lock
);
336 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n",
338 fuse_request_end(fc
, req
);
344 * Returns 1 if queue is full and sender should wait a bit before sending
345 * next request, 0 otherwise.
347 static int send_forget_request(struct virtio_fs_vq
*fsvq
,
348 struct virtio_fs_forget
*forget
,
351 struct scatterlist sg
;
352 struct virtqueue
*vq
;
355 struct virtio_fs_forget_req
*req
= &forget
->req
;
357 spin_lock(&fsvq
->lock
);
358 if (!fsvq
->connected
) {
360 dec_in_flight_req(fsvq
);
365 sg_init_one(&sg
, req
, sizeof(*req
));
367 dev_dbg(&vq
->vdev
->dev
, "%s\n", __func__
);
369 ret
= virtqueue_add_outbuf(vq
, &sg
, 1, forget
, GFP_ATOMIC
);
371 if (ret
== -ENOMEM
|| ret
== -ENOSPC
) {
372 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
374 list_add_tail(&forget
->list
, &fsvq
->queued_reqs
);
375 schedule_delayed_work(&fsvq
->dispatch_work
,
376 msecs_to_jiffies(1));
378 inc_in_flight_req(fsvq
);
382 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
386 dec_in_flight_req(fsvq
);
392 inc_in_flight_req(fsvq
);
393 notify
= virtqueue_kick_prepare(vq
);
394 spin_unlock(&fsvq
->lock
);
397 virtqueue_notify(vq
);
400 spin_unlock(&fsvq
->lock
);
404 static void virtio_fs_hiprio_dispatch_work(struct work_struct
*work
)
406 struct virtio_fs_forget
*forget
;
407 struct virtio_fs_vq
*fsvq
= container_of(work
, struct virtio_fs_vq
,
409 pr_debug("virtio-fs: worker %s called.\n", __func__
);
411 spin_lock(&fsvq
->lock
);
412 forget
= list_first_entry_or_null(&fsvq
->queued_reqs
,
413 struct virtio_fs_forget
, list
);
415 spin_unlock(&fsvq
->lock
);
419 list_del(&forget
->list
);
420 spin_unlock(&fsvq
->lock
);
421 if (send_forget_request(fsvq
, forget
, true))
426 /* Allocate and copy args into req->argbuf */
427 static int copy_args_to_argbuf(struct fuse_req
*req
)
429 struct fuse_args
*args
= req
->args
;
430 unsigned int offset
= 0;
432 unsigned int num_out
;
436 num_in
= args
->in_numargs
- args
->in_pages
;
437 num_out
= args
->out_numargs
- args
->out_pages
;
438 len
= fuse_len_args(num_in
, (struct fuse_arg
*) args
->in_args
) +
439 fuse_len_args(num_out
, args
->out_args
);
441 req
->argbuf
= kmalloc(len
, GFP_ATOMIC
);
445 for (i
= 0; i
< num_in
; i
++) {
446 memcpy(req
->argbuf
+ offset
,
447 args
->in_args
[i
].value
,
448 args
->in_args
[i
].size
);
449 offset
+= args
->in_args
[i
].size
;
455 /* Copy args out of and free req->argbuf */
456 static void copy_args_from_argbuf(struct fuse_args
*args
, struct fuse_req
*req
)
458 unsigned int remaining
;
461 unsigned int num_out
;
464 remaining
= req
->out
.h
.len
- sizeof(req
->out
.h
);
465 num_in
= args
->in_numargs
- args
->in_pages
;
466 num_out
= args
->out_numargs
- args
->out_pages
;
467 offset
= fuse_len_args(num_in
, (struct fuse_arg
*)args
->in_args
);
469 for (i
= 0; i
< num_out
; i
++) {
470 unsigned int argsize
= args
->out_args
[i
].size
;
472 if (args
->out_argvar
&&
473 i
== args
->out_numargs
- 1 &&
474 argsize
> remaining
) {
478 memcpy(args
->out_args
[i
].value
, req
->argbuf
+ offset
, argsize
);
481 if (i
!= args
->out_numargs
- 1)
482 remaining
-= argsize
;
485 /* Store the actual size of the variable-length arg */
486 if (args
->out_argvar
)
487 args
->out_args
[args
->out_numargs
- 1].size
= remaining
;
493 /* Work function for request completion */
494 static void virtio_fs_request_complete(struct fuse_req
*req
,
495 struct virtio_fs_vq
*fsvq
)
497 struct fuse_pqueue
*fpq
= &fsvq
->fud
->pq
;
498 struct fuse_conn
*fc
= fsvq
->fud
->fc
;
499 struct fuse_args
*args
;
500 struct fuse_args_pages
*ap
;
501 unsigned int len
, i
, thislen
;
505 * TODO verify that server properly follows FUSE protocol
509 copy_args_from_argbuf(args
, req
);
511 if (args
->out_pages
&& args
->page_zeroing
) {
512 len
= args
->out_args
[args
->out_numargs
- 1].size
;
513 ap
= container_of(args
, typeof(*ap
), args
);
514 for (i
= 0; i
< ap
->num_pages
; i
++) {
515 thislen
= ap
->descs
[i
].length
;
517 WARN_ON(ap
->descs
[i
].offset
);
519 zero_user_segment(page
, len
, thislen
);
527 spin_lock(&fpq
->lock
);
528 clear_bit(FR_SENT
, &req
->flags
);
529 spin_unlock(&fpq
->lock
);
531 fuse_request_end(fc
, req
);
532 spin_lock(&fsvq
->lock
);
533 dec_in_flight_req(fsvq
);
534 spin_unlock(&fsvq
->lock
);
537 static void virtio_fs_complete_req_work(struct work_struct
*work
)
539 struct virtio_fs_req_work
*w
=
540 container_of(work
, typeof(*w
), done_work
);
542 virtio_fs_request_complete(w
->req
, w
->fsvq
);
546 static void virtio_fs_requests_done_work(struct work_struct
*work
)
548 struct virtio_fs_vq
*fsvq
= container_of(work
, struct virtio_fs_vq
,
550 struct fuse_pqueue
*fpq
= &fsvq
->fud
->pq
;
551 struct virtqueue
*vq
= fsvq
->vq
;
552 struct fuse_req
*req
;
553 struct fuse_req
*next
;
557 /* Collect completed requests off the virtqueue */
558 spin_lock(&fsvq
->lock
);
560 virtqueue_disable_cb(vq
);
562 while ((req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
563 spin_lock(&fpq
->lock
);
564 list_move_tail(&req
->list
, &reqs
);
565 spin_unlock(&fpq
->lock
);
567 } while (!virtqueue_enable_cb(vq
) && likely(!virtqueue_is_broken(vq
)));
568 spin_unlock(&fsvq
->lock
);
571 list_for_each_entry_safe(req
, next
, &reqs
, list
) {
572 list_del_init(&req
->list
);
574 /* blocking async request completes in a worker context */
575 if (req
->args
->may_block
) {
576 struct virtio_fs_req_work
*w
;
578 w
= kzalloc(sizeof(*w
), GFP_NOFS
| __GFP_NOFAIL
);
579 INIT_WORK(&w
->done_work
, virtio_fs_complete_req_work
);
582 schedule_work(&w
->done_work
);
584 virtio_fs_request_complete(req
, fsvq
);
589 /* Virtqueue interrupt handler */
590 static void virtio_fs_vq_done(struct virtqueue
*vq
)
592 struct virtio_fs_vq
*fsvq
= vq_to_fsvq(vq
);
594 dev_dbg(&vq
->vdev
->dev
, "%s %s\n", __func__
, fsvq
->name
);
596 schedule_work(&fsvq
->done_work
);
599 /* Initialize virtqueues */
600 static int virtio_fs_setup_vqs(struct virtio_device
*vdev
,
601 struct virtio_fs
*fs
)
603 struct virtqueue
**vqs
;
604 vq_callback_t
**callbacks
;
609 virtio_cread(vdev
, struct virtio_fs_config
, num_request_queues
,
610 &fs
->num_request_queues
);
611 if (fs
->num_request_queues
== 0)
614 fs
->nvqs
= 1 + fs
->num_request_queues
;
615 fs
->vqs
= kcalloc(fs
->nvqs
, sizeof(fs
->vqs
[VQ_HIPRIO
]), GFP_KERNEL
);
619 vqs
= kmalloc_array(fs
->nvqs
, sizeof(vqs
[VQ_HIPRIO
]), GFP_KERNEL
);
620 callbacks
= kmalloc_array(fs
->nvqs
, sizeof(callbacks
[VQ_HIPRIO
]),
622 names
= kmalloc_array(fs
->nvqs
, sizeof(names
[VQ_HIPRIO
]), GFP_KERNEL
);
623 if (!vqs
|| !callbacks
|| !names
) {
628 callbacks
[VQ_HIPRIO
] = virtio_fs_vq_done
;
629 snprintf(fs
->vqs
[VQ_HIPRIO
].name
, sizeof(fs
->vqs
[VQ_HIPRIO
].name
),
631 names
[VQ_HIPRIO
] = fs
->vqs
[VQ_HIPRIO
].name
;
632 INIT_WORK(&fs
->vqs
[VQ_HIPRIO
].done_work
, virtio_fs_hiprio_done_work
);
633 INIT_LIST_HEAD(&fs
->vqs
[VQ_HIPRIO
].queued_reqs
);
634 INIT_LIST_HEAD(&fs
->vqs
[VQ_HIPRIO
].end_reqs
);
635 INIT_DELAYED_WORK(&fs
->vqs
[VQ_HIPRIO
].dispatch_work
,
636 virtio_fs_hiprio_dispatch_work
);
637 init_completion(&fs
->vqs
[VQ_HIPRIO
].in_flight_zero
);
638 spin_lock_init(&fs
->vqs
[VQ_HIPRIO
].lock
);
640 /* Initialize the requests virtqueues */
641 for (i
= VQ_REQUEST
; i
< fs
->nvqs
; i
++) {
642 spin_lock_init(&fs
->vqs
[i
].lock
);
643 INIT_WORK(&fs
->vqs
[i
].done_work
, virtio_fs_requests_done_work
);
644 INIT_DELAYED_WORK(&fs
->vqs
[i
].dispatch_work
,
645 virtio_fs_request_dispatch_work
);
646 INIT_LIST_HEAD(&fs
->vqs
[i
].queued_reqs
);
647 INIT_LIST_HEAD(&fs
->vqs
[i
].end_reqs
);
648 init_completion(&fs
->vqs
[i
].in_flight_zero
);
649 snprintf(fs
->vqs
[i
].name
, sizeof(fs
->vqs
[i
].name
),
650 "requests.%u", i
- VQ_REQUEST
);
651 callbacks
[i
] = virtio_fs_vq_done
;
652 names
[i
] = fs
->vqs
[i
].name
;
655 ret
= virtio_find_vqs(vdev
, fs
->nvqs
, vqs
, callbacks
, names
, NULL
);
659 for (i
= 0; i
< fs
->nvqs
; i
++)
660 fs
->vqs
[i
].vq
= vqs
[i
];
662 virtio_fs_start_all_queues(fs
);
672 /* Free virtqueues (device must already be reset) */
673 static void virtio_fs_cleanup_vqs(struct virtio_device
*vdev
,
674 struct virtio_fs
*fs
)
676 vdev
->config
->del_vqs(vdev
);
679 static int virtio_fs_probe(struct virtio_device
*vdev
)
681 struct virtio_fs
*fs
;
684 fs
= kzalloc(sizeof(*fs
), GFP_KERNEL
);
687 kref_init(&fs
->refcount
);
690 ret
= virtio_fs_read_tag(vdev
, fs
);
694 ret
= virtio_fs_setup_vqs(vdev
, fs
);
698 /* TODO vq affinity */
700 /* Bring the device online in case the filesystem is mounted and
701 * requests need to be sent before we return.
703 virtio_device_ready(vdev
);
705 ret
= virtio_fs_add_instance(fs
);
712 vdev
->config
->reset(vdev
);
713 virtio_fs_cleanup_vqs(vdev
, fs
);
721 static void virtio_fs_stop_all_queues(struct virtio_fs
*fs
)
723 struct virtio_fs_vq
*fsvq
;
726 for (i
= 0; i
< fs
->nvqs
; i
++) {
728 spin_lock(&fsvq
->lock
);
729 fsvq
->connected
= false;
730 spin_unlock(&fsvq
->lock
);
734 static void virtio_fs_remove(struct virtio_device
*vdev
)
736 struct virtio_fs
*fs
= vdev
->priv
;
738 mutex_lock(&virtio_fs_mutex
);
739 /* This device is going away. No one should get new reference */
740 list_del_init(&fs
->list
);
741 virtio_fs_stop_all_queues(fs
);
742 virtio_fs_drain_all_queues_locked(fs
);
743 vdev
->config
->reset(vdev
);
744 virtio_fs_cleanup_vqs(vdev
, fs
);
747 /* Put device reference on virtio_fs object */
749 mutex_unlock(&virtio_fs_mutex
);
752 #ifdef CONFIG_PM_SLEEP
753 static int virtio_fs_freeze(struct virtio_device
*vdev
)
755 /* TODO need to save state here */
756 pr_warn("virtio-fs: suspend/resume not yet supported\n");
760 static int virtio_fs_restore(struct virtio_device
*vdev
)
762 /* TODO need to restore state here */
765 #endif /* CONFIG_PM_SLEEP */
767 static const struct virtio_device_id id_table
[] = {
768 { VIRTIO_ID_FS
, VIRTIO_DEV_ANY_ID
},
772 static const unsigned int feature_table
[] = {};
774 static struct virtio_driver virtio_fs_driver
= {
775 .driver
.name
= KBUILD_MODNAME
,
776 .driver
.owner
= THIS_MODULE
,
777 .id_table
= id_table
,
778 .feature_table
= feature_table
,
779 .feature_table_size
= ARRAY_SIZE(feature_table
),
780 .probe
= virtio_fs_probe
,
781 .remove
= virtio_fs_remove
,
782 #ifdef CONFIG_PM_SLEEP
783 .freeze
= virtio_fs_freeze
,
784 .restore
= virtio_fs_restore
,
788 static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue
*fiq
)
789 __releases(fiq
->lock
)
791 struct fuse_forget_link
*link
;
792 struct virtio_fs_forget
*forget
;
793 struct virtio_fs_forget_req
*req
;
794 struct virtio_fs
*fs
;
795 struct virtio_fs_vq
*fsvq
;
798 link
= fuse_dequeue_forget(fiq
, 1, NULL
);
799 unique
= fuse_get_unique(fiq
);
802 fsvq
= &fs
->vqs
[VQ_HIPRIO
];
803 spin_unlock(&fiq
->lock
);
805 /* Allocate a buffer for the request */
806 forget
= kmalloc(sizeof(*forget
), GFP_NOFS
| __GFP_NOFAIL
);
809 req
->ih
= (struct fuse_in_header
){
810 .opcode
= FUSE_FORGET
,
811 .nodeid
= link
->forget_one
.nodeid
,
815 req
->arg
= (struct fuse_forget_in
){
816 .nlookup
= link
->forget_one
.nlookup
,
819 send_forget_request(fsvq
, forget
, false);
823 static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue
*fiq
)
824 __releases(fiq
->lock
)
829 * Normal fs operations on a local filesystems aren't interruptible.
830 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
831 * with shared lock between host and guest.
833 spin_unlock(&fiq
->lock
);
836 /* Return the number of scatter-gather list elements required */
837 static unsigned int sg_count_fuse_req(struct fuse_req
*req
)
839 struct fuse_args
*args
= req
->args
;
840 struct fuse_args_pages
*ap
= container_of(args
, typeof(*ap
), args
);
841 unsigned int total_sgs
= 1 /* fuse_in_header */;
843 if (args
->in_numargs
- args
->in_pages
)
847 total_sgs
+= ap
->num_pages
;
849 if (!test_bit(FR_ISREPLY
, &req
->flags
))
852 total_sgs
+= 1 /* fuse_out_header */;
854 if (args
->out_numargs
- args
->out_pages
)
858 total_sgs
+= ap
->num_pages
;
863 /* Add pages to scatter-gather list and return number of elements used */
864 static unsigned int sg_init_fuse_pages(struct scatterlist
*sg
,
866 struct fuse_page_desc
*page_descs
,
867 unsigned int num_pages
,
868 unsigned int total_len
)
871 unsigned int this_len
;
873 for (i
= 0; i
< num_pages
&& total_len
; i
++) {
874 sg_init_table(&sg
[i
], 1);
875 this_len
= min(page_descs
[i
].length
, total_len
);
876 sg_set_page(&sg
[i
], pages
[i
], this_len
, page_descs
[i
].offset
);
877 total_len
-= this_len
;
883 /* Add args to scatter-gather list and return number of elements used */
884 static unsigned int sg_init_fuse_args(struct scatterlist
*sg
,
885 struct fuse_req
*req
,
886 struct fuse_arg
*args
,
887 unsigned int numargs
,
890 unsigned int *len_used
)
892 struct fuse_args_pages
*ap
= container_of(req
->args
, typeof(*ap
), args
);
893 unsigned int total_sgs
= 0;
896 len
= fuse_len_args(numargs
- argpages
, args
);
898 sg_init_one(&sg
[total_sgs
++], argbuf
, len
);
901 total_sgs
+= sg_init_fuse_pages(&sg
[total_sgs
],
902 ap
->pages
, ap
->descs
,
904 args
[numargs
- 1].size
);
912 /* Add a request to a virtqueue and kick the device */
913 static int virtio_fs_enqueue_req(struct virtio_fs_vq
*fsvq
,
914 struct fuse_req
*req
, bool in_flight
)
916 /* requests need at least 4 elements */
917 struct scatterlist
*stack_sgs
[6];
918 struct scatterlist stack_sg
[ARRAY_SIZE(stack_sgs
)];
919 struct scatterlist
**sgs
= stack_sgs
;
920 struct scatterlist
*sg
= stack_sg
;
921 struct virtqueue
*vq
;
922 struct fuse_args
*args
= req
->args
;
923 unsigned int argbuf_used
= 0;
924 unsigned int out_sgs
= 0;
925 unsigned int in_sgs
= 0;
926 unsigned int total_sgs
;
930 struct fuse_pqueue
*fpq
;
932 /* Does the sglist fit on the stack? */
933 total_sgs
= sg_count_fuse_req(req
);
934 if (total_sgs
> ARRAY_SIZE(stack_sgs
)) {
935 sgs
= kmalloc_array(total_sgs
, sizeof(sgs
[0]), GFP_ATOMIC
);
936 sg
= kmalloc_array(total_sgs
, sizeof(sg
[0]), GFP_ATOMIC
);
943 /* Use a bounce buffer since stack args cannot be mapped */
944 ret
= copy_args_to_argbuf(req
);
948 /* Request elements */
949 sg_init_one(&sg
[out_sgs
++], &req
->in
.h
, sizeof(req
->in
.h
));
950 out_sgs
+= sg_init_fuse_args(&sg
[out_sgs
], req
,
951 (struct fuse_arg
*)args
->in_args
,
952 args
->in_numargs
, args
->in_pages
,
953 req
->argbuf
, &argbuf_used
);
956 if (test_bit(FR_ISREPLY
, &req
->flags
)) {
957 sg_init_one(&sg
[out_sgs
+ in_sgs
++],
958 &req
->out
.h
, sizeof(req
->out
.h
));
959 in_sgs
+= sg_init_fuse_args(&sg
[out_sgs
+ in_sgs
], req
,
960 args
->out_args
, args
->out_numargs
,
962 req
->argbuf
+ argbuf_used
, NULL
);
965 WARN_ON(out_sgs
+ in_sgs
!= total_sgs
);
967 for (i
= 0; i
< total_sgs
; i
++)
970 spin_lock(&fsvq
->lock
);
972 if (!fsvq
->connected
) {
973 spin_unlock(&fsvq
->lock
);
979 ret
= virtqueue_add_sgs(vq
, sgs
, out_sgs
, in_sgs
, req
, GFP_ATOMIC
);
981 spin_unlock(&fsvq
->lock
);
985 /* Request successfully sent. */
986 fpq
= &fsvq
->fud
->pq
;
987 spin_lock(&fpq
->lock
);
988 list_add_tail(&req
->list
, fpq
->processing
);
989 spin_unlock(&fpq
->lock
);
990 set_bit(FR_SENT
, &req
->flags
);
991 /* matches barrier in request_wait_answer() */
992 smp_mb__after_atomic();
995 inc_in_flight_req(fsvq
);
996 notify
= virtqueue_kick_prepare(vq
);
998 spin_unlock(&fsvq
->lock
);
1001 virtqueue_notify(vq
);
1004 if (ret
< 0 && req
->argbuf
) {
1008 if (sgs
!= stack_sgs
) {
1016 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue
*fiq
)
1017 __releases(fiq
->lock
)
1019 unsigned int queue_id
= VQ_REQUEST
; /* TODO multiqueue */
1020 struct virtio_fs
*fs
;
1021 struct fuse_req
*req
;
1022 struct virtio_fs_vq
*fsvq
;
1025 WARN_ON(list_empty(&fiq
->pending
));
1026 req
= list_last_entry(&fiq
->pending
, struct fuse_req
, list
);
1027 clear_bit(FR_PENDING
, &req
->flags
);
1028 list_del_init(&req
->list
);
1029 WARN_ON(!list_empty(&fiq
->pending
));
1030 spin_unlock(&fiq
->lock
);
1034 pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
1035 __func__
, req
->in
.h
.opcode
, req
->in
.h
.unique
,
1036 req
->in
.h
.nodeid
, req
->in
.h
.len
,
1037 fuse_len_args(req
->args
->out_numargs
, req
->args
->out_args
));
1039 fsvq
= &fs
->vqs
[queue_id
];
1040 ret
= virtio_fs_enqueue_req(fsvq
, req
, false);
1042 if (ret
== -ENOMEM
|| ret
== -ENOSPC
) {
1044 * Virtqueue full. Retry submission from worker
1045 * context as we might be holding fc->bg_lock.
1047 spin_lock(&fsvq
->lock
);
1048 list_add_tail(&req
->list
, &fsvq
->queued_reqs
);
1049 inc_in_flight_req(fsvq
);
1050 schedule_delayed_work(&fsvq
->dispatch_work
,
1051 msecs_to_jiffies(1));
1052 spin_unlock(&fsvq
->lock
);
1055 req
->out
.h
.error
= ret
;
1056 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret
);
1058 /* Can't end request in submission context. Use a worker */
1059 spin_lock(&fsvq
->lock
);
1060 list_add_tail(&req
->list
, &fsvq
->end_reqs
);
1061 schedule_delayed_work(&fsvq
->dispatch_work
, 0);
1062 spin_unlock(&fsvq
->lock
);
1067 static const struct fuse_iqueue_ops virtio_fs_fiq_ops
= {
1068 .wake_forget_and_unlock
= virtio_fs_wake_forget_and_unlock
,
1069 .wake_interrupt_and_unlock
= virtio_fs_wake_interrupt_and_unlock
,
1070 .wake_pending_and_unlock
= virtio_fs_wake_pending_and_unlock
,
1071 .release
= virtio_fs_fiq_release
,
1074 static int virtio_fs_fill_super(struct super_block
*sb
)
1076 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
1077 struct virtio_fs
*fs
= fc
->iq
.priv
;
1080 struct fuse_fs_context ctx
= {
1081 .rootmode
= S_IFDIR
,
1082 .default_permissions
= 1,
1084 .max_read
= UINT_MAX
,
1088 .no_force_umount
= true,
1089 .no_mount_options
= true,
1092 mutex_lock(&virtio_fs_mutex
);
1094 /* After holding mutex, make sure virtiofs device is still there.
1095 * Though we are holding a reference to it, drive ->remove might
1096 * still have cleaned up virtual queues. In that case bail out.
1099 if (list_empty(&fs
->list
)) {
1100 pr_info("virtio-fs: tag <%s> not found\n", fs
->tag
);
1105 /* Allocate fuse_dev for hiprio and notification queues */
1106 for (i
= 0; i
< VQ_REQUEST
; i
++) {
1107 struct virtio_fs_vq
*fsvq
= &fs
->vqs
[i
];
1109 fsvq
->fud
= fuse_dev_alloc();
1111 goto err_free_fuse_devs
;
1114 ctx
.fudptr
= (void **)&fs
->vqs
[VQ_REQUEST
].fud
;
1115 err
= fuse_fill_super_common(sb
, &ctx
);
1117 goto err_free_fuse_devs
;
1119 fc
= fs
->vqs
[VQ_REQUEST
].fud
->fc
;
1121 for (i
= 0; i
< fs
->nvqs
; i
++) {
1122 struct virtio_fs_vq
*fsvq
= &fs
->vqs
[i
];
1124 if (i
== VQ_REQUEST
)
1125 continue; /* already initialized */
1126 fuse_dev_install(fsvq
->fud
, fc
);
1129 /* Previous unmount will stop all queues. Start these again */
1130 virtio_fs_start_all_queues(fs
);
1132 mutex_unlock(&virtio_fs_mutex
);
1136 virtio_fs_free_devs(fs
);
1138 mutex_unlock(&virtio_fs_mutex
);
1142 static void virtio_kill_sb(struct super_block
*sb
)
1144 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
1145 struct virtio_fs
*vfs
;
1146 struct virtio_fs_vq
*fsvq
;
1148 /* If mount failed, we can still be called without any fc */
1150 return fuse_kill_sb_anon(sb
);
1153 fsvq
= &vfs
->vqs
[VQ_HIPRIO
];
1155 /* Stop forget queue. Soon destroy will be sent */
1156 spin_lock(&fsvq
->lock
);
1157 fsvq
->connected
= false;
1158 spin_unlock(&fsvq
->lock
);
1159 virtio_fs_drain_all_queues(vfs
);
1161 fuse_kill_sb_anon(sb
);
1163 /* fuse_kill_sb_anon() must have sent destroy. Stop all queues
1164 * and drain one more time and free fuse devices. Freeing fuse
1165 * devices will drop their reference on fuse_conn and that in
1166 * turn will drop its reference on virtio_fs object.
1168 virtio_fs_stop_all_queues(vfs
);
1169 virtio_fs_drain_all_queues(vfs
);
1170 virtio_fs_free_devs(vfs
);
1173 static int virtio_fs_test_super(struct super_block
*sb
,
1174 struct fs_context
*fsc
)
1176 struct fuse_conn
*fc
= fsc
->s_fs_info
;
1178 return fc
->iq
.priv
== get_fuse_conn_super(sb
)->iq
.priv
;
1181 static int virtio_fs_set_super(struct super_block
*sb
,
1182 struct fs_context
*fsc
)
1186 err
= get_anon_bdev(&sb
->s_dev
);
1188 fuse_conn_get(fsc
->s_fs_info
);
1193 static int virtio_fs_get_tree(struct fs_context
*fsc
)
1195 struct virtio_fs
*fs
;
1196 struct super_block
*sb
;
1197 struct fuse_conn
*fc
;
1200 /* This gets a reference on virtio_fs object. This ptr gets installed
1201 * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1202 * to drop the reference to this object.
1204 fs
= virtio_fs_find_instance(fsc
->source
);
1206 pr_info("virtio-fs: tag <%s> not found\n", fsc
->source
);
1210 fc
= kzalloc(sizeof(struct fuse_conn
), GFP_KERNEL
);
1212 mutex_lock(&virtio_fs_mutex
);
1214 mutex_unlock(&virtio_fs_mutex
);
1218 fuse_conn_init(fc
, get_user_ns(current_user_ns()), &virtio_fs_fiq_ops
,
1220 fc
->release
= fuse_free_conn
;
1221 fc
->delete_stale
= true;
1223 fsc
->s_fs_info
= fc
;
1224 sb
= sget_fc(fsc
, virtio_fs_test_super
, virtio_fs_set_super
);
1230 err
= virtio_fs_fill_super(sb
);
1232 deactivate_locked_super(sb
);
1236 sb
->s_flags
|= SB_ACTIVE
;
1240 fsc
->root
= dget(sb
->s_root
);
1244 static const struct fs_context_operations virtio_fs_context_ops
= {
1245 .get_tree
= virtio_fs_get_tree
,
1248 static int virtio_fs_init_fs_context(struct fs_context
*fsc
)
1250 fsc
->ops
= &virtio_fs_context_ops
;
1254 static struct file_system_type virtio_fs_type
= {
1255 .owner
= THIS_MODULE
,
1257 .init_fs_context
= virtio_fs_init_fs_context
,
1258 .kill_sb
= virtio_kill_sb
,
1261 static int __init
virtio_fs_init(void)
1265 ret
= register_virtio_driver(&virtio_fs_driver
);
1269 ret
= register_filesystem(&virtio_fs_type
);
1271 unregister_virtio_driver(&virtio_fs_driver
);
1277 module_init(virtio_fs_init
);
1279 static void __exit
virtio_fs_exit(void)
1281 unregister_filesystem(&virtio_fs_type
);
1282 unregister_virtio_driver(&virtio_fs_driver
);
1284 module_exit(virtio_fs_exit
);
1286 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1287 MODULE_DESCRIPTION("Virtio Filesystem");
1288 MODULE_LICENSE("GPL");
1289 MODULE_ALIAS_FS(KBUILD_MODNAME
);
1290 MODULE_DEVICE_TABLE(virtio
, id_table
);