blob: b1ee8b7a5a9b026cbd134d0e2aa785958c833898 [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"
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053010#include "fuse_passthrough.h"
Miklos Szeredib6aeade2005-09-09 13:10:30 -070011
12#include <linux/pagemap.h>
13#include <linux/slab.h>
14#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090016#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010017#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020018#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080022static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070023
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020024static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053025 int opcode, struct fuse_open_out *outargp,
26 struct file **passthrough_filpp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070027{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053028 int ret_val;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070029 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010030 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080031
32 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070033 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
34 if (!fc->atomic_o_trunc)
35 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010036 args.in.h.opcode = opcode;
37 args.in.h.nodeid = nodeid;
38 args.in.numargs = 1;
39 args.in.args[0].size = sizeof(inarg);
40 args.in.args[0].value = &inarg;
41 args.out.numargs = 1;
42 args.out.args[0].size = sizeof(*outargp);
43 args.out.args[0].value = outargp;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053044 args.out.passthrough_filp = NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080045
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053046 ret_val = fuse_simple_request(fc, &args);
47
48 if (args.out.passthrough_filp != NULL)
49 *passthrough_filpp = args.out.passthrough_filp;
50
51 return ret_val;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052}
53
Tejun Heoacf99432008-11-26 12:03:55 +010054struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080055{
56 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090057
Mateusz Jurczyk227559e2017-06-07 12:26:49 +020058 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090059 if (unlikely(!ff))
60 return NULL;
61
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053062 ff->passthrough_filp = NULL;
63 ff->passthrough_enabled = 0;
64 if (fc->passthrough)
65 ff->passthrough_enabled = 1;
66
Miklos Szeredida5e4712009-04-28 16:56:36 +020067 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040068 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090069 if (unlikely(!ff->reserved_req)) {
70 kfree(ff);
71 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080072 }
Tejun Heo6b2db282009-04-14 10:54:49 +090073
74 INIT_LIST_HEAD(&ff->write_entry);
75 atomic_set(&ff->count, 0);
76 RB_CLEAR_NODE(&ff->polled_node);
77 init_waitqueue_head(&ff->poll_wait);
78
79 spin_lock(&fc->lock);
80 ff->kh = ++fc->khctr;
81 spin_unlock(&fc->lock);
82
Miklos Szeredifd72faa2005-11-07 00:59:51 -080083 return ff;
84}
85
86void fuse_file_free(struct fuse_file *ff)
87{
Miklos Szeredi33649c92006-06-25 05:48:52 -070088 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080089 kfree(ff);
90}
91
Miklos Szeredic7b71432009-04-28 16:56:37 +020092struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070093{
94 atomic_inc(&ff->count);
95 return ff;
96}
97
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010098static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
99{
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100100 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100101}
102
103static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700104{
105 if (atomic_dec_and_test(&ff->count)) {
106 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200107
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100108 if (ff->fc->no_open) {
109 /*
110 * Drop the release request when client does not
111 * implement 'open'
112 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200113 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100114 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100115 fuse_put_request(ff->fc, req);
116 } else if (sync) {
Miklos Szeredi1e6be9c2017-02-22 20:08:25 +0100117 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200118 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100119 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100120 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100121 fuse_put_request(ff->fc, req);
122 } else {
123 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200124 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100125 fuse_request_send_background(ff->fc, req);
126 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700127 kfree(ff);
128 }
129}
130
Tejun Heo08cbf542009-04-14 10:54:53 +0900131int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
132 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200133{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200134 struct fuse_file *ff;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530135 struct file *passthrough_filp = NULL;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200136 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
137
138 ff = fuse_file_alloc(fc);
139 if (!ff)
140 return -ENOMEM;
141
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100142 ff->fh = 0;
143 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
144 if (!fc->no_open || isdir) {
145 struct fuse_open_out outarg;
146 int err;
147
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530148 err = fuse_send_open(fc, nodeid, file, opcode, &outarg,
149 &(passthrough_filp));
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100150 if (!err) {
151 ff->fh = outarg.fh;
152 ff->open_flags = outarg.open_flags;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530153 ff->passthrough_filp = passthrough_filp;
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100154 } else if (err != -ENOSYS || isdir) {
155 fuse_file_free(ff);
156 return err;
157 } else {
158 fc->no_open = 1;
159 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200160 }
161
162 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100163 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200164
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200165 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200166 file->private_data = fuse_file_get(ff);
167
168 return 0;
169}
Tejun Heo08cbf542009-04-14 10:54:53 +0900170EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200171
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400172static void fuse_link_write_file(struct file *file)
173{
174 struct inode *inode = file_inode(file);
175 struct fuse_conn *fc = get_fuse_conn(inode);
176 struct fuse_inode *fi = get_fuse_inode(inode);
177 struct fuse_file *ff = file->private_data;
178 /*
179 * file may be written through mmap, so chain it onto the
180 * inodes's write_file list
181 */
182 spin_lock(&fc->lock);
183 if (list_empty(&ff->write_entry))
184 list_add(&ff->write_entry, &fi->write_files);
185 spin_unlock(&fc->lock);
186}
187
Miklos Szeredic7b71432009-04-28 16:56:37 +0200188void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800189{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200190 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800191 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200192
193 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800194 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200195 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700196 invalidate_inode_pages2(inode->i_mapping);
Kirill Smelkovcfd8d2e2019-04-24 07:13:57 +0000197 if (ff->open_flags & FOPEN_STREAM)
198 stream_open(inode, file);
199 else if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200200 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800201 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 struct fuse_inode *fi = get_fuse_inode(inode);
203
204 spin_lock(&fc->lock);
205 fi->attr_version = ++fc->attr_version;
206 i_size_write(inode, 0);
207 spin_unlock(&fc->lock);
208 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200209 if (fc->writeback_cache)
210 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800211 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400212 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
213 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800214}
215
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200216int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800217{
Tejun Heoacf99432008-11-26 12:03:55 +0100218 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219 int err;
Miklos Szeredi709c37e2019-10-23 14:26:37 +0200220 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200221 fc->atomic_o_trunc &&
222 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700223
224 err = generic_file_open(inode, file);
225 if (err)
226 return err;
227
Miklos Szeredi709c37e2019-10-23 14:26:37 +0200228 if (is_wb_truncate) {
Al Viro59551022016-01-22 15:40:57 -0500229 inode_lock(inode);
Miklos Szeredi709c37e2019-10-23 14:26:37 +0200230 fuse_set_nowrite(inode);
231 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200232
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200233 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200235 if (!err)
236 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200237
Miklos Szeredi709c37e2019-10-23 14:26:37 +0200238 if (is_wb_truncate) {
239 fuse_release_nowrite(inode);
Al Viro59551022016-01-22 15:40:57 -0500240 inode_unlock(inode);
Miklos Szeredi709c37e2019-10-23 14:26:37 +0200241 }
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200242
243 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244}
245
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200246static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800247{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200248 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700249 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800250 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700251
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200252 spin_lock(&fc->lock);
253 list_del(&ff->write_entry);
254 if (!RB_EMPTY_NODE(&ff->polled_node))
255 rb_erase(&ff->polled_node, &fc->polled_files);
256 spin_unlock(&fc->lock);
257
Bryan Green357ccf22011-03-01 16:43:52 -0800258 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200259
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700260 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800261 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700262 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200263 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700264 req->in.numargs = 1;
265 req->in.args[0].size = sizeof(struct fuse_release_in);
266 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800267}
268
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200269void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800270{
Tejun Heo6b2db282009-04-14 10:54:49 +0900271 struct fuse_file *ff;
272 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700273
Tejun Heo6b2db282009-04-14 10:54:49 +0900274 ff = file->private_data;
275 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200276 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700277
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530278 fuse_passthrough_release(ff);
279
Tejun Heo6b2db282009-04-14 10:54:49 +0900280 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200281 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100282
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200283 if (ff->flock) {
284 struct fuse_release_in *inarg = &req->misc.release.in;
285 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
286 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
287 (fl_owner_t) file);
288 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100289 /* Hold inode until release is finished */
290 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700291
Tejun Heo6b2db282009-04-14 10:54:49 +0900292 /*
293 * Normally this will send the RELEASE request, however if
294 * some asynchronous READ or WRITE requests are outstanding,
295 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100296 *
297 * Make the release synchronous if this is a fuseblk mount,
298 * synchronous RELEASE is allowed (and desirable) in this case
299 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900300 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100301 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700302}
303
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700304static int fuse_open(struct inode *inode, struct file *file)
305{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200306 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307}
308
309static int fuse_release(struct inode *inode, struct file *file)
310{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400311 struct fuse_conn *fc = get_fuse_conn(inode);
312
313 /* see fuse_vma_close() for !writeback_cache case */
314 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200315 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400316
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200317 fuse_release_common(file, FUSE_RELEASE);
318
319 /* return value is ignored by VFS */
320 return 0;
321}
322
323void fuse_sync_release(struct fuse_file *ff, int flags)
324{
325 WARN_ON(atomic_read(&ff->count) > 1);
326 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200327 __set_bit(FR_FORCE, &ff->reserved_req->flags);
328 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200329 fuse_request_send(ff->fc, ff->reserved_req);
330 fuse_put_request(ff->fc, ff->reserved_req);
331 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700332}
Tejun Heo08cbf542009-04-14 10:54:53 +0900333EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700334
Miklos Szeredi71421252006-06-25 05:48:52 -0700335/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700336 * Scramble the ID space with XTEA, so that the value of the files_struct
337 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700338 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700339u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700340{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700341 u32 *k = fc->scramble_key;
342 u64 v = (unsigned long) id;
343 u32 v0 = v;
344 u32 v1 = v >> 32;
345 u32 sum = 0;
346 int i;
347
348 for (i = 0; i < 32; i++) {
349 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
350 sum += 0x9E3779B9;
351 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
352 }
353
354 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700355}
356
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700357/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400358 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700359 *
360 * This is currently done by walking the list of writepage requests
361 * for the inode, which can be pretty inefficient.
362 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400363static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
364 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700365{
366 struct fuse_conn *fc = get_fuse_conn(inode);
367 struct fuse_inode *fi = get_fuse_inode(inode);
368 struct fuse_req *req;
369 bool found = false;
370
371 spin_lock(&fc->lock);
372 list_for_each_entry(req, &fi->writepages, writepages_entry) {
373 pgoff_t curr_index;
374
375 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300376 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400377 if (idx_from < curr_index + req->num_pages &&
378 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700379 found = true;
380 break;
381 }
382 }
383 spin_unlock(&fc->lock);
384
385 return found;
386}
387
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400388static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
389{
390 return fuse_range_is_writeback(inode, index, index);
391}
392
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700393/*
394 * Wait for page writeback to be completed.
395 *
396 * Since fuse doesn't rely on the VM writeback tracking, this has to
397 * use some other means.
398 */
399static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
400{
401 struct fuse_inode *fi = get_fuse_inode(inode);
402
403 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
404 return 0;
405}
406
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400407/*
408 * Wait for all pending writepages on the inode to finish.
409 *
410 * This is currently done by blocking further writes with FUSE_NOWRITE
411 * and waiting for all sent writes to complete.
412 *
413 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
414 * could conflict with truncation.
415 */
416static void fuse_sync_writes(struct inode *inode)
417{
418 fuse_set_nowrite(inode);
419 fuse_release_nowrite(inode);
420}
421
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700422static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423{
Al Viro6131ffa2013-02-27 16:59:05 -0500424 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 struct fuse_conn *fc = get_fuse_conn(inode);
426 struct fuse_file *ff = file->private_data;
427 struct fuse_req *req;
428 struct fuse_flush_in inarg;
429 int err;
430
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800431 if (is_bad_inode(inode))
432 return -EIO;
433
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 if (fc->no_flush)
435 return 0;
436
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200437 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400438 if (err)
439 return err;
440
Al Viro59551022016-01-22 15:40:57 -0500441 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400442 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500443 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400444
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200445 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700446 if (err)
447 return err;
448
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400449 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700450 memset(&inarg, 0, sizeof(inarg));
451 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700452 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700453 req->in.h.opcode = FUSE_FLUSH;
454 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700455 req->in.numargs = 1;
456 req->in.args[0].size = sizeof(inarg);
457 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200458 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100459 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460 err = req->out.h.error;
461 fuse_put_request(fc, req);
462 if (err == -ENOSYS) {
463 fc->no_flush = 1;
464 err = 0;
465 }
466 return err;
467}
468
Josef Bacik02c24a82011-07-16 20:44:56 -0400469int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
470 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200472 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700473 struct fuse_conn *fc = get_fuse_conn(inode);
474 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100475 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700476 struct fuse_fsync_in inarg;
477 int err;
478
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800479 if (is_bad_inode(inode))
480 return -EIO;
481
Al Viro59551022016-01-22 15:40:57 -0500482 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400483
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700484 /*
485 * Start writeback against all dirty pages of the inode, then
486 * wait for all outstanding writes, before sending the FSYNC
487 * request.
488 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200489 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700490 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400491 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492
493 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700494
495 /*
496 * Due to implementation of fuse writeback
497 * filemap_write_and_wait_range() does not catch errors.
498 * We have to do this directly after fuse_sync_writes()
499 */
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200500 err = filemap_check_errors(file->f_mapping);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700501 if (err)
502 goto out;
503
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200504 err = sync_inode_metadata(inode, 1);
505 if (err)
506 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700507
Miklos Szeredi22401e72014-04-28 14:19:23 +0200508 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
509 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400510
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 memset(&inarg, 0, sizeof(inarg));
512 inarg.fh = ff->fh;
513 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100514 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
515 args.in.h.nodeid = get_node_id(inode);
516 args.in.numargs = 1;
517 args.in.args[0].size = sizeof(inarg);
518 args.in.args[0].value = &inarg;
519 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700520 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700521 if (isdir)
522 fc->no_fsyncdir = 1;
523 else
524 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 err = 0;
526 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400527out:
Al Viro59551022016-01-22 15:40:57 -0500528 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 return err;
530}
531
Josef Bacik02c24a82011-07-16 20:44:56 -0400532static int fuse_fsync(struct file *file, loff_t start, loff_t end,
533 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700534{
Josef Bacik02c24a82011-07-16 20:44:56 -0400535 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700536}
537
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200538void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
539 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700540{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700541 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800542 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700543
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800544 inarg->fh = ff->fh;
545 inarg->offset = pos;
546 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800547 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800548 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200549 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700550 req->in.numargs = 1;
551 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800552 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700553 req->out.argvar = 1;
554 req->out.numargs = 1;
555 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700556}
557
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200558static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400559{
560 unsigned i;
561
562 for (i = 0; i < req->num_pages; i++) {
563 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200564 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400565 set_page_dirty_lock(page);
566 put_page(page);
567 }
568}
569
Seth Forshee744742d2016-03-11 10:35:34 -0600570static void fuse_io_release(struct kref *kref)
571{
572 kfree(container_of(kref, struct fuse_io_priv, refcnt));
573}
574
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100575static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
576{
577 if (io->err)
578 return io->err;
579
580 if (io->bytes >= 0 && io->write)
581 return -EIO;
582
583 return io->bytes < 0 ? io->size : io->bytes;
584}
585
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400586/**
587 * In case of short read, the caller sets 'pos' to the position of
588 * actual end of fuse request in IO request. Otherwise, if bytes_requested
589 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
590 *
591 * An example:
592 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
593 * both submitted asynchronously. The first of them was ACKed by userspace as
594 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
595 * second request was ACKed as short, e.g. only 1K was read, resulting in
596 * pos == 33K.
597 *
598 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
599 * will be equal to the length of the longest contiguous fragment of
600 * transferred data starting from the beginning of IO request.
601 */
602static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
603{
604 int left;
605
606 spin_lock(&io->lock);
607 if (err)
608 io->err = io->err ? : err;
609 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
610 io->bytes = pos;
611
612 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530613 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100614 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400615 spin_unlock(&io->lock);
616
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530617 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100618 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400619
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100620 if (res >= 0) {
621 struct inode *inode = file_inode(io->iocb->ki_filp);
622 struct fuse_conn *fc = get_fuse_conn(inode);
623 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400624
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100625 spin_lock(&fc->lock);
626 fi->attr_version = ++fc->attr_version;
627 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400628 }
629
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100630 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400631 }
Seth Forshee744742d2016-03-11 10:35:34 -0600632
633 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400634}
635
636static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
637{
638 struct fuse_io_priv *io = req->io;
639 ssize_t pos = -1;
640
641 fuse_release_user_pages(req, !io->write);
642
643 if (io->write) {
644 if (req->misc.write.in.size != req->misc.write.out.size)
645 pos = req->misc.write.in.offset - io->offset +
646 req->misc.write.out.size;
647 } else {
648 if (req->misc.read.in.size != req->out.args[0].size)
649 pos = req->misc.read.in.offset - io->offset +
650 req->out.args[0].size;
651 }
652
653 fuse_aio_complete(io, req->out.h.error, pos);
654}
655
656static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
657 size_t num_bytes, struct fuse_io_priv *io)
658{
659 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600660 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400661 io->size += num_bytes;
662 io->reqs++;
663 spin_unlock(&io->lock);
664
665 req->io = io;
666 req->end = fuse_aio_complete_req;
667
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400668 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400669 fuse_request_send_background(fc, req);
670
671 return num_bytes;
672}
673
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400674static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200675 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700676{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400677 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200678 struct fuse_file *ff = file->private_data;
679 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700680
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200681 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700682 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700683 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700684
685 inarg->read_flags |= FUSE_READ_LOCKOWNER;
686 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
687 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400688
689 if (io->async)
690 return fuse_async_req_send(fc, req, count, io);
691
Tejun Heob93f8582008-11-26 12:03:55 +0100692 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800693 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700694}
695
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700696static void fuse_read_update_size(struct inode *inode, loff_t size,
697 u64 attr_ver)
698{
699 struct fuse_conn *fc = get_fuse_conn(inode);
700 struct fuse_inode *fi = get_fuse_inode(inode);
701
702 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400703 if (attr_ver == fi->attr_version && size < inode->i_size &&
704 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700705 fi->attr_version = ++fc->attr_version;
706 i_size_write(inode, size);
707 }
708 spin_unlock(&fc->lock);
709}
710
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400711static void fuse_short_read(struct fuse_req *req, struct inode *inode,
712 u64 attr_ver)
713{
714 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400715 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400716
Pavel Emelyanov83732002013-10-10 17:10:46 +0400717 if (fc->writeback_cache) {
718 /*
719 * A hole in a file. Some data after the hole are in page cache,
720 * but have not reached the client fs yet. So, the hole is not
721 * present there.
722 */
723 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300724 int start_idx = num_read >> PAGE_SHIFT;
725 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400726
727 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300728 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400729 off = 0;
730 }
731 } else {
732 loff_t pos = page_offset(req->pages[0]) + num_read;
733 fuse_read_update_size(inode, pos, attr_ver);
734 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400735}
736
Maxim Patlasov482fce52013-10-10 17:11:25 +0400737static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738{
Seth Forshee744742d2016-03-11 10:35:34 -0600739 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700740 struct inode *inode = page->mapping->host;
741 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800742 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700743 size_t num_read;
744 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300745 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800747 int err;
748
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700749 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300750 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700751 * page-cache page, so make sure we read a properly synced
752 * page.
753 */
754 fuse_wait_on_page_writeback(inode, page->index);
755
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400756 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700757 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400758 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700759
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700760 attr_ver = fuse_get_attr_version(fc);
761
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200763 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700764 req->num_pages = 1;
765 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400766 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400767 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700768 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700769
770 if (!err) {
771 /*
772 * Short read means EOF. If file size is larger, truncate it
773 */
774 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400775 fuse_short_read(req, inode, attr_ver);
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)
818 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200819
Andrew Gallagher451418f2013-11-05 03:55:43 -0800820 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700821 }
822
Miklos Szeredidb50b962005-09-09 13:10:33 -0700823 for (i = 0; i < req->num_pages; i++) {
824 struct page *page = req->pages[i];
825 if (!req->out.h.error)
826 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800827 else
828 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300830 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700831 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700832 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100833 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800834}
835
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200836static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800837{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200838 struct fuse_file *ff = file->private_data;
839 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800840 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300841 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200842
843 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800844 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200845 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200846 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700847 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800848 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700849 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800850 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100851 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800852 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100853 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800854 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100855 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800856 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700857}
858
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700859struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700860 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800861 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700862 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400863 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864};
865
Sami Tolvanen4fd840d2017-08-16 14:38:13 -0700866static int fuse_readpages_fill(struct file *_data, struct page *page)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867{
Sami Tolvanen4fd840d2017-08-16 14:38:13 -0700868 struct fuse_fill_data *data = (struct fuse_fill_data *)_data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 struct fuse_req *req = data->req;
870 struct inode *inode = data->inode;
871 struct fuse_conn *fc = get_fuse_conn(inode);
872
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700873 fuse_wait_on_page_writeback(inode, page->index);
874
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 if (req->num_pages &&
876 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300877 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879 int nr_alloc = min_t(unsigned, data->nr_pages,
880 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200881 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400882 if (fc->async_read)
883 req = fuse_get_req_for_background(fc, nr_alloc);
884 else
885 req = fuse_get_req(fc, nr_alloc);
886
887 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700888 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700889 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700890 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700891 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700892 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400893
894 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai01f07722018-07-19 15:49:39 +0300895 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400896 fuse_put_request(fc, req);
897 return -EIO;
898 }
899
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300900 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700901 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400902 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100903 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400904 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700905 return 0;
906}
907
908static int fuse_readpages(struct file *file, struct address_space *mapping,
909 struct list_head *pages, unsigned nr_pages)
910{
911 struct inode *inode = mapping->host;
912 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700913 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700914 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400915 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800916
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700917 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800918 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800919 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800920
Miklos Szeredia6643092007-11-28 16:22:00 -0800921 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700922 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400923 if (fc->async_read)
924 data.req = fuse_get_req_for_background(fc, nr_alloc);
925 else
926 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400927 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700928 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700929 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800930 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700931
932 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700933 if (!err) {
934 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200935 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700936 else
937 fuse_put_request(fc, data.req);
938 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800939out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700940 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700941}
942
Al Viro37c20f12014-04-02 14:47:09 -0400943static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800944{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530945 ssize_t ret_val;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800946 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);
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530948 struct fuse_file *ff = iocb->ki_filp->private_data;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800949
Brian Fostera8894272012-07-16 15:23:50 -0400950 /*
951 * In auto invalidate mode, always update attributes on read.
952 * Otherwise, only update if we attempt to read past EOF (to ensure
953 * i_size is up to date).
954 */
955 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400956 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800957 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800958 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
959 if (err)
960 return err;
961 }
962
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530963 if (ff && ff->passthrough_enabled && ff->passthrough_filp)
964 ret_val = fuse_passthrough_read_iter(iocb, to);
965 else
966 ret_val = generic_file_read_iter(iocb, to);
967
968 return ret_val;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800969}
970
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200972 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700973{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974 struct fuse_write_in *inarg = &req->misc.write.in;
975 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700976
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700977 inarg->fh = ff->fh;
978 inarg->offset = pos;
979 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700980 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200981 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700982 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200983 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700984 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
985 else
986 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700987 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700988 req->in.args[1].size = count;
989 req->out.numargs = 1;
990 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700991 req->out.args[0].value = outarg;
992}
993
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400994static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200995 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700996{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400997 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200998 struct fuse_file *ff = file->private_data;
999 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001000 struct fuse_write_in *inarg = &req->misc.write.in;
1001
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001002 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001003 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -07001004 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001005 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1006 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1007 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001008
1009 if (io->async)
1010 return fuse_async_req_send(fc, req, count, io);
1011
Tejun Heob93f8582008-11-26 12:03:55 +01001012 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001013 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001014}
1015
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001016bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001017{
1018 struct fuse_conn *fc = get_fuse_conn(inode);
1019 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001020 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001021
1022 spin_lock(&fc->lock);
1023 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001024 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001025 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001026 ret = true;
1027 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001028 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001029
1030 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001031}
1032
Nick Pigginea9b9902008-04-30 00:54:42 -07001033static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1034 struct inode *inode, loff_t pos,
1035 size_t count)
1036{
1037 size_t res;
1038 unsigned offset;
1039 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001040 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001041
1042 for (i = 0; i < req->num_pages; i++)
1043 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1044
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001045 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001046
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001047 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001048 count = res;
1049 for (i = 0; i < req->num_pages; i++) {
1050 struct page *page = req->pages[i];
1051
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001052 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001053 SetPageUptodate(page);
1054
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 if (count > PAGE_SIZE - offset)
1056 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001057 else
1058 count = 0;
1059 offset = 0;
1060
1061 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001063 }
1064
1065 return res;
1066}
1067
1068static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1069 struct address_space *mapping,
1070 struct iov_iter *ii, loff_t pos)
1071{
1072 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 size_t count = 0;
1075 int err;
1076
Miklos Szeredif4975c62009-04-02 14:25:34 +02001077 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001078 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001079
1080 do {
1081 size_t tmp;
1082 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001083 pgoff_t index = pos >> PAGE_SHIFT;
1084 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 iov_iter_count(ii));
1086
1087 bytes = min_t(size_t, bytes, fc->max_write - count);
1088
1089 again:
1090 err = -EFAULT;
1091 if (iov_iter_fault_in_readable(ii, bytes))
1092 break;
1093
1094 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001095 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 if (!page)
1097 break;
1098
anfei zhou931e80e2010-02-02 13:44:02 -08001099 if (mapping_writably_mapped(mapping))
1100 flush_dcache_page(page);
1101
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001103 flush_dcache_page(page);
1104
Roman Gushchin3ca81382015-10-12 16:33:44 +03001105 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001106 if (!tmp) {
1107 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001108 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 bytes = min(bytes, iov_iter_single_seg_count(ii));
1110 goto again;
1111 }
1112
1113 err = 0;
1114 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001115 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001116 req->num_pages++;
1117
Nick Pigginea9b9902008-04-30 00:54:42 -07001118 count += tmp;
1119 pos += tmp;
1120 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001121 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001122 offset = 0;
1123
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001124 if (!fc->big_writes)
1125 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001126 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001127 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001128
1129 return count > 0 ? count : err;
1130}
1131
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001132static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1133{
1134 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001135 ((pos + len - 1) >> PAGE_SHIFT) -
1136 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001137 FUSE_MAX_PAGES_PER_REQ);
1138}
1139
Nick Pigginea9b9902008-04-30 00:54:42 -07001140static ssize_t fuse_perform_write(struct file *file,
1141 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
1150 if (is_bad_inode(inode))
1151 return -EIO;
1152
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001153 if (inode->i_size < pos + iov_iter_count(ii))
1154 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1155
Nick Pigginea9b9902008-04-30 00:54:42 -07001156 do {
1157 struct fuse_req *req;
1158 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001159 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001160
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001161 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001162 if (IS_ERR(req)) {
1163 err = PTR_ERR(req);
1164 break;
1165 }
1166
1167 count = fuse_fill_write_pages(req, mapping, ii, pos);
1168 if (count <= 0) {
1169 err = count;
1170 } else {
1171 size_t num_written;
1172
1173 num_written = fuse_send_write_pages(req, file, inode,
1174 pos, count);
1175 err = req->out.h.error;
1176 if (!err) {
1177 res += num_written;
1178 pos += num_written;
1179
1180 /* break out of the loop on short write */
1181 if (num_written != count)
1182 err = -EIO;
1183 }
1184 }
1185 fuse_put_request(fc, req);
1186 } while (!err && iov_iter_count(ii));
1187
1188 if (res > 0)
1189 fuse_write_update_size(inode, pos);
1190
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001191 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001192 fuse_invalidate_attr(inode);
1193
1194 return res > 0 ? res : err;
1195}
1196
Al Viro84c3d552014-04-03 14:33:23 -04001197static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001198{
1199 struct file *file = iocb->ki_filp;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301200 struct fuse_file *ff = file->private_data;
Nick Pigginea9b9902008-04-30 00:54:42 -07001201 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001202 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001203 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001204 struct inode *inode = mapping->host;
1205 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001206 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001207
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001208 if (get_fuse_conn(inode)->writeback_cache) {
1209 /* Update size (EOF optimization) and mode (SUID clearing) */
1210 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1211 if (err)
1212 return err;
1213
Al Viro84c3d552014-04-03 14:33:23 -04001214 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001215 }
1216
Al Viro59551022016-01-22 15:40:57 -05001217 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001218
1219 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001220 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001221
Al Viro3309dd02015-04-09 12:55:47 -04001222 err = generic_write_checks(iocb, from);
1223 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001224 goto out;
1225
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001226 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001227 if (err)
1228 goto out;
1229
Josef Bacikc3b2da32012-03-26 09:59:21 -04001230 err = file_update_time(file);
1231 if (err)
1232 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001233
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301234 if (ff && ff->passthrough_enabled && ff->passthrough_filp) {
1235 written = fuse_passthrough_write_iter(iocb, from);
1236 goto out;
1237 }
1238
Al Viro2ba48ce2015-04-09 13:52:01 -04001239 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001240 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001241 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001242 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001243 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001244
Anand Avati4273b792012-02-17 12:46:25 -05001245 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001246
Al Viro84c3d552014-04-03 14:33:23 -04001247 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001248 if (written_buffered < 0) {
1249 err = written_buffered;
1250 goto out;
1251 }
1252 endbyte = pos + written_buffered - 1;
1253
1254 err = filemap_write_and_wait_range(file->f_mapping, pos,
1255 endbyte);
1256 if (err)
1257 goto out;
1258
1259 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001260 pos >> PAGE_SHIFT,
1261 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001262
1263 written += written_buffered;
1264 iocb->ki_pos = pos + written_buffered;
1265 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001266 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001267 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001268 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001269 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001270out:
1271 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001272 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001273
1274 return written ? written : err;
1275}
1276
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001277static inline void fuse_page_descs_length_init(struct fuse_req *req,
1278 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001279{
1280 int i;
1281
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001282 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001283 req->page_descs[i].length = PAGE_SIZE -
1284 req->page_descs[i].offset;
1285}
1286
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001287static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1288{
1289 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1290}
1291
1292static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1293 size_t max_size)
1294{
1295 return min(iov_iter_single_seg_count(ii), max_size);
1296}
1297
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001298static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001299 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001300{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001301 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001302 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001303
Miklos Szeredif4975c62009-04-02 14:25:34 +02001304 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001305 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306 unsigned long user_addr = fuse_get_user_addr(ii);
1307 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1308
Miklos Szeredif4975c62009-04-02 14:25:34 +02001309 if (write)
1310 req->in.args[1].value = (void *) user_addr;
1311 else
1312 req->out.args[0].value = (void *) user_addr;
1313
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001314 iov_iter_advance(ii, frag_size);
1315 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001316 return 0;
1317 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001318
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001319 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001320 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001321 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001322 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001323 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001324 req->max_pages - req->num_pages,
1325 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001326 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001327 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001328
Al Viroc9c37e22014-03-16 16:08:30 -04001329 iov_iter_advance(ii, ret);
1330 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001331
Al Viroc9c37e22014-03-16 16:08:30 -04001332 ret += start;
1333 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1334
1335 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001336 fuse_page_descs_length_init(req, req->num_pages, npages);
1337
1338 req->num_pages += npages;
1339 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001340 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001341 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001342
1343 if (write)
1344 req->in.argpages = 1;
1345 else
1346 req->out.argpages = 1;
1347
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001348 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001349
Ashish Samant2c932d42016-03-25 10:53:41 -07001350 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001351}
1352
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001353static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1354{
Al Virof67da302014-03-19 01:16:16 -04001355 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001356}
1357
Al Virod22a9432014-03-16 15:50:47 -04001358ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1359 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001361 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001362 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001363 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001364 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001365 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001366 struct fuse_file *ff = file->private_data;
1367 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 size_t nmax = write ? fc->max_write : fc->max_read;
1369 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001370 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001371 pgoff_t idx_from = pos >> PAGE_SHIFT;
1372 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001373 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001374 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001375 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001376
Brian Fosterde82b922013-05-14 11:25:39 -04001377 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001378 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001379 else
Al Virod22a9432014-03-16 15:50:47 -04001380 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001381 if (IS_ERR(req))
1382 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001384 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1385 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001386 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001387 fuse_sync_writes(inode);
1388 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001389 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001390 }
1391
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001393 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001394 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001395 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001396 err = fuse_get_user_pages(req, iter, &nbytes, write);
1397 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001399
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001401 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001402 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001403 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001404
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001405 if (!io->async)
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001406 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001408 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409 break;
1410 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001411 res = 0;
1412 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413 break;
1414 }
1415 count -= nres;
1416 res += nres;
1417 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418 if (nres != nbytes)
1419 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001420 if (count) {
1421 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001422 if (io->async)
1423 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001424 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001425 else
Al Virod22a9432014-03-16 15:50:47 -04001426 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001427 if (IS_ERR(req))
1428 break;
1429 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001430 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001431 if (!IS_ERR(req))
1432 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001433 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001435
Ashish Samant742f9922016-03-14 21:57:35 -07001436 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001437}
Tejun Heo08cbf542009-04-14 10:54:53 +09001438EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001439
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001440static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001441 struct iov_iter *iter,
1442 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001443{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001444 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001445 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001446 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001447
1448 if (is_bad_inode(inode))
1449 return -EIO;
1450
Al Virod22a9432014-03-16 15:50:47 -04001451 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001452
1453 fuse_invalidate_attr(inode);
1454
1455 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001456}
1457
Al Viro15316262015-03-30 22:08:36 -04001458static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001459{
Seth Forshee744742d2016-03-11 10:35:34 -06001460 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001461 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001462}
1463
Al Viro15316262015-03-30 22:08:36 -04001464static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001465{
Al Viro15316262015-03-30 22:08:36 -04001466 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001467 struct inode *inode = file_inode(file);
Seth Forshee744742d2016-03-11 10:35:34 -06001468 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001469 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001470
1471 if (is_bad_inode(inode))
1472 return -EIO;
1473
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001474 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001475 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001476 res = generic_write_checks(iocb, from);
1477 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001478 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001479 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001480 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001481 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001482 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001483
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001484 return res;
1485}
1486
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001487static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001488{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001489 int i;
1490
1491 for (i = 0; i < req->num_pages; i++)
1492 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001493
1494 if (req->ff)
1495 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001496}
1497
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001498static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001499{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001500 struct inode *inode = req->inode;
1501 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001502 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001503 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504
1505 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001506 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001507 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001508 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001509 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001510 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001511 wake_up(&fi->page_waitq);
1512}
1513
1514/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001515static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1516 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001517__releases(fc->lock)
1518__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001519{
1520 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001521 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001522 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001523
1524 if (!fc->connected)
1525 goto out_free;
1526
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001527 if (inarg->offset + data_size <= size) {
1528 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001530 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001531 } else {
1532 /* Got truncated off completely */
1533 goto out_free;
1534 }
1535
1536 req->in.args[1].size = inarg->size;
1537 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001538 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539 return;
1540
1541 out_free:
1542 fuse_writepage_finish(fc, req);
1543 spin_unlock(&fc->lock);
1544 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001545 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001546 spin_lock(&fc->lock);
1547}
1548
1549/*
1550 * If fi->writectr is positive (no truncate or fsync going on) send
1551 * all queued writepage requests.
1552 *
1553 * Called with fc->lock
1554 */
1555void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001556__releases(fc->lock)
1557__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001558{
1559 struct fuse_conn *fc = get_fuse_conn(inode);
1560 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi3a78b1f2019-04-24 17:05:06 +02001561 loff_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001562 struct fuse_req *req;
1563
1564 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1565 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1566 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001567 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001568 }
1569}
1570
1571static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1572{
1573 struct inode *inode = req->inode;
1574 struct fuse_inode *fi = get_fuse_inode(inode);
1575
1576 mapping_set_error(inode->i_mapping, req->out.h.error);
1577 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001578 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001579 struct fuse_conn *fc = get_fuse_conn(inode);
1580 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001581 struct fuse_req *next = req->misc.write.next;
1582 req->misc.write.next = next->misc.write.next;
1583 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001584 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001585 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001586
1587 /*
1588 * Skip fuse_flush_writepages() to make it easy to crop requests
1589 * based on primary request size.
1590 *
1591 * 1st case (trivial): there are no concurrent activities using
1592 * fuse_set/release_nowrite. Then we're on safe side because
1593 * fuse_flush_writepages() would call fuse_send_writepage()
1594 * anyway.
1595 *
1596 * 2nd case: someone called fuse_set_nowrite and it is waiting
1597 * now for completion of all in-flight requests. This happens
1598 * rarely and no more than once per page, so this should be
1599 * okay.
1600 *
1601 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1602 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1603 * that fuse_set_nowrite returned implies that all in-flight
1604 * requests were completed along with all of their secondary
1605 * requests. Further primary requests are blocked by negative
1606 * writectr. Hence there cannot be any in-flight requests and
1607 * no invocations of fuse_writepage_end() while we're in
1608 * fuse_set_nowrite..fuse_release_nowrite section.
1609 */
1610 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001611 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001612 fi->writectr--;
1613 fuse_writepage_finish(fc, req);
1614 spin_unlock(&fc->lock);
1615 fuse_writepage_free(fc, req);
1616}
1617
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001618static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1619 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001620{
Miklos Szeredi72523422013-10-01 16:44:52 +02001621 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001622
1623 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001624 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001625 ff = list_entry(fi->write_files.next, struct fuse_file,
1626 write_entry);
1627 fuse_file_get(ff);
1628 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001629 spin_unlock(&fc->lock);
1630
1631 return ff;
1632}
1633
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001634static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1635 struct fuse_inode *fi)
1636{
1637 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1638 WARN_ON(!ff);
1639 return ff;
1640}
1641
1642int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1643{
1644 struct fuse_conn *fc = get_fuse_conn(inode);
1645 struct fuse_inode *fi = get_fuse_inode(inode);
1646 struct fuse_file *ff;
1647 int err;
1648
1649 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001650 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001651 if (ff)
1652 fuse_file_put(ff, 0);
1653
1654 return err;
1655}
1656
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657static int fuse_writepage_locked(struct page *page)
1658{
1659 struct address_space *mapping = page->mapping;
1660 struct inode *inode = mapping->host;
1661 struct fuse_conn *fc = get_fuse_conn(inode);
1662 struct fuse_inode *fi = get_fuse_inode(inode);
1663 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001664 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001665 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001666
1667 set_page_writeback(page);
1668
Maxim Patlasov4250c062012-10-26 19:48:07 +04001669 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001670 if (!req)
1671 goto err;
1672
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001673 /* writeback always goes to bg_queue */
1674 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001675 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1676 if (!tmp_page)
1677 goto err_free;
1678
Miklos Szeredi72523422013-10-01 16:44:52 +02001679 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001680 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001681 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001682 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001683
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001684 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001685
1686 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001687 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001688 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001689 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690 req->num_pages = 1;
1691 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001692 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001693 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694 req->end = fuse_writepage_end;
1695 req->inode = inode;
1696
Tejun Heo93f78d82015-05-22 17:13:27 -04001697 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001698 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699
1700 spin_lock(&fc->lock);
1701 list_add(&req->writepages_entry, &fi->writepages);
1702 list_add_tail(&req->list, &fi->queued_writes);
1703 fuse_flush_writepages(inode);
1704 spin_unlock(&fc->lock);
1705
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001706 end_page_writeback(page);
1707
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708 return 0;
1709
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001710err_nofile:
1711 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001712err_free:
1713 fuse_request_free(req);
1714err:
1715 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001716 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001717}
1718
1719static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1720{
1721 int err;
1722
Miklos Szerediff17be02013-10-01 16:44:53 +02001723 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1724 /*
1725 * ->writepages() should be called for sync() and friends. We
1726 * should only get here on direct reclaim and then we are
1727 * allowed to skip a page which is already in flight
1728 */
1729 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1730
1731 redirty_page_for_writepage(wbc, page);
Vasily Averina4891542019-09-13 18:17:11 +03001732 unlock_page(page);
Miklos Szerediff17be02013-10-01 16:44:53 +02001733 return 0;
1734 }
1735
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001736 err = fuse_writepage_locked(page);
1737 unlock_page(page);
1738
1739 return err;
1740}
1741
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001742struct fuse_fill_wb_data {
1743 struct fuse_req *req;
1744 struct fuse_file *ff;
1745 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001746 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001747};
1748
1749static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1750{
1751 struct fuse_req *req = data->req;
1752 struct inode *inode = data->inode;
1753 struct fuse_conn *fc = get_fuse_conn(inode);
1754 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001755 int num_pages = req->num_pages;
1756 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001757
1758 req->ff = fuse_file_get(data->ff);
1759 spin_lock(&fc->lock);
1760 list_add_tail(&req->list, &fi->queued_writes);
1761 fuse_flush_writepages(inode);
1762 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001763
1764 for (i = 0; i < num_pages; i++)
1765 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001766}
1767
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1769 struct page *page)
1770{
1771 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1772 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1773 struct fuse_req *tmp;
1774 struct fuse_req *old_req;
1775 bool found = false;
1776 pgoff_t curr_index;
1777
1778 BUG_ON(new_req->num_pages != 0);
1779
1780 spin_lock(&fc->lock);
1781 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001782 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1783 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001784 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001785 if (curr_index <= page->index &&
1786 page->index < curr_index + old_req->num_pages) {
1787 found = true;
1788 break;
1789 }
1790 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001791 if (!found) {
1792 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001793 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001794 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001795
Maxim Patlasovf6011082013-10-02 15:01:07 +04001796 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001797 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1798 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001799 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001800 if (tmp->num_pages == 1 &&
1801 curr_index == page->index) {
1802 old_req = tmp;
1803 }
1804 }
1805
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001806 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001807 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001808
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809 copy_highpage(old_req->pages[0], page);
1810 spin_unlock(&fc->lock);
1811
Tejun Heo93f78d82015-05-22 17:13:27 -04001812 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi4f3d6982019-01-16 10:27:59 +01001813 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001814 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001815 fuse_writepage_free(fc, new_req);
1816 fuse_request_free(new_req);
1817 goto out;
1818 } else {
1819 new_req->misc.write.next = old_req->misc.write.next;
1820 old_req->misc.write.next = new_req;
1821 }
1822out_unlock:
1823 spin_unlock(&fc->lock);
1824out:
1825 return found;
1826}
1827
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001828static int fuse_writepages_fill(struct page *page,
1829 struct writeback_control *wbc, void *_data)
1830{
1831 struct fuse_fill_wb_data *data = _data;
1832 struct fuse_req *req = data->req;
1833 struct inode *inode = data->inode;
1834 struct fuse_conn *fc = get_fuse_conn(inode);
1835 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001836 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001837 int err;
1838
1839 if (!data->ff) {
1840 err = -EIO;
1841 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1842 if (!data->ff)
1843 goto out_unlock;
1844 }
1845
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001846 /*
1847 * Being under writeback is unlikely but possible. For example direct
1848 * read to an mmaped fuse file will set the page dirty twice; once when
1849 * the pages are faulted with get_user_pages(), and then after the read
1850 * completed.
1851 */
1852 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001853
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001854 if (req && req->num_pages &&
1855 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001856 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001857 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1858 fuse_writepages_send(data);
1859 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001860 }
1861 err = -ENOMEM;
1862 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1863 if (!tmp_page)
1864 goto out_unlock;
1865
1866 /*
1867 * The page must not be redirtied until the writeout is completed
1868 * (i.e. userspace has sent a reply to the write request). Otherwise
1869 * there could be more than one temporary page instance for each real
1870 * page.
1871 *
1872 * This is ensured by holding the page lock in page_mkwrite() while
1873 * checking fuse_page_is_writeback(). We already hold the page lock
1874 * since clear_page_dirty_for_io() and keep it held until we add the
1875 * request to the fi->writepages list and increment req->num_pages.
1876 * After this fuse_page_is_writeback() will indicate that the page is
1877 * under writeback, so we can release the page lock.
1878 */
1879 if (data->req == NULL) {
1880 struct fuse_inode *fi = get_fuse_inode(inode);
1881
1882 err = -ENOMEM;
1883 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1884 if (!req) {
1885 __free_page(tmp_page);
1886 goto out_unlock;
1887 }
1888
1889 fuse_write_fill(req, data->ff, page_offset(page), 0);
1890 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001891 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001892 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001893 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894 req->num_pages = 0;
1895 req->end = fuse_writepage_end;
1896 req->inode = inode;
1897
1898 spin_lock(&fc->lock);
1899 list_add(&req->writepages_entry, &fi->writepages);
1900 spin_unlock(&fc->lock);
1901
1902 data->req = req;
1903 }
1904 set_page_writeback(page);
1905
1906 copy_highpage(tmp_page, page);
1907 req->pages[req->num_pages] = tmp_page;
1908 req->page_descs[req->num_pages].offset = 0;
1909 req->page_descs[req->num_pages].length = PAGE_SIZE;
1910
Tejun Heo93f78d82015-05-22 17:13:27 -04001911 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001912 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001913
1914 err = 0;
1915 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1916 end_page_writeback(page);
1917 data->req = NULL;
1918 goto out_unlock;
1919 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001920 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001921
1922 /*
1923 * Protected by fc->lock against concurrent access by
1924 * fuse_page_is_writeback().
1925 */
1926 spin_lock(&fc->lock);
1927 req->num_pages++;
1928 spin_unlock(&fc->lock);
1929
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001930out_unlock:
1931 unlock_page(page);
1932
1933 return err;
1934}
1935
1936static int fuse_writepages(struct address_space *mapping,
1937 struct writeback_control *wbc)
1938{
1939 struct inode *inode = mapping->host;
1940 struct fuse_fill_wb_data data;
1941 int err;
1942
1943 err = -EIO;
1944 if (is_bad_inode(inode))
1945 goto out;
1946
1947 data.inode = inode;
1948 data.req = NULL;
1949 data.ff = NULL;
1950
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001951 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001952 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1953 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001954 GFP_NOFS);
1955 if (!data.orig_pages)
1956 goto out;
1957
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001958 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1959 if (data.req) {
1960 /* Ignore errors if we can write at least one page */
1961 BUG_ON(!data.req->num_pages);
1962 fuse_writepages_send(&data);
1963 err = 0;
1964 }
1965 if (data.ff)
1966 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001967
1968 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001969out:
1970 return err;
1971}
1972
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001973/*
1974 * It's worthy to make sure that space is reserved on disk for the write,
1975 * but how to implement it without killing performance need more thinking.
1976 */
1977static int fuse_write_begin(struct file *file, struct address_space *mapping,
1978 loff_t pos, unsigned len, unsigned flags,
1979 struct page **pagep, void **fsdata)
1980{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001982 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001983 struct page *page;
1984 loff_t fsize;
1985 int err = -ENOMEM;
1986
1987 WARN_ON(!fc->writeback_cache);
1988
1989 page = grab_cache_page_write_begin(mapping, index, flags);
1990 if (!page)
1991 goto error;
1992
1993 fuse_wait_on_page_writeback(mapping->host, page->index);
1994
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001996 goto success;
1997 /*
1998 * Check if the start this page comes after the end of file, in which
1999 * case the readpage can be optimized away.
2000 */
2001 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 if (fsize <= (pos & PAGE_MASK)) {
2003 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002004 if (off)
2005 zero_user_segment(page, 0, off);
2006 goto success;
2007 }
2008 err = fuse_do_readpage(file, page);
2009 if (err)
2010 goto cleanup;
2011success:
2012 *pagep = page;
2013 return 0;
2014
2015cleanup:
2016 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002017 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002018error:
2019 return err;
2020}
2021
2022static int fuse_write_end(struct file *file, struct address_space *mapping,
2023 loff_t pos, unsigned len, unsigned copied,
2024 struct page *page, void *fsdata)
2025{
2026 struct inode *inode = page->mapping->host;
2027
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002028 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2029 if (!copied)
2030 goto unlock;
2031
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002032 if (!PageUptodate(page)) {
2033 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002034 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002035 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002036 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002037 SetPageUptodate(page);
2038 }
2039
2040 fuse_write_update_size(inode, pos + copied);
2041 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002042
2043unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002044 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002045 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002046
2047 return copied;
2048}
2049
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002050static int fuse_launder_page(struct page *page)
2051{
2052 int err = 0;
2053 if (clear_page_dirty_for_io(page)) {
2054 struct inode *inode = page->mapping->host;
2055 err = fuse_writepage_locked(page);
2056 if (!err)
2057 fuse_wait_on_page_writeback(inode, page->index);
2058 }
2059 return err;
2060}
2061
2062/*
2063 * Write back dirty pages now, because there may not be any suitable
2064 * open files later
2065 */
2066static void fuse_vma_close(struct vm_area_struct *vma)
2067{
2068 filemap_write_and_wait(vma->vm_file->f_mapping);
2069}
2070
2071/*
2072 * Wait for writeback against this page to complete before allowing it
2073 * to be marked dirty again, and hence written back again, possibly
2074 * before the previous writepage completed.
2075 *
2076 * Block here, instead of in ->writepage(), so that the userspace fs
2077 * can only block processes actually operating on the filesystem.
2078 *
2079 * Otherwise unprivileged userspace fs would be able to block
2080 * unrelated:
2081 *
2082 * - page migration
2083 * - sync(2)
2084 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2085 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002086static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002087{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002088 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002089 struct inode *inode = file_inode(vma->vm_file);
2090
2091 file_update_time(vma->vm_file);
2092 lock_page(page);
2093 if (page->mapping != inode->i_mapping) {
2094 unlock_page(page);
2095 return VM_FAULT_NOPAGE;
2096 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002097
2098 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002099 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002100}
2101
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002102static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002103 .close = fuse_vma_close,
2104 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002105 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002106 .page_mkwrite = fuse_page_mkwrite,
2107};
2108
2109static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2110{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05302111 struct fuse_file *ff = file->private_data;
2112
2113 ff->passthrough_enabled = 0;
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002114 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2115 fuse_link_write_file(file);
2116
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002117 file_accessed(file);
2118 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002119 return 0;
2120}
2121
Miklos Szeredifc280c92009-04-02 14:25:35 +02002122static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2123{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05302124 struct fuse_file *ff = file->private_data;
2125
2126 ff->passthrough_enabled = 0;
Miklos Szeredifc280c92009-04-02 14:25:35 +02002127 /* Can't provide the coherency needed for MAP_SHARED */
2128 if (vma->vm_flags & VM_MAYSHARE)
2129 return -ENODEV;
2130
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002131 invalidate_inode_pages2(file->f_mapping);
2132
Miklos Szeredifc280c92009-04-02 14:25:35 +02002133 return generic_file_mmap(file, vma);
2134}
2135
Miklos Szeredi71421252006-06-25 05:48:52 -07002136static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2137 struct file_lock *fl)
2138{
2139 switch (ffl->type) {
2140 case F_UNLCK:
2141 break;
2142
2143 case F_RDLCK:
2144 case F_WRLCK:
2145 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2146 ffl->end < ffl->start)
2147 return -EIO;
2148
2149 fl->fl_start = ffl->start;
2150 fl->fl_end = ffl->end;
2151 fl->fl_pid = ffl->pid;
2152 break;
2153
2154 default:
2155 return -EIO;
2156 }
2157 fl->fl_type = ffl->type;
2158 return 0;
2159}
2160
Miklos Szeredi70781872014-12-12 09:49:05 +01002161static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002162 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002163 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002164{
Al Viro6131ffa2013-02-27 16:59:05 -05002165 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002166 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002167 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002168
Miklos Szeredi70781872014-12-12 09:49:05 +01002169 memset(inarg, 0, sizeof(*inarg));
2170 inarg->fh = ff->fh;
2171 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2172 inarg->lk.start = fl->fl_start;
2173 inarg->lk.end = fl->fl_end;
2174 inarg->lk.type = fl->fl_type;
2175 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002176 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002177 inarg->lk_flags |= FUSE_LK_FLOCK;
2178 args->in.h.opcode = opcode;
2179 args->in.h.nodeid = get_node_id(inode);
2180 args->in.numargs = 1;
2181 args->in.args[0].size = sizeof(*inarg);
2182 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002183}
2184
2185static int fuse_getlk(struct file *file, struct file_lock *fl)
2186{
Al Viro6131ffa2013-02-27 16:59:05 -05002187 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002188 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002189 FUSE_ARGS(args);
2190 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 struct fuse_lk_out outarg;
2192 int err;
2193
Miklos Szeredi70781872014-12-12 09:49:05 +01002194 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2195 args.out.numargs = 1;
2196 args.out.args[0].size = sizeof(outarg);
2197 args.out.args[0].value = &outarg;
2198 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002199 if (!err)
2200 err = convert_fuse_file_lock(&outarg.lk, fl);
2201
2202 return err;
2203}
2204
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002205static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002206{
Al Viro6131ffa2013-02-27 16:59:05 -05002207 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002208 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002209 FUSE_ARGS(args);
2210 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2212 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2213 int err;
2214
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002215 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002216 /* NLM needs asynchronous locks, which we don't support yet */
2217 return -ENOLCK;
2218 }
2219
Miklos Szeredi71421252006-06-25 05:48:52 -07002220 /* Unlock on close is handled by the flush method */
2221 if (fl->fl_flags & FL_CLOSE)
2222 return 0;
2223
Miklos Szeredi70781872014-12-12 09:49:05 +01002224 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2225 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002226
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002227 /* locking is restartable */
2228 if (err == -EINTR)
2229 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002230
Miklos Szeredi71421252006-06-25 05:48:52 -07002231 return err;
2232}
2233
2234static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2235{
Al Viro6131ffa2013-02-27 16:59:05 -05002236 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002237 struct fuse_conn *fc = get_fuse_conn(inode);
2238 int err;
2239
Miklos Szeredi48e90762008-07-25 01:49:02 -07002240 if (cmd == F_CANCELLK) {
2241 err = 0;
2242 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002243 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002244 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002245 err = 0;
2246 } else
2247 err = fuse_getlk(file, fl);
2248 } else {
2249 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002250 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002251 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002252 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002253 }
2254 return err;
2255}
2256
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002257static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2258{
Al Viro6131ffa2013-02-27 16:59:05 -05002259 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002260 struct fuse_conn *fc = get_fuse_conn(inode);
2261 int err;
2262
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002263 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002264 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002265 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002266 struct fuse_file *ff = file->private_data;
2267
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002268 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002269 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002270 err = fuse_setlk(file, fl, 1);
2271 }
2272
2273 return err;
2274}
2275
Miklos Szeredib2d22722006-12-06 20:35:51 -08002276static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2277{
2278 struct inode *inode = mapping->host;
2279 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002280 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002281 struct fuse_bmap_in inarg;
2282 struct fuse_bmap_out outarg;
2283 int err;
2284
2285 if (!inode->i_sb->s_bdev || fc->no_bmap)
2286 return 0;
2287
Miklos Szeredib2d22722006-12-06 20:35:51 -08002288 memset(&inarg, 0, sizeof(inarg));
2289 inarg.block = block;
2290 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002291 args.in.h.opcode = FUSE_BMAP;
2292 args.in.h.nodeid = get_node_id(inode);
2293 args.in.numargs = 1;
2294 args.in.args[0].size = sizeof(inarg);
2295 args.in.args[0].value = &inarg;
2296 args.out.numargs = 1;
2297 args.out.args[0].size = sizeof(outarg);
2298 args.out.args[0].value = &outarg;
2299 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002300 if (err == -ENOSYS)
2301 fc->no_bmap = 1;
2302
2303 return err ? 0 : outarg.block;
2304}
2305
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302306static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2307{
2308 struct inode *inode = file->f_mapping->host;
2309 struct fuse_conn *fc = get_fuse_conn(inode);
2310 struct fuse_file *ff = file->private_data;
2311 FUSE_ARGS(args);
2312 struct fuse_lseek_in inarg = {
2313 .fh = ff->fh,
2314 .offset = offset,
2315 .whence = whence
2316 };
2317 struct fuse_lseek_out outarg;
2318 int err;
2319
2320 if (fc->no_lseek)
2321 goto fallback;
2322
2323 args.in.h.opcode = FUSE_LSEEK;
2324 args.in.h.nodeid = ff->nodeid;
2325 args.in.numargs = 1;
2326 args.in.args[0].size = sizeof(inarg);
2327 args.in.args[0].value = &inarg;
2328 args.out.numargs = 1;
2329 args.out.args[0].size = sizeof(outarg);
2330 args.out.args[0].value = &outarg;
2331 err = fuse_simple_request(fc, &args);
2332 if (err) {
2333 if (err == -ENOSYS) {
2334 fc->no_lseek = 1;
2335 goto fallback;
2336 }
2337 return err;
2338 }
2339
2340 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2341
2342fallback:
2343 err = fuse_update_attributes(inode, NULL, file, NULL);
2344 if (!err)
2345 return generic_file_llseek(file, offset, whence);
2346 else
2347 return err;
2348}
2349
Andrew Morton965c8e52012-12-17 15:59:39 -08002350static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002351{
2352 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002353 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002354
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302355 switch (whence) {
2356 case SEEK_SET:
2357 case SEEK_CUR:
2358 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002359 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302360 break;
2361 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002362 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302363 retval = fuse_update_attributes(inode, NULL, file, NULL);
2364 if (!retval)
2365 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002366 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302367 break;
2368 case SEEK_HOLE:
2369 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002370 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302371 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002372 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302373 break;
2374 default:
2375 retval = -EINVAL;
2376 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002377
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002378 return retval;
2379}
2380
Tejun Heo59efec72008-11-26 12:03:55 +01002381/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002382 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2383 * ABI was defined to be 'struct iovec' which is different on 32bit
2384 * and 64bit. Fortunately we can determine which structure the server
2385 * used from the size of the reply.
2386 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002387static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2388 size_t transferred, unsigned count,
2389 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002390{
2391#ifdef CONFIG_COMPAT
2392 if (count * sizeof(struct compat_iovec) == transferred) {
2393 struct compat_iovec *ciov = src;
2394 unsigned i;
2395
2396 /*
2397 * With this interface a 32bit server cannot support
2398 * non-compat (i.e. ones coming from 64bit apps) ioctl
2399 * requests
2400 */
2401 if (!is_compat)
2402 return -EINVAL;
2403
2404 for (i = 0; i < count; i++) {
2405 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2406 dst[i].iov_len = ciov[i].iov_len;
2407 }
2408 return 0;
2409 }
2410#endif
2411
2412 if (count * sizeof(struct iovec) != transferred)
2413 return -EIO;
2414
2415 memcpy(dst, src, transferred);
2416 return 0;
2417}
2418
Miklos Szeredi75727772010-11-30 16:39:27 +01002419/* Make sure iov_length() won't overflow */
2420static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2421{
2422 size_t n;
2423 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2424
Zach Brownfb6ccff2012-07-24 12:10:11 -07002425 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002426 if (iov->iov_len > (size_t) max)
2427 return -ENOMEM;
2428 max -= iov->iov_len;
2429 }
2430 return 0;
2431}
2432
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002433static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2434 void *src, size_t transferred, unsigned count,
2435 bool is_compat)
2436{
2437 unsigned i;
2438 struct fuse_ioctl_iovec *fiov = src;
2439
2440 if (fc->minor < 16) {
2441 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2442 count, is_compat);
2443 }
2444
2445 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2446 return -EIO;
2447
2448 for (i = 0; i < count; i++) {
2449 /* Did the server supply an inappropriate value? */
2450 if (fiov[i].base != (unsigned long) fiov[i].base ||
2451 fiov[i].len != (unsigned long) fiov[i].len)
2452 return -EIO;
2453
2454 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2455 dst[i].iov_len = (size_t) fiov[i].len;
2456
2457#ifdef CONFIG_COMPAT
2458 if (is_compat &&
2459 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2460 (compat_size_t) dst[i].iov_len != fiov[i].len))
2461 return -EIO;
2462#endif
2463 }
2464
2465 return 0;
2466}
2467
2468
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002469/*
Tejun Heo59efec72008-11-26 12:03:55 +01002470 * For ioctls, there is no generic way to determine how much memory
2471 * needs to be read and/or written. Furthermore, ioctls are allowed
2472 * to dereference the passed pointer, so the parameter requires deep
2473 * copying but FUSE has no idea whatsoever about what to copy in or
2474 * out.
2475 *
2476 * This is solved by allowing FUSE server to retry ioctl with
2477 * necessary in/out iovecs. Let's assume the ioctl implementation
2478 * needs to read in the following structure.
2479 *
2480 * struct a {
2481 * char *buf;
2482 * size_t buflen;
2483 * }
2484 *
2485 * On the first callout to FUSE server, inarg->in_size and
2486 * inarg->out_size will be NULL; then, the server completes the ioctl
2487 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2488 * the actual iov array to
2489 *
2490 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2491 *
2492 * which tells FUSE to copy in the requested area and retry the ioctl.
2493 * On the second round, the server has access to the structure and
2494 * from that it can tell what to look for next, so on the invocation,
2495 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2496 *
2497 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2498 * { .iov_base = a.buf, .iov_len = a.buflen } }
2499 *
2500 * FUSE will copy both struct a and the pointed buffer from the
2501 * process doing the ioctl and retry ioctl with both struct a and the
2502 * buffer.
2503 *
2504 * This time, FUSE server has everything it needs and completes ioctl
2505 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2506 *
2507 * Copying data out works the same way.
2508 *
2509 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2510 * automatically initializes in and out iovs by decoding @cmd with
2511 * _IOC_* macros and the server is not allowed to request RETRY. This
2512 * limits ioctl data transfers to well-formed ioctls and is the forced
2513 * behavior for all FUSE servers.
2514 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002515long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2516 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002517{
Tejun Heo59efec72008-11-26 12:03:55 +01002518 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002519 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002520 struct fuse_ioctl_in inarg = {
2521 .fh = ff->fh,
2522 .cmd = cmd,
2523 .arg = arg,
2524 .flags = flags
2525 };
2526 struct fuse_ioctl_out outarg;
2527 struct fuse_req *req = NULL;
2528 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002529 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002530 struct iovec *in_iov = NULL, *out_iov = NULL;
2531 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002532 size_t in_size, out_size, transferred, c;
2533 int err, i;
2534 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002535
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002536#if BITS_PER_LONG == 32
2537 inarg.flags |= FUSE_IOCTL_32BIT;
2538#else
2539 if (flags & FUSE_IOCTL_COMPAT)
2540 inarg.flags |= FUSE_IOCTL_32BIT;
2541#endif
2542
Tejun Heo59efec72008-11-26 12:03:55 +01002543 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002544 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002545
Tejun Heo59efec72008-11-26 12:03:55 +01002546 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002547 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002548 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002549 if (!pages || !iov_page)
2550 goto out;
2551
2552 /*
2553 * If restricted, initialize IO parameters as encoded in @cmd.
2554 * RETRY from server is not allowed.
2555 */
2556 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002557 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002558
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002559 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002560 iov->iov_len = _IOC_SIZE(cmd);
2561
2562 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2563 in_iov = iov;
2564 in_iovs = 1;
2565 }
2566
2567 if (_IOC_DIR(cmd) & _IOC_READ) {
2568 out_iov = iov;
2569 out_iovs = 1;
2570 }
2571 }
2572
2573 retry:
2574 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2575 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2576
2577 /*
2578 * Out data can be used either for actual out data or iovs,
2579 * make sure there always is at least one page.
2580 */
2581 out_size = max_t(size_t, out_size, PAGE_SIZE);
2582 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2583
2584 /* make sure there are enough buffer pages and init request with them */
2585 err = -ENOMEM;
2586 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2587 goto out;
2588 while (num_pages < max_pages) {
2589 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2590 if (!pages[num_pages])
2591 goto out;
2592 num_pages++;
2593 }
2594
Maxim Patlasov54b96672012-10-26 19:49:13 +04002595 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002596 if (IS_ERR(req)) {
2597 err = PTR_ERR(req);
2598 req = NULL;
2599 goto out;
2600 }
2601 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2602 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002603 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002604
2605 /* okay, let's send it to the client */
2606 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002607 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002608 req->in.numargs = 1;
2609 req->in.args[0].size = sizeof(inarg);
2610 req->in.args[0].value = &inarg;
2611 if (in_size) {
2612 req->in.numargs++;
2613 req->in.args[1].size = in_size;
2614 req->in.argpages = 1;
2615
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002616 err = -EFAULT;
2617 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2618 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2619 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2620 if (c != PAGE_SIZE && iov_iter_count(&ii))
2621 goto out;
2622 }
Tejun Heo59efec72008-11-26 12:03:55 +01002623 }
2624
2625 req->out.numargs = 2;
2626 req->out.args[0].size = sizeof(outarg);
2627 req->out.args[0].value = &outarg;
2628 req->out.args[1].size = out_size;
2629 req->out.argpages = 1;
2630 req->out.argvar = 1;
2631
Tejun Heob93f8582008-11-26 12:03:55 +01002632 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002633 err = req->out.h.error;
2634 transferred = req->out.args[1].size;
2635 fuse_put_request(fc, req);
2636 req = NULL;
2637 if (err)
2638 goto out;
2639
2640 /* did it ask for retry? */
2641 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002642 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002643
2644 /* no retry if in restricted mode */
2645 err = -EIO;
2646 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2647 goto out;
2648
2649 in_iovs = outarg.in_iovs;
2650 out_iovs = outarg.out_iovs;
2651
2652 /*
2653 * Make sure things are in boundary, separate checks
2654 * are to protect against overflow.
2655 */
2656 err = -ENOMEM;
2657 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2658 out_iovs > FUSE_IOCTL_MAX_IOV ||
2659 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2660 goto out;
2661
Cong Wang2408f6e2011-11-25 23:14:30 +08002662 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002663 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002664 transferred, in_iovs + out_iovs,
2665 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002666 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002667 if (err)
2668 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002669
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002670 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002671 out_iov = in_iov + in_iovs;
2672
Miklos Szeredi75727772010-11-30 16:39:27 +01002673 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2674 if (err)
2675 goto out;
2676
2677 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2678 if (err)
2679 goto out;
2680
Tejun Heo59efec72008-11-26 12:03:55 +01002681 goto retry;
2682 }
2683
2684 err = -EIO;
2685 if (transferred > inarg.out_size)
2686 goto out;
2687
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002688 err = -EFAULT;
2689 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2690 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2691 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2692 if (c != PAGE_SIZE && iov_iter_count(&ii))
2693 goto out;
2694 }
2695 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002696 out:
2697 if (req)
2698 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002699 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002700 while (num_pages)
2701 __free_page(pages[--num_pages]);
2702 kfree(pages);
2703
2704 return err ? err : outarg.result;
2705}
Tejun Heo08cbf542009-04-14 10:54:53 +09002706EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002707
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002708long fuse_ioctl_common(struct file *file, unsigned int cmd,
2709 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002710{
Al Viro6131ffa2013-02-27 16:59:05 -05002711 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002712 struct fuse_conn *fc = get_fuse_conn(inode);
2713
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002714 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002715 return -EACCES;
2716
2717 if (is_bad_inode(inode))
2718 return -EIO;
2719
2720 return fuse_do_ioctl(file, cmd, arg, flags);
2721}
2722
Tejun Heo59efec72008-11-26 12:03:55 +01002723static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2724 unsigned long arg)
2725{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002726 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002727}
2728
2729static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2730 unsigned long arg)
2731{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002732 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002733}
2734
Tejun Heo95668a62008-11-26 12:03:55 +01002735/*
2736 * All files which have been polled are linked to RB tree
2737 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2738 * find the matching one.
2739 */
2740static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2741 struct rb_node **parent_out)
2742{
2743 struct rb_node **link = &fc->polled_files.rb_node;
2744 struct rb_node *last = NULL;
2745
2746 while (*link) {
2747 struct fuse_file *ff;
2748
2749 last = *link;
2750 ff = rb_entry(last, struct fuse_file, polled_node);
2751
2752 if (kh < ff->kh)
2753 link = &last->rb_left;
2754 else if (kh > ff->kh)
2755 link = &last->rb_right;
2756 else
2757 return link;
2758 }
2759
2760 if (parent_out)
2761 *parent_out = last;
2762 return link;
2763}
2764
2765/*
2766 * The file is about to be polled. Make sure it's on the polled_files
2767 * RB tree. Note that files once added to the polled_files tree are
2768 * not removed before the file is released. This is because a file
2769 * polled once is likely to be polled again.
2770 */
2771static void fuse_register_polled_file(struct fuse_conn *fc,
2772 struct fuse_file *ff)
2773{
2774 spin_lock(&fc->lock);
2775 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002776 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002777
2778 link = fuse_find_polled_node(fc, ff->kh, &parent);
2779 BUG_ON(*link);
2780 rb_link_node(&ff->polled_node, parent, link);
2781 rb_insert_color(&ff->polled_node, &fc->polled_files);
2782 }
2783 spin_unlock(&fc->lock);
2784}
2785
Tejun Heo08cbf542009-04-14 10:54:53 +09002786unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002787{
Tejun Heo95668a62008-11-26 12:03:55 +01002788 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002789 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002790 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2791 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002792 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002793 int err;
2794
2795 if (fc->no_poll)
2796 return DEFAULT_POLLMASK;
2797
2798 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002799 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002800
2801 /*
2802 * Ask for notification iff there's someone waiting for it.
2803 * The client may ignore the flag and always notify.
2804 */
2805 if (waitqueue_active(&ff->poll_wait)) {
2806 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2807 fuse_register_polled_file(fc, ff);
2808 }
2809
Miklos Szeredi70781872014-12-12 09:49:05 +01002810 args.in.h.opcode = FUSE_POLL;
2811 args.in.h.nodeid = ff->nodeid;
2812 args.in.numargs = 1;
2813 args.in.args[0].size = sizeof(inarg);
2814 args.in.args[0].value = &inarg;
2815 args.out.numargs = 1;
2816 args.out.args[0].size = sizeof(outarg);
2817 args.out.args[0].value = &outarg;
2818 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002819
2820 if (!err)
2821 return outarg.revents;
2822 if (err == -ENOSYS) {
2823 fc->no_poll = 1;
2824 return DEFAULT_POLLMASK;
2825 }
2826 return POLLERR;
2827}
Tejun Heo08cbf542009-04-14 10:54:53 +09002828EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002829
2830/*
2831 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2832 * wakes up the poll waiters.
2833 */
2834int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2835 struct fuse_notify_poll_wakeup_out *outarg)
2836{
2837 u64 kh = outarg->kh;
2838 struct rb_node **link;
2839
2840 spin_lock(&fc->lock);
2841
2842 link = fuse_find_polled_node(fc, kh, NULL);
2843 if (*link) {
2844 struct fuse_file *ff;
2845
2846 ff = rb_entry(*link, struct fuse_file, polled_node);
2847 wake_up_interruptible_sync(&ff->poll_wait);
2848 }
2849
2850 spin_unlock(&fc->lock);
2851 return 0;
2852}
2853
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002854static void fuse_do_truncate(struct file *file)
2855{
2856 struct inode *inode = file->f_mapping->host;
2857 struct iattr attr;
2858
2859 attr.ia_valid = ATTR_SIZE;
2860 attr.ia_size = i_size_read(inode);
2861
2862 attr.ia_file = file;
2863 attr.ia_valid |= ATTR_FILE;
2864
Jan Kara62490332016-05-26 17:12:41 +02002865 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002866}
2867
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002868static inline loff_t fuse_round_up(loff_t off)
2869{
2870 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2871}
2872
Anand Avati4273b792012-02-17 12:46:25 -05002873static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002874fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002875{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002876 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002877 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002878 struct file *file = iocb->ki_filp;
2879 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002880 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002881 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002882 struct inode *inode;
2883 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002884 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002885 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002886 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002887
Anand Avati4273b792012-02-17 12:46:25 -05002888 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 inode = file->f_mapping->host;
2890 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002891
Omar Sandoval6f673762015-03-16 04:33:52 -07002892 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002893 return 0;
2894
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002895 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002896 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002897 if (offset >= i_size)
2898 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002899 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2900 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002901 }
2902
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002903 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002904 if (!io)
2905 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002907 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002908 io->reqs = 1;
2909 io->bytes = -1;
2910 io->size = 0;
2911 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002912 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002913 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002914 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002915 /*
2916 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002917 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002918 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002919 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002920 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302921 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002922
2923 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302924 * We cannot asynchronously extend the size of a file.
2925 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002926 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302927 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2928 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002929
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302930 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002931 /*
2932 * Additional reference to keep io around after
2933 * calling fuse_aio_complete()
2934 */
2935 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002936 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002937 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002938
Omar Sandoval6f673762015-03-16 04:33:52 -07002939 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002940 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002941 fuse_invalidate_attr(inode);
2942 } else {
Al Virod22a9432014-03-16 15:50:47 -04002943 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002944 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002945
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002946 if (io->async) {
Lukas Czernerfdd93792018-11-09 14:51:46 +01002947 bool blocking = io->blocking;
2948
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002949 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2950
2951 /* we have a non-extending, async request, so return */
Lukas Czernerfdd93792018-11-09 14:51:46 +01002952 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002953 return -EIOCBQUEUED;
2954
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002955 wait_for_completion(&wait);
2956 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002957 }
2958
Seth Forshee744742d2016-03-11 10:35:34 -06002959 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002960
Omar Sandoval6f673762015-03-16 04:33:52 -07002961 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002962 if (ret > 0)
2963 fuse_write_update_size(inode, pos);
2964 else if (ret < 0 && offset + count > i_size)
2965 fuse_do_truncate(file);
2966 }
Anand Avati4273b792012-02-17 12:46:25 -05002967
2968 return ret;
2969}
2970
Miklos Szeredicdadb112012-11-10 16:55:56 +01002971static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2972 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002973{
2974 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002975 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002976 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002977 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002978 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002979 struct fuse_fallocate_in inarg = {
2980 .fh = ff->fh,
2981 .offset = offset,
2982 .length = length,
2983 .mode = mode
2984 };
2985 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002986 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2987 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002988
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002989 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2990 return -EOPNOTSUPP;
2991
Miklos Szeredi519c6042012-04-26 10:56:36 +02002992 if (fc->no_fallocate)
2993 return -EOPNOTSUPP;
2994
Maxim Patlasov14c14412013-06-13 12:16:39 +04002995 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002996 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002997 if (mode & FALLOC_FL_PUNCH_HOLE) {
2998 loff_t endbyte = offset + length - 1;
2999 err = filemap_write_and_wait_range(inode->i_mapping,
3000 offset, endbyte);
3001 if (err)
3002 goto out;
3003
3004 fuse_sync_writes(inode);
3005 }
Brian Foster3634a632013-05-17 09:30:32 -04003006 }
3007
Liu Boeb607962019-04-18 04:04:41 +08003008 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3009 offset + length > i_size_read(inode)) {
3010 err = inode_newsize_ok(inode, offset + length);
3011 if (err)
Miklos Szeredi4fb410b2019-05-27 11:42:07 +02003012 goto out;
Liu Boeb607962019-04-18 04:04:41 +08003013 }
3014
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003015 if (!(mode & FALLOC_FL_KEEP_SIZE))
3016 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3017
Miklos Szeredi70781872014-12-12 09:49:05 +01003018 args.in.h.opcode = FUSE_FALLOCATE;
3019 args.in.h.nodeid = ff->nodeid;
3020 args.in.numargs = 1;
3021 args.in.args[0].size = sizeof(inarg);
3022 args.in.args[0].value = &inarg;
3023 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003024 if (err == -ENOSYS) {
3025 fc->no_fallocate = 1;
3026 err = -EOPNOTSUPP;
3027 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003028 if (err)
3029 goto out;
3030
3031 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003032 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3033 bool changed = fuse_write_update_size(inode, offset + length);
3034
Miklos Szeredi93d22692014-04-28 14:19:22 +02003035 if (changed && fc->writeback_cache)
3036 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003037 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003038
3039 if (mode & FALLOC_FL_PUNCH_HOLE)
3040 truncate_pagecache_range(inode, offset, offset + length - 1);
3041
3042 fuse_invalidate_attr(inode);
3043
Brian Foster3634a632013-05-17 09:30:32 -04003044out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003045 if (!(mode & FALLOC_FL_KEEP_SIZE))
3046 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3047
Maxim Patlasovbde52782013-09-13 19:19:54 +04003048 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003049 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003050
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003051 return err;
3052}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003053
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003054static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003055 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003056 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003057 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003058 .mmap = fuse_file_mmap,
3059 .open = fuse_open,
3060 .flush = fuse_flush,
3061 .release = fuse_release,
3062 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003063 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003064 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003065 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003066 .unlocked_ioctl = fuse_file_ioctl,
3067 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003068 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003069 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003070};
3071
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003072static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003073 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003074 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003075 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003076 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003077 .open = fuse_open,
3078 .flush = fuse_flush,
3079 .release = fuse_release,
3080 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003081 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003082 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003083 .unlocked_ioctl = fuse_file_ioctl,
3084 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003085 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003086 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003087 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003088};
3089
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003090static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003091 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003092 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003093 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003094 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003095 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003096 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003097 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003098 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003099 .write_begin = fuse_write_begin,
3100 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003101};
3102
3103void fuse_init_file_inode(struct inode *inode)
3104{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003105 inode->i_fop = &fuse_file_operations;
3106 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003107}