blob: aa0a44f7028f208e6f85c17b1515a55b7ac01c2f [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>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010017#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020018#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Chirantan Ekbote31070f62020-07-14 19:26:39 +090021#include <linux/fs.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070022
Miklos Szeredi72133942019-09-10 15:04:11 +020023static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
24 struct fuse_page_desc **desc)
Miklos Szeredi4c4f03f2019-09-10 15:04:09 +020025{
26 struct page **pages;
27
28 pages = kzalloc(npages * (sizeof(struct page *) +
29 sizeof(struct fuse_page_desc)), flags);
30 *desc = (void *) (pages + npages);
31
32 return pages;
33}
34
Max Reitzfcee2162020-05-06 17:44:12 +020035static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020036 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070037{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070038 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010039 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080040
41 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070042 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
Max Reitzfcee2162020-05-06 17:44:12 +020043 if (!fm->fc->atomic_o_trunc)
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070044 inarg.flags &= ~O_TRUNC;
Miklos Szeredid5b48542019-09-10 15:04:08 +020045 args.opcode = opcode;
46 args.nodeid = nodeid;
47 args.in_numargs = 1;
48 args.in_args[0].size = sizeof(inarg);
49 args.in_args[0].value = &inarg;
50 args.out_numargs = 1;
51 args.out_args[0].size = sizeof(*outargp);
52 args.out_args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080053
Max Reitzfcee2162020-05-06 17:44:12 +020054 return fuse_simple_request(fm, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080055}
56
Miklos Szeredi4cb54862019-09-10 15:04:10 +020057struct fuse_release_args {
58 struct fuse_args args;
59 struct fuse_release_in inarg;
60 struct inode *inode;
61};
62
Max Reitzfcee2162020-05-06 17:44:12 +020063struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080064{
65 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090066
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070067 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
Tejun Heo6b2db282009-04-14 10:54:49 +090068 if (unlikely(!ff))
69 return NULL;
70
Max Reitzfcee2162020-05-06 17:44:12 +020071 ff->fm = fm;
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070072 ff->release_args = kzalloc(sizeof(*ff->release_args),
73 GFP_KERNEL_ACCOUNT);
Miklos Szeredi4cb54862019-09-10 15:04:10 +020074 if (!ff->release_args) {
Tejun Heo6b2db282009-04-14 10:54:49 +090075 kfree(ff);
76 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080077 }
Tejun Heo6b2db282009-04-14 10:54:49 +090078
79 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020080 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020081 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090082 RB_CLEAR_NODE(&ff->polled_node);
83 init_waitqueue_head(&ff->poll_wait);
84
Max Reitzfcee2162020-05-06 17:44:12 +020085 ff->kh = atomic64_inc_return(&fm->fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090086
Miklos Szeredifd72faa2005-11-07 00:59:51 -080087 return ff;
88}
89
90void fuse_file_free(struct fuse_file *ff)
91{
Miklos Szeredi4cb54862019-09-10 15:04:10 +020092 kfree(ff->release_args);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020093 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080094 kfree(ff);
95}
96
Miklos Szeredi267d8442017-02-22 20:08:25 +010097static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070098{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020099 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700100 return ff;
101}
102
Max Reitzfcee2162020-05-06 17:44:12 +0200103static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200104 int error)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100105{
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200106 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
107
108 iput(ra->inode);
109 kfree(ra);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100110}
111
Chad Austin2e64ff12018-12-10 10:54:52 -0800112static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700113{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200114 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200115 struct fuse_args *args = &ff->release_args->args;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200116
Max Reitzfcee2162020-05-06 17:44:12 +0200117 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200118 /* Do nothing when client does not implement 'open' */
Max Reitzfcee2162020-05-06 17:44:12 +0200119 fuse_release_end(ff->fm, args, 0);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100120 } else if (sync) {
Max Reitzfcee2162020-05-06 17:44:12 +0200121 fuse_simple_request(ff->fm, args);
122 fuse_release_end(ff->fm, args, 0);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100123 } else {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200124 args->end = fuse_release_end;
Max Reitzfcee2162020-05-06 17:44:12 +0200125 if (fuse_simple_background(ff->fm, args,
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200126 GFP_KERNEL | __GFP_NOFAIL))
Max Reitzfcee2162020-05-06 17:44:12 +0200127 fuse_release_end(ff->fm, args, -ENOTCONN);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100128 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700129 kfree(ff);
130 }
131}
132
Max Reitzfcee2162020-05-06 17:44:12 +0200133int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
Tejun Heo08cbf542009-04-14 10:54:53 +0900134 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200135{
Max Reitzfcee2162020-05-06 17:44:12 +0200136 struct fuse_conn *fc = fm->fc;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200137 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200138 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
139
Max Reitzfcee2162020-05-06 17:44:12 +0200140 ff = fuse_file_alloc(fm);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200141 if (!ff)
142 return -ENOMEM;
143
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100144 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800145 /* Default for no-open */
146 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800147 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100148 struct fuse_open_out outarg;
149 int err;
150
Max Reitzfcee2162020-05-06 17:44:12 +0200151 err = fuse_send_open(fm, nodeid, file, opcode, &outarg);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100152 if (!err) {
153 ff->fh = outarg.fh;
154 ff->open_flags = outarg.open_flags;
155
Chad Austind9a9ea92019-01-07 16:53:17 -0800156 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100157 fuse_file_free(ff);
158 return err;
159 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800160 if (isdir)
161 fc->no_opendir = 1;
162 else
163 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100164 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200165 }
166
167 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100168 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200169
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200170 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100171 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200172
173 return 0;
174}
Tejun Heo08cbf542009-04-14 10:54:53 +0900175EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200176
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400177static void fuse_link_write_file(struct file *file)
178{
179 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400180 struct fuse_inode *fi = get_fuse_inode(inode);
181 struct fuse_file *ff = file->private_data;
182 /*
183 * file may be written through mmap, so chain it onto the
184 * inodes's write_file list
185 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300186 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400187 if (list_empty(&ff->write_entry))
188 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300189 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400190}
191
Miklos Szeredic7b71432009-04-28 16:56:37 +0200192void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800193{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200194 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800195 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200196
Miklos Szeredic7b71432009-04-28 16:56:37 +0200197 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700198 invalidate_inode_pages2(inode->i_mapping);
Kirill Smelkovbbd84f32019-04-24 07:13:57 +0000199 if (ff->open_flags & FOPEN_STREAM)
200 stream_open(inode, file);
201 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200202 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800203 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
204 struct fuse_inode *fi = get_fuse_inode(inode);
205
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300206 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300207 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800208 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300209 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800210 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200211 if (fc->writeback_cache)
212 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800213 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400214 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
215 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800216}
217
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200218int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800219{
Max Reitzfcee2162020-05-06 17:44:12 +0200220 struct fuse_mount *fm = get_fuse_mount(inode);
221 struct fuse_conn *fc = fm->fc;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222 int err;
Miklos Szeredie4648302019-10-23 14:26:37 +0200223 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200224 fc->atomic_o_trunc &&
225 fc->writeback_cache;
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400226 bool dax_truncate = (file->f_flags & O_TRUNC) &&
227 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700228
229 err = generic_file_open(inode, file);
230 if (err)
231 return err;
232
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400233 if (is_wb_truncate || dax_truncate) {
Al Viro59551022016-01-22 15:40:57 -0500234 inode_lock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200235 fuse_set_nowrite(inode);
236 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200237
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400238 if (dax_truncate) {
239 down_write(&get_fuse_inode(inode)->i_mmap_sem);
240 err = fuse_dax_break_layouts(inode, 0, 0);
241 if (err)
242 goto out;
243 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244
Max Reitzfcee2162020-05-06 17:44:12 +0200245 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200246 if (!err)
247 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200248
Vivek Goyal6ae330c2020-08-19 18:19:54 -0400249out:
250 if (dax_truncate)
251 up_write(&get_fuse_inode(inode)->i_mmap_sem);
252
253 if (is_wb_truncate | dax_truncate) {
Miklos Szeredie4648302019-10-23 14:26:37 +0200254 fuse_release_nowrite(inode);
Al Viro59551022016-01-22 15:40:57 -0500255 inode_unlock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200256 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200257
258 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700259}
260
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300261static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
262 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800263{
Max Reitzfcee2162020-05-06 17:44:12 +0200264 struct fuse_conn *fc = ff->fm->fc;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200265 struct fuse_release_args *ra = ff->release_args;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300267 /* Inode is NULL on error path of fuse_create_open() */
268 if (likely(fi)) {
269 spin_lock(&fi->lock);
270 list_del(&ff->write_entry);
271 spin_unlock(&fi->lock);
272 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200273 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200274 if (!RB_EMPTY_NODE(&ff->polled_node))
275 rb_erase(&ff->polled_node, &fc->polled_files);
276 spin_unlock(&fc->lock);
277
Bryan Green357ccf22011-03-01 16:43:52 -0800278 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200279
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200280 ra->inarg.fh = ff->fh;
281 ra->inarg.flags = flags;
282 ra->args.in_numargs = 1;
283 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
284 ra->args.in_args[0].value = &ra->inarg;
285 ra->args.opcode = opcode;
286 ra->args.nodeid = ff->nodeid;
287 ra->args.force = true;
288 ra->args.nocreds = true;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800289}
290
Chad Austin2e64ff12018-12-10 10:54:52 -0800291void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800292{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300293 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100294 struct fuse_file *ff = file->private_data;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200295 struct fuse_release_args *ra = ff->release_args;
Chad Austin2e64ff12018-12-10 10:54:52 -0800296 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700297
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300298 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100299
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200300 if (ff->flock) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200301 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
Max Reitzfcee2162020-05-06 17:44:12 +0200302 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc,
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200303 (fl_owner_t) file);
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200304 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100305 /* Hold inode until release is finished */
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200306 ra->inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700307
Tejun Heo6b2db282009-04-14 10:54:49 +0900308 /*
309 * Normally this will send the RELEASE request, however if
310 * some asynchronous READ or WRITE requests are outstanding,
311 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100312 *
313 * Make the release synchronous if this is a fuseblk mount,
314 * synchronous RELEASE is allowed (and desirable) in this case
315 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900316 */
Max Reitzfcee2162020-05-06 17:44:12 +0200317 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700318}
319
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700320static int fuse_open(struct inode *inode, struct file *file)
321{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200322 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700323}
324
325static int fuse_release(struct inode *inode, struct file *file)
326{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400327 struct fuse_conn *fc = get_fuse_conn(inode);
328
329 /* see fuse_vma_close() for !writeback_cache case */
330 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200331 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400332
Chad Austin2e64ff12018-12-10 10:54:52 -0800333 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200334
335 /* return value is ignored by VFS */
336 return 0;
337}
338
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300339void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200340{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200341 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300342 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100343 /*
344 * iput(NULL) is a no-op and since the refcount is 1 and everything's
345 * synchronous, we are fine with not doing igrab() here"
346 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800347 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700348}
Tejun Heo08cbf542009-04-14 10:54:53 +0900349EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700350
Miklos Szeredi71421252006-06-25 05:48:52 -0700351/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700352 * Scramble the ID space with XTEA, so that the value of the files_struct
353 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700354 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700355u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700356{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700357 u32 *k = fc->scramble_key;
358 u64 v = (unsigned long) id;
359 u32 v0 = v;
360 u32 v1 = v >> 32;
361 u32 sum = 0;
362 int i;
363
364 for (i = 0; i < 32; i++) {
365 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
366 sum += 0x9E3779B9;
367 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
368 }
369
370 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700371}
372
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200373struct fuse_writepage_args {
374 struct fuse_io_args ia;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300375 struct rb_node writepages_entry;
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200376 struct list_head queue_entry;
377 struct fuse_writepage_args *next;
378 struct inode *inode;
379};
380
381static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100382 pgoff_t idx_from, pgoff_t idx_to)
383{
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300384 struct rb_node *n;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100385
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300386 n = fi->writepages.rb_node;
387
388 while (n) {
389 struct fuse_writepage_args *wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100390 pgoff_t curr_index;
391
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300392 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200393 WARN_ON(get_fuse_inode(wpa->inode) != fi);
394 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +0300395 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
396 n = n->rb_right;
397 else if (idx_to < curr_index)
398 n = n->rb_left;
399 else
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200400 return wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100401 }
402 return NULL;
403}
404
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700405/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400406 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700407 *
408 * This is currently done by walking the list of writepage requests
409 * for the inode, which can be pretty inefficient.
410 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400411static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
412 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700413{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700414 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100415 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700416
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300417 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100418 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300419 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700420
421 return found;
422}
423
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400424static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
425{
426 return fuse_range_is_writeback(inode, index, index);
427}
428
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700429/*
430 * Wait for page writeback to be completed.
431 *
432 * Since fuse doesn't rely on the VM writeback tracking, this has to
433 * use some other means.
434 */
Maxim Patlasov17b2cbe2019-07-22 10:17:17 +0300435static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700436{
437 struct fuse_inode *fi = get_fuse_inode(inode);
438
439 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700440}
441
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400442/*
443 * Wait for all pending writepages on the inode to finish.
444 *
445 * This is currently done by blocking further writes with FUSE_NOWRITE
446 * and waiting for all sent writes to complete.
447 *
448 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
449 * could conflict with truncation.
450 */
451static void fuse_sync_writes(struct inode *inode)
452{
453 fuse_set_nowrite(inode);
454 fuse_release_nowrite(inode);
455}
456
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700457static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458{
Al Viro6131ffa2013-02-27 16:59:05 -0500459 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +0200460 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 struct fuse_flush_in inarg;
Miklos Szeredic500eba2019-09-10 15:04:08 +0200463 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464 int err;
465
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800466 if (is_bad_inode(inode))
467 return -EIO;
468
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200469 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400470 if (err)
471 return err;
472
Al Viro59551022016-01-22 15:40:57 -0500473 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400474 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500475 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400476
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200477 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700478 if (err)
479 return err;
480
Miklos Szeredi614c0262020-05-19 14:50:37 +0200481 err = 0;
Max Reitzfcee2162020-05-06 17:44:12 +0200482 if (fm->fc->no_flush)
Miklos Szeredi614c0262020-05-19 14:50:37 +0200483 goto inval_attr_out;
484
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700485 memset(&inarg, 0, sizeof(inarg));
486 inarg.fh = ff->fh;
Max Reitzfcee2162020-05-06 17:44:12 +0200487 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200488 args.opcode = FUSE_FLUSH;
489 args.nodeid = get_node_id(inode);
490 args.in_numargs = 1;
491 args.in_args[0].size = sizeof(inarg);
492 args.in_args[0].value = &inarg;
493 args.force = true;
494
Max Reitzfcee2162020-05-06 17:44:12 +0200495 err = fuse_simple_request(fm, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700496 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +0200497 fm->fc->no_flush = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498 err = 0;
499 }
Eryu Guancf576c52020-05-12 10:29:04 +0800500
501inval_attr_out:
502 /*
503 * In memory i_blocks is not maintained by fuse, if writeback cache is
504 * enabled, i_blocks from cached attr may not be accurate.
505 */
Max Reitzfcee2162020-05-06 17:44:12 +0200506 if (!err && fm->fc->writeback_cache)
Eryu Guancf576c52020-05-12 10:29:04 +0800507 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700508 return err;
509}
510
Josef Bacik02c24a82011-07-16 20:44:56 -0400511int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100512 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700513{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200514 struct inode *inode = file->f_mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +0200515 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100517 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700518 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100519
520 memset(&inarg, 0, sizeof(inarg));
521 inarg.fh = ff->fh;
Alan Somers154603f2019-04-19 15:42:44 -0600522 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200523 args.opcode = opcode;
524 args.nodeid = get_node_id(inode);
525 args.in_numargs = 1;
526 args.in_args[0].size = sizeof(inarg);
527 args.in_args[0].value = &inarg;
Max Reitzfcee2162020-05-06 17:44:12 +0200528 return fuse_simple_request(fm, &args);
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100529}
530
531static int fuse_fsync(struct file *file, loff_t start, loff_t end,
532 int datasync)
533{
534 struct inode *inode = file->f_mapping->host;
535 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700536 int err;
537
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800538 if (is_bad_inode(inode))
539 return -EIO;
540
Al Viro59551022016-01-22 15:40:57 -0500541 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400542
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700543 /*
544 * Start writeback against all dirty pages of the inode, then
545 * wait for all outstanding writes, before sending the FSYNC
546 * request.
547 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400548 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700549 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400550 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700551
552 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700553
554 /*
555 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400556 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700557 * We have to do this directly after fuse_sync_writes()
558 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400559 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700560 if (err)
561 goto out;
562
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200563 err = sync_inode_metadata(inode, 1);
564 if (err)
565 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700566
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100567 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200568 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400569
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100570 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700571 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100572 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700573 err = 0;
574 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400575out:
Al Viro59551022016-01-22 15:40:57 -0500576 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700577
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100578 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700579}
580
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200581void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
582 size_t count, int opcode)
583{
584 struct fuse_file *ff = file->private_data;
585 struct fuse_args *args = &ia->ap.args;
586
587 ia->read.in.fh = ff->fh;
588 ia->read.in.offset = pos;
589 ia->read.in.size = count;
590 ia->read.in.flags = file->f_flags;
591 args->opcode = opcode;
592 args->nodeid = ff->nodeid;
593 args->in_numargs = 1;
594 args->in_args[0].size = sizeof(ia->read.in);
595 args->in_args[0].value = &ia->read.in;
596 args->out_argvar = true;
597 args->out_numargs = 1;
598 args->out_args[0].size = count;
599}
600
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200601static void fuse_release_user_pages(struct fuse_args_pages *ap,
602 bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400603{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200604 unsigned int i;
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400605
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200606 for (i = 0; i < ap->num_pages; i++) {
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200607 if (should_dirty)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200608 set_page_dirty_lock(ap->pages[i]);
609 put_page(ap->pages[i]);
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400610 }
611}
612
Seth Forshee744742d2016-03-11 10:35:34 -0600613static void fuse_io_release(struct kref *kref)
614{
615 kfree(container_of(kref, struct fuse_io_priv, refcnt));
616}
617
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100618static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
619{
620 if (io->err)
621 return io->err;
622
623 if (io->bytes >= 0 && io->write)
624 return -EIO;
625
626 return io->bytes < 0 ? io->size : io->bytes;
627}
628
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400629/**
630 * In case of short read, the caller sets 'pos' to the position of
631 * actual end of fuse request in IO request. Otherwise, if bytes_requested
632 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
633 *
634 * An example:
635 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
636 * both submitted asynchronously. The first of them was ACKed by userspace as
637 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
638 * second request was ACKed as short, e.g. only 1K was read, resulting in
639 * pos == 33K.
640 *
641 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
642 * will be equal to the length of the longest contiguous fragment of
643 * transferred data starting from the beginning of IO request.
644 */
645static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
646{
647 int left;
648
649 spin_lock(&io->lock);
650 if (err)
651 io->err = io->err ? : err;
652 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
653 io->bytes = pos;
654
655 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530656 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100657 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400658 spin_unlock(&io->lock);
659
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530660 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100661 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400662
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100663 if (res >= 0) {
664 struct inode *inode = file_inode(io->iocb->ki_filp);
665 struct fuse_conn *fc = get_fuse_conn(inode);
666 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400667
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300668 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300669 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300670 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400671 }
672
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100673 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400674 }
Seth Forshee744742d2016-03-11 10:35:34 -0600675
676 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400677}
678
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200679static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
680 unsigned int npages)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400681{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200682 struct fuse_io_args *ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400683
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200684 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
685 if (ia) {
686 ia->io = io;
687 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
688 &ia->ap.descs);
689 if (!ia->ap.pages) {
690 kfree(ia);
691 ia = NULL;
692 }
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400693 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200694 return ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400695}
696
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200697static void fuse_io_free(struct fuse_io_args *ia)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400698{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200699 kfree(ia->ap.pages);
700 kfree(ia);
701}
702
Max Reitzfcee2162020-05-06 17:44:12 +0200703static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200704 int err)
705{
706 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
707 struct fuse_io_priv *io = ia->io;
708 ssize_t pos = -1;
709
710 fuse_release_user_pages(&ia->ap, io->should_dirty);
711
712 if (err) {
713 /* Nothing */
714 } else if (io->write) {
715 if (ia->write.out.size > ia->write.in.size) {
716 err = -EIO;
717 } else if (ia->write.in.size != ia->write.out.size) {
718 pos = ia->write.in.offset - io->offset +
719 ia->write.out.size;
720 }
721 } else {
722 u32 outsize = args->out_args[0].size;
723
724 if (ia->read.in.size != outsize)
725 pos = ia->read.in.offset - io->offset + outsize;
726 }
727
728 fuse_aio_complete(io, err, pos);
729 fuse_io_free(ia);
730}
731
Max Reitzfcee2162020-05-06 17:44:12 +0200732static ssize_t fuse_async_req_send(struct fuse_mount *fm,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200733 struct fuse_io_args *ia, size_t num_bytes)
734{
735 ssize_t err;
736 struct fuse_io_priv *io = ia->io;
737
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400738 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600739 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400740 io->size += num_bytes;
741 io->reqs++;
742 spin_unlock(&io->lock);
743
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200744 ia->ap.args.end = fuse_aio_complete_req;
Vivek Goyalbb737bb2020-04-20 17:01:34 +0200745 ia->ap.args.may_block = io->should_dirty;
Max Reitzfcee2162020-05-06 17:44:12 +0200746 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100747 if (err)
Max Reitzfcee2162020-05-06 17:44:12 +0200748 fuse_aio_complete_req(fm, &ia->ap.args, err);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400749
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100750 return num_bytes;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400751}
752
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200753static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
754 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700755{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200756 struct file *file = ia->io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200757 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +0200758 struct fuse_mount *fm = ff->fm;
Miklos Szeredif3332112007-10-18 03:07:04 -0700759
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200760 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700761 if (owner != NULL) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200762 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
Max Reitzfcee2162020-05-06 17:44:12 +0200763 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -0700764 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400765
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200766 if (ia->io->async)
Max Reitzfcee2162020-05-06 17:44:12 +0200767 return fuse_async_req_send(fm, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400768
Max Reitzfcee2162020-05-06 17:44:12 +0200769 return fuse_simple_request(fm, &ia->ap.args);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700770}
771
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700772static void fuse_read_update_size(struct inode *inode, loff_t size,
773 u64 attr_ver)
774{
775 struct fuse_conn *fc = get_fuse_conn(inode);
776 struct fuse_inode *fi = get_fuse_inode(inode);
777
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300778 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400779 if (attr_ver == fi->attr_version && size < inode->i_size &&
780 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300781 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700782 i_size_write(inode, size);
783 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300784 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700785}
786
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200787static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200788 struct fuse_args_pages *ap)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400789{
Pavel Emelyanov83732002013-10-10 17:10:46 +0400790 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400791
Pavel Emelyanov83732002013-10-10 17:10:46 +0400792 if (fc->writeback_cache) {
793 /*
794 * A hole in a file. Some data after the hole are in page cache,
795 * but have not reached the client fs yet. So, the hole is not
796 * present there.
797 */
798 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300799 int start_idx = num_read >> PAGE_SHIFT;
800 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400801
Miklos Szeredi134831e2019-09-10 15:04:10 +0200802 for (i = start_idx; i < ap->num_pages; i++) {
803 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400804 off = 0;
805 }
806 } else {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200807 loff_t pos = page_offset(ap->pages[0]) + num_read;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400808 fuse_read_update_size(inode, pos, attr_ver);
809 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400810}
811
Maxim Patlasov482fce52013-10-10 17:11:25 +0400812static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700813{
814 struct inode *inode = page->mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +0200815 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700816 loff_t pos = page_offset(page);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200817 struct fuse_page_desc desc = { .length = PAGE_SIZE };
818 struct fuse_io_args ia = {
819 .ap.args.page_zeroing = true,
820 .ap.args.out_pages = true,
821 .ap.num_pages = 1,
822 .ap.pages = &page,
823 .ap.descs = &desc,
824 };
825 ssize_t res;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700826 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800827
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700828 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300829 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700830 * page-cache page, so make sure we read a properly synced
831 * page.
832 */
833 fuse_wait_on_page_writeback(inode, page->index);
834
Max Reitzfcee2162020-05-06 17:44:12 +0200835 attr_ver = fuse_get_attr_version(fm->fc);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700836
Miklos Szeredi2f139822020-02-06 16:39:28 +0100837 /* Don't overflow end offset */
838 if (pos + (desc.length - 1) == LLONG_MAX)
839 desc.length--;
840
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200841 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
Max Reitzfcee2162020-05-06 17:44:12 +0200842 res = fuse_simple_request(fm, &ia.ap.args);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200843 if (res < 0)
844 return res;
845 /*
846 * Short read means EOF. If file size is larger, truncate it
847 */
848 if (res < desc.length)
Miklos Szeredi134831e2019-09-10 15:04:10 +0200849 fuse_short_read(inode, attr_ver, res, &ia.ap);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700850
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200851 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700852
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200853 return 0;
Maxim Patlasov482fce52013-10-10 17:11:25 +0400854}
855
856static int fuse_readpage(struct file *file, struct page *page)
857{
858 struct inode *inode = page->mapping->host;
859 int err;
860
861 err = -EIO;
862 if (is_bad_inode(inode))
863 goto out;
864
865 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800866 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700867 out:
868 unlock_page(page);
869 return err;
870}
871
Max Reitzfcee2162020-05-06 17:44:12 +0200872static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200873 int err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700874{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800875 int i;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200876 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
877 struct fuse_args_pages *ap = &ia->ap;
878 size_t count = ia->read.in.size;
879 size_t num_read = args->out_args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200880 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800881
Miklos Szeredi134831e2019-09-10 15:04:10 +0200882 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
883 mapping = ap->pages[i]->mapping;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200884
885 if (mapping) {
886 struct inode *inode = mapping->host;
887
888 /*
889 * Short read means EOF. If file size is larger, truncate it
890 */
Miklos Szeredi134831e2019-09-10 15:04:10 +0200891 if (!err && num_read < count)
892 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200893
Andrew Gallagher451418f2013-11-05 03:55:43 -0800894 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700895 }
896
Miklos Szeredi134831e2019-09-10 15:04:10 +0200897 for (i = 0; i < ap->num_pages; i++) {
898 struct page *page = ap->pages[i];
899
900 if (!err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700901 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800902 else
903 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700904 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300905 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700906 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200907 if (ia->ff)
908 fuse_file_put(ia->ff, false, false);
909
910 fuse_io_free(ia);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800911}
912
Miklos Szeredi134831e2019-09-10 15:04:10 +0200913static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800914{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200915 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +0200916 struct fuse_mount *fm = ff->fm;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200917 struct fuse_args_pages *ap = &ia->ap;
918 loff_t pos = page_offset(ap->pages[0]);
919 size_t count = ap->num_pages << PAGE_SHIFT;
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100920 ssize_t res;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200921 int err;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200922
Miklos Szeredi134831e2019-09-10 15:04:10 +0200923 ap->args.out_pages = true;
924 ap->args.page_zeroing = true;
925 ap->args.page_replace = true;
Miklos Szeredi2f139822020-02-06 16:39:28 +0100926
927 /* Don't overflow end offset */
928 if (pos + (count - 1) == LLONG_MAX) {
929 count--;
930 ap->descs[ap->num_pages - 1].length--;
931 }
932 WARN_ON((loff_t) (pos + count) < 0);
933
Miklos Szeredi134831e2019-09-10 15:04:10 +0200934 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Max Reitzfcee2162020-05-06 17:44:12 +0200935 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
936 if (fm->fc->async_read) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200937 ia->ff = fuse_file_get(ff);
938 ap->args.end = fuse_readpages_end;
Max Reitzfcee2162020-05-06 17:44:12 +0200939 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
Miklos Szeredi134831e2019-09-10 15:04:10 +0200940 if (!err)
941 return;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800942 } else {
Max Reitzfcee2162020-05-06 17:44:12 +0200943 res = fuse_simple_request(fm, &ap->args);
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100944 err = res < 0 ? res : 0;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800945 }
Max Reitzfcee2162020-05-06 17:44:12 +0200946 fuse_readpages_end(fm, &ap->args, err);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700947}
948
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700949static void fuse_readahead(struct readahead_control *rac)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700950{
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700951 struct inode *inode = rac->mapping->host;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700952 struct fuse_conn *fc = get_fuse_conn(inode);
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700953 unsigned int i, max_pages, nr_pages = 0;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700954
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800955 if (is_bad_inode(inode))
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700956 return;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800957
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700958 max_pages = min_t(unsigned int, fc->max_pages,
959 fc->max_read / PAGE_SIZE);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700960
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700961 for (;;) {
962 struct fuse_io_args *ia;
963 struct fuse_args_pages *ap;
964
965 nr_pages = readahead_count(rac) - nr_pages;
966 if (nr_pages > max_pages)
967 nr_pages = max_pages;
968 if (nr_pages == 0)
969 break;
970 ia = fuse_io_alloc(NULL, nr_pages);
971 if (!ia)
972 return;
973 ap = &ia->ap;
974 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
975 for (i = 0; i < nr_pages; i++) {
976 fuse_wait_on_page_writeback(inode,
977 readahead_index(rac) + i);
978 ap->descs[i].length = PAGE_SIZE;
979 }
980 ap->num_pages = nr_pages;
981 fuse_send_readpages(ia, rac->file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700982 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700983}
984
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100985static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800986{
987 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400988 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800989
Brian Fostera8894272012-07-16 15:23:50 -0400990 /*
991 * In auto invalidate mode, always update attributes on read.
992 * Otherwise, only update if we attempt to read past EOF (to ensure
993 * i_size is up to date).
994 */
995 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400996 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800997 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200998 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800999 if (err)
1000 return err;
1001 }
1002
Al Viro37c20f12014-04-02 14:47:09 -04001003 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001004}
1005
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001006static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1007 loff_t pos, size_t count)
1008{
1009 struct fuse_args *args = &ia->ap.args;
1010
1011 ia->write.in.fh = ff->fh;
1012 ia->write.in.offset = pos;
1013 ia->write.in.size = count;
1014 args->opcode = FUSE_WRITE;
1015 args->nodeid = ff->nodeid;
1016 args->in_numargs = 2;
Max Reitzfcee2162020-05-06 17:44:12 +02001017 if (ff->fm->fc->minor < 9)
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001018 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1019 else
1020 args->in_args[0].size = sizeof(ia->write.in);
1021 args->in_args[0].value = &ia->write.in;
1022 args->in_args[1].size = count;
1023 args->out_numargs = 1;
1024 args->out_args[0].size = sizeof(ia->write.out);
1025 args->out_args[0].value = &ia->write.out;
1026}
1027
1028static unsigned int fuse_write_flags(struct kiocb *iocb)
1029{
1030 unsigned int flags = iocb->ki_filp->f_flags;
1031
1032 if (iocb->ki_flags & IOCB_DSYNC)
1033 flags |= O_DSYNC;
1034 if (iocb->ki_flags & IOCB_SYNC)
1035 flags |= O_SYNC;
1036
1037 return flags;
1038}
1039
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001040static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1041 size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001042{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001043 struct kiocb *iocb = ia->io->iocb;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001044 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001045 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001046 struct fuse_mount *fm = ff->fm;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001047 struct fuse_write_in *inarg = &ia->write.in;
1048 ssize_t err;
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001049
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001050 fuse_write_args_fill(ia, ff, pos, count);
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001051 inarg->flags = fuse_write_flags(iocb);
Miklos Szeredif3332112007-10-18 03:07:04 -07001052 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001053 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
Max Reitzfcee2162020-05-06 17:44:12 +02001054 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -07001055 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001056
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001057 if (ia->io->async)
Max Reitzfcee2162020-05-06 17:44:12 +02001058 return fuse_async_req_send(fm, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001059
Max Reitzfcee2162020-05-06 17:44:12 +02001060 err = fuse_simple_request(fm, &ia->ap.args);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001061 if (!err && ia->write.out.size > count)
1062 err = -EIO;
1063
1064 return err ?: ia->write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001065}
1066
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001067bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001068{
1069 struct fuse_conn *fc = get_fuse_conn(inode);
1070 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001071 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001072
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001073 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001074 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001075 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001076 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001077 ret = true;
1078 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001079 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001080
1081 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001082}
1083
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001084static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1085 struct kiocb *iocb, struct inode *inode,
1086 loff_t pos, size_t count)
Nick Pigginea9b9902008-04-30 00:54:42 -07001087{
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001088 struct fuse_args_pages *ap = &ia->ap;
1089 struct file *file = iocb->ki_filp;
1090 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001091 struct fuse_mount *fm = ff->fm;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001092 unsigned int offset, i;
1093 int err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001094
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001095 for (i = 0; i < ap->num_pages; i++)
1096 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Nick Pigginea9b9902008-04-30 00:54:42 -07001097
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001098 fuse_write_args_fill(ia, ff, pos, count);
1099 ia->write.in.flags = fuse_write_flags(iocb);
Vivek Goyalb8667392020-10-09 14:15:08 -04001100 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1101 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
Nick Pigginea9b9902008-04-30 00:54:42 -07001102
Max Reitzfcee2162020-05-06 17:44:12 +02001103 err = fuse_simple_request(fm, &ap->args);
Miklos Szeredi8aab3362019-11-12 11:49:04 +01001104 if (!err && ia->write.out.size > count)
1105 err = -EIO;
Nick Pigginea9b9902008-04-30 00:54:42 -07001106
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001107 offset = ap->descs[0].offset;
1108 count = ia->write.out.size;
1109 for (i = 0; i < ap->num_pages; i++) {
1110 struct page *page = ap->pages[i];
1111
1112 if (!err && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001113 SetPageUptodate(page);
1114
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001115 if (count > PAGE_SIZE - offset)
1116 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001117 else
1118 count = 0;
1119 offset = 0;
1120
1121 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001122 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001123 }
1124
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001125 return err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001126}
1127
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001128static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1129 struct address_space *mapping,
1130 struct iov_iter *ii, loff_t pos,
1131 unsigned int max_pages)
Nick Pigginea9b9902008-04-30 00:54:42 -07001132{
1133 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001134 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001135 size_t count = 0;
1136 int err;
1137
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001138 ap->args.in_pages = true;
1139 ap->descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001140
1141 do {
1142 size_t tmp;
1143 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001144 pgoff_t index = pos >> PAGE_SHIFT;
1145 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 iov_iter_count(ii));
1147
1148 bytes = min_t(size_t, bytes, fc->max_write - count);
1149
1150 again:
1151 err = -EFAULT;
1152 if (iov_iter_fault_in_readable(ii, bytes))
1153 break;
1154
1155 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001156 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001157 if (!page)
1158 break;
1159
anfei zhou931e80e2010-02-02 13:44:02 -08001160 if (mapping_writably_mapped(mapping))
1161 flush_dcache_page(page);
1162
Nick Pigginea9b9902008-04-30 00:54:42 -07001163 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001164 flush_dcache_page(page);
1165
Roman Gushchin3ca81382015-10-12 16:33:44 +03001166 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001167 if (!tmp) {
1168 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 bytes = min(bytes, iov_iter_single_seg_count(ii));
1171 goto again;
1172 }
1173
1174 err = 0;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001175 ap->pages[ap->num_pages] = page;
1176 ap->descs[ap->num_pages].length = tmp;
1177 ap->num_pages++;
Nick Pigginea9b9902008-04-30 00:54:42 -07001178
Nick Pigginea9b9902008-04-30 00:54:42 -07001179 count += tmp;
1180 pos += tmp;
1181 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001182 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001183 offset = 0;
1184
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001185 if (!fc->big_writes)
1186 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001187 } while (iov_iter_count(ii) && count < fc->max_write &&
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001188 ap->num_pages < max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001189
1190 return count > 0 ? count : err;
1191}
1192
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001193static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1194 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001195{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001196 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001197 ((pos + len - 1) >> PAGE_SHIFT) -
1198 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001199 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001200}
1201
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001202static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001203 struct address_space *mapping,
1204 struct iov_iter *ii, loff_t pos)
1205{
1206 struct inode *inode = mapping->host;
1207 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001208 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001209 int err = 0;
1210 ssize_t res = 0;
1211
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001212 if (inode->i_size < pos + iov_iter_count(ii))
1213 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1214
Nick Pigginea9b9902008-04-30 00:54:42 -07001215 do {
Nick Pigginea9b9902008-04-30 00:54:42 -07001216 ssize_t count;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001217 struct fuse_io_args ia = {};
1218 struct fuse_args_pages *ap = &ia.ap;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001219 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1220 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001221
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001222 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1223 if (!ap->pages) {
1224 err = -ENOMEM;
Nick Pigginea9b9902008-04-30 00:54:42 -07001225 break;
1226 }
1227
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001228 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001229 if (count <= 0) {
1230 err = count;
1231 } else {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001232 err = fuse_send_write_pages(&ia, iocb, inode,
1233 pos, count);
Nick Pigginea9b9902008-04-30 00:54:42 -07001234 if (!err) {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001235 size_t num_written = ia.write.out.size;
1236
Nick Pigginea9b9902008-04-30 00:54:42 -07001237 res += num_written;
1238 pos += num_written;
1239
1240 /* break out of the loop on short write */
1241 if (num_written != count)
1242 err = -EIO;
1243 }
1244 }
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001245 kfree(ap->pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001246 } while (!err && iov_iter_count(ii));
1247
1248 if (res > 0)
1249 fuse_write_update_size(inode, pos);
1250
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001251 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001252 fuse_invalidate_attr(inode);
1253
1254 return res > 0 ? res : err;
1255}
1256
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001257static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001258{
1259 struct file *file = iocb->ki_filp;
1260 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001261 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001262 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001263 struct inode *inode = mapping->host;
1264 ssize_t err;
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001265 struct fuse_conn *fc = get_fuse_conn(inode);
Anand Avati4273b792012-02-17 12:46:25 -05001266 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001267
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001268 if (fc->writeback_cache) {
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001269 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001270 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001271 if (err)
1272 return err;
1273
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001274 if (fc->handle_killpriv_v2 &&
1275 should_remove_suid(file_dentry(file))) {
1276 goto writethrough;
1277 }
1278
Al Viro84c3d552014-04-03 14:33:23 -04001279 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001280 }
1281
Vivek Goyal8981bdf2020-10-09 14:15:10 -04001282writethrough:
Al Viro59551022016-01-22 15:40:57 -05001283 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001284
1285 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001286 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001287
Al Viro3309dd02015-04-09 12:55:47 -04001288 err = generic_write_checks(iocb, from);
1289 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001290 goto out;
1291
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001292 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001293 if (err)
1294 goto out;
1295
Josef Bacikc3b2da32012-03-26 09:59:21 -04001296 err = file_update_time(file);
1297 if (err)
1298 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001299
Al Viro2ba48ce2015-04-09 13:52:01 -04001300 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001301 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001302 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001303 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001304 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001305
Anand Avati4273b792012-02-17 12:46:25 -05001306 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001307
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001308 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001309 if (written_buffered < 0) {
1310 err = written_buffered;
1311 goto out;
1312 }
1313 endbyte = pos + written_buffered - 1;
1314
1315 err = filemap_write_and_wait_range(file->f_mapping, pos,
1316 endbyte);
1317 if (err)
1318 goto out;
1319
1320 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001321 pos >> PAGE_SHIFT,
1322 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001323
1324 written += written_buffered;
1325 iocb->ki_pos = pos + written_buffered;
1326 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001327 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001328 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001329 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001330 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001331out:
1332 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001333 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001334 if (written > 0)
1335 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001336
1337 return written ? written : err;
1338}
1339
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001340static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1341 unsigned int index,
1342 unsigned int nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001343{
1344 int i;
1345
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001346 for (i = index; i < index + nr_pages; i++)
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001347 descs[i].length = PAGE_SIZE - descs[i].offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001348}
1349
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001350static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1351{
1352 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1353}
1354
1355static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1356 size_t max_size)
1357{
1358 return min(iov_iter_single_seg_count(ii), max_size);
1359}
1360
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001361static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1362 size_t *nbytesp, int write,
1363 unsigned int max_pages)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001365 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001366 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001367
Miklos Szeredif4975c62009-04-02 14:25:34 +02001368 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001369 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001370 unsigned long user_addr = fuse_get_user_addr(ii);
1371 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1372
Miklos Szeredif4975c62009-04-02 14:25:34 +02001373 if (write)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001374 ap->args.in_args[1].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001375 else
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001376 ap->args.out_args[0].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001377
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001378 iov_iter_advance(ii, frag_size);
1379 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001380 return 0;
1381 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001382
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001383 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001384 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001385 size_t start;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001386 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001387 *nbytesp - nbytes,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001388 max_pages - ap->num_pages,
Al Viroc7f38882014-06-18 20:34:33 -04001389 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001390 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001391 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001392
Al Viroc9c37e22014-03-16 16:08:30 -04001393 iov_iter_advance(ii, ret);
1394 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001395
Al Viroc9c37e22014-03-16 16:08:30 -04001396 ret += start;
1397 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1398
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001399 ap->descs[ap->num_pages].offset = start;
1400 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001401
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001402 ap->num_pages += npages;
1403 ap->descs[ap->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001404 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001405 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001406
1407 if (write)
zhengbincabdb4f2020-01-14 20:39:45 +08001408 ap->args.in_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001409 else
zhengbincabdb4f2020-01-14 20:39:45 +08001410 ap->args.out_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001411
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001412 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001413
Ashish Samant2c932d42016-03-25 10:53:41 -07001414 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415}
1416
Al Virod22a9432014-03-16 15:50:47 -04001417ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1418 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001420 int write = flags & FUSE_DIO_WRITE;
1421 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001422 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001423 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001424 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02001425 struct fuse_conn *fc = ff->fm->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001426 size_t nmax = write ? fc->max_write : fc->max_read;
1427 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001428 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001429 pgoff_t idx_from = pos >> PAGE_SHIFT;
1430 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001431 ssize_t res = 0;
Ashish Samant742f9922016-03-14 21:57:35 -07001432 int err = 0;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001433 struct fuse_io_args *ia;
1434 unsigned int max_pages;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001435
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001436 max_pages = iov_iter_npages(iter, fc->max_pages);
1437 ia = fuse_io_alloc(io, max_pages);
1438 if (!ia)
1439 return -ENOMEM;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001441 ia->io = io;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001442 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1443 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001444 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001445 fuse_sync_writes(inode);
1446 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001447 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001448 }
1449
Ashish Samant61c12b42017-07-12 19:26:58 -07001450 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001451 while (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001452 ssize_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001453 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001454 size_t nbytes = min(count, nmax);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001455
1456 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1457 max_pages);
Ashish Samant742f9922016-03-14 21:57:35 -07001458 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001459 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001460
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001461 if (write) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001462 if (!capable(CAP_FSETID))
Miklos Szeredi10c52c82020-11-11 17:22:32 +01001463 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001464
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001465 nres = fuse_send_write(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001466 } else {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001467 nres = fuse_send_read(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001468 }
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001469
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001470 if (!io->async || nres < 0) {
1471 fuse_release_user_pages(&ia->ap, io->should_dirty);
1472 fuse_io_free(ia);
1473 }
1474 ia = NULL;
1475 if (nres < 0) {
Miklos Szeredif658ade2020-02-06 16:39:28 +01001476 iov_iter_revert(iter, nbytes);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001477 err = nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001478 break;
1479 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001480 WARN_ON(nres > nbytes);
1481
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001482 count -= nres;
1483 res += nres;
1484 pos += nres;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001485 if (nres != nbytes) {
1486 iov_iter_revert(iter, nbytes - nres);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001487 break;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001488 }
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001489 if (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001490 max_pages = iov_iter_npages(iter, fc->max_pages);
1491 ia = fuse_io_alloc(io, max_pages);
1492 if (!ia)
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001493 break;
1494 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001495 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001496 if (ia)
1497 fuse_io_free(ia);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001498 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001499 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001500
Ashish Samant742f9922016-03-14 21:57:35 -07001501 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001502}
Tejun Heo08cbf542009-04-14 10:54:53 +09001503EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001504
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001505static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001506 struct iov_iter *iter,
1507 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001508{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001509 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001510 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001511
Al Virod22a9432014-03-16 15:50:47 -04001512 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001513
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001514 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001515
1516 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001517}
1518
Martin Raiber23c94e12018-10-27 16:48:48 +00001519static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1520
Al Viro153162632015-03-30 22:08:36 -04001521static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001522{
Martin Raiber23c94e12018-10-27 16:48:48 +00001523 ssize_t res;
1524
1525 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001526 res = fuse_direct_IO(iocb, to);
1527 } else {
1528 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1529
1530 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1531 }
1532
1533 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001534}
1535
Al Viro153162632015-03-30 22:08:36 -04001536static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001537{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001538 struct inode *inode = file_inode(iocb->ki_filp);
1539 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001540 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001541
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001542 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001543 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001544 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001545 if (res > 0) {
1546 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1547 res = fuse_direct_IO(iocb, from);
1548 } else {
1549 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1550 FUSE_DIO_WRITE);
1551 }
1552 }
Al Viro812408f2015-03-30 22:15:58 -04001553 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001554 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001555 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001556 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001557
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001558 return res;
1559}
1560
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001561static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1562{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001563 struct file *file = iocb->ki_filp;
1564 struct fuse_file *ff = file->private_data;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001565 struct inode *inode = file_inode(file);
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001566
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001567 if (is_bad_inode(inode))
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001568 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001569
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001570 if (FUSE_IS_DAX(inode))
1571 return fuse_dax_read_iter(iocb, to);
1572
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001573 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1574 return fuse_cache_read_iter(iocb, to);
1575 else
1576 return fuse_direct_read_iter(iocb, to);
1577}
1578
1579static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1580{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001581 struct file *file = iocb->ki_filp;
1582 struct fuse_file *ff = file->private_data;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001583 struct inode *inode = file_inode(file);
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001584
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001585 if (is_bad_inode(inode))
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001586 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001587
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04001588 if (FUSE_IS_DAX(inode))
1589 return fuse_dax_write_iter(iocb, from);
1590
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001591 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1592 return fuse_cache_write_iter(iocb, from);
1593 else
1594 return fuse_direct_write_iter(iocb, from);
1595}
1596
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001597static void fuse_writepage_free(struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001598{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001599 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001600 int i;
1601
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001602 for (i = 0; i < ap->num_pages; i++)
1603 __free_page(ap->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001604
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001605 if (wpa->ia.ff)
1606 fuse_file_put(wpa->ia.ff, false, false);
1607
1608 kfree(ap->pages);
1609 kfree(wpa);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001610}
1611
Max Reitzfcee2162020-05-06 17:44:12 +02001612static void fuse_writepage_finish(struct fuse_mount *fm,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001613 struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001614{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001615 struct fuse_args_pages *ap = &wpa->ia.ap;
1616 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001617 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001618 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001619 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001620
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001621 for (i = 0; i < ap->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001622 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001623 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001624 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001625 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001626 wake_up(&fi->page_waitq);
1627}
1628
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001629/* Called under fi->lock, may release and reacquire it */
Max Reitzfcee2162020-05-06 17:44:12 +02001630static void fuse_send_writepage(struct fuse_mount *fm,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001631 struct fuse_writepage_args *wpa, loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001632__releases(fi->lock)
1633__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001634{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001635 struct fuse_writepage_args *aux, *next;
1636 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1637 struct fuse_write_in *inarg = &wpa->ia.write.in;
1638 struct fuse_args *args = &wpa->ia.ap.args;
1639 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1640 int err;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001641
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001642 fi->writectr++;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001643 if (inarg->offset + data_size <= size) {
1644 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001645 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001646 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001647 } else {
1648 /* Got truncated off completely */
1649 goto out_free;
1650 }
1651
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001652 args->in_args[1].size = inarg->size;
1653 args->force = true;
1654 args->nocreds = true;
1655
Max Reitzfcee2162020-05-06 17:44:12 +02001656 err = fuse_simple_background(fm, args, GFP_ATOMIC);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001657 if (err == -ENOMEM) {
1658 spin_unlock(&fi->lock);
Max Reitzfcee2162020-05-06 17:44:12 +02001659 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001660 spin_lock(&fi->lock);
1661 }
1662
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001663 /* Fails on broken connection only */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001664 if (unlikely(err))
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001665 goto out_free;
1666
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001667 return;
1668
1669 out_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001670 fi->writectr--;
Miklos Szeredi69a64872020-07-14 14:45:41 +02001671 rb_erase(&wpa->writepages_entry, &fi->writepages);
Max Reitzfcee2162020-05-06 17:44:12 +02001672 fuse_writepage_finish(fm, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001673 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001674
1675 /* After fuse_writepage_finish() aux request list is private */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001676 for (aux = wpa->next; aux; aux = next) {
1677 next = aux->next;
1678 aux->next = NULL;
1679 fuse_writepage_free(aux);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001680 }
1681
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001682 fuse_writepage_free(wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001683 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001684}
1685
1686/*
1687 * If fi->writectr is positive (no truncate or fsync going on) send
1688 * all queued writepage requests.
1689 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001690 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001691 */
1692void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001693__releases(fi->lock)
1694__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001695{
Max Reitzfcee2162020-05-06 17:44:12 +02001696 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001697 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9de5be02019-04-24 17:05:06 +02001698 loff_t crop = i_size_read(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001699 struct fuse_writepage_args *wpa;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700
1701 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001702 wpa = list_entry(fi->queued_writes.next,
1703 struct fuse_writepage_args, queue_entry);
1704 list_del_init(&wpa->queue_entry);
Max Reitzfcee2162020-05-06 17:44:12 +02001705 fuse_send_writepage(fm, wpa, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706 }
1707}
1708
Miklos Szeredic1460242020-07-14 14:45:41 +02001709static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1710 struct fuse_writepage_args *wpa)
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001711{
1712 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1713 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1714 struct rb_node **p = &root->rb_node;
1715 struct rb_node *parent = NULL;
1716
1717 WARN_ON(!wpa->ia.ap.num_pages);
1718 while (*p) {
1719 struct fuse_writepage_args *curr;
1720 pgoff_t curr_index;
1721
1722 parent = *p;
1723 curr = rb_entry(parent, struct fuse_writepage_args,
1724 writepages_entry);
1725 WARN_ON(curr->inode != wpa->inode);
1726 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1727
1728 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1729 p = &(*p)->rb_right;
1730 else if (idx_to < curr_index)
1731 p = &(*p)->rb_left;
1732 else
Miklos Szeredic1460242020-07-14 14:45:41 +02001733 return curr;
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001734 }
1735
1736 rb_link_node(&wpa->writepages_entry, parent, p);
1737 rb_insert_color(&wpa->writepages_entry, root);
Miklos Szeredic1460242020-07-14 14:45:41 +02001738 return NULL;
1739}
1740
1741static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1742{
1743 WARN_ON(fuse_insert_writeback(root, wpa));
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001744}
1745
Max Reitzfcee2162020-05-06 17:44:12 +02001746static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001747 int error)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001748{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001749 struct fuse_writepage_args *wpa =
1750 container_of(args, typeof(*wpa), ia.ap.args);
1751 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001752 struct fuse_inode *fi = get_fuse_inode(inode);
1753
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001754 mapping_set_error(inode->i_mapping, error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001755 spin_lock(&fi->lock);
Miklos Szeredi69a64872020-07-14 14:45:41 +02001756 rb_erase(&wpa->writepages_entry, &fi->writepages);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001757 while (wpa->next) {
Max Reitzfcee2162020-05-06 17:44:12 +02001758 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001759 struct fuse_write_in *inarg = &wpa->ia.write.in;
1760 struct fuse_writepage_args *next = wpa->next;
1761
1762 wpa->next = next->next;
1763 next->next = NULL;
1764 next->ia.ff = fuse_file_get(wpa->ia.ff);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001765 tree_insert(&fi->writepages, next);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001766
1767 /*
1768 * Skip fuse_flush_writepages() to make it easy to crop requests
1769 * based on primary request size.
1770 *
1771 * 1st case (trivial): there are no concurrent activities using
1772 * fuse_set/release_nowrite. Then we're on safe side because
1773 * fuse_flush_writepages() would call fuse_send_writepage()
1774 * anyway.
1775 *
1776 * 2nd case: someone called fuse_set_nowrite and it is waiting
1777 * now for completion of all in-flight requests. This happens
1778 * rarely and no more than once per page, so this should be
1779 * okay.
1780 *
1781 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1782 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1783 * that fuse_set_nowrite returned implies that all in-flight
1784 * requests were completed along with all of their secondary
1785 * requests. Further primary requests are blocked by negative
1786 * writectr. Hence there cannot be any in-flight requests and
1787 * no invocations of fuse_writepage_end() while we're in
1788 * fuse_set_nowrite..fuse_release_nowrite section.
1789 */
Max Reitzfcee2162020-05-06 17:44:12 +02001790 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001791 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001792 fi->writectr--;
Max Reitzfcee2162020-05-06 17:44:12 +02001793 fuse_writepage_finish(fm, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001794 spin_unlock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001795 fuse_writepage_free(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001796}
1797
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001798static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1799 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001800{
Miklos Szeredi72523422013-10-01 16:44:52 +02001801 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001802
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001803 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001804 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001805 ff = list_entry(fi->write_files.next, struct fuse_file,
1806 write_entry);
1807 fuse_file_get(ff);
1808 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001809 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001810
1811 return ff;
1812}
1813
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001814static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1815 struct fuse_inode *fi)
1816{
1817 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1818 WARN_ON(!ff);
1819 return ff;
1820}
1821
1822int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1823{
1824 struct fuse_conn *fc = get_fuse_conn(inode);
1825 struct fuse_inode *fi = get_fuse_inode(inode);
1826 struct fuse_file *ff;
1827 int err;
1828
1829 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001830 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001831 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001832 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001833
1834 return err;
1835}
1836
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001837static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1838{
1839 struct fuse_writepage_args *wpa;
1840 struct fuse_args_pages *ap;
1841
1842 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1843 if (wpa) {
1844 ap = &wpa->ia.ap;
1845 ap->num_pages = 0;
1846 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1847 if (!ap->pages) {
1848 kfree(wpa);
1849 wpa = NULL;
1850 }
1851 }
1852 return wpa;
1853
1854}
1855
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001856static int fuse_writepage_locked(struct page *page)
1857{
1858 struct address_space *mapping = page->mapping;
1859 struct inode *inode = mapping->host;
1860 struct fuse_conn *fc = get_fuse_conn(inode);
1861 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001862 struct fuse_writepage_args *wpa;
1863 struct fuse_args_pages *ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001864 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001865 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001866
1867 set_page_writeback(page);
1868
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001869 wpa = fuse_writepage_args_alloc();
1870 if (!wpa)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001871 goto err;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001872 ap = &wpa->ia.ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001873
1874 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1875 if (!tmp_page)
1876 goto err_free;
1877
Miklos Szeredi72523422013-10-01 16:44:52 +02001878 error = -EIO;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001879 wpa->ia.ff = fuse_write_file_get(fc, fi);
1880 if (!wpa->ia.ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001881 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001882
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001883 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001884
1885 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001886 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1887 wpa->next = NULL;
1888 ap->args.in_pages = true;
1889 ap->num_pages = 1;
1890 ap->pages[0] = tmp_page;
1891 ap->descs[0].offset = 0;
1892 ap->descs[0].length = PAGE_SIZE;
1893 ap->args.end = fuse_writepage_end;
1894 wpa->inode = inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001895
Tejun Heo93f78d82015-05-22 17:13:27 -04001896 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001897 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001898
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001899 spin_lock(&fi->lock);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03001900 tree_insert(&fi->writepages, wpa);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001901 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001902 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001903 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001904
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001905 end_page_writeback(page);
1906
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001907 return 0;
1908
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001909err_nofile:
1910 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001911err_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001912 kfree(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001913err:
Jeff Layton91839762017-05-25 06:57:50 -04001914 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001915 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001916 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001917}
1918
1919static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1920{
1921 int err;
1922
Miklos Szerediff17be02013-10-01 16:44:53 +02001923 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1924 /*
1925 * ->writepages() should be called for sync() and friends. We
1926 * should only get here on direct reclaim and then we are
1927 * allowed to skip a page which is already in flight
1928 */
1929 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1930
1931 redirty_page_for_writepage(wbc, page);
Vasily Averind5880c72019-09-13 18:17:11 +03001932 unlock_page(page);
Miklos Szerediff17be02013-10-01 16:44:53 +02001933 return 0;
1934 }
1935
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001936 err = fuse_writepage_locked(page);
1937 unlock_page(page);
1938
1939 return err;
1940}
1941
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001942struct fuse_fill_wb_data {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001943 struct fuse_writepage_args *wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001944 struct fuse_file *ff;
1945 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001946 struct page **orig_pages;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001947 unsigned int max_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001948};
1949
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001950static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1951{
1952 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1953 struct fuse_conn *fc = get_fuse_conn(data->inode);
1954 struct page **pages;
1955 struct fuse_page_desc *descs;
1956 unsigned int npages = min_t(unsigned int,
1957 max_t(unsigned int, data->max_pages * 2,
1958 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1959 fc->max_pages);
1960 WARN_ON(npages <= data->max_pages);
1961
1962 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1963 if (!pages)
1964 return false;
1965
1966 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1967 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1968 kfree(ap->pages);
1969 ap->pages = pages;
1970 ap->descs = descs;
1971 data->max_pages = npages;
1972
1973 return true;
1974}
1975
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001976static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1977{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001978 struct fuse_writepage_args *wpa = data->wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001979 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001980 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001981 int num_pages = wpa->ia.ap.num_pages;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001982 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001983
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001984 wpa->ia.ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001985 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001986 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001987 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001988 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001989
1990 for (i = 0; i < num_pages; i++)
1991 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001992}
1993
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001994/*
Miklos Szeredic1460242020-07-14 14:45:41 +02001995 * Check under fi->lock if the page is under writeback, and insert it onto the
1996 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
Miklos Szeredi419234d2019-01-16 10:27:59 +01001997 * one already added for a page at this offset. If there's none, then insert
1998 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredic1460242020-07-14 14:45:41 +02001999 * swapping the new temp page with the old one.
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002000 */
Miklos Szeredic1460242020-07-14 14:45:41 +02002001static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2002 struct page *page)
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002003{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002004 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2005 struct fuse_writepage_args *tmp;
2006 struct fuse_writepage_args *old_wpa;
2007 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002008
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002009 WARN_ON(new_ap->num_pages != 0);
Miklos Szeredic1460242020-07-14 14:45:41 +02002010 new_ap->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002011
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002012 spin_lock(&fi->lock);
Miklos Szeredic1460242020-07-14 14:45:41 +02002013 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002014 if (!old_wpa) {
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002015 spin_unlock(&fi->lock);
Miklos Szeredic1460242020-07-14 14:45:41 +02002016 return true;
Maxim Patlasovf6011082013-10-02 15:01:07 +04002017 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002018
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002019 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002020 pgoff_t curr_index;
2021
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002022 WARN_ON(tmp->inode != new_wpa->inode);
2023 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01002024 if (curr_index == page->index) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002025 WARN_ON(tmp->ia.ap.num_pages != 1);
2026 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002027 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002028 }
2029 }
2030
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002031 if (!tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002032 new_wpa->next = old_wpa->next;
2033 old_wpa->next = new_wpa;
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002034 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04002035
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002036 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002037
2038 if (tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002039 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002040
Tejun Heo93f78d82015-05-22 17:13:27 -04002041 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002042 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04002043 wb_writeout_inc(&bdi->wb);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002044 fuse_writepage_free(new_wpa);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002045 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01002046
Miklos Szeredic1460242020-07-14 14:45:41 +02002047 return false;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002048}
2049
Miklos Szeredi6ddf3af2020-07-14 14:45:41 +02002050static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2051 struct fuse_args_pages *ap,
2052 struct fuse_fill_wb_data *data)
2053{
2054 WARN_ON(!ap->num_pages);
2055
2056 /*
2057 * Being under writeback is unlikely but possible. For example direct
2058 * read to an mmaped fuse file will set the page dirty twice; once when
2059 * the pages are faulted with get_user_pages(), and then after the read
2060 * completed.
2061 */
2062 if (fuse_page_is_writeback(data->inode, page->index))
2063 return true;
2064
2065 /* Reached max pages */
2066 if (ap->num_pages == fc->max_pages)
2067 return true;
2068
2069 /* Reached max write bytes */
2070 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2071 return true;
2072
2073 /* Discontinuity */
2074 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2075 return true;
2076
2077 /* Need to grow the pages array? If so, did the expansion fail? */
2078 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2079 return true;
2080
2081 return false;
2082}
2083
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002084static int fuse_writepages_fill(struct page *page,
2085 struct writeback_control *wbc, void *_data)
2086{
2087 struct fuse_fill_wb_data *data = _data;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002088 struct fuse_writepage_args *wpa = data->wpa;
2089 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002090 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002091 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002092 struct fuse_conn *fc = get_fuse_conn(inode);
2093 struct page *tmp_page;
2094 int err;
2095
2096 if (!data->ff) {
2097 err = -EIO;
Vasily Averin091d1a72019-08-19 08:48:26 +03002098 data->ff = fuse_write_file_get(fc, fi);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002099 if (!data->ff)
2100 goto out_unlock;
2101 }
2102
Miklos Szeredi6ddf3af2020-07-14 14:45:41 +02002103 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002104 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002105 data->wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002106 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02002107
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002108 err = -ENOMEM;
2109 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2110 if (!tmp_page)
2111 goto out_unlock;
2112
2113 /*
2114 * The page must not be redirtied until the writeout is completed
2115 * (i.e. userspace has sent a reply to the write request). Otherwise
2116 * there could be more than one temporary page instance for each real
2117 * page.
2118 *
2119 * This is ensured by holding the page lock in page_mkwrite() while
2120 * checking fuse_page_is_writeback(). We already hold the page lock
2121 * since clear_page_dirty_for_io() and keep it held until we add the
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002122 * request to the fi->writepages list and increment ap->num_pages.
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002123 * After this fuse_page_is_writeback() will indicate that the page is
2124 * under writeback, so we can release the page lock.
2125 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002126 if (data->wpa == NULL) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002127 err = -ENOMEM;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002128 wpa = fuse_writepage_args_alloc();
2129 if (!wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002130 __free_page(tmp_page);
2131 goto out_unlock;
2132 }
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002133 data->max_pages = 1;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002134
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002135 ap = &wpa->ia.ap;
2136 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2137 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2138 wpa->next = NULL;
2139 ap->args.in_pages = true;
2140 ap->args.end = fuse_writepage_end;
2141 ap->num_pages = 0;
2142 wpa->inode = inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002143 }
2144 set_page_writeback(page);
2145
2146 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002147 ap->pages[ap->num_pages] = tmp_page;
2148 ap->descs[ap->num_pages].offset = 0;
2149 ap->descs[ap->num_pages].length = PAGE_SIZE;
Miklos Szeredic1460242020-07-14 14:45:41 +02002150 data->orig_pages[ap->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002151
Tejun Heo93f78d82015-05-22 17:13:27 -04002152 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07002153 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002154
2155 err = 0;
Miklos Szeredic1460242020-07-14 14:45:41 +02002156 if (data->wpa) {
2157 /*
2158 * Protected by fi->lock against concurrent access by
2159 * fuse_page_is_writeback().
2160 */
2161 spin_lock(&fi->lock);
2162 ap->num_pages++;
2163 spin_unlock(&fi->lock);
2164 } else if (fuse_writepage_add(wpa, page)) {
2165 data->wpa = wpa;
2166 } else {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002167 end_page_writeback(page);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002168 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002169out_unlock:
2170 unlock_page(page);
2171
2172 return err;
2173}
2174
2175static int fuse_writepages(struct address_space *mapping,
2176 struct writeback_control *wbc)
2177{
2178 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002179 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002180 struct fuse_fill_wb_data data;
2181 int err;
2182
2183 err = -EIO;
2184 if (is_bad_inode(inode))
2185 goto out;
2186
2187 data.inode = inode;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002188 data.wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002189 data.ff = NULL;
2190
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002191 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002192 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02002193 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002194 GFP_NOFS);
2195 if (!data.orig_pages)
2196 goto out;
2197
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002198 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002199 if (data.wpa) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002200 WARN_ON(!data.wpa->ia.ap.num_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002201 fuse_writepages_send(&data);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002202 }
2203 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002204 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002205
2206 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002207out:
2208 return err;
2209}
2210
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002211/*
2212 * It's worthy to make sure that space is reserved on disk for the write,
2213 * but how to implement it without killing performance need more thinking.
2214 */
2215static int fuse_write_begin(struct file *file, struct address_space *mapping,
2216 loff_t pos, unsigned len, unsigned flags,
2217 struct page **pagep, void **fsdata)
2218{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002219 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002220 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002221 struct page *page;
2222 loff_t fsize;
2223 int err = -ENOMEM;
2224
2225 WARN_ON(!fc->writeback_cache);
2226
2227 page = grab_cache_page_write_begin(mapping, index, flags);
2228 if (!page)
2229 goto error;
2230
2231 fuse_wait_on_page_writeback(mapping->host, page->index);
2232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002233 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002234 goto success;
2235 /*
2236 * Check if the start this page comes after the end of file, in which
2237 * case the readpage can be optimized away.
2238 */
2239 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002240 if (fsize <= (pos & PAGE_MASK)) {
2241 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002242 if (off)
2243 zero_user_segment(page, 0, off);
2244 goto success;
2245 }
2246 err = fuse_do_readpage(file, page);
2247 if (err)
2248 goto cleanup;
2249success:
2250 *pagep = page;
2251 return 0;
2252
2253cleanup:
2254 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002255 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002256error:
2257 return err;
2258}
2259
2260static int fuse_write_end(struct file *file, struct address_space *mapping,
2261 loff_t pos, unsigned len, unsigned copied,
2262 struct page *page, void *fsdata)
2263{
2264 struct inode *inode = page->mapping->host;
2265
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002266 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2267 if (!copied)
2268 goto unlock;
2269
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002270 if (!PageUptodate(page)) {
2271 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002272 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002273 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002274 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002275 SetPageUptodate(page);
2276 }
2277
2278 fuse_write_update_size(inode, pos + copied);
2279 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002280
2281unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002282 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002283 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002284
2285 return copied;
2286}
2287
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002288static int fuse_launder_page(struct page *page)
2289{
2290 int err = 0;
2291 if (clear_page_dirty_for_io(page)) {
2292 struct inode *inode = page->mapping->host;
Miklos Szeredi3993382b2020-11-11 17:22:31 +01002293
2294 /* Serialize with pending writeback for the same page */
2295 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002296 err = fuse_writepage_locked(page);
2297 if (!err)
2298 fuse_wait_on_page_writeback(inode, page->index);
2299 }
2300 return err;
2301}
2302
2303/*
2304 * Write back dirty pages now, because there may not be any suitable
2305 * open files later
2306 */
2307static void fuse_vma_close(struct vm_area_struct *vma)
2308{
2309 filemap_write_and_wait(vma->vm_file->f_mapping);
2310}
2311
2312/*
2313 * Wait for writeback against this page to complete before allowing it
2314 * to be marked dirty again, and hence written back again, possibly
2315 * before the previous writepage completed.
2316 *
2317 * Block here, instead of in ->writepage(), so that the userspace fs
2318 * can only block processes actually operating on the filesystem.
2319 *
2320 * Otherwise unprivileged userspace fs would be able to block
2321 * unrelated:
2322 *
2323 * - page migration
2324 * - sync(2)
2325 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2326 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302327static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002328{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002329 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002330 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002331
Dave Jiang11bac802017-02-24 14:56:41 -08002332 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002333 lock_page(page);
2334 if (page->mapping != inode->i_mapping) {
2335 unlock_page(page);
2336 return VM_FAULT_NOPAGE;
2337 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002338
2339 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002340 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002341}
2342
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002343static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002344 .close = fuse_vma_close,
2345 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002346 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002347 .page_mkwrite = fuse_page_mkwrite,
2348};
2349
2350static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2351{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002352 struct fuse_file *ff = file->private_data;
2353
Stefan Hajnoczi2a9a6092020-08-19 18:19:52 -04002354 /* DAX mmap is superior to direct_io mmap */
2355 if (FUSE_IS_DAX(file_inode(file)))
2356 return fuse_dax_mmap(file, vma);
2357
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002358 if (ff->open_flags & FOPEN_DIRECT_IO) {
2359 /* Can't provide the coherency needed for MAP_SHARED */
2360 if (vma->vm_flags & VM_MAYSHARE)
2361 return -ENODEV;
2362
2363 invalidate_inode_pages2(file->f_mapping);
2364
2365 return generic_file_mmap(file, vma);
2366 }
2367
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002368 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2369 fuse_link_write_file(file);
2370
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002371 file_accessed(file);
2372 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002373 return 0;
2374}
2375
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002376static int convert_fuse_file_lock(struct fuse_conn *fc,
2377 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002378 struct file_lock *fl)
2379{
2380 switch (ffl->type) {
2381 case F_UNLCK:
2382 break;
2383
2384 case F_RDLCK:
2385 case F_WRLCK:
2386 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2387 ffl->end < ffl->start)
2388 return -EIO;
2389
2390 fl->fl_start = ffl->start;
2391 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002392
2393 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002394 * Convert pid into init's pid namespace. The locks API will
2395 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002396 */
2397 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002398 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002399 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002400 break;
2401
2402 default:
2403 return -EIO;
2404 }
2405 fl->fl_type = ffl->type;
2406 return 0;
2407}
2408
Miklos Szeredi70781872014-12-12 09:49:05 +01002409static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002410 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002411 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002412{
Al Viro6131ffa2013-02-27 16:59:05 -05002413 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002414 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002415 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002416
Miklos Szeredi70781872014-12-12 09:49:05 +01002417 memset(inarg, 0, sizeof(*inarg));
2418 inarg->fh = ff->fh;
2419 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2420 inarg->lk.start = fl->fl_start;
2421 inarg->lk.end = fl->fl_end;
2422 inarg->lk.type = fl->fl_type;
2423 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002424 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002425 inarg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002426 args->opcode = opcode;
2427 args->nodeid = get_node_id(inode);
2428 args->in_numargs = 1;
2429 args->in_args[0].size = sizeof(*inarg);
2430 args->in_args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002431}
2432
2433static int fuse_getlk(struct file *file, struct file_lock *fl)
2434{
Al Viro6131ffa2013-02-27 16:59:05 -05002435 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +02002436 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002437 FUSE_ARGS(args);
2438 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002439 struct fuse_lk_out outarg;
2440 int err;
2441
Miklos Szeredi70781872014-12-12 09:49:05 +01002442 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
Miklos Szeredid5b48542019-09-10 15:04:08 +02002443 args.out_numargs = 1;
2444 args.out_args[0].size = sizeof(outarg);
2445 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002446 err = fuse_simple_request(fm, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002447 if (!err)
Max Reitzfcee2162020-05-06 17:44:12 +02002448 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002449
2450 return err;
2451}
2452
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002453static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002454{
Al Viro6131ffa2013-02-27 16:59:05 -05002455 struct inode *inode = file_inode(file);
Max Reitzfcee2162020-05-06 17:44:12 +02002456 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002457 FUSE_ARGS(args);
2458 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002459 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002460 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
Max Reitzfcee2162020-05-06 17:44:12 +02002461 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002462 int err;
2463
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002464 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002465 /* NLM needs asynchronous locks, which we don't support yet */
2466 return -ENOLCK;
2467 }
2468
Miklos Szeredi71421252006-06-25 05:48:52 -07002469 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002470 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002471 return 0;
2472
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002473 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Max Reitzfcee2162020-05-06 17:44:12 +02002474 err = fuse_simple_request(fm, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002475
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002476 /* locking is restartable */
2477 if (err == -EINTR)
2478 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002479
Miklos Szeredi71421252006-06-25 05:48:52 -07002480 return err;
2481}
2482
2483static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2484{
Al Viro6131ffa2013-02-27 16:59:05 -05002485 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002486 struct fuse_conn *fc = get_fuse_conn(inode);
2487 int err;
2488
Miklos Szeredi48e90762008-07-25 01:49:02 -07002489 if (cmd == F_CANCELLK) {
2490 err = 0;
2491 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002492 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002493 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002494 err = 0;
2495 } else
2496 err = fuse_getlk(file, fl);
2497 } else {
2498 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002499 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002500 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002501 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002502 }
2503 return err;
2504}
2505
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002506static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2507{
Al Viro6131ffa2013-02-27 16:59:05 -05002508 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002509 struct fuse_conn *fc = get_fuse_conn(inode);
2510 int err;
2511
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002512 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002513 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002514 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002515 struct fuse_file *ff = file->private_data;
2516
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002517 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002518 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002519 err = fuse_setlk(file, fl, 1);
2520 }
2521
2522 return err;
2523}
2524
Miklos Szeredib2d22722006-12-06 20:35:51 -08002525static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2526{
2527 struct inode *inode = mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +02002528 struct fuse_mount *fm = get_fuse_mount(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002529 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002530 struct fuse_bmap_in inarg;
2531 struct fuse_bmap_out outarg;
2532 int err;
2533
Max Reitzfcee2162020-05-06 17:44:12 +02002534 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
Miklos Szeredib2d22722006-12-06 20:35:51 -08002535 return 0;
2536
Miklos Szeredib2d22722006-12-06 20:35:51 -08002537 memset(&inarg, 0, sizeof(inarg));
2538 inarg.block = block;
2539 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002540 args.opcode = FUSE_BMAP;
2541 args.nodeid = get_node_id(inode);
2542 args.in_numargs = 1;
2543 args.in_args[0].size = sizeof(inarg);
2544 args.in_args[0].value = &inarg;
2545 args.out_numargs = 1;
2546 args.out_args[0].size = sizeof(outarg);
2547 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002548 err = fuse_simple_request(fm, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002549 if (err == -ENOSYS)
Max Reitzfcee2162020-05-06 17:44:12 +02002550 fm->fc->no_bmap = 1;
Miklos Szeredib2d22722006-12-06 20:35:51 -08002551
2552 return err ? 0 : outarg.block;
2553}
2554
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302555static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2556{
2557 struct inode *inode = file->f_mapping->host;
Max Reitzfcee2162020-05-06 17:44:12 +02002558 struct fuse_mount *fm = get_fuse_mount(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302559 struct fuse_file *ff = file->private_data;
2560 FUSE_ARGS(args);
2561 struct fuse_lseek_in inarg = {
2562 .fh = ff->fh,
2563 .offset = offset,
2564 .whence = whence
2565 };
2566 struct fuse_lseek_out outarg;
2567 int err;
2568
Max Reitzfcee2162020-05-06 17:44:12 +02002569 if (fm->fc->no_lseek)
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302570 goto fallback;
2571
Miklos Szeredid5b48542019-09-10 15:04:08 +02002572 args.opcode = FUSE_LSEEK;
2573 args.nodeid = ff->nodeid;
2574 args.in_numargs = 1;
2575 args.in_args[0].size = sizeof(inarg);
2576 args.in_args[0].value = &inarg;
2577 args.out_numargs = 1;
2578 args.out_args[0].size = sizeof(outarg);
2579 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02002580 err = fuse_simple_request(fm, &args);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302581 if (err) {
2582 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02002583 fm->fc->no_lseek = 1;
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302584 goto fallback;
2585 }
2586 return err;
2587 }
2588
2589 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2590
2591fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002592 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302593 if (!err)
2594 return generic_file_llseek(file, offset, whence);
2595 else
2596 return err;
2597}
2598
Andrew Morton965c8e52012-12-17 15:59:39 -08002599static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002600{
2601 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002602 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002603
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302604 switch (whence) {
2605 case SEEK_SET:
2606 case SEEK_CUR:
2607 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002608 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302609 break;
2610 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002611 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002612 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302613 if (!retval)
2614 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002615 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302616 break;
2617 case SEEK_HOLE:
2618 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002619 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302620 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002621 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302622 break;
2623 default:
2624 retval = -EINVAL;
2625 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002626
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002627 return retval;
2628}
2629
Tejun Heo59efec72008-11-26 12:03:55 +01002630/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002631 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2632 * ABI was defined to be 'struct iovec' which is different on 32bit
2633 * and 64bit. Fortunately we can determine which structure the server
2634 * used from the size of the reply.
2635 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002636static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2637 size_t transferred, unsigned count,
2638 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002639{
2640#ifdef CONFIG_COMPAT
2641 if (count * sizeof(struct compat_iovec) == transferred) {
2642 struct compat_iovec *ciov = src;
2643 unsigned i;
2644
2645 /*
2646 * With this interface a 32bit server cannot support
2647 * non-compat (i.e. ones coming from 64bit apps) ioctl
2648 * requests
2649 */
2650 if (!is_compat)
2651 return -EINVAL;
2652
2653 for (i = 0; i < count; i++) {
2654 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2655 dst[i].iov_len = ciov[i].iov_len;
2656 }
2657 return 0;
2658 }
2659#endif
2660
2661 if (count * sizeof(struct iovec) != transferred)
2662 return -EIO;
2663
2664 memcpy(dst, src, transferred);
2665 return 0;
2666}
2667
Miklos Szeredi75727772010-11-30 16:39:27 +01002668/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002669static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2670 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002671{
2672 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002673 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002674
Zach Brownfb6ccff2012-07-24 12:10:11 -07002675 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002676 if (iov->iov_len > (size_t) max)
2677 return -ENOMEM;
2678 max -= iov->iov_len;
2679 }
2680 return 0;
2681}
2682
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002683static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2684 void *src, size_t transferred, unsigned count,
2685 bool is_compat)
2686{
2687 unsigned i;
2688 struct fuse_ioctl_iovec *fiov = src;
2689
2690 if (fc->minor < 16) {
2691 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2692 count, is_compat);
2693 }
2694
2695 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2696 return -EIO;
2697
2698 for (i = 0; i < count; i++) {
2699 /* Did the server supply an inappropriate value? */
2700 if (fiov[i].base != (unsigned long) fiov[i].base ||
2701 fiov[i].len != (unsigned long) fiov[i].len)
2702 return -EIO;
2703
2704 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2705 dst[i].iov_len = (size_t) fiov[i].len;
2706
2707#ifdef CONFIG_COMPAT
2708 if (is_compat &&
2709 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2710 (compat_size_t) dst[i].iov_len != fiov[i].len))
2711 return -EIO;
2712#endif
2713 }
2714
2715 return 0;
2716}
2717
2718
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002719/*
Tejun Heo59efec72008-11-26 12:03:55 +01002720 * For ioctls, there is no generic way to determine how much memory
2721 * needs to be read and/or written. Furthermore, ioctls are allowed
2722 * to dereference the passed pointer, so the parameter requires deep
2723 * copying but FUSE has no idea whatsoever about what to copy in or
2724 * out.
2725 *
2726 * This is solved by allowing FUSE server to retry ioctl with
2727 * necessary in/out iovecs. Let's assume the ioctl implementation
2728 * needs to read in the following structure.
2729 *
2730 * struct a {
2731 * char *buf;
2732 * size_t buflen;
2733 * }
2734 *
2735 * On the first callout to FUSE server, inarg->in_size and
2736 * inarg->out_size will be NULL; then, the server completes the ioctl
2737 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2738 * the actual iov array to
2739 *
2740 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2741 *
2742 * which tells FUSE to copy in the requested area and retry the ioctl.
2743 * On the second round, the server has access to the structure and
2744 * from that it can tell what to look for next, so on the invocation,
2745 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2746 *
2747 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2748 * { .iov_base = a.buf, .iov_len = a.buflen } }
2749 *
2750 * FUSE will copy both struct a and the pointed buffer from the
2751 * process doing the ioctl and retry ioctl with both struct a and the
2752 * buffer.
2753 *
2754 * This time, FUSE server has everything it needs and completes ioctl
2755 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2756 *
2757 * Copying data out works the same way.
2758 *
2759 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2760 * automatically initializes in and out iovs by decoding @cmd with
2761 * _IOC_* macros and the server is not allowed to request RETRY. This
2762 * limits ioctl data transfers to well-formed ioctls and is the forced
2763 * behavior for all FUSE servers.
2764 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002765long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2766 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002767{
Tejun Heo59efec72008-11-26 12:03:55 +01002768 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02002769 struct fuse_mount *fm = ff->fm;
Tejun Heo59efec72008-11-26 12:03:55 +01002770 struct fuse_ioctl_in inarg = {
2771 .fh = ff->fh,
2772 .cmd = cmd,
2773 .arg = arg,
2774 .flags = flags
2775 };
2776 struct fuse_ioctl_out outarg;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002777 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002778 struct iovec *in_iov = NULL, *out_iov = NULL;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002779 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2780 size_t in_size, out_size, c;
2781 ssize_t transferred;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002782 int err, i;
2783 struct iov_iter ii;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002784 struct fuse_args_pages ap = {};
Tejun Heo59efec72008-11-26 12:03:55 +01002785
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002786#if BITS_PER_LONG == 32
2787 inarg.flags |= FUSE_IOCTL_32BIT;
2788#else
Ian Abbott6407f442019-04-24 15:14:11 +01002789 if (flags & FUSE_IOCTL_COMPAT) {
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002790 inarg.flags |= FUSE_IOCTL_32BIT;
Ian Abbott6407f442019-04-24 15:14:11 +01002791#ifdef CONFIG_X86_X32
2792 if (in_x32_syscall())
2793 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2794#endif
2795 }
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002796#endif
2797
Tejun Heo59efec72008-11-26 12:03:55 +01002798 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002799 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002800
Tejun Heo59efec72008-11-26 12:03:55 +01002801 err = -ENOMEM;
Max Reitzfcee2162020-05-06 17:44:12 +02002802 ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002803 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002804 if (!ap.pages || !iov_page)
Tejun Heo59efec72008-11-26 12:03:55 +01002805 goto out;
2806
Max Reitzfcee2162020-05-06 17:44:12 +02002807 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002808
Tejun Heo59efec72008-11-26 12:03:55 +01002809 /*
2810 * If restricted, initialize IO parameters as encoded in @cmd.
2811 * RETRY from server is not allowed.
2812 */
2813 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002814 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002815
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002816 iov->iov_base = (void __user *)arg;
Chirantan Ekbote31070f62020-07-14 19:26:39 +09002817
2818 switch (cmd) {
2819 case FS_IOC_GETFLAGS:
2820 case FS_IOC_SETFLAGS:
2821 iov->iov_len = sizeof(int);
2822 break;
2823 default:
2824 iov->iov_len = _IOC_SIZE(cmd);
2825 break;
2826 }
Tejun Heo59efec72008-11-26 12:03:55 +01002827
2828 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2829 in_iov = iov;
2830 in_iovs = 1;
2831 }
2832
2833 if (_IOC_DIR(cmd) & _IOC_READ) {
2834 out_iov = iov;
2835 out_iovs = 1;
2836 }
2837 }
2838
2839 retry:
2840 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2841 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2842
2843 /*
2844 * Out data can be used either for actual out data or iovs,
2845 * make sure there always is at least one page.
2846 */
2847 out_size = max_t(size_t, out_size, PAGE_SIZE);
2848 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2849
2850 /* make sure there are enough buffer pages and init request with them */
2851 err = -ENOMEM;
Max Reitzfcee2162020-05-06 17:44:12 +02002852 if (max_pages > fm->fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002853 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002854 while (ap.num_pages < max_pages) {
2855 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2856 if (!ap.pages[ap.num_pages])
Tejun Heo59efec72008-11-26 12:03:55 +01002857 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002858 ap.num_pages++;
Tejun Heo59efec72008-11-26 12:03:55 +01002859 }
2860
Tejun Heo59efec72008-11-26 12:03:55 +01002861
2862 /* okay, let's send it to the client */
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002863 ap.args.opcode = FUSE_IOCTL;
2864 ap.args.nodeid = ff->nodeid;
2865 ap.args.in_numargs = 1;
2866 ap.args.in_args[0].size = sizeof(inarg);
2867 ap.args.in_args[0].value = &inarg;
Tejun Heo59efec72008-11-26 12:03:55 +01002868 if (in_size) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002869 ap.args.in_numargs++;
2870 ap.args.in_args[1].size = in_size;
2871 ap.args.in_pages = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002872
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002873 err = -EFAULT;
2874 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002875 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2876 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002877 if (c != PAGE_SIZE && iov_iter_count(&ii))
2878 goto out;
2879 }
Tejun Heo59efec72008-11-26 12:03:55 +01002880 }
2881
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002882 ap.args.out_numargs = 2;
2883 ap.args.out_args[0].size = sizeof(outarg);
2884 ap.args.out_args[0].value = &outarg;
2885 ap.args.out_args[1].size = out_size;
2886 ap.args.out_pages = true;
2887 ap.args.out_argvar = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002888
Max Reitzfcee2162020-05-06 17:44:12 +02002889 transferred = fuse_simple_request(fm, &ap.args);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002890 err = transferred;
2891 if (transferred < 0)
Tejun Heo59efec72008-11-26 12:03:55 +01002892 goto out;
2893
2894 /* did it ask for retry? */
2895 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002896 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002897
2898 /* no retry if in restricted mode */
2899 err = -EIO;
2900 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2901 goto out;
2902
2903 in_iovs = outarg.in_iovs;
2904 out_iovs = outarg.out_iovs;
2905
2906 /*
2907 * Make sure things are in boundary, separate checks
2908 * are to protect against overflow.
2909 */
2910 err = -ENOMEM;
2911 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2912 out_iovs > FUSE_IOCTL_MAX_IOV ||
2913 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2914 goto out;
2915
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002916 vaddr = kmap_atomic(ap.pages[0]);
Max Reitzfcee2162020-05-06 17:44:12 +02002917 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002918 transferred, in_iovs + out_iovs,
2919 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002920 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002921 if (err)
2922 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002923
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002924 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002925 out_iov = in_iov + in_iovs;
2926
Max Reitzfcee2162020-05-06 17:44:12 +02002927 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002928 if (err)
2929 goto out;
2930
Max Reitzfcee2162020-05-06 17:44:12 +02002931 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002932 if (err)
2933 goto out;
2934
Tejun Heo59efec72008-11-26 12:03:55 +01002935 goto retry;
2936 }
2937
2938 err = -EIO;
2939 if (transferred > inarg.out_size)
2940 goto out;
2941
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002942 err = -EFAULT;
2943 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002944 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2945 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002946 if (c != PAGE_SIZE && iov_iter_count(&ii))
2947 goto out;
2948 }
2949 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002950 out:
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002951 free_page((unsigned long) iov_page);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002952 while (ap.num_pages)
2953 __free_page(ap.pages[--ap.num_pages]);
2954 kfree(ap.pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002955
2956 return err ? err : outarg.result;
2957}
Tejun Heo08cbf542009-04-14 10:54:53 +09002958EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002959
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002960long fuse_ioctl_common(struct file *file, unsigned int cmd,
2961 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002962{
Al Viro6131ffa2013-02-27 16:59:05 -05002963 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002964 struct fuse_conn *fc = get_fuse_conn(inode);
2965
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002966 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002967 return -EACCES;
2968
2969 if (is_bad_inode(inode))
2970 return -EIO;
2971
2972 return fuse_do_ioctl(file, cmd, arg, flags);
2973}
2974
Tejun Heo59efec72008-11-26 12:03:55 +01002975static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2976 unsigned long arg)
2977{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002978 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002979}
2980
2981static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2982 unsigned long arg)
2983{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002984 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002985}
2986
Tejun Heo95668a62008-11-26 12:03:55 +01002987/*
2988 * All files which have been polled are linked to RB tree
2989 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2990 * find the matching one.
2991 */
2992static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2993 struct rb_node **parent_out)
2994{
2995 struct rb_node **link = &fc->polled_files.rb_node;
2996 struct rb_node *last = NULL;
2997
2998 while (*link) {
2999 struct fuse_file *ff;
3000
3001 last = *link;
3002 ff = rb_entry(last, struct fuse_file, polled_node);
3003
3004 if (kh < ff->kh)
3005 link = &last->rb_left;
3006 else if (kh > ff->kh)
3007 link = &last->rb_right;
3008 else
3009 return link;
3010 }
3011
3012 if (parent_out)
3013 *parent_out = last;
3014 return link;
3015}
3016
3017/*
3018 * The file is about to be polled. Make sure it's on the polled_files
3019 * RB tree. Note that files once added to the polled_files tree are
3020 * not removed before the file is released. This is because a file
3021 * polled once is likely to be polled again.
3022 */
3023static void fuse_register_polled_file(struct fuse_conn *fc,
3024 struct fuse_file *ff)
3025{
3026 spin_lock(&fc->lock);
3027 if (RB_EMPTY_NODE(&ff->polled_node)) {
Kees Cook3f649ab2020-06-03 13:09:38 -07003028 struct rb_node **link, *parent;
Tejun Heo95668a62008-11-26 12:03:55 +01003029
3030 link = fuse_find_polled_node(fc, ff->kh, &parent);
3031 BUG_ON(*link);
3032 rb_link_node(&ff->polled_node, parent, link);
3033 rb_insert_color(&ff->polled_node, &fc->polled_files);
3034 }
3035 spin_unlock(&fc->lock);
3036}
3037
Al Viro076ccb72017-07-03 01:02:18 -04003038__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01003039{
Tejun Heo95668a62008-11-26 12:03:55 +01003040 struct fuse_file *ff = file->private_data;
Max Reitzfcee2162020-05-06 17:44:12 +02003041 struct fuse_mount *fm = ff->fm;
Tejun Heo95668a62008-11-26 12:03:55 +01003042 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
3043 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01003044 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01003045 int err;
3046
Max Reitzfcee2162020-05-06 17:44:12 +02003047 if (fm->fc->no_poll)
Tejun Heo95668a62008-11-26 12:03:55 +01003048 return DEFAULT_POLLMASK;
3049
3050 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05003051 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01003052
3053 /*
3054 * Ask for notification iff there's someone waiting for it.
3055 * The client may ignore the flag and always notify.
3056 */
3057 if (waitqueue_active(&ff->poll_wait)) {
3058 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
Max Reitzfcee2162020-05-06 17:44:12 +02003059 fuse_register_polled_file(fm->fc, ff);
Tejun Heo95668a62008-11-26 12:03:55 +01003060 }
3061
Miklos Szeredid5b48542019-09-10 15:04:08 +02003062 args.opcode = FUSE_POLL;
3063 args.nodeid = ff->nodeid;
3064 args.in_numargs = 1;
3065 args.in_args[0].size = sizeof(inarg);
3066 args.in_args[0].value = &inarg;
3067 args.out_numargs = 1;
3068 args.out_args[0].size = sizeof(outarg);
3069 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02003070 err = fuse_simple_request(fm, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01003071
3072 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05003073 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01003074 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02003075 fm->fc->no_poll = 1;
Tejun Heo95668a62008-11-26 12:03:55 +01003076 return DEFAULT_POLLMASK;
3077 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003078 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01003079}
Tejun Heo08cbf542009-04-14 10:54:53 +09003080EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01003081
3082/*
3083 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3084 * wakes up the poll waiters.
3085 */
3086int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3087 struct fuse_notify_poll_wakeup_out *outarg)
3088{
3089 u64 kh = outarg->kh;
3090 struct rb_node **link;
3091
3092 spin_lock(&fc->lock);
3093
3094 link = fuse_find_polled_node(fc, kh, NULL);
3095 if (*link) {
3096 struct fuse_file *ff;
3097
3098 ff = rb_entry(*link, struct fuse_file, polled_node);
3099 wake_up_interruptible_sync(&ff->poll_wait);
3100 }
3101
3102 spin_unlock(&fc->lock);
3103 return 0;
3104}
3105
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003106static void fuse_do_truncate(struct file *file)
3107{
3108 struct inode *inode = file->f_mapping->host;
3109 struct iattr attr;
3110
3111 attr.ia_valid = ATTR_SIZE;
3112 attr.ia_size = i_size_read(inode);
3113
3114 attr.ia_file = file;
3115 attr.ia_valid |= ATTR_FILE;
3116
Jan Kara62490332016-05-26 17:12:41 +02003117 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003118}
3119
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003120static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003121{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003122 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003123}
3124
Anand Avati4273b792012-02-17 12:46:25 -05003125static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003126fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05003127{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003128 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05003129 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003130 struct file *file = iocb->ki_filp;
3131 struct fuse_file *ff = file->private_data;
Anand Avati4273b792012-02-17 12:46:25 -05003132 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003133 struct inode *inode;
3134 loff_t i_size;
Al Viro933a3752020-09-17 17:26:56 -04003135 size_t count = iov_iter_count(iter), shortened = 0;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003136 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003137 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05003138
Anand Avati4273b792012-02-17 12:46:25 -05003139 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003140 inode = file->f_mapping->host;
3141 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05003142
Al Viro933a3752020-09-17 17:26:56 -04003143 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00003144 return 0;
3145
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003146 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003147 if (!io)
3148 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003149 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06003150 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003151 io->reqs = 1;
3152 io->bytes = -1;
3153 io->size = 0;
3154 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07003155 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003156 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003157 /*
3158 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003159 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003160 */
Linus Torvalds69456532020-10-19 14:28:30 -07003161 io->async = ff->fm->fc->async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003162 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303163 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003164
Al Viro933a3752020-09-17 17:26:56 -04003165 /* optimization for short read */
3166 if (io->async && !io->write && offset + count > i_size) {
Linus Torvalds69456532020-10-19 14:28:30 -07003167 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
Al Viro933a3752020-09-17 17:26:56 -04003168 shortened = count - iov_iter_count(iter);
3169 count -= shortened;
3170 }
3171
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003172 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303173 * We cannot asynchronously extend the size of a file.
3174 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003175 */
Al Viro933a3752020-09-17 17:26:56 -04003176 if ((offset + count > i_size) && io->write)
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303177 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05003178
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303179 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06003180 /*
3181 * Additional reference to keep io around after
3182 * calling fuse_aio_complete()
3183 */
3184 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003185 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06003186 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003187
Omar Sandoval6f673762015-03-16 04:33:52 -07003188 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04003189 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04003190 fuse_invalidate_attr(inode);
3191 } else {
Al Virod22a9432014-03-16 15:50:47 -04003192 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04003193 }
Al Viro933a3752020-09-17 17:26:56 -04003194 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003195
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003196 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01003197 bool blocking = io->blocking;
3198
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003199 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3200
3201 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01003202 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003203 return -EIOCBQUEUED;
3204
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003205 wait_for_completion(&wait);
3206 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003207 }
3208
Seth Forshee744742d2016-03-11 10:35:34 -06003209 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003210
Omar Sandoval6f673762015-03-16 04:33:52 -07003211 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003212 if (ret > 0)
3213 fuse_write_update_size(inode, pos);
3214 else if (ret < 0 && offset + count > i_size)
3215 fuse_do_truncate(file);
3216 }
Anand Avati4273b792012-02-17 12:46:25 -05003217
3218 return ret;
3219}
3220
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003221static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3222{
3223 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3224
3225 if (!err)
3226 fuse_sync_writes(inode);
3227
3228 return err;
3229}
3230
Miklos Szeredicdadb112012-11-10 16:55:56 +01003231static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3232 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003233{
3234 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003235 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003236 struct fuse_inode *fi = get_fuse_inode(inode);
Max Reitzfcee2162020-05-06 17:44:12 +02003237 struct fuse_mount *fm = ff->fm;
Miklos Szeredi70781872014-12-12 09:49:05 +01003238 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003239 struct fuse_fallocate_in inarg = {
3240 .fh = ff->fh,
3241 .offset = offset,
3242 .length = length,
3243 .mode = mode
3244 };
3245 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003246 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3247 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003248
Vivek Goyal6ae330c2020-08-19 18:19:54 -04003249 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
3250
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003251 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3252 return -EOPNOTSUPP;
3253
Max Reitzfcee2162020-05-06 17:44:12 +02003254 if (fm->fc->no_fallocate)
Miklos Szeredi519c6042012-04-26 10:56:36 +02003255 return -EOPNOTSUPP;
3256
Maxim Patlasov14c14412013-06-13 12:16:39 +04003257 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003258 inode_lock(inode);
Vivek Goyal6ae330c2020-08-19 18:19:54 -04003259 if (block_faults) {
3260 down_write(&fi->i_mmap_sem);
3261 err = fuse_dax_break_layouts(inode, 0, 0);
3262 if (err)
3263 goto out;
3264 }
3265
Maxim Patlasovbde52782013-09-13 19:19:54 +04003266 if (mode & FALLOC_FL_PUNCH_HOLE) {
3267 loff_t endbyte = offset + length - 1;
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003268
3269 err = fuse_writeback_range(inode, offset, endbyte);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003270 if (err)
3271 goto out;
Maxim Patlasovbde52782013-09-13 19:19:54 +04003272 }
Brian Foster3634a632013-05-17 09:30:32 -04003273 }
3274
Liu Bo0cbade02019-04-18 04:04:41 +08003275 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3276 offset + length > i_size_read(inode)) {
3277 err = inode_newsize_ok(inode, offset + length);
3278 if (err)
Miklos Szeredi35d6fcb2019-05-27 11:42:07 +02003279 goto out;
Liu Bo0cbade02019-04-18 04:04:41 +08003280 }
3281
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003282 if (!(mode & FALLOC_FL_KEEP_SIZE))
3283 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3284
Miklos Szeredid5b48542019-09-10 15:04:08 +02003285 args.opcode = FUSE_FALLOCATE;
3286 args.nodeid = ff->nodeid;
3287 args.in_numargs = 1;
3288 args.in_args[0].size = sizeof(inarg);
3289 args.in_args[0].value = &inarg;
Max Reitzfcee2162020-05-06 17:44:12 +02003290 err = fuse_simple_request(fm, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003291 if (err == -ENOSYS) {
Max Reitzfcee2162020-05-06 17:44:12 +02003292 fm->fc->no_fallocate = 1;
Miklos Szeredi519c6042012-04-26 10:56:36 +02003293 err = -EOPNOTSUPP;
3294 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003295 if (err)
3296 goto out;
3297
3298 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003299 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3300 bool changed = fuse_write_update_size(inode, offset + length);
3301
Max Reitzfcee2162020-05-06 17:44:12 +02003302 if (changed && fm->fc->writeback_cache)
Miklos Szeredi93d22692014-04-28 14:19:22 +02003303 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003304 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003305
3306 if (mode & FALLOC_FL_PUNCH_HOLE)
3307 truncate_pagecache_range(inode, offset, offset + length - 1);
3308
3309 fuse_invalidate_attr(inode);
3310
Brian Foster3634a632013-05-17 09:30:32 -04003311out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003312 if (!(mode & FALLOC_FL_KEEP_SIZE))
3313 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3314
Vivek Goyal6ae330c2020-08-19 18:19:54 -04003315 if (block_faults)
3316 up_write(&fi->i_mmap_sem);
3317
Maxim Patlasovbde52782013-09-13 19:19:54 +04003318 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003319 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003320
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003321 return err;
3322}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003323
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003324static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3325 struct file *file_out, loff_t pos_out,
3326 size_t len, unsigned int flags)
Niels de Vos88bc7d52018-08-21 14:36:31 +02003327{
3328 struct fuse_file *ff_in = file_in->private_data;
3329 struct fuse_file *ff_out = file_out->private_data;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003330 struct inode *inode_in = file_inode(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003331 struct inode *inode_out = file_inode(file_out);
3332 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
Max Reitzfcee2162020-05-06 17:44:12 +02003333 struct fuse_mount *fm = ff_in->fm;
3334 struct fuse_conn *fc = fm->fc;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003335 FUSE_ARGS(args);
3336 struct fuse_copy_file_range_in inarg = {
3337 .fh_in = ff_in->fh,
3338 .off_in = pos_in,
3339 .nodeid_out = ff_out->nodeid,
3340 .fh_out = ff_out->fh,
3341 .off_out = pos_out,
3342 .len = len,
3343 .flags = flags
3344 };
3345 struct fuse_write_out outarg;
3346 ssize_t err;
3347 /* mark unstable when write-back is not used, and file_out gets
3348 * extended */
3349 bool is_unstable = (!fc->writeback_cache) &&
3350 ((pos_out + len) > inode_out->i_size);
3351
3352 if (fc->no_copy_file_range)
3353 return -EOPNOTSUPP;
3354
Amir Goldstein5dae2222019-06-05 08:04:50 -07003355 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3356 return -EXDEV;
3357
Miklos Szeredi2c4656d2020-05-20 11:39:35 +02003358 inode_lock(inode_in);
3359 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3360 inode_unlock(inode_in);
3361 if (err)
3362 return err;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003363
Niels de Vos88bc7d52018-08-21 14:36:31 +02003364 inode_lock(inode_out);
3365
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003366 err = file_modified(file_out);
3367 if (err)
3368 goto out;
3369
Miklos Szeredi9b464182020-05-20 11:39:35 +02003370 /*
3371 * Write out dirty pages in the destination file before sending the COPY
3372 * request to userspace. After the request is completed, truncate off
3373 * pages (including partial ones) from the cache that have been copied,
3374 * since these contain stale data at that point.
3375 *
3376 * This should be mostly correct, but if the COPY writes to partial
3377 * pages (at the start or end) and the parts not covered by the COPY are
3378 * written through a memory map after calling fuse_writeback_range(),
3379 * then these partial page modifications will be lost on truncation.
3380 *
3381 * It is unlikely that someone would rely on such mixed style
3382 * modifications. Yet this does give less guarantees than if the
3383 * copying was performed with write(2).
3384 *
3385 * To fix this a i_mmap_sem style lock could be used to prevent new
3386 * faults while the copy is ongoing.
3387 */
Miklos Szeredi2c4656d2020-05-20 11:39:35 +02003388 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3389 if (err)
3390 goto out;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003391
3392 if (is_unstable)
3393 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3394
Miklos Szeredid5b48542019-09-10 15:04:08 +02003395 args.opcode = FUSE_COPY_FILE_RANGE;
3396 args.nodeid = ff_in->nodeid;
3397 args.in_numargs = 1;
3398 args.in_args[0].size = sizeof(inarg);
3399 args.in_args[0].value = &inarg;
3400 args.out_numargs = 1;
3401 args.out_args[0].size = sizeof(outarg);
3402 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02003403 err = fuse_simple_request(fm, &args);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003404 if (err == -ENOSYS) {
3405 fc->no_copy_file_range = 1;
3406 err = -EOPNOTSUPP;
3407 }
3408 if (err)
3409 goto out;
3410
Miklos Szeredi9b464182020-05-20 11:39:35 +02003411 truncate_inode_pages_range(inode_out->i_mapping,
3412 ALIGN_DOWN(pos_out, PAGE_SIZE),
3413 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3414
Niels de Vos88bc7d52018-08-21 14:36:31 +02003415 if (fc->writeback_cache) {
3416 fuse_write_update_size(inode_out, pos_out + outarg.size);
3417 file_update_time(file_out);
3418 }
3419
3420 fuse_invalidate_attr(inode_out);
3421
3422 err = outarg.size;
3423out:
3424 if (is_unstable)
3425 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3426
3427 inode_unlock(inode_out);
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003428 file_accessed(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003429
3430 return err;
3431}
3432
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003433static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3434 struct file *dst_file, loff_t dst_off,
3435 size_t len, unsigned int flags)
3436{
3437 ssize_t ret;
3438
3439 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3440 len, flags);
3441
Amir Goldstein5dae2222019-06-05 08:04:50 -07003442 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003443 ret = generic_copy_file_range(src_file, src_off, dst_file,
3444 dst_off, len, flags);
3445 return ret;
3446}
3447
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003448static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003449 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003450 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003451 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003452 .mmap = fuse_file_mmap,
3453 .open = fuse_open,
3454 .flush = fuse_flush,
3455 .release = fuse_release,
3456 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003457 .lock = fuse_file_lock,
Stefan Hajnoczi2a9a6092020-08-19 18:19:52 -04003458 .get_unmapped_area = thp_get_unmapped_area,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003459 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003460 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003461 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003462 .unlocked_ioctl = fuse_file_ioctl,
3463 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003464 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003465 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003466 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003467};
3468
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003469static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003470 .readpage = fuse_readpage,
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -07003471 .readahead = fuse_readahead,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003472 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003473 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003474 .launder_page = fuse_launder_page,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003475 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003476 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003477 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003478 .write_begin = fuse_write_begin,
3479 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003480};
3481
3482void fuse_init_file_inode(struct inode *inode)
3483{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003484 struct fuse_inode *fi = get_fuse_inode(inode);
3485
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003486 inode->i_fop = &fuse_file_operations;
3487 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003488
3489 INIT_LIST_HEAD(&fi->write_files);
3490 INIT_LIST_HEAD(&fi->queued_writes);
3491 fi->writectr = 0;
3492 init_waitqueue_head(&fi->page_waitq);
Maxim Patlasov6b2fb792019-09-19 17:11:20 +03003493 fi->writepages = RB_ROOT;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -04003494
3495 if (IS_ENABLED(CONFIG_FUSE_DAX))
3496 fuse_dax_inode_init(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003497}