blob: a2ea347c4d2c760a9d6b3d2dbfc1896dbd538e0c [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050015#include <linux/sched/signal.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090016#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010017#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020018#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021
Miklos Szeredi72133942019-09-10 15:04:11 +020022static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
Miklos Szeredi4c4f03f2019-09-10 15:04:09 +020024{
25 struct page **pages;
26
27 pages = kzalloc(npages * (sizeof(struct page *) +
28 sizeof(struct fuse_page_desc)), flags);
29 *desc = (void *) (pages + npages);
30
31 return pages;
32}
33
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020034static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070036{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070037 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010038 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080039
40 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070041 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 if (!fc->atomic_o_trunc)
43 inarg.flags &= ~O_TRUNC;
Miklos Szeredid5b48542019-09-10 15:04:08 +020044 args.opcode = opcode;
45 args.nodeid = nodeid;
46 args.in_numargs = 1;
47 args.in_args[0].size = sizeof(inarg);
48 args.in_args[0].value = &inarg;
49 args.out_numargs = 1;
50 args.out_args[0].size = sizeof(*outargp);
51 args.out_args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052
Miklos Szeredi70781872014-12-12 09:49:05 +010053 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054}
55
Miklos Szeredi4cb54862019-09-10 15:04:10 +020056struct fuse_release_args {
57 struct fuse_args args;
58 struct fuse_release_in inarg;
59 struct inode *inode;
60};
61
Tejun Heoacf99432008-11-26 12:03:55 +010062struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080063{
64 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090065
Mateusz Jurczyk68227c02017-06-07 12:26:49 +020066 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090067 if (unlikely(!ff))
68 return NULL;
69
Miklos Szeredida5e4712009-04-28 16:56:36 +020070 ff->fc = fc;
Miklos Szeredi4cb54862019-09-10 15:04:10 +020071 ff->release_args = kzalloc(sizeof(*ff->release_args), GFP_KERNEL);
72 if (!ff->release_args) {
Tejun Heo6b2db282009-04-14 10:54:49 +090073 kfree(ff);
74 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 }
Tejun Heo6b2db282009-04-14 10:54:49 +090076
77 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020078 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020079 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090080 RB_CLEAR_NODE(&ff->polled_node);
81 init_waitqueue_head(&ff->poll_wait);
82
Miklos Szeredi75126f52019-01-24 10:40:17 +010083 ff->kh = atomic64_inc_return(&fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090084
Miklos Szeredifd72faa2005-11-07 00:59:51 -080085 return ff;
86}
87
88void fuse_file_free(struct fuse_file *ff)
89{
Miklos Szeredi4cb54862019-09-10 15:04:10 +020090 kfree(ff->release_args);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020091 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080092 kfree(ff);
93}
94
Miklos Szeredi267d8442017-02-22 20:08:25 +010095static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070096{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020097 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070098 return ff;
99}
100
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200101static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
102 int error)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100103{
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200104 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
105
106 iput(ra->inode);
107 kfree(ra);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100108}
109
Chad Austin2e64ff12018-12-10 10:54:52 -0800110static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700111{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200112 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200113 struct fuse_args *args = &ff->release_args->args;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200114
Chad Austind9a9ea92019-01-07 16:53:17 -0800115 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200116 /* Do nothing when client does not implement 'open' */
117 fuse_release_end(ff->fc, args, 0);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100118 } else if (sync) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200119 fuse_simple_request(ff->fc, args);
120 fuse_release_end(ff->fc, args, 0);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100121 } else {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200122 args->end = fuse_release_end;
123 if (fuse_simple_background(ff->fc, args,
124 GFP_KERNEL | __GFP_NOFAIL))
125 fuse_release_end(ff->fc, args, -ENOTCONN);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100126 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700127 kfree(ff);
128 }
129}
130
Tejun Heo08cbf542009-04-14 10:54:53 +0900131int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
132 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200133{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200134 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200135 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
136
137 ff = fuse_file_alloc(fc);
138 if (!ff)
139 return -ENOMEM;
140
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100141 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800142 /* Default for no-open */
143 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800144 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100145 struct fuse_open_out outarg;
146 int err;
147
148 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
149 if (!err) {
150 ff->fh = outarg.fh;
151 ff->open_flags = outarg.open_flags;
152
Chad Austind9a9ea92019-01-07 16:53:17 -0800153 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100154 fuse_file_free(ff);
155 return err;
156 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800157 if (isdir)
158 fc->no_opendir = 1;
159 else
160 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100161 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200162 }
163
164 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100165 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200166
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200167 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100168 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200169
170 return 0;
171}
Tejun Heo08cbf542009-04-14 10:54:53 +0900172EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200173
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400174static void fuse_link_write_file(struct file *file)
175{
176 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400177 struct fuse_inode *fi = get_fuse_inode(inode);
178 struct fuse_file *ff = file->private_data;
179 /*
180 * file may be written through mmap, so chain it onto the
181 * inodes's write_file list
182 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300183 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400184 if (list_empty(&ff->write_entry))
185 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300186 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400187}
188
Miklos Szeredic7b71432009-04-28 16:56:37 +0200189void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800190{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200191 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800192 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200193
Miklos Szeredic7b71432009-04-28 16:56:37 +0200194 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700195 invalidate_inode_pages2(inode->i_mapping);
Kirill Smelkovbbd84f32019-04-24 07:13:57 +0000196 if (ff->open_flags & FOPEN_STREAM)
197 stream_open(inode, file);
198 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200199 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800200 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
201 struct fuse_inode *fi = get_fuse_inode(inode);
202
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300203 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300204 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800205 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300206 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800207 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200208 if (fc->writeback_cache)
209 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800210 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400211 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
212 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800213}
214
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200215int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800216{
Tejun Heoacf99432008-11-26 12:03:55 +0100217 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700218 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200219 bool lock_inode = (file->f_flags & O_TRUNC) &&
220 fc->atomic_o_trunc &&
221 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222
223 err = generic_file_open(inode, file);
224 if (err)
225 return err;
226
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200227 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500228 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200229
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200230 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700231
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200232 if (!err)
233 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200234
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200235 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500236 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200237
238 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700239}
240
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300241static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
242 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800243{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200244 struct fuse_conn *fc = ff->fc;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200245 struct fuse_release_args *ra = ff->release_args;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700246
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300247 /* Inode is NULL on error path of fuse_create_open() */
248 if (likely(fi)) {
249 spin_lock(&fi->lock);
250 list_del(&ff->write_entry);
251 spin_unlock(&fi->lock);
252 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200253 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200254 if (!RB_EMPTY_NODE(&ff->polled_node))
255 rb_erase(&ff->polled_node, &fc->polled_files);
256 spin_unlock(&fc->lock);
257
Bryan Green357ccf22011-03-01 16:43:52 -0800258 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200259
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200260 ra->inarg.fh = ff->fh;
261 ra->inarg.flags = flags;
262 ra->args.in_numargs = 1;
263 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
264 ra->args.in_args[0].value = &ra->inarg;
265 ra->args.opcode = opcode;
266 ra->args.nodeid = ff->nodeid;
267 ra->args.force = true;
268 ra->args.nocreds = true;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800269}
270
Chad Austin2e64ff12018-12-10 10:54:52 -0800271void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800272{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300273 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100274 struct fuse_file *ff = file->private_data;
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200275 struct fuse_release_args *ra = ff->release_args;
Chad Austin2e64ff12018-12-10 10:54:52 -0800276 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700277
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300278 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100279
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200280 if (ff->flock) {
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200281 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
282 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
283 (fl_owner_t) file);
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200284 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100285 /* Hold inode until release is finished */
Miklos Szeredi4cb54862019-09-10 15:04:10 +0200286 ra->inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700287
Tejun Heo6b2db282009-04-14 10:54:49 +0900288 /*
289 * Normally this will send the RELEASE request, however if
290 * some asynchronous READ or WRITE requests are outstanding,
291 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100292 *
293 * Make the release synchronous if this is a fuseblk mount,
294 * synchronous RELEASE is allowed (and desirable) in this case
295 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900296 */
Miklos Szeredi1ccd1ea2019-09-10 15:04:09 +0200297 fuse_file_put(ff, ff->fc->destroy, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700298}
299
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700300static int fuse_open(struct inode *inode, struct file *file)
301{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200302 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700303}
304
305static int fuse_release(struct inode *inode, struct file *file)
306{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400307 struct fuse_conn *fc = get_fuse_conn(inode);
308
309 /* see fuse_vma_close() for !writeback_cache case */
310 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200311 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400312
Chad Austin2e64ff12018-12-10 10:54:52 -0800313 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200314
315 /* return value is ignored by VFS */
316 return 0;
317}
318
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300319void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200320{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200321 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300322 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100323 /*
324 * iput(NULL) is a no-op and since the refcount is 1 and everything's
325 * synchronous, we are fine with not doing igrab() here"
326 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800327 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700328}
Tejun Heo08cbf542009-04-14 10:54:53 +0900329EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700330
Miklos Szeredi71421252006-06-25 05:48:52 -0700331/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700332 * Scramble the ID space with XTEA, so that the value of the files_struct
333 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700334 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700335u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700336{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700337 u32 *k = fc->scramble_key;
338 u64 v = (unsigned long) id;
339 u32 v0 = v;
340 u32 v1 = v >> 32;
341 u32 sum = 0;
342 int i;
343
344 for (i = 0; i < 32; i++) {
345 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
346 sum += 0x9E3779B9;
347 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
348 }
349
350 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700351}
352
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200353struct fuse_writepage_args {
354 struct fuse_io_args ia;
355 struct list_head writepages_entry;
356 struct list_head queue_entry;
357 struct fuse_writepage_args *next;
358 struct inode *inode;
359};
360
361static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100362 pgoff_t idx_from, pgoff_t idx_to)
363{
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200364 struct fuse_writepage_args *wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100365
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200366 list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100367 pgoff_t curr_index;
368
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200369 WARN_ON(get_fuse_inode(wpa->inode) != fi);
370 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
371 if (idx_from < curr_index + wpa->ia.ap.num_pages &&
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100372 curr_index <= idx_to) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +0200373 return wpa;
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100374 }
375 }
376 return NULL;
377}
378
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700379/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400380 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700381 *
382 * This is currently done by walking the list of writepage requests
383 * for the inode, which can be pretty inefficient.
384 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400385static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
386 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700387{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700388 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100389 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700390
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300391 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100392 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300393 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700394
395 return found;
396}
397
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400398static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
399{
400 return fuse_range_is_writeback(inode, index, index);
401}
402
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700403/*
404 * Wait for page writeback to be completed.
405 *
406 * Since fuse doesn't rely on the VM writeback tracking, this has to
407 * use some other means.
408 */
Maxim Patlasov17b2cbe2019-07-22 10:17:17 +0300409static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700410{
411 struct fuse_inode *fi = get_fuse_inode(inode);
412
413 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700414}
415
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400416/*
417 * Wait for all pending writepages on the inode to finish.
418 *
419 * This is currently done by blocking further writes with FUSE_NOWRITE
420 * and waiting for all sent writes to complete.
421 *
422 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
423 * could conflict with truncation.
424 */
425static void fuse_sync_writes(struct inode *inode)
426{
427 fuse_set_nowrite(inode);
428 fuse_release_nowrite(inode);
429}
430
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700431static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700432{
Al Viro6131ffa2013-02-27 16:59:05 -0500433 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 struct fuse_conn *fc = get_fuse_conn(inode);
435 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700436 struct fuse_flush_in inarg;
Miklos Szeredic500eba2019-09-10 15:04:08 +0200437 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700438 int err;
439
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800440 if (is_bad_inode(inode))
441 return -EIO;
442
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700443 if (fc->no_flush)
444 return 0;
445
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200446 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400447 if (err)
448 return err;
449
Al Viro59551022016-01-22 15:40:57 -0500450 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400451 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500452 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400453
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200454 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700455 if (err)
456 return err;
457
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 memset(&inarg, 0, sizeof(inarg));
459 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700460 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200461 args.opcode = FUSE_FLUSH;
462 args.nodeid = get_node_id(inode);
463 args.in_numargs = 1;
464 args.in_args[0].size = sizeof(inarg);
465 args.in_args[0].value = &inarg;
466 args.force = true;
467
468 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700469 if (err == -ENOSYS) {
470 fc->no_flush = 1;
471 err = 0;
472 }
473 return err;
474}
475
Josef Bacik02c24a82011-07-16 20:44:56 -0400476int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100477 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700478{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200479 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480 struct fuse_conn *fc = get_fuse_conn(inode);
481 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100482 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100484
485 memset(&inarg, 0, sizeof(inarg));
486 inarg.fh = ff->fh;
Alan Somers154603f2019-04-19 15:42:44 -0600487 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200488 args.opcode = opcode;
489 args.nodeid = get_node_id(inode);
490 args.in_numargs = 1;
491 args.in_args[0].size = sizeof(inarg);
492 args.in_args[0].value = &inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100493 return fuse_simple_request(fc, &args);
494}
495
496static int fuse_fsync(struct file *file, loff_t start, loff_t end,
497 int datasync)
498{
499 struct inode *inode = file->f_mapping->host;
500 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501 int err;
502
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800503 if (is_bad_inode(inode))
504 return -EIO;
505
Al Viro59551022016-01-22 15:40:57 -0500506 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400507
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700508 /*
509 * Start writeback against all dirty pages of the inode, then
510 * wait for all outstanding writes, before sending the FSYNC
511 * request.
512 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400513 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700514 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400515 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700516
517 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700518
519 /*
520 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400521 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700522 * We have to do this directly after fuse_sync_writes()
523 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400524 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700525 if (err)
526 goto out;
527
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200528 err = sync_inode_metadata(inode, 1);
529 if (err)
530 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700531
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100532 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200533 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400534
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100535 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700536 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100537 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700538 err = 0;
539 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400540out:
Al Viro59551022016-01-22 15:40:57 -0500541 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700542
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100543 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700544}
545
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200546void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
547 size_t count, int opcode)
548{
549 struct fuse_file *ff = file->private_data;
550 struct fuse_args *args = &ia->ap.args;
551
552 ia->read.in.fh = ff->fh;
553 ia->read.in.offset = pos;
554 ia->read.in.size = count;
555 ia->read.in.flags = file->f_flags;
556 args->opcode = opcode;
557 args->nodeid = ff->nodeid;
558 args->in_numargs = 1;
559 args->in_args[0].size = sizeof(ia->read.in);
560 args->in_args[0].value = &ia->read.in;
561 args->out_argvar = true;
562 args->out_numargs = 1;
563 args->out_args[0].size = count;
564}
565
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200566static void fuse_release_user_pages(struct fuse_args_pages *ap,
567 bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400568{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200569 unsigned int i;
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400570
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200571 for (i = 0; i < ap->num_pages; i++) {
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200572 if (should_dirty)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200573 set_page_dirty_lock(ap->pages[i]);
574 put_page(ap->pages[i]);
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400575 }
576}
577
Seth Forshee744742d2016-03-11 10:35:34 -0600578static void fuse_io_release(struct kref *kref)
579{
580 kfree(container_of(kref, struct fuse_io_priv, refcnt));
581}
582
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100583static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
584{
585 if (io->err)
586 return io->err;
587
588 if (io->bytes >= 0 && io->write)
589 return -EIO;
590
591 return io->bytes < 0 ? io->size : io->bytes;
592}
593
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400594/**
595 * In case of short read, the caller sets 'pos' to the position of
596 * actual end of fuse request in IO request. Otherwise, if bytes_requested
597 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
598 *
599 * An example:
600 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
601 * both submitted asynchronously. The first of them was ACKed by userspace as
602 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
603 * second request was ACKed as short, e.g. only 1K was read, resulting in
604 * pos == 33K.
605 *
606 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
607 * will be equal to the length of the longest contiguous fragment of
608 * transferred data starting from the beginning of IO request.
609 */
610static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
611{
612 int left;
613
614 spin_lock(&io->lock);
615 if (err)
616 io->err = io->err ? : err;
617 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
618 io->bytes = pos;
619
620 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530621 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100622 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400623 spin_unlock(&io->lock);
624
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530625 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100626 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400627
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100628 if (res >= 0) {
629 struct inode *inode = file_inode(io->iocb->ki_filp);
630 struct fuse_conn *fc = get_fuse_conn(inode);
631 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400632
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300633 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300634 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300635 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400636 }
637
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100638 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400639 }
Seth Forshee744742d2016-03-11 10:35:34 -0600640
641 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400642}
643
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200644static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
645 unsigned int npages)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400646{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200647 struct fuse_io_args *ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400648
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200649 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
650 if (ia) {
651 ia->io = io;
652 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
653 &ia->ap.descs);
654 if (!ia->ap.pages) {
655 kfree(ia);
656 ia = NULL;
657 }
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400658 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200659 return ia;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400660}
661
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200662static void fuse_io_free(struct fuse_io_args *ia)
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400663{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200664 kfree(ia->ap.pages);
665 kfree(ia);
666}
667
668static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
669 int err)
670{
671 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
672 struct fuse_io_priv *io = ia->io;
673 ssize_t pos = -1;
674
675 fuse_release_user_pages(&ia->ap, io->should_dirty);
676
677 if (err) {
678 /* Nothing */
679 } else if (io->write) {
680 if (ia->write.out.size > ia->write.in.size) {
681 err = -EIO;
682 } else if (ia->write.in.size != ia->write.out.size) {
683 pos = ia->write.in.offset - io->offset +
684 ia->write.out.size;
685 }
686 } else {
687 u32 outsize = args->out_args[0].size;
688
689 if (ia->read.in.size != outsize)
690 pos = ia->read.in.offset - io->offset + outsize;
691 }
692
693 fuse_aio_complete(io, err, pos);
694 fuse_io_free(ia);
695}
696
697static ssize_t fuse_async_req_send(struct fuse_conn *fc,
698 struct fuse_io_args *ia, size_t num_bytes)
699{
700 ssize_t err;
701 struct fuse_io_priv *io = ia->io;
702
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400703 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600704 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400705 io->size += num_bytes;
706 io->reqs++;
707 spin_unlock(&io->lock);
708
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200709 ia->ap.args.end = fuse_aio_complete_req;
710 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400711
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200712 return err ?: num_bytes;
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400713}
714
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200715static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
716 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700717{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200718 struct file *file = ia->io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200719 struct fuse_file *ff = file->private_data;
720 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700721
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200722 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700723 if (owner != NULL) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200724 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
725 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
Miklos Szeredif3332112007-10-18 03:07:04 -0700726 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400727
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200728 if (ia->io->async)
729 return fuse_async_req_send(fc, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400730
Miklos Szeredi45ac96e2019-09-10 15:04:10 +0200731 return fuse_simple_request(fc, &ia->ap.args);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700732}
733
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700734static void fuse_read_update_size(struct inode *inode, loff_t size,
735 u64 attr_ver)
736{
737 struct fuse_conn *fc = get_fuse_conn(inode);
738 struct fuse_inode *fi = get_fuse_inode(inode);
739
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300740 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400741 if (attr_ver == fi->attr_version && size < inode->i_size &&
742 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300743 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700744 i_size_write(inode, size);
745 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300746 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747}
748
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200749static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
Miklos Szeredi134831e2019-09-10 15:04:10 +0200750 struct fuse_args_pages *ap)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400751{
Pavel Emelyanov83732002013-10-10 17:10:46 +0400752 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400753
Pavel Emelyanov83732002013-10-10 17:10:46 +0400754 if (fc->writeback_cache) {
755 /*
756 * A hole in a file. Some data after the hole are in page cache,
757 * but have not reached the client fs yet. So, the hole is not
758 * present there.
759 */
760 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 int start_idx = num_read >> PAGE_SHIFT;
762 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400763
Miklos Szeredi134831e2019-09-10 15:04:10 +0200764 for (i = start_idx; i < ap->num_pages; i++) {
765 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400766 off = 0;
767 }
768 } else {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200769 loff_t pos = page_offset(ap->pages[0]) + num_read;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400770 fuse_read_update_size(inode, pos, attr_ver);
771 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400772}
773
Maxim Patlasov482fce52013-10-10 17:11:25 +0400774static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700775{
776 struct inode *inode = page->mapping->host;
777 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700778 loff_t pos = page_offset(page);
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200779 struct fuse_page_desc desc = { .length = PAGE_SIZE };
780 struct fuse_io_args ia = {
781 .ap.args.page_zeroing = true,
782 .ap.args.out_pages = true,
783 .ap.num_pages = 1,
784 .ap.pages = &page,
785 .ap.descs = &desc,
786 };
787 ssize_t res;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700788 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800789
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700790 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300791 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700792 * page-cache page, so make sure we read a properly synced
793 * page.
794 */
795 fuse_wait_on_page_writeback(inode, page->index);
796
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700797 attr_ver = fuse_get_attr_version(fc);
798
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200799 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
800 res = fuse_simple_request(fc, &ia.ap.args);
801 if (res < 0)
802 return res;
803 /*
804 * Short read means EOF. If file size is larger, truncate it
805 */
806 if (res < desc.length)
Miklos Szeredi134831e2019-09-10 15:04:10 +0200807 fuse_short_read(inode, attr_ver, res, &ia.ap);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700808
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200809 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700810
Miklos Szeredi00793ca2019-09-10 15:04:09 +0200811 return 0;
Maxim Patlasov482fce52013-10-10 17:11:25 +0400812}
813
814static int fuse_readpage(struct file *file, struct page *page)
815{
816 struct inode *inode = page->mapping->host;
817 int err;
818
819 err = -EIO;
820 if (is_bad_inode(inode))
821 goto out;
822
823 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800824 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700825 out:
826 unlock_page(page);
827 return err;
828}
829
Miklos Szeredi134831e2019-09-10 15:04:10 +0200830static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
831 int err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700832{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800833 int i;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200834 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
835 struct fuse_args_pages *ap = &ia->ap;
836 size_t count = ia->read.in.size;
837 size_t num_read = args->out_args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200838 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800839
Miklos Szeredi134831e2019-09-10 15:04:10 +0200840 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
841 mapping = ap->pages[i]->mapping;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200842
843 if (mapping) {
844 struct inode *inode = mapping->host;
845
846 /*
847 * Short read means EOF. If file size is larger, truncate it
848 */
Miklos Szeredi134831e2019-09-10 15:04:10 +0200849 if (!err && num_read < count)
850 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200851
Andrew Gallagher451418f2013-11-05 03:55:43 -0800852 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700853 }
854
Miklos Szeredi134831e2019-09-10 15:04:10 +0200855 for (i = 0; i < ap->num_pages; i++) {
856 struct page *page = ap->pages[i];
857
858 if (!err)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700859 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800860 else
861 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700862 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300863 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200865 if (ia->ff)
866 fuse_file_put(ia->ff, false, false);
867
868 fuse_io_free(ia);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800869}
870
Miklos Szeredi134831e2019-09-10 15:04:10 +0200871static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800872{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200873 struct fuse_file *ff = file->private_data;
874 struct fuse_conn *fc = ff->fc;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200875 struct fuse_args_pages *ap = &ia->ap;
876 loff_t pos = page_offset(ap->pages[0]);
877 size_t count = ap->num_pages << PAGE_SHIFT;
878 int err;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200879
Miklos Szeredi134831e2019-09-10 15:04:10 +0200880 ap->args.out_pages = true;
881 ap->args.page_zeroing = true;
882 ap->args.page_replace = true;
883 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
884 ia->read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800885 if (fc->async_read) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200886 ia->ff = fuse_file_get(ff);
887 ap->args.end = fuse_readpages_end;
888 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
889 if (!err)
890 return;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800891 } else {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200892 err = fuse_simple_request(fc, &ap->args);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800893 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200894 fuse_readpages_end(fc, &ap->args, err);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700895}
896
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700897struct fuse_fill_data {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200898 struct fuse_io_args *ia;
Miklos Szeredia6643092007-11-28 16:22:00 -0800899 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700900 struct inode *inode;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200901 unsigned int nr_pages;
902 unsigned int max_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700903};
904
905static int fuse_readpages_fill(void *_data, struct page *page)
906{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700907 struct fuse_fill_data *data = _data;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200908 struct fuse_io_args *ia = data->ia;
909 struct fuse_args_pages *ap = &ia->ap;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700910 struct inode *inode = data->inode;
911 struct fuse_conn *fc = get_fuse_conn(inode);
912
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700913 fuse_wait_on_page_writeback(inode, page->index);
914
Miklos Szeredi134831e2019-09-10 15:04:10 +0200915 if (ap->num_pages &&
916 (ap->num_pages == fc->max_pages ||
917 (ap->num_pages + 1) * PAGE_SIZE > fc->max_read ||
918 ap->pages[ap->num_pages - 1]->index + 1 != page->index)) {
919 data->max_pages = min_t(unsigned int, data->nr_pages,
920 fc->max_pages);
921 fuse_send_readpages(ia, data->file);
922 data->ia = ia = fuse_io_alloc(NULL, data->max_pages);
923 if (!ia) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700924 unlock_page(page);
Miklos Szeredi134831e2019-09-10 15:04:10 +0200925 return -ENOMEM;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700926 }
Miklos Szeredi134831e2019-09-10 15:04:10 +0200927 ap = &ia->ap;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700928 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400929
Miklos Szeredi134831e2019-09-10 15:04:10 +0200930 if (WARN_ON(ap->num_pages >= data->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300931 unlock_page(page);
Miklos Szeredi134831e2019-09-10 15:04:10 +0200932 fuse_io_free(ia);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400933 return -EIO;
934 }
935
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300936 get_page(page);
Miklos Szeredi134831e2019-09-10 15:04:10 +0200937 ap->pages[ap->num_pages] = page;
938 ap->descs[ap->num_pages].length = PAGE_SIZE;
939 ap->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400940 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700941 return 0;
942}
943
944static int fuse_readpages(struct file *file, struct address_space *mapping,
945 struct list_head *pages, unsigned nr_pages)
946{
947 struct inode *inode = mapping->host;
948 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700949 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700950 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800951
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700952 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800953 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800954 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800955
Miklos Szeredia6643092007-11-28 16:22:00 -0800956 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700957 data.inode = inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400958 data.nr_pages = nr_pages;
Miklos Szeredi134831e2019-09-10 15:04:10 +0200959 data.max_pages = min_t(unsigned int, nr_pages, fc->max_pages);
960;
961 data.ia = fuse_io_alloc(NULL, data.max_pages);
962 err = -ENOMEM;
963 if (!data.ia)
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800964 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700965
966 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700967 if (!err) {
Miklos Szeredi134831e2019-09-10 15:04:10 +0200968 if (data.ia->ap.num_pages)
969 fuse_send_readpages(data.ia, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700970 else
Miklos Szeredi134831e2019-09-10 15:04:10 +0200971 fuse_io_free(data.ia);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700972 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800973out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700974 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700975}
976
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100977static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800978{
979 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400980 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800981
Brian Fostera8894272012-07-16 15:23:50 -0400982 /*
983 * In auto invalidate mode, always update attributes on read.
984 * Otherwise, only update if we attempt to read past EOF (to ensure
985 * i_size is up to date).
986 */
987 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400988 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800989 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200990 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800991 if (err)
992 return err;
993 }
994
Al Viro37c20f12014-04-02 14:47:09 -0400995 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800996}
997
Miklos Szeredi338f2e32019-09-10 15:04:09 +0200998static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
999 loff_t pos, size_t count)
1000{
1001 struct fuse_args *args = &ia->ap.args;
1002
1003 ia->write.in.fh = ff->fh;
1004 ia->write.in.offset = pos;
1005 ia->write.in.size = count;
1006 args->opcode = FUSE_WRITE;
1007 args->nodeid = ff->nodeid;
1008 args->in_numargs = 2;
1009 if (ff->fc->minor < 9)
1010 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1011 else
1012 args->in_args[0].size = sizeof(ia->write.in);
1013 args->in_args[0].value = &ia->write.in;
1014 args->in_args[1].size = count;
1015 args->out_numargs = 1;
1016 args->out_args[0].size = sizeof(ia->write.out);
1017 args->out_args[0].value = &ia->write.out;
1018}
1019
1020static unsigned int fuse_write_flags(struct kiocb *iocb)
1021{
1022 unsigned int flags = iocb->ki_filp->f_flags;
1023
1024 if (iocb->ki_flags & IOCB_DSYNC)
1025 flags |= O_DSYNC;
1026 if (iocb->ki_flags & IOCB_SYNC)
1027 flags |= O_SYNC;
1028
1029 return flags;
1030}
1031
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001032static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1033 size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001034{
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001035 struct kiocb *iocb = ia->io->iocb;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001036 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001037 struct fuse_file *ff = file->private_data;
1038 struct fuse_conn *fc = ff->fc;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001039 struct fuse_write_in *inarg = &ia->write.in;
1040 ssize_t err;
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001041
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001042 fuse_write_args_fill(ia, ff, pos, count);
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001043 inarg->flags = fuse_write_flags(iocb);
Miklos Szeredif3332112007-10-18 03:07:04 -07001044 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001045 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1046 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1047 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001048
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001049 if (ia->io->async)
1050 return fuse_async_req_send(fc, ia, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001051
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001052 err = fuse_simple_request(fc, &ia->ap.args);
1053 if (!err && ia->write.out.size > count)
1054 err = -EIO;
1055
1056 return err ?: ia->write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001057}
1058
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001059bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001060{
1061 struct fuse_conn *fc = get_fuse_conn(inode);
1062 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001063 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001064
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001065 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001066 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001067 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001068 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001069 ret = true;
1070 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001071 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001072
1073 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001074}
1075
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001076static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1077 struct kiocb *iocb, struct inode *inode,
1078 loff_t pos, size_t count)
Nick Pigginea9b9902008-04-30 00:54:42 -07001079{
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001080 struct fuse_args_pages *ap = &ia->ap;
1081 struct file *file = iocb->ki_filp;
1082 struct fuse_file *ff = file->private_data;
1083 struct fuse_conn *fc = ff->fc;
1084 unsigned int offset, i;
1085 int err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001086
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001087 for (i = 0; i < ap->num_pages; i++)
1088 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
Nick Pigginea9b9902008-04-30 00:54:42 -07001089
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001090 fuse_write_args_fill(ia, ff, pos, count);
1091 ia->write.in.flags = fuse_write_flags(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001092
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001093 err = fuse_simple_request(fc, &ap->args);
Nick Pigginea9b9902008-04-30 00:54:42 -07001094
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001095 offset = ap->descs[0].offset;
1096 count = ia->write.out.size;
1097 for (i = 0; i < ap->num_pages; i++) {
1098 struct page *page = ap->pages[i];
1099
1100 if (!err && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001101 SetPageUptodate(page);
1102
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001103 if (count > PAGE_SIZE - offset)
1104 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 else
1106 count = 0;
1107 offset = 0;
1108
1109 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001110 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001111 }
1112
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001113 return err;
Nick Pigginea9b9902008-04-30 00:54:42 -07001114}
1115
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001116static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1117 struct address_space *mapping,
1118 struct iov_iter *ii, loff_t pos,
1119 unsigned int max_pages)
Nick Pigginea9b9902008-04-30 00:54:42 -07001120{
1121 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001122 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001123 size_t count = 0;
1124 int err;
1125
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001126 ap->args.in_pages = true;
1127 ap->descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001128
1129 do {
1130 size_t tmp;
1131 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 pgoff_t index = pos >> PAGE_SHIFT;
1133 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001134 iov_iter_count(ii));
1135
1136 bytes = min_t(size_t, bytes, fc->max_write - count);
1137
1138 again:
1139 err = -EFAULT;
1140 if (iov_iter_fault_in_readable(ii, bytes))
1141 break;
1142
1143 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001144 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001145 if (!page)
1146 break;
1147
anfei zhou931e80e2010-02-02 13:44:02 -08001148 if (mapping_writably_mapped(mapping))
1149 flush_dcache_page(page);
1150
Nick Pigginea9b9902008-04-30 00:54:42 -07001151 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001152 flush_dcache_page(page);
1153
Roman Gushchin3ca81382015-10-12 16:33:44 +03001154 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001155 if (!tmp) {
1156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001157 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001158 bytes = min(bytes, iov_iter_single_seg_count(ii));
1159 goto again;
1160 }
1161
1162 err = 0;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001163 ap->pages[ap->num_pages] = page;
1164 ap->descs[ap->num_pages].length = tmp;
1165 ap->num_pages++;
Nick Pigginea9b9902008-04-30 00:54:42 -07001166
Nick Pigginea9b9902008-04-30 00:54:42 -07001167 count += tmp;
1168 pos += tmp;
1169 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001171 offset = 0;
1172
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001173 if (!fc->big_writes)
1174 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001175 } while (iov_iter_count(ii) && count < fc->max_write &&
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001176 ap->num_pages < max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001177
1178 return count > 0 ? count : err;
1179}
1180
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001181static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1182 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001183{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001184 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001185 ((pos + len - 1) >> PAGE_SHIFT) -
1186 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001187 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001188}
1189
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001190static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001191 struct address_space *mapping,
1192 struct iov_iter *ii, loff_t pos)
1193{
1194 struct inode *inode = mapping->host;
1195 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001196 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001197 int err = 0;
1198 ssize_t res = 0;
1199
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001200 if (inode->i_size < pos + iov_iter_count(ii))
1201 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1202
Nick Pigginea9b9902008-04-30 00:54:42 -07001203 do {
Nick Pigginea9b9902008-04-30 00:54:42 -07001204 ssize_t count;
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001205 struct fuse_io_args ia = {};
1206 struct fuse_args_pages *ap = &ia.ap;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001207 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1208 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001209
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001210 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1211 if (!ap->pages) {
1212 err = -ENOMEM;
Nick Pigginea9b9902008-04-30 00:54:42 -07001213 break;
1214 }
1215
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001216 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001217 if (count <= 0) {
1218 err = count;
1219 } else {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001220 err = fuse_send_write_pages(&ia, iocb, inode,
1221 pos, count);
Nick Pigginea9b9902008-04-30 00:54:42 -07001222 if (!err) {
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001223 size_t num_written = ia.write.out.size;
1224
Nick Pigginea9b9902008-04-30 00:54:42 -07001225 res += num_written;
1226 pos += num_written;
1227
1228 /* break out of the loop on short write */
1229 if (num_written != count)
1230 err = -EIO;
1231 }
1232 }
Miklos Szeredi338f2e32019-09-10 15:04:09 +02001233 kfree(ap->pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001234 } while (!err && iov_iter_count(ii));
1235
1236 if (res > 0)
1237 fuse_write_update_size(inode, pos);
1238
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001239 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001240 fuse_invalidate_attr(inode);
1241
1242 return res > 0 ? res : err;
1243}
1244
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001245static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001246{
1247 struct file *file = iocb->ki_filp;
1248 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001249 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001250 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001251 struct inode *inode = mapping->host;
1252 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001253 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001254
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001255 if (get_fuse_conn(inode)->writeback_cache) {
1256 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001257 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001258 if (err)
1259 return err;
1260
Al Viro84c3d552014-04-03 14:33:23 -04001261 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001262 }
1263
Al Viro59551022016-01-22 15:40:57 -05001264 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001265
1266 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001267 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001268
Al Viro3309dd02015-04-09 12:55:47 -04001269 err = generic_write_checks(iocb, from);
1270 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001271 goto out;
1272
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001273 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001274 if (err)
1275 goto out;
1276
Josef Bacikc3b2da32012-03-26 09:59:21 -04001277 err = file_update_time(file);
1278 if (err)
1279 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001280
Al Viro2ba48ce2015-04-09 13:52:01 -04001281 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001282 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001283 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001284 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001285 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001286
Anand Avati4273b792012-02-17 12:46:25 -05001287 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001288
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001289 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001290 if (written_buffered < 0) {
1291 err = written_buffered;
1292 goto out;
1293 }
1294 endbyte = pos + written_buffered - 1;
1295
1296 err = filemap_write_and_wait_range(file->f_mapping, pos,
1297 endbyte);
1298 if (err)
1299 goto out;
1300
1301 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001302 pos >> PAGE_SHIFT,
1303 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001304
1305 written += written_buffered;
1306 iocb->ki_pos = pos + written_buffered;
1307 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001308 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001309 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001310 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001311 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001312out:
1313 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001314 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001315 if (written > 0)
1316 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001317
1318 return written ? written : err;
1319}
1320
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001321static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1322 unsigned int index,
1323 unsigned int nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001324{
1325 int i;
1326
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001327 for (i = index; i < index + nr_pages; i++)
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001328 descs[i].length = PAGE_SIZE - descs[i].offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001329}
1330
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001331static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1332{
1333 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1334}
1335
1336static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1337 size_t max_size)
1338{
1339 return min(iov_iter_single_seg_count(ii), max_size);
1340}
1341
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001342static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1343 size_t *nbytesp, int write,
1344 unsigned int max_pages)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001345{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001346 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001347 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001348
Miklos Szeredif4975c62009-04-02 14:25:34 +02001349 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001350 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001351 unsigned long user_addr = fuse_get_user_addr(ii);
1352 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1353
Miklos Szeredif4975c62009-04-02 14:25:34 +02001354 if (write)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001355 ap->args.in_args[1].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001356 else
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001357 ap->args.out_args[0].value = (void *) user_addr;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001358
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001359 iov_iter_advance(ii, frag_size);
1360 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001361 return 0;
1362 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001364 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001365 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001366 size_t start;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001367 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001368 *nbytesp - nbytes,
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001369 max_pages - ap->num_pages,
Al Viroc7f38882014-06-18 20:34:33 -04001370 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001371 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001372 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001373
Al Viroc9c37e22014-03-16 16:08:30 -04001374 iov_iter_advance(ii, ret);
1375 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001376
Al Viroc9c37e22014-03-16 16:08:30 -04001377 ret += start;
1378 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1379
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001380 ap->descs[ap->num_pages].offset = start;
1381 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001382
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001383 ap->num_pages += npages;
1384 ap->descs[ap->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001385 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001386 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001387
1388 if (write)
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001389 ap->args.in_pages = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001390 else
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001391 ap->args.out_pages = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001392
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001393 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001394
Ashish Samant2c932d42016-03-25 10:53:41 -07001395 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396}
1397
Al Virod22a9432014-03-16 15:50:47 -04001398ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1399 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001401 int write = flags & FUSE_DIO_WRITE;
1402 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001403 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001404 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001405 struct fuse_file *ff = file->private_data;
1406 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407 size_t nmax = write ? fc->max_write : fc->max_read;
1408 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001409 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001410 pgoff_t idx_from = pos >> PAGE_SHIFT;
1411 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001412 ssize_t res = 0;
Ashish Samant742f9922016-03-14 21:57:35 -07001413 int err = 0;
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001414 struct fuse_io_args *ia;
1415 unsigned int max_pages;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001416
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001417 max_pages = iov_iter_npages(iter, fc->max_pages);
1418 ia = fuse_io_alloc(io, max_pages);
1419 if (!ia)
1420 return -ENOMEM;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001421
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001422 ia->io = io;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001423 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1424 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001425 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001426 fuse_sync_writes(inode);
1427 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001428 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001429 }
1430
Ashish Samant61c12b42017-07-12 19:26:58 -07001431 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001432 while (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001433 ssize_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001434 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001435 size_t nbytes = min(count, nmax);
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001436
1437 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1438 max_pages);
Ashish Samant742f9922016-03-14 21:57:35 -07001439 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001441
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001442 if (write) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001443 if (!capable(CAP_FSETID))
1444 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001445
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001446 nres = fuse_send_write(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001447 } else {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001448 nres = fuse_send_read(ia, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001449 }
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001450
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001451 if (!io->async || nres < 0) {
1452 fuse_release_user_pages(&ia->ap, io->should_dirty);
1453 fuse_io_free(ia);
1454 }
1455 ia = NULL;
1456 if (nres < 0) {
1457 err = nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001458 break;
1459 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001460 WARN_ON(nres > nbytes);
1461
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001462 count -= nres;
1463 res += nres;
1464 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001465 if (nres != nbytes)
1466 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001467 if (count) {
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001468 max_pages = iov_iter_npages(iter, fc->max_pages);
1469 ia = fuse_io_alloc(io, max_pages);
1470 if (!ia)
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001471 break;
1472 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001473 }
Miklos Szeredi45ac96e2019-09-10 15:04:10 +02001474 if (ia)
1475 fuse_io_free(ia);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001476 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001477 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001478
Ashish Samant742f9922016-03-14 21:57:35 -07001479 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001480}
Tejun Heo08cbf542009-04-14 10:54:53 +09001481EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001482
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001483static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001484 struct iov_iter *iter,
1485 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001486{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001487 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001488 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001489
Al Virod22a9432014-03-16 15:50:47 -04001490 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001491
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001492 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001493
1494 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001495}
1496
Martin Raiber23c94e12018-10-27 16:48:48 +00001497static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1498
Al Viro153162632015-03-30 22:08:36 -04001499static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001500{
Martin Raiber23c94e12018-10-27 16:48:48 +00001501 ssize_t res;
1502
1503 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001504 res = fuse_direct_IO(iocb, to);
1505 } else {
1506 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1507
1508 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1509 }
1510
1511 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001512}
1513
Al Viro153162632015-03-30 22:08:36 -04001514static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001515{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001516 struct inode *inode = file_inode(iocb->ki_filp);
1517 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001518 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001519
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001520 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001521 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001522 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001523 if (res > 0) {
1524 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1525 res = fuse_direct_IO(iocb, from);
1526 } else {
1527 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1528 FUSE_DIO_WRITE);
1529 }
1530 }
Al Viro812408f2015-03-30 22:15:58 -04001531 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001532 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001533 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001534 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001535
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001536 return res;
1537}
1538
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001539static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1540{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001541 struct file *file = iocb->ki_filp;
1542 struct fuse_file *ff = file->private_data;
1543
1544 if (is_bad_inode(file_inode(file)))
1545 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001546
1547 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1548 return fuse_cache_read_iter(iocb, to);
1549 else
1550 return fuse_direct_read_iter(iocb, to);
1551}
1552
1553static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1554{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001555 struct file *file = iocb->ki_filp;
1556 struct fuse_file *ff = file->private_data;
1557
1558 if (is_bad_inode(file_inode(file)))
1559 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001560
1561 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1562 return fuse_cache_write_iter(iocb, from);
1563 else
1564 return fuse_direct_write_iter(iocb, from);
1565}
1566
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001567static void fuse_writepage_free(struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001568{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001569 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001570 int i;
1571
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001572 for (i = 0; i < ap->num_pages; i++)
1573 __free_page(ap->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001574
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001575 if (wpa->ia.ff)
1576 fuse_file_put(wpa->ia.ff, false, false);
1577
1578 kfree(ap->pages);
1579 kfree(wpa);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001580}
1581
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001582static void fuse_writepage_finish(struct fuse_conn *fc,
1583 struct fuse_writepage_args *wpa)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001584{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001585 struct fuse_args_pages *ap = &wpa->ia.ap;
1586 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001587 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001588 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001589 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001590
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001591 list_del(&wpa->writepages_entry);
1592 for (i = 0; i < ap->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001593 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001594 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001595 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001596 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001597 wake_up(&fi->page_waitq);
1598}
1599
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001600/* Called under fi->lock, may release and reacquire it */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001601static void fuse_send_writepage(struct fuse_conn *fc,
1602 struct fuse_writepage_args *wpa, loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001603__releases(fi->lock)
1604__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001605{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001606 struct fuse_writepage_args *aux, *next;
1607 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1608 struct fuse_write_in *inarg = &wpa->ia.write.in;
1609 struct fuse_args *args = &wpa->ia.ap.args;
1610 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1611 int err;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001612
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001613 fi->writectr++;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001614 if (inarg->offset + data_size <= size) {
1615 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001616 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001617 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001618 } else {
1619 /* Got truncated off completely */
1620 goto out_free;
1621 }
1622
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001623 args->in_args[1].size = inarg->size;
1624 args->force = true;
1625 args->nocreds = true;
1626
1627 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1628 if (err == -ENOMEM) {
1629 spin_unlock(&fi->lock);
1630 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1631 spin_lock(&fi->lock);
1632 }
1633
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001634 /* Fails on broken connection only */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001635 if (unlikely(err))
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001636 goto out_free;
1637
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001638 return;
1639
1640 out_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001641 fi->writectr--;
1642 fuse_writepage_finish(fc, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001643 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001644
1645 /* After fuse_writepage_finish() aux request list is private */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001646 for (aux = wpa->next; aux; aux = next) {
1647 next = aux->next;
1648 aux->next = NULL;
1649 fuse_writepage_free(aux);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001650 }
1651
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001652 fuse_writepage_free(wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001653 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001654}
1655
1656/*
1657 * If fi->writectr is positive (no truncate or fsync going on) send
1658 * all queued writepage requests.
1659 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001660 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001661 */
1662void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001663__releases(fi->lock)
1664__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001665{
1666 struct fuse_conn *fc = get_fuse_conn(inode);
1667 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9de5be02019-04-24 17:05:06 +02001668 loff_t crop = i_size_read(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001669 struct fuse_writepage_args *wpa;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001670
1671 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001672 wpa = list_entry(fi->queued_writes.next,
1673 struct fuse_writepage_args, queue_entry);
1674 list_del_init(&wpa->queue_entry);
1675 fuse_send_writepage(fc, wpa, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001676 }
1677}
1678
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001679static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1680 int error)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001681{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001682 struct fuse_writepage_args *wpa =
1683 container_of(args, typeof(*wpa), ia.ap.args);
1684 struct inode *inode = wpa->inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001685 struct fuse_inode *fi = get_fuse_inode(inode);
1686
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001687 mapping_set_error(inode->i_mapping, error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001688 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001689 while (wpa->next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001690 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001691 struct fuse_write_in *inarg = &wpa->ia.write.in;
1692 struct fuse_writepage_args *next = wpa->next;
1693
1694 wpa->next = next->next;
1695 next->next = NULL;
1696 next->ia.ff = fuse_file_get(wpa->ia.ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001697 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001698
1699 /*
1700 * Skip fuse_flush_writepages() to make it easy to crop requests
1701 * based on primary request size.
1702 *
1703 * 1st case (trivial): there are no concurrent activities using
1704 * fuse_set/release_nowrite. Then we're on safe side because
1705 * fuse_flush_writepages() would call fuse_send_writepage()
1706 * anyway.
1707 *
1708 * 2nd case: someone called fuse_set_nowrite and it is waiting
1709 * now for completion of all in-flight requests. This happens
1710 * rarely and no more than once per page, so this should be
1711 * okay.
1712 *
1713 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1714 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1715 * that fuse_set_nowrite returned implies that all in-flight
1716 * requests were completed along with all of their secondary
1717 * requests. Further primary requests are blocked by negative
1718 * writectr. Hence there cannot be any in-flight requests and
1719 * no invocations of fuse_writepage_end() while we're in
1720 * fuse_set_nowrite..fuse_release_nowrite section.
1721 */
1722 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001723 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001724 fi->writectr--;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001725 fuse_writepage_finish(fc, wpa);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001726 spin_unlock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001727 fuse_writepage_free(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001728}
1729
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001730static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1731 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001732{
Miklos Szeredi72523422013-10-01 16:44:52 +02001733 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001734
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001735 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001736 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001737 ff = list_entry(fi->write_files.next, struct fuse_file,
1738 write_entry);
1739 fuse_file_get(ff);
1740 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001741 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001742
1743 return ff;
1744}
1745
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001746static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1747 struct fuse_inode *fi)
1748{
1749 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1750 WARN_ON(!ff);
1751 return ff;
1752}
1753
1754int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1755{
1756 struct fuse_conn *fc = get_fuse_conn(inode);
1757 struct fuse_inode *fi = get_fuse_inode(inode);
1758 struct fuse_file *ff;
1759 int err;
1760
1761 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001762 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001763 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001764 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001765
1766 return err;
1767}
1768
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001769static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1770{
1771 struct fuse_writepage_args *wpa;
1772 struct fuse_args_pages *ap;
1773
1774 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1775 if (wpa) {
1776 ap = &wpa->ia.ap;
1777 ap->num_pages = 0;
1778 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1779 if (!ap->pages) {
1780 kfree(wpa);
1781 wpa = NULL;
1782 }
1783 }
1784 return wpa;
1785
1786}
1787
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001788static int fuse_writepage_locked(struct page *page)
1789{
1790 struct address_space *mapping = page->mapping;
1791 struct inode *inode = mapping->host;
1792 struct fuse_conn *fc = get_fuse_conn(inode);
1793 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001794 struct fuse_writepage_args *wpa;
1795 struct fuse_args_pages *ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001796 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001797 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001798
1799 set_page_writeback(page);
1800
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001801 wpa = fuse_writepage_args_alloc();
1802 if (!wpa)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001803 goto err;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001804 ap = &wpa->ia.ap;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001805
1806 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1807 if (!tmp_page)
1808 goto err_free;
1809
Miklos Szeredi72523422013-10-01 16:44:52 +02001810 error = -EIO;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001811 wpa->ia.ff = fuse_write_file_get(fc, fi);
1812 if (!wpa->ia.ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001813 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001814
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001815 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001816
1817 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001818 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1819 wpa->next = NULL;
1820 ap->args.in_pages = true;
1821 ap->num_pages = 1;
1822 ap->pages[0] = tmp_page;
1823 ap->descs[0].offset = 0;
1824 ap->descs[0].length = PAGE_SIZE;
1825 ap->args.end = fuse_writepage_end;
1826 wpa->inode = inode;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001827
Tejun Heo93f78d82015-05-22 17:13:27 -04001828 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001829 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001830
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001831 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001832 list_add(&wpa->writepages_entry, &fi->writepages);
1833 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001834 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001835 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001836
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001837 end_page_writeback(page);
1838
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001839 return 0;
1840
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001841err_nofile:
1842 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001843err_free:
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001844 kfree(wpa);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001845err:
Jeff Layton91839762017-05-25 06:57:50 -04001846 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001847 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001848 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001849}
1850
1851static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1852{
1853 int err;
1854
Miklos Szerediff17be02013-10-01 16:44:53 +02001855 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1856 /*
1857 * ->writepages() should be called for sync() and friends. We
1858 * should only get here on direct reclaim and then we are
1859 * allowed to skip a page which is already in flight
1860 */
1861 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1862
1863 redirty_page_for_writepage(wbc, page);
1864 return 0;
1865 }
1866
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001867 err = fuse_writepage_locked(page);
1868 unlock_page(page);
1869
1870 return err;
1871}
1872
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001873struct fuse_fill_wb_data {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001874 struct fuse_writepage_args *wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001875 struct fuse_file *ff;
1876 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001877 struct page **orig_pages;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001878 unsigned int max_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001879};
1880
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001881static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1882{
1883 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1884 struct fuse_conn *fc = get_fuse_conn(data->inode);
1885 struct page **pages;
1886 struct fuse_page_desc *descs;
1887 unsigned int npages = min_t(unsigned int,
1888 max_t(unsigned int, data->max_pages * 2,
1889 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1890 fc->max_pages);
1891 WARN_ON(npages <= data->max_pages);
1892
1893 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1894 if (!pages)
1895 return false;
1896
1897 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1898 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1899 kfree(ap->pages);
1900 ap->pages = pages;
1901 ap->descs = descs;
1902 data->max_pages = npages;
1903
1904 return true;
1905}
1906
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001907static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1908{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001909 struct fuse_writepage_args *wpa = data->wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001910 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001911 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001912 int num_pages = wpa->ia.ap.num_pages;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001913 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001914
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001915 wpa->ia.ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001916 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001917 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001918 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001919 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001920
1921 for (i = 0; i < num_pages; i++)
1922 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001923}
1924
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001925/*
1926 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001927 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1928 * one already added for a page at this offset. If there's none, then insert
1929 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001930 * copying the new page contents over to the old temporary page.
1931 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001932static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001933 struct page *page)
1934{
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001935 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1936 struct fuse_writepage_args *tmp;
1937 struct fuse_writepage_args *old_wpa;
1938 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001939
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001940 WARN_ON(new_ap->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001941
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001942 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001943 list_del(&new_wpa->writepages_entry);
1944 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1945 if (!old_wpa) {
1946 list_add(&new_wpa->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001947 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001948 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001949 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001950
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001951 new_ap->num_pages = 1;
1952 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001953 pgoff_t curr_index;
1954
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001955 WARN_ON(tmp->inode != new_wpa->inode);
1956 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001957 if (curr_index == page->index) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001958 WARN_ON(tmp->ia.ap.num_pages != 1);
1959 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001960 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001961 }
1962 }
1963
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001964 if (!tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001965 new_wpa->next = old_wpa->next;
1966 old_wpa->next = new_wpa;
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001967 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001968
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001969 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001970
1971 if (tmp) {
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001972 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001973
Tejun Heo93f78d82015-05-22 17:13:27 -04001974 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001975 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001976 wb_writeout_inc(&bdi->wb);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001977 fuse_writepage_free(new_wpa);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001978 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001979
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001980 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001981}
1982
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001983static int fuse_writepages_fill(struct page *page,
1984 struct writeback_control *wbc, void *_data)
1985{
1986 struct fuse_fill_wb_data *data = _data;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02001987 struct fuse_writepage_args *wpa = data->wpa;
1988 struct fuse_args_pages *ap = &wpa->ia.ap;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001989 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001990 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001991 struct fuse_conn *fc = get_fuse_conn(inode);
1992 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001993 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001994 int err;
1995
1996 if (!data->ff) {
1997 err = -EIO;
1998 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1999 if (!data->ff)
2000 goto out_unlock;
2001 }
2002
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002003 /*
2004 * Being under writeback is unlikely but possible. For example direct
2005 * read to an mmaped fuse file will set the page dirty twice; once when
2006 * the pages are faulted with get_user_pages(), and then after the read
2007 * completed.
2008 */
2009 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002010
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002011 if (wpa && ap->num_pages &&
2012 (is_writeback || ap->num_pages == fc->max_pages ||
2013 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2014 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002015 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002016 data->wpa = NULL;
2017 } else if (wpa && ap->num_pages == data->max_pages) {
2018 if (!fuse_pages_realloc(data)) {
Miklos Szeredie52a82502018-10-01 10:07:06 +02002019 fuse_writepages_send(data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002020 data->wpa = NULL;
Miklos Szeredie52a82502018-10-01 10:07:06 +02002021 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002022 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02002023
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002024 err = -ENOMEM;
2025 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2026 if (!tmp_page)
2027 goto out_unlock;
2028
2029 /*
2030 * The page must not be redirtied until the writeout is completed
2031 * (i.e. userspace has sent a reply to the write request). Otherwise
2032 * there could be more than one temporary page instance for each real
2033 * page.
2034 *
2035 * This is ensured by holding the page lock in page_mkwrite() while
2036 * checking fuse_page_is_writeback(). We already hold the page lock
2037 * since clear_page_dirty_for_io() and keep it held until we add the
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002038 * request to the fi->writepages list and increment ap->num_pages.
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002039 * After this fuse_page_is_writeback() will indicate that the page is
2040 * under writeback, so we can release the page lock.
2041 */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002042 if (data->wpa == NULL) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002043 struct fuse_inode *fi = get_fuse_inode(inode);
2044
2045 err = -ENOMEM;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002046 wpa = fuse_writepage_args_alloc();
2047 if (!wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002048 __free_page(tmp_page);
2049 goto out_unlock;
2050 }
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002051 data->max_pages = 1;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002052
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002053 ap = &wpa->ia.ap;
2054 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2055 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2056 wpa->next = NULL;
2057 ap->args.in_pages = true;
2058 ap->args.end = fuse_writepage_end;
2059 ap->num_pages = 0;
2060 wpa->inode = inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002061
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002062 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002063 list_add(&wpa->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002064 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002065
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002066 data->wpa = wpa;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002067 }
2068 set_page_writeback(page);
2069
2070 copy_highpage(tmp_page, page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002071 ap->pages[ap->num_pages] = tmp_page;
2072 ap->descs[ap->num_pages].offset = 0;
2073 ap->descs[ap->num_pages].length = PAGE_SIZE;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002074
Tejun Heo93f78d82015-05-22 17:13:27 -04002075 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07002076 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002077
2078 err = 0;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002079 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002080 end_page_writeback(page);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002081 data->wpa = NULL;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02002082 goto out_unlock;
2083 }
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002084 data->orig_pages[ap->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002085
2086 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002087 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002088 * fuse_page_is_writeback().
2089 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002090 spin_lock(&fi->lock);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002091 ap->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03002092 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002093
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002094out_unlock:
2095 unlock_page(page);
2096
2097 return err;
2098}
2099
2100static int fuse_writepages(struct address_space *mapping,
2101 struct writeback_control *wbc)
2102{
2103 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002104 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002105 struct fuse_fill_wb_data data;
2106 int err;
2107
2108 err = -EIO;
2109 if (is_bad_inode(inode))
2110 goto out;
2111
2112 data.inode = inode;
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002113 data.wpa = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002114 data.ff = NULL;
2115
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002116 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002117 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02002118 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002119 GFP_NOFS);
2120 if (!data.orig_pages)
2121 goto out;
2122
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002123 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002124 if (data.wpa) {
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002125 /* Ignore errors if we can write at least one page */
Miklos Szeredi33826eb2019-09-10 15:04:10 +02002126 WARN_ON(!data.wpa->ia.ap.num_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002127 fuse_writepages_send(&data);
2128 err = 0;
2129 }
2130 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002131 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002132
2133 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002134out:
2135 return err;
2136}
2137
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002138/*
2139 * It's worthy to make sure that space is reserved on disk for the write,
2140 * but how to implement it without killing performance need more thinking.
2141 */
2142static int fuse_write_begin(struct file *file, struct address_space *mapping,
2143 loff_t pos, unsigned len, unsigned flags,
2144 struct page **pagep, void **fsdata)
2145{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002146 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002147 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002148 struct page *page;
2149 loff_t fsize;
2150 int err = -ENOMEM;
2151
2152 WARN_ON(!fc->writeback_cache);
2153
2154 page = grab_cache_page_write_begin(mapping, index, flags);
2155 if (!page)
2156 goto error;
2157
2158 fuse_wait_on_page_writeback(mapping->host, page->index);
2159
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002160 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002161 goto success;
2162 /*
2163 * Check if the start this page comes after the end of file, in which
2164 * case the readpage can be optimized away.
2165 */
2166 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002167 if (fsize <= (pos & PAGE_MASK)) {
2168 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002169 if (off)
2170 zero_user_segment(page, 0, off);
2171 goto success;
2172 }
2173 err = fuse_do_readpage(file, page);
2174 if (err)
2175 goto cleanup;
2176success:
2177 *pagep = page;
2178 return 0;
2179
2180cleanup:
2181 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002182 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002183error:
2184 return err;
2185}
2186
2187static int fuse_write_end(struct file *file, struct address_space *mapping,
2188 loff_t pos, unsigned len, unsigned copied,
2189 struct page *page, void *fsdata)
2190{
2191 struct inode *inode = page->mapping->host;
2192
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002193 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2194 if (!copied)
2195 goto unlock;
2196
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002197 if (!PageUptodate(page)) {
2198 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002199 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002200 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002201 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002202 SetPageUptodate(page);
2203 }
2204
2205 fuse_write_update_size(inode, pos + copied);
2206 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002207
2208unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002209 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002210 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002211
2212 return copied;
2213}
2214
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002215static int fuse_launder_page(struct page *page)
2216{
2217 int err = 0;
2218 if (clear_page_dirty_for_io(page)) {
2219 struct inode *inode = page->mapping->host;
2220 err = fuse_writepage_locked(page);
2221 if (!err)
2222 fuse_wait_on_page_writeback(inode, page->index);
2223 }
2224 return err;
2225}
2226
2227/*
2228 * Write back dirty pages now, because there may not be any suitable
2229 * open files later
2230 */
2231static void fuse_vma_close(struct vm_area_struct *vma)
2232{
2233 filemap_write_and_wait(vma->vm_file->f_mapping);
2234}
2235
2236/*
2237 * Wait for writeback against this page to complete before allowing it
2238 * to be marked dirty again, and hence written back again, possibly
2239 * before the previous writepage completed.
2240 *
2241 * Block here, instead of in ->writepage(), so that the userspace fs
2242 * can only block processes actually operating on the filesystem.
2243 *
2244 * Otherwise unprivileged userspace fs would be able to block
2245 * unrelated:
2246 *
2247 * - page migration
2248 * - sync(2)
2249 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2250 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302251static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002252{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002253 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002254 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002255
Dave Jiang11bac802017-02-24 14:56:41 -08002256 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002257 lock_page(page);
2258 if (page->mapping != inode->i_mapping) {
2259 unlock_page(page);
2260 return VM_FAULT_NOPAGE;
2261 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002262
2263 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002264 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002265}
2266
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002267static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002268 .close = fuse_vma_close,
2269 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002270 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002271 .page_mkwrite = fuse_page_mkwrite,
2272};
2273
2274static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2275{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002276 struct fuse_file *ff = file->private_data;
2277
2278 if (ff->open_flags & FOPEN_DIRECT_IO) {
2279 /* Can't provide the coherency needed for MAP_SHARED */
2280 if (vma->vm_flags & VM_MAYSHARE)
2281 return -ENODEV;
2282
2283 invalidate_inode_pages2(file->f_mapping);
2284
2285 return generic_file_mmap(file, vma);
2286 }
2287
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002288 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2289 fuse_link_write_file(file);
2290
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002291 file_accessed(file);
2292 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002293 return 0;
2294}
2295
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002296static int convert_fuse_file_lock(struct fuse_conn *fc,
2297 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002298 struct file_lock *fl)
2299{
2300 switch (ffl->type) {
2301 case F_UNLCK:
2302 break;
2303
2304 case F_RDLCK:
2305 case F_WRLCK:
2306 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2307 ffl->end < ffl->start)
2308 return -EIO;
2309
2310 fl->fl_start = ffl->start;
2311 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002312
2313 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002314 * Convert pid into init's pid namespace. The locks API will
2315 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002316 */
2317 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002318 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002319 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002320 break;
2321
2322 default:
2323 return -EIO;
2324 }
2325 fl->fl_type = ffl->type;
2326 return 0;
2327}
2328
Miklos Szeredi70781872014-12-12 09:49:05 +01002329static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002330 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002331 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002332{
Al Viro6131ffa2013-02-27 16:59:05 -05002333 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002334 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002335 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002336
Miklos Szeredi70781872014-12-12 09:49:05 +01002337 memset(inarg, 0, sizeof(*inarg));
2338 inarg->fh = ff->fh;
2339 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2340 inarg->lk.start = fl->fl_start;
2341 inarg->lk.end = fl->fl_end;
2342 inarg->lk.type = fl->fl_type;
2343 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002344 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002345 inarg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002346 args->opcode = opcode;
2347 args->nodeid = get_node_id(inode);
2348 args->in_numargs = 1;
2349 args->in_args[0].size = sizeof(*inarg);
2350 args->in_args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002351}
2352
2353static int fuse_getlk(struct file *file, struct file_lock *fl)
2354{
Al Viro6131ffa2013-02-27 16:59:05 -05002355 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002356 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002357 FUSE_ARGS(args);
2358 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002359 struct fuse_lk_out outarg;
2360 int err;
2361
Miklos Szeredi70781872014-12-12 09:49:05 +01002362 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
Miklos Szeredid5b48542019-09-10 15:04:08 +02002363 args.out_numargs = 1;
2364 args.out_args[0].size = sizeof(outarg);
2365 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002366 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002367 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002368 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002369
2370 return err;
2371}
2372
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002373static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002374{
Al Viro6131ffa2013-02-27 16:59:05 -05002375 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002376 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002377 FUSE_ARGS(args);
2378 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002379 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002380 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2381 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002382 int err;
2383
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002384 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002385 /* NLM needs asynchronous locks, which we don't support yet */
2386 return -ENOLCK;
2387 }
2388
Miklos Szeredi71421252006-06-25 05:48:52 -07002389 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002390 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002391 return 0;
2392
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002393 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002394 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002395
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002396 /* locking is restartable */
2397 if (err == -EINTR)
2398 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002399
Miklos Szeredi71421252006-06-25 05:48:52 -07002400 return err;
2401}
2402
2403static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2404{
Al Viro6131ffa2013-02-27 16:59:05 -05002405 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002406 struct fuse_conn *fc = get_fuse_conn(inode);
2407 int err;
2408
Miklos Szeredi48e90762008-07-25 01:49:02 -07002409 if (cmd == F_CANCELLK) {
2410 err = 0;
2411 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002412 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002413 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002414 err = 0;
2415 } else
2416 err = fuse_getlk(file, fl);
2417 } else {
2418 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002419 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002420 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002421 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002422 }
2423 return err;
2424}
2425
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002426static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2427{
Al Viro6131ffa2013-02-27 16:59:05 -05002428 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002429 struct fuse_conn *fc = get_fuse_conn(inode);
2430 int err;
2431
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002432 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002433 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002434 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002435 struct fuse_file *ff = file->private_data;
2436
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002437 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002438 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002439 err = fuse_setlk(file, fl, 1);
2440 }
2441
2442 return err;
2443}
2444
Miklos Szeredib2d22722006-12-06 20:35:51 -08002445static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2446{
2447 struct inode *inode = mapping->host;
2448 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002449 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002450 struct fuse_bmap_in inarg;
2451 struct fuse_bmap_out outarg;
2452 int err;
2453
2454 if (!inode->i_sb->s_bdev || fc->no_bmap)
2455 return 0;
2456
Miklos Szeredib2d22722006-12-06 20:35:51 -08002457 memset(&inarg, 0, sizeof(inarg));
2458 inarg.block = block;
2459 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002460 args.opcode = FUSE_BMAP;
2461 args.nodeid = get_node_id(inode);
2462 args.in_numargs = 1;
2463 args.in_args[0].size = sizeof(inarg);
2464 args.in_args[0].value = &inarg;
2465 args.out_numargs = 1;
2466 args.out_args[0].size = sizeof(outarg);
2467 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002468 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002469 if (err == -ENOSYS)
2470 fc->no_bmap = 1;
2471
2472 return err ? 0 : outarg.block;
2473}
2474
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302475static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2476{
2477 struct inode *inode = file->f_mapping->host;
2478 struct fuse_conn *fc = get_fuse_conn(inode);
2479 struct fuse_file *ff = file->private_data;
2480 FUSE_ARGS(args);
2481 struct fuse_lseek_in inarg = {
2482 .fh = ff->fh,
2483 .offset = offset,
2484 .whence = whence
2485 };
2486 struct fuse_lseek_out outarg;
2487 int err;
2488
2489 if (fc->no_lseek)
2490 goto fallback;
2491
Miklos Szeredid5b48542019-09-10 15:04:08 +02002492 args.opcode = FUSE_LSEEK;
2493 args.nodeid = ff->nodeid;
2494 args.in_numargs = 1;
2495 args.in_args[0].size = sizeof(inarg);
2496 args.in_args[0].value = &inarg;
2497 args.out_numargs = 1;
2498 args.out_args[0].size = sizeof(outarg);
2499 args.out_args[0].value = &outarg;
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302500 err = fuse_simple_request(fc, &args);
2501 if (err) {
2502 if (err == -ENOSYS) {
2503 fc->no_lseek = 1;
2504 goto fallback;
2505 }
2506 return err;
2507 }
2508
2509 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2510
2511fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002512 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302513 if (!err)
2514 return generic_file_llseek(file, offset, whence);
2515 else
2516 return err;
2517}
2518
Andrew Morton965c8e52012-12-17 15:59:39 -08002519static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002520{
2521 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002522 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002523
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302524 switch (whence) {
2525 case SEEK_SET:
2526 case SEEK_CUR:
2527 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002528 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302529 break;
2530 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002531 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002532 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302533 if (!retval)
2534 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002535 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302536 break;
2537 case SEEK_HOLE:
2538 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002539 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302540 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002541 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302542 break;
2543 default:
2544 retval = -EINVAL;
2545 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002546
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002547 return retval;
2548}
2549
Tejun Heo59efec72008-11-26 12:03:55 +01002550/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002551 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2552 * ABI was defined to be 'struct iovec' which is different on 32bit
2553 * and 64bit. Fortunately we can determine which structure the server
2554 * used from the size of the reply.
2555 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002556static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2557 size_t transferred, unsigned count,
2558 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002559{
2560#ifdef CONFIG_COMPAT
2561 if (count * sizeof(struct compat_iovec) == transferred) {
2562 struct compat_iovec *ciov = src;
2563 unsigned i;
2564
2565 /*
2566 * With this interface a 32bit server cannot support
2567 * non-compat (i.e. ones coming from 64bit apps) ioctl
2568 * requests
2569 */
2570 if (!is_compat)
2571 return -EINVAL;
2572
2573 for (i = 0; i < count; i++) {
2574 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2575 dst[i].iov_len = ciov[i].iov_len;
2576 }
2577 return 0;
2578 }
2579#endif
2580
2581 if (count * sizeof(struct iovec) != transferred)
2582 return -EIO;
2583
2584 memcpy(dst, src, transferred);
2585 return 0;
2586}
2587
Miklos Szeredi75727772010-11-30 16:39:27 +01002588/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002589static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2590 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002591{
2592 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002593 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002594
Zach Brownfb6ccff2012-07-24 12:10:11 -07002595 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002596 if (iov->iov_len > (size_t) max)
2597 return -ENOMEM;
2598 max -= iov->iov_len;
2599 }
2600 return 0;
2601}
2602
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002603static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2604 void *src, size_t transferred, unsigned count,
2605 bool is_compat)
2606{
2607 unsigned i;
2608 struct fuse_ioctl_iovec *fiov = src;
2609
2610 if (fc->minor < 16) {
2611 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2612 count, is_compat);
2613 }
2614
2615 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2616 return -EIO;
2617
2618 for (i = 0; i < count; i++) {
2619 /* Did the server supply an inappropriate value? */
2620 if (fiov[i].base != (unsigned long) fiov[i].base ||
2621 fiov[i].len != (unsigned long) fiov[i].len)
2622 return -EIO;
2623
2624 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2625 dst[i].iov_len = (size_t) fiov[i].len;
2626
2627#ifdef CONFIG_COMPAT
2628 if (is_compat &&
2629 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2630 (compat_size_t) dst[i].iov_len != fiov[i].len))
2631 return -EIO;
2632#endif
2633 }
2634
2635 return 0;
2636}
2637
2638
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002639/*
Tejun Heo59efec72008-11-26 12:03:55 +01002640 * For ioctls, there is no generic way to determine how much memory
2641 * needs to be read and/or written. Furthermore, ioctls are allowed
2642 * to dereference the passed pointer, so the parameter requires deep
2643 * copying but FUSE has no idea whatsoever about what to copy in or
2644 * out.
2645 *
2646 * This is solved by allowing FUSE server to retry ioctl with
2647 * necessary in/out iovecs. Let's assume the ioctl implementation
2648 * needs to read in the following structure.
2649 *
2650 * struct a {
2651 * char *buf;
2652 * size_t buflen;
2653 * }
2654 *
2655 * On the first callout to FUSE server, inarg->in_size and
2656 * inarg->out_size will be NULL; then, the server completes the ioctl
2657 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2658 * the actual iov array to
2659 *
2660 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2661 *
2662 * which tells FUSE to copy in the requested area and retry the ioctl.
2663 * On the second round, the server has access to the structure and
2664 * from that it can tell what to look for next, so on the invocation,
2665 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2666 *
2667 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2668 * { .iov_base = a.buf, .iov_len = a.buflen } }
2669 *
2670 * FUSE will copy both struct a and the pointed buffer from the
2671 * process doing the ioctl and retry ioctl with both struct a and the
2672 * buffer.
2673 *
2674 * This time, FUSE server has everything it needs and completes ioctl
2675 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2676 *
2677 * Copying data out works the same way.
2678 *
2679 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2680 * automatically initializes in and out iovs by decoding @cmd with
2681 * _IOC_* macros and the server is not allowed to request RETRY. This
2682 * limits ioctl data transfers to well-formed ioctls and is the forced
2683 * behavior for all FUSE servers.
2684 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002685long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2686 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002687{
Tejun Heo59efec72008-11-26 12:03:55 +01002688 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002689 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002690 struct fuse_ioctl_in inarg = {
2691 .fh = ff->fh,
2692 .cmd = cmd,
2693 .arg = arg,
2694 .flags = flags
2695 };
2696 struct fuse_ioctl_out outarg;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002697 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002698 struct iovec *in_iov = NULL, *out_iov = NULL;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002699 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2700 size_t in_size, out_size, c;
2701 ssize_t transferred;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002702 int err, i;
2703 struct iov_iter ii;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002704 struct fuse_args_pages ap = {};
Tejun Heo59efec72008-11-26 12:03:55 +01002705
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002706#if BITS_PER_LONG == 32
2707 inarg.flags |= FUSE_IOCTL_32BIT;
2708#else
Ian Abbott6407f442019-04-24 15:14:11 +01002709 if (flags & FUSE_IOCTL_COMPAT) {
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002710 inarg.flags |= FUSE_IOCTL_32BIT;
Ian Abbott6407f442019-04-24 15:14:11 +01002711#ifdef CONFIG_X86_X32
2712 if (in_x32_syscall())
2713 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2714#endif
2715 }
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002716#endif
2717
Tejun Heo59efec72008-11-26 12:03:55 +01002718 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002719 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002720
Tejun Heo59efec72008-11-26 12:03:55 +01002721 err = -ENOMEM;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002722 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002723 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002724 if (!ap.pages || !iov_page)
Tejun Heo59efec72008-11-26 12:03:55 +01002725 goto out;
2726
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002727 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2728
Tejun Heo59efec72008-11-26 12:03:55 +01002729 /*
2730 * If restricted, initialize IO parameters as encoded in @cmd.
2731 * RETRY from server is not allowed.
2732 */
2733 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002734 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002735
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002736 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002737 iov->iov_len = _IOC_SIZE(cmd);
2738
2739 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2740 in_iov = iov;
2741 in_iovs = 1;
2742 }
2743
2744 if (_IOC_DIR(cmd) & _IOC_READ) {
2745 out_iov = iov;
2746 out_iovs = 1;
2747 }
2748 }
2749
2750 retry:
2751 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2752 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2753
2754 /*
2755 * Out data can be used either for actual out data or iovs,
2756 * make sure there always is at least one page.
2757 */
2758 out_size = max_t(size_t, out_size, PAGE_SIZE);
2759 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2760
2761 /* make sure there are enough buffer pages and init request with them */
2762 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002763 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002764 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002765 while (ap.num_pages < max_pages) {
2766 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2767 if (!ap.pages[ap.num_pages])
Tejun Heo59efec72008-11-26 12:03:55 +01002768 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002769 ap.num_pages++;
Tejun Heo59efec72008-11-26 12:03:55 +01002770 }
2771
Tejun Heo59efec72008-11-26 12:03:55 +01002772
2773 /* okay, let's send it to the client */
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002774 ap.args.opcode = FUSE_IOCTL;
2775 ap.args.nodeid = ff->nodeid;
2776 ap.args.in_numargs = 1;
2777 ap.args.in_args[0].size = sizeof(inarg);
2778 ap.args.in_args[0].value = &inarg;
Tejun Heo59efec72008-11-26 12:03:55 +01002779 if (in_size) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002780 ap.args.in_numargs++;
2781 ap.args.in_args[1].size = in_size;
2782 ap.args.in_pages = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002783
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002784 err = -EFAULT;
2785 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002786 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2787 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002788 if (c != PAGE_SIZE && iov_iter_count(&ii))
2789 goto out;
2790 }
Tejun Heo59efec72008-11-26 12:03:55 +01002791 }
2792
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002793 ap.args.out_numargs = 2;
2794 ap.args.out_args[0].size = sizeof(outarg);
2795 ap.args.out_args[0].value = &outarg;
2796 ap.args.out_args[1].size = out_size;
2797 ap.args.out_pages = true;
2798 ap.args.out_argvar = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002799
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002800 transferred = fuse_simple_request(fc, &ap.args);
2801 err = transferred;
2802 if (transferred < 0)
Tejun Heo59efec72008-11-26 12:03:55 +01002803 goto out;
2804
2805 /* did it ask for retry? */
2806 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002807 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002808
2809 /* no retry if in restricted mode */
2810 err = -EIO;
2811 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2812 goto out;
2813
2814 in_iovs = outarg.in_iovs;
2815 out_iovs = outarg.out_iovs;
2816
2817 /*
2818 * Make sure things are in boundary, separate checks
2819 * are to protect against overflow.
2820 */
2821 err = -ENOMEM;
2822 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2823 out_iovs > FUSE_IOCTL_MAX_IOV ||
2824 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2825 goto out;
2826
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002827 vaddr = kmap_atomic(ap.pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002828 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002829 transferred, in_iovs + out_iovs,
2830 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002831 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002832 if (err)
2833 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002834
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002835 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002836 out_iov = in_iov + in_iovs;
2837
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002838 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002839 if (err)
2840 goto out;
2841
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002842 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002843 if (err)
2844 goto out;
2845
Tejun Heo59efec72008-11-26 12:03:55 +01002846 goto retry;
2847 }
2848
2849 err = -EIO;
2850 if (transferred > inarg.out_size)
2851 goto out;
2852
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002853 err = -EFAULT;
2854 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002855 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2856 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002857 if (c != PAGE_SIZE && iov_iter_count(&ii))
2858 goto out;
2859 }
2860 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002861 out:
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002862 free_page((unsigned long) iov_page);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002863 while (ap.num_pages)
2864 __free_page(ap.pages[--ap.num_pages]);
2865 kfree(ap.pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002866
2867 return err ? err : outarg.result;
2868}
Tejun Heo08cbf542009-04-14 10:54:53 +09002869EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002870
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002871long fuse_ioctl_common(struct file *file, unsigned int cmd,
2872 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002873{
Al Viro6131ffa2013-02-27 16:59:05 -05002874 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002875 struct fuse_conn *fc = get_fuse_conn(inode);
2876
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002877 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002878 return -EACCES;
2879
2880 if (is_bad_inode(inode))
2881 return -EIO;
2882
2883 return fuse_do_ioctl(file, cmd, arg, flags);
2884}
2885
Tejun Heo59efec72008-11-26 12:03:55 +01002886static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2887 unsigned long arg)
2888{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002889 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002890}
2891
2892static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2893 unsigned long arg)
2894{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002895 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002896}
2897
Tejun Heo95668a62008-11-26 12:03:55 +01002898/*
2899 * All files which have been polled are linked to RB tree
2900 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2901 * find the matching one.
2902 */
2903static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2904 struct rb_node **parent_out)
2905{
2906 struct rb_node **link = &fc->polled_files.rb_node;
2907 struct rb_node *last = NULL;
2908
2909 while (*link) {
2910 struct fuse_file *ff;
2911
2912 last = *link;
2913 ff = rb_entry(last, struct fuse_file, polled_node);
2914
2915 if (kh < ff->kh)
2916 link = &last->rb_left;
2917 else if (kh > ff->kh)
2918 link = &last->rb_right;
2919 else
2920 return link;
2921 }
2922
2923 if (parent_out)
2924 *parent_out = last;
2925 return link;
2926}
2927
2928/*
2929 * The file is about to be polled. Make sure it's on the polled_files
2930 * RB tree. Note that files once added to the polled_files tree are
2931 * not removed before the file is released. This is because a file
2932 * polled once is likely to be polled again.
2933 */
2934static void fuse_register_polled_file(struct fuse_conn *fc,
2935 struct fuse_file *ff)
2936{
2937 spin_lock(&fc->lock);
2938 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002939 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002940
2941 link = fuse_find_polled_node(fc, ff->kh, &parent);
2942 BUG_ON(*link);
2943 rb_link_node(&ff->polled_node, parent, link);
2944 rb_insert_color(&ff->polled_node, &fc->polled_files);
2945 }
2946 spin_unlock(&fc->lock);
2947}
2948
Al Viro076ccb72017-07-03 01:02:18 -04002949__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002950{
Tejun Heo95668a62008-11-26 12:03:55 +01002951 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002952 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002953 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2954 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002955 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002956 int err;
2957
2958 if (fc->no_poll)
2959 return DEFAULT_POLLMASK;
2960
2961 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002962 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002963
2964 /*
2965 * Ask for notification iff there's someone waiting for it.
2966 * The client may ignore the flag and always notify.
2967 */
2968 if (waitqueue_active(&ff->poll_wait)) {
2969 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2970 fuse_register_polled_file(fc, ff);
2971 }
2972
Miklos Szeredid5b48542019-09-10 15:04:08 +02002973 args.opcode = FUSE_POLL;
2974 args.nodeid = ff->nodeid;
2975 args.in_numargs = 1;
2976 args.in_args[0].size = sizeof(inarg);
2977 args.in_args[0].value = &inarg;
2978 args.out_numargs = 1;
2979 args.out_args[0].size = sizeof(outarg);
2980 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002981 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002982
2983 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002984 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002985 if (err == -ENOSYS) {
2986 fc->no_poll = 1;
2987 return DEFAULT_POLLMASK;
2988 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002989 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002990}
Tejun Heo08cbf542009-04-14 10:54:53 +09002991EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002992
2993/*
2994 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2995 * wakes up the poll waiters.
2996 */
2997int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2998 struct fuse_notify_poll_wakeup_out *outarg)
2999{
3000 u64 kh = outarg->kh;
3001 struct rb_node **link;
3002
3003 spin_lock(&fc->lock);
3004
3005 link = fuse_find_polled_node(fc, kh, NULL);
3006 if (*link) {
3007 struct fuse_file *ff;
3008
3009 ff = rb_entry(*link, struct fuse_file, polled_node);
3010 wake_up_interruptible_sync(&ff->poll_wait);
3011 }
3012
3013 spin_unlock(&fc->lock);
3014 return 0;
3015}
3016
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003017static void fuse_do_truncate(struct file *file)
3018{
3019 struct inode *inode = file->f_mapping->host;
3020 struct iattr attr;
3021
3022 attr.ia_valid = ATTR_SIZE;
3023 attr.ia_size = i_size_read(inode);
3024
3025 attr.ia_file = file;
3026 attr.ia_valid |= ATTR_FILE;
3027
Jan Kara62490332016-05-26 17:12:41 +02003028 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003029}
3030
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003031static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003032{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003033 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003034}
3035
Anand Avati4273b792012-02-17 12:46:25 -05003036static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003037fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05003038{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003039 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05003040 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003041 struct file *file = iocb->ki_filp;
3042 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003043 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05003044 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003045 struct inode *inode;
3046 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05003047 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003048 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003049 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05003050
Anand Avati4273b792012-02-17 12:46:25 -05003051 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003052 inode = file->f_mapping->host;
3053 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05003054
Omar Sandoval6f673762015-03-16 04:33:52 -07003055 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00003056 return 0;
3057
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003058 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07003059 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003060 if (offset >= i_size)
3061 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03003062 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04003063 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04003064 }
3065
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003066 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003067 if (!io)
3068 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003069 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06003070 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003071 io->reqs = 1;
3072 io->bytes = -1;
3073 io->size = 0;
3074 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07003075 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003076 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003077 /*
3078 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02003079 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003080 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04003081 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003082 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303083 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003084
3085 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303086 * We cannot asynchronously extend the size of a file.
3087 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003088 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303089 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3090 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05003091
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05303092 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06003093 /*
3094 * Additional reference to keep io around after
3095 * calling fuse_aio_complete()
3096 */
3097 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003098 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06003099 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003100
Omar Sandoval6f673762015-03-16 04:33:52 -07003101 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04003102 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04003103 fuse_invalidate_attr(inode);
3104 } else {
Al Virod22a9432014-03-16 15:50:47 -04003105 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04003106 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04003107
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003108 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01003109 bool blocking = io->blocking;
3110
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003111 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3112
3113 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01003114 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003115 return -EIOCBQUEUED;
3116
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003117 wait_for_completion(&wait);
3118 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003119 }
3120
Seth Forshee744742d2016-03-11 10:35:34 -06003121 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003122
Omar Sandoval6f673762015-03-16 04:33:52 -07003123 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003124 if (ret > 0)
3125 fuse_write_update_size(inode, pos);
3126 else if (ret < 0 && offset + count > i_size)
3127 fuse_do_truncate(file);
3128 }
Anand Avati4273b792012-02-17 12:46:25 -05003129
3130 return ret;
3131}
3132
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003133static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3134{
3135 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3136
3137 if (!err)
3138 fuse_sync_writes(inode);
3139
3140 return err;
3141}
3142
Miklos Szeredicdadb112012-11-10 16:55:56 +01003143static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3144 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003145{
3146 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003147 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003148 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003149 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01003150 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003151 struct fuse_fallocate_in inarg = {
3152 .fh = ff->fh,
3153 .offset = offset,
3154 .length = length,
3155 .mode = mode
3156 };
3157 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003158 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3159 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003160
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003161 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3162 return -EOPNOTSUPP;
3163
Miklos Szeredi519c6042012-04-26 10:56:36 +02003164 if (fc->no_fallocate)
3165 return -EOPNOTSUPP;
3166
Maxim Patlasov14c14412013-06-13 12:16:39 +04003167 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003168 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003169 if (mode & FALLOC_FL_PUNCH_HOLE) {
3170 loff_t endbyte = offset + length - 1;
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003171
3172 err = fuse_writeback_range(inode, offset, endbyte);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003173 if (err)
3174 goto out;
Maxim Patlasovbde52782013-09-13 19:19:54 +04003175 }
Brian Foster3634a632013-05-17 09:30:32 -04003176 }
3177
Liu Bo0cbade02019-04-18 04:04:41 +08003178 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3179 offset + length > i_size_read(inode)) {
3180 err = inode_newsize_ok(inode, offset + length);
3181 if (err)
Miklos Szeredi35d6fcb2019-05-27 11:42:07 +02003182 goto out;
Liu Bo0cbade02019-04-18 04:04:41 +08003183 }
3184
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003185 if (!(mode & FALLOC_FL_KEEP_SIZE))
3186 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3187
Miklos Szeredid5b48542019-09-10 15:04:08 +02003188 args.opcode = FUSE_FALLOCATE;
3189 args.nodeid = ff->nodeid;
3190 args.in_numargs = 1;
3191 args.in_args[0].size = sizeof(inarg);
3192 args.in_args[0].value = &inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01003193 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003194 if (err == -ENOSYS) {
3195 fc->no_fallocate = 1;
3196 err = -EOPNOTSUPP;
3197 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003198 if (err)
3199 goto out;
3200
3201 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003202 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3203 bool changed = fuse_write_update_size(inode, offset + length);
3204
Miklos Szeredi93d22692014-04-28 14:19:22 +02003205 if (changed && fc->writeback_cache)
3206 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003207 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003208
3209 if (mode & FALLOC_FL_PUNCH_HOLE)
3210 truncate_pagecache_range(inode, offset, offset + length - 1);
3211
3212 fuse_invalidate_attr(inode);
3213
Brian Foster3634a632013-05-17 09:30:32 -04003214out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003215 if (!(mode & FALLOC_FL_KEEP_SIZE))
3216 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3217
Maxim Patlasovbde52782013-09-13 19:19:54 +04003218 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003219 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003220
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003221 return err;
3222}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003223
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003224static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3225 struct file *file_out, loff_t pos_out,
3226 size_t len, unsigned int flags)
Niels de Vos88bc7d52018-08-21 14:36:31 +02003227{
3228 struct fuse_file *ff_in = file_in->private_data;
3229 struct fuse_file *ff_out = file_out->private_data;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003230 struct inode *inode_in = file_inode(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003231 struct inode *inode_out = file_inode(file_out);
3232 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3233 struct fuse_conn *fc = ff_in->fc;
3234 FUSE_ARGS(args);
3235 struct fuse_copy_file_range_in inarg = {
3236 .fh_in = ff_in->fh,
3237 .off_in = pos_in,
3238 .nodeid_out = ff_out->nodeid,
3239 .fh_out = ff_out->fh,
3240 .off_out = pos_out,
3241 .len = len,
3242 .flags = flags
3243 };
3244 struct fuse_write_out outarg;
3245 ssize_t err;
3246 /* mark unstable when write-back is not used, and file_out gets
3247 * extended */
3248 bool is_unstable = (!fc->writeback_cache) &&
3249 ((pos_out + len) > inode_out->i_size);
3250
3251 if (fc->no_copy_file_range)
3252 return -EOPNOTSUPP;
3253
Amir Goldstein5dae2222019-06-05 08:04:50 -07003254 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3255 return -EXDEV;
3256
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003257 if (fc->writeback_cache) {
3258 inode_lock(inode_in);
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003259 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003260 inode_unlock(inode_in);
3261 if (err)
3262 return err;
3263 }
3264
Niels de Vos88bc7d52018-08-21 14:36:31 +02003265 inode_lock(inode_out);
3266
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003267 err = file_modified(file_out);
3268 if (err)
3269 goto out;
3270
Niels de Vos88bc7d52018-08-21 14:36:31 +02003271 if (fc->writeback_cache) {
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003272 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003273 if (err)
3274 goto out;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003275 }
3276
3277 if (is_unstable)
3278 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3279
Miklos Szeredid5b48542019-09-10 15:04:08 +02003280 args.opcode = FUSE_COPY_FILE_RANGE;
3281 args.nodeid = ff_in->nodeid;
3282 args.in_numargs = 1;
3283 args.in_args[0].size = sizeof(inarg);
3284 args.in_args[0].value = &inarg;
3285 args.out_numargs = 1;
3286 args.out_args[0].size = sizeof(outarg);
3287 args.out_args[0].value = &outarg;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003288 err = fuse_simple_request(fc, &args);
3289 if (err == -ENOSYS) {
3290 fc->no_copy_file_range = 1;
3291 err = -EOPNOTSUPP;
3292 }
3293 if (err)
3294 goto out;
3295
3296 if (fc->writeback_cache) {
3297 fuse_write_update_size(inode_out, pos_out + outarg.size);
3298 file_update_time(file_out);
3299 }
3300
3301 fuse_invalidate_attr(inode_out);
3302
3303 err = outarg.size;
3304out:
3305 if (is_unstable)
3306 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3307
3308 inode_unlock(inode_out);
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003309 file_accessed(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003310
3311 return err;
3312}
3313
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003314static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3315 struct file *dst_file, loff_t dst_off,
3316 size_t len, unsigned int flags)
3317{
3318 ssize_t ret;
3319
3320 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3321 len, flags);
3322
Amir Goldstein5dae2222019-06-05 08:04:50 -07003323 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003324 ret = generic_copy_file_range(src_file, src_off, dst_file,
3325 dst_off, len, flags);
3326 return ret;
3327}
3328
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003329static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003330 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003331 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003332 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003333 .mmap = fuse_file_mmap,
3334 .open = fuse_open,
3335 .flush = fuse_flush,
3336 .release = fuse_release,
3337 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003338 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003339 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003340 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003341 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003342 .unlocked_ioctl = fuse_file_ioctl,
3343 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003344 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003345 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003346 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003347};
3348
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003349static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003350 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003351 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003352 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003353 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003354 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003355 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003356 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003357 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003358 .write_begin = fuse_write_begin,
3359 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003360};
3361
3362void fuse_init_file_inode(struct inode *inode)
3363{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003364 struct fuse_inode *fi = get_fuse_inode(inode);
3365
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003366 inode->i_fop = &fuse_file_operations;
3367 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003368
3369 INIT_LIST_HEAD(&fi->write_files);
3370 INIT_LIST_HEAD(&fi->queued_writes);
3371 fi->writectr = 0;
3372 init_waitqueue_head(&fi->page_waitq);
3373 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003374}