blob: 1de59998e0e757a134295fc082aecb6d3a82a7c6 [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
35static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
36 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);
43 if (!fc->atomic_o_trunc)
44 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
54 return fuse_simple_request(fc, &args);
55}
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
64{
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
71 ff->fc = fc;
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
David Brazdil0f672f62019-12-10 10:32:29 +000085 ff->kh = atomic64_inc_return(&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
David Brazdil0f672f62019-12-10 10:32:29 +0000103static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
104 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
David Brazdil0f672f62019-12-10 10:32:29 +0000117 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
118 /* Do nothing when client does not implement 'open' */
119 fuse_release_end(ff->fc, args, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 } else if (sync) {
David Brazdil0f672f62019-12-10 10:32:29 +0000121 fuse_simple_request(ff->fc, args);
122 fuse_release_end(ff->fc, args, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000124 args->end = fuse_release_end;
125 if (fuse_simple_background(ff->fc, args,
126 GFP_KERNEL | __GFP_NOFAIL))
127 fuse_release_end(ff->fc, args, -ENOTCONN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 }
129 kfree(ff);
130 }
131}
132
133int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
134 bool isdir)
135{
136 struct fuse_file *ff;
137 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
138
139 ff = fuse_file_alloc(fc);
140 if (!ff)
141 return -ENOMEM;
142
143 ff->fh = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000144 /* Default for no-open */
145 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
146 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 struct fuse_open_out outarg;
148 int err;
149
150 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
151 if (!err) {
152 ff->fh = outarg.fh;
153 ff->open_flags = outarg.open_flags;
154
David Brazdil0f672f62019-12-10 10:32:29 +0000155 } else if (err != -ENOSYS) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156 fuse_file_free(ff);
157 return err;
158 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000159 if (isdir)
160 fc->no_opendir = 1;
161 else
162 fc->no_open = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163 }
164 }
165
166 if (isdir)
167 ff->open_flags &= ~FOPEN_DIRECT_IO;
168
169 ff->nodeid = nodeid;
170 file->private_data = ff;
171
172 return 0;
173}
174EXPORT_SYMBOL_GPL(fuse_do_open);
175
176static void fuse_link_write_file(struct file *file)
177{
178 struct inode *inode = file_inode(file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179 struct fuse_inode *fi = get_fuse_inode(inode);
180 struct fuse_file *ff = file->private_data;
181 /*
182 * file may be written through mmap, so chain it onto the
183 * inodes's write_file list
184 */
David Brazdil0f672f62019-12-10 10:32:29 +0000185 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186 if (list_empty(&ff->write_entry))
187 list_add(&ff->write_entry, &fi->write_files);
David Brazdil0f672f62019-12-10 10:32:29 +0000188 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189}
190
191void fuse_finish_open(struct inode *inode, struct file *file)
192{
193 struct fuse_file *ff = file->private_data;
194 struct fuse_conn *fc = get_fuse_conn(inode);
195
David Brazdil0f672f62019-12-10 10:32:29 +0000196 if (ff->open_flags & FOPEN_STREAM)
197 stream_open(inode, file);
198 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199 nonseekable_open(inode, file);
Olivier Deprez0e641232021-09-23 10:07:05 +0200200
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 struct fuse_inode *fi = get_fuse_inode(inode);
203
David Brazdil0f672f62019-12-10 10:32:29 +0000204 spin_lock(&fi->lock);
205 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 i_size_write(inode, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000207 spin_unlock(&fi->lock);
Olivier Deprez0e641232021-09-23 10:07:05 +0200208 truncate_pagecache(inode, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209 fuse_invalidate_attr(inode);
210 if (fc->writeback_cache)
211 file_update_time(file);
Olivier Deprez0e641232021-09-23 10:07:05 +0200212 } else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) {
213 invalidate_inode_pages2(inode->i_mapping);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200215
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
217 fuse_link_write_file(file);
218}
219
220int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
221{
222 struct fuse_conn *fc = get_fuse_conn(inode);
223 int err;
David Brazdil0f672f62019-12-10 10:32:29 +0000224 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 fc->atomic_o_trunc &&
226 fc->writeback_cache;
227
Olivier Deprez0e641232021-09-23 10:07:05 +0200228 if (fuse_is_bad(inode))
229 return -EIO;
230
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231 err = generic_file_open(inode, file);
232 if (err)
233 return err;
234
David Brazdil0f672f62019-12-10 10:32:29 +0000235 if (is_wb_truncate) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236 inode_lock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000237 fuse_set_nowrite(inode);
238 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239
240 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
241
242 if (!err)
243 fuse_finish_open(inode, file);
244
David Brazdil0f672f62019-12-10 10:32:29 +0000245 if (is_wb_truncate) {
246 fuse_release_nowrite(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 inode_unlock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000248 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000249
250 return err;
251}
252
David Brazdil0f672f62019-12-10 10:32:29 +0000253static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
254 int flags, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255{
256 struct fuse_conn *fc = ff->fc;
David Brazdil0f672f62019-12-10 10:32:29 +0000257 struct fuse_release_args *ra = ff->release_args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258
David Brazdil0f672f62019-12-10 10:32:29 +0000259 /* Inode is NULL on error path of fuse_create_open() */
260 if (likely(fi)) {
261 spin_lock(&fi->lock);
262 list_del(&ff->write_entry);
263 spin_unlock(&fi->lock);
264 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265 spin_lock(&fc->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 if (!RB_EMPTY_NODE(&ff->polled_node))
267 rb_erase(&ff->polled_node, &fc->polled_files);
268 spin_unlock(&fc->lock);
269
270 wake_up_interruptible_all(&ff->poll_wait);
271
David Brazdil0f672f62019-12-10 10:32:29 +0000272 ra->inarg.fh = ff->fh;
273 ra->inarg.flags = flags;
274 ra->args.in_numargs = 1;
275 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
276 ra->args.in_args[0].value = &ra->inarg;
277 ra->args.opcode = opcode;
278 ra->args.nodeid = ff->nodeid;
279 ra->args.force = true;
280 ra->args.nocreds = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281}
282
283void fuse_release_common(struct file *file, bool isdir)
284{
David Brazdil0f672f62019-12-10 10:32:29 +0000285 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286 struct fuse_file *ff = file->private_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000287 struct fuse_release_args *ra = ff->release_args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291
292 if (ff->flock) {
David Brazdil0f672f62019-12-10 10:32:29 +0000293 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
294 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
295 (fl_owner_t) file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296 }
297 /* Hold inode until release is finished */
David Brazdil0f672f62019-12-10 10:32:29 +0000298 ra->inode = igrab(file_inode(file));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299
300 /*
301 * Normally this will send the RELEASE request, however if
302 * some asynchronous READ or WRITE requests are outstanding,
303 * the sending will be delayed.
304 *
305 * Make the release synchronous if this is a fuseblk mount,
306 * synchronous RELEASE is allowed (and desirable) in this case
307 * because the server can be trusted not to screw up.
308 */
David Brazdil0f672f62019-12-10 10:32:29 +0000309 fuse_file_put(ff, ff->fc->destroy, isdir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310}
311
312static int fuse_open(struct inode *inode, struct file *file)
313{
314 return fuse_open_common(inode, file, false);
315}
316
317static int fuse_release(struct inode *inode, struct file *file)
318{
319 struct fuse_conn *fc = get_fuse_conn(inode);
320
321 /* see fuse_vma_close() for !writeback_cache case */
322 if (fc->writeback_cache)
323 write_inode_now(inode, 1);
324
325 fuse_release_common(file, false);
326
327 /* return value is ignored by VFS */
328 return 0;
329}
330
David Brazdil0f672f62019-12-10 10:32:29 +0000331void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332{
333 WARN_ON(refcount_read(&ff->count) > 1);
David Brazdil0f672f62019-12-10 10:32:29 +0000334 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335 /*
336 * iput(NULL) is a no-op and since the refcount is 1 and everything's
337 * synchronous, we are fine with not doing igrab() here"
338 */
339 fuse_file_put(ff, true, false);
340}
341EXPORT_SYMBOL_GPL(fuse_sync_release);
342
343/*
344 * Scramble the ID space with XTEA, so that the value of the files_struct
345 * pointer is not exposed to userspace.
346 */
347u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
348{
349 u32 *k = fc->scramble_key;
350 u64 v = (unsigned long) id;
351 u32 v0 = v;
352 u32 v1 = v >> 32;
353 u32 sum = 0;
354 int i;
355
356 for (i = 0; i < 32; i++) {
357 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
358 sum += 0x9E3779B9;
359 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
360 }
361
362 return (u64) v0 + ((u64) v1 << 32);
363}
364
David Brazdil0f672f62019-12-10 10:32:29 +0000365struct fuse_writepage_args {
366 struct fuse_io_args ia;
367 struct list_head writepages_entry;
368 struct list_head queue_entry;
369 struct fuse_writepage_args *next;
370 struct inode *inode;
371};
372
373static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
374 pgoff_t idx_from, pgoff_t idx_to)
375{
376 struct fuse_writepage_args *wpa;
377
378 list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
379 pgoff_t curr_index;
380
381 WARN_ON(get_fuse_inode(wpa->inode) != fi);
382 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
383 if (idx_from < curr_index + wpa->ia.ap.num_pages &&
384 curr_index <= idx_to) {
385 return wpa;
386 }
387 }
388 return NULL;
389}
390
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391/*
392 * Check if any page in a range is under writeback
393 *
394 * This is currently done by walking the list of writepage requests
395 * for the inode, which can be pretty inefficient.
396 */
397static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
398 pgoff_t idx_to)
399{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000401 bool found;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402
David Brazdil0f672f62019-12-10 10:32:29 +0000403 spin_lock(&fi->lock);
404 found = fuse_find_writeback(fi, idx_from, idx_to);
405 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406
407 return found;
408}
409
410static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
411{
412 return fuse_range_is_writeback(inode, index, index);
413}
414
415/*
416 * Wait for page writeback to be completed.
417 *
418 * Since fuse doesn't rely on the VM writeback tracking, this has to
419 * use some other means.
420 */
David Brazdil0f672f62019-12-10 10:32:29 +0000421static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422{
423 struct fuse_inode *fi = get_fuse_inode(inode);
424
425 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426}
427
428/*
429 * Wait for all pending writepages on the inode to finish.
430 *
431 * This is currently done by blocking further writes with FUSE_NOWRITE
432 * and waiting for all sent writes to complete.
433 *
434 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
435 * could conflict with truncation.
436 */
437static void fuse_sync_writes(struct inode *inode)
438{
439 fuse_set_nowrite(inode);
440 fuse_release_nowrite(inode);
441}
442
443static int fuse_flush(struct file *file, fl_owner_t id)
444{
445 struct inode *inode = file_inode(file);
446 struct fuse_conn *fc = get_fuse_conn(inode);
447 struct fuse_file *ff = file->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 struct fuse_flush_in inarg;
David Brazdil0f672f62019-12-10 10:32:29 +0000449 FUSE_ARGS(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450 int err;
451
Olivier Deprez0e641232021-09-23 10:07:05 +0200452 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453 return -EIO;
454
455 if (fc->no_flush)
456 return 0;
457
458 err = write_inode_now(inode, 1);
459 if (err)
460 return err;
461
462 inode_lock(inode);
463 fuse_sync_writes(inode);
464 inode_unlock(inode);
465
466 err = filemap_check_errors(file->f_mapping);
467 if (err)
468 return err;
469
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470 memset(&inarg, 0, sizeof(inarg));
471 inarg.fh = ff->fh;
472 inarg.lock_owner = fuse_lock_owner_id(fc, id);
David Brazdil0f672f62019-12-10 10:32:29 +0000473 args.opcode = FUSE_FLUSH;
474 args.nodeid = get_node_id(inode);
475 args.in_numargs = 1;
476 args.in_args[0].size = sizeof(inarg);
477 args.in_args[0].value = &inarg;
478 args.force = true;
479
480 err = fuse_simple_request(fc, &args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000481 if (err == -ENOSYS) {
482 fc->no_flush = 1;
483 err = 0;
484 }
485 return err;
486}
487
488int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
David Brazdil0f672f62019-12-10 10:32:29 +0000489 int datasync, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490{
491 struct inode *inode = file->f_mapping->host;
492 struct fuse_conn *fc = get_fuse_conn(inode);
493 struct fuse_file *ff = file->private_data;
494 FUSE_ARGS(args);
495 struct fuse_fsync_in inarg;
David Brazdil0f672f62019-12-10 10:32:29 +0000496
497 memset(&inarg, 0, sizeof(inarg));
498 inarg.fh = ff->fh;
499 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
500 args.opcode = opcode;
501 args.nodeid = get_node_id(inode);
502 args.in_numargs = 1;
503 args.in_args[0].size = sizeof(inarg);
504 args.in_args[0].value = &inarg;
505 return fuse_simple_request(fc, &args);
506}
507
508static int fuse_fsync(struct file *file, loff_t start, loff_t end,
509 int datasync)
510{
511 struct inode *inode = file->f_mapping->host;
512 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513 int err;
514
Olivier Deprez0e641232021-09-23 10:07:05 +0200515 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516 return -EIO;
517
518 inode_lock(inode);
519
520 /*
521 * Start writeback against all dirty pages of the inode, then
522 * wait for all outstanding writes, before sending the FSYNC
523 * request.
524 */
525 err = file_write_and_wait_range(file, start, end);
526 if (err)
527 goto out;
528
529 fuse_sync_writes(inode);
530
531 /*
532 * Due to implementation of fuse writeback
533 * file_write_and_wait_range() does not catch errors.
534 * We have to do this directly after fuse_sync_writes()
535 */
536 err = file_check_and_advance_wb_err(file);
537 if (err)
538 goto out;
539
540 err = sync_inode_metadata(inode, 1);
541 if (err)
542 goto out;
543
David Brazdil0f672f62019-12-10 10:32:29 +0000544 if (fc->no_fsync)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545 goto out;
546
David Brazdil0f672f62019-12-10 10:32:29 +0000547 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548 if (err == -ENOSYS) {
David Brazdil0f672f62019-12-10 10:32:29 +0000549 fc->no_fsync = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550 err = 0;
551 }
552out:
553 inode_unlock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000554
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000555 return err;
556}
557
David Brazdil0f672f62019-12-10 10:32:29 +0000558void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
559 size_t count, int opcode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000561 struct fuse_file *ff = file->private_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000562 struct fuse_args *args = &ia->ap.args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563
David Brazdil0f672f62019-12-10 10:32:29 +0000564 ia->read.in.fh = ff->fh;
565 ia->read.in.offset = pos;
566 ia->read.in.size = count;
567 ia->read.in.flags = file->f_flags;
568 args->opcode = opcode;
569 args->nodeid = ff->nodeid;
570 args->in_numargs = 1;
571 args->in_args[0].size = sizeof(ia->read.in);
572 args->in_args[0].value = &ia->read.in;
573 args->out_argvar = true;
574 args->out_numargs = 1;
575 args->out_args[0].size = count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576}
577
David Brazdil0f672f62019-12-10 10:32:29 +0000578static void fuse_release_user_pages(struct fuse_args_pages *ap,
579 bool should_dirty)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580{
David Brazdil0f672f62019-12-10 10:32:29 +0000581 unsigned int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582
David Brazdil0f672f62019-12-10 10:32:29 +0000583 for (i = 0; i < ap->num_pages; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 if (should_dirty)
David Brazdil0f672f62019-12-10 10:32:29 +0000585 set_page_dirty_lock(ap->pages[i]);
586 put_page(ap->pages[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587 }
588}
589
590static void fuse_io_release(struct kref *kref)
591{
592 kfree(container_of(kref, struct fuse_io_priv, refcnt));
593}
594
595static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
596{
597 if (io->err)
598 return io->err;
599
600 if (io->bytes >= 0 && io->write)
601 return -EIO;
602
603 return io->bytes < 0 ? io->size : io->bytes;
604}
605
606/**
607 * In case of short read, the caller sets 'pos' to the position of
608 * actual end of fuse request in IO request. Otherwise, if bytes_requested
609 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
610 *
611 * An example:
612 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
613 * both submitted asynchronously. The first of them was ACKed by userspace as
614 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
615 * second request was ACKed as short, e.g. only 1K was read, resulting in
616 * pos == 33K.
617 *
618 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
619 * will be equal to the length of the longest contiguous fragment of
620 * transferred data starting from the beginning of IO request.
621 */
622static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
623{
624 int left;
625
626 spin_lock(&io->lock);
627 if (err)
628 io->err = io->err ? : err;
629 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
630 io->bytes = pos;
631
632 left = --io->reqs;
633 if (!left && io->blocking)
634 complete(io->done);
635 spin_unlock(&io->lock);
636
637 if (!left && !io->blocking) {
638 ssize_t res = fuse_get_res_by_io(io);
639
640 if (res >= 0) {
641 struct inode *inode = file_inode(io->iocb->ki_filp);
642 struct fuse_conn *fc = get_fuse_conn(inode);
643 struct fuse_inode *fi = get_fuse_inode(inode);
644
David Brazdil0f672f62019-12-10 10:32:29 +0000645 spin_lock(&fi->lock);
646 fi->attr_version = atomic64_inc_return(&fc->attr_version);
647 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000648 }
649
650 io->iocb->ki_complete(io->iocb, res, 0);
651 }
652
653 kref_put(&io->refcnt, fuse_io_release);
654}
655
David Brazdil0f672f62019-12-10 10:32:29 +0000656static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
657 unsigned int npages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000658{
David Brazdil0f672f62019-12-10 10:32:29 +0000659 struct fuse_io_args *ia;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660
David Brazdil0f672f62019-12-10 10:32:29 +0000661 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
662 if (ia) {
663 ia->io = io;
664 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
665 &ia->ap.descs);
666 if (!ia->ap.pages) {
667 kfree(ia);
668 ia = NULL;
669 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000670 }
David Brazdil0f672f62019-12-10 10:32:29 +0000671 return ia;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672}
673
David Brazdil0f672f62019-12-10 10:32:29 +0000674static void fuse_io_free(struct fuse_io_args *ia)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675{
David Brazdil0f672f62019-12-10 10:32:29 +0000676 kfree(ia->ap.pages);
677 kfree(ia);
678}
679
680static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
681 int err)
682{
683 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
684 struct fuse_io_priv *io = ia->io;
685 ssize_t pos = -1;
686
687 fuse_release_user_pages(&ia->ap, io->should_dirty);
688
689 if (err) {
690 /* Nothing */
691 } else if (io->write) {
692 if (ia->write.out.size > ia->write.in.size) {
693 err = -EIO;
694 } else if (ia->write.in.size != ia->write.out.size) {
695 pos = ia->write.in.offset - io->offset +
696 ia->write.out.size;
697 }
698 } else {
699 u32 outsize = args->out_args[0].size;
700
701 if (ia->read.in.size != outsize)
702 pos = ia->read.in.offset - io->offset + outsize;
703 }
704
705 fuse_aio_complete(io, err, pos);
706 fuse_io_free(ia);
707}
708
709static ssize_t fuse_async_req_send(struct fuse_conn *fc,
710 struct fuse_io_args *ia, size_t num_bytes)
711{
712 ssize_t err;
713 struct fuse_io_priv *io = ia->io;
714
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000715 spin_lock(&io->lock);
716 kref_get(&io->refcnt);
717 io->size += num_bytes;
718 io->reqs++;
719 spin_unlock(&io->lock);
720
David Brazdil0f672f62019-12-10 10:32:29 +0000721 ia->ap.args.end = fuse_aio_complete_req;
Olivier Deprez0e641232021-09-23 10:07:05 +0200722 ia->ap.args.may_block = io->should_dirty;
David Brazdil0f672f62019-12-10 10:32:29 +0000723 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
Olivier Deprez0e641232021-09-23 10:07:05 +0200724 if (err)
725 fuse_aio_complete_req(fc, &ia->ap.args, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726
Olivier Deprez0e641232021-09-23 10:07:05 +0200727 return num_bytes;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728}
729
David Brazdil0f672f62019-12-10 10:32:29 +0000730static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
731 fl_owner_t owner)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732{
David Brazdil0f672f62019-12-10 10:32:29 +0000733 struct file *file = ia->io->iocb->ki_filp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 struct fuse_file *ff = file->private_data;
735 struct fuse_conn *fc = ff->fc;
736
David Brazdil0f672f62019-12-10 10:32:29 +0000737 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000738 if (owner != NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000739 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
740 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 }
742
David Brazdil0f672f62019-12-10 10:32:29 +0000743 if (ia->io->async)
744 return fuse_async_req_send(fc, ia, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745
David Brazdil0f672f62019-12-10 10:32:29 +0000746 return fuse_simple_request(fc, &ia->ap.args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747}
748
749static void fuse_read_update_size(struct inode *inode, loff_t size,
750 u64 attr_ver)
751{
752 struct fuse_conn *fc = get_fuse_conn(inode);
753 struct fuse_inode *fi = get_fuse_inode(inode);
754
David Brazdil0f672f62019-12-10 10:32:29 +0000755 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000756 if (attr_ver == fi->attr_version && size < inode->i_size &&
757 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000758 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000759 i_size_write(inode, size);
760 }
David Brazdil0f672f62019-12-10 10:32:29 +0000761 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000762}
763
David Brazdil0f672f62019-12-10 10:32:29 +0000764static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
765 struct fuse_args_pages *ap)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000766{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767 struct fuse_conn *fc = get_fuse_conn(inode);
768
769 if (fc->writeback_cache) {
770 /*
771 * A hole in a file. Some data after the hole are in page cache,
772 * but have not reached the client fs yet. So, the hole is not
773 * present there.
774 */
775 int i;
776 int start_idx = num_read >> PAGE_SHIFT;
777 size_t off = num_read & (PAGE_SIZE - 1);
778
David Brazdil0f672f62019-12-10 10:32:29 +0000779 for (i = start_idx; i < ap->num_pages; i++) {
780 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781 off = 0;
782 }
783 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000784 loff_t pos = page_offset(ap->pages[0]) + num_read;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785 fuse_read_update_size(inode, pos, attr_ver);
786 }
787}
788
789static int fuse_do_readpage(struct file *file, struct page *page)
790{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791 struct inode *inode = page->mapping->host;
792 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793 loff_t pos = page_offset(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000794 struct fuse_page_desc desc = { .length = PAGE_SIZE };
795 struct fuse_io_args ia = {
796 .ap.args.page_zeroing = true,
797 .ap.args.out_pages = true,
798 .ap.num_pages = 1,
799 .ap.pages = &page,
800 .ap.descs = &desc,
801 };
802 ssize_t res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 u64 attr_ver;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804
805 /*
806 * Page writeback can extend beyond the lifetime of the
807 * page-cache page, so make sure we read a properly synced
808 * page.
809 */
810 fuse_wait_on_page_writeback(inode, page->index);
811
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812 attr_ver = fuse_get_attr_version(fc);
813
Olivier Deprez0e641232021-09-23 10:07:05 +0200814 /* Don't overflow end offset */
815 if (pos + (desc.length - 1) == LLONG_MAX)
816 desc.length--;
817
David Brazdil0f672f62019-12-10 10:32:29 +0000818 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
819 res = fuse_simple_request(fc, &ia.ap.args);
820 if (res < 0)
821 return res;
822 /*
823 * Short read means EOF. If file size is larger, truncate it
824 */
825 if (res < desc.length)
826 fuse_short_read(inode, attr_ver, res, &ia.ap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827
David Brazdil0f672f62019-12-10 10:32:29 +0000828 SetPageUptodate(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829
David Brazdil0f672f62019-12-10 10:32:29 +0000830 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000831}
832
833static int fuse_readpage(struct file *file, struct page *page)
834{
835 struct inode *inode = page->mapping->host;
836 int err;
837
838 err = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +0200839 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000840 goto out;
841
842 err = fuse_do_readpage(file, page);
843 fuse_invalidate_atime(inode);
844 out:
845 unlock_page(page);
846 return err;
847}
848
David Brazdil0f672f62019-12-10 10:32:29 +0000849static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
850 int err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851{
852 int i;
David Brazdil0f672f62019-12-10 10:32:29 +0000853 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
854 struct fuse_args_pages *ap = &ia->ap;
855 size_t count = ia->read.in.size;
856 size_t num_read = args->out_args[0].size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000857 struct address_space *mapping = NULL;
858
David Brazdil0f672f62019-12-10 10:32:29 +0000859 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
860 mapping = ap->pages[i]->mapping;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861
862 if (mapping) {
863 struct inode *inode = mapping->host;
864
865 /*
866 * Short read means EOF. If file size is larger, truncate it
867 */
David Brazdil0f672f62019-12-10 10:32:29 +0000868 if (!err && num_read < count)
869 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000870
871 fuse_invalidate_atime(inode);
872 }
873
David Brazdil0f672f62019-12-10 10:32:29 +0000874 for (i = 0; i < ap->num_pages; i++) {
875 struct page *page = ap->pages[i];
876
877 if (!err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000878 SetPageUptodate(page);
879 else
880 SetPageError(page);
881 unlock_page(page);
882 put_page(page);
883 }
David Brazdil0f672f62019-12-10 10:32:29 +0000884 if (ia->ff)
885 fuse_file_put(ia->ff, false, false);
886
887 fuse_io_free(ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000888}
889
David Brazdil0f672f62019-12-10 10:32:29 +0000890static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000891{
892 struct fuse_file *ff = file->private_data;
893 struct fuse_conn *fc = ff->fc;
David Brazdil0f672f62019-12-10 10:32:29 +0000894 struct fuse_args_pages *ap = &ia->ap;
895 loff_t pos = page_offset(ap->pages[0]);
896 size_t count = ap->num_pages << PAGE_SHIFT;
Olivier Deprez0e641232021-09-23 10:07:05 +0200897 ssize_t res;
David Brazdil0f672f62019-12-10 10:32:29 +0000898 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899
David Brazdil0f672f62019-12-10 10:32:29 +0000900 ap->args.out_pages = true;
901 ap->args.page_zeroing = true;
902 ap->args.page_replace = true;
Olivier Deprez0e641232021-09-23 10:07:05 +0200903
904 /* Don't overflow end offset */
905 if (pos + (count - 1) == LLONG_MAX) {
906 count--;
907 ap->descs[ap->num_pages - 1].length--;
908 }
909 WARN_ON((loff_t) (pos + count) < 0);
910
David Brazdil0f672f62019-12-10 10:32:29 +0000911 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
912 ia->read.attr_ver = fuse_get_attr_version(fc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 if (fc->async_read) {
David Brazdil0f672f62019-12-10 10:32:29 +0000914 ia->ff = fuse_file_get(ff);
915 ap->args.end = fuse_readpages_end;
916 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
917 if (!err)
918 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919 } else {
Olivier Deprez0e641232021-09-23 10:07:05 +0200920 res = fuse_simple_request(fc, &ap->args);
921 err = res < 0 ? res : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922 }
David Brazdil0f672f62019-12-10 10:32:29 +0000923 fuse_readpages_end(fc, &ap->args, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924}
925
926struct fuse_fill_data {
David Brazdil0f672f62019-12-10 10:32:29 +0000927 struct fuse_io_args *ia;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928 struct file *file;
929 struct inode *inode;
David Brazdil0f672f62019-12-10 10:32:29 +0000930 unsigned int nr_pages;
931 unsigned int max_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000932};
933
934static int fuse_readpages_fill(void *_data, struct page *page)
935{
936 struct fuse_fill_data *data = _data;
David Brazdil0f672f62019-12-10 10:32:29 +0000937 struct fuse_io_args *ia = data->ia;
938 struct fuse_args_pages *ap = &ia->ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939 struct inode *inode = data->inode;
940 struct fuse_conn *fc = get_fuse_conn(inode);
941
942 fuse_wait_on_page_writeback(inode, page->index);
943
David Brazdil0f672f62019-12-10 10:32:29 +0000944 if (ap->num_pages &&
945 (ap->num_pages == fc->max_pages ||
946 (ap->num_pages + 1) * PAGE_SIZE > fc->max_read ||
947 ap->pages[ap->num_pages - 1]->index + 1 != page->index)) {
948 data->max_pages = min_t(unsigned int, data->nr_pages,
949 fc->max_pages);
950 fuse_send_readpages(ia, data->file);
951 data->ia = ia = fuse_io_alloc(NULL, data->max_pages);
952 if (!ia) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953 unlock_page(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000954 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955 }
David Brazdil0f672f62019-12-10 10:32:29 +0000956 ap = &ia->ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957 }
958
David Brazdil0f672f62019-12-10 10:32:29 +0000959 if (WARN_ON(ap->num_pages >= data->max_pages)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960 unlock_page(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000961 fuse_io_free(ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962 return -EIO;
963 }
964
965 get_page(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000966 ap->pages[ap->num_pages] = page;
967 ap->descs[ap->num_pages].length = PAGE_SIZE;
968 ap->num_pages++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000969 data->nr_pages--;
970 return 0;
971}
972
973static int fuse_readpages(struct file *file, struct address_space *mapping,
974 struct list_head *pages, unsigned nr_pages)
975{
976 struct inode *inode = mapping->host;
977 struct fuse_conn *fc = get_fuse_conn(inode);
978 struct fuse_fill_data data;
979 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980
981 err = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +0200982 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000983 goto out;
984
985 data.file = file;
986 data.inode = inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000987 data.nr_pages = nr_pages;
David Brazdil0f672f62019-12-10 10:32:29 +0000988 data.max_pages = min_t(unsigned int, nr_pages, fc->max_pages);
989;
990 data.ia = fuse_io_alloc(NULL, data.max_pages);
991 err = -ENOMEM;
992 if (!data.ia)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000993 goto out;
994
995 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
996 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +0000997 if (data.ia->ap.num_pages)
998 fuse_send_readpages(data.ia, file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000999 else
David Brazdil0f672f62019-12-10 10:32:29 +00001000 fuse_io_free(data.ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001001 }
1002out:
1003 return err;
1004}
1005
David Brazdil0f672f62019-12-10 10:32:29 +00001006static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007{
1008 struct inode *inode = iocb->ki_filp->f_mapping->host;
1009 struct fuse_conn *fc = get_fuse_conn(inode);
1010
1011 /*
1012 * In auto invalidate mode, always update attributes on read.
1013 * Otherwise, only update if we attempt to read past EOF (to ensure
1014 * i_size is up to date).
1015 */
1016 if (fc->auto_inval_data ||
1017 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1018 int err;
1019 err = fuse_update_attributes(inode, iocb->ki_filp);
1020 if (err)
1021 return err;
1022 }
1023
1024 return generic_file_read_iter(iocb, to);
1025}
1026
David Brazdil0f672f62019-12-10 10:32:29 +00001027static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1028 loff_t pos, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029{
David Brazdil0f672f62019-12-10 10:32:29 +00001030 struct fuse_args *args = &ia->ap.args;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031
David Brazdil0f672f62019-12-10 10:32:29 +00001032 ia->write.in.fh = ff->fh;
1033 ia->write.in.offset = pos;
1034 ia->write.in.size = count;
1035 args->opcode = FUSE_WRITE;
1036 args->nodeid = ff->nodeid;
1037 args->in_numargs = 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 if (ff->fc->minor < 9)
David Brazdil0f672f62019-12-10 10:32:29 +00001039 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001040 else
David Brazdil0f672f62019-12-10 10:32:29 +00001041 args->in_args[0].size = sizeof(ia->write.in);
1042 args->in_args[0].value = &ia->write.in;
1043 args->in_args[1].size = count;
1044 args->out_numargs = 1;
1045 args->out_args[0].size = sizeof(ia->write.out);
1046 args->out_args[0].value = &ia->write.out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047}
1048
David Brazdil0f672f62019-12-10 10:32:29 +00001049static unsigned int fuse_write_flags(struct kiocb *iocb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050{
David Brazdil0f672f62019-12-10 10:32:29 +00001051 unsigned int flags = iocb->ki_filp->f_flags;
1052
1053 if (iocb->ki_flags & IOCB_DSYNC)
1054 flags |= O_DSYNC;
1055 if (iocb->ki_flags & IOCB_SYNC)
1056 flags |= O_SYNC;
1057
1058 return flags;
1059}
1060
1061static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1062 size_t count, fl_owner_t owner)
1063{
1064 struct kiocb *iocb = ia->io->iocb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001065 struct file *file = iocb->ki_filp;
1066 struct fuse_file *ff = file->private_data;
1067 struct fuse_conn *fc = ff->fc;
David Brazdil0f672f62019-12-10 10:32:29 +00001068 struct fuse_write_in *inarg = &ia->write.in;
1069 ssize_t err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001070
David Brazdil0f672f62019-12-10 10:32:29 +00001071 fuse_write_args_fill(ia, ff, pos, count);
1072 inarg->flags = fuse_write_flags(iocb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 if (owner != NULL) {
1074 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1075 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1076 }
1077
David Brazdil0f672f62019-12-10 10:32:29 +00001078 if (ia->io->async)
1079 return fuse_async_req_send(fc, ia, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001080
David Brazdil0f672f62019-12-10 10:32:29 +00001081 err = fuse_simple_request(fc, &ia->ap.args);
1082 if (!err && ia->write.out.size > count)
1083 err = -EIO;
1084
1085 return err ?: ia->write.out.size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001086}
1087
1088bool fuse_write_update_size(struct inode *inode, loff_t pos)
1089{
1090 struct fuse_conn *fc = get_fuse_conn(inode);
1091 struct fuse_inode *fi = get_fuse_inode(inode);
1092 bool ret = false;
1093
David Brazdil0f672f62019-12-10 10:32:29 +00001094 spin_lock(&fi->lock);
1095 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001096 if (pos > inode->i_size) {
1097 i_size_write(inode, pos);
1098 ret = true;
1099 }
David Brazdil0f672f62019-12-10 10:32:29 +00001100 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101
1102 return ret;
1103}
1104
David Brazdil0f672f62019-12-10 10:32:29 +00001105static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1106 struct kiocb *iocb, struct inode *inode,
1107 loff_t pos, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108{
David Brazdil0f672f62019-12-10 10:32:29 +00001109 struct fuse_args_pages *ap = &ia->ap;
1110 struct file *file = iocb->ki_filp;
1111 struct fuse_file *ff = file->private_data;
1112 struct fuse_conn *fc = ff->fc;
1113 unsigned int offset, i;
Olivier Deprez0e641232021-09-23 10:07:05 +02001114 bool short_write;
David Brazdil0f672f62019-12-10 10:32:29 +00001115 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116
David Brazdil0f672f62019-12-10 10:32:29 +00001117 for (i = 0; i < ap->num_pages; i++)
1118 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001119
David Brazdil0f672f62019-12-10 10:32:29 +00001120 fuse_write_args_fill(ia, ff, pos, count);
1121 ia->write.in.flags = fuse_write_flags(iocb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122
David Brazdil0f672f62019-12-10 10:32:29 +00001123 err = fuse_simple_request(fc, &ap->args);
Olivier Deprez0e641232021-09-23 10:07:05 +02001124 if (!err && ia->write.out.size > count)
1125 err = -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001126
Olivier Deprez0e641232021-09-23 10:07:05 +02001127 short_write = ia->write.out.size < count;
David Brazdil0f672f62019-12-10 10:32:29 +00001128 offset = ap->descs[0].offset;
1129 count = ia->write.out.size;
1130 for (i = 0; i < ap->num_pages; i++) {
1131 struct page *page = ap->pages[i];
1132
Olivier Deprez0e641232021-09-23 10:07:05 +02001133 if (err) {
1134 ClearPageUptodate(page);
1135 } else {
1136 if (count >= PAGE_SIZE - offset)
1137 count -= PAGE_SIZE - offset;
1138 else {
1139 if (short_write)
1140 ClearPageUptodate(page);
1141 count = 0;
1142 }
1143 offset = 0;
1144 }
1145 if (ia->write.page_locked && (i == ap->num_pages - 1))
1146 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001147 put_page(page);
1148 }
1149
David Brazdil0f672f62019-12-10 10:32:29 +00001150 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151}
1152
Olivier Deprez0e641232021-09-23 10:07:05 +02001153static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
David Brazdil0f672f62019-12-10 10:32:29 +00001154 struct address_space *mapping,
1155 struct iov_iter *ii, loff_t pos,
1156 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157{
Olivier Deprez0e641232021-09-23 10:07:05 +02001158 struct fuse_args_pages *ap = &ia->ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001159 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1160 unsigned offset = pos & (PAGE_SIZE - 1);
1161 size_t count = 0;
1162 int err;
1163
David Brazdil0f672f62019-12-10 10:32:29 +00001164 ap->args.in_pages = true;
1165 ap->descs[0].offset = offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001166
1167 do {
1168 size_t tmp;
1169 struct page *page;
1170 pgoff_t index = pos >> PAGE_SHIFT;
1171 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1172 iov_iter_count(ii));
1173
1174 bytes = min_t(size_t, bytes, fc->max_write - count);
1175
1176 again:
1177 err = -EFAULT;
1178 if (iov_iter_fault_in_readable(ii, bytes))
1179 break;
1180
1181 err = -ENOMEM;
1182 page = grab_cache_page_write_begin(mapping, index, 0);
1183 if (!page)
1184 break;
1185
1186 if (mapping_writably_mapped(mapping))
1187 flush_dcache_page(page);
1188
1189 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1190 flush_dcache_page(page);
1191
1192 iov_iter_advance(ii, tmp);
1193 if (!tmp) {
1194 unlock_page(page);
1195 put_page(page);
1196 bytes = min(bytes, iov_iter_single_seg_count(ii));
1197 goto again;
1198 }
1199
1200 err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001201 ap->pages[ap->num_pages] = page;
1202 ap->descs[ap->num_pages].length = tmp;
1203 ap->num_pages++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204
1205 count += tmp;
1206 pos += tmp;
1207 offset += tmp;
1208 if (offset == PAGE_SIZE)
1209 offset = 0;
1210
Olivier Deprez0e641232021-09-23 10:07:05 +02001211 /* If we copied full page, mark it uptodate */
1212 if (tmp == PAGE_SIZE)
1213 SetPageUptodate(page);
1214
1215 if (PageUptodate(page)) {
1216 unlock_page(page);
1217 } else {
1218 ia->write.page_locked = true;
1219 break;
1220 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001221 if (!fc->big_writes)
1222 break;
1223 } while (iov_iter_count(ii) && count < fc->max_write &&
David Brazdil0f672f62019-12-10 10:32:29 +00001224 ap->num_pages < max_pages && offset == 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225
1226 return count > 0 ? count : err;
1227}
1228
David Brazdil0f672f62019-12-10 10:32:29 +00001229static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1230 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001231{
David Brazdil0f672f62019-12-10 10:32:29 +00001232 return min_t(unsigned int,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001233 ((pos + len - 1) >> PAGE_SHIFT) -
1234 (pos >> PAGE_SHIFT) + 1,
David Brazdil0f672f62019-12-10 10:32:29 +00001235 max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001236}
1237
1238static ssize_t fuse_perform_write(struct kiocb *iocb,
1239 struct address_space *mapping,
1240 struct iov_iter *ii, loff_t pos)
1241{
1242 struct inode *inode = mapping->host;
1243 struct fuse_conn *fc = get_fuse_conn(inode);
1244 struct fuse_inode *fi = get_fuse_inode(inode);
1245 int err = 0;
1246 ssize_t res = 0;
1247
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001248 if (inode->i_size < pos + iov_iter_count(ii))
1249 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1250
1251 do {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001252 ssize_t count;
David Brazdil0f672f62019-12-10 10:32:29 +00001253 struct fuse_io_args ia = {};
1254 struct fuse_args_pages *ap = &ia.ap;
1255 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1256 fc->max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001257
David Brazdil0f672f62019-12-10 10:32:29 +00001258 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1259 if (!ap->pages) {
1260 err = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001261 break;
1262 }
1263
Olivier Deprez0e641232021-09-23 10:07:05 +02001264 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265 if (count <= 0) {
1266 err = count;
1267 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001268 err = fuse_send_write_pages(&ia, iocb, inode,
1269 pos, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001270 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001271 size_t num_written = ia.write.out.size;
1272
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001273 res += num_written;
1274 pos += num_written;
1275
1276 /* break out of the loop on short write */
1277 if (num_written != count)
1278 err = -EIO;
1279 }
1280 }
David Brazdil0f672f62019-12-10 10:32:29 +00001281 kfree(ap->pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001282 } while (!err && iov_iter_count(ii));
1283
1284 if (res > 0)
1285 fuse_write_update_size(inode, pos);
1286
1287 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1288 fuse_invalidate_attr(inode);
1289
1290 return res > 0 ? res : err;
1291}
1292
David Brazdil0f672f62019-12-10 10:32:29 +00001293static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001294{
1295 struct file *file = iocb->ki_filp;
1296 struct address_space *mapping = file->f_mapping;
1297 ssize_t written = 0;
1298 ssize_t written_buffered = 0;
1299 struct inode *inode = mapping->host;
1300 ssize_t err;
1301 loff_t endbyte = 0;
1302
1303 if (get_fuse_conn(inode)->writeback_cache) {
1304 /* Update size (EOF optimization) and mode (SUID clearing) */
1305 err = fuse_update_attributes(mapping->host, file);
1306 if (err)
1307 return err;
1308
1309 return generic_file_write_iter(iocb, from);
1310 }
1311
1312 inode_lock(inode);
1313
1314 /* We can write back this queue in page reclaim */
1315 current->backing_dev_info = inode_to_bdi(inode);
1316
1317 err = generic_write_checks(iocb, from);
1318 if (err <= 0)
1319 goto out;
1320
1321 err = file_remove_privs(file);
1322 if (err)
1323 goto out;
1324
1325 err = file_update_time(file);
1326 if (err)
1327 goto out;
1328
1329 if (iocb->ki_flags & IOCB_DIRECT) {
1330 loff_t pos = iocb->ki_pos;
1331 written = generic_file_direct_write(iocb, from);
1332 if (written < 0 || !iov_iter_count(from))
1333 goto out;
1334
1335 pos += written;
1336
1337 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1338 if (written_buffered < 0) {
1339 err = written_buffered;
1340 goto out;
1341 }
1342 endbyte = pos + written_buffered - 1;
1343
1344 err = filemap_write_and_wait_range(file->f_mapping, pos,
1345 endbyte);
1346 if (err)
1347 goto out;
1348
1349 invalidate_mapping_pages(file->f_mapping,
1350 pos >> PAGE_SHIFT,
1351 endbyte >> PAGE_SHIFT);
1352
1353 written += written_buffered;
1354 iocb->ki_pos = pos + written_buffered;
1355 } else {
1356 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1357 if (written >= 0)
1358 iocb->ki_pos += written;
1359 }
1360out:
1361 current->backing_dev_info = NULL;
1362 inode_unlock(inode);
1363 if (written > 0)
1364 written = generic_write_sync(iocb, written);
1365
1366 return written ? written : err;
1367}
1368
David Brazdil0f672f62019-12-10 10:32:29 +00001369static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1370 unsigned int index,
1371 unsigned int nr_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001372{
1373 int i;
1374
1375 for (i = index; i < index + nr_pages; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00001376 descs[i].length = PAGE_SIZE - descs[i].offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001377}
1378
1379static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1380{
1381 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1382}
1383
1384static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1385 size_t max_size)
1386{
1387 return min(iov_iter_single_seg_count(ii), max_size);
1388}
1389
David Brazdil0f672f62019-12-10 10:32:29 +00001390static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1391 size_t *nbytesp, int write,
1392 unsigned int max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393{
1394 size_t nbytes = 0; /* # bytes already packed in req */
1395 ssize_t ret = 0;
1396
1397 /* Special case for kernel I/O: can copy directly into the buffer */
David Brazdil0f672f62019-12-10 10:32:29 +00001398 if (iov_iter_is_kvec(ii)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001399 unsigned long user_addr = fuse_get_user_addr(ii);
1400 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1401
1402 if (write)
David Brazdil0f672f62019-12-10 10:32:29 +00001403 ap->args.in_args[1].value = (void *) user_addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001404 else
David Brazdil0f672f62019-12-10 10:32:29 +00001405 ap->args.out_args[0].value = (void *) user_addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001406
1407 iov_iter_advance(ii, frag_size);
1408 *nbytesp = frag_size;
1409 return 0;
1410 }
1411
David Brazdil0f672f62019-12-10 10:32:29 +00001412 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001413 unsigned npages;
1414 size_t start;
David Brazdil0f672f62019-12-10 10:32:29 +00001415 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001416 *nbytesp - nbytes,
David Brazdil0f672f62019-12-10 10:32:29 +00001417 max_pages - ap->num_pages,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418 &start);
1419 if (ret < 0)
1420 break;
1421
1422 iov_iter_advance(ii, ret);
1423 nbytes += ret;
1424
1425 ret += start;
1426 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1427
David Brazdil0f672f62019-12-10 10:32:29 +00001428 ap->descs[ap->num_pages].offset = start;
1429 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001430
David Brazdil0f672f62019-12-10 10:32:29 +00001431 ap->num_pages += npages;
1432 ap->descs[ap->num_pages - 1].length -=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1434 }
1435
1436 if (write)
David Brazdil0f672f62019-12-10 10:32:29 +00001437 ap->args.in_pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001438 else
David Brazdil0f672f62019-12-10 10:32:29 +00001439 ap->args.out_pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001440
1441 *nbytesp = nbytes;
1442
1443 return ret < 0 ? ret : 0;
1444}
1445
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001446ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1447 loff_t *ppos, int flags)
1448{
1449 int write = flags & FUSE_DIO_WRITE;
1450 int cuse = flags & FUSE_DIO_CUSE;
1451 struct file *file = io->iocb->ki_filp;
1452 struct inode *inode = file->f_mapping->host;
1453 struct fuse_file *ff = file->private_data;
1454 struct fuse_conn *fc = ff->fc;
1455 size_t nmax = write ? fc->max_write : fc->max_read;
1456 loff_t pos = *ppos;
1457 size_t count = iov_iter_count(iter);
1458 pgoff_t idx_from = pos >> PAGE_SHIFT;
1459 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1460 ssize_t res = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001462 struct fuse_io_args *ia;
1463 unsigned int max_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001464
David Brazdil0f672f62019-12-10 10:32:29 +00001465 max_pages = iov_iter_npages(iter, fc->max_pages);
1466 ia = fuse_io_alloc(io, max_pages);
1467 if (!ia)
1468 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001469
David Brazdil0f672f62019-12-10 10:32:29 +00001470 ia->io = io;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001471 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1472 if (!write)
1473 inode_lock(inode);
1474 fuse_sync_writes(inode);
1475 if (!write)
1476 inode_unlock(inode);
1477 }
1478
1479 io->should_dirty = !write && iter_is_iovec(iter);
1480 while (count) {
David Brazdil0f672f62019-12-10 10:32:29 +00001481 ssize_t nres;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482 fl_owner_t owner = current->files;
1483 size_t nbytes = min(count, nmax);
David Brazdil0f672f62019-12-10 10:32:29 +00001484
1485 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1486 max_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001487 if (err && !nbytes)
1488 break;
1489
David Brazdil0f672f62019-12-10 10:32:29 +00001490 if (write) {
1491 if (!capable(CAP_FSETID))
1492 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001493
David Brazdil0f672f62019-12-10 10:32:29 +00001494 nres = fuse_send_write(ia, pos, nbytes, owner);
1495 } else {
1496 nres = fuse_send_read(ia, pos, nbytes, owner);
1497 }
1498
1499 if (!io->async || nres < 0) {
1500 fuse_release_user_pages(&ia->ap, io->should_dirty);
1501 fuse_io_free(ia);
1502 }
1503 ia = NULL;
1504 if (nres < 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001505 iov_iter_revert(iter, nbytes);
David Brazdil0f672f62019-12-10 10:32:29 +00001506 err = nres;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001507 break;
1508 }
David Brazdil0f672f62019-12-10 10:32:29 +00001509 WARN_ON(nres > nbytes);
1510
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001511 count -= nres;
1512 res += nres;
1513 pos += nres;
Olivier Deprez0e641232021-09-23 10:07:05 +02001514 if (nres != nbytes) {
1515 iov_iter_revert(iter, nbytes - nres);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001516 break;
Olivier Deprez0e641232021-09-23 10:07:05 +02001517 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001518 if (count) {
David Brazdil0f672f62019-12-10 10:32:29 +00001519 max_pages = iov_iter_npages(iter, fc->max_pages);
1520 ia = fuse_io_alloc(io, max_pages);
1521 if (!ia)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001522 break;
1523 }
1524 }
David Brazdil0f672f62019-12-10 10:32:29 +00001525 if (ia)
1526 fuse_io_free(ia);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001527 if (res > 0)
1528 *ppos = pos;
1529
1530 return res > 0 ? res : err;
1531}
1532EXPORT_SYMBOL_GPL(fuse_direct_io);
1533
1534static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1535 struct iov_iter *iter,
1536 loff_t *ppos)
1537{
1538 ssize_t res;
1539 struct inode *inode = file_inode(io->iocb->ki_filp);
1540
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001541 res = fuse_direct_io(io, iter, ppos, 0);
1542
David Brazdil0f672f62019-12-10 10:32:29 +00001543 fuse_invalidate_atime(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001544
1545 return res;
1546}
1547
David Brazdil0f672f62019-12-10 10:32:29 +00001548static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1549
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001550static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1551{
David Brazdil0f672f62019-12-10 10:32:29 +00001552 ssize_t res;
1553
1554 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1555 res = fuse_direct_IO(iocb, to);
1556 } else {
1557 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1558
1559 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1560 }
1561
1562 return res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001563}
1564
1565static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1566{
1567 struct inode *inode = file_inode(iocb->ki_filp);
1568 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1569 ssize_t res;
1570
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001571 /* Don't allow parallel writes to the same file */
1572 inode_lock(inode);
1573 res = generic_write_checks(iocb, from);
David Brazdil0f672f62019-12-10 10:32:29 +00001574 if (res > 0) {
1575 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1576 res = fuse_direct_IO(iocb, from);
1577 } else {
1578 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1579 FUSE_DIO_WRITE);
1580 }
1581 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001582 fuse_invalidate_attr(inode);
1583 if (res > 0)
1584 fuse_write_update_size(inode, iocb->ki_pos);
1585 inode_unlock(inode);
1586
1587 return res;
1588}
1589
David Brazdil0f672f62019-12-10 10:32:29 +00001590static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001591{
David Brazdil0f672f62019-12-10 10:32:29 +00001592 struct file *file = iocb->ki_filp;
1593 struct fuse_file *ff = file->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001594
Olivier Deprez0e641232021-09-23 10:07:05 +02001595 if (fuse_is_bad(file_inode(file)))
David Brazdil0f672f62019-12-10 10:32:29 +00001596 return -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001597
David Brazdil0f672f62019-12-10 10:32:29 +00001598 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1599 return fuse_cache_read_iter(iocb, to);
1600 else
1601 return fuse_direct_read_iter(iocb, to);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001602}
1603
David Brazdil0f672f62019-12-10 10:32:29 +00001604static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001605{
David Brazdil0f672f62019-12-10 10:32:29 +00001606 struct file *file = iocb->ki_filp;
1607 struct fuse_file *ff = file->private_data;
1608
Olivier Deprez0e641232021-09-23 10:07:05 +02001609 if (fuse_is_bad(file_inode(file)))
David Brazdil0f672f62019-12-10 10:32:29 +00001610 return -EIO;
1611
1612 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1613 return fuse_cache_write_iter(iocb, from);
1614 else
1615 return fuse_direct_write_iter(iocb, from);
1616}
1617
1618static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1619{
1620 struct fuse_args_pages *ap = &wpa->ia.ap;
1621 int i;
1622
1623 for (i = 0; i < ap->num_pages; i++)
1624 __free_page(ap->pages[i]);
1625
1626 if (wpa->ia.ff)
1627 fuse_file_put(wpa->ia.ff, false, false);
1628
1629 kfree(ap->pages);
1630 kfree(wpa);
1631}
1632
1633static void fuse_writepage_finish(struct fuse_conn *fc,
1634 struct fuse_writepage_args *wpa)
1635{
1636 struct fuse_args_pages *ap = &wpa->ia.ap;
1637 struct inode *inode = wpa->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001638 struct fuse_inode *fi = get_fuse_inode(inode);
1639 struct backing_dev_info *bdi = inode_to_bdi(inode);
1640 int i;
1641
David Brazdil0f672f62019-12-10 10:32:29 +00001642 list_del(&wpa->writepages_entry);
1643 for (i = 0; i < ap->num_pages; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001644 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
David Brazdil0f672f62019-12-10 10:32:29 +00001645 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646 wb_writeout_inc(&bdi->wb);
1647 }
1648 wake_up(&fi->page_waitq);
1649}
1650
David Brazdil0f672f62019-12-10 10:32:29 +00001651/* Called under fi->lock, may release and reacquire it */
1652static void fuse_send_writepage(struct fuse_conn *fc,
1653 struct fuse_writepage_args *wpa, loff_t size)
1654__releases(fi->lock)
1655__acquires(fi->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001656{
David Brazdil0f672f62019-12-10 10:32:29 +00001657 struct fuse_writepage_args *aux, *next;
1658 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1659 struct fuse_write_in *inarg = &wpa->ia.write.in;
1660 struct fuse_args *args = &wpa->ia.ap.args;
1661 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1662 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001663
David Brazdil0f672f62019-12-10 10:32:29 +00001664 fi->writectr++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665 if (inarg->offset + data_size <= size) {
1666 inarg->size = data_size;
1667 } else if (inarg->offset < size) {
1668 inarg->size = size - inarg->offset;
1669 } else {
1670 /* Got truncated off completely */
1671 goto out_free;
1672 }
1673
David Brazdil0f672f62019-12-10 10:32:29 +00001674 args->in_args[1].size = inarg->size;
1675 args->force = true;
1676 args->nocreds = true;
1677
1678 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1679 if (err == -ENOMEM) {
1680 spin_unlock(&fi->lock);
1681 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1682 spin_lock(&fi->lock);
1683 }
1684
1685 /* Fails on broken connection only */
1686 if (unlikely(err))
1687 goto out_free;
1688
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001689 return;
1690
1691 out_free:
David Brazdil0f672f62019-12-10 10:32:29 +00001692 fi->writectr--;
1693 fuse_writepage_finish(fc, wpa);
1694 spin_unlock(&fi->lock);
1695
1696 /* After fuse_writepage_finish() aux request list is private */
1697 for (aux = wpa->next; aux; aux = next) {
1698 next = aux->next;
1699 aux->next = NULL;
1700 fuse_writepage_free(aux);
1701 }
1702
1703 fuse_writepage_free(wpa);
1704 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001705}
1706
1707/*
1708 * If fi->writectr is positive (no truncate or fsync going on) send
1709 * all queued writepage requests.
1710 *
David Brazdil0f672f62019-12-10 10:32:29 +00001711 * Called with fi->lock
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001712 */
1713void fuse_flush_writepages(struct inode *inode)
David Brazdil0f672f62019-12-10 10:32:29 +00001714__releases(fi->lock)
1715__acquires(fi->lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001716{
1717 struct fuse_conn *fc = get_fuse_conn(inode);
1718 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001719 loff_t crop = i_size_read(inode);
1720 struct fuse_writepage_args *wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001721
1722 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001723 wpa = list_entry(fi->queued_writes.next,
1724 struct fuse_writepage_args, queue_entry);
1725 list_del_init(&wpa->queue_entry);
1726 fuse_send_writepage(fc, wpa, crop);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001727 }
1728}
1729
David Brazdil0f672f62019-12-10 10:32:29 +00001730static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1731 int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001732{
David Brazdil0f672f62019-12-10 10:32:29 +00001733 struct fuse_writepage_args *wpa =
1734 container_of(args, typeof(*wpa), ia.ap.args);
1735 struct inode *inode = wpa->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001736 struct fuse_inode *fi = get_fuse_inode(inode);
1737
David Brazdil0f672f62019-12-10 10:32:29 +00001738 mapping_set_error(inode->i_mapping, error);
1739 spin_lock(&fi->lock);
1740 while (wpa->next) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001741 struct fuse_conn *fc = get_fuse_conn(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001742 struct fuse_write_in *inarg = &wpa->ia.write.in;
1743 struct fuse_writepage_args *next = wpa->next;
1744
1745 wpa->next = next->next;
1746 next->next = NULL;
1747 next->ia.ff = fuse_file_get(wpa->ia.ff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001748 list_add(&next->writepages_entry, &fi->writepages);
1749
1750 /*
1751 * Skip fuse_flush_writepages() to make it easy to crop requests
1752 * based on primary request size.
1753 *
1754 * 1st case (trivial): there are no concurrent activities using
1755 * fuse_set/release_nowrite. Then we're on safe side because
1756 * fuse_flush_writepages() would call fuse_send_writepage()
1757 * anyway.
1758 *
1759 * 2nd case: someone called fuse_set_nowrite and it is waiting
1760 * now for completion of all in-flight requests. This happens
1761 * rarely and no more than once per page, so this should be
1762 * okay.
1763 *
1764 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1765 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1766 * that fuse_set_nowrite returned implies that all in-flight
1767 * requests were completed along with all of their secondary
1768 * requests. Further primary requests are blocked by negative
1769 * writectr. Hence there cannot be any in-flight requests and
1770 * no invocations of fuse_writepage_end() while we're in
1771 * fuse_set_nowrite..fuse_release_nowrite section.
1772 */
1773 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1774 }
1775 fi->writectr--;
David Brazdil0f672f62019-12-10 10:32:29 +00001776 fuse_writepage_finish(fc, wpa);
1777 spin_unlock(&fi->lock);
1778 fuse_writepage_free(wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001779}
1780
1781static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1782 struct fuse_inode *fi)
1783{
1784 struct fuse_file *ff = NULL;
1785
David Brazdil0f672f62019-12-10 10:32:29 +00001786 spin_lock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001787 if (!list_empty(&fi->write_files)) {
1788 ff = list_entry(fi->write_files.next, struct fuse_file,
1789 write_entry);
1790 fuse_file_get(ff);
1791 }
David Brazdil0f672f62019-12-10 10:32:29 +00001792 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001793
1794 return ff;
1795}
1796
1797static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1798 struct fuse_inode *fi)
1799{
1800 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1801 WARN_ON(!ff);
1802 return ff;
1803}
1804
1805int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1806{
1807 struct fuse_conn *fc = get_fuse_conn(inode);
1808 struct fuse_inode *fi = get_fuse_inode(inode);
1809 struct fuse_file *ff;
1810 int err;
1811
1812 ff = __fuse_write_file_get(fc, fi);
1813 err = fuse_flush_times(inode, ff);
1814 if (ff)
1815 fuse_file_put(ff, false, false);
1816
1817 return err;
1818}
1819
David Brazdil0f672f62019-12-10 10:32:29 +00001820static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1821{
1822 struct fuse_writepage_args *wpa;
1823 struct fuse_args_pages *ap;
1824
1825 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1826 if (wpa) {
1827 ap = &wpa->ia.ap;
1828 ap->num_pages = 0;
1829 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1830 if (!ap->pages) {
1831 kfree(wpa);
1832 wpa = NULL;
1833 }
1834 }
1835 return wpa;
1836
1837}
1838
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001839static int fuse_writepage_locked(struct page *page)
1840{
1841 struct address_space *mapping = page->mapping;
1842 struct inode *inode = mapping->host;
1843 struct fuse_conn *fc = get_fuse_conn(inode);
1844 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001845 struct fuse_writepage_args *wpa;
1846 struct fuse_args_pages *ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001847 struct page *tmp_page;
1848 int error = -ENOMEM;
1849
1850 set_page_writeback(page);
1851
David Brazdil0f672f62019-12-10 10:32:29 +00001852 wpa = fuse_writepage_args_alloc();
1853 if (!wpa)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854 goto err;
David Brazdil0f672f62019-12-10 10:32:29 +00001855 ap = &wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001856
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001857 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1858 if (!tmp_page)
1859 goto err_free;
1860
1861 error = -EIO;
David Brazdil0f672f62019-12-10 10:32:29 +00001862 wpa->ia.ff = fuse_write_file_get(fc, fi);
1863 if (!wpa->ia.ff)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 goto err_nofile;
1865
David Brazdil0f672f62019-12-10 10:32:29 +00001866 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867
1868 copy_highpage(tmp_page, page);
David Brazdil0f672f62019-12-10 10:32:29 +00001869 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1870 wpa->next = NULL;
1871 ap->args.in_pages = true;
1872 ap->num_pages = 1;
1873 ap->pages[0] = tmp_page;
1874 ap->descs[0].offset = 0;
1875 ap->descs[0].length = PAGE_SIZE;
1876 ap->args.end = fuse_writepage_end;
1877 wpa->inode = inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001878
1879 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1880 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1881
David Brazdil0f672f62019-12-10 10:32:29 +00001882 spin_lock(&fi->lock);
1883 list_add(&wpa->writepages_entry, &fi->writepages);
1884 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001885 fuse_flush_writepages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001886 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887
1888 end_page_writeback(page);
1889
1890 return 0;
1891
1892err_nofile:
1893 __free_page(tmp_page);
1894err_free:
David Brazdil0f672f62019-12-10 10:32:29 +00001895 kfree(wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001896err:
1897 mapping_set_error(page->mapping, error);
1898 end_page_writeback(page);
1899 return error;
1900}
1901
1902static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1903{
1904 int err;
1905
1906 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1907 /*
1908 * ->writepages() should be called for sync() and friends. We
1909 * should only get here on direct reclaim and then we are
1910 * allowed to skip a page which is already in flight
1911 */
1912 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1913
1914 redirty_page_for_writepage(wbc, page);
David Brazdil0f672f62019-12-10 10:32:29 +00001915 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001916 return 0;
1917 }
1918
1919 err = fuse_writepage_locked(page);
1920 unlock_page(page);
1921
1922 return err;
1923}
1924
1925struct fuse_fill_wb_data {
David Brazdil0f672f62019-12-10 10:32:29 +00001926 struct fuse_writepage_args *wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001927 struct fuse_file *ff;
1928 struct inode *inode;
1929 struct page **orig_pages;
David Brazdil0f672f62019-12-10 10:32:29 +00001930 unsigned int max_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001931};
1932
David Brazdil0f672f62019-12-10 10:32:29 +00001933static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1934{
1935 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1936 struct fuse_conn *fc = get_fuse_conn(data->inode);
1937 struct page **pages;
1938 struct fuse_page_desc *descs;
1939 unsigned int npages = min_t(unsigned int,
1940 max_t(unsigned int, data->max_pages * 2,
1941 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1942 fc->max_pages);
1943 WARN_ON(npages <= data->max_pages);
1944
1945 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1946 if (!pages)
1947 return false;
1948
1949 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1950 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1951 kfree(ap->pages);
1952 ap->pages = pages;
1953 ap->descs = descs;
1954 data->max_pages = npages;
1955
1956 return true;
1957}
1958
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001959static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1960{
David Brazdil0f672f62019-12-10 10:32:29 +00001961 struct fuse_writepage_args *wpa = data->wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001962 struct inode *inode = data->inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001963 struct fuse_inode *fi = get_fuse_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001964 int num_pages = wpa->ia.ap.num_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001965 int i;
1966
David Brazdil0f672f62019-12-10 10:32:29 +00001967 wpa->ia.ff = fuse_file_get(data->ff);
1968 spin_lock(&fi->lock);
1969 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001970 fuse_flush_writepages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001971 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001972
1973 for (i = 0; i < num_pages; i++)
1974 end_page_writeback(data->orig_pages[i]);
1975}
1976
David Brazdil0f672f62019-12-10 10:32:29 +00001977/*
1978 * First recheck under fi->lock if the offending offset is still under
1979 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1980 * one already added for a page at this offset. If there's none, then insert
1981 * this new request onto the auxiliary list, otherwise reuse the existing one by
1982 * copying the new page contents over to the old temporary page.
1983 */
1984static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001985 struct page *page)
1986{
David Brazdil0f672f62019-12-10 10:32:29 +00001987 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1988 struct fuse_writepage_args *tmp;
1989 struct fuse_writepage_args *old_wpa;
1990 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001991
David Brazdil0f672f62019-12-10 10:32:29 +00001992 WARN_ON(new_ap->num_pages != 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001993
David Brazdil0f672f62019-12-10 10:32:29 +00001994 spin_lock(&fi->lock);
1995 list_del(&new_wpa->writepages_entry);
1996 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1997 if (!old_wpa) {
1998 list_add(&new_wpa->writepages_entry, &fi->writepages);
1999 spin_unlock(&fi->lock);
2000 return false;
2001 }
2002
2003 new_ap->num_pages = 1;
2004 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2005 pgoff_t curr_index;
2006
2007 WARN_ON(tmp->inode != new_wpa->inode);
2008 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2009 if (curr_index == page->index) {
2010 WARN_ON(tmp->ia.ap.num_pages != 1);
2011 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002012 break;
2013 }
2014 }
David Brazdil0f672f62019-12-10 10:32:29 +00002015
2016 if (!tmp) {
2017 new_wpa->next = old_wpa->next;
2018 old_wpa->next = new_wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002019 }
2020
David Brazdil0f672f62019-12-10 10:32:29 +00002021 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002022
David Brazdil0f672f62019-12-10 10:32:29 +00002023 if (tmp) {
2024 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002025
2026 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
David Brazdil0f672f62019-12-10 10:32:29 +00002027 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002028 wb_writeout_inc(&bdi->wb);
David Brazdil0f672f62019-12-10 10:32:29 +00002029 fuse_writepage_free(new_wpa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002030 }
David Brazdil0f672f62019-12-10 10:32:29 +00002031
2032 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002033}
2034
2035static int fuse_writepages_fill(struct page *page,
2036 struct writeback_control *wbc, void *_data)
2037{
2038 struct fuse_fill_wb_data *data = _data;
David Brazdil0f672f62019-12-10 10:32:29 +00002039 struct fuse_writepage_args *wpa = data->wpa;
2040 struct fuse_args_pages *ap = &wpa->ia.ap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002041 struct inode *inode = data->inode;
David Brazdil0f672f62019-12-10 10:32:29 +00002042 struct fuse_inode *fi = get_fuse_inode(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002043 struct fuse_conn *fc = get_fuse_conn(inode);
2044 struct page *tmp_page;
2045 bool is_writeback;
2046 int err;
2047
2048 if (!data->ff) {
2049 err = -EIO;
David Brazdil0f672f62019-12-10 10:32:29 +00002050 data->ff = fuse_write_file_get(fc, fi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002051 if (!data->ff)
2052 goto out_unlock;
2053 }
2054
2055 /*
2056 * Being under writeback is unlikely but possible. For example direct
2057 * read to an mmaped fuse file will set the page dirty twice; once when
2058 * the pages are faulted with get_user_pages(), and then after the read
2059 * completed.
2060 */
2061 is_writeback = fuse_page_is_writeback(inode, page->index);
2062
David Brazdil0f672f62019-12-10 10:32:29 +00002063 if (wpa && ap->num_pages &&
2064 (is_writeback || ap->num_pages == fc->max_pages ||
2065 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2066 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002067 fuse_writepages_send(data);
David Brazdil0f672f62019-12-10 10:32:29 +00002068 data->wpa = NULL;
2069 } else if (wpa && ap->num_pages == data->max_pages) {
2070 if (!fuse_pages_realloc(data)) {
2071 fuse_writepages_send(data);
2072 data->wpa = NULL;
2073 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002074 }
David Brazdil0f672f62019-12-10 10:32:29 +00002075
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002076 err = -ENOMEM;
2077 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2078 if (!tmp_page)
2079 goto out_unlock;
2080
2081 /*
2082 * The page must not be redirtied until the writeout is completed
2083 * (i.e. userspace has sent a reply to the write request). Otherwise
2084 * there could be more than one temporary page instance for each real
2085 * page.
2086 *
2087 * This is ensured by holding the page lock in page_mkwrite() while
2088 * checking fuse_page_is_writeback(). We already hold the page lock
2089 * since clear_page_dirty_for_io() and keep it held until we add the
David Brazdil0f672f62019-12-10 10:32:29 +00002090 * request to the fi->writepages list and increment ap->num_pages.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002091 * After this fuse_page_is_writeback() will indicate that the page is
2092 * under writeback, so we can release the page lock.
2093 */
David Brazdil0f672f62019-12-10 10:32:29 +00002094 if (data->wpa == NULL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002095 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002096 wpa = fuse_writepage_args_alloc();
2097 if (!wpa) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002098 __free_page(tmp_page);
2099 goto out_unlock;
2100 }
David Brazdil0f672f62019-12-10 10:32:29 +00002101 data->max_pages = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002102
David Brazdil0f672f62019-12-10 10:32:29 +00002103 ap = &wpa->ia.ap;
2104 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2105 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2106 wpa->next = NULL;
2107 ap->args.in_pages = true;
2108 ap->args.end = fuse_writepage_end;
2109 ap->num_pages = 0;
2110 wpa->inode = inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002111
David Brazdil0f672f62019-12-10 10:32:29 +00002112 spin_lock(&fi->lock);
2113 list_add(&wpa->writepages_entry, &fi->writepages);
2114 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002115
David Brazdil0f672f62019-12-10 10:32:29 +00002116 data->wpa = wpa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002117 }
2118 set_page_writeback(page);
2119
2120 copy_highpage(tmp_page, page);
David Brazdil0f672f62019-12-10 10:32:29 +00002121 ap->pages[ap->num_pages] = tmp_page;
2122 ap->descs[ap->num_pages].offset = 0;
2123 ap->descs[ap->num_pages].length = PAGE_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002124
2125 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2126 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2127
2128 err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002129 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002130 end_page_writeback(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002131 data->wpa = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002132 goto out_unlock;
2133 }
David Brazdil0f672f62019-12-10 10:32:29 +00002134 data->orig_pages[ap->num_pages] = page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002135
2136 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002137 * Protected by fi->lock against concurrent access by
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002138 * fuse_page_is_writeback().
2139 */
David Brazdil0f672f62019-12-10 10:32:29 +00002140 spin_lock(&fi->lock);
2141 ap->num_pages++;
2142 spin_unlock(&fi->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002143
2144out_unlock:
2145 unlock_page(page);
2146
2147 return err;
2148}
2149
2150static int fuse_writepages(struct address_space *mapping,
2151 struct writeback_control *wbc)
2152{
2153 struct inode *inode = mapping->host;
David Brazdil0f672f62019-12-10 10:32:29 +00002154 struct fuse_conn *fc = get_fuse_conn(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002155 struct fuse_fill_wb_data data;
2156 int err;
2157
2158 err = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02002159 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002160 goto out;
2161
2162 data.inode = inode;
David Brazdil0f672f62019-12-10 10:32:29 +00002163 data.wpa = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002164 data.ff = NULL;
2165
2166 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002167 data.orig_pages = kcalloc(fc->max_pages,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002168 sizeof(struct page *),
2169 GFP_NOFS);
2170 if (!data.orig_pages)
2171 goto out;
2172
2173 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
David Brazdil0f672f62019-12-10 10:32:29 +00002174 if (data.wpa) {
David Brazdil0f672f62019-12-10 10:32:29 +00002175 WARN_ON(!data.wpa->ia.ap.num_pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176 fuse_writepages_send(&data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002177 }
2178 if (data.ff)
2179 fuse_file_put(data.ff, false, false);
2180
2181 kfree(data.orig_pages);
2182out:
2183 return err;
2184}
2185
2186/*
2187 * It's worthy to make sure that space is reserved on disk for the write,
2188 * but how to implement it without killing performance need more thinking.
2189 */
2190static int fuse_write_begin(struct file *file, struct address_space *mapping,
2191 loff_t pos, unsigned len, unsigned flags,
2192 struct page **pagep, void **fsdata)
2193{
2194 pgoff_t index = pos >> PAGE_SHIFT;
2195 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2196 struct page *page;
2197 loff_t fsize;
2198 int err = -ENOMEM;
2199
2200 WARN_ON(!fc->writeback_cache);
2201
2202 page = grab_cache_page_write_begin(mapping, index, flags);
2203 if (!page)
2204 goto error;
2205
2206 fuse_wait_on_page_writeback(mapping->host, page->index);
2207
2208 if (PageUptodate(page) || len == PAGE_SIZE)
2209 goto success;
2210 /*
2211 * Check if the start this page comes after the end of file, in which
2212 * case the readpage can be optimized away.
2213 */
2214 fsize = i_size_read(mapping->host);
2215 if (fsize <= (pos & PAGE_MASK)) {
2216 size_t off = pos & ~PAGE_MASK;
2217 if (off)
2218 zero_user_segment(page, 0, off);
2219 goto success;
2220 }
2221 err = fuse_do_readpage(file, page);
2222 if (err)
2223 goto cleanup;
2224success:
2225 *pagep = page;
2226 return 0;
2227
2228cleanup:
2229 unlock_page(page);
2230 put_page(page);
2231error:
2232 return err;
2233}
2234
2235static int fuse_write_end(struct file *file, struct address_space *mapping,
2236 loff_t pos, unsigned len, unsigned copied,
2237 struct page *page, void *fsdata)
2238{
2239 struct inode *inode = page->mapping->host;
2240
2241 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2242 if (!copied)
2243 goto unlock;
2244
2245 if (!PageUptodate(page)) {
2246 /* Zero any unwritten bytes at the end of the page */
2247 size_t endoff = (pos + copied) & ~PAGE_MASK;
2248 if (endoff)
2249 zero_user_segment(page, endoff, PAGE_SIZE);
2250 SetPageUptodate(page);
2251 }
2252
2253 fuse_write_update_size(inode, pos + copied);
2254 set_page_dirty(page);
2255
2256unlock:
2257 unlock_page(page);
2258 put_page(page);
2259
2260 return copied;
2261}
2262
2263static int fuse_launder_page(struct page *page)
2264{
2265 int err = 0;
2266 if (clear_page_dirty_for_io(page)) {
2267 struct inode *inode = page->mapping->host;
2268 err = fuse_writepage_locked(page);
2269 if (!err)
2270 fuse_wait_on_page_writeback(inode, page->index);
2271 }
2272 return err;
2273}
2274
2275/*
2276 * Write back dirty pages now, because there may not be any suitable
2277 * open files later
2278 */
2279static void fuse_vma_close(struct vm_area_struct *vma)
2280{
2281 filemap_write_and_wait(vma->vm_file->f_mapping);
2282}
2283
2284/*
2285 * Wait for writeback against this page to complete before allowing it
2286 * to be marked dirty again, and hence written back again, possibly
2287 * before the previous writepage completed.
2288 *
2289 * Block here, instead of in ->writepage(), so that the userspace fs
2290 * can only block processes actually operating on the filesystem.
2291 *
2292 * Otherwise unprivileged userspace fs would be able to block
2293 * unrelated:
2294 *
2295 * - page migration
2296 * - sync(2)
2297 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2298 */
2299static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2300{
2301 struct page *page = vmf->page;
2302 struct inode *inode = file_inode(vmf->vma->vm_file);
2303
2304 file_update_time(vmf->vma->vm_file);
2305 lock_page(page);
2306 if (page->mapping != inode->i_mapping) {
2307 unlock_page(page);
2308 return VM_FAULT_NOPAGE;
2309 }
2310
2311 fuse_wait_on_page_writeback(inode, page->index);
2312 return VM_FAULT_LOCKED;
2313}
2314
2315static const struct vm_operations_struct fuse_file_vm_ops = {
2316 .close = fuse_vma_close,
2317 .fault = filemap_fault,
2318 .map_pages = filemap_map_pages,
2319 .page_mkwrite = fuse_page_mkwrite,
2320};
2321
2322static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2323{
David Brazdil0f672f62019-12-10 10:32:29 +00002324 struct fuse_file *ff = file->private_data;
2325
2326 if (ff->open_flags & FOPEN_DIRECT_IO) {
2327 /* Can't provide the coherency needed for MAP_SHARED */
2328 if (vma->vm_flags & VM_MAYSHARE)
2329 return -ENODEV;
2330
2331 invalidate_inode_pages2(file->f_mapping);
2332
2333 return generic_file_mmap(file, vma);
2334 }
2335
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002336 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2337 fuse_link_write_file(file);
2338
2339 file_accessed(file);
2340 vma->vm_ops = &fuse_file_vm_ops;
2341 return 0;
2342}
2343
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002344static int convert_fuse_file_lock(struct fuse_conn *fc,
2345 const struct fuse_file_lock *ffl,
2346 struct file_lock *fl)
2347{
2348 switch (ffl->type) {
2349 case F_UNLCK:
2350 break;
2351
2352 case F_RDLCK:
2353 case F_WRLCK:
2354 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2355 ffl->end < ffl->start)
2356 return -EIO;
2357
2358 fl->fl_start = ffl->start;
2359 fl->fl_end = ffl->end;
2360
2361 /*
2362 * Convert pid into init's pid namespace. The locks API will
2363 * translate it into the caller's pid namespace.
2364 */
2365 rcu_read_lock();
2366 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2367 rcu_read_unlock();
2368 break;
2369
2370 default:
2371 return -EIO;
2372 }
2373 fl->fl_type = ffl->type;
2374 return 0;
2375}
2376
2377static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2378 const struct file_lock *fl, int opcode, pid_t pid,
2379 int flock, struct fuse_lk_in *inarg)
2380{
2381 struct inode *inode = file_inode(file);
2382 struct fuse_conn *fc = get_fuse_conn(inode);
2383 struct fuse_file *ff = file->private_data;
2384
2385 memset(inarg, 0, sizeof(*inarg));
2386 inarg->fh = ff->fh;
2387 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2388 inarg->lk.start = fl->fl_start;
2389 inarg->lk.end = fl->fl_end;
2390 inarg->lk.type = fl->fl_type;
2391 inarg->lk.pid = pid;
2392 if (flock)
2393 inarg->lk_flags |= FUSE_LK_FLOCK;
David Brazdil0f672f62019-12-10 10:32:29 +00002394 args->opcode = opcode;
2395 args->nodeid = get_node_id(inode);
2396 args->in_numargs = 1;
2397 args->in_args[0].size = sizeof(*inarg);
2398 args->in_args[0].value = inarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002399}
2400
2401static int fuse_getlk(struct file *file, struct file_lock *fl)
2402{
2403 struct inode *inode = file_inode(file);
2404 struct fuse_conn *fc = get_fuse_conn(inode);
2405 FUSE_ARGS(args);
2406 struct fuse_lk_in inarg;
2407 struct fuse_lk_out outarg;
2408 int err;
2409
2410 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
David Brazdil0f672f62019-12-10 10:32:29 +00002411 args.out_numargs = 1;
2412 args.out_args[0].size = sizeof(outarg);
2413 args.out_args[0].value = &outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002414 err = fuse_simple_request(fc, &args);
2415 if (!err)
2416 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2417
2418 return err;
2419}
2420
2421static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2422{
2423 struct inode *inode = file_inode(file);
2424 struct fuse_conn *fc = get_fuse_conn(inode);
2425 FUSE_ARGS(args);
2426 struct fuse_lk_in inarg;
2427 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2428 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2429 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2430 int err;
2431
2432 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2433 /* NLM needs asynchronous locks, which we don't support yet */
2434 return -ENOLCK;
2435 }
2436
2437 /* Unlock on close is handled by the flush method */
2438 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2439 return 0;
2440
2441 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2442 err = fuse_simple_request(fc, &args);
2443
2444 /* locking is restartable */
2445 if (err == -EINTR)
2446 err = -ERESTARTSYS;
2447
2448 return err;
2449}
2450
2451static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2452{
2453 struct inode *inode = file_inode(file);
2454 struct fuse_conn *fc = get_fuse_conn(inode);
2455 int err;
2456
2457 if (cmd == F_CANCELLK) {
2458 err = 0;
2459 } else if (cmd == F_GETLK) {
2460 if (fc->no_lock) {
2461 posix_test_lock(file, fl);
2462 err = 0;
2463 } else
2464 err = fuse_getlk(file, fl);
2465 } else {
2466 if (fc->no_lock)
2467 err = posix_lock_file(file, fl, NULL);
2468 else
2469 err = fuse_setlk(file, fl, 0);
2470 }
2471 return err;
2472}
2473
2474static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2475{
2476 struct inode *inode = file_inode(file);
2477 struct fuse_conn *fc = get_fuse_conn(inode);
2478 int err;
2479
2480 if (fc->no_flock) {
2481 err = locks_lock_file_wait(file, fl);
2482 } else {
2483 struct fuse_file *ff = file->private_data;
2484
2485 /* emulate flock with POSIX locks */
2486 ff->flock = true;
2487 err = fuse_setlk(file, fl, 1);
2488 }
2489
2490 return err;
2491}
2492
2493static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2494{
2495 struct inode *inode = mapping->host;
2496 struct fuse_conn *fc = get_fuse_conn(inode);
2497 FUSE_ARGS(args);
2498 struct fuse_bmap_in inarg;
2499 struct fuse_bmap_out outarg;
2500 int err;
2501
2502 if (!inode->i_sb->s_bdev || fc->no_bmap)
2503 return 0;
2504
2505 memset(&inarg, 0, sizeof(inarg));
2506 inarg.block = block;
2507 inarg.blocksize = inode->i_sb->s_blocksize;
David Brazdil0f672f62019-12-10 10:32:29 +00002508 args.opcode = FUSE_BMAP;
2509 args.nodeid = get_node_id(inode);
2510 args.in_numargs = 1;
2511 args.in_args[0].size = sizeof(inarg);
2512 args.in_args[0].value = &inarg;
2513 args.out_numargs = 1;
2514 args.out_args[0].size = sizeof(outarg);
2515 args.out_args[0].value = &outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002516 err = fuse_simple_request(fc, &args);
2517 if (err == -ENOSYS)
2518 fc->no_bmap = 1;
2519
2520 return err ? 0 : outarg.block;
2521}
2522
2523static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2524{
2525 struct inode *inode = file->f_mapping->host;
2526 struct fuse_conn *fc = get_fuse_conn(inode);
2527 struct fuse_file *ff = file->private_data;
2528 FUSE_ARGS(args);
2529 struct fuse_lseek_in inarg = {
2530 .fh = ff->fh,
2531 .offset = offset,
2532 .whence = whence
2533 };
2534 struct fuse_lseek_out outarg;
2535 int err;
2536
2537 if (fc->no_lseek)
2538 goto fallback;
2539
David Brazdil0f672f62019-12-10 10:32:29 +00002540 args.opcode = FUSE_LSEEK;
2541 args.nodeid = ff->nodeid;
2542 args.in_numargs = 1;
2543 args.in_args[0].size = sizeof(inarg);
2544 args.in_args[0].value = &inarg;
2545 args.out_numargs = 1;
2546 args.out_args[0].size = sizeof(outarg);
2547 args.out_args[0].value = &outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002548 err = fuse_simple_request(fc, &args);
2549 if (err) {
2550 if (err == -ENOSYS) {
2551 fc->no_lseek = 1;
2552 goto fallback;
2553 }
2554 return err;
2555 }
2556
2557 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2558
2559fallback:
2560 err = fuse_update_attributes(inode, file);
2561 if (!err)
2562 return generic_file_llseek(file, offset, whence);
2563 else
2564 return err;
2565}
2566
2567static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2568{
2569 loff_t retval;
2570 struct inode *inode = file_inode(file);
2571
2572 switch (whence) {
2573 case SEEK_SET:
2574 case SEEK_CUR:
2575 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2576 retval = generic_file_llseek(file, offset, whence);
2577 break;
2578 case SEEK_END:
2579 inode_lock(inode);
2580 retval = fuse_update_attributes(inode, file);
2581 if (!retval)
2582 retval = generic_file_llseek(file, offset, whence);
2583 inode_unlock(inode);
2584 break;
2585 case SEEK_HOLE:
2586 case SEEK_DATA:
2587 inode_lock(inode);
2588 retval = fuse_lseek(file, offset, whence);
2589 inode_unlock(inode);
2590 break;
2591 default:
2592 retval = -EINVAL;
2593 }
2594
2595 return retval;
2596}
2597
2598/*
2599 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2600 * ABI was defined to be 'struct iovec' which is different on 32bit
2601 * and 64bit. Fortunately we can determine which structure the server
2602 * used from the size of the reply.
2603 */
2604static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2605 size_t transferred, unsigned count,
2606 bool is_compat)
2607{
2608#ifdef CONFIG_COMPAT
2609 if (count * sizeof(struct compat_iovec) == transferred) {
2610 struct compat_iovec *ciov = src;
2611 unsigned i;
2612
2613 /*
2614 * With this interface a 32bit server cannot support
2615 * non-compat (i.e. ones coming from 64bit apps) ioctl
2616 * requests
2617 */
2618 if (!is_compat)
2619 return -EINVAL;
2620
2621 for (i = 0; i < count; i++) {
2622 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2623 dst[i].iov_len = ciov[i].iov_len;
2624 }
2625 return 0;
2626 }
2627#endif
2628
2629 if (count * sizeof(struct iovec) != transferred)
2630 return -EIO;
2631
2632 memcpy(dst, src, transferred);
2633 return 0;
2634}
2635
2636/* Make sure iov_length() won't overflow */
David Brazdil0f672f62019-12-10 10:32:29 +00002637static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2638 size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002639{
2640 size_t n;
David Brazdil0f672f62019-12-10 10:32:29 +00002641 u32 max = fc->max_pages << PAGE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002642
2643 for (n = 0; n < count; n++, iov++) {
2644 if (iov->iov_len > (size_t) max)
2645 return -ENOMEM;
2646 max -= iov->iov_len;
2647 }
2648 return 0;
2649}
2650
2651static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2652 void *src, size_t transferred, unsigned count,
2653 bool is_compat)
2654{
2655 unsigned i;
2656 struct fuse_ioctl_iovec *fiov = src;
2657
2658 if (fc->minor < 16) {
2659 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2660 count, is_compat);
2661 }
2662
2663 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2664 return -EIO;
2665
2666 for (i = 0; i < count; i++) {
2667 /* Did the server supply an inappropriate value? */
2668 if (fiov[i].base != (unsigned long) fiov[i].base ||
2669 fiov[i].len != (unsigned long) fiov[i].len)
2670 return -EIO;
2671
2672 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2673 dst[i].iov_len = (size_t) fiov[i].len;
2674
2675#ifdef CONFIG_COMPAT
2676 if (is_compat &&
2677 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2678 (compat_size_t) dst[i].iov_len != fiov[i].len))
2679 return -EIO;
2680#endif
2681 }
2682
2683 return 0;
2684}
2685
2686
2687/*
2688 * For ioctls, there is no generic way to determine how much memory
2689 * needs to be read and/or written. Furthermore, ioctls are allowed
2690 * to dereference the passed pointer, so the parameter requires deep
2691 * copying but FUSE has no idea whatsoever about what to copy in or
2692 * out.
2693 *
2694 * This is solved by allowing FUSE server to retry ioctl with
2695 * necessary in/out iovecs. Let's assume the ioctl implementation
2696 * needs to read in the following structure.
2697 *
2698 * struct a {
2699 * char *buf;
2700 * size_t buflen;
2701 * }
2702 *
2703 * On the first callout to FUSE server, inarg->in_size and
2704 * inarg->out_size will be NULL; then, the server completes the ioctl
2705 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2706 * the actual iov array to
2707 *
2708 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2709 *
2710 * which tells FUSE to copy in the requested area and retry the ioctl.
2711 * On the second round, the server has access to the structure and
2712 * from that it can tell what to look for next, so on the invocation,
2713 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2714 *
2715 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2716 * { .iov_base = a.buf, .iov_len = a.buflen } }
2717 *
2718 * FUSE will copy both struct a and the pointed buffer from the
2719 * process doing the ioctl and retry ioctl with both struct a and the
2720 * buffer.
2721 *
2722 * This time, FUSE server has everything it needs and completes ioctl
2723 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2724 *
2725 * Copying data out works the same way.
2726 *
2727 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2728 * automatically initializes in and out iovs by decoding @cmd with
2729 * _IOC_* macros and the server is not allowed to request RETRY. This
2730 * limits ioctl data transfers to well-formed ioctls and is the forced
2731 * behavior for all FUSE servers.
2732 */
2733long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2734 unsigned int flags)
2735{
2736 struct fuse_file *ff = file->private_data;
2737 struct fuse_conn *fc = ff->fc;
2738 struct fuse_ioctl_in inarg = {
2739 .fh = ff->fh,
2740 .cmd = cmd,
2741 .arg = arg,
2742 .flags = flags
2743 };
2744 struct fuse_ioctl_out outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002745 struct iovec *iov_page = NULL;
2746 struct iovec *in_iov = NULL, *out_iov = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00002747 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2748 size_t in_size, out_size, c;
2749 ssize_t transferred;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002750 int err, i;
2751 struct iov_iter ii;
David Brazdil0f672f62019-12-10 10:32:29 +00002752 struct fuse_args_pages ap = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002753
2754#if BITS_PER_LONG == 32
2755 inarg.flags |= FUSE_IOCTL_32BIT;
2756#else
David Brazdil0f672f62019-12-10 10:32:29 +00002757 if (flags & FUSE_IOCTL_COMPAT) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002758 inarg.flags |= FUSE_IOCTL_32BIT;
David Brazdil0f672f62019-12-10 10:32:29 +00002759#ifdef CONFIG_X86_X32
2760 if (in_x32_syscall())
2761 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2762#endif
2763 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002764#endif
2765
2766 /* assume all the iovs returned by client always fits in a page */
2767 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2768
2769 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002770 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002771 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +00002772 if (!ap.pages || !iov_page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002773 goto out;
2774
David Brazdil0f672f62019-12-10 10:32:29 +00002775 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2776
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002777 /*
2778 * If restricted, initialize IO parameters as encoded in @cmd.
2779 * RETRY from server is not allowed.
2780 */
2781 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2782 struct iovec *iov = iov_page;
2783
2784 iov->iov_base = (void __user *)arg;
Olivier Deprez0e641232021-09-23 10:07:05 +02002785
2786 switch (cmd) {
2787 case FS_IOC_GETFLAGS:
2788 case FS_IOC_SETFLAGS:
2789 iov->iov_len = sizeof(int);
2790 break;
2791 default:
2792 iov->iov_len = _IOC_SIZE(cmd);
2793 break;
2794 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002795
2796 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2797 in_iov = iov;
2798 in_iovs = 1;
2799 }
2800
2801 if (_IOC_DIR(cmd) & _IOC_READ) {
2802 out_iov = iov;
2803 out_iovs = 1;
2804 }
2805 }
2806
2807 retry:
2808 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2809 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2810
2811 /*
2812 * Out data can be used either for actual out data or iovs,
2813 * make sure there always is at least one page.
2814 */
2815 out_size = max_t(size_t, out_size, PAGE_SIZE);
2816 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2817
2818 /* make sure there are enough buffer pages and init request with them */
2819 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00002820 if (max_pages > fc->max_pages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002821 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00002822 while (ap.num_pages < max_pages) {
2823 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2824 if (!ap.pages[ap.num_pages])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002825 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00002826 ap.num_pages++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002827 }
2828
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002829
2830 /* okay, let's send it to the client */
David Brazdil0f672f62019-12-10 10:32:29 +00002831 ap.args.opcode = FUSE_IOCTL;
2832 ap.args.nodeid = ff->nodeid;
2833 ap.args.in_numargs = 1;
2834 ap.args.in_args[0].size = sizeof(inarg);
2835 ap.args.in_args[0].value = &inarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002836 if (in_size) {
David Brazdil0f672f62019-12-10 10:32:29 +00002837 ap.args.in_numargs++;
2838 ap.args.in_args[1].size = in_size;
2839 ap.args.in_pages = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002840
2841 err = -EFAULT;
2842 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
David Brazdil0f672f62019-12-10 10:32:29 +00002843 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2844 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002845 if (c != PAGE_SIZE && iov_iter_count(&ii))
2846 goto out;
2847 }
2848 }
2849
David Brazdil0f672f62019-12-10 10:32:29 +00002850 ap.args.out_numargs = 2;
2851 ap.args.out_args[0].size = sizeof(outarg);
2852 ap.args.out_args[0].value = &outarg;
2853 ap.args.out_args[1].size = out_size;
2854 ap.args.out_pages = true;
2855 ap.args.out_argvar = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002856
David Brazdil0f672f62019-12-10 10:32:29 +00002857 transferred = fuse_simple_request(fc, &ap.args);
2858 err = transferred;
2859 if (transferred < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002860 goto out;
2861
2862 /* did it ask for retry? */
2863 if (outarg.flags & FUSE_IOCTL_RETRY) {
2864 void *vaddr;
2865
2866 /* no retry if in restricted mode */
2867 err = -EIO;
2868 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2869 goto out;
2870
2871 in_iovs = outarg.in_iovs;
2872 out_iovs = outarg.out_iovs;
2873
2874 /*
2875 * Make sure things are in boundary, separate checks
2876 * are to protect against overflow.
2877 */
2878 err = -ENOMEM;
2879 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2880 out_iovs > FUSE_IOCTL_MAX_IOV ||
2881 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2882 goto out;
2883
David Brazdil0f672f62019-12-10 10:32:29 +00002884 vaddr = kmap_atomic(ap.pages[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002885 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2886 transferred, in_iovs + out_iovs,
2887 (flags & FUSE_IOCTL_COMPAT) != 0);
2888 kunmap_atomic(vaddr);
2889 if (err)
2890 goto out;
2891
2892 in_iov = iov_page;
2893 out_iov = in_iov + in_iovs;
2894
David Brazdil0f672f62019-12-10 10:32:29 +00002895 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002896 if (err)
2897 goto out;
2898
David Brazdil0f672f62019-12-10 10:32:29 +00002899 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002900 if (err)
2901 goto out;
2902
2903 goto retry;
2904 }
2905
2906 err = -EIO;
2907 if (transferred > inarg.out_size)
2908 goto out;
2909
2910 err = -EFAULT;
2911 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
David Brazdil0f672f62019-12-10 10:32:29 +00002912 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2913 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002914 if (c != PAGE_SIZE && iov_iter_count(&ii))
2915 goto out;
2916 }
2917 err = 0;
2918 out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002919 free_page((unsigned long) iov_page);
David Brazdil0f672f62019-12-10 10:32:29 +00002920 while (ap.num_pages)
2921 __free_page(ap.pages[--ap.num_pages]);
2922 kfree(ap.pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002923
2924 return err ? err : outarg.result;
2925}
2926EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2927
2928long fuse_ioctl_common(struct file *file, unsigned int cmd,
2929 unsigned long arg, unsigned int flags)
2930{
2931 struct inode *inode = file_inode(file);
2932 struct fuse_conn *fc = get_fuse_conn(inode);
2933
2934 if (!fuse_allow_current_process(fc))
2935 return -EACCES;
2936
Olivier Deprez0e641232021-09-23 10:07:05 +02002937 if (fuse_is_bad(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002938 return -EIO;
2939
2940 return fuse_do_ioctl(file, cmd, arg, flags);
2941}
2942
2943static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2944 unsigned long arg)
2945{
2946 return fuse_ioctl_common(file, cmd, arg, 0);
2947}
2948
2949static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2950 unsigned long arg)
2951{
2952 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2953}
2954
2955/*
2956 * All files which have been polled are linked to RB tree
2957 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2958 * find the matching one.
2959 */
2960static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2961 struct rb_node **parent_out)
2962{
2963 struct rb_node **link = &fc->polled_files.rb_node;
2964 struct rb_node *last = NULL;
2965
2966 while (*link) {
2967 struct fuse_file *ff;
2968
2969 last = *link;
2970 ff = rb_entry(last, struct fuse_file, polled_node);
2971
2972 if (kh < ff->kh)
2973 link = &last->rb_left;
2974 else if (kh > ff->kh)
2975 link = &last->rb_right;
2976 else
2977 return link;
2978 }
2979
2980 if (parent_out)
2981 *parent_out = last;
2982 return link;
2983}
2984
2985/*
2986 * The file is about to be polled. Make sure it's on the polled_files
2987 * RB tree. Note that files once added to the polled_files tree are
2988 * not removed before the file is released. This is because a file
2989 * polled once is likely to be polled again.
2990 */
2991static void fuse_register_polled_file(struct fuse_conn *fc,
2992 struct fuse_file *ff)
2993{
2994 spin_lock(&fc->lock);
2995 if (RB_EMPTY_NODE(&ff->polled_node)) {
2996 struct rb_node **link, *uninitialized_var(parent);
2997
2998 link = fuse_find_polled_node(fc, ff->kh, &parent);
2999 BUG_ON(*link);
3000 rb_link_node(&ff->polled_node, parent, link);
3001 rb_insert_color(&ff->polled_node, &fc->polled_files);
3002 }
3003 spin_unlock(&fc->lock);
3004}
3005
3006__poll_t fuse_file_poll(struct file *file, poll_table *wait)
3007{
3008 struct fuse_file *ff = file->private_data;
3009 struct fuse_conn *fc = ff->fc;
3010 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
3011 struct fuse_poll_out outarg;
3012 FUSE_ARGS(args);
3013 int err;
3014
3015 if (fc->no_poll)
3016 return DEFAULT_POLLMASK;
3017
3018 poll_wait(file, &ff->poll_wait, wait);
3019 inarg.events = mangle_poll(poll_requested_events(wait));
3020
3021 /*
3022 * Ask for notification iff there's someone waiting for it.
3023 * The client may ignore the flag and always notify.
3024 */
3025 if (waitqueue_active(&ff->poll_wait)) {
3026 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
3027 fuse_register_polled_file(fc, ff);
3028 }
3029
David Brazdil0f672f62019-12-10 10:32:29 +00003030 args.opcode = FUSE_POLL;
3031 args.nodeid = ff->nodeid;
3032 args.in_numargs = 1;
3033 args.in_args[0].size = sizeof(inarg);
3034 args.in_args[0].value = &inarg;
3035 args.out_numargs = 1;
3036 args.out_args[0].size = sizeof(outarg);
3037 args.out_args[0].value = &outarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003038 err = fuse_simple_request(fc, &args);
3039
3040 if (!err)
3041 return demangle_poll(outarg.revents);
3042 if (err == -ENOSYS) {
3043 fc->no_poll = 1;
3044 return DEFAULT_POLLMASK;
3045 }
3046 return EPOLLERR;
3047}
3048EXPORT_SYMBOL_GPL(fuse_file_poll);
3049
3050/*
3051 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3052 * wakes up the poll waiters.
3053 */
3054int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3055 struct fuse_notify_poll_wakeup_out *outarg)
3056{
3057 u64 kh = outarg->kh;
3058 struct rb_node **link;
3059
3060 spin_lock(&fc->lock);
3061
3062 link = fuse_find_polled_node(fc, kh, NULL);
3063 if (*link) {
3064 struct fuse_file *ff;
3065
3066 ff = rb_entry(*link, struct fuse_file, polled_node);
3067 wake_up_interruptible_sync(&ff->poll_wait);
3068 }
3069
3070 spin_unlock(&fc->lock);
3071 return 0;
3072}
3073
3074static void fuse_do_truncate(struct file *file)
3075{
3076 struct inode *inode = file->f_mapping->host;
3077 struct iattr attr;
3078
3079 attr.ia_valid = ATTR_SIZE;
3080 attr.ia_size = i_size_read(inode);
3081
3082 attr.ia_file = file;
3083 attr.ia_valid |= ATTR_FILE;
3084
3085 fuse_do_setattr(file_dentry(file), &attr, file);
3086}
3087
David Brazdil0f672f62019-12-10 10:32:29 +00003088static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003089{
David Brazdil0f672f62019-12-10 10:32:29 +00003090 return round_up(off, fc->max_pages << PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003091}
3092
3093static ssize_t
3094fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3095{
3096 DECLARE_COMPLETION_ONSTACK(wait);
3097 ssize_t ret = 0;
3098 struct file *file = iocb->ki_filp;
3099 struct fuse_file *ff = file->private_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003100 loff_t pos = 0;
3101 struct inode *inode;
3102 loff_t i_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02003103 size_t count = iov_iter_count(iter), shortened = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003104 loff_t offset = iocb->ki_pos;
3105 struct fuse_io_priv *io;
3106
3107 pos = offset;
3108 inode = file->f_mapping->host;
3109 i_size = i_size_read(inode);
3110
Olivier Deprez0e641232021-09-23 10:07:05 +02003111 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003112 return 0;
3113
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003114 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3115 if (!io)
3116 return -ENOMEM;
3117 spin_lock_init(&io->lock);
3118 kref_init(&io->refcnt);
3119 io->reqs = 1;
3120 io->bytes = -1;
3121 io->size = 0;
3122 io->offset = offset;
3123 io->write = (iov_iter_rw(iter) == WRITE);
3124 io->err = 0;
3125 /*
3126 * By default, we want to optimize all I/Os with async request
3127 * submission to the client filesystem if supported.
3128 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003129 io->async = ff->fc->async_dio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003130 io->iocb = iocb;
3131 io->blocking = is_sync_kiocb(iocb);
3132
Olivier Deprez0e641232021-09-23 10:07:05 +02003133 /* optimization for short read */
3134 if (io->async && !io->write && offset + count > i_size) {
3135 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3136 shortened = count - iov_iter_count(iter);
3137 count -= shortened;
3138 }
3139
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003140 /*
3141 * We cannot asynchronously extend the size of a file.
3142 * In such case the aio will behave exactly like sync io.
3143 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003144 if ((offset + count > i_size) && io->write)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003145 io->blocking = true;
3146
3147 if (io->async && io->blocking) {
3148 /*
3149 * Additional reference to keep io around after
3150 * calling fuse_aio_complete()
3151 */
3152 kref_get(&io->refcnt);
3153 io->done = &wait;
3154 }
3155
3156 if (iov_iter_rw(iter) == WRITE) {
3157 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3158 fuse_invalidate_attr(inode);
3159 } else {
3160 ret = __fuse_direct_read(io, iter, &pos);
3161 }
Olivier Deprez0e641232021-09-23 10:07:05 +02003162 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003163
3164 if (io->async) {
3165 bool blocking = io->blocking;
3166
3167 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3168
3169 /* we have a non-extending, async request, so return */
3170 if (!blocking)
3171 return -EIOCBQUEUED;
3172
3173 wait_for_completion(&wait);
3174 ret = fuse_get_res_by_io(io);
3175 }
3176
3177 kref_put(&io->refcnt, fuse_io_release);
3178
3179 if (iov_iter_rw(iter) == WRITE) {
3180 if (ret > 0)
3181 fuse_write_update_size(inode, pos);
3182 else if (ret < 0 && offset + count > i_size)
3183 fuse_do_truncate(file);
3184 }
3185
3186 return ret;
3187}
3188
David Brazdil0f672f62019-12-10 10:32:29 +00003189static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3190{
Olivier Deprez0e641232021-09-23 10:07:05 +02003191 int err = filemap_write_and_wait_range(inode->i_mapping, start, -1);
David Brazdil0f672f62019-12-10 10:32:29 +00003192
3193 if (!err)
3194 fuse_sync_writes(inode);
3195
3196 return err;
3197}
3198
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003199static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3200 loff_t length)
3201{
3202 struct fuse_file *ff = file->private_data;
3203 struct inode *inode = file_inode(file);
3204 struct fuse_inode *fi = get_fuse_inode(inode);
3205 struct fuse_conn *fc = ff->fc;
3206 FUSE_ARGS(args);
3207 struct fuse_fallocate_in inarg = {
3208 .fh = ff->fh,
3209 .offset = offset,
3210 .length = length,
3211 .mode = mode
3212 };
3213 int err;
3214 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3215 (mode & FALLOC_FL_PUNCH_HOLE);
3216
3217 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3218 return -EOPNOTSUPP;
3219
3220 if (fc->no_fallocate)
3221 return -EOPNOTSUPP;
3222
3223 if (lock_inode) {
3224 inode_lock(inode);
3225 if (mode & FALLOC_FL_PUNCH_HOLE) {
3226 loff_t endbyte = offset + length - 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003227
3228 err = fuse_writeback_range(inode, offset, endbyte);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003229 if (err)
3230 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003231 }
3232 }
3233
David Brazdil0f672f62019-12-10 10:32:29 +00003234 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3235 offset + length > i_size_read(inode)) {
3236 err = inode_newsize_ok(inode, offset + length);
3237 if (err)
3238 goto out;
3239 }
3240
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003241 if (!(mode & FALLOC_FL_KEEP_SIZE))
3242 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3243
David Brazdil0f672f62019-12-10 10:32:29 +00003244 args.opcode = FUSE_FALLOCATE;
3245 args.nodeid = ff->nodeid;
3246 args.in_numargs = 1;
3247 args.in_args[0].size = sizeof(inarg);
3248 args.in_args[0].value = &inarg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003249 err = fuse_simple_request(fc, &args);
3250 if (err == -ENOSYS) {
3251 fc->no_fallocate = 1;
3252 err = -EOPNOTSUPP;
3253 }
3254 if (err)
3255 goto out;
3256
3257 /* we could have extended the file */
3258 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3259 bool changed = fuse_write_update_size(inode, offset + length);
3260
3261 if (changed && fc->writeback_cache)
3262 file_update_time(file);
3263 }
3264
3265 if (mode & FALLOC_FL_PUNCH_HOLE)
3266 truncate_pagecache_range(inode, offset, offset + length - 1);
3267
3268 fuse_invalidate_attr(inode);
3269
3270out:
3271 if (!(mode & FALLOC_FL_KEEP_SIZE))
3272 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3273
3274 if (lock_inode)
3275 inode_unlock(inode);
3276
3277 return err;
3278}
3279
David Brazdil0f672f62019-12-10 10:32:29 +00003280static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3281 struct file *file_out, loff_t pos_out,
3282 size_t len, unsigned int flags)
3283{
3284 struct fuse_file *ff_in = file_in->private_data;
3285 struct fuse_file *ff_out = file_out->private_data;
3286 struct inode *inode_in = file_inode(file_in);
3287 struct inode *inode_out = file_inode(file_out);
3288 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3289 struct fuse_conn *fc = ff_in->fc;
3290 FUSE_ARGS(args);
3291 struct fuse_copy_file_range_in inarg = {
3292 .fh_in = ff_in->fh,
3293 .off_in = pos_in,
3294 .nodeid_out = ff_out->nodeid,
3295 .fh_out = ff_out->fh,
3296 .off_out = pos_out,
3297 .len = len,
3298 .flags = flags
3299 };
3300 struct fuse_write_out outarg;
3301 ssize_t err;
3302 /* mark unstable when write-back is not used, and file_out gets
3303 * extended */
3304 bool is_unstable = (!fc->writeback_cache) &&
3305 ((pos_out + len) > inode_out->i_size);
3306
3307 if (fc->no_copy_file_range)
3308 return -EOPNOTSUPP;
3309
3310 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3311 return -EXDEV;
3312
Olivier Deprez0e641232021-09-23 10:07:05 +02003313 inode_lock(inode_in);
3314 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3315 inode_unlock(inode_in);
3316 if (err)
3317 return err;
David Brazdil0f672f62019-12-10 10:32:29 +00003318
3319 inode_lock(inode_out);
3320
3321 err = file_modified(file_out);
3322 if (err)
3323 goto out;
3324
Olivier Deprez0e641232021-09-23 10:07:05 +02003325 /*
3326 * Write out dirty pages in the destination file before sending the COPY
3327 * request to userspace. After the request is completed, truncate off
3328 * pages (including partial ones) from the cache that have been copied,
3329 * since these contain stale data at that point.
3330 *
3331 * This should be mostly correct, but if the COPY writes to partial
3332 * pages (at the start or end) and the parts not covered by the COPY are
3333 * written through a memory map after calling fuse_writeback_range(),
3334 * then these partial page modifications will be lost on truncation.
3335 *
3336 * It is unlikely that someone would rely on such mixed style
3337 * modifications. Yet this does give less guarantees than if the
3338 * copying was performed with write(2).
3339 *
3340 * To fix this a i_mmap_sem style lock could be used to prevent new
3341 * faults while the copy is ongoing.
3342 */
3343 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3344 if (err)
3345 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00003346
3347 if (is_unstable)
3348 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3349
3350 args.opcode = FUSE_COPY_FILE_RANGE;
3351 args.nodeid = ff_in->nodeid;
3352 args.in_numargs = 1;
3353 args.in_args[0].size = sizeof(inarg);
3354 args.in_args[0].value = &inarg;
3355 args.out_numargs = 1;
3356 args.out_args[0].size = sizeof(outarg);
3357 args.out_args[0].value = &outarg;
3358 err = fuse_simple_request(fc, &args);
3359 if (err == -ENOSYS) {
3360 fc->no_copy_file_range = 1;
3361 err = -EOPNOTSUPP;
3362 }
3363 if (err)
3364 goto out;
3365
Olivier Deprez0e641232021-09-23 10:07:05 +02003366 truncate_inode_pages_range(inode_out->i_mapping,
3367 ALIGN_DOWN(pos_out, PAGE_SIZE),
3368 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3369
David Brazdil0f672f62019-12-10 10:32:29 +00003370 if (fc->writeback_cache) {
3371 fuse_write_update_size(inode_out, pos_out + outarg.size);
3372 file_update_time(file_out);
3373 }
3374
3375 fuse_invalidate_attr(inode_out);
3376
3377 err = outarg.size;
3378out:
3379 if (is_unstable)
3380 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3381
3382 inode_unlock(inode_out);
3383 file_accessed(file_in);
3384
3385 return err;
3386}
3387
3388static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3389 struct file *dst_file, loff_t dst_off,
3390 size_t len, unsigned int flags)
3391{
3392 ssize_t ret;
3393
3394 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3395 len, flags);
3396
3397 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3398 ret = generic_copy_file_range(src_file, src_off, dst_file,
3399 dst_off, len, flags);
3400 return ret;
3401}
3402
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003403static const struct file_operations fuse_file_operations = {
3404 .llseek = fuse_file_llseek,
3405 .read_iter = fuse_file_read_iter,
3406 .write_iter = fuse_file_write_iter,
3407 .mmap = fuse_file_mmap,
3408 .open = fuse_open,
3409 .flush = fuse_flush,
3410 .release = fuse_release,
3411 .fsync = fuse_fsync,
3412 .lock = fuse_file_lock,
3413 .flock = fuse_file_flock,
3414 .splice_read = generic_file_splice_read,
David Brazdil0f672f62019-12-10 10:32:29 +00003415 .splice_write = iter_file_splice_write,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003416 .unlocked_ioctl = fuse_file_ioctl,
3417 .compat_ioctl = fuse_file_compat_ioctl,
3418 .poll = fuse_file_poll,
3419 .fallocate = fuse_file_fallocate,
David Brazdil0f672f62019-12-10 10:32:29 +00003420 .copy_file_range = fuse_copy_file_range,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003421};
3422
3423static const struct address_space_operations fuse_file_aops = {
3424 .readpage = fuse_readpage,
3425 .writepage = fuse_writepage,
3426 .writepages = fuse_writepages,
3427 .launder_page = fuse_launder_page,
3428 .readpages = fuse_readpages,
3429 .set_page_dirty = __set_page_dirty_nobuffers,
3430 .bmap = fuse_bmap,
3431 .direct_IO = fuse_direct_IO,
3432 .write_begin = fuse_write_begin,
3433 .write_end = fuse_write_end,
3434};
3435
3436void fuse_init_file_inode(struct inode *inode)
3437{
David Brazdil0f672f62019-12-10 10:32:29 +00003438 struct fuse_inode *fi = get_fuse_inode(inode);
3439
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003440 inode->i_fop = &fuse_file_operations;
3441 inode->i_data.a_ops = &fuse_file_aops;
David Brazdil0f672f62019-12-10 10:32:29 +00003442
3443 INIT_LIST_HEAD(&fi->write_files);
3444 INIT_LIST_HEAD(&fi->queued_writes);
3445 fi->writectr = 0;
3446 init_waitqueue_head(&fi->page_waitq);
3447 INIT_LIST_HEAD(&fi->writepages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003448}