1a62a8ef9SStefan Hajnoczi // SPDX-License-Identifier: GPL-2.0
2a62a8ef9SStefan Hajnoczi /*
3a62a8ef9SStefan Hajnoczi * virtio-fs: Virtio Filesystem
4a62a8ef9SStefan Hajnoczi * Copyright (C) 2018 Red Hat, Inc.
5a62a8ef9SStefan Hajnoczi */
6a62a8ef9SStefan Hajnoczi
7a62a8ef9SStefan Hajnoczi #include <linux/fs.h>
822f3787eSStefan Hajnoczi #include <linux/dax.h>
922f3787eSStefan Hajnoczi #include <linux/pci.h>
10529395d2SPeter-Jan Gootzen #include <linux/interrupt.h>
11529395d2SPeter-Jan Gootzen #include <linux/group_cpus.h>
1222f3787eSStefan Hajnoczi #include <linux/pfn_t.h>
13dc90f084SChristoph Hellwig #include <linux/memremap.h>
14a62a8ef9SStefan Hajnoczi #include <linux/module.h>
15a62a8ef9SStefan Hajnoczi #include <linux/virtio.h>
16a62a8ef9SStefan Hajnoczi #include <linux/virtio_fs.h>
17a62a8ef9SStefan Hajnoczi #include <linux/delay.h>
18a62a8ef9SStefan Hajnoczi #include <linux/fs_context.h>
191dd53957SVivek Goyal #include <linux/fs_parser.h>
20a62a8ef9SStefan Hajnoczi #include <linux/highmem.h>
21562ce828SMathieu Desnoyers #include <linux/cleanup.h>
2222f3787eSStefan Hajnoczi #include <linux/uio.h>
23a62a8ef9SStefan Hajnoczi #include "fuse_i.h"
24a62a8ef9SStefan Hajnoczi
25a7f0d7aaSConnor Kuehl /* Used to help calculate the FUSE connection's max_pages limit for a request's
26a7f0d7aaSConnor Kuehl * size. Parts of the struct fuse_req are sliced into scattergather lists in
27a7f0d7aaSConnor Kuehl * addition to the pages used, so this can help account for that overhead.
28a7f0d7aaSConnor Kuehl */
29a7f0d7aaSConnor Kuehl #define FUSE_HEADER_OVERHEAD 4
30a7f0d7aaSConnor Kuehl
31a62a8ef9SStefan Hajnoczi /* List of virtio-fs device instances and a lock for the list. Also provides
32a62a8ef9SStefan Hajnoczi * mutual exclusion in device removal and mounting path
33a62a8ef9SStefan Hajnoczi */
34a62a8ef9SStefan Hajnoczi static DEFINE_MUTEX(virtio_fs_mutex);
35a62a8ef9SStefan Hajnoczi static LIST_HEAD(virtio_fs_instances);
36a62a8ef9SStefan Hajnoczi
37a8f62f50SStefan Hajnoczi /* The /sys/fs/virtio_fs/ kset */
38a8f62f50SStefan Hajnoczi static struct kset *virtio_fs_kset;
39a8f62f50SStefan Hajnoczi
40a62a8ef9SStefan Hajnoczi enum {
41a62a8ef9SStefan Hajnoczi VQ_HIPRIO,
42a62a8ef9SStefan Hajnoczi VQ_REQUEST
43a62a8ef9SStefan Hajnoczi };
44a62a8ef9SStefan Hajnoczi
45b43b7e81SVivek Goyal #define VQ_NAME_LEN 24
46b43b7e81SVivek Goyal
47a62a8ef9SStefan Hajnoczi /* Per-virtqueue state */
48a62a8ef9SStefan Hajnoczi struct virtio_fs_vq {
49a62a8ef9SStefan Hajnoczi spinlock_t lock;
50a62a8ef9SStefan Hajnoczi struct virtqueue *vq; /* protected by ->lock */
51a62a8ef9SStefan Hajnoczi struct work_struct done_work;
52a62a8ef9SStefan Hajnoczi struct list_head queued_reqs;
5351fecdd2SVivek Goyal struct list_head end_reqs; /* End these requests */
54106e4df1SPeter-Jan Gootzen struct work_struct dispatch_work;
55a62a8ef9SStefan Hajnoczi struct fuse_dev *fud;
56a62a8ef9SStefan Hajnoczi bool connected;
57a62a8ef9SStefan Hajnoczi long in_flight;
58724c15a4SVivek Goyal struct completion in_flight_zero; /* No inflight requests */
59*87cbdc39SMax Gurtovoy struct kobject *kobj;
60b43b7e81SVivek Goyal char name[VQ_NAME_LEN];
61a62a8ef9SStefan Hajnoczi } ____cacheline_aligned_in_smp;
62a62a8ef9SStefan Hajnoczi
63a62a8ef9SStefan Hajnoczi /* A virtio-fs device instance */
64a62a8ef9SStefan Hajnoczi struct virtio_fs {
65a8f62f50SStefan Hajnoczi struct kobject kobj;
66*87cbdc39SMax Gurtovoy struct kobject *mqs_kobj;
67a62a8ef9SStefan Hajnoczi struct list_head list; /* on virtio_fs_instances */
68a62a8ef9SStefan Hajnoczi char *tag;
69a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *vqs;
70a62a8ef9SStefan Hajnoczi unsigned int nvqs; /* number of virtqueues */
71a62a8ef9SStefan Hajnoczi unsigned int num_request_queues; /* number of request queues */
7222f3787eSStefan Hajnoczi struct dax_device *dax_dev;
7322f3787eSStefan Hajnoczi
74529395d2SPeter-Jan Gootzen unsigned int *mq_map; /* index = cpu id, value = request vq id */
75529395d2SPeter-Jan Gootzen
7622f3787eSStefan Hajnoczi /* DAX memory window where file contents are mapped */
7722f3787eSStefan Hajnoczi void *window_kaddr;
7822f3787eSStefan Hajnoczi phys_addr_t window_phys_addr;
7922f3787eSStefan Hajnoczi size_t window_len;
80a62a8ef9SStefan Hajnoczi };
81a62a8ef9SStefan Hajnoczi
821efcf39eSVivek Goyal struct virtio_fs_forget_req {
83a62a8ef9SStefan Hajnoczi struct fuse_in_header ih;
84a62a8ef9SStefan Hajnoczi struct fuse_forget_in arg;
851efcf39eSVivek Goyal };
861efcf39eSVivek Goyal
871efcf39eSVivek Goyal struct virtio_fs_forget {
88a62a8ef9SStefan Hajnoczi /* This request can be temporarily queued on virt queue */
89a62a8ef9SStefan Hajnoczi struct list_head list;
901efcf39eSVivek Goyal struct virtio_fs_forget_req req;
91a62a8ef9SStefan Hajnoczi };
92a62a8ef9SStefan Hajnoczi
93bb737bbeSVivek Goyal struct virtio_fs_req_work {
94bb737bbeSVivek Goyal struct fuse_req *req;
95bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq;
96bb737bbeSVivek Goyal struct work_struct done_work;
97bb737bbeSVivek Goyal };
98bb737bbeSVivek Goyal
99a9bfd9ddSVivek Goyal static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
100a9bfd9ddSVivek Goyal struct fuse_req *req, bool in_flight);
101a9bfd9ddSVivek Goyal
102780b1b95SJeffle Xu static const struct constant_table dax_param_enums[] = {
103780b1b95SJeffle Xu {"always", FUSE_DAX_ALWAYS },
104780b1b95SJeffle Xu {"never", FUSE_DAX_NEVER },
105780b1b95SJeffle Xu {"inode", FUSE_DAX_INODE_USER },
106780b1b95SJeffle Xu {}
107780b1b95SJeffle Xu };
108780b1b95SJeffle Xu
1091dd53957SVivek Goyal enum {
1101dd53957SVivek Goyal OPT_DAX,
111780b1b95SJeffle Xu OPT_DAX_ENUM,
1121dd53957SVivek Goyal };
1131dd53957SVivek Goyal
1141dd53957SVivek Goyal static const struct fs_parameter_spec virtio_fs_parameters[] = {
1151dd53957SVivek Goyal fsparam_flag("dax", OPT_DAX),
116780b1b95SJeffle Xu fsparam_enum("dax", OPT_DAX_ENUM, dax_param_enums),
1171dd53957SVivek Goyal {}
1181dd53957SVivek Goyal };
1191dd53957SVivek Goyal
virtio_fs_parse_param(struct fs_context * fsc,struct fs_parameter * param)12084c21507SMiklos Szeredi static int virtio_fs_parse_param(struct fs_context *fsc,
1211dd53957SVivek Goyal struct fs_parameter *param)
1221dd53957SVivek Goyal {
1231dd53957SVivek Goyal struct fs_parse_result result;
12484c21507SMiklos Szeredi struct fuse_fs_context *ctx = fsc->fs_private;
1251dd53957SVivek Goyal int opt;
1261dd53957SVivek Goyal
12784c21507SMiklos Szeredi opt = fs_parse(fsc, virtio_fs_parameters, param, &result);
1281dd53957SVivek Goyal if (opt < 0)
1291dd53957SVivek Goyal return opt;
1301dd53957SVivek Goyal
1311dd53957SVivek Goyal switch (opt) {
1321dd53957SVivek Goyal case OPT_DAX:
133780b1b95SJeffle Xu ctx->dax_mode = FUSE_DAX_ALWAYS;
134780b1b95SJeffle Xu break;
135780b1b95SJeffle Xu case OPT_DAX_ENUM:
136780b1b95SJeffle Xu ctx->dax_mode = result.uint_32;
1371dd53957SVivek Goyal break;
1381dd53957SVivek Goyal default:
1391dd53957SVivek Goyal return -EINVAL;
1401dd53957SVivek Goyal }
1411dd53957SVivek Goyal
1421dd53957SVivek Goyal return 0;
1431dd53957SVivek Goyal }
1441dd53957SVivek Goyal
virtio_fs_free_fsc(struct fs_context * fsc)14584c21507SMiklos Szeredi static void virtio_fs_free_fsc(struct fs_context *fsc)
1461dd53957SVivek Goyal {
14784c21507SMiklos Szeredi struct fuse_fs_context *ctx = fsc->fs_private;
1481dd53957SVivek Goyal
1491dd53957SVivek Goyal kfree(ctx);
1501dd53957SVivek Goyal }
1511dd53957SVivek Goyal
vq_to_fsvq(struct virtqueue * vq)152a62a8ef9SStefan Hajnoczi static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
153a62a8ef9SStefan Hajnoczi {
154a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = vq->vdev->priv;
155a62a8ef9SStefan Hajnoczi
156a62a8ef9SStefan Hajnoczi return &fs->vqs[vq->index];
157a62a8ef9SStefan Hajnoczi }
158a62a8ef9SStefan Hajnoczi
159c17ea009SVivek Goyal /* Should be called with fsvq->lock held. */
inc_in_flight_req(struct virtio_fs_vq * fsvq)160c17ea009SVivek Goyal static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq)
161c17ea009SVivek Goyal {
162c17ea009SVivek Goyal fsvq->in_flight++;
163c17ea009SVivek Goyal }
164c17ea009SVivek Goyal
165c17ea009SVivek Goyal /* Should be called with fsvq->lock held. */
dec_in_flight_req(struct virtio_fs_vq * fsvq)166c17ea009SVivek Goyal static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq)
167c17ea009SVivek Goyal {
168c17ea009SVivek Goyal WARN_ON(fsvq->in_flight <= 0);
169c17ea009SVivek Goyal fsvq->in_flight--;
170724c15a4SVivek Goyal if (!fsvq->in_flight)
171724c15a4SVivek Goyal complete(&fsvq->in_flight_zero);
172c17ea009SVivek Goyal }
173c17ea009SVivek Goyal
tag_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)174a8f62f50SStefan Hajnoczi static ssize_t tag_show(struct kobject *kobj,
175a8f62f50SStefan Hajnoczi struct kobj_attribute *attr, char *buf)
176a62a8ef9SStefan Hajnoczi {
177a8f62f50SStefan Hajnoczi struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj);
178a8f62f50SStefan Hajnoczi
17996d88f65SBrian Foster return sysfs_emit(buf, "%s\n", fs->tag);
180a8f62f50SStefan Hajnoczi }
181a8f62f50SStefan Hajnoczi
182a8f62f50SStefan Hajnoczi static struct kobj_attribute virtio_fs_tag_attr = __ATTR_RO(tag);
183a8f62f50SStefan Hajnoczi
184a8f62f50SStefan Hajnoczi static struct attribute *virtio_fs_attrs[] = {
185a8f62f50SStefan Hajnoczi &virtio_fs_tag_attr.attr,
186a8f62f50SStefan Hajnoczi NULL
187a8f62f50SStefan Hajnoczi };
188a8f62f50SStefan Hajnoczi ATTRIBUTE_GROUPS(virtio_fs);
189a8f62f50SStefan Hajnoczi
virtio_fs_ktype_release(struct kobject * kobj)190a8f62f50SStefan Hajnoczi static void virtio_fs_ktype_release(struct kobject *kobj)
191a8f62f50SStefan Hajnoczi {
192a8f62f50SStefan Hajnoczi struct virtio_fs *vfs = container_of(kobj, struct virtio_fs, kobj);
193a62a8ef9SStefan Hajnoczi
194529395d2SPeter-Jan Gootzen kfree(vfs->mq_map);
195a62a8ef9SStefan Hajnoczi kfree(vfs->vqs);
196a62a8ef9SStefan Hajnoczi kfree(vfs);
197a62a8ef9SStefan Hajnoczi }
198a62a8ef9SStefan Hajnoczi
199a8f62f50SStefan Hajnoczi static const struct kobj_type virtio_fs_ktype = {
200a8f62f50SStefan Hajnoczi .release = virtio_fs_ktype_release,
201a8f62f50SStefan Hajnoczi .sysfs_ops = &kobj_sysfs_ops,
202a8f62f50SStefan Hajnoczi .default_groups = virtio_fs_groups,
203a8f62f50SStefan Hajnoczi };
204a8f62f50SStefan Hajnoczi
virtio_fs_kobj_to_vq(struct virtio_fs * fs,struct kobject * kobj)205*87cbdc39SMax Gurtovoy static struct virtio_fs_vq *virtio_fs_kobj_to_vq(struct virtio_fs *fs,
206*87cbdc39SMax Gurtovoy struct kobject *kobj)
207*87cbdc39SMax Gurtovoy {
208*87cbdc39SMax Gurtovoy int i;
209*87cbdc39SMax Gurtovoy
210*87cbdc39SMax Gurtovoy for (i = 0; i < fs->nvqs; i++) {
211*87cbdc39SMax Gurtovoy if (kobj == fs->vqs[i].kobj)
212*87cbdc39SMax Gurtovoy return &fs->vqs[i];
213*87cbdc39SMax Gurtovoy }
214*87cbdc39SMax Gurtovoy return NULL;
215*87cbdc39SMax Gurtovoy }
216*87cbdc39SMax Gurtovoy
name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)217*87cbdc39SMax Gurtovoy static ssize_t name_show(struct kobject *kobj,
218*87cbdc39SMax Gurtovoy struct kobj_attribute *attr, char *buf)
219*87cbdc39SMax Gurtovoy {
220*87cbdc39SMax Gurtovoy struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj);
221*87cbdc39SMax Gurtovoy struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj);
222*87cbdc39SMax Gurtovoy
223*87cbdc39SMax Gurtovoy if (!fsvq)
224*87cbdc39SMax Gurtovoy return -EINVAL;
225*87cbdc39SMax Gurtovoy return sysfs_emit(buf, "%s\n", fsvq->name);
226*87cbdc39SMax Gurtovoy }
227*87cbdc39SMax Gurtovoy
228*87cbdc39SMax Gurtovoy static struct kobj_attribute virtio_fs_vq_name_attr = __ATTR_RO(name);
229*87cbdc39SMax Gurtovoy
cpu_list_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)230*87cbdc39SMax Gurtovoy static ssize_t cpu_list_show(struct kobject *kobj,
231*87cbdc39SMax Gurtovoy struct kobj_attribute *attr, char *buf)
232*87cbdc39SMax Gurtovoy {
233*87cbdc39SMax Gurtovoy struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj);
234*87cbdc39SMax Gurtovoy struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj);
235*87cbdc39SMax Gurtovoy unsigned int cpu, qid;
236*87cbdc39SMax Gurtovoy const size_t size = PAGE_SIZE - 1;
237*87cbdc39SMax Gurtovoy bool first = true;
238*87cbdc39SMax Gurtovoy int ret = 0, pos = 0;
239*87cbdc39SMax Gurtovoy
240*87cbdc39SMax Gurtovoy if (!fsvq)
241*87cbdc39SMax Gurtovoy return -EINVAL;
242*87cbdc39SMax Gurtovoy
243*87cbdc39SMax Gurtovoy qid = fsvq->vq->index;
244*87cbdc39SMax Gurtovoy for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
245*87cbdc39SMax Gurtovoy if (qid < VQ_REQUEST || (fs->mq_map[cpu] == qid - VQ_REQUEST)) {
246*87cbdc39SMax Gurtovoy if (first)
247*87cbdc39SMax Gurtovoy ret = snprintf(buf + pos, size - pos, "%u", cpu);
248*87cbdc39SMax Gurtovoy else
249*87cbdc39SMax Gurtovoy ret = snprintf(buf + pos, size - pos, ", %u", cpu);
250*87cbdc39SMax Gurtovoy
251*87cbdc39SMax Gurtovoy if (ret >= size - pos)
252*87cbdc39SMax Gurtovoy break;
253*87cbdc39SMax Gurtovoy first = false;
254*87cbdc39SMax Gurtovoy pos += ret;
255*87cbdc39SMax Gurtovoy }
256*87cbdc39SMax Gurtovoy }
257*87cbdc39SMax Gurtovoy ret = snprintf(buf + pos, size + 1 - pos, "\n");
258*87cbdc39SMax Gurtovoy return pos + ret;
259*87cbdc39SMax Gurtovoy }
260*87cbdc39SMax Gurtovoy
261*87cbdc39SMax Gurtovoy static struct kobj_attribute virtio_fs_vq_cpu_list_attr = __ATTR_RO(cpu_list);
262*87cbdc39SMax Gurtovoy
263*87cbdc39SMax Gurtovoy static struct attribute *virtio_fs_vq_attrs[] = {
264*87cbdc39SMax Gurtovoy &virtio_fs_vq_name_attr.attr,
265*87cbdc39SMax Gurtovoy &virtio_fs_vq_cpu_list_attr.attr,
266*87cbdc39SMax Gurtovoy NULL
267*87cbdc39SMax Gurtovoy };
268*87cbdc39SMax Gurtovoy
269*87cbdc39SMax Gurtovoy static struct attribute_group virtio_fs_vq_attr_group = {
270*87cbdc39SMax Gurtovoy .attrs = virtio_fs_vq_attrs,
271*87cbdc39SMax Gurtovoy };
272*87cbdc39SMax Gurtovoy
273a62a8ef9SStefan Hajnoczi /* Make sure virtiofs_mutex is held */
virtio_fs_put_locked(struct virtio_fs * fs)2744045b642SMax Gurtovoy static void virtio_fs_put_locked(struct virtio_fs *fs)
2754045b642SMax Gurtovoy {
2764045b642SMax Gurtovoy lockdep_assert_held(&virtio_fs_mutex);
2774045b642SMax Gurtovoy
2784045b642SMax Gurtovoy kobject_put(&fs->kobj);
2794045b642SMax Gurtovoy }
2804045b642SMax Gurtovoy
virtio_fs_put(struct virtio_fs * fs)281a62a8ef9SStefan Hajnoczi static void virtio_fs_put(struct virtio_fs *fs)
282a62a8ef9SStefan Hajnoczi {
2834045b642SMax Gurtovoy mutex_lock(&virtio_fs_mutex);
2844045b642SMax Gurtovoy virtio_fs_put_locked(fs);
2854045b642SMax Gurtovoy mutex_unlock(&virtio_fs_mutex);
286a62a8ef9SStefan Hajnoczi }
287a62a8ef9SStefan Hajnoczi
virtio_fs_fiq_release(struct fuse_iqueue * fiq)288a62a8ef9SStefan Hajnoczi static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
289a62a8ef9SStefan Hajnoczi {
290a62a8ef9SStefan Hajnoczi struct virtio_fs *vfs = fiq->priv;
291a62a8ef9SStefan Hajnoczi
292a62a8ef9SStefan Hajnoczi virtio_fs_put(vfs);
293a62a8ef9SStefan Hajnoczi }
294a62a8ef9SStefan Hajnoczi
virtio_fs_drain_queue(struct virtio_fs_vq * fsvq)295a62a8ef9SStefan Hajnoczi static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
296a62a8ef9SStefan Hajnoczi {
297a62a8ef9SStefan Hajnoczi WARN_ON(fsvq->in_flight < 0);
298a62a8ef9SStefan Hajnoczi
299a62a8ef9SStefan Hajnoczi /* Wait for in flight requests to finish.*/
300a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
301724c15a4SVivek Goyal if (fsvq->in_flight) {
302724c15a4SVivek Goyal /* We are holding virtio_fs_mutex. There should not be any
303724c15a4SVivek Goyal * waiters waiting for completion.
304724c15a4SVivek Goyal */
305724c15a4SVivek Goyal reinit_completion(&fsvq->in_flight_zero);
306a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
307724c15a4SVivek Goyal wait_for_completion(&fsvq->in_flight_zero);
308724c15a4SVivek Goyal } else {
309a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
310a62a8ef9SStefan Hajnoczi }
311a62a8ef9SStefan Hajnoczi
312a62a8ef9SStefan Hajnoczi flush_work(&fsvq->done_work);
313106e4df1SPeter-Jan Gootzen flush_work(&fsvq->dispatch_work);
314a62a8ef9SStefan Hajnoczi }
315a62a8ef9SStefan Hajnoczi
virtio_fs_drain_all_queues_locked(struct virtio_fs * fs)316724c15a4SVivek Goyal static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs)
317a62a8ef9SStefan Hajnoczi {
318a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq;
319a62a8ef9SStefan Hajnoczi int i;
320a62a8ef9SStefan Hajnoczi
321a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) {
322a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i];
323a62a8ef9SStefan Hajnoczi virtio_fs_drain_queue(fsvq);
324a62a8ef9SStefan Hajnoczi }
325a62a8ef9SStefan Hajnoczi }
326a62a8ef9SStefan Hajnoczi
virtio_fs_drain_all_queues(struct virtio_fs * fs)327724c15a4SVivek Goyal static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
328724c15a4SVivek Goyal {
329724c15a4SVivek Goyal /* Provides mutual exclusion between ->remove and ->kill_sb
330724c15a4SVivek Goyal * paths. We don't want both of these draining queue at the
331724c15a4SVivek Goyal * same time. Current completion logic reinits completion
332724c15a4SVivek Goyal * and that means there should not be any other thread
333724c15a4SVivek Goyal * doing reinit or waiting for completion already.
334724c15a4SVivek Goyal */
335724c15a4SVivek Goyal mutex_lock(&virtio_fs_mutex);
336724c15a4SVivek Goyal virtio_fs_drain_all_queues_locked(fs);
337724c15a4SVivek Goyal mutex_unlock(&virtio_fs_mutex);
338724c15a4SVivek Goyal }
339724c15a4SVivek Goyal
virtio_fs_start_all_queues(struct virtio_fs * fs)340a62a8ef9SStefan Hajnoczi static void virtio_fs_start_all_queues(struct virtio_fs *fs)
341a62a8ef9SStefan Hajnoczi {
342a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq;
343a62a8ef9SStefan Hajnoczi int i;
344a62a8ef9SStefan Hajnoczi
345a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) {
346a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i];
347a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
348a62a8ef9SStefan Hajnoczi fsvq->connected = true;
349a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
350a62a8ef9SStefan Hajnoczi }
351a62a8ef9SStefan Hajnoczi }
352a62a8ef9SStefan Hajnoczi
virtio_fs_delete_queues_sysfs(struct virtio_fs * fs)353*87cbdc39SMax Gurtovoy static void virtio_fs_delete_queues_sysfs(struct virtio_fs *fs)
354*87cbdc39SMax Gurtovoy {
355*87cbdc39SMax Gurtovoy struct virtio_fs_vq *fsvq;
356*87cbdc39SMax Gurtovoy int i;
357*87cbdc39SMax Gurtovoy
358*87cbdc39SMax Gurtovoy for (i = 0; i < fs->nvqs; i++) {
359*87cbdc39SMax Gurtovoy fsvq = &fs->vqs[i];
360*87cbdc39SMax Gurtovoy kobject_put(fsvq->kobj);
361*87cbdc39SMax Gurtovoy }
362*87cbdc39SMax Gurtovoy }
363*87cbdc39SMax Gurtovoy
virtio_fs_add_queues_sysfs(struct virtio_fs * fs)364*87cbdc39SMax Gurtovoy static int virtio_fs_add_queues_sysfs(struct virtio_fs *fs)
365*87cbdc39SMax Gurtovoy {
366*87cbdc39SMax Gurtovoy struct virtio_fs_vq *fsvq;
367*87cbdc39SMax Gurtovoy char buff[12];
368*87cbdc39SMax Gurtovoy int i, j, ret;
369*87cbdc39SMax Gurtovoy
370*87cbdc39SMax Gurtovoy for (i = 0; i < fs->nvqs; i++) {
371*87cbdc39SMax Gurtovoy fsvq = &fs->vqs[i];
372*87cbdc39SMax Gurtovoy
373*87cbdc39SMax Gurtovoy sprintf(buff, "%d", i);
374*87cbdc39SMax Gurtovoy fsvq->kobj = kobject_create_and_add(buff, fs->mqs_kobj);
375*87cbdc39SMax Gurtovoy if (!fs->mqs_kobj) {
376*87cbdc39SMax Gurtovoy ret = -ENOMEM;
377*87cbdc39SMax Gurtovoy goto out_del;
378*87cbdc39SMax Gurtovoy }
379*87cbdc39SMax Gurtovoy
380*87cbdc39SMax Gurtovoy ret = sysfs_create_group(fsvq->kobj, &virtio_fs_vq_attr_group);
381*87cbdc39SMax Gurtovoy if (ret) {
382*87cbdc39SMax Gurtovoy kobject_put(fsvq->kobj);
383*87cbdc39SMax Gurtovoy goto out_del;
384*87cbdc39SMax Gurtovoy }
385*87cbdc39SMax Gurtovoy }
386*87cbdc39SMax Gurtovoy
387*87cbdc39SMax Gurtovoy return 0;
388*87cbdc39SMax Gurtovoy
389*87cbdc39SMax Gurtovoy out_del:
390*87cbdc39SMax Gurtovoy for (j = 0; j < i; j++) {
391*87cbdc39SMax Gurtovoy fsvq = &fs->vqs[j];
392*87cbdc39SMax Gurtovoy kobject_put(fsvq->kobj);
393*87cbdc39SMax Gurtovoy }
394*87cbdc39SMax Gurtovoy return ret;
395*87cbdc39SMax Gurtovoy }
396*87cbdc39SMax Gurtovoy
397a62a8ef9SStefan Hajnoczi /* Add a new instance to the list or return -EEXIST if tag name exists*/
virtio_fs_add_instance(struct virtio_device * vdev,struct virtio_fs * fs)398a8f62f50SStefan Hajnoczi static int virtio_fs_add_instance(struct virtio_device *vdev,
399a8f62f50SStefan Hajnoczi struct virtio_fs *fs)
400a62a8ef9SStefan Hajnoczi {
401a62a8ef9SStefan Hajnoczi struct virtio_fs *fs2;
402a8f62f50SStefan Hajnoczi int ret;
403a62a8ef9SStefan Hajnoczi
404a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex);
405a62a8ef9SStefan Hajnoczi
406a62a8ef9SStefan Hajnoczi list_for_each_entry(fs2, &virtio_fs_instances, list) {
407a8f62f50SStefan Hajnoczi if (strcmp(fs->tag, fs2->tag) == 0) {
408a8f62f50SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
409a8f62f50SStefan Hajnoczi return -EEXIST;
410a8f62f50SStefan Hajnoczi }
411a62a8ef9SStefan Hajnoczi }
412a62a8ef9SStefan Hajnoczi
413a8f62f50SStefan Hajnoczi /* Use the virtio_device's index as a unique identifier, there is no
414a8f62f50SStefan Hajnoczi * need to allocate our own identifiers because the virtio_fs instance
415a8f62f50SStefan Hajnoczi * is only visible to userspace as long as the underlying virtio_device
416a8f62f50SStefan Hajnoczi * exists.
417a8f62f50SStefan Hajnoczi */
418a8f62f50SStefan Hajnoczi fs->kobj.kset = virtio_fs_kset;
419a8f62f50SStefan Hajnoczi ret = kobject_add(&fs->kobj, NULL, "%d", vdev->index);
420*87cbdc39SMax Gurtovoy if (ret < 0)
421*87cbdc39SMax Gurtovoy goto out_unlock;
422*87cbdc39SMax Gurtovoy
423*87cbdc39SMax Gurtovoy fs->mqs_kobj = kobject_create_and_add("mqs", &fs->kobj);
424*87cbdc39SMax Gurtovoy if (!fs->mqs_kobj) {
425*87cbdc39SMax Gurtovoy ret = -ENOMEM;
426*87cbdc39SMax Gurtovoy goto out_del;
427a8f62f50SStefan Hajnoczi }
428a8f62f50SStefan Hajnoczi
429a8f62f50SStefan Hajnoczi ret = sysfs_create_link(&fs->kobj, &vdev->dev.kobj, "device");
430*87cbdc39SMax Gurtovoy if (ret < 0)
431*87cbdc39SMax Gurtovoy goto out_put;
432*87cbdc39SMax Gurtovoy
433*87cbdc39SMax Gurtovoy ret = virtio_fs_add_queues_sysfs(fs);
434*87cbdc39SMax Gurtovoy if (ret)
435*87cbdc39SMax Gurtovoy goto out_remove;
436a8f62f50SStefan Hajnoczi
437a62a8ef9SStefan Hajnoczi list_add_tail(&fs->list, &virtio_fs_instances);
438a62a8ef9SStefan Hajnoczi
439a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
440a62a8ef9SStefan Hajnoczi
4419086b2d9SStefan Hajnoczi kobject_uevent(&fs->kobj, KOBJ_ADD);
4429086b2d9SStefan Hajnoczi
443a62a8ef9SStefan Hajnoczi return 0;
444*87cbdc39SMax Gurtovoy
445*87cbdc39SMax Gurtovoy out_remove:
446*87cbdc39SMax Gurtovoy sysfs_remove_link(&fs->kobj, "device");
447*87cbdc39SMax Gurtovoy out_put:
448*87cbdc39SMax Gurtovoy kobject_put(fs->mqs_kobj);
449*87cbdc39SMax Gurtovoy out_del:
450*87cbdc39SMax Gurtovoy kobject_del(&fs->kobj);
451*87cbdc39SMax Gurtovoy out_unlock:
452*87cbdc39SMax Gurtovoy mutex_unlock(&virtio_fs_mutex);
453*87cbdc39SMax Gurtovoy return ret;
454a62a8ef9SStefan Hajnoczi }
455a62a8ef9SStefan Hajnoczi
456a62a8ef9SStefan Hajnoczi /* Return the virtio_fs with a given tag, or NULL */
virtio_fs_find_instance(const char * tag)457a62a8ef9SStefan Hajnoczi static struct virtio_fs *virtio_fs_find_instance(const char *tag)
458a62a8ef9SStefan Hajnoczi {
459a62a8ef9SStefan Hajnoczi struct virtio_fs *fs;
460a62a8ef9SStefan Hajnoczi
461a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex);
462a62a8ef9SStefan Hajnoczi
463a62a8ef9SStefan Hajnoczi list_for_each_entry(fs, &virtio_fs_instances, list) {
464a62a8ef9SStefan Hajnoczi if (strcmp(fs->tag, tag) == 0) {
465a8f62f50SStefan Hajnoczi kobject_get(&fs->kobj);
466a62a8ef9SStefan Hajnoczi goto found;
467a62a8ef9SStefan Hajnoczi }
468a62a8ef9SStefan Hajnoczi }
469a62a8ef9SStefan Hajnoczi
470a62a8ef9SStefan Hajnoczi fs = NULL; /* not found */
471a62a8ef9SStefan Hajnoczi
472a62a8ef9SStefan Hajnoczi found:
473a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
474a62a8ef9SStefan Hajnoczi
475a62a8ef9SStefan Hajnoczi return fs;
476a62a8ef9SStefan Hajnoczi }
477a62a8ef9SStefan Hajnoczi
virtio_fs_free_devs(struct virtio_fs * fs)478a62a8ef9SStefan Hajnoczi static void virtio_fs_free_devs(struct virtio_fs *fs)
479a62a8ef9SStefan Hajnoczi {
480a62a8ef9SStefan Hajnoczi unsigned int i;
481a62a8ef9SStefan Hajnoczi
482a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) {
483a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i];
484a62a8ef9SStefan Hajnoczi
485a62a8ef9SStefan Hajnoczi if (!fsvq->fud)
486a62a8ef9SStefan Hajnoczi continue;
487a62a8ef9SStefan Hajnoczi
488a62a8ef9SStefan Hajnoczi fuse_dev_free(fsvq->fud);
489a62a8ef9SStefan Hajnoczi fsvq->fud = NULL;
490a62a8ef9SStefan Hajnoczi }
491a62a8ef9SStefan Hajnoczi }
492a62a8ef9SStefan Hajnoczi
493a62a8ef9SStefan Hajnoczi /* Read filesystem name from virtio config into fs->tag (must kfree()). */
virtio_fs_read_tag(struct virtio_device * vdev,struct virtio_fs * fs)494a62a8ef9SStefan Hajnoczi static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
495a62a8ef9SStefan Hajnoczi {
496a62a8ef9SStefan Hajnoczi char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
497a62a8ef9SStefan Hajnoczi char *end;
498a62a8ef9SStefan Hajnoczi size_t len;
499a62a8ef9SStefan Hajnoczi
500a62a8ef9SStefan Hajnoczi virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
501a62a8ef9SStefan Hajnoczi &tag_buf, sizeof(tag_buf));
502a62a8ef9SStefan Hajnoczi end = memchr(tag_buf, '\0', sizeof(tag_buf));
503a62a8ef9SStefan Hajnoczi if (end == tag_buf)
504a62a8ef9SStefan Hajnoczi return -EINVAL; /* empty tag */
505a62a8ef9SStefan Hajnoczi if (!end)
506a62a8ef9SStefan Hajnoczi end = &tag_buf[sizeof(tag_buf)];
507a62a8ef9SStefan Hajnoczi
508a62a8ef9SStefan Hajnoczi len = end - tag_buf;
509a62a8ef9SStefan Hajnoczi fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
510a62a8ef9SStefan Hajnoczi if (!fs->tag)
511a62a8ef9SStefan Hajnoczi return -ENOMEM;
512a62a8ef9SStefan Hajnoczi memcpy(fs->tag, tag_buf, len);
513a62a8ef9SStefan Hajnoczi fs->tag[len] = '\0';
51440488cc1SStefan Hajnoczi
51540488cc1SStefan Hajnoczi /* While the VIRTIO specification allows any character, newlines are
51640488cc1SStefan Hajnoczi * awkward on mount(8) command-lines and cause problems in the sysfs
51740488cc1SStefan Hajnoczi * "tag" attr and uevent TAG= properties. Forbid them.
51840488cc1SStefan Hajnoczi */
51940488cc1SStefan Hajnoczi if (strchr(fs->tag, '\n')) {
52040488cc1SStefan Hajnoczi dev_dbg(&vdev->dev, "refusing virtiofs tag with newline character\n");
52140488cc1SStefan Hajnoczi return -EINVAL;
52240488cc1SStefan Hajnoczi }
52340488cc1SStefan Hajnoczi
524a62a8ef9SStefan Hajnoczi return 0;
525a62a8ef9SStefan Hajnoczi }
526a62a8ef9SStefan Hajnoczi
527a62a8ef9SStefan Hajnoczi /* Work function for hiprio completion */
virtio_fs_hiprio_done_work(struct work_struct * work)528a62a8ef9SStefan Hajnoczi static void virtio_fs_hiprio_done_work(struct work_struct *work)
529a62a8ef9SStefan Hajnoczi {
530a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
531a62a8ef9SStefan Hajnoczi done_work);
532a62a8ef9SStefan Hajnoczi struct virtqueue *vq = fsvq->vq;
533a62a8ef9SStefan Hajnoczi
534a62a8ef9SStefan Hajnoczi /* Free completed FUSE_FORGET requests */
535a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
536a62a8ef9SStefan Hajnoczi do {
537a62a8ef9SStefan Hajnoczi unsigned int len;
538a62a8ef9SStefan Hajnoczi void *req;
539a62a8ef9SStefan Hajnoczi
540a62a8ef9SStefan Hajnoczi virtqueue_disable_cb(vq);
541a62a8ef9SStefan Hajnoczi
542a62a8ef9SStefan Hajnoczi while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
543a62a8ef9SStefan Hajnoczi kfree(req);
544c17ea009SVivek Goyal dec_in_flight_req(fsvq);
545a62a8ef9SStefan Hajnoczi }
546f9c29137SLi RongQing } while (!virtqueue_enable_cb(vq));
547106e4df1SPeter-Jan Gootzen
548106e4df1SPeter-Jan Gootzen if (!list_empty(&fsvq->queued_reqs))
549106e4df1SPeter-Jan Gootzen schedule_work(&fsvq->dispatch_work);
550106e4df1SPeter-Jan Gootzen
551a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
552a62a8ef9SStefan Hajnoczi }
553a62a8ef9SStefan Hajnoczi
virtio_fs_request_dispatch_work(struct work_struct * work)55451fecdd2SVivek Goyal static void virtio_fs_request_dispatch_work(struct work_struct *work)
555a62a8ef9SStefan Hajnoczi {
55651fecdd2SVivek Goyal struct fuse_req *req;
55751fecdd2SVivek Goyal struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
558106e4df1SPeter-Jan Gootzen dispatch_work);
559a9bfd9ddSVivek Goyal int ret;
56051fecdd2SVivek Goyal
56151fecdd2SVivek Goyal pr_debug("virtio-fs: worker %s called.\n", __func__);
56251fecdd2SVivek Goyal while (1) {
56351fecdd2SVivek Goyal spin_lock(&fsvq->lock);
56451fecdd2SVivek Goyal req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
56551fecdd2SVivek Goyal list);
56651fecdd2SVivek Goyal if (!req) {
56751fecdd2SVivek Goyal spin_unlock(&fsvq->lock);
568a9bfd9ddSVivek Goyal break;
56951fecdd2SVivek Goyal }
57051fecdd2SVivek Goyal
57151fecdd2SVivek Goyal list_del_init(&req->list);
57251fecdd2SVivek Goyal spin_unlock(&fsvq->lock);
5738f622e94SMax Reitz fuse_request_end(req);
57451fecdd2SVivek Goyal }
575a9bfd9ddSVivek Goyal
576a9bfd9ddSVivek Goyal /* Dispatch pending requests */
577a9bfd9ddSVivek Goyal while (1) {
578a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock);
579a9bfd9ddSVivek Goyal req = list_first_entry_or_null(&fsvq->queued_reqs,
580a9bfd9ddSVivek Goyal struct fuse_req, list);
581a9bfd9ddSVivek Goyal if (!req) {
582a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock);
583a9bfd9ddSVivek Goyal return;
584a9bfd9ddSVivek Goyal }
585a9bfd9ddSVivek Goyal list_del_init(&req->list);
586a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock);
587a9bfd9ddSVivek Goyal
588a9bfd9ddSVivek Goyal ret = virtio_fs_enqueue_req(fsvq, req, true);
589a9bfd9ddSVivek Goyal if (ret < 0) {
5902106e1f4SPeter-Jan Gootzen if (ret == -ENOSPC) {
591a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock);
592a9bfd9ddSVivek Goyal list_add_tail(&req->list, &fsvq->queued_reqs);
593a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock);
594a9bfd9ddSVivek Goyal return;
595a9bfd9ddSVivek Goyal }
596a9bfd9ddSVivek Goyal req->out.h.error = ret;
597a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock);
598a9bfd9ddSVivek Goyal dec_in_flight_req(fsvq);
599a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock);
600a9bfd9ddSVivek Goyal pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n",
601a9bfd9ddSVivek Goyal ret);
6028f622e94SMax Reitz fuse_request_end(req);
603a9bfd9ddSVivek Goyal }
604a9bfd9ddSVivek Goyal }
605a62a8ef9SStefan Hajnoczi }
606a62a8ef9SStefan Hajnoczi
60758ada94fSVivek Goyal /*
60858ada94fSVivek Goyal * Returns 1 if queue is full and sender should wait a bit before sending
60958ada94fSVivek Goyal * next request, 0 otherwise.
61058ada94fSVivek Goyal */
send_forget_request(struct virtio_fs_vq * fsvq,struct virtio_fs_forget * forget,bool in_flight)61158ada94fSVivek Goyal static int send_forget_request(struct virtio_fs_vq *fsvq,
61258ada94fSVivek Goyal struct virtio_fs_forget *forget,
61358ada94fSVivek Goyal bool in_flight)
61458ada94fSVivek Goyal {
61558ada94fSVivek Goyal struct scatterlist sg;
61658ada94fSVivek Goyal struct virtqueue *vq;
61758ada94fSVivek Goyal int ret = 0;
61858ada94fSVivek Goyal bool notify;
6191efcf39eSVivek Goyal struct virtio_fs_forget_req *req = &forget->req;
62058ada94fSVivek Goyal
62158ada94fSVivek Goyal spin_lock(&fsvq->lock);
62258ada94fSVivek Goyal if (!fsvq->connected) {
62358ada94fSVivek Goyal if (in_flight)
62458ada94fSVivek Goyal dec_in_flight_req(fsvq);
62558ada94fSVivek Goyal kfree(forget);
62658ada94fSVivek Goyal goto out;
62758ada94fSVivek Goyal }
62858ada94fSVivek Goyal
6291efcf39eSVivek Goyal sg_init_one(&sg, req, sizeof(*req));
63058ada94fSVivek Goyal vq = fsvq->vq;
63158ada94fSVivek Goyal dev_dbg(&vq->vdev->dev, "%s\n", __func__);
63258ada94fSVivek Goyal
63358ada94fSVivek Goyal ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC);
63458ada94fSVivek Goyal if (ret < 0) {
6352106e1f4SPeter-Jan Gootzen if (ret == -ENOSPC) {
63658ada94fSVivek Goyal pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n",
63758ada94fSVivek Goyal ret);
63858ada94fSVivek Goyal list_add_tail(&forget->list, &fsvq->queued_reqs);
63958ada94fSVivek Goyal if (!in_flight)
64058ada94fSVivek Goyal inc_in_flight_req(fsvq);
64158ada94fSVivek Goyal /* Queue is full */
64258ada94fSVivek Goyal ret = 1;
64358ada94fSVivek Goyal } else {
64458ada94fSVivek Goyal pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n",
64558ada94fSVivek Goyal ret);
64658ada94fSVivek Goyal kfree(forget);
64758ada94fSVivek Goyal if (in_flight)
64858ada94fSVivek Goyal dec_in_flight_req(fsvq);
64958ada94fSVivek Goyal }
65058ada94fSVivek Goyal goto out;
65158ada94fSVivek Goyal }
65258ada94fSVivek Goyal
65358ada94fSVivek Goyal if (!in_flight)
65458ada94fSVivek Goyal inc_in_flight_req(fsvq);
65558ada94fSVivek Goyal notify = virtqueue_kick_prepare(vq);
65658ada94fSVivek Goyal spin_unlock(&fsvq->lock);
65758ada94fSVivek Goyal
65858ada94fSVivek Goyal if (notify)
65958ada94fSVivek Goyal virtqueue_notify(vq);
66058ada94fSVivek Goyal return ret;
66158ada94fSVivek Goyal out:
66258ada94fSVivek Goyal spin_unlock(&fsvq->lock);
66358ada94fSVivek Goyal return ret;
66458ada94fSVivek Goyal }
66558ada94fSVivek Goyal
virtio_fs_hiprio_dispatch_work(struct work_struct * work)666a62a8ef9SStefan Hajnoczi static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
667a62a8ef9SStefan Hajnoczi {
668a62a8ef9SStefan Hajnoczi struct virtio_fs_forget *forget;
669a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
670106e4df1SPeter-Jan Gootzen dispatch_work);
671a62a8ef9SStefan Hajnoczi pr_debug("virtio-fs: worker %s called.\n", __func__);
672a62a8ef9SStefan Hajnoczi while (1) {
673a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
674a62a8ef9SStefan Hajnoczi forget = list_first_entry_or_null(&fsvq->queued_reqs,
675a62a8ef9SStefan Hajnoczi struct virtio_fs_forget, list);
676a62a8ef9SStefan Hajnoczi if (!forget) {
677a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
678a62a8ef9SStefan Hajnoczi return;
679a62a8ef9SStefan Hajnoczi }
680a62a8ef9SStefan Hajnoczi
681a62a8ef9SStefan Hajnoczi list_del(&forget->list);
682a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
68358ada94fSVivek Goyal if (send_forget_request(fsvq, forget, true))
684a62a8ef9SStefan Hajnoczi return;
685a62a8ef9SStefan Hajnoczi }
686a62a8ef9SStefan Hajnoczi }
687a62a8ef9SStefan Hajnoczi
688a62a8ef9SStefan Hajnoczi /* Allocate and copy args into req->argbuf */
copy_args_to_argbuf(struct fuse_req * req)689a62a8ef9SStefan Hajnoczi static int copy_args_to_argbuf(struct fuse_req *req)
690a62a8ef9SStefan Hajnoczi {
691a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args;
692a62a8ef9SStefan Hajnoczi unsigned int offset = 0;
693a62a8ef9SStefan Hajnoczi unsigned int num_in;
694a62a8ef9SStefan Hajnoczi unsigned int num_out;
695a62a8ef9SStefan Hajnoczi unsigned int len;
696a62a8ef9SStefan Hajnoczi unsigned int i;
697a62a8ef9SStefan Hajnoczi
698a62a8ef9SStefan Hajnoczi num_in = args->in_numargs - args->in_pages;
699a62a8ef9SStefan Hajnoczi num_out = args->out_numargs - args->out_pages;
700a62a8ef9SStefan Hajnoczi len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
701a62a8ef9SStefan Hajnoczi fuse_len_args(num_out, args->out_args);
702a62a8ef9SStefan Hajnoczi
703a62a8ef9SStefan Hajnoczi req->argbuf = kmalloc(len, GFP_ATOMIC);
704a62a8ef9SStefan Hajnoczi if (!req->argbuf)
705a62a8ef9SStefan Hajnoczi return -ENOMEM;
706a62a8ef9SStefan Hajnoczi
707a62a8ef9SStefan Hajnoczi for (i = 0; i < num_in; i++) {
708a62a8ef9SStefan Hajnoczi memcpy(req->argbuf + offset,
709a62a8ef9SStefan Hajnoczi args->in_args[i].value,
710a62a8ef9SStefan Hajnoczi args->in_args[i].size);
711a62a8ef9SStefan Hajnoczi offset += args->in_args[i].size;
712a62a8ef9SStefan Hajnoczi }
713a62a8ef9SStefan Hajnoczi
714a62a8ef9SStefan Hajnoczi return 0;
715a62a8ef9SStefan Hajnoczi }
716a62a8ef9SStefan Hajnoczi
717a62a8ef9SStefan Hajnoczi /* Copy args out of and free req->argbuf */
copy_args_from_argbuf(struct fuse_args * args,struct fuse_req * req)718a62a8ef9SStefan Hajnoczi static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
719a62a8ef9SStefan Hajnoczi {
720a62a8ef9SStefan Hajnoczi unsigned int remaining;
721a62a8ef9SStefan Hajnoczi unsigned int offset;
722a62a8ef9SStefan Hajnoczi unsigned int num_in;
723a62a8ef9SStefan Hajnoczi unsigned int num_out;
724a62a8ef9SStefan Hajnoczi unsigned int i;
725a62a8ef9SStefan Hajnoczi
726a62a8ef9SStefan Hajnoczi remaining = req->out.h.len - sizeof(req->out.h);
727a62a8ef9SStefan Hajnoczi num_in = args->in_numargs - args->in_pages;
728a62a8ef9SStefan Hajnoczi num_out = args->out_numargs - args->out_pages;
729a62a8ef9SStefan Hajnoczi offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
730a62a8ef9SStefan Hajnoczi
731a62a8ef9SStefan Hajnoczi for (i = 0; i < num_out; i++) {
732a62a8ef9SStefan Hajnoczi unsigned int argsize = args->out_args[i].size;
733a62a8ef9SStefan Hajnoczi
734a62a8ef9SStefan Hajnoczi if (args->out_argvar &&
735a62a8ef9SStefan Hajnoczi i == args->out_numargs - 1 &&
736a62a8ef9SStefan Hajnoczi argsize > remaining) {
737a62a8ef9SStefan Hajnoczi argsize = remaining;
738a62a8ef9SStefan Hajnoczi }
739a62a8ef9SStefan Hajnoczi
740a62a8ef9SStefan Hajnoczi memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
741a62a8ef9SStefan Hajnoczi offset += argsize;
742a62a8ef9SStefan Hajnoczi
743a62a8ef9SStefan Hajnoczi if (i != args->out_numargs - 1)
744a62a8ef9SStefan Hajnoczi remaining -= argsize;
745a62a8ef9SStefan Hajnoczi }
746a62a8ef9SStefan Hajnoczi
747a62a8ef9SStefan Hajnoczi /* Store the actual size of the variable-length arg */
748a62a8ef9SStefan Hajnoczi if (args->out_argvar)
749a62a8ef9SStefan Hajnoczi args->out_args[args->out_numargs - 1].size = remaining;
750a62a8ef9SStefan Hajnoczi
751a62a8ef9SStefan Hajnoczi kfree(req->argbuf);
752a62a8ef9SStefan Hajnoczi req->argbuf = NULL;
753a62a8ef9SStefan Hajnoczi }
754a62a8ef9SStefan Hajnoczi
755a62a8ef9SStefan Hajnoczi /* Work function for request completion */
virtio_fs_request_complete(struct fuse_req * req,struct virtio_fs_vq * fsvq)756bb737bbeSVivek Goyal static void virtio_fs_request_complete(struct fuse_req *req,
757bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq)
758a62a8ef9SStefan Hajnoczi {
759a62a8ef9SStefan Hajnoczi struct fuse_pqueue *fpq = &fsvq->fud->pq;
760a62a8ef9SStefan Hajnoczi struct fuse_args *args;
761bb737bbeSVivek Goyal struct fuse_args_pages *ap;
762a62a8ef9SStefan Hajnoczi unsigned int len, i, thislen;
763a62a8ef9SStefan Hajnoczi struct page *page;
764a62a8ef9SStefan Hajnoczi
765a62a8ef9SStefan Hajnoczi /*
766a62a8ef9SStefan Hajnoczi * TODO verify that server properly follows FUSE protocol
767a62a8ef9SStefan Hajnoczi * (oh.uniq, oh.len)
768a62a8ef9SStefan Hajnoczi */
769a62a8ef9SStefan Hajnoczi args = req->args;
770a62a8ef9SStefan Hajnoczi copy_args_from_argbuf(args, req);
771a62a8ef9SStefan Hajnoczi
772a62a8ef9SStefan Hajnoczi if (args->out_pages && args->page_zeroing) {
773a62a8ef9SStefan Hajnoczi len = args->out_args[args->out_numargs - 1].size;
774a62a8ef9SStefan Hajnoczi ap = container_of(args, typeof(*ap), args);
775a62a8ef9SStefan Hajnoczi for (i = 0; i < ap->num_pages; i++) {
776a62a8ef9SStefan Hajnoczi thislen = ap->descs[i].length;
777a62a8ef9SStefan Hajnoczi if (len < thislen) {
778a62a8ef9SStefan Hajnoczi WARN_ON(ap->descs[i].offset);
779a62a8ef9SStefan Hajnoczi page = ap->pages[i];
780a62a8ef9SStefan Hajnoczi zero_user_segment(page, len, thislen);
781a62a8ef9SStefan Hajnoczi len = 0;
782a62a8ef9SStefan Hajnoczi } else {
783a62a8ef9SStefan Hajnoczi len -= thislen;
784a62a8ef9SStefan Hajnoczi }
785a62a8ef9SStefan Hajnoczi }
786a62a8ef9SStefan Hajnoczi }
787a62a8ef9SStefan Hajnoczi
788a62a8ef9SStefan Hajnoczi spin_lock(&fpq->lock);
789a62a8ef9SStefan Hajnoczi clear_bit(FR_SENT, &req->flags);
790a62a8ef9SStefan Hajnoczi spin_unlock(&fpq->lock);
791a62a8ef9SStefan Hajnoczi
7928f622e94SMax Reitz fuse_request_end(req);
793a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
794c17ea009SVivek Goyal dec_in_flight_req(fsvq);
795a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
796a62a8ef9SStefan Hajnoczi }
797bb737bbeSVivek Goyal
virtio_fs_complete_req_work(struct work_struct * work)798bb737bbeSVivek Goyal static void virtio_fs_complete_req_work(struct work_struct *work)
799bb737bbeSVivek Goyal {
800bb737bbeSVivek Goyal struct virtio_fs_req_work *w =
801bb737bbeSVivek Goyal container_of(work, typeof(*w), done_work);
802bb737bbeSVivek Goyal
803bb737bbeSVivek Goyal virtio_fs_request_complete(w->req, w->fsvq);
804bb737bbeSVivek Goyal kfree(w);
805bb737bbeSVivek Goyal }
806bb737bbeSVivek Goyal
virtio_fs_requests_done_work(struct work_struct * work)807bb737bbeSVivek Goyal static void virtio_fs_requests_done_work(struct work_struct *work)
808bb737bbeSVivek Goyal {
809bb737bbeSVivek Goyal struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
810bb737bbeSVivek Goyal done_work);
811bb737bbeSVivek Goyal struct fuse_pqueue *fpq = &fsvq->fud->pq;
812bb737bbeSVivek Goyal struct virtqueue *vq = fsvq->vq;
813bb737bbeSVivek Goyal struct fuse_req *req;
814bb737bbeSVivek Goyal struct fuse_req *next;
815bb737bbeSVivek Goyal unsigned int len;
816bb737bbeSVivek Goyal LIST_HEAD(reqs);
817bb737bbeSVivek Goyal
818bb737bbeSVivek Goyal /* Collect completed requests off the virtqueue */
819bb737bbeSVivek Goyal spin_lock(&fsvq->lock);
820bb737bbeSVivek Goyal do {
821bb737bbeSVivek Goyal virtqueue_disable_cb(vq);
822bb737bbeSVivek Goyal
823bb737bbeSVivek Goyal while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
824bb737bbeSVivek Goyal spin_lock(&fpq->lock);
825bb737bbeSVivek Goyal list_move_tail(&req->list, &reqs);
826bb737bbeSVivek Goyal spin_unlock(&fpq->lock);
827bb737bbeSVivek Goyal }
828f9c29137SLi RongQing } while (!virtqueue_enable_cb(vq));
829bb737bbeSVivek Goyal spin_unlock(&fsvq->lock);
830bb737bbeSVivek Goyal
831bb737bbeSVivek Goyal /* End requests */
832bb737bbeSVivek Goyal list_for_each_entry_safe(req, next, &reqs, list) {
833bb737bbeSVivek Goyal list_del_init(&req->list);
834bb737bbeSVivek Goyal
835bb737bbeSVivek Goyal /* blocking async request completes in a worker context */
836bb737bbeSVivek Goyal if (req->args->may_block) {
837bb737bbeSVivek Goyal struct virtio_fs_req_work *w;
838bb737bbeSVivek Goyal
839bb737bbeSVivek Goyal w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL);
840bb737bbeSVivek Goyal INIT_WORK(&w->done_work, virtio_fs_complete_req_work);
841bb737bbeSVivek Goyal w->fsvq = fsvq;
842bb737bbeSVivek Goyal w->req = req;
843bb737bbeSVivek Goyal schedule_work(&w->done_work);
844bb737bbeSVivek Goyal } else {
845bb737bbeSVivek Goyal virtio_fs_request_complete(req, fsvq);
846bb737bbeSVivek Goyal }
847bb737bbeSVivek Goyal }
848106e4df1SPeter-Jan Gootzen
849106e4df1SPeter-Jan Gootzen /* Try to push previously queued requests, as the queue might no longer be full */
850106e4df1SPeter-Jan Gootzen spin_lock(&fsvq->lock);
851106e4df1SPeter-Jan Gootzen if (!list_empty(&fsvq->queued_reqs))
852106e4df1SPeter-Jan Gootzen schedule_work(&fsvq->dispatch_work);
853106e4df1SPeter-Jan Gootzen spin_unlock(&fsvq->lock);
854a62a8ef9SStefan Hajnoczi }
855a62a8ef9SStefan Hajnoczi
virtio_fs_map_queues(struct virtio_device * vdev,struct virtio_fs * fs)856529395d2SPeter-Jan Gootzen static void virtio_fs_map_queues(struct virtio_device *vdev, struct virtio_fs *fs)
857529395d2SPeter-Jan Gootzen {
858529395d2SPeter-Jan Gootzen const struct cpumask *mask, *masks;
859529395d2SPeter-Jan Gootzen unsigned int q, cpu;
860529395d2SPeter-Jan Gootzen
861529395d2SPeter-Jan Gootzen /* First attempt to map using existing transport layer affinities
862529395d2SPeter-Jan Gootzen * e.g. PCIe MSI-X
863529395d2SPeter-Jan Gootzen */
864529395d2SPeter-Jan Gootzen if (!vdev->config->get_vq_affinity)
865529395d2SPeter-Jan Gootzen goto fallback;
866529395d2SPeter-Jan Gootzen
867529395d2SPeter-Jan Gootzen for (q = 0; q < fs->num_request_queues; q++) {
868529395d2SPeter-Jan Gootzen mask = vdev->config->get_vq_affinity(vdev, VQ_REQUEST + q);
869529395d2SPeter-Jan Gootzen if (!mask)
870529395d2SPeter-Jan Gootzen goto fallback;
871529395d2SPeter-Jan Gootzen
872529395d2SPeter-Jan Gootzen for_each_cpu(cpu, mask)
873529395d2SPeter-Jan Gootzen fs->mq_map[cpu] = q;
874529395d2SPeter-Jan Gootzen }
875529395d2SPeter-Jan Gootzen
876529395d2SPeter-Jan Gootzen return;
877529395d2SPeter-Jan Gootzen fallback:
878529395d2SPeter-Jan Gootzen /* Attempt to map evenly in groups over the CPUs */
879529395d2SPeter-Jan Gootzen masks = group_cpus_evenly(fs->num_request_queues);
880529395d2SPeter-Jan Gootzen /* If even this fails we default to all CPUs use queue zero */
881529395d2SPeter-Jan Gootzen if (!masks) {
882529395d2SPeter-Jan Gootzen for_each_possible_cpu(cpu)
883529395d2SPeter-Jan Gootzen fs->mq_map[cpu] = 0;
884529395d2SPeter-Jan Gootzen return;
885529395d2SPeter-Jan Gootzen }
886529395d2SPeter-Jan Gootzen
887529395d2SPeter-Jan Gootzen for (q = 0; q < fs->num_request_queues; q++) {
888529395d2SPeter-Jan Gootzen for_each_cpu(cpu, &masks[q])
889529395d2SPeter-Jan Gootzen fs->mq_map[cpu] = q;
890529395d2SPeter-Jan Gootzen }
891529395d2SPeter-Jan Gootzen kfree(masks);
892529395d2SPeter-Jan Gootzen }
893529395d2SPeter-Jan Gootzen
894a62a8ef9SStefan Hajnoczi /* Virtqueue interrupt handler */
virtio_fs_vq_done(struct virtqueue * vq)895a62a8ef9SStefan Hajnoczi static void virtio_fs_vq_done(struct virtqueue *vq)
896a62a8ef9SStefan Hajnoczi {
897a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
898a62a8ef9SStefan Hajnoczi
899a62a8ef9SStefan Hajnoczi dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
900a62a8ef9SStefan Hajnoczi
901a62a8ef9SStefan Hajnoczi schedule_work(&fsvq->done_work);
902a62a8ef9SStefan Hajnoczi }
903a62a8ef9SStefan Hajnoczi
virtio_fs_init_vq(struct virtio_fs_vq * fsvq,char * name,int vq_type)904b43b7e81SVivek Goyal static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name,
905b43b7e81SVivek Goyal int vq_type)
906b43b7e81SVivek Goyal {
9077c594bbdSMiklos Szeredi strscpy(fsvq->name, name, VQ_NAME_LEN);
908b43b7e81SVivek Goyal spin_lock_init(&fsvq->lock);
909b43b7e81SVivek Goyal INIT_LIST_HEAD(&fsvq->queued_reqs);
910b43b7e81SVivek Goyal INIT_LIST_HEAD(&fsvq->end_reqs);
911b43b7e81SVivek Goyal init_completion(&fsvq->in_flight_zero);
912b43b7e81SVivek Goyal
913b43b7e81SVivek Goyal if (vq_type == VQ_REQUEST) {
914b43b7e81SVivek Goyal INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work);
915106e4df1SPeter-Jan Gootzen INIT_WORK(&fsvq->dispatch_work,
916b43b7e81SVivek Goyal virtio_fs_request_dispatch_work);
917b43b7e81SVivek Goyal } else {
918b43b7e81SVivek Goyal INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work);
919106e4df1SPeter-Jan Gootzen INIT_WORK(&fsvq->dispatch_work,
920b43b7e81SVivek Goyal virtio_fs_hiprio_dispatch_work);
921b43b7e81SVivek Goyal }
922b43b7e81SVivek Goyal }
923b43b7e81SVivek Goyal
924a62a8ef9SStefan Hajnoczi /* Initialize virtqueues */
virtio_fs_setup_vqs(struct virtio_device * vdev,struct virtio_fs * fs)925a62a8ef9SStefan Hajnoczi static int virtio_fs_setup_vqs(struct virtio_device *vdev,
926a62a8ef9SStefan Hajnoczi struct virtio_fs *fs)
927a62a8ef9SStefan Hajnoczi {
928fc496dcdSJiri Pirko struct virtqueue_info *vqs_info;
929a62a8ef9SStefan Hajnoczi struct virtqueue **vqs;
930529395d2SPeter-Jan Gootzen /* Specify pre_vectors to ensure that the queues before the
931529395d2SPeter-Jan Gootzen * request queues (e.g. hiprio) don't claim any of the CPUs in
932529395d2SPeter-Jan Gootzen * the multi-queue mapping and interrupt affinities
933529395d2SPeter-Jan Gootzen */
934529395d2SPeter-Jan Gootzen struct irq_affinity desc = { .pre_vectors = VQ_REQUEST };
935a62a8ef9SStefan Hajnoczi unsigned int i;
936a62a8ef9SStefan Hajnoczi int ret = 0;
937a62a8ef9SStefan Hajnoczi
9382c0349ecSMichael S. Tsirkin virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues,
939a62a8ef9SStefan Hajnoczi &fs->num_request_queues);
940a62a8ef9SStefan Hajnoczi if (fs->num_request_queues == 0)
941a62a8ef9SStefan Hajnoczi return -EINVAL;
942a62a8ef9SStefan Hajnoczi
943103c2de1SPeter-Jan Gootzen /* Truncate nr of request queues to nr_cpu_id */
944103c2de1SPeter-Jan Gootzen fs->num_request_queues = min_t(unsigned int, fs->num_request_queues,
945103c2de1SPeter-Jan Gootzen nr_cpu_ids);
946b43b7e81SVivek Goyal fs->nvqs = VQ_REQUEST + fs->num_request_queues;
947a62a8ef9SStefan Hajnoczi fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
948a62a8ef9SStefan Hajnoczi if (!fs->vqs)
949a62a8ef9SStefan Hajnoczi return -ENOMEM;
950a62a8ef9SStefan Hajnoczi
951a62a8ef9SStefan Hajnoczi vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
952529395d2SPeter-Jan Gootzen fs->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*fs->mq_map), GFP_KERNEL,
953529395d2SPeter-Jan Gootzen dev_to_node(&vdev->dev));
954fc496dcdSJiri Pirko vqs_info = kcalloc(fs->nvqs, sizeof(*vqs_info), GFP_KERNEL);
955fc496dcdSJiri Pirko if (!vqs || !vqs_info || !fs->mq_map) {
956a62a8ef9SStefan Hajnoczi ret = -ENOMEM;
957a62a8ef9SStefan Hajnoczi goto out;
958a62a8ef9SStefan Hajnoczi }
959a62a8ef9SStefan Hajnoczi
960b43b7e81SVivek Goyal /* Initialize the hiprio/forget request virtqueue */
961fc496dcdSJiri Pirko vqs_info[VQ_HIPRIO].callback = virtio_fs_vq_done;
962b43b7e81SVivek Goyal virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO);
963fc496dcdSJiri Pirko vqs_info[VQ_HIPRIO].name = fs->vqs[VQ_HIPRIO].name;
964a62a8ef9SStefan Hajnoczi
965a62a8ef9SStefan Hajnoczi /* Initialize the requests virtqueues */
966a62a8ef9SStefan Hajnoczi for (i = VQ_REQUEST; i < fs->nvqs; i++) {
967b43b7e81SVivek Goyal char vq_name[VQ_NAME_LEN];
968b43b7e81SVivek Goyal
969b43b7e81SVivek Goyal snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST);
970b43b7e81SVivek Goyal virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST);
971fc496dcdSJiri Pirko vqs_info[i].callback = virtio_fs_vq_done;
972fc496dcdSJiri Pirko vqs_info[i].name = fs->vqs[i].name;
973a62a8ef9SStefan Hajnoczi }
974a62a8ef9SStefan Hajnoczi
9756c85d6b6SJiri Pirko ret = virtio_find_vqs(vdev, fs->nvqs, vqs, vqs_info, &desc);
976a62a8ef9SStefan Hajnoczi if (ret < 0)
977a62a8ef9SStefan Hajnoczi goto out;
978a62a8ef9SStefan Hajnoczi
979a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++)
980a62a8ef9SStefan Hajnoczi fs->vqs[i].vq = vqs[i];
981a62a8ef9SStefan Hajnoczi
982a62a8ef9SStefan Hajnoczi virtio_fs_start_all_queues(fs);
983a62a8ef9SStefan Hajnoczi out:
984fc496dcdSJiri Pirko kfree(vqs_info);
985a62a8ef9SStefan Hajnoczi kfree(vqs);
986529395d2SPeter-Jan Gootzen if (ret) {
987a62a8ef9SStefan Hajnoczi kfree(fs->vqs);
988529395d2SPeter-Jan Gootzen kfree(fs->mq_map);
989529395d2SPeter-Jan Gootzen }
990a62a8ef9SStefan Hajnoczi return ret;
991a62a8ef9SStefan Hajnoczi }
992a62a8ef9SStefan Hajnoczi
993a62a8ef9SStefan Hajnoczi /* Free virtqueues (device must already be reset) */
virtio_fs_cleanup_vqs(struct virtio_device * vdev)9941e5b9e04SDeming Wang static void virtio_fs_cleanup_vqs(struct virtio_device *vdev)
995a62a8ef9SStefan Hajnoczi {
996a62a8ef9SStefan Hajnoczi vdev->config->del_vqs(vdev);
997a62a8ef9SStefan Hajnoczi }
998a62a8ef9SStefan Hajnoczi
99922f3787eSStefan Hajnoczi /* Map a window offset to a page frame number. The window offset will have
100022f3787eSStefan Hajnoczi * been produced by .iomap_begin(), which maps a file offset to a window
100122f3787eSStefan Hajnoczi * offset.
100222f3787eSStefan Hajnoczi */
virtio_fs_direct_access(struct dax_device * dax_dev,pgoff_t pgoff,long nr_pages,enum dax_access_mode mode,void ** kaddr,pfn_t * pfn)100322f3787eSStefan Hajnoczi static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1004e511c4a3SJane Chu long nr_pages, enum dax_access_mode mode,
1005e511c4a3SJane Chu void **kaddr, pfn_t *pfn)
100622f3787eSStefan Hajnoczi {
100722f3787eSStefan Hajnoczi struct virtio_fs *fs = dax_get_private(dax_dev);
100822f3787eSStefan Hajnoczi phys_addr_t offset = PFN_PHYS(pgoff);
100922f3787eSStefan Hajnoczi size_t max_nr_pages = fs->window_len / PAGE_SIZE - pgoff;
101022f3787eSStefan Hajnoczi
101122f3787eSStefan Hajnoczi if (kaddr)
101222f3787eSStefan Hajnoczi *kaddr = fs->window_kaddr + offset;
101322f3787eSStefan Hajnoczi if (pfn)
101422f3787eSStefan Hajnoczi *pfn = phys_to_pfn_t(fs->window_phys_addr + offset,
101522f3787eSStefan Hajnoczi PFN_DEV | PFN_MAP);
101622f3787eSStefan Hajnoczi return nr_pages > max_nr_pages ? max_nr_pages : nr_pages;
101722f3787eSStefan Hajnoczi }
101822f3787eSStefan Hajnoczi
virtio_fs_zero_page_range(struct dax_device * dax_dev,pgoff_t pgoff,size_t nr_pages)101922f3787eSStefan Hajnoczi static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
102022f3787eSStefan Hajnoczi pgoff_t pgoff, size_t nr_pages)
102122f3787eSStefan Hajnoczi {
102222f3787eSStefan Hajnoczi long rc;
102322f3787eSStefan Hajnoczi void *kaddr;
102422f3787eSStefan Hajnoczi
1025e511c4a3SJane Chu rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS, &kaddr,
1026e511c4a3SJane Chu NULL);
102722f3787eSStefan Hajnoczi if (rc < 0)
10281ea7ca1bSJane Chu return dax_mem2blk_err(rc);
10291ea7ca1bSJane Chu
103022f3787eSStefan Hajnoczi memset(kaddr, 0, nr_pages << PAGE_SHIFT);
103122f3787eSStefan Hajnoczi dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
103222f3787eSStefan Hajnoczi return 0;
103322f3787eSStefan Hajnoczi }
103422f3787eSStefan Hajnoczi
103522f3787eSStefan Hajnoczi static const struct dax_operations virtio_fs_dax_ops = {
103622f3787eSStefan Hajnoczi .direct_access = virtio_fs_direct_access,
103722f3787eSStefan Hajnoczi .zero_page_range = virtio_fs_zero_page_range,
103822f3787eSStefan Hajnoczi };
103922f3787eSStefan Hajnoczi
virtio_fs_cleanup_dax(void * data)104022f3787eSStefan Hajnoczi static void virtio_fs_cleanup_dax(void *data)
104122f3787eSStefan Hajnoczi {
104222f3787eSStefan Hajnoczi struct dax_device *dax_dev = data;
104322f3787eSStefan Hajnoczi
104422f3787eSStefan Hajnoczi kill_dax(dax_dev);
104522f3787eSStefan Hajnoczi put_dax(dax_dev);
104622f3787eSStefan Hajnoczi }
104722f3787eSStefan Hajnoczi
1048562ce828SMathieu Desnoyers DEFINE_FREE(cleanup_dax, struct dax_dev *, if (!IS_ERR_OR_NULL(_T)) virtio_fs_cleanup_dax(_T))
1049562ce828SMathieu Desnoyers
virtio_fs_setup_dax(struct virtio_device * vdev,struct virtio_fs * fs)105022f3787eSStefan Hajnoczi static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs)
105122f3787eSStefan Hajnoczi {
1052562ce828SMathieu Desnoyers struct dax_device *dax_dev __free(cleanup_dax) = NULL;
105322f3787eSStefan Hajnoczi struct virtio_shm_region cache_reg;
105422f3787eSStefan Hajnoczi struct dev_pagemap *pgmap;
105522f3787eSStefan Hajnoczi bool have_cache;
105622f3787eSStefan Hajnoczi
105722f3787eSStefan Hajnoczi if (!IS_ENABLED(CONFIG_FUSE_DAX))
105822f3787eSStefan Hajnoczi return 0;
105922f3787eSStefan Hajnoczi
1060562ce828SMathieu Desnoyers dax_dev = alloc_dax(fs, &virtio_fs_dax_ops);
1061562ce828SMathieu Desnoyers if (IS_ERR(dax_dev)) {
1062562ce828SMathieu Desnoyers int rc = PTR_ERR(dax_dev);
1063562ce828SMathieu Desnoyers return rc == -EOPNOTSUPP ? 0 : rc;
1064562ce828SMathieu Desnoyers }
1065562ce828SMathieu Desnoyers
106622f3787eSStefan Hajnoczi /* Get cache region */
106722f3787eSStefan Hajnoczi have_cache = virtio_get_shm_region(vdev, &cache_reg,
106822f3787eSStefan Hajnoczi (u8)VIRTIO_FS_SHMCAP_ID_CACHE);
106922f3787eSStefan Hajnoczi if (!have_cache) {
107022f3787eSStefan Hajnoczi dev_notice(&vdev->dev, "%s: No cache capability\n", __func__);
107122f3787eSStefan Hajnoczi return 0;
107222f3787eSStefan Hajnoczi }
107322f3787eSStefan Hajnoczi
107422f3787eSStefan Hajnoczi if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len,
107522f3787eSStefan Hajnoczi dev_name(&vdev->dev))) {
107622f3787eSStefan Hajnoczi dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n",
107722f3787eSStefan Hajnoczi cache_reg.addr, cache_reg.len);
107822f3787eSStefan Hajnoczi return -EBUSY;
107922f3787eSStefan Hajnoczi }
108022f3787eSStefan Hajnoczi
108122f3787eSStefan Hajnoczi dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len,
108222f3787eSStefan Hajnoczi cache_reg.addr);
108322f3787eSStefan Hajnoczi
108422f3787eSStefan Hajnoczi pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL);
108522f3787eSStefan Hajnoczi if (!pgmap)
108622f3787eSStefan Hajnoczi return -ENOMEM;
108722f3787eSStefan Hajnoczi
108822f3787eSStefan Hajnoczi pgmap->type = MEMORY_DEVICE_FS_DAX;
108922f3787eSStefan Hajnoczi
109022f3787eSStefan Hajnoczi /* Ideally we would directly use the PCI BAR resource but
109122f3787eSStefan Hajnoczi * devm_memremap_pages() wants its own copy in pgmap. So
109222f3787eSStefan Hajnoczi * initialize a struct resource from scratch (only the start
109322f3787eSStefan Hajnoczi * and end fields will be used).
109422f3787eSStefan Hajnoczi */
109569456535SLinus Torvalds pgmap->range = (struct range) {
109622f3787eSStefan Hajnoczi .start = (phys_addr_t) cache_reg.addr,
109722f3787eSStefan Hajnoczi .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1,
109822f3787eSStefan Hajnoczi };
109969456535SLinus Torvalds pgmap->nr_range = 1;
110022f3787eSStefan Hajnoczi
110122f3787eSStefan Hajnoczi fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap);
110222f3787eSStefan Hajnoczi if (IS_ERR(fs->window_kaddr))
110322f3787eSStefan Hajnoczi return PTR_ERR(fs->window_kaddr);
110422f3787eSStefan Hajnoczi
110522f3787eSStefan Hajnoczi fs->window_phys_addr = (phys_addr_t) cache_reg.addr;
110622f3787eSStefan Hajnoczi fs->window_len = (phys_addr_t) cache_reg.len;
110722f3787eSStefan Hajnoczi
110822f3787eSStefan Hajnoczi dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n",
110922f3787eSStefan Hajnoczi __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len);
111022f3787eSStefan Hajnoczi
1111562ce828SMathieu Desnoyers fs->dax_dev = no_free_ptr(dax_dev);
111222f3787eSStefan Hajnoczi return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax,
111322f3787eSStefan Hajnoczi fs->dax_dev);
111422f3787eSStefan Hajnoczi }
111522f3787eSStefan Hajnoczi
virtio_fs_probe(struct virtio_device * vdev)1116a62a8ef9SStefan Hajnoczi static int virtio_fs_probe(struct virtio_device *vdev)
1117a62a8ef9SStefan Hajnoczi {
1118a62a8ef9SStefan Hajnoczi struct virtio_fs *fs;
1119a62a8ef9SStefan Hajnoczi int ret;
1120a62a8ef9SStefan Hajnoczi
1121a62a8ef9SStefan Hajnoczi fs = kzalloc(sizeof(*fs), GFP_KERNEL);
1122a62a8ef9SStefan Hajnoczi if (!fs)
1123a62a8ef9SStefan Hajnoczi return -ENOMEM;
1124a8f62f50SStefan Hajnoczi kobject_init(&fs->kobj, &virtio_fs_ktype);
1125a62a8ef9SStefan Hajnoczi vdev->priv = fs;
1126a62a8ef9SStefan Hajnoczi
1127a62a8ef9SStefan Hajnoczi ret = virtio_fs_read_tag(vdev, fs);
1128a62a8ef9SStefan Hajnoczi if (ret < 0)
1129a62a8ef9SStefan Hajnoczi goto out;
1130a62a8ef9SStefan Hajnoczi
1131a62a8ef9SStefan Hajnoczi ret = virtio_fs_setup_vqs(vdev, fs);
1132a62a8ef9SStefan Hajnoczi if (ret < 0)
1133a62a8ef9SStefan Hajnoczi goto out;
1134a62a8ef9SStefan Hajnoczi
1135529395d2SPeter-Jan Gootzen virtio_fs_map_queues(vdev, fs);
1136a62a8ef9SStefan Hajnoczi
113722f3787eSStefan Hajnoczi ret = virtio_fs_setup_dax(vdev, fs);
113822f3787eSStefan Hajnoczi if (ret < 0)
113922f3787eSStefan Hajnoczi goto out_vqs;
114022f3787eSStefan Hajnoczi
1141a62a8ef9SStefan Hajnoczi /* Bring the device online in case the filesystem is mounted and
1142a62a8ef9SStefan Hajnoczi * requests need to be sent before we return.
1143a62a8ef9SStefan Hajnoczi */
1144a62a8ef9SStefan Hajnoczi virtio_device_ready(vdev);
1145a62a8ef9SStefan Hajnoczi
1146a8f62f50SStefan Hajnoczi ret = virtio_fs_add_instance(vdev, fs);
1147a62a8ef9SStefan Hajnoczi if (ret < 0)
1148a62a8ef9SStefan Hajnoczi goto out_vqs;
1149a62a8ef9SStefan Hajnoczi
1150a62a8ef9SStefan Hajnoczi return 0;
1151a62a8ef9SStefan Hajnoczi
1152a62a8ef9SStefan Hajnoczi out_vqs:
1153d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
11541e5b9e04SDeming Wang virtio_fs_cleanup_vqs(vdev);
1155a62a8ef9SStefan Hajnoczi
1156a62a8ef9SStefan Hajnoczi out:
1157a62a8ef9SStefan Hajnoczi vdev->priv = NULL;
1158a8f62f50SStefan Hajnoczi kobject_put(&fs->kobj);
1159a62a8ef9SStefan Hajnoczi return ret;
1160a62a8ef9SStefan Hajnoczi }
1161a62a8ef9SStefan Hajnoczi
virtio_fs_stop_all_queues(struct virtio_fs * fs)1162a62a8ef9SStefan Hajnoczi static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
1163a62a8ef9SStefan Hajnoczi {
1164a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq;
1165a62a8ef9SStefan Hajnoczi int i;
1166a62a8ef9SStefan Hajnoczi
1167a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) {
1168a62a8ef9SStefan Hajnoczi fsvq = &fs->vqs[i];
1169a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
1170a62a8ef9SStefan Hajnoczi fsvq->connected = false;
1171a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
1172a62a8ef9SStefan Hajnoczi }
1173a62a8ef9SStefan Hajnoczi }
1174a62a8ef9SStefan Hajnoczi
virtio_fs_remove(struct virtio_device * vdev)1175a62a8ef9SStefan Hajnoczi static void virtio_fs_remove(struct virtio_device *vdev)
1176a62a8ef9SStefan Hajnoczi {
1177a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = vdev->priv;
1178a62a8ef9SStefan Hajnoczi
1179a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex);
1180a62a8ef9SStefan Hajnoczi /* This device is going away. No one should get new reference */
1181a62a8ef9SStefan Hajnoczi list_del_init(&fs->list);
1182*87cbdc39SMax Gurtovoy virtio_fs_delete_queues_sysfs(fs);
1183a8f62f50SStefan Hajnoczi sysfs_remove_link(&fs->kobj, "device");
1184*87cbdc39SMax Gurtovoy kobject_put(fs->mqs_kobj);
1185a8f62f50SStefan Hajnoczi kobject_del(&fs->kobj);
1186a62a8ef9SStefan Hajnoczi virtio_fs_stop_all_queues(fs);
1187724c15a4SVivek Goyal virtio_fs_drain_all_queues_locked(fs);
1188d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
11891e5b9e04SDeming Wang virtio_fs_cleanup_vqs(vdev);
1190a62a8ef9SStefan Hajnoczi
1191a62a8ef9SStefan Hajnoczi vdev->priv = NULL;
1192a62a8ef9SStefan Hajnoczi /* Put device reference on virtio_fs object */
11934045b642SMax Gurtovoy virtio_fs_put_locked(fs);
1194a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
1195a62a8ef9SStefan Hajnoczi }
1196a62a8ef9SStefan Hajnoczi
1197a62a8ef9SStefan Hajnoczi #ifdef CONFIG_PM_SLEEP
virtio_fs_freeze(struct virtio_device * vdev)1198a62a8ef9SStefan Hajnoczi static int virtio_fs_freeze(struct virtio_device *vdev)
1199a62a8ef9SStefan Hajnoczi {
1200a62a8ef9SStefan Hajnoczi /* TODO need to save state here */
1201a62a8ef9SStefan Hajnoczi pr_warn("virtio-fs: suspend/resume not yet supported\n");
1202a62a8ef9SStefan Hajnoczi return -EOPNOTSUPP;
1203a62a8ef9SStefan Hajnoczi }
1204a62a8ef9SStefan Hajnoczi
virtio_fs_restore(struct virtio_device * vdev)1205a62a8ef9SStefan Hajnoczi static int virtio_fs_restore(struct virtio_device *vdev)
1206a62a8ef9SStefan Hajnoczi {
1207a62a8ef9SStefan Hajnoczi /* TODO need to restore state here */
1208a62a8ef9SStefan Hajnoczi return 0;
1209a62a8ef9SStefan Hajnoczi }
1210a62a8ef9SStefan Hajnoczi #endif /* CONFIG_PM_SLEEP */
1211a62a8ef9SStefan Hajnoczi
121200929447SYueHaibing static const struct virtio_device_id id_table[] = {
1213a62a8ef9SStefan Hajnoczi { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
1214a62a8ef9SStefan Hajnoczi {},
1215a62a8ef9SStefan Hajnoczi };
1216a62a8ef9SStefan Hajnoczi
121700929447SYueHaibing static const unsigned int feature_table[] = {};
1218a62a8ef9SStefan Hajnoczi
1219a62a8ef9SStefan Hajnoczi static struct virtio_driver virtio_fs_driver = {
1220a62a8ef9SStefan Hajnoczi .driver.name = KBUILD_MODNAME,
1221a62a8ef9SStefan Hajnoczi .id_table = id_table,
1222a62a8ef9SStefan Hajnoczi .feature_table = feature_table,
1223a62a8ef9SStefan Hajnoczi .feature_table_size = ARRAY_SIZE(feature_table),
1224a62a8ef9SStefan Hajnoczi .probe = virtio_fs_probe,
1225a62a8ef9SStefan Hajnoczi .remove = virtio_fs_remove,
1226a62a8ef9SStefan Hajnoczi #ifdef CONFIG_PM_SLEEP
1227a62a8ef9SStefan Hajnoczi .freeze = virtio_fs_freeze,
1228a62a8ef9SStefan Hajnoczi .restore = virtio_fs_restore,
1229a62a8ef9SStefan Hajnoczi #endif
1230a62a8ef9SStefan Hajnoczi };
1231a62a8ef9SStefan Hajnoczi
virtio_fs_send_forget(struct fuse_iqueue * fiq,struct fuse_forget_link * link)12325de8acb4SMiklos Szeredi static void virtio_fs_send_forget(struct fuse_iqueue *fiq, struct fuse_forget_link *link)
1233a62a8ef9SStefan Hajnoczi {
1234a62a8ef9SStefan Hajnoczi struct virtio_fs_forget *forget;
12351efcf39eSVivek Goyal struct virtio_fs_forget_req *req;
12365de8acb4SMiklos Szeredi struct virtio_fs *fs = fiq->priv;
12375de8acb4SMiklos Szeredi struct virtio_fs_vq *fsvq = &fs->vqs[VQ_HIPRIO];
12385de8acb4SMiklos Szeredi u64 unique = fuse_get_unique(fiq);
1239a62a8ef9SStefan Hajnoczi
1240a62a8ef9SStefan Hajnoczi /* Allocate a buffer for the request */
1241a62a8ef9SStefan Hajnoczi forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
12421efcf39eSVivek Goyal req = &forget->req;
1243a62a8ef9SStefan Hajnoczi
12441efcf39eSVivek Goyal req->ih = (struct fuse_in_header){
1245a62a8ef9SStefan Hajnoczi .opcode = FUSE_FORGET,
1246a62a8ef9SStefan Hajnoczi .nodeid = link->forget_one.nodeid,
1247a62a8ef9SStefan Hajnoczi .unique = unique,
12481efcf39eSVivek Goyal .len = sizeof(*req),
1249a62a8ef9SStefan Hajnoczi };
12501efcf39eSVivek Goyal req->arg = (struct fuse_forget_in){
1251a62a8ef9SStefan Hajnoczi .nlookup = link->forget_one.nlookup,
1252a62a8ef9SStefan Hajnoczi };
1253a62a8ef9SStefan Hajnoczi
125458ada94fSVivek Goyal send_forget_request(fsvq, forget, false);
1255a62a8ef9SStefan Hajnoczi kfree(link);
1256a62a8ef9SStefan Hajnoczi }
1257a62a8ef9SStefan Hajnoczi
virtio_fs_send_interrupt(struct fuse_iqueue * fiq,struct fuse_req * req)12585de8acb4SMiklos Szeredi static void virtio_fs_send_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
1259a62a8ef9SStefan Hajnoczi {
1260a62a8ef9SStefan Hajnoczi /*
1261a62a8ef9SStefan Hajnoczi * TODO interrupts.
1262a62a8ef9SStefan Hajnoczi *
1263a62a8ef9SStefan Hajnoczi * Normal fs operations on a local filesystems aren't interruptible.
1264a62a8ef9SStefan Hajnoczi * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
1265a62a8ef9SStefan Hajnoczi * with shared lock between host and guest.
1266a62a8ef9SStefan Hajnoczi */
1267a62a8ef9SStefan Hajnoczi }
1268a62a8ef9SStefan Hajnoczi
126942d3e2d0SVivek Goyal /* Count number of scatter-gather elements required */
sg_count_fuse_pages(struct fuse_page_desc * page_descs,unsigned int num_pages,unsigned int total_len)127042d3e2d0SVivek Goyal static unsigned int sg_count_fuse_pages(struct fuse_page_desc *page_descs,
127142d3e2d0SVivek Goyal unsigned int num_pages,
127242d3e2d0SVivek Goyal unsigned int total_len)
127342d3e2d0SVivek Goyal {
127442d3e2d0SVivek Goyal unsigned int i;
127542d3e2d0SVivek Goyal unsigned int this_len;
127642d3e2d0SVivek Goyal
127742d3e2d0SVivek Goyal for (i = 0; i < num_pages && total_len; i++) {
127842d3e2d0SVivek Goyal this_len = min(page_descs[i].length, total_len);
127942d3e2d0SVivek Goyal total_len -= this_len;
128042d3e2d0SVivek Goyal }
128142d3e2d0SVivek Goyal
128242d3e2d0SVivek Goyal return i;
128342d3e2d0SVivek Goyal }
128442d3e2d0SVivek Goyal
1285a62a8ef9SStefan Hajnoczi /* Return the number of scatter-gather list elements required */
sg_count_fuse_req(struct fuse_req * req)1286a62a8ef9SStefan Hajnoczi static unsigned int sg_count_fuse_req(struct fuse_req *req)
1287a62a8ef9SStefan Hajnoczi {
1288a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args;
1289a62a8ef9SStefan Hajnoczi struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
129042d3e2d0SVivek Goyal unsigned int size, total_sgs = 1 /* fuse_in_header */;
1291a62a8ef9SStefan Hajnoczi
1292a62a8ef9SStefan Hajnoczi if (args->in_numargs - args->in_pages)
1293a62a8ef9SStefan Hajnoczi total_sgs += 1;
1294a62a8ef9SStefan Hajnoczi
129542d3e2d0SVivek Goyal if (args->in_pages) {
129642d3e2d0SVivek Goyal size = args->in_args[args->in_numargs - 1].size;
129742d3e2d0SVivek Goyal total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
129842d3e2d0SVivek Goyal size);
129942d3e2d0SVivek Goyal }
1300a62a8ef9SStefan Hajnoczi
1301a62a8ef9SStefan Hajnoczi if (!test_bit(FR_ISREPLY, &req->flags))
1302a62a8ef9SStefan Hajnoczi return total_sgs;
1303a62a8ef9SStefan Hajnoczi
1304a62a8ef9SStefan Hajnoczi total_sgs += 1 /* fuse_out_header */;
1305a62a8ef9SStefan Hajnoczi
1306a62a8ef9SStefan Hajnoczi if (args->out_numargs - args->out_pages)
1307a62a8ef9SStefan Hajnoczi total_sgs += 1;
1308a62a8ef9SStefan Hajnoczi
130942d3e2d0SVivek Goyal if (args->out_pages) {
131042d3e2d0SVivek Goyal size = args->out_args[args->out_numargs - 1].size;
131142d3e2d0SVivek Goyal total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
131242d3e2d0SVivek Goyal size);
131342d3e2d0SVivek Goyal }
1314a62a8ef9SStefan Hajnoczi
1315a62a8ef9SStefan Hajnoczi return total_sgs;
1316a62a8ef9SStefan Hajnoczi }
1317a62a8ef9SStefan Hajnoczi
1318a62a8ef9SStefan Hajnoczi /* Add pages to scatter-gather list and return number of elements used */
sg_init_fuse_pages(struct scatterlist * sg,struct page ** pages,struct fuse_page_desc * page_descs,unsigned int num_pages,unsigned int total_len)1319a62a8ef9SStefan Hajnoczi static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
1320a62a8ef9SStefan Hajnoczi struct page **pages,
1321a62a8ef9SStefan Hajnoczi struct fuse_page_desc *page_descs,
1322a62a8ef9SStefan Hajnoczi unsigned int num_pages,
1323a62a8ef9SStefan Hajnoczi unsigned int total_len)
1324a62a8ef9SStefan Hajnoczi {
1325a62a8ef9SStefan Hajnoczi unsigned int i;
1326a62a8ef9SStefan Hajnoczi unsigned int this_len;
1327a62a8ef9SStefan Hajnoczi
1328a62a8ef9SStefan Hajnoczi for (i = 0; i < num_pages && total_len; i++) {
1329a62a8ef9SStefan Hajnoczi sg_init_table(&sg[i], 1);
1330a62a8ef9SStefan Hajnoczi this_len = min(page_descs[i].length, total_len);
1331a62a8ef9SStefan Hajnoczi sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
1332a62a8ef9SStefan Hajnoczi total_len -= this_len;
1333a62a8ef9SStefan Hajnoczi }
1334a62a8ef9SStefan Hajnoczi
1335a62a8ef9SStefan Hajnoczi return i;
1336a62a8ef9SStefan Hajnoczi }
1337a62a8ef9SStefan Hajnoczi
1338a62a8ef9SStefan Hajnoczi /* Add args to scatter-gather list and return number of elements used */
sg_init_fuse_args(struct scatterlist * sg,struct fuse_req * req,struct fuse_arg * args,unsigned int numargs,bool argpages,void * argbuf,unsigned int * len_used)1339a62a8ef9SStefan Hajnoczi static unsigned int sg_init_fuse_args(struct scatterlist *sg,
1340a62a8ef9SStefan Hajnoczi struct fuse_req *req,
1341a62a8ef9SStefan Hajnoczi struct fuse_arg *args,
1342a62a8ef9SStefan Hajnoczi unsigned int numargs,
1343a62a8ef9SStefan Hajnoczi bool argpages,
1344a62a8ef9SStefan Hajnoczi void *argbuf,
1345a62a8ef9SStefan Hajnoczi unsigned int *len_used)
1346a62a8ef9SStefan Hajnoczi {
1347a62a8ef9SStefan Hajnoczi struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
1348a62a8ef9SStefan Hajnoczi unsigned int total_sgs = 0;
1349a62a8ef9SStefan Hajnoczi unsigned int len;
1350a62a8ef9SStefan Hajnoczi
1351a62a8ef9SStefan Hajnoczi len = fuse_len_args(numargs - argpages, args);
1352a62a8ef9SStefan Hajnoczi if (len)
1353a62a8ef9SStefan Hajnoczi sg_init_one(&sg[total_sgs++], argbuf, len);
1354a62a8ef9SStefan Hajnoczi
1355a62a8ef9SStefan Hajnoczi if (argpages)
1356a62a8ef9SStefan Hajnoczi total_sgs += sg_init_fuse_pages(&sg[total_sgs],
1357a62a8ef9SStefan Hajnoczi ap->pages, ap->descs,
1358a62a8ef9SStefan Hajnoczi ap->num_pages,
1359a62a8ef9SStefan Hajnoczi args[numargs - 1].size);
1360a62a8ef9SStefan Hajnoczi
1361a62a8ef9SStefan Hajnoczi if (len_used)
1362a62a8ef9SStefan Hajnoczi *len_used = len;
1363a62a8ef9SStefan Hajnoczi
1364a62a8ef9SStefan Hajnoczi return total_sgs;
1365a62a8ef9SStefan Hajnoczi }
1366a62a8ef9SStefan Hajnoczi
1367a62a8ef9SStefan Hajnoczi /* Add a request to a virtqueue and kick the device */
virtio_fs_enqueue_req(struct virtio_fs_vq * fsvq,struct fuse_req * req,bool in_flight)1368a62a8ef9SStefan Hajnoczi static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
1369a9bfd9ddSVivek Goyal struct fuse_req *req, bool in_flight)
1370a62a8ef9SStefan Hajnoczi {
1371a62a8ef9SStefan Hajnoczi /* requests need at least 4 elements */
1372a62a8ef9SStefan Hajnoczi struct scatterlist *stack_sgs[6];
1373a62a8ef9SStefan Hajnoczi struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
1374a62a8ef9SStefan Hajnoczi struct scatterlist **sgs = stack_sgs;
1375a62a8ef9SStefan Hajnoczi struct scatterlist *sg = stack_sg;
1376a62a8ef9SStefan Hajnoczi struct virtqueue *vq;
1377a62a8ef9SStefan Hajnoczi struct fuse_args *args = req->args;
1378a62a8ef9SStefan Hajnoczi unsigned int argbuf_used = 0;
1379a62a8ef9SStefan Hajnoczi unsigned int out_sgs = 0;
1380a62a8ef9SStefan Hajnoczi unsigned int in_sgs = 0;
1381a62a8ef9SStefan Hajnoczi unsigned int total_sgs;
1382a62a8ef9SStefan Hajnoczi unsigned int i;
1383a62a8ef9SStefan Hajnoczi int ret;
1384a62a8ef9SStefan Hajnoczi bool notify;
13855dbe190fSVivek Goyal struct fuse_pqueue *fpq;
1386a62a8ef9SStefan Hajnoczi
1387a62a8ef9SStefan Hajnoczi /* Does the sglist fit on the stack? */
1388a62a8ef9SStefan Hajnoczi total_sgs = sg_count_fuse_req(req);
1389a62a8ef9SStefan Hajnoczi if (total_sgs > ARRAY_SIZE(stack_sgs)) {
1390a62a8ef9SStefan Hajnoczi sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
1391a62a8ef9SStefan Hajnoczi sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
1392a62a8ef9SStefan Hajnoczi if (!sgs || !sg) {
1393a62a8ef9SStefan Hajnoczi ret = -ENOMEM;
1394a62a8ef9SStefan Hajnoczi goto out;
1395a62a8ef9SStefan Hajnoczi }
1396a62a8ef9SStefan Hajnoczi }
1397a62a8ef9SStefan Hajnoczi
1398a62a8ef9SStefan Hajnoczi /* Use a bounce buffer since stack args cannot be mapped */
1399a62a8ef9SStefan Hajnoczi ret = copy_args_to_argbuf(req);
1400a62a8ef9SStefan Hajnoczi if (ret < 0)
1401a62a8ef9SStefan Hajnoczi goto out;
1402a62a8ef9SStefan Hajnoczi
1403a62a8ef9SStefan Hajnoczi /* Request elements */
1404a62a8ef9SStefan Hajnoczi sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
1405a62a8ef9SStefan Hajnoczi out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
1406a62a8ef9SStefan Hajnoczi (struct fuse_arg *)args->in_args,
1407a62a8ef9SStefan Hajnoczi args->in_numargs, args->in_pages,
1408a62a8ef9SStefan Hajnoczi req->argbuf, &argbuf_used);
1409a62a8ef9SStefan Hajnoczi
1410a62a8ef9SStefan Hajnoczi /* Reply elements */
1411a62a8ef9SStefan Hajnoczi if (test_bit(FR_ISREPLY, &req->flags)) {
1412a62a8ef9SStefan Hajnoczi sg_init_one(&sg[out_sgs + in_sgs++],
1413a62a8ef9SStefan Hajnoczi &req->out.h, sizeof(req->out.h));
1414a62a8ef9SStefan Hajnoczi in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
1415a62a8ef9SStefan Hajnoczi args->out_args, args->out_numargs,
1416a62a8ef9SStefan Hajnoczi args->out_pages,
1417a62a8ef9SStefan Hajnoczi req->argbuf + argbuf_used, NULL);
1418a62a8ef9SStefan Hajnoczi }
1419a62a8ef9SStefan Hajnoczi
1420a62a8ef9SStefan Hajnoczi WARN_ON(out_sgs + in_sgs != total_sgs);
1421a62a8ef9SStefan Hajnoczi
1422a62a8ef9SStefan Hajnoczi for (i = 0; i < total_sgs; i++)
1423a62a8ef9SStefan Hajnoczi sgs[i] = &sg[i];
1424a62a8ef9SStefan Hajnoczi
1425a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
1426a62a8ef9SStefan Hajnoczi
1427a62a8ef9SStefan Hajnoczi if (!fsvq->connected) {
1428a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
1429a62a8ef9SStefan Hajnoczi ret = -ENOTCONN;
1430a62a8ef9SStefan Hajnoczi goto out;
1431a62a8ef9SStefan Hajnoczi }
1432a62a8ef9SStefan Hajnoczi
1433a62a8ef9SStefan Hajnoczi vq = fsvq->vq;
1434a62a8ef9SStefan Hajnoczi ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
1435a62a8ef9SStefan Hajnoczi if (ret < 0) {
1436a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
1437a62a8ef9SStefan Hajnoczi goto out;
1438a62a8ef9SStefan Hajnoczi }
1439a62a8ef9SStefan Hajnoczi
14405dbe190fSVivek Goyal /* Request successfully sent. */
14415dbe190fSVivek Goyal fpq = &fsvq->fud->pq;
14425dbe190fSVivek Goyal spin_lock(&fpq->lock);
14435dbe190fSVivek Goyal list_add_tail(&req->list, fpq->processing);
14445dbe190fSVivek Goyal spin_unlock(&fpq->lock);
14455dbe190fSVivek Goyal set_bit(FR_SENT, &req->flags);
14465dbe190fSVivek Goyal /* matches barrier in request_wait_answer() */
14475dbe190fSVivek Goyal smp_mb__after_atomic();
14485dbe190fSVivek Goyal
1449a9bfd9ddSVivek Goyal if (!in_flight)
1450c17ea009SVivek Goyal inc_in_flight_req(fsvq);
1451a62a8ef9SStefan Hajnoczi notify = virtqueue_kick_prepare(vq);
1452a62a8ef9SStefan Hajnoczi
1453a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
1454a62a8ef9SStefan Hajnoczi
1455a62a8ef9SStefan Hajnoczi if (notify)
1456a62a8ef9SStefan Hajnoczi virtqueue_notify(vq);
1457a62a8ef9SStefan Hajnoczi
1458a62a8ef9SStefan Hajnoczi out:
1459a62a8ef9SStefan Hajnoczi if (ret < 0 && req->argbuf) {
1460a62a8ef9SStefan Hajnoczi kfree(req->argbuf);
1461a62a8ef9SStefan Hajnoczi req->argbuf = NULL;
1462a62a8ef9SStefan Hajnoczi }
1463a62a8ef9SStefan Hajnoczi if (sgs != stack_sgs) {
1464a62a8ef9SStefan Hajnoczi kfree(sgs);
1465a62a8ef9SStefan Hajnoczi kfree(sg);
1466a62a8ef9SStefan Hajnoczi }
1467a62a8ef9SStefan Hajnoczi
1468a62a8ef9SStefan Hajnoczi return ret;
1469a62a8ef9SStefan Hajnoczi }
1470a62a8ef9SStefan Hajnoczi
virtio_fs_send_req(struct fuse_iqueue * fiq,struct fuse_req * req)14715de8acb4SMiklos Szeredi static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req)
1472a62a8ef9SStefan Hajnoczi {
1473529395d2SPeter-Jan Gootzen unsigned int queue_id;
1474a62a8ef9SStefan Hajnoczi struct virtio_fs *fs;
147551fecdd2SVivek Goyal struct virtio_fs_vq *fsvq;
1476a62a8ef9SStefan Hajnoczi int ret;
1477a62a8ef9SStefan Hajnoczi
14785de8acb4SMiklos Szeredi if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
14795de8acb4SMiklos Szeredi req->in.h.unique = fuse_get_unique(fiq);
14805de8acb4SMiklos Szeredi
1481a62a8ef9SStefan Hajnoczi clear_bit(FR_PENDING, &req->flags);
1482a62a8ef9SStefan Hajnoczi
1483a62a8ef9SStefan Hajnoczi fs = fiq->priv;
1484529395d2SPeter-Jan Gootzen queue_id = VQ_REQUEST + fs->mq_map[raw_smp_processor_id()];
1485a62a8ef9SStefan Hajnoczi
1486529395d2SPeter-Jan Gootzen pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u queue_id %u\n",
1487a62a8ef9SStefan Hajnoczi __func__, req->in.h.opcode, req->in.h.unique,
1488a62a8ef9SStefan Hajnoczi req->in.h.nodeid, req->in.h.len,
1489529395d2SPeter-Jan Gootzen fuse_len_args(req->args->out_numargs, req->args->out_args),
1490529395d2SPeter-Jan Gootzen queue_id);
1491a62a8ef9SStefan Hajnoczi
149251fecdd2SVivek Goyal fsvq = &fs->vqs[queue_id];
1493a9bfd9ddSVivek Goyal ret = virtio_fs_enqueue_req(fsvq, req, false);
1494a62a8ef9SStefan Hajnoczi if (ret < 0) {
14952106e1f4SPeter-Jan Gootzen if (ret == -ENOSPC) {
1496a9bfd9ddSVivek Goyal /*
1497a9bfd9ddSVivek Goyal * Virtqueue full. Retry submission from worker
1498a9bfd9ddSVivek Goyal * context as we might be holding fc->bg_lock.
1499a9bfd9ddSVivek Goyal */
1500a9bfd9ddSVivek Goyal spin_lock(&fsvq->lock);
1501a9bfd9ddSVivek Goyal list_add_tail(&req->list, &fsvq->queued_reqs);
1502a9bfd9ddSVivek Goyal inc_in_flight_req(fsvq);
1503a9bfd9ddSVivek Goyal spin_unlock(&fsvq->lock);
1504a9bfd9ddSVivek Goyal return;
1505a62a8ef9SStefan Hajnoczi }
1506a62a8ef9SStefan Hajnoczi req->out.h.error = ret;
1507a62a8ef9SStefan Hajnoczi pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
150851fecdd2SVivek Goyal
150951fecdd2SVivek Goyal /* Can't end request in submission context. Use a worker */
151051fecdd2SVivek Goyal spin_lock(&fsvq->lock);
151151fecdd2SVivek Goyal list_add_tail(&req->list, &fsvq->end_reqs);
1512106e4df1SPeter-Jan Gootzen schedule_work(&fsvq->dispatch_work);
151351fecdd2SVivek Goyal spin_unlock(&fsvq->lock);
1514a62a8ef9SStefan Hajnoczi return;
1515a62a8ef9SStefan Hajnoczi }
1516a62a8ef9SStefan Hajnoczi }
1517a62a8ef9SStefan Hajnoczi
151800929447SYueHaibing static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
15195de8acb4SMiklos Szeredi .send_forget = virtio_fs_send_forget,
15205de8acb4SMiklos Szeredi .send_interrupt = virtio_fs_send_interrupt,
15215de8acb4SMiklos Szeredi .send_req = virtio_fs_send_req,
1522a62a8ef9SStefan Hajnoczi .release = virtio_fs_fiq_release,
1523a62a8ef9SStefan Hajnoczi };
1524a62a8ef9SStefan Hajnoczi
virtio_fs_ctx_set_defaults(struct fuse_fs_context * ctx)15251dd53957SVivek Goyal static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
1526a62a8ef9SStefan Hajnoczi {
15271dd53957SVivek Goyal ctx->rootmode = S_IFDIR;
15281dd53957SVivek Goyal ctx->default_permissions = 1;
15291dd53957SVivek Goyal ctx->allow_other = 1;
15301dd53957SVivek Goyal ctx->max_read = UINT_MAX;
15311dd53957SVivek Goyal ctx->blksize = 512;
15321dd53957SVivek Goyal ctx->destroy = true;
15331dd53957SVivek Goyal ctx->no_control = true;
15341dd53957SVivek Goyal ctx->no_force_umount = true;
15351dd53957SVivek Goyal }
15361dd53957SVivek Goyal
virtio_fs_fill_super(struct super_block * sb,struct fs_context * fsc)15371dd53957SVivek Goyal static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
1538a62a8ef9SStefan Hajnoczi {
1539fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount_super(sb);
1540fcee216bSMax Reitz struct fuse_conn *fc = fm->fc;
1541a62a8ef9SStefan Hajnoczi struct virtio_fs *fs = fc->iq.priv;
15421dd53957SVivek Goyal struct fuse_fs_context *ctx = fsc->fs_private;
1543a62a8ef9SStefan Hajnoczi unsigned int i;
1544a62a8ef9SStefan Hajnoczi int err;
1545a62a8ef9SStefan Hajnoczi
15461dd53957SVivek Goyal virtio_fs_ctx_set_defaults(ctx);
1547a62a8ef9SStefan Hajnoczi mutex_lock(&virtio_fs_mutex);
1548a62a8ef9SStefan Hajnoczi
1549a62a8ef9SStefan Hajnoczi /* After holding mutex, make sure virtiofs device is still there.
1550a62a8ef9SStefan Hajnoczi * Though we are holding a reference to it, drive ->remove might
1551a62a8ef9SStefan Hajnoczi * still have cleaned up virtual queues. In that case bail out.
1552a62a8ef9SStefan Hajnoczi */
1553a62a8ef9SStefan Hajnoczi err = -EINVAL;
1554a62a8ef9SStefan Hajnoczi if (list_empty(&fs->list)) {
1555a62a8ef9SStefan Hajnoczi pr_info("virtio-fs: tag <%s> not found\n", fs->tag);
1556a62a8ef9SStefan Hajnoczi goto err;
1557a62a8ef9SStefan Hajnoczi }
1558a62a8ef9SStefan Hajnoczi
1559a62a8ef9SStefan Hajnoczi err = -ENOMEM;
1560a62a8ef9SStefan Hajnoczi /* Allocate fuse_dev for hiprio and notification queues */
15617fd3abfaSVivek Goyal for (i = 0; i < fs->nvqs; i++) {
1562a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i];
1563a62a8ef9SStefan Hajnoczi
1564a62a8ef9SStefan Hajnoczi fsvq->fud = fuse_dev_alloc();
1565a62a8ef9SStefan Hajnoczi if (!fsvq->fud)
1566a62a8ef9SStefan Hajnoczi goto err_free_fuse_devs;
1567a62a8ef9SStefan Hajnoczi }
1568a62a8ef9SStefan Hajnoczi
15697fd3abfaSVivek Goyal /* virtiofs allocates and installs its own fuse devices */
15701dd53957SVivek Goyal ctx->fudptr = NULL;
1571780b1b95SJeffle Xu if (ctx->dax_mode != FUSE_DAX_NEVER) {
1572780b1b95SJeffle Xu if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) {
15733f9b9efdSVivek Goyal err = -EINVAL;
15743f9b9efdSVivek Goyal pr_err("virtio-fs: dax can't be enabled as filesystem"
15753f9b9efdSVivek Goyal " device does not support it.\n");
15763f9b9efdSVivek Goyal goto err_free_fuse_devs;
15773f9b9efdSVivek Goyal }
15781dd53957SVivek Goyal ctx->dax_dev = fs->dax_dev;
15793f9b9efdSVivek Goyal }
15801dd53957SVivek Goyal err = fuse_fill_super_common(sb, ctx);
1581a62a8ef9SStefan Hajnoczi if (err < 0)
1582a62a8ef9SStefan Hajnoczi goto err_free_fuse_devs;
1583a62a8ef9SStefan Hajnoczi
1584a62a8ef9SStefan Hajnoczi for (i = 0; i < fs->nvqs; i++) {
1585a62a8ef9SStefan Hajnoczi struct virtio_fs_vq *fsvq = &fs->vqs[i];
1586a62a8ef9SStefan Hajnoczi
1587a62a8ef9SStefan Hajnoczi fuse_dev_install(fsvq->fud, fc);
1588a62a8ef9SStefan Hajnoczi }
1589a62a8ef9SStefan Hajnoczi
1590a62a8ef9SStefan Hajnoczi /* Previous unmount will stop all queues. Start these again */
1591a62a8ef9SStefan Hajnoczi virtio_fs_start_all_queues(fs);
1592fcee216bSMax Reitz fuse_send_init(fm);
1593a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
1594a62a8ef9SStefan Hajnoczi return 0;
1595a62a8ef9SStefan Hajnoczi
1596a62a8ef9SStefan Hajnoczi err_free_fuse_devs:
1597a62a8ef9SStefan Hajnoczi virtio_fs_free_devs(fs);
1598a62a8ef9SStefan Hajnoczi err:
1599a62a8ef9SStefan Hajnoczi mutex_unlock(&virtio_fs_mutex);
1600a62a8ef9SStefan Hajnoczi return err;
1601a62a8ef9SStefan Hajnoczi }
1602a62a8ef9SStefan Hajnoczi
virtio_fs_conn_destroy(struct fuse_mount * fm)1603fcee216bSMax Reitz static void virtio_fs_conn_destroy(struct fuse_mount *fm)
1604a62a8ef9SStefan Hajnoczi {
1605fcee216bSMax Reitz struct fuse_conn *fc = fm->fc;
1606fcee216bSMax Reitz struct virtio_fs *vfs = fc->iq.priv;
1607fcee216bSMax Reitz struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO];
1608a62a8ef9SStefan Hajnoczi
1609fcee216bSMax Reitz /* Stop dax worker. Soon evict_inodes() will be called which
1610fcee216bSMax Reitz * will free all memory ranges belonging to all inodes.
16119a752d18SVivek Goyal */
16129a752d18SVivek Goyal if (IS_ENABLED(CONFIG_FUSE_DAX))
16139a752d18SVivek Goyal fuse_dax_cancel_work(fc);
1614a62a8ef9SStefan Hajnoczi
1615a62a8ef9SStefan Hajnoczi /* Stop forget queue. Soon destroy will be sent */
1616a62a8ef9SStefan Hajnoczi spin_lock(&fsvq->lock);
1617a62a8ef9SStefan Hajnoczi fsvq->connected = false;
1618a62a8ef9SStefan Hajnoczi spin_unlock(&fsvq->lock);
1619a62a8ef9SStefan Hajnoczi virtio_fs_drain_all_queues(vfs);
1620a62a8ef9SStefan Hajnoczi
1621fcee216bSMax Reitz fuse_conn_destroy(fm);
1622a62a8ef9SStefan Hajnoczi
1623fcee216bSMax Reitz /* fuse_conn_destroy() must have sent destroy. Stop all queues
1624a62a8ef9SStefan Hajnoczi * and drain one more time and free fuse devices. Freeing fuse
1625a62a8ef9SStefan Hajnoczi * devices will drop their reference on fuse_conn and that in
1626a62a8ef9SStefan Hajnoczi * turn will drop its reference on virtio_fs object.
1627a62a8ef9SStefan Hajnoczi */
1628a62a8ef9SStefan Hajnoczi virtio_fs_stop_all_queues(vfs);
1629a62a8ef9SStefan Hajnoczi virtio_fs_drain_all_queues(vfs);
1630a62a8ef9SStefan Hajnoczi virtio_fs_free_devs(vfs);
1631a62a8ef9SStefan Hajnoczi }
1632a62a8ef9SStefan Hajnoczi
virtio_kill_sb(struct super_block * sb)1633fcee216bSMax Reitz static void virtio_kill_sb(struct super_block *sb)
1634fcee216bSMax Reitz {
1635fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount_super(sb);
1636fcee216bSMax Reitz bool last;
1637fcee216bSMax Reitz
1638fcee216bSMax Reitz /* If mount failed, we can still be called without any fc */
1639d534d31dSMiklos Szeredi if (sb->s_root) {
1640fcee216bSMax Reitz last = fuse_mount_remove(fm);
1641fcee216bSMax Reitz if (last)
1642fcee216bSMax Reitz virtio_fs_conn_destroy(fm);
1643fcee216bSMax Reitz }
1644fcee216bSMax Reitz kill_anon_super(sb);
1645a27c061aSMiklos Szeredi fuse_mount_destroy(fm);
1646fcee216bSMax Reitz }
1647fcee216bSMax Reitz
virtio_fs_test_super(struct super_block * sb,struct fs_context * fsc)1648a62a8ef9SStefan Hajnoczi static int virtio_fs_test_super(struct super_block *sb,
1649a62a8ef9SStefan Hajnoczi struct fs_context *fsc)
1650a62a8ef9SStefan Hajnoczi {
1651fcee216bSMax Reitz struct fuse_mount *fsc_fm = fsc->s_fs_info;
1652fcee216bSMax Reitz struct fuse_mount *sb_fm = get_fuse_mount_super(sb);
1653a62a8ef9SStefan Hajnoczi
1654fcee216bSMax Reitz return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv;
1655a62a8ef9SStefan Hajnoczi }
1656a62a8ef9SStefan Hajnoczi
virtio_fs_get_tree(struct fs_context * fsc)1657a62a8ef9SStefan Hajnoczi static int virtio_fs_get_tree(struct fs_context *fsc)
1658a62a8ef9SStefan Hajnoczi {
1659a62a8ef9SStefan Hajnoczi struct virtio_fs *fs;
1660a62a8ef9SStefan Hajnoczi struct super_block *sb;
1661a7f0d7aaSConnor Kuehl struct fuse_conn *fc = NULL;
1662fcee216bSMax Reitz struct fuse_mount *fm;
1663a7f0d7aaSConnor Kuehl unsigned int virtqueue_size;
1664a7f0d7aaSConnor Kuehl int err = -EIO;
1665a62a8ef9SStefan Hajnoczi
1666a62a8ef9SStefan Hajnoczi /* This gets a reference on virtio_fs object. This ptr gets installed
1667a62a8ef9SStefan Hajnoczi * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
1668a62a8ef9SStefan Hajnoczi * to drop the reference to this object.
1669a62a8ef9SStefan Hajnoczi */
1670a62a8ef9SStefan Hajnoczi fs = virtio_fs_find_instance(fsc->source);
1671a62a8ef9SStefan Hajnoczi if (!fs) {
1672a62a8ef9SStefan Hajnoczi pr_info("virtio-fs: tag <%s> not found\n", fsc->source);
1673a62a8ef9SStefan Hajnoczi return -EINVAL;
1674a62a8ef9SStefan Hajnoczi }
1675a62a8ef9SStefan Hajnoczi
1676a7f0d7aaSConnor Kuehl virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq);
1677a7f0d7aaSConnor Kuehl if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD))
1678a7f0d7aaSConnor Kuehl goto out_err;
1679a7f0d7aaSConnor Kuehl
1680833c5a42SMiklos Szeredi err = -ENOMEM;
1681a62a8ef9SStefan Hajnoczi fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
1682833c5a42SMiklos Szeredi if (!fc)
1683833c5a42SMiklos Szeredi goto out_err;
1684a62a8ef9SStefan Hajnoczi
1685fcee216bSMax Reitz fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1686833c5a42SMiklos Szeredi if (!fm)
1687833c5a42SMiklos Szeredi goto out_err;
1688fcee216bSMax Reitz
16890a7419c6SMiklos Szeredi fuse_conn_init(fc, fm, fsc->user_ns, &virtio_fs_fiq_ops, fs);
1690a62a8ef9SStefan Hajnoczi fc->release = fuse_free_conn;
1691a62a8ef9SStefan Hajnoczi fc->delete_stale = true;
1692bf109c64SMax Reitz fc->auto_submounts = true;
16932d82ab25SGreg Kurz fc->sync_fs = true;
1694a62a8ef9SStefan Hajnoczi
1695a7f0d7aaSConnor Kuehl /* Tell FUSE to split requests that exceed the virtqueue's size */
1696a7f0d7aaSConnor Kuehl fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit,
1697a7f0d7aaSConnor Kuehl virtqueue_size - FUSE_HEADER_OVERHEAD);
1698a7f0d7aaSConnor Kuehl
1699fcee216bSMax Reitz fsc->s_fs_info = fm;
1700b19d3d00SMiklos Szeredi sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc);
1701c191cd07SMiklos Szeredi if (fsc->s_fs_info)
1702c191cd07SMiklos Szeredi fuse_mount_destroy(fm);
1703a62a8ef9SStefan Hajnoczi if (IS_ERR(sb))
1704a62a8ef9SStefan Hajnoczi return PTR_ERR(sb);
1705a62a8ef9SStefan Hajnoczi
1706a62a8ef9SStefan Hajnoczi if (!sb->s_root) {
17071dd53957SVivek Goyal err = virtio_fs_fill_super(sb, fsc);
1708a62a8ef9SStefan Hajnoczi if (err) {
1709a62a8ef9SStefan Hajnoczi deactivate_locked_super(sb);
1710a62a8ef9SStefan Hajnoczi return err;
1711a62a8ef9SStefan Hajnoczi }
1712a62a8ef9SStefan Hajnoczi
1713a62a8ef9SStefan Hajnoczi sb->s_flags |= SB_ACTIVE;
1714a62a8ef9SStefan Hajnoczi }
1715a62a8ef9SStefan Hajnoczi
1716a62a8ef9SStefan Hajnoczi WARN_ON(fsc->root);
1717a62a8ef9SStefan Hajnoczi fsc->root = dget(sb->s_root);
1718a62a8ef9SStefan Hajnoczi return 0;
1719833c5a42SMiklos Szeredi
1720833c5a42SMiklos Szeredi out_err:
1721833c5a42SMiklos Szeredi kfree(fc);
1722833c5a42SMiklos Szeredi virtio_fs_put(fs);
1723833c5a42SMiklos Szeredi return err;
1724a62a8ef9SStefan Hajnoczi }
1725a62a8ef9SStefan Hajnoczi
1726a62a8ef9SStefan Hajnoczi static const struct fs_context_operations virtio_fs_context_ops = {
172784c21507SMiklos Szeredi .free = virtio_fs_free_fsc,
17281dd53957SVivek Goyal .parse_param = virtio_fs_parse_param,
1729a62a8ef9SStefan Hajnoczi .get_tree = virtio_fs_get_tree,
1730a62a8ef9SStefan Hajnoczi };
1731a62a8ef9SStefan Hajnoczi
virtio_fs_init_fs_context(struct fs_context * fsc)1732a62a8ef9SStefan Hajnoczi static int virtio_fs_init_fs_context(struct fs_context *fsc)
1733a62a8ef9SStefan Hajnoczi {
17341dd53957SVivek Goyal struct fuse_fs_context *ctx;
17351dd53957SVivek Goyal
1736fe0a7bd8SGreg Kurz if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT)
1737fe0a7bd8SGreg Kurz return fuse_init_fs_context_submount(fsc);
1738fe0a7bd8SGreg Kurz
17391dd53957SVivek Goyal ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
17401dd53957SVivek Goyal if (!ctx)
17411dd53957SVivek Goyal return -ENOMEM;
17421dd53957SVivek Goyal fsc->fs_private = ctx;
1743a62a8ef9SStefan Hajnoczi fsc->ops = &virtio_fs_context_ops;
1744a62a8ef9SStefan Hajnoczi return 0;
1745a62a8ef9SStefan Hajnoczi }
1746a62a8ef9SStefan Hajnoczi
1747a62a8ef9SStefan Hajnoczi static struct file_system_type virtio_fs_type = {
1748a62a8ef9SStefan Hajnoczi .owner = THIS_MODULE,
1749a62a8ef9SStefan Hajnoczi .name = "virtiofs",
1750a62a8ef9SStefan Hajnoczi .init_fs_context = virtio_fs_init_fs_context,
1751a62a8ef9SStefan Hajnoczi .kill_sb = virtio_kill_sb,
1752862b9a8eSAlexander Mikhalitsyn .fs_flags = FS_ALLOW_IDMAP,
1753a62a8ef9SStefan Hajnoczi };
1754a62a8ef9SStefan Hajnoczi
virtio_fs_uevent(const struct kobject * kobj,struct kobj_uevent_env * env)17559086b2d9SStefan Hajnoczi static int virtio_fs_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
17569086b2d9SStefan Hajnoczi {
17579086b2d9SStefan Hajnoczi const struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj);
17589086b2d9SStefan Hajnoczi
17599086b2d9SStefan Hajnoczi add_uevent_var(env, "TAG=%s", fs->tag);
17609086b2d9SStefan Hajnoczi return 0;
17619086b2d9SStefan Hajnoczi }
17629086b2d9SStefan Hajnoczi
17639086b2d9SStefan Hajnoczi static const struct kset_uevent_ops virtio_fs_uevent_ops = {
17649086b2d9SStefan Hajnoczi .uevent = virtio_fs_uevent,
17659086b2d9SStefan Hajnoczi };
17669086b2d9SStefan Hajnoczi
virtio_fs_sysfs_init(void)1767a8f62f50SStefan Hajnoczi static int __init virtio_fs_sysfs_init(void)
1768a8f62f50SStefan Hajnoczi {
17699086b2d9SStefan Hajnoczi virtio_fs_kset = kset_create_and_add("virtiofs", &virtio_fs_uevent_ops,
17709086b2d9SStefan Hajnoczi fs_kobj);
1771a8f62f50SStefan Hajnoczi if (!virtio_fs_kset)
1772a8f62f50SStefan Hajnoczi return -ENOMEM;
1773a8f62f50SStefan Hajnoczi return 0;
1774a8f62f50SStefan Hajnoczi }
1775a8f62f50SStefan Hajnoczi
virtio_fs_sysfs_exit(void)1776d30ff898SStefan Hajnoczi static void virtio_fs_sysfs_exit(void)
1777a8f62f50SStefan Hajnoczi {
1778a8f62f50SStefan Hajnoczi kset_unregister(virtio_fs_kset);
1779a8f62f50SStefan Hajnoczi virtio_fs_kset = NULL;
1780a8f62f50SStefan Hajnoczi }
1781a8f62f50SStefan Hajnoczi
virtio_fs_init(void)1782a62a8ef9SStefan Hajnoczi static int __init virtio_fs_init(void)
1783a62a8ef9SStefan Hajnoczi {
1784a62a8ef9SStefan Hajnoczi int ret;
1785a62a8ef9SStefan Hajnoczi
1786a8f62f50SStefan Hajnoczi ret = virtio_fs_sysfs_init();
1787a62a8ef9SStefan Hajnoczi if (ret < 0)
1788a62a8ef9SStefan Hajnoczi return ret;
1789a62a8ef9SStefan Hajnoczi
1790a8f62f50SStefan Hajnoczi ret = register_virtio_driver(&virtio_fs_driver);
1791a8f62f50SStefan Hajnoczi if (ret < 0)
1792a8f62f50SStefan Hajnoczi goto sysfs_exit;
1793a8f62f50SStefan Hajnoczi
1794a62a8ef9SStefan Hajnoczi ret = register_filesystem(&virtio_fs_type);
1795a8f62f50SStefan Hajnoczi if (ret < 0)
1796a8f62f50SStefan Hajnoczi goto unregister_virtio_driver;
1797a62a8ef9SStefan Hajnoczi
1798a62a8ef9SStefan Hajnoczi return 0;
1799a8f62f50SStefan Hajnoczi
1800a8f62f50SStefan Hajnoczi unregister_virtio_driver:
1801a8f62f50SStefan Hajnoczi unregister_virtio_driver(&virtio_fs_driver);
1802a8f62f50SStefan Hajnoczi sysfs_exit:
1803a8f62f50SStefan Hajnoczi virtio_fs_sysfs_exit();
1804a8f62f50SStefan Hajnoczi return ret;
1805a62a8ef9SStefan Hajnoczi }
1806a62a8ef9SStefan Hajnoczi module_init(virtio_fs_init);
1807a62a8ef9SStefan Hajnoczi
virtio_fs_exit(void)1808a62a8ef9SStefan Hajnoczi static void __exit virtio_fs_exit(void)
1809a62a8ef9SStefan Hajnoczi {
1810a62a8ef9SStefan Hajnoczi unregister_filesystem(&virtio_fs_type);
1811a62a8ef9SStefan Hajnoczi unregister_virtio_driver(&virtio_fs_driver);
1812a8f62f50SStefan Hajnoczi virtio_fs_sysfs_exit();
1813a62a8ef9SStefan Hajnoczi }
1814a62a8ef9SStefan Hajnoczi module_exit(virtio_fs_exit);
1815a62a8ef9SStefan Hajnoczi
1816a62a8ef9SStefan Hajnoczi MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
1817a62a8ef9SStefan Hajnoczi MODULE_DESCRIPTION("Virtio Filesystem");
1818a62a8ef9SStefan Hajnoczi MODULE_LICENSE("GPL");
1819a62a8ef9SStefan Hajnoczi MODULE_ALIAS_FS(KBUILD_MODNAME);
1820a62a8ef9SStefan Hajnoczi MODULE_DEVICE_TABLE(virtio, id_table);
1821