]>
Commit | Line | Data |
---|---|---|
a62a8ef9 SH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * virtio-fs: Virtio Filesystem | |
4 | * Copyright (C) 2018 Red Hat, Inc. | |
5 | */ | |
6 | ||
7 | #include <linux/fs.h> | |
22f3787e SH |
8 | #include <linux/dax.h> |
9 | #include <linux/pci.h> | |
529395d2 PJG |
10 | #include <linux/interrupt.h> |
11 | #include <linux/group_cpus.h> | |
22f3787e | 12 | #include <linux/pfn_t.h> |
dc90f084 | 13 | #include <linux/memremap.h> |
a62a8ef9 SH |
14 | #include <linux/module.h> |
15 | #include <linux/virtio.h> | |
16 | #include <linux/virtio_fs.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/fs_context.h> | |
1dd53957 | 19 | #include <linux/fs_parser.h> |
a62a8ef9 | 20 | #include <linux/highmem.h> |
562ce828 | 21 | #include <linux/cleanup.h> |
22f3787e | 22 | #include <linux/uio.h> |
a62a8ef9 SH |
23 | #include "fuse_i.h" |
24 | ||
a7f0d7aa CK |
25 | /* Used to help calculate the FUSE connection's max_pages limit for a request's |
26 | * size. Parts of the struct fuse_req are sliced into scattergather lists in | |
27 | * addition to the pages used, so this can help account for that overhead. | |
28 | */ | |
29 | #define FUSE_HEADER_OVERHEAD 4 | |
30 | ||
a62a8ef9 SH |
31 | /* List of virtio-fs device instances and a lock for the list. Also provides |
32 | * mutual exclusion in device removal and mounting path | |
33 | */ | |
34 | static DEFINE_MUTEX(virtio_fs_mutex); | |
35 | static LIST_HEAD(virtio_fs_instances); | |
36 | ||
a8f62f50 SH |
37 | /* The /sys/fs/virtio_fs/ kset */ |
38 | static struct kset *virtio_fs_kset; | |
39 | ||
a62a8ef9 SH |
40 | enum { |
41 | VQ_HIPRIO, | |
42 | VQ_REQUEST | |
43 | }; | |
44 | ||
b43b7e81 VG |
45 | #define VQ_NAME_LEN 24 |
46 | ||
a62a8ef9 SH |
47 | /* Per-virtqueue state */ |
48 | struct virtio_fs_vq { | |
49 | spinlock_t lock; | |
50 | struct virtqueue *vq; /* protected by ->lock */ | |
51 | struct work_struct done_work; | |
52 | struct list_head queued_reqs; | |
51fecdd2 | 53 | struct list_head end_reqs; /* End these requests */ |
106e4df1 | 54 | struct work_struct dispatch_work; |
a62a8ef9 SH |
55 | struct fuse_dev *fud; |
56 | bool connected; | |
57 | long in_flight; | |
724c15a4 | 58 | struct completion in_flight_zero; /* No inflight requests */ |
87cbdc39 | 59 | struct kobject *kobj; |
b43b7e81 | 60 | char name[VQ_NAME_LEN]; |
a62a8ef9 SH |
61 | } ____cacheline_aligned_in_smp; |
62 | ||
63 | /* A virtio-fs device instance */ | |
64 | struct virtio_fs { | |
a8f62f50 | 65 | struct kobject kobj; |
87cbdc39 | 66 | struct kobject *mqs_kobj; |
a62a8ef9 SH |
67 | struct list_head list; /* on virtio_fs_instances */ |
68 | char *tag; | |
69 | struct virtio_fs_vq *vqs; | |
70 | unsigned int nvqs; /* number of virtqueues */ | |
71 | unsigned int num_request_queues; /* number of request queues */ | |
22f3787e SH |
72 | struct dax_device *dax_dev; |
73 | ||
529395d2 PJG |
74 | unsigned int *mq_map; /* index = cpu id, value = request vq id */ |
75 | ||
22f3787e SH |
76 | /* DAX memory window where file contents are mapped */ |
77 | void *window_kaddr; | |
78 | phys_addr_t window_phys_addr; | |
79 | size_t window_len; | |
a62a8ef9 SH |
80 | }; |
81 | ||
1efcf39e | 82 | struct virtio_fs_forget_req { |
a62a8ef9 SH |
83 | struct fuse_in_header ih; |
84 | struct fuse_forget_in arg; | |
1efcf39e VG |
85 | }; |
86 | ||
87 | struct virtio_fs_forget { | |
a62a8ef9 SH |
88 | /* This request can be temporarily queued on virt queue */ |
89 | struct list_head list; | |
1efcf39e | 90 | struct virtio_fs_forget_req req; |
a62a8ef9 SH |
91 | }; |
92 | ||
bb737bbe VG |
93 | struct virtio_fs_req_work { |
94 | struct fuse_req *req; | |
95 | struct virtio_fs_vq *fsvq; | |
96 | struct work_struct done_work; | |
97 | }; | |
98 | ||
a9bfd9dd | 99 | static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, |
86b74eb5 HT |
100 | struct fuse_req *req, bool in_flight, |
101 | gfp_t gfp); | |
a9bfd9dd | 102 | |
780b1b95 JX |
103 | static const struct constant_table dax_param_enums[] = { |
104 | {"always", FUSE_DAX_ALWAYS }, | |
105 | {"never", FUSE_DAX_NEVER }, | |
106 | {"inode", FUSE_DAX_INODE_USER }, | |
107 | {} | |
108 | }; | |
109 | ||
1dd53957 VG |
110 | enum { |
111 | OPT_DAX, | |
780b1b95 | 112 | OPT_DAX_ENUM, |
1dd53957 VG |
113 | }; |
114 | ||
115 | static const struct fs_parameter_spec virtio_fs_parameters[] = { | |
116 | fsparam_flag("dax", OPT_DAX), | |
780b1b95 | 117 | fsparam_enum("dax", OPT_DAX_ENUM, dax_param_enums), |
1dd53957 VG |
118 | {} |
119 | }; | |
120 | ||
84c21507 | 121 | static int virtio_fs_parse_param(struct fs_context *fsc, |
1dd53957 VG |
122 | struct fs_parameter *param) |
123 | { | |
124 | struct fs_parse_result result; | |
84c21507 | 125 | struct fuse_fs_context *ctx = fsc->fs_private; |
1dd53957 VG |
126 | int opt; |
127 | ||
84c21507 | 128 | opt = fs_parse(fsc, virtio_fs_parameters, param, &result); |
1dd53957 VG |
129 | if (opt < 0) |
130 | return opt; | |
131 | ||
132 | switch (opt) { | |
133 | case OPT_DAX: | |
780b1b95 JX |
134 | ctx->dax_mode = FUSE_DAX_ALWAYS; |
135 | break; | |
136 | case OPT_DAX_ENUM: | |
137 | ctx->dax_mode = result.uint_32; | |
1dd53957 VG |
138 | break; |
139 | default: | |
140 | return -EINVAL; | |
141 | } | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
84c21507 | 146 | static void virtio_fs_free_fsc(struct fs_context *fsc) |
1dd53957 | 147 | { |
84c21507 | 148 | struct fuse_fs_context *ctx = fsc->fs_private; |
1dd53957 VG |
149 | |
150 | kfree(ctx); | |
151 | } | |
152 | ||
a62a8ef9 SH |
153 | static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq) |
154 | { | |
155 | struct virtio_fs *fs = vq->vdev->priv; | |
156 | ||
157 | return &fs->vqs[vq->index]; | |
158 | } | |
159 | ||
c17ea009 VG |
160 | /* Should be called with fsvq->lock held. */ |
161 | static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq) | |
162 | { | |
163 | fsvq->in_flight++; | |
164 | } | |
165 | ||
166 | /* Should be called with fsvq->lock held. */ | |
167 | static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq) | |
168 | { | |
169 | WARN_ON(fsvq->in_flight <= 0); | |
170 | fsvq->in_flight--; | |
724c15a4 VG |
171 | if (!fsvq->in_flight) |
172 | complete(&fsvq->in_flight_zero); | |
c17ea009 VG |
173 | } |
174 | ||
a8f62f50 SH |
175 | static ssize_t tag_show(struct kobject *kobj, |
176 | struct kobj_attribute *attr, char *buf) | |
177 | { | |
178 | struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj); | |
179 | ||
96d88f65 | 180 | return sysfs_emit(buf, "%s\n", fs->tag); |
a8f62f50 SH |
181 | } |
182 | ||
183 | static struct kobj_attribute virtio_fs_tag_attr = __ATTR_RO(tag); | |
184 | ||
185 | static struct attribute *virtio_fs_attrs[] = { | |
186 | &virtio_fs_tag_attr.attr, | |
187 | NULL | |
188 | }; | |
189 | ATTRIBUTE_GROUPS(virtio_fs); | |
190 | ||
191 | static void virtio_fs_ktype_release(struct kobject *kobj) | |
a62a8ef9 | 192 | { |
a8f62f50 | 193 | struct virtio_fs *vfs = container_of(kobj, struct virtio_fs, kobj); |
a62a8ef9 | 194 | |
529395d2 | 195 | kfree(vfs->mq_map); |
a62a8ef9 SH |
196 | kfree(vfs->vqs); |
197 | kfree(vfs); | |
198 | } | |
199 | ||
a8f62f50 SH |
200 | static const struct kobj_type virtio_fs_ktype = { |
201 | .release = virtio_fs_ktype_release, | |
202 | .sysfs_ops = &kobj_sysfs_ops, | |
203 | .default_groups = virtio_fs_groups, | |
204 | }; | |
205 | ||
87cbdc39 MG |
206 | static struct virtio_fs_vq *virtio_fs_kobj_to_vq(struct virtio_fs *fs, |
207 | struct kobject *kobj) | |
208 | { | |
209 | int i; | |
210 | ||
211 | for (i = 0; i < fs->nvqs; i++) { | |
212 | if (kobj == fs->vqs[i].kobj) | |
213 | return &fs->vqs[i]; | |
214 | } | |
215 | return NULL; | |
216 | } | |
217 | ||
218 | static ssize_t name_show(struct kobject *kobj, | |
219 | struct kobj_attribute *attr, char *buf) | |
220 | { | |
221 | struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj); | |
222 | struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj); | |
223 | ||
224 | if (!fsvq) | |
225 | return -EINVAL; | |
226 | return sysfs_emit(buf, "%s\n", fsvq->name); | |
227 | } | |
228 | ||
229 | static struct kobj_attribute virtio_fs_vq_name_attr = __ATTR_RO(name); | |
230 | ||
231 | static ssize_t cpu_list_show(struct kobject *kobj, | |
232 | struct kobj_attribute *attr, char *buf) | |
233 | { | |
234 | struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj); | |
235 | struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj); | |
236 | unsigned int cpu, qid; | |
237 | const size_t size = PAGE_SIZE - 1; | |
238 | bool first = true; | |
239 | int ret = 0, pos = 0; | |
240 | ||
241 | if (!fsvq) | |
242 | return -EINVAL; | |
243 | ||
244 | qid = fsvq->vq->index; | |
245 | for (cpu = 0; cpu < nr_cpu_ids; cpu++) { | |
df28040c | 246 | if (qid < VQ_REQUEST || (fs->mq_map[cpu] == qid)) { |
87cbdc39 MG |
247 | if (first) |
248 | ret = snprintf(buf + pos, size - pos, "%u", cpu); | |
249 | else | |
250 | ret = snprintf(buf + pos, size - pos, ", %u", cpu); | |
251 | ||
252 | if (ret >= size - pos) | |
253 | break; | |
254 | first = false; | |
255 | pos += ret; | |
256 | } | |
257 | } | |
258 | ret = snprintf(buf + pos, size + 1 - pos, "\n"); | |
259 | return pos + ret; | |
260 | } | |
261 | ||
262 | static struct kobj_attribute virtio_fs_vq_cpu_list_attr = __ATTR_RO(cpu_list); | |
263 | ||
264 | static struct attribute *virtio_fs_vq_attrs[] = { | |
265 | &virtio_fs_vq_name_attr.attr, | |
266 | &virtio_fs_vq_cpu_list_attr.attr, | |
267 | NULL | |
268 | }; | |
269 | ||
270 | static struct attribute_group virtio_fs_vq_attr_group = { | |
271 | .attrs = virtio_fs_vq_attrs, | |
272 | }; | |
273 | ||
a62a8ef9 | 274 | /* Make sure virtiofs_mutex is held */ |
4045b642 | 275 | static void virtio_fs_put_locked(struct virtio_fs *fs) |
a62a8ef9 | 276 | { |
4045b642 MG |
277 | lockdep_assert_held(&virtio_fs_mutex); |
278 | ||
a8f62f50 | 279 | kobject_put(&fs->kobj); |
a62a8ef9 SH |
280 | } |
281 | ||
4045b642 MG |
282 | static void virtio_fs_put(struct virtio_fs *fs) |
283 | { | |
284 | mutex_lock(&virtio_fs_mutex); | |
285 | virtio_fs_put_locked(fs); | |
286 | mutex_unlock(&virtio_fs_mutex); | |
287 | } | |
288 | ||
a62a8ef9 SH |
289 | static void virtio_fs_fiq_release(struct fuse_iqueue *fiq) |
290 | { | |
291 | struct virtio_fs *vfs = fiq->priv; | |
292 | ||
a62a8ef9 | 293 | virtio_fs_put(vfs); |
a62a8ef9 SH |
294 | } |
295 | ||
296 | static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq) | |
297 | { | |
298 | WARN_ON(fsvq->in_flight < 0); | |
299 | ||
300 | /* Wait for in flight requests to finish.*/ | |
724c15a4 VG |
301 | spin_lock(&fsvq->lock); |
302 | if (fsvq->in_flight) { | |
303 | /* We are holding virtio_fs_mutex. There should not be any | |
304 | * waiters waiting for completion. | |
305 | */ | |
306 | reinit_completion(&fsvq->in_flight_zero); | |
307 | spin_unlock(&fsvq->lock); | |
308 | wait_for_completion(&fsvq->in_flight_zero); | |
309 | } else { | |
a62a8ef9 | 310 | spin_unlock(&fsvq->lock); |
a62a8ef9 SH |
311 | } |
312 | ||
313 | flush_work(&fsvq->done_work); | |
106e4df1 | 314 | flush_work(&fsvq->dispatch_work); |
a62a8ef9 SH |
315 | } |
316 | ||
724c15a4 | 317 | static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs) |
a62a8ef9 SH |
318 | { |
319 | struct virtio_fs_vq *fsvq; | |
320 | int i; | |
321 | ||
322 | for (i = 0; i < fs->nvqs; i++) { | |
323 | fsvq = &fs->vqs[i]; | |
a62a8ef9 SH |
324 | virtio_fs_drain_queue(fsvq); |
325 | } | |
326 | } | |
327 | ||
724c15a4 VG |
328 | static void virtio_fs_drain_all_queues(struct virtio_fs *fs) |
329 | { | |
330 | /* Provides mutual exclusion between ->remove and ->kill_sb | |
331 | * paths. We don't want both of these draining queue at the | |
332 | * same time. Current completion logic reinits completion | |
333 | * and that means there should not be any other thread | |
334 | * doing reinit or waiting for completion already. | |
335 | */ | |
336 | mutex_lock(&virtio_fs_mutex); | |
337 | virtio_fs_drain_all_queues_locked(fs); | |
338 | mutex_unlock(&virtio_fs_mutex); | |
339 | } | |
340 | ||
a62a8ef9 SH |
341 | static void virtio_fs_start_all_queues(struct virtio_fs *fs) |
342 | { | |
343 | struct virtio_fs_vq *fsvq; | |
344 | int i; | |
345 | ||
346 | for (i = 0; i < fs->nvqs; i++) { | |
347 | fsvq = &fs->vqs[i]; | |
348 | spin_lock(&fsvq->lock); | |
349 | fsvq->connected = true; | |
350 | spin_unlock(&fsvq->lock); | |
351 | } | |
352 | } | |
353 | ||
87cbdc39 MG |
354 | static void virtio_fs_delete_queues_sysfs(struct virtio_fs *fs) |
355 | { | |
356 | struct virtio_fs_vq *fsvq; | |
357 | int i; | |
358 | ||
359 | for (i = 0; i < fs->nvqs; i++) { | |
360 | fsvq = &fs->vqs[i]; | |
361 | kobject_put(fsvq->kobj); | |
362 | } | |
363 | } | |
364 | ||
365 | static int virtio_fs_add_queues_sysfs(struct virtio_fs *fs) | |
366 | { | |
367 | struct virtio_fs_vq *fsvq; | |
368 | char buff[12]; | |
369 | int i, j, ret; | |
370 | ||
371 | for (i = 0; i < fs->nvqs; i++) { | |
372 | fsvq = &fs->vqs[i]; | |
373 | ||
374 | sprintf(buff, "%d", i); | |
375 | fsvq->kobj = kobject_create_and_add(buff, fs->mqs_kobj); | |
376 | if (!fs->mqs_kobj) { | |
377 | ret = -ENOMEM; | |
378 | goto out_del; | |
379 | } | |
380 | ||
381 | ret = sysfs_create_group(fsvq->kobj, &virtio_fs_vq_attr_group); | |
382 | if (ret) { | |
383 | kobject_put(fsvq->kobj); | |
384 | goto out_del; | |
385 | } | |
386 | } | |
387 | ||
388 | return 0; | |
389 | ||
390 | out_del: | |
391 | for (j = 0; j < i; j++) { | |
392 | fsvq = &fs->vqs[j]; | |
393 | kobject_put(fsvq->kobj); | |
394 | } | |
395 | return ret; | |
396 | } | |
397 | ||
a62a8ef9 | 398 | /* Add a new instance to the list or return -EEXIST if tag name exists*/ |
a8f62f50 SH |
399 | static int virtio_fs_add_instance(struct virtio_device *vdev, |
400 | struct virtio_fs *fs) | |
a62a8ef9 SH |
401 | { |
402 | struct virtio_fs *fs2; | |
a8f62f50 | 403 | int ret; |
a62a8ef9 SH |
404 | |
405 | mutex_lock(&virtio_fs_mutex); | |
406 | ||
407 | list_for_each_entry(fs2, &virtio_fs_instances, list) { | |
a8f62f50 SH |
408 | if (strcmp(fs->tag, fs2->tag) == 0) { |
409 | mutex_unlock(&virtio_fs_mutex); | |
410 | return -EEXIST; | |
411 | } | |
a62a8ef9 SH |
412 | } |
413 | ||
a8f62f50 SH |
414 | /* Use the virtio_device's index as a unique identifier, there is no |
415 | * need to allocate our own identifiers because the virtio_fs instance | |
416 | * is only visible to userspace as long as the underlying virtio_device | |
417 | * exists. | |
418 | */ | |
419 | fs->kobj.kset = virtio_fs_kset; | |
420 | ret = kobject_add(&fs->kobj, NULL, "%d", vdev->index); | |
87cbdc39 MG |
421 | if (ret < 0) |
422 | goto out_unlock; | |
423 | ||
424 | fs->mqs_kobj = kobject_create_and_add("mqs", &fs->kobj); | |
425 | if (!fs->mqs_kobj) { | |
426 | ret = -ENOMEM; | |
427 | goto out_del; | |
a8f62f50 SH |
428 | } |
429 | ||
430 | ret = sysfs_create_link(&fs->kobj, &vdev->dev.kobj, "device"); | |
87cbdc39 MG |
431 | if (ret < 0) |
432 | goto out_put; | |
433 | ||
434 | ret = virtio_fs_add_queues_sysfs(fs); | |
435 | if (ret) | |
436 | goto out_remove; | |
a62a8ef9 | 437 | |
a8f62f50 | 438 | list_add_tail(&fs->list, &virtio_fs_instances); |
a62a8ef9 SH |
439 | |
440 | mutex_unlock(&virtio_fs_mutex); | |
441 | ||
9086b2d9 SH |
442 | kobject_uevent(&fs->kobj, KOBJ_ADD); |
443 | ||
a62a8ef9 | 444 | return 0; |
87cbdc39 MG |
445 | |
446 | out_remove: | |
447 | sysfs_remove_link(&fs->kobj, "device"); | |
448 | out_put: | |
449 | kobject_put(fs->mqs_kobj); | |
450 | out_del: | |
451 | kobject_del(&fs->kobj); | |
452 | out_unlock: | |
453 | mutex_unlock(&virtio_fs_mutex); | |
454 | return ret; | |
a62a8ef9 SH |
455 | } |
456 | ||
457 | /* Return the virtio_fs with a given tag, or NULL */ | |
458 | static struct virtio_fs *virtio_fs_find_instance(const char *tag) | |
459 | { | |
460 | struct virtio_fs *fs; | |
461 | ||
462 | mutex_lock(&virtio_fs_mutex); | |
463 | ||
464 | list_for_each_entry(fs, &virtio_fs_instances, list) { | |
465 | if (strcmp(fs->tag, tag) == 0) { | |
a8f62f50 | 466 | kobject_get(&fs->kobj); |
a62a8ef9 SH |
467 | goto found; |
468 | } | |
469 | } | |
470 | ||
471 | fs = NULL; /* not found */ | |
472 | ||
473 | found: | |
474 | mutex_unlock(&virtio_fs_mutex); | |
475 | ||
476 | return fs; | |
477 | } | |
478 | ||
479 | static void virtio_fs_free_devs(struct virtio_fs *fs) | |
480 | { | |
481 | unsigned int i; | |
482 | ||
483 | for (i = 0; i < fs->nvqs; i++) { | |
484 | struct virtio_fs_vq *fsvq = &fs->vqs[i]; | |
485 | ||
486 | if (!fsvq->fud) | |
487 | continue; | |
488 | ||
489 | fuse_dev_free(fsvq->fud); | |
490 | fsvq->fud = NULL; | |
491 | } | |
492 | } | |
493 | ||
494 | /* Read filesystem name from virtio config into fs->tag (must kfree()). */ | |
495 | static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) | |
496 | { | |
497 | char tag_buf[sizeof_field(struct virtio_fs_config, tag)]; | |
498 | char *end; | |
499 | size_t len; | |
500 | ||
501 | virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag), | |
502 | &tag_buf, sizeof(tag_buf)); | |
503 | end = memchr(tag_buf, '\0', sizeof(tag_buf)); | |
504 | if (end == tag_buf) | |
505 | return -EINVAL; /* empty tag */ | |
506 | if (!end) | |
507 | end = &tag_buf[sizeof(tag_buf)]; | |
508 | ||
509 | len = end - tag_buf; | |
510 | fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL); | |
511 | if (!fs->tag) | |
512 | return -ENOMEM; | |
513 | memcpy(fs->tag, tag_buf, len); | |
514 | fs->tag[len] = '\0'; | |
40488cc1 SH |
515 | |
516 | /* While the VIRTIO specification allows any character, newlines are | |
517 | * awkward on mount(8) command-lines and cause problems in the sysfs | |
518 | * "tag" attr and uevent TAG= properties. Forbid them. | |
519 | */ | |
520 | if (strchr(fs->tag, '\n')) { | |
521 | dev_dbg(&vdev->dev, "refusing virtiofs tag with newline character\n"); | |
522 | return -EINVAL; | |
523 | } | |
524 | ||
22d984f1 | 525 | dev_info(&vdev->dev, "discovered new tag: %s\n", fs->tag); |
a62a8ef9 SH |
526 | return 0; |
527 | } | |
528 | ||
529 | /* Work function for hiprio completion */ | |
530 | static void virtio_fs_hiprio_done_work(struct work_struct *work) | |
531 | { | |
532 | struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, | |
533 | done_work); | |
534 | struct virtqueue *vq = fsvq->vq; | |
535 | ||
536 | /* Free completed FUSE_FORGET requests */ | |
537 | spin_lock(&fsvq->lock); | |
538 | do { | |
539 | unsigned int len; | |
540 | void *req; | |
541 | ||
542 | virtqueue_disable_cb(vq); | |
543 | ||
544 | while ((req = virtqueue_get_buf(vq, &len)) != NULL) { | |
545 | kfree(req); | |
c17ea009 | 546 | dec_in_flight_req(fsvq); |
a62a8ef9 | 547 | } |
f9c29137 | 548 | } while (!virtqueue_enable_cb(vq)); |
106e4df1 PJG |
549 | |
550 | if (!list_empty(&fsvq->queued_reqs)) | |
551 | schedule_work(&fsvq->dispatch_work); | |
552 | ||
a62a8ef9 SH |
553 | spin_unlock(&fsvq->lock); |
554 | } | |
555 | ||
51fecdd2 | 556 | static void virtio_fs_request_dispatch_work(struct work_struct *work) |
a62a8ef9 | 557 | { |
51fecdd2 VG |
558 | struct fuse_req *req; |
559 | struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, | |
106e4df1 | 560 | dispatch_work); |
a9bfd9dd | 561 | int ret; |
51fecdd2 VG |
562 | |
563 | pr_debug("virtio-fs: worker %s called.\n", __func__); | |
564 | while (1) { | |
565 | spin_lock(&fsvq->lock); | |
566 | req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req, | |
567 | list); | |
568 | if (!req) { | |
569 | spin_unlock(&fsvq->lock); | |
a9bfd9dd | 570 | break; |
51fecdd2 VG |
571 | } |
572 | ||
573 | list_del_init(&req->list); | |
574 | spin_unlock(&fsvq->lock); | |
8f622e94 | 575 | fuse_request_end(req); |
51fecdd2 | 576 | } |
a9bfd9dd VG |
577 | |
578 | /* Dispatch pending requests */ | |
579 | while (1) { | |
86b74eb5 HT |
580 | unsigned int flags; |
581 | ||
a9bfd9dd VG |
582 | spin_lock(&fsvq->lock); |
583 | req = list_first_entry_or_null(&fsvq->queued_reqs, | |
584 | struct fuse_req, list); | |
585 | if (!req) { | |
586 | spin_unlock(&fsvq->lock); | |
587 | return; | |
588 | } | |
589 | list_del_init(&req->list); | |
590 | spin_unlock(&fsvq->lock); | |
591 | ||
86b74eb5 HT |
592 | flags = memalloc_nofs_save(); |
593 | ret = virtio_fs_enqueue_req(fsvq, req, true, GFP_KERNEL); | |
594 | memalloc_nofs_restore(flags); | |
a9bfd9dd | 595 | if (ret < 0) { |
2106e1f4 | 596 | if (ret == -ENOSPC) { |
a9bfd9dd VG |
597 | spin_lock(&fsvq->lock); |
598 | list_add_tail(&req->list, &fsvq->queued_reqs); | |
a9bfd9dd VG |
599 | spin_unlock(&fsvq->lock); |
600 | return; | |
601 | } | |
602 | req->out.h.error = ret; | |
603 | spin_lock(&fsvq->lock); | |
604 | dec_in_flight_req(fsvq); | |
605 | spin_unlock(&fsvq->lock); | |
606 | pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", | |
607 | ret); | |
8f622e94 | 608 | fuse_request_end(req); |
a9bfd9dd VG |
609 | } |
610 | } | |
a62a8ef9 SH |
611 | } |
612 | ||
58ada94f VG |
613 | /* |
614 | * Returns 1 if queue is full and sender should wait a bit before sending | |
615 | * next request, 0 otherwise. | |
616 | */ | |
617 | static int send_forget_request(struct virtio_fs_vq *fsvq, | |
618 | struct virtio_fs_forget *forget, | |
619 | bool in_flight) | |
620 | { | |
621 | struct scatterlist sg; | |
622 | struct virtqueue *vq; | |
623 | int ret = 0; | |
624 | bool notify; | |
1efcf39e | 625 | struct virtio_fs_forget_req *req = &forget->req; |
58ada94f VG |
626 | |
627 | spin_lock(&fsvq->lock); | |
628 | if (!fsvq->connected) { | |
629 | if (in_flight) | |
630 | dec_in_flight_req(fsvq); | |
631 | kfree(forget); | |
632 | goto out; | |
633 | } | |
634 | ||
1efcf39e | 635 | sg_init_one(&sg, req, sizeof(*req)); |
58ada94f VG |
636 | vq = fsvq->vq; |
637 | dev_dbg(&vq->vdev->dev, "%s\n", __func__); | |
638 | ||
639 | ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC); | |
640 | if (ret < 0) { | |
2106e1f4 | 641 | if (ret == -ENOSPC) { |
58ada94f VG |
642 | pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n", |
643 | ret); | |
644 | list_add_tail(&forget->list, &fsvq->queued_reqs); | |
58ada94f VG |
645 | if (!in_flight) |
646 | inc_in_flight_req(fsvq); | |
647 | /* Queue is full */ | |
648 | ret = 1; | |
649 | } else { | |
650 | pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n", | |
651 | ret); | |
652 | kfree(forget); | |
653 | if (in_flight) | |
654 | dec_in_flight_req(fsvq); | |
655 | } | |
656 | goto out; | |
657 | } | |
658 | ||
659 | if (!in_flight) | |
660 | inc_in_flight_req(fsvq); | |
661 | notify = virtqueue_kick_prepare(vq); | |
662 | spin_unlock(&fsvq->lock); | |
663 | ||
664 | if (notify) | |
665 | virtqueue_notify(vq); | |
666 | return ret; | |
667 | out: | |
668 | spin_unlock(&fsvq->lock); | |
669 | return ret; | |
670 | } | |
671 | ||
a62a8ef9 SH |
672 | static void virtio_fs_hiprio_dispatch_work(struct work_struct *work) |
673 | { | |
674 | struct virtio_fs_forget *forget; | |
675 | struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, | |
106e4df1 | 676 | dispatch_work); |
a62a8ef9 SH |
677 | pr_debug("virtio-fs: worker %s called.\n", __func__); |
678 | while (1) { | |
679 | spin_lock(&fsvq->lock); | |
680 | forget = list_first_entry_or_null(&fsvq->queued_reqs, | |
681 | struct virtio_fs_forget, list); | |
682 | if (!forget) { | |
683 | spin_unlock(&fsvq->lock); | |
684 | return; | |
685 | } | |
686 | ||
687 | list_del(&forget->list); | |
a62a8ef9 | 688 | spin_unlock(&fsvq->lock); |
58ada94f VG |
689 | if (send_forget_request(fsvq, forget, true)) |
690 | return; | |
a62a8ef9 SH |
691 | } |
692 | } | |
693 | ||
694 | /* Allocate and copy args into req->argbuf */ | |
86b74eb5 | 695 | static int copy_args_to_argbuf(struct fuse_req *req, gfp_t gfp) |
a62a8ef9 SH |
696 | { |
697 | struct fuse_args *args = req->args; | |
698 | unsigned int offset = 0; | |
699 | unsigned int num_in; | |
700 | unsigned int num_out; | |
701 | unsigned int len; | |
702 | unsigned int i; | |
703 | ||
704 | num_in = args->in_numargs - args->in_pages; | |
705 | num_out = args->out_numargs - args->out_pages; | |
706 | len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) + | |
707 | fuse_len_args(num_out, args->out_args); | |
708 | ||
86b74eb5 | 709 | req->argbuf = kmalloc(len, gfp); |
a62a8ef9 SH |
710 | if (!req->argbuf) |
711 | return -ENOMEM; | |
712 | ||
713 | for (i = 0; i < num_in; i++) { | |
714 | memcpy(req->argbuf + offset, | |
715 | args->in_args[i].value, | |
716 | args->in_args[i].size); | |
717 | offset += args->in_args[i].size; | |
718 | } | |
719 | ||
720 | return 0; | |
721 | } | |
722 | ||
723 | /* Copy args out of and free req->argbuf */ | |
724 | static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req) | |
725 | { | |
726 | unsigned int remaining; | |
727 | unsigned int offset; | |
728 | unsigned int num_in; | |
729 | unsigned int num_out; | |
730 | unsigned int i; | |
731 | ||
732 | remaining = req->out.h.len - sizeof(req->out.h); | |
733 | num_in = args->in_numargs - args->in_pages; | |
734 | num_out = args->out_numargs - args->out_pages; | |
735 | offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args); | |
736 | ||
737 | for (i = 0; i < num_out; i++) { | |
738 | unsigned int argsize = args->out_args[i].size; | |
739 | ||
740 | if (args->out_argvar && | |
741 | i == args->out_numargs - 1 && | |
742 | argsize > remaining) { | |
743 | argsize = remaining; | |
744 | } | |
745 | ||
746 | memcpy(args->out_args[i].value, req->argbuf + offset, argsize); | |
747 | offset += argsize; | |
748 | ||
749 | if (i != args->out_numargs - 1) | |
750 | remaining -= argsize; | |
751 | } | |
752 | ||
753 | /* Store the actual size of the variable-length arg */ | |
754 | if (args->out_argvar) | |
755 | args->out_args[args->out_numargs - 1].size = remaining; | |
756 | ||
757 | kfree(req->argbuf); | |
758 | req->argbuf = NULL; | |
759 | } | |
760 | ||
761 | /* Work function for request completion */ | |
bb737bbe VG |
762 | static void virtio_fs_request_complete(struct fuse_req *req, |
763 | struct virtio_fs_vq *fsvq) | |
764 | { | |
765 | struct fuse_pqueue *fpq = &fsvq->fud->pq; | |
bb737bbe VG |
766 | struct fuse_args *args; |
767 | struct fuse_args_pages *ap; | |
768 | unsigned int len, i, thislen; | |
29279e1d | 769 | struct folio *folio; |
bb737bbe VG |
770 | |
771 | /* | |
772 | * TODO verify that server properly follows FUSE protocol | |
773 | * (oh.uniq, oh.len) | |
774 | */ | |
775 | args = req->args; | |
776 | copy_args_from_argbuf(args, req); | |
777 | ||
778 | if (args->out_pages && args->page_zeroing) { | |
779 | len = args->out_args[args->out_numargs - 1].size; | |
780 | ap = container_of(args, typeof(*ap), args); | |
68bfb7eb JK |
781 | for (i = 0; i < ap->num_folios; i++) { |
782 | thislen = ap->descs[i].length; | |
783 | if (len < thislen) { | |
784 | WARN_ON(ap->descs[i].offset); | |
785 | folio = ap->folios[i]; | |
786 | folio_zero_segment(folio, len, thislen); | |
787 | len = 0; | |
788 | } else { | |
789 | len -= thislen; | |
bb737bbe VG |
790 | } |
791 | } | |
792 | } | |
793 | ||
794 | spin_lock(&fpq->lock); | |
795 | clear_bit(FR_SENT, &req->flags); | |
796 | spin_unlock(&fpq->lock); | |
797 | ||
8f622e94 | 798 | fuse_request_end(req); |
bb737bbe VG |
799 | spin_lock(&fsvq->lock); |
800 | dec_in_flight_req(fsvq); | |
801 | spin_unlock(&fsvq->lock); | |
802 | } | |
803 | ||
804 | static void virtio_fs_complete_req_work(struct work_struct *work) | |
805 | { | |
806 | struct virtio_fs_req_work *w = | |
807 | container_of(work, typeof(*w), done_work); | |
808 | ||
809 | virtio_fs_request_complete(w->req, w->fsvq); | |
810 | kfree(w); | |
811 | } | |
812 | ||
a62a8ef9 SH |
813 | static void virtio_fs_requests_done_work(struct work_struct *work) |
814 | { | |
815 | struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, | |
816 | done_work); | |
817 | struct fuse_pqueue *fpq = &fsvq->fud->pq; | |
a62a8ef9 SH |
818 | struct virtqueue *vq = fsvq->vq; |
819 | struct fuse_req *req; | |
a62a8ef9 | 820 | struct fuse_req *next; |
bb737bbe | 821 | unsigned int len; |
a62a8ef9 SH |
822 | LIST_HEAD(reqs); |
823 | ||
824 | /* Collect completed requests off the virtqueue */ | |
825 | spin_lock(&fsvq->lock); | |
826 | do { | |
827 | virtqueue_disable_cb(vq); | |
828 | ||
829 | while ((req = virtqueue_get_buf(vq, &len)) != NULL) { | |
830 | spin_lock(&fpq->lock); | |
831 | list_move_tail(&req->list, &reqs); | |
832 | spin_unlock(&fpq->lock); | |
833 | } | |
f9c29137 | 834 | } while (!virtqueue_enable_cb(vq)); |
a62a8ef9 SH |
835 | spin_unlock(&fsvq->lock); |
836 | ||
837 | /* End requests */ | |
838 | list_for_each_entry_safe(req, next, &reqs, list) { | |
a62a8ef9 | 839 | list_del_init(&req->list); |
a62a8ef9 | 840 | |
bb737bbe VG |
841 | /* blocking async request completes in a worker context */ |
842 | if (req->args->may_block) { | |
843 | struct virtio_fs_req_work *w; | |
844 | ||
845 | w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL); | |
846 | INIT_WORK(&w->done_work, virtio_fs_complete_req_work); | |
847 | w->fsvq = fsvq; | |
848 | w->req = req; | |
849 | schedule_work(&w->done_work); | |
850 | } else { | |
851 | virtio_fs_request_complete(req, fsvq); | |
852 | } | |
a62a8ef9 | 853 | } |
106e4df1 PJG |
854 | |
855 | /* Try to push previously queued requests, as the queue might no longer be full */ | |
856 | spin_lock(&fsvq->lock); | |
857 | if (!list_empty(&fsvq->queued_reqs)) | |
858 | schedule_work(&fsvq->dispatch_work); | |
859 | spin_unlock(&fsvq->lock); | |
a62a8ef9 SH |
860 | } |
861 | ||
529395d2 PJG |
862 | static void virtio_fs_map_queues(struct virtio_device *vdev, struct virtio_fs *fs) |
863 | { | |
864 | const struct cpumask *mask, *masks; | |
865 | unsigned int q, cpu; | |
866 | ||
867 | /* First attempt to map using existing transport layer affinities | |
868 | * e.g. PCIe MSI-X | |
869 | */ | |
870 | if (!vdev->config->get_vq_affinity) | |
871 | goto fallback; | |
872 | ||
873 | for (q = 0; q < fs->num_request_queues; q++) { | |
874 | mask = vdev->config->get_vq_affinity(vdev, VQ_REQUEST + q); | |
875 | if (!mask) | |
876 | goto fallback; | |
877 | ||
878 | for_each_cpu(cpu, mask) | |
df28040c | 879 | fs->mq_map[cpu] = q + VQ_REQUEST; |
529395d2 PJG |
880 | } |
881 | ||
882 | return; | |
883 | fallback: | |
884 | /* Attempt to map evenly in groups over the CPUs */ | |
885 | masks = group_cpus_evenly(fs->num_request_queues); | |
df28040c | 886 | /* If even this fails we default to all CPUs use first request queue */ |
529395d2 PJG |
887 | if (!masks) { |
888 | for_each_possible_cpu(cpu) | |
df28040c | 889 | fs->mq_map[cpu] = VQ_REQUEST; |
529395d2 PJG |
890 | return; |
891 | } | |
892 | ||
893 | for (q = 0; q < fs->num_request_queues; q++) { | |
894 | for_each_cpu(cpu, &masks[q]) | |
df28040c | 895 | fs->mq_map[cpu] = q + VQ_REQUEST; |
529395d2 PJG |
896 | } |
897 | kfree(masks); | |
898 | } | |
899 | ||
a62a8ef9 SH |
900 | /* Virtqueue interrupt handler */ |
901 | static void virtio_fs_vq_done(struct virtqueue *vq) | |
902 | { | |
903 | struct virtio_fs_vq *fsvq = vq_to_fsvq(vq); | |
904 | ||
905 | dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name); | |
906 | ||
907 | schedule_work(&fsvq->done_work); | |
908 | } | |
909 | ||
b43b7e81 VG |
910 | static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name, |
911 | int vq_type) | |
912 | { | |
7c594bbd | 913 | strscpy(fsvq->name, name, VQ_NAME_LEN); |
b43b7e81 VG |
914 | spin_lock_init(&fsvq->lock); |
915 | INIT_LIST_HEAD(&fsvq->queued_reqs); | |
916 | INIT_LIST_HEAD(&fsvq->end_reqs); | |
917 | init_completion(&fsvq->in_flight_zero); | |
918 | ||
919 | if (vq_type == VQ_REQUEST) { | |
920 | INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work); | |
106e4df1 PJG |
921 | INIT_WORK(&fsvq->dispatch_work, |
922 | virtio_fs_request_dispatch_work); | |
b43b7e81 VG |
923 | } else { |
924 | INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work); | |
106e4df1 PJG |
925 | INIT_WORK(&fsvq->dispatch_work, |
926 | virtio_fs_hiprio_dispatch_work); | |
b43b7e81 VG |
927 | } |
928 | } | |
929 | ||
a62a8ef9 SH |
930 | /* Initialize virtqueues */ |
931 | static int virtio_fs_setup_vqs(struct virtio_device *vdev, | |
932 | struct virtio_fs *fs) | |
933 | { | |
fc496dcd | 934 | struct virtqueue_info *vqs_info; |
a62a8ef9 | 935 | struct virtqueue **vqs; |
529395d2 PJG |
936 | /* Specify pre_vectors to ensure that the queues before the |
937 | * request queues (e.g. hiprio) don't claim any of the CPUs in | |
938 | * the multi-queue mapping and interrupt affinities | |
939 | */ | |
940 | struct irq_affinity desc = { .pre_vectors = VQ_REQUEST }; | |
a62a8ef9 SH |
941 | unsigned int i; |
942 | int ret = 0; | |
943 | ||
2c0349ec MT |
944 | virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues, |
945 | &fs->num_request_queues); | |
a62a8ef9 SH |
946 | if (fs->num_request_queues == 0) |
947 | return -EINVAL; | |
948 | ||
103c2de1 PJG |
949 | /* Truncate nr of request queues to nr_cpu_id */ |
950 | fs->num_request_queues = min_t(unsigned int, fs->num_request_queues, | |
951 | nr_cpu_ids); | |
b43b7e81 | 952 | fs->nvqs = VQ_REQUEST + fs->num_request_queues; |
a62a8ef9 SH |
953 | fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL); |
954 | if (!fs->vqs) | |
955 | return -ENOMEM; | |
956 | ||
957 | vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL); | |
529395d2 PJG |
958 | fs->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*fs->mq_map), GFP_KERNEL, |
959 | dev_to_node(&vdev->dev)); | |
fc496dcd JP |
960 | vqs_info = kcalloc(fs->nvqs, sizeof(*vqs_info), GFP_KERNEL); |
961 | if (!vqs || !vqs_info || !fs->mq_map) { | |
a62a8ef9 SH |
962 | ret = -ENOMEM; |
963 | goto out; | |
964 | } | |
965 | ||
b43b7e81 | 966 | /* Initialize the hiprio/forget request virtqueue */ |
fc496dcd | 967 | vqs_info[VQ_HIPRIO].callback = virtio_fs_vq_done; |
b43b7e81 | 968 | virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO); |
fc496dcd | 969 | vqs_info[VQ_HIPRIO].name = fs->vqs[VQ_HIPRIO].name; |
a62a8ef9 SH |
970 | |
971 | /* Initialize the requests virtqueues */ | |
972 | for (i = VQ_REQUEST; i < fs->nvqs; i++) { | |
b43b7e81 VG |
973 | char vq_name[VQ_NAME_LEN]; |
974 | ||
975 | snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST); | |
976 | virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST); | |
fc496dcd JP |
977 | vqs_info[i].callback = virtio_fs_vq_done; |
978 | vqs_info[i].name = fs->vqs[i].name; | |
a62a8ef9 SH |
979 | } |
980 | ||
6c85d6b6 | 981 | ret = virtio_find_vqs(vdev, fs->nvqs, vqs, vqs_info, &desc); |
a62a8ef9 SH |
982 | if (ret < 0) |
983 | goto out; | |
984 | ||
985 | for (i = 0; i < fs->nvqs; i++) | |
986 | fs->vqs[i].vq = vqs[i]; | |
987 | ||
988 | virtio_fs_start_all_queues(fs); | |
989 | out: | |
fc496dcd | 990 | kfree(vqs_info); |
a62a8ef9 | 991 | kfree(vqs); |
529395d2 | 992 | if (ret) { |
a62a8ef9 | 993 | kfree(fs->vqs); |
529395d2 PJG |
994 | kfree(fs->mq_map); |
995 | } | |
a62a8ef9 SH |
996 | return ret; |
997 | } | |
998 | ||
999 | /* Free virtqueues (device must already be reset) */ | |
1e5b9e04 | 1000 | static void virtio_fs_cleanup_vqs(struct virtio_device *vdev) |
a62a8ef9 SH |
1001 | { |
1002 | vdev->config->del_vqs(vdev); | |
1003 | } | |
1004 | ||
22f3787e SH |
1005 | /* Map a window offset to a page frame number. The window offset will have |
1006 | * been produced by .iomap_begin(), which maps a file offset to a window | |
1007 | * offset. | |
1008 | */ | |
1009 | static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, | |
e511c4a3 JC |
1010 | long nr_pages, enum dax_access_mode mode, |
1011 | void **kaddr, pfn_t *pfn) | |
22f3787e SH |
1012 | { |
1013 | struct virtio_fs *fs = dax_get_private(dax_dev); | |
1014 | phys_addr_t offset = PFN_PHYS(pgoff); | |
73fb2c8b | 1015 | size_t max_nr_pages = fs->window_len / PAGE_SIZE - pgoff; |
22f3787e SH |
1016 | |
1017 | if (kaddr) | |
1018 | *kaddr = fs->window_kaddr + offset; | |
1019 | if (pfn) | |
1020 | *pfn = phys_to_pfn_t(fs->window_phys_addr + offset, | |
1021 | PFN_DEV | PFN_MAP); | |
1022 | return nr_pages > max_nr_pages ? max_nr_pages : nr_pages; | |
1023 | } | |
1024 | ||
22f3787e SH |
1025 | static int virtio_fs_zero_page_range(struct dax_device *dax_dev, |
1026 | pgoff_t pgoff, size_t nr_pages) | |
1027 | { | |
1028 | long rc; | |
1029 | void *kaddr; | |
1030 | ||
e511c4a3 JC |
1031 | rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS, &kaddr, |
1032 | NULL); | |
22f3787e | 1033 | if (rc < 0) |
1ea7ca1b JC |
1034 | return dax_mem2blk_err(rc); |
1035 | ||
22f3787e SH |
1036 | memset(kaddr, 0, nr_pages << PAGE_SHIFT); |
1037 | dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT); | |
1038 | return 0; | |
1039 | } | |
1040 | ||
1041 | static const struct dax_operations virtio_fs_dax_ops = { | |
1042 | .direct_access = virtio_fs_direct_access, | |
22f3787e SH |
1043 | .zero_page_range = virtio_fs_zero_page_range, |
1044 | }; | |
1045 | ||
1046 | static void virtio_fs_cleanup_dax(void *data) | |
1047 | { | |
1048 | struct dax_device *dax_dev = data; | |
1049 | ||
1050 | kill_dax(dax_dev); | |
1051 | put_dax(dax_dev); | |
1052 | } | |
1053 | ||
562ce828 MD |
1054 | DEFINE_FREE(cleanup_dax, struct dax_dev *, if (!IS_ERR_OR_NULL(_T)) virtio_fs_cleanup_dax(_T)) |
1055 | ||
22f3787e SH |
1056 | static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs) |
1057 | { | |
562ce828 | 1058 | struct dax_device *dax_dev __free(cleanup_dax) = NULL; |
22f3787e SH |
1059 | struct virtio_shm_region cache_reg; |
1060 | struct dev_pagemap *pgmap; | |
1061 | bool have_cache; | |
1062 | ||
1063 | if (!IS_ENABLED(CONFIG_FUSE_DAX)) | |
1064 | return 0; | |
1065 | ||
562ce828 MD |
1066 | dax_dev = alloc_dax(fs, &virtio_fs_dax_ops); |
1067 | if (IS_ERR(dax_dev)) { | |
1068 | int rc = PTR_ERR(dax_dev); | |
1069 | return rc == -EOPNOTSUPP ? 0 : rc; | |
1070 | } | |
1071 | ||
22f3787e SH |
1072 | /* Get cache region */ |
1073 | have_cache = virtio_get_shm_region(vdev, &cache_reg, | |
1074 | (u8)VIRTIO_FS_SHMCAP_ID_CACHE); | |
1075 | if (!have_cache) { | |
1076 | dev_notice(&vdev->dev, "%s: No cache capability\n", __func__); | |
1077 | return 0; | |
1078 | } | |
1079 | ||
1080 | if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len, | |
1081 | dev_name(&vdev->dev))) { | |
1082 | dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n", | |
1083 | cache_reg.addr, cache_reg.len); | |
1084 | return -EBUSY; | |
1085 | } | |
1086 | ||
1087 | dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len, | |
1088 | cache_reg.addr); | |
1089 | ||
1090 | pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL); | |
1091 | if (!pgmap) | |
1092 | return -ENOMEM; | |
1093 | ||
1094 | pgmap->type = MEMORY_DEVICE_FS_DAX; | |
1095 | ||
1096 | /* Ideally we would directly use the PCI BAR resource but | |
1097 | * devm_memremap_pages() wants its own copy in pgmap. So | |
1098 | * initialize a struct resource from scratch (only the start | |
1099 | * and end fields will be used). | |
1100 | */ | |
69456535 | 1101 | pgmap->range = (struct range) { |
22f3787e SH |
1102 | .start = (phys_addr_t) cache_reg.addr, |
1103 | .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1, | |
1104 | }; | |
69456535 | 1105 | pgmap->nr_range = 1; |
22f3787e SH |
1106 | |
1107 | fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap); | |
1108 | if (IS_ERR(fs->window_kaddr)) | |
1109 | return PTR_ERR(fs->window_kaddr); | |
1110 | ||
1111 | fs->window_phys_addr = (phys_addr_t) cache_reg.addr; | |
1112 | fs->window_len = (phys_addr_t) cache_reg.len; | |
1113 | ||
1114 | dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n", | |
1115 | __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len); | |
1116 | ||
562ce828 | 1117 | fs->dax_dev = no_free_ptr(dax_dev); |
22f3787e SH |
1118 | return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax, |
1119 | fs->dax_dev); | |
1120 | } | |
1121 | ||
a62a8ef9 SH |
1122 | static int virtio_fs_probe(struct virtio_device *vdev) |
1123 | { | |
1124 | struct virtio_fs *fs; | |
1125 | int ret; | |
1126 | ||
1127 | fs = kzalloc(sizeof(*fs), GFP_KERNEL); | |
1128 | if (!fs) | |
1129 | return -ENOMEM; | |
a8f62f50 | 1130 | kobject_init(&fs->kobj, &virtio_fs_ktype); |
a62a8ef9 SH |
1131 | vdev->priv = fs; |
1132 | ||
1133 | ret = virtio_fs_read_tag(vdev, fs); | |
1134 | if (ret < 0) | |
1135 | goto out; | |
1136 | ||
1137 | ret = virtio_fs_setup_vqs(vdev, fs); | |
1138 | if (ret < 0) | |
1139 | goto out; | |
1140 | ||
529395d2 | 1141 | virtio_fs_map_queues(vdev, fs); |
a62a8ef9 | 1142 | |
22f3787e SH |
1143 | ret = virtio_fs_setup_dax(vdev, fs); |
1144 | if (ret < 0) | |
1145 | goto out_vqs; | |
1146 | ||
a62a8ef9 SH |
1147 | /* Bring the device online in case the filesystem is mounted and |
1148 | * requests need to be sent before we return. | |
1149 | */ | |
1150 | virtio_device_ready(vdev); | |
1151 | ||
a8f62f50 | 1152 | ret = virtio_fs_add_instance(vdev, fs); |
a62a8ef9 SH |
1153 | if (ret < 0) |
1154 | goto out_vqs; | |
1155 | ||
1156 | return 0; | |
1157 | ||
1158 | out_vqs: | |
d9679d00 | 1159 | virtio_reset_device(vdev); |
1e5b9e04 | 1160 | virtio_fs_cleanup_vqs(vdev); |
a62a8ef9 SH |
1161 | |
1162 | out: | |
1163 | vdev->priv = NULL; | |
a8f62f50 | 1164 | kobject_put(&fs->kobj); |
a62a8ef9 SH |
1165 | return ret; |
1166 | } | |
1167 | ||
1168 | static void virtio_fs_stop_all_queues(struct virtio_fs *fs) | |
1169 | { | |
1170 | struct virtio_fs_vq *fsvq; | |
1171 | int i; | |
1172 | ||
1173 | for (i = 0; i < fs->nvqs; i++) { | |
1174 | fsvq = &fs->vqs[i]; | |
1175 | spin_lock(&fsvq->lock); | |
1176 | fsvq->connected = false; | |
1177 | spin_unlock(&fsvq->lock); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | static void virtio_fs_remove(struct virtio_device *vdev) | |
1182 | { | |
1183 | struct virtio_fs *fs = vdev->priv; | |
1184 | ||
1185 | mutex_lock(&virtio_fs_mutex); | |
1186 | /* This device is going away. No one should get new reference */ | |
1187 | list_del_init(&fs->list); | |
87cbdc39 | 1188 | virtio_fs_delete_queues_sysfs(fs); |
a8f62f50 | 1189 | sysfs_remove_link(&fs->kobj, "device"); |
87cbdc39 | 1190 | kobject_put(fs->mqs_kobj); |
a8f62f50 | 1191 | kobject_del(&fs->kobj); |
a62a8ef9 | 1192 | virtio_fs_stop_all_queues(fs); |
724c15a4 | 1193 | virtio_fs_drain_all_queues_locked(fs); |
d9679d00 | 1194 | virtio_reset_device(vdev); |
1e5b9e04 | 1195 | virtio_fs_cleanup_vqs(vdev); |
a62a8ef9 SH |
1196 | |
1197 | vdev->priv = NULL; | |
1198 | /* Put device reference on virtio_fs object */ | |
4045b642 | 1199 | virtio_fs_put_locked(fs); |
a62a8ef9 SH |
1200 | mutex_unlock(&virtio_fs_mutex); |
1201 | } | |
1202 | ||
1203 | #ifdef CONFIG_PM_SLEEP | |
1204 | static int virtio_fs_freeze(struct virtio_device *vdev) | |
1205 | { | |
1206 | /* TODO need to save state here */ | |
1207 | pr_warn("virtio-fs: suspend/resume not yet supported\n"); | |
1208 | return -EOPNOTSUPP; | |
1209 | } | |
1210 | ||
1211 | static int virtio_fs_restore(struct virtio_device *vdev) | |
1212 | { | |
1213 | /* TODO need to restore state here */ | |
1214 | return 0; | |
1215 | } | |
1216 | #endif /* CONFIG_PM_SLEEP */ | |
1217 | ||
00929447 | 1218 | static const struct virtio_device_id id_table[] = { |
a62a8ef9 SH |
1219 | { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID }, |
1220 | {}, | |
1221 | }; | |
1222 | ||
00929447 | 1223 | static const unsigned int feature_table[] = {}; |
a62a8ef9 SH |
1224 | |
1225 | static struct virtio_driver virtio_fs_driver = { | |
1226 | .driver.name = KBUILD_MODNAME, | |
a62a8ef9 SH |
1227 | .id_table = id_table, |
1228 | .feature_table = feature_table, | |
1229 | .feature_table_size = ARRAY_SIZE(feature_table), | |
1230 | .probe = virtio_fs_probe, | |
1231 | .remove = virtio_fs_remove, | |
1232 | #ifdef CONFIG_PM_SLEEP | |
1233 | .freeze = virtio_fs_freeze, | |
1234 | .restore = virtio_fs_restore, | |
1235 | #endif | |
1236 | }; | |
1237 | ||
5de8acb4 | 1238 | static void virtio_fs_send_forget(struct fuse_iqueue *fiq, struct fuse_forget_link *link) |
a62a8ef9 | 1239 | { |
a62a8ef9 | 1240 | struct virtio_fs_forget *forget; |
1efcf39e | 1241 | struct virtio_fs_forget_req *req; |
5de8acb4 MS |
1242 | struct virtio_fs *fs = fiq->priv; |
1243 | struct virtio_fs_vq *fsvq = &fs->vqs[VQ_HIPRIO]; | |
1244 | u64 unique = fuse_get_unique(fiq); | |
a62a8ef9 SH |
1245 | |
1246 | /* Allocate a buffer for the request */ | |
1247 | forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL); | |
1efcf39e | 1248 | req = &forget->req; |
a62a8ef9 | 1249 | |
1efcf39e | 1250 | req->ih = (struct fuse_in_header){ |
a62a8ef9 SH |
1251 | .opcode = FUSE_FORGET, |
1252 | .nodeid = link->forget_one.nodeid, | |
1253 | .unique = unique, | |
1efcf39e | 1254 | .len = sizeof(*req), |
a62a8ef9 | 1255 | }; |
1efcf39e | 1256 | req->arg = (struct fuse_forget_in){ |
a62a8ef9 SH |
1257 | .nlookup = link->forget_one.nlookup, |
1258 | }; | |
1259 | ||
58ada94f | 1260 | send_forget_request(fsvq, forget, false); |
a62a8ef9 SH |
1261 | kfree(link); |
1262 | } | |
1263 | ||
5de8acb4 | 1264 | static void virtio_fs_send_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) |
a62a8ef9 SH |
1265 | { |
1266 | /* | |
1267 | * TODO interrupts. | |
1268 | * | |
1269 | * Normal fs operations on a local filesystems aren't interruptible. | |
1270 | * Exceptions are blocking lock operations; for example fcntl(F_SETLKW) | |
1271 | * with shared lock between host and guest. | |
1272 | */ | |
a62a8ef9 SH |
1273 | } |
1274 | ||
42d3e2d0 | 1275 | /* Count number of scatter-gather elements required */ |
68bfb7eb JK |
1276 | static unsigned int sg_count_fuse_folios(struct fuse_folio_desc *folio_descs, |
1277 | unsigned int num_folios, | |
1278 | unsigned int total_len) | |
42d3e2d0 VG |
1279 | { |
1280 | unsigned int i; | |
1281 | unsigned int this_len; | |
1282 | ||
68bfb7eb JK |
1283 | for (i = 0; i < num_folios && total_len; i++) { |
1284 | this_len = min(folio_descs[i].length, total_len); | |
1285 | total_len -= this_len; | |
42d3e2d0 VG |
1286 | } |
1287 | ||
1288 | return i; | |
1289 | } | |
1290 | ||
a62a8ef9 SH |
1291 | /* Return the number of scatter-gather list elements required */ |
1292 | static unsigned int sg_count_fuse_req(struct fuse_req *req) | |
1293 | { | |
1294 | struct fuse_args *args = req->args; | |
1295 | struct fuse_args_pages *ap = container_of(args, typeof(*ap), args); | |
42d3e2d0 | 1296 | unsigned int size, total_sgs = 1 /* fuse_in_header */; |
a62a8ef9 SH |
1297 | |
1298 | if (args->in_numargs - args->in_pages) | |
1299 | total_sgs += 1; | |
1300 | ||
42d3e2d0 VG |
1301 | if (args->in_pages) { |
1302 | size = args->in_args[args->in_numargs - 1].size; | |
68bfb7eb JK |
1303 | total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, |
1304 | size); | |
42d3e2d0 | 1305 | } |
a62a8ef9 SH |
1306 | |
1307 | if (!test_bit(FR_ISREPLY, &req->flags)) | |
1308 | return total_sgs; | |
1309 | ||
1310 | total_sgs += 1 /* fuse_out_header */; | |
1311 | ||
1312 | if (args->out_numargs - args->out_pages) | |
1313 | total_sgs += 1; | |
1314 | ||
42d3e2d0 VG |
1315 | if (args->out_pages) { |
1316 | size = args->out_args[args->out_numargs - 1].size; | |
68bfb7eb JK |
1317 | total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, |
1318 | size); | |
42d3e2d0 | 1319 | } |
a62a8ef9 SH |
1320 | |
1321 | return total_sgs; | |
1322 | } | |
1323 | ||
68bfb7eb JK |
1324 | /* Add folios to scatter-gather list and return number of elements used */ |
1325 | static unsigned int sg_init_fuse_folios(struct scatterlist *sg, | |
1326 | struct folio **folios, | |
1327 | struct fuse_folio_desc *folio_descs, | |
1328 | unsigned int num_folios, | |
1329 | unsigned int total_len) | |
a62a8ef9 SH |
1330 | { |
1331 | unsigned int i; | |
1332 | unsigned int this_len; | |
1333 | ||
68bfb7eb JK |
1334 | for (i = 0; i < num_folios && total_len; i++) { |
1335 | sg_init_table(&sg[i], 1); | |
1336 | this_len = min(folio_descs[i].length, total_len); | |
1337 | sg_set_folio(&sg[i], folios[i], this_len, folio_descs[i].offset); | |
1338 | total_len -= this_len; | |
a62a8ef9 SH |
1339 | } |
1340 | ||
1341 | return i; | |
1342 | } | |
1343 | ||
1344 | /* Add args to scatter-gather list and return number of elements used */ | |
1345 | static unsigned int sg_init_fuse_args(struct scatterlist *sg, | |
1346 | struct fuse_req *req, | |
1347 | struct fuse_arg *args, | |
1348 | unsigned int numargs, | |
1349 | bool argpages, | |
1350 | void *argbuf, | |
1351 | unsigned int *len_used) | |
1352 | { | |
1353 | struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); | |
1354 | unsigned int total_sgs = 0; | |
1355 | unsigned int len; | |
1356 | ||
1357 | len = fuse_len_args(numargs - argpages, args); | |
1358 | if (len) | |
1359 | sg_init_one(&sg[total_sgs++], argbuf, len); | |
1360 | ||
1361 | if (argpages) | |
68bfb7eb JK |
1362 | total_sgs += sg_init_fuse_folios(&sg[total_sgs], |
1363 | ap->folios, ap->descs, | |
1364 | ap->num_folios, | |
1365 | args[numargs - 1].size); | |
a62a8ef9 SH |
1366 | |
1367 | if (len_used) | |
1368 | *len_used = len; | |
1369 | ||
1370 | return total_sgs; | |
1371 | } | |
1372 | ||
1373 | /* Add a request to a virtqueue and kick the device */ | |
1374 | static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, | |
86b74eb5 HT |
1375 | struct fuse_req *req, bool in_flight, |
1376 | gfp_t gfp) | |
a62a8ef9 SH |
1377 | { |
1378 | /* requests need at least 4 elements */ | |
1379 | struct scatterlist *stack_sgs[6]; | |
1380 | struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)]; | |
1381 | struct scatterlist **sgs = stack_sgs; | |
1382 | struct scatterlist *sg = stack_sg; | |
1383 | struct virtqueue *vq; | |
1384 | struct fuse_args *args = req->args; | |
1385 | unsigned int argbuf_used = 0; | |
1386 | unsigned int out_sgs = 0; | |
1387 | unsigned int in_sgs = 0; | |
1388 | unsigned int total_sgs; | |
1389 | unsigned int i; | |
1390 | int ret; | |
1391 | bool notify; | |
5dbe190f | 1392 | struct fuse_pqueue *fpq; |
a62a8ef9 SH |
1393 | |
1394 | /* Does the sglist fit on the stack? */ | |
1395 | total_sgs = sg_count_fuse_req(req); | |
1396 | if (total_sgs > ARRAY_SIZE(stack_sgs)) { | |
86b74eb5 HT |
1397 | sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), gfp); |
1398 | sg = kmalloc_array(total_sgs, sizeof(sg[0]), gfp); | |
a62a8ef9 SH |
1399 | if (!sgs || !sg) { |
1400 | ret = -ENOMEM; | |
1401 | goto out; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | /* Use a bounce buffer since stack args cannot be mapped */ | |
86b74eb5 | 1406 | ret = copy_args_to_argbuf(req, gfp); |
a62a8ef9 SH |
1407 | if (ret < 0) |
1408 | goto out; | |
1409 | ||
1410 | /* Request elements */ | |
1411 | sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h)); | |
1412 | out_sgs += sg_init_fuse_args(&sg[out_sgs], req, | |
1413 | (struct fuse_arg *)args->in_args, | |
1414 | args->in_numargs, args->in_pages, | |
1415 | req->argbuf, &argbuf_used); | |
1416 | ||
1417 | /* Reply elements */ | |
1418 | if (test_bit(FR_ISREPLY, &req->flags)) { | |
1419 | sg_init_one(&sg[out_sgs + in_sgs++], | |
1420 | &req->out.h, sizeof(req->out.h)); | |
1421 | in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req, | |
1422 | args->out_args, args->out_numargs, | |
1423 | args->out_pages, | |
1424 | req->argbuf + argbuf_used, NULL); | |
1425 | } | |
1426 | ||
1427 | WARN_ON(out_sgs + in_sgs != total_sgs); | |
1428 | ||
1429 | for (i = 0; i < total_sgs; i++) | |
1430 | sgs[i] = &sg[i]; | |
1431 | ||
1432 | spin_lock(&fsvq->lock); | |
1433 | ||
1434 | if (!fsvq->connected) { | |
1435 | spin_unlock(&fsvq->lock); | |
1436 | ret = -ENOTCONN; | |
1437 | goto out; | |
1438 | } | |
1439 | ||
1440 | vq = fsvq->vq; | |
1441 | ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC); | |
1442 | if (ret < 0) { | |
1443 | spin_unlock(&fsvq->lock); | |
1444 | goto out; | |
1445 | } | |
1446 | ||
5dbe190f VG |
1447 | /* Request successfully sent. */ |
1448 | fpq = &fsvq->fud->pq; | |
1449 | spin_lock(&fpq->lock); | |
1450 | list_add_tail(&req->list, fpq->processing); | |
1451 | spin_unlock(&fpq->lock); | |
1452 | set_bit(FR_SENT, &req->flags); | |
1453 | /* matches barrier in request_wait_answer() */ | |
1454 | smp_mb__after_atomic(); | |
1455 | ||
a9bfd9dd VG |
1456 | if (!in_flight) |
1457 | inc_in_flight_req(fsvq); | |
a62a8ef9 SH |
1458 | notify = virtqueue_kick_prepare(vq); |
1459 | ||
1460 | spin_unlock(&fsvq->lock); | |
1461 | ||
1462 | if (notify) | |
1463 | virtqueue_notify(vq); | |
1464 | ||
1465 | out: | |
1466 | if (ret < 0 && req->argbuf) { | |
1467 | kfree(req->argbuf); | |
1468 | req->argbuf = NULL; | |
1469 | } | |
1470 | if (sgs != stack_sgs) { | |
1471 | kfree(sgs); | |
1472 | kfree(sg); | |
1473 | } | |
1474 | ||
1475 | return ret; | |
1476 | } | |
1477 | ||
5de8acb4 | 1478 | static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req) |
a62a8ef9 | 1479 | { |
529395d2 | 1480 | unsigned int queue_id; |
a62a8ef9 | 1481 | struct virtio_fs *fs; |
51fecdd2 | 1482 | struct virtio_fs_vq *fsvq; |
a62a8ef9 SH |
1483 | int ret; |
1484 | ||
5de8acb4 MS |
1485 | if (req->in.h.opcode != FUSE_NOTIFY_REPLY) |
1486 | req->in.h.unique = fuse_get_unique(fiq); | |
1487 | ||
a62a8ef9 | 1488 | clear_bit(FR_PENDING, &req->flags); |
a62a8ef9 SH |
1489 | |
1490 | fs = fiq->priv; | |
df28040c | 1491 | queue_id = fs->mq_map[raw_smp_processor_id()]; |
a62a8ef9 | 1492 | |
529395d2 PJG |
1493 | pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u queue_id %u\n", |
1494 | __func__, req->in.h.opcode, req->in.h.unique, | |
a62a8ef9 | 1495 | req->in.h.nodeid, req->in.h.len, |
529395d2 PJG |
1496 | fuse_len_args(req->args->out_numargs, req->args->out_args), |
1497 | queue_id); | |
a62a8ef9 | 1498 | |
51fecdd2 | 1499 | fsvq = &fs->vqs[queue_id]; |
86b74eb5 | 1500 | ret = virtio_fs_enqueue_req(fsvq, req, false, GFP_ATOMIC); |
a62a8ef9 | 1501 | if (ret < 0) { |
2106e1f4 | 1502 | if (ret == -ENOSPC) { |
a9bfd9dd VG |
1503 | /* |
1504 | * Virtqueue full. Retry submission from worker | |
1505 | * context as we might be holding fc->bg_lock. | |
1506 | */ | |
1507 | spin_lock(&fsvq->lock); | |
1508 | list_add_tail(&req->list, &fsvq->queued_reqs); | |
1509 | inc_in_flight_req(fsvq); | |
a9bfd9dd VG |
1510 | spin_unlock(&fsvq->lock); |
1511 | return; | |
a62a8ef9 SH |
1512 | } |
1513 | req->out.h.error = ret; | |
1514 | pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret); | |
51fecdd2 VG |
1515 | |
1516 | /* Can't end request in submission context. Use a worker */ | |
1517 | spin_lock(&fsvq->lock); | |
1518 | list_add_tail(&req->list, &fsvq->end_reqs); | |
106e4df1 | 1519 | schedule_work(&fsvq->dispatch_work); |
51fecdd2 | 1520 | spin_unlock(&fsvq->lock); |
a62a8ef9 SH |
1521 | return; |
1522 | } | |
1523 | } | |
1524 | ||
00929447 | 1525 | static const struct fuse_iqueue_ops virtio_fs_fiq_ops = { |
5de8acb4 MS |
1526 | .send_forget = virtio_fs_send_forget, |
1527 | .send_interrupt = virtio_fs_send_interrupt, | |
1528 | .send_req = virtio_fs_send_req, | |
1529 | .release = virtio_fs_fiq_release, | |
a62a8ef9 SH |
1530 | }; |
1531 | ||
1dd53957 | 1532 | static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx) |
a62a8ef9 | 1533 | { |
1dd53957 VG |
1534 | ctx->rootmode = S_IFDIR; |
1535 | ctx->default_permissions = 1; | |
1536 | ctx->allow_other = 1; | |
1537 | ctx->max_read = UINT_MAX; | |
1538 | ctx->blksize = 512; | |
1539 | ctx->destroy = true; | |
1540 | ctx->no_control = true; | |
1541 | ctx->no_force_umount = true; | |
1542 | } | |
1543 | ||
1544 | static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) | |
a62a8ef9 | 1545 | { |
fcee216b MR |
1546 | struct fuse_mount *fm = get_fuse_mount_super(sb); |
1547 | struct fuse_conn *fc = fm->fc; | |
a62a8ef9 | 1548 | struct virtio_fs *fs = fc->iq.priv; |
1dd53957 | 1549 | struct fuse_fs_context *ctx = fsc->fs_private; |
a62a8ef9 SH |
1550 | unsigned int i; |
1551 | int err; | |
a62a8ef9 | 1552 | |
1dd53957 | 1553 | virtio_fs_ctx_set_defaults(ctx); |
a62a8ef9 SH |
1554 | mutex_lock(&virtio_fs_mutex); |
1555 | ||
1556 | /* After holding mutex, make sure virtiofs device is still there. | |
1557 | * Though we are holding a reference to it, drive ->remove might | |
1558 | * still have cleaned up virtual queues. In that case bail out. | |
1559 | */ | |
1560 | err = -EINVAL; | |
1561 | if (list_empty(&fs->list)) { | |
1562 | pr_info("virtio-fs: tag <%s> not found\n", fs->tag); | |
1563 | goto err; | |
1564 | } | |
1565 | ||
1566 | err = -ENOMEM; | |
1567 | /* Allocate fuse_dev for hiprio and notification queues */ | |
7fd3abfa | 1568 | for (i = 0; i < fs->nvqs; i++) { |
a62a8ef9 SH |
1569 | struct virtio_fs_vq *fsvq = &fs->vqs[i]; |
1570 | ||
1571 | fsvq->fud = fuse_dev_alloc(); | |
1572 | if (!fsvq->fud) | |
1573 | goto err_free_fuse_devs; | |
1574 | } | |
1575 | ||
7fd3abfa | 1576 | /* virtiofs allocates and installs its own fuse devices */ |
1dd53957 | 1577 | ctx->fudptr = NULL; |
780b1b95 JX |
1578 | if (ctx->dax_mode != FUSE_DAX_NEVER) { |
1579 | if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) { | |
3f9b9efd VG |
1580 | err = -EINVAL; |
1581 | pr_err("virtio-fs: dax can't be enabled as filesystem" | |
1582 | " device does not support it.\n"); | |
1583 | goto err_free_fuse_devs; | |
1584 | } | |
1dd53957 | 1585 | ctx->dax_dev = fs->dax_dev; |
3f9b9efd | 1586 | } |
1dd53957 | 1587 | err = fuse_fill_super_common(sb, ctx); |
a62a8ef9 SH |
1588 | if (err < 0) |
1589 | goto err_free_fuse_devs; | |
1590 | ||
a62a8ef9 SH |
1591 | for (i = 0; i < fs->nvqs; i++) { |
1592 | struct virtio_fs_vq *fsvq = &fs->vqs[i]; | |
1593 | ||
a62a8ef9 SH |
1594 | fuse_dev_install(fsvq->fud, fc); |
1595 | } | |
1596 | ||
1597 | /* Previous unmount will stop all queues. Start these again */ | |
1598 | virtio_fs_start_all_queues(fs); | |
fcee216b | 1599 | fuse_send_init(fm); |
a62a8ef9 SH |
1600 | mutex_unlock(&virtio_fs_mutex); |
1601 | return 0; | |
1602 | ||
1603 | err_free_fuse_devs: | |
1604 | virtio_fs_free_devs(fs); | |
1605 | err: | |
1606 | mutex_unlock(&virtio_fs_mutex); | |
1607 | return err; | |
1608 | } | |
1609 | ||
fcee216b | 1610 | static void virtio_fs_conn_destroy(struct fuse_mount *fm) |
a62a8ef9 | 1611 | { |
fcee216b MR |
1612 | struct fuse_conn *fc = fm->fc; |
1613 | struct virtio_fs *vfs = fc->iq.priv; | |
1614 | struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO]; | |
a62a8ef9 | 1615 | |
fcee216b MR |
1616 | /* Stop dax worker. Soon evict_inodes() will be called which |
1617 | * will free all memory ranges belonging to all inodes. | |
9a752d18 VG |
1618 | */ |
1619 | if (IS_ENABLED(CONFIG_FUSE_DAX)) | |
1620 | fuse_dax_cancel_work(fc); | |
a62a8ef9 SH |
1621 | |
1622 | /* Stop forget queue. Soon destroy will be sent */ | |
1623 | spin_lock(&fsvq->lock); | |
1624 | fsvq->connected = false; | |
1625 | spin_unlock(&fsvq->lock); | |
1626 | virtio_fs_drain_all_queues(vfs); | |
1627 | ||
fcee216b | 1628 | fuse_conn_destroy(fm); |
a62a8ef9 | 1629 | |
fcee216b | 1630 | /* fuse_conn_destroy() must have sent destroy. Stop all queues |
a62a8ef9 SH |
1631 | * and drain one more time and free fuse devices. Freeing fuse |
1632 | * devices will drop their reference on fuse_conn and that in | |
1633 | * turn will drop its reference on virtio_fs object. | |
1634 | */ | |
1635 | virtio_fs_stop_all_queues(vfs); | |
1636 | virtio_fs_drain_all_queues(vfs); | |
1637 | virtio_fs_free_devs(vfs); | |
1638 | } | |
1639 | ||
fcee216b MR |
1640 | static void virtio_kill_sb(struct super_block *sb) |
1641 | { | |
1642 | struct fuse_mount *fm = get_fuse_mount_super(sb); | |
1643 | bool last; | |
1644 | ||
1645 | /* If mount failed, we can still be called without any fc */ | |
d534d31d | 1646 | if (sb->s_root) { |
fcee216b MR |
1647 | last = fuse_mount_remove(fm); |
1648 | if (last) | |
1649 | virtio_fs_conn_destroy(fm); | |
1650 | } | |
1651 | kill_anon_super(sb); | |
a27c061a | 1652 | fuse_mount_destroy(fm); |
fcee216b MR |
1653 | } |
1654 | ||
a62a8ef9 SH |
1655 | static int virtio_fs_test_super(struct super_block *sb, |
1656 | struct fs_context *fsc) | |
1657 | { | |
fcee216b MR |
1658 | struct fuse_mount *fsc_fm = fsc->s_fs_info; |
1659 | struct fuse_mount *sb_fm = get_fuse_mount_super(sb); | |
a62a8ef9 | 1660 | |
fcee216b | 1661 | return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv; |
a62a8ef9 SH |
1662 | } |
1663 | ||
a62a8ef9 SH |
1664 | static int virtio_fs_get_tree(struct fs_context *fsc) |
1665 | { | |
1666 | struct virtio_fs *fs; | |
1667 | struct super_block *sb; | |
a7f0d7aa | 1668 | struct fuse_conn *fc = NULL; |
fcee216b | 1669 | struct fuse_mount *fm; |
a7f0d7aa CK |
1670 | unsigned int virtqueue_size; |
1671 | int err = -EIO; | |
a62a8ef9 SH |
1672 | |
1673 | /* This gets a reference on virtio_fs object. This ptr gets installed | |
1674 | * in fc->iq->priv. Once fuse_conn is going away, it calls ->put() | |
1675 | * to drop the reference to this object. | |
1676 | */ | |
1677 | fs = virtio_fs_find_instance(fsc->source); | |
1678 | if (!fs) { | |
1679 | pr_info("virtio-fs: tag <%s> not found\n", fsc->source); | |
1680 | return -EINVAL; | |
1681 | } | |
1682 | ||
a7f0d7aa CK |
1683 | virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq); |
1684 | if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD)) | |
1685 | goto out_err; | |
1686 | ||
833c5a42 | 1687 | err = -ENOMEM; |
a62a8ef9 | 1688 | fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL); |
833c5a42 MS |
1689 | if (!fc) |
1690 | goto out_err; | |
a62a8ef9 | 1691 | |
fcee216b | 1692 | fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL); |
833c5a42 MS |
1693 | if (!fm) |
1694 | goto out_err; | |
fcee216b | 1695 | |
0a7419c6 | 1696 | fuse_conn_init(fc, fm, fsc->user_ns, &virtio_fs_fiq_ops, fs); |
a62a8ef9 SH |
1697 | fc->release = fuse_free_conn; |
1698 | fc->delete_stale = true; | |
bf109c64 | 1699 | fc->auto_submounts = true; |
2d82ab25 | 1700 | fc->sync_fs = true; |
41748675 | 1701 | fc->use_pages_for_kvec_io = true; |
a62a8ef9 | 1702 | |
a7f0d7aa CK |
1703 | /* Tell FUSE to split requests that exceed the virtqueue's size */ |
1704 | fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit, | |
1705 | virtqueue_size - FUSE_HEADER_OVERHEAD); | |
1706 | ||
fcee216b | 1707 | fsc->s_fs_info = fm; |
b19d3d00 | 1708 | sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc); |
c191cd07 MS |
1709 | if (fsc->s_fs_info) |
1710 | fuse_mount_destroy(fm); | |
a62a8ef9 SH |
1711 | if (IS_ERR(sb)) |
1712 | return PTR_ERR(sb); | |
1713 | ||
1714 | if (!sb->s_root) { | |
1dd53957 | 1715 | err = virtio_fs_fill_super(sb, fsc); |
a62a8ef9 SH |
1716 | if (err) { |
1717 | deactivate_locked_super(sb); | |
1718 | return err; | |
1719 | } | |
1720 | ||
1721 | sb->s_flags |= SB_ACTIVE; | |
1722 | } | |
1723 | ||
1724 | WARN_ON(fsc->root); | |
1725 | fsc->root = dget(sb->s_root); | |
1726 | return 0; | |
833c5a42 MS |
1727 | |
1728 | out_err: | |
1729 | kfree(fc); | |
833c5a42 | 1730 | virtio_fs_put(fs); |
833c5a42 | 1731 | return err; |
a62a8ef9 SH |
1732 | } |
1733 | ||
1734 | static const struct fs_context_operations virtio_fs_context_ops = { | |
84c21507 | 1735 | .free = virtio_fs_free_fsc, |
1dd53957 | 1736 | .parse_param = virtio_fs_parse_param, |
a62a8ef9 SH |
1737 | .get_tree = virtio_fs_get_tree, |
1738 | }; | |
1739 | ||
1740 | static int virtio_fs_init_fs_context(struct fs_context *fsc) | |
1741 | { | |
1dd53957 VG |
1742 | struct fuse_fs_context *ctx; |
1743 | ||
fe0a7bd8 GK |
1744 | if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT) |
1745 | return fuse_init_fs_context_submount(fsc); | |
1746 | ||
1dd53957 VG |
1747 | ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL); |
1748 | if (!ctx) | |
1749 | return -ENOMEM; | |
1750 | fsc->fs_private = ctx; | |
a62a8ef9 SH |
1751 | fsc->ops = &virtio_fs_context_ops; |
1752 | return 0; | |
1753 | } | |
1754 | ||
1755 | static struct file_system_type virtio_fs_type = { | |
1756 | .owner = THIS_MODULE, | |
1757 | .name = "virtiofs", | |
1758 | .init_fs_context = virtio_fs_init_fs_context, | |
1759 | .kill_sb = virtio_kill_sb, | |
862b9a8e | 1760 | .fs_flags = FS_ALLOW_IDMAP, |
a62a8ef9 SH |
1761 | }; |
1762 | ||
9086b2d9 SH |
1763 | static int virtio_fs_uevent(const struct kobject *kobj, struct kobj_uevent_env *env) |
1764 | { | |
1765 | const struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj); | |
1766 | ||
1767 | add_uevent_var(env, "TAG=%s", fs->tag); | |
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | static const struct kset_uevent_ops virtio_fs_uevent_ops = { | |
1772 | .uevent = virtio_fs_uevent, | |
1773 | }; | |
1774 | ||
a8f62f50 SH |
1775 | static int __init virtio_fs_sysfs_init(void) |
1776 | { | |
9086b2d9 SH |
1777 | virtio_fs_kset = kset_create_and_add("virtiofs", &virtio_fs_uevent_ops, |
1778 | fs_kobj); | |
a8f62f50 SH |
1779 | if (!virtio_fs_kset) |
1780 | return -ENOMEM; | |
1781 | return 0; | |
1782 | } | |
1783 | ||
d30ff898 | 1784 | static void virtio_fs_sysfs_exit(void) |
a8f62f50 SH |
1785 | { |
1786 | kset_unregister(virtio_fs_kset); | |
1787 | virtio_fs_kset = NULL; | |
1788 | } | |
1789 | ||
a62a8ef9 SH |
1790 | static int __init virtio_fs_init(void) |
1791 | { | |
1792 | int ret; | |
1793 | ||
a8f62f50 | 1794 | ret = virtio_fs_sysfs_init(); |
a62a8ef9 SH |
1795 | if (ret < 0) |
1796 | return ret; | |
1797 | ||
a8f62f50 SH |
1798 | ret = register_virtio_driver(&virtio_fs_driver); |
1799 | if (ret < 0) | |
1800 | goto sysfs_exit; | |
1801 | ||
a62a8ef9 | 1802 | ret = register_filesystem(&virtio_fs_type); |
a8f62f50 SH |
1803 | if (ret < 0) |
1804 | goto unregister_virtio_driver; | |
a62a8ef9 SH |
1805 | |
1806 | return 0; | |
a8f62f50 SH |
1807 | |
1808 | unregister_virtio_driver: | |
1809 | unregister_virtio_driver(&virtio_fs_driver); | |
1810 | sysfs_exit: | |
1811 | virtio_fs_sysfs_exit(); | |
1812 | return ret; | |
a62a8ef9 SH |
1813 | } |
1814 | module_init(virtio_fs_init); | |
1815 | ||
1816 | static void __exit virtio_fs_exit(void) | |
1817 | { | |
1818 | unregister_filesystem(&virtio_fs_type); | |
1819 | unregister_virtio_driver(&virtio_fs_driver); | |
a8f62f50 | 1820 | virtio_fs_sysfs_exit(); |
a62a8ef9 SH |
1821 | } |
1822 | module_exit(virtio_fs_exit); | |
1823 | ||
1824 | MODULE_AUTHOR("Stefan Hajnoczi <[email protected]>"); | |
1825 | MODULE_DESCRIPTION("Virtio Filesystem"); | |
1826 | MODULE_LICENSE("GPL"); | |
1827 | MODULE_ALIAS_FS(KBUILD_MODNAME); | |
1828 | MODULE_DEVICE_TABLE(virtio, id_table); |