blob: e5dfc5e4b999f86342558f7a860eef8c3022945d [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 Austin2e64ff12018-12-10 10:54:52 -080093 if (ff->fc->no_open && !isdir) {
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;
127 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
128 if (!fc->no_open || isdir) {
129 struct fuse_open_out outarg;
130 int err;
131
132 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
133 if (!err) {
134 ff->fh = outarg.fh;
135 ff->open_flags = outarg.open_flags;
136
137 } else if (err != -ENOSYS || isdir) {
138 fuse_file_free(ff);
139 return err;
140 } else {
141 fc->no_open = 1;
142 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200143 }
144
145 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100146 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200147
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200148 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100149 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200150
151 return 0;
152}
Tejun Heo08cbf542009-04-14 10:54:53 +0900153EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400155static void fuse_link_write_file(struct file *file)
156{
157 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400158 struct fuse_inode *fi = get_fuse_inode(inode);
159 struct fuse_file *ff = file->private_data;
160 /*
161 * file may be written through mmap, so chain it onto the
162 * inodes's write_file list
163 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300164 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400165 if (list_empty(&ff->write_entry))
166 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300167 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400168}
169
Miklos Szeredic7b71432009-04-28 16:56:37 +0200170void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800171{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200172 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800173 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174
Miklos Szeredic7b71432009-04-28 16:56:37 +0200175 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700176 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200177 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200178 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800179 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
180 struct fuse_inode *fi = get_fuse_inode(inode);
181
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300182 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300183 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800184 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300185 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800186 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200187 if (fc->writeback_cache)
188 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800189 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400190 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
191 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800192}
193
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200194int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800195{
Tejun Heoacf99432008-11-26 12:03:55 +0100196 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200198 bool lock_inode = (file->f_flags & O_TRUNC) &&
199 fc->atomic_o_trunc &&
200 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201
202 err = generic_file_open(inode, file);
203 if (err)
204 return err;
205
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200206 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500207 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200208
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200209 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700210
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200211 if (!err)
212 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200213
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200214 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500215 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200216
217 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700218}
219
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300220static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
221 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800222{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200223 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700224 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800225 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700226
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300227 /* Inode is NULL on error path of fuse_create_open() */
228 if (likely(fi)) {
229 spin_lock(&fi->lock);
230 list_del(&ff->write_entry);
231 spin_unlock(&fi->lock);
232 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200233 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200234 if (!RB_EMPTY_NODE(&ff->polled_node))
235 rb_erase(&ff->polled_node, &fc->polled_files);
236 spin_unlock(&fc->lock);
237
Bryan Green357ccf22011-03-01 16:43:52 -0800238 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200239
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700240 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800241 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700242 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200243 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244 req->in.numargs = 1;
245 req->in.args[0].size = sizeof(struct fuse_release_in);
246 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800247}
248
Chad Austin2e64ff12018-12-10 10:54:52 -0800249void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800250{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300251 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100252 struct fuse_file *ff = file->private_data;
253 struct fuse_req *req = ff->reserved_req;
Chad Austin2e64ff12018-12-10 10:54:52 -0800254 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700255
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300256 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100257
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200258 if (ff->flock) {
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
262 (fl_owner_t) file);
263 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266
Tejun Heo6b2db282009-04-14 10:54:49 +0900267 /*
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100271 *
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900275 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800276 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277}
278
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700279static int fuse_open(struct inode *inode, struct file *file)
280{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200281 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700282}
283
284static int fuse_release(struct inode *inode, struct file *file)
285{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400286 struct fuse_conn *fc = get_fuse_conn(inode);
287
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200290 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400291
Chad Austin2e64ff12018-12-10 10:54:52 -0800292 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200293
294 /* return value is ignored by VFS */
295 return 0;
296}
297
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300298void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200299{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200300 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300301 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100302 /*
303 * iput(NULL) is a no-op and since the refcount is 1 and everything's
304 * synchronous, we are fine with not doing igrab() here"
305 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800306 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307}
Tejun Heo08cbf542009-04-14 10:54:53 +0900308EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700309
Miklos Szeredi71421252006-06-25 05:48:52 -0700310/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700313 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700314u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700315{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
318 u32 v0 = v;
319 u32 v1 = v >> 32;
320 u32 sum = 0;
321 int i;
322
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
325 sum += 0x9E3779B9;
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
327 }
328
329 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700330}
331
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100332static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
333 pgoff_t idx_from, pgoff_t idx_to)
334{
335 struct fuse_req *req;
336
337 list_for_each_entry(req, &fi->writepages, writepages_entry) {
338 pgoff_t curr_index;
339
340 WARN_ON(get_fuse_inode(req->inode) != fi);
341 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
342 if (idx_from < curr_index + req->num_pages &&
343 curr_index <= idx_to) {
344 return req;
345 }
346 }
347 return NULL;
348}
349
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700350/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400351 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700352 *
353 * This is currently done by walking the list of writepage requests
354 * for the inode, which can be pretty inefficient.
355 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400356static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
357 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700358{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700359 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100360 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700361
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300362 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100363 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300364 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700365
366 return found;
367}
368
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400369static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
370{
371 return fuse_range_is_writeback(inode, index, index);
372}
373
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700374/*
375 * Wait for page writeback to be completed.
376 *
377 * Since fuse doesn't rely on the VM writeback tracking, this has to
378 * use some other means.
379 */
380static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
381{
382 struct fuse_inode *fi = get_fuse_inode(inode);
383
384 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
385 return 0;
386}
387
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400388/*
389 * Wait for all pending writepages on the inode to finish.
390 *
391 * This is currently done by blocking further writes with FUSE_NOWRITE
392 * and waiting for all sent writes to complete.
393 *
394 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
395 * could conflict with truncation.
396 */
397static void fuse_sync_writes(struct inode *inode)
398{
399 fuse_set_nowrite(inode);
400 fuse_release_nowrite(inode);
401}
402
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700403static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700404{
Al Viro6131ffa2013-02-27 16:59:05 -0500405 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700406 struct fuse_conn *fc = get_fuse_conn(inode);
407 struct fuse_file *ff = file->private_data;
408 struct fuse_req *req;
409 struct fuse_flush_in inarg;
410 int err;
411
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800412 if (is_bad_inode(inode))
413 return -EIO;
414
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700415 if (fc->no_flush)
416 return 0;
417
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200418 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400419 if (err)
420 return err;
421
Al Viro59551022016-01-22 15:40:57 -0500422 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400423 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500424 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400425
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200426 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700427 if (err)
428 return err;
429
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400430 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 memset(&inarg, 0, sizeof(inarg));
432 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700433 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 req->in.h.opcode = FUSE_FLUSH;
435 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700436 req->in.numargs = 1;
437 req->in.args[0].size = sizeof(inarg);
438 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200439 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100440 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700441 err = req->out.h.error;
442 fuse_put_request(fc, req);
443 if (err == -ENOSYS) {
444 fc->no_flush = 1;
445 err = 0;
446 }
447 return err;
448}
449
Josef Bacik02c24a82011-07-16 20:44:56 -0400450int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100451 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700452{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200453 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700454 struct fuse_conn *fc = get_fuse_conn(inode);
455 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100456 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700457 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100458
459 memset(&inarg, 0, sizeof(inarg));
460 inarg.fh = ff->fh;
461 inarg.fsync_flags = datasync ? 1 : 0;
462 args.in.h.opcode = opcode;
463 args.in.h.nodeid = get_node_id(inode);
464 args.in.numargs = 1;
465 args.in.args[0].size = sizeof(inarg);
466 args.in.args[0].value = &inarg;
467 return fuse_simple_request(fc, &args);
468}
469
470static int fuse_fsync(struct file *file, loff_t start, loff_t end,
471 int datasync)
472{
473 struct inode *inode = file->f_mapping->host;
474 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700475 int err;
476
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800477 if (is_bad_inode(inode))
478 return -EIO;
479
Al Viro59551022016-01-22 15:40:57 -0500480 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400481
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700482 /*
483 * Start writeback against all dirty pages of the inode, then
484 * wait for all outstanding writes, before sending the FSYNC
485 * request.
486 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400487 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700488 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400489 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700490
491 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700492
493 /*
494 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400495 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700496 * We have to do this directly after fuse_sync_writes()
497 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400498 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700499 if (err)
500 goto out;
501
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200502 err = sync_inode_metadata(inode, 1);
503 if (err)
504 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700505
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100506 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200507 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400508
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100509 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700510 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100511 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700512 err = 0;
513 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400514out:
Al Viro59551022016-01-22 15:40:57 -0500515 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100517 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700518}
519
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200520void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
521 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700522{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700523 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800524 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800526 inarg->fh = ff->fh;
527 inarg->offset = pos;
528 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800529 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800530 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200531 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700532 req->in.numargs = 1;
533 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800534 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700535 req->out.argvar = 1;
536 req->out.numargs = 1;
537 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700538}
539
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200540static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400541{
542 unsigned i;
543
544 for (i = 0; i < req->num_pages; i++) {
545 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200546 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400547 set_page_dirty_lock(page);
548 put_page(page);
549 }
550}
551
Seth Forshee744742d2016-03-11 10:35:34 -0600552static void fuse_io_release(struct kref *kref)
553{
554 kfree(container_of(kref, struct fuse_io_priv, refcnt));
555}
556
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100557static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
558{
559 if (io->err)
560 return io->err;
561
562 if (io->bytes >= 0 && io->write)
563 return -EIO;
564
565 return io->bytes < 0 ? io->size : io->bytes;
566}
567
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400568/**
569 * In case of short read, the caller sets 'pos' to the position of
570 * actual end of fuse request in IO request. Otherwise, if bytes_requested
571 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
572 *
573 * An example:
574 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
575 * both submitted asynchronously. The first of them was ACKed by userspace as
576 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
577 * second request was ACKed as short, e.g. only 1K was read, resulting in
578 * pos == 33K.
579 *
580 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
581 * will be equal to the length of the longest contiguous fragment of
582 * transferred data starting from the beginning of IO request.
583 */
584static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
585{
586 int left;
587
588 spin_lock(&io->lock);
589 if (err)
590 io->err = io->err ? : err;
591 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
592 io->bytes = pos;
593
594 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530595 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100596 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400597 spin_unlock(&io->lock);
598
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530599 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100600 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400601
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100602 if (res >= 0) {
603 struct inode *inode = file_inode(io->iocb->ki_filp);
604 struct fuse_conn *fc = get_fuse_conn(inode);
605 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400606
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300607 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300608 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300609 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400610 }
611
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100612 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400613 }
Seth Forshee744742d2016-03-11 10:35:34 -0600614
615 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400616}
617
618static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
619{
620 struct fuse_io_priv *io = req->io;
621 ssize_t pos = -1;
622
Ashish Samant61c12b42017-07-12 19:26:58 -0700623 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400624
625 if (io->write) {
626 if (req->misc.write.in.size != req->misc.write.out.size)
627 pos = req->misc.write.in.offset - io->offset +
628 req->misc.write.out.size;
629 } else {
630 if (req->misc.read.in.size != req->out.args[0].size)
631 pos = req->misc.read.in.offset - io->offset +
632 req->out.args[0].size;
633 }
634
635 fuse_aio_complete(io, req->out.h.error, pos);
636}
637
638static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
639 size_t num_bytes, struct fuse_io_priv *io)
640{
641 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600642 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400643 io->size += num_bytes;
644 io->reqs++;
645 spin_unlock(&io->lock);
646
647 req->io = io;
648 req->end = fuse_aio_complete_req;
649
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400650 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400651 fuse_request_send_background(fc, req);
652
653 return num_bytes;
654}
655
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400656static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200657 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700658{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200659 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200660 struct fuse_file *ff = file->private_data;
661 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700662
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200663 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700664 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700665 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700666
667 inarg->read_flags |= FUSE_READ_LOCKOWNER;
668 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
669 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400670
671 if (io->async)
672 return fuse_async_req_send(fc, req, count, io);
673
Tejun Heob93f8582008-11-26 12:03:55 +0100674 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800675 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700676}
677
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700678static void fuse_read_update_size(struct inode *inode, loff_t size,
679 u64 attr_ver)
680{
681 struct fuse_conn *fc = get_fuse_conn(inode);
682 struct fuse_inode *fi = get_fuse_inode(inode);
683
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300684 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400685 if (attr_ver == fi->attr_version && size < inode->i_size &&
686 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300687 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700688 i_size_write(inode, size);
689 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300690 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700691}
692
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400693static void fuse_short_read(struct fuse_req *req, struct inode *inode,
694 u64 attr_ver)
695{
696 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400697 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400698
Pavel Emelyanov83732002013-10-10 17:10:46 +0400699 if (fc->writeback_cache) {
700 /*
701 * A hole in a file. Some data after the hole are in page cache,
702 * but have not reached the client fs yet. So, the hole is not
703 * present there.
704 */
705 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 int start_idx = num_read >> PAGE_SHIFT;
707 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400708
709 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300710 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400711 off = 0;
712 }
713 } else {
714 loff_t pos = page_offset(req->pages[0]) + num_read;
715 fuse_read_update_size(inode, pos, attr_ver);
716 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400717}
718
Maxim Patlasov482fce52013-10-10 17:11:25 +0400719static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700720{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200721 struct kiocb iocb;
722 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700723 struct inode *inode = page->mapping->host;
724 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800725 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700726 size_t num_read;
727 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300728 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700729 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800730 int err;
731
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700732 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300733 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700734 * page-cache page, so make sure we read a properly synced
735 * page.
736 */
737 fuse_wait_on_page_writeback(inode, page->index);
738
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400739 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700740 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400741 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700742
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700743 attr_ver = fuse_get_attr_version(fc);
744
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200746 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700747 req->num_pages = 1;
748 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400749 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200750 init_sync_kiocb(&iocb, file);
751 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400752 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754
755 if (!err) {
756 /*
757 * Short read means EOF. If file size is larger, truncate it
758 */
759 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400760 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700761
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700763 }
764
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400765 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400766
767 return err;
768}
769
770static int fuse_readpage(struct file *file, struct page *page)
771{
772 struct inode *inode = page->mapping->host;
773 int err;
774
775 err = -EIO;
776 if (is_bad_inode(inode))
777 goto out;
778
779 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800780 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700781 out:
782 unlock_page(page);
783 return err;
784}
785
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800786static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700787{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800788 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700789 size_t count = req->misc.read.in.size;
790 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200791 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800792
Miklos Szeredice534fb2010-05-25 15:06:07 +0200793 for (i = 0; mapping == NULL && i < req->num_pages; i++)
794 mapping = req->pages[i]->mapping;
795
796 if (mapping) {
797 struct inode *inode = mapping->host;
798
799 /*
800 * Short read means EOF. If file size is larger, truncate it
801 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400802 if (!req->out.h.error && num_read < count)
803 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200804
Andrew Gallagher451418f2013-11-05 03:55:43 -0800805 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700806 }
807
Miklos Szeredidb50b962005-09-09 13:10:33 -0700808 for (i = 0; i < req->num_pages; i++) {
809 struct page *page = req->pages[i];
810 if (!req->out.h.error)
811 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800812 else
813 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700814 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300815 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700816 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700817 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -0800818 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800819}
820
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200821static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800822{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200823 struct fuse_file *ff = file->private_data;
824 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800825 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300826 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200827
828 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800829 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200830 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200831 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700832 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800833 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700834 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800835 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100836 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800837 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100838 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800839 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100840 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800841 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700842}
843
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700844struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800846 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700847 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400848 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700849};
850
851static int fuse_readpages_fill(void *_data, struct page *page)
852{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700853 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 struct fuse_req *req = data->req;
855 struct inode *inode = data->inode;
856 struct fuse_conn *fc = get_fuse_conn(inode);
857
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700858 fuse_wait_on_page_writeback(inode, page->index);
859
Miklos Szeredidb50b962005-09-09 13:10:33 -0700860 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300861 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300862 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300864 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
865 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200866 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400867 if (fc->async_read)
868 req = fuse_get_req_for_background(fc, nr_alloc);
869 else
870 req = fuse_get_req(fc, nr_alloc);
871
872 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700873 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700874 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700875 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700877 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400878
879 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300880 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400881 fuse_put_request(fc, req);
882 return -EIO;
883 }
884
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300885 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400887 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100888 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400889 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 return 0;
891}
892
893static int fuse_readpages(struct file *file, struct address_space *mapping,
894 struct list_head *pages, unsigned nr_pages)
895{
896 struct inode *inode = mapping->host;
897 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700898 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300900 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800901
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700902 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800903 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800904 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800905
Miklos Szeredia6643092007-11-28 16:22:00 -0800906 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400908 if (fc->async_read)
909 data.req = fuse_get_req_for_background(fc, nr_alloc);
910 else
911 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400912 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700913 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700914 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800915 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916
917 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700918 if (!err) {
919 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200920 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700921 else
922 fuse_put_request(fc, data.req);
923 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800924out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700925 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700926}
927
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100928static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800929{
930 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400931 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932
Brian Fostera8894272012-07-16 15:23:50 -0400933 /*
934 * In auto invalidate mode, always update attributes on read.
935 * Otherwise, only update if we attempt to read past EOF (to ensure
936 * i_size is up to date).
937 */
938 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400939 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800940 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200941 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942 if (err)
943 return err;
944 }
945
Al Viro37c20f12014-04-02 14:47:09 -0400946 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800947}
948
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200949static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700952 struct fuse_write_in *inarg = &req->misc.write.in;
953 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700954
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700955 inarg->fh = ff->fh;
956 inarg->offset = pos;
957 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700958 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200959 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200961 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700962 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
963 else
964 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700965 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700966 req->in.args[1].size = count;
967 req->out.numargs = 1;
968 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700969 req->out.args[0].value = outarg;
970}
971
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400972static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200973 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200975 struct kiocb *iocb = io->iocb;
976 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200977 struct fuse_file *ff = file->private_data;
978 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200979 struct fuse_write_in *inarg = &req->misc.write.in;
980
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200981 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200982 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200983 if (iocb->ki_flags & IOCB_DSYNC)
984 inarg->flags |= O_DSYNC;
985 if (iocb->ki_flags & IOCB_SYNC)
986 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700987 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700988 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
989 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
990 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400991
992 if (io->async)
993 return fuse_async_req_send(fc, req, count, io);
994
Tejun Heob93f8582008-11-26 12:03:55 +0100995 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700996 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700997}
998
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400999bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000{
1001 struct fuse_conn *fc = get_fuse_conn(inode);
1002 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001005 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001006 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001007 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001009 ret = true;
1010 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001011 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001012
1013 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001014}
1015
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001016static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001017 struct inode *inode, loff_t pos,
1018 size_t count)
1019{
1020 size_t res;
1021 unsigned offset;
1022 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001023 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001024
1025 for (i = 0; i < req->num_pages; i++)
1026 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1027
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001028 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001029
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001030 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001031 count = res;
1032 for (i = 0; i < req->num_pages; i++) {
1033 struct page *page = req->pages[i];
1034
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001036 SetPageUptodate(page);
1037
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001038 if (count > PAGE_SIZE - offset)
1039 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001040 else
1041 count = 0;
1042 offset = 0;
1043
1044 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001045 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001046 }
1047
1048 return res;
1049}
1050
1051static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1052 struct address_space *mapping,
1053 struct iov_iter *ii, loff_t pos)
1054{
1055 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001056 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001057 size_t count = 0;
1058 int err;
1059
Miklos Szeredif4975c62009-04-02 14:25:34 +02001060 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001061 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001062
1063 do {
1064 size_t tmp;
1065 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001066 pgoff_t index = pos >> PAGE_SHIFT;
1067 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001068 iov_iter_count(ii));
1069
1070 bytes = min_t(size_t, bytes, fc->max_write - count);
1071
1072 again:
1073 err = -EFAULT;
1074 if (iov_iter_fault_in_readable(ii, bytes))
1075 break;
1076
1077 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001078 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001079 if (!page)
1080 break;
1081
anfei zhou931e80e2010-02-02 13:44:02 -08001082 if (mapping_writably_mapped(mapping))
1083 flush_dcache_page(page);
1084
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001086 flush_dcache_page(page);
1087
Roman Gushchin3ca81382015-10-12 16:33:44 +03001088 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 if (!tmp) {
1090 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001092 bytes = min(bytes, iov_iter_single_seg_count(ii));
1093 goto again;
1094 }
1095
1096 err = 0;
1097 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001098 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 req->num_pages++;
1100
Nick Pigginea9b9902008-04-30 00:54:42 -07001101 count += tmp;
1102 pos += tmp;
1103 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001104 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 offset = 0;
1106
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001107 if (!fc->big_writes)
1108 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001110 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001111
1112 return count > 0 ? count : err;
1113}
1114
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001115static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1116 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001117{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001118 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001119 ((pos + len - 1) >> PAGE_SHIFT) -
1120 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001121 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001122}
1123
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001124static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 struct address_space *mapping,
1126 struct iov_iter *ii, loff_t pos)
1127{
1128 struct inode *inode = mapping->host;
1129 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001130 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001131 int err = 0;
1132 ssize_t res = 0;
1133
1134 if (is_bad_inode(inode))
1135 return -EIO;
1136
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001137 if (inode->i_size < pos + iov_iter_count(ii))
1138 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1139
Nick Pigginea9b9902008-04-30 00:54:42 -07001140 do {
1141 struct fuse_req *req;
1142 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001143 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1144 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001145
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001146 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001147 if (IS_ERR(req)) {
1148 err = PTR_ERR(req);
1149 break;
1150 }
1151
1152 count = fuse_fill_write_pages(req, mapping, ii, pos);
1153 if (count <= 0) {
1154 err = count;
1155 } else {
1156 size_t num_written;
1157
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001158 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001159 pos, count);
1160 err = req->out.h.error;
1161 if (!err) {
1162 res += num_written;
1163 pos += num_written;
1164
1165 /* break out of the loop on short write */
1166 if (num_written != count)
1167 err = -EIO;
1168 }
1169 }
1170 fuse_put_request(fc, req);
1171 } while (!err && iov_iter_count(ii));
1172
1173 if (res > 0)
1174 fuse_write_update_size(inode, pos);
1175
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001176 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001177 fuse_invalidate_attr(inode);
1178
1179 return res > 0 ? res : err;
1180}
1181
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001182static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001183{
1184 struct file *file = iocb->ki_filp;
1185 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001186 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001187 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001188 struct inode *inode = mapping->host;
1189 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001190 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001191
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001192 if (get_fuse_conn(inode)->writeback_cache) {
1193 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001194 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001195 if (err)
1196 return err;
1197
Al Viro84c3d552014-04-03 14:33:23 -04001198 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001199 }
1200
Al Viro59551022016-01-22 15:40:57 -05001201 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001202
1203 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001204 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001205
Al Viro3309dd02015-04-09 12:55:47 -04001206 err = generic_write_checks(iocb, from);
1207 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001208 goto out;
1209
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001210 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001211 if (err)
1212 goto out;
1213
Josef Bacikc3b2da32012-03-26 09:59:21 -04001214 err = file_update_time(file);
1215 if (err)
1216 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001217
Al Viro2ba48ce2015-04-09 13:52:01 -04001218 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001219 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001220 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001221 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001222 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001223
Anand Avati4273b792012-02-17 12:46:25 -05001224 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001225
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001226 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001227 if (written_buffered < 0) {
1228 err = written_buffered;
1229 goto out;
1230 }
1231 endbyte = pos + written_buffered - 1;
1232
1233 err = filemap_write_and_wait_range(file->f_mapping, pos,
1234 endbyte);
1235 if (err)
1236 goto out;
1237
1238 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001239 pos >> PAGE_SHIFT,
1240 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001241
1242 written += written_buffered;
1243 iocb->ki_pos = pos + written_buffered;
1244 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001245 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001246 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001247 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001248 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001249out:
1250 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001251 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001252 if (written > 0)
1253 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001254
1255 return written ? written : err;
1256}
1257
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001258static inline void fuse_page_descs_length_init(struct fuse_req *req,
1259 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001260{
1261 int i;
1262
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001263 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001264 req->page_descs[i].length = PAGE_SIZE -
1265 req->page_descs[i].offset;
1266}
1267
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001268static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1269{
1270 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1271}
1272
1273static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1274 size_t max_size)
1275{
1276 return min(iov_iter_single_seg_count(ii), max_size);
1277}
1278
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001279static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001280 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001281{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001282 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001283 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001284
Miklos Szeredif4975c62009-04-02 14:25:34 +02001285 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001286 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001287 unsigned long user_addr = fuse_get_user_addr(ii);
1288 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1289
Miklos Szeredif4975c62009-04-02 14:25:34 +02001290 if (write)
1291 req->in.args[1].value = (void *) user_addr;
1292 else
1293 req->out.args[0].value = (void *) user_addr;
1294
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001295 iov_iter_advance(ii, frag_size);
1296 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001297 return 0;
1298 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001299
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001300 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001301 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001302 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001303 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001304 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001305 req->max_pages - req->num_pages,
1306 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001307 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001308 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309
Al Viroc9c37e22014-03-16 16:08:30 -04001310 iov_iter_advance(ii, ret);
1311 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001312
Al Viroc9c37e22014-03-16 16:08:30 -04001313 ret += start;
1314 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1315
1316 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001317 fuse_page_descs_length_init(req, req->num_pages, npages);
1318
1319 req->num_pages += npages;
1320 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001321 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001322 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001323
1324 if (write)
1325 req->in.argpages = 1;
1326 else
1327 req->out.argpages = 1;
1328
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001329 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001330
Ashish Samant2c932d42016-03-25 10:53:41 -07001331 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001332}
1333
Al Virod22a9432014-03-16 15:50:47 -04001334ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1335 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001336{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001337 int write = flags & FUSE_DIO_WRITE;
1338 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001339 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001340 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001341 struct fuse_file *ff = file->private_data;
1342 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001343 size_t nmax = write ? fc->max_write : fc->max_read;
1344 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001345 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001346 pgoff_t idx_from = pos >> PAGE_SHIFT;
1347 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001348 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001349 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001350 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001351
Brian Fosterde82b922013-05-14 11:25:39 -04001352 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001353 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1354 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001355 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001356 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001357 if (IS_ERR(req))
1358 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001359
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001360 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1361 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001362 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001363 fuse_sync_writes(inode);
1364 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001365 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001366 }
1367
Ashish Samant61c12b42017-07-12 19:26:58 -07001368 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001371 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001372 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001373 err = fuse_get_user_pages(req, iter, &nbytes, write);
1374 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001376
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001377 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001378 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001380 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001381
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001382 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001383 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001384 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001385 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001386 break;
1387 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001388 res = 0;
1389 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001390 break;
1391 }
1392 count -= nres;
1393 res += nres;
1394 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001395 if (nres != nbytes)
1396 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001397 if (count) {
1398 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001399 if (io->async)
1400 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001401 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001402 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001403 req = fuse_get_req(fc, iov_iter_npages(iter,
1404 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001405 if (IS_ERR(req))
1406 break;
1407 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001409 if (!IS_ERR(req))
1410 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001411 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001412 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413
Ashish Samant742f9922016-03-14 21:57:35 -07001414 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415}
Tejun Heo08cbf542009-04-14 10:54:53 +09001416EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001418static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001419 struct iov_iter *iter,
1420 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001421{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001422 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001423 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001424
1425 if (is_bad_inode(inode))
1426 return -EIO;
1427
Al Virod22a9432014-03-16 15:50:47 -04001428 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001429
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001430 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001431
1432 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001433}
1434
Martin Raiber23c94e12018-10-27 16:48:48 +00001435static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1436
Al Viro153162632015-03-30 22:08:36 -04001437static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001438{
Martin Raiber23c94e12018-10-27 16:48:48 +00001439 ssize_t res;
1440
1441 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1442 struct file *file = iocb->ki_filp;
1443
1444 if (is_bad_inode(file_inode(file)))
1445 return -EIO;
1446
1447 res = fuse_direct_IO(iocb, to);
1448 } else {
1449 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1450
1451 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1452 }
1453
1454 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001455}
1456
Al Viro153162632015-03-30 22:08:36 -04001457static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001458{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001459 struct inode *inode = file_inode(iocb->ki_filp);
1460 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001461 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001462
1463 if (is_bad_inode(inode))
1464 return -EIO;
1465
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001466 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001467 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001468 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001469 if (res > 0) {
1470 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1471 res = fuse_direct_IO(iocb, from);
1472 } else {
1473 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1474 FUSE_DIO_WRITE);
1475 }
1476 }
Al Viro812408f2015-03-30 22:15:58 -04001477 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001478 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001479 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001480 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001481
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001482 return res;
1483}
1484
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001485static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1486{
1487 struct fuse_file *ff = iocb->ki_filp->private_data;
1488
1489 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1490 return fuse_cache_read_iter(iocb, to);
1491 else
1492 return fuse_direct_read_iter(iocb, to);
1493}
1494
1495static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1496{
1497 struct fuse_file *ff = iocb->ki_filp->private_data;
1498
1499 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1500 return fuse_cache_write_iter(iocb, from);
1501 else
1502 return fuse_direct_write_iter(iocb, from);
1503}
1504
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001505static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001506{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001507 int i;
1508
1509 for (i = 0; i < req->num_pages; i++)
1510 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001511
1512 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001513 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001514}
1515
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001516static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001517{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001518 struct inode *inode = req->inode;
1519 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001520 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001521 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001522
1523 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001524 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001525 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001526 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001527 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001528 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529 wake_up(&fi->page_waitq);
1530}
1531
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001532/* Called under fi->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001533static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1534 loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001535__releases(fi->lock)
1536__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537{
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001538 struct fuse_req *aux, *next;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001540 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001541 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001542 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001543
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001544 if (inarg->offset + data_size <= size) {
1545 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001546 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001547 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001548 } else {
1549 /* Got truncated off completely */
1550 goto out_free;
1551 }
1552
1553 req->in.args[1].size = inarg->size;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001554 queued = fuse_request_queue_background(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001555 /* Fails on broken connection only */
1556 if (unlikely(!queued))
1557 goto out_free;
1558
1559 fi->writectr++;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001560 return;
1561
1562 out_free:
1563 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001564 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001565
1566 /* After fuse_writepage_finish() aux request list is private */
1567 for (aux = req->misc.write.next; aux; aux = next) {
1568 next = aux->misc.write.next;
1569 aux->misc.write.next = NULL;
1570 fuse_writepage_free(fc, aux);
1571 fuse_put_request(fc, aux);
1572 }
1573
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001574 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001575 fuse_put_request(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001576 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001577}
1578
1579/*
1580 * If fi->writectr is positive (no truncate or fsync going on) send
1581 * all queued writepage requests.
1582 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001583 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001584 */
1585void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001586__releases(fi->lock)
1587__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001588{
1589 struct fuse_conn *fc = get_fuse_conn(inode);
1590 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001591 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001592 struct fuse_req *req;
1593
1594 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1595 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1596 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001597 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001598 }
1599}
1600
1601static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1602{
1603 struct inode *inode = req->inode;
1604 struct fuse_inode *fi = get_fuse_inode(inode);
1605
1606 mapping_set_error(inode->i_mapping, req->out.h.error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001607 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001608 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001609 struct fuse_conn *fc = get_fuse_conn(inode);
1610 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001611 struct fuse_req *next = req->misc.write.next;
1612 req->misc.write.next = next->misc.write.next;
1613 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001614 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001615 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001616
1617 /*
1618 * Skip fuse_flush_writepages() to make it easy to crop requests
1619 * based on primary request size.
1620 *
1621 * 1st case (trivial): there are no concurrent activities using
1622 * fuse_set/release_nowrite. Then we're on safe side because
1623 * fuse_flush_writepages() would call fuse_send_writepage()
1624 * anyway.
1625 *
1626 * 2nd case: someone called fuse_set_nowrite and it is waiting
1627 * now for completion of all in-flight requests. This happens
1628 * rarely and no more than once per page, so this should be
1629 * okay.
1630 *
1631 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1632 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1633 * that fuse_set_nowrite returned implies that all in-flight
1634 * requests were completed along with all of their secondary
1635 * requests. Further primary requests are blocked by negative
1636 * writectr. Hence there cannot be any in-flight requests and
1637 * no invocations of fuse_writepage_end() while we're in
1638 * fuse_set_nowrite..fuse_release_nowrite section.
1639 */
1640 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001641 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642 fi->writectr--;
1643 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001644 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001645 fuse_writepage_free(fc, req);
1646}
1647
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001648static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1649 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001650{
Miklos Szeredi72523422013-10-01 16:44:52 +02001651 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001652
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001653 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001654 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001655 ff = list_entry(fi->write_files.next, struct fuse_file,
1656 write_entry);
1657 fuse_file_get(ff);
1658 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001659 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001660
1661 return ff;
1662}
1663
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001664static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1665 struct fuse_inode *fi)
1666{
1667 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1668 WARN_ON(!ff);
1669 return ff;
1670}
1671
1672int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1673{
1674 struct fuse_conn *fc = get_fuse_conn(inode);
1675 struct fuse_inode *fi = get_fuse_inode(inode);
1676 struct fuse_file *ff;
1677 int err;
1678
1679 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001680 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001681 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001682 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001683
1684 return err;
1685}
1686
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001687static int fuse_writepage_locked(struct page *page)
1688{
1689 struct address_space *mapping = page->mapping;
1690 struct inode *inode = mapping->host;
1691 struct fuse_conn *fc = get_fuse_conn(inode);
1692 struct fuse_inode *fi = get_fuse_inode(inode);
1693 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001695 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001696
1697 set_page_writeback(page);
1698
Maxim Patlasov4250c062012-10-26 19:48:07 +04001699 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700 if (!req)
1701 goto err;
1702
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001703 /* writeback always goes to bg_queue */
1704 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001705 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1706 if (!tmp_page)
1707 goto err_free;
1708
Miklos Szeredi72523422013-10-01 16:44:52 +02001709 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001710 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001711 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001712 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001713
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001714 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001715
1716 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001717 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001718 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001719 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001720 req->num_pages = 1;
1721 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001722 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001723 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001724 req->end = fuse_writepage_end;
1725 req->inode = inode;
1726
Tejun Heo93f78d82015-05-22 17:13:27 -04001727 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001728 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001729
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001730 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001731 list_add(&req->writepages_entry, &fi->writepages);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001734 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001735
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001736 end_page_writeback(page);
1737
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001738 return 0;
1739
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001740err_nofile:
1741 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001742err_free:
1743 fuse_request_free(req);
1744err:
Jeff Layton91839762017-05-25 06:57:50 -04001745 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001746 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001747 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001748}
1749
1750static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1751{
1752 int err;
1753
Miklos Szerediff17be02013-10-01 16:44:53 +02001754 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1755 /*
1756 * ->writepages() should be called for sync() and friends. We
1757 * should only get here on direct reclaim and then we are
1758 * allowed to skip a page which is already in flight
1759 */
1760 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1761
1762 redirty_page_for_writepage(wbc, page);
1763 return 0;
1764 }
1765
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001766 err = fuse_writepage_locked(page);
1767 unlock_page(page);
1768
1769 return err;
1770}
1771
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001772struct fuse_fill_wb_data {
1773 struct fuse_req *req;
1774 struct fuse_file *ff;
1775 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001776 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001777};
1778
1779static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1780{
1781 struct fuse_req *req = data->req;
1782 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001783 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001784 int num_pages = req->num_pages;
1785 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001786
1787 req->ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001788 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001789 list_add_tail(&req->list, &fi->queued_writes);
1790 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001791 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001792
1793 for (i = 0; i < num_pages; i++)
1794 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001795}
1796
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001797/*
1798 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001799 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1800 * one already added for a page at this offset. If there's none, then insert
1801 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001802 * copying the new page contents over to the old temporary page.
1803 */
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001804static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1805 struct page *page)
1806{
1807 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1808 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1809 struct fuse_req *tmp;
1810 struct fuse_req *old_req;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001811
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001812 WARN_ON(new_req->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001813
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001814 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001815 list_del(&new_req->writepages_entry);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001816 old_req = fuse_find_writeback(fi, page->index, page->index);
1817 if (!old_req) {
Maxim Patlasovf6011082013-10-02 15:01:07 +04001818 list_add(&new_req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001819 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001820 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001821 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001822
Maxim Patlasovf6011082013-10-02 15:01:07 +04001823 new_req->num_pages = 1;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001824 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001825 pgoff_t curr_index;
1826
1827 WARN_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001828 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001829 if (curr_index == page->index) {
1830 WARN_ON(tmp->num_pages != 1);
1831 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
Kirill Tkhaic5de16cc2018-11-26 12:46:20 +03001832 swap(tmp->pages[0], new_req->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001833 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001834 }
1835 }
1836
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001837 if (!tmp) {
1838 new_req->misc.write.next = old_req->misc.write.next;
1839 old_req->misc.write.next = new_req;
1840 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001841
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001842 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001843
1844 if (tmp) {
1845 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001846
Tejun Heo93f78d82015-05-22 17:13:27 -04001847 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredia2ebba82019-01-16 10:27:59 +01001848 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001849 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001850 fuse_writepage_free(fc, new_req);
1851 fuse_request_free(new_req);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001852 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001853
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001854 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001855}
1856
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001857static int fuse_writepages_fill(struct page *page,
1858 struct writeback_control *wbc, void *_data)
1859{
1860 struct fuse_fill_wb_data *data = _data;
1861 struct fuse_req *req = data->req;
1862 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001863 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001864 struct fuse_conn *fc = get_fuse_conn(inode);
1865 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001866 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001867 int err;
1868
1869 if (!data->ff) {
1870 err = -EIO;
1871 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1872 if (!data->ff)
1873 goto out_unlock;
1874 }
1875
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001876 /*
1877 * Being under writeback is unlikely but possible. For example direct
1878 * read to an mmaped fuse file will set the page dirty twice; once when
1879 * the pages are faulted with get_user_pages(), and then after the read
1880 * completed.
1881 */
1882 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001883
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001884 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001885 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001886 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001887 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1888 fuse_writepages_send(data);
1889 data->req = NULL;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001890 } else if (req && req->num_pages == req->max_pages) {
1891 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1892 fuse_writepages_send(data);
1893 req = data->req = NULL;
1894 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001895 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02001896
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001897 err = -ENOMEM;
1898 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1899 if (!tmp_page)
1900 goto out_unlock;
1901
1902 /*
1903 * The page must not be redirtied until the writeout is completed
1904 * (i.e. userspace has sent a reply to the write request). Otherwise
1905 * there could be more than one temporary page instance for each real
1906 * page.
1907 *
1908 * This is ensured by holding the page lock in page_mkwrite() while
1909 * checking fuse_page_is_writeback(). We already hold the page lock
1910 * since clear_page_dirty_for_io() and keep it held until we add the
1911 * request to the fi->writepages list and increment req->num_pages.
1912 * After this fuse_page_is_writeback() will indicate that the page is
1913 * under writeback, so we can release the page lock.
1914 */
1915 if (data->req == NULL) {
1916 struct fuse_inode *fi = get_fuse_inode(inode);
1917
1918 err = -ENOMEM;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001919 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001920 if (!req) {
1921 __free_page(tmp_page);
1922 goto out_unlock;
1923 }
1924
1925 fuse_write_fill(req, data->ff, page_offset(page), 0);
1926 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001927 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001928 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001929 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001930 req->num_pages = 0;
1931 req->end = fuse_writepage_end;
1932 req->inode = inode;
1933
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001934 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001935 list_add(&req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001936 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001937
1938 data->req = req;
1939 }
1940 set_page_writeback(page);
1941
1942 copy_highpage(tmp_page, page);
1943 req->pages[req->num_pages] = tmp_page;
1944 req->page_descs[req->num_pages].offset = 0;
1945 req->page_descs[req->num_pages].length = PAGE_SIZE;
1946
Tejun Heo93f78d82015-05-22 17:13:27 -04001947 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001948 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001949
1950 err = 0;
1951 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1952 end_page_writeback(page);
1953 data->req = NULL;
1954 goto out_unlock;
1955 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001956 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001957
1958 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001959 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001960 * fuse_page_is_writeback().
1961 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001962 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001963 req->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001964 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001965
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001966out_unlock:
1967 unlock_page(page);
1968
1969 return err;
1970}
1971
1972static int fuse_writepages(struct address_space *mapping,
1973 struct writeback_control *wbc)
1974{
1975 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001976 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001977 struct fuse_fill_wb_data data;
1978 int err;
1979
1980 err = -EIO;
1981 if (is_bad_inode(inode))
1982 goto out;
1983
1984 data.inode = inode;
1985 data.req = NULL;
1986 data.ff = NULL;
1987
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001988 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001989 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001990 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001991 GFP_NOFS);
1992 if (!data.orig_pages)
1993 goto out;
1994
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001995 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1996 if (data.req) {
1997 /* Ignore errors if we can write at least one page */
1998 BUG_ON(!data.req->num_pages);
1999 fuse_writepages_send(&data);
2000 err = 0;
2001 }
2002 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002003 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002004
2005 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002006out:
2007 return err;
2008}
2009
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002010/*
2011 * It's worthy to make sure that space is reserved on disk for the write,
2012 * but how to implement it without killing performance need more thinking.
2013 */
2014static int fuse_write_begin(struct file *file, struct address_space *mapping,
2015 loff_t pos, unsigned len, unsigned flags,
2016 struct page **pagep, void **fsdata)
2017{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002018 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002019 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002020 struct page *page;
2021 loff_t fsize;
2022 int err = -ENOMEM;
2023
2024 WARN_ON(!fc->writeback_cache);
2025
2026 page = grab_cache_page_write_begin(mapping, index, flags);
2027 if (!page)
2028 goto error;
2029
2030 fuse_wait_on_page_writeback(mapping->host, page->index);
2031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002032 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002033 goto success;
2034 /*
2035 * Check if the start this page comes after the end of file, in which
2036 * case the readpage can be optimized away.
2037 */
2038 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002039 if (fsize <= (pos & PAGE_MASK)) {
2040 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002041 if (off)
2042 zero_user_segment(page, 0, off);
2043 goto success;
2044 }
2045 err = fuse_do_readpage(file, page);
2046 if (err)
2047 goto cleanup;
2048success:
2049 *pagep = page;
2050 return 0;
2051
2052cleanup:
2053 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002054 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002055error:
2056 return err;
2057}
2058
2059static int fuse_write_end(struct file *file, struct address_space *mapping,
2060 loff_t pos, unsigned len, unsigned copied,
2061 struct page *page, void *fsdata)
2062{
2063 struct inode *inode = page->mapping->host;
2064
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002065 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2066 if (!copied)
2067 goto unlock;
2068
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002069 if (!PageUptodate(page)) {
2070 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002072 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002074 SetPageUptodate(page);
2075 }
2076
2077 fuse_write_update_size(inode, pos + copied);
2078 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002079
2080unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002081 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002083
2084 return copied;
2085}
2086
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002087static int fuse_launder_page(struct page *page)
2088{
2089 int err = 0;
2090 if (clear_page_dirty_for_io(page)) {
2091 struct inode *inode = page->mapping->host;
2092 err = fuse_writepage_locked(page);
2093 if (!err)
2094 fuse_wait_on_page_writeback(inode, page->index);
2095 }
2096 return err;
2097}
2098
2099/*
2100 * Write back dirty pages now, because there may not be any suitable
2101 * open files later
2102 */
2103static void fuse_vma_close(struct vm_area_struct *vma)
2104{
2105 filemap_write_and_wait(vma->vm_file->f_mapping);
2106}
2107
2108/*
2109 * Wait for writeback against this page to complete before allowing it
2110 * to be marked dirty again, and hence written back again, possibly
2111 * before the previous writepage completed.
2112 *
2113 * Block here, instead of in ->writepage(), so that the userspace fs
2114 * can only block processes actually operating on the filesystem.
2115 *
2116 * Otherwise unprivileged userspace fs would be able to block
2117 * unrelated:
2118 *
2119 * - page migration
2120 * - sync(2)
2121 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2122 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302123static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002124{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002125 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002126 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002127
Dave Jiang11bac802017-02-24 14:56:41 -08002128 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002129 lock_page(page);
2130 if (page->mapping != inode->i_mapping) {
2131 unlock_page(page);
2132 return VM_FAULT_NOPAGE;
2133 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002134
2135 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002136 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002137}
2138
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002139static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002140 .close = fuse_vma_close,
2141 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002142 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002143 .page_mkwrite = fuse_page_mkwrite,
2144};
2145
2146static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2147{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002148 struct fuse_file *ff = file->private_data;
2149
2150 if (ff->open_flags & FOPEN_DIRECT_IO) {
2151 /* Can't provide the coherency needed for MAP_SHARED */
2152 if (vma->vm_flags & VM_MAYSHARE)
2153 return -ENODEV;
2154
2155 invalidate_inode_pages2(file->f_mapping);
2156
2157 return generic_file_mmap(file, vma);
2158 }
2159
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002160 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2161 fuse_link_write_file(file);
2162
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002163 file_accessed(file);
2164 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002165 return 0;
2166}
2167
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002168static int convert_fuse_file_lock(struct fuse_conn *fc,
2169 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002170 struct file_lock *fl)
2171{
2172 switch (ffl->type) {
2173 case F_UNLCK:
2174 break;
2175
2176 case F_RDLCK:
2177 case F_WRLCK:
2178 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2179 ffl->end < ffl->start)
2180 return -EIO;
2181
2182 fl->fl_start = ffl->start;
2183 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002184
2185 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002186 * Convert pid into init's pid namespace. The locks API will
2187 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002188 */
2189 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002190 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002191 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002192 break;
2193
2194 default:
2195 return -EIO;
2196 }
2197 fl->fl_type = ffl->type;
2198 return 0;
2199}
2200
Miklos Szeredi70781872014-12-12 09:49:05 +01002201static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002202 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002203 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002204{
Al Viro6131ffa2013-02-27 16:59:05 -05002205 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002206 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002207 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002208
Miklos Szeredi70781872014-12-12 09:49:05 +01002209 memset(inarg, 0, sizeof(*inarg));
2210 inarg->fh = ff->fh;
2211 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2212 inarg->lk.start = fl->fl_start;
2213 inarg->lk.end = fl->fl_end;
2214 inarg->lk.type = fl->fl_type;
2215 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002216 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002217 inarg->lk_flags |= FUSE_LK_FLOCK;
2218 args->in.h.opcode = opcode;
2219 args->in.h.nodeid = get_node_id(inode);
2220 args->in.numargs = 1;
2221 args->in.args[0].size = sizeof(*inarg);
2222 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002223}
2224
2225static int fuse_getlk(struct file *file, struct file_lock *fl)
2226{
Al Viro6131ffa2013-02-27 16:59:05 -05002227 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002228 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002229 FUSE_ARGS(args);
2230 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002231 struct fuse_lk_out outarg;
2232 int err;
2233
Miklos Szeredi70781872014-12-12 09:49:05 +01002234 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2235 args.out.numargs = 1;
2236 args.out.args[0].size = sizeof(outarg);
2237 args.out.args[0].value = &outarg;
2238 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002239 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002240 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002241
2242 return err;
2243}
2244
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002245static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002246{
Al Viro6131ffa2013-02-27 16:59:05 -05002247 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002248 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002249 FUSE_ARGS(args);
2250 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002251 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002252 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2253 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002254 int err;
2255
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002256 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002257 /* NLM needs asynchronous locks, which we don't support yet */
2258 return -ENOLCK;
2259 }
2260
Miklos Szeredi71421252006-06-25 05:48:52 -07002261 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002262 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002263 return 0;
2264
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002265 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002266 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002267
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002268 /* locking is restartable */
2269 if (err == -EINTR)
2270 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002271
Miklos Szeredi71421252006-06-25 05:48:52 -07002272 return err;
2273}
2274
2275static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2276{
Al Viro6131ffa2013-02-27 16:59:05 -05002277 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002278 struct fuse_conn *fc = get_fuse_conn(inode);
2279 int err;
2280
Miklos Szeredi48e90762008-07-25 01:49:02 -07002281 if (cmd == F_CANCELLK) {
2282 err = 0;
2283 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002284 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002285 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002286 err = 0;
2287 } else
2288 err = fuse_getlk(file, fl);
2289 } else {
2290 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002291 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002292 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002293 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002294 }
2295 return err;
2296}
2297
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002298static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2299{
Al Viro6131ffa2013-02-27 16:59:05 -05002300 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002301 struct fuse_conn *fc = get_fuse_conn(inode);
2302 int err;
2303
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002304 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002305 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002306 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002307 struct fuse_file *ff = file->private_data;
2308
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002309 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002310 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002311 err = fuse_setlk(file, fl, 1);
2312 }
2313
2314 return err;
2315}
2316
Miklos Szeredib2d22722006-12-06 20:35:51 -08002317static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2318{
2319 struct inode *inode = mapping->host;
2320 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002321 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002322 struct fuse_bmap_in inarg;
2323 struct fuse_bmap_out outarg;
2324 int err;
2325
2326 if (!inode->i_sb->s_bdev || fc->no_bmap)
2327 return 0;
2328
Miklos Szeredib2d22722006-12-06 20:35:51 -08002329 memset(&inarg, 0, sizeof(inarg));
2330 inarg.block = block;
2331 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002332 args.in.h.opcode = FUSE_BMAP;
2333 args.in.h.nodeid = get_node_id(inode);
2334 args.in.numargs = 1;
2335 args.in.args[0].size = sizeof(inarg);
2336 args.in.args[0].value = &inarg;
2337 args.out.numargs = 1;
2338 args.out.args[0].size = sizeof(outarg);
2339 args.out.args[0].value = &outarg;
2340 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002341 if (err == -ENOSYS)
2342 fc->no_bmap = 1;
2343
2344 return err ? 0 : outarg.block;
2345}
2346
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302347static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2348{
2349 struct inode *inode = file->f_mapping->host;
2350 struct fuse_conn *fc = get_fuse_conn(inode);
2351 struct fuse_file *ff = file->private_data;
2352 FUSE_ARGS(args);
2353 struct fuse_lseek_in inarg = {
2354 .fh = ff->fh,
2355 .offset = offset,
2356 .whence = whence
2357 };
2358 struct fuse_lseek_out outarg;
2359 int err;
2360
2361 if (fc->no_lseek)
2362 goto fallback;
2363
2364 args.in.h.opcode = FUSE_LSEEK;
2365 args.in.h.nodeid = ff->nodeid;
2366 args.in.numargs = 1;
2367 args.in.args[0].size = sizeof(inarg);
2368 args.in.args[0].value = &inarg;
2369 args.out.numargs = 1;
2370 args.out.args[0].size = sizeof(outarg);
2371 args.out.args[0].value = &outarg;
2372 err = fuse_simple_request(fc, &args);
2373 if (err) {
2374 if (err == -ENOSYS) {
2375 fc->no_lseek = 1;
2376 goto fallback;
2377 }
2378 return err;
2379 }
2380
2381 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2382
2383fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002384 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302385 if (!err)
2386 return generic_file_llseek(file, offset, whence);
2387 else
2388 return err;
2389}
2390
Andrew Morton965c8e52012-12-17 15:59:39 -08002391static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002392{
2393 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002394 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002395
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302396 switch (whence) {
2397 case SEEK_SET:
2398 case SEEK_CUR:
2399 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002400 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302401 break;
2402 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002403 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002404 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302405 if (!retval)
2406 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002407 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302408 break;
2409 case SEEK_HOLE:
2410 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002411 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302412 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002413 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302414 break;
2415 default:
2416 retval = -EINVAL;
2417 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002418
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002419 return retval;
2420}
2421
Tejun Heo59efec72008-11-26 12:03:55 +01002422/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002423 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2424 * ABI was defined to be 'struct iovec' which is different on 32bit
2425 * and 64bit. Fortunately we can determine which structure the server
2426 * used from the size of the reply.
2427 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002428static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2429 size_t transferred, unsigned count,
2430 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002431{
2432#ifdef CONFIG_COMPAT
2433 if (count * sizeof(struct compat_iovec) == transferred) {
2434 struct compat_iovec *ciov = src;
2435 unsigned i;
2436
2437 /*
2438 * With this interface a 32bit server cannot support
2439 * non-compat (i.e. ones coming from 64bit apps) ioctl
2440 * requests
2441 */
2442 if (!is_compat)
2443 return -EINVAL;
2444
2445 for (i = 0; i < count; i++) {
2446 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2447 dst[i].iov_len = ciov[i].iov_len;
2448 }
2449 return 0;
2450 }
2451#endif
2452
2453 if (count * sizeof(struct iovec) != transferred)
2454 return -EIO;
2455
2456 memcpy(dst, src, transferred);
2457 return 0;
2458}
2459
Miklos Szeredi75727772010-11-30 16:39:27 +01002460/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002461static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2462 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002463{
2464 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002465 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002466
Zach Brownfb6ccff2012-07-24 12:10:11 -07002467 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002468 if (iov->iov_len > (size_t) max)
2469 return -ENOMEM;
2470 max -= iov->iov_len;
2471 }
2472 return 0;
2473}
2474
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002475static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2476 void *src, size_t transferred, unsigned count,
2477 bool is_compat)
2478{
2479 unsigned i;
2480 struct fuse_ioctl_iovec *fiov = src;
2481
2482 if (fc->minor < 16) {
2483 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2484 count, is_compat);
2485 }
2486
2487 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2488 return -EIO;
2489
2490 for (i = 0; i < count; i++) {
2491 /* Did the server supply an inappropriate value? */
2492 if (fiov[i].base != (unsigned long) fiov[i].base ||
2493 fiov[i].len != (unsigned long) fiov[i].len)
2494 return -EIO;
2495
2496 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2497 dst[i].iov_len = (size_t) fiov[i].len;
2498
2499#ifdef CONFIG_COMPAT
2500 if (is_compat &&
2501 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2502 (compat_size_t) dst[i].iov_len != fiov[i].len))
2503 return -EIO;
2504#endif
2505 }
2506
2507 return 0;
2508}
2509
2510
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002511/*
Tejun Heo59efec72008-11-26 12:03:55 +01002512 * For ioctls, there is no generic way to determine how much memory
2513 * needs to be read and/or written. Furthermore, ioctls are allowed
2514 * to dereference the passed pointer, so the parameter requires deep
2515 * copying but FUSE has no idea whatsoever about what to copy in or
2516 * out.
2517 *
2518 * This is solved by allowing FUSE server to retry ioctl with
2519 * necessary in/out iovecs. Let's assume the ioctl implementation
2520 * needs to read in the following structure.
2521 *
2522 * struct a {
2523 * char *buf;
2524 * size_t buflen;
2525 * }
2526 *
2527 * On the first callout to FUSE server, inarg->in_size and
2528 * inarg->out_size will be NULL; then, the server completes the ioctl
2529 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2530 * the actual iov array to
2531 *
2532 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2533 *
2534 * which tells FUSE to copy in the requested area and retry the ioctl.
2535 * On the second round, the server has access to the structure and
2536 * from that it can tell what to look for next, so on the invocation,
2537 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2538 *
2539 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2540 * { .iov_base = a.buf, .iov_len = a.buflen } }
2541 *
2542 * FUSE will copy both struct a and the pointed buffer from the
2543 * process doing the ioctl and retry ioctl with both struct a and the
2544 * buffer.
2545 *
2546 * This time, FUSE server has everything it needs and completes ioctl
2547 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2548 *
2549 * Copying data out works the same way.
2550 *
2551 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2552 * automatically initializes in and out iovs by decoding @cmd with
2553 * _IOC_* macros and the server is not allowed to request RETRY. This
2554 * limits ioctl data transfers to well-formed ioctls and is the forced
2555 * behavior for all FUSE servers.
2556 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002557long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2558 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002559{
Tejun Heo59efec72008-11-26 12:03:55 +01002560 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002561 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002562 struct fuse_ioctl_in inarg = {
2563 .fh = ff->fh,
2564 .cmd = cmd,
2565 .arg = arg,
2566 .flags = flags
2567 };
2568 struct fuse_ioctl_out outarg;
2569 struct fuse_req *req = NULL;
2570 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002571 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002572 struct iovec *in_iov = NULL, *out_iov = NULL;
2573 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002574 size_t in_size, out_size, transferred, c;
2575 int err, i;
2576 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002577
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002578#if BITS_PER_LONG == 32
2579 inarg.flags |= FUSE_IOCTL_32BIT;
2580#else
2581 if (flags & FUSE_IOCTL_COMPAT)
2582 inarg.flags |= FUSE_IOCTL_32BIT;
2583#endif
2584
Tejun Heo59efec72008-11-26 12:03:55 +01002585 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002586 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002587
Tejun Heo59efec72008-11-26 12:03:55 +01002588 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002589 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002590 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002591 if (!pages || !iov_page)
2592 goto out;
2593
2594 /*
2595 * If restricted, initialize IO parameters as encoded in @cmd.
2596 * RETRY from server is not allowed.
2597 */
2598 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002599 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002600
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002601 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002602 iov->iov_len = _IOC_SIZE(cmd);
2603
2604 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2605 in_iov = iov;
2606 in_iovs = 1;
2607 }
2608
2609 if (_IOC_DIR(cmd) & _IOC_READ) {
2610 out_iov = iov;
2611 out_iovs = 1;
2612 }
2613 }
2614
2615 retry:
2616 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2617 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2618
2619 /*
2620 * Out data can be used either for actual out data or iovs,
2621 * make sure there always is at least one page.
2622 */
2623 out_size = max_t(size_t, out_size, PAGE_SIZE);
2624 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2625
2626 /* make sure there are enough buffer pages and init request with them */
2627 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002628 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002629 goto out;
2630 while (num_pages < max_pages) {
2631 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2632 if (!pages[num_pages])
2633 goto out;
2634 num_pages++;
2635 }
2636
Maxim Patlasov54b96672012-10-26 19:49:13 +04002637 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002638 if (IS_ERR(req)) {
2639 err = PTR_ERR(req);
2640 req = NULL;
2641 goto out;
2642 }
2643 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2644 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002645 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002646
2647 /* okay, let's send it to the client */
2648 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002649 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002650 req->in.numargs = 1;
2651 req->in.args[0].size = sizeof(inarg);
2652 req->in.args[0].value = &inarg;
2653 if (in_size) {
2654 req->in.numargs++;
2655 req->in.args[1].size = in_size;
2656 req->in.argpages = 1;
2657
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002658 err = -EFAULT;
2659 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2660 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2661 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2662 if (c != PAGE_SIZE && iov_iter_count(&ii))
2663 goto out;
2664 }
Tejun Heo59efec72008-11-26 12:03:55 +01002665 }
2666
2667 req->out.numargs = 2;
2668 req->out.args[0].size = sizeof(outarg);
2669 req->out.args[0].value = &outarg;
2670 req->out.args[1].size = out_size;
2671 req->out.argpages = 1;
2672 req->out.argvar = 1;
2673
Tejun Heob93f8582008-11-26 12:03:55 +01002674 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002675 err = req->out.h.error;
2676 transferred = req->out.args[1].size;
2677 fuse_put_request(fc, req);
2678 req = NULL;
2679 if (err)
2680 goto out;
2681
2682 /* did it ask for retry? */
2683 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002684 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002685
2686 /* no retry if in restricted mode */
2687 err = -EIO;
2688 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2689 goto out;
2690
2691 in_iovs = outarg.in_iovs;
2692 out_iovs = outarg.out_iovs;
2693
2694 /*
2695 * Make sure things are in boundary, separate checks
2696 * are to protect against overflow.
2697 */
2698 err = -ENOMEM;
2699 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2700 out_iovs > FUSE_IOCTL_MAX_IOV ||
2701 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2702 goto out;
2703
Cong Wang2408f6e2011-11-25 23:14:30 +08002704 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002705 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002706 transferred, in_iovs + out_iovs,
2707 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002708 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002709 if (err)
2710 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002711
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002712 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002713 out_iov = in_iov + in_iovs;
2714
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002715 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002716 if (err)
2717 goto out;
2718
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002719 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002720 if (err)
2721 goto out;
2722
Tejun Heo59efec72008-11-26 12:03:55 +01002723 goto retry;
2724 }
2725
2726 err = -EIO;
2727 if (transferred > inarg.out_size)
2728 goto out;
2729
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002730 err = -EFAULT;
2731 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2732 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2733 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2734 if (c != PAGE_SIZE && iov_iter_count(&ii))
2735 goto out;
2736 }
2737 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002738 out:
2739 if (req)
2740 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002741 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002742 while (num_pages)
2743 __free_page(pages[--num_pages]);
2744 kfree(pages);
2745
2746 return err ? err : outarg.result;
2747}
Tejun Heo08cbf542009-04-14 10:54:53 +09002748EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002749
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002750long fuse_ioctl_common(struct file *file, unsigned int cmd,
2751 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002752{
Al Viro6131ffa2013-02-27 16:59:05 -05002753 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002754 struct fuse_conn *fc = get_fuse_conn(inode);
2755
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002756 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002757 return -EACCES;
2758
2759 if (is_bad_inode(inode))
2760 return -EIO;
2761
2762 return fuse_do_ioctl(file, cmd, arg, flags);
2763}
2764
Tejun Heo59efec72008-11-26 12:03:55 +01002765static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2766 unsigned long arg)
2767{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002768 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002769}
2770
2771static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2772 unsigned long arg)
2773{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002774 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002775}
2776
Tejun Heo95668a62008-11-26 12:03:55 +01002777/*
2778 * All files which have been polled are linked to RB tree
2779 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2780 * find the matching one.
2781 */
2782static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2783 struct rb_node **parent_out)
2784{
2785 struct rb_node **link = &fc->polled_files.rb_node;
2786 struct rb_node *last = NULL;
2787
2788 while (*link) {
2789 struct fuse_file *ff;
2790
2791 last = *link;
2792 ff = rb_entry(last, struct fuse_file, polled_node);
2793
2794 if (kh < ff->kh)
2795 link = &last->rb_left;
2796 else if (kh > ff->kh)
2797 link = &last->rb_right;
2798 else
2799 return link;
2800 }
2801
2802 if (parent_out)
2803 *parent_out = last;
2804 return link;
2805}
2806
2807/*
2808 * The file is about to be polled. Make sure it's on the polled_files
2809 * RB tree. Note that files once added to the polled_files tree are
2810 * not removed before the file is released. This is because a file
2811 * polled once is likely to be polled again.
2812 */
2813static void fuse_register_polled_file(struct fuse_conn *fc,
2814 struct fuse_file *ff)
2815{
2816 spin_lock(&fc->lock);
2817 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002818 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002819
2820 link = fuse_find_polled_node(fc, ff->kh, &parent);
2821 BUG_ON(*link);
2822 rb_link_node(&ff->polled_node, parent, link);
2823 rb_insert_color(&ff->polled_node, &fc->polled_files);
2824 }
2825 spin_unlock(&fc->lock);
2826}
2827
Al Viro076ccb72017-07-03 01:02:18 -04002828__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002829{
Tejun Heo95668a62008-11-26 12:03:55 +01002830 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002831 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002832 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2833 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002834 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002835 int err;
2836
2837 if (fc->no_poll)
2838 return DEFAULT_POLLMASK;
2839
2840 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002841 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002842
2843 /*
2844 * Ask for notification iff there's someone waiting for it.
2845 * The client may ignore the flag and always notify.
2846 */
2847 if (waitqueue_active(&ff->poll_wait)) {
2848 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2849 fuse_register_polled_file(fc, ff);
2850 }
2851
Miklos Szeredi70781872014-12-12 09:49:05 +01002852 args.in.h.opcode = FUSE_POLL;
2853 args.in.h.nodeid = ff->nodeid;
2854 args.in.numargs = 1;
2855 args.in.args[0].size = sizeof(inarg);
2856 args.in.args[0].value = &inarg;
2857 args.out.numargs = 1;
2858 args.out.args[0].size = sizeof(outarg);
2859 args.out.args[0].value = &outarg;
2860 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002861
2862 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002863 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002864 if (err == -ENOSYS) {
2865 fc->no_poll = 1;
2866 return DEFAULT_POLLMASK;
2867 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002868 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002869}
Tejun Heo08cbf542009-04-14 10:54:53 +09002870EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002871
2872/*
2873 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2874 * wakes up the poll waiters.
2875 */
2876int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2877 struct fuse_notify_poll_wakeup_out *outarg)
2878{
2879 u64 kh = outarg->kh;
2880 struct rb_node **link;
2881
2882 spin_lock(&fc->lock);
2883
2884 link = fuse_find_polled_node(fc, kh, NULL);
2885 if (*link) {
2886 struct fuse_file *ff;
2887
2888 ff = rb_entry(*link, struct fuse_file, polled_node);
2889 wake_up_interruptible_sync(&ff->poll_wait);
2890 }
2891
2892 spin_unlock(&fc->lock);
2893 return 0;
2894}
2895
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002896static void fuse_do_truncate(struct file *file)
2897{
2898 struct inode *inode = file->f_mapping->host;
2899 struct iattr attr;
2900
2901 attr.ia_valid = ATTR_SIZE;
2902 attr.ia_size = i_size_read(inode);
2903
2904 attr.ia_file = file;
2905 attr.ia_valid |= ATTR_FILE;
2906
Jan Kara62490332016-05-26 17:12:41 +02002907 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002908}
2909
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002910static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002911{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002912 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002913}
2914
Anand Avati4273b792012-02-17 12:46:25 -05002915static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002916fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002917{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002918 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002919 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002920 struct file *file = iocb->ki_filp;
2921 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002922 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002923 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002924 struct inode *inode;
2925 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002926 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002927 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002928 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002929
Anand Avati4273b792012-02-17 12:46:25 -05002930 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002931 inode = file->f_mapping->host;
2932 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002933
Omar Sandoval6f673762015-03-16 04:33:52 -07002934 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002935 return 0;
2936
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002937 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002938 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002939 if (offset >= i_size)
2940 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002941 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002942 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002943 }
2944
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002945 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002946 if (!io)
2947 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002948 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002949 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002950 io->reqs = 1;
2951 io->bytes = -1;
2952 io->size = 0;
2953 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002954 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002955 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002956 /*
2957 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002958 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002959 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002960 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002961 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302962 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002963
2964 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302965 * We cannot asynchronously extend the size of a file.
2966 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002967 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302968 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2969 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002970
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302971 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002972 /*
2973 * Additional reference to keep io around after
2974 * calling fuse_aio_complete()
2975 */
2976 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002977 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002978 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002979
Omar Sandoval6f673762015-03-16 04:33:52 -07002980 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002981 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002982 fuse_invalidate_attr(inode);
2983 } else {
Al Virod22a9432014-03-16 15:50:47 -04002984 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002985 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002986
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002987 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002988 bool blocking = io->blocking;
2989
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002990 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2991
2992 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002993 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002994 return -EIOCBQUEUED;
2995
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002996 wait_for_completion(&wait);
2997 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002998 }
2999
Seth Forshee744742d2016-03-11 10:35:34 -06003000 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003001
Omar Sandoval6f673762015-03-16 04:33:52 -07003002 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003003 if (ret > 0)
3004 fuse_write_update_size(inode, pos);
3005 else if (ret < 0 && offset + count > i_size)
3006 fuse_do_truncate(file);
3007 }
Anand Avati4273b792012-02-17 12:46:25 -05003008
3009 return ret;
3010}
3011
Miklos Szeredicdadb112012-11-10 16:55:56 +01003012static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3013 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003014{
3015 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003016 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003017 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003018 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01003019 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003020 struct fuse_fallocate_in inarg = {
3021 .fh = ff->fh,
3022 .offset = offset,
3023 .length = length,
3024 .mode = mode
3025 };
3026 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003027 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3028 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003029
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003030 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3031 return -EOPNOTSUPP;
3032
Miklos Szeredi519c6042012-04-26 10:56:36 +02003033 if (fc->no_fallocate)
3034 return -EOPNOTSUPP;
3035
Maxim Patlasov14c14412013-06-13 12:16:39 +04003036 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003037 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003038 if (mode & FALLOC_FL_PUNCH_HOLE) {
3039 loff_t endbyte = offset + length - 1;
3040 err = filemap_write_and_wait_range(inode->i_mapping,
3041 offset, endbyte);
3042 if (err)
3043 goto out;
3044
3045 fuse_sync_writes(inode);
3046 }
Brian Foster3634a632013-05-17 09:30:32 -04003047 }
3048
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003049 if (!(mode & FALLOC_FL_KEEP_SIZE))
3050 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3051
Miklos Szeredi70781872014-12-12 09:49:05 +01003052 args.in.h.opcode = FUSE_FALLOCATE;
3053 args.in.h.nodeid = ff->nodeid;
3054 args.in.numargs = 1;
3055 args.in.args[0].size = sizeof(inarg);
3056 args.in.args[0].value = &inarg;
3057 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003058 if (err == -ENOSYS) {
3059 fc->no_fallocate = 1;
3060 err = -EOPNOTSUPP;
3061 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003062 if (err)
3063 goto out;
3064
3065 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003066 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3067 bool changed = fuse_write_update_size(inode, offset + length);
3068
Miklos Szeredi93d22692014-04-28 14:19:22 +02003069 if (changed && fc->writeback_cache)
3070 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003071 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003072
3073 if (mode & FALLOC_FL_PUNCH_HOLE)
3074 truncate_pagecache_range(inode, offset, offset + length - 1);
3075
3076 fuse_invalidate_attr(inode);
3077
Brian Foster3634a632013-05-17 09:30:32 -04003078out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003079 if (!(mode & FALLOC_FL_KEEP_SIZE))
3080 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3081
Maxim Patlasovbde52782013-09-13 19:19:54 +04003082 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003083 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003084
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003085 return err;
3086}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003087
Niels de Vos88bc7d52018-08-21 14:36:31 +02003088static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3089 struct file *file_out, loff_t pos_out,
3090 size_t len, unsigned int flags)
3091{
3092 struct fuse_file *ff_in = file_in->private_data;
3093 struct fuse_file *ff_out = file_out->private_data;
3094 struct inode *inode_out = file_inode(file_out);
3095 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3096 struct fuse_conn *fc = ff_in->fc;
3097 FUSE_ARGS(args);
3098 struct fuse_copy_file_range_in inarg = {
3099 .fh_in = ff_in->fh,
3100 .off_in = pos_in,
3101 .nodeid_out = ff_out->nodeid,
3102 .fh_out = ff_out->fh,
3103 .off_out = pos_out,
3104 .len = len,
3105 .flags = flags
3106 };
3107 struct fuse_write_out outarg;
3108 ssize_t err;
3109 /* mark unstable when write-back is not used, and file_out gets
3110 * extended */
3111 bool is_unstable = (!fc->writeback_cache) &&
3112 ((pos_out + len) > inode_out->i_size);
3113
3114 if (fc->no_copy_file_range)
3115 return -EOPNOTSUPP;
3116
3117 inode_lock(inode_out);
3118
3119 if (fc->writeback_cache) {
3120 err = filemap_write_and_wait_range(inode_out->i_mapping,
3121 pos_out, pos_out + len);
3122 if (err)
3123 goto out;
3124
3125 fuse_sync_writes(inode_out);
3126 }
3127
3128 if (is_unstable)
3129 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3130
3131 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3132 args.in.h.nodeid = ff_in->nodeid;
3133 args.in.numargs = 1;
3134 args.in.args[0].size = sizeof(inarg);
3135 args.in.args[0].value = &inarg;
3136 args.out.numargs = 1;
3137 args.out.args[0].size = sizeof(outarg);
3138 args.out.args[0].value = &outarg;
3139 err = fuse_simple_request(fc, &args);
3140 if (err == -ENOSYS) {
3141 fc->no_copy_file_range = 1;
3142 err = -EOPNOTSUPP;
3143 }
3144 if (err)
3145 goto out;
3146
3147 if (fc->writeback_cache) {
3148 fuse_write_update_size(inode_out, pos_out + outarg.size);
3149 file_update_time(file_out);
3150 }
3151
3152 fuse_invalidate_attr(inode_out);
3153
3154 err = outarg.size;
3155out:
3156 if (is_unstable)
3157 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3158
3159 inode_unlock(inode_out);
3160
3161 return err;
3162}
3163
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003164static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003165 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003166 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003167 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003168 .mmap = fuse_file_mmap,
3169 .open = fuse_open,
3170 .flush = fuse_flush,
3171 .release = fuse_release,
3172 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003173 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003174 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003175 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003176 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003177 .unlocked_ioctl = fuse_file_ioctl,
3178 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003179 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003180 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003181 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003182};
3183
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003184static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003185 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003186 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003187 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003188 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003189 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003190 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003191 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003192 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003193 .write_begin = fuse_write_begin,
3194 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003195};
3196
3197void fuse_init_file_inode(struct inode *inode)
3198{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003199 struct fuse_inode *fi = get_fuse_inode(inode);
3200
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003201 inode->i_fop = &fuse_file_operations;
3202 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003203
3204 INIT_LIST_HEAD(&fi->write_files);
3205 INIT_LIST_HEAD(&fi->queued_writes);
3206 fi->writectr = 0;
3207 init_waitqueue_head(&fi->page_waitq);
3208 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003209}