blob: 8e67add0f37f7369126b8790358eb16bfa2b7b1e [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 Szeredi4c4f03f2019-09-10 15:04:09 +020022struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
24{
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
Tejun Heoacf99432008-11-26 12:03:55 +010056struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080057{
58 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090059
Mateusz Jurczyk68227c02017-06-07 12:26:49 +020060 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090061 if (unlikely(!ff))
62 return NULL;
63
Miklos Szeredida5e4712009-04-28 16:56:36 +020064 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040065 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090066 if (unlikely(!ff->reserved_req)) {
67 kfree(ff);
68 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080069 }
Tejun Heo6b2db282009-04-14 10:54:49 +090070
71 INIT_LIST_HEAD(&ff->write_entry);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020072 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020073 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090074 RB_CLEAR_NODE(&ff->polled_node);
75 init_waitqueue_head(&ff->poll_wait);
76
Miklos Szeredi75126f52019-01-24 10:40:17 +010077 ff->kh = atomic64_inc_return(&fc->khctr);
Tejun Heo6b2db282009-04-14 10:54:49 +090078
Miklos Szeredifd72faa2005-11-07 00:59:51 -080079 return ff;
80}
81
82void fuse_file_free(struct fuse_file *ff)
83{
Miklos Szeredi33649c92006-06-25 05:48:52 -070084 fuse_request_free(ff->reserved_req);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020085 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080086 kfree(ff);
87}
88
Miklos Szeredi267d8442017-02-22 20:08:25 +010089static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070090{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020091 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070092 return ff;
93}
94
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010095static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
96{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010097 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010098}
99
Chad Austin2e64ff12018-12-10 10:54:52 -0800100static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700101{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200102 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700103 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200104
Chad Austind9a9ea92019-01-07 16:53:17 -0800105 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100106 /*
107 * Drop the release request when client does not
108 * implement 'open'
109 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200110 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100111 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100112 fuse_put_request(ff->fc, req);
113 } else if (sync) {
Miklos Szeredi2e38bea2017-02-22 20:08:25 +0100114 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200115 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100116 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100117 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100118 fuse_put_request(ff->fc, req);
119 } else {
120 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200121 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100122 fuse_request_send_background(ff->fc, req);
123 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700124 kfree(ff);
125 }
126}
127
Tejun Heo08cbf542009-04-14 10:54:53 +0900128int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
129 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200130{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200131 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200132 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
133
134 ff = fuse_file_alloc(fc);
135 if (!ff)
136 return -ENOMEM;
137
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100138 ff->fh = 0;
Chad Austinfabf7e02019-01-28 16:34:34 -0800139 /* Default for no-open */
140 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
Chad Austind9a9ea92019-01-07 16:53:17 -0800141 if (isdir ? !fc->no_opendir : !fc->no_open) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100142 struct fuse_open_out outarg;
143 int err;
144
145 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
146 if (!err) {
147 ff->fh = outarg.fh;
148 ff->open_flags = outarg.open_flags;
149
Chad Austind9a9ea92019-01-07 16:53:17 -0800150 } else if (err != -ENOSYS) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100151 fuse_file_free(ff);
152 return err;
153 } else {
Chad Austind9a9ea92019-01-07 16:53:17 -0800154 if (isdir)
155 fc->no_opendir = 1;
156 else
157 fc->no_open = 1;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100158 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200159 }
160
161 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200163
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200164 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100165 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200166
167 return 0;
168}
Tejun Heo08cbf542009-04-14 10:54:53 +0900169EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200170
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400171static void fuse_link_write_file(struct file *file)
172{
173 struct inode *inode = file_inode(file);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400174 struct fuse_inode *fi = get_fuse_inode(inode);
175 struct fuse_file *ff = file->private_data;
176 /*
177 * file may be written through mmap, so chain it onto the
178 * inodes's write_file list
179 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300180 spin_lock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400181 if (list_empty(&ff->write_entry))
182 list_add(&ff->write_entry, &fi->write_files);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300183 spin_unlock(&fi->lock);
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400184}
185
Miklos Szeredic7b71432009-04-28 16:56:37 +0200186void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800187{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200188 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800189 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200190
Miklos Szeredic7b71432009-04-28 16:56:37 +0200191 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700192 invalidate_inode_pages2(inode->i_mapping);
Kirill Smelkovbbd84f32019-04-24 07:13:57 +0000193 if (ff->open_flags & FOPEN_STREAM)
194 stream_open(inode, file);
195 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200196 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800197 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
198 struct fuse_inode *fi = get_fuse_inode(inode);
199
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300200 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300201 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Ken Sumralla0822c52010-11-24 12:57:00 -0800202 i_size_write(inode, 0);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300203 spin_unlock(&fi->lock);
Ken Sumralla0822c52010-11-24 12:57:00 -0800204 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200205 if (fc->writeback_cache)
206 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800207 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400208 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
209 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800210}
211
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200212int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800213{
Tejun Heoacf99432008-11-26 12:03:55 +0100214 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700215 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200216 bool lock_inode = (file->f_flags & O_TRUNC) &&
217 fc->atomic_o_trunc &&
218 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219
220 err = generic_file_open(inode, file);
221 if (err)
222 return err;
223
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200224 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500225 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200226
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200227 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700228
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200229 if (!err)
230 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200231
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200232 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500233 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200234
235 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700236}
237
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300238static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
239 int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800240{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200241 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700242 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800243 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300245 /* Inode is NULL on error path of fuse_create_open() */
246 if (likely(fi)) {
247 spin_lock(&fi->lock);
248 list_del(&ff->write_entry);
249 spin_unlock(&fi->lock);
250 }
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200251 spin_lock(&fc->lock);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200252 if (!RB_EMPTY_NODE(&ff->polled_node))
253 rb_erase(&ff->polled_node, &fc->polled_files);
254 spin_unlock(&fc->lock);
255
Bryan Green357ccf22011-03-01 16:43:52 -0800256 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200257
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700258 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800259 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700260 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200261 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700262 req->in.numargs = 1;
263 req->in.args[0].size = sizeof(struct fuse_release_in);
264 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800265}
266
Chad Austin2e64ff12018-12-10 10:54:52 -0800267void fuse_release_common(struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800268{
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300269 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100270 struct fuse_file *ff = file->private_data;
271 struct fuse_req *req = ff->reserved_req;
Chad Austin2e64ff12018-12-10 10:54:52 -0800272 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700273
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300274 fuse_prepare_release(fi, ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100275
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200276 if (ff->flock) {
277 struct fuse_release_in *inarg = &req->misc.release.in;
278 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
279 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
280 (fl_owner_t) file);
281 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100282 /* Hold inode until release is finished */
283 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700284
Tejun Heo6b2db282009-04-14 10:54:49 +0900285 /*
286 * Normally this will send the RELEASE request, however if
287 * some asynchronous READ or WRITE requests are outstanding,
288 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100289 *
290 * Make the release synchronous if this is a fuseblk mount,
291 * synchronous RELEASE is allowed (and desirable) in this case
292 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900293 */
Miklos Szeredi1ccd1ea2019-09-10 15:04:09 +0200294 fuse_file_put(ff, ff->fc->destroy, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700295}
296
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700297static int fuse_open(struct inode *inode, struct file *file)
298{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200299 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700300}
301
302static int fuse_release(struct inode *inode, struct file *file)
303{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400304 struct fuse_conn *fc = get_fuse_conn(inode);
305
306 /* see fuse_vma_close() for !writeback_cache case */
307 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200308 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400309
Chad Austin2e64ff12018-12-10 10:54:52 -0800310 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200311
312 /* return value is ignored by VFS */
313 return 0;
314}
315
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300316void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200317{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200318 WARN_ON(refcount_read(&ff->count) > 1);
Kirill Tkhaiebf84d02018-11-09 13:33:11 +0300319 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100320 /*
321 * iput(NULL) is a no-op and since the refcount is 1 and everything's
322 * synchronous, we are fine with not doing igrab() here"
323 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800324 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700325}
Tejun Heo08cbf542009-04-14 10:54:53 +0900326EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700327
Miklos Szeredi71421252006-06-25 05:48:52 -0700328/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700329 * Scramble the ID space with XTEA, so that the value of the files_struct
330 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700331 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700332u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700333{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700334 u32 *k = fc->scramble_key;
335 u64 v = (unsigned long) id;
336 u32 v0 = v;
337 u32 v1 = v >> 32;
338 u32 sum = 0;
339 int i;
340
341 for (i = 0; i < 32; i++) {
342 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
343 sum += 0x9E3779B9;
344 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
345 }
346
347 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700348}
349
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100350static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
351 pgoff_t idx_from, pgoff_t idx_to)
352{
353 struct fuse_req *req;
354
355 list_for_each_entry(req, &fi->writepages, writepages_entry) {
356 pgoff_t curr_index;
357
358 WARN_ON(get_fuse_inode(req->inode) != fi);
359 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
360 if (idx_from < curr_index + req->num_pages &&
361 curr_index <= idx_to) {
362 return req;
363 }
364 }
365 return NULL;
366}
367
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700368/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400369 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700370 *
371 * This is currently done by walking the list of writepage requests
372 * for the inode, which can be pretty inefficient.
373 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400374static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
375 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700376{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700377 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100378 bool found;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700379
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300380 spin_lock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +0100381 found = fuse_find_writeback(fi, idx_from, idx_to);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300382 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700383
384 return found;
385}
386
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400387static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
388{
389 return fuse_range_is_writeback(inode, index, index);
390}
391
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700392/*
393 * Wait for page writeback to be completed.
394 *
395 * Since fuse doesn't rely on the VM writeback tracking, this has to
396 * use some other means.
397 */
Maxim Patlasov17b2cbe2019-07-22 10:17:17 +0300398static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700399{
400 struct fuse_inode *fi = get_fuse_inode(inode);
401
402 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700403}
404
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400405/*
406 * Wait for all pending writepages on the inode to finish.
407 *
408 * This is currently done by blocking further writes with FUSE_NOWRITE
409 * and waiting for all sent writes to complete.
410 *
411 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
412 * could conflict with truncation.
413 */
414static void fuse_sync_writes(struct inode *inode)
415{
416 fuse_set_nowrite(inode);
417 fuse_release_nowrite(inode);
418}
419
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700420static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421{
Al Viro6131ffa2013-02-27 16:59:05 -0500422 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 struct fuse_conn *fc = get_fuse_conn(inode);
424 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 struct fuse_flush_in inarg;
Miklos Szeredic500eba2019-09-10 15:04:08 +0200426 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 int err;
428
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800429 if (is_bad_inode(inode))
430 return -EIO;
431
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700432 if (fc->no_flush)
433 return 0;
434
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200435 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400436 if (err)
437 return err;
438
Al Viro59551022016-01-22 15:40:57 -0500439 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400440 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500441 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400442
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200443 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700444 if (err)
445 return err;
446
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 memset(&inarg, 0, sizeof(inarg));
448 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700449 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200450 args.opcode = FUSE_FLUSH;
451 args.nodeid = get_node_id(inode);
452 args.in_numargs = 1;
453 args.in_args[0].size = sizeof(inarg);
454 args.in_args[0].value = &inarg;
455 args.force = true;
456
457 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 if (err == -ENOSYS) {
459 fc->no_flush = 1;
460 err = 0;
461 }
462 return err;
463}
464
Josef Bacik02c24a82011-07-16 20:44:56 -0400465int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100466 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200468 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700469 struct fuse_conn *fc = get_fuse_conn(inode);
470 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100471 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100473
474 memset(&inarg, 0, sizeof(inarg));
475 inarg.fh = ff->fh;
Alan Somers154603f2019-04-19 15:42:44 -0600476 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200477 args.opcode = opcode;
478 args.nodeid = get_node_id(inode);
479 args.in_numargs = 1;
480 args.in_args[0].size = sizeof(inarg);
481 args.in_args[0].value = &inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100482 return fuse_simple_request(fc, &args);
483}
484
485static int fuse_fsync(struct file *file, loff_t start, loff_t end,
486 int datasync)
487{
488 struct inode *inode = file->f_mapping->host;
489 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700490 int err;
491
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800492 if (is_bad_inode(inode))
493 return -EIO;
494
Al Viro59551022016-01-22 15:40:57 -0500495 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400496
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700497 /*
498 * Start writeback against all dirty pages of the inode, then
499 * wait for all outstanding writes, before sending the FSYNC
500 * request.
501 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400502 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700503 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400504 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700505
506 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700507
508 /*
509 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400510 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700511 * We have to do this directly after fuse_sync_writes()
512 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400513 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700514 if (err)
515 goto out;
516
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200517 err = sync_inode_metadata(inode, 1);
518 if (err)
519 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700520
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100521 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200522 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400523
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100524 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100526 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700527 err = 0;
528 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400529out:
Al Viro59551022016-01-22 15:40:57 -0500530 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100532 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700533}
534
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200535void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
536 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700537{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700538 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800539 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700540
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800541 inarg->fh = ff->fh;
542 inarg->offset = pos;
543 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800544 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800545 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200546 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700547 req->in.numargs = 1;
548 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800549 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700550 req->out.argvar = 1;
551 req->out.numargs = 1;
552 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700553}
554
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200555static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400556{
557 unsigned i;
558
559 for (i = 0; i < req->num_pages; i++) {
560 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200561 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400562 set_page_dirty_lock(page);
563 put_page(page);
564 }
565}
566
Seth Forshee744742d2016-03-11 10:35:34 -0600567static void fuse_io_release(struct kref *kref)
568{
569 kfree(container_of(kref, struct fuse_io_priv, refcnt));
570}
571
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100572static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
573{
574 if (io->err)
575 return io->err;
576
577 if (io->bytes >= 0 && io->write)
578 return -EIO;
579
580 return io->bytes < 0 ? io->size : io->bytes;
581}
582
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400583/**
584 * In case of short read, the caller sets 'pos' to the position of
585 * actual end of fuse request in IO request. Otherwise, if bytes_requested
586 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
587 *
588 * An example:
589 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
590 * both submitted asynchronously. The first of them was ACKed by userspace as
591 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
592 * second request was ACKed as short, e.g. only 1K was read, resulting in
593 * pos == 33K.
594 *
595 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
596 * will be equal to the length of the longest contiguous fragment of
597 * transferred data starting from the beginning of IO request.
598 */
599static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
600{
601 int left;
602
603 spin_lock(&io->lock);
604 if (err)
605 io->err = io->err ? : err;
606 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
607 io->bytes = pos;
608
609 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530610 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100611 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400612 spin_unlock(&io->lock);
613
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530614 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100615 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400616
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100617 if (res >= 0) {
618 struct inode *inode = file_inode(io->iocb->ki_filp);
619 struct fuse_conn *fc = get_fuse_conn(inode);
620 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400621
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300622 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +0300623 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300624 spin_unlock(&fi->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400625 }
626
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100627 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400628 }
Seth Forshee744742d2016-03-11 10:35:34 -0600629
630 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400631}
632
633static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
634{
635 struct fuse_io_priv *io = req->io;
636 ssize_t pos = -1;
637
Ashish Samant61c12b42017-07-12 19:26:58 -0700638 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400639
640 if (io->write) {
641 if (req->misc.write.in.size != req->misc.write.out.size)
642 pos = req->misc.write.in.offset - io->offset +
643 req->misc.write.out.size;
644 } else {
645 if (req->misc.read.in.size != req->out.args[0].size)
646 pos = req->misc.read.in.offset - io->offset +
647 req->out.args[0].size;
648 }
649
650 fuse_aio_complete(io, req->out.h.error, pos);
651}
652
653static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
654 size_t num_bytes, struct fuse_io_priv *io)
655{
656 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600657 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400658 io->size += num_bytes;
659 io->reqs++;
660 spin_unlock(&io->lock);
661
662 req->io = io;
663 req->end = fuse_aio_complete_req;
664
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400665 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400666 fuse_request_send_background(fc, req);
667
668 return num_bytes;
669}
670
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400671static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200672 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700673{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200674 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200675 struct fuse_file *ff = file->private_data;
676 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700677
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200678 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700679 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700680 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700681
682 inarg->read_flags |= FUSE_READ_LOCKOWNER;
683 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
684 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400685
686 if (io->async)
687 return fuse_async_req_send(fc, req, count, io);
688
Tejun Heob93f8582008-11-26 12:03:55 +0100689 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800690 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700691}
692
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700693static void fuse_read_update_size(struct inode *inode, loff_t size,
694 u64 attr_ver)
695{
696 struct fuse_conn *fc = get_fuse_conn(inode);
697 struct fuse_inode *fi = get_fuse_inode(inode);
698
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300699 spin_lock(&fi->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400700 if (attr_ver == fi->attr_version && size < inode->i_size &&
701 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhai4510d862018-11-09 13:33:17 +0300702 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700703 i_size_write(inode, size);
704 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300705 spin_unlock(&fi->lock);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700706}
707
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200708static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
709 struct page **pages, unsigned int num_pages)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710{
Pavel Emelyanov83732002013-10-10 17:10:46 +0400711 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400712
Pavel Emelyanov83732002013-10-10 17:10:46 +0400713 if (fc->writeback_cache) {
714 /*
715 * A hole in a file. Some data after the hole are in page cache,
716 * but have not reached the client fs yet. So, the hole is not
717 * present there.
718 */
719 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300720 int start_idx = num_read >> PAGE_SHIFT;
721 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400722
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200723 for (i = start_idx; i < num_pages; i++) {
724 zero_user_segment(pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400725 off = 0;
726 }
727 } else {
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200728 loff_t pos = page_offset(pages[0]) + num_read;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400729 fuse_read_update_size(inode, pos, attr_ver);
730 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400731}
732
Maxim Patlasov482fce52013-10-10 17:11:25 +0400733static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200735 struct kiocb iocb;
736 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737 struct inode *inode = page->mapping->host;
738 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800739 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700740 size_t num_read;
741 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300742 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700743 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800744 int err;
745
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700746 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300747 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700748 * page-cache page, so make sure we read a properly synced
749 * page.
750 */
751 fuse_wait_on_page_writeback(inode, page->index);
752
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400753 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700754 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400755 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700757 attr_ver = fuse_get_attr_version(fc);
758
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700759 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200760 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700761 req->num_pages = 1;
762 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400763 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200764 init_sync_kiocb(&iocb, file);
765 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400766 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700767 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700768
769 if (!err) {
770 /*
771 * Short read means EOF. If file size is larger, truncate it
772 */
773 if (num_read < count)
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200774 fuse_short_read(inode, attr_ver, num_read, req->pages,
775 req->num_pages);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700776
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700777 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700778 }
779
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400780 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400781
782 return err;
783}
784
785static int fuse_readpage(struct file *file, struct page *page)
786{
787 struct inode *inode = page->mapping->host;
788 int err;
789
790 err = -EIO;
791 if (is_bad_inode(inode))
792 goto out;
793
794 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800795 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700796 out:
797 unlock_page(page);
798 return err;
799}
800
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800801static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700802{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800803 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700804 size_t count = req->misc.read.in.size;
805 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200806 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800807
Miklos Szeredice534fb2010-05-25 15:06:07 +0200808 for (i = 0; mapping == NULL && i < req->num_pages; i++)
809 mapping = req->pages[i]->mapping;
810
811 if (mapping) {
812 struct inode *inode = mapping->host;
813
814 /*
815 * Short read means EOF. If file size is larger, truncate it
816 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400817 if (!req->out.h.error && num_read < count)
Miklos Szeredia0d45d82019-09-10 15:04:09 +0200818 fuse_short_read(inode, req->misc.read.attr_ver,
819 num_read, req->pages, req->num_pages);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820
Andrew Gallagher451418f2013-11-05 03:55:43 -0800821 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700822 }
823
Miklos Szeredidb50b962005-09-09 13:10:33 -0700824 for (i = 0; i < req->num_pages; i++) {
825 struct page *page = req->pages[i];
826 if (!req->out.h.error)
827 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800828 else
829 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700830 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300831 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700832 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700833 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -0800834 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800835}
836
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200837static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800838{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200839 struct fuse_file *ff = file->private_data;
840 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800841 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300842 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200843
844 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800845 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200847 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700848 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800849 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700850 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800851 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100852 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800853 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100854 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800855 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100856 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800857 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858}
859
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700860struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800862 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400864 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700865};
866
867static int fuse_readpages_fill(void *_data, struct page *page)
868{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700869 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 struct fuse_req *req = data->req;
871 struct inode *inode = data->inode;
872 struct fuse_conn *fc = get_fuse_conn(inode);
873
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700874 fuse_wait_on_page_writeback(inode, page->index);
875
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300877 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300878 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300880 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
881 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200882 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400883 if (fc->async_read)
884 req = fuse_get_req_for_background(fc, nr_alloc);
885 else
886 req = fuse_get_req(fc, nr_alloc);
887
888 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700889 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700891 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700892 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700893 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400894
895 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300896 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400897 fuse_put_request(fc, req);
898 return -EIO;
899 }
900
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700902 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400903 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100904 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400905 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700906 return 0;
907}
908
909static int fuse_readpages(struct file *file, struct address_space *mapping,
910 struct list_head *pages, unsigned nr_pages)
911{
912 struct inode *inode = mapping->host;
913 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700914 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300916 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800917
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700918 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800919 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800920 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800921
Miklos Szeredia6643092007-11-28 16:22:00 -0800922 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700923 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400924 if (fc->async_read)
925 data.req = fuse_get_req_for_background(fc, nr_alloc);
926 else
927 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400928 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700929 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700930 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800931 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700932
933 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700934 if (!err) {
935 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200936 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700937 else
938 fuse_put_request(fc, data.req);
939 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800940out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700941 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700942}
943
Miklos Szeredi55752a3a2019-01-24 10:40:17 +0100944static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800945{
946 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400947 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800948
Brian Fostera8894272012-07-16 15:23:50 -0400949 /*
950 * In auto invalidate mode, always update attributes on read.
951 * Otherwise, only update if we attempt to read past EOF (to ensure
952 * i_size is up to date).
953 */
954 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400955 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800956 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200957 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800958 if (err)
959 return err;
960 }
961
Al Viro37c20f12014-04-02 14:47:09 -0400962 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800963}
964
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200965static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700967{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700968 struct fuse_write_in *inarg = &req->misc.write.in;
969 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700970
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700971 inarg->fh = ff->fh;
972 inarg->offset = pos;
973 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700974 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200975 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700976 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200977 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700978 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
979 else
980 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700981 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700982 req->in.args[1].size = count;
983 req->out.numargs = 1;
984 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700985 req->out.args[0].value = outarg;
986}
987
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400988static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200989 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700990{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200991 struct kiocb *iocb = io->iocb;
992 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200993 struct fuse_file *ff = file->private_data;
994 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200995 struct fuse_write_in *inarg = &req->misc.write.in;
996
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200997 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200998 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200999 if (iocb->ki_flags & IOCB_DSYNC)
1000 inarg->flags |= O_DSYNC;
1001 if (iocb->ki_flags & IOCB_SYNC)
1002 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -07001003 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001004 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1005 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1006 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001007
1008 if (io->async)
1009 return fuse_async_req_send(fc, req, count, io);
1010
Tejun Heob93f8582008-11-26 12:03:55 +01001011 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001012 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001013}
1014
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001015bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001016{
1017 struct fuse_conn *fc = get_fuse_conn(inode);
1018 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001019 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001020
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001021 spin_lock(&fi->lock);
Kirill Tkhai4510d862018-11-09 13:33:17 +03001022 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001023 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001024 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001025 ret = true;
1026 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001027 spin_unlock(&fi->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001028
1029 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001030}
1031
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001032static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001033 struct inode *inode, loff_t pos,
1034 size_t count)
1035{
1036 size_t res;
1037 unsigned offset;
1038 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001039 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001040
1041 for (i = 0; i < req->num_pages; i++)
1042 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1043
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001044 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001045
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001046 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001047 count = res;
1048 for (i = 0; i < req->num_pages; i++) {
1049 struct page *page = req->pages[i];
1050
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001051 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001052 SetPageUptodate(page);
1053
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001054 if (count > PAGE_SIZE - offset)
1055 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001056 else
1057 count = 0;
1058 offset = 0;
1059
1060 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001061 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001062 }
1063
1064 return res;
1065}
1066
1067static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1068 struct address_space *mapping,
1069 struct iov_iter *ii, loff_t pos)
1070{
1071 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001073 size_t count = 0;
1074 int err;
1075
Miklos Szeredif4975c62009-04-02 14:25:34 +02001076 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001077 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001078
1079 do {
1080 size_t tmp;
1081 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001082 pgoff_t index = pos >> PAGE_SHIFT;
1083 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 iov_iter_count(ii));
1085
1086 bytes = min_t(size_t, bytes, fc->max_write - count);
1087
1088 again:
1089 err = -EFAULT;
1090 if (iov_iter_fault_in_readable(ii, bytes))
1091 break;
1092
1093 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001094 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 if (!page)
1096 break;
1097
anfei zhou931e80e2010-02-02 13:44:02 -08001098 if (mapping_writably_mapped(mapping))
1099 flush_dcache_page(page);
1100
Nick Pigginea9b9902008-04-30 00:54:42 -07001101 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 flush_dcache_page(page);
1103
Roman Gushchin3ca81382015-10-12 16:33:44 +03001104 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 if (!tmp) {
1106 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001107 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001108 bytes = min(bytes, iov_iter_single_seg_count(ii));
1109 goto again;
1110 }
1111
1112 err = 0;
1113 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001114 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 req->num_pages++;
1116
Nick Pigginea9b9902008-04-30 00:54:42 -07001117 count += tmp;
1118 pos += tmp;
1119 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001120 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001121 offset = 0;
1122
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001123 if (!fc->big_writes)
1124 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001126 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001127
1128 return count > 0 ? count : err;
1129}
1130
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001131static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1132 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001133{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001134 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001135 ((pos + len - 1) >> PAGE_SHIFT) -
1136 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001137 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001138}
1139
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001140static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001141 struct address_space *mapping,
1142 struct iov_iter *ii, loff_t pos)
1143{
1144 struct inode *inode = mapping->host;
1145 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001146 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001147 int err = 0;
1148 ssize_t res = 0;
1149
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001150 if (inode->i_size < pos + iov_iter_count(ii))
1151 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1152
Nick Pigginea9b9902008-04-30 00:54:42 -07001153 do {
1154 struct fuse_req *req;
1155 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001156 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1157 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001158
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001159 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001160 if (IS_ERR(req)) {
1161 err = PTR_ERR(req);
1162 break;
1163 }
1164
1165 count = fuse_fill_write_pages(req, mapping, ii, pos);
1166 if (count <= 0) {
1167 err = count;
1168 } else {
1169 size_t num_written;
1170
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001171 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001172 pos, count);
1173 err = req->out.h.error;
1174 if (!err) {
1175 res += num_written;
1176 pos += num_written;
1177
1178 /* break out of the loop on short write */
1179 if (num_written != count)
1180 err = -EIO;
1181 }
1182 }
1183 fuse_put_request(fc, req);
1184 } while (!err && iov_iter_count(ii));
1185
1186 if (res > 0)
1187 fuse_write_update_size(inode, pos);
1188
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001189 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001190 fuse_invalidate_attr(inode);
1191
1192 return res > 0 ? res : err;
1193}
1194
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001195static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001196{
1197 struct file *file = iocb->ki_filp;
1198 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001199 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001200 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001201 struct inode *inode = mapping->host;
1202 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001203 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001204
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001205 if (get_fuse_conn(inode)->writeback_cache) {
1206 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001207 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001208 if (err)
1209 return err;
1210
Al Viro84c3d552014-04-03 14:33:23 -04001211 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001212 }
1213
Al Viro59551022016-01-22 15:40:57 -05001214 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001215
1216 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001217 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001218
Al Viro3309dd02015-04-09 12:55:47 -04001219 err = generic_write_checks(iocb, from);
1220 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001221 goto out;
1222
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001223 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001224 if (err)
1225 goto out;
1226
Josef Bacikc3b2da32012-03-26 09:59:21 -04001227 err = file_update_time(file);
1228 if (err)
1229 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001230
Al Viro2ba48ce2015-04-09 13:52:01 -04001231 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001232 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001233 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001234 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001235 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001236
Anand Avati4273b792012-02-17 12:46:25 -05001237 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001238
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001239 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001240 if (written_buffered < 0) {
1241 err = written_buffered;
1242 goto out;
1243 }
1244 endbyte = pos + written_buffered - 1;
1245
1246 err = filemap_write_and_wait_range(file->f_mapping, pos,
1247 endbyte);
1248 if (err)
1249 goto out;
1250
1251 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001252 pos >> PAGE_SHIFT,
1253 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001254
1255 written += written_buffered;
1256 iocb->ki_pos = pos + written_buffered;
1257 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001258 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001259 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001260 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001261 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001262out:
1263 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001264 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001265 if (written > 0)
1266 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001267
1268 return written ? written : err;
1269}
1270
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001271static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1272 unsigned int index,
1273 unsigned int nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001274{
1275 int i;
1276
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001277 for (i = index; i < index + nr_pages; i++)
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001278 descs[i].length = PAGE_SIZE - descs[i].offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001279}
1280
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1282{
1283 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1284}
1285
1286static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1287 size_t max_size)
1288{
1289 return min(iov_iter_single_seg_count(ii), max_size);
1290}
1291
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001292static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001293 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001294{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001295 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001296 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001297
Miklos Szeredif4975c62009-04-02 14:25:34 +02001298 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001299 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001300 unsigned long user_addr = fuse_get_user_addr(ii);
1301 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1302
Miklos Szeredif4975c62009-04-02 14:25:34 +02001303 if (write)
1304 req->in.args[1].value = (void *) user_addr;
1305 else
1306 req->out.args[0].value = (void *) user_addr;
1307
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001308 iov_iter_advance(ii, frag_size);
1309 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001310 return 0;
1311 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001312
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001313 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001314 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001315 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001316 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001317 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001318 req->max_pages - req->num_pages,
1319 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001320 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001321 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001322
Al Viroc9c37e22014-03-16 16:08:30 -04001323 iov_iter_advance(ii, ret);
1324 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001325
Al Viroc9c37e22014-03-16 16:08:30 -04001326 ret += start;
1327 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1328
1329 req->page_descs[req->num_pages].offset = start;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02001330 fuse_page_descs_length_init(req->page_descs, req->num_pages,
1331 npages);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001332
1333 req->num_pages += npages;
1334 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001335 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001336 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001337
1338 if (write)
1339 req->in.argpages = 1;
1340 else
1341 req->out.argpages = 1;
1342
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001343 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001344
Ashish Samant2c932d42016-03-25 10:53:41 -07001345 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001346}
1347
Al Virod22a9432014-03-16 15:50:47 -04001348ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1349 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001350{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001351 int write = flags & FUSE_DIO_WRITE;
1352 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001353 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001354 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001355 struct fuse_file *ff = file->private_data;
1356 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001357 size_t nmax = write ? fc->max_write : fc->max_read;
1358 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001359 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 pgoff_t idx_from = pos >> PAGE_SHIFT;
1361 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001363 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001364 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001365
Brian Fosterde82b922013-05-14 11:25:39 -04001366 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001367 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1368 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001369 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001370 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001371 if (IS_ERR(req))
1372 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001373
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001374 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1375 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001376 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001377 fuse_sync_writes(inode);
1378 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001379 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001380 }
1381
Ashish Samant61c12b42017-07-12 19:26:58 -07001382 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001384 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001385 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001386 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001387 err = fuse_get_user_pages(req, iter, &nbytes, write);
1388 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001389 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001390
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001391 if (write) {
1392 if (!capable(CAP_FSETID)) {
1393 struct fuse_write_in *inarg;
1394
1395 inarg = &req->misc.write.in;
1396 inarg->write_flags |= FUSE_WRITE_KILL_PRIV;
1397 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001398 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001399 } else {
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001400 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi4a2abf92019-05-27 09:08:12 +02001401 }
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001402
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001403 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001404 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001406 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407 break;
1408 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001409 res = 0;
1410 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001411 break;
1412 }
1413 count -= nres;
1414 res += nres;
1415 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001416 if (nres != nbytes)
1417 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001418 if (count) {
1419 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001420 if (io->async)
1421 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001422 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001423 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001424 req = fuse_get_req(fc, iov_iter_npages(iter,
1425 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001426 if (IS_ERR(req))
1427 break;
1428 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001430 if (!IS_ERR(req))
1431 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001432 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001433 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434
Ashish Samant742f9922016-03-14 21:57:35 -07001435 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436}
Tejun Heo08cbf542009-04-14 10:54:53 +09001437EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001438
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001439static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001440 struct iov_iter *iter,
1441 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001442{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001443 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001444 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001445
Al Virod22a9432014-03-16 15:50:47 -04001446 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001447
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001448 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001449
1450 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001451}
1452
Martin Raiber23c94e12018-10-27 16:48:48 +00001453static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1454
Al Viro153162632015-03-30 22:08:36 -04001455static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001456{
Martin Raiber23c94e12018-10-27 16:48:48 +00001457 ssize_t res;
1458
1459 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
Martin Raiber23c94e12018-10-27 16:48:48 +00001460 res = fuse_direct_IO(iocb, to);
1461 } else {
1462 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1463
1464 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1465 }
1466
1467 return res;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001468}
1469
Al Viro153162632015-03-30 22:08:36 -04001470static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001471{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001472 struct inode *inode = file_inode(iocb->ki_filp);
1473 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001474 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001475
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001476 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001477 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001478 res = generic_write_checks(iocb, from);
Martin Raiber23c94e12018-10-27 16:48:48 +00001479 if (res > 0) {
1480 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1481 res = fuse_direct_IO(iocb, from);
1482 } else {
1483 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1484 FUSE_DIO_WRITE);
1485 }
1486 }
Al Viro812408f2015-03-30 22:15:58 -04001487 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001488 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001489 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001490 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001491
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001492 return res;
1493}
1494
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001495static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1496{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001497 struct file *file = iocb->ki_filp;
1498 struct fuse_file *ff = file->private_data;
1499
1500 if (is_bad_inode(file_inode(file)))
1501 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001502
1503 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1504 return fuse_cache_read_iter(iocb, to);
1505 else
1506 return fuse_direct_read_iter(iocb, to);
1507}
1508
1509static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1510{
Miklos Szeredi2f7b6f52019-01-24 10:40:17 +01001511 struct file *file = iocb->ki_filp;
1512 struct fuse_file *ff = file->private_data;
1513
1514 if (is_bad_inode(file_inode(file)))
1515 return -EIO;
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01001516
1517 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1518 return fuse_cache_write_iter(iocb, from);
1519 else
1520 return fuse_direct_write_iter(iocb, from);
1521}
1522
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001523static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001524{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001525 int i;
1526
1527 for (i = 0; i < req->num_pages; i++)
1528 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001529
1530 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001531 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001532}
1533
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001534static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001535{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001536 struct inode *inode = req->inode;
1537 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001538 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001539 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001540
1541 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001542 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001543 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001544 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001545 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001546 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547 wake_up(&fi->page_waitq);
1548}
1549
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001550/* Called under fi->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001551static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1552 loff_t size)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001553__releases(fi->lock)
1554__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001555{
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001556 struct fuse_req *aux, *next;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001557 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001558 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001560 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001561
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001562 if (inarg->offset + data_size <= size) {
1563 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001564 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001565 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001566 } else {
1567 /* Got truncated off completely */
1568 goto out_free;
1569 }
1570
1571 req->in.args[1].size = inarg->size;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001572 queued = fuse_request_queue_background(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001573 /* Fails on broken connection only */
1574 if (unlikely(!queued))
1575 goto out_free;
1576
1577 fi->writectr++;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001578 return;
1579
1580 out_free:
1581 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001582 spin_unlock(&fi->lock);
Miklos Szeredie2653bd2019-01-24 10:40:15 +01001583
1584 /* After fuse_writepage_finish() aux request list is private */
1585 for (aux = req->misc.write.next; aux; aux = next) {
1586 next = aux->misc.write.next;
1587 aux->misc.write.next = NULL;
1588 fuse_writepage_free(fc, aux);
1589 fuse_put_request(fc, aux);
1590 }
1591
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001592 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001593 fuse_put_request(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001594 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001595}
1596
1597/*
1598 * If fi->writectr is positive (no truncate or fsync going on) send
1599 * all queued writepage requests.
1600 *
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001601 * Called with fi->lock
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001602 */
1603void fuse_flush_writepages(struct inode *inode)
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001604__releases(fi->lock)
1605__acquires(fi->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001606{
1607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9de5be02019-04-24 17:05:06 +02001609 loff_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001610 struct fuse_req *req;
1611
1612 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1613 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1614 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001615 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001616 }
1617}
1618
1619static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1620{
1621 struct inode *inode = req->inode;
1622 struct fuse_inode *fi = get_fuse_inode(inode);
1623
1624 mapping_set_error(inode->i_mapping, req->out.h.error);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001625 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001626 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001627 struct fuse_conn *fc = get_fuse_conn(inode);
1628 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001629 struct fuse_req *next = req->misc.write.next;
1630 req->misc.write.next = next->misc.write.next;
1631 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001632 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001633 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001634
1635 /*
1636 * Skip fuse_flush_writepages() to make it easy to crop requests
1637 * based on primary request size.
1638 *
1639 * 1st case (trivial): there are no concurrent activities using
1640 * fuse_set/release_nowrite. Then we're on safe side because
1641 * fuse_flush_writepages() would call fuse_send_writepage()
1642 * anyway.
1643 *
1644 * 2nd case: someone called fuse_set_nowrite and it is waiting
1645 * now for completion of all in-flight requests. This happens
1646 * rarely and no more than once per page, so this should be
1647 * okay.
1648 *
1649 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1650 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1651 * that fuse_set_nowrite returned implies that all in-flight
1652 * requests were completed along with all of their secondary
1653 * requests. Further primary requests are blocked by negative
1654 * writectr. Hence there cannot be any in-flight requests and
1655 * no invocations of fuse_writepage_end() while we're in
1656 * fuse_set_nowrite..fuse_release_nowrite section.
1657 */
1658 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001659 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660 fi->writectr--;
1661 fuse_writepage_finish(fc, req);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001662 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663 fuse_writepage_free(fc, req);
1664}
1665
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001666static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1667 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001668{
Miklos Szeredi72523422013-10-01 16:44:52 +02001669 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001670
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001671 spin_lock(&fi->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001672 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001673 ff = list_entry(fi->write_files.next, struct fuse_file,
1674 write_entry);
1675 fuse_file_get(ff);
1676 }
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001677 spin_unlock(&fi->lock);
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001678
1679 return ff;
1680}
1681
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001682static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1683 struct fuse_inode *fi)
1684{
1685 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1686 WARN_ON(!ff);
1687 return ff;
1688}
1689
1690int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1691{
1692 struct fuse_conn *fc = get_fuse_conn(inode);
1693 struct fuse_inode *fi = get_fuse_inode(inode);
1694 struct fuse_file *ff;
1695 int err;
1696
1697 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001698 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001699 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001700 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001701
1702 return err;
1703}
1704
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001705static int fuse_writepage_locked(struct page *page)
1706{
1707 struct address_space *mapping = page->mapping;
1708 struct inode *inode = mapping->host;
1709 struct fuse_conn *fc = get_fuse_conn(inode);
1710 struct fuse_inode *fi = get_fuse_inode(inode);
1711 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001712 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001713 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001714
1715 set_page_writeback(page);
1716
Maxim Patlasov4250c062012-10-26 19:48:07 +04001717 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001718 if (!req)
1719 goto err;
1720
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001721 /* writeback always goes to bg_queue */
1722 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001723 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1724 if (!tmp_page)
1725 goto err_free;
1726
Miklos Szeredi72523422013-10-01 16:44:52 +02001727 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001728 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001729 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001730 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001731
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001732 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001733
1734 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001735 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001736 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001737 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001738 req->num_pages = 1;
1739 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001740 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001741 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001742 req->end = fuse_writepage_end;
1743 req->inode = inode;
1744
Tejun Heo93f78d82015-05-22 17:13:27 -04001745 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001746 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001747
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001748 spin_lock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001749 list_add(&req->writepages_entry, &fi->writepages);
1750 list_add_tail(&req->list, &fi->queued_writes);
1751 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001752 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001753
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001754 end_page_writeback(page);
1755
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001756 return 0;
1757
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001758err_nofile:
1759 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001760err_free:
1761 fuse_request_free(req);
1762err:
Jeff Layton91839762017-05-25 06:57:50 -04001763 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001764 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001765 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001766}
1767
1768static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1769{
1770 int err;
1771
Miklos Szerediff17be02013-10-01 16:44:53 +02001772 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1773 /*
1774 * ->writepages() should be called for sync() and friends. We
1775 * should only get here on direct reclaim and then we are
1776 * allowed to skip a page which is already in flight
1777 */
1778 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1779
1780 redirty_page_for_writepage(wbc, page);
1781 return 0;
1782 }
1783
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001784 err = fuse_writepage_locked(page);
1785 unlock_page(page);
1786
1787 return err;
1788}
1789
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001790struct fuse_fill_wb_data {
1791 struct fuse_req *req;
1792 struct fuse_file *ff;
1793 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001794 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001795};
1796
1797static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1798{
1799 struct fuse_req *req = data->req;
1800 struct inode *inode = data->inode;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001801 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001802 int num_pages = req->num_pages;
1803 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001804
1805 req->ff = fuse_file_get(data->ff);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001806 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001807 list_add_tail(&req->list, &fi->queued_writes);
1808 fuse_flush_writepages(inode);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001809 spin_unlock(&fi->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001810
1811 for (i = 0; i < num_pages; i++)
1812 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001813}
1814
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001815/*
1816 * First recheck under fi->lock if the offending offset is still under
Miklos Szeredi419234d2019-01-16 10:27:59 +01001817 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1818 * one already added for a page at this offset. If there's none, then insert
1819 * this new request onto the auxiliary list, otherwise reuse the existing one by
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001820 * copying the new page contents over to the old temporary page.
1821 */
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001822static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1823 struct page *page)
1824{
1825 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1826 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1827 struct fuse_req *tmp;
1828 struct fuse_req *old_req;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001829
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001830 WARN_ON(new_req->num_pages != 0);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001831
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001832 spin_lock(&fi->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001833 list_del(&new_req->writepages_entry);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001834 old_req = fuse_find_writeback(fi, page->index, page->index);
1835 if (!old_req) {
Maxim Patlasovf6011082013-10-02 15:01:07 +04001836 list_add(&new_req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001837 spin_unlock(&fi->lock);
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001838 return false;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001839 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001840
Maxim Patlasovf6011082013-10-02 15:01:07 +04001841 new_req->num_pages = 1;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001842 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001843 pgoff_t curr_index;
1844
1845 WARN_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001846 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi419234d2019-01-16 10:27:59 +01001847 if (curr_index == page->index) {
1848 WARN_ON(tmp->num_pages != 1);
1849 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
Kirill Tkhaic5de16cc2018-11-26 12:46:20 +03001850 swap(tmp->pages[0], new_req->pages[0]);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001851 break;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001852 }
1853 }
1854
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001855 if (!tmp) {
1856 new_req->misc.write.next = old_req->misc.write.next;
1857 old_req->misc.write.next = new_req;
1858 }
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001859
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001860 spin_unlock(&fi->lock);
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001861
1862 if (tmp) {
1863 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001864
Tejun Heo93f78d82015-05-22 17:13:27 -04001865 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredia2ebba82019-01-16 10:27:59 +01001866 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001867 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001868 fuse_writepage_free(fc, new_req);
1869 fuse_request_free(new_req);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001870 }
Miklos Szeredi7f305ca2019-01-16 10:27:59 +01001871
Miklos Szeredi2fe93bd2019-01-16 10:27:59 +01001872 return true;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001873}
1874
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001875static int fuse_writepages_fill(struct page *page,
1876 struct writeback_control *wbc, void *_data)
1877{
1878 struct fuse_fill_wb_data *data = _data;
1879 struct fuse_req *req = data->req;
1880 struct inode *inode = data->inode;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001881 struct fuse_inode *fi = get_fuse_inode(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001882 struct fuse_conn *fc = get_fuse_conn(inode);
1883 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001884 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001885 int err;
1886
1887 if (!data->ff) {
1888 err = -EIO;
1889 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1890 if (!data->ff)
1891 goto out_unlock;
1892 }
1893
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001894 /*
1895 * Being under writeback is unlikely but possible. For example direct
1896 * read to an mmaped fuse file will set the page dirty twice; once when
1897 * the pages are faulted with get_user_pages(), and then after the read
1898 * completed.
1899 */
1900 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001901
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001902 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001903 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001904 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001905 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1906 fuse_writepages_send(data);
1907 data->req = NULL;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001908 } else if (req && req->num_pages == req->max_pages) {
1909 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1910 fuse_writepages_send(data);
1911 req = data->req = NULL;
1912 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001913 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02001914
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001915 err = -ENOMEM;
1916 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1917 if (!tmp_page)
1918 goto out_unlock;
1919
1920 /*
1921 * The page must not be redirtied until the writeout is completed
1922 * (i.e. userspace has sent a reply to the write request). Otherwise
1923 * there could be more than one temporary page instance for each real
1924 * page.
1925 *
1926 * This is ensured by holding the page lock in page_mkwrite() while
1927 * checking fuse_page_is_writeback(). We already hold the page lock
1928 * since clear_page_dirty_for_io() and keep it held until we add the
1929 * request to the fi->writepages list and increment req->num_pages.
1930 * After this fuse_page_is_writeback() will indicate that the page is
1931 * under writeback, so we can release the page lock.
1932 */
1933 if (data->req == NULL) {
1934 struct fuse_inode *fi = get_fuse_inode(inode);
1935
1936 err = -ENOMEM;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001937 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001938 if (!req) {
1939 __free_page(tmp_page);
1940 goto out_unlock;
1941 }
1942
1943 fuse_write_fill(req, data->ff, page_offset(page), 0);
1944 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001945 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001946 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001947 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001948 req->num_pages = 0;
1949 req->end = fuse_writepage_end;
1950 req->inode = inode;
1951
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001952 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001953 list_add(&req->writepages_entry, &fi->writepages);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001954 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001955
1956 data->req = req;
1957 }
1958 set_page_writeback(page);
1959
1960 copy_highpage(tmp_page, page);
1961 req->pages[req->num_pages] = tmp_page;
1962 req->page_descs[req->num_pages].offset = 0;
1963 req->page_descs[req->num_pages].length = PAGE_SIZE;
1964
Tejun Heo93f78d82015-05-22 17:13:27 -04001965 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001966 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001967
1968 err = 0;
1969 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1970 end_page_writeback(page);
1971 data->req = NULL;
1972 goto out_unlock;
1973 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001974 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001975
1976 /*
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001977 * Protected by fi->lock against concurrent access by
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001978 * fuse_page_is_writeback().
1979 */
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001980 spin_lock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001981 req->num_pages++;
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +03001982 spin_unlock(&fi->lock);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001983
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001984out_unlock:
1985 unlock_page(page);
1986
1987 return err;
1988}
1989
1990static int fuse_writepages(struct address_space *mapping,
1991 struct writeback_control *wbc)
1992{
1993 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001994 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001995 struct fuse_fill_wb_data data;
1996 int err;
1997
1998 err = -EIO;
1999 if (is_bad_inode(inode))
2000 goto out;
2001
2002 data.inode = inode;
2003 data.req = NULL;
2004 data.ff = NULL;
2005
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002006 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002007 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02002008 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002009 GFP_NOFS);
2010 if (!data.orig_pages)
2011 goto out;
2012
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002013 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2014 if (data.req) {
2015 /* Ignore errors if we can write at least one page */
2016 BUG_ON(!data.req->num_pages);
2017 fuse_writepages_send(&data);
2018 err = 0;
2019 }
2020 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08002021 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002022
2023 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002024out:
2025 return err;
2026}
2027
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002028/*
2029 * It's worthy to make sure that space is reserved on disk for the write,
2030 * but how to implement it without killing performance need more thinking.
2031 */
2032static int fuse_write_begin(struct file *file, struct address_space *mapping,
2033 loff_t pos, unsigned len, unsigned flags,
2034 struct page **pagep, void **fsdata)
2035{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002036 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04002037 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002038 struct page *page;
2039 loff_t fsize;
2040 int err = -ENOMEM;
2041
2042 WARN_ON(!fc->writeback_cache);
2043
2044 page = grab_cache_page_write_begin(mapping, index, flags);
2045 if (!page)
2046 goto error;
2047
2048 fuse_wait_on_page_writeback(mapping->host, page->index);
2049
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002050 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002051 goto success;
2052 /*
2053 * Check if the start this page comes after the end of file, in which
2054 * case the readpage can be optimized away.
2055 */
2056 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 if (fsize <= (pos & PAGE_MASK)) {
2058 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002059 if (off)
2060 zero_user_segment(page, 0, off);
2061 goto success;
2062 }
2063 err = fuse_do_readpage(file, page);
2064 if (err)
2065 goto cleanup;
2066success:
2067 *pagep = page;
2068 return 0;
2069
2070cleanup:
2071 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002073error:
2074 return err;
2075}
2076
2077static int fuse_write_end(struct file *file, struct address_space *mapping,
2078 loff_t pos, unsigned len, unsigned copied,
2079 struct page *page, void *fsdata)
2080{
2081 struct inode *inode = page->mapping->host;
2082
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002083 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2084 if (!copied)
2085 goto unlock;
2086
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002087 if (!PageUptodate(page)) {
2088 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002090 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002091 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002092 SetPageUptodate(page);
2093 }
2094
2095 fuse_write_update_size(inode, pos + copied);
2096 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002097
2098unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002099 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002101
2102 return copied;
2103}
2104
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002105static int fuse_launder_page(struct page *page)
2106{
2107 int err = 0;
2108 if (clear_page_dirty_for_io(page)) {
2109 struct inode *inode = page->mapping->host;
2110 err = fuse_writepage_locked(page);
2111 if (!err)
2112 fuse_wait_on_page_writeback(inode, page->index);
2113 }
2114 return err;
2115}
2116
2117/*
2118 * Write back dirty pages now, because there may not be any suitable
2119 * open files later
2120 */
2121static void fuse_vma_close(struct vm_area_struct *vma)
2122{
2123 filemap_write_and_wait(vma->vm_file->f_mapping);
2124}
2125
2126/*
2127 * Wait for writeback against this page to complete before allowing it
2128 * to be marked dirty again, and hence written back again, possibly
2129 * before the previous writepage completed.
2130 *
2131 * Block here, instead of in ->writepage(), so that the userspace fs
2132 * can only block processes actually operating on the filesystem.
2133 *
2134 * Otherwise unprivileged userspace fs would be able to block
2135 * unrelated:
2136 *
2137 * - page migration
2138 * - sync(2)
2139 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2140 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302141static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002142{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002143 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002144 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002145
Dave Jiang11bac802017-02-24 14:56:41 -08002146 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002147 lock_page(page);
2148 if (page->mapping != inode->i_mapping) {
2149 unlock_page(page);
2150 return VM_FAULT_NOPAGE;
2151 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002152
2153 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002154 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002155}
2156
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002157static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002158 .close = fuse_vma_close,
2159 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002160 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002161 .page_mkwrite = fuse_page_mkwrite,
2162};
2163
2164static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2165{
Miklos Szeredi55752a3a2019-01-24 10:40:17 +01002166 struct fuse_file *ff = file->private_data;
2167
2168 if (ff->open_flags & FOPEN_DIRECT_IO) {
2169 /* Can't provide the coherency needed for MAP_SHARED */
2170 if (vma->vm_flags & VM_MAYSHARE)
2171 return -ENODEV;
2172
2173 invalidate_inode_pages2(file->f_mapping);
2174
2175 return generic_file_mmap(file, vma);
2176 }
2177
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002178 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2179 fuse_link_write_file(file);
2180
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002181 file_accessed(file);
2182 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002183 return 0;
2184}
2185
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002186static int convert_fuse_file_lock(struct fuse_conn *fc,
2187 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002188 struct file_lock *fl)
2189{
2190 switch (ffl->type) {
2191 case F_UNLCK:
2192 break;
2193
2194 case F_RDLCK:
2195 case F_WRLCK:
2196 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2197 ffl->end < ffl->start)
2198 return -EIO;
2199
2200 fl->fl_start = ffl->start;
2201 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002202
2203 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002204 * Convert pid into init's pid namespace. The locks API will
2205 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002206 */
2207 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002208 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002209 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002210 break;
2211
2212 default:
2213 return -EIO;
2214 }
2215 fl->fl_type = ffl->type;
2216 return 0;
2217}
2218
Miklos Szeredi70781872014-12-12 09:49:05 +01002219static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002220 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002221 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002222{
Al Viro6131ffa2013-02-27 16:59:05 -05002223 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002224 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002225 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002226
Miklos Szeredi70781872014-12-12 09:49:05 +01002227 memset(inarg, 0, sizeof(*inarg));
2228 inarg->fh = ff->fh;
2229 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2230 inarg->lk.start = fl->fl_start;
2231 inarg->lk.end = fl->fl_end;
2232 inarg->lk.type = fl->fl_type;
2233 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002234 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002235 inarg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002236 args->opcode = opcode;
2237 args->nodeid = get_node_id(inode);
2238 args->in_numargs = 1;
2239 args->in_args[0].size = sizeof(*inarg);
2240 args->in_args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002241}
2242
2243static int fuse_getlk(struct file *file, struct file_lock *fl)
2244{
Al Viro6131ffa2013-02-27 16:59:05 -05002245 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002246 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002247 FUSE_ARGS(args);
2248 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002249 struct fuse_lk_out outarg;
2250 int err;
2251
Miklos Szeredi70781872014-12-12 09:49:05 +01002252 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
Miklos Szeredid5b48542019-09-10 15:04:08 +02002253 args.out_numargs = 1;
2254 args.out_args[0].size = sizeof(outarg);
2255 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002256 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002257 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002258 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002259
2260 return err;
2261}
2262
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002263static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002264{
Al Viro6131ffa2013-02-27 16:59:05 -05002265 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002266 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002267 FUSE_ARGS(args);
2268 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002269 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002270 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2271 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002272 int err;
2273
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002274 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002275 /* NLM needs asynchronous locks, which we don't support yet */
2276 return -ENOLCK;
2277 }
2278
Miklos Szeredi71421252006-06-25 05:48:52 -07002279 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002280 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002281 return 0;
2282
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002283 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002284 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002285
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002286 /* locking is restartable */
2287 if (err == -EINTR)
2288 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002289
Miklos Szeredi71421252006-06-25 05:48:52 -07002290 return err;
2291}
2292
2293static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2294{
Al Viro6131ffa2013-02-27 16:59:05 -05002295 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002296 struct fuse_conn *fc = get_fuse_conn(inode);
2297 int err;
2298
Miklos Szeredi48e90762008-07-25 01:49:02 -07002299 if (cmd == F_CANCELLK) {
2300 err = 0;
2301 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002302 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002303 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002304 err = 0;
2305 } else
2306 err = fuse_getlk(file, fl);
2307 } else {
2308 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002309 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002310 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002311 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002312 }
2313 return err;
2314}
2315
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002316static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2317{
Al Viro6131ffa2013-02-27 16:59:05 -05002318 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002319 struct fuse_conn *fc = get_fuse_conn(inode);
2320 int err;
2321
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002322 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002323 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002324 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002325 struct fuse_file *ff = file->private_data;
2326
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002327 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002328 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002329 err = fuse_setlk(file, fl, 1);
2330 }
2331
2332 return err;
2333}
2334
Miklos Szeredib2d22722006-12-06 20:35:51 -08002335static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2336{
2337 struct inode *inode = mapping->host;
2338 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002339 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002340 struct fuse_bmap_in inarg;
2341 struct fuse_bmap_out outarg;
2342 int err;
2343
2344 if (!inode->i_sb->s_bdev || fc->no_bmap)
2345 return 0;
2346
Miklos Szeredib2d22722006-12-06 20:35:51 -08002347 memset(&inarg, 0, sizeof(inarg));
2348 inarg.block = block;
2349 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredid5b48542019-09-10 15:04:08 +02002350 args.opcode = FUSE_BMAP;
2351 args.nodeid = get_node_id(inode);
2352 args.in_numargs = 1;
2353 args.in_args[0].size = sizeof(inarg);
2354 args.in_args[0].value = &inarg;
2355 args.out_numargs = 1;
2356 args.out_args[0].size = sizeof(outarg);
2357 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002358 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002359 if (err == -ENOSYS)
2360 fc->no_bmap = 1;
2361
2362 return err ? 0 : outarg.block;
2363}
2364
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302365static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2366{
2367 struct inode *inode = file->f_mapping->host;
2368 struct fuse_conn *fc = get_fuse_conn(inode);
2369 struct fuse_file *ff = file->private_data;
2370 FUSE_ARGS(args);
2371 struct fuse_lseek_in inarg = {
2372 .fh = ff->fh,
2373 .offset = offset,
2374 .whence = whence
2375 };
2376 struct fuse_lseek_out outarg;
2377 int err;
2378
2379 if (fc->no_lseek)
2380 goto fallback;
2381
Miklos Szeredid5b48542019-09-10 15:04:08 +02002382 args.opcode = FUSE_LSEEK;
2383 args.nodeid = ff->nodeid;
2384 args.in_numargs = 1;
2385 args.in_args[0].size = sizeof(inarg);
2386 args.in_args[0].value = &inarg;
2387 args.out_numargs = 1;
2388 args.out_args[0].size = sizeof(outarg);
2389 args.out_args[0].value = &outarg;
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302390 err = fuse_simple_request(fc, &args);
2391 if (err) {
2392 if (err == -ENOSYS) {
2393 fc->no_lseek = 1;
2394 goto fallback;
2395 }
2396 return err;
2397 }
2398
2399 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2400
2401fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002402 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302403 if (!err)
2404 return generic_file_llseek(file, offset, whence);
2405 else
2406 return err;
2407}
2408
Andrew Morton965c8e52012-12-17 15:59:39 -08002409static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002410{
2411 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002412 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002413
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302414 switch (whence) {
2415 case SEEK_SET:
2416 case SEEK_CUR:
2417 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002418 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302419 break;
2420 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002421 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002422 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302423 if (!retval)
2424 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002425 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302426 break;
2427 case SEEK_HOLE:
2428 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002429 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302430 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002431 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302432 break;
2433 default:
2434 retval = -EINVAL;
2435 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002436
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002437 return retval;
2438}
2439
Tejun Heo59efec72008-11-26 12:03:55 +01002440/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002441 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2442 * ABI was defined to be 'struct iovec' which is different on 32bit
2443 * and 64bit. Fortunately we can determine which structure the server
2444 * used from the size of the reply.
2445 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002446static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2447 size_t transferred, unsigned count,
2448 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002449{
2450#ifdef CONFIG_COMPAT
2451 if (count * sizeof(struct compat_iovec) == transferred) {
2452 struct compat_iovec *ciov = src;
2453 unsigned i;
2454
2455 /*
2456 * With this interface a 32bit server cannot support
2457 * non-compat (i.e. ones coming from 64bit apps) ioctl
2458 * requests
2459 */
2460 if (!is_compat)
2461 return -EINVAL;
2462
2463 for (i = 0; i < count; i++) {
2464 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2465 dst[i].iov_len = ciov[i].iov_len;
2466 }
2467 return 0;
2468 }
2469#endif
2470
2471 if (count * sizeof(struct iovec) != transferred)
2472 return -EIO;
2473
2474 memcpy(dst, src, transferred);
2475 return 0;
2476}
2477
Miklos Szeredi75727772010-11-30 16:39:27 +01002478/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002479static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2480 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002481{
2482 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002483 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002484
Zach Brownfb6ccff2012-07-24 12:10:11 -07002485 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002486 if (iov->iov_len > (size_t) max)
2487 return -ENOMEM;
2488 max -= iov->iov_len;
2489 }
2490 return 0;
2491}
2492
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002493static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2494 void *src, size_t transferred, unsigned count,
2495 bool is_compat)
2496{
2497 unsigned i;
2498 struct fuse_ioctl_iovec *fiov = src;
2499
2500 if (fc->minor < 16) {
2501 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2502 count, is_compat);
2503 }
2504
2505 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2506 return -EIO;
2507
2508 for (i = 0; i < count; i++) {
2509 /* Did the server supply an inappropriate value? */
2510 if (fiov[i].base != (unsigned long) fiov[i].base ||
2511 fiov[i].len != (unsigned long) fiov[i].len)
2512 return -EIO;
2513
2514 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2515 dst[i].iov_len = (size_t) fiov[i].len;
2516
2517#ifdef CONFIG_COMPAT
2518 if (is_compat &&
2519 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2520 (compat_size_t) dst[i].iov_len != fiov[i].len))
2521 return -EIO;
2522#endif
2523 }
2524
2525 return 0;
2526}
2527
2528
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002529/*
Tejun Heo59efec72008-11-26 12:03:55 +01002530 * For ioctls, there is no generic way to determine how much memory
2531 * needs to be read and/or written. Furthermore, ioctls are allowed
2532 * to dereference the passed pointer, so the parameter requires deep
2533 * copying but FUSE has no idea whatsoever about what to copy in or
2534 * out.
2535 *
2536 * This is solved by allowing FUSE server to retry ioctl with
2537 * necessary in/out iovecs. Let's assume the ioctl implementation
2538 * needs to read in the following structure.
2539 *
2540 * struct a {
2541 * char *buf;
2542 * size_t buflen;
2543 * }
2544 *
2545 * On the first callout to FUSE server, inarg->in_size and
2546 * inarg->out_size will be NULL; then, the server completes the ioctl
2547 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2548 * the actual iov array to
2549 *
2550 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2551 *
2552 * which tells FUSE to copy in the requested area and retry the ioctl.
2553 * On the second round, the server has access to the structure and
2554 * from that it can tell what to look for next, so on the invocation,
2555 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2556 *
2557 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2558 * { .iov_base = a.buf, .iov_len = a.buflen } }
2559 *
2560 * FUSE will copy both struct a and the pointed buffer from the
2561 * process doing the ioctl and retry ioctl with both struct a and the
2562 * buffer.
2563 *
2564 * This time, FUSE server has everything it needs and completes ioctl
2565 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2566 *
2567 * Copying data out works the same way.
2568 *
2569 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2570 * automatically initializes in and out iovs by decoding @cmd with
2571 * _IOC_* macros and the server is not allowed to request RETRY. This
2572 * limits ioctl data transfers to well-formed ioctls and is the forced
2573 * behavior for all FUSE servers.
2574 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002575long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2576 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002577{
Tejun Heo59efec72008-11-26 12:03:55 +01002578 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002579 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002580 struct fuse_ioctl_in inarg = {
2581 .fh = ff->fh,
2582 .cmd = cmd,
2583 .arg = arg,
2584 .flags = flags
2585 };
2586 struct fuse_ioctl_out outarg;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002587 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002588 struct iovec *in_iov = NULL, *out_iov = NULL;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002589 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2590 size_t in_size, out_size, c;
2591 ssize_t transferred;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002592 int err, i;
2593 struct iov_iter ii;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002594 struct fuse_args_pages ap = {};
Tejun Heo59efec72008-11-26 12:03:55 +01002595
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002596#if BITS_PER_LONG == 32
2597 inarg.flags |= FUSE_IOCTL_32BIT;
2598#else
Ian Abbott6407f442019-04-24 15:14:11 +01002599 if (flags & FUSE_IOCTL_COMPAT) {
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002600 inarg.flags |= FUSE_IOCTL_32BIT;
Ian Abbott6407f442019-04-24 15:14:11 +01002601#ifdef CONFIG_X86_X32
2602 if (in_x32_syscall())
2603 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2604#endif
2605 }
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002606#endif
2607
Tejun Heo59efec72008-11-26 12:03:55 +01002608 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002609 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002610
Tejun Heo59efec72008-11-26 12:03:55 +01002611 err = -ENOMEM;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002612 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002613 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002614 if (!ap.pages || !iov_page)
Tejun Heo59efec72008-11-26 12:03:55 +01002615 goto out;
2616
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002617 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2618
Tejun Heo59efec72008-11-26 12:03:55 +01002619 /*
2620 * If restricted, initialize IO parameters as encoded in @cmd.
2621 * RETRY from server is not allowed.
2622 */
2623 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002624 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002625
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002626 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002627 iov->iov_len = _IOC_SIZE(cmd);
2628
2629 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2630 in_iov = iov;
2631 in_iovs = 1;
2632 }
2633
2634 if (_IOC_DIR(cmd) & _IOC_READ) {
2635 out_iov = iov;
2636 out_iovs = 1;
2637 }
2638 }
2639
2640 retry:
2641 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2642 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2643
2644 /*
2645 * Out data can be used either for actual out data or iovs,
2646 * make sure there always is at least one page.
2647 */
2648 out_size = max_t(size_t, out_size, PAGE_SIZE);
2649 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2650
2651 /* make sure there are enough buffer pages and init request with them */
2652 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002653 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002654 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002655 while (ap.num_pages < max_pages) {
2656 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2657 if (!ap.pages[ap.num_pages])
Tejun Heo59efec72008-11-26 12:03:55 +01002658 goto out;
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002659 ap.num_pages++;
Tejun Heo59efec72008-11-26 12:03:55 +01002660 }
2661
Tejun Heo59efec72008-11-26 12:03:55 +01002662
2663 /* okay, let's send it to the client */
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002664 ap.args.opcode = FUSE_IOCTL;
2665 ap.args.nodeid = ff->nodeid;
2666 ap.args.in_numargs = 1;
2667 ap.args.in_args[0].size = sizeof(inarg);
2668 ap.args.in_args[0].value = &inarg;
Tejun Heo59efec72008-11-26 12:03:55 +01002669 if (in_size) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002670 ap.args.in_numargs++;
2671 ap.args.in_args[1].size = in_size;
2672 ap.args.in_pages = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002673
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002674 err = -EFAULT;
2675 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002676 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2677 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002678 if (c != PAGE_SIZE && iov_iter_count(&ii))
2679 goto out;
2680 }
Tejun Heo59efec72008-11-26 12:03:55 +01002681 }
2682
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002683 ap.args.out_numargs = 2;
2684 ap.args.out_args[0].size = sizeof(outarg);
2685 ap.args.out_args[0].value = &outarg;
2686 ap.args.out_args[1].size = out_size;
2687 ap.args.out_pages = true;
2688 ap.args.out_argvar = true;
Tejun Heo59efec72008-11-26 12:03:55 +01002689
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002690 transferred = fuse_simple_request(fc, &ap.args);
2691 err = transferred;
2692 if (transferred < 0)
Tejun Heo59efec72008-11-26 12:03:55 +01002693 goto out;
2694
2695 /* did it ask for retry? */
2696 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002697 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002698
2699 /* no retry if in restricted mode */
2700 err = -EIO;
2701 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2702 goto out;
2703
2704 in_iovs = outarg.in_iovs;
2705 out_iovs = outarg.out_iovs;
2706
2707 /*
2708 * Make sure things are in boundary, separate checks
2709 * are to protect against overflow.
2710 */
2711 err = -ENOMEM;
2712 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2713 out_iovs > FUSE_IOCTL_MAX_IOV ||
2714 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2715 goto out;
2716
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002717 vaddr = kmap_atomic(ap.pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002718 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002719 transferred, in_iovs + out_iovs,
2720 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002721 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002722 if (err)
2723 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002724
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002725 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002726 out_iov = in_iov + in_iovs;
2727
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002728 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002729 if (err)
2730 goto out;
2731
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002732 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002733 if (err)
2734 goto out;
2735
Tejun Heo59efec72008-11-26 12:03:55 +01002736 goto retry;
2737 }
2738
2739 err = -EIO;
2740 if (transferred > inarg.out_size)
2741 goto out;
2742
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002743 err = -EFAULT;
2744 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002745 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2746 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002747 if (c != PAGE_SIZE && iov_iter_count(&ii))
2748 goto out;
2749 }
2750 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002751 out:
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002752 free_page((unsigned long) iov_page);
Miklos Szeredi093f38a2019-09-10 15:04:09 +02002753 while (ap.num_pages)
2754 __free_page(ap.pages[--ap.num_pages]);
2755 kfree(ap.pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002756
2757 return err ? err : outarg.result;
2758}
Tejun Heo08cbf542009-04-14 10:54:53 +09002759EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002760
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002761long fuse_ioctl_common(struct file *file, unsigned int cmd,
2762 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002763{
Al Viro6131ffa2013-02-27 16:59:05 -05002764 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002765 struct fuse_conn *fc = get_fuse_conn(inode);
2766
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002767 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002768 return -EACCES;
2769
2770 if (is_bad_inode(inode))
2771 return -EIO;
2772
2773 return fuse_do_ioctl(file, cmd, arg, flags);
2774}
2775
Tejun Heo59efec72008-11-26 12:03:55 +01002776static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2777 unsigned long arg)
2778{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002779 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002780}
2781
2782static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2783 unsigned long arg)
2784{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002785 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002786}
2787
Tejun Heo95668a62008-11-26 12:03:55 +01002788/*
2789 * All files which have been polled are linked to RB tree
2790 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2791 * find the matching one.
2792 */
2793static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2794 struct rb_node **parent_out)
2795{
2796 struct rb_node **link = &fc->polled_files.rb_node;
2797 struct rb_node *last = NULL;
2798
2799 while (*link) {
2800 struct fuse_file *ff;
2801
2802 last = *link;
2803 ff = rb_entry(last, struct fuse_file, polled_node);
2804
2805 if (kh < ff->kh)
2806 link = &last->rb_left;
2807 else if (kh > ff->kh)
2808 link = &last->rb_right;
2809 else
2810 return link;
2811 }
2812
2813 if (parent_out)
2814 *parent_out = last;
2815 return link;
2816}
2817
2818/*
2819 * The file is about to be polled. Make sure it's on the polled_files
2820 * RB tree. Note that files once added to the polled_files tree are
2821 * not removed before the file is released. This is because a file
2822 * polled once is likely to be polled again.
2823 */
2824static void fuse_register_polled_file(struct fuse_conn *fc,
2825 struct fuse_file *ff)
2826{
2827 spin_lock(&fc->lock);
2828 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002829 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002830
2831 link = fuse_find_polled_node(fc, ff->kh, &parent);
2832 BUG_ON(*link);
2833 rb_link_node(&ff->polled_node, parent, link);
2834 rb_insert_color(&ff->polled_node, &fc->polled_files);
2835 }
2836 spin_unlock(&fc->lock);
2837}
2838
Al Viro076ccb72017-07-03 01:02:18 -04002839__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002840{
Tejun Heo95668a62008-11-26 12:03:55 +01002841 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002842 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002843 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2844 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002845 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002846 int err;
2847
2848 if (fc->no_poll)
2849 return DEFAULT_POLLMASK;
2850
2851 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002852 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002853
2854 /*
2855 * Ask for notification iff there's someone waiting for it.
2856 * The client may ignore the flag and always notify.
2857 */
2858 if (waitqueue_active(&ff->poll_wait)) {
2859 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2860 fuse_register_polled_file(fc, ff);
2861 }
2862
Miklos Szeredid5b48542019-09-10 15:04:08 +02002863 args.opcode = FUSE_POLL;
2864 args.nodeid = ff->nodeid;
2865 args.in_numargs = 1;
2866 args.in_args[0].size = sizeof(inarg);
2867 args.in_args[0].value = &inarg;
2868 args.out_numargs = 1;
2869 args.out_args[0].size = sizeof(outarg);
2870 args.out_args[0].value = &outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002871 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002872
2873 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002874 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002875 if (err == -ENOSYS) {
2876 fc->no_poll = 1;
2877 return DEFAULT_POLLMASK;
2878 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002879 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002880}
Tejun Heo08cbf542009-04-14 10:54:53 +09002881EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002882
2883/*
2884 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2885 * wakes up the poll waiters.
2886 */
2887int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2888 struct fuse_notify_poll_wakeup_out *outarg)
2889{
2890 u64 kh = outarg->kh;
2891 struct rb_node **link;
2892
2893 spin_lock(&fc->lock);
2894
2895 link = fuse_find_polled_node(fc, kh, NULL);
2896 if (*link) {
2897 struct fuse_file *ff;
2898
2899 ff = rb_entry(*link, struct fuse_file, polled_node);
2900 wake_up_interruptible_sync(&ff->poll_wait);
2901 }
2902
2903 spin_unlock(&fc->lock);
2904 return 0;
2905}
2906
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002907static void fuse_do_truncate(struct file *file)
2908{
2909 struct inode *inode = file->f_mapping->host;
2910 struct iattr attr;
2911
2912 attr.ia_valid = ATTR_SIZE;
2913 attr.ia_size = i_size_read(inode);
2914
2915 attr.ia_file = file;
2916 attr.ia_valid |= ATTR_FILE;
2917
Jan Kara62490332016-05-26 17:12:41 +02002918 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002919}
2920
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002921static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002922{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002923 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002924}
2925
Anand Avati4273b792012-02-17 12:46:25 -05002926static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002927fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002928{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002929 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002930 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002931 struct file *file = iocb->ki_filp;
2932 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002933 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002934 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002935 struct inode *inode;
2936 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002937 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002938 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002939 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002940
Anand Avati4273b792012-02-17 12:46:25 -05002941 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002942 inode = file->f_mapping->host;
2943 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002944
Omar Sandoval6f673762015-03-16 04:33:52 -07002945 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002946 return 0;
2947
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002948 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002949 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002950 if (offset >= i_size)
2951 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002952 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002953 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002954 }
2955
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002956 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002957 if (!io)
2958 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002959 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002960 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002961 io->reqs = 1;
2962 io->bytes = -1;
2963 io->size = 0;
2964 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002965 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002966 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002967 /*
2968 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002969 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002970 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002971 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002972 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302973 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002974
2975 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302976 * We cannot asynchronously extend the size of a file.
2977 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002978 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302979 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2980 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002981
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302982 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002983 /*
2984 * Additional reference to keep io around after
2985 * calling fuse_aio_complete()
2986 */
2987 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002988 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002989 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002990
Omar Sandoval6f673762015-03-16 04:33:52 -07002991 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002992 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002993 fuse_invalidate_attr(inode);
2994 } else {
Al Virod22a9432014-03-16 15:50:47 -04002995 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002996 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002997
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002998 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002999 bool blocking = io->blocking;
3000
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003001 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3002
3003 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01003004 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003005 return -EIOCBQUEUED;
3006
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003007 wait_for_completion(&wait);
3008 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04003009 }
3010
Seth Forshee744742d2016-03-11 10:35:34 -06003011 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01003012
Omar Sandoval6f673762015-03-16 04:33:52 -07003013 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04003014 if (ret > 0)
3015 fuse_write_update_size(inode, pos);
3016 else if (ret < 0 && offset + count > i_size)
3017 fuse_do_truncate(file);
3018 }
Anand Avati4273b792012-02-17 12:46:25 -05003019
3020 return ret;
3021}
3022
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003023static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3024{
3025 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3026
3027 if (!err)
3028 fuse_sync_writes(inode);
3029
3030 return err;
3031}
3032
Miklos Szeredicdadb112012-11-10 16:55:56 +01003033static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3034 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003035{
3036 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01003037 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003038 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003039 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01003040 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003041 struct fuse_fallocate_in inarg = {
3042 .fh = ff->fh,
3043 .offset = offset,
3044 .length = length,
3045 .mode = mode
3046 };
3047 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04003048 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3049 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003050
Miklos Szeredi4adb8302014-04-28 14:19:21 +02003051 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3052 return -EOPNOTSUPP;
3053
Miklos Szeredi519c6042012-04-26 10:56:36 +02003054 if (fc->no_fallocate)
3055 return -EOPNOTSUPP;
3056
Maxim Patlasov14c14412013-06-13 12:16:39 +04003057 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05003058 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003059 if (mode & FALLOC_FL_PUNCH_HOLE) {
3060 loff_t endbyte = offset + length - 1;
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003061
3062 err = fuse_writeback_range(inode, offset, endbyte);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003063 if (err)
3064 goto out;
Maxim Patlasovbde52782013-09-13 19:19:54 +04003065 }
Brian Foster3634a632013-05-17 09:30:32 -04003066 }
3067
Liu Bo0cbade02019-04-18 04:04:41 +08003068 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3069 offset + length > i_size_read(inode)) {
3070 err = inode_newsize_ok(inode, offset + length);
3071 if (err)
Miklos Szeredi35d6fcb2019-05-27 11:42:07 +02003072 goto out;
Liu Bo0cbade02019-04-18 04:04:41 +08003073 }
3074
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003075 if (!(mode & FALLOC_FL_KEEP_SIZE))
3076 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3077
Miklos Szeredid5b48542019-09-10 15:04:08 +02003078 args.opcode = FUSE_FALLOCATE;
3079 args.nodeid = ff->nodeid;
3080 args.in_numargs = 1;
3081 args.in_args[0].size = sizeof(inarg);
3082 args.in_args[0].value = &inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01003083 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003084 if (err == -ENOSYS) {
3085 fc->no_fallocate = 1;
3086 err = -EOPNOTSUPP;
3087 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003088 if (err)
3089 goto out;
3090
3091 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003092 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3093 bool changed = fuse_write_update_size(inode, offset + length);
3094
Miklos Szeredi93d22692014-04-28 14:19:22 +02003095 if (changed && fc->writeback_cache)
3096 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003097 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003098
3099 if (mode & FALLOC_FL_PUNCH_HOLE)
3100 truncate_pagecache_range(inode, offset, offset + length - 1);
3101
3102 fuse_invalidate_attr(inode);
3103
Brian Foster3634a632013-05-17 09:30:32 -04003104out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003105 if (!(mode & FALLOC_FL_KEEP_SIZE))
3106 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3107
Maxim Patlasovbde52782013-09-13 19:19:54 +04003108 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003109 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003110
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003111 return err;
3112}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003113
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003114static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3115 struct file *file_out, loff_t pos_out,
3116 size_t len, unsigned int flags)
Niels de Vos88bc7d52018-08-21 14:36:31 +02003117{
3118 struct fuse_file *ff_in = file_in->private_data;
3119 struct fuse_file *ff_out = file_out->private_data;
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003120 struct inode *inode_in = file_inode(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003121 struct inode *inode_out = file_inode(file_out);
3122 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3123 struct fuse_conn *fc = ff_in->fc;
3124 FUSE_ARGS(args);
3125 struct fuse_copy_file_range_in inarg = {
3126 .fh_in = ff_in->fh,
3127 .off_in = pos_in,
3128 .nodeid_out = ff_out->nodeid,
3129 .fh_out = ff_out->fh,
3130 .off_out = pos_out,
3131 .len = len,
3132 .flags = flags
3133 };
3134 struct fuse_write_out outarg;
3135 ssize_t err;
3136 /* mark unstable when write-back is not used, and file_out gets
3137 * extended */
3138 bool is_unstable = (!fc->writeback_cache) &&
3139 ((pos_out + len) > inode_out->i_size);
3140
3141 if (fc->no_copy_file_range)
3142 return -EOPNOTSUPP;
3143
Amir Goldstein5dae2222019-06-05 08:04:50 -07003144 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3145 return -EXDEV;
3146
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003147 if (fc->writeback_cache) {
3148 inode_lock(inode_in);
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003149 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
Miklos Szeredia2bc9232019-05-28 13:22:50 +02003150 inode_unlock(inode_in);
3151 if (err)
3152 return err;
3153 }
3154
Niels de Vos88bc7d52018-08-21 14:36:31 +02003155 inode_lock(inode_out);
3156
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003157 err = file_modified(file_out);
3158 if (err)
3159 goto out;
3160
Niels de Vos88bc7d52018-08-21 14:36:31 +02003161 if (fc->writeback_cache) {
Miklos Szeredi26eb3ba2019-05-28 13:22:50 +02003162 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003163 if (err)
3164 goto out;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003165 }
3166
3167 if (is_unstable)
3168 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3169
Miklos Szeredid5b48542019-09-10 15:04:08 +02003170 args.opcode = FUSE_COPY_FILE_RANGE;
3171 args.nodeid = ff_in->nodeid;
3172 args.in_numargs = 1;
3173 args.in_args[0].size = sizeof(inarg);
3174 args.in_args[0].value = &inarg;
3175 args.out_numargs = 1;
3176 args.out_args[0].size = sizeof(outarg);
3177 args.out_args[0].value = &outarg;
Niels de Vos88bc7d52018-08-21 14:36:31 +02003178 err = fuse_simple_request(fc, &args);
3179 if (err == -ENOSYS) {
3180 fc->no_copy_file_range = 1;
3181 err = -EOPNOTSUPP;
3182 }
3183 if (err)
3184 goto out;
3185
3186 if (fc->writeback_cache) {
3187 fuse_write_update_size(inode_out, pos_out + outarg.size);
3188 file_update_time(file_out);
3189 }
3190
3191 fuse_invalidate_attr(inode_out);
3192
3193 err = outarg.size;
3194out:
3195 if (is_unstable)
3196 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3197
3198 inode_unlock(inode_out);
Amir Goldsteinfe0da9c2019-06-05 08:04:51 -07003199 file_accessed(file_in);
Niels de Vos88bc7d52018-08-21 14:36:31 +02003200
3201 return err;
3202}
3203
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003204static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3205 struct file *dst_file, loff_t dst_off,
3206 size_t len, unsigned int flags)
3207{
3208 ssize_t ret;
3209
3210 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3211 len, flags);
3212
Amir Goldstein5dae2222019-06-05 08:04:50 -07003213 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07003214 ret = generic_copy_file_range(src_file, src_off, dst_file,
3215 dst_off, len, flags);
3216 return ret;
3217}
3218
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003219static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003220 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003221 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003222 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003223 .mmap = fuse_file_mmap,
3224 .open = fuse_open,
3225 .flush = fuse_flush,
3226 .release = fuse_release,
3227 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003228 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003229 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003230 .splice_read = generic_file_splice_read,
Miklos Szeredi3c3db092019-01-24 10:40:17 +01003231 .splice_write = iter_file_splice_write,
Tejun Heo59efec72008-11-26 12:03:55 +01003232 .unlocked_ioctl = fuse_file_ioctl,
3233 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003234 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003235 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003236 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003237};
3238
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003239static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003240 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003241 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003242 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003243 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003244 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003245 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003246 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003247 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003248 .write_begin = fuse_write_begin,
3249 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003250};
3251
3252void fuse_init_file_inode(struct inode *inode)
3253{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003254 struct fuse_inode *fi = get_fuse_inode(inode);
3255
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003256 inode->i_fop = &fuse_file_operations;
3257 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003258
3259 INIT_LIST_HEAD(&fi->write_files);
3260 INIT_LIST_HEAD(&fi->queued_writes);
3261 fi->writectr = 0;
3262 init_waitqueue_head(&fi->page_waitq);
3263 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003264}