blob: bac51c32d660263112182c7fdeb8c917b3c33455 [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>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021
Miklos Szeredi72133942019-09-10 15:04:11 +020022static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
Miklos Szeredi4c4f03f2019-09-10 15:04:09 +020024{
25 struct page **pages;
26
27 pages = kzalloc(npages * (sizeof(struct page *) +
28 sizeof(struct fuse_page_desc)), flags);
29 *desc = (void *) (pages + npages);
30
31 return pages;
32}
33
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020034static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070036{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070037 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010038 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080039
40 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070041 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 if (!fc->atomic_o_trunc)
43 inarg.flags &= ~O_TRUNC;
Miklos Szeredid5b48542019-09-10 15:04:08 +020044 args.opcode = opcode;
45 args.nodeid = nodeid;
46 args.in_numargs = 1;
47 args.in_args[0].size = sizeof(inarg);
48 args.in_args[0].value = &inarg;
49 args.out_numargs = 1;
50 args.out_args[0].size = sizeof(*outargp);
51 args.out_args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052
Miklos Szeredi70781872014-12-12 09:49:05 +010053 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054}
55
Miklos Szeredi4cb54862019-09-10 15:04:10 +020056struct fuse_release_args {
57 struct fuse_args args;
58 struct fuse_release_in inarg;
59 struct inode *inode;
60};
61
Tejun Heoacf99432008-11-26 12:03:55 +010062struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080063{
64 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090065
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070066 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
Tejun Heo6b2db282009-04-14 10:54:49 +090067 if (unlikely(!ff))
68 return NULL;
69
Miklos Szeredida5e4712009-04-28 16:56:36 +020070 ff->fc = fc;
Khazhismel Kumykovdc69e982019-09-17 12:35:33 -070071 ff->release_args = kzalloc(sizeof(*ff->release_args),
72 GFP_KERNEL_ACCOUNT);
Miklos Szeredi4cb54862019-09-10 15:04:10 +020073 if (!ff->release_args) {
Tejun Heo6b2db282009-04-14 10:54:49 +090074 kfree(ff);
75 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080076 }
Tejun Heo6b2db282009-04-14 10:54:49 +090077
78 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020079 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020080 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090081 RB_CLEAR_NODE(&ff->polled_node);
82 init_waitqueue_head(&ff->poll_wait);
83
Miklos Szeredi75126f52019-01-24 10:40:17 +010084 ff->kh = atomic64_inc_return(&fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090085
Miklos Szeredifd72faa2005-11-07 00:59:51 -080086 return ff;
87}
88
89void fuse_file_free(struct fuse_file *ff)
90{
Miklos Szeredi4cb54862019-09-10 15:04:10 +020091 kfree(ff->release_args);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020092 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080093 kfree(ff);
94}
95
Miklos Szeredi267d8442017-02-22 20:08:25 +010096static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070097{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020098 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070099 return ff;
100}
101
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200102static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
103 int error)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104{
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200105 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
106
107 iput(ra->inode);
108 kfree(ra);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100109}
110
Chad Austin2e64ff12018-12-10 10:54:52 -0800111static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700112{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200113 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200114 struct fuse_args *args = &ff->release_args->args;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200115
Chad Austind9a9ea92019-01-07 16:53:17 -0800116 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200117 /* Do nothing when client does not implement 'open' */
118 fuse_release_end(ff->fc, args, 0);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100119 } else if (sync) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200120 fuse_simple_request(ff->fc, args);
121 fuse_release_end(ff->fc, args, 0);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100122 } else {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200123 args->end = fuse_release_end;
124 if (fuse_simple_background(ff->fc, args,
125 GFP_KERNEL | __GFP_NOFAIL))
126 fuse_release_end(ff->fc, args, -ENOTCONN);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100127 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700128 kfree(ff);
129 }
130}
131
Tejun Heo08cbf542009-04-14 10:54:53 +0900132int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
133 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200134{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200135 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200136 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
137
138 ff = fuse_file_alloc(fc);
139 if (!ff)
140 return -ENOMEM;
141
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100142 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800143 /* Default for no-open */
144 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800145 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100146 struct fuse_open_out outarg;
147 int err;
148
149 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
150 if (!err) {
151 ff->fh = outarg.fh;
152 ff->open_flags = outarg.open_flags;
153
Chad Austind9a9ea92019-01-07 16:53:17 -0800154 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100155 fuse_file_free(ff);
156 return err;
157 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800158 if (isdir)
159 fc->no_opendir = 1;
160 else
161 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200163 }
164
165 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100166 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200167
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200168 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100169 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200170
171 return 0;
172}
Tejun Heo08cbf542009-04-14 10:54:53 +0900173EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200174
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400175static void fuse_link_write_file(struct file *file)
176{
177 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400178 struct fuse_inode *fi = get_fuse_inode(inode);
179 struct fuse_file *ff = file->private_data;
180 /*
181 * file may be written through mmap, so chain it onto the
182 * inodes's write_file list
183 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300184 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400185 if (list_empty(&ff->write_entry))
186 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300187 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400188}
189
Miklos Szeredic7b71432009-04-28 16:56:37 +0200190void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800191{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200192 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800193 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200194
Miklos Szeredic7b71432009-04-28 16:56:37 +0200195 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700196 invalidate_inode_pages2(inode->i_mapping);
Kirill Smelkovbbd84f32019-04-24 07:13:57 +0000197 if (ff->open_flags & FOPEN_STREAM)
198 stream_open(inode, file);
199 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200200 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800201 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 struct fuse_inode *fi = get_fuse_inode(inode);
203
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300204 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300205 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800206 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300207 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800208 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200209 if (fc->writeback_cache)
210 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800211 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400212 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
213 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800214}
215
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200216int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800217{
Tejun Heoacf99432008-11-26 12:03:55 +0100218 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219 int err;
Miklos Szeredie4648302019-10-23 14:26:37 +0200220 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200221 fc->atomic_o_trunc &&
222 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700223
224 err = generic_file_open(inode, file);
225 if (err)
226 return err;
227
Miklos Szeredie4648302019-10-23 14:26:37 +0200228 if (is_wb_truncate) {
Al Viro59551022016-01-22 15:40:57 -0500229 inode_lock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200230 fuse_set_nowrite(inode);
231 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200232
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200233 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200235 if (!err)
236 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200237
Miklos Szeredie4648302019-10-23 14:26:37 +0200238 if (is_wb_truncate) {
239 fuse_release_nowrite(inode);
Al Viro59551022016-01-22 15:40:57 -0500240 inode_unlock(inode);
Miklos Szeredie4648302019-10-23 14:26:37 +0200241 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200242
243 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244}
245
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300246static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
247 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800248{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200249 struct fuse_conn *fc = ff->fc;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200250 struct fuse_release_args *ra = ff->release_args;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700251
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300252 /* Inode is NULL on error path of fuse_create_open() */
253 if (likely(fi)) {
254 spin_lock(&fi->lock);
255 list_del(&ff->write_entry);
256 spin_unlock(&fi->lock);
257 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200258 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200259 if (!RB_EMPTY_NODE(&ff->polled_node))
260 rb_erase(&ff->polled_node, &fc->polled_files);
261 spin_unlock(&fc->lock);
262
Bryan Green357ccf22011-03-01 16:43:52 -0800263 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200264
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200265 ra->inarg.fh = ff->fh;
266 ra->inarg.flags = flags;
267 ra->args.in_numargs = 1;
268 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
269 ra->args.in_args[0].value = &ra->inarg;
270 ra->args.opcode = opcode;
271 ra->args.nodeid = ff->nodeid;
272 ra->args.force = true;
273 ra->args.nocreds = true;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800274}
275
Chad Austin2e64ff12018-12-10 10:54:52 -0800276void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800277{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300278 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100279 struct fuse_file *ff = file->private_data;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200280 struct fuse_release_args *ra = ff->release_args;
Chad Austin2e64ff12018-12-10 10:54:52 -0800281 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700282
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300283 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100284
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200285 if (ff->flock) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200286 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
287 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
288 (fl_owner_t) file);
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200289 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100290 /* Hold inode until release is finished */
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200291 ra->inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700292
Tejun Heo6b2db282009-04-14 10:54:49 +0900293 /*
294 * Normally this will send the RELEASE request, however if
295 * some asynchronous READ or WRITE requests are outstanding,
296 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100297 *
298 * Make the release synchronous if this is a fuseblk mount,
299 * synchronous RELEASE is allowed (and desirable) in this case
300 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900301 */
Miklos Szeredi1ccd1ea2019-09-10 15:04:09 +0200302 fuse_file_put(ff, ff->fc->destroy, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700303}
304
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700305static int fuse_open(struct inode *inode, struct file *file)
306{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200307 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700308}
309
310static int fuse_release(struct inode *inode, struct file *file)
311{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400312 struct fuse_conn *fc = get_fuse_conn(inode);
313
314 /* see fuse_vma_close() for !writeback_cache case */
315 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200316 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400317
Chad Austin2e64ff12018-12-10 10:54:52 -0800318 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200319
320 /* return value is ignored by VFS */
321 return 0;
322}
323
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300324void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200325{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200326 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300327 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100328 /*
329 * iput(NULL) is a no-op and since the refcount is 1 and everything's
330 * synchronous, we are fine with not doing igrab() here"
331 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800332 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700333}
Tejun Heo08cbf542009-04-14 10:54:53 +0900334EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700335
Miklos Szeredi71421252006-06-25 05:48:52 -0700336/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700337 * Scramble the ID space with XTEA, so that the value of the files_struct
338 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700339 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700340u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700341{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700342 u32 *k = fc->scramble_key;
343 u64 v = (unsigned long) id;
344 u32 v0 = v;
345 u32 v1 = v >> 32;
346 u32 sum = 0;
347 int i;
348
349 for (i = 0; i < 32; i++) {
350 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
351 sum += 0x9E3779B9;
352 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
353 }
354
355 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700356}
357
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200358struct fuse_writepage_args {
359 struct fuse_io_args ia;
360 struct list_head writepages_entry;
361 struct list_head queue_entry;
362 struct fuse_writepage_args *next;
363 struct inode *inode;
364};
365
366static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100367 pgoff_t idx_from, pgoff_t idx_to)
368{
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200369 struct fuse_writepage_args *wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100370
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200371 list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100372 pgoff_t curr_index;
373
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200374 WARN_ON(get_fuse_inode(wpa->inode) != fi);
375 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
376 if (idx_from < curr_index + wpa->ia.ap.num_pages &&
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100377 curr_index <= idx_to) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200378 return wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100379 }
380 }
381 return NULL;
382}
383
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700384/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400385 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700386 *
387 * This is currently done by walking the list of writepage requests
388 * for the inode, which can be pretty inefficient.
389 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400390static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
391 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700392{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700393 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100394 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700395
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300396 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100397 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300398 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700399
400 return found;
401}
402
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400403static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
404{
405 return fuse_range_is_writeback(inode, index, index);
406}
407
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700408/*
409 * Wait for page writeback to be completed.
410 *
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
413 */
Maxim Patlasov17b2cbe2019-07-22 10:17:17 +0300414static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700415{
416 struct fuse_inode *fi = get_fuse_inode(inode);
417
418 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700419}
420
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400421/*
422 * Wait for all pending writepages on the inode to finish.
423 *
424 * This is currently done by blocking further writes with FUSE_NOWRITE
425 * and waiting for all sent writes to complete.
426 *
427 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
428 * could conflict with truncation.
429 */
430static void fuse_sync_writes(struct inode *inode)
431{
432 fuse_set_nowrite(inode);
433 fuse_release_nowrite(inode);
434}
435
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700436static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700437{
Al Viro6131ffa2013-02-27 16:59:05 -0500438 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700439 struct fuse_conn *fc = get_fuse_conn(inode);
440 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700441 struct fuse_flush_in inarg;
Miklos Szeredic500eba2019-09-10 15:04:08 +0200442 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700443 int err;
444
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800445 if (is_bad_inode(inode))
446 return -EIO;
447
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 if (fc->no_flush)
449 return 0;
450
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200451 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400452 if (err)
453 return err;
454
Al Viro59551022016-01-22 15:40:57 -0500455 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400456 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500457 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400458
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200459 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700460 if (err)
461 return err;
462
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700463 memset(&inarg, 0, sizeof(inarg));
464 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700465 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200466 args.opcode = FUSE_FLUSH;
467 args.nodeid = get_node_id(inode);
468 args.in_numargs = 1;
469 args.in_args[0].size = sizeof(inarg);
470 args.in_args[0].value = &inarg;
471 args.force = true;
472
473 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700474 if (err == -ENOSYS) {
475 fc->no_flush = 1;
476 err = 0;
477 }
478 return err;
479}
480
Josef Bacik02c24a82011-07-16 20:44:56 -0400481int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100482 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200484 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700485 struct fuse_conn *fc = get_fuse_conn(inode);
486 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100487 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700488 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100489
490 memset(&inarg, 0, sizeof(inarg));
491 inarg.fh = ff->fh;
Alan Somers154603f2019-04-19 15:42:44 -0600492 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200493 args.opcode = opcode;
494 args.nodeid = get_node_id(inode);
495 args.in_numargs = 1;
496 args.in_args[0].size = sizeof(inarg);
497 args.in_args[0].value = &inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100498 return fuse_simple_request(fc, &args);
499}
500
501static int fuse_fsync(struct file *file, loff_t start, loff_t end,
502 int datasync)
503{
504 struct inode *inode = file->f_mapping->host;
505 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700506 int err;
507
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800508 if (is_bad_inode(inode))
509 return -EIO;
510
Al Viro59551022016-01-22 15:40:57 -0500511 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400512
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700513 /*
514 * Start writeback against all dirty pages of the inode, then
515 * wait for all outstanding writes, before sending the FSYNC
516 * request.
517 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400518 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700519 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400520 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700521
522 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700523
524 /*
525 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400526 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700527 * We have to do this directly after fuse_sync_writes()
528 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400529 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700530 if (err)
531 goto out;
532
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200533 err = sync_inode_metadata(inode, 1);
534 if (err)
535 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700536
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100537 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200538 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400539
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100540 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100542 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700543 err = 0;
544 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400545out:
Al Viro59551022016-01-22 15:40:57 -0500546 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700547
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100548 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700549}
550
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200551void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
552 size_t count, int opcode)
553{
554 struct fuse_file *ff = file->private_data;
555 struct fuse_args *args = &ia->ap.args;
556
557 ia->read.in.fh = ff->fh;
558 ia->read.in.offset = pos;
559 ia->read.in.size = count;
560 ia->read.in.flags = file->f_flags;
561 args->opcode = opcode;
562 args->nodeid = ff->nodeid;
563 args->in_numargs = 1;
564 args->in_args[0].size = sizeof(ia->read.in);
565 args->in_args[0].value = &ia->read.in;
566 args->out_argvar = true;
567 args->out_numargs = 1;
568 args->out_args[0].size = count;
569}
570
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200571static void fuse_release_user_pages(struct fuse_args_pages *ap,
572 bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400573{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200574 unsigned int i;
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400575
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200576 for (i = 0; i < ap->num_pages; i++) {
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200577 if (should_dirty)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200578 set_page_dirty_lock(ap->pages[i]);
579 put_page(ap->pages[i]);
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400580 }
581}
582
Seth Forshee744742d2016-03-11 10:35:34 -0600583static void fuse_io_release(struct kref *kref)
584{
585 kfree(container_of(kref, struct fuse_io_priv, refcnt));
586}
587
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100588static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
589{
590 if (io->err)
591 return io->err;
592
593 if (io->bytes >= 0 && io->write)
594 return -EIO;
595
596 return io->bytes < 0 ? io->size : io->bytes;
597}
598
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400599/**
600 * In case of short read, the caller sets 'pos' to the position of
601 * actual end of fuse request in IO request. Otherwise, if bytes_requested
602 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
603 *
604 * An example:
605 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
606 * both submitted asynchronously. The first of them was ACKed by userspace as
607 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
608 * second request was ACKed as short, e.g. only 1K was read, resulting in
609 * pos == 33K.
610 *
611 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
612 * will be equal to the length of the longest contiguous fragment of
613 * transferred data starting from the beginning of IO request.
614 */
615static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
616{
617 int left;
618
619 spin_lock(&io->lock);
620 if (err)
621 io->err = io->err ? : err;
622 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
623 io->bytes = pos;
624
625 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530626 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100627 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400628 spin_unlock(&io->lock);
629
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530630 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100631 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400632
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100633 if (res >= 0) {
634 struct inode *inode = file_inode(io->iocb->ki_filp);
635 struct fuse_conn *fc = get_fuse_conn(inode);
636 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400637
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300638 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300639 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300640 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400641 }
642
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100643 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400644 }
Seth Forshee744742d2016-03-11 10:35:34 -0600645
646 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400647}
648
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200649static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
650 unsigned int npages)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400651{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200652 struct fuse_io_args *ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400653
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200654 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
655 if (ia) {
656 ia->io = io;
657 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
658 &ia->ap.descs);
659 if (!ia->ap.pages) {
660 kfree(ia);
661 ia = NULL;
662 }
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400663 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200664 return ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400665}
666
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200667static void fuse_io_free(struct fuse_io_args *ia)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400668{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200669 kfree(ia->ap.pages);
670 kfree(ia);
671}
672
673static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
674 int err)
675{
676 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
677 struct fuse_io_priv *io = ia->io;
678 ssize_t pos = -1;
679
680 fuse_release_user_pages(&ia->ap, io->should_dirty);
681
682 if (err) {
683 /* Nothing */
684 } else if (io->write) {
685 if (ia->write.out.size > ia->write.in.size) {
686 err = -EIO;
687 } else if (ia->write.in.size != ia->write.out.size) {
688 pos = ia->write.in.offset - io->offset +
689 ia->write.out.size;
690 }
691 } else {
692 u32 outsize = args->out_args[0].size;
693
694 if (ia->read.in.size != outsize)
695 pos = ia->read.in.offset - io->offset + outsize;
696 }
697
698 fuse_aio_complete(io, err, pos);
699 fuse_io_free(ia);
700}
701
702static ssize_t fuse_async_req_send(struct fuse_conn *fc,
703 struct fuse_io_args *ia, size_t num_bytes)
704{
705 ssize_t err;
706 struct fuse_io_priv *io = ia->io;
707
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400708 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600709 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400710 io->size += num_bytes;
711 io->reqs++;
712 spin_unlock(&io->lock);
713
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200714 ia->ap.args.end = fuse_aio_complete_req;
715 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100716 if (err)
717 fuse_aio_complete_req(fc, &ia->ap.args, err);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400718
Miklos Szeredif1ebdef2019-11-25 20:48:46 +0100719 return num_bytes;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400720}
721
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200722static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
723 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700724{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200725 struct file *file = ia->io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200726 struct fuse_file *ff = file->private_data;
727 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700728
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200729 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700730 if (owner != NULL) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200731 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
732 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -0700733 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400734
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200735 if (ia->io->async)
736 return fuse_async_req_send(fc, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400737
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200738 return fuse_simple_request(fc, &ia->ap.args);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700739}
740
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700741static void fuse_read_update_size(struct inode *inode, loff_t size,
742 u64 attr_ver)
743{
744 struct fuse_conn *fc = get_fuse_conn(inode);
745 struct fuse_inode *fi = get_fuse_inode(inode);
746
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300747 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400748 if (attr_ver == fi->attr_version && size < inode->i_size &&
749 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300750 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700751 i_size_write(inode, size);
752 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300753 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754}
755
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200756static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200757 struct fuse_args_pages *ap)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400758{
Pavel Emelyanov83732002013-10-10 17:10:46 +0400759 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400760
Pavel Emelyanov83732002013-10-10 17:10:46 +0400761 if (fc->writeback_cache) {
762 /*
763 * A hole in a file. Some data after the hole are in page cache,
764 * but have not reached the client fs yet. So, the hole is not
765 * present there.
766 */
767 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300768 int start_idx = num_read >> PAGE_SHIFT;
769 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400770
Miklos Szeredi134831e2019-09-10 15:04:10 +0200771 for (i = start_idx; i < ap->num_pages; i++) {
772 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400773 off = 0;
774 }
775 } else {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200776 loff_t pos = page_offset(ap->pages[0]) + num_read;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400777 fuse_read_update_size(inode, pos, attr_ver);
778 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400779}
780
Maxim Patlasov482fce52013-10-10 17:11:25 +0400781static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700782{
783 struct inode *inode = page->mapping->host;
784 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700785 loff_t pos = page_offset(page);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200786 struct fuse_page_desc desc = { .length = PAGE_SIZE };
787 struct fuse_io_args ia = {
788 .ap.args.page_zeroing = true,
789 .ap.args.out_pages = true,
790 .ap.num_pages = 1,
791 .ap.pages = &page,
792 .ap.descs = &desc,
793 };
794 ssize_t res;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700795 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800796
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700797 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300798 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700799 * page-cache page, so make sure we read a properly synced
800 * page.
801 */
802 fuse_wait_on_page_writeback(inode, page->index);
803
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700804 attr_ver = fuse_get_attr_version(fc);
805
Miklos Szeredi2f139822020-02-06 16:39:28 +0100806 /* Don't overflow end offset */
807 if (pos + (desc.length - 1) == LLONG_MAX)
808 desc.length--;
809
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200810 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
811 res = fuse_simple_request(fc, &ia.ap.args);
812 if (res < 0)
813 return res;
814 /*
815 * Short read means EOF. If file size is larger, truncate it
816 */
817 if (res < desc.length)
Miklos Szeredi134831e2019-09-10 15:04:10 +0200818 fuse_short_read(inode, attr_ver, res, &ia.ap);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700819
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200820 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700821
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200822 return 0;
Maxim Patlasov482fce52013-10-10 17:11:25 +0400823}
824
825static int fuse_readpage(struct file *file, struct page *page)
826{
827 struct inode *inode = page->mapping->host;
828 int err;
829
830 err = -EIO;
831 if (is_bad_inode(inode))
832 goto out;
833
834 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800835 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700836 out:
837 unlock_page(page);
838 return err;
839}
840
Miklos Szeredi134831e2019-09-10 15:04:10 +0200841static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
842 int err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800844 int i;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200845 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
846 struct fuse_args_pages *ap = &ia->ap;
847 size_t count = ia->read.in.size;
848 size_t num_read = args->out_args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200849 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800850
Miklos Szeredi134831e2019-09-10 15:04:10 +0200851 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
852 mapping = ap->pages[i]->mapping;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200853
854 if (mapping) {
855 struct inode *inode = mapping->host;
856
857 /*
858 * Short read means EOF. If file size is larger, truncate it
859 */
Miklos Szeredi134831e2019-09-10 15:04:10 +0200860 if (!err && num_read < count)
861 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200862
Andrew Gallagher451418f2013-11-05 03:55:43 -0800863 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700864 }
865
Miklos Szeredi134831e2019-09-10 15:04:10 +0200866 for (i = 0; i < ap->num_pages; i++) {
867 struct page *page = ap->pages[i];
868
869 if (!err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800871 else
872 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700873 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300874 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200876 if (ia->ff)
877 fuse_file_put(ia->ff, false, false);
878
879 fuse_io_free(ia);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800880}
881
Miklos Szeredi134831e2019-09-10 15:04:10 +0200882static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800883{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200884 struct fuse_file *ff = file->private_data;
885 struct fuse_conn *fc = ff->fc;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200886 struct fuse_args_pages *ap = &ia->ap;
887 loff_t pos = page_offset(ap->pages[0]);
888 size_t count = ap->num_pages << PAGE_SHIFT;
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100889 ssize_t res;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200890 int err;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200891
Miklos Szeredi134831e2019-09-10 15:04:10 +0200892 ap->args.out_pages = true;
893 ap->args.page_zeroing = true;
894 ap->args.page_replace = true;
Miklos Szeredi2f139822020-02-06 16:39:28 +0100895
896 /* Don't overflow end offset */
897 if (pos + (count - 1) == LLONG_MAX) {
898 count--;
899 ap->descs[ap->num_pages - 1].length--;
900 }
901 WARN_ON((loff_t) (pos + count) < 0);
902
Miklos Szeredi134831e2019-09-10 15:04:10 +0200903 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
904 ia->read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800905 if (fc->async_read) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200906 ia->ff = fuse_file_get(ff);
907 ap->args.end = fuse_readpages_end;
908 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
909 if (!err)
910 return;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800911 } else {
Miklos Szeredi7df1e982020-01-16 11:09:36 +0100912 res = fuse_simple_request(fc, &ap->args);
913 err = res < 0 ? res : 0;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800914 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200915 fuse_readpages_end(fc, &ap->args, err);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916}
917
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700918static void fuse_readahead(struct readahead_control *rac)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700919{
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700920 struct inode *inode = rac->mapping->host;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700921 struct fuse_conn *fc = get_fuse_conn(inode);
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700922 unsigned int i, max_pages, nr_pages = 0;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700923
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800924 if (is_bad_inode(inode))
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700925 return;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800926
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700927 max_pages = min_t(unsigned int, fc->max_pages,
928 fc->max_read / PAGE_SIZE);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700929
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -0700930 for (;;) {
931 struct fuse_io_args *ia;
932 struct fuse_args_pages *ap;
933
934 nr_pages = readahead_count(rac) - nr_pages;
935 if (nr_pages > max_pages)
936 nr_pages = max_pages;
937 if (nr_pages == 0)
938 break;
939 ia = fuse_io_alloc(NULL, nr_pages);
940 if (!ia)
941 return;
942 ap = &ia->ap;
943 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
944 for (i = 0; i < nr_pages; i++) {
945 fuse_wait_on_page_writeback(inode,
946 readahead_index(rac) + i);
947 ap->descs[i].length = PAGE_SIZE;
948 }
949 ap->num_pages = nr_pages;
950 fuse_send_readpages(ia, rac->file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700951 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700952}
953
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100954static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800955{
956 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400957 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800958
Brian Fostera8894272012-07-16 15:23:50 -0400959 /*
960 * In auto invalidate mode, always update attributes on read.
961 * Otherwise, only update if we attempt to read past EOF (to ensure
962 * i_size is up to date).
963 */
964 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400965 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800966 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200967 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800968 if (err)
969 return err;
970 }
971
Al Viro37c20f12014-04-02 14:47:09 -0400972 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800973}
974
Miklos Szeredi338f2e32019-09-10 15:04:09 +0200975static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
976 loff_t pos, size_t count)
977{
978 struct fuse_args *args = &ia->ap.args;
979
980 ia->write.in.fh = ff->fh;
981 ia->write.in.offset = pos;
982 ia->write.in.size = count;
983 args->opcode = FUSE_WRITE;
984 args->nodeid = ff->nodeid;
985 args->in_numargs = 2;
986 if (ff->fc->minor < 9)
987 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
988 else
989 args->in_args[0].size = sizeof(ia->write.in);
990 args->in_args[0].value = &ia->write.in;
991 args->in_args[1].size = count;
992 args->out_numargs = 1;
993 args->out_args[0].size = sizeof(ia->write.out);
994 args->out_args[0].value = &ia->write.out;
995}
996
997static unsigned int fuse_write_flags(struct kiocb *iocb)
998{
999 unsigned int flags = iocb->ki_filp->f_flags;
1000
1001 if (iocb->ki_flags & IOCB_DSYNC)
1002 flags |= O_DSYNC;
1003 if (iocb->ki_flags & IOCB_SYNC)
1004 flags |= O_SYNC;
1005
1006 return flags;
1007}
1008
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001009static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1010 size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001011{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001012 struct kiocb *iocb = ia->io->iocb;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001013 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001014 struct fuse_file *ff = file->private_data;
1015 struct fuse_conn *fc = ff->fc;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001016 struct fuse_write_in *inarg = &ia->write.in;
1017 ssize_t err;
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001018
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001019 fuse_write_args_fill(ia, ff, pos, count);
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001020 inarg->flags = fuse_write_flags(iocb);
Miklos Szeredif3332112007-10-18 03:07:04 -07001021 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001022 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1023 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1024 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001025
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001026 if (ia->io->async)
1027 return fuse_async_req_send(fc, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001028
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001029 err = fuse_simple_request(fc, &ia->ap.args);
1030 if (!err && ia->write.out.size > count)
1031 err = -EIO;
1032
1033 return err ?: ia->write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001034}
1035
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001036bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001037{
1038 struct fuse_conn *fc = get_fuse_conn(inode);
1039 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001040 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001041
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001042 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001043 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001044 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001045 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001046 ret = true;
1047 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001048 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001049
1050 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001051}
1052
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001053static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1054 struct kiocb *iocb, struct inode *inode,
1055 loff_t pos, size_t count)
Nick Pigginea9b9902008-04-30 00:54:42 -07001056{
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001057 struct fuse_args_pages *ap = &ia->ap;
1058 struct file *file = iocb->ki_filp;
1059 struct fuse_file *ff = file->private_data;
1060 struct fuse_conn *fc = ff->fc;
1061 unsigned int offset, i;
1062 int err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001063
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001064 for (i = 0; i < ap->num_pages; i++)
1065 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Nick Pigginea9b9902008-04-30 00:54:42 -07001066
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001067 fuse_write_args_fill(ia, ff, pos, count);
1068 ia->write.in.flags = fuse_write_flags(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001069
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001070 err = fuse_simple_request(fc, &ap->args);
Miklos Szeredi8aab3362019-11-12 11:49:04 +01001071 if (!err && ia->write.out.size > count)
1072 err = -EIO;
Nick Pigginea9b9902008-04-30 00:54:42 -07001073
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001074 offset = ap->descs[0].offset;
1075 count = ia->write.out.size;
1076 for (i = 0; i < ap->num_pages; i++) {
1077 struct page *page = ap->pages[i];
1078
1079 if (!err && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001080 SetPageUptodate(page);
1081
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001082 if (count > PAGE_SIZE - offset)
1083 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 else
1085 count = 0;
1086 offset = 0;
1087
1088 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001089 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 }
1091
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001092 return err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001093}
1094
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001095static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1096 struct address_space *mapping,
1097 struct iov_iter *ii, loff_t pos,
1098 unsigned int max_pages)
Nick Pigginea9b9902008-04-30 00:54:42 -07001099{
1100 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001101 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 size_t count = 0;
1103 int err;
1104
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001105 ap->args.in_pages = true;
1106 ap->descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001107
1108 do {
1109 size_t tmp;
1110 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001111 pgoff_t index = pos >> PAGE_SHIFT;
1112 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001113 iov_iter_count(ii));
1114
1115 bytes = min_t(size_t, bytes, fc->max_write - count);
1116
1117 again:
1118 err = -EFAULT;
1119 if (iov_iter_fault_in_readable(ii, bytes))
1120 break;
1121
1122 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001123 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001124 if (!page)
1125 break;
1126
anfei zhou931e80e2010-02-02 13:44:02 -08001127 if (mapping_writably_mapped(mapping))
1128 flush_dcache_page(page);
1129
Nick Pigginea9b9902008-04-30 00:54:42 -07001130 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001131 flush_dcache_page(page);
1132
Roman Gushchin3ca81382015-10-12 16:33:44 +03001133 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001134 if (!tmp) {
1135 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001136 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001137 bytes = min(bytes, iov_iter_single_seg_count(ii));
1138 goto again;
1139 }
1140
1141 err = 0;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001142 ap->pages[ap->num_pages] = page;
1143 ap->descs[ap->num_pages].length = tmp;
1144 ap->num_pages++;
Nick Pigginea9b9902008-04-30 00:54:42 -07001145
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 count += tmp;
1147 pos += tmp;
1148 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001150 offset = 0;
1151
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001152 if (!fc->big_writes)
1153 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001154 } while (iov_iter_count(ii) && count < fc->max_write &&
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001155 ap->num_pages < max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001156
1157 return count > 0 ? count : err;
1158}
1159
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001160static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1161 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001162{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001163 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001164 ((pos + len - 1) >> PAGE_SHIFT) -
1165 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001166 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001167}
1168
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001169static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 struct address_space *mapping,
1171 struct iov_iter *ii, loff_t pos)
1172{
1173 struct inode *inode = mapping->host;
1174 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001175 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001176 int err = 0;
1177 ssize_t res = 0;
1178
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001179 if (inode->i_size < pos + iov_iter_count(ii))
1180 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1181
Nick Pigginea9b9902008-04-30 00:54:42 -07001182 do {
Nick Pigginea9b9902008-04-30 00:54:42 -07001183 ssize_t count;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001184 struct fuse_io_args ia = {};
1185 struct fuse_args_pages *ap = &ia.ap;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001186 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1187 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001188
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001189 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1190 if (!ap->pages) {
1191 err = -ENOMEM;
Nick Pigginea9b9902008-04-30 00:54:42 -07001192 break;
1193 }
1194
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001195 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001196 if (count <= 0) {
1197 err = count;
1198 } else {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001199 err = fuse_send_write_pages(&ia, iocb, inode,
1200 pos, count);
Nick Pigginea9b9902008-04-30 00:54:42 -07001201 if (!err) {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001202 size_t num_written = ia.write.out.size;
1203
Nick Pigginea9b9902008-04-30 00:54:42 -07001204 res += num_written;
1205 pos += num_written;
1206
1207 /* break out of the loop on short write */
1208 if (num_written != count)
1209 err = -EIO;
1210 }
1211 }
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001212 kfree(ap->pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001213 } while (!err && iov_iter_count(ii));
1214
1215 if (res > 0)
1216 fuse_write_update_size(inode, pos);
1217
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001218 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001219 fuse_invalidate_attr(inode);
1220
1221 return res > 0 ? res : err;
1222}
1223
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001224static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001225{
1226 struct file *file = iocb->ki_filp;
1227 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001228 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001229 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001230 struct inode *inode = mapping->host;
1231 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001232 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001233
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001234 if (get_fuse_conn(inode)->writeback_cache) {
1235 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001236 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001237 if (err)
1238 return err;
1239
Al Viro84c3d552014-04-03 14:33:23 -04001240 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001241 }
1242
Al Viro59551022016-01-22 15:40:57 -05001243 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001244
1245 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001246 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001247
Al Viro3309dd02015-04-09 12:55:47 -04001248 err = generic_write_checks(iocb, from);
1249 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001250 goto out;
1251
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001252 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001253 if (err)
1254 goto out;
1255
Josef Bacikc3b2da32012-03-26 09:59:21 -04001256 err = file_update_time(file);
1257 if (err)
1258 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001259
Al Viro2ba48ce2015-04-09 13:52:01 -04001260 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001261 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001262 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001263 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001264 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001265
Anand Avati4273b792012-02-17 12:46:25 -05001266 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001267
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001268 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001269 if (written_buffered < 0) {
1270 err = written_buffered;
1271 goto out;
1272 }
1273 endbyte = pos + written_buffered - 1;
1274
1275 err = filemap_write_and_wait_range(file->f_mapping, pos,
1276 endbyte);
1277 if (err)
1278 goto out;
1279
1280 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001281 pos >> PAGE_SHIFT,
1282 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001283
1284 written += written_buffered;
1285 iocb->ki_pos = pos + written_buffered;
1286 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001287 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001288 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001289 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001290 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001291out:
1292 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001293 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001294 if (written > 0)
1295 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001296
1297 return written ? written : err;
1298}
1299
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001300static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1301 unsigned int index,
1302 unsigned int nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001303{
1304 int i;
1305
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306 for (i = index; i < index + nr_pages; i++)
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001307 descs[i].length = PAGE_SIZE - descs[i].offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001308}
1309
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1311{
1312 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1313}
1314
1315static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1316 size_t max_size)
1317{
1318 return min(iov_iter_single_seg_count(ii), max_size);
1319}
1320
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001321static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1322 size_t *nbytesp, int write,
1323 unsigned int max_pages)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001324{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001325 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001326 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001327
Miklos Szeredif4975c62009-04-02 14:25:34 +02001328 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001329 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001330 unsigned long user_addr = fuse_get_user_addr(ii);
1331 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1332
Miklos Szeredif4975c62009-04-02 14:25:34 +02001333 if (write)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001334 ap->args.in_args[1].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001335 else
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001336 ap->args.out_args[0].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001337
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001338 iov_iter_advance(ii, frag_size);
1339 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001340 return 0;
1341 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001342
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001343 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001344 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001345 size_t start;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001346 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001347 *nbytesp - nbytes,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001348 max_pages - ap->num_pages,
Al Viroc7f38882014-06-18 20:34:33 -04001349 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001350 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001351 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001352
Al Viroc9c37e22014-03-16 16:08:30 -04001353 iov_iter_advance(ii, ret);
1354 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001355
Al Viroc9c37e22014-03-16 16:08:30 -04001356 ret += start;
1357 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1358
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001359 ap->descs[ap->num_pages].offset = start;
1360 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001361
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001362 ap->num_pages += npages;
1363 ap->descs[ap->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001364 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001365 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001366
1367 if (write)
zhengbincabdb4f2020-01-14 20:39:45 +08001368 ap->args.in_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001369 else
zhengbincabdb4f2020-01-14 20:39:45 +08001370 ap->args.out_pages = true;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001371
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001372 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001373
Ashish Samant2c932d42016-03-25 10:53:41 -07001374 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375}
1376
Al Virod22a9432014-03-16 15:50:47 -04001377ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1378 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001380 int write = flags & FUSE_DIO_WRITE;
1381 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001382 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001383 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001384 struct fuse_file *ff = file->private_data;
1385 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001386 size_t nmax = write ? fc->max_write : fc->max_read;
1387 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001388 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001389 pgoff_t idx_from = pos >> PAGE_SHIFT;
1390 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 ssize_t res = 0;
Ashish Samant742f9922016-03-14 21:57:35 -07001392 int err = 0;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001393 struct fuse_io_args *ia;
1394 unsigned int max_pages;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001395
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001396 max_pages = iov_iter_npages(iter, fc->max_pages);
1397 ia = fuse_io_alloc(io, max_pages);
1398 if (!ia)
1399 return -ENOMEM;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001401 ia->io = io;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001402 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1403 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001404 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001405 fuse_sync_writes(inode);
1406 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001407 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001408 }
1409
Ashish Samant61c12b42017-07-12 19:26:58 -07001410 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001411 while (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001412 ssize_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001413 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001414 size_t nbytes = min(count, nmax);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001415
1416 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1417 max_pages);
Ashish Samant742f9922016-03-14 21:57:35 -07001418 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001420
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001421 if (write) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001422 if (!capable(CAP_FSETID))
1423 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001424
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001425 nres = fuse_send_write(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001426 } else {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001427 nres = fuse_send_read(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001428 }
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001429
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001430 if (!io->async || nres < 0) {
1431 fuse_release_user_pages(&ia->ap, io->should_dirty);
1432 fuse_io_free(ia);
1433 }
1434 ia = NULL;
1435 if (nres < 0) {
Miklos Szeredif658ade2020-02-06 16:39:28 +01001436 iov_iter_revert(iter, nbytes);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001437 err = nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001438 break;
1439 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001440 WARN_ON(nres > nbytes);
1441
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001442 count -= nres;
1443 res += nres;
1444 pos += nres;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001445 if (nres != nbytes) {
1446 iov_iter_revert(iter, nbytes - nres);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001447 break;
Miklos Szeredif658ade2020-02-06 16:39:28 +01001448 }
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001449 if (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001450 max_pages = iov_iter_npages(iter, fc->max_pages);
1451 ia = fuse_io_alloc(io, max_pages);
1452 if (!ia)
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001453 break;
1454 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001455 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001456 if (ia)
1457 fuse_io_free(ia);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001458 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001459 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001460
Ashish Samant742f9922016-03-14 21:57:35 -07001461 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001462}
Tejun Heo08cbf542009-04-14 10:54:53 +09001463EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001464
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001465static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001466 struct iov_iter *iter,
1467 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001468{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001469 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001470 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001471
Al Virod22a9432014-03-16 15:50:47 -04001472 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001473
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001474 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001475
1476 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001477}
1478
Martin Raiber23c94e12018-10-27 16:48:48 +00001479static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1480
Al Viro153162632015-03-30 22:08:36 -04001481static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001482{
Martin Raiber23c94e12018-10-27 16:48:48 +00001483 ssize_t res;
1484
1485 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001486 res = fuse_direct_IO(iocb, to);
1487 } else {
1488 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1489
1490 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1491 }
1492
1493 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001494}
1495
Al Viro153162632015-03-30 22:08:36 -04001496static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001497{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001498 struct inode *inode = file_inode(iocb->ki_filp);
1499 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001500 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001501
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001502 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001503 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001504 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001505 if (res > 0) {
1506 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1507 res = fuse_direct_IO(iocb, from);
1508 } else {
1509 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1510 FUSE_DIO_WRITE);
1511 }
1512 }
Al Viro812408f2015-03-30 22:15:58 -04001513 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001514 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001515 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001516 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001517
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001518 return res;
1519}
1520
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001521static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1522{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001523 struct file *file = iocb->ki_filp;
1524 struct fuse_file *ff = file->private_data;
1525
1526 if (is_bad_inode(file_inode(file)))
1527 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001528
1529 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1530 return fuse_cache_read_iter(iocb, to);
1531 else
1532 return fuse_direct_read_iter(iocb, to);
1533}
1534
1535static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1536{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001537 struct file *file = iocb->ki_filp;
1538 struct fuse_file *ff = file->private_data;
1539
1540 if (is_bad_inode(file_inode(file)))
1541 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001542
1543 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1544 return fuse_cache_write_iter(iocb, from);
1545 else
1546 return fuse_direct_write_iter(iocb, from);
1547}
1548
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001549static void fuse_writepage_free(struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001550{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001551 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001552 int i;
1553
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001554 for (i = 0; i < ap->num_pages; i++)
1555 __free_page(ap->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001556
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001557 if (wpa->ia.ff)
1558 fuse_file_put(wpa->ia.ff, false, false);
1559
1560 kfree(ap->pages);
1561 kfree(wpa);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001562}
1563
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001564static void fuse_writepage_finish(struct fuse_conn *fc,
1565 struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001566{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001567 struct fuse_args_pages *ap = &wpa->ia.ap;
1568 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001569 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001570 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001571 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001572
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001573 list_del(&wpa->writepages_entry);
1574 for (i = 0; i < ap->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001575 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001576 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001577 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001578 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001579 wake_up(&fi->page_waitq);
1580}
1581
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001582/* Called under fi->lock, may release and reacquire it */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001583static void fuse_send_writepage(struct fuse_conn *fc,
1584 struct fuse_writepage_args *wpa, loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001585__releases(fi->lock)
1586__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001587{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001588 struct fuse_writepage_args *aux, *next;
1589 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1590 struct fuse_write_in *inarg = &wpa->ia.write.in;
1591 struct fuse_args *args = &wpa->ia.ap.args;
1592 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1593 int err;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001594
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001595 fi->writectr++;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001596 if (inarg->offset + data_size <= size) {
1597 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001598 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001599 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001600 } else {
1601 /* Got truncated off completely */
1602 goto out_free;
1603 }
1604
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001605 args->in_args[1].size = inarg->size;
1606 args->force = true;
1607 args->nocreds = true;
1608
1609 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1610 if (err == -ENOMEM) {
1611 spin_unlock(&fi->lock);
1612 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1613 spin_lock(&fi->lock);
1614 }
1615
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001616 /* Fails on broken connection only */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001617 if (unlikely(err))
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001618 goto out_free;
1619
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001620 return;
1621
1622 out_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001623 fi->writectr--;
1624 fuse_writepage_finish(fc, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001625 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001626
1627 /* After fuse_writepage_finish() aux request list is private */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001628 for (aux = wpa->next; aux; aux = next) {
1629 next = aux->next;
1630 aux->next = NULL;
1631 fuse_writepage_free(aux);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001632 }
1633
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001634 fuse_writepage_free(wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001635 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636}
1637
1638/*
1639 * If fi->writectr is positive (no truncate or fsync going on) send
1640 * all queued writepage requests.
1641 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001642 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001643 */
1644void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001645__releases(fi->lock)
1646__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001647{
1648 struct fuse_conn *fc = get_fuse_conn(inode);
1649 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9de5be02019-04-24 17:05:06 +02001650 loff_t crop = i_size_read(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001651 struct fuse_writepage_args *wpa;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001652
1653 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001654 wpa = list_entry(fi->queued_writes.next,
1655 struct fuse_writepage_args, queue_entry);
1656 list_del_init(&wpa->queue_entry);
1657 fuse_send_writepage(fc, wpa, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658 }
1659}
1660
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001661static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1662 int error)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001664 struct fuse_writepage_args *wpa =
1665 container_of(args, typeof(*wpa), ia.ap.args);
1666 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001667 struct fuse_inode *fi = get_fuse_inode(inode);
1668
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001669 mapping_set_error(inode->i_mapping, error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001670 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001671 while (wpa->next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001672 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001673 struct fuse_write_in *inarg = &wpa->ia.write.in;
1674 struct fuse_writepage_args *next = wpa->next;
1675
1676 wpa->next = next->next;
1677 next->next = NULL;
1678 next->ia.ff = fuse_file_get(wpa->ia.ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001679 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001680
1681 /*
1682 * Skip fuse_flush_writepages() to make it easy to crop requests
1683 * based on primary request size.
1684 *
1685 * 1st case (trivial): there are no concurrent activities using
1686 * fuse_set/release_nowrite. Then we're on safe side because
1687 * fuse_flush_writepages() would call fuse_send_writepage()
1688 * anyway.
1689 *
1690 * 2nd case: someone called fuse_set_nowrite and it is waiting
1691 * now for completion of all in-flight requests. This happens
1692 * rarely and no more than once per page, so this should be
1693 * okay.
1694 *
1695 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1696 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1697 * that fuse_set_nowrite returned implies that all in-flight
1698 * requests were completed along with all of their secondary
1699 * requests. Further primary requests are blocked by negative
1700 * writectr. Hence there cannot be any in-flight requests and
1701 * no invocations of fuse_writepage_end() while we're in
1702 * fuse_set_nowrite..fuse_release_nowrite section.
1703 */
1704 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001705 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706 fi->writectr--;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001707 fuse_writepage_finish(fc, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001708 spin_unlock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001709 fuse_writepage_free(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001710}
1711
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001712static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1713 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001714{
Miklos Szeredi72523422013-10-01 16:44:52 +02001715 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001716
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001717 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001718 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001719 ff = list_entry(fi->write_files.next, struct fuse_file,
1720 write_entry);
1721 fuse_file_get(ff);
1722 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001723 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001724
1725 return ff;
1726}
1727
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001728static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1729 struct fuse_inode *fi)
1730{
1731 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1732 WARN_ON(!ff);
1733 return ff;
1734}
1735
1736int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1737{
1738 struct fuse_conn *fc = get_fuse_conn(inode);
1739 struct fuse_inode *fi = get_fuse_inode(inode);
1740 struct fuse_file *ff;
1741 int err;
1742
1743 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001744 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001745 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001746 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001747
1748 return err;
1749}
1750
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001751static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1752{
1753 struct fuse_writepage_args *wpa;
1754 struct fuse_args_pages *ap;
1755
1756 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1757 if (wpa) {
1758 ap = &wpa->ia.ap;
1759 ap->num_pages = 0;
1760 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1761 if (!ap->pages) {
1762 kfree(wpa);
1763 wpa = NULL;
1764 }
1765 }
1766 return wpa;
1767
1768}
1769
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001770static int fuse_writepage_locked(struct page *page)
1771{
1772 struct address_space *mapping = page->mapping;
1773 struct inode *inode = mapping->host;
1774 struct fuse_conn *fc = get_fuse_conn(inode);
1775 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001776 struct fuse_writepage_args *wpa;
1777 struct fuse_args_pages *ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001778 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001779 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001780
1781 set_page_writeback(page);
1782
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001783 wpa = fuse_writepage_args_alloc();
1784 if (!wpa)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001785 goto err;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001786 ap = &wpa->ia.ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001787
1788 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1789 if (!tmp_page)
1790 goto err_free;
1791
Miklos Szeredi72523422013-10-01 16:44:52 +02001792 error = -EIO;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001793 wpa->ia.ff = fuse_write_file_get(fc, fi);
1794 if (!wpa->ia.ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001795 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001796
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001797 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001798
1799 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001800 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1801 wpa->next = NULL;
1802 ap->args.in_pages = true;
1803 ap->num_pages = 1;
1804 ap->pages[0] = tmp_page;
1805 ap->descs[0].offset = 0;
1806 ap->descs[0].length = PAGE_SIZE;
1807 ap->args.end = fuse_writepage_end;
1808 wpa->inode = inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001809
Tejun Heo93f78d82015-05-22 17:13:27 -04001810 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001811 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001812
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001813 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001814 list_add(&wpa->writepages_entry, &fi->writepages);
1815 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001816 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001817 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001818
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001819 end_page_writeback(page);
1820
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001821 return 0;
1822
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001823err_nofile:
1824 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001825err_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001826 kfree(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001827err:
Jeff Layton91839762017-05-25 06:57:50 -04001828 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001829 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001830 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001831}
1832
1833static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1834{
1835 int err;
1836
Miklos Szerediff17be02013-10-01 16:44:53 +02001837 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1838 /*
1839 * ->writepages() should be called for sync() and friends. We
1840 * should only get here on direct reclaim and then we are
1841 * allowed to skip a page which is already in flight
1842 */
1843 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1844
1845 redirty_page_for_writepage(wbc, page);
Vasily Averind5880c72019-09-13 18:17:11 +03001846 unlock_page(page);
Miklos Szerediff17be02013-10-01 16:44:53 +02001847 return 0;
1848 }
1849
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001850 err = fuse_writepage_locked(page);
1851 unlock_page(page);
1852
1853 return err;
1854}
1855
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001856struct fuse_fill_wb_data {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001857 struct fuse_writepage_args *wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001858 struct fuse_file *ff;
1859 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001860 struct page **orig_pages;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001861 unsigned int max_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001862};
1863
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001864static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1865{
1866 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1867 struct fuse_conn *fc = get_fuse_conn(data->inode);
1868 struct page **pages;
1869 struct fuse_page_desc *descs;
1870 unsigned int npages = min_t(unsigned int,
1871 max_t(unsigned int, data->max_pages * 2,
1872 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1873 fc->max_pages);
1874 WARN_ON(npages <= data->max_pages);
1875
1876 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1877 if (!pages)
1878 return false;
1879
1880 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1881 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1882 kfree(ap->pages);
1883 ap->pages = pages;
1884 ap->descs = descs;
1885 data->max_pages = npages;
1886
1887 return true;
1888}
1889
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001890static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1891{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001892 struct fuse_writepage_args *wpa = data->wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001893 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001895 int num_pages = wpa->ia.ap.num_pages;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001896 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001897
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001898 wpa->ia.ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001899 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001900 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001901 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001902 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001903
1904 for (i = 0; i < num_pages; i++)
1905 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001906}
1907
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001908/*
1909 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001910 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1911 * one already added for a page at this offset. If there's none, then insert
1912 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001913 * copying the new page contents over to the old temporary page.
1914 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001915static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001916 struct page *page)
1917{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001918 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1919 struct fuse_writepage_args *tmp;
1920 struct fuse_writepage_args *old_wpa;
1921 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001922
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001923 WARN_ON(new_ap->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001924
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001925 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001926 list_del(&new_wpa->writepages_entry);
1927 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1928 if (!old_wpa) {
1929 list_add(&new_wpa->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001930 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001931 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001932 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001933
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001934 new_ap->num_pages = 1;
1935 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001936 pgoff_t curr_index;
1937
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001938 WARN_ON(tmp->inode != new_wpa->inode);
1939 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001940 if (curr_index == page->index) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001941 WARN_ON(tmp->ia.ap.num_pages != 1);
1942 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001943 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001944 }
1945 }
1946
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001947 if (!tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001948 new_wpa->next = old_wpa->next;
1949 old_wpa->next = new_wpa;
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001950 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001951
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001952 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001953
1954 if (tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001955 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001956
Tejun Heo93f78d82015-05-22 17:13:27 -04001957 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001958 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001959 wb_writeout_inc(&bdi->wb);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001960 fuse_writepage_free(new_wpa);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001961 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001962
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001963 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001964}
1965
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001966static int fuse_writepages_fill(struct page *page,
1967 struct writeback_control *wbc, void *_data)
1968{
1969 struct fuse_fill_wb_data *data = _data;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001970 struct fuse_writepage_args *wpa = data->wpa;
1971 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001972 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001973 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001974 struct fuse_conn *fc = get_fuse_conn(inode);
1975 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001976 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001977 int err;
1978
1979 if (!data->ff) {
1980 err = -EIO;
Vasily Averin091d1a72019-08-19 08:48:26 +03001981 data->ff = fuse_write_file_get(fc, fi);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001982 if (!data->ff)
1983 goto out_unlock;
1984 }
1985
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001986 /*
1987 * Being under writeback is unlikely but possible. For example direct
1988 * read to an mmaped fuse file will set the page dirty twice; once when
1989 * the pages are faulted with get_user_pages(), and then after the read
1990 * completed.
1991 */
1992 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001993
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001994 if (wpa && ap->num_pages &&
1995 (is_writeback || ap->num_pages == fc->max_pages ||
1996 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1997 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001998 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001999 data->wpa = NULL;
2000 } else if (wpa && ap->num_pages == data->max_pages) {
2001 if (!fuse_pages_realloc(data)) {
Miklos Szeredie52a82502018-10-01 10:07:06 +02002002 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002003 data->wpa = NULL;
Miklos Szeredie52a82502018-10-01 10:07:06 +02002004 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002005 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02002006
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002007 err = -ENOMEM;
2008 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2009 if (!tmp_page)
2010 goto out_unlock;
2011
2012 /*
2013 * The page must not be redirtied until the writeout is completed
2014 * (i.e. userspace has sent a reply to the write request). Otherwise
2015 * there could be more than one temporary page instance for each real
2016 * page.
2017 *
2018 * This is ensured by holding the page lock in page_mkwrite() while
2019 * checking fuse_page_is_writeback(). We already hold the page lock
2020 * since clear_page_dirty_for_io() and keep it held until we add the
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002021 * request to the fi->writepages list and increment ap->num_pages.
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002022 * After this fuse_page_is_writeback() will indicate that the page is
2023 * under writeback, so we can release the page lock.
2024 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002025 if (data->wpa == NULL) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002026 err = -ENOMEM;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002027 wpa = fuse_writepage_args_alloc();
2028 if (!wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002029 __free_page(tmp_page);
2030 goto out_unlock;
2031 }
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002032 data->max_pages = 1;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002033
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002034 ap = &wpa->ia.ap;
2035 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2036 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2037 wpa->next = NULL;
2038 ap->args.in_pages = true;
2039 ap->args.end = fuse_writepage_end;
2040 ap->num_pages = 0;
2041 wpa->inode = inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002042
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002043 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002044 list_add(&wpa->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002045 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002046
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002047 data->wpa = wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002048 }
2049 set_page_writeback(page);
2050
2051 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002052 ap->pages[ap->num_pages] = tmp_page;
2053 ap->descs[ap->num_pages].offset = 0;
2054 ap->descs[ap->num_pages].length = PAGE_SIZE;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002055
Tejun Heo93f78d82015-05-22 17:13:27 -04002056 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07002057 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002058
2059 err = 0;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002060 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002061 end_page_writeback(page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002062 data->wpa = NULL;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002063 goto out_unlock;
2064 }
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002065 data->orig_pages[ap->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002066
2067 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002068 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002069 * fuse_page_is_writeback().
2070 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002071 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002072 ap->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002073 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002074
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002075out_unlock:
2076 unlock_page(page);
2077
2078 return err;
2079}
2080
2081static int fuse_writepages(struct address_space *mapping,
2082 struct writeback_control *wbc)
2083{
2084 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002085 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002086 struct fuse_fill_wb_data data;
2087 int err;
2088
2089 err = -EIO;
2090 if (is_bad_inode(inode))
2091 goto out;
2092
2093 data.inode = inode;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002094 data.wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002095 data.ff = NULL;
2096
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002097 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002098 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02002099 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002100 GFP_NOFS);
2101 if (!data.orig_pages)
2102 goto out;
2103
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002104 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002105 if (data.wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002106 /* Ignore errors if we can write at least one page */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002107 WARN_ON(!data.wpa->ia.ap.num_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002108 fuse_writepages_send(&data);
2109 err = 0;
2110 }
2111 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002112 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002113
2114 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002115out:
2116 return err;
2117}
2118
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002119/*
2120 * It's worthy to make sure that space is reserved on disk for the write,
2121 * but how to implement it without killing performance need more thinking.
2122 */
2123static int fuse_write_begin(struct file *file, struct address_space *mapping,
2124 loff_t pos, unsigned len, unsigned flags,
2125 struct page **pagep, void **fsdata)
2126{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002127 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002128 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002129 struct page *page;
2130 loff_t fsize;
2131 int err = -ENOMEM;
2132
2133 WARN_ON(!fc->writeback_cache);
2134
2135 page = grab_cache_page_write_begin(mapping, index, flags);
2136 if (!page)
2137 goto error;
2138
2139 fuse_wait_on_page_writeback(mapping->host, page->index);
2140
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002141 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002142 goto success;
2143 /*
2144 * Check if the start this page comes after the end of file, in which
2145 * case the readpage can be optimized away.
2146 */
2147 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002148 if (fsize <= (pos & PAGE_MASK)) {
2149 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002150 if (off)
2151 zero_user_segment(page, 0, off);
2152 goto success;
2153 }
2154 err = fuse_do_readpage(file, page);
2155 if (err)
2156 goto cleanup;
2157success:
2158 *pagep = page;
2159 return 0;
2160
2161cleanup:
2162 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002163 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002164error:
2165 return err;
2166}
2167
2168static int fuse_write_end(struct file *file, struct address_space *mapping,
2169 loff_t pos, unsigned len, unsigned copied,
2170 struct page *page, void *fsdata)
2171{
2172 struct inode *inode = page->mapping->host;
2173
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002174 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2175 if (!copied)
2176 goto unlock;
2177
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002178 if (!PageUptodate(page)) {
2179 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002180 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002181 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002182 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002183 SetPageUptodate(page);
2184 }
2185
2186 fuse_write_update_size(inode, pos + copied);
2187 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002188
2189unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002190 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002192
2193 return copied;
2194}
2195
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002196static int fuse_launder_page(struct page *page)
2197{
2198 int err = 0;
2199 if (clear_page_dirty_for_io(page)) {
2200 struct inode *inode = page->mapping->host;
2201 err = fuse_writepage_locked(page);
2202 if (!err)
2203 fuse_wait_on_page_writeback(inode, page->index);
2204 }
2205 return err;
2206}
2207
2208/*
2209 * Write back dirty pages now, because there may not be any suitable
2210 * open files later
2211 */
2212static void fuse_vma_close(struct vm_area_struct *vma)
2213{
2214 filemap_write_and_wait(vma->vm_file->f_mapping);
2215}
2216
2217/*
2218 * Wait for writeback against this page to complete before allowing it
2219 * to be marked dirty again, and hence written back again, possibly
2220 * before the previous writepage completed.
2221 *
2222 * Block here, instead of in ->writepage(), so that the userspace fs
2223 * can only block processes actually operating on the filesystem.
2224 *
2225 * Otherwise unprivileged userspace fs would be able to block
2226 * unrelated:
2227 *
2228 * - page migration
2229 * - sync(2)
2230 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2231 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302232static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002233{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002234 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002235 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002236
Dave Jiang11bac802017-02-24 14:56:41 -08002237 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002238 lock_page(page);
2239 if (page->mapping != inode->i_mapping) {
2240 unlock_page(page);
2241 return VM_FAULT_NOPAGE;
2242 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002243
2244 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002245 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002246}
2247
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002248static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002249 .close = fuse_vma_close,
2250 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002251 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002252 .page_mkwrite = fuse_page_mkwrite,
2253};
2254
2255static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2256{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002257 struct fuse_file *ff = file->private_data;
2258
2259 if (ff->open_flags & FOPEN_DIRECT_IO) {
2260 /* Can't provide the coherency needed for MAP_SHARED */
2261 if (vma->vm_flags & VM_MAYSHARE)
2262 return -ENODEV;
2263
2264 invalidate_inode_pages2(file->f_mapping);
2265
2266 return generic_file_mmap(file, vma);
2267 }
2268
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002269 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2270 fuse_link_write_file(file);
2271
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002272 file_accessed(file);
2273 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002274 return 0;
2275}
2276
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002277static int convert_fuse_file_lock(struct fuse_conn *fc,
2278 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002279 struct file_lock *fl)
2280{
2281 switch (ffl->type) {
2282 case F_UNLCK:
2283 break;
2284
2285 case F_RDLCK:
2286 case F_WRLCK:
2287 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2288 ffl->end < ffl->start)
2289 return -EIO;
2290
2291 fl->fl_start = ffl->start;
2292 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002293
2294 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002295 * Convert pid into init's pid namespace. The locks API will
2296 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002297 */
2298 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002299 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002300 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002301 break;
2302
2303 default:
2304 return -EIO;
2305 }
2306 fl->fl_type = ffl->type;
2307 return 0;
2308}
2309
Miklos Szeredi70781872014-12-12 09:49:05 +01002310static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002311 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002312 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002313{
Al Viro6131ffa2013-02-27 16:59:05 -05002314 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002315 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002316 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002317
Miklos Szeredi70781872014-12-12 09:49:05 +01002318 memset(inarg, 0, sizeof(*inarg));
2319 inarg->fh = ff->fh;
2320 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2321 inarg->lk.start = fl->fl_start;
2322 inarg->lk.end = fl->fl_end;
2323 inarg->lk.type = fl->fl_type;
2324 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002325 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002326 inarg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002327 args->opcode = opcode;
2328 args->nodeid = get_node_id(inode);
2329 args->in_numargs = 1;
2330 args->in_args[0].size = sizeof(*inarg);
2331 args->in_args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002332}
2333
2334static int fuse_getlk(struct file *file, struct file_lock *fl)
2335{
Al Viro6131ffa2013-02-27 16:59:05 -05002336 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002337 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002338 FUSE_ARGS(args);
2339 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002340 struct fuse_lk_out outarg;
2341 int err;
2342
Miklos Szeredi70781872014-12-12 09:49:05 +01002343 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
Miklos Szeredid5b48542019-09-10 15:04:08 +02002344 args.out_numargs = 1;
2345 args.out_args[0].size = sizeof(outarg);
2346 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002347 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002348 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002349 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002350
2351 return err;
2352}
2353
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002354static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002355{
Al Viro6131ffa2013-02-27 16:59:05 -05002356 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002357 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002358 FUSE_ARGS(args);
2359 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002360 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002361 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2362 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002363 int err;
2364
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002365 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002366 /* NLM needs asynchronous locks, which we don't support yet */
2367 return -ENOLCK;
2368 }
2369
Miklos Szeredi71421252006-06-25 05:48:52 -07002370 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002371 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002372 return 0;
2373
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002374 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002375 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002376
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002377 /* locking is restartable */
2378 if (err == -EINTR)
2379 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002380
Miklos Szeredi71421252006-06-25 05:48:52 -07002381 return err;
2382}
2383
2384static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2385{
Al Viro6131ffa2013-02-27 16:59:05 -05002386 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002387 struct fuse_conn *fc = get_fuse_conn(inode);
2388 int err;
2389
Miklos Szeredi48e90762008-07-25 01:49:02 -07002390 if (cmd == F_CANCELLK) {
2391 err = 0;
2392 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002393 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002394 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002395 err = 0;
2396 } else
2397 err = fuse_getlk(file, fl);
2398 } else {
2399 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002400 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002401 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002402 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002403 }
2404 return err;
2405}
2406
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002407static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2408{
Al Viro6131ffa2013-02-27 16:59:05 -05002409 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002410 struct fuse_conn *fc = get_fuse_conn(inode);
2411 int err;
2412
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002413 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002414 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002415 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002416 struct fuse_file *ff = file->private_data;
2417
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002418 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002419 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002420 err = fuse_setlk(file, fl, 1);
2421 }
2422
2423 return err;
2424}
2425
Miklos Szeredib2d22722006-12-06 20:35:51 -08002426static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2427{
2428 struct inode *inode = mapping->host;
2429 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002430 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002431 struct fuse_bmap_in inarg;
2432 struct fuse_bmap_out outarg;
2433 int err;
2434
2435 if (!inode->i_sb->s_bdev || fc->no_bmap)
2436 return 0;
2437
Miklos Szeredib2d22722006-12-06 20:35:51 -08002438 memset(&inarg, 0, sizeof(inarg));
2439 inarg.block = block;
2440 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002441 args.opcode = FUSE_BMAP;
2442 args.nodeid = get_node_id(inode);
2443 args.in_numargs = 1;
2444 args.in_args[0].size = sizeof(inarg);
2445 args.in_args[0].value = &inarg;
2446 args.out_numargs = 1;
2447 args.out_args[0].size = sizeof(outarg);
2448 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002449 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002450 if (err == -ENOSYS)
2451 fc->no_bmap = 1;
2452
2453 return err ? 0 : outarg.block;
2454}
2455
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302456static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2457{
2458 struct inode *inode = file->f_mapping->host;
2459 struct fuse_conn *fc = get_fuse_conn(inode);
2460 struct fuse_file *ff = file->private_data;
2461 FUSE_ARGS(args);
2462 struct fuse_lseek_in inarg = {
2463 .fh = ff->fh,
2464 .offset = offset,
2465 .whence = whence
2466 };
2467 struct fuse_lseek_out outarg;
2468 int err;
2469
2470 if (fc->no_lseek)
2471 goto fallback;
2472
Miklos Szeredid5b48542019-09-10 15:04:08 +02002473 args.opcode = FUSE_LSEEK;
2474 args.nodeid = ff->nodeid;
2475 args.in_numargs = 1;
2476 args.in_args[0].size = sizeof(inarg);
2477 args.in_args[0].value = &inarg;
2478 args.out_numargs = 1;
2479 args.out_args[0].size = sizeof(outarg);
2480 args.out_args[0].value = &outarg;
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302481 err = fuse_simple_request(fc, &args);
2482 if (err) {
2483 if (err == -ENOSYS) {
2484 fc->no_lseek = 1;
2485 goto fallback;
2486 }
2487 return err;
2488 }
2489
2490 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2491
2492fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002493 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302494 if (!err)
2495 return generic_file_llseek(file, offset, whence);
2496 else
2497 return err;
2498}
2499
Andrew Morton965c8e52012-12-17 15:59:39 -08002500static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002501{
2502 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002503 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002504
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302505 switch (whence) {
2506 case SEEK_SET:
2507 case SEEK_CUR:
2508 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002509 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302510 break;
2511 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002512 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002513 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302514 if (!retval)
2515 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002516 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302517 break;
2518 case SEEK_HOLE:
2519 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002520 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302521 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002522 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302523 break;
2524 default:
2525 retval = -EINVAL;
2526 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002527
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002528 return retval;
2529}
2530
Tejun Heo59efec72008-11-26 12:03:55 +01002531/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002532 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2533 * ABI was defined to be 'struct iovec' which is different on 32bit
2534 * and 64bit. Fortunately we can determine which structure the server
2535 * used from the size of the reply.
2536 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002537static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2538 size_t transferred, unsigned count,
2539 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002540{
2541#ifdef CONFIG_COMPAT
2542 if (count * sizeof(struct compat_iovec) == transferred) {
2543 struct compat_iovec *ciov = src;
2544 unsigned i;
2545
2546 /*
2547 * With this interface a 32bit server cannot support
2548 * non-compat (i.e. ones coming from 64bit apps) ioctl
2549 * requests
2550 */
2551 if (!is_compat)
2552 return -EINVAL;
2553
2554 for (i = 0; i < count; i++) {
2555 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2556 dst[i].iov_len = ciov[i].iov_len;
2557 }
2558 return 0;
2559 }
2560#endif
2561
2562 if (count * sizeof(struct iovec) != transferred)
2563 return -EIO;
2564
2565 memcpy(dst, src, transferred);
2566 return 0;
2567}
2568
Miklos Szeredi75727772010-11-30 16:39:27 +01002569/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002570static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2571 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002572{
2573 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002574 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002575
Zach Brownfb6ccff2012-07-24 12:10:11 -07002576 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002577 if (iov->iov_len > (size_t) max)
2578 return -ENOMEM;
2579 max -= iov->iov_len;
2580 }
2581 return 0;
2582}
2583
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002584static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2585 void *src, size_t transferred, unsigned count,
2586 bool is_compat)
2587{
2588 unsigned i;
2589 struct fuse_ioctl_iovec *fiov = src;
2590
2591 if (fc->minor < 16) {
2592 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2593 count, is_compat);
2594 }
2595
2596 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2597 return -EIO;
2598
2599 for (i = 0; i < count; i++) {
2600 /* Did the server supply an inappropriate value? */
2601 if (fiov[i].base != (unsigned long) fiov[i].base ||
2602 fiov[i].len != (unsigned long) fiov[i].len)
2603 return -EIO;
2604
2605 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2606 dst[i].iov_len = (size_t) fiov[i].len;
2607
2608#ifdef CONFIG_COMPAT
2609 if (is_compat &&
2610 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2611 (compat_size_t) dst[i].iov_len != fiov[i].len))
2612 return -EIO;
2613#endif
2614 }
2615
2616 return 0;
2617}
2618
2619
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002620/*
Tejun Heo59efec72008-11-26 12:03:55 +01002621 * For ioctls, there is no generic way to determine how much memory
2622 * needs to be read and/or written. Furthermore, ioctls are allowed
2623 * to dereference the passed pointer, so the parameter requires deep
2624 * copying but FUSE has no idea whatsoever about what to copy in or
2625 * out.
2626 *
2627 * This is solved by allowing FUSE server to retry ioctl with
2628 * necessary in/out iovecs. Let's assume the ioctl implementation
2629 * needs to read in the following structure.
2630 *
2631 * struct a {
2632 * char *buf;
2633 * size_t buflen;
2634 * }
2635 *
2636 * On the first callout to FUSE server, inarg->in_size and
2637 * inarg->out_size will be NULL; then, the server completes the ioctl
2638 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2639 * the actual iov array to
2640 *
2641 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2642 *
2643 * which tells FUSE to copy in the requested area and retry the ioctl.
2644 * On the second round, the server has access to the structure and
2645 * from that it can tell what to look for next, so on the invocation,
2646 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2647 *
2648 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2649 * { .iov_base = a.buf, .iov_len = a.buflen } }
2650 *
2651 * FUSE will copy both struct a and the pointed buffer from the
2652 * process doing the ioctl and retry ioctl with both struct a and the
2653 * buffer.
2654 *
2655 * This time, FUSE server has everything it needs and completes ioctl
2656 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2657 *
2658 * Copying data out works the same way.
2659 *
2660 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2661 * automatically initializes in and out iovs by decoding @cmd with
2662 * _IOC_* macros and the server is not allowed to request RETRY. This
2663 * limits ioctl data transfers to well-formed ioctls and is the forced
2664 * behavior for all FUSE servers.
2665 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002666long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2667 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002668{
Tejun Heo59efec72008-11-26 12:03:55 +01002669 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002670 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002671 struct fuse_ioctl_in inarg = {
2672 .fh = ff->fh,
2673 .cmd = cmd,
2674 .arg = arg,
2675 .flags = flags
2676 };
2677 struct fuse_ioctl_out outarg;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002678 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002679 struct iovec *in_iov = NULL, *out_iov = NULL;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002680 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2681 size_t in_size, out_size, c;
2682 ssize_t transferred;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002683 int err, i;
2684 struct iov_iter ii;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002685 struct fuse_args_pages ap = {};
Tejun Heo59efec72008-11-26 12:03:55 +01002686
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002687#if BITS_PER_LONG == 32
2688 inarg.flags |= FUSE_IOCTL_32BIT;
2689#else
Ian Abbott6407f442019-04-24 15:14:11 +01002690 if (flags & FUSE_IOCTL_COMPAT) {
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002691 inarg.flags |= FUSE_IOCTL_32BIT;
Ian Abbott6407f442019-04-24 15:14:11 +01002692#ifdef CONFIG_X86_X32
2693 if (in_x32_syscall())
2694 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2695#endif
2696 }
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002697#endif
2698
Tejun Heo59efec72008-11-26 12:03:55 +01002699 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002700 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002701
Tejun Heo59efec72008-11-26 12:03:55 +01002702 err = -ENOMEM;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002703 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002704 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002705 if (!ap.pages || !iov_page)
Tejun Heo59efec72008-11-26 12:03:55 +01002706 goto out;
2707
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002708 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2709
Tejun Heo59efec72008-11-26 12:03:55 +01002710 /*
2711 * If restricted, initialize IO parameters as encoded in @cmd.
2712 * RETRY from server is not allowed.
2713 */
2714 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002715 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002716
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002717 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002718 iov->iov_len = _IOC_SIZE(cmd);
2719
2720 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2721 in_iov = iov;
2722 in_iovs = 1;
2723 }
2724
2725 if (_IOC_DIR(cmd) & _IOC_READ) {
2726 out_iov = iov;
2727 out_iovs = 1;
2728 }
2729 }
2730
2731 retry:
2732 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2733 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2734
2735 /*
2736 * Out data can be used either for actual out data or iovs,
2737 * make sure there always is at least one page.
2738 */
2739 out_size = max_t(size_t, out_size, PAGE_SIZE);
2740 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2741
2742 /* make sure there are enough buffer pages and init request with them */
2743 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002744 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002745 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002746 while (ap.num_pages < max_pages) {
2747 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2748 if (!ap.pages[ap.num_pages])
Tejun Heo59efec72008-11-26 12:03:55 +01002749 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002750 ap.num_pages++;
Tejun Heo59efec72008-11-26 12:03:55 +01002751 }
2752
Tejun Heo59efec72008-11-26 12:03:55 +01002753
2754 /* okay, let's send it to the client */
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002755 ap.args.opcode = FUSE_IOCTL;
2756 ap.args.nodeid = ff->nodeid;
2757 ap.args.in_numargs = 1;
2758 ap.args.in_args[0].size = sizeof(inarg);
2759 ap.args.in_args[0].value = &inarg;
Tejun Heo59efec72008-11-26 12:03:55 +01002760 if (in_size) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002761 ap.args.in_numargs++;
2762 ap.args.in_args[1].size = in_size;
2763 ap.args.in_pages = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002764
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002765 err = -EFAULT;
2766 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002767 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2768 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002769 if (c != PAGE_SIZE && iov_iter_count(&ii))
2770 goto out;
2771 }
Tejun Heo59efec72008-11-26 12:03:55 +01002772 }
2773
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002774 ap.args.out_numargs = 2;
2775 ap.args.out_args[0].size = sizeof(outarg);
2776 ap.args.out_args[0].value = &outarg;
2777 ap.args.out_args[1].size = out_size;
2778 ap.args.out_pages = true;
2779 ap.args.out_argvar = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002780
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002781 transferred = fuse_simple_request(fc, &ap.args);
2782 err = transferred;
2783 if (transferred < 0)
Tejun Heo59efec72008-11-26 12:03:55 +01002784 goto out;
2785
2786 /* did it ask for retry? */
2787 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002788 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002789
2790 /* no retry if in restricted mode */
2791 err = -EIO;
2792 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2793 goto out;
2794
2795 in_iovs = outarg.in_iovs;
2796 out_iovs = outarg.out_iovs;
2797
2798 /*
2799 * Make sure things are in boundary, separate checks
2800 * are to protect against overflow.
2801 */
2802 err = -ENOMEM;
2803 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2804 out_iovs > FUSE_IOCTL_MAX_IOV ||
2805 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2806 goto out;
2807
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002808 vaddr = kmap_atomic(ap.pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002809 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002810 transferred, in_iovs + out_iovs,
2811 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002812 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002813 if (err)
2814 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002815
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002816 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002817 out_iov = in_iov + in_iovs;
2818
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002819 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002820 if (err)
2821 goto out;
2822
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002823 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002824 if (err)
2825 goto out;
2826
Tejun Heo59efec72008-11-26 12:03:55 +01002827 goto retry;
2828 }
2829
2830 err = -EIO;
2831 if (transferred > inarg.out_size)
2832 goto out;
2833
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002834 err = -EFAULT;
2835 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002836 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2837 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002838 if (c != PAGE_SIZE && iov_iter_count(&ii))
2839 goto out;
2840 }
2841 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002842 out:
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002843 free_page((unsigned long) iov_page);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002844 while (ap.num_pages)
2845 __free_page(ap.pages[--ap.num_pages]);
2846 kfree(ap.pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002847
2848 return err ? err : outarg.result;
2849}
Tejun Heo08cbf542009-04-14 10:54:53 +09002850EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002851
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002852long fuse_ioctl_common(struct file *file, unsigned int cmd,
2853 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002854{
Al Viro6131ffa2013-02-27 16:59:05 -05002855 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002856 struct fuse_conn *fc = get_fuse_conn(inode);
2857
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002858 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002859 return -EACCES;
2860
2861 if (is_bad_inode(inode))
2862 return -EIO;
2863
2864 return fuse_do_ioctl(file, cmd, arg, flags);
2865}
2866
Tejun Heo59efec72008-11-26 12:03:55 +01002867static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2868 unsigned long arg)
2869{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002870 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002871}
2872
2873static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2874 unsigned long arg)
2875{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002876 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002877}
2878
Tejun Heo95668a62008-11-26 12:03:55 +01002879/*
2880 * All files which have been polled are linked to RB tree
2881 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2882 * find the matching one.
2883 */
2884static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2885 struct rb_node **parent_out)
2886{
2887 struct rb_node **link = &fc->polled_files.rb_node;
2888 struct rb_node *last = NULL;
2889
2890 while (*link) {
2891 struct fuse_file *ff;
2892
2893 last = *link;
2894 ff = rb_entry(last, struct fuse_file, polled_node);
2895
2896 if (kh < ff->kh)
2897 link = &last->rb_left;
2898 else if (kh > ff->kh)
2899 link = &last->rb_right;
2900 else
2901 return link;
2902 }
2903
2904 if (parent_out)
2905 *parent_out = last;
2906 return link;
2907}
2908
2909/*
2910 * The file is about to be polled. Make sure it's on the polled_files
2911 * RB tree. Note that files once added to the polled_files tree are
2912 * not removed before the file is released. This is because a file
2913 * polled once is likely to be polled again.
2914 */
2915static void fuse_register_polled_file(struct fuse_conn *fc,
2916 struct fuse_file *ff)
2917{
2918 spin_lock(&fc->lock);
2919 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002920 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002921
2922 link = fuse_find_polled_node(fc, ff->kh, &parent);
2923 BUG_ON(*link);
2924 rb_link_node(&ff->polled_node, parent, link);
2925 rb_insert_color(&ff->polled_node, &fc->polled_files);
2926 }
2927 spin_unlock(&fc->lock);
2928}
2929
Al Viro076ccb72017-07-03 01:02:18 -04002930__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002931{
Tejun Heo95668a62008-11-26 12:03:55 +01002932 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002933 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002934 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2935 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002936 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002937 int err;
2938
2939 if (fc->no_poll)
2940 return DEFAULT_POLLMASK;
2941
2942 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002943 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002944
2945 /*
2946 * Ask for notification iff there's someone waiting for it.
2947 * The client may ignore the flag and always notify.
2948 */
2949 if (waitqueue_active(&ff->poll_wait)) {
2950 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2951 fuse_register_polled_file(fc, ff);
2952 }
2953
Miklos Szeredid5b48542019-09-10 15:04:08 +02002954 args.opcode = FUSE_POLL;
2955 args.nodeid = ff->nodeid;
2956 args.in_numargs = 1;
2957 args.in_args[0].size = sizeof(inarg);
2958 args.in_args[0].value = &inarg;
2959 args.out_numargs = 1;
2960 args.out_args[0].size = sizeof(outarg);
2961 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002962 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002963
2964 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002965 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002966 if (err == -ENOSYS) {
2967 fc->no_poll = 1;
2968 return DEFAULT_POLLMASK;
2969 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002970 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002971}
Tejun Heo08cbf542009-04-14 10:54:53 +09002972EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002973
2974/*
2975 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2976 * wakes up the poll waiters.
2977 */
2978int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2979 struct fuse_notify_poll_wakeup_out *outarg)
2980{
2981 u64 kh = outarg->kh;
2982 struct rb_node **link;
2983
2984 spin_lock(&fc->lock);
2985
2986 link = fuse_find_polled_node(fc, kh, NULL);
2987 if (*link) {
2988 struct fuse_file *ff;
2989
2990 ff = rb_entry(*link, struct fuse_file, polled_node);
2991 wake_up_interruptible_sync(&ff->poll_wait);
2992 }
2993
2994 spin_unlock(&fc->lock);
2995 return 0;
2996}
2997
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002998static void fuse_do_truncate(struct file *file)
2999{
3000 struct inode *inode = file->f_mapping->host;
3001 struct iattr attr;
3002
3003 attr.ia_valid = ATTR_SIZE;
3004 attr.ia_size = i_size_read(inode);
3005
3006 attr.ia_file = file;
3007 attr.ia_valid |= ATTR_FILE;
3008
Jan Kara62490332016-05-26 17:12:41 +02003009 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003010}
3011
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003012static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003013{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003014 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003015}
3016
Anand Avati4273b792012-02-17 12:46:25 -05003017static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003018fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05003019{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003020 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05003021 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003022 struct file *file = iocb->ki_filp;
3023 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003024 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05003025 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003026 struct inode *inode;
3027 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05003028 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003029 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003030 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05003031
Anand Avati4273b792012-02-17 12:46:25 -05003032 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003033 inode = file->f_mapping->host;
3034 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05003035
Omar Sandoval6f673762015-03-16 04:33:52 -07003036 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00003037 return 0;
3038
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003039 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07003040 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003041 if (offset >= i_size)
3042 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003043 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04003044 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003045 }
3046
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003047 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003048 if (!io)
3049 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003050 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06003051 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003052 io->reqs = 1;
3053 io->bytes = -1;
3054 io->size = 0;
3055 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07003056 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003057 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003058 /*
3059 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003060 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003061 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003062 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003063 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303064 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003065
3066 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303067 * We cannot asynchronously extend the size of a file.
3068 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003069 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303070 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3071 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05003072
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303073 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06003074 /*
3075 * Additional reference to keep io around after
3076 * calling fuse_aio_complete()
3077 */
3078 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003079 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06003080 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003081
Omar Sandoval6f673762015-03-16 04:33:52 -07003082 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04003083 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04003084 fuse_invalidate_attr(inode);
3085 } else {
Al Virod22a9432014-03-16 15:50:47 -04003086 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04003087 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003088
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003089 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01003090 bool blocking = io->blocking;
3091
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003092 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3093
3094 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01003095 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003096 return -EIOCBQUEUED;
3097
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003098 wait_for_completion(&wait);
3099 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003100 }
3101
Seth Forshee744742d2016-03-11 10:35:34 -06003102 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003103
Omar Sandoval6f673762015-03-16 04:33:52 -07003104 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003105 if (ret > 0)
3106 fuse_write_update_size(inode, pos);
3107 else if (ret < 0 && offset + count > i_size)
3108 fuse_do_truncate(file);
3109 }
Anand Avati4273b792012-02-17 12:46:25 -05003110
3111 return ret;
3112}
3113
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003114static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3115{
3116 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3117
3118 if (!err)
3119 fuse_sync_writes(inode);
3120
3121 return err;
3122}
3123
Miklos Szeredicdadb112012-11-10 16:55:56 +01003124static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3125 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003126{
3127 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003128 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003129 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003130 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01003131 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003132 struct fuse_fallocate_in inarg = {
3133 .fh = ff->fh,
3134 .offset = offset,
3135 .length = length,
3136 .mode = mode
3137 };
3138 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003139 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3140 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003141
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003142 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3143 return -EOPNOTSUPP;
3144
Miklos Szeredi519c6042012-04-26 10:56:36 +02003145 if (fc->no_fallocate)
3146 return -EOPNOTSUPP;
3147
Maxim Patlasov14c14412013-06-13 12:16:39 +04003148 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003149 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003150 if (mode & FALLOC_FL_PUNCH_HOLE) {
3151 loff_t endbyte = offset + length - 1;
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003152
3153 err = fuse_writeback_range(inode, offset, endbyte);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003154 if (err)
3155 goto out;
Maxim Patlasovbde52782013-09-13 19:19:54 +04003156 }
Brian Foster3634a632013-05-17 09:30:32 -04003157 }
3158
Liu Bo0cbade02019-04-18 04:04:41 +08003159 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3160 offset + length > i_size_read(inode)) {
3161 err = inode_newsize_ok(inode, offset + length);
3162 if (err)
Miklos Szeredi35d6fcb2019-05-27 11:42:07 +02003163 goto out;
Liu Bo0cbade02019-04-18 04:04:41 +08003164 }
3165
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003166 if (!(mode & FALLOC_FL_KEEP_SIZE))
3167 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3168
Miklos Szeredid5b48542019-09-10 15:04:08 +02003169 args.opcode = FUSE_FALLOCATE;
3170 args.nodeid = ff->nodeid;
3171 args.in_numargs = 1;
3172 args.in_args[0].size = sizeof(inarg);
3173 args.in_args[0].value = &inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01003174 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003175 if (err == -ENOSYS) {
3176 fc->no_fallocate = 1;
3177 err = -EOPNOTSUPP;
3178 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003179 if (err)
3180 goto out;
3181
3182 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003183 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3184 bool changed = fuse_write_update_size(inode, offset + length);
3185
Miklos Szeredi93d22692014-04-28 14:19:22 +02003186 if (changed && fc->writeback_cache)
3187 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003188 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003189
3190 if (mode & FALLOC_FL_PUNCH_HOLE)
3191 truncate_pagecache_range(inode, offset, offset + length - 1);
3192
3193 fuse_invalidate_attr(inode);
3194
Brian Foster3634a632013-05-17 09:30:32 -04003195out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003196 if (!(mode & FALLOC_FL_KEEP_SIZE))
3197 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3198
Maxim Patlasovbde52782013-09-13 19:19:54 +04003199 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003200 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003201
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003202 return err;
3203}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003204
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003205static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3206 struct file *file_out, loff_t pos_out,
3207 size_t len, unsigned int flags)
Niels de Vos88bc7d52018-08-21 14:36:31 +02003208{
3209 struct fuse_file *ff_in = file_in->private_data;
3210 struct fuse_file *ff_out = file_out->private_data;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003211 struct inode *inode_in = file_inode(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003212 struct inode *inode_out = file_inode(file_out);
3213 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3214 struct fuse_conn *fc = ff_in->fc;
3215 FUSE_ARGS(args);
3216 struct fuse_copy_file_range_in inarg = {
3217 .fh_in = ff_in->fh,
3218 .off_in = pos_in,
3219 .nodeid_out = ff_out->nodeid,
3220 .fh_out = ff_out->fh,
3221 .off_out = pos_out,
3222 .len = len,
3223 .flags = flags
3224 };
3225 struct fuse_write_out outarg;
3226 ssize_t err;
3227 /* mark unstable when write-back is not used, and file_out gets
3228 * extended */
3229 bool is_unstable = (!fc->writeback_cache) &&
3230 ((pos_out + len) > inode_out->i_size);
3231
3232 if (fc->no_copy_file_range)
3233 return -EOPNOTSUPP;
3234
Amir Goldstein5dae2222019-06-05 08:04:50 -07003235 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3236 return -EXDEV;
3237
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003238 if (fc->writeback_cache) {
3239 inode_lock(inode_in);
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003240 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003241 inode_unlock(inode_in);
3242 if (err)
3243 return err;
3244 }
3245
Niels de Vos88bc7d52018-08-21 14:36:31 +02003246 inode_lock(inode_out);
3247
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003248 err = file_modified(file_out);
3249 if (err)
3250 goto out;
3251
Niels de Vos88bc7d52018-08-21 14:36:31 +02003252 if (fc->writeback_cache) {
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003253 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003254 if (err)
3255 goto out;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003256 }
3257
3258 if (is_unstable)
3259 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3260
Miklos Szeredid5b48542019-09-10 15:04:08 +02003261 args.opcode = FUSE_COPY_FILE_RANGE;
3262 args.nodeid = ff_in->nodeid;
3263 args.in_numargs = 1;
3264 args.in_args[0].size = sizeof(inarg);
3265 args.in_args[0].value = &inarg;
3266 args.out_numargs = 1;
3267 args.out_args[0].size = sizeof(outarg);
3268 args.out_args[0].value = &outarg;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003269 err = fuse_simple_request(fc, &args);
3270 if (err == -ENOSYS) {
3271 fc->no_copy_file_range = 1;
3272 err = -EOPNOTSUPP;
3273 }
3274 if (err)
3275 goto out;
3276
3277 if (fc->writeback_cache) {
3278 fuse_write_update_size(inode_out, pos_out + outarg.size);
3279 file_update_time(file_out);
3280 }
3281
3282 fuse_invalidate_attr(inode_out);
3283
3284 err = outarg.size;
3285out:
3286 if (is_unstable)
3287 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3288
3289 inode_unlock(inode_out);
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003290 file_accessed(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003291
3292 return err;
3293}
3294
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003295static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3296 struct file *dst_file, loff_t dst_off,
3297 size_t len, unsigned int flags)
3298{
3299 ssize_t ret;
3300
3301 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3302 len, flags);
3303
Amir Goldstein5dae2222019-06-05 08:04:50 -07003304 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003305 ret = generic_copy_file_range(src_file, src_off, dst_file,
3306 dst_off, len, flags);
3307 return ret;
3308}
3309
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003310static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003311 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003312 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003313 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003314 .mmap = fuse_file_mmap,
3315 .open = fuse_open,
3316 .flush = fuse_flush,
3317 .release = fuse_release,
3318 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003319 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003320 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003321 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003322 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003323 .unlocked_ioctl = fuse_file_ioctl,
3324 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003325 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003326 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003327 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003328};
3329
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003330static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003331 .readpage = fuse_readpage,
Matthew Wilcox (Oracle)76a02942020-06-01 21:47:31 -07003332 .readahead = fuse_readahead,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003333 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003334 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003335 .launder_page = fuse_launder_page,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003336 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003337 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003338 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003339 .write_begin = fuse_write_begin,
3340 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003341};
3342
3343void fuse_init_file_inode(struct inode *inode)
3344{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003345 struct fuse_inode *fi = get_fuse_inode(inode);
3346
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003347 inode->i_fop = &fuse_file_operations;
3348 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003349
3350 INIT_LIST_HEAD(&fi->write_files);
3351 INIT_LIST_HEAD(&fi->queued_writes);
3352 fi->writectr = 0;
3353 init_waitqueue_head(&fi->page_waitq);
3354 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003355}