blob: d1bc96ee6eb3d89b37f118673912d5ea34ce2e0e [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/sched/signal.h>
16#include <linux/module.h>
17#include <linux/compat.h>
18#include <linux/swap.h>
19#include <linux/falloc.h>
20#include <linux/uio.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020021#include <linux/fs.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
David Brazdil0f672f62019-12-10 10:32:29 +000023static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
24 struct fuse_page_desc **desc)
25{
26 struct page **pages;
27
28 pages = kzalloc(npages * (sizeof(struct page *) +
29 sizeof(struct fuse_page_desc)), flags);
30 *desc = (void *) (pages + npages);
31
32 return pages;
33}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
Olivier Deprez157378f2022-04-04 15:47:50 +020035static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 int opcode, struct fuse_open_out *outargp)
37{
38 struct fuse_open_in inarg;
39 FUSE_ARGS(args);
40
41 memset(&inarg, 0, sizeof(inarg));
42 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
Olivier Deprez157378f2022-04-04 15:47:50 +020043 if (!fm->fc->atomic_o_trunc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 inarg.flags &= ~O_TRUNC;
David Brazdil0f672f62019-12-10 10:32:29 +000045 args.opcode = opcode;
46 args.nodeid = nodeid;
47 args.in_numargs = 1;
48 args.in_args[0].size = sizeof(inarg);
49 args.in_args[0].value = &inarg;
50 args.out_numargs = 1;
51 args.out_args[0].size = sizeof(*outargp);
52 args.out_args[0].value = outargp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053
Olivier Deprez157378f2022-04-04 15:47:50 +020054 return fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055}
56
David Brazdil0f672f62019-12-10 10:32:29 +000057struct fuse_release_args {
58 struct fuse_args args;
59 struct fuse_release_in inarg;
60 struct inode *inode;
61};
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064{
65 struct fuse_file *ff;
66
David Brazdil0f672f62019-12-10 10:32:29 +000067 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 if (unlikely(!ff))
69 return NULL;
70
Olivier Deprez157378f2022-04-04 15:47:50 +020071 ff->fm = fm;
David Brazdil0f672f62019-12-10 10:32:29 +000072 ff->release_args = kzalloc(sizeof(*ff->release_args),
73 GFP_KERNEL_ACCOUNT);
74 if (!ff->release_args) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 kfree(ff);
76 return NULL;
77 }
78
79 INIT_LIST_HEAD(&ff->write_entry);
David Brazdil0f672f62019-12-10 10:32:29 +000080 mutex_init(&ff->readdir.lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 refcount_set(&ff->count, 1);
82 RB_CLEAR_NODE(&ff->polled_node);
83 init_waitqueue_head(&ff->poll_wait);
84
Olivier Deprez157378f2022-04-04 15:47:50 +020085 ff->kh = atomic64_inc_return(&fm->fc->khctr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086
87 return ff;
88}
89
90void fuse_file_free(struct fuse_file *ff)
91{
David Brazdil0f672f62019-12-10 10:32:29 +000092 kfree(ff->release_args);
93 mutex_destroy(&ff->readdir.lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 kfree(ff);
95}
96
97static struct fuse_file *fuse_file_get(struct fuse_file *ff)
98{
99 refcount_inc(&ff->count);
100 return ff;
101}
102
Olivier Deprez157378f2022-04-04 15:47:50 +0200103static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
David Brazdil0f672f62019-12-10 10:32:29 +0000104 int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105{
David Brazdil0f672f62019-12-10 10:32:29 +0000106 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
107
108 iput(ra->inode);
109 kfree(ra);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110}
111
112static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
113{
114 if (refcount_dec_and_test(&ff->count)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000115 struct fuse_args *args = &ff->release_args->args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
David Brazdil0f672f62019-12-10 10:32:29 +0000118 /* Do nothing when client does not implement 'open' */
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 fuse_release_end(ff->fm, args, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 } else if (sync) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200121 fuse_simple_request(ff->fm, args);
122 fuse_release_end(ff->fm, args, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000124 args->end = fuse_release_end;
Olivier Deprez157378f2022-04-04 15:47:50 +0200125 if (fuse_simple_background(ff->fm, args,
David Brazdil0f672f62019-12-10 10:32:29 +0000126 GFP_KERNEL | __GFP_NOFAIL))
Olivier Deprez157378f2022-04-04 15:47:50 +0200127 fuse_release_end(ff->fm, args, -ENOTCONN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 }
129 kfree(ff);
130 }
131}
132
Olivier Deprez157378f2022-04-04 15:47:50 +0200133int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134 bool isdir)
135{
Olivier Deprez157378f2022-04-04 15:47:50 +0200136 struct fuse_conn *fc = fm->fc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137 struct fuse_file *ff;
138 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
139
Olivier Deprez157378f2022-04-04 15:47:50 +0200140 ff = fuse_file_alloc(fm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 if (!ff)
142 return -ENOMEM;
143
144 ff->fh = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000145 /* Default for no-open */
146 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
147 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 struct fuse_open_out outarg;
149 int err;
150
Olivier Deprez157378f2022-04-04 15:47:50 +0200151 err = fuse_send_open(fm, nodeid, file, opcode, &outarg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 if (!err) {
153 ff->fh = outarg.fh;
154 ff->open_flags = outarg.open_flags;
155
David Brazdil0f672f62019-12-10 10:32:29 +0000156 } else if (err != -ENOSYS) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157 fuse_file_free(ff);
158 return err;
159 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000160 if (isdir)
161 fc->no_opendir = 1;
162 else
163 fc->no_open = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164 }
165 }
166
167 if (isdir)
168 ff->open_flags &= ~FOPEN_DIRECT_IO;
169
170 ff->nodeid = nodeid;
171 file->private_data = ff;
172
173 return 0;
174}
175EXPORT_SYMBOL_GPL(fuse_do_open);
176
177static void fuse_link_write_file(struct file *file)
178{
179 struct inode *inode = file_inode(file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180 struct fuse_inode *fi = get_fuse_inode(inode);
181 struct fuse_file *ff = file->private_data;
182 /*
183 * file may be written through mmap, so chain it onto the
184 * inodes's write_file list
185 */
David Brazdil0f672f62019-12-10 10:32:29 +0000186 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000187 if (list_empty(&ff->write_entry))
188 list_add(&ff->write_entry, &fi->write_files);
David Brazdil0f672f62019-12-10 10:32:29 +0000189 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190}
191
192void fuse_finish_open(struct inode *inode, struct file *file)
193{
194 struct fuse_file *ff = file->private_data;
195 struct fuse_conn *fc = get_fuse_conn(inode);
196
David Brazdil0f672f62019-12-10 10:32:29 +0000197 if (ff->open_flags & FOPEN_STREAM)
198 stream_open(inode, file);
199 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 nonseekable_open(inode, file);
Olivier Deprez0e641232021-09-23 10:07:05 +0200201
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
203 struct fuse_inode *fi = get_fuse_inode(inode);
204
David Brazdil0f672f62019-12-10 10:32:29 +0000205 spin_lock(&fi->lock);
206 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207 i_size_write(inode, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000208 spin_unlock(&fi->lock);
Olivier Deprez0e641232021-09-23 10:07:05 +0200209 truncate_pagecache(inode, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 fuse_invalidate_attr(inode);
211 if (fc->writeback_cache)
212 file_update_time(file);
Olivier Deprez0e641232021-09-23 10:07:05 +0200213 } else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) {
214 invalidate_inode_pages2(inode->i_mapping);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200216
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
218 fuse_link_write_file(file);
219}
220
221int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
222{
Olivier Deprez157378f2022-04-04 15:47:50 +0200223 struct fuse_mount *fm = get_fuse_mount(inode);
224 struct fuse_conn *fc = fm->fc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 int err;
David Brazdil0f672f62019-12-10 10:32:29 +0000226 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 fc->atomic_o_trunc &&
228 fc->writeback_cache;
Olivier Deprez157378f2022-04-04 15:47:50 +0200229 bool dax_truncate = (file->f_flags & O_TRUNC) &&
230 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231
Olivier Deprez0e641232021-09-23 10:07:05 +0200232 if (fuse_is_bad(inode))
233 return -EIO;
234
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235 err = generic_file_open(inode, file);
236 if (err)
237 return err;
238
Olivier Deprez157378f2022-04-04 15:47:50 +0200239 if (is_wb_truncate || dax_truncate) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000240 inode_lock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000241 fuse_set_nowrite(inode);
242 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243
Olivier Deprez157378f2022-04-04 15:47:50 +0200244 if (dax_truncate) {
245 down_write(&get_fuse_inode(inode)->i_mmap_sem);
246 err = fuse_dax_break_layouts(inode, 0, 0);
247 if (err)
248 goto out;
249 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250
Olivier Deprez157378f2022-04-04 15:47:50 +0200251 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252 if (!err)
253 fuse_finish_open(inode, file);
254
Olivier Deprez157378f2022-04-04 15:47:50 +0200255out:
256 if (dax_truncate)
257 up_write(&get_fuse_inode(inode)->i_mmap_sem);
258
259 if (is_wb_truncate | dax_truncate) {
David Brazdil0f672f62019-12-10 10:32:29 +0000260 fuse_release_nowrite(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261 inode_unlock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000262 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263
264 return err;
265}
266
David Brazdil0f672f62019-12-10 10:32:29 +0000267static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
268 int flags, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269{
Olivier Deprez157378f2022-04-04 15:47:50 +0200270 struct fuse_conn *fc = ff->fm->fc;
David Brazdil0f672f62019-12-10 10:32:29 +0000271 struct fuse_release_args *ra = ff->release_args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000272
David Brazdil0f672f62019-12-10 10:32:29 +0000273 /* Inode is NULL on error path of fuse_create_open() */
274 if (likely(fi)) {
275 spin_lock(&fi->lock);
276 list_del(&ff->write_entry);
277 spin_unlock(&fi->lock);
278 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 spin_lock(&fc->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280 if (!RB_EMPTY_NODE(&ff->polled_node))
281 rb_erase(&ff->polled_node, &fc->polled_files);
282 spin_unlock(&fc->lock);
283
284 wake_up_interruptible_all(&ff->poll_wait);
285
David Brazdil0f672f62019-12-10 10:32:29 +0000286 ra->inarg.fh = ff->fh;
287 ra->inarg.flags = flags;
288 ra->args.in_numargs = 1;
289 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
290 ra->args.in_args[0].value = &ra->inarg;
291 ra->args.opcode = opcode;
292 ra->args.nodeid = ff->nodeid;
293 ra->args.force = true;
294 ra->args.nocreds = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295}
296
297void fuse_release_common(struct file *file, bool isdir)
298{
David Brazdil0f672f62019-12-10 10:32:29 +0000299 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000300 struct fuse_file *ff = file->private_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000301 struct fuse_release_args *ra = ff->release_args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
303
David Brazdil0f672f62019-12-10 10:32:29 +0000304 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305
306 if (ff->flock) {
David Brazdil0f672f62019-12-10 10:32:29 +0000307 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
Olivier Deprez157378f2022-04-04 15:47:50 +0200308 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc,
David Brazdil0f672f62019-12-10 10:32:29 +0000309 (fl_owner_t) file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310 }
311 /* Hold inode until release is finished */
David Brazdil0f672f62019-12-10 10:32:29 +0000312 ra->inode = igrab(file_inode(file));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000313
314 /*
315 * Normally this will send the RELEASE request, however if
316 * some asynchronous READ or WRITE requests are outstanding,
317 * the sending will be delayed.
318 *
319 * Make the release synchronous if this is a fuseblk mount,
320 * synchronous RELEASE is allowed (and desirable) in this case
321 * because the server can be trusted not to screw up.
322 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200323 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324}
325
326static int fuse_open(struct inode *inode, struct file *file)
327{
328 return fuse_open_common(inode, file, false);
329}
330
331static int fuse_release(struct inode *inode, struct file *file)
332{
333 struct fuse_conn *fc = get_fuse_conn(inode);
334
335 /* see fuse_vma_close() for !writeback_cache case */
336 if (fc->writeback_cache)
337 write_inode_now(inode, 1);
338
339 fuse_release_common(file, false);
340
341 /* return value is ignored by VFS */
342 return 0;
343}
344
David Brazdil0f672f62019-12-10 10:32:29 +0000345void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000346{
347 WARN_ON(refcount_read(&ff->count) > 1);
David Brazdil0f672f62019-12-10 10:32:29 +0000348 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 /*
350 * iput(NULL) is a no-op and since the refcount is 1 and everything's
351 * synchronous, we are fine with not doing igrab() here"
352 */
353 fuse_file_put(ff, true, false);
354}
355EXPORT_SYMBOL_GPL(fuse_sync_release);
356
357/*
358 * Scramble the ID space with XTEA, so that the value of the files_struct
359 * pointer is not exposed to userspace.
360 */
361u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
362{
363 u32 *k = fc->scramble_key;
364 u64 v = (unsigned long) id;
365 u32 v0 = v;
366 u32 v1 = v >> 32;
367 u32 sum = 0;
368 int i;
369
370 for (i = 0; i < 32; i++) {
371 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
372 sum += 0x9E3779B9;
373 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
374 }
375
376 return (u64) v0 + ((u64) v1 << 32);
377}
378
David Brazdil0f672f62019-12-10 10:32:29 +0000379struct fuse_writepage_args {
380 struct fuse_io_args ia;
Olivier Deprez157378f2022-04-04 15:47:50 +0200381 struct rb_node writepages_entry;
David Brazdil0f672f62019-12-10 10:32:29 +0000382 struct list_head queue_entry;
383 struct fuse_writepage_args *next;
384 struct inode *inode;
385};
386
387static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
388 pgoff_t idx_from, pgoff_t idx_to)
389{
Olivier Deprez157378f2022-04-04 15:47:50 +0200390 struct rb_node *n;
David Brazdil0f672f62019-12-10 10:32:29 +0000391
Olivier Deprez157378f2022-04-04 15:47:50 +0200392 n = fi->writepages.rb_node;
393
394 while (n) {
395 struct fuse_writepage_args *wpa;
David Brazdil0f672f62019-12-10 10:32:29 +0000396 pgoff_t curr_index;
397
Olivier Deprez157378f2022-04-04 15:47:50 +0200398 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
David Brazdil0f672f62019-12-10 10:32:29 +0000399 WARN_ON(get_fuse_inode(wpa->inode) != fi);
400 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
Olivier Deprez157378f2022-04-04 15:47:50 +0200401 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
402 n = n->rb_right;
403 else if (idx_to < curr_index)
404 n = n->rb_left;
405 else
David Brazdil0f672f62019-12-10 10:32:29 +0000406 return wpa;
David Brazdil0f672f62019-12-10 10:32:29 +0000407 }
408 return NULL;
409}
410
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411/*
412 * Check if any page in a range is under writeback
413 *
414 * This is currently done by walking the list of writepage requests
415 * for the inode, which can be pretty inefficient.
416 */
417static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
418 pgoff_t idx_to)
419{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000421 bool found;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422
David Brazdil0f672f62019-12-10 10:32:29 +0000423 spin_lock(&fi->lock);
424 found = fuse_find_writeback(fi, idx_from, idx_to);
425 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426
427 return found;
428}
429
430static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
431{
432 return fuse_range_is_writeback(inode, index, index);
433}
434
435/*
436 * Wait for page writeback to be completed.
437 *
438 * Since fuse doesn't rely on the VM writeback tracking, this has to
439 * use some other means.
440 */
David Brazdil0f672f62019-12-10 10:32:29 +0000441static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442{
443 struct fuse_inode *fi = get_fuse_inode(inode);
444
445 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446}
447
448/*
449 * Wait for all pending writepages on the inode to finish.
450 *
451 * This is currently done by blocking further writes with FUSE_NOWRITE
452 * and waiting for all sent writes to complete.
453 *
454 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
455 * could conflict with truncation.
456 */
457static void fuse_sync_writes(struct inode *inode)
458{
459 fuse_set_nowrite(inode);
460 fuse_release_nowrite(inode);
461}
462
463static int fuse_flush(struct file *file, fl_owner_t id)
464{
465 struct inode *inode = file_inode(file);
Olivier Deprez157378f2022-04-04 15:47:50 +0200466 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467 struct fuse_file *ff = file->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000468 struct fuse_flush_in inarg;
David Brazdil0f672f62019-12-10 10:32:29 +0000469 FUSE_ARGS(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470 int err;
471
Olivier Deprez0e641232021-09-23 10:07:05 +0200472 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 return -EIO;
474
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000475 err = write_inode_now(inode, 1);
476 if (err)
477 return err;
478
479 inode_lock(inode);
480 fuse_sync_writes(inode);
481 inode_unlock(inode);
482
483 err = filemap_check_errors(file->f_mapping);
484 if (err)
485 return err;
486
Olivier Deprez157378f2022-04-04 15:47:50 +0200487 err = 0;
488 if (fm->fc->no_flush)
489 goto inval_attr_out;
490
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 memset(&inarg, 0, sizeof(inarg));
492 inarg.fh = ff->fh;
Olivier Deprez157378f2022-04-04 15:47:50 +0200493 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
David Brazdil0f672f62019-12-10 10:32:29 +0000494 args.opcode = FUSE_FLUSH;
495 args.nodeid = get_node_id(inode);
496 args.in_numargs = 1;
497 args.in_args[0].size = sizeof(inarg);
498 args.in_args[0].value = &inarg;
499 args.force = true;
500
Olivier Deprez157378f2022-04-04 15:47:50 +0200501 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 if (err == -ENOSYS) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200503 fm->fc->no_flush = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000504 err = 0;
505 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200506
507inval_attr_out:
508 /*
509 * In memory i_blocks is not maintained by fuse, if writeback cache is
510 * enabled, i_blocks from cached attr may not be accurate.
511 */
512 if (!err && fm->fc->writeback_cache)
513 fuse_invalidate_attr(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514 return err;
515}
516
517int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
David Brazdil0f672f62019-12-10 10:32:29 +0000518 int datasync, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000519{
520 struct inode *inode = file->f_mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +0200521 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522 struct fuse_file *ff = file->private_data;
523 FUSE_ARGS(args);
524 struct fuse_fsync_in inarg;
David Brazdil0f672f62019-12-10 10:32:29 +0000525
526 memset(&inarg, 0, sizeof(inarg));
527 inarg.fh = ff->fh;
528 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
529 args.opcode = opcode;
530 args.nodeid = get_node_id(inode);
531 args.in_numargs = 1;
532 args.in_args[0].size = sizeof(inarg);
533 args.in_args[0].value = &inarg;
Olivier Deprez157378f2022-04-04 15:47:50 +0200534 return fuse_simple_request(fm, &args);
David Brazdil0f672f62019-12-10 10:32:29 +0000535}
536
537static int fuse_fsync(struct file *file, loff_t start, loff_t end,
538 int datasync)
539{
540 struct inode *inode = file->f_mapping->host;
541 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542 int err;
543
Olivier Deprez0e641232021-09-23 10:07:05 +0200544 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545 return -EIO;
546
547 inode_lock(inode);
548
549 /*
550 * Start writeback against all dirty pages of the inode, then
551 * wait for all outstanding writes, before sending the FSYNC
552 * request.
553 */
554 err = file_write_and_wait_range(file, start, end);
555 if (err)
556 goto out;
557
558 fuse_sync_writes(inode);
559
560 /*
561 * Due to implementation of fuse writeback
562 * file_write_and_wait_range() does not catch errors.
563 * We have to do this directly after fuse_sync_writes()
564 */
565 err = file_check_and_advance_wb_err(file);
566 if (err)
567 goto out;
568
569 err = sync_inode_metadata(inode, 1);
570 if (err)
571 goto out;
572
David Brazdil0f672f62019-12-10 10:32:29 +0000573 if (fc->no_fsync)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000574 goto out;
575
David Brazdil0f672f62019-12-10 10:32:29 +0000576 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000577 if (err == -ENOSYS) {
David Brazdil0f672f62019-12-10 10:32:29 +0000578 fc->no_fsync = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 err = 0;
580 }
581out:
582 inode_unlock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000583
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 return err;
585}
586
David Brazdil0f672f62019-12-10 10:32:29 +0000587void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
588 size_t count, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590 struct fuse_file *ff = file->private_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000591 struct fuse_args *args = &ia->ap.args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592
David Brazdil0f672f62019-12-10 10:32:29 +0000593 ia->read.in.fh = ff->fh;
594 ia->read.in.offset = pos;
595 ia->read.in.size = count;
596 ia->read.in.flags = file->f_flags;
597 args->opcode = opcode;
598 args->nodeid = ff->nodeid;
599 args->in_numargs = 1;
600 args->in_args[0].size = sizeof(ia->read.in);
601 args->in_args[0].value = &ia->read.in;
602 args->out_argvar = true;
603 args->out_numargs = 1;
604 args->out_args[0].size = count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605}
606
David Brazdil0f672f62019-12-10 10:32:29 +0000607static void fuse_release_user_pages(struct fuse_args_pages *ap,
608 bool should_dirty)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609{
David Brazdil0f672f62019-12-10 10:32:29 +0000610 unsigned int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000611
David Brazdil0f672f62019-12-10 10:32:29 +0000612 for (i = 0; i < ap->num_pages; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613 if (should_dirty)
David Brazdil0f672f62019-12-10 10:32:29 +0000614 set_page_dirty_lock(ap->pages[i]);
615 put_page(ap->pages[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000616 }
617}
618
619static void fuse_io_release(struct kref *kref)
620{
621 kfree(container_of(kref, struct fuse_io_priv, refcnt));
622}
623
624static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
625{
626 if (io->err)
627 return io->err;
628
629 if (io->bytes >= 0 && io->write)
630 return -EIO;
631
632 return io->bytes < 0 ? io->size : io->bytes;
633}
634
635/**
636 * In case of short read, the caller sets 'pos' to the position of
637 * actual end of fuse request in IO request. Otherwise, if bytes_requested
638 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
639 *
640 * An example:
641 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
642 * both submitted asynchronously. The first of them was ACKed by userspace as
643 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
644 * second request was ACKed as short, e.g. only 1K was read, resulting in
645 * pos == 33K.
646 *
647 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
648 * will be equal to the length of the longest contiguous fragment of
649 * transferred data starting from the beginning of IO request.
650 */
651static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
652{
653 int left;
654
655 spin_lock(&io->lock);
656 if (err)
657 io->err = io->err ? : err;
658 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
659 io->bytes = pos;
660
661 left = --io->reqs;
662 if (!left && io->blocking)
663 complete(io->done);
664 spin_unlock(&io->lock);
665
666 if (!left && !io->blocking) {
667 ssize_t res = fuse_get_res_by_io(io);
668
669 if (res >= 0) {
670 struct inode *inode = file_inode(io->iocb->ki_filp);
671 struct fuse_conn *fc = get_fuse_conn(inode);
672 struct fuse_inode *fi = get_fuse_inode(inode);
673
David Brazdil0f672f62019-12-10 10:32:29 +0000674 spin_lock(&fi->lock);
675 fi->attr_version = atomic64_inc_return(&fc->attr_version);
676 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677 }
678
679 io->iocb->ki_complete(io->iocb, res, 0);
680 }
681
682 kref_put(&io->refcnt, fuse_io_release);
683}
684
David Brazdil0f672f62019-12-10 10:32:29 +0000685static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
686 unsigned int npages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687{
David Brazdil0f672f62019-12-10 10:32:29 +0000688 struct fuse_io_args *ia;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000689
David Brazdil0f672f62019-12-10 10:32:29 +0000690 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
691 if (ia) {
692 ia->io = io;
693 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
694 &ia->ap.descs);
695 if (!ia->ap.pages) {
696 kfree(ia);
697 ia = NULL;
698 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699 }
David Brazdil0f672f62019-12-10 10:32:29 +0000700 return ia;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701}
702
David Brazdil0f672f62019-12-10 10:32:29 +0000703static void fuse_io_free(struct fuse_io_args *ia)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000704{
David Brazdil0f672f62019-12-10 10:32:29 +0000705 kfree(ia->ap.pages);
706 kfree(ia);
707}
708
Olivier Deprez157378f2022-04-04 15:47:50 +0200709static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
David Brazdil0f672f62019-12-10 10:32:29 +0000710 int err)
711{
712 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
713 struct fuse_io_priv *io = ia->io;
714 ssize_t pos = -1;
715
716 fuse_release_user_pages(&ia->ap, io->should_dirty);
717
718 if (err) {
719 /* Nothing */
720 } else if (io->write) {
721 if (ia->write.out.size > ia->write.in.size) {
722 err = -EIO;
723 } else if (ia->write.in.size != ia->write.out.size) {
724 pos = ia->write.in.offset - io->offset +
725 ia->write.out.size;
726 }
727 } else {
728 u32 outsize = args->out_args[0].size;
729
730 if (ia->read.in.size != outsize)
731 pos = ia->read.in.offset - io->offset + outsize;
732 }
733
734 fuse_aio_complete(io, err, pos);
735 fuse_io_free(ia);
736}
737
Olivier Deprez157378f2022-04-04 15:47:50 +0200738static ssize_t fuse_async_req_send(struct fuse_mount *fm,
David Brazdil0f672f62019-12-10 10:32:29 +0000739 struct fuse_io_args *ia, size_t num_bytes)
740{
741 ssize_t err;
742 struct fuse_io_priv *io = ia->io;
743
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000744 spin_lock(&io->lock);
745 kref_get(&io->refcnt);
746 io->size += num_bytes;
747 io->reqs++;
748 spin_unlock(&io->lock);
749
David Brazdil0f672f62019-12-10 10:32:29 +0000750 ia->ap.args.end = fuse_aio_complete_req;
Olivier Deprez0e641232021-09-23 10:07:05 +0200751 ia->ap.args.may_block = io->should_dirty;
Olivier Deprez157378f2022-04-04 15:47:50 +0200752 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
Olivier Deprez0e641232021-09-23 10:07:05 +0200753 if (err)
Olivier Deprez157378f2022-04-04 15:47:50 +0200754 fuse_aio_complete_req(fm, &ia->ap.args, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000755
Olivier Deprez0e641232021-09-23 10:07:05 +0200756 return num_bytes;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757}
758
David Brazdil0f672f62019-12-10 10:32:29 +0000759static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
760 fl_owner_t owner)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000761{
David Brazdil0f672f62019-12-10 10:32:29 +0000762 struct file *file = ia->io->iocb->ki_filp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000763 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +0200764 struct fuse_mount *fm = ff->fm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000765
David Brazdil0f672f62019-12-10 10:32:29 +0000766 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767 if (owner != NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000768 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
Olivier Deprez157378f2022-04-04 15:47:50 +0200769 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770 }
771
David Brazdil0f672f62019-12-10 10:32:29 +0000772 if (ia->io->async)
Olivier Deprez157378f2022-04-04 15:47:50 +0200773 return fuse_async_req_send(fm, ia, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000774
Olivier Deprez157378f2022-04-04 15:47:50 +0200775 return fuse_simple_request(fm, &ia->ap.args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776}
777
778static void fuse_read_update_size(struct inode *inode, loff_t size,
779 u64 attr_ver)
780{
781 struct fuse_conn *fc = get_fuse_conn(inode);
782 struct fuse_inode *fi = get_fuse_inode(inode);
783
David Brazdil0f672f62019-12-10 10:32:29 +0000784 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785 if (attr_ver == fi->attr_version && size < inode->i_size &&
786 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000787 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000788 i_size_write(inode, size);
789 }
David Brazdil0f672f62019-12-10 10:32:29 +0000790 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791}
792
David Brazdil0f672f62019-12-10 10:32:29 +0000793static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
794 struct fuse_args_pages *ap)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796 struct fuse_conn *fc = get_fuse_conn(inode);
797
798 if (fc->writeback_cache) {
799 /*
800 * A hole in a file. Some data after the hole are in page cache,
801 * but have not reached the client fs yet. So, the hole is not
802 * present there.
803 */
804 int i;
805 int start_idx = num_read >> PAGE_SHIFT;
806 size_t off = num_read & (PAGE_SIZE - 1);
807
David Brazdil0f672f62019-12-10 10:32:29 +0000808 for (i = start_idx; i < ap->num_pages; i++) {
809 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810 off = 0;
811 }
812 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000813 loff_t pos = page_offset(ap->pages[0]) + num_read;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000814 fuse_read_update_size(inode, pos, attr_ver);
815 }
816}
817
818static int fuse_do_readpage(struct file *file, struct page *page)
819{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 struct inode *inode = page->mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +0200821 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000822 loff_t pos = page_offset(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000823 struct fuse_page_desc desc = { .length = PAGE_SIZE };
824 struct fuse_io_args ia = {
825 .ap.args.page_zeroing = true,
826 .ap.args.out_pages = true,
827 .ap.num_pages = 1,
828 .ap.pages = &page,
829 .ap.descs = &desc,
830 };
831 ssize_t res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000832 u64 attr_ver;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000833
834 /*
835 * Page writeback can extend beyond the lifetime of the
836 * page-cache page, so make sure we read a properly synced
837 * page.
838 */
839 fuse_wait_on_page_writeback(inode, page->index);
840
Olivier Deprez157378f2022-04-04 15:47:50 +0200841 attr_ver = fuse_get_attr_version(fm->fc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000842
Olivier Deprez0e641232021-09-23 10:07:05 +0200843 /* Don't overflow end offset */
844 if (pos + (desc.length - 1) == LLONG_MAX)
845 desc.length--;
846
David Brazdil0f672f62019-12-10 10:32:29 +0000847 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
Olivier Deprez157378f2022-04-04 15:47:50 +0200848 res = fuse_simple_request(fm, &ia.ap.args);
David Brazdil0f672f62019-12-10 10:32:29 +0000849 if (res < 0)
850 return res;
851 /*
852 * Short read means EOF. If file size is larger, truncate it
853 */
854 if (res < desc.length)
855 fuse_short_read(inode, attr_ver, res, &ia.ap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856
David Brazdil0f672f62019-12-10 10:32:29 +0000857 SetPageUptodate(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000858
David Brazdil0f672f62019-12-10 10:32:29 +0000859 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000860}
861
862static int fuse_readpage(struct file *file, struct page *page)
863{
864 struct inode *inode = page->mapping->host;
865 int err;
866
867 err = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +0200868 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869 goto out;
870
871 err = fuse_do_readpage(file, page);
872 fuse_invalidate_atime(inode);
873 out:
874 unlock_page(page);
875 return err;
876}
877
Olivier Deprez157378f2022-04-04 15:47:50 +0200878static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
David Brazdil0f672f62019-12-10 10:32:29 +0000879 int err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000880{
881 int i;
David Brazdil0f672f62019-12-10 10:32:29 +0000882 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
883 struct fuse_args_pages *ap = &ia->ap;
884 size_t count = ia->read.in.size;
885 size_t num_read = args->out_args[0].size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000886 struct address_space *mapping = NULL;
887
David Brazdil0f672f62019-12-10 10:32:29 +0000888 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
889 mapping = ap->pages[i]->mapping;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000890
891 if (mapping) {
892 struct inode *inode = mapping->host;
893
894 /*
895 * Short read means EOF. If file size is larger, truncate it
896 */
David Brazdil0f672f62019-12-10 10:32:29 +0000897 if (!err && num_read < count)
898 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899
900 fuse_invalidate_atime(inode);
901 }
902
David Brazdil0f672f62019-12-10 10:32:29 +0000903 for (i = 0; i < ap->num_pages; i++) {
904 struct page *page = ap->pages[i];
905
906 if (!err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000907 SetPageUptodate(page);
908 else
909 SetPageError(page);
910 unlock_page(page);
911 put_page(page);
912 }
David Brazdil0f672f62019-12-10 10:32:29 +0000913 if (ia->ff)
914 fuse_file_put(ia->ff, false, false);
915
916 fuse_io_free(ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000917}
918
David Brazdil0f672f62019-12-10 10:32:29 +0000919static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000920{
921 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +0200922 struct fuse_mount *fm = ff->fm;
David Brazdil0f672f62019-12-10 10:32:29 +0000923 struct fuse_args_pages *ap = &ia->ap;
924 loff_t pos = page_offset(ap->pages[0]);
925 size_t count = ap->num_pages << PAGE_SHIFT;
Olivier Deprez0e641232021-09-23 10:07:05 +0200926 ssize_t res;
David Brazdil0f672f62019-12-10 10:32:29 +0000927 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928
David Brazdil0f672f62019-12-10 10:32:29 +0000929 ap->args.out_pages = true;
930 ap->args.page_zeroing = true;
931 ap->args.page_replace = true;
Olivier Deprez0e641232021-09-23 10:07:05 +0200932
933 /* Don't overflow end offset */
934 if (pos + (count - 1) == LLONG_MAX) {
935 count--;
936 ap->descs[ap->num_pages - 1].length--;
937 }
938 WARN_ON((loff_t) (pos + count) < 0);
939
David Brazdil0f672f62019-12-10 10:32:29 +0000940 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Olivier Deprez157378f2022-04-04 15:47:50 +0200941 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
942 if (fm->fc->async_read) {
David Brazdil0f672f62019-12-10 10:32:29 +0000943 ia->ff = fuse_file_get(ff);
944 ap->args.end = fuse_readpages_end;
Olivier Deprez157378f2022-04-04 15:47:50 +0200945 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +0000946 if (!err)
947 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +0200949 res = fuse_simple_request(fm, &ap->args);
Olivier Deprez0e641232021-09-23 10:07:05 +0200950 err = res < 0 ? res : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200952 fuse_readpages_end(fm, &ap->args, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953}
954
Olivier Deprez157378f2022-04-04 15:47:50 +0200955static void fuse_readahead(struct readahead_control *rac)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956{
Olivier Deprez157378f2022-04-04 15:47:50 +0200957 struct inode *inode = rac->mapping->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958 struct fuse_conn *fc = get_fuse_conn(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +0200959 unsigned int i, max_pages, nr_pages = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960
Olivier Deprez0e641232021-09-23 10:07:05 +0200961 if (fuse_is_bad(inode))
Olivier Deprez157378f2022-04-04 15:47:50 +0200962 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963
Olivier Deprez157378f2022-04-04 15:47:50 +0200964 max_pages = min_t(unsigned int, fc->max_pages,
965 fc->max_read / PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966
Olivier Deprez157378f2022-04-04 15:47:50 +0200967 for (;;) {
968 struct fuse_io_args *ia;
969 struct fuse_args_pages *ap;
970
971 nr_pages = readahead_count(rac) - nr_pages;
972 if (nr_pages > max_pages)
973 nr_pages = max_pages;
974 if (nr_pages == 0)
975 break;
976 ia = fuse_io_alloc(NULL, nr_pages);
977 if (!ia)
978 return;
979 ap = &ia->ap;
980 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
981 for (i = 0; i < nr_pages; i++) {
982 fuse_wait_on_page_writeback(inode,
983 readahead_index(rac) + i);
984 ap->descs[i].length = PAGE_SIZE;
985 }
986 ap->num_pages = nr_pages;
987 fuse_send_readpages(ia, rac->file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000988 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000989}
990
David Brazdil0f672f62019-12-10 10:32:29 +0000991static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992{
993 struct inode *inode = iocb->ki_filp->f_mapping->host;
994 struct fuse_conn *fc = get_fuse_conn(inode);
995
996 /*
997 * In auto invalidate mode, always update attributes on read.
998 * Otherwise, only update if we attempt to read past EOF (to ensure
999 * i_size is up to date).
1000 */
1001 if (fc->auto_inval_data ||
1002 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1003 int err;
1004 err = fuse_update_attributes(inode, iocb->ki_filp);
1005 if (err)
1006 return err;
1007 }
1008
1009 return generic_file_read_iter(iocb, to);
1010}
1011
David Brazdil0f672f62019-12-10 10:32:29 +00001012static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1013 loff_t pos, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001014{
David Brazdil0f672f62019-12-10 10:32:29 +00001015 struct fuse_args *args = &ia->ap.args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001016
David Brazdil0f672f62019-12-10 10:32:29 +00001017 ia->write.in.fh = ff->fh;
1018 ia->write.in.offset = pos;
1019 ia->write.in.size = count;
1020 args->opcode = FUSE_WRITE;
1021 args->nodeid = ff->nodeid;
1022 args->in_numargs = 2;
Olivier Deprez157378f2022-04-04 15:47:50 +02001023 if (ff->fm->fc->minor < 9)
David Brazdil0f672f62019-12-10 10:32:29 +00001024 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001025 else
David Brazdil0f672f62019-12-10 10:32:29 +00001026 args->in_args[0].size = sizeof(ia->write.in);
1027 args->in_args[0].value = &ia->write.in;
1028 args->in_args[1].size = count;
1029 args->out_numargs = 1;
1030 args->out_args[0].size = sizeof(ia->write.out);
1031 args->out_args[0].value = &ia->write.out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032}
1033
David Brazdil0f672f62019-12-10 10:32:29 +00001034static unsigned int fuse_write_flags(struct kiocb *iocb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035{
David Brazdil0f672f62019-12-10 10:32:29 +00001036 unsigned int flags = iocb->ki_filp->f_flags;
1037
1038 if (iocb->ki_flags & IOCB_DSYNC)
1039 flags |= O_DSYNC;
1040 if (iocb->ki_flags & IOCB_SYNC)
1041 flags |= O_SYNC;
1042
1043 return flags;
1044}
1045
1046static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1047 size_t count, fl_owner_t owner)
1048{
1049 struct kiocb *iocb = ia->io->iocb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050 struct file *file = iocb->ki_filp;
1051 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02001052 struct fuse_mount *fm = ff->fm;
David Brazdil0f672f62019-12-10 10:32:29 +00001053 struct fuse_write_in *inarg = &ia->write.in;
1054 ssize_t err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001055
David Brazdil0f672f62019-12-10 10:32:29 +00001056 fuse_write_args_fill(ia, ff, pos, count);
1057 inarg->flags = fuse_write_flags(iocb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058 if (owner != NULL) {
1059 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
Olivier Deprez157378f2022-04-04 15:47:50 +02001060 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 }
1062
David Brazdil0f672f62019-12-10 10:32:29 +00001063 if (ia->io->async)
Olivier Deprez157378f2022-04-04 15:47:50 +02001064 return fuse_async_req_send(fm, ia, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001065
Olivier Deprez157378f2022-04-04 15:47:50 +02001066 err = fuse_simple_request(fm, &ia->ap.args);
David Brazdil0f672f62019-12-10 10:32:29 +00001067 if (!err && ia->write.out.size > count)
1068 err = -EIO;
1069
1070 return err ?: ia->write.out.size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071}
1072
1073bool fuse_write_update_size(struct inode *inode, loff_t pos)
1074{
1075 struct fuse_conn *fc = get_fuse_conn(inode);
1076 struct fuse_inode *fi = get_fuse_inode(inode);
1077 bool ret = false;
1078
David Brazdil0f672f62019-12-10 10:32:29 +00001079 spin_lock(&fi->lock);
1080 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001081 if (pos > inode->i_size) {
1082 i_size_write(inode, pos);
1083 ret = true;
1084 }
David Brazdil0f672f62019-12-10 10:32:29 +00001085 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001086
1087 return ret;
1088}
1089
David Brazdil0f672f62019-12-10 10:32:29 +00001090static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1091 struct kiocb *iocb, struct inode *inode,
1092 loff_t pos, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093{
David Brazdil0f672f62019-12-10 10:32:29 +00001094 struct fuse_args_pages *ap = &ia->ap;
1095 struct file *file = iocb->ki_filp;
1096 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02001097 struct fuse_mount *fm = ff->fm;
David Brazdil0f672f62019-12-10 10:32:29 +00001098 unsigned int offset, i;
Olivier Deprez0e641232021-09-23 10:07:05 +02001099 bool short_write;
David Brazdil0f672f62019-12-10 10:32:29 +00001100 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101
David Brazdil0f672f62019-12-10 10:32:29 +00001102 for (i = 0; i < ap->num_pages; i++)
1103 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001104
David Brazdil0f672f62019-12-10 10:32:29 +00001105 fuse_write_args_fill(ia, ff, pos, count);
1106 ia->write.in.flags = fuse_write_flags(iocb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107
Olivier Deprez157378f2022-04-04 15:47:50 +02001108 err = fuse_simple_request(fm, &ap->args);
Olivier Deprez0e641232021-09-23 10:07:05 +02001109 if (!err && ia->write.out.size > count)
1110 err = -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001111
Olivier Deprez0e641232021-09-23 10:07:05 +02001112 short_write = ia->write.out.size < count;
David Brazdil0f672f62019-12-10 10:32:29 +00001113 offset = ap->descs[0].offset;
1114 count = ia->write.out.size;
1115 for (i = 0; i < ap->num_pages; i++) {
1116 struct page *page = ap->pages[i];
1117
Olivier Deprez0e641232021-09-23 10:07:05 +02001118 if (err) {
1119 ClearPageUptodate(page);
1120 } else {
1121 if (count >= PAGE_SIZE - offset)
1122 count -= PAGE_SIZE - offset;
1123 else {
1124 if (short_write)
1125 ClearPageUptodate(page);
1126 count = 0;
1127 }
1128 offset = 0;
1129 }
1130 if (ia->write.page_locked && (i == ap->num_pages - 1))
1131 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001132 put_page(page);
1133 }
1134
David Brazdil0f672f62019-12-10 10:32:29 +00001135 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001136}
1137
Olivier Deprez0e641232021-09-23 10:07:05 +02001138static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
David Brazdil0f672f62019-12-10 10:32:29 +00001139 struct address_space *mapping,
1140 struct iov_iter *ii, loff_t pos,
1141 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142{
Olivier Deprez0e641232021-09-23 10:07:05 +02001143 struct fuse_args_pages *ap = &ia->ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001144 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1145 unsigned offset = pos & (PAGE_SIZE - 1);
1146 size_t count = 0;
1147 int err;
1148
David Brazdil0f672f62019-12-10 10:32:29 +00001149 ap->args.in_pages = true;
1150 ap->descs[0].offset = offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151
1152 do {
1153 size_t tmp;
1154 struct page *page;
1155 pgoff_t index = pos >> PAGE_SHIFT;
1156 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1157 iov_iter_count(ii));
1158
1159 bytes = min_t(size_t, bytes, fc->max_write - count);
1160
1161 again:
1162 err = -EFAULT;
1163 if (iov_iter_fault_in_readable(ii, bytes))
1164 break;
1165
1166 err = -ENOMEM;
1167 page = grab_cache_page_write_begin(mapping, index, 0);
1168 if (!page)
1169 break;
1170
1171 if (mapping_writably_mapped(mapping))
1172 flush_dcache_page(page);
1173
1174 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1175 flush_dcache_page(page);
1176
1177 iov_iter_advance(ii, tmp);
1178 if (!tmp) {
1179 unlock_page(page);
1180 put_page(page);
1181 bytes = min(bytes, iov_iter_single_seg_count(ii));
1182 goto again;
1183 }
1184
1185 err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001186 ap->pages[ap->num_pages] = page;
1187 ap->descs[ap->num_pages].length = tmp;
1188 ap->num_pages++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001189
1190 count += tmp;
1191 pos += tmp;
1192 offset += tmp;
1193 if (offset == PAGE_SIZE)
1194 offset = 0;
1195
Olivier Deprez0e641232021-09-23 10:07:05 +02001196 /* If we copied full page, mark it uptodate */
1197 if (tmp == PAGE_SIZE)
1198 SetPageUptodate(page);
1199
1200 if (PageUptodate(page)) {
1201 unlock_page(page);
1202 } else {
1203 ia->write.page_locked = true;
1204 break;
1205 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001206 if (!fc->big_writes)
1207 break;
1208 } while (iov_iter_count(ii) && count < fc->max_write &&
David Brazdil0f672f62019-12-10 10:32:29 +00001209 ap->num_pages < max_pages && offset == 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001210
1211 return count > 0 ? count : err;
1212}
1213
David Brazdil0f672f62019-12-10 10:32:29 +00001214static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1215 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001216{
David Brazdil0f672f62019-12-10 10:32:29 +00001217 return min_t(unsigned int,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001218 ((pos + len - 1) >> PAGE_SHIFT) -
1219 (pos >> PAGE_SHIFT) + 1,
David Brazdil0f672f62019-12-10 10:32:29 +00001220 max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001221}
1222
1223static ssize_t fuse_perform_write(struct kiocb *iocb,
1224 struct address_space *mapping,
1225 struct iov_iter *ii, loff_t pos)
1226{
1227 struct inode *inode = mapping->host;
1228 struct fuse_conn *fc = get_fuse_conn(inode);
1229 struct fuse_inode *fi = get_fuse_inode(inode);
1230 int err = 0;
1231 ssize_t res = 0;
1232
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001233 if (inode->i_size < pos + iov_iter_count(ii))
1234 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1235
1236 do {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001237 ssize_t count;
David Brazdil0f672f62019-12-10 10:32:29 +00001238 struct fuse_io_args ia = {};
1239 struct fuse_args_pages *ap = &ia.ap;
1240 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1241 fc->max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001242
David Brazdil0f672f62019-12-10 10:32:29 +00001243 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1244 if (!ap->pages) {
1245 err = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001246 break;
1247 }
1248
Olivier Deprez0e641232021-09-23 10:07:05 +02001249 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001250 if (count <= 0) {
1251 err = count;
1252 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001253 err = fuse_send_write_pages(&ia, iocb, inode,
1254 pos, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001255 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001256 size_t num_written = ia.write.out.size;
1257
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001258 res += num_written;
1259 pos += num_written;
1260
1261 /* break out of the loop on short write */
1262 if (num_written != count)
1263 err = -EIO;
1264 }
1265 }
David Brazdil0f672f62019-12-10 10:32:29 +00001266 kfree(ap->pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001267 } while (!err && iov_iter_count(ii));
1268
1269 if (res > 0)
1270 fuse_write_update_size(inode, pos);
1271
1272 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1273 fuse_invalidate_attr(inode);
1274
1275 return res > 0 ? res : err;
1276}
1277
David Brazdil0f672f62019-12-10 10:32:29 +00001278static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001279{
1280 struct file *file = iocb->ki_filp;
1281 struct address_space *mapping = file->f_mapping;
1282 ssize_t written = 0;
1283 ssize_t written_buffered = 0;
1284 struct inode *inode = mapping->host;
1285 ssize_t err;
1286 loff_t endbyte = 0;
1287
1288 if (get_fuse_conn(inode)->writeback_cache) {
1289 /* Update size (EOF optimization) and mode (SUID clearing) */
1290 err = fuse_update_attributes(mapping->host, file);
1291 if (err)
1292 return err;
1293
1294 return generic_file_write_iter(iocb, from);
1295 }
1296
1297 inode_lock(inode);
1298
1299 /* We can write back this queue in page reclaim */
1300 current->backing_dev_info = inode_to_bdi(inode);
1301
1302 err = generic_write_checks(iocb, from);
1303 if (err <= 0)
1304 goto out;
1305
1306 err = file_remove_privs(file);
1307 if (err)
1308 goto out;
1309
1310 err = file_update_time(file);
1311 if (err)
1312 goto out;
1313
1314 if (iocb->ki_flags & IOCB_DIRECT) {
1315 loff_t pos = iocb->ki_pos;
1316 written = generic_file_direct_write(iocb, from);
1317 if (written < 0 || !iov_iter_count(from))
1318 goto out;
1319
1320 pos += written;
1321
1322 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1323 if (written_buffered < 0) {
1324 err = written_buffered;
1325 goto out;
1326 }
1327 endbyte = pos + written_buffered - 1;
1328
1329 err = filemap_write_and_wait_range(file->f_mapping, pos,
1330 endbyte);
1331 if (err)
1332 goto out;
1333
1334 invalidate_mapping_pages(file->f_mapping,
1335 pos >> PAGE_SHIFT,
1336 endbyte >> PAGE_SHIFT);
1337
1338 written += written_buffered;
1339 iocb->ki_pos = pos + written_buffered;
1340 } else {
1341 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1342 if (written >= 0)
1343 iocb->ki_pos += written;
1344 }
1345out:
1346 current->backing_dev_info = NULL;
1347 inode_unlock(inode);
1348 if (written > 0)
1349 written = generic_write_sync(iocb, written);
1350
1351 return written ? written : err;
1352}
1353
David Brazdil0f672f62019-12-10 10:32:29 +00001354static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1355 unsigned int index,
1356 unsigned int nr_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357{
1358 int i;
1359
1360 for (i = index; i < index + nr_pages; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00001361 descs[i].length = PAGE_SIZE - descs[i].offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001362}
1363
1364static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1365{
1366 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1367}
1368
1369static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1370 size_t max_size)
1371{
1372 return min(iov_iter_single_seg_count(ii), max_size);
1373}
1374
David Brazdil0f672f62019-12-10 10:32:29 +00001375static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1376 size_t *nbytesp, int write,
1377 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378{
1379 size_t nbytes = 0; /* # bytes already packed in req */
1380 ssize_t ret = 0;
1381
1382 /* Special case for kernel I/O: can copy directly into the buffer */
David Brazdil0f672f62019-12-10 10:32:29 +00001383 if (iov_iter_is_kvec(ii)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001384 unsigned long user_addr = fuse_get_user_addr(ii);
1385 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1386
1387 if (write)
David Brazdil0f672f62019-12-10 10:32:29 +00001388 ap->args.in_args[1].value = (void *) user_addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001389 else
David Brazdil0f672f62019-12-10 10:32:29 +00001390 ap->args.out_args[0].value = (void *) user_addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391
1392 iov_iter_advance(ii, frag_size);
1393 *nbytesp = frag_size;
1394 return 0;
1395 }
1396
David Brazdil0f672f62019-12-10 10:32:29 +00001397 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001398 unsigned npages;
1399 size_t start;
David Brazdil0f672f62019-12-10 10:32:29 +00001400 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401 *nbytesp - nbytes,
David Brazdil0f672f62019-12-10 10:32:29 +00001402 max_pages - ap->num_pages,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001403 &start);
1404 if (ret < 0)
1405 break;
1406
1407 iov_iter_advance(ii, ret);
1408 nbytes += ret;
1409
1410 ret += start;
1411 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1412
David Brazdil0f672f62019-12-10 10:32:29 +00001413 ap->descs[ap->num_pages].offset = start;
1414 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001415
David Brazdil0f672f62019-12-10 10:32:29 +00001416 ap->num_pages += npages;
1417 ap->descs[ap->num_pages - 1].length -=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1419 }
1420
Olivier Deprez157378f2022-04-04 15:47:50 +02001421 ap->args.user_pages = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001422 if (write)
Olivier Deprez157378f2022-04-04 15:47:50 +02001423 ap->args.in_pages = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001424 else
Olivier Deprez157378f2022-04-04 15:47:50 +02001425 ap->args.out_pages = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426
1427 *nbytesp = nbytes;
1428
1429 return ret < 0 ? ret : 0;
1430}
1431
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001432ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1433 loff_t *ppos, int flags)
1434{
1435 int write = flags & FUSE_DIO_WRITE;
1436 int cuse = flags & FUSE_DIO_CUSE;
1437 struct file *file = io->iocb->ki_filp;
1438 struct inode *inode = file->f_mapping->host;
1439 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02001440 struct fuse_conn *fc = ff->fm->fc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001441 size_t nmax = write ? fc->max_write : fc->max_read;
1442 loff_t pos = *ppos;
1443 size_t count = iov_iter_count(iter);
1444 pgoff_t idx_from = pos >> PAGE_SHIFT;
1445 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1446 ssize_t res = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001447 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001448 struct fuse_io_args *ia;
1449 unsigned int max_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001450
David Brazdil0f672f62019-12-10 10:32:29 +00001451 max_pages = iov_iter_npages(iter, fc->max_pages);
1452 ia = fuse_io_alloc(io, max_pages);
1453 if (!ia)
1454 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001455
David Brazdil0f672f62019-12-10 10:32:29 +00001456 ia->io = io;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001457 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1458 if (!write)
1459 inode_lock(inode);
1460 fuse_sync_writes(inode);
1461 if (!write)
1462 inode_unlock(inode);
1463 }
1464
1465 io->should_dirty = !write && iter_is_iovec(iter);
1466 while (count) {
David Brazdil0f672f62019-12-10 10:32:29 +00001467 ssize_t nres;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001468 fl_owner_t owner = current->files;
1469 size_t nbytes = min(count, nmax);
David Brazdil0f672f62019-12-10 10:32:29 +00001470
1471 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1472 max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001473 if (err && !nbytes)
1474 break;
1475
David Brazdil0f672f62019-12-10 10:32:29 +00001476 if (write) {
1477 if (!capable(CAP_FSETID))
1478 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479
David Brazdil0f672f62019-12-10 10:32:29 +00001480 nres = fuse_send_write(ia, pos, nbytes, owner);
1481 } else {
1482 nres = fuse_send_read(ia, pos, nbytes, owner);
1483 }
1484
1485 if (!io->async || nres < 0) {
1486 fuse_release_user_pages(&ia->ap, io->should_dirty);
1487 fuse_io_free(ia);
1488 }
1489 ia = NULL;
1490 if (nres < 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001491 iov_iter_revert(iter, nbytes);
David Brazdil0f672f62019-12-10 10:32:29 +00001492 err = nres;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001493 break;
1494 }
David Brazdil0f672f62019-12-10 10:32:29 +00001495 WARN_ON(nres > nbytes);
1496
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001497 count -= nres;
1498 res += nres;
1499 pos += nres;
Olivier Deprez0e641232021-09-23 10:07:05 +02001500 if (nres != nbytes) {
1501 iov_iter_revert(iter, nbytes - nres);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001502 break;
Olivier Deprez0e641232021-09-23 10:07:05 +02001503 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504 if (count) {
David Brazdil0f672f62019-12-10 10:32:29 +00001505 max_pages = iov_iter_npages(iter, fc->max_pages);
1506 ia = fuse_io_alloc(io, max_pages);
1507 if (!ia)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508 break;
1509 }
1510 }
David Brazdil0f672f62019-12-10 10:32:29 +00001511 if (ia)
1512 fuse_io_free(ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513 if (res > 0)
1514 *ppos = pos;
1515
1516 return res > 0 ? res : err;
1517}
1518EXPORT_SYMBOL_GPL(fuse_direct_io);
1519
1520static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1521 struct iov_iter *iter,
1522 loff_t *ppos)
1523{
1524 ssize_t res;
1525 struct inode *inode = file_inode(io->iocb->ki_filp);
1526
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001527 res = fuse_direct_io(io, iter, ppos, 0);
1528
David Brazdil0f672f62019-12-10 10:32:29 +00001529 fuse_invalidate_atime(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001530
1531 return res;
1532}
1533
David Brazdil0f672f62019-12-10 10:32:29 +00001534static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1535
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001536static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1537{
David Brazdil0f672f62019-12-10 10:32:29 +00001538 ssize_t res;
1539
1540 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1541 res = fuse_direct_IO(iocb, to);
1542 } else {
1543 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1544
1545 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1546 }
1547
1548 return res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001549}
1550
1551static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1552{
1553 struct inode *inode = file_inode(iocb->ki_filp);
1554 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1555 ssize_t res;
1556
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001557 /* Don't allow parallel writes to the same file */
1558 inode_lock(inode);
1559 res = generic_write_checks(iocb, from);
David Brazdil0f672f62019-12-10 10:32:29 +00001560 if (res > 0) {
1561 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1562 res = fuse_direct_IO(iocb, from);
1563 } else {
1564 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1565 FUSE_DIO_WRITE);
1566 }
1567 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001568 fuse_invalidate_attr(inode);
1569 if (res > 0)
1570 fuse_write_update_size(inode, iocb->ki_pos);
1571 inode_unlock(inode);
1572
1573 return res;
1574}
1575
David Brazdil0f672f62019-12-10 10:32:29 +00001576static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001577{
David Brazdil0f672f62019-12-10 10:32:29 +00001578 struct file *file = iocb->ki_filp;
1579 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02001580 struct inode *inode = file_inode(file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001581
Olivier Deprez157378f2022-04-04 15:47:50 +02001582 if (fuse_is_bad(inode))
David Brazdil0f672f62019-12-10 10:32:29 +00001583 return -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001584
Olivier Deprez157378f2022-04-04 15:47:50 +02001585 if (FUSE_IS_DAX(inode))
1586 return fuse_dax_read_iter(iocb, to);
1587
David Brazdil0f672f62019-12-10 10:32:29 +00001588 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1589 return fuse_cache_read_iter(iocb, to);
1590 else
1591 return fuse_direct_read_iter(iocb, to);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592}
1593
David Brazdil0f672f62019-12-10 10:32:29 +00001594static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001595{
David Brazdil0f672f62019-12-10 10:32:29 +00001596 struct file *file = iocb->ki_filp;
1597 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02001598 struct inode *inode = file_inode(file);
David Brazdil0f672f62019-12-10 10:32:29 +00001599
Olivier Deprez157378f2022-04-04 15:47:50 +02001600 if (fuse_is_bad(inode))
David Brazdil0f672f62019-12-10 10:32:29 +00001601 return -EIO;
1602
Olivier Deprez157378f2022-04-04 15:47:50 +02001603 if (FUSE_IS_DAX(inode))
1604 return fuse_dax_write_iter(iocb, from);
1605
David Brazdil0f672f62019-12-10 10:32:29 +00001606 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1607 return fuse_cache_write_iter(iocb, from);
1608 else
1609 return fuse_direct_write_iter(iocb, from);
1610}
1611
1612static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1613{
1614 struct fuse_args_pages *ap = &wpa->ia.ap;
1615 int i;
1616
1617 for (i = 0; i < ap->num_pages; i++)
1618 __free_page(ap->pages[i]);
1619
1620 if (wpa->ia.ff)
1621 fuse_file_put(wpa->ia.ff, false, false);
1622
1623 kfree(ap->pages);
1624 kfree(wpa);
1625}
1626
Olivier Deprez157378f2022-04-04 15:47:50 +02001627static void fuse_writepage_finish(struct fuse_mount *fm,
David Brazdil0f672f62019-12-10 10:32:29 +00001628 struct fuse_writepage_args *wpa)
1629{
1630 struct fuse_args_pages *ap = &wpa->ia.ap;
1631 struct inode *inode = wpa->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001632 struct fuse_inode *fi = get_fuse_inode(inode);
1633 struct backing_dev_info *bdi = inode_to_bdi(inode);
1634 int i;
1635
David Brazdil0f672f62019-12-10 10:32:29 +00001636 for (i = 0; i < ap->num_pages; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001637 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
David Brazdil0f672f62019-12-10 10:32:29 +00001638 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001639 wb_writeout_inc(&bdi->wb);
1640 }
1641 wake_up(&fi->page_waitq);
1642}
1643
David Brazdil0f672f62019-12-10 10:32:29 +00001644/* Called under fi->lock, may release and reacquire it */
Olivier Deprez157378f2022-04-04 15:47:50 +02001645static void fuse_send_writepage(struct fuse_mount *fm,
David Brazdil0f672f62019-12-10 10:32:29 +00001646 struct fuse_writepage_args *wpa, loff_t size)
1647__releases(fi->lock)
1648__acquires(fi->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001649{
David Brazdil0f672f62019-12-10 10:32:29 +00001650 struct fuse_writepage_args *aux, *next;
1651 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1652 struct fuse_write_in *inarg = &wpa->ia.write.in;
1653 struct fuse_args *args = &wpa->ia.ap.args;
1654 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1655 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001656
David Brazdil0f672f62019-12-10 10:32:29 +00001657 fi->writectr++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001658 if (inarg->offset + data_size <= size) {
1659 inarg->size = data_size;
1660 } else if (inarg->offset < size) {
1661 inarg->size = size - inarg->offset;
1662 } else {
1663 /* Got truncated off completely */
1664 goto out_free;
1665 }
1666
David Brazdil0f672f62019-12-10 10:32:29 +00001667 args->in_args[1].size = inarg->size;
1668 args->force = true;
1669 args->nocreds = true;
1670
Olivier Deprez157378f2022-04-04 15:47:50 +02001671 err = fuse_simple_background(fm, args, GFP_ATOMIC);
David Brazdil0f672f62019-12-10 10:32:29 +00001672 if (err == -ENOMEM) {
1673 spin_unlock(&fi->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001674 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
David Brazdil0f672f62019-12-10 10:32:29 +00001675 spin_lock(&fi->lock);
1676 }
1677
1678 /* Fails on broken connection only */
1679 if (unlikely(err))
1680 goto out_free;
1681
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001682 return;
1683
1684 out_free:
David Brazdil0f672f62019-12-10 10:32:29 +00001685 fi->writectr--;
Olivier Deprez157378f2022-04-04 15:47:50 +02001686 rb_erase(&wpa->writepages_entry, &fi->writepages);
1687 fuse_writepage_finish(fm, wpa);
David Brazdil0f672f62019-12-10 10:32:29 +00001688 spin_unlock(&fi->lock);
1689
1690 /* After fuse_writepage_finish() aux request list is private */
1691 for (aux = wpa->next; aux; aux = next) {
1692 next = aux->next;
1693 aux->next = NULL;
1694 fuse_writepage_free(aux);
1695 }
1696
1697 fuse_writepage_free(wpa);
1698 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001699}
1700
1701/*
1702 * If fi->writectr is positive (no truncate or fsync going on) send
1703 * all queued writepage requests.
1704 *
David Brazdil0f672f62019-12-10 10:32:29 +00001705 * Called with fi->lock
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001706 */
1707void fuse_flush_writepages(struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +00001708__releases(fi->lock)
1709__acquires(fi->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001710{
Olivier Deprez157378f2022-04-04 15:47:50 +02001711 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001712 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001713 loff_t crop = i_size_read(inode);
1714 struct fuse_writepage_args *wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001715
1716 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001717 wpa = list_entry(fi->queued_writes.next,
1718 struct fuse_writepage_args, queue_entry);
1719 list_del_init(&wpa->queue_entry);
Olivier Deprez157378f2022-04-04 15:47:50 +02001720 fuse_send_writepage(fm, wpa, crop);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001721 }
1722}
1723
Olivier Deprez157378f2022-04-04 15:47:50 +02001724static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1725 struct fuse_writepage_args *wpa)
1726{
1727 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1728 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1729 struct rb_node **p = &root->rb_node;
1730 struct rb_node *parent = NULL;
1731
1732 WARN_ON(!wpa->ia.ap.num_pages);
1733 while (*p) {
1734 struct fuse_writepage_args *curr;
1735 pgoff_t curr_index;
1736
1737 parent = *p;
1738 curr = rb_entry(parent, struct fuse_writepage_args,
1739 writepages_entry);
1740 WARN_ON(curr->inode != wpa->inode);
1741 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1742
1743 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1744 p = &(*p)->rb_right;
1745 else if (idx_to < curr_index)
1746 p = &(*p)->rb_left;
1747 else
1748 return curr;
1749 }
1750
1751 rb_link_node(&wpa->writepages_entry, parent, p);
1752 rb_insert_color(&wpa->writepages_entry, root);
1753 return NULL;
1754}
1755
1756static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1757{
1758 WARN_ON(fuse_insert_writeback(root, wpa));
1759}
1760
1761static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
David Brazdil0f672f62019-12-10 10:32:29 +00001762 int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001763{
David Brazdil0f672f62019-12-10 10:32:29 +00001764 struct fuse_writepage_args *wpa =
1765 container_of(args, typeof(*wpa), ia.ap.args);
1766 struct inode *inode = wpa->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001767 struct fuse_inode *fi = get_fuse_inode(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02001768 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001769
David Brazdil0f672f62019-12-10 10:32:29 +00001770 mapping_set_error(inode->i_mapping, error);
Olivier Deprez157378f2022-04-04 15:47:50 +02001771 /*
1772 * A writeback finished and this might have updated mtime/ctime on
1773 * server making local mtime/ctime stale. Hence invalidate attrs.
1774 * Do this only if writeback_cache is not enabled. If writeback_cache
1775 * is enabled, we trust local ctime/mtime.
1776 */
1777 if (!fc->writeback_cache)
1778 fuse_invalidate_attr(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001779 spin_lock(&fi->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001780 rb_erase(&wpa->writepages_entry, &fi->writepages);
David Brazdil0f672f62019-12-10 10:32:29 +00001781 while (wpa->next) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001782 struct fuse_mount *fm = get_fuse_mount(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001783 struct fuse_write_in *inarg = &wpa->ia.write.in;
1784 struct fuse_writepage_args *next = wpa->next;
1785
1786 wpa->next = next->next;
1787 next->next = NULL;
1788 next->ia.ff = fuse_file_get(wpa->ia.ff);
Olivier Deprez157378f2022-04-04 15:47:50 +02001789 tree_insert(&fi->writepages, next);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001790
1791 /*
1792 * Skip fuse_flush_writepages() to make it easy to crop requests
1793 * based on primary request size.
1794 *
1795 * 1st case (trivial): there are no concurrent activities using
1796 * fuse_set/release_nowrite. Then we're on safe side because
1797 * fuse_flush_writepages() would call fuse_send_writepage()
1798 * anyway.
1799 *
1800 * 2nd case: someone called fuse_set_nowrite and it is waiting
1801 * now for completion of all in-flight requests. This happens
1802 * rarely and no more than once per page, so this should be
1803 * okay.
1804 *
1805 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1806 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1807 * that fuse_set_nowrite returned implies that all in-flight
1808 * requests were completed along with all of their secondary
1809 * requests. Further primary requests are blocked by negative
1810 * writectr. Hence there cannot be any in-flight requests and
1811 * no invocations of fuse_writepage_end() while we're in
1812 * fuse_set_nowrite..fuse_release_nowrite section.
1813 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001814 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001815 }
1816 fi->writectr--;
Olivier Deprez157378f2022-04-04 15:47:50 +02001817 fuse_writepage_finish(fm, wpa);
David Brazdil0f672f62019-12-10 10:32:29 +00001818 spin_unlock(&fi->lock);
1819 fuse_writepage_free(wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001820}
1821
1822static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1823 struct fuse_inode *fi)
1824{
1825 struct fuse_file *ff = NULL;
1826
David Brazdil0f672f62019-12-10 10:32:29 +00001827 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001828 if (!list_empty(&fi->write_files)) {
1829 ff = list_entry(fi->write_files.next, struct fuse_file,
1830 write_entry);
1831 fuse_file_get(ff);
1832 }
David Brazdil0f672f62019-12-10 10:32:29 +00001833 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001834
1835 return ff;
1836}
1837
1838static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1839 struct fuse_inode *fi)
1840{
1841 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1842 WARN_ON(!ff);
1843 return ff;
1844}
1845
1846int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1847{
1848 struct fuse_conn *fc = get_fuse_conn(inode);
1849 struct fuse_inode *fi = get_fuse_inode(inode);
1850 struct fuse_file *ff;
1851 int err;
1852
Olivier Deprez157378f2022-04-04 15:47:50 +02001853 /*
1854 * Inode is always written before the last reference is dropped and
1855 * hence this should not be reached from reclaim.
1856 *
1857 * Writing back the inode from reclaim can deadlock if the request
1858 * processing itself needs an allocation. Allocations triggering
1859 * reclaim while serving a request can't be prevented, because it can
1860 * involve any number of unrelated userspace processes.
1861 */
1862 WARN_ON(wbc->for_reclaim);
1863
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 ff = __fuse_write_file_get(fc, fi);
1865 err = fuse_flush_times(inode, ff);
1866 if (ff)
1867 fuse_file_put(ff, false, false);
1868
1869 return err;
1870}
1871
David Brazdil0f672f62019-12-10 10:32:29 +00001872static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1873{
1874 struct fuse_writepage_args *wpa;
1875 struct fuse_args_pages *ap;
1876
1877 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1878 if (wpa) {
1879 ap = &wpa->ia.ap;
1880 ap->num_pages = 0;
1881 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1882 if (!ap->pages) {
1883 kfree(wpa);
1884 wpa = NULL;
1885 }
1886 }
1887 return wpa;
1888
1889}
1890
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001891static int fuse_writepage_locked(struct page *page)
1892{
1893 struct address_space *mapping = page->mapping;
1894 struct inode *inode = mapping->host;
1895 struct fuse_conn *fc = get_fuse_conn(inode);
1896 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001897 struct fuse_writepage_args *wpa;
1898 struct fuse_args_pages *ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001899 struct page *tmp_page;
1900 int error = -ENOMEM;
1901
1902 set_page_writeback(page);
1903
David Brazdil0f672f62019-12-10 10:32:29 +00001904 wpa = fuse_writepage_args_alloc();
1905 if (!wpa)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001906 goto err;
David Brazdil0f672f62019-12-10 10:32:29 +00001907 ap = &wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001908
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001909 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1910 if (!tmp_page)
1911 goto err_free;
1912
1913 error = -EIO;
David Brazdil0f672f62019-12-10 10:32:29 +00001914 wpa->ia.ff = fuse_write_file_get(fc, fi);
1915 if (!wpa->ia.ff)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001916 goto err_nofile;
1917
David Brazdil0f672f62019-12-10 10:32:29 +00001918 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001919
1920 copy_highpage(tmp_page, page);
David Brazdil0f672f62019-12-10 10:32:29 +00001921 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1922 wpa->next = NULL;
1923 ap->args.in_pages = true;
1924 ap->num_pages = 1;
1925 ap->pages[0] = tmp_page;
1926 ap->descs[0].offset = 0;
1927 ap->descs[0].length = PAGE_SIZE;
1928 ap->args.end = fuse_writepage_end;
1929 wpa->inode = inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001930
1931 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1932 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1933
David Brazdil0f672f62019-12-10 10:32:29 +00001934 spin_lock(&fi->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001935 tree_insert(&fi->writepages, wpa);
David Brazdil0f672f62019-12-10 10:32:29 +00001936 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001937 fuse_flush_writepages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001938 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001939
1940 end_page_writeback(page);
1941
1942 return 0;
1943
1944err_nofile:
1945 __free_page(tmp_page);
1946err_free:
David Brazdil0f672f62019-12-10 10:32:29 +00001947 kfree(wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001948err:
1949 mapping_set_error(page->mapping, error);
1950 end_page_writeback(page);
1951 return error;
1952}
1953
1954static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1955{
1956 int err;
1957
1958 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1959 /*
1960 * ->writepages() should be called for sync() and friends. We
1961 * should only get here on direct reclaim and then we are
1962 * allowed to skip a page which is already in flight
1963 */
1964 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1965
1966 redirty_page_for_writepage(wbc, page);
David Brazdil0f672f62019-12-10 10:32:29 +00001967 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001968 return 0;
1969 }
1970
1971 err = fuse_writepage_locked(page);
1972 unlock_page(page);
1973
1974 return err;
1975}
1976
1977struct fuse_fill_wb_data {
David Brazdil0f672f62019-12-10 10:32:29 +00001978 struct fuse_writepage_args *wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001979 struct fuse_file *ff;
1980 struct inode *inode;
1981 struct page **orig_pages;
David Brazdil0f672f62019-12-10 10:32:29 +00001982 unsigned int max_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001983};
1984
David Brazdil0f672f62019-12-10 10:32:29 +00001985static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1986{
1987 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1988 struct fuse_conn *fc = get_fuse_conn(data->inode);
1989 struct page **pages;
1990 struct fuse_page_desc *descs;
1991 unsigned int npages = min_t(unsigned int,
1992 max_t(unsigned int, data->max_pages * 2,
1993 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1994 fc->max_pages);
1995 WARN_ON(npages <= data->max_pages);
1996
1997 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1998 if (!pages)
1999 return false;
2000
2001 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
2002 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
2003 kfree(ap->pages);
2004 ap->pages = pages;
2005 ap->descs = descs;
2006 data->max_pages = npages;
2007
2008 return true;
2009}
2010
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002011static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2012{
David Brazdil0f672f62019-12-10 10:32:29 +00002013 struct fuse_writepage_args *wpa = data->wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002014 struct inode *inode = data->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002015 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00002016 int num_pages = wpa->ia.ap.num_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002017 int i;
2018
David Brazdil0f672f62019-12-10 10:32:29 +00002019 wpa->ia.ff = fuse_file_get(data->ff);
2020 spin_lock(&fi->lock);
2021 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002022 fuse_flush_writepages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00002023 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002024
2025 for (i = 0; i < num_pages; i++)
2026 end_page_writeback(data->orig_pages[i]);
2027}
2028
David Brazdil0f672f62019-12-10 10:32:29 +00002029/*
Olivier Deprez157378f2022-04-04 15:47:50 +02002030 * Check under fi->lock if the page is under writeback, and insert it onto the
2031 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
David Brazdil0f672f62019-12-10 10:32:29 +00002032 * one already added for a page at this offset. If there's none, then insert
2033 * this new request onto the auxiliary list, otherwise reuse the existing one by
Olivier Deprez157378f2022-04-04 15:47:50 +02002034 * swapping the new temp page with the old one.
David Brazdil0f672f62019-12-10 10:32:29 +00002035 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002036static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2037 struct page *page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002038{
David Brazdil0f672f62019-12-10 10:32:29 +00002039 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2040 struct fuse_writepage_args *tmp;
2041 struct fuse_writepage_args *old_wpa;
2042 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002043
David Brazdil0f672f62019-12-10 10:32:29 +00002044 WARN_ON(new_ap->num_pages != 0);
Olivier Deprez157378f2022-04-04 15:47:50 +02002045 new_ap->num_pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002046
David Brazdil0f672f62019-12-10 10:32:29 +00002047 spin_lock(&fi->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02002048 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
David Brazdil0f672f62019-12-10 10:32:29 +00002049 if (!old_wpa) {
David Brazdil0f672f62019-12-10 10:32:29 +00002050 spin_unlock(&fi->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02002051 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00002052 }
2053
David Brazdil0f672f62019-12-10 10:32:29 +00002054 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2055 pgoff_t curr_index;
2056
2057 WARN_ON(tmp->inode != new_wpa->inode);
2058 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2059 if (curr_index == page->index) {
2060 WARN_ON(tmp->ia.ap.num_pages != 1);
2061 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002062 break;
2063 }
2064 }
David Brazdil0f672f62019-12-10 10:32:29 +00002065
2066 if (!tmp) {
2067 new_wpa->next = old_wpa->next;
2068 old_wpa->next = new_wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002069 }
2070
David Brazdil0f672f62019-12-10 10:32:29 +00002071 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002072
David Brazdil0f672f62019-12-10 10:32:29 +00002073 if (tmp) {
2074 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002075
2076 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
David Brazdil0f672f62019-12-10 10:32:29 +00002077 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002078 wb_writeout_inc(&bdi->wb);
David Brazdil0f672f62019-12-10 10:32:29 +00002079 fuse_writepage_free(new_wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002080 }
David Brazdil0f672f62019-12-10 10:32:29 +00002081
Olivier Deprez157378f2022-04-04 15:47:50 +02002082 return false;
2083}
2084
2085static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2086 struct fuse_args_pages *ap,
2087 struct fuse_fill_wb_data *data)
2088{
2089 WARN_ON(!ap->num_pages);
2090
2091 /*
2092 * Being under writeback is unlikely but possible. For example direct
2093 * read to an mmaped fuse file will set the page dirty twice; once when
2094 * the pages are faulted with get_user_pages(), and then after the read
2095 * completed.
2096 */
2097 if (fuse_page_is_writeback(data->inode, page->index))
2098 return true;
2099
2100 /* Reached max pages */
2101 if (ap->num_pages == fc->max_pages)
2102 return true;
2103
2104 /* Reached max write bytes */
2105 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2106 return true;
2107
2108 /* Discontinuity */
2109 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2110 return true;
2111
2112 /* Need to grow the pages array? If so, did the expansion fail? */
2113 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2114 return true;
2115
2116 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002117}
2118
2119static int fuse_writepages_fill(struct page *page,
2120 struct writeback_control *wbc, void *_data)
2121{
2122 struct fuse_fill_wb_data *data = _data;
David Brazdil0f672f62019-12-10 10:32:29 +00002123 struct fuse_writepage_args *wpa = data->wpa;
2124 struct fuse_args_pages *ap = &wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002125 struct inode *inode = data->inode;
David Brazdil0f672f62019-12-10 10:32:29 +00002126 struct fuse_inode *fi = get_fuse_inode(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002127 struct fuse_conn *fc = get_fuse_conn(inode);
2128 struct page *tmp_page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002129 int err;
2130
2131 if (!data->ff) {
2132 err = -EIO;
David Brazdil0f672f62019-12-10 10:32:29 +00002133 data->ff = fuse_write_file_get(fc, fi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002134 if (!data->ff)
2135 goto out_unlock;
2136 }
2137
Olivier Deprez157378f2022-04-04 15:47:50 +02002138 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002139 fuse_writepages_send(data);
David Brazdil0f672f62019-12-10 10:32:29 +00002140 data->wpa = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002141 }
David Brazdil0f672f62019-12-10 10:32:29 +00002142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002143 err = -ENOMEM;
2144 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2145 if (!tmp_page)
2146 goto out_unlock;
2147
2148 /*
2149 * The page must not be redirtied until the writeout is completed
2150 * (i.e. userspace has sent a reply to the write request). Otherwise
2151 * there could be more than one temporary page instance for each real
2152 * page.
2153 *
2154 * This is ensured by holding the page lock in page_mkwrite() while
2155 * checking fuse_page_is_writeback(). We already hold the page lock
2156 * since clear_page_dirty_for_io() and keep it held until we add the
David Brazdil0f672f62019-12-10 10:32:29 +00002157 * request to the fi->writepages list and increment ap->num_pages.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002158 * After this fuse_page_is_writeback() will indicate that the page is
2159 * under writeback, so we can release the page lock.
2160 */
David Brazdil0f672f62019-12-10 10:32:29 +00002161 if (data->wpa == NULL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002162 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002163 wpa = fuse_writepage_args_alloc();
2164 if (!wpa) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002165 __free_page(tmp_page);
2166 goto out_unlock;
2167 }
David Brazdil0f672f62019-12-10 10:32:29 +00002168 data->max_pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002169
David Brazdil0f672f62019-12-10 10:32:29 +00002170 ap = &wpa->ia.ap;
2171 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2172 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2173 wpa->next = NULL;
2174 ap->args.in_pages = true;
2175 ap->args.end = fuse_writepage_end;
2176 ap->num_pages = 0;
2177 wpa->inode = inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002178 }
2179 set_page_writeback(page);
2180
2181 copy_highpage(tmp_page, page);
David Brazdil0f672f62019-12-10 10:32:29 +00002182 ap->pages[ap->num_pages] = tmp_page;
2183 ap->descs[ap->num_pages].offset = 0;
2184 ap->descs[ap->num_pages].length = PAGE_SIZE;
Olivier Deprez157378f2022-04-04 15:47:50 +02002185 data->orig_pages[ap->num_pages] = page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002186
2187 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2188 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2189
2190 err = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002191 if (data->wpa) {
2192 /*
2193 * Protected by fi->lock against concurrent access by
2194 * fuse_page_is_writeback().
2195 */
2196 spin_lock(&fi->lock);
2197 ap->num_pages++;
2198 spin_unlock(&fi->lock);
2199 } else if (fuse_writepage_add(wpa, page)) {
2200 data->wpa = wpa;
2201 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002202 end_page_writeback(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002203 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002204out_unlock:
2205 unlock_page(page);
2206
2207 return err;
2208}
2209
2210static int fuse_writepages(struct address_space *mapping,
2211 struct writeback_control *wbc)
2212{
2213 struct inode *inode = mapping->host;
David Brazdil0f672f62019-12-10 10:32:29 +00002214 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002215 struct fuse_fill_wb_data data;
2216 int err;
2217
2218 err = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02002219 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002220 goto out;
2221
2222 data.inode = inode;
David Brazdil0f672f62019-12-10 10:32:29 +00002223 data.wpa = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002224 data.ff = NULL;
2225
2226 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002227 data.orig_pages = kcalloc(fc->max_pages,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002228 sizeof(struct page *),
2229 GFP_NOFS);
2230 if (!data.orig_pages)
2231 goto out;
2232
2233 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
David Brazdil0f672f62019-12-10 10:32:29 +00002234 if (data.wpa) {
David Brazdil0f672f62019-12-10 10:32:29 +00002235 WARN_ON(!data.wpa->ia.ap.num_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002236 fuse_writepages_send(&data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002237 }
2238 if (data.ff)
2239 fuse_file_put(data.ff, false, false);
2240
2241 kfree(data.orig_pages);
2242out:
2243 return err;
2244}
2245
2246/*
2247 * It's worthy to make sure that space is reserved on disk for the write,
2248 * but how to implement it without killing performance need more thinking.
2249 */
2250static int fuse_write_begin(struct file *file, struct address_space *mapping,
2251 loff_t pos, unsigned len, unsigned flags,
2252 struct page **pagep, void **fsdata)
2253{
2254 pgoff_t index = pos >> PAGE_SHIFT;
2255 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2256 struct page *page;
2257 loff_t fsize;
2258 int err = -ENOMEM;
2259
2260 WARN_ON(!fc->writeback_cache);
2261
2262 page = grab_cache_page_write_begin(mapping, index, flags);
2263 if (!page)
2264 goto error;
2265
2266 fuse_wait_on_page_writeback(mapping->host, page->index);
2267
2268 if (PageUptodate(page) || len == PAGE_SIZE)
2269 goto success;
2270 /*
2271 * Check if the start this page comes after the end of file, in which
2272 * case the readpage can be optimized away.
2273 */
2274 fsize = i_size_read(mapping->host);
2275 if (fsize <= (pos & PAGE_MASK)) {
2276 size_t off = pos & ~PAGE_MASK;
2277 if (off)
2278 zero_user_segment(page, 0, off);
2279 goto success;
2280 }
2281 err = fuse_do_readpage(file, page);
2282 if (err)
2283 goto cleanup;
2284success:
2285 *pagep = page;
2286 return 0;
2287
2288cleanup:
2289 unlock_page(page);
2290 put_page(page);
2291error:
2292 return err;
2293}
2294
2295static int fuse_write_end(struct file *file, struct address_space *mapping,
2296 loff_t pos, unsigned len, unsigned copied,
2297 struct page *page, void *fsdata)
2298{
2299 struct inode *inode = page->mapping->host;
2300
2301 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2302 if (!copied)
2303 goto unlock;
2304
2305 if (!PageUptodate(page)) {
2306 /* Zero any unwritten bytes at the end of the page */
2307 size_t endoff = (pos + copied) & ~PAGE_MASK;
2308 if (endoff)
2309 zero_user_segment(page, endoff, PAGE_SIZE);
2310 SetPageUptodate(page);
2311 }
2312
2313 fuse_write_update_size(inode, pos + copied);
2314 set_page_dirty(page);
2315
2316unlock:
2317 unlock_page(page);
2318 put_page(page);
2319
2320 return copied;
2321}
2322
2323static int fuse_launder_page(struct page *page)
2324{
2325 int err = 0;
2326 if (clear_page_dirty_for_io(page)) {
2327 struct inode *inode = page->mapping->host;
2328 err = fuse_writepage_locked(page);
2329 if (!err)
2330 fuse_wait_on_page_writeback(inode, page->index);
2331 }
2332 return err;
2333}
2334
2335/*
2336 * Write back dirty pages now, because there may not be any suitable
2337 * open files later
2338 */
2339static void fuse_vma_close(struct vm_area_struct *vma)
2340{
2341 filemap_write_and_wait(vma->vm_file->f_mapping);
2342}
2343
2344/*
2345 * Wait for writeback against this page to complete before allowing it
2346 * to be marked dirty again, and hence written back again, possibly
2347 * before the previous writepage completed.
2348 *
2349 * Block here, instead of in ->writepage(), so that the userspace fs
2350 * can only block processes actually operating on the filesystem.
2351 *
2352 * Otherwise unprivileged userspace fs would be able to block
2353 * unrelated:
2354 *
2355 * - page migration
2356 * - sync(2)
2357 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2358 */
2359static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2360{
2361 struct page *page = vmf->page;
2362 struct inode *inode = file_inode(vmf->vma->vm_file);
2363
2364 file_update_time(vmf->vma->vm_file);
2365 lock_page(page);
2366 if (page->mapping != inode->i_mapping) {
2367 unlock_page(page);
2368 return VM_FAULT_NOPAGE;
2369 }
2370
2371 fuse_wait_on_page_writeback(inode, page->index);
2372 return VM_FAULT_LOCKED;
2373}
2374
2375static const struct vm_operations_struct fuse_file_vm_ops = {
2376 .close = fuse_vma_close,
2377 .fault = filemap_fault,
2378 .map_pages = filemap_map_pages,
2379 .page_mkwrite = fuse_page_mkwrite,
2380};
2381
2382static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2383{
David Brazdil0f672f62019-12-10 10:32:29 +00002384 struct fuse_file *ff = file->private_data;
2385
Olivier Deprez157378f2022-04-04 15:47:50 +02002386 /* DAX mmap is superior to direct_io mmap */
2387 if (FUSE_IS_DAX(file_inode(file)))
2388 return fuse_dax_mmap(file, vma);
2389
David Brazdil0f672f62019-12-10 10:32:29 +00002390 if (ff->open_flags & FOPEN_DIRECT_IO) {
2391 /* Can't provide the coherency needed for MAP_SHARED */
2392 if (vma->vm_flags & VM_MAYSHARE)
2393 return -ENODEV;
2394
2395 invalidate_inode_pages2(file->f_mapping);
2396
2397 return generic_file_mmap(file, vma);
2398 }
2399
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002400 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2401 fuse_link_write_file(file);
2402
2403 file_accessed(file);
2404 vma->vm_ops = &fuse_file_vm_ops;
2405 return 0;
2406}
2407
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002408static int convert_fuse_file_lock(struct fuse_conn *fc,
2409 const struct fuse_file_lock *ffl,
2410 struct file_lock *fl)
2411{
2412 switch (ffl->type) {
2413 case F_UNLCK:
2414 break;
2415
2416 case F_RDLCK:
2417 case F_WRLCK:
2418 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2419 ffl->end < ffl->start)
2420 return -EIO;
2421
2422 fl->fl_start = ffl->start;
2423 fl->fl_end = ffl->end;
2424
2425 /*
2426 * Convert pid into init's pid namespace. The locks API will
2427 * translate it into the caller's pid namespace.
2428 */
2429 rcu_read_lock();
2430 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2431 rcu_read_unlock();
2432 break;
2433
2434 default:
2435 return -EIO;
2436 }
2437 fl->fl_type = ffl->type;
2438 return 0;
2439}
2440
2441static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2442 const struct file_lock *fl, int opcode, pid_t pid,
2443 int flock, struct fuse_lk_in *inarg)
2444{
2445 struct inode *inode = file_inode(file);
2446 struct fuse_conn *fc = get_fuse_conn(inode);
2447 struct fuse_file *ff = file->private_data;
2448
2449 memset(inarg, 0, sizeof(*inarg));
2450 inarg->fh = ff->fh;
2451 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2452 inarg->lk.start = fl->fl_start;
2453 inarg->lk.end = fl->fl_end;
2454 inarg->lk.type = fl->fl_type;
2455 inarg->lk.pid = pid;
2456 if (flock)
2457 inarg->lk_flags |= FUSE_LK_FLOCK;
David Brazdil0f672f62019-12-10 10:32:29 +00002458 args->opcode = opcode;
2459 args->nodeid = get_node_id(inode);
2460 args->in_numargs = 1;
2461 args->in_args[0].size = sizeof(*inarg);
2462 args->in_args[0].value = inarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002463}
2464
2465static int fuse_getlk(struct file *file, struct file_lock *fl)
2466{
2467 struct inode *inode = file_inode(file);
Olivier Deprez157378f2022-04-04 15:47:50 +02002468 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002469 FUSE_ARGS(args);
2470 struct fuse_lk_in inarg;
2471 struct fuse_lk_out outarg;
2472 int err;
2473
2474 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
David Brazdil0f672f62019-12-10 10:32:29 +00002475 args.out_numargs = 1;
2476 args.out_args[0].size = sizeof(outarg);
2477 args.out_args[0].value = &outarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02002478 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002479 if (!err)
Olivier Deprez157378f2022-04-04 15:47:50 +02002480 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002481
2482 return err;
2483}
2484
2485static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2486{
2487 struct inode *inode = file_inode(file);
Olivier Deprez157378f2022-04-04 15:47:50 +02002488 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002489 FUSE_ARGS(args);
2490 struct fuse_lk_in inarg;
2491 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2492 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02002493 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002494 int err;
2495
2496 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2497 /* NLM needs asynchronous locks, which we don't support yet */
2498 return -ENOLCK;
2499 }
2500
2501 /* Unlock on close is handled by the flush method */
2502 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2503 return 0;
2504
2505 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Olivier Deprez157378f2022-04-04 15:47:50 +02002506 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002507
2508 /* locking is restartable */
2509 if (err == -EINTR)
2510 err = -ERESTARTSYS;
2511
2512 return err;
2513}
2514
2515static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2516{
2517 struct inode *inode = file_inode(file);
2518 struct fuse_conn *fc = get_fuse_conn(inode);
2519 int err;
2520
2521 if (cmd == F_CANCELLK) {
2522 err = 0;
2523 } else if (cmd == F_GETLK) {
2524 if (fc->no_lock) {
2525 posix_test_lock(file, fl);
2526 err = 0;
2527 } else
2528 err = fuse_getlk(file, fl);
2529 } else {
2530 if (fc->no_lock)
2531 err = posix_lock_file(file, fl, NULL);
2532 else
2533 err = fuse_setlk(file, fl, 0);
2534 }
2535 return err;
2536}
2537
2538static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2539{
2540 struct inode *inode = file_inode(file);
2541 struct fuse_conn *fc = get_fuse_conn(inode);
2542 int err;
2543
2544 if (fc->no_flock) {
2545 err = locks_lock_file_wait(file, fl);
2546 } else {
2547 struct fuse_file *ff = file->private_data;
2548
2549 /* emulate flock with POSIX locks */
2550 ff->flock = true;
2551 err = fuse_setlk(file, fl, 1);
2552 }
2553
2554 return err;
2555}
2556
2557static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2558{
2559 struct inode *inode = mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +02002560 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002561 FUSE_ARGS(args);
2562 struct fuse_bmap_in inarg;
2563 struct fuse_bmap_out outarg;
2564 int err;
2565
Olivier Deprez157378f2022-04-04 15:47:50 +02002566 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002567 return 0;
2568
2569 memset(&inarg, 0, sizeof(inarg));
2570 inarg.block = block;
2571 inarg.blocksize = inode->i_sb->s_blocksize;
David Brazdil0f672f62019-12-10 10:32:29 +00002572 args.opcode = FUSE_BMAP;
2573 args.nodeid = get_node_id(inode);
2574 args.in_numargs = 1;
2575 args.in_args[0].size = sizeof(inarg);
2576 args.in_args[0].value = &inarg;
2577 args.out_numargs = 1;
2578 args.out_args[0].size = sizeof(outarg);
2579 args.out_args[0].value = &outarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02002580 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002581 if (err == -ENOSYS)
Olivier Deprez157378f2022-04-04 15:47:50 +02002582 fm->fc->no_bmap = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002583
2584 return err ? 0 : outarg.block;
2585}
2586
2587static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2588{
2589 struct inode *inode = file->f_mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +02002590 struct fuse_mount *fm = get_fuse_mount(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002591 struct fuse_file *ff = file->private_data;
2592 FUSE_ARGS(args);
2593 struct fuse_lseek_in inarg = {
2594 .fh = ff->fh,
2595 .offset = offset,
2596 .whence = whence
2597 };
2598 struct fuse_lseek_out outarg;
2599 int err;
2600
Olivier Deprez157378f2022-04-04 15:47:50 +02002601 if (fm->fc->no_lseek)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002602 goto fallback;
2603
David Brazdil0f672f62019-12-10 10:32:29 +00002604 args.opcode = FUSE_LSEEK;
2605 args.nodeid = ff->nodeid;
2606 args.in_numargs = 1;
2607 args.in_args[0].size = sizeof(inarg);
2608 args.in_args[0].value = &inarg;
2609 args.out_numargs = 1;
2610 args.out_args[0].size = sizeof(outarg);
2611 args.out_args[0].value = &outarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02002612 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002613 if (err) {
2614 if (err == -ENOSYS) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002615 fm->fc->no_lseek = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002616 goto fallback;
2617 }
2618 return err;
2619 }
2620
2621 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2622
2623fallback:
2624 err = fuse_update_attributes(inode, file);
2625 if (!err)
2626 return generic_file_llseek(file, offset, whence);
2627 else
2628 return err;
2629}
2630
2631static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2632{
2633 loff_t retval;
2634 struct inode *inode = file_inode(file);
2635
2636 switch (whence) {
2637 case SEEK_SET:
2638 case SEEK_CUR:
2639 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2640 retval = generic_file_llseek(file, offset, whence);
2641 break;
2642 case SEEK_END:
2643 inode_lock(inode);
2644 retval = fuse_update_attributes(inode, file);
2645 if (!retval)
2646 retval = generic_file_llseek(file, offset, whence);
2647 inode_unlock(inode);
2648 break;
2649 case SEEK_HOLE:
2650 case SEEK_DATA:
2651 inode_lock(inode);
2652 retval = fuse_lseek(file, offset, whence);
2653 inode_unlock(inode);
2654 break;
2655 default:
2656 retval = -EINVAL;
2657 }
2658
2659 return retval;
2660}
2661
2662/*
2663 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2664 * ABI was defined to be 'struct iovec' which is different on 32bit
2665 * and 64bit. Fortunately we can determine which structure the server
2666 * used from the size of the reply.
2667 */
2668static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2669 size_t transferred, unsigned count,
2670 bool is_compat)
2671{
2672#ifdef CONFIG_COMPAT
2673 if (count * sizeof(struct compat_iovec) == transferred) {
2674 struct compat_iovec *ciov = src;
2675 unsigned i;
2676
2677 /*
2678 * With this interface a 32bit server cannot support
2679 * non-compat (i.e. ones coming from 64bit apps) ioctl
2680 * requests
2681 */
2682 if (!is_compat)
2683 return -EINVAL;
2684
2685 for (i = 0; i < count; i++) {
2686 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2687 dst[i].iov_len = ciov[i].iov_len;
2688 }
2689 return 0;
2690 }
2691#endif
2692
2693 if (count * sizeof(struct iovec) != transferred)
2694 return -EIO;
2695
2696 memcpy(dst, src, transferred);
2697 return 0;
2698}
2699
2700/* Make sure iov_length() won't overflow */
David Brazdil0f672f62019-12-10 10:32:29 +00002701static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2702 size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002703{
2704 size_t n;
David Brazdil0f672f62019-12-10 10:32:29 +00002705 u32 max = fc->max_pages << PAGE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002706
2707 for (n = 0; n < count; n++, iov++) {
2708 if (iov->iov_len > (size_t) max)
2709 return -ENOMEM;
2710 max -= iov->iov_len;
2711 }
2712 return 0;
2713}
2714
2715static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2716 void *src, size_t transferred, unsigned count,
2717 bool is_compat)
2718{
2719 unsigned i;
2720 struct fuse_ioctl_iovec *fiov = src;
2721
2722 if (fc->minor < 16) {
2723 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2724 count, is_compat);
2725 }
2726
2727 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2728 return -EIO;
2729
2730 for (i = 0; i < count; i++) {
2731 /* Did the server supply an inappropriate value? */
2732 if (fiov[i].base != (unsigned long) fiov[i].base ||
2733 fiov[i].len != (unsigned long) fiov[i].len)
2734 return -EIO;
2735
2736 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2737 dst[i].iov_len = (size_t) fiov[i].len;
2738
2739#ifdef CONFIG_COMPAT
2740 if (is_compat &&
2741 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2742 (compat_size_t) dst[i].iov_len != fiov[i].len))
2743 return -EIO;
2744#endif
2745 }
2746
2747 return 0;
2748}
2749
2750
2751/*
2752 * For ioctls, there is no generic way to determine how much memory
2753 * needs to be read and/or written. Furthermore, ioctls are allowed
2754 * to dereference the passed pointer, so the parameter requires deep
2755 * copying but FUSE has no idea whatsoever about what to copy in or
2756 * out.
2757 *
2758 * This is solved by allowing FUSE server to retry ioctl with
2759 * necessary in/out iovecs. Let's assume the ioctl implementation
2760 * needs to read in the following structure.
2761 *
2762 * struct a {
2763 * char *buf;
2764 * size_t buflen;
2765 * }
2766 *
2767 * On the first callout to FUSE server, inarg->in_size and
2768 * inarg->out_size will be NULL; then, the server completes the ioctl
2769 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2770 * the actual iov array to
2771 *
2772 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2773 *
2774 * which tells FUSE to copy in the requested area and retry the ioctl.
2775 * On the second round, the server has access to the structure and
2776 * from that it can tell what to look for next, so on the invocation,
2777 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2778 *
2779 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2780 * { .iov_base = a.buf, .iov_len = a.buflen } }
2781 *
2782 * FUSE will copy both struct a and the pointed buffer from the
2783 * process doing the ioctl and retry ioctl with both struct a and the
2784 * buffer.
2785 *
2786 * This time, FUSE server has everything it needs and completes ioctl
2787 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2788 *
2789 * Copying data out works the same way.
2790 *
2791 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2792 * automatically initializes in and out iovs by decoding @cmd with
2793 * _IOC_* macros and the server is not allowed to request RETRY. This
2794 * limits ioctl data transfers to well-formed ioctls and is the forced
2795 * behavior for all FUSE servers.
2796 */
2797long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2798 unsigned int flags)
2799{
2800 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02002801 struct fuse_mount *fm = ff->fm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002802 struct fuse_ioctl_in inarg = {
2803 .fh = ff->fh,
2804 .cmd = cmd,
2805 .arg = arg,
2806 .flags = flags
2807 };
2808 struct fuse_ioctl_out outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002809 struct iovec *iov_page = NULL;
2810 struct iovec *in_iov = NULL, *out_iov = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00002811 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2812 size_t in_size, out_size, c;
2813 ssize_t transferred;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002814 int err, i;
2815 struct iov_iter ii;
David Brazdil0f672f62019-12-10 10:32:29 +00002816 struct fuse_args_pages ap = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002817
2818#if BITS_PER_LONG == 32
2819 inarg.flags |= FUSE_IOCTL_32BIT;
2820#else
David Brazdil0f672f62019-12-10 10:32:29 +00002821 if (flags & FUSE_IOCTL_COMPAT) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002822 inarg.flags |= FUSE_IOCTL_32BIT;
David Brazdil0f672f62019-12-10 10:32:29 +00002823#ifdef CONFIG_X86_X32
2824 if (in_x32_syscall())
2825 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2826#endif
2827 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002828#endif
2829
2830 /* assume all the iovs returned by client always fits in a page */
2831 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2832
2833 err = -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +02002834 ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002835 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +00002836 if (!ap.pages || !iov_page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002837 goto out;
2838
Olivier Deprez157378f2022-04-04 15:47:50 +02002839 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
David Brazdil0f672f62019-12-10 10:32:29 +00002840
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002841 /*
2842 * If restricted, initialize IO parameters as encoded in @cmd.
2843 * RETRY from server is not allowed.
2844 */
2845 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2846 struct iovec *iov = iov_page;
2847
2848 iov->iov_base = (void __user *)arg;
Olivier Deprez0e641232021-09-23 10:07:05 +02002849
2850 switch (cmd) {
2851 case FS_IOC_GETFLAGS:
2852 case FS_IOC_SETFLAGS:
2853 iov->iov_len = sizeof(int);
2854 break;
2855 default:
2856 iov->iov_len = _IOC_SIZE(cmd);
2857 break;
2858 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002859
2860 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2861 in_iov = iov;
2862 in_iovs = 1;
2863 }
2864
2865 if (_IOC_DIR(cmd) & _IOC_READ) {
2866 out_iov = iov;
2867 out_iovs = 1;
2868 }
2869 }
2870
2871 retry:
2872 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2873 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2874
2875 /*
2876 * Out data can be used either for actual out data or iovs,
2877 * make sure there always is at least one page.
2878 */
2879 out_size = max_t(size_t, out_size, PAGE_SIZE);
2880 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2881
2882 /* make sure there are enough buffer pages and init request with them */
2883 err = -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +02002884 if (max_pages > fm->fc->max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002885 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00002886 while (ap.num_pages < max_pages) {
2887 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2888 if (!ap.pages[ap.num_pages])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002889 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00002890 ap.num_pages++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002891 }
2892
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002893
2894 /* okay, let's send it to the client */
David Brazdil0f672f62019-12-10 10:32:29 +00002895 ap.args.opcode = FUSE_IOCTL;
2896 ap.args.nodeid = ff->nodeid;
2897 ap.args.in_numargs = 1;
2898 ap.args.in_args[0].size = sizeof(inarg);
2899 ap.args.in_args[0].value = &inarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002900 if (in_size) {
David Brazdil0f672f62019-12-10 10:32:29 +00002901 ap.args.in_numargs++;
2902 ap.args.in_args[1].size = in_size;
2903 ap.args.in_pages = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002904
2905 err = -EFAULT;
2906 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
David Brazdil0f672f62019-12-10 10:32:29 +00002907 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2908 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002909 if (c != PAGE_SIZE && iov_iter_count(&ii))
2910 goto out;
2911 }
2912 }
2913
David Brazdil0f672f62019-12-10 10:32:29 +00002914 ap.args.out_numargs = 2;
2915 ap.args.out_args[0].size = sizeof(outarg);
2916 ap.args.out_args[0].value = &outarg;
2917 ap.args.out_args[1].size = out_size;
2918 ap.args.out_pages = true;
2919 ap.args.out_argvar = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002920
Olivier Deprez157378f2022-04-04 15:47:50 +02002921 transferred = fuse_simple_request(fm, &ap.args);
David Brazdil0f672f62019-12-10 10:32:29 +00002922 err = transferred;
2923 if (transferred < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002924 goto out;
2925
2926 /* did it ask for retry? */
2927 if (outarg.flags & FUSE_IOCTL_RETRY) {
2928 void *vaddr;
2929
2930 /* no retry if in restricted mode */
2931 err = -EIO;
2932 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2933 goto out;
2934
2935 in_iovs = outarg.in_iovs;
2936 out_iovs = outarg.out_iovs;
2937
2938 /*
2939 * Make sure things are in boundary, separate checks
2940 * are to protect against overflow.
2941 */
2942 err = -ENOMEM;
2943 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2944 out_iovs > FUSE_IOCTL_MAX_IOV ||
2945 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2946 goto out;
2947
David Brazdil0f672f62019-12-10 10:32:29 +00002948 vaddr = kmap_atomic(ap.pages[0]);
Olivier Deprez157378f2022-04-04 15:47:50 +02002949 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002950 transferred, in_iovs + out_iovs,
2951 (flags & FUSE_IOCTL_COMPAT) != 0);
2952 kunmap_atomic(vaddr);
2953 if (err)
2954 goto out;
2955
2956 in_iov = iov_page;
2957 out_iov = in_iov + in_iovs;
2958
Olivier Deprez157378f2022-04-04 15:47:50 +02002959 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002960 if (err)
2961 goto out;
2962
Olivier Deprez157378f2022-04-04 15:47:50 +02002963 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002964 if (err)
2965 goto out;
2966
2967 goto retry;
2968 }
2969
2970 err = -EIO;
2971 if (transferred > inarg.out_size)
2972 goto out;
2973
2974 err = -EFAULT;
2975 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
David Brazdil0f672f62019-12-10 10:32:29 +00002976 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2977 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002978 if (c != PAGE_SIZE && iov_iter_count(&ii))
2979 goto out;
2980 }
2981 err = 0;
2982 out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002983 free_page((unsigned long) iov_page);
David Brazdil0f672f62019-12-10 10:32:29 +00002984 while (ap.num_pages)
2985 __free_page(ap.pages[--ap.num_pages]);
2986 kfree(ap.pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002987
2988 return err ? err : outarg.result;
2989}
2990EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2991
2992long fuse_ioctl_common(struct file *file, unsigned int cmd,
2993 unsigned long arg, unsigned int flags)
2994{
2995 struct inode *inode = file_inode(file);
2996 struct fuse_conn *fc = get_fuse_conn(inode);
2997
2998 if (!fuse_allow_current_process(fc))
2999 return -EACCES;
3000
Olivier Deprez0e641232021-09-23 10:07:05 +02003001 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003002 return -EIO;
3003
3004 return fuse_do_ioctl(file, cmd, arg, flags);
3005}
3006
3007static long fuse_file_ioctl(struct file *file, unsigned int cmd,
3008 unsigned long arg)
3009{
3010 return fuse_ioctl_common(file, cmd, arg, 0);
3011}
3012
3013static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
3014 unsigned long arg)
3015{
3016 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
3017}
3018
3019/*
3020 * All files which have been polled are linked to RB tree
3021 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
3022 * find the matching one.
3023 */
3024static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
3025 struct rb_node **parent_out)
3026{
3027 struct rb_node **link = &fc->polled_files.rb_node;
3028 struct rb_node *last = NULL;
3029
3030 while (*link) {
3031 struct fuse_file *ff;
3032
3033 last = *link;
3034 ff = rb_entry(last, struct fuse_file, polled_node);
3035
3036 if (kh < ff->kh)
3037 link = &last->rb_left;
3038 else if (kh > ff->kh)
3039 link = &last->rb_right;
3040 else
3041 return link;
3042 }
3043
3044 if (parent_out)
3045 *parent_out = last;
3046 return link;
3047}
3048
3049/*
3050 * The file is about to be polled. Make sure it's on the polled_files
3051 * RB tree. Note that files once added to the polled_files tree are
3052 * not removed before the file is released. This is because a file
3053 * polled once is likely to be polled again.
3054 */
3055static void fuse_register_polled_file(struct fuse_conn *fc,
3056 struct fuse_file *ff)
3057{
3058 spin_lock(&fc->lock);
3059 if (RB_EMPTY_NODE(&ff->polled_node)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003060 struct rb_node **link, *parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003061
3062 link = fuse_find_polled_node(fc, ff->kh, &parent);
3063 BUG_ON(*link);
3064 rb_link_node(&ff->polled_node, parent, link);
3065 rb_insert_color(&ff->polled_node, &fc->polled_files);
3066 }
3067 spin_unlock(&fc->lock);
3068}
3069
3070__poll_t fuse_file_poll(struct file *file, poll_table *wait)
3071{
3072 struct fuse_file *ff = file->private_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02003073 struct fuse_mount *fm = ff->fm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003074 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
3075 struct fuse_poll_out outarg;
3076 FUSE_ARGS(args);
3077 int err;
3078
Olivier Deprez157378f2022-04-04 15:47:50 +02003079 if (fm->fc->no_poll)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003080 return DEFAULT_POLLMASK;
3081
3082 poll_wait(file, &ff->poll_wait, wait);
3083 inarg.events = mangle_poll(poll_requested_events(wait));
3084
3085 /*
3086 * Ask for notification iff there's someone waiting for it.
3087 * The client may ignore the flag and always notify.
3088 */
3089 if (waitqueue_active(&ff->poll_wait)) {
3090 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
Olivier Deprez157378f2022-04-04 15:47:50 +02003091 fuse_register_polled_file(fm->fc, ff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003092 }
3093
David Brazdil0f672f62019-12-10 10:32:29 +00003094 args.opcode = FUSE_POLL;
3095 args.nodeid = ff->nodeid;
3096 args.in_numargs = 1;
3097 args.in_args[0].size = sizeof(inarg);
3098 args.in_args[0].value = &inarg;
3099 args.out_numargs = 1;
3100 args.out_args[0].size = sizeof(outarg);
3101 args.out_args[0].value = &outarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02003102 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003103
3104 if (!err)
3105 return demangle_poll(outarg.revents);
3106 if (err == -ENOSYS) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003107 fm->fc->no_poll = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003108 return DEFAULT_POLLMASK;
3109 }
3110 return EPOLLERR;
3111}
3112EXPORT_SYMBOL_GPL(fuse_file_poll);
3113
3114/*
3115 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3116 * wakes up the poll waiters.
3117 */
3118int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3119 struct fuse_notify_poll_wakeup_out *outarg)
3120{
3121 u64 kh = outarg->kh;
3122 struct rb_node **link;
3123
3124 spin_lock(&fc->lock);
3125
3126 link = fuse_find_polled_node(fc, kh, NULL);
3127 if (*link) {
3128 struct fuse_file *ff;
3129
3130 ff = rb_entry(*link, struct fuse_file, polled_node);
3131 wake_up_interruptible_sync(&ff->poll_wait);
3132 }
3133
3134 spin_unlock(&fc->lock);
3135 return 0;
3136}
3137
3138static void fuse_do_truncate(struct file *file)
3139{
3140 struct inode *inode = file->f_mapping->host;
3141 struct iattr attr;
3142
3143 attr.ia_valid = ATTR_SIZE;
3144 attr.ia_size = i_size_read(inode);
3145
3146 attr.ia_file = file;
3147 attr.ia_valid |= ATTR_FILE;
3148
3149 fuse_do_setattr(file_dentry(file), &attr, file);
3150}
3151
David Brazdil0f672f62019-12-10 10:32:29 +00003152static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003153{
David Brazdil0f672f62019-12-10 10:32:29 +00003154 return round_up(off, fc->max_pages << PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003155}
3156
3157static ssize_t
3158fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3159{
3160 DECLARE_COMPLETION_ONSTACK(wait);
3161 ssize_t ret = 0;
3162 struct file *file = iocb->ki_filp;
3163 struct fuse_file *ff = file->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003164 loff_t pos = 0;
3165 struct inode *inode;
3166 loff_t i_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02003167 size_t count = iov_iter_count(iter), shortened = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003168 loff_t offset = iocb->ki_pos;
3169 struct fuse_io_priv *io;
3170
3171 pos = offset;
3172 inode = file->f_mapping->host;
3173 i_size = i_size_read(inode);
3174
Olivier Deprez0e641232021-09-23 10:07:05 +02003175 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003176 return 0;
3177
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003178 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3179 if (!io)
3180 return -ENOMEM;
3181 spin_lock_init(&io->lock);
3182 kref_init(&io->refcnt);
3183 io->reqs = 1;
3184 io->bytes = -1;
3185 io->size = 0;
3186 io->offset = offset;
3187 io->write = (iov_iter_rw(iter) == WRITE);
3188 io->err = 0;
3189 /*
3190 * By default, we want to optimize all I/Os with async request
3191 * submission to the client filesystem if supported.
3192 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003193 io->async = ff->fm->fc->async_dio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003194 io->iocb = iocb;
3195 io->blocking = is_sync_kiocb(iocb);
3196
Olivier Deprez0e641232021-09-23 10:07:05 +02003197 /* optimization for short read */
3198 if (io->async && !io->write && offset + count > i_size) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003199 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
Olivier Deprez0e641232021-09-23 10:07:05 +02003200 shortened = count - iov_iter_count(iter);
3201 count -= shortened;
3202 }
3203
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003204 /*
3205 * We cannot asynchronously extend the size of a file.
3206 * In such case the aio will behave exactly like sync io.
3207 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003208 if ((offset + count > i_size) && io->write)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003209 io->blocking = true;
3210
3211 if (io->async && io->blocking) {
3212 /*
3213 * Additional reference to keep io around after
3214 * calling fuse_aio_complete()
3215 */
3216 kref_get(&io->refcnt);
3217 io->done = &wait;
3218 }
3219
3220 if (iov_iter_rw(iter) == WRITE) {
3221 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3222 fuse_invalidate_attr(inode);
3223 } else {
3224 ret = __fuse_direct_read(io, iter, &pos);
3225 }
Olivier Deprez0e641232021-09-23 10:07:05 +02003226 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003227
3228 if (io->async) {
3229 bool blocking = io->blocking;
3230
3231 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3232
3233 /* we have a non-extending, async request, so return */
3234 if (!blocking)
3235 return -EIOCBQUEUED;
3236
3237 wait_for_completion(&wait);
3238 ret = fuse_get_res_by_io(io);
3239 }
3240
3241 kref_put(&io->refcnt, fuse_io_release);
3242
3243 if (iov_iter_rw(iter) == WRITE) {
3244 if (ret > 0)
3245 fuse_write_update_size(inode, pos);
3246 else if (ret < 0 && offset + count > i_size)
3247 fuse_do_truncate(file);
3248 }
3249
3250 return ret;
3251}
3252
David Brazdil0f672f62019-12-10 10:32:29 +00003253static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3254{
Olivier Deprez157378f2022-04-04 15:47:50 +02003255 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
David Brazdil0f672f62019-12-10 10:32:29 +00003256
3257 if (!err)
3258 fuse_sync_writes(inode);
3259
3260 return err;
3261}
3262
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003263static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3264 loff_t length)
3265{
3266 struct fuse_file *ff = file->private_data;
3267 struct inode *inode = file_inode(file);
3268 struct fuse_inode *fi = get_fuse_inode(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02003269 struct fuse_mount *fm = ff->fm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003270 FUSE_ARGS(args);
3271 struct fuse_fallocate_in inarg = {
3272 .fh = ff->fh,
3273 .offset = offset,
3274 .length = length,
3275 .mode = mode
3276 };
3277 int err;
3278 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3279 (mode & FALLOC_FL_PUNCH_HOLE);
3280
Olivier Deprez157378f2022-04-04 15:47:50 +02003281 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
3282
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003283 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3284 return -EOPNOTSUPP;
3285
Olivier Deprez157378f2022-04-04 15:47:50 +02003286 if (fm->fc->no_fallocate)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003287 return -EOPNOTSUPP;
3288
3289 if (lock_inode) {
3290 inode_lock(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02003291 if (block_faults) {
3292 down_write(&fi->i_mmap_sem);
3293 err = fuse_dax_break_layouts(inode, 0, 0);
3294 if (err)
3295 goto out;
3296 }
3297
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003298 if (mode & FALLOC_FL_PUNCH_HOLE) {
3299 loff_t endbyte = offset + length - 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003300
3301 err = fuse_writeback_range(inode, offset, endbyte);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003302 if (err)
3303 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003304 }
3305 }
3306
David Brazdil0f672f62019-12-10 10:32:29 +00003307 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3308 offset + length > i_size_read(inode)) {
3309 err = inode_newsize_ok(inode, offset + length);
3310 if (err)
3311 goto out;
3312 }
3313
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003314 if (!(mode & FALLOC_FL_KEEP_SIZE))
3315 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3316
David Brazdil0f672f62019-12-10 10:32:29 +00003317 args.opcode = FUSE_FALLOCATE;
3318 args.nodeid = ff->nodeid;
3319 args.in_numargs = 1;
3320 args.in_args[0].size = sizeof(inarg);
3321 args.in_args[0].value = &inarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02003322 err = fuse_simple_request(fm, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003323 if (err == -ENOSYS) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003324 fm->fc->no_fallocate = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003325 err = -EOPNOTSUPP;
3326 }
3327 if (err)
3328 goto out;
3329
3330 /* we could have extended the file */
3331 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3332 bool changed = fuse_write_update_size(inode, offset + length);
3333
Olivier Deprez157378f2022-04-04 15:47:50 +02003334 if (changed && fm->fc->writeback_cache)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003335 file_update_time(file);
3336 }
3337
3338 if (mode & FALLOC_FL_PUNCH_HOLE)
3339 truncate_pagecache_range(inode, offset, offset + length - 1);
3340
3341 fuse_invalidate_attr(inode);
3342
3343out:
3344 if (!(mode & FALLOC_FL_KEEP_SIZE))
3345 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3346
Olivier Deprez157378f2022-04-04 15:47:50 +02003347 if (block_faults)
3348 up_write(&fi->i_mmap_sem);
3349
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003350 if (lock_inode)
3351 inode_unlock(inode);
3352
Olivier Deprez157378f2022-04-04 15:47:50 +02003353 fuse_flush_time_update(inode);
3354
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003355 return err;
3356}
3357
David Brazdil0f672f62019-12-10 10:32:29 +00003358static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3359 struct file *file_out, loff_t pos_out,
3360 size_t len, unsigned int flags)
3361{
3362 struct fuse_file *ff_in = file_in->private_data;
3363 struct fuse_file *ff_out = file_out->private_data;
3364 struct inode *inode_in = file_inode(file_in);
3365 struct inode *inode_out = file_inode(file_out);
3366 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
Olivier Deprez157378f2022-04-04 15:47:50 +02003367 struct fuse_mount *fm = ff_in->fm;
3368 struct fuse_conn *fc = fm->fc;
David Brazdil0f672f62019-12-10 10:32:29 +00003369 FUSE_ARGS(args);
3370 struct fuse_copy_file_range_in inarg = {
3371 .fh_in = ff_in->fh,
3372 .off_in = pos_in,
3373 .nodeid_out = ff_out->nodeid,
3374 .fh_out = ff_out->fh,
3375 .off_out = pos_out,
3376 .len = len,
3377 .flags = flags
3378 };
3379 struct fuse_write_out outarg;
3380 ssize_t err;
3381 /* mark unstable when write-back is not used, and file_out gets
3382 * extended */
3383 bool is_unstable = (!fc->writeback_cache) &&
3384 ((pos_out + len) > inode_out->i_size);
3385
3386 if (fc->no_copy_file_range)
3387 return -EOPNOTSUPP;
3388
3389 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3390 return -EXDEV;
3391
Olivier Deprez0e641232021-09-23 10:07:05 +02003392 inode_lock(inode_in);
3393 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3394 inode_unlock(inode_in);
3395 if (err)
3396 return err;
David Brazdil0f672f62019-12-10 10:32:29 +00003397
3398 inode_lock(inode_out);
3399
3400 err = file_modified(file_out);
3401 if (err)
3402 goto out;
3403
Olivier Deprez0e641232021-09-23 10:07:05 +02003404 /*
3405 * Write out dirty pages in the destination file before sending the COPY
3406 * request to userspace. After the request is completed, truncate off
3407 * pages (including partial ones) from the cache that have been copied,
3408 * since these contain stale data at that point.
3409 *
3410 * This should be mostly correct, but if the COPY writes to partial
3411 * pages (at the start or end) and the parts not covered by the COPY are
3412 * written through a memory map after calling fuse_writeback_range(),
3413 * then these partial page modifications will be lost on truncation.
3414 *
3415 * It is unlikely that someone would rely on such mixed style
3416 * modifications. Yet this does give less guarantees than if the
3417 * copying was performed with write(2).
3418 *
3419 * To fix this a i_mmap_sem style lock could be used to prevent new
3420 * faults while the copy is ongoing.
3421 */
3422 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3423 if (err)
3424 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00003425
3426 if (is_unstable)
3427 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3428
3429 args.opcode = FUSE_COPY_FILE_RANGE;
3430 args.nodeid = ff_in->nodeid;
3431 args.in_numargs = 1;
3432 args.in_args[0].size = sizeof(inarg);
3433 args.in_args[0].value = &inarg;
3434 args.out_numargs = 1;
3435 args.out_args[0].size = sizeof(outarg);
3436 args.out_args[0].value = &outarg;
Olivier Deprez157378f2022-04-04 15:47:50 +02003437 err = fuse_simple_request(fm, &args);
David Brazdil0f672f62019-12-10 10:32:29 +00003438 if (err == -ENOSYS) {
3439 fc->no_copy_file_range = 1;
3440 err = -EOPNOTSUPP;
3441 }
3442 if (err)
3443 goto out;
3444
Olivier Deprez0e641232021-09-23 10:07:05 +02003445 truncate_inode_pages_range(inode_out->i_mapping,
3446 ALIGN_DOWN(pos_out, PAGE_SIZE),
3447 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3448
David Brazdil0f672f62019-12-10 10:32:29 +00003449 if (fc->writeback_cache) {
3450 fuse_write_update_size(inode_out, pos_out + outarg.size);
3451 file_update_time(file_out);
3452 }
3453
3454 fuse_invalidate_attr(inode_out);
3455
3456 err = outarg.size;
3457out:
3458 if (is_unstable)
3459 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3460
3461 inode_unlock(inode_out);
3462 file_accessed(file_in);
3463
Olivier Deprez157378f2022-04-04 15:47:50 +02003464 fuse_flush_time_update(inode_out);
3465
David Brazdil0f672f62019-12-10 10:32:29 +00003466 return err;
3467}
3468
3469static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3470 struct file *dst_file, loff_t dst_off,
3471 size_t len, unsigned int flags)
3472{
3473 ssize_t ret;
3474
3475 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3476 len, flags);
3477
3478 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3479 ret = generic_copy_file_range(src_file, src_off, dst_file,
3480 dst_off, len, flags);
3481 return ret;
3482}
3483
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003484static const struct file_operations fuse_file_operations = {
3485 .llseek = fuse_file_llseek,
3486 .read_iter = fuse_file_read_iter,
3487 .write_iter = fuse_file_write_iter,
3488 .mmap = fuse_file_mmap,
3489 .open = fuse_open,
3490 .flush = fuse_flush,
3491 .release = fuse_release,
3492 .fsync = fuse_fsync,
3493 .lock = fuse_file_lock,
Olivier Deprez157378f2022-04-04 15:47:50 +02003494 .get_unmapped_area = thp_get_unmapped_area,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003495 .flock = fuse_file_flock,
3496 .splice_read = generic_file_splice_read,
David Brazdil0f672f62019-12-10 10:32:29 +00003497 .splice_write = iter_file_splice_write,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003498 .unlocked_ioctl = fuse_file_ioctl,
3499 .compat_ioctl = fuse_file_compat_ioctl,
3500 .poll = fuse_file_poll,
3501 .fallocate = fuse_file_fallocate,
David Brazdil0f672f62019-12-10 10:32:29 +00003502 .copy_file_range = fuse_copy_file_range,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003503};
3504
3505static const struct address_space_operations fuse_file_aops = {
3506 .readpage = fuse_readpage,
Olivier Deprez157378f2022-04-04 15:47:50 +02003507 .readahead = fuse_readahead,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003508 .writepage = fuse_writepage,
3509 .writepages = fuse_writepages,
3510 .launder_page = fuse_launder_page,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003511 .set_page_dirty = __set_page_dirty_nobuffers,
3512 .bmap = fuse_bmap,
3513 .direct_IO = fuse_direct_IO,
3514 .write_begin = fuse_write_begin,
3515 .write_end = fuse_write_end,
3516};
3517
3518void fuse_init_file_inode(struct inode *inode)
3519{
David Brazdil0f672f62019-12-10 10:32:29 +00003520 struct fuse_inode *fi = get_fuse_inode(inode);
3521
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003522 inode->i_fop = &fuse_file_operations;
3523 inode->i_data.a_ops = &fuse_file_aops;
David Brazdil0f672f62019-12-10 10:32:29 +00003524
3525 INIT_LIST_HEAD(&fi->write_files);
3526 INIT_LIST_HEAD(&fi->queued_writes);
3527 fi->writectr = 0;
3528 init_waitqueue_head(&fi->page_waitq);
Olivier Deprez157378f2022-04-04 15:47:50 +02003529 fi->writepages = RB_ROOT;
3530
3531 if (IS_ENABLED(CONFIG_FUSE_DAX))
3532 fuse_dax_inode_init(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003533}