blob: 26c2523120bc893c873f13be83ceeb0033702eb5 [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
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080022static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070023
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020024static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
25 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070027 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010028 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080029
30 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070031 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010034 args.in.h.opcode = opcode;
35 args.in.h.nodeid = nodeid;
36 args.in.numargs = 1;
37 args.in.args[0].size = sizeof(inarg);
38 args.in.args[0].value = &inarg;
39 args.out.numargs = 1;
40 args.out.args[0].size = sizeof(*outargp);
41 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080042
Miklos Szeredi70781872014-12-12 09:49:05 +010043 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080044}
45
Tejun Heoacf99432008-11-26 12:03:55 +010046struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047{
48 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090049
Mateusz Jurczyk68227c02017-06-07 12:26:49 +020050 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090051 if (unlikely(!ff))
52 return NULL;
53
Miklos Szeredida5e4712009-04-28 16:56:36 +020054 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040055 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090056 if (unlikely(!ff->reserved_req)) {
57 kfree(ff);
58 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080059 }
Tejun Heo6b2db282009-04-14 10:54:49 +090060
61 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020062 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020063 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090064 RB_CLEAR_NODE(&ff->polled_node);
65 init_waitqueue_head(&ff->poll_wait);
66
67 spin_lock(&fc->lock);
68 ff->kh = ++fc->khctr;
69 spin_unlock(&fc->lock);
70
Miklos Szeredifd72faa2005-11-07 00:59:51 -080071 return ff;
72}
73
74void fuse_file_free(struct fuse_file *ff)
75{
Miklos Szeredi33649c92006-06-25 05:48:52 -070076 fuse_request_free(ff->reserved_req);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020077 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080078 kfree(ff);
79}
80
Miklos Szeredi267d8442017-02-22 20:08:25 +010081static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070082{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020083 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070084 return ff;
85}
86
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010087static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
88{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010089 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010090}
91
Chad Austin2e64ff12018-12-10 10:54:52 -080092static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070093{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020094 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -070095 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020096
Chad Austin2e64ff12018-12-10 10:54:52 -080097 if (ff->fc->no_open && !isdir) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +010098 /*
99 * Drop the release request when client does not
100 * implement 'open'
101 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200102 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100103 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100104 fuse_put_request(ff->fc, req);
105 } else if (sync) {
Miklos Szeredi2e38bea2017-02-22 20:08:25 +0100106 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200107 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100108 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100109 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100110 fuse_put_request(ff->fc, req);
111 } else {
112 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200113 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100114 fuse_request_send_background(ff->fc, req);
115 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700116 kfree(ff);
117 }
118}
119
Tejun Heo08cbf542009-04-14 10:54:53 +0900120int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
121 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200122{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200123 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200124 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
125
126 ff = fuse_file_alloc(fc);
127 if (!ff)
128 return -ENOMEM;
129
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 ff->fh = 0;
131 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
132 if (!fc->no_open || isdir) {
133 struct fuse_open_out outarg;
134 int err;
135
136 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
137 if (!err) {
138 ff->fh = outarg.fh;
139 ff->open_flags = outarg.open_flags;
140
141 } else if (err != -ENOSYS || isdir) {
142 fuse_file_free(ff);
143 return err;
144 } else {
145 fc->no_open = 1;
146 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200147 }
148
149 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100150 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200151
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200152 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100153 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154
155 return 0;
156}
Tejun Heo08cbf542009-04-14 10:54:53 +0900157EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200158
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400159static void fuse_link_write_file(struct file *file)
160{
161 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400162 struct fuse_inode *fi = get_fuse_inode(inode);
163 struct fuse_file *ff = file->private_data;
164 /*
165 * file may be written through mmap, so chain it onto the
166 * inodes's write_file list
167 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300168 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400169 if (list_empty(&ff->write_entry))
170 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300171 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400172}
173
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800175{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800177 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178
179 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800180 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200181 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700182 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200183 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200184 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800185 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
186 struct fuse_inode *fi = get_fuse_inode(inode);
187
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300188 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300189 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800190 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300191 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800192 fuse_invalidate_attr(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200193 if (fc->writeback_cache)
194 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800195 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400196 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
197 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800198}
199
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200200int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800201{
Tejun Heoacf99432008-11-26 12:03:55 +0100202 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700203 int err;
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200204 bool lock_inode = (file->f_flags & O_TRUNC) &&
205 fc->atomic_o_trunc &&
206 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700207
208 err = generic_file_open(inode, file);
209 if (err)
210 return err;
211
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200212 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500213 inode_lock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200214
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200215 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700216
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200217 if (!err)
218 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200219
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200220 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500221 inode_unlock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200222
223 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700224}
225
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300226static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
227 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800228{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200229 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700230 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800231 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700232
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300233 /* Inode is NULL on error path of fuse_create_open() */
234 if (likely(fi)) {
235 spin_lock(&fi->lock);
236 list_del(&ff->write_entry);
237 spin_unlock(&fi->lock);
238 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200239 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200240 if (!RB_EMPTY_NODE(&ff->polled_node))
241 rb_erase(&ff->polled_node, &fc->polled_files);
242 spin_unlock(&fc->lock);
243
Bryan Green357ccf22011-03-01 16:43:52 -0800244 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200245
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700246 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800247 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700248 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200249 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700250 req->in.numargs = 1;
251 req->in.args[0].size = sizeof(struct fuse_release_in);
252 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800253}
254
Chad Austin2e64ff12018-12-10 10:54:52 -0800255void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800256{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300257 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100258 struct fuse_file *ff = file->private_data;
259 struct fuse_req *req = ff->reserved_req;
Chad Austin2e64ff12018-12-10 10:54:52 -0800260 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700261
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300262 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100263
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200264 if (ff->flock) {
265 struct fuse_release_in *inarg = &req->misc.release.in;
266 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
267 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
268 (fl_owner_t) file);
269 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100270 /* Hold inode until release is finished */
271 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700272
Tejun Heo6b2db282009-04-14 10:54:49 +0900273 /*
274 * Normally this will send the RELEASE request, however if
275 * some asynchronous READ or WRITE requests are outstanding,
276 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100277 *
278 * Make the release synchronous if this is a fuseblk mount,
279 * synchronous RELEASE is allowed (and desirable) in this case
280 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900281 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800282 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700283}
284
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700285static int fuse_open(struct inode *inode, struct file *file)
286{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200287 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700288}
289
290static int fuse_release(struct inode *inode, struct file *file)
291{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400292 struct fuse_conn *fc = get_fuse_conn(inode);
293
294 /* see fuse_vma_close() for !writeback_cache case */
295 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200296 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400297
Chad Austin2e64ff12018-12-10 10:54:52 -0800298 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200299
300 /* return value is ignored by VFS */
301 return 0;
302}
303
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300304void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200305{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200306 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300307 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100308 /*
309 * iput(NULL) is a no-op and since the refcount is 1 and everything's
310 * synchronous, we are fine with not doing igrab() here"
311 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800312 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700313}
Tejun Heo08cbf542009-04-14 10:54:53 +0900314EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700315
Miklos Szeredi71421252006-06-25 05:48:52 -0700316/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700317 * Scramble the ID space with XTEA, so that the value of the files_struct
318 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700319 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700320u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700321{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700322 u32 *k = fc->scramble_key;
323 u64 v = (unsigned long) id;
324 u32 v0 = v;
325 u32 v1 = v >> 32;
326 u32 sum = 0;
327 int i;
328
329 for (i = 0; i < 32; i++) {
330 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
331 sum += 0x9E3779B9;
332 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
333 }
334
335 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700336}
337
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100338static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
339 pgoff_t idx_from, pgoff_t idx_to)
340{
341 struct fuse_req *req;
342
343 list_for_each_entry(req, &fi->writepages, writepages_entry) {
344 pgoff_t curr_index;
345
346 WARN_ON(get_fuse_inode(req->inode) != fi);
347 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
348 if (idx_from < curr_index + req->num_pages &&
349 curr_index <= idx_to) {
350 return req;
351 }
352 }
353 return NULL;
354}
355
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700356/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400357 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700358 *
359 * This is currently done by walking the list of writepage requests
360 * for the inode, which can be pretty inefficient.
361 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400362static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
363 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700364{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700365 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100366 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700367
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300368 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100369 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300370 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700371
372 return found;
373}
374
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400375static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
376{
377 return fuse_range_is_writeback(inode, index, index);
378}
379
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700380/*
381 * Wait for page writeback to be completed.
382 *
383 * Since fuse doesn't rely on the VM writeback tracking, this has to
384 * use some other means.
385 */
386static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
387{
388 struct fuse_inode *fi = get_fuse_inode(inode);
389
390 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
391 return 0;
392}
393
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400394/*
395 * Wait for all pending writepages on the inode to finish.
396 *
397 * This is currently done by blocking further writes with FUSE_NOWRITE
398 * and waiting for all sent writes to complete.
399 *
400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
401 * could conflict with truncation.
402 */
403static void fuse_sync_writes(struct inode *inode)
404{
405 fuse_set_nowrite(inode);
406 fuse_release_nowrite(inode);
407}
408
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700409static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700410{
Al Viro6131ffa2013-02-27 16:59:05 -0500411 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700412 struct fuse_conn *fc = get_fuse_conn(inode);
413 struct fuse_file *ff = file->private_data;
414 struct fuse_req *req;
415 struct fuse_flush_in inarg;
416 int err;
417
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800418 if (is_bad_inode(inode))
419 return -EIO;
420
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421 if (fc->no_flush)
422 return 0;
423
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200424 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400425 if (err)
426 return err;
427
Al Viro59551022016-01-22 15:40:57 -0500428 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400429 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500430 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400431
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200432 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700433 if (err)
434 return err;
435
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400436 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700437 memset(&inarg, 0, sizeof(inarg));
438 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700439 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 req->in.h.opcode = FUSE_FLUSH;
441 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700442 req->in.numargs = 1;
443 req->in.args[0].size = sizeof(inarg);
444 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200445 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100446 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 err = req->out.h.error;
448 fuse_put_request(fc, req);
449 if (err == -ENOSYS) {
450 fc->no_flush = 1;
451 err = 0;
452 }
453 return err;
454}
455
Josef Bacik02c24a82011-07-16 20:44:56 -0400456int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100457 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200459 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460 struct fuse_conn *fc = get_fuse_conn(inode);
461 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100462 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700463 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100464
465 memset(&inarg, 0, sizeof(inarg));
466 inarg.fh = ff->fh;
467 inarg.fsync_flags = datasync ? 1 : 0;
468 args.in.h.opcode = opcode;
469 args.in.h.nodeid = get_node_id(inode);
470 args.in.numargs = 1;
471 args.in.args[0].size = sizeof(inarg);
472 args.in.args[0].value = &inarg;
473 return fuse_simple_request(fc, &args);
474}
475
476static int fuse_fsync(struct file *file, loff_t start, loff_t end,
477 int datasync)
478{
479 struct inode *inode = file->f_mapping->host;
480 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 int err;
482
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800483 if (is_bad_inode(inode))
484 return -EIO;
485
Al Viro59551022016-01-22 15:40:57 -0500486 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400487
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700488 /*
489 * Start writeback against all dirty pages of the inode, then
490 * wait for all outstanding writes, before sending the FSYNC
491 * request.
492 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400493 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700494 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400495 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700496
497 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700498
499 /*
500 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400501 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700502 * We have to do this directly after fuse_sync_writes()
503 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400504 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700505 if (err)
506 goto out;
507
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200508 err = sync_inode_metadata(inode, 1);
509 if (err)
510 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700511
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100512 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200513 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400514
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100515 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100517 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700518 err = 0;
519 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400520out:
Al Viro59551022016-01-22 15:40:57 -0500521 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700522
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100523 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700524}
525
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200526void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
527 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700529 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800530 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800532 inarg->fh = ff->fh;
533 inarg->offset = pos;
534 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800535 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800536 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200537 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700538 req->in.numargs = 1;
539 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800540 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541 req->out.argvar = 1;
542 req->out.numargs = 1;
543 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700544}
545
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200546static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400547{
548 unsigned i;
549
550 for (i = 0; i < req->num_pages; i++) {
551 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200552 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400553 set_page_dirty_lock(page);
554 put_page(page);
555 }
556}
557
Seth Forshee744742d2016-03-11 10:35:34 -0600558static void fuse_io_release(struct kref *kref)
559{
560 kfree(container_of(kref, struct fuse_io_priv, refcnt));
561}
562
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100563static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
564{
565 if (io->err)
566 return io->err;
567
568 if (io->bytes >= 0 && io->write)
569 return -EIO;
570
571 return io->bytes < 0 ? io->size : io->bytes;
572}
573
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400574/**
575 * In case of short read, the caller sets 'pos' to the position of
576 * actual end of fuse request in IO request. Otherwise, if bytes_requested
577 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
578 *
579 * An example:
580 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
581 * both submitted asynchronously. The first of them was ACKed by userspace as
582 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
583 * second request was ACKed as short, e.g. only 1K was read, resulting in
584 * pos == 33K.
585 *
586 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
587 * will be equal to the length of the longest contiguous fragment of
588 * transferred data starting from the beginning of IO request.
589 */
590static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
591{
592 int left;
593
594 spin_lock(&io->lock);
595 if (err)
596 io->err = io->err ? : err;
597 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
598 io->bytes = pos;
599
600 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530601 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100602 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400603 spin_unlock(&io->lock);
604
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530605 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100606 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400607
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100608 if (res >= 0) {
609 struct inode *inode = file_inode(io->iocb->ki_filp);
610 struct fuse_conn *fc = get_fuse_conn(inode);
611 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400612
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300613 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300614 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300615 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400616 }
617
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100618 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400619 }
Seth Forshee744742d2016-03-11 10:35:34 -0600620
621 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400622}
623
624static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
625{
626 struct fuse_io_priv *io = req->io;
627 ssize_t pos = -1;
628
Ashish Samant61c12b42017-07-12 19:26:58 -0700629 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400630
631 if (io->write) {
632 if (req->misc.write.in.size != req->misc.write.out.size)
633 pos = req->misc.write.in.offset - io->offset +
634 req->misc.write.out.size;
635 } else {
636 if (req->misc.read.in.size != req->out.args[0].size)
637 pos = req->misc.read.in.offset - io->offset +
638 req->out.args[0].size;
639 }
640
641 fuse_aio_complete(io, req->out.h.error, pos);
642}
643
644static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
645 size_t num_bytes, struct fuse_io_priv *io)
646{
647 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600648 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400649 io->size += num_bytes;
650 io->reqs++;
651 spin_unlock(&io->lock);
652
653 req->io = io;
654 req->end = fuse_aio_complete_req;
655
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400656 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400657 fuse_request_send_background(fc, req);
658
659 return num_bytes;
660}
661
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400662static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200663 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700664{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200665 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200666 struct fuse_file *ff = file->private_data;
667 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700668
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200669 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700670 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700671 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700672
673 inarg->read_flags |= FUSE_READ_LOCKOWNER;
674 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
675 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400676
677 if (io->async)
678 return fuse_async_req_send(fc, req, count, io);
679
Tejun Heob93f8582008-11-26 12:03:55 +0100680 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800681 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700682}
683
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700684static void fuse_read_update_size(struct inode *inode, loff_t size,
685 u64 attr_ver)
686{
687 struct fuse_conn *fc = get_fuse_conn(inode);
688 struct fuse_inode *fi = get_fuse_inode(inode);
689
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300690 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400691 if (attr_ver == fi->attr_version && size < inode->i_size &&
692 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300693 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700694 i_size_write(inode, size);
695 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300696 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700697}
698
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400699static void fuse_short_read(struct fuse_req *req, struct inode *inode,
700 u64 attr_ver)
701{
702 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400703 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400704
Pavel Emelyanov83732002013-10-10 17:10:46 +0400705 if (fc->writeback_cache) {
706 /*
707 * A hole in a file. Some data after the hole are in page cache,
708 * but have not reached the client fs yet. So, the hole is not
709 * present there.
710 */
711 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300712 int start_idx = num_read >> PAGE_SHIFT;
713 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400714
715 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300716 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400717 off = 0;
718 }
719 } else {
720 loff_t pos = page_offset(req->pages[0]) + num_read;
721 fuse_read_update_size(inode, pos, attr_ver);
722 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400723}
724
Maxim Patlasov482fce52013-10-10 17:11:25 +0400725static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700726{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200727 struct kiocb iocb;
728 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700729 struct inode *inode = page->mapping->host;
730 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800731 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700732 size_t num_read;
733 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300734 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700735 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800736 int err;
737
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700738 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300739 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700740 * page-cache page, so make sure we read a properly synced
741 * page.
742 */
743 fuse_wait_on_page_writeback(inode, page->index);
744
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400745 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700746 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400747 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700748
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700749 attr_ver = fuse_get_attr_version(fc);
750
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700751 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200752 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753 req->num_pages = 1;
754 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400755 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200756 init_sync_kiocb(&iocb, file);
757 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400758 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700759 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700760
761 if (!err) {
762 /*
763 * Short read means EOF. If file size is larger, truncate it
764 */
765 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400766 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700767
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700768 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700769 }
770
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400771 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400772
773 return err;
774}
775
776static int fuse_readpage(struct file *file, struct page *page)
777{
778 struct inode *inode = page->mapping->host;
779 int err;
780
781 err = -EIO;
782 if (is_bad_inode(inode))
783 goto out;
784
785 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800786 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700787 out:
788 unlock_page(page);
789 return err;
790}
791
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800792static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700793{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800794 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700795 size_t count = req->misc.read.in.size;
796 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200797 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800798
Miklos Szeredice534fb2010-05-25 15:06:07 +0200799 for (i = 0; mapping == NULL && i < req->num_pages; i++)
800 mapping = req->pages[i]->mapping;
801
802 if (mapping) {
803 struct inode *inode = mapping->host;
804
805 /*
806 * Short read means EOF. If file size is larger, truncate it
807 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400808 if (!req->out.h.error && num_read < count)
809 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200810
Andrew Gallagher451418f2013-11-05 03:55:43 -0800811 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700812 }
813
Miklos Szeredidb50b962005-09-09 13:10:33 -0700814 for (i = 0; i < req->num_pages; i++) {
815 struct page *page = req->pages[i];
816 if (!req->out.h.error)
817 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800818 else
819 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700820 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300821 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700822 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700823 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -0800824 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800825}
826
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200827static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800828{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200829 struct fuse_file *ff = file->private_data;
830 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300832 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200833
834 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800835 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200836 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200837 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700838 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800839 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700840 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800841 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100842 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800843 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100844 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800845 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100846 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800847 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700848}
849
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700850struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800852 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400854 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700855};
856
857static int fuse_readpages_fill(void *_data, struct page *page)
858{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700859 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700860 struct fuse_req *req = data->req;
861 struct inode *inode = data->inode;
862 struct fuse_conn *fc = get_fuse_conn(inode);
863
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700864 fuse_wait_on_page_writeback(inode, page->index);
865
Miklos Szeredidb50b962005-09-09 13:10:33 -0700866 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300867 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300868 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300870 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
871 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200872 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400873 if (fc->async_read)
874 req = fuse_get_req_for_background(fc, nr_alloc);
875 else
876 req = fuse_get_req(fc, nr_alloc);
877
878 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700879 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700881 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700882 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700883 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400884
885 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300886 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400887 fuse_put_request(fc, req);
888 return -EIO;
889 }
890
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300891 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700892 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400893 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100894 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400895 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700896 return 0;
897}
898
899static int fuse_readpages(struct file *file, struct address_space *mapping,
900 struct list_head *pages, unsigned nr_pages)
901{
902 struct inode *inode = mapping->host;
903 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700904 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700905 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300906 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800907
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700908 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800909 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800910 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800911
Miklos Szeredia6643092007-11-28 16:22:00 -0800912 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700913 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400914 if (fc->async_read)
915 data.req = fuse_get_req_for_background(fc, nr_alloc);
916 else
917 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400918 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700919 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700920 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800921 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700922
923 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700924 if (!err) {
925 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200926 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700927 else
928 fuse_put_request(fc, data.req);
929 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800930out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700931 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700932}
933
Al Viro37c20f12014-04-02 14:47:09 -0400934static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800935{
936 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400937 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800938
Brian Fostera8894272012-07-16 15:23:50 -0400939 /*
940 * In auto invalidate mode, always update attributes on read.
941 * Otherwise, only update if we attempt to read past EOF (to ensure
942 * i_size is up to date).
943 */
944 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400945 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800946 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200947 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800948 if (err)
949 return err;
950 }
951
Al Viro37c20f12014-04-02 14:47:09 -0400952 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800953}
954
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200955static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200956 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700957{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958 struct fuse_write_in *inarg = &req->misc.write.in;
959 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700961 inarg->fh = ff->fh;
962 inarg->offset = pos;
963 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700964 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200965 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700966 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200967 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700968 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
969 else
970 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700971 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700972 req->in.args[1].size = count;
973 req->out.numargs = 1;
974 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700975 req->out.args[0].value = outarg;
976}
977
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400978static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200979 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700980{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200981 struct kiocb *iocb = io->iocb;
982 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200983 struct fuse_file *ff = file->private_data;
984 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200985 struct fuse_write_in *inarg = &req->misc.write.in;
986
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200987 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200988 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200989 if (iocb->ki_flags & IOCB_DSYNC)
990 inarg->flags |= O_DSYNC;
991 if (iocb->ki_flags & IOCB_SYNC)
992 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700993 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700994 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
995 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
996 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400997
998 if (io->async)
999 return fuse_async_req_send(fc, req, count, io);
1000
Tejun Heob93f8582008-11-26 12:03:55 +01001001 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001002 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001003}
1004
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001005bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001006{
1007 struct fuse_conn *fc = get_fuse_conn(inode);
1008 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001009 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001010
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001011 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001012 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001014 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001015 ret = true;
1016 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001017 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001018
1019 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001020}
1021
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001022static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001023 struct inode *inode, loff_t pos,
1024 size_t count)
1025{
1026 size_t res;
1027 unsigned offset;
1028 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001029 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001030
1031 for (i = 0; i < req->num_pages; i++)
1032 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1033
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001034 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001035
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001036 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001037 count = res;
1038 for (i = 0; i < req->num_pages; i++) {
1039 struct page *page = req->pages[i];
1040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001042 SetPageUptodate(page);
1043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001044 if (count > PAGE_SIZE - offset)
1045 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001046 else
1047 count = 0;
1048 offset = 0;
1049
1050 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001051 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001052 }
1053
1054 return res;
1055}
1056
1057static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1058 struct address_space *mapping,
1059 struct iov_iter *ii, loff_t pos)
1060{
1061 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001063 size_t count = 0;
1064 int err;
1065
Miklos Szeredif4975c62009-04-02 14:25:34 +02001066 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001067 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001068
1069 do {
1070 size_t tmp;
1071 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 pgoff_t index = pos >> PAGE_SHIFT;
1073 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 iov_iter_count(ii));
1075
1076 bytes = min_t(size_t, bytes, fc->max_write - count);
1077
1078 again:
1079 err = -EFAULT;
1080 if (iov_iter_fault_in_readable(ii, bytes))
1081 break;
1082
1083 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001084 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 if (!page)
1086 break;
1087
anfei zhou931e80e2010-02-02 13:44:02 -08001088 if (mapping_writably_mapped(mapping))
1089 flush_dcache_page(page);
1090
Nick Pigginea9b9902008-04-30 00:54:42 -07001091 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001092 flush_dcache_page(page);
1093
Roman Gushchin3ca81382015-10-12 16:33:44 +03001094 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 if (!tmp) {
1096 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001097 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001098 bytes = min(bytes, iov_iter_single_seg_count(ii));
1099 goto again;
1100 }
1101
1102 err = 0;
1103 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001104 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 req->num_pages++;
1106
Nick Pigginea9b9902008-04-30 00:54:42 -07001107 count += tmp;
1108 pos += tmp;
1109 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001110 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001111 offset = 0;
1112
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001113 if (!fc->big_writes)
1114 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001116 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001117
1118 return count > 0 ? count : err;
1119}
1120
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001121static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1122 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001123{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001124 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001125 ((pos + len - 1) >> PAGE_SHIFT) -
1126 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001127 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001128}
1129
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001130static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001131 struct address_space *mapping,
1132 struct iov_iter *ii, loff_t pos)
1133{
1134 struct inode *inode = mapping->host;
1135 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001136 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001137 int err = 0;
1138 ssize_t res = 0;
1139
1140 if (is_bad_inode(inode))
1141 return -EIO;
1142
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001143 if (inode->i_size < pos + iov_iter_count(ii))
1144 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1145
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 do {
1147 struct fuse_req *req;
1148 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001149 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1150 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001151
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001152 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001153 if (IS_ERR(req)) {
1154 err = PTR_ERR(req);
1155 break;
1156 }
1157
1158 count = fuse_fill_write_pages(req, mapping, ii, pos);
1159 if (count <= 0) {
1160 err = count;
1161 } else {
1162 size_t num_written;
1163
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001164 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001165 pos, count);
1166 err = req->out.h.error;
1167 if (!err) {
1168 res += num_written;
1169 pos += num_written;
1170
1171 /* break out of the loop on short write */
1172 if (num_written != count)
1173 err = -EIO;
1174 }
1175 }
1176 fuse_put_request(fc, req);
1177 } while (!err && iov_iter_count(ii));
1178
1179 if (res > 0)
1180 fuse_write_update_size(inode, pos);
1181
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001182 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001183 fuse_invalidate_attr(inode);
1184
1185 return res > 0 ? res : err;
1186}
1187
Al Viro84c3d552014-04-03 14:33:23 -04001188static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001189{
1190 struct file *file = iocb->ki_filp;
1191 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001192 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001193 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 struct inode *inode = mapping->host;
1195 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001196 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001197
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001198 if (get_fuse_conn(inode)->writeback_cache) {
1199 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001200 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001201 if (err)
1202 return err;
1203
Al Viro84c3d552014-04-03 14:33:23 -04001204 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001205 }
1206
Al Viro59551022016-01-22 15:40:57 -05001207 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001208
1209 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001210 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
Al Viro3309dd02015-04-09 12:55:47 -04001212 err = generic_write_checks(iocb, from);
1213 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001214 goto out;
1215
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001216 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001217 if (err)
1218 goto out;
1219
Josef Bacikc3b2da32012-03-26 09:59:21 -04001220 err = file_update_time(file);
1221 if (err)
1222 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001223
Al Viro2ba48ce2015-04-09 13:52:01 -04001224 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001225 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001226 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001227 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001228 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001229
Anand Avati4273b792012-02-17 12:46:25 -05001230 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001231
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001232 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001233 if (written_buffered < 0) {
1234 err = written_buffered;
1235 goto out;
1236 }
1237 endbyte = pos + written_buffered - 1;
1238
1239 err = filemap_write_and_wait_range(file->f_mapping, pos,
1240 endbyte);
1241 if (err)
1242 goto out;
1243
1244 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001245 pos >> PAGE_SHIFT,
1246 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001247
1248 written += written_buffered;
1249 iocb->ki_pos = pos + written_buffered;
1250 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001251 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001252 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001253 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001254 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001255out:
1256 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001257 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001258 if (written > 0)
1259 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001260
1261 return written ? written : err;
1262}
1263
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001264static inline void fuse_page_descs_length_init(struct fuse_req *req,
1265 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001266{
1267 int i;
1268
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001270 req->page_descs[i].length = PAGE_SIZE -
1271 req->page_descs[i].offset;
1272}
1273
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001274static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1275{
1276 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1277}
1278
1279static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1280 size_t max_size)
1281{
1282 return min(iov_iter_single_seg_count(ii), max_size);
1283}
1284
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001285static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001286 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001287{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001289 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001290
Miklos Szeredif4975c62009-04-02 14:25:34 +02001291 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001292 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293 unsigned long user_addr = fuse_get_user_addr(ii);
1294 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1295
Miklos Szeredif4975c62009-04-02 14:25:34 +02001296 if (write)
1297 req->in.args[1].value = (void *) user_addr;
1298 else
1299 req->out.args[0].value = (void *) user_addr;
1300
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001301 iov_iter_advance(ii, frag_size);
1302 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001303 return 0;
1304 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001305
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001306 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001307 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001308 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001309 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001310 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001311 req->max_pages - req->num_pages,
1312 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001313 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001314 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001315
Al Viroc9c37e22014-03-16 16:08:30 -04001316 iov_iter_advance(ii, ret);
1317 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001318
Al Viroc9c37e22014-03-16 16:08:30 -04001319 ret += start;
1320 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1321
1322 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001323 fuse_page_descs_length_init(req, req->num_pages, npages);
1324
1325 req->num_pages += npages;
1326 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001327 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001328 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001329
1330 if (write)
1331 req->in.argpages = 1;
1332 else
1333 req->out.argpages = 1;
1334
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001335 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001336
Ashish Samant2c932d42016-03-25 10:53:41 -07001337 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001338}
1339
Al Virod22a9432014-03-16 15:50:47 -04001340ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1341 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001342{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001343 int write = flags & FUSE_DIO_WRITE;
1344 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001345 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001346 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001347 struct fuse_file *ff = file->private_data;
1348 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001349 size_t nmax = write ? fc->max_write : fc->max_read;
1350 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001351 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001352 pgoff_t idx_from = pos >> PAGE_SHIFT;
1353 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001354 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001355 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001356 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001357
Brian Fosterde82b922013-05-14 11:25:39 -04001358 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001359 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1360 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001361 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001362 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001363 if (IS_ERR(req))
1364 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001365
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001366 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1367 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001368 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001369 fuse_sync_writes(inode);
1370 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001371 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001372 }
1373
Ashish Samant61c12b42017-07-12 19:26:58 -07001374 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001377 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001378 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001379 err = fuse_get_user_pages(req, iter, &nbytes, write);
1380 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001381 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001382
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001384 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001385 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001386 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001387
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001388 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001389 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001390 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001391 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 break;
1393 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001394 res = 0;
1395 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396 break;
1397 }
1398 count -= nres;
1399 res += nres;
1400 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001401 if (nres != nbytes)
1402 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001403 if (count) {
1404 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001405 if (io->async)
1406 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001407 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001408 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001409 req = fuse_get_req(fc, iov_iter_npages(iter,
1410 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001411 if (IS_ERR(req))
1412 break;
1413 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001414 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001415 if (!IS_ERR(req))
1416 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001417 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419
Ashish Samant742f9922016-03-14 21:57:35 -07001420 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001421}
Tejun Heo08cbf542009-04-14 10:54:53 +09001422EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001423
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001424static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001425 struct iov_iter *iter,
1426 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001428 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001429 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001430
1431 if (is_bad_inode(inode))
1432 return -EIO;
1433
Al Virod22a9432014-03-16 15:50:47 -04001434 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001435
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001436 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001437
1438 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001439}
1440
Al Viro153162632015-03-30 22:08:36 -04001441static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001442{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001443 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001444 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001445}
1446
Al Viro153162632015-03-30 22:08:36 -04001447static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001448{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001449 struct inode *inode = file_inode(iocb->ki_filp);
1450 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001451 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001452
1453 if (is_bad_inode(inode))
1454 return -EIO;
1455
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001456 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001457 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001458 res = generic_write_checks(iocb, from);
1459 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001460 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001461 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001462 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001463 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001464 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001465
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001466 return res;
1467}
1468
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001469static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001470{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001471 int i;
1472
1473 for (i = 0; i < req->num_pages; i++)
1474 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001475
1476 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001477 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001478}
1479
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001480static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001481{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001482 struct inode *inode = req->inode;
1483 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001484 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001485 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486
1487 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001488 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001489 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001490 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001491 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001492 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493 wake_up(&fi->page_waitq);
1494}
1495
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001496/* Called under fi->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001497static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1498 loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001499__releases(fi->lock)
1500__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001501{
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001502 struct fuse_req *aux, *next;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001503 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001505 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001506 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001507
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001508 if (inarg->offset + data_size <= size) {
1509 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001510 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001511 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512 } else {
1513 /* Got truncated off completely */
1514 goto out_free;
1515 }
1516
1517 req->in.args[1].size = inarg->size;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001518 queued = fuse_request_queue_background(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001519 /* Fails on broken connection only */
1520 if (unlikely(!queued))
1521 goto out_free;
1522
1523 fi->writectr++;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001524 return;
1525
1526 out_free:
1527 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001528 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001529
1530 /* After fuse_writepage_finish() aux request list is private */
1531 for (aux = req->misc.write.next; aux; aux = next) {
1532 next = aux->misc.write.next;
1533 aux->misc.write.next = NULL;
1534 fuse_writepage_free(fc, aux);
1535 fuse_put_request(fc, aux);
1536 }
1537
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001538 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001539 fuse_put_request(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001540 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541}
1542
1543/*
1544 * If fi->writectr is positive (no truncate or fsync going on) send
1545 * all queued writepage requests.
1546 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001547 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001548 */
1549void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001550__releases(fi->lock)
1551__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001552{
1553 struct fuse_conn *fc = get_fuse_conn(inode);
1554 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001555 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001556 struct fuse_req *req;
1557
1558 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1559 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1560 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001561 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001562 }
1563}
1564
1565static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1566{
1567 struct inode *inode = req->inode;
1568 struct fuse_inode *fi = get_fuse_inode(inode);
1569
1570 mapping_set_error(inode->i_mapping, req->out.h.error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001571 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001572 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001573 struct fuse_conn *fc = get_fuse_conn(inode);
1574 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001575 struct fuse_req *next = req->misc.write.next;
1576 req->misc.write.next = next->misc.write.next;
1577 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001578 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001579 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001580
1581 /*
1582 * Skip fuse_flush_writepages() to make it easy to crop requests
1583 * based on primary request size.
1584 *
1585 * 1st case (trivial): there are no concurrent activities using
1586 * fuse_set/release_nowrite. Then we're on safe side because
1587 * fuse_flush_writepages() would call fuse_send_writepage()
1588 * anyway.
1589 *
1590 * 2nd case: someone called fuse_set_nowrite and it is waiting
1591 * now for completion of all in-flight requests. This happens
1592 * rarely and no more than once per page, so this should be
1593 * okay.
1594 *
1595 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1596 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1597 * that fuse_set_nowrite returned implies that all in-flight
1598 * requests were completed along with all of their secondary
1599 * requests. Further primary requests are blocked by negative
1600 * writectr. Hence there cannot be any in-flight requests and
1601 * no invocations of fuse_writepage_end() while we're in
1602 * fuse_set_nowrite..fuse_release_nowrite section.
1603 */
1604 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001605 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001606 fi->writectr--;
1607 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001608 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001609 fuse_writepage_free(fc, req);
1610}
1611
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001612static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1613 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001614{
Miklos Szeredi72523422013-10-01 16:44:52 +02001615 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001616
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001617 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001618 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001619 ff = list_entry(fi->write_files.next, struct fuse_file,
1620 write_entry);
1621 fuse_file_get(ff);
1622 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001623 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001624
1625 return ff;
1626}
1627
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001628static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1629 struct fuse_inode *fi)
1630{
1631 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1632 WARN_ON(!ff);
1633 return ff;
1634}
1635
1636int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1637{
1638 struct fuse_conn *fc = get_fuse_conn(inode);
1639 struct fuse_inode *fi = get_fuse_inode(inode);
1640 struct fuse_file *ff;
1641 int err;
1642
1643 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001644 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001645 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001646 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001647
1648 return err;
1649}
1650
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001651static int fuse_writepage_locked(struct page *page)
1652{
1653 struct address_space *mapping = page->mapping;
1654 struct inode *inode = mapping->host;
1655 struct fuse_conn *fc = get_fuse_conn(inode);
1656 struct fuse_inode *fi = get_fuse_inode(inode);
1657 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001659 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660
1661 set_page_writeback(page);
1662
Maxim Patlasov4250c062012-10-26 19:48:07 +04001663 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001664 if (!req)
1665 goto err;
1666
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001667 /* writeback always goes to bg_queue */
1668 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1670 if (!tmp_page)
1671 goto err_free;
1672
Miklos Szeredi72523422013-10-01 16:44:52 +02001673 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001674 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001675 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001676 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001677
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001678 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001679
1680 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001681 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001682 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001683 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001684 req->num_pages = 1;
1685 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001686 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001687 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688 req->end = fuse_writepage_end;
1689 req->inode = inode;
1690
Tejun Heo93f78d82015-05-22 17:13:27 -04001691 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001692 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001693
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001694 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001695 list_add(&req->writepages_entry, &fi->writepages);
1696 list_add_tail(&req->list, &fi->queued_writes);
1697 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001698 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001700 end_page_writeback(page);
1701
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001702 return 0;
1703
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001704err_nofile:
1705 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706err_free:
1707 fuse_request_free(req);
1708err:
Jeff Layton91839762017-05-25 06:57:50 -04001709 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001710 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001711 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001712}
1713
1714static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1715{
1716 int err;
1717
Miklos Szerediff17be02013-10-01 16:44:53 +02001718 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1719 /*
1720 * ->writepages() should be called for sync() and friends. We
1721 * should only get here on direct reclaim and then we are
1722 * allowed to skip a page which is already in flight
1723 */
1724 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1725
1726 redirty_page_for_writepage(wbc, page);
1727 return 0;
1728 }
1729
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001730 err = fuse_writepage_locked(page);
1731 unlock_page(page);
1732
1733 return err;
1734}
1735
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001736struct fuse_fill_wb_data {
1737 struct fuse_req *req;
1738 struct fuse_file *ff;
1739 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001740 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001741};
1742
1743static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1744{
1745 struct fuse_req *req = data->req;
1746 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001747 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001748 int num_pages = req->num_pages;
1749 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001750
1751 req->ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001752 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001753 list_add_tail(&req->list, &fi->queued_writes);
1754 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001755 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001756
1757 for (i = 0; i < num_pages; i++)
1758 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001759}
1760
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001761/*
1762 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001763 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1764 * one already added for a page at this offset. If there's none, then insert
1765 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001766 * copying the new page contents over to the old temporary page.
1767 */
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1769 struct page *page)
1770{
1771 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1772 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1773 struct fuse_req *tmp;
1774 struct fuse_req *old_req;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001775
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001776 WARN_ON(new_req->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001777
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001778 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001779 list_del(&new_req->writepages_entry);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001780 old_req = fuse_find_writeback(fi, page->index, page->index);
1781 if (!old_req) {
Maxim Patlasovf6011082013-10-02 15:01:07 +04001782 list_add(&new_req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001783 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001784 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001785 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001786
Maxim Patlasovf6011082013-10-02 15:01:07 +04001787 new_req->num_pages = 1;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001788 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001789 pgoff_t curr_index;
1790
1791 WARN_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001792 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001793 if (curr_index == page->index) {
1794 WARN_ON(tmp->num_pages != 1);
1795 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
Kirill Tkhaic5de16cc2018-11-26 12:46:20 +03001796 swap(tmp->pages[0], new_req->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001797 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001798 }
1799 }
1800
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001801 if (!tmp) {
1802 new_req->misc.write.next = old_req->misc.write.next;
1803 old_req->misc.write.next = new_req;
1804 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001805
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001806 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001807
1808 if (tmp) {
1809 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001810
Tejun Heo93f78d82015-05-22 17:13:27 -04001811 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredia2ebba82019-01-16 10:27:59 +01001812 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001813 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001814 fuse_writepage_free(fc, new_req);
1815 fuse_request_free(new_req);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001816 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001817
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001818 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001819}
1820
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001821static int fuse_writepages_fill(struct page *page,
1822 struct writeback_control *wbc, void *_data)
1823{
1824 struct fuse_fill_wb_data *data = _data;
1825 struct fuse_req *req = data->req;
1826 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001827 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001828 struct fuse_conn *fc = get_fuse_conn(inode);
1829 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001830 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001831 int err;
1832
1833 if (!data->ff) {
1834 err = -EIO;
1835 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1836 if (!data->ff)
1837 goto out_unlock;
1838 }
1839
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001840 /*
1841 * Being under writeback is unlikely but possible. For example direct
1842 * read to an mmaped fuse file will set the page dirty twice; once when
1843 * the pages are faulted with get_user_pages(), and then after the read
1844 * completed.
1845 */
1846 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001847
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001848 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001849 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001850 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001851 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1852 fuse_writepages_send(data);
1853 data->req = NULL;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001854 } else if (req && req->num_pages == req->max_pages) {
1855 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1856 fuse_writepages_send(data);
1857 req = data->req = NULL;
1858 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001859 }
Miklos Szeredie52a8252018-10-01 10:07:06 +02001860
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001861 err = -ENOMEM;
1862 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1863 if (!tmp_page)
1864 goto out_unlock;
1865
1866 /*
1867 * The page must not be redirtied until the writeout is completed
1868 * (i.e. userspace has sent a reply to the write request). Otherwise
1869 * there could be more than one temporary page instance for each real
1870 * page.
1871 *
1872 * This is ensured by holding the page lock in page_mkwrite() while
1873 * checking fuse_page_is_writeback(). We already hold the page lock
1874 * since clear_page_dirty_for_io() and keep it held until we add the
1875 * request to the fi->writepages list and increment req->num_pages.
1876 * After this fuse_page_is_writeback() will indicate that the page is
1877 * under writeback, so we can release the page lock.
1878 */
1879 if (data->req == NULL) {
1880 struct fuse_inode *fi = get_fuse_inode(inode);
1881
1882 err = -ENOMEM;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001883 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001884 if (!req) {
1885 __free_page(tmp_page);
1886 goto out_unlock;
1887 }
1888
1889 fuse_write_fill(req, data->ff, page_offset(page), 0);
1890 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001891 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001892 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001893 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894 req->num_pages = 0;
1895 req->end = fuse_writepage_end;
1896 req->inode = inode;
1897
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001898 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001899 list_add(&req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001900 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001901
1902 data->req = req;
1903 }
1904 set_page_writeback(page);
1905
1906 copy_highpage(tmp_page, page);
1907 req->pages[req->num_pages] = tmp_page;
1908 req->page_descs[req->num_pages].offset = 0;
1909 req->page_descs[req->num_pages].length = PAGE_SIZE;
1910
Tejun Heo93f78d82015-05-22 17:13:27 -04001911 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001912 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001913
1914 err = 0;
1915 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1916 end_page_writeback(page);
1917 data->req = NULL;
1918 goto out_unlock;
1919 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001920 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001921
1922 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001923 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001924 * fuse_page_is_writeback().
1925 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001926 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001927 req->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001928 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001929
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001930out_unlock:
1931 unlock_page(page);
1932
1933 return err;
1934}
1935
1936static int fuse_writepages(struct address_space *mapping,
1937 struct writeback_control *wbc)
1938{
1939 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001940 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001941 struct fuse_fill_wb_data data;
1942 int err;
1943
1944 err = -EIO;
1945 if (is_bad_inode(inode))
1946 goto out;
1947
1948 data.inode = inode;
1949 data.req = NULL;
1950 data.ff = NULL;
1951
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001952 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001953 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001954 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001955 GFP_NOFS);
1956 if (!data.orig_pages)
1957 goto out;
1958
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001959 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1960 if (data.req) {
1961 /* Ignore errors if we can write at least one page */
1962 BUG_ON(!data.req->num_pages);
1963 fuse_writepages_send(&data);
1964 err = 0;
1965 }
1966 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001967 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001968
1969 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001970out:
1971 return err;
1972}
1973
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001974/*
1975 * It's worthy to make sure that space is reserved on disk for the write,
1976 * but how to implement it without killing performance need more thinking.
1977 */
1978static int fuse_write_begin(struct file *file, struct address_space *mapping,
1979 loff_t pos, unsigned len, unsigned flags,
1980 struct page **pagep, void **fsdata)
1981{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001982 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001983 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001984 struct page *page;
1985 loff_t fsize;
1986 int err = -ENOMEM;
1987
1988 WARN_ON(!fc->writeback_cache);
1989
1990 page = grab_cache_page_write_begin(mapping, index, flags);
1991 if (!page)
1992 goto error;
1993
1994 fuse_wait_on_page_writeback(mapping->host, page->index);
1995
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001996 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001997 goto success;
1998 /*
1999 * Check if the start this page comes after the end of file, in which
2000 * case the readpage can be optimized away.
2001 */
2002 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002003 if (fsize <= (pos & PAGE_MASK)) {
2004 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002005 if (off)
2006 zero_user_segment(page, 0, off);
2007 goto success;
2008 }
2009 err = fuse_do_readpage(file, page);
2010 if (err)
2011 goto cleanup;
2012success:
2013 *pagep = page;
2014 return 0;
2015
2016cleanup:
2017 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002018 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002019error:
2020 return err;
2021}
2022
2023static int fuse_write_end(struct file *file, struct address_space *mapping,
2024 loff_t pos, unsigned len, unsigned copied,
2025 struct page *page, void *fsdata)
2026{
2027 struct inode *inode = page->mapping->host;
2028
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002029 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2030 if (!copied)
2031 goto unlock;
2032
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002033 if (!PageUptodate(page)) {
2034 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002035 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002036 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002038 SetPageUptodate(page);
2039 }
2040
2041 fuse_write_update_size(inode, pos + copied);
2042 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002043
2044unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002045 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002046 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002047
2048 return copied;
2049}
2050
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002051static int fuse_launder_page(struct page *page)
2052{
2053 int err = 0;
2054 if (clear_page_dirty_for_io(page)) {
2055 struct inode *inode = page->mapping->host;
2056 err = fuse_writepage_locked(page);
2057 if (!err)
2058 fuse_wait_on_page_writeback(inode, page->index);
2059 }
2060 return err;
2061}
2062
2063/*
2064 * Write back dirty pages now, because there may not be any suitable
2065 * open files later
2066 */
2067static void fuse_vma_close(struct vm_area_struct *vma)
2068{
2069 filemap_write_and_wait(vma->vm_file->f_mapping);
2070}
2071
2072/*
2073 * Wait for writeback against this page to complete before allowing it
2074 * to be marked dirty again, and hence written back again, possibly
2075 * before the previous writepage completed.
2076 *
2077 * Block here, instead of in ->writepage(), so that the userspace fs
2078 * can only block processes actually operating on the filesystem.
2079 *
2080 * Otherwise unprivileged userspace fs would be able to block
2081 * unrelated:
2082 *
2083 * - page migration
2084 * - sync(2)
2085 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2086 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302087static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002088{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002089 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002090 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002091
Dave Jiang11bac802017-02-24 14:56:41 -08002092 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002093 lock_page(page);
2094 if (page->mapping != inode->i_mapping) {
2095 unlock_page(page);
2096 return VM_FAULT_NOPAGE;
2097 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002098
2099 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002100 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002101}
2102
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002103static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002104 .close = fuse_vma_close,
2105 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002106 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002107 .page_mkwrite = fuse_page_mkwrite,
2108};
2109
2110static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2111{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002112 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2113 fuse_link_write_file(file);
2114
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002115 file_accessed(file);
2116 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002117 return 0;
2118}
2119
Miklos Szeredifc280c92009-04-02 14:25:35 +02002120static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2121{
2122 /* Can't provide the coherency needed for MAP_SHARED */
2123 if (vma->vm_flags & VM_MAYSHARE)
2124 return -ENODEV;
2125
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002126 invalidate_inode_pages2(file->f_mapping);
2127
Miklos Szeredifc280c92009-04-02 14:25:35 +02002128 return generic_file_mmap(file, vma);
2129}
2130
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002131static int convert_fuse_file_lock(struct fuse_conn *fc,
2132 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002133 struct file_lock *fl)
2134{
2135 switch (ffl->type) {
2136 case F_UNLCK:
2137 break;
2138
2139 case F_RDLCK:
2140 case F_WRLCK:
2141 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2142 ffl->end < ffl->start)
2143 return -EIO;
2144
2145 fl->fl_start = ffl->start;
2146 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002147
2148 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002149 * Convert pid into init's pid namespace. The locks API will
2150 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002151 */
2152 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002153 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002154 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002155 break;
2156
2157 default:
2158 return -EIO;
2159 }
2160 fl->fl_type = ffl->type;
2161 return 0;
2162}
2163
Miklos Szeredi70781872014-12-12 09:49:05 +01002164static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002165 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002166 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002167{
Al Viro6131ffa2013-02-27 16:59:05 -05002168 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002169 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002170 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002171
Miklos Szeredi70781872014-12-12 09:49:05 +01002172 memset(inarg, 0, sizeof(*inarg));
2173 inarg->fh = ff->fh;
2174 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2175 inarg->lk.start = fl->fl_start;
2176 inarg->lk.end = fl->fl_end;
2177 inarg->lk.type = fl->fl_type;
2178 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002179 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002180 inarg->lk_flags |= FUSE_LK_FLOCK;
2181 args->in.h.opcode = opcode;
2182 args->in.h.nodeid = get_node_id(inode);
2183 args->in.numargs = 1;
2184 args->in.args[0].size = sizeof(*inarg);
2185 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002186}
2187
2188static int fuse_getlk(struct file *file, struct file_lock *fl)
2189{
Al Viro6131ffa2013-02-27 16:59:05 -05002190 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002192 FUSE_ARGS(args);
2193 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002194 struct fuse_lk_out outarg;
2195 int err;
2196
Miklos Szeredi70781872014-12-12 09:49:05 +01002197 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2198 args.out.numargs = 1;
2199 args.out.args[0].size = sizeof(outarg);
2200 args.out.args[0].value = &outarg;
2201 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002202 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002203 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002204
2205 return err;
2206}
2207
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002208static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002209{
Al Viro6131ffa2013-02-27 16:59:05 -05002210 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002212 FUSE_ARGS(args);
2213 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002214 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002215 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2216 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002217 int err;
2218
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002219 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002220 /* NLM needs asynchronous locks, which we don't support yet */
2221 return -ENOLCK;
2222 }
2223
Miklos Szeredi71421252006-06-25 05:48:52 -07002224 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002225 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002226 return 0;
2227
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002228 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002229 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002230
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002231 /* locking is restartable */
2232 if (err == -EINTR)
2233 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002234
Miklos Szeredi71421252006-06-25 05:48:52 -07002235 return err;
2236}
2237
2238static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2239{
Al Viro6131ffa2013-02-27 16:59:05 -05002240 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002241 struct fuse_conn *fc = get_fuse_conn(inode);
2242 int err;
2243
Miklos Szeredi48e90762008-07-25 01:49:02 -07002244 if (cmd == F_CANCELLK) {
2245 err = 0;
2246 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002247 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002248 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002249 err = 0;
2250 } else
2251 err = fuse_getlk(file, fl);
2252 } else {
2253 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002254 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002255 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002256 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002257 }
2258 return err;
2259}
2260
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002261static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2262{
Al Viro6131ffa2013-02-27 16:59:05 -05002263 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002264 struct fuse_conn *fc = get_fuse_conn(inode);
2265 int err;
2266
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002267 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002268 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002269 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002270 struct fuse_file *ff = file->private_data;
2271
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002272 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002273 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002274 err = fuse_setlk(file, fl, 1);
2275 }
2276
2277 return err;
2278}
2279
Miklos Szeredib2d22722006-12-06 20:35:51 -08002280static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2281{
2282 struct inode *inode = mapping->host;
2283 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002284 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002285 struct fuse_bmap_in inarg;
2286 struct fuse_bmap_out outarg;
2287 int err;
2288
2289 if (!inode->i_sb->s_bdev || fc->no_bmap)
2290 return 0;
2291
Miklos Szeredib2d22722006-12-06 20:35:51 -08002292 memset(&inarg, 0, sizeof(inarg));
2293 inarg.block = block;
2294 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002295 args.in.h.opcode = FUSE_BMAP;
2296 args.in.h.nodeid = get_node_id(inode);
2297 args.in.numargs = 1;
2298 args.in.args[0].size = sizeof(inarg);
2299 args.in.args[0].value = &inarg;
2300 args.out.numargs = 1;
2301 args.out.args[0].size = sizeof(outarg);
2302 args.out.args[0].value = &outarg;
2303 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002304 if (err == -ENOSYS)
2305 fc->no_bmap = 1;
2306
2307 return err ? 0 : outarg.block;
2308}
2309
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302310static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2311{
2312 struct inode *inode = file->f_mapping->host;
2313 struct fuse_conn *fc = get_fuse_conn(inode);
2314 struct fuse_file *ff = file->private_data;
2315 FUSE_ARGS(args);
2316 struct fuse_lseek_in inarg = {
2317 .fh = ff->fh,
2318 .offset = offset,
2319 .whence = whence
2320 };
2321 struct fuse_lseek_out outarg;
2322 int err;
2323
2324 if (fc->no_lseek)
2325 goto fallback;
2326
2327 args.in.h.opcode = FUSE_LSEEK;
2328 args.in.h.nodeid = ff->nodeid;
2329 args.in.numargs = 1;
2330 args.in.args[0].size = sizeof(inarg);
2331 args.in.args[0].value = &inarg;
2332 args.out.numargs = 1;
2333 args.out.args[0].size = sizeof(outarg);
2334 args.out.args[0].value = &outarg;
2335 err = fuse_simple_request(fc, &args);
2336 if (err) {
2337 if (err == -ENOSYS) {
2338 fc->no_lseek = 1;
2339 goto fallback;
2340 }
2341 return err;
2342 }
2343
2344 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2345
2346fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002347 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302348 if (!err)
2349 return generic_file_llseek(file, offset, whence);
2350 else
2351 return err;
2352}
2353
Andrew Morton965c8e52012-12-17 15:59:39 -08002354static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002355{
2356 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002357 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002358
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302359 switch (whence) {
2360 case SEEK_SET:
2361 case SEEK_CUR:
2362 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002363 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302364 break;
2365 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002366 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002367 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302368 if (!retval)
2369 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002370 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302371 break;
2372 case SEEK_HOLE:
2373 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002374 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302375 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002376 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302377 break;
2378 default:
2379 retval = -EINVAL;
2380 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002381
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002382 return retval;
2383}
2384
Tejun Heo59efec72008-11-26 12:03:55 +01002385/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002386 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2387 * ABI was defined to be 'struct iovec' which is different on 32bit
2388 * and 64bit. Fortunately we can determine which structure the server
2389 * used from the size of the reply.
2390 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002391static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2392 size_t transferred, unsigned count,
2393 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002394{
2395#ifdef CONFIG_COMPAT
2396 if (count * sizeof(struct compat_iovec) == transferred) {
2397 struct compat_iovec *ciov = src;
2398 unsigned i;
2399
2400 /*
2401 * With this interface a 32bit server cannot support
2402 * non-compat (i.e. ones coming from 64bit apps) ioctl
2403 * requests
2404 */
2405 if (!is_compat)
2406 return -EINVAL;
2407
2408 for (i = 0; i < count; i++) {
2409 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2410 dst[i].iov_len = ciov[i].iov_len;
2411 }
2412 return 0;
2413 }
2414#endif
2415
2416 if (count * sizeof(struct iovec) != transferred)
2417 return -EIO;
2418
2419 memcpy(dst, src, transferred);
2420 return 0;
2421}
2422
Miklos Szeredi75727772010-11-30 16:39:27 +01002423/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002424static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2425 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002426{
2427 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002428 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002429
Zach Brownfb6ccff2012-07-24 12:10:11 -07002430 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002431 if (iov->iov_len > (size_t) max)
2432 return -ENOMEM;
2433 max -= iov->iov_len;
2434 }
2435 return 0;
2436}
2437
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002438static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2439 void *src, size_t transferred, unsigned count,
2440 bool is_compat)
2441{
2442 unsigned i;
2443 struct fuse_ioctl_iovec *fiov = src;
2444
2445 if (fc->minor < 16) {
2446 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2447 count, is_compat);
2448 }
2449
2450 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2451 return -EIO;
2452
2453 for (i = 0; i < count; i++) {
2454 /* Did the server supply an inappropriate value? */
2455 if (fiov[i].base != (unsigned long) fiov[i].base ||
2456 fiov[i].len != (unsigned long) fiov[i].len)
2457 return -EIO;
2458
2459 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2460 dst[i].iov_len = (size_t) fiov[i].len;
2461
2462#ifdef CONFIG_COMPAT
2463 if (is_compat &&
2464 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2465 (compat_size_t) dst[i].iov_len != fiov[i].len))
2466 return -EIO;
2467#endif
2468 }
2469
2470 return 0;
2471}
2472
2473
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002474/*
Tejun Heo59efec72008-11-26 12:03:55 +01002475 * For ioctls, there is no generic way to determine how much memory
2476 * needs to be read and/or written. Furthermore, ioctls are allowed
2477 * to dereference the passed pointer, so the parameter requires deep
2478 * copying but FUSE has no idea whatsoever about what to copy in or
2479 * out.
2480 *
2481 * This is solved by allowing FUSE server to retry ioctl with
2482 * necessary in/out iovecs. Let's assume the ioctl implementation
2483 * needs to read in the following structure.
2484 *
2485 * struct a {
2486 * char *buf;
2487 * size_t buflen;
2488 * }
2489 *
2490 * On the first callout to FUSE server, inarg->in_size and
2491 * inarg->out_size will be NULL; then, the server completes the ioctl
2492 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2493 * the actual iov array to
2494 *
2495 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2496 *
2497 * which tells FUSE to copy in the requested area and retry the ioctl.
2498 * On the second round, the server has access to the structure and
2499 * from that it can tell what to look for next, so on the invocation,
2500 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2501 *
2502 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2503 * { .iov_base = a.buf, .iov_len = a.buflen } }
2504 *
2505 * FUSE will copy both struct a and the pointed buffer from the
2506 * process doing the ioctl and retry ioctl with both struct a and the
2507 * buffer.
2508 *
2509 * This time, FUSE server has everything it needs and completes ioctl
2510 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2511 *
2512 * Copying data out works the same way.
2513 *
2514 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2515 * automatically initializes in and out iovs by decoding @cmd with
2516 * _IOC_* macros and the server is not allowed to request RETRY. This
2517 * limits ioctl data transfers to well-formed ioctls and is the forced
2518 * behavior for all FUSE servers.
2519 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002520long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2521 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002522{
Tejun Heo59efec72008-11-26 12:03:55 +01002523 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002524 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002525 struct fuse_ioctl_in inarg = {
2526 .fh = ff->fh,
2527 .cmd = cmd,
2528 .arg = arg,
2529 .flags = flags
2530 };
2531 struct fuse_ioctl_out outarg;
2532 struct fuse_req *req = NULL;
2533 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002534 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002535 struct iovec *in_iov = NULL, *out_iov = NULL;
2536 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002537 size_t in_size, out_size, transferred, c;
2538 int err, i;
2539 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002540
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002541#if BITS_PER_LONG == 32
2542 inarg.flags |= FUSE_IOCTL_32BIT;
2543#else
2544 if (flags & FUSE_IOCTL_COMPAT)
2545 inarg.flags |= FUSE_IOCTL_32BIT;
2546#endif
2547
Tejun Heo59efec72008-11-26 12:03:55 +01002548 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002549 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002550
Tejun Heo59efec72008-11-26 12:03:55 +01002551 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002552 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002553 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002554 if (!pages || !iov_page)
2555 goto out;
2556
2557 /*
2558 * If restricted, initialize IO parameters as encoded in @cmd.
2559 * RETRY from server is not allowed.
2560 */
2561 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002562 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002563
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002564 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002565 iov->iov_len = _IOC_SIZE(cmd);
2566
2567 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2568 in_iov = iov;
2569 in_iovs = 1;
2570 }
2571
2572 if (_IOC_DIR(cmd) & _IOC_READ) {
2573 out_iov = iov;
2574 out_iovs = 1;
2575 }
2576 }
2577
2578 retry:
2579 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2580 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2581
2582 /*
2583 * Out data can be used either for actual out data or iovs,
2584 * make sure there always is at least one page.
2585 */
2586 out_size = max_t(size_t, out_size, PAGE_SIZE);
2587 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2588
2589 /* make sure there are enough buffer pages and init request with them */
2590 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002591 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002592 goto out;
2593 while (num_pages < max_pages) {
2594 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2595 if (!pages[num_pages])
2596 goto out;
2597 num_pages++;
2598 }
2599
Maxim Patlasov54b96672012-10-26 19:49:13 +04002600 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002601 if (IS_ERR(req)) {
2602 err = PTR_ERR(req);
2603 req = NULL;
2604 goto out;
2605 }
2606 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2607 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002608 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002609
2610 /* okay, let's send it to the client */
2611 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002612 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002613 req->in.numargs = 1;
2614 req->in.args[0].size = sizeof(inarg);
2615 req->in.args[0].value = &inarg;
2616 if (in_size) {
2617 req->in.numargs++;
2618 req->in.args[1].size = in_size;
2619 req->in.argpages = 1;
2620
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002621 err = -EFAULT;
2622 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2623 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2624 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2625 if (c != PAGE_SIZE && iov_iter_count(&ii))
2626 goto out;
2627 }
Tejun Heo59efec72008-11-26 12:03:55 +01002628 }
2629
2630 req->out.numargs = 2;
2631 req->out.args[0].size = sizeof(outarg);
2632 req->out.args[0].value = &outarg;
2633 req->out.args[1].size = out_size;
2634 req->out.argpages = 1;
2635 req->out.argvar = 1;
2636
Tejun Heob93f8582008-11-26 12:03:55 +01002637 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002638 err = req->out.h.error;
2639 transferred = req->out.args[1].size;
2640 fuse_put_request(fc, req);
2641 req = NULL;
2642 if (err)
2643 goto out;
2644
2645 /* did it ask for retry? */
2646 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002647 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002648
2649 /* no retry if in restricted mode */
2650 err = -EIO;
2651 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2652 goto out;
2653
2654 in_iovs = outarg.in_iovs;
2655 out_iovs = outarg.out_iovs;
2656
2657 /*
2658 * Make sure things are in boundary, separate checks
2659 * are to protect against overflow.
2660 */
2661 err = -ENOMEM;
2662 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2663 out_iovs > FUSE_IOCTL_MAX_IOV ||
2664 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2665 goto out;
2666
Cong Wang2408f6e2011-11-25 23:14:30 +08002667 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002668 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002669 transferred, in_iovs + out_iovs,
2670 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002671 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002672 if (err)
2673 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002674
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002675 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002676 out_iov = in_iov + in_iovs;
2677
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002678 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002679 if (err)
2680 goto out;
2681
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002682 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002683 if (err)
2684 goto out;
2685
Tejun Heo59efec72008-11-26 12:03:55 +01002686 goto retry;
2687 }
2688
2689 err = -EIO;
2690 if (transferred > inarg.out_size)
2691 goto out;
2692
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002693 err = -EFAULT;
2694 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2695 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2696 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2697 if (c != PAGE_SIZE && iov_iter_count(&ii))
2698 goto out;
2699 }
2700 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002701 out:
2702 if (req)
2703 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002704 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002705 while (num_pages)
2706 __free_page(pages[--num_pages]);
2707 kfree(pages);
2708
2709 return err ? err : outarg.result;
2710}
Tejun Heo08cbf542009-04-14 10:54:53 +09002711EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002712
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002713long fuse_ioctl_common(struct file *file, unsigned int cmd,
2714 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002715{
Al Viro6131ffa2013-02-27 16:59:05 -05002716 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002717 struct fuse_conn *fc = get_fuse_conn(inode);
2718
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002719 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002720 return -EACCES;
2721
2722 if (is_bad_inode(inode))
2723 return -EIO;
2724
2725 return fuse_do_ioctl(file, cmd, arg, flags);
2726}
2727
Tejun Heo59efec72008-11-26 12:03:55 +01002728static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2729 unsigned long arg)
2730{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002731 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002732}
2733
2734static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2735 unsigned long arg)
2736{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002737 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002738}
2739
Tejun Heo95668a62008-11-26 12:03:55 +01002740/*
2741 * All files which have been polled are linked to RB tree
2742 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2743 * find the matching one.
2744 */
2745static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2746 struct rb_node **parent_out)
2747{
2748 struct rb_node **link = &fc->polled_files.rb_node;
2749 struct rb_node *last = NULL;
2750
2751 while (*link) {
2752 struct fuse_file *ff;
2753
2754 last = *link;
2755 ff = rb_entry(last, struct fuse_file, polled_node);
2756
2757 if (kh < ff->kh)
2758 link = &last->rb_left;
2759 else if (kh > ff->kh)
2760 link = &last->rb_right;
2761 else
2762 return link;
2763 }
2764
2765 if (parent_out)
2766 *parent_out = last;
2767 return link;
2768}
2769
2770/*
2771 * The file is about to be polled. Make sure it's on the polled_files
2772 * RB tree. Note that files once added to the polled_files tree are
2773 * not removed before the file is released. This is because a file
2774 * polled once is likely to be polled again.
2775 */
2776static void fuse_register_polled_file(struct fuse_conn *fc,
2777 struct fuse_file *ff)
2778{
2779 spin_lock(&fc->lock);
2780 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002781 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002782
2783 link = fuse_find_polled_node(fc, ff->kh, &parent);
2784 BUG_ON(*link);
2785 rb_link_node(&ff->polled_node, parent, link);
2786 rb_insert_color(&ff->polled_node, &fc->polled_files);
2787 }
2788 spin_unlock(&fc->lock);
2789}
2790
Al Viro076ccb72017-07-03 01:02:18 -04002791__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002792{
Tejun Heo95668a62008-11-26 12:03:55 +01002793 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002794 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002795 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2796 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002797 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002798 int err;
2799
2800 if (fc->no_poll)
2801 return DEFAULT_POLLMASK;
2802
2803 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002804 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002805
2806 /*
2807 * Ask for notification iff there's someone waiting for it.
2808 * The client may ignore the flag and always notify.
2809 */
2810 if (waitqueue_active(&ff->poll_wait)) {
2811 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2812 fuse_register_polled_file(fc, ff);
2813 }
2814
Miklos Szeredi70781872014-12-12 09:49:05 +01002815 args.in.h.opcode = FUSE_POLL;
2816 args.in.h.nodeid = ff->nodeid;
2817 args.in.numargs = 1;
2818 args.in.args[0].size = sizeof(inarg);
2819 args.in.args[0].value = &inarg;
2820 args.out.numargs = 1;
2821 args.out.args[0].size = sizeof(outarg);
2822 args.out.args[0].value = &outarg;
2823 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002824
2825 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002826 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002827 if (err == -ENOSYS) {
2828 fc->no_poll = 1;
2829 return DEFAULT_POLLMASK;
2830 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002831 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002832}
Tejun Heo08cbf542009-04-14 10:54:53 +09002833EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002834
2835/*
2836 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2837 * wakes up the poll waiters.
2838 */
2839int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2840 struct fuse_notify_poll_wakeup_out *outarg)
2841{
2842 u64 kh = outarg->kh;
2843 struct rb_node **link;
2844
2845 spin_lock(&fc->lock);
2846
2847 link = fuse_find_polled_node(fc, kh, NULL);
2848 if (*link) {
2849 struct fuse_file *ff;
2850
2851 ff = rb_entry(*link, struct fuse_file, polled_node);
2852 wake_up_interruptible_sync(&ff->poll_wait);
2853 }
2854
2855 spin_unlock(&fc->lock);
2856 return 0;
2857}
2858
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002859static void fuse_do_truncate(struct file *file)
2860{
2861 struct inode *inode = file->f_mapping->host;
2862 struct iattr attr;
2863
2864 attr.ia_valid = ATTR_SIZE;
2865 attr.ia_size = i_size_read(inode);
2866
2867 attr.ia_file = file;
2868 attr.ia_valid |= ATTR_FILE;
2869
Jan Kara62490332016-05-26 17:12:41 +02002870 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002871}
2872
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002873static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002874{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002875 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002876}
2877
Anand Avati4273b792012-02-17 12:46:25 -05002878static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002879fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002880{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002881 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002882 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002883 struct file *file = iocb->ki_filp;
2884 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002885 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002886 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002887 struct inode *inode;
2888 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002889 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002890 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002891 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002892
Anand Avati4273b792012-02-17 12:46:25 -05002893 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 inode = file->f_mapping->host;
2895 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002896
Omar Sandoval6f673762015-03-16 04:33:52 -07002897 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002898 return 0;
2899
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002900 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002901 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002902 if (offset >= i_size)
2903 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002904 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002905 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002906 }
2907
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002908 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002909 if (!io)
2910 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002911 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002912 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002913 io->reqs = 1;
2914 io->bytes = -1;
2915 io->size = 0;
2916 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002917 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002918 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002919 /*
2920 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002921 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002922 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002923 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002924 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302925 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002926
2927 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302928 * We cannot asynchronously extend the size of a file.
2929 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002930 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302931 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2932 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002933
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302934 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002935 /*
2936 * Additional reference to keep io around after
2937 * calling fuse_aio_complete()
2938 */
2939 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002940 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002941 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002942
Omar Sandoval6f673762015-03-16 04:33:52 -07002943 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002944 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002945 fuse_invalidate_attr(inode);
2946 } else {
Al Virod22a9432014-03-16 15:50:47 -04002947 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002948 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002949
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002950 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002951 bool blocking = io->blocking;
2952
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002953 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2954
2955 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002956 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002957 return -EIOCBQUEUED;
2958
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002959 wait_for_completion(&wait);
2960 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002961 }
2962
Seth Forshee744742d2016-03-11 10:35:34 -06002963 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002964
Omar Sandoval6f673762015-03-16 04:33:52 -07002965 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002966 if (ret > 0)
2967 fuse_write_update_size(inode, pos);
2968 else if (ret < 0 && offset + count > i_size)
2969 fuse_do_truncate(file);
2970 }
Anand Avati4273b792012-02-17 12:46:25 -05002971
2972 return ret;
2973}
2974
Miklos Szeredicdadb112012-11-10 16:55:56 +01002975static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2976 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002977{
2978 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002979 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002980 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002981 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002982 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002983 struct fuse_fallocate_in inarg = {
2984 .fh = ff->fh,
2985 .offset = offset,
2986 .length = length,
2987 .mode = mode
2988 };
2989 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002990 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2991 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002992
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002993 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2994 return -EOPNOTSUPP;
2995
Miklos Szeredi519c6042012-04-26 10:56:36 +02002996 if (fc->no_fallocate)
2997 return -EOPNOTSUPP;
2998
Maxim Patlasov14c14412013-06-13 12:16:39 +04002999 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003000 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003001 if (mode & FALLOC_FL_PUNCH_HOLE) {
3002 loff_t endbyte = offset + length - 1;
3003 err = filemap_write_and_wait_range(inode->i_mapping,
3004 offset, endbyte);
3005 if (err)
3006 goto out;
3007
3008 fuse_sync_writes(inode);
3009 }
Brian Foster3634a632013-05-17 09:30:32 -04003010 }
3011
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003012 if (!(mode & FALLOC_FL_KEEP_SIZE))
3013 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3014
Miklos Szeredi70781872014-12-12 09:49:05 +01003015 args.in.h.opcode = FUSE_FALLOCATE;
3016 args.in.h.nodeid = ff->nodeid;
3017 args.in.numargs = 1;
3018 args.in.args[0].size = sizeof(inarg);
3019 args.in.args[0].value = &inarg;
3020 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003021 if (err == -ENOSYS) {
3022 fc->no_fallocate = 1;
3023 err = -EOPNOTSUPP;
3024 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003025 if (err)
3026 goto out;
3027
3028 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003029 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3030 bool changed = fuse_write_update_size(inode, offset + length);
3031
Miklos Szeredi93d22692014-04-28 14:19:22 +02003032 if (changed && fc->writeback_cache)
3033 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003034 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003035
3036 if (mode & FALLOC_FL_PUNCH_HOLE)
3037 truncate_pagecache_range(inode, offset, offset + length - 1);
3038
3039 fuse_invalidate_attr(inode);
3040
Brian Foster3634a632013-05-17 09:30:32 -04003041out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003042 if (!(mode & FALLOC_FL_KEEP_SIZE))
3043 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3044
Maxim Patlasovbde52782013-09-13 19:19:54 +04003045 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003046 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003047
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003048 return err;
3049}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003050
Niels de Vos88bc7d52018-08-21 14:36:31 +02003051static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3052 struct file *file_out, loff_t pos_out,
3053 size_t len, unsigned int flags)
3054{
3055 struct fuse_file *ff_in = file_in->private_data;
3056 struct fuse_file *ff_out = file_out->private_data;
3057 struct inode *inode_out = file_inode(file_out);
3058 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3059 struct fuse_conn *fc = ff_in->fc;
3060 FUSE_ARGS(args);
3061 struct fuse_copy_file_range_in inarg = {
3062 .fh_in = ff_in->fh,
3063 .off_in = pos_in,
3064 .nodeid_out = ff_out->nodeid,
3065 .fh_out = ff_out->fh,
3066 .off_out = pos_out,
3067 .len = len,
3068 .flags = flags
3069 };
3070 struct fuse_write_out outarg;
3071 ssize_t err;
3072 /* mark unstable when write-back is not used, and file_out gets
3073 * extended */
3074 bool is_unstable = (!fc->writeback_cache) &&
3075 ((pos_out + len) > inode_out->i_size);
3076
3077 if (fc->no_copy_file_range)
3078 return -EOPNOTSUPP;
3079
3080 inode_lock(inode_out);
3081
3082 if (fc->writeback_cache) {
3083 err = filemap_write_and_wait_range(inode_out->i_mapping,
3084 pos_out, pos_out + len);
3085 if (err)
3086 goto out;
3087
3088 fuse_sync_writes(inode_out);
3089 }
3090
3091 if (is_unstable)
3092 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3093
3094 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3095 args.in.h.nodeid = ff_in->nodeid;
3096 args.in.numargs = 1;
3097 args.in.args[0].size = sizeof(inarg);
3098 args.in.args[0].value = &inarg;
3099 args.out.numargs = 1;
3100 args.out.args[0].size = sizeof(outarg);
3101 args.out.args[0].value = &outarg;
3102 err = fuse_simple_request(fc, &args);
3103 if (err == -ENOSYS) {
3104 fc->no_copy_file_range = 1;
3105 err = -EOPNOTSUPP;
3106 }
3107 if (err)
3108 goto out;
3109
3110 if (fc->writeback_cache) {
3111 fuse_write_update_size(inode_out, pos_out + outarg.size);
3112 file_update_time(file_out);
3113 }
3114
3115 fuse_invalidate_attr(inode_out);
3116
3117 err = outarg.size;
3118out:
3119 if (is_unstable)
3120 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3121
3122 inode_unlock(inode_out);
3123
3124 return err;
3125}
3126
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003127static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003128 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003129 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003130 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003131 .mmap = fuse_file_mmap,
3132 .open = fuse_open,
3133 .flush = fuse_flush,
3134 .release = fuse_release,
3135 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003136 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003137 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003138 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003139 .unlocked_ioctl = fuse_file_ioctl,
3140 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003141 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003142 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003143 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003144};
3145
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003146static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003147 .llseek = fuse_file_llseek,
Al Viro153162632015-03-30 22:08:36 -04003148 .read_iter = fuse_direct_read_iter,
Al Viro153162632015-03-30 22:08:36 -04003149 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003150 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003151 .open = fuse_open,
3152 .flush = fuse_flush,
3153 .release = fuse_release,
3154 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003155 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003156 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003157 .unlocked_ioctl = fuse_file_ioctl,
3158 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003159 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003160 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003161 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003162};
3163
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003164static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003165 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003166 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003167 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003168 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003169 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003170 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003171 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003172 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003173 .write_begin = fuse_write_begin,
3174 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003175};
3176
3177void fuse_init_file_inode(struct inode *inode)
3178{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003179 struct fuse_inode *fi = get_fuse_inode(inode);
3180
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003181 inode->i_fop = &fuse_file_operations;
3182 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003183
3184 INIT_LIST_HEAD(&fi->write_files);
3185 INIT_LIST_HEAD(&fi->queued_writes);
3186 fi->writectr = 0;
3187 init_waitqueue_head(&fi->page_waitq);
3188 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003189}