blob: 9d6c5f6361f7d907cefafa60ee0f14c2450c5165 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050015#include <linux/sched/signal.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090016#include <linux/module.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040018#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Chirantan Ekbote31070f62020-07-14 19:26:39 +090020#include <linux/fs.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021
Miklos Szeredib9d54c62021-04-07 14:36:45 +020022static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
23 unsigned int open_flags, int opcode,
24 struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010027 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredib9d54c62021-04-07 14:36:45 +020030 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
Max Reitzfcee2162020-05-06 17:44:12 +020031 if (!fm->fc->atomic_o_trunc)
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070032 inarg.flags &= ~O_TRUNC;
Vivek Goyal643a6662020-10-09 14:15:11 -040033
34 if (fm->fc->handle_killpriv_v2 &&
35 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
36 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
37 }
38
Miklos Szeredid5b48542019-09-10 15:04:08 +020039 args.opcode = opcode;
40 args.nodeid = nodeid;
41 args.in_numargs = 1;
42 args.in_args[0].size = sizeof(inarg);
43 args.in_args[0].value = &inarg;
44 args.out_numargs = 1;
45 args.out_args[0].size = sizeof(*outargp);
46 args.out_args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047
Max Reitzfcee2162020-05-06 17:44:12 +020048 return fuse_simple_request(fm, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080049}
50
Miklos Szeredi4cb54862019-09-10 15:04:10 +020051struct fuse_release_args {
52 struct fuse_args args;
53 struct fuse_release_in inarg;
54 struct inode *inode;
55};
56
Max Reitzfcee2162020-05-06 17:44:12 +020057struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080058{
59 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090060
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070061 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
Tejun Heo6b2db282009-04-14 10:54:49 +090062 if (unlikely(!ff))
63 return NULL;
64
Max Reitzfcee2162020-05-06 17:44:12 +020065 ff->fm = fm;
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070066 ff->release_args = kzalloc(sizeof(*ff->release_args),
67 GFP_KERNEL_ACCOUNT);
Miklos Szeredi4cb54862019-09-10 15:04:10 +020068 if (!ff->release_args) {
Tejun Heo6b2db282009-04-14 10:54:49 +090069 kfree(ff);
70 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080071 }
Tejun Heo6b2db282009-04-14 10:54:49 +090072
73 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020074 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020075 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090076 RB_CLEAR_NODE(&ff->polled_node);
77 init_waitqueue_head(&ff->poll_wait);
78
Max Reitzfcee2162020-05-06 17:44:12 +020079 ff->kh = atomic64_inc_return(&fm->fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090080
Miklos Szeredifd72faa2005-11-07 00:59:51 -080081 return ff;
82}
83
84void fuse_file_free(struct fuse_file *ff)
85{
Miklos Szeredi4cb54862019-09-10 15:04:10 +020086 kfree(ff->release_args);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020087 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080088 kfree(ff);
89}
90
Miklos Szeredi267d8442017-02-22 20:08:25 +010091static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070092{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020093 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070094 return ff;
95}
96
Max Reitzfcee2162020-05-06 17:44:12 +020097static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi4cb54862019-09-10 15:04:10 +020098 int error)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010099{
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200100 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
101
102 iput(ra->inode);
103 kfree(ra);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104}
105
Chad Austin2e64ff12018-12-10 10:54:52 -0800106static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700107{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200108 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200109 struct fuse_args *args = &ff->release_args->args;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200110
Max Reitzfcee2162020-05-06 17:44:12 +0200111 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200112 /* Do nothing when client does not implement 'open' */
Max Reitzfcee2162020-05-06 17:44:12 +0200113 fuse_release_end(ff->fm, args, 0);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100114 } else if (sync) {
Max Reitzfcee2162020-05-06 17:44:12 +0200115 fuse_simple_request(ff->fm, args);
116 fuse_release_end(ff->fm, args, 0);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100117 } else {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200118 args->end = fuse_release_end;
Max Reitzfcee2162020-05-06 17:44:12 +0200119 if (fuse_simple_background(ff->fm, args,
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200120 GFP_KERNEL | __GFP_NOFAIL))
Max Reitzfcee2162020-05-06 17:44:12 +0200121 fuse_release_end(ff->fm, args, -ENOTCONN);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100122 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700123 kfree(ff);
124 }
125}
126
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200127struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
128 unsigned int open_flags, bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200129{
Max Reitzfcee2162020-05-06 17:44:12 +0200130 struct fuse_conn *fc = fm->fc;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200131 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200132 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
133
Max Reitzfcee2162020-05-06 17:44:12 +0200134 ff = fuse_file_alloc(fm);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200135 if (!ff)
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200136 return ERR_PTR(-ENOMEM);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200137
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100138 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800139 /* Default for no-open */
140 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800141 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100142 struct fuse_open_out outarg;
143 int err;
144
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200145 err = fuse_send_open(fm, nodeid, open_flags, opcode, &outarg);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100146 if (!err) {
147 ff->fh = outarg.fh;
148 ff->open_flags = outarg.open_flags;
149
Chad Austind9a9ea92019-01-07 16:53:17 -0800150 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100151 fuse_file_free(ff);
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200152 return ERR_PTR(err);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100153 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800154 if (isdir)
155 fc->no_opendir = 1;
156 else
157 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100158 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200159 }
160
161 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200163
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200164 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200165
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200166 return ff;
167}
168
169int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
170 bool isdir)
171{
172 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
173
174 if (!IS_ERR(ff))
175 file->private_data = ff;
176
177 return PTR_ERR_OR_ZERO(ff);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200178}
Tejun Heo08cbf542009-04-14 10:54:53 +0900179EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200180
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400181static void fuse_link_write_file(struct file *file)
182{
183 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400184 struct fuse_inode *fi = get_fuse_inode(inode);
185 struct fuse_file *ff = file->private_data;
186 /*
187 * file may be written through mmap, so chain it onto the
188 * inodes's write_file list
189 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300190 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400191 if (list_empty(&ff->write_entry))
192 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300193 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400194}
195
Miklos Szeredic7b71432009-04-28 16:56:37 +0200196void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800197{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200198 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800199 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200200
Kirill Smelkovbbd84f32019-04-24 07:13:57 +0000201 if (ff->open_flags & FOPEN_STREAM)
202 stream_open(inode, file);
203 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200204 nonseekable_open(inode, file);
Miklos Szeredi76224352021-08-17 21:05:16 +0200205
Ken Sumralla0822c52010-11-24 12:57:00 -0800206 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
207 struct fuse_inode *fi = get_fuse_inode(inode);
208
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300209 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300210 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800211 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300212 spin_unlock(&fi->lock);
Miklos Szeredi76224352021-08-17 21:05:16 +0200213 truncate_pagecache(inode, 0);
Miklos Szeredi20235b42021-10-22 17:03:03 +0200214 file_update_time(file);
Miklos Szeredifa5eee52021-10-22 17:03:02 +0200215 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
Miklos Szeredi76224352021-08-17 21:05:16 +0200216 } else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) {
217 invalidate_inode_pages2(inode->i_mapping);
Ken Sumralla0822c52010-11-24 12:57:00 -0800218 }
Miklos Szeredi76224352021-08-17 21:05:16 +0200219
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400220 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
221 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800222}
223
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200224int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800225{
Max Reitzfcee2162020-05-06 17:44:12 +0200226 struct fuse_mount *fm = get_fuse_mount(inode);
227 struct fuse_conn *fc = fm->fc;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700228 int err;
Miklos Szeredie4648302019-10-23 14:26:37 +0200229 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200230 fc->atomic_o_trunc &&
231 fc->writeback_cache;
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400232 bool dax_truncate = (file->f_flags & O_TRUNC) &&
233 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100235 if (fuse_is_bad(inode))
236 return -EIO;
237
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700238 err = generic_file_open(inode, file);
239 if (err)
240 return err;
241
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400242 if (is_wb_truncate || dax_truncate) {
Al Viro59551022016-01-22 15:40:57 -0500243 inode_lock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200244 fuse_set_nowrite(inode);
245 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200246
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400247 if (dax_truncate) {
Jan Kara8bcbbe92021-04-21 17:18:39 +0200248 filemap_invalidate_lock(inode->i_mapping);
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400249 err = fuse_dax_break_layouts(inode, 0, 0);
250 if (err)
251 goto out;
252 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700253
Max Reitzfcee2162020-05-06 17:44:12 +0200254 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200255 if (!err)
256 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200257
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400258out:
259 if (dax_truncate)
Jan Kara8bcbbe92021-04-21 17:18:39 +0200260 filemap_invalidate_unlock(inode->i_mapping);
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400261
262 if (is_wb_truncate | dax_truncate) {
Miklos Szeredie4648302019-10-23 14:26:37 +0200263 fuse_release_nowrite(inode);
Al Viro59551022016-01-22 15:40:57 -0500264 inode_unlock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200265 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200266
267 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700268}
269
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300270static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
Miklos Szeredi54d601c2021-04-07 14:36:45 +0200271 unsigned int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800272{
Max Reitzfcee2162020-05-06 17:44:12 +0200273 struct fuse_conn *fc = ff->fm->fc;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200274 struct fuse_release_args *ra = ff->release_args;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700275
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300276 /* Inode is NULL on error path of fuse_create_open() */
277 if (likely(fi)) {
278 spin_lock(&fi->lock);
279 list_del(&ff->write_entry);
280 spin_unlock(&fi->lock);
281 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200282 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200283 if (!RB_EMPTY_NODE(&ff->polled_node))
284 rb_erase(&ff->polled_node, &fc->polled_files);
285 spin_unlock(&fc->lock);
286
Bryan Green357ccf22011-03-01 16:43:52 -0800287 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200288
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200289 ra->inarg.fh = ff->fh;
290 ra->inarg.flags = flags;
291 ra->args.in_numargs = 1;
292 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
293 ra->args.in_args[0].value = &ra->inarg;
294 ra->args.opcode = opcode;
295 ra->args.nodeid = ff->nodeid;
296 ra->args.force = true;
297 ra->args.nocreds = true;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800298}
299
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200300void fuse_file_release(struct inode *inode, struct fuse_file *ff,
301 unsigned int open_flags, fl_owner_t id, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800302{
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200303 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200304 struct fuse_release_args *ra = ff->release_args;
Chad Austin2e64ff12018-12-10 10:54:52 -0800305 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700306
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200307 fuse_prepare_release(fi, ff, open_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100308
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200309 if (ff->flock) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200310 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200311 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200312 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100313 /* Hold inode until release is finished */
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200314 ra->inode = igrab(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700315
Tejun Heo6b2db282009-04-14 10:54:49 +0900316 /*
317 * Normally this will send the RELEASE request, however if
318 * some asynchronous READ or WRITE requests are outstanding,
319 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100320 *
321 * Make the release synchronous if this is a fuseblk mount,
322 * synchronous RELEASE is allowed (and desirable) in this case
323 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900324 */
Max Reitzfcee2162020-05-06 17:44:12 +0200325 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700326}
327
Miklos Szeredib9d54c62021-04-07 14:36:45 +0200328void fuse_release_common(struct file *file, bool isdir)
329{
330 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
331 (fl_owner_t) file, isdir);
332}
333
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700334static int fuse_open(struct inode *inode, struct file *file)
335{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200336 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700337}
338
339static int fuse_release(struct inode *inode, struct file *file)
340{
Chad Austin2e64ff12018-12-10 10:54:52 -0800341 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200342
343 /* return value is ignored by VFS */
344 return 0;
345}
346
Miklos Szeredi54d601c2021-04-07 14:36:45 +0200347void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
348 unsigned int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200349{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200350 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300351 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100352 /*
353 * iput(NULL) is a no-op and since the refcount is 1 and everything's
354 * synchronous, we are fine with not doing igrab() here"
355 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800356 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700357}
Tejun Heo08cbf542009-04-14 10:54:53 +0900358EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700359
Miklos Szeredi71421252006-06-25 05:48:52 -0700360/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700361 * Scramble the ID space with XTEA, so that the value of the files_struct
362 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700363 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700364u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700365{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700366 u32 *k = fc->scramble_key;
367 u64 v = (unsigned long) id;
368 u32 v0 = v;
369 u32 v1 = v >> 32;
370 u32 sum = 0;
371 int i;
372
373 for (i = 0; i < 32; i++) {
374 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
375 sum += 0x9E3779B9;
376 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
377 }
378
379 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700380}
381
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200382struct fuse_writepage_args {
383 struct fuse_io_args ia;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300384 struct rb_node writepages_entry;
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200385 struct list_head queue_entry;
386 struct fuse_writepage_args *next;
387 struct inode *inode;
Miklos Szeredi660585b2021-09-01 12:39:02 +0200388 struct fuse_sync_bucket *bucket;
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200389};
390
391static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100392 pgoff_t idx_from, pgoff_t idx_to)
393{
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300394 struct rb_node *n;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100395
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300396 n = fi->writepages.rb_node;
397
398 while (n) {
399 struct fuse_writepage_args *wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100400 pgoff_t curr_index;
401
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300402 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200403 WARN_ON(get_fuse_inode(wpa->inode) != fi);
404 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300405 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
406 n = n->rb_right;
407 else if (idx_to < curr_index)
408 n = n->rb_left;
409 else
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200410 return wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100411 }
412 return NULL;
413}
414
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700415/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400416 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700417 *
418 * This is currently done by walking the list of writepage requests
419 * for the inode, which can be pretty inefficient.
420 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400421static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
422 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700423{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700424 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100425 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700426
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300427 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100428 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300429 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700430
431 return found;
432}
433
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400434static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
435{
436 return fuse_range_is_writeback(inode, index, index);
437}
438
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700439/*
440 * Wait for page writeback to be completed.
441 *
442 * Since fuse doesn't rely on the VM writeback tracking, this has to
443 * use some other means.
444 */
Maxim Patlasov17b2cbe2019-07-22 10:17:17 +0300445static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700446{
447 struct fuse_inode *fi = get_fuse_inode(inode);
448
449 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700450}
451
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400452/*
453 * Wait for all pending writepages on the inode to finish.
454 *
455 * This is currently done by blocking further writes with FUSE_NOWRITE
456 * and waiting for all sent writes to complete.
457 *
458 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
459 * could conflict with truncation.
460 */
461static void fuse_sync_writes(struct inode *inode)
462{
463 fuse_set_nowrite(inode);
464 fuse_release_nowrite(inode);
465}
466
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700467static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468{
Al Viro6131ffa2013-02-27 16:59:05 -0500469 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +0200470 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472 struct fuse_flush_in inarg;
Miklos Szeredic500eba2019-09-10 15:04:08 +0200473 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700474 int err;
475
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100476 if (fuse_is_bad(inode))
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800477 return -EIO;
478
Amir Goldsteina390ccb2021-10-24 16:26:07 +0300479 if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
480 return 0;
481
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200482 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400483 if (err)
484 return err;
485
Al Viro59551022016-01-22 15:40:57 -0500486 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400487 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500488 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400489
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200490 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700491 if (err)
492 return err;
493
Miklos Szeredi614c0262020-05-19 14:50:37 +0200494 err = 0;
Max Reitzfcee2162020-05-06 17:44:12 +0200495 if (fm->fc->no_flush)
Miklos Szeredi614c0262020-05-19 14:50:37 +0200496 goto inval_attr_out;
497
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498 memset(&inarg, 0, sizeof(inarg));
499 inarg.fh = ff->fh;
Max Reitzfcee2162020-05-06 17:44:12 +0200500 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200501 args.opcode = FUSE_FLUSH;
502 args.nodeid = get_node_id(inode);
503 args.in_numargs = 1;
504 args.in_args[0].size = sizeof(inarg);
505 args.in_args[0].value = &inarg;
506 args.force = true;
507
Max Reitzfcee2162020-05-06 17:44:12 +0200508 err = fuse_simple_request(fm, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700509 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200510 fm->fc->no_flush = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 err = 0;
512 }
Eryu Guancf576c52020-05-12 10:29:04 +0800513
514inval_attr_out:
515 /*
516 * In memory i_blocks is not maintained by fuse, if writeback cache is
517 * enabled, i_blocks from cached attr may not be accurate.
518 */
Max Reitzfcee2162020-05-06 17:44:12 +0200519 if (!err && fm->fc->writeback_cache)
Miklos Szeredifa5eee52021-10-22 17:03:02 +0200520 fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700521 return err;
522}
523
Josef Bacik02c24a82011-07-16 20:44:56 -0400524int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100525 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200527 struct inode *inode = file->f_mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +0200528 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100530 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100532
533 memset(&inarg, 0, sizeof(inarg));
534 inarg.fh = ff->fh;
Alan Somers154603f2019-04-19 15:42:44 -0600535 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200536 args.opcode = opcode;
537 args.nodeid = get_node_id(inode);
538 args.in_numargs = 1;
539 args.in_args[0].size = sizeof(inarg);
540 args.in_args[0].value = &inarg;
Max Reitzfcee2162020-05-06 17:44:12 +0200541 return fuse_simple_request(fm, &args);
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100542}
543
544static int fuse_fsync(struct file *file, loff_t start, loff_t end,
545 int datasync)
546{
547 struct inode *inode = file->f_mapping->host;
548 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700549 int err;
550
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100551 if (fuse_is_bad(inode))
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800552 return -EIO;
553
Al Viro59551022016-01-22 15:40:57 -0500554 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400555
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700556 /*
557 * Start writeback against all dirty pages of the inode, then
558 * wait for all outstanding writes, before sending the FSYNC
559 * request.
560 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400561 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700562 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400563 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700564
565 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700566
567 /*
568 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400569 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700570 * We have to do this directly after fuse_sync_writes()
571 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400572 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700573 if (err)
574 goto out;
575
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200576 err = sync_inode_metadata(inode, 1);
577 if (err)
578 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700579
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100580 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200581 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400582
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100583 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700584 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100585 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700586 err = 0;
587 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400588out:
Al Viro59551022016-01-22 15:40:57 -0500589 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700590
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100591 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700592}
593
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200594void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
595 size_t count, int opcode)
596{
597 struct fuse_file *ff = file->private_data;
598 struct fuse_args *args = &ia->ap.args;
599
600 ia->read.in.fh = ff->fh;
601 ia->read.in.offset = pos;
602 ia->read.in.size = count;
603 ia->read.in.flags = file->f_flags;
604 args->opcode = opcode;
605 args->nodeid = ff->nodeid;
606 args->in_numargs = 1;
607 args->in_args[0].size = sizeof(ia->read.in);
608 args->in_args[0].value = &ia->read.in;
609 args->out_argvar = true;
610 args->out_numargs = 1;
611 args->out_args[0].size = count;
612}
613
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200614static void fuse_release_user_pages(struct fuse_args_pages *ap,
615 bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400616{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200617 unsigned int i;
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400618
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200619 for (i = 0; i < ap->num_pages; i++) {
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200620 if (should_dirty)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200621 set_page_dirty_lock(ap->pages[i]);
622 put_page(ap->pages[i]);
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400623 }
624}
625
Seth Forshee744742d2016-03-11 10:35:34 -0600626static void fuse_io_release(struct kref *kref)
627{
628 kfree(container_of(kref, struct fuse_io_priv, refcnt));
629}
630
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100631static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
632{
633 if (io->err)
634 return io->err;
635
636 if (io->bytes >= 0 && io->write)
637 return -EIO;
638
639 return io->bytes < 0 ? io->size : io->bytes;
640}
641
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400642/**
643 * In case of short read, the caller sets 'pos' to the position of
644 * actual end of fuse request in IO request. Otherwise, if bytes_requested
645 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
646 *
647 * An example:
Zheng Yongjunc4e0cd42021-06-04 09:46:17 +0800648 * User requested DIO read of 64K. It was split into two 32K fuse requests,
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400649 * both submitted asynchronously. The first of them was ACKed by userspace as
650 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
651 * second request was ACKed as short, e.g. only 1K was read, resulting in
652 * pos == 33K.
653 *
654 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
655 * will be equal to the length of the longest contiguous fragment of
656 * transferred data starting from the beginning of IO request.
657 */
658static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
659{
660 int left;
661
662 spin_lock(&io->lock);
663 if (err)
664 io->err = io->err ? : err;
665 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
666 io->bytes = pos;
667
668 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530669 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100670 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400671 spin_unlock(&io->lock);
672
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530673 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100674 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400675
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100676 if (res >= 0) {
677 struct inode *inode = file_inode(io->iocb->ki_filp);
678 struct fuse_conn *fc = get_fuse_conn(inode);
679 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400680
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300681 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300682 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300683 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400684 }
685
Jens Axboe6b19b762021-10-21 09:22:35 -0600686 io->iocb->ki_complete(io->iocb, res);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400687 }
Seth Forshee744742d2016-03-11 10:35:34 -0600688
689 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400690}
691
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200692static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
693 unsigned int npages)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400694{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200695 struct fuse_io_args *ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400696
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200697 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
698 if (ia) {
699 ia->io = io;
700 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
701 &ia->ap.descs);
702 if (!ia->ap.pages) {
703 kfree(ia);
704 ia = NULL;
705 }
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400706 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200707 return ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400708}
709
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200710static void fuse_io_free(struct fuse_io_args *ia)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400711{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200712 kfree(ia->ap.pages);
713 kfree(ia);
714}
715
Max Reitzfcee2162020-05-06 17:44:12 +0200716static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200717 int err)
718{
719 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
720 struct fuse_io_priv *io = ia->io;
721 ssize_t pos = -1;
722
723 fuse_release_user_pages(&ia->ap, io->should_dirty);
724
725 if (err) {
726 /* Nothing */
727 } else if (io->write) {
728 if (ia->write.out.size > ia->write.in.size) {
729 err = -EIO;
730 } else if (ia->write.in.size != ia->write.out.size) {
731 pos = ia->write.in.offset - io->offset +
732 ia->write.out.size;
733 }
734 } else {
735 u32 outsize = args->out_args[0].size;
736
737 if (ia->read.in.size != outsize)
738 pos = ia->read.in.offset - io->offset + outsize;
739 }
740
741 fuse_aio_complete(io, err, pos);
742 fuse_io_free(ia);
743}
744
Max Reitzfcee2162020-05-06 17:44:12 +0200745static ssize_t fuse_async_req_send(struct fuse_mount *fm,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200746 struct fuse_io_args *ia, size_t num_bytes)
747{
748 ssize_t err;
749 struct fuse_io_priv *io = ia->io;
750
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400751 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600752 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400753 io->size += num_bytes;
754 io->reqs++;
755 spin_unlock(&io->lock);
756
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200757 ia->ap.args.end = fuse_aio_complete_req;
Vivek Goyalbb737bb2020-04-20 17:01:34 +0200758 ia->ap.args.may_block = io->should_dirty;
Max Reitzfcee2162020-05-06 17:44:12 +0200759 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100760 if (err)
Max Reitzfcee2162020-05-06 17:44:12 +0200761 fuse_aio_complete_req(fm, &ia->ap.args, err);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400762
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100763 return num_bytes;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400764}
765
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200766static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
767 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700768{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200769 struct file *file = ia->io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200770 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +0200771 struct fuse_mount *fm = ff->fm;
Miklos Szeredif3332112007-10-18 03:07:04 -0700772
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200773 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700774 if (owner != NULL) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200775 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
Max Reitzfcee2162020-05-06 17:44:12 +0200776 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -0700777 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400778
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200779 if (ia->io->async)
Max Reitzfcee2162020-05-06 17:44:12 +0200780 return fuse_async_req_send(fm, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400781
Max Reitzfcee2162020-05-06 17:44:12 +0200782 return fuse_simple_request(fm, &ia->ap.args);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700783}
784
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700785static void fuse_read_update_size(struct inode *inode, loff_t size,
786 u64 attr_ver)
787{
788 struct fuse_conn *fc = get_fuse_conn(inode);
789 struct fuse_inode *fi = get_fuse_inode(inode);
790
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300791 spin_lock(&fi->lock);
Miklos Szeredi484ce652021-10-22 17:03:03 +0200792 if (attr_ver >= fi->attr_version && size < inode->i_size &&
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400793 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300794 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700795 i_size_write(inode, size);
796 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300797 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700798}
799
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200800static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200801 struct fuse_args_pages *ap)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400802{
Pavel Emelyanov83732002013-10-10 17:10:46 +0400803 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400804
Miklos Szeredia73d47f2021-04-14 10:40:56 +0200805 /*
806 * If writeback_cache is enabled, a short read means there's a hole in
807 * the file. Some data after the hole is in page cache, but has not
808 * reached the client fs yet. So the hole is not present there.
809 */
810 if (!fc->writeback_cache) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200811 loff_t pos = page_offset(ap->pages[0]) + num_read;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400812 fuse_read_update_size(inode, pos, attr_ver);
813 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400814}
815
Maxim Patlasov482fce52013-10-10 17:11:25 +0400816static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700817{
818 struct inode *inode = page->mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +0200819 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700820 loff_t pos = page_offset(page);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200821 struct fuse_page_desc desc = { .length = PAGE_SIZE };
822 struct fuse_io_args ia = {
823 .ap.args.page_zeroing = true,
824 .ap.args.out_pages = true,
825 .ap.num_pages = 1,
826 .ap.pages = &page,
827 .ap.descs = &desc,
828 };
829 ssize_t res;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700830 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800831
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700832 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300833 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700834 * page-cache page, so make sure we read a properly synced
835 * page.
836 */
837 fuse_wait_on_page_writeback(inode, page->index);
838
Max Reitzfcee2162020-05-06 17:44:12 +0200839 attr_ver = fuse_get_attr_version(fm->fc);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700840
Miklos Szeredi2f139822020-02-06 16:39:28 +0100841 /* Don't overflow end offset */
842 if (pos + (desc.length - 1) == LLONG_MAX)
843 desc.length--;
844
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200845 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
Max Reitzfcee2162020-05-06 17:44:12 +0200846 res = fuse_simple_request(fm, &ia.ap.args);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200847 if (res < 0)
848 return res;
849 /*
850 * Short read means EOF. If file size is larger, truncate it
851 */
852 if (res < desc.length)
Miklos Szeredi134831e2019-09-10 15:04:10 +0200853 fuse_short_read(inode, attr_ver, res, &ia.ap);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700854
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200855 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700856
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200857 return 0;
Maxim Patlasov482fce52013-10-10 17:11:25 +0400858}
859
860static int fuse_readpage(struct file *file, struct page *page)
861{
862 struct inode *inode = page->mapping->host;
863 int err;
864
865 err = -EIO;
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100866 if (fuse_is_bad(inode))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400867 goto out;
868
869 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800870 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700871 out:
872 unlock_page(page);
873 return err;
874}
875
Max Reitzfcee2162020-05-06 17:44:12 +0200876static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200877 int err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800879 int i;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200880 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
881 struct fuse_args_pages *ap = &ia->ap;
882 size_t count = ia->read.in.size;
883 size_t num_read = args->out_args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200884 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800885
Miklos Szeredi134831e2019-09-10 15:04:10 +0200886 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
887 mapping = ap->pages[i]->mapping;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200888
889 if (mapping) {
890 struct inode *inode = mapping->host;
891
892 /*
893 * Short read means EOF. If file size is larger, truncate it
894 */
Miklos Szeredi134831e2019-09-10 15:04:10 +0200895 if (!err && num_read < count)
896 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200897
Andrew Gallagher451418f2013-11-05 03:55:43 -0800898 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700899 }
900
Miklos Szeredi134831e2019-09-10 15:04:10 +0200901 for (i = 0; i < ap->num_pages; i++) {
902 struct page *page = ap->pages[i];
903
904 if (!err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700905 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800906 else
907 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700908 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300909 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700910 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200911 if (ia->ff)
912 fuse_file_put(ia->ff, false, false);
913
914 fuse_io_free(ia);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800915}
916
Miklos Szeredi134831e2019-09-10 15:04:10 +0200917static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800918{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200919 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +0200920 struct fuse_mount *fm = ff->fm;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200921 struct fuse_args_pages *ap = &ia->ap;
922 loff_t pos = page_offset(ap->pages[0]);
923 size_t count = ap->num_pages << PAGE_SHIFT;
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100924 ssize_t res;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200925 int err;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200926
Miklos Szeredi134831e2019-09-10 15:04:10 +0200927 ap->args.out_pages = true;
928 ap->args.page_zeroing = true;
929 ap->args.page_replace = true;
Miklos Szeredi2f139822020-02-06 16:39:28 +0100930
931 /* Don't overflow end offset */
932 if (pos + (count - 1) == LLONG_MAX) {
933 count--;
934 ap->descs[ap->num_pages - 1].length--;
935 }
936 WARN_ON((loff_t) (pos + count) < 0);
937
Miklos Szeredi134831e2019-09-10 15:04:10 +0200938 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Max Reitzfcee2162020-05-06 17:44:12 +0200939 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
940 if (fm->fc->async_read) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200941 ia->ff = fuse_file_get(ff);
942 ap->args.end = fuse_readpages_end;
Max Reitzfcee2162020-05-06 17:44:12 +0200943 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
Miklos Szeredi134831e2019-09-10 15:04:10 +0200944 if (!err)
945 return;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800946 } else {
Max Reitzfcee2162020-05-06 17:44:12 +0200947 res = fuse_simple_request(fm, &ap->args);
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100948 err = res < 0 ? res : 0;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800949 }
Max Reitzfcee2162020-05-06 17:44:12 +0200950 fuse_readpages_end(fm, &ap->args, err);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700951}
952
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700953static void fuse_readahead(struct readahead_control *rac)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700954{
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700955 struct inode *inode = rac->mapping->host;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700956 struct fuse_conn *fc = get_fuse_conn(inode);
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700957 unsigned int i, max_pages, nr_pages = 0;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700958
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100959 if (fuse_is_bad(inode))
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700960 return;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800961
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700962 max_pages = min_t(unsigned int, fc->max_pages,
963 fc->max_read / PAGE_SIZE);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700964
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700965 for (;;) {
966 struct fuse_io_args *ia;
967 struct fuse_args_pages *ap;
968
969 nr_pages = readahead_count(rac) - nr_pages;
970 if (nr_pages > max_pages)
971 nr_pages = max_pages;
972 if (nr_pages == 0)
973 break;
974 ia = fuse_io_alloc(NULL, nr_pages);
975 if (!ia)
976 return;
977 ap = &ia->ap;
978 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
979 for (i = 0; i < nr_pages; i++) {
980 fuse_wait_on_page_writeback(inode,
981 readahead_index(rac) + i);
982 ap->descs[i].length = PAGE_SIZE;
983 }
984 ap->num_pages = nr_pages;
985 fuse_send_readpages(ia, rac->file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700986 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700987}
988
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100989static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800990{
991 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400992 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800993
Brian Fostera8894272012-07-16 15:23:50 -0400994 /*
995 * In auto invalidate mode, always update attributes on read.
996 * Otherwise, only update if we attempt to read past EOF (to ensure
997 * i_size is up to date).
998 */
999 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -04001000 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001001 int err;
Miklos Szeredic6c745b2021-10-22 17:03:03 +02001002 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE);
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001003 if (err)
1004 return err;
1005 }
1006
Al Viro37c20f12014-04-02 14:47:09 -04001007 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001008}
1009
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001010static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1011 loff_t pos, size_t count)
1012{
1013 struct fuse_args *args = &ia->ap.args;
1014
1015 ia->write.in.fh = ff->fh;
1016 ia->write.in.offset = pos;
1017 ia->write.in.size = count;
1018 args->opcode = FUSE_WRITE;
1019 args->nodeid = ff->nodeid;
1020 args->in_numargs = 2;
Max Reitzfcee2162020-05-06 17:44:12 +02001021 if (ff->fm->fc->minor < 9)
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001022 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1023 else
1024 args->in_args[0].size = sizeof(ia->write.in);
1025 args->in_args[0].value = &ia->write.in;
1026 args->in_args[1].size = count;
1027 args->out_numargs = 1;
1028 args->out_args[0].size = sizeof(ia->write.out);
1029 args->out_args[0].value = &ia->write.out;
1030}
1031
1032static unsigned int fuse_write_flags(struct kiocb *iocb)
1033{
1034 unsigned int flags = iocb->ki_filp->f_flags;
1035
1036 if (iocb->ki_flags & IOCB_DSYNC)
1037 flags |= O_DSYNC;
1038 if (iocb->ki_flags & IOCB_SYNC)
1039 flags |= O_SYNC;
1040
1041 return flags;
1042}
1043
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001044static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1045 size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001046{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001047 struct kiocb *iocb = ia->io->iocb;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001048 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001049 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001050 struct fuse_mount *fm = ff->fm;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001051 struct fuse_write_in *inarg = &ia->write.in;
1052 ssize_t err;
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001053
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001054 fuse_write_args_fill(ia, ff, pos, count);
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001055 inarg->flags = fuse_write_flags(iocb);
Miklos Szeredif3332112007-10-18 03:07:04 -07001056 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001057 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
Max Reitzfcee2162020-05-06 17:44:12 +02001058 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -07001059 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001060
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001061 if (ia->io->async)
Max Reitzfcee2162020-05-06 17:44:12 +02001062 return fuse_async_req_send(fm, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001063
Max Reitzfcee2162020-05-06 17:44:12 +02001064 err = fuse_simple_request(fm, &ia->ap.args);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001065 if (!err && ia->write.out.size > count)
1066 err = -EIO;
1067
1068 return err ?: ia->write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001069}
1070
Miklos Szeredid3477392021-10-22 17:03:02 +02001071bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001072{
1073 struct fuse_conn *fc = get_fuse_conn(inode);
1074 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001075 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001076
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001077 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001078 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredid3477392021-10-22 17:03:02 +02001079 if (written > 0 && pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001080 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001081 ret = true;
1082 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001083 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001084
Miklos Szeredid3477392021-10-22 17:03:02 +02001085 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
1086
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001087 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001088}
1089
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001090static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1091 struct kiocb *iocb, struct inode *inode,
1092 loff_t pos, size_t count)
Nick Pigginea9b9902008-04-30 00:54:42 -07001093{
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001094 struct fuse_args_pages *ap = &ia->ap;
1095 struct file *file = iocb->ki_filp;
1096 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001097 struct fuse_mount *fm = ff->fm;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001098 unsigned int offset, i;
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001099 bool short_write;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001100 int err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001101
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001102 for (i = 0; i < ap->num_pages; i++)
1103 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Nick Pigginea9b9902008-04-30 00:54:42 -07001104
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001105 fuse_write_args_fill(ia, ff, pos, count);
1106 ia->write.in.flags = fuse_write_flags(iocb);
Vivek Goyalb8667392020-10-09 14:15:08 -04001107 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1108 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109
Max Reitzfcee2162020-05-06 17:44:12 +02001110 err = fuse_simple_request(fm, &ap->args);
Miklos Szeredi8aab3362019-11-12 11:49:04 +01001111 if (!err && ia->write.out.size > count)
1112 err = -EIO;
Nick Pigginea9b9902008-04-30 00:54:42 -07001113
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001114 short_write = ia->write.out.size < count;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001115 offset = ap->descs[0].offset;
1116 count = ia->write.out.size;
1117 for (i = 0; i < ap->num_pages; i++) {
1118 struct page *page = ap->pages[i];
1119
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001120 if (err) {
1121 ClearPageUptodate(page);
1122 } else {
1123 if (count >= PAGE_SIZE - offset)
1124 count -= PAGE_SIZE - offset;
1125 else {
1126 if (short_write)
1127 ClearPageUptodate(page);
1128 count = 0;
1129 }
1130 offset = 0;
1131 }
1132 if (ia->write.page_locked && (i == ap->num_pages - 1))
1133 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001134 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001135 }
1136
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001137 return err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001138}
1139
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001140static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001141 struct address_space *mapping,
1142 struct iov_iter *ii, loff_t pos,
1143 unsigned int max_pages)
Nick Pigginea9b9902008-04-30 00:54:42 -07001144{
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001145 struct fuse_args_pages *ap = &ia->ap;
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001147 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001148 size_t count = 0;
1149 int err;
1150
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001151 ap->args.in_pages = true;
1152 ap->descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001153
1154 do {
1155 size_t tmp;
1156 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001157 pgoff_t index = pos >> PAGE_SHIFT;
1158 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001159 iov_iter_count(ii));
1160
1161 bytes = min_t(size_t, bytes, fc->max_write - count);
1162
1163 again:
1164 err = -EFAULT;
Andreas Gruenbachera6294592021-08-02 14:54:16 +02001165 if (fault_in_iov_iter_readable(ii, bytes))
Nick Pigginea9b9902008-04-30 00:54:42 -07001166 break;
1167
1168 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001169 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 if (!page)
1171 break;
1172
anfei zhou931e80e2010-02-02 13:44:02 -08001173 if (mapping_writably_mapped(mapping))
1174 flush_dcache_page(page);
1175
Al Virof0b65f32021-04-30 10:26:41 -04001176 tmp = copy_page_from_iter_atomic(page, offset, bytes, ii);
Nick Pigginea9b9902008-04-30 00:54:42 -07001177 flush_dcache_page(page);
1178
1179 if (!tmp) {
1180 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001181 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001182 goto again;
1183 }
1184
1185 err = 0;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001186 ap->pages[ap->num_pages] = page;
1187 ap->descs[ap->num_pages].length = tmp;
1188 ap->num_pages++;
Nick Pigginea9b9902008-04-30 00:54:42 -07001189
Nick Pigginea9b9902008-04-30 00:54:42 -07001190 count += tmp;
1191 pos += tmp;
1192 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001193 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 offset = 0;
1195
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001196 /* If we copied full page, mark it uptodate */
1197 if (tmp == PAGE_SIZE)
1198 SetPageUptodate(page);
1199
1200 if (PageUptodate(page)) {
1201 unlock_page(page);
1202 } else {
1203 ia->write.page_locked = true;
1204 break;
1205 }
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001206 if (!fc->big_writes)
1207 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001208 } while (iov_iter_count(ii) && count < fc->max_write &&
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001209 ap->num_pages < max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001210
1211 return count > 0 ? count : err;
1212}
1213
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001214static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1215 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001216{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001217 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 ((pos + len - 1) >> PAGE_SHIFT) -
1219 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001220 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001221}
1222
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001223static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001224 struct address_space *mapping,
1225 struct iov_iter *ii, loff_t pos)
1226{
1227 struct inode *inode = mapping->host;
1228 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001229 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001230 int err = 0;
1231 ssize_t res = 0;
1232
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001233 if (inode->i_size < pos + iov_iter_count(ii))
1234 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1235
Nick Pigginea9b9902008-04-30 00:54:42 -07001236 do {
Nick Pigginea9b9902008-04-30 00:54:42 -07001237 ssize_t count;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001238 struct fuse_io_args ia = {};
1239 struct fuse_args_pages *ap = &ia.ap;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001240 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1241 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001242
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001243 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1244 if (!ap->pages) {
1245 err = -ENOMEM;
Nick Pigginea9b9902008-04-30 00:54:42 -07001246 break;
1247 }
1248
Vivek Goyal4f06dd92020-10-21 16:12:49 -04001249 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001250 if (count <= 0) {
1251 err = count;
1252 } else {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001253 err = fuse_send_write_pages(&ia, iocb, inode,
1254 pos, count);
Nick Pigginea9b9902008-04-30 00:54:42 -07001255 if (!err) {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001256 size_t num_written = ia.write.out.size;
1257
Nick Pigginea9b9902008-04-30 00:54:42 -07001258 res += num_written;
1259 pos += num_written;
1260
1261 /* break out of the loop on short write */
1262 if (num_written != count)
1263 err = -EIO;
1264 }
1265 }
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001266 kfree(ap->pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001267 } while (!err && iov_iter_count(ii));
1268
Miklos Szeredid3477392021-10-22 17:03:02 +02001269 fuse_write_update_attr(inode, pos, res);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001270 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001271
1272 return res > 0 ? res : err;
1273}
1274
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001275static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001276{
1277 struct file *file = iocb->ki_filp;
1278 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001279 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001280 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001281 struct inode *inode = mapping->host;
1282 ssize_t err;
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001283 struct fuse_conn *fc = get_fuse_conn(inode);
Anand Avati4273b792012-02-17 12:46:25 -05001284 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001285
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001286 if (fc->writeback_cache) {
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001287 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredic6c745b2021-10-22 17:03:03 +02001288 err = fuse_update_attributes(mapping->host, file,
1289 STATX_SIZE | STATX_MODE);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001290 if (err)
1291 return err;
1292
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001293 if (fc->handle_killpriv_v2 &&
1294 should_remove_suid(file_dentry(file))) {
1295 goto writethrough;
1296 }
1297
Al Viro84c3d552014-04-03 14:33:23 -04001298 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001299 }
1300
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001301writethrough:
Al Viro59551022016-01-22 15:40:57 -05001302 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001303
1304 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001305 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001306
Al Viro3309dd02015-04-09 12:55:47 -04001307 err = generic_write_checks(iocb, from);
1308 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001309 goto out;
1310
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001311 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001312 if (err)
1313 goto out;
1314
Josef Bacikc3b2da32012-03-26 09:59:21 -04001315 err = file_update_time(file);
1316 if (err)
1317 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001318
Al Viro2ba48ce2015-04-09 13:52:01 -04001319 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001320 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001321 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001322 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001323 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001324
Anand Avati4273b792012-02-17 12:46:25 -05001325 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001326
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001327 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001328 if (written_buffered < 0) {
1329 err = written_buffered;
1330 goto out;
1331 }
1332 endbyte = pos + written_buffered - 1;
1333
1334 err = filemap_write_and_wait_range(file->f_mapping, pos,
1335 endbyte);
1336 if (err)
1337 goto out;
1338
1339 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 pos >> PAGE_SHIFT,
1341 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001342
1343 written += written_buffered;
1344 iocb->ki_pos = pos + written_buffered;
1345 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001346 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001347 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001348 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001349 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001350out:
1351 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001352 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001353 if (written > 0)
1354 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001355
1356 return written ? written : err;
1357}
1358
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001359static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1360{
1361 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1362}
1363
1364static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1365 size_t max_size)
1366{
1367 return min(iov_iter_single_seg_count(ii), max_size);
1368}
1369
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001370static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1371 size_t *nbytesp, int write,
1372 unsigned int max_pages)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001373{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001374 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001375 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001376
Miklos Szeredif4975c62009-04-02 14:25:34 +02001377 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001378 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001379 unsigned long user_addr = fuse_get_user_addr(ii);
1380 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1381
Miklos Szeredif4975c62009-04-02 14:25:34 +02001382 if (write)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001383 ap->args.in_args[1].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001384 else
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001385 ap->args.out_args[0].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001386
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001387 iov_iter_advance(ii, frag_size);
1388 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001389 return 0;
1390 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001392 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001393 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001394 size_t start;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001395 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001396 *nbytesp - nbytes,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001397 max_pages - ap->num_pages,
Al Viroc7f38882014-06-18 20:34:33 -04001398 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001399 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001400 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001401
Al Viroc9c37e22014-03-16 16:08:30 -04001402 iov_iter_advance(ii, ret);
1403 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001404
Al Viroc9c37e22014-03-16 16:08:30 -04001405 ret += start;
Wu Bo6c886322021-05-25 15:40:47 +08001406 npages = DIV_ROUND_UP(ret, PAGE_SIZE);
Al Viroc9c37e22014-03-16 16:08:30 -04001407
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001408 ap->descs[ap->num_pages].offset = start;
1409 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001410
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001411 ap->num_pages += npages;
1412 ap->descs[ap->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001413 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001414 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001415
1416 if (write)
zhengbincabdb4f2020-01-14 20:39:45 +08001417 ap->args.in_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001418 else
zhengbincabdb4f2020-01-14 20:39:45 +08001419 ap->args.out_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001420
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001421 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001422
Ashish Samant2c932d42016-03-25 10:53:41 -07001423 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424}
1425
Al Virod22a9432014-03-16 15:50:47 -04001426ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1427 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001428{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001429 int write = flags & FUSE_DIO_WRITE;
1430 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001431 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001432 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001433 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001434 struct fuse_conn *fc = ff->fm->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001435 size_t nmax = write ? fc->max_write : fc->max_read;
1436 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001437 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001438 pgoff_t idx_from = pos >> PAGE_SHIFT;
1439 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440 ssize_t res = 0;
Ashish Samant742f9922016-03-14 21:57:35 -07001441 int err = 0;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001442 struct fuse_io_args *ia;
1443 unsigned int max_pages;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001444
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001445 max_pages = iov_iter_npages(iter, fc->max_pages);
1446 ia = fuse_io_alloc(io, max_pages);
1447 if (!ia)
1448 return -ENOMEM;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001449
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001450 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1451 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001452 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001453 fuse_sync_writes(inode);
1454 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001455 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001456 }
1457
Ashish Samant61c12b42017-07-12 19:26:58 -07001458 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001459 while (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001460 ssize_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001461 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001462 size_t nbytes = min(count, nmax);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001463
1464 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1465 max_pages);
Ashish Samant742f9922016-03-14 21:57:35 -07001466 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001467 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001468
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001469 if (write) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001470 if (!capable(CAP_FSETID))
Miklos Szeredi10c52c82020-11-11 17:22:32 +01001471 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001472
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001473 nres = fuse_send_write(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001474 } else {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001475 nres = fuse_send_read(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001476 }
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001477
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001478 if (!io->async || nres < 0) {
1479 fuse_release_user_pages(&ia->ap, io->should_dirty);
1480 fuse_io_free(ia);
1481 }
1482 ia = NULL;
1483 if (nres < 0) {
Miklos Szeredif658ade2020-02-06 16:39:28 +01001484 iov_iter_revert(iter, nbytes);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001485 err = nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001486 break;
1487 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001488 WARN_ON(nres > nbytes);
1489
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001490 count -= nres;
1491 res += nres;
1492 pos += nres;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001493 if (nres != nbytes) {
1494 iov_iter_revert(iter, nbytes - nres);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001495 break;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001496 }
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001497 if (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001498 max_pages = iov_iter_npages(iter, fc->max_pages);
1499 ia = fuse_io_alloc(io, max_pages);
1500 if (!ia)
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001501 break;
1502 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001503 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001504 if (ia)
1505 fuse_io_free(ia);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001506 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001507 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001508
Ashish Samant742f9922016-03-14 21:57:35 -07001509 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001510}
Tejun Heo08cbf542009-04-14 10:54:53 +09001511EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001512
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001513static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001514 struct iov_iter *iter,
1515 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001516{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001517 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001518 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001519
Al Virod22a9432014-03-16 15:50:47 -04001520 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001521
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001522 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001523
1524 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001525}
1526
Martin Raiber23c94e12018-10-27 16:48:48 +00001527static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1528
Al Viro153162632015-03-30 22:08:36 -04001529static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001530{
Martin Raiber23c94e12018-10-27 16:48:48 +00001531 ssize_t res;
1532
1533 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001534 res = fuse_direct_IO(iocb, to);
1535 } else {
1536 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1537
1538 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1539 }
1540
1541 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001542}
1543
Al Viro153162632015-03-30 22:08:36 -04001544static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001545{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001546 struct inode *inode = file_inode(iocb->ki_filp);
1547 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001548 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001549
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001550 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001551 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001552 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001553 if (res > 0) {
1554 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1555 res = fuse_direct_IO(iocb, from);
1556 } else {
1557 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1558 FUSE_DIO_WRITE);
Miklos Szeredid3477392021-10-22 17:03:02 +02001559 fuse_write_update_attr(inode, iocb->ki_pos, res);
Martin Raiber23c94e12018-10-27 16:48:48 +00001560 }
1561 }
Al Viro59551022016-01-22 15:40:57 -05001562 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001563
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001564 return res;
1565}
1566
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001567static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1568{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001569 struct file *file = iocb->ki_filp;
1570 struct fuse_file *ff = file->private_data;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001571 struct inode *inode = file_inode(file);
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001572
Miklos Szeredi5d069db2020-12-10 15:33:14 +01001573 if (fuse_is_bad(inode))
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001574 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001575
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001576 if (FUSE_IS_DAX(inode))
1577 return fuse_dax_read_iter(iocb, to);
1578
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001579 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1580 return fuse_cache_read_iter(iocb, to);
1581 else
1582 return fuse_direct_read_iter(iocb, to);
1583}
1584
1585static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1586{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001587 struct file *file = iocb->ki_filp;
1588 struct fuse_file *ff = file->private_data;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001589 struct inode *inode = file_inode(file);
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001590
Miklos Szeredi5d069db2020-12-10 15:33:14 +01001591 if (fuse_is_bad(inode))
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001592 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001593
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001594 if (FUSE_IS_DAX(inode))
1595 return fuse_dax_write_iter(iocb, from);
1596
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001597 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1598 return fuse_cache_write_iter(iocb, from);
1599 else
1600 return fuse_direct_write_iter(iocb, from);
1601}
1602
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001603static void fuse_writepage_free(struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001604{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001605 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001606 int i;
1607
Miklos Szeredi660585b2021-09-01 12:39:02 +02001608 if (wpa->bucket)
1609 fuse_sync_bucket_dec(wpa->bucket);
1610
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001611 for (i = 0; i < ap->num_pages; i++)
1612 __free_page(ap->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001613
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001614 if (wpa->ia.ff)
1615 fuse_file_put(wpa->ia.ff, false, false);
1616
1617 kfree(ap->pages);
1618 kfree(wpa);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001619}
1620
Max Reitzfcee2162020-05-06 17:44:12 +02001621static void fuse_writepage_finish(struct fuse_mount *fm,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001622 struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001623{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001624 struct fuse_args_pages *ap = &wpa->ia.ap;
1625 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001626 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001627 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001628 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001629
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001630 for (i = 0; i < ap->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001631 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001632 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001633 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001634 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001635 wake_up(&fi->page_waitq);
1636}
1637
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001638/* Called under fi->lock, may release and reacquire it */
Max Reitzfcee2162020-05-06 17:44:12 +02001639static void fuse_send_writepage(struct fuse_mount *fm,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001640 struct fuse_writepage_args *wpa, loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001641__releases(fi->lock)
1642__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001643{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001644 struct fuse_writepage_args *aux, *next;
1645 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1646 struct fuse_write_in *inarg = &wpa->ia.write.in;
1647 struct fuse_args *args = &wpa->ia.ap.args;
1648 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1649 int err;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001651 fi->writectr++;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001652 if (inarg->offset + data_size <= size) {
1653 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001654 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001655 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001656 } else {
1657 /* Got truncated off completely */
1658 goto out_free;
1659 }
1660
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001661 args->in_args[1].size = inarg->size;
1662 args->force = true;
1663 args->nocreds = true;
1664
Max Reitzfcee2162020-05-06 17:44:12 +02001665 err = fuse_simple_background(fm, args, GFP_ATOMIC);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001666 if (err == -ENOMEM) {
1667 spin_unlock(&fi->lock);
Max Reitzfcee2162020-05-06 17:44:12 +02001668 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001669 spin_lock(&fi->lock);
1670 }
1671
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001672 /* Fails on broken connection only */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001673 if (unlikely(err))
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001674 goto out_free;
1675
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001676 return;
1677
1678 out_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001679 fi->writectr--;
Miklos Szeredi69a64872020-07-14 14:45:41 +02001680 rb_erase(&wpa->writepages_entry, &fi->writepages);
Max Reitzfcee2162020-05-06 17:44:12 +02001681 fuse_writepage_finish(fm, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001682 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001683
1684 /* After fuse_writepage_finish() aux request list is private */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001685 for (aux = wpa->next; aux; aux = next) {
1686 next = aux->next;
1687 aux->next = NULL;
1688 fuse_writepage_free(aux);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001689 }
1690
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001691 fuse_writepage_free(wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001692 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001693}
1694
1695/*
1696 * If fi->writectr is positive (no truncate or fsync going on) send
1697 * all queued writepage requests.
1698 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001699 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700 */
1701void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001702__releases(fi->lock)
1703__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001704{
Max Reitzfcee2162020-05-06 17:44:12 +02001705 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9de5be02019-04-24 17:05:06 +02001707 loff_t crop = i_size_read(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001708 struct fuse_writepage_args *wpa;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001709
1710 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001711 wpa = list_entry(fi->queued_writes.next,
1712 struct fuse_writepage_args, queue_entry);
1713 list_del_init(&wpa->queue_entry);
Max Reitzfcee2162020-05-06 17:44:12 +02001714 fuse_send_writepage(fm, wpa, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001715 }
1716}
1717
Miklos Szeredic1460242020-07-14 14:45:41 +02001718static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1719 struct fuse_writepage_args *wpa)
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001720{
1721 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1722 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1723 struct rb_node **p = &root->rb_node;
1724 struct rb_node *parent = NULL;
1725
1726 WARN_ON(!wpa->ia.ap.num_pages);
1727 while (*p) {
1728 struct fuse_writepage_args *curr;
1729 pgoff_t curr_index;
1730
1731 parent = *p;
1732 curr = rb_entry(parent, struct fuse_writepage_args,
1733 writepages_entry);
1734 WARN_ON(curr->inode != wpa->inode);
1735 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1736
1737 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1738 p = &(*p)->rb_right;
1739 else if (idx_to < curr_index)
1740 p = &(*p)->rb_left;
1741 else
Miklos Szeredic1460242020-07-14 14:45:41 +02001742 return curr;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001743 }
1744
1745 rb_link_node(&wpa->writepages_entry, parent, p);
1746 rb_insert_color(&wpa->writepages_entry, root);
Miklos Szeredic1460242020-07-14 14:45:41 +02001747 return NULL;
1748}
1749
1750static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1751{
1752 WARN_ON(fuse_insert_writeback(root, wpa));
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001753}
1754
Max Reitzfcee2162020-05-06 17:44:12 +02001755static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001756 int error)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001757{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001758 struct fuse_writepage_args *wpa =
1759 container_of(args, typeof(*wpa), ia.ap.args);
1760 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001761 struct fuse_inode *fi = get_fuse_inode(inode);
Vivek Goyal34669582021-04-06 10:07:06 -04001762 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001763
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001764 mapping_set_error(inode->i_mapping, error);
Vivek Goyal34669582021-04-06 10:07:06 -04001765 /*
1766 * A writeback finished and this might have updated mtime/ctime on
1767 * server making local mtime/ctime stale. Hence invalidate attrs.
1768 * Do this only if writeback_cache is not enabled. If writeback_cache
1769 * is enabled, we trust local ctime/mtime.
1770 */
1771 if (!fc->writeback_cache)
Miklos Szeredifa5eee52021-10-22 17:03:02 +02001772 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001773 spin_lock(&fi->lock);
Miklos Szeredi69a64872020-07-14 14:45:41 +02001774 rb_erase(&wpa->writepages_entry, &fi->writepages);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001775 while (wpa->next) {
Max Reitzfcee2162020-05-06 17:44:12 +02001776 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001777 struct fuse_write_in *inarg = &wpa->ia.write.in;
1778 struct fuse_writepage_args *next = wpa->next;
1779
1780 wpa->next = next->next;
1781 next->next = NULL;
1782 next->ia.ff = fuse_file_get(wpa->ia.ff);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001783 tree_insert(&fi->writepages, next);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001784
1785 /*
1786 * Skip fuse_flush_writepages() to make it easy to crop requests
1787 * based on primary request size.
1788 *
1789 * 1st case (trivial): there are no concurrent activities using
1790 * fuse_set/release_nowrite. Then we're on safe side because
1791 * fuse_flush_writepages() would call fuse_send_writepage()
1792 * anyway.
1793 *
1794 * 2nd case: someone called fuse_set_nowrite and it is waiting
1795 * now for completion of all in-flight requests. This happens
1796 * rarely and no more than once per page, so this should be
1797 * okay.
1798 *
1799 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1800 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1801 * that fuse_set_nowrite returned implies that all in-flight
1802 * requests were completed along with all of their secondary
1803 * requests. Further primary requests are blocked by negative
1804 * writectr. Hence there cannot be any in-flight requests and
1805 * no invocations of fuse_writepage_end() while we're in
1806 * fuse_set_nowrite..fuse_release_nowrite section.
1807 */
Max Reitzfcee2162020-05-06 17:44:12 +02001808 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001810 fi->writectr--;
Max Reitzfcee2162020-05-06 17:44:12 +02001811 fuse_writepage_finish(fm, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001812 spin_unlock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001813 fuse_writepage_free(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001814}
1815
Miklos Szeredia9667ac2021-09-01 12:39:02 +02001816static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001817{
Miklos Szeredi84840ef2021-10-22 17:03:02 +02001818 struct fuse_file *ff;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001819
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001820 spin_lock(&fi->lock);
Miklos Szeredi84840ef2021-10-22 17:03:02 +02001821 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file,
1822 write_entry);
1823 if (ff)
Miklos Szeredi72523422013-10-01 16:44:52 +02001824 fuse_file_get(ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001825 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001826
1827 return ff;
1828}
1829
Miklos Szeredia9667ac2021-09-01 12:39:02 +02001830static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001831{
Miklos Szeredia9667ac2021-09-01 12:39:02 +02001832 struct fuse_file *ff = __fuse_write_file_get(fi);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001833 WARN_ON(!ff);
1834 return ff;
1835}
1836
1837int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1838{
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001839 struct fuse_inode *fi = get_fuse_inode(inode);
1840 struct fuse_file *ff;
1841 int err;
1842
Miklos Szeredi5c791fe2021-10-22 17:03:01 +02001843 /*
1844 * Inode is always written before the last reference is dropped and
1845 * hence this should not be reached from reclaim.
1846 *
1847 * Writing back the inode from reclaim can deadlock if the request
1848 * processing itself needs an allocation. Allocations triggering
1849 * reclaim while serving a request can't be prevented, because it can
1850 * involve any number of unrelated userspace processes.
1851 */
1852 WARN_ON(wbc->for_reclaim);
1853
Miklos Szeredia9667ac2021-09-01 12:39:02 +02001854 ff = __fuse_write_file_get(fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001855 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001856 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001857 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001858
1859 return err;
1860}
1861
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001862static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1863{
1864 struct fuse_writepage_args *wpa;
1865 struct fuse_args_pages *ap;
1866
1867 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1868 if (wpa) {
1869 ap = &wpa->ia.ap;
1870 ap->num_pages = 0;
1871 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1872 if (!ap->pages) {
1873 kfree(wpa);
1874 wpa = NULL;
1875 }
1876 }
1877 return wpa;
1878
1879}
1880
Miklos Szeredi660585b2021-09-01 12:39:02 +02001881static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
1882 struct fuse_writepage_args *wpa)
1883{
1884 if (!fc->sync_fs)
1885 return;
1886
1887 rcu_read_lock();
1888 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
1889 do {
1890 wpa->bucket = rcu_dereference(fc->curr_bucket);
1891 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
1892 rcu_read_unlock();
1893}
1894
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001895static int fuse_writepage_locked(struct page *page)
1896{
1897 struct address_space *mapping = page->mapping;
1898 struct inode *inode = mapping->host;
1899 struct fuse_conn *fc = get_fuse_conn(inode);
1900 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001901 struct fuse_writepage_args *wpa;
1902 struct fuse_args_pages *ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001903 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001904 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001905
1906 set_page_writeback(page);
1907
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001908 wpa = fuse_writepage_args_alloc();
1909 if (!wpa)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001910 goto err;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001911 ap = &wpa->ia.ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001912
1913 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1914 if (!tmp_page)
1915 goto err_free;
1916
Miklos Szeredi72523422013-10-01 16:44:52 +02001917 error = -EIO;
Miklos Szeredia9667ac2021-09-01 12:39:02 +02001918 wpa->ia.ff = fuse_write_file_get(fi);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001919 if (!wpa->ia.ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001920 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001921
Miklos Szeredi660585b2021-09-01 12:39:02 +02001922 fuse_writepage_add_to_bucket(fc, wpa);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001923 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001924
1925 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001926 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1927 wpa->next = NULL;
1928 ap->args.in_pages = true;
1929 ap->num_pages = 1;
1930 ap->pages[0] = tmp_page;
1931 ap->descs[0].offset = 0;
1932 ap->descs[0].length = PAGE_SIZE;
1933 ap->args.end = fuse_writepage_end;
1934 wpa->inode = inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001935
Tejun Heo93f78d82015-05-22 17:13:27 -04001936 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001937 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001938
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001939 spin_lock(&fi->lock);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001940 tree_insert(&fi->writepages, wpa);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001941 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001942 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001943 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001944
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001945 end_page_writeback(page);
1946
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001947 return 0;
1948
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001949err_nofile:
1950 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001951err_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001952 kfree(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001953err:
Jeff Layton91839762017-05-25 06:57:50 -04001954 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001955 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001956 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001957}
1958
1959static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1960{
1961 int err;
1962
Miklos Szerediff17be02013-10-01 16:44:53 +02001963 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1964 /*
1965 * ->writepages() should be called for sync() and friends. We
1966 * should only get here on direct reclaim and then we are
1967 * allowed to skip a page which is already in flight
1968 */
1969 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1970
1971 redirty_page_for_writepage(wbc, page);
Vasily Averind5880c72019-09-13 18:17:11 +03001972 unlock_page(page);
Miklos Szerediff17be02013-10-01 16:44:53 +02001973 return 0;
1974 }
1975
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001976 err = fuse_writepage_locked(page);
1977 unlock_page(page);
1978
1979 return err;
1980}
1981
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001982struct fuse_fill_wb_data {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001983 struct fuse_writepage_args *wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001984 struct fuse_file *ff;
1985 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001986 struct page **orig_pages;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001987 unsigned int max_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001988};
1989
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001990static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1991{
1992 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1993 struct fuse_conn *fc = get_fuse_conn(data->inode);
1994 struct page **pages;
1995 struct fuse_page_desc *descs;
1996 unsigned int npages = min_t(unsigned int,
1997 max_t(unsigned int, data->max_pages * 2,
1998 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1999 fc->max_pages);
2000 WARN_ON(npages <= data->max_pages);
2001
2002 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
2003 if (!pages)
2004 return false;
2005
2006 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
2007 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
2008 kfree(ap->pages);
2009 ap->pages = pages;
2010 ap->descs = descs;
2011 data->max_pages = npages;
2012
2013 return true;
2014}
2015
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002016static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2017{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002018 struct fuse_writepage_args *wpa = data->wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002019 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002020 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002021 int num_pages = wpa->ia.ap.num_pages;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002022 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002023
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002024 wpa->ia.ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002025 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002026 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002027 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002028 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002029
2030 for (i = 0; i < num_pages; i++)
2031 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002032}
2033
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002034/*
Miklos Szeredic1460242020-07-14 14:45:41 +02002035 * Check under fi->lock if the page is under writeback, and insert it onto the
2036 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
Miklos Szeredi419234d2019-01-16 10:27:59 +01002037 * one already added for a page at this offset. If there's none, then insert
2038 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredic1460242020-07-14 14:45:41 +02002039 * swapping the new temp page with the old one.
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002040 */
Miklos Szeredic1460242020-07-14 14:45:41 +02002041static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2042 struct page *page)
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002043{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002044 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2045 struct fuse_writepage_args *tmp;
2046 struct fuse_writepage_args *old_wpa;
2047 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002048
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002049 WARN_ON(new_ap->num_pages != 0);
Miklos Szeredic1460242020-07-14 14:45:41 +02002050 new_ap->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002051
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002052 spin_lock(&fi->lock);
Miklos Szeredic1460242020-07-14 14:45:41 +02002053 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002054 if (!old_wpa) {
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002055 spin_unlock(&fi->lock);
Miklos Szeredic1460242020-07-14 14:45:41 +02002056 return true;
Maxim Patlasovf6011082013-10-02 15:01:07 +04002057 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002058
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002059 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002060 pgoff_t curr_index;
2061
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002062 WARN_ON(tmp->inode != new_wpa->inode);
2063 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01002064 if (curr_index == page->index) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002065 WARN_ON(tmp->ia.ap.num_pages != 1);
2066 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002067 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002068 }
2069 }
2070
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002071 if (!tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002072 new_wpa->next = old_wpa->next;
2073 old_wpa->next = new_wpa;
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002074 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04002075
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002076 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002077
2078 if (tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002079 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002080
Tejun Heo93f78d82015-05-22 17:13:27 -04002081 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002082 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04002083 wb_writeout_inc(&bdi->wb);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002084 fuse_writepage_free(new_wpa);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002085 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002086
Miklos Szeredic1460242020-07-14 14:45:41 +02002087 return false;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002088}
2089
Miklos Szeredi6ddf3af2020-07-14 14:45:41 +02002090static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2091 struct fuse_args_pages *ap,
2092 struct fuse_fill_wb_data *data)
2093{
2094 WARN_ON(!ap->num_pages);
2095
2096 /*
2097 * Being under writeback is unlikely but possible. For example direct
2098 * read to an mmaped fuse file will set the page dirty twice; once when
2099 * the pages are faulted with get_user_pages(), and then after the read
2100 * completed.
2101 */
2102 if (fuse_page_is_writeback(data->inode, page->index))
2103 return true;
2104
2105 /* Reached max pages */
2106 if (ap->num_pages == fc->max_pages)
2107 return true;
2108
2109 /* Reached max write bytes */
2110 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2111 return true;
2112
2113 /* Discontinuity */
2114 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2115 return true;
2116
2117 /* Need to grow the pages array? If so, did the expansion fail? */
2118 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2119 return true;
2120
2121 return false;
2122}
2123
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002124static int fuse_writepages_fill(struct page *page,
2125 struct writeback_control *wbc, void *_data)
2126{
2127 struct fuse_fill_wb_data *data = _data;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002128 struct fuse_writepage_args *wpa = data->wpa;
2129 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002130 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002131 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002132 struct fuse_conn *fc = get_fuse_conn(inode);
2133 struct page *tmp_page;
2134 int err;
2135
2136 if (!data->ff) {
2137 err = -EIO;
Miklos Szeredia9667ac2021-09-01 12:39:02 +02002138 data->ff = fuse_write_file_get(fi);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002139 if (!data->ff)
2140 goto out_unlock;
2141 }
2142
Miklos Szeredi6ddf3af2020-07-14 14:45:41 +02002143 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002144 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002145 data->wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002146 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02002147
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002148 err = -ENOMEM;
2149 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2150 if (!tmp_page)
2151 goto out_unlock;
2152
2153 /*
2154 * The page must not be redirtied until the writeout is completed
2155 * (i.e. userspace has sent a reply to the write request). Otherwise
2156 * there could be more than one temporary page instance for each real
2157 * page.
2158 *
2159 * This is ensured by holding the page lock in page_mkwrite() while
2160 * checking fuse_page_is_writeback(). We already hold the page lock
2161 * since clear_page_dirty_for_io() and keep it held until we add the
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002162 * request to the fi->writepages list and increment ap->num_pages.
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002163 * After this fuse_page_is_writeback() will indicate that the page is
2164 * under writeback, so we can release the page lock.
2165 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002166 if (data->wpa == NULL) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002167 err = -ENOMEM;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002168 wpa = fuse_writepage_args_alloc();
2169 if (!wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002170 __free_page(tmp_page);
2171 goto out_unlock;
2172 }
Miklos Szeredi660585b2021-09-01 12:39:02 +02002173 fuse_writepage_add_to_bucket(fc, wpa);
2174
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002175 data->max_pages = 1;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002176
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002177 ap = &wpa->ia.ap;
2178 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2179 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2180 wpa->next = NULL;
2181 ap->args.in_pages = true;
2182 ap->args.end = fuse_writepage_end;
2183 ap->num_pages = 0;
2184 wpa->inode = inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002185 }
2186 set_page_writeback(page);
2187
2188 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002189 ap->pages[ap->num_pages] = tmp_page;
2190 ap->descs[ap->num_pages].offset = 0;
2191 ap->descs[ap->num_pages].length = PAGE_SIZE;
Miklos Szeredic1460242020-07-14 14:45:41 +02002192 data->orig_pages[ap->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002193
Tejun Heo93f78d82015-05-22 17:13:27 -04002194 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07002195 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002196
2197 err = 0;
Miklos Szeredic1460242020-07-14 14:45:41 +02002198 if (data->wpa) {
2199 /*
2200 * Protected by fi->lock against concurrent access by
2201 * fuse_page_is_writeback().
2202 */
2203 spin_lock(&fi->lock);
2204 ap->num_pages++;
2205 spin_unlock(&fi->lock);
2206 } else if (fuse_writepage_add(wpa, page)) {
2207 data->wpa = wpa;
2208 } else {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002209 end_page_writeback(page);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002210 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002211out_unlock:
2212 unlock_page(page);
2213
2214 return err;
2215}
2216
2217static int fuse_writepages(struct address_space *mapping,
2218 struct writeback_control *wbc)
2219{
2220 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002221 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002222 struct fuse_fill_wb_data data;
2223 int err;
2224
2225 err = -EIO;
Miklos Szeredi5d069db2020-12-10 15:33:14 +01002226 if (fuse_is_bad(inode))
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002227 goto out;
2228
2229 data.inode = inode;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002230 data.wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002231 data.ff = NULL;
2232
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002233 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002234 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02002235 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002236 GFP_NOFS);
2237 if (!data.orig_pages)
2238 goto out;
2239
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002240 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002241 if (data.wpa) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002242 WARN_ON(!data.wpa->ia.ap.num_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002243 fuse_writepages_send(&data);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002244 }
2245 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002246 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002247
2248 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002249out:
2250 return err;
2251}
2252
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002253/*
2254 * It's worthy to make sure that space is reserved on disk for the write,
2255 * but how to implement it without killing performance need more thinking.
2256 */
2257static int fuse_write_begin(struct file *file, struct address_space *mapping,
2258 loff_t pos, unsigned len, unsigned flags,
2259 struct page **pagep, void **fsdata)
2260{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002261 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002262 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002263 struct page *page;
2264 loff_t fsize;
2265 int err = -ENOMEM;
2266
2267 WARN_ON(!fc->writeback_cache);
2268
2269 page = grab_cache_page_write_begin(mapping, index, flags);
2270 if (!page)
2271 goto error;
2272
2273 fuse_wait_on_page_writeback(mapping->host, page->index);
2274
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002275 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002276 goto success;
2277 /*
2278 * Check if the start this page comes after the end of file, in which
2279 * case the readpage can be optimized away.
2280 */
2281 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002282 if (fsize <= (pos & PAGE_MASK)) {
2283 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002284 if (off)
2285 zero_user_segment(page, 0, off);
2286 goto success;
2287 }
2288 err = fuse_do_readpage(file, page);
2289 if (err)
2290 goto cleanup;
2291success:
2292 *pagep = page;
2293 return 0;
2294
2295cleanup:
2296 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002297 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002298error:
2299 return err;
2300}
2301
2302static int fuse_write_end(struct file *file, struct address_space *mapping,
2303 loff_t pos, unsigned len, unsigned copied,
2304 struct page *page, void *fsdata)
2305{
2306 struct inode *inode = page->mapping->host;
2307
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002308 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2309 if (!copied)
2310 goto unlock;
2311
Miklos Szeredi8c56e032021-10-22 17:03:02 +02002312 pos += copied;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002313 if (!PageUptodate(page)) {
2314 /* Zero any unwritten bytes at the end of the page */
Miklos Szeredi8c56e032021-10-22 17:03:02 +02002315 size_t endoff = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002316 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002317 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002318 SetPageUptodate(page);
2319 }
2320
Miklos Szeredi8c56e032021-10-22 17:03:02 +02002321 if (pos > inode->i_size)
2322 i_size_write(inode, pos);
2323
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002324 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002325
2326unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002327 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002328 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002329
2330 return copied;
2331}
2332
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002333static int fuse_launder_page(struct page *page)
2334{
2335 int err = 0;
2336 if (clear_page_dirty_for_io(page)) {
2337 struct inode *inode = page->mapping->host;
Miklos Szeredi3993382b2020-11-11 17:22:31 +01002338
2339 /* Serialize with pending writeback for the same page */
2340 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002341 err = fuse_writepage_locked(page);
2342 if (!err)
2343 fuse_wait_on_page_writeback(inode, page->index);
2344 }
2345 return err;
2346}
2347
2348/*
Miklos Szeredi36ea2332021-10-22 17:03:01 +02002349 * Write back dirty data/metadata now (there may not be any suitable
2350 * open files later for data)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002351 */
2352static void fuse_vma_close(struct vm_area_struct *vma)
2353{
Miklos Szeredi36ea2332021-10-22 17:03:01 +02002354 int err;
2355
2356 err = write_inode_now(vma->vm_file->f_mapping->host, 1);
2357 mapping_set_error(vma->vm_file->f_mapping, err);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002358}
2359
2360/*
2361 * Wait for writeback against this page to complete before allowing it
2362 * to be marked dirty again, and hence written back again, possibly
2363 * before the previous writepage completed.
2364 *
2365 * Block here, instead of in ->writepage(), so that the userspace fs
2366 * can only block processes actually operating on the filesystem.
2367 *
2368 * Otherwise unprivileged userspace fs would be able to block
2369 * unrelated:
2370 *
2371 * - page migration
2372 * - sync(2)
2373 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2374 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302375static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002376{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002377 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002378 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002379
Dave Jiang11bac802017-02-24 14:56:41 -08002380 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002381 lock_page(page);
2382 if (page->mapping != inode->i_mapping) {
2383 unlock_page(page);
2384 return VM_FAULT_NOPAGE;
2385 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002386
2387 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002388 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002389}
2390
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002391static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002392 .close = fuse_vma_close,
2393 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002394 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002395 .page_mkwrite = fuse_page_mkwrite,
2396};
2397
2398static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2399{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002400 struct fuse_file *ff = file->private_data;
2401
Stefan Hajnoczi2a9a6092020-08-19 18:19:52 -04002402 /* DAX mmap is superior to direct_io mmap */
2403 if (FUSE_IS_DAX(file_inode(file)))
2404 return fuse_dax_mmap(file, vma);
2405
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002406 if (ff->open_flags & FOPEN_DIRECT_IO) {
2407 /* Can't provide the coherency needed for MAP_SHARED */
2408 if (vma->vm_flags & VM_MAYSHARE)
2409 return -ENODEV;
2410
2411 invalidate_inode_pages2(file->f_mapping);
2412
2413 return generic_file_mmap(file, vma);
2414 }
2415
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002416 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2417 fuse_link_write_file(file);
2418
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002419 file_accessed(file);
2420 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002421 return 0;
2422}
2423
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002424static int convert_fuse_file_lock(struct fuse_conn *fc,
2425 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002426 struct file_lock *fl)
2427{
2428 switch (ffl->type) {
2429 case F_UNLCK:
2430 break;
2431
2432 case F_RDLCK:
2433 case F_WRLCK:
2434 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2435 ffl->end < ffl->start)
2436 return -EIO;
2437
2438 fl->fl_start = ffl->start;
2439 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002440
2441 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002442 * Convert pid into init's pid namespace. The locks API will
2443 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002444 */
2445 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002446 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002447 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002448 break;
2449
2450 default:
2451 return -EIO;
2452 }
2453 fl->fl_type = ffl->type;
2454 return 0;
2455}
2456
Miklos Szeredi70781872014-12-12 09:49:05 +01002457static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002458 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002459 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002460{
Al Viro6131ffa2013-02-27 16:59:05 -05002461 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002462 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002463 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002464
Miklos Szeredi70781872014-12-12 09:49:05 +01002465 memset(inarg, 0, sizeof(*inarg));
2466 inarg->fh = ff->fh;
2467 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2468 inarg->lk.start = fl->fl_start;
2469 inarg->lk.end = fl->fl_end;
2470 inarg->lk.type = fl->fl_type;
2471 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002472 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002473 inarg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002474 args->opcode = opcode;
2475 args->nodeid = get_node_id(inode);
2476 args->in_numargs = 1;
2477 args->in_args[0].size = sizeof(*inarg);
2478 args->in_args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002479}
2480
2481static int fuse_getlk(struct file *file, struct file_lock *fl)
2482{
Al Viro6131ffa2013-02-27 16:59:05 -05002483 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +02002484 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002485 FUSE_ARGS(args);
2486 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002487 struct fuse_lk_out outarg;
2488 int err;
2489
Miklos Szeredi70781872014-12-12 09:49:05 +01002490 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
Miklos Szeredid5b48542019-09-10 15:04:08 +02002491 args.out_numargs = 1;
2492 args.out_args[0].size = sizeof(outarg);
2493 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002494 err = fuse_simple_request(fm, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002495 if (!err)
Max Reitzfcee2162020-05-06 17:44:12 +02002496 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002497
2498 return err;
2499}
2500
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002501static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002502{
Al Viro6131ffa2013-02-27 16:59:05 -05002503 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +02002504 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002505 FUSE_ARGS(args);
2506 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002507 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002508 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
Max Reitzfcee2162020-05-06 17:44:12 +02002509 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002510 int err;
2511
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002512 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002513 /* NLM needs asynchronous locks, which we don't support yet */
2514 return -ENOLCK;
2515 }
2516
Miklos Szeredi71421252006-06-25 05:48:52 -07002517 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002518 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002519 return 0;
2520
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002521 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Max Reitzfcee2162020-05-06 17:44:12 +02002522 err = fuse_simple_request(fm, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002523
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002524 /* locking is restartable */
2525 if (err == -EINTR)
2526 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002527
Miklos Szeredi71421252006-06-25 05:48:52 -07002528 return err;
2529}
2530
2531static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2532{
Al Viro6131ffa2013-02-27 16:59:05 -05002533 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002534 struct fuse_conn *fc = get_fuse_conn(inode);
2535 int err;
2536
Miklos Szeredi48e90762008-07-25 01:49:02 -07002537 if (cmd == F_CANCELLK) {
2538 err = 0;
2539 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002540 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002541 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002542 err = 0;
2543 } else
2544 err = fuse_getlk(file, fl);
2545 } else {
2546 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002547 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002548 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002549 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002550 }
2551 return err;
2552}
2553
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002554static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2555{
Al Viro6131ffa2013-02-27 16:59:05 -05002556 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002557 struct fuse_conn *fc = get_fuse_conn(inode);
2558 int err;
2559
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002560 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002561 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002562 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002563 struct fuse_file *ff = file->private_data;
2564
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002565 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002566 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002567 err = fuse_setlk(file, fl, 1);
2568 }
2569
2570 return err;
2571}
2572
Miklos Szeredib2d22722006-12-06 20:35:51 -08002573static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2574{
2575 struct inode *inode = mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +02002576 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002577 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002578 struct fuse_bmap_in inarg;
2579 struct fuse_bmap_out outarg;
2580 int err;
2581
Max Reitzfcee2162020-05-06 17:44:12 +02002582 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
Miklos Szeredib2d22722006-12-06 20:35:51 -08002583 return 0;
2584
Miklos Szeredib2d22722006-12-06 20:35:51 -08002585 memset(&inarg, 0, sizeof(inarg));
2586 inarg.block = block;
2587 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002588 args.opcode = FUSE_BMAP;
2589 args.nodeid = get_node_id(inode);
2590 args.in_numargs = 1;
2591 args.in_args[0].size = sizeof(inarg);
2592 args.in_args[0].value = &inarg;
2593 args.out_numargs = 1;
2594 args.out_args[0].size = sizeof(outarg);
2595 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002596 err = fuse_simple_request(fm, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002597 if (err == -ENOSYS)
Max Reitzfcee2162020-05-06 17:44:12 +02002598 fm->fc->no_bmap = 1;
Miklos Szeredib2d22722006-12-06 20:35:51 -08002599
2600 return err ? 0 : outarg.block;
2601}
2602
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302603static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2604{
2605 struct inode *inode = file->f_mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +02002606 struct fuse_mount *fm = get_fuse_mount(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302607 struct fuse_file *ff = file->private_data;
2608 FUSE_ARGS(args);
2609 struct fuse_lseek_in inarg = {
2610 .fh = ff->fh,
2611 .offset = offset,
2612 .whence = whence
2613 };
2614 struct fuse_lseek_out outarg;
2615 int err;
2616
Max Reitzfcee2162020-05-06 17:44:12 +02002617 if (fm->fc->no_lseek)
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302618 goto fallback;
2619
Miklos Szeredid5b48542019-09-10 15:04:08 +02002620 args.opcode = FUSE_LSEEK;
2621 args.nodeid = ff->nodeid;
2622 args.in_numargs = 1;
2623 args.in_args[0].size = sizeof(inarg);
2624 args.in_args[0].value = &inarg;
2625 args.out_numargs = 1;
2626 args.out_args[0].size = sizeof(outarg);
2627 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002628 err = fuse_simple_request(fm, &args);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302629 if (err) {
2630 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02002631 fm->fc->no_lseek = 1;
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302632 goto fallback;
2633 }
2634 return err;
2635 }
2636
2637 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2638
2639fallback:
Miklos Szeredic6c745b2021-10-22 17:03:03 +02002640 err = fuse_update_attributes(inode, file, STATX_SIZE);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302641 if (!err)
2642 return generic_file_llseek(file, offset, whence);
2643 else
2644 return err;
2645}
2646
Andrew Morton965c8e52012-12-17 15:59:39 -08002647static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002648{
2649 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002650 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002651
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302652 switch (whence) {
2653 case SEEK_SET:
2654 case SEEK_CUR:
2655 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002656 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302657 break;
2658 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002659 inode_lock(inode);
Miklos Szeredic6c745b2021-10-22 17:03:03 +02002660 retval = fuse_update_attributes(inode, file, STATX_SIZE);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302661 if (!retval)
2662 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002663 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302664 break;
2665 case SEEK_HOLE:
2666 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002667 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302668 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002669 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302670 break;
2671 default:
2672 retval = -EINVAL;
2673 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002674
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002675 return retval;
2676}
2677
Tejun Heo59efec72008-11-26 12:03:55 +01002678/*
Tejun Heo95668a62008-11-26 12:03:55 +01002679 * All files which have been polled are linked to RB tree
2680 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2681 * find the matching one.
2682 */
2683static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2684 struct rb_node **parent_out)
2685{
2686 struct rb_node **link = &fc->polled_files.rb_node;
2687 struct rb_node *last = NULL;
2688
2689 while (*link) {
2690 struct fuse_file *ff;
2691
2692 last = *link;
2693 ff = rb_entry(last, struct fuse_file, polled_node);
2694
2695 if (kh < ff->kh)
2696 link = &last->rb_left;
2697 else if (kh > ff->kh)
2698 link = &last->rb_right;
2699 else
2700 return link;
2701 }
2702
2703 if (parent_out)
2704 *parent_out = last;
2705 return link;
2706}
2707
2708/*
2709 * The file is about to be polled. Make sure it's on the polled_files
2710 * RB tree. Note that files once added to the polled_files tree are
2711 * not removed before the file is released. This is because a file
2712 * polled once is likely to be polled again.
2713 */
2714static void fuse_register_polled_file(struct fuse_conn *fc,
2715 struct fuse_file *ff)
2716{
2717 spin_lock(&fc->lock);
2718 if (RB_EMPTY_NODE(&ff->polled_node)) {
Kees Cook3f649ab2020-06-03 13:09:38 -07002719 struct rb_node **link, *parent;
Tejun Heo95668a62008-11-26 12:03:55 +01002720
2721 link = fuse_find_polled_node(fc, ff->kh, &parent);
2722 BUG_ON(*link);
2723 rb_link_node(&ff->polled_node, parent, link);
2724 rb_insert_color(&ff->polled_node, &fc->polled_files);
2725 }
2726 spin_unlock(&fc->lock);
2727}
2728
Al Viro076ccb72017-07-03 01:02:18 -04002729__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002730{
Tejun Heo95668a62008-11-26 12:03:55 +01002731 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02002732 struct fuse_mount *fm = ff->fm;
Tejun Heo95668a62008-11-26 12:03:55 +01002733 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2734 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002735 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002736 int err;
2737
Max Reitzfcee2162020-05-06 17:44:12 +02002738 if (fm->fc->no_poll)
Tejun Heo95668a62008-11-26 12:03:55 +01002739 return DEFAULT_POLLMASK;
2740
2741 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002742 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002743
2744 /*
2745 * Ask for notification iff there's someone waiting for it.
2746 * The client may ignore the flag and always notify.
2747 */
2748 if (waitqueue_active(&ff->poll_wait)) {
2749 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
Max Reitzfcee2162020-05-06 17:44:12 +02002750 fuse_register_polled_file(fm->fc, ff);
Tejun Heo95668a62008-11-26 12:03:55 +01002751 }
2752
Miklos Szeredid5b48542019-09-10 15:04:08 +02002753 args.opcode = FUSE_POLL;
2754 args.nodeid = ff->nodeid;
2755 args.in_numargs = 1;
2756 args.in_args[0].size = sizeof(inarg);
2757 args.in_args[0].value = &inarg;
2758 args.out_numargs = 1;
2759 args.out_args[0].size = sizeof(outarg);
2760 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002761 err = fuse_simple_request(fm, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002762
2763 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002764 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002765 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02002766 fm->fc->no_poll = 1;
Tejun Heo95668a62008-11-26 12:03:55 +01002767 return DEFAULT_POLLMASK;
2768 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002769 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002770}
Tejun Heo08cbf542009-04-14 10:54:53 +09002771EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002772
2773/*
2774 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2775 * wakes up the poll waiters.
2776 */
2777int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2778 struct fuse_notify_poll_wakeup_out *outarg)
2779{
2780 u64 kh = outarg->kh;
2781 struct rb_node **link;
2782
2783 spin_lock(&fc->lock);
2784
2785 link = fuse_find_polled_node(fc, kh, NULL);
2786 if (*link) {
2787 struct fuse_file *ff;
2788
2789 ff = rb_entry(*link, struct fuse_file, polled_node);
2790 wake_up_interruptible_sync(&ff->poll_wait);
2791 }
2792
2793 spin_unlock(&fc->lock);
2794 return 0;
2795}
2796
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002797static void fuse_do_truncate(struct file *file)
2798{
2799 struct inode *inode = file->f_mapping->host;
2800 struct iattr attr;
2801
2802 attr.ia_valid = ATTR_SIZE;
2803 attr.ia_size = i_size_read(inode);
2804
2805 attr.ia_file = file;
2806 attr.ia_valid |= ATTR_FILE;
2807
Jan Kara62490332016-05-26 17:12:41 +02002808 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002809}
2810
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002811static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002812{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002813 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002814}
2815
Anand Avati4273b792012-02-17 12:46:25 -05002816static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002817fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002818{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002819 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002820 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002821 struct file *file = iocb->ki_filp;
2822 struct fuse_file *ff = file->private_data;
Anand Avati4273b792012-02-17 12:46:25 -05002823 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002824 struct inode *inode;
2825 loff_t i_size;
Al Viro933a3752020-09-17 17:26:56 -04002826 size_t count = iov_iter_count(iter), shortened = 0;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002827 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002828 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002829
Anand Avati4273b792012-02-17 12:46:25 -05002830 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002831 inode = file->f_mapping->host;
2832 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002833
Al Viro933a3752020-09-17 17:26:56 -04002834 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002835 return 0;
2836
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002837 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002838 if (!io)
2839 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002840 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002841 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002842 io->reqs = 1;
2843 io->bytes = -1;
2844 io->size = 0;
2845 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002846 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002847 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002848 /*
2849 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002850 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002851 */
Linus Torvalds69456532020-10-19 14:28:30 -07002852 io->async = ff->fm->fc->async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002853 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302854 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002855
Al Viro933a3752020-09-17 17:26:56 -04002856 /* optimization for short read */
2857 if (io->async && !io->write && offset + count > i_size) {
Linus Torvalds69456532020-10-19 14:28:30 -07002858 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
Al Viro933a3752020-09-17 17:26:56 -04002859 shortened = count - iov_iter_count(iter);
2860 count -= shortened;
2861 }
2862
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002863 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302864 * We cannot asynchronously extend the size of a file.
2865 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002866 */
Al Viro933a3752020-09-17 17:26:56 -04002867 if ((offset + count > i_size) && io->write)
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302868 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002869
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302870 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002871 /*
2872 * Additional reference to keep io around after
2873 * calling fuse_aio_complete()
2874 */
2875 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002876 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002877 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002878
Omar Sandoval6f673762015-03-16 04:33:52 -07002879 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002880 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Miklos Szeredifa5eee52021-10-22 17:03:02 +02002881 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
Al Viro812408f2015-03-30 22:15:58 -04002882 } else {
Al Virod22a9432014-03-16 15:50:47 -04002883 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002884 }
Al Viro933a3752020-09-17 17:26:56 -04002885 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002886
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002887 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002888 bool blocking = io->blocking;
2889
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2891
2892 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002893 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 return -EIOCBQUEUED;
2895
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002896 wait_for_completion(&wait);
2897 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 }
2899
Seth Forshee744742d2016-03-11 10:35:34 -06002900 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002901
Omar Sandoval6f673762015-03-16 04:33:52 -07002902 if (iov_iter_rw(iter) == WRITE) {
Miklos Szeredid3477392021-10-22 17:03:02 +02002903 fuse_write_update_attr(inode, pos, ret);
2904 if (ret < 0 && offset + count > i_size)
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002905 fuse_do_truncate(file);
2906 }
Anand Avati4273b792012-02-17 12:46:25 -05002907
2908 return ret;
2909}
2910
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02002911static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
2912{
Miklos Szeredi59bda8e2021-08-31 14:18:08 +02002913 int err = filemap_write_and_wait_range(inode->i_mapping, start, -1);
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02002914
2915 if (!err)
2916 fuse_sync_writes(inode);
2917
2918 return err;
2919}
2920
Miklos Szeredicdadb112012-11-10 16:55:56 +01002921static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2922 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002923{
2924 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002925 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002926 struct fuse_inode *fi = get_fuse_inode(inode);
Max Reitzfcee2162020-05-06 17:44:12 +02002927 struct fuse_mount *fm = ff->fm;
Miklos Szeredi70781872014-12-12 09:49:05 +01002928 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002929 struct fuse_fallocate_in inarg = {
2930 .fh = ff->fh,
2931 .offset = offset,
2932 .length = length,
2933 .mode = mode
2934 };
2935 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002936 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
Richard W.M. Jones6b1bdb52021-05-12 17:18:48 +01002937 (mode & (FALLOC_FL_PUNCH_HOLE |
2938 FALLOC_FL_ZERO_RANGE));
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002939
Vivek Goyal6ae330c2020-08-19 18:19:54 -04002940 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
2941
Richard W.M. Jones6b1bdb52021-05-12 17:18:48 +01002942 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2943 FALLOC_FL_ZERO_RANGE))
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002944 return -EOPNOTSUPP;
2945
Max Reitzfcee2162020-05-06 17:44:12 +02002946 if (fm->fc->no_fallocate)
Miklos Szeredi519c6042012-04-26 10:56:36 +02002947 return -EOPNOTSUPP;
2948
Maxim Patlasov14c14412013-06-13 12:16:39 +04002949 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002950 inode_lock(inode);
Vivek Goyal6ae330c2020-08-19 18:19:54 -04002951 if (block_faults) {
Jan Kara8bcbbe92021-04-21 17:18:39 +02002952 filemap_invalidate_lock(inode->i_mapping);
Vivek Goyal6ae330c2020-08-19 18:19:54 -04002953 err = fuse_dax_break_layouts(inode, 0, 0);
2954 if (err)
2955 goto out;
2956 }
2957
Richard W.M. Jones6b1bdb52021-05-12 17:18:48 +01002958 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
Maxim Patlasovbde52782013-09-13 19:19:54 +04002959 loff_t endbyte = offset + length - 1;
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02002960
2961 err = fuse_writeback_range(inode, offset, endbyte);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002962 if (err)
2963 goto out;
Maxim Patlasovbde52782013-09-13 19:19:54 +04002964 }
Brian Foster3634a632013-05-17 09:30:32 -04002965 }
2966
Liu Bo0cbade02019-04-18 04:04:41 +08002967 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2968 offset + length > i_size_read(inode)) {
2969 err = inode_newsize_ok(inode, offset + length);
2970 if (err)
Miklos Szeredi35d6fcb2019-05-27 11:42:07 +02002971 goto out;
Liu Bo0cbade02019-04-18 04:04:41 +08002972 }
2973
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002974 if (!(mode & FALLOC_FL_KEEP_SIZE))
2975 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2976
Miklos Szeredid5b48542019-09-10 15:04:08 +02002977 args.opcode = FUSE_FALLOCATE;
2978 args.nodeid = ff->nodeid;
2979 args.in_numargs = 1;
2980 args.in_args[0].size = sizeof(inarg);
2981 args.in_args[0].value = &inarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002982 err = fuse_simple_request(fm, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002983 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02002984 fm->fc->no_fallocate = 1;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002985 err = -EOPNOTSUPP;
2986 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002987 if (err)
2988 goto out;
2989
2990 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002991 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
Miklos Szeredi20235b42021-10-22 17:03:03 +02002992 if (fuse_write_update_attr(inode, offset + length, length))
Miklos Szeredi93d22692014-04-28 14:19:22 +02002993 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002994 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002995
Richard W.M. Jones6b1bdb52021-05-12 17:18:48 +01002996 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
Brian Fosterbee6c302013-05-17 15:27:34 -04002997 truncate_pagecache_range(inode, offset, offset + length - 1);
2998
Miklos Szeredifa5eee52021-10-22 17:03:02 +02002999 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
Brian Fosterbee6c302013-05-17 15:27:34 -04003000
Brian Foster3634a632013-05-17 09:30:32 -04003001out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003002 if (!(mode & FALLOC_FL_KEEP_SIZE))
3003 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3004
Vivek Goyal6ae330c2020-08-19 18:19:54 -04003005 if (block_faults)
Jan Kara8bcbbe92021-04-21 17:18:39 +02003006 filemap_invalidate_unlock(inode->i_mapping);
Vivek Goyal6ae330c2020-08-19 18:19:54 -04003007
Maxim Patlasovbde52782013-09-13 19:19:54 +04003008 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003009 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003010
Miklos Szeredi5c791fe2021-10-22 17:03:01 +02003011 fuse_flush_time_update(inode);
3012
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003013 return err;
3014}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003015
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003016static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3017 struct file *file_out, loff_t pos_out,
3018 size_t len, unsigned int flags)
Niels de Vos88bc7d52018-08-21 14:36:31 +02003019{
3020 struct fuse_file *ff_in = file_in->private_data;
3021 struct fuse_file *ff_out = file_out->private_data;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003022 struct inode *inode_in = file_inode(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003023 struct inode *inode_out = file_inode(file_out);
3024 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
Max Reitzfcee2162020-05-06 17:44:12 +02003025 struct fuse_mount *fm = ff_in->fm;
3026 struct fuse_conn *fc = fm->fc;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003027 FUSE_ARGS(args);
3028 struct fuse_copy_file_range_in inarg = {
3029 .fh_in = ff_in->fh,
3030 .off_in = pos_in,
3031 .nodeid_out = ff_out->nodeid,
3032 .fh_out = ff_out->fh,
3033 .off_out = pos_out,
3034 .len = len,
3035 .flags = flags
3036 };
3037 struct fuse_write_out outarg;
3038 ssize_t err;
3039 /* mark unstable when write-back is not used, and file_out gets
3040 * extended */
3041 bool is_unstable = (!fc->writeback_cache) &&
3042 ((pos_out + len) > inode_out->i_size);
3043
3044 if (fc->no_copy_file_range)
3045 return -EOPNOTSUPP;
3046
Amir Goldstein5dae2222019-06-05 08:04:50 -07003047 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3048 return -EXDEV;
3049
Miklos Szeredi2c4656d2020-05-20 11:39:35 +02003050 inode_lock(inode_in);
3051 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3052 inode_unlock(inode_in);
3053 if (err)
3054 return err;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003055
Niels de Vos88bc7d52018-08-21 14:36:31 +02003056 inode_lock(inode_out);
3057
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003058 err = file_modified(file_out);
3059 if (err)
3060 goto out;
3061
Miklos Szeredi9b464182020-05-20 11:39:35 +02003062 /*
3063 * Write out dirty pages in the destination file before sending the COPY
3064 * request to userspace. After the request is completed, truncate off
3065 * pages (including partial ones) from the cache that have been copied,
3066 * since these contain stale data at that point.
3067 *
3068 * This should be mostly correct, but if the COPY writes to partial
3069 * pages (at the start or end) and the parts not covered by the COPY are
3070 * written through a memory map after calling fuse_writeback_range(),
3071 * then these partial page modifications will be lost on truncation.
3072 *
3073 * It is unlikely that someone would rely on such mixed style
3074 * modifications. Yet this does give less guarantees than if the
3075 * copying was performed with write(2).
3076 *
Jan Kara8bcbbe92021-04-21 17:18:39 +02003077 * To fix this a mapping->invalidate_lock could be used to prevent new
Miklos Szeredi9b464182020-05-20 11:39:35 +02003078 * faults while the copy is ongoing.
3079 */
Miklos Szeredi2c4656d2020-05-20 11:39:35 +02003080 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3081 if (err)
3082 goto out;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003083
3084 if (is_unstable)
3085 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3086
Miklos Szeredid5b48542019-09-10 15:04:08 +02003087 args.opcode = FUSE_COPY_FILE_RANGE;
3088 args.nodeid = ff_in->nodeid;
3089 args.in_numargs = 1;
3090 args.in_args[0].size = sizeof(inarg);
3091 args.in_args[0].value = &inarg;
3092 args.out_numargs = 1;
3093 args.out_args[0].size = sizeof(outarg);
3094 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02003095 err = fuse_simple_request(fm, &args);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003096 if (err == -ENOSYS) {
3097 fc->no_copy_file_range = 1;
3098 err = -EOPNOTSUPP;
3099 }
3100 if (err)
3101 goto out;
3102
Miklos Szeredi9b464182020-05-20 11:39:35 +02003103 truncate_inode_pages_range(inode_out->i_mapping,
3104 ALIGN_DOWN(pos_out, PAGE_SIZE),
3105 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3106
Miklos Szeredi20235b42021-10-22 17:03:03 +02003107 file_update_time(file_out);
3108 fuse_write_update_attr(inode_out, pos_out + outarg.size, outarg.size);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003109
3110 err = outarg.size;
3111out:
3112 if (is_unstable)
3113 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3114
3115 inode_unlock(inode_out);
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003116 file_accessed(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003117
Miklos Szeredi5c791fe2021-10-22 17:03:01 +02003118 fuse_flush_time_update(inode_out);
3119
Niels de Vos88bc7d52018-08-21 14:36:31 +02003120 return err;
3121}
3122
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003123static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3124 struct file *dst_file, loff_t dst_off,
3125 size_t len, unsigned int flags)
3126{
3127 ssize_t ret;
3128
3129 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3130 len, flags);
3131
Amir Goldstein5dae2222019-06-05 08:04:50 -07003132 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003133 ret = generic_copy_file_range(src_file, src_off, dst_file,
3134 dst_off, len, flags);
3135 return ret;
3136}
3137
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003138static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003139 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003140 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003141 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003142 .mmap = fuse_file_mmap,
3143 .open = fuse_open,
3144 .flush = fuse_flush,
3145 .release = fuse_release,
3146 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003147 .lock = fuse_file_lock,
Stefan Hajnoczi2a9a6092020-08-19 18:19:52 -04003148 .get_unmapped_area = thp_get_unmapped_area,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003149 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003150 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003151 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003152 .unlocked_ioctl = fuse_file_ioctl,
3153 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003154 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003155 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003156 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003157};
3158
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003159static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003160 .readpage = fuse_readpage,
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -07003161 .readahead = fuse_readahead,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003162 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003163 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003164 .launder_page = fuse_launder_page,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003165 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003166 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003167 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003168 .write_begin = fuse_write_begin,
3169 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003170};
3171
3172void fuse_init_file_inode(struct inode *inode)
3173{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003174 struct fuse_inode *fi = get_fuse_inode(inode);
3175
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003176 inode->i_fop = &fuse_file_operations;
3177 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003178
3179 INIT_LIST_HEAD(&fi->write_files);
3180 INIT_LIST_HEAD(&fi->queued_writes);
3181 fi->writectr = 0;
3182 init_waitqueue_head(&fi->page_waitq);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03003183 fi->writepages = RB_ROOT;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04003184
3185 if (IS_ENABLED(CONFIG_FUSE_DAX))
3186 fuse_dax_inode_init(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003187}