blob: 06096b60f1df23cb60234fe2fe3ce2ddc2b39c4e [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 Szeredi91fe96b2009-04-28 16:56:37 +020022static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070024{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010026 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080027
28 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070029 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
30 if (!fc->atomic_o_trunc)
31 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010032 args.in.h.opcode = opcode;
33 args.in.h.nodeid = nodeid;
34 args.in.numargs = 1;
35 args.in.args[0].size = sizeof(inarg);
36 args.in.args[0].value = &inarg;
37 args.out.numargs = 1;
38 args.out.args[0].size = sizeof(*outargp);
39 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080040
Miklos Szeredi70781872014-12-12 09:49:05 +010041 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080042}
43
Tejun Heoacf99432008-11-26 12:03:55 +010044struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080045{
46 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090047
Mateusz Jurczyk68227c02017-06-07 12:26:49 +020048 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090049 if (unlikely(!ff))
50 return NULL;
51
Miklos Szeredida5e4712009-04-28 16:56:36 +020052 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040053 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090054 if (unlikely(!ff->reserved_req)) {
55 kfree(ff);
56 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080057 }
Tejun Heo6b2db282009-04-14 10:54:49 +090058
59 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020060 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020061 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090062 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
64
Miklos Szeredi75126f52019-01-24 10:40:17 +010065 ff->kh = atomic64_inc_return(&fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090066
Miklos Szeredifd72faa2005-11-07 00:59:51 -080067 return ff;
68}
69
70void fuse_file_free(struct fuse_file *ff)
71{
Miklos Szeredi33649c92006-06-25 05:48:52 -070072 fuse_request_free(ff->reserved_req);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020073 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080074 kfree(ff);
75}
76
Miklos Szeredi267d8442017-02-22 20:08:25 +010077static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070078{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020079 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070080 return ff;
81}
82
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010083static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
84{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010085 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010086}
87
Chad Austin2e64ff12018-12-10 10:54:52 -080088static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070089{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020090 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -070091 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020092
Chad Austind9a9ea92019-01-07 16:53:17 -080093 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +010094 /*
95 * Drop the release request when client does not
96 * implement 'open'
97 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +020098 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +010099 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100100 fuse_put_request(ff->fc, req);
101 } else if (sync) {
Miklos Szeredi2e38bea2017-02-22 20:08:25 +0100102 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200103 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100105 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106 fuse_put_request(ff->fc, req);
107 } else {
108 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200109 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100110 fuse_request_send_background(ff->fc, req);
111 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700112 kfree(ff);
113 }
114}
115
Tejun Heo08cbf542009-04-14 10:54:53 +0900116int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
117 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200118{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200119 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
121
122 ff = fuse_file_alloc(fc);
123 if (!ff)
124 return -ENOMEM;
125
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100126 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800127 /* Default for no-open */
128 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800129 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 struct fuse_open_out outarg;
131 int err;
132
133 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
134 if (!err) {
135 ff->fh = outarg.fh;
136 ff->open_flags = outarg.open_flags;
137
Chad Austind9a9ea92019-01-07 16:53:17 -0800138 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100139 fuse_file_free(ff);
140 return err;
141 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800142 if (isdir)
143 fc->no_opendir = 1;
144 else
145 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100146 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200147 }
148
149 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100150 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200151
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200152 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100153 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154
155 return 0;
156}
Tejun Heo08cbf542009-04-14 10:54:53 +0900157EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200158
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400159static void fuse_link_write_file(struct file *file)
160{
161 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400162 struct fuse_inode *fi = get_fuse_inode(inode);
163 struct fuse_file *ff = file->private_data;
164 /*
165 * file may be written through mmap, so chain it onto the
166 * inodes's write_file list
167 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300168 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400169 if (list_empty(&ff->write_entry))
170 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300171 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400172}
173
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800175{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800177 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700180 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200181 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200182 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800183 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
184 struct fuse_inode *fi = get_fuse_inode(inode);
185
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300186 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300187 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800188 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300189 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800190 fuse_invalidate_attr(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200191 if (fc->writeback_cache)
192 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800193 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400194 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
195 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800196}
197
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200198int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800199{
Tejun Heoacf99432008-11-26 12:03:55 +0100200 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 int err;
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200202 bool lock_inode = (file->f_flags & O_TRUNC) &&
203 fc->atomic_o_trunc &&
204 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700205
206 err = generic_file_open(inode, file);
207 if (err)
208 return err;
209
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200210 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500211 inode_lock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200212
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200213 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700214
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200215 if (!err)
216 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200217
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200218 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500219 inode_unlock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200220
221 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222}
223
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300224static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
225 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800226{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200227 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700228 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800229 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700230
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300231 /* Inode is NULL on error path of fuse_create_open() */
232 if (likely(fi)) {
233 spin_lock(&fi->lock);
234 list_del(&ff->write_entry);
235 spin_unlock(&fi->lock);
236 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200237 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200238 if (!RB_EMPTY_NODE(&ff->polled_node))
239 rb_erase(&ff->polled_node, &fc->polled_files);
240 spin_unlock(&fc->lock);
241
Bryan Green357ccf22011-03-01 16:43:52 -0800242 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200243
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800245 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700246 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200247 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700248 req->in.numargs = 1;
249 req->in.args[0].size = sizeof(struct fuse_release_in);
250 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800251}
252
Chad Austin2e64ff12018-12-10 10:54:52 -0800253void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800254{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300255 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100256 struct fuse_file *ff = file->private_data;
257 struct fuse_req *req = ff->reserved_req;
Chad Austin2e64ff12018-12-10 10:54:52 -0800258 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700259
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300260 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100261
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200262 if (ff->flock) {
263 struct fuse_release_in *inarg = &req->misc.release.in;
264 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
265 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
266 (fl_owner_t) file);
267 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100268 /* Hold inode until release is finished */
269 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700270
Tejun Heo6b2db282009-04-14 10:54:49 +0900271 /*
272 * Normally this will send the RELEASE request, however if
273 * some asynchronous READ or WRITE requests are outstanding,
274 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100275 *
276 * Make the release synchronous if this is a fuseblk mount,
277 * synchronous RELEASE is allowed (and desirable) in this case
278 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900279 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800280 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700281}
282
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700283static int fuse_open(struct inode *inode, struct file *file)
284{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200285 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700286}
287
288static int fuse_release(struct inode *inode, struct file *file)
289{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400290 struct fuse_conn *fc = get_fuse_conn(inode);
291
292 /* see fuse_vma_close() for !writeback_cache case */
293 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200294 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400295
Chad Austin2e64ff12018-12-10 10:54:52 -0800296 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200297
298 /* return value is ignored by VFS */
299 return 0;
300}
301
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300302void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200303{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200304 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300305 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100306 /*
307 * iput(NULL) is a no-op and since the refcount is 1 and everything's
308 * synchronous, we are fine with not doing igrab() here"
309 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800310 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700311}
Tejun Heo08cbf542009-04-14 10:54:53 +0900312EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700313
Miklos Szeredi71421252006-06-25 05:48:52 -0700314/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700315 * Scramble the ID space with XTEA, so that the value of the files_struct
316 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700317 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700318u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700319{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700320 u32 *k = fc->scramble_key;
321 u64 v = (unsigned long) id;
322 u32 v0 = v;
323 u32 v1 = v >> 32;
324 u32 sum = 0;
325 int i;
326
327 for (i = 0; i < 32; i++) {
328 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
329 sum += 0x9E3779B9;
330 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
331 }
332
333 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700334}
335
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100336static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
337 pgoff_t idx_from, pgoff_t idx_to)
338{
339 struct fuse_req *req;
340
341 list_for_each_entry(req, &fi->writepages, writepages_entry) {
342 pgoff_t curr_index;
343
344 WARN_ON(get_fuse_inode(req->inode) != fi);
345 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
346 if (idx_from < curr_index + req->num_pages &&
347 curr_index <= idx_to) {
348 return req;
349 }
350 }
351 return NULL;
352}
353
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700354/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400355 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700356 *
357 * This is currently done by walking the list of writepage requests
358 * for the inode, which can be pretty inefficient.
359 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400360static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
361 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700362{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700363 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100364 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700365
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300366 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100367 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300368 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700369
370 return found;
371}
372
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400373static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
374{
375 return fuse_range_is_writeback(inode, index, index);
376}
377
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700378/*
379 * Wait for page writeback to be completed.
380 *
381 * Since fuse doesn't rely on the VM writeback tracking, this has to
382 * use some other means.
383 */
384static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
385{
386 struct fuse_inode *fi = get_fuse_inode(inode);
387
388 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
389 return 0;
390}
391
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400392/*
393 * Wait for all pending writepages on the inode to finish.
394 *
395 * This is currently done by blocking further writes with FUSE_NOWRITE
396 * and waiting for all sent writes to complete.
397 *
398 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
399 * could conflict with truncation.
400 */
401static void fuse_sync_writes(struct inode *inode)
402{
403 fuse_set_nowrite(inode);
404 fuse_release_nowrite(inode);
405}
406
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700407static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700408{
Al Viro6131ffa2013-02-27 16:59:05 -0500409 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700410 struct fuse_conn *fc = get_fuse_conn(inode);
411 struct fuse_file *ff = file->private_data;
412 struct fuse_req *req;
413 struct fuse_flush_in inarg;
414 int err;
415
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800416 if (is_bad_inode(inode))
417 return -EIO;
418
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700419 if (fc->no_flush)
420 return 0;
421
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200422 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400423 if (err)
424 return err;
425
Al Viro59551022016-01-22 15:40:57 -0500426 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400427 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500428 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400429
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200430 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700431 if (err)
432 return err;
433
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400434 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435 memset(&inarg, 0, sizeof(inarg));
436 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700437 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700438 req->in.h.opcode = FUSE_FLUSH;
439 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 req->in.numargs = 1;
441 req->in.args[0].size = sizeof(inarg);
442 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200443 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100444 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700445 err = req->out.h.error;
446 fuse_put_request(fc, req);
447 if (err == -ENOSYS) {
448 fc->no_flush = 1;
449 err = 0;
450 }
451 return err;
452}
453
Josef Bacik02c24a82011-07-16 20:44:56 -0400454int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100455 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200457 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 struct fuse_conn *fc = get_fuse_conn(inode);
459 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100460 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100462
463 memset(&inarg, 0, sizeof(inarg));
464 inarg.fh = ff->fh;
465 inarg.fsync_flags = datasync ? 1 : 0;
466 args.in.h.opcode = opcode;
467 args.in.h.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 return fuse_simple_request(fc, &args);
472}
473
474static int fuse_fsync(struct file *file, loff_t start, loff_t end,
475 int datasync)
476{
477 struct inode *inode = file->f_mapping->host;
478 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700479 int err;
480
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800481 if (is_bad_inode(inode))
482 return -EIO;
483
Al Viro59551022016-01-22 15:40:57 -0500484 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400485
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700486 /*
487 * Start writeback against all dirty pages of the inode, then
488 * wait for all outstanding writes, before sending the FSYNC
489 * request.
490 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400491 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400493 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700494
495 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700496
497 /*
498 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400499 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700500 * We have to do this directly after fuse_sync_writes()
501 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400502 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700503 if (err)
504 goto out;
505
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200506 err = sync_inode_metadata(inode, 1);
507 if (err)
508 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700509
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100510 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200511 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400512
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100513 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100515 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516 err = 0;
517 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400518out:
Al Viro59551022016-01-22 15:40:57 -0500519 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700520
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100521 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700522}
523
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200524void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
525 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700527 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800528 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800530 inarg->fh = ff->fh;
531 inarg->offset = pos;
532 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800533 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800534 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200535 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700536 req->in.numargs = 1;
537 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800538 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700539 req->out.argvar = 1;
540 req->out.numargs = 1;
541 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700542}
543
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200544static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400545{
546 unsigned i;
547
548 for (i = 0; i < req->num_pages; i++) {
549 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200550 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400551 set_page_dirty_lock(page);
552 put_page(page);
553 }
554}
555
Seth Forshee744742d2016-03-11 10:35:34 -0600556static void fuse_io_release(struct kref *kref)
557{
558 kfree(container_of(kref, struct fuse_io_priv, refcnt));
559}
560
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100561static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
562{
563 if (io->err)
564 return io->err;
565
566 if (io->bytes >= 0 && io->write)
567 return -EIO;
568
569 return io->bytes < 0 ? io->size : io->bytes;
570}
571
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400572/**
573 * In case of short read, the caller sets 'pos' to the position of
574 * actual end of fuse request in IO request. Otherwise, if bytes_requested
575 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
576 *
577 * An example:
578 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
579 * both submitted asynchronously. The first of them was ACKed by userspace as
580 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
581 * second request was ACKed as short, e.g. only 1K was read, resulting in
582 * pos == 33K.
583 *
584 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
585 * will be equal to the length of the longest contiguous fragment of
586 * transferred data starting from the beginning of IO request.
587 */
588static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
589{
590 int left;
591
592 spin_lock(&io->lock);
593 if (err)
594 io->err = io->err ? : err;
595 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
596 io->bytes = pos;
597
598 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530599 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100600 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400601 spin_unlock(&io->lock);
602
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530603 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100604 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400605
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100606 if (res >= 0) {
607 struct inode *inode = file_inode(io->iocb->ki_filp);
608 struct fuse_conn *fc = get_fuse_conn(inode);
609 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400610
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300611 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300612 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300613 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400614 }
615
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100616 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400617 }
Seth Forshee744742d2016-03-11 10:35:34 -0600618
619 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400620}
621
622static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
623{
624 struct fuse_io_priv *io = req->io;
625 ssize_t pos = -1;
626
Ashish Samant61c12b42017-07-12 19:26:58 -0700627 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400628
629 if (io->write) {
630 if (req->misc.write.in.size != req->misc.write.out.size)
631 pos = req->misc.write.in.offset - io->offset +
632 req->misc.write.out.size;
633 } else {
634 if (req->misc.read.in.size != req->out.args[0].size)
635 pos = req->misc.read.in.offset - io->offset +
636 req->out.args[0].size;
637 }
638
639 fuse_aio_complete(io, req->out.h.error, pos);
640}
641
642static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
643 size_t num_bytes, struct fuse_io_priv *io)
644{
645 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600646 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400647 io->size += num_bytes;
648 io->reqs++;
649 spin_unlock(&io->lock);
650
651 req->io = io;
652 req->end = fuse_aio_complete_req;
653
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400654 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400655 fuse_request_send_background(fc, req);
656
657 return num_bytes;
658}
659
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400660static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200661 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700662{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200663 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200664 struct fuse_file *ff = file->private_data;
665 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700666
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200667 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700668 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700670
671 inarg->read_flags |= FUSE_READ_LOCKOWNER;
672 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
673 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400674
675 if (io->async)
676 return fuse_async_req_send(fc, req, count, io);
677
Tejun Heob93f8582008-11-26 12:03:55 +0100678 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800679 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700680}
681
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700682static void fuse_read_update_size(struct inode *inode, loff_t size,
683 u64 attr_ver)
684{
685 struct fuse_conn *fc = get_fuse_conn(inode);
686 struct fuse_inode *fi = get_fuse_inode(inode);
687
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300688 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400689 if (attr_ver == fi->attr_version && size < inode->i_size &&
690 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300691 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700692 i_size_write(inode, size);
693 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300694 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700695}
696
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400697static void fuse_short_read(struct fuse_req *req, struct inode *inode,
698 u64 attr_ver)
699{
700 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400702
Pavel Emelyanov83732002013-10-10 17:10:46 +0400703 if (fc->writeback_cache) {
704 /*
705 * A hole in a file. Some data after the hole are in page cache,
706 * but have not reached the client fs yet. So, the hole is not
707 * present there.
708 */
709 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300710 int start_idx = num_read >> PAGE_SHIFT;
711 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400712
713 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300714 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400715 off = 0;
716 }
717 } else {
718 loff_t pos = page_offset(req->pages[0]) + num_read;
719 fuse_read_update_size(inode, pos, attr_ver);
720 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400721}
722
Maxim Patlasov482fce52013-10-10 17:11:25 +0400723static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700724{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200725 struct kiocb iocb;
726 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700727 struct inode *inode = page->mapping->host;
728 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800729 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700730 size_t num_read;
731 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300732 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700733 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800734 int err;
735
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700736 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300737 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700738 * page-cache page, so make sure we read a properly synced
739 * page.
740 */
741 fuse_wait_on_page_writeback(inode, page->index);
742
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400743 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700744 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400745 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700746
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747 attr_ver = fuse_get_attr_version(fc);
748
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700749 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200750 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700751 req->num_pages = 1;
752 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400753 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200754 init_sync_kiocb(&iocb, file);
755 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400756 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700757 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700758
759 if (!err) {
760 /*
761 * Short read means EOF. If file size is larger, truncate it
762 */
763 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400764 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700765
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700766 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700767 }
768
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400769 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400770
771 return err;
772}
773
774static int fuse_readpage(struct file *file, struct page *page)
775{
776 struct inode *inode = page->mapping->host;
777 int err;
778
779 err = -EIO;
780 if (is_bad_inode(inode))
781 goto out;
782
783 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800784 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700785 out:
786 unlock_page(page);
787 return err;
788}
789
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800790static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700791{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800792 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700793 size_t count = req->misc.read.in.size;
794 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800796
Miklos Szeredice534fb2010-05-25 15:06:07 +0200797 for (i = 0; mapping == NULL && i < req->num_pages; i++)
798 mapping = req->pages[i]->mapping;
799
800 if (mapping) {
801 struct inode *inode = mapping->host;
802
803 /*
804 * Short read means EOF. If file size is larger, truncate it
805 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400806 if (!req->out.h.error && num_read < count)
807 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200808
Andrew Gallagher451418f2013-11-05 03:55:43 -0800809 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700810 }
811
Miklos Szeredidb50b962005-09-09 13:10:33 -0700812 for (i = 0; i < req->num_pages; i++) {
813 struct page *page = req->pages[i];
814 if (!req->out.h.error)
815 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800816 else
817 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700818 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700820 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700821 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -0800822 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800823}
824
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200825static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800826{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200827 struct fuse_file *ff = file->private_data;
828 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800829 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300830 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200831
832 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800833 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200834 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200835 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700836 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800837 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700838 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800839 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100840 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800841 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100842 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800843 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100844 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800845 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700846}
847
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700848struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700849 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800850 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400852 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853};
854
855static int fuse_readpages_fill(void *_data, struct page *page)
856{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700857 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858 struct fuse_req *req = data->req;
859 struct inode *inode = data->inode;
860 struct fuse_conn *fc = get_fuse_conn(inode);
861
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700862 fuse_wait_on_page_writeback(inode, page->index);
863
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300865 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300866 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300868 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
869 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200870 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400871 if (fc->async_read)
872 req = fuse_get_req_for_background(fc, nr_alloc);
873 else
874 req = fuse_get_req(fc, nr_alloc);
875
876 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700877 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700879 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700881 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400882
883 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300884 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400885 fuse_put_request(fc, req);
886 return -EIO;
887 }
888
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300889 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400891 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100892 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400893 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700894 return 0;
895}
896
897static int fuse_readpages(struct file *file, struct address_space *mapping,
898 struct list_head *pages, unsigned nr_pages)
899{
900 struct inode *inode = mapping->host;
901 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700902 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700903 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300904 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800905
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700906 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800907 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800908 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800909
Miklos Szeredia6643092007-11-28 16:22:00 -0800910 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700911 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400912 if (fc->async_read)
913 data.req = fuse_get_req_for_background(fc, nr_alloc);
914 else
915 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400916 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700917 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700918 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800919 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700920
921 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700922 if (!err) {
923 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200924 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700925 else
926 fuse_put_request(fc, data.req);
927 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800928out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700929 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700930}
931
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100932static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933{
934 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400935 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936
Brian Fostera8894272012-07-16 15:23:50 -0400937 /*
938 * In auto invalidate mode, always update attributes on read.
939 * Otherwise, only update if we attempt to read past EOF (to ensure
940 * i_size is up to date).
941 */
942 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400943 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800944 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200945 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800946 if (err)
947 return err;
948 }
949
Al Viro37c20f12014-04-02 14:47:09 -0400950 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800951}
952
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200953static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200954 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700956 struct fuse_write_in *inarg = &req->misc.write.in;
957 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700958
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700959 inarg->fh = ff->fh;
960 inarg->offset = pos;
961 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700962 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200963 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700964 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200965 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700966 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
967 else
968 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700969 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700970 req->in.args[1].size = count;
971 req->out.numargs = 1;
972 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700973 req->out.args[0].value = outarg;
974}
975
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400976static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200977 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700978{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200979 struct kiocb *iocb = io->iocb;
980 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200981 struct fuse_file *ff = file->private_data;
982 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200983 struct fuse_write_in *inarg = &req->misc.write.in;
984
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200985 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200986 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200987 if (iocb->ki_flags & IOCB_DSYNC)
988 inarg->flags |= O_DSYNC;
989 if (iocb->ki_flags & IOCB_SYNC)
990 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700991 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700992 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
993 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
994 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400995
996 if (io->async)
997 return fuse_async_req_send(fc, req, count, io);
998
Tejun Heob93f8582008-11-26 12:03:55 +0100999 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001000 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001001}
1002
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004{
1005 struct fuse_conn *fc = get_fuse_conn(inode);
1006 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001007 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001009 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001010 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001011 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001012 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013 ret = true;
1014 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001015 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001016
1017 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001018}
1019
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001020static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001021 struct inode *inode, loff_t pos,
1022 size_t count)
1023{
1024 size_t res;
1025 unsigned offset;
1026 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001027 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001028
1029 for (i = 0; i < req->num_pages; i++)
1030 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1031
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001032 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001033
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001034 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001035 count = res;
1036 for (i = 0; i < req->num_pages; i++) {
1037 struct page *page = req->pages[i];
1038
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001039 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001040 SetPageUptodate(page);
1041
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001042 if (count > PAGE_SIZE - offset)
1043 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001044 else
1045 count = 0;
1046 offset = 0;
1047
1048 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001049 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001050 }
1051
1052 return res;
1053}
1054
1055static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1056 struct address_space *mapping,
1057 struct iov_iter *ii, loff_t pos)
1058{
1059 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001060 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001061 size_t count = 0;
1062 int err;
1063
Miklos Szeredif4975c62009-04-02 14:25:34 +02001064 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001065 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001066
1067 do {
1068 size_t tmp;
1069 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001070 pgoff_t index = pos >> PAGE_SHIFT;
1071 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001072 iov_iter_count(ii));
1073
1074 bytes = min_t(size_t, bytes, fc->max_write - count);
1075
1076 again:
1077 err = -EFAULT;
1078 if (iov_iter_fault_in_readable(ii, bytes))
1079 break;
1080
1081 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001082 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 if (!page)
1084 break;
1085
anfei zhou931e80e2010-02-02 13:44:02 -08001086 if (mapping_writably_mapped(mapping))
1087 flush_dcache_page(page);
1088
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 flush_dcache_page(page);
1091
Roman Gushchin3ca81382015-10-12 16:33:44 +03001092 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 if (!tmp) {
1094 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001095 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 bytes = min(bytes, iov_iter_single_seg_count(ii));
1097 goto again;
1098 }
1099
1100 err = 0;
1101 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001102 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001103 req->num_pages++;
1104
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 count += tmp;
1106 pos += tmp;
1107 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001108 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 offset = 0;
1110
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001111 if (!fc->big_writes)
1112 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001113 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001114 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001115
1116 return count > 0 ? count : err;
1117}
1118
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001119static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1120 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001121{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001122 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001123 ((pos + len - 1) >> PAGE_SHIFT) -
1124 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001125 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001126}
1127
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001128static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001129 struct address_space *mapping,
1130 struct iov_iter *ii, loff_t pos)
1131{
1132 struct inode *inode = mapping->host;
1133 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001134 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001135 int err = 0;
1136 ssize_t res = 0;
1137
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001138 if (inode->i_size < pos + iov_iter_count(ii))
1139 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1140
Nick Pigginea9b9902008-04-30 00:54:42 -07001141 do {
1142 struct fuse_req *req;
1143 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001144 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1145 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001146
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001147 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001148 if (IS_ERR(req)) {
1149 err = PTR_ERR(req);
1150 break;
1151 }
1152
1153 count = fuse_fill_write_pages(req, mapping, ii, pos);
1154 if (count <= 0) {
1155 err = count;
1156 } else {
1157 size_t num_written;
1158
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001159 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001160 pos, count);
1161 err = req->out.h.error;
1162 if (!err) {
1163 res += num_written;
1164 pos += num_written;
1165
1166 /* break out of the loop on short write */
1167 if (num_written != count)
1168 err = -EIO;
1169 }
1170 }
1171 fuse_put_request(fc, req);
1172 } while (!err && iov_iter_count(ii));
1173
1174 if (res > 0)
1175 fuse_write_update_size(inode, pos);
1176
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001177 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001178 fuse_invalidate_attr(inode);
1179
1180 return res > 0 ? res : err;
1181}
1182
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001183static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001184{
1185 struct file *file = iocb->ki_filp;
1186 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001187 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001188 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001189 struct inode *inode = mapping->host;
1190 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001191 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001192
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001193 if (get_fuse_conn(inode)->writeback_cache) {
1194 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001195 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001196 if (err)
1197 return err;
1198
Al Viro84c3d552014-04-03 14:33:23 -04001199 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001200 }
1201
Al Viro59551022016-01-22 15:40:57 -05001202 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001203
1204 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001205 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001206
Al Viro3309dd02015-04-09 12:55:47 -04001207 err = generic_write_checks(iocb, from);
1208 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001209 goto out;
1210
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001211 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001212 if (err)
1213 goto out;
1214
Josef Bacikc3b2da32012-03-26 09:59:21 -04001215 err = file_update_time(file);
1216 if (err)
1217 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001218
Al Viro2ba48ce2015-04-09 13:52:01 -04001219 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001220 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001221 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001222 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001223 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001224
Anand Avati4273b792012-02-17 12:46:25 -05001225 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001226
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001227 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001228 if (written_buffered < 0) {
1229 err = written_buffered;
1230 goto out;
1231 }
1232 endbyte = pos + written_buffered - 1;
1233
1234 err = filemap_write_and_wait_range(file->f_mapping, pos,
1235 endbyte);
1236 if (err)
1237 goto out;
1238
1239 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001240 pos >> PAGE_SHIFT,
1241 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001242
1243 written += written_buffered;
1244 iocb->ki_pos = pos + written_buffered;
1245 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001246 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001247 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001248 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001249 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001250out:
1251 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001252 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001253 if (written > 0)
1254 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001255
1256 return written ? written : err;
1257}
1258
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001259static inline void fuse_page_descs_length_init(struct fuse_req *req,
1260 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001261{
1262 int i;
1263
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001264 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001265 req->page_descs[i].length = PAGE_SIZE -
1266 req->page_descs[i].offset;
1267}
1268
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1270{
1271 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1272}
1273
1274static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1275 size_t max_size)
1276{
1277 return min(iov_iter_single_seg_count(ii), max_size);
1278}
1279
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001280static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001281 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001282{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001283 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001284 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001285
Miklos Szeredif4975c62009-04-02 14:25:34 +02001286 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001287 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 unsigned long user_addr = fuse_get_user_addr(ii);
1289 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1290
Miklos Szeredif4975c62009-04-02 14:25:34 +02001291 if (write)
1292 req->in.args[1].value = (void *) user_addr;
1293 else
1294 req->out.args[0].value = (void *) user_addr;
1295
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001296 iov_iter_advance(ii, frag_size);
1297 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001298 return 0;
1299 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001300
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001301 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001302 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001303 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001304 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001305 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001306 req->max_pages - req->num_pages,
1307 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001308 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001309 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310
Al Viroc9c37e22014-03-16 16:08:30 -04001311 iov_iter_advance(ii, ret);
1312 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001313
Al Viroc9c37e22014-03-16 16:08:30 -04001314 ret += start;
1315 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1316
1317 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001318 fuse_page_descs_length_init(req, req->num_pages, npages);
1319
1320 req->num_pages += npages;
1321 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001322 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001323 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001324
1325 if (write)
1326 req->in.argpages = 1;
1327 else
1328 req->out.argpages = 1;
1329
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001330 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001331
Ashish Samant2c932d42016-03-25 10:53:41 -07001332 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001333}
1334
Al Virod22a9432014-03-16 15:50:47 -04001335ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1336 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001337{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001338 int write = flags & FUSE_DIO_WRITE;
1339 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001340 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001341 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001342 struct fuse_file *ff = file->private_data;
1343 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001344 size_t nmax = write ? fc->max_write : fc->max_read;
1345 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001346 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001347 pgoff_t idx_from = pos >> PAGE_SHIFT;
1348 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001349 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001350 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001351 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001352
Brian Fosterde82b922013-05-14 11:25:39 -04001353 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001354 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1355 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001356 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001357 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001358 if (IS_ERR(req))
1359 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001361 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1362 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001363 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001364 fuse_sync_writes(inode);
1365 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001366 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001367 }
1368
Ashish Samant61c12b42017-07-12 19:26:58 -07001369 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001372 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001373 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001374 err = fuse_get_user_pages(req, iter, &nbytes, write);
1375 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001377
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001379 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001381 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001382
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001383 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001384 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001385 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001386 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387 break;
1388 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001389 res = 0;
1390 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 break;
1392 }
1393 count -= nres;
1394 res += nres;
1395 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396 if (nres != nbytes)
1397 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001398 if (count) {
1399 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001400 if (io->async)
1401 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001402 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001403 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001404 req = fuse_get_req(fc, iov_iter_npages(iter,
1405 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001406 if (IS_ERR(req))
1407 break;
1408 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001410 if (!IS_ERR(req))
1411 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001412 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001414
Ashish Samant742f9922016-03-14 21:57:35 -07001415 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001416}
Tejun Heo08cbf542009-04-14 10:54:53 +09001417EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001419static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001420 struct iov_iter *iter,
1421 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001422{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001423 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001424 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001425
Al Virod22a9432014-03-16 15:50:47 -04001426 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001427
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001428 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001429
1430 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001431}
1432
Martin Raiber23c94e12018-10-27 16:48:48 +00001433static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1434
Al Viro153162632015-03-30 22:08:36 -04001435static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001436{
Martin Raiber23c94e12018-10-27 16:48:48 +00001437 ssize_t res;
1438
1439 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001440 res = fuse_direct_IO(iocb, to);
1441 } else {
1442 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1443
1444 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1445 }
1446
1447 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001448}
1449
Al Viro153162632015-03-30 22:08:36 -04001450static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001451{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001452 struct inode *inode = file_inode(iocb->ki_filp);
1453 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001454 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001455
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001456 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001457 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001458 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001459 if (res > 0) {
1460 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1461 res = fuse_direct_IO(iocb, from);
1462 } else {
1463 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1464 FUSE_DIO_WRITE);
1465 }
1466 }
Al Viro812408f2015-03-30 22:15:58 -04001467 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001468 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001469 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001470 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001471
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001472 return res;
1473}
1474
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001475static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1476{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001477 struct file *file = iocb->ki_filp;
1478 struct fuse_file *ff = file->private_data;
1479
1480 if (is_bad_inode(file_inode(file)))
1481 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001482
1483 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1484 return fuse_cache_read_iter(iocb, to);
1485 else
1486 return fuse_direct_read_iter(iocb, to);
1487}
1488
1489static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1490{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001491 struct file *file = iocb->ki_filp;
1492 struct fuse_file *ff = file->private_data;
1493
1494 if (is_bad_inode(file_inode(file)))
1495 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001496
1497 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1498 return fuse_cache_write_iter(iocb, from);
1499 else
1500 return fuse_direct_write_iter(iocb, from);
1501}
1502
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001503static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001504{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001505 int i;
1506
1507 for (i = 0; i < req->num_pages; i++)
1508 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001509
1510 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001511 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001512}
1513
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001514static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001515{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001516 struct inode *inode = req->inode;
1517 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001518 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001519 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001520
1521 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001522 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001523 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001524 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001525 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001526 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001527 wake_up(&fi->page_waitq);
1528}
1529
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001530/* Called under fi->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001531static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1532 loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001533__releases(fi->lock)
1534__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001535{
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001536 struct fuse_req *aux, *next;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001538 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001539 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001540 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001542 if (inarg->offset + data_size <= size) {
1543 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001544 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001545 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001546 } else {
1547 /* Got truncated off completely */
1548 goto out_free;
1549 }
1550
1551 req->in.args[1].size = inarg->size;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001552 queued = fuse_request_queue_background(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001553 /* Fails on broken connection only */
1554 if (unlikely(!queued))
1555 goto out_free;
1556
1557 fi->writectr++;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001558 return;
1559
1560 out_free:
1561 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001562 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001563
1564 /* After fuse_writepage_finish() aux request list is private */
1565 for (aux = req->misc.write.next; aux; aux = next) {
1566 next = aux->misc.write.next;
1567 aux->misc.write.next = NULL;
1568 fuse_writepage_free(fc, aux);
1569 fuse_put_request(fc, aux);
1570 }
1571
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001572 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001573 fuse_put_request(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001574 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001575}
1576
1577/*
1578 * If fi->writectr is positive (no truncate or fsync going on) send
1579 * all queued writepage requests.
1580 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001581 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001582 */
1583void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001584__releases(fi->lock)
1585__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001586{
1587 struct fuse_conn *fc = get_fuse_conn(inode);
1588 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001589 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001590 struct fuse_req *req;
1591
1592 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1593 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1594 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001595 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001596 }
1597}
1598
1599static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1600{
1601 struct inode *inode = req->inode;
1602 struct fuse_inode *fi = get_fuse_inode(inode);
1603
1604 mapping_set_error(inode->i_mapping, req->out.h.error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001605 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001606 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001609 struct fuse_req *next = req->misc.write.next;
1610 req->misc.write.next = next->misc.write.next;
1611 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001612 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001613 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001614
1615 /*
1616 * Skip fuse_flush_writepages() to make it easy to crop requests
1617 * based on primary request size.
1618 *
1619 * 1st case (trivial): there are no concurrent activities using
1620 * fuse_set/release_nowrite. Then we're on safe side because
1621 * fuse_flush_writepages() would call fuse_send_writepage()
1622 * anyway.
1623 *
1624 * 2nd case: someone called fuse_set_nowrite and it is waiting
1625 * now for completion of all in-flight requests. This happens
1626 * rarely and no more than once per page, so this should be
1627 * okay.
1628 *
1629 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1630 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1631 * that fuse_set_nowrite returned implies that all in-flight
1632 * requests were completed along with all of their secondary
1633 * requests. Further primary requests are blocked by negative
1634 * writectr. Hence there cannot be any in-flight requests and
1635 * no invocations of fuse_writepage_end() while we're in
1636 * fuse_set_nowrite..fuse_release_nowrite section.
1637 */
1638 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001639 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640 fi->writectr--;
1641 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001642 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001643 fuse_writepage_free(fc, req);
1644}
1645
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001646static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1647 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001648{
Miklos Szeredi72523422013-10-01 16:44:52 +02001649 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001650
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001651 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001652 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001653 ff = list_entry(fi->write_files.next, struct fuse_file,
1654 write_entry);
1655 fuse_file_get(ff);
1656 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001657 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001658
1659 return ff;
1660}
1661
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001662static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1663 struct fuse_inode *fi)
1664{
1665 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1666 WARN_ON(!ff);
1667 return ff;
1668}
1669
1670int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1671{
1672 struct fuse_conn *fc = get_fuse_conn(inode);
1673 struct fuse_inode *fi = get_fuse_inode(inode);
1674 struct fuse_file *ff;
1675 int err;
1676
1677 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001678 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001679 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001680 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001681
1682 return err;
1683}
1684
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001685static int fuse_writepage_locked(struct page *page)
1686{
1687 struct address_space *mapping = page->mapping;
1688 struct inode *inode = mapping->host;
1689 struct fuse_conn *fc = get_fuse_conn(inode);
1690 struct fuse_inode *fi = get_fuse_inode(inode);
1691 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001692 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001693 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694
1695 set_page_writeback(page);
1696
Maxim Patlasov4250c062012-10-26 19:48:07 +04001697 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001698 if (!req)
1699 goto err;
1700
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001701 /* writeback always goes to bg_queue */
1702 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001703 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1704 if (!tmp_page)
1705 goto err_free;
1706
Miklos Szeredi72523422013-10-01 16:44:52 +02001707 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001708 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001709 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001710 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001711
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001712 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001713
1714 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001715 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001716 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001717 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001718 req->num_pages = 1;
1719 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001720 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001721 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001722 req->end = fuse_writepage_end;
1723 req->inode = inode;
1724
Tejun Heo93f78d82015-05-22 17:13:27 -04001725 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001726 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001727
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001728 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001729 list_add(&req->writepages_entry, &fi->writepages);
1730 list_add_tail(&req->list, &fi->queued_writes);
1731 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001732 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001733
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001734 end_page_writeback(page);
1735
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001736 return 0;
1737
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001738err_nofile:
1739 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001740err_free:
1741 fuse_request_free(req);
1742err:
Jeff Layton91839762017-05-25 06:57:50 -04001743 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001744 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001745 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001746}
1747
1748static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1749{
1750 int err;
1751
Miklos Szerediff17be02013-10-01 16:44:53 +02001752 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1753 /*
1754 * ->writepages() should be called for sync() and friends. We
1755 * should only get here on direct reclaim and then we are
1756 * allowed to skip a page which is already in flight
1757 */
1758 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1759
1760 redirty_page_for_writepage(wbc, page);
1761 return 0;
1762 }
1763
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001764 err = fuse_writepage_locked(page);
1765 unlock_page(page);
1766
1767 return err;
1768}
1769
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001770struct fuse_fill_wb_data {
1771 struct fuse_req *req;
1772 struct fuse_file *ff;
1773 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001774 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001775};
1776
1777static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1778{
1779 struct fuse_req *req = data->req;
1780 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001781 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001782 int num_pages = req->num_pages;
1783 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001784
1785 req->ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001786 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001787 list_add_tail(&req->list, &fi->queued_writes);
1788 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001789 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001790
1791 for (i = 0; i < num_pages; i++)
1792 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001793}
1794
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001795/*
1796 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001797 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1798 * one already added for a page at this offset. If there's none, then insert
1799 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001800 * copying the new page contents over to the old temporary page.
1801 */
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001802static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1803 struct page *page)
1804{
1805 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1806 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1807 struct fuse_req *tmp;
1808 struct fuse_req *old_req;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001810 WARN_ON(new_req->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001811
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001812 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001813 list_del(&new_req->writepages_entry);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001814 old_req = fuse_find_writeback(fi, page->index, page->index);
1815 if (!old_req) {
Maxim Patlasovf6011082013-10-02 15:01:07 +04001816 list_add(&new_req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001817 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001818 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001819 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001820
Maxim Patlasovf6011082013-10-02 15:01:07 +04001821 new_req->num_pages = 1;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001822 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001823 pgoff_t curr_index;
1824
1825 WARN_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001826 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001827 if (curr_index == page->index) {
1828 WARN_ON(tmp->num_pages != 1);
1829 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
Kirill Tkhaic5de16cc2018-11-26 12:46:20 +03001830 swap(tmp->pages[0], new_req->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001831 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001832 }
1833 }
1834
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001835 if (!tmp) {
1836 new_req->misc.write.next = old_req->misc.write.next;
1837 old_req->misc.write.next = new_req;
1838 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001839
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001840 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001841
1842 if (tmp) {
1843 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001844
Tejun Heo93f78d82015-05-22 17:13:27 -04001845 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredia2ebba82019-01-16 10:27:59 +01001846 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001847 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001848 fuse_writepage_free(fc, new_req);
1849 fuse_request_free(new_req);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001850 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001851
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001852 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001853}
1854
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001855static int fuse_writepages_fill(struct page *page,
1856 struct writeback_control *wbc, void *_data)
1857{
1858 struct fuse_fill_wb_data *data = _data;
1859 struct fuse_req *req = data->req;
1860 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001861 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001862 struct fuse_conn *fc = get_fuse_conn(inode);
1863 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001864 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001865 int err;
1866
1867 if (!data->ff) {
1868 err = -EIO;
1869 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1870 if (!data->ff)
1871 goto out_unlock;
1872 }
1873
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001874 /*
1875 * Being under writeback is unlikely but possible. For example direct
1876 * read to an mmaped fuse file will set the page dirty twice; once when
1877 * the pages are faulted with get_user_pages(), and then after the read
1878 * completed.
1879 */
1880 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001881
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001882 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001883 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001884 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001885 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1886 fuse_writepages_send(data);
1887 data->req = NULL;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001888 } else if (req && req->num_pages == req->max_pages) {
1889 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1890 fuse_writepages_send(data);
1891 req = data->req = NULL;
1892 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001893 }
Miklos Szeredie52a8252018-10-01 10:07:06 +02001894
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001895 err = -ENOMEM;
1896 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1897 if (!tmp_page)
1898 goto out_unlock;
1899
1900 /*
1901 * The page must not be redirtied until the writeout is completed
1902 * (i.e. userspace has sent a reply to the write request). Otherwise
1903 * there could be more than one temporary page instance for each real
1904 * page.
1905 *
1906 * This is ensured by holding the page lock in page_mkwrite() while
1907 * checking fuse_page_is_writeback(). We already hold the page lock
1908 * since clear_page_dirty_for_io() and keep it held until we add the
1909 * request to the fi->writepages list and increment req->num_pages.
1910 * After this fuse_page_is_writeback() will indicate that the page is
1911 * under writeback, so we can release the page lock.
1912 */
1913 if (data->req == NULL) {
1914 struct fuse_inode *fi = get_fuse_inode(inode);
1915
1916 err = -ENOMEM;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001917 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001918 if (!req) {
1919 __free_page(tmp_page);
1920 goto out_unlock;
1921 }
1922
1923 fuse_write_fill(req, data->ff, page_offset(page), 0);
1924 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001925 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001926 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001927 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001928 req->num_pages = 0;
1929 req->end = fuse_writepage_end;
1930 req->inode = inode;
1931
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001932 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001933 list_add(&req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001934 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001935
1936 data->req = req;
1937 }
1938 set_page_writeback(page);
1939
1940 copy_highpage(tmp_page, page);
1941 req->pages[req->num_pages] = tmp_page;
1942 req->page_descs[req->num_pages].offset = 0;
1943 req->page_descs[req->num_pages].length = PAGE_SIZE;
1944
Tejun Heo93f78d82015-05-22 17:13:27 -04001945 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001946 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001947
1948 err = 0;
1949 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1950 end_page_writeback(page);
1951 data->req = NULL;
1952 goto out_unlock;
1953 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001954 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001955
1956 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001957 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001958 * fuse_page_is_writeback().
1959 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001960 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001961 req->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001962 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001963
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001964out_unlock:
1965 unlock_page(page);
1966
1967 return err;
1968}
1969
1970static int fuse_writepages(struct address_space *mapping,
1971 struct writeback_control *wbc)
1972{
1973 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001974 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001975 struct fuse_fill_wb_data data;
1976 int err;
1977
1978 err = -EIO;
1979 if (is_bad_inode(inode))
1980 goto out;
1981
1982 data.inode = inode;
1983 data.req = NULL;
1984 data.ff = NULL;
1985
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001986 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001987 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001988 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001989 GFP_NOFS);
1990 if (!data.orig_pages)
1991 goto out;
1992
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001993 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1994 if (data.req) {
1995 /* Ignore errors if we can write at least one page */
1996 BUG_ON(!data.req->num_pages);
1997 fuse_writepages_send(&data);
1998 err = 0;
1999 }
2000 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002001 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002002
2003 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002004out:
2005 return err;
2006}
2007
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002008/*
2009 * It's worthy to make sure that space is reserved on disk for the write,
2010 * but how to implement it without killing performance need more thinking.
2011 */
2012static int fuse_write_begin(struct file *file, struct address_space *mapping,
2013 loff_t pos, unsigned len, unsigned flags,
2014 struct page **pagep, void **fsdata)
2015{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002016 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002017 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002018 struct page *page;
2019 loff_t fsize;
2020 int err = -ENOMEM;
2021
2022 WARN_ON(!fc->writeback_cache);
2023
2024 page = grab_cache_page_write_begin(mapping, index, flags);
2025 if (!page)
2026 goto error;
2027
2028 fuse_wait_on_page_writeback(mapping->host, page->index);
2029
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002030 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002031 goto success;
2032 /*
2033 * Check if the start this page comes after the end of file, in which
2034 * case the readpage can be optimized away.
2035 */
2036 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 if (fsize <= (pos & PAGE_MASK)) {
2038 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002039 if (off)
2040 zero_user_segment(page, 0, off);
2041 goto success;
2042 }
2043 err = fuse_do_readpage(file, page);
2044 if (err)
2045 goto cleanup;
2046success:
2047 *pagep = page;
2048 return 0;
2049
2050cleanup:
2051 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002053error:
2054 return err;
2055}
2056
2057static int fuse_write_end(struct file *file, struct address_space *mapping,
2058 loff_t pos, unsigned len, unsigned copied,
2059 struct page *page, void *fsdata)
2060{
2061 struct inode *inode = page->mapping->host;
2062
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002063 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2064 if (!copied)
2065 goto unlock;
2066
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002067 if (!PageUptodate(page)) {
2068 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002069 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002070 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002072 SetPageUptodate(page);
2073 }
2074
2075 fuse_write_update_size(inode, pos + copied);
2076 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002077
2078unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002079 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002080 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002081
2082 return copied;
2083}
2084
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002085static int fuse_launder_page(struct page *page)
2086{
2087 int err = 0;
2088 if (clear_page_dirty_for_io(page)) {
2089 struct inode *inode = page->mapping->host;
2090 err = fuse_writepage_locked(page);
2091 if (!err)
2092 fuse_wait_on_page_writeback(inode, page->index);
2093 }
2094 return err;
2095}
2096
2097/*
2098 * Write back dirty pages now, because there may not be any suitable
2099 * open files later
2100 */
2101static void fuse_vma_close(struct vm_area_struct *vma)
2102{
2103 filemap_write_and_wait(vma->vm_file->f_mapping);
2104}
2105
2106/*
2107 * Wait for writeback against this page to complete before allowing it
2108 * to be marked dirty again, and hence written back again, possibly
2109 * before the previous writepage completed.
2110 *
2111 * Block here, instead of in ->writepage(), so that the userspace fs
2112 * can only block processes actually operating on the filesystem.
2113 *
2114 * Otherwise unprivileged userspace fs would be able to block
2115 * unrelated:
2116 *
2117 * - page migration
2118 * - sync(2)
2119 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2120 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302121static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002122{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002123 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002124 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002125
Dave Jiang11bac802017-02-24 14:56:41 -08002126 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002127 lock_page(page);
2128 if (page->mapping != inode->i_mapping) {
2129 unlock_page(page);
2130 return VM_FAULT_NOPAGE;
2131 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002132
2133 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002134 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002135}
2136
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002137static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002138 .close = fuse_vma_close,
2139 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002140 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002141 .page_mkwrite = fuse_page_mkwrite,
2142};
2143
2144static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2145{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002146 struct fuse_file *ff = file->private_data;
2147
2148 if (ff->open_flags & FOPEN_DIRECT_IO) {
2149 /* Can't provide the coherency needed for MAP_SHARED */
2150 if (vma->vm_flags & VM_MAYSHARE)
2151 return -ENODEV;
2152
2153 invalidate_inode_pages2(file->f_mapping);
2154
2155 return generic_file_mmap(file, vma);
2156 }
2157
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002158 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2159 fuse_link_write_file(file);
2160
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002161 file_accessed(file);
2162 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002163 return 0;
2164}
2165
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002166static int convert_fuse_file_lock(struct fuse_conn *fc,
2167 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 struct file_lock *fl)
2169{
2170 switch (ffl->type) {
2171 case F_UNLCK:
2172 break;
2173
2174 case F_RDLCK:
2175 case F_WRLCK:
2176 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2177 ffl->end < ffl->start)
2178 return -EIO;
2179
2180 fl->fl_start = ffl->start;
2181 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002182
2183 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002184 * Convert pid into init's pid namespace. The locks API will
2185 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002186 */
2187 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002188 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002189 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002190 break;
2191
2192 default:
2193 return -EIO;
2194 }
2195 fl->fl_type = ffl->type;
2196 return 0;
2197}
2198
Miklos Szeredi70781872014-12-12 09:49:05 +01002199static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002200 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002201 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002202{
Al Viro6131ffa2013-02-27 16:59:05 -05002203 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002204 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002205 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002206
Miklos Szeredi70781872014-12-12 09:49:05 +01002207 memset(inarg, 0, sizeof(*inarg));
2208 inarg->fh = ff->fh;
2209 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2210 inarg->lk.start = fl->fl_start;
2211 inarg->lk.end = fl->fl_end;
2212 inarg->lk.type = fl->fl_type;
2213 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002214 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002215 inarg->lk_flags |= FUSE_LK_FLOCK;
2216 args->in.h.opcode = opcode;
2217 args->in.h.nodeid = get_node_id(inode);
2218 args->in.numargs = 1;
2219 args->in.args[0].size = sizeof(*inarg);
2220 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002221}
2222
2223static int fuse_getlk(struct file *file, struct file_lock *fl)
2224{
Al Viro6131ffa2013-02-27 16:59:05 -05002225 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002226 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002227 FUSE_ARGS(args);
2228 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002229 struct fuse_lk_out outarg;
2230 int err;
2231
Miklos Szeredi70781872014-12-12 09:49:05 +01002232 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2233 args.out.numargs = 1;
2234 args.out.args[0].size = sizeof(outarg);
2235 args.out.args[0].value = &outarg;
2236 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002237 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002238 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002239
2240 return err;
2241}
2242
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002243static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002244{
Al Viro6131ffa2013-02-27 16:59:05 -05002245 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002246 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002247 FUSE_ARGS(args);
2248 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002249 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002250 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2251 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002252 int err;
2253
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002254 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002255 /* NLM needs asynchronous locks, which we don't support yet */
2256 return -ENOLCK;
2257 }
2258
Miklos Szeredi71421252006-06-25 05:48:52 -07002259 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002260 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002261 return 0;
2262
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002263 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002264 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002265
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002266 /* locking is restartable */
2267 if (err == -EINTR)
2268 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002269
Miklos Szeredi71421252006-06-25 05:48:52 -07002270 return err;
2271}
2272
2273static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2274{
Al Viro6131ffa2013-02-27 16:59:05 -05002275 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002276 struct fuse_conn *fc = get_fuse_conn(inode);
2277 int err;
2278
Miklos Szeredi48e90762008-07-25 01:49:02 -07002279 if (cmd == F_CANCELLK) {
2280 err = 0;
2281 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002282 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002283 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002284 err = 0;
2285 } else
2286 err = fuse_getlk(file, fl);
2287 } else {
2288 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002289 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002290 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002291 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002292 }
2293 return err;
2294}
2295
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002296static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2297{
Al Viro6131ffa2013-02-27 16:59:05 -05002298 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002299 struct fuse_conn *fc = get_fuse_conn(inode);
2300 int err;
2301
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002302 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002303 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002304 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002305 struct fuse_file *ff = file->private_data;
2306
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002307 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002308 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002309 err = fuse_setlk(file, fl, 1);
2310 }
2311
2312 return err;
2313}
2314
Miklos Szeredib2d22722006-12-06 20:35:51 -08002315static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2316{
2317 struct inode *inode = mapping->host;
2318 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002319 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002320 struct fuse_bmap_in inarg;
2321 struct fuse_bmap_out outarg;
2322 int err;
2323
2324 if (!inode->i_sb->s_bdev || fc->no_bmap)
2325 return 0;
2326
Miklos Szeredib2d22722006-12-06 20:35:51 -08002327 memset(&inarg, 0, sizeof(inarg));
2328 inarg.block = block;
2329 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002330 args.in.h.opcode = FUSE_BMAP;
2331 args.in.h.nodeid = get_node_id(inode);
2332 args.in.numargs = 1;
2333 args.in.args[0].size = sizeof(inarg);
2334 args.in.args[0].value = &inarg;
2335 args.out.numargs = 1;
2336 args.out.args[0].size = sizeof(outarg);
2337 args.out.args[0].value = &outarg;
2338 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002339 if (err == -ENOSYS)
2340 fc->no_bmap = 1;
2341
2342 return err ? 0 : outarg.block;
2343}
2344
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302345static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2346{
2347 struct inode *inode = file->f_mapping->host;
2348 struct fuse_conn *fc = get_fuse_conn(inode);
2349 struct fuse_file *ff = file->private_data;
2350 FUSE_ARGS(args);
2351 struct fuse_lseek_in inarg = {
2352 .fh = ff->fh,
2353 .offset = offset,
2354 .whence = whence
2355 };
2356 struct fuse_lseek_out outarg;
2357 int err;
2358
2359 if (fc->no_lseek)
2360 goto fallback;
2361
2362 args.in.h.opcode = FUSE_LSEEK;
2363 args.in.h.nodeid = ff->nodeid;
2364 args.in.numargs = 1;
2365 args.in.args[0].size = sizeof(inarg);
2366 args.in.args[0].value = &inarg;
2367 args.out.numargs = 1;
2368 args.out.args[0].size = sizeof(outarg);
2369 args.out.args[0].value = &outarg;
2370 err = fuse_simple_request(fc, &args);
2371 if (err) {
2372 if (err == -ENOSYS) {
2373 fc->no_lseek = 1;
2374 goto fallback;
2375 }
2376 return err;
2377 }
2378
2379 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2380
2381fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002382 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302383 if (!err)
2384 return generic_file_llseek(file, offset, whence);
2385 else
2386 return err;
2387}
2388
Andrew Morton965c8e52012-12-17 15:59:39 -08002389static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002390{
2391 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002392 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002393
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302394 switch (whence) {
2395 case SEEK_SET:
2396 case SEEK_CUR:
2397 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002398 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302399 break;
2400 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002401 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002402 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302403 if (!retval)
2404 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002405 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302406 break;
2407 case SEEK_HOLE:
2408 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002409 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302410 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002411 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302412 break;
2413 default:
2414 retval = -EINVAL;
2415 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002416
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002417 return retval;
2418}
2419
Tejun Heo59efec72008-11-26 12:03:55 +01002420/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002421 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2422 * ABI was defined to be 'struct iovec' which is different on 32bit
2423 * and 64bit. Fortunately we can determine which structure the server
2424 * used from the size of the reply.
2425 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002426static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2427 size_t transferred, unsigned count,
2428 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002429{
2430#ifdef CONFIG_COMPAT
2431 if (count * sizeof(struct compat_iovec) == transferred) {
2432 struct compat_iovec *ciov = src;
2433 unsigned i;
2434
2435 /*
2436 * With this interface a 32bit server cannot support
2437 * non-compat (i.e. ones coming from 64bit apps) ioctl
2438 * requests
2439 */
2440 if (!is_compat)
2441 return -EINVAL;
2442
2443 for (i = 0; i < count; i++) {
2444 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2445 dst[i].iov_len = ciov[i].iov_len;
2446 }
2447 return 0;
2448 }
2449#endif
2450
2451 if (count * sizeof(struct iovec) != transferred)
2452 return -EIO;
2453
2454 memcpy(dst, src, transferred);
2455 return 0;
2456}
2457
Miklos Szeredi75727772010-11-30 16:39:27 +01002458/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002459static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2460 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002461{
2462 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002463 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002464
Zach Brownfb6ccff2012-07-24 12:10:11 -07002465 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002466 if (iov->iov_len > (size_t) max)
2467 return -ENOMEM;
2468 max -= iov->iov_len;
2469 }
2470 return 0;
2471}
2472
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002473static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2474 void *src, size_t transferred, unsigned count,
2475 bool is_compat)
2476{
2477 unsigned i;
2478 struct fuse_ioctl_iovec *fiov = src;
2479
2480 if (fc->minor < 16) {
2481 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2482 count, is_compat);
2483 }
2484
2485 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2486 return -EIO;
2487
2488 for (i = 0; i < count; i++) {
2489 /* Did the server supply an inappropriate value? */
2490 if (fiov[i].base != (unsigned long) fiov[i].base ||
2491 fiov[i].len != (unsigned long) fiov[i].len)
2492 return -EIO;
2493
2494 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2495 dst[i].iov_len = (size_t) fiov[i].len;
2496
2497#ifdef CONFIG_COMPAT
2498 if (is_compat &&
2499 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2500 (compat_size_t) dst[i].iov_len != fiov[i].len))
2501 return -EIO;
2502#endif
2503 }
2504
2505 return 0;
2506}
2507
2508
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002509/*
Tejun Heo59efec72008-11-26 12:03:55 +01002510 * For ioctls, there is no generic way to determine how much memory
2511 * needs to be read and/or written. Furthermore, ioctls are allowed
2512 * to dereference the passed pointer, so the parameter requires deep
2513 * copying but FUSE has no idea whatsoever about what to copy in or
2514 * out.
2515 *
2516 * This is solved by allowing FUSE server to retry ioctl with
2517 * necessary in/out iovecs. Let's assume the ioctl implementation
2518 * needs to read in the following structure.
2519 *
2520 * struct a {
2521 * char *buf;
2522 * size_t buflen;
2523 * }
2524 *
2525 * On the first callout to FUSE server, inarg->in_size and
2526 * inarg->out_size will be NULL; then, the server completes the ioctl
2527 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2528 * the actual iov array to
2529 *
2530 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2531 *
2532 * which tells FUSE to copy in the requested area and retry the ioctl.
2533 * On the second round, the server has access to the structure and
2534 * from that it can tell what to look for next, so on the invocation,
2535 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2536 *
2537 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2538 * { .iov_base = a.buf, .iov_len = a.buflen } }
2539 *
2540 * FUSE will copy both struct a and the pointed buffer from the
2541 * process doing the ioctl and retry ioctl with both struct a and the
2542 * buffer.
2543 *
2544 * This time, FUSE server has everything it needs and completes ioctl
2545 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2546 *
2547 * Copying data out works the same way.
2548 *
2549 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2550 * automatically initializes in and out iovs by decoding @cmd with
2551 * _IOC_* macros and the server is not allowed to request RETRY. This
2552 * limits ioctl data transfers to well-formed ioctls and is the forced
2553 * behavior for all FUSE servers.
2554 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002555long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2556 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002557{
Tejun Heo59efec72008-11-26 12:03:55 +01002558 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002559 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002560 struct fuse_ioctl_in inarg = {
2561 .fh = ff->fh,
2562 .cmd = cmd,
2563 .arg = arg,
2564 .flags = flags
2565 };
2566 struct fuse_ioctl_out outarg;
2567 struct fuse_req *req = NULL;
2568 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002569 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002570 struct iovec *in_iov = NULL, *out_iov = NULL;
2571 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002572 size_t in_size, out_size, transferred, c;
2573 int err, i;
2574 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002575
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002576#if BITS_PER_LONG == 32
2577 inarg.flags |= FUSE_IOCTL_32BIT;
2578#else
2579 if (flags & FUSE_IOCTL_COMPAT)
2580 inarg.flags |= FUSE_IOCTL_32BIT;
2581#endif
2582
Tejun Heo59efec72008-11-26 12:03:55 +01002583 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002584 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002585
Tejun Heo59efec72008-11-26 12:03:55 +01002586 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002587 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002588 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002589 if (!pages || !iov_page)
2590 goto out;
2591
2592 /*
2593 * If restricted, initialize IO parameters as encoded in @cmd.
2594 * RETRY from server is not allowed.
2595 */
2596 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002597 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002598
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002599 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002600 iov->iov_len = _IOC_SIZE(cmd);
2601
2602 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2603 in_iov = iov;
2604 in_iovs = 1;
2605 }
2606
2607 if (_IOC_DIR(cmd) & _IOC_READ) {
2608 out_iov = iov;
2609 out_iovs = 1;
2610 }
2611 }
2612
2613 retry:
2614 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2615 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2616
2617 /*
2618 * Out data can be used either for actual out data or iovs,
2619 * make sure there always is at least one page.
2620 */
2621 out_size = max_t(size_t, out_size, PAGE_SIZE);
2622 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2623
2624 /* make sure there are enough buffer pages and init request with them */
2625 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002626 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002627 goto out;
2628 while (num_pages < max_pages) {
2629 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2630 if (!pages[num_pages])
2631 goto out;
2632 num_pages++;
2633 }
2634
Maxim Patlasov54b96672012-10-26 19:49:13 +04002635 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002636 if (IS_ERR(req)) {
2637 err = PTR_ERR(req);
2638 req = NULL;
2639 goto out;
2640 }
2641 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2642 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002643 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002644
2645 /* okay, let's send it to the client */
2646 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002647 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002648 req->in.numargs = 1;
2649 req->in.args[0].size = sizeof(inarg);
2650 req->in.args[0].value = &inarg;
2651 if (in_size) {
2652 req->in.numargs++;
2653 req->in.args[1].size = in_size;
2654 req->in.argpages = 1;
2655
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002656 err = -EFAULT;
2657 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2658 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2659 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2660 if (c != PAGE_SIZE && iov_iter_count(&ii))
2661 goto out;
2662 }
Tejun Heo59efec72008-11-26 12:03:55 +01002663 }
2664
2665 req->out.numargs = 2;
2666 req->out.args[0].size = sizeof(outarg);
2667 req->out.args[0].value = &outarg;
2668 req->out.args[1].size = out_size;
2669 req->out.argpages = 1;
2670 req->out.argvar = 1;
2671
Tejun Heob93f8582008-11-26 12:03:55 +01002672 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002673 err = req->out.h.error;
2674 transferred = req->out.args[1].size;
2675 fuse_put_request(fc, req);
2676 req = NULL;
2677 if (err)
2678 goto out;
2679
2680 /* did it ask for retry? */
2681 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002682 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002683
2684 /* no retry if in restricted mode */
2685 err = -EIO;
2686 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2687 goto out;
2688
2689 in_iovs = outarg.in_iovs;
2690 out_iovs = outarg.out_iovs;
2691
2692 /*
2693 * Make sure things are in boundary, separate checks
2694 * are to protect against overflow.
2695 */
2696 err = -ENOMEM;
2697 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2698 out_iovs > FUSE_IOCTL_MAX_IOV ||
2699 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2700 goto out;
2701
Cong Wang2408f6e2011-11-25 23:14:30 +08002702 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002703 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002704 transferred, in_iovs + out_iovs,
2705 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002706 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002707 if (err)
2708 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002709
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002710 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002711 out_iov = in_iov + in_iovs;
2712
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002713 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002714 if (err)
2715 goto out;
2716
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002717 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002718 if (err)
2719 goto out;
2720
Tejun Heo59efec72008-11-26 12:03:55 +01002721 goto retry;
2722 }
2723
2724 err = -EIO;
2725 if (transferred > inarg.out_size)
2726 goto out;
2727
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002728 err = -EFAULT;
2729 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2730 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2731 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2732 if (c != PAGE_SIZE && iov_iter_count(&ii))
2733 goto out;
2734 }
2735 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002736 out:
2737 if (req)
2738 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002739 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002740 while (num_pages)
2741 __free_page(pages[--num_pages]);
2742 kfree(pages);
2743
2744 return err ? err : outarg.result;
2745}
Tejun Heo08cbf542009-04-14 10:54:53 +09002746EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002747
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002748long fuse_ioctl_common(struct file *file, unsigned int cmd,
2749 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002750{
Al Viro6131ffa2013-02-27 16:59:05 -05002751 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002752 struct fuse_conn *fc = get_fuse_conn(inode);
2753
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002754 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002755 return -EACCES;
2756
2757 if (is_bad_inode(inode))
2758 return -EIO;
2759
2760 return fuse_do_ioctl(file, cmd, arg, flags);
2761}
2762
Tejun Heo59efec72008-11-26 12:03:55 +01002763static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2764 unsigned long arg)
2765{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002766 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002767}
2768
2769static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2770 unsigned long arg)
2771{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002772 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002773}
2774
Tejun Heo95668a62008-11-26 12:03:55 +01002775/*
2776 * All files which have been polled are linked to RB tree
2777 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2778 * find the matching one.
2779 */
2780static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2781 struct rb_node **parent_out)
2782{
2783 struct rb_node **link = &fc->polled_files.rb_node;
2784 struct rb_node *last = NULL;
2785
2786 while (*link) {
2787 struct fuse_file *ff;
2788
2789 last = *link;
2790 ff = rb_entry(last, struct fuse_file, polled_node);
2791
2792 if (kh < ff->kh)
2793 link = &last->rb_left;
2794 else if (kh > ff->kh)
2795 link = &last->rb_right;
2796 else
2797 return link;
2798 }
2799
2800 if (parent_out)
2801 *parent_out = last;
2802 return link;
2803}
2804
2805/*
2806 * The file is about to be polled. Make sure it's on the polled_files
2807 * RB tree. Note that files once added to the polled_files tree are
2808 * not removed before the file is released. This is because a file
2809 * polled once is likely to be polled again.
2810 */
2811static void fuse_register_polled_file(struct fuse_conn *fc,
2812 struct fuse_file *ff)
2813{
2814 spin_lock(&fc->lock);
2815 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002816 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002817
2818 link = fuse_find_polled_node(fc, ff->kh, &parent);
2819 BUG_ON(*link);
2820 rb_link_node(&ff->polled_node, parent, link);
2821 rb_insert_color(&ff->polled_node, &fc->polled_files);
2822 }
2823 spin_unlock(&fc->lock);
2824}
2825
Al Viro076ccb72017-07-03 01:02:18 -04002826__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002827{
Tejun Heo95668a62008-11-26 12:03:55 +01002828 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002829 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002830 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2831 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002832 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002833 int err;
2834
2835 if (fc->no_poll)
2836 return DEFAULT_POLLMASK;
2837
2838 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002839 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002840
2841 /*
2842 * Ask for notification iff there's someone waiting for it.
2843 * The client may ignore the flag and always notify.
2844 */
2845 if (waitqueue_active(&ff->poll_wait)) {
2846 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2847 fuse_register_polled_file(fc, ff);
2848 }
2849
Miklos Szeredi70781872014-12-12 09:49:05 +01002850 args.in.h.opcode = FUSE_POLL;
2851 args.in.h.nodeid = ff->nodeid;
2852 args.in.numargs = 1;
2853 args.in.args[0].size = sizeof(inarg);
2854 args.in.args[0].value = &inarg;
2855 args.out.numargs = 1;
2856 args.out.args[0].size = sizeof(outarg);
2857 args.out.args[0].value = &outarg;
2858 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002859
2860 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002861 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002862 if (err == -ENOSYS) {
2863 fc->no_poll = 1;
2864 return DEFAULT_POLLMASK;
2865 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002866 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002867}
Tejun Heo08cbf542009-04-14 10:54:53 +09002868EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002869
2870/*
2871 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2872 * wakes up the poll waiters.
2873 */
2874int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2875 struct fuse_notify_poll_wakeup_out *outarg)
2876{
2877 u64 kh = outarg->kh;
2878 struct rb_node **link;
2879
2880 spin_lock(&fc->lock);
2881
2882 link = fuse_find_polled_node(fc, kh, NULL);
2883 if (*link) {
2884 struct fuse_file *ff;
2885
2886 ff = rb_entry(*link, struct fuse_file, polled_node);
2887 wake_up_interruptible_sync(&ff->poll_wait);
2888 }
2889
2890 spin_unlock(&fc->lock);
2891 return 0;
2892}
2893
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002894static void fuse_do_truncate(struct file *file)
2895{
2896 struct inode *inode = file->f_mapping->host;
2897 struct iattr attr;
2898
2899 attr.ia_valid = ATTR_SIZE;
2900 attr.ia_size = i_size_read(inode);
2901
2902 attr.ia_file = file;
2903 attr.ia_valid |= ATTR_FILE;
2904
Jan Kara62490332016-05-26 17:12:41 +02002905 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002906}
2907
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002908static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002909{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002910 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002911}
2912
Anand Avati4273b792012-02-17 12:46:25 -05002913static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002914fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002915{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002916 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002917 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002918 struct file *file = iocb->ki_filp;
2919 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002920 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002921 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002922 struct inode *inode;
2923 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002924 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002925 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002926 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002927
Anand Avati4273b792012-02-17 12:46:25 -05002928 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002929 inode = file->f_mapping->host;
2930 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002931
Omar Sandoval6f673762015-03-16 04:33:52 -07002932 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002933 return 0;
2934
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002935 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002936 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002937 if (offset >= i_size)
2938 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002939 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002940 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002941 }
2942
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002943 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002944 if (!io)
2945 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002946 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002947 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002948 io->reqs = 1;
2949 io->bytes = -1;
2950 io->size = 0;
2951 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002952 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002953 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002954 /*
2955 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002956 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002957 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002958 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002959 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302960 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002961
2962 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302963 * We cannot asynchronously extend the size of a file.
2964 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002965 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302966 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2967 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002968
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302969 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002970 /*
2971 * Additional reference to keep io around after
2972 * calling fuse_aio_complete()
2973 */
2974 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002975 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002976 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002977
Omar Sandoval6f673762015-03-16 04:33:52 -07002978 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002979 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002980 fuse_invalidate_attr(inode);
2981 } else {
Al Virod22a9432014-03-16 15:50:47 -04002982 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002983 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002984
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002985 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002986 bool blocking = io->blocking;
2987
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002988 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2989
2990 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002991 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002992 return -EIOCBQUEUED;
2993
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002994 wait_for_completion(&wait);
2995 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002996 }
2997
Seth Forshee744742d2016-03-11 10:35:34 -06002998 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002999
Omar Sandoval6f673762015-03-16 04:33:52 -07003000 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003001 if (ret > 0)
3002 fuse_write_update_size(inode, pos);
3003 else if (ret < 0 && offset + count > i_size)
3004 fuse_do_truncate(file);
3005 }
Anand Avati4273b792012-02-17 12:46:25 -05003006
3007 return ret;
3008}
3009
Miklos Szeredicdadb112012-11-10 16:55:56 +01003010static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3011 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003012{
3013 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003014 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003015 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003016 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01003017 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003018 struct fuse_fallocate_in inarg = {
3019 .fh = ff->fh,
3020 .offset = offset,
3021 .length = length,
3022 .mode = mode
3023 };
3024 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003025 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3026 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003027
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003028 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3029 return -EOPNOTSUPP;
3030
Miklos Szeredi519c6042012-04-26 10:56:36 +02003031 if (fc->no_fallocate)
3032 return -EOPNOTSUPP;
3033
Maxim Patlasov14c14412013-06-13 12:16:39 +04003034 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003035 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003036 if (mode & FALLOC_FL_PUNCH_HOLE) {
3037 loff_t endbyte = offset + length - 1;
3038 err = filemap_write_and_wait_range(inode->i_mapping,
3039 offset, endbyte);
3040 if (err)
3041 goto out;
3042
3043 fuse_sync_writes(inode);
3044 }
Brian Foster3634a632013-05-17 09:30:32 -04003045 }
3046
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003047 if (!(mode & FALLOC_FL_KEEP_SIZE))
3048 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3049
Miklos Szeredi70781872014-12-12 09:49:05 +01003050 args.in.h.opcode = FUSE_FALLOCATE;
3051 args.in.h.nodeid = ff->nodeid;
3052 args.in.numargs = 1;
3053 args.in.args[0].size = sizeof(inarg);
3054 args.in.args[0].value = &inarg;
3055 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003056 if (err == -ENOSYS) {
3057 fc->no_fallocate = 1;
3058 err = -EOPNOTSUPP;
3059 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003060 if (err)
3061 goto out;
3062
3063 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003064 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3065 bool changed = fuse_write_update_size(inode, offset + length);
3066
Miklos Szeredi93d22692014-04-28 14:19:22 +02003067 if (changed && fc->writeback_cache)
3068 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003069 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003070
3071 if (mode & FALLOC_FL_PUNCH_HOLE)
3072 truncate_pagecache_range(inode, offset, offset + length - 1);
3073
3074 fuse_invalidate_attr(inode);
3075
Brian Foster3634a632013-05-17 09:30:32 -04003076out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003077 if (!(mode & FALLOC_FL_KEEP_SIZE))
3078 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3079
Maxim Patlasovbde52782013-09-13 19:19:54 +04003080 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003081 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003082
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003083 return err;
3084}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003085
Niels de Vos88bc7d52018-08-21 14:36:31 +02003086static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3087 struct file *file_out, loff_t pos_out,
3088 size_t len, unsigned int flags)
3089{
3090 struct fuse_file *ff_in = file_in->private_data;
3091 struct fuse_file *ff_out = file_out->private_data;
3092 struct inode *inode_out = file_inode(file_out);
3093 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3094 struct fuse_conn *fc = ff_in->fc;
3095 FUSE_ARGS(args);
3096 struct fuse_copy_file_range_in inarg = {
3097 .fh_in = ff_in->fh,
3098 .off_in = pos_in,
3099 .nodeid_out = ff_out->nodeid,
3100 .fh_out = ff_out->fh,
3101 .off_out = pos_out,
3102 .len = len,
3103 .flags = flags
3104 };
3105 struct fuse_write_out outarg;
3106 ssize_t err;
3107 /* mark unstable when write-back is not used, and file_out gets
3108 * extended */
3109 bool is_unstable = (!fc->writeback_cache) &&
3110 ((pos_out + len) > inode_out->i_size);
3111
3112 if (fc->no_copy_file_range)
3113 return -EOPNOTSUPP;
3114
3115 inode_lock(inode_out);
3116
3117 if (fc->writeback_cache) {
3118 err = filemap_write_and_wait_range(inode_out->i_mapping,
3119 pos_out, pos_out + len);
3120 if (err)
3121 goto out;
3122
3123 fuse_sync_writes(inode_out);
3124 }
3125
3126 if (is_unstable)
3127 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3128
3129 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3130 args.in.h.nodeid = ff_in->nodeid;
3131 args.in.numargs = 1;
3132 args.in.args[0].size = sizeof(inarg);
3133 args.in.args[0].value = &inarg;
3134 args.out.numargs = 1;
3135 args.out.args[0].size = sizeof(outarg);
3136 args.out.args[0].value = &outarg;
3137 err = fuse_simple_request(fc, &args);
3138 if (err == -ENOSYS) {
3139 fc->no_copy_file_range = 1;
3140 err = -EOPNOTSUPP;
3141 }
3142 if (err)
3143 goto out;
3144
3145 if (fc->writeback_cache) {
3146 fuse_write_update_size(inode_out, pos_out + outarg.size);
3147 file_update_time(file_out);
3148 }
3149
3150 fuse_invalidate_attr(inode_out);
3151
3152 err = outarg.size;
3153out:
3154 if (is_unstable)
3155 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3156
3157 inode_unlock(inode_out);
3158
3159 return err;
3160}
3161
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003162static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003163 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003164 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003165 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003166 .mmap = fuse_file_mmap,
3167 .open = fuse_open,
3168 .flush = fuse_flush,
3169 .release = fuse_release,
3170 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003171 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003172 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003173 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003174 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003175 .unlocked_ioctl = fuse_file_ioctl,
3176 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003177 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003178 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003179 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003180};
3181
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003182static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003183 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003184 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003185 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003186 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003187 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003188 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003189 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003190 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003191 .write_begin = fuse_write_begin,
3192 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003193};
3194
3195void fuse_init_file_inode(struct inode *inode)
3196{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003197 struct fuse_inode *fi = get_fuse_inode(inode);
3198
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003199 inode->i_fop = &fuse_file_operations;
3200 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003201
3202 INIT_LIST_HEAD(&fi->write_files);
3203 INIT_LIST_HEAD(&fi->queued_writes);
3204 fi->writectr = 0;
3205 init_waitqueue_head(&fi->page_waitq);
3206 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003207}