blob: 21d829b1a8f1fbc852e69037576b3b319c89e9e1 [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);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200197 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200198 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800199 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
200 struct fuse_inode *fi = get_fuse_inode(inode);
201
202 spin_lock(&fc->lock);
203 fi->attr_version = ++fc->attr_version;
204 i_size_write(inode, 0);
205 spin_unlock(&fc->lock);
206 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200207 if (fc->writeback_cache)
208 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800209 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400210 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
211 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800212}
213
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200214int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800215{
Tejun Heoacf99432008-11-26 12:03:55 +0100216 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700217 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200218 bool lock_inode = (file->f_flags & O_TRUNC) &&
219 fc->atomic_o_trunc &&
220 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700221
222 err = generic_file_open(inode, file);
223 if (err)
224 return err;
225
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200226 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500227 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200228
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200229 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700230
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200231 if (!err)
232 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200233
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200234 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500235 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200236
237 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700238}
239
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200240static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800241{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200242 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700243 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800244 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700245
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200246 spin_lock(&fc->lock);
247 list_del(&ff->write_entry);
248 if (!RB_EMPTY_NODE(&ff->polled_node))
249 rb_erase(&ff->polled_node, &fc->polled_files);
250 spin_unlock(&fc->lock);
251
Bryan Green357ccf22011-03-01 16:43:52 -0800252 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200253
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700254 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800255 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700256 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200257 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700258 req->in.numargs = 1;
259 req->in.args[0].size = sizeof(struct fuse_release_in);
260 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800261}
262
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200263void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800264{
Tejun Heo6b2db282009-04-14 10:54:49 +0900265 struct fuse_file *ff;
266 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700267
Tejun Heo6b2db282009-04-14 10:54:49 +0900268 ff = file->private_data;
269 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200270 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700271
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530272 fuse_passthrough_release(ff);
273
Tejun Heo6b2db282009-04-14 10:54:49 +0900274 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200275 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100276
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200277 if (ff->flock) {
278 struct fuse_release_in *inarg = &req->misc.release.in;
279 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
280 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
281 (fl_owner_t) file);
282 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100283 /* Hold inode until release is finished */
284 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700285
Tejun Heo6b2db282009-04-14 10:54:49 +0900286 /*
287 * Normally this will send the RELEASE request, however if
288 * some asynchronous READ or WRITE requests are outstanding,
289 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100290 *
291 * Make the release synchronous if this is a fuseblk mount,
292 * synchronous RELEASE is allowed (and desirable) in this case
293 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900294 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100295 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296}
297
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700298static int fuse_open(struct inode *inode, struct file *file)
299{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200300 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700301}
302
303static int fuse_release(struct inode *inode, struct file *file)
304{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400305 struct fuse_conn *fc = get_fuse_conn(inode);
306
307 /* see fuse_vma_close() for !writeback_cache case */
308 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200309 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400310
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200311 fuse_release_common(file, FUSE_RELEASE);
312
313 /* return value is ignored by VFS */
314 return 0;
315}
316
317void fuse_sync_release(struct fuse_file *ff, int flags)
318{
319 WARN_ON(atomic_read(&ff->count) > 1);
320 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200321 __set_bit(FR_FORCE, &ff->reserved_req->flags);
322 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200323 fuse_request_send(ff->fc, ff->reserved_req);
324 fuse_put_request(ff->fc, ff->reserved_req);
325 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700326}
Tejun Heo08cbf542009-04-14 10:54:53 +0900327EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700328
Miklos Szeredi71421252006-06-25 05:48:52 -0700329/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700330 * Scramble the ID space with XTEA, so that the value of the files_struct
331 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700332 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700333u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700334{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700335 u32 *k = fc->scramble_key;
336 u64 v = (unsigned long) id;
337 u32 v0 = v;
338 u32 v1 = v >> 32;
339 u32 sum = 0;
340 int i;
341
342 for (i = 0; i < 32; i++) {
343 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
344 sum += 0x9E3779B9;
345 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
346 }
347
348 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700349}
350
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700351/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400352 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700353 *
354 * This is currently done by walking the list of writepage requests
355 * for the inode, which can be pretty inefficient.
356 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400357static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
358 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700359{
360 struct fuse_conn *fc = get_fuse_conn(inode);
361 struct fuse_inode *fi = get_fuse_inode(inode);
362 struct fuse_req *req;
363 bool found = false;
364
365 spin_lock(&fc->lock);
366 list_for_each_entry(req, &fi->writepages, writepages_entry) {
367 pgoff_t curr_index;
368
369 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300370 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400371 if (idx_from < curr_index + req->num_pages &&
372 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700373 found = true;
374 break;
375 }
376 }
377 spin_unlock(&fc->lock);
378
379 return found;
380}
381
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400382static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
383{
384 return fuse_range_is_writeback(inode, index, index);
385}
386
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700387/*
388 * Wait for page writeback to be completed.
389 *
390 * Since fuse doesn't rely on the VM writeback tracking, this has to
391 * use some other means.
392 */
393static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
394{
395 struct fuse_inode *fi = get_fuse_inode(inode);
396
397 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
398 return 0;
399}
400
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400401/*
402 * Wait for all pending writepages on the inode to finish.
403 *
404 * This is currently done by blocking further writes with FUSE_NOWRITE
405 * and waiting for all sent writes to complete.
406 *
407 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
408 * could conflict with truncation.
409 */
410static void fuse_sync_writes(struct inode *inode)
411{
412 fuse_set_nowrite(inode);
413 fuse_release_nowrite(inode);
414}
415
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700416static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700417{
Al Viro6131ffa2013-02-27 16:59:05 -0500418 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700419 struct fuse_conn *fc = get_fuse_conn(inode);
420 struct fuse_file *ff = file->private_data;
421 struct fuse_req *req;
422 struct fuse_flush_in inarg;
423 int err;
424
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800425 if (is_bad_inode(inode))
426 return -EIO;
427
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700428 if (fc->no_flush)
429 return 0;
430
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200431 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400432 if (err)
433 return err;
434
Al Viro59551022016-01-22 15:40:57 -0500435 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400436 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500437 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400438
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200439 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700440 if (err)
441 return err;
442
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400443 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 memset(&inarg, 0, sizeof(inarg));
445 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700446 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 req->in.h.opcode = FUSE_FLUSH;
448 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 req->in.numargs = 1;
450 req->in.args[0].size = sizeof(inarg);
451 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200452 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100453 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700454 err = req->out.h.error;
455 fuse_put_request(fc, req);
456 if (err == -ENOSYS) {
457 fc->no_flush = 1;
458 err = 0;
459 }
460 return err;
461}
462
Josef Bacik02c24a82011-07-16 20:44:56 -0400463int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
464 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700465{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200466 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467 struct fuse_conn *fc = get_fuse_conn(inode);
468 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100469 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700470 struct fuse_fsync_in inarg;
471 int err;
472
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800473 if (is_bad_inode(inode))
474 return -EIO;
475
Al Viro59551022016-01-22 15:40:57 -0500476 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400477
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700478 /*
479 * Start writeback against all dirty pages of the inode, then
480 * wait for all outstanding writes, before sending the FSYNC
481 * request.
482 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200483 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700484 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400485 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700486
487 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700488
489 /*
490 * Due to implementation of fuse writeback
491 * filemap_write_and_wait_range() does not catch errors.
492 * We have to do this directly after fuse_sync_writes()
493 */
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200494 err = filemap_check_errors(file->f_mapping);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700495 if (err)
496 goto out;
497
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200498 err = sync_inode_metadata(inode, 1);
499 if (err)
500 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700501
Miklos Szeredi22401e72014-04-28 14:19:23 +0200502 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
503 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400504
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505 memset(&inarg, 0, sizeof(inarg));
506 inarg.fh = ff->fh;
507 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100508 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
509 args.in.h.nodeid = get_node_id(inode);
510 args.in.numargs = 1;
511 args.in.args[0].size = sizeof(inarg);
512 args.in.args[0].value = &inarg;
513 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700515 if (isdir)
516 fc->no_fsyncdir = 1;
517 else
518 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519 err = 0;
520 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400521out:
Al Viro59551022016-01-22 15:40:57 -0500522 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700523 return err;
524}
525
Josef Bacik02c24a82011-07-16 20:44:56 -0400526static int fuse_fsync(struct file *file, loff_t start, loff_t end,
527 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700528{
Josef Bacik02c24a82011-07-16 20:44:56 -0400529 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700530}
531
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200532void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
533 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700534{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700535 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800536 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700537
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800538 inarg->fh = ff->fh;
539 inarg->offset = pos;
540 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800541 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800542 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200543 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700544 req->in.numargs = 1;
545 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800546 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700547 req->out.argvar = 1;
548 req->out.numargs = 1;
549 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700550}
551
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200552static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400553{
554 unsigned i;
555
556 for (i = 0; i < req->num_pages; i++) {
557 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200558 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400559 set_page_dirty_lock(page);
560 put_page(page);
561 }
562}
563
Seth Forshee744742d2016-03-11 10:35:34 -0600564static void fuse_io_release(struct kref *kref)
565{
566 kfree(container_of(kref, struct fuse_io_priv, refcnt));
567}
568
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100569static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
570{
571 if (io->err)
572 return io->err;
573
574 if (io->bytes >= 0 && io->write)
575 return -EIO;
576
577 return io->bytes < 0 ? io->size : io->bytes;
578}
579
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400580/**
581 * In case of short read, the caller sets 'pos' to the position of
582 * actual end of fuse request in IO request. Otherwise, if bytes_requested
583 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
584 *
585 * An example:
586 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
587 * both submitted asynchronously. The first of them was ACKed by userspace as
588 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
589 * second request was ACKed as short, e.g. only 1K was read, resulting in
590 * pos == 33K.
591 *
592 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
593 * will be equal to the length of the longest contiguous fragment of
594 * transferred data starting from the beginning of IO request.
595 */
596static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
597{
598 int left;
599
600 spin_lock(&io->lock);
601 if (err)
602 io->err = io->err ? : err;
603 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
604 io->bytes = pos;
605
606 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530607 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100608 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400609 spin_unlock(&io->lock);
610
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530611 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100612 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400613
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100614 if (res >= 0) {
615 struct inode *inode = file_inode(io->iocb->ki_filp);
616 struct fuse_conn *fc = get_fuse_conn(inode);
617 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400618
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100619 spin_lock(&fc->lock);
620 fi->attr_version = ++fc->attr_version;
621 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400622 }
623
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100624 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400625 }
Seth Forshee744742d2016-03-11 10:35:34 -0600626
627 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400628}
629
630static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
631{
632 struct fuse_io_priv *io = req->io;
633 ssize_t pos = -1;
634
635 fuse_release_user_pages(req, !io->write);
636
637 if (io->write) {
638 if (req->misc.write.in.size != req->misc.write.out.size)
639 pos = req->misc.write.in.offset - io->offset +
640 req->misc.write.out.size;
641 } else {
642 if (req->misc.read.in.size != req->out.args[0].size)
643 pos = req->misc.read.in.offset - io->offset +
644 req->out.args[0].size;
645 }
646
647 fuse_aio_complete(io, req->out.h.error, pos);
648}
649
650static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
651 size_t num_bytes, struct fuse_io_priv *io)
652{
653 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600654 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400655 io->size += num_bytes;
656 io->reqs++;
657 spin_unlock(&io->lock);
658
659 req->io = io;
660 req->end = fuse_aio_complete_req;
661
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400662 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400663 fuse_request_send_background(fc, req);
664
665 return num_bytes;
666}
667
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400668static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200669 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700670{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400671 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200672 struct fuse_file *ff = file->private_data;
673 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700674
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200675 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700676 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700677 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700678
679 inarg->read_flags |= FUSE_READ_LOCKOWNER;
680 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
681 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400682
683 if (io->async)
684 return fuse_async_req_send(fc, req, count, io);
685
Tejun Heob93f8582008-11-26 12:03:55 +0100686 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800687 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700688}
689
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700690static void fuse_read_update_size(struct inode *inode, loff_t size,
691 u64 attr_ver)
692{
693 struct fuse_conn *fc = get_fuse_conn(inode);
694 struct fuse_inode *fi = get_fuse_inode(inode);
695
696 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400697 if (attr_ver == fi->attr_version && size < inode->i_size &&
698 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700699 fi->attr_version = ++fc->attr_version;
700 i_size_write(inode, size);
701 }
702 spin_unlock(&fc->lock);
703}
704
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400705static void fuse_short_read(struct fuse_req *req, struct inode *inode,
706 u64 attr_ver)
707{
708 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400709 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710
Pavel Emelyanov83732002013-10-10 17:10:46 +0400711 if (fc->writeback_cache) {
712 /*
713 * A hole in a file. Some data after the hole are in page cache,
714 * but have not reached the client fs yet. So, the hole is not
715 * present there.
716 */
717 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300718 int start_idx = num_read >> PAGE_SHIFT;
719 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400720
721 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300722 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400723 off = 0;
724 }
725 } else {
726 loff_t pos = page_offset(req->pages[0]) + num_read;
727 fuse_read_update_size(inode, pos, attr_ver);
728 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400729}
730
Maxim Patlasov482fce52013-10-10 17:11:25 +0400731static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700732{
Seth Forshee744742d2016-03-11 10:35:34 -0600733 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734 struct inode *inode = page->mapping->host;
735 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800736 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700737 size_t num_read;
738 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700740 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800741 int err;
742
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700743 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300744 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700745 * page-cache page, so make sure we read a properly synced
746 * page.
747 */
748 fuse_wait_on_page_writeback(inode, page->index);
749
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400750 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700751 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400752 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 attr_ver = fuse_get_attr_version(fc);
755
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200757 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700758 req->num_pages = 1;
759 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400760 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400761 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700763
764 if (!err) {
765 /*
766 * Short read means EOF. If file size is larger, truncate it
767 */
768 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400769 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700770
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700771 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700772 }
773
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400774 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400775
776 return err;
777}
778
779static int fuse_readpage(struct file *file, struct page *page)
780{
781 struct inode *inode = page->mapping->host;
782 int err;
783
784 err = -EIO;
785 if (is_bad_inode(inode))
786 goto out;
787
788 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800789 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700790 out:
791 unlock_page(page);
792 return err;
793}
794
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800795static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700796{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800797 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700798 size_t count = req->misc.read.in.size;
799 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200800 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800801
Miklos Szeredice534fb2010-05-25 15:06:07 +0200802 for (i = 0; mapping == NULL && i < req->num_pages; i++)
803 mapping = req->pages[i]->mapping;
804
805 if (mapping) {
806 struct inode *inode = mapping->host;
807
808 /*
809 * Short read means EOF. If file size is larger, truncate it
810 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400811 if (!req->out.h.error && num_read < count)
812 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200813
Andrew Gallagher451418f2013-11-05 03:55:43 -0800814 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700815 }
816
Miklos Szeredidb50b962005-09-09 13:10:33 -0700817 for (i = 0; i < req->num_pages; i++) {
818 struct page *page = req->pages[i];
819 if (!req->out.h.error)
820 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800821 else
822 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700823 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300824 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700825 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700826 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100827 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800828}
829
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200830static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200832 struct fuse_file *ff = file->private_data;
833 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800834 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300835 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200836
837 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800838 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200839 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200840 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700841 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800842 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700843 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800844 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100845 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800846 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100847 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800848 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100849 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800850 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851}
852
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700853struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800855 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400857 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858};
859
860static int fuse_readpages_fill(void *_data, struct page *page)
861{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700862 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863 struct fuse_req *req = data->req;
864 struct inode *inode = data->inode;
865 struct fuse_conn *fc = get_fuse_conn(inode);
866
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700867 fuse_wait_on_page_writeback(inode, page->index);
868
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 if (req->num_pages &&
870 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300871 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700872 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400873 int nr_alloc = min_t(unsigned, data->nr_pages,
874 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200875 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400876 if (fc->async_read)
877 req = fuse_get_req_for_background(fc, nr_alloc);
878 else
879 req = fuse_get_req(fc, nr_alloc);
880
881 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700882 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700883 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700884 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700885 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400887
888 if (WARN_ON(req->num_pages >= req->max_pages)) {
889 fuse_put_request(fc, req);
890 return -EIO;
891 }
892
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300893 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700894 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400895 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100896 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400897 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700898 return 0;
899}
900
901static int fuse_readpages(struct file *file, struct address_space *mapping,
902 struct list_head *pages, unsigned nr_pages)
903{
904 struct inode *inode = mapping->host;
905 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700906 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400908 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800909
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700910 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800911 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800912 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800913
Miklos Szeredia6643092007-11-28 16:22:00 -0800914 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400916 if (fc->async_read)
917 data.req = fuse_get_req_for_background(fc, nr_alloc);
918 else
919 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400920 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700921 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700922 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800923 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700924
925 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700926 if (!err) {
927 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200928 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700929 else
930 fuse_put_request(fc, data.req);
931 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800932out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700933 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700934}
935
Al Viro37c20f12014-04-02 14:47:09 -0400936static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800937{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530938 ssize_t ret_val;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800939 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400940 struct fuse_conn *fc = get_fuse_conn(inode);
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530941 struct fuse_file *ff = iocb->ki_filp->private_data;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942
Brian Fostera8894272012-07-16 15:23:50 -0400943 /*
944 * In auto invalidate mode, always update attributes on read.
945 * Otherwise, only update if we attempt to read past EOF (to ensure
946 * i_size is up to date).
947 */
948 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400949 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800950 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800951 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
952 if (err)
953 return err;
954 }
955
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530956 if (ff && ff->passthrough_enabled && ff->passthrough_filp)
957 ret_val = fuse_passthrough_read_iter(iocb, to);
958 else
959 ret_val = generic_file_read_iter(iocb, to);
960
961 return ret_val;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800962}
963
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200964static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200965 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700966{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700967 struct fuse_write_in *inarg = &req->misc.write.in;
968 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700969
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700970 inarg->fh = ff->fh;
971 inarg->offset = pos;
972 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700973 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200974 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700975 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200976 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700977 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
978 else
979 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700980 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700981 req->in.args[1].size = count;
982 req->out.numargs = 1;
983 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700984 req->out.args[0].value = outarg;
985}
986
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400987static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200988 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700989{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400990 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200991 struct fuse_file *ff = file->private_data;
992 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200993 struct fuse_write_in *inarg = &req->misc.write.in;
994
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200995 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200996 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700997 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700998 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
999 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1000 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001001
1002 if (io->async)
1003 return fuse_async_req_send(fc, req, count, io);
1004
Tejun Heob93f8582008-11-26 12:03:55 +01001005 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001006 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001007}
1008
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001009bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001010{
1011 struct fuse_conn *fc = get_fuse_conn(inode);
1012 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001014
1015 spin_lock(&fc->lock);
1016 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001017 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001018 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001019 ret = true;
1020 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001021 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001022
1023 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001024}
1025
Nick Pigginea9b9902008-04-30 00:54:42 -07001026static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1027 struct inode *inode, loff_t pos,
1028 size_t count)
1029{
1030 size_t res;
1031 unsigned offset;
1032 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001033 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001034
1035 for (i = 0; i < req->num_pages; i++)
1036 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1037
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001038 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001039
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001040 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001041 count = res;
1042 for (i = 0; i < req->num_pages; i++) {
1043 struct page *page = req->pages[i];
1044
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001045 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001046 SetPageUptodate(page);
1047
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001048 if (count > PAGE_SIZE - offset)
1049 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001050 else
1051 count = 0;
1052 offset = 0;
1053
1054 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001056 }
1057
1058 return res;
1059}
1060
1061static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1062 struct address_space *mapping,
1063 struct iov_iter *ii, loff_t pos)
1064{
1065 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001066 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001067 size_t count = 0;
1068 int err;
1069
Miklos Szeredif4975c62009-04-02 14:25:34 +02001070 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001071 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001072
1073 do {
1074 size_t tmp;
1075 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001076 pgoff_t index = pos >> PAGE_SHIFT;
1077 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001078 iov_iter_count(ii));
1079
1080 bytes = min_t(size_t, bytes, fc->max_write - count);
1081
1082 again:
1083 err = -EFAULT;
1084 if (iov_iter_fault_in_readable(ii, bytes))
1085 break;
1086
1087 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001088 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 if (!page)
1090 break;
1091
anfei zhou931e80e2010-02-02 13:44:02 -08001092 if (mapping_writably_mapped(mapping))
1093 flush_dcache_page(page);
1094
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 flush_dcache_page(page);
1097
Roman Gushchin3ca81382015-10-12 16:33:44 +03001098 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 if (!tmp) {
1100 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001101 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 bytes = min(bytes, iov_iter_single_seg_count(ii));
1103 goto again;
1104 }
1105
1106 err = 0;
1107 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001108 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 req->num_pages++;
1110
Nick Pigginea9b9902008-04-30 00:54:42 -07001111 count += tmp;
1112 pos += tmp;
1113 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001114 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 offset = 0;
1116
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001117 if (!fc->big_writes)
1118 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001119 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001120 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001121
1122 return count > 0 ? count : err;
1123}
1124
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001125static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1126{
1127 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001128 ((pos + len - 1) >> PAGE_SHIFT) -
1129 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001130 FUSE_MAX_PAGES_PER_REQ);
1131}
1132
Nick Pigginea9b9902008-04-30 00:54:42 -07001133static ssize_t fuse_perform_write(struct file *file,
1134 struct address_space *mapping,
1135 struct iov_iter *ii, loff_t pos)
1136{
1137 struct inode *inode = mapping->host;
1138 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001139 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001140 int err = 0;
1141 ssize_t res = 0;
1142
1143 if (is_bad_inode(inode))
1144 return -EIO;
1145
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001146 if (inode->i_size < pos + iov_iter_count(ii))
1147 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1148
Nick Pigginea9b9902008-04-30 00:54:42 -07001149 do {
1150 struct fuse_req *req;
1151 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001152 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001153
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001154 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001155 if (IS_ERR(req)) {
1156 err = PTR_ERR(req);
1157 break;
1158 }
1159
1160 count = fuse_fill_write_pages(req, mapping, ii, pos);
1161 if (count <= 0) {
1162 err = count;
1163 } else {
1164 size_t num_written;
1165
1166 num_written = fuse_send_write_pages(req, file, inode,
1167 pos, count);
1168 err = req->out.h.error;
1169 if (!err) {
1170 res += num_written;
1171 pos += num_written;
1172
1173 /* break out of the loop on short write */
1174 if (num_written != count)
1175 err = -EIO;
1176 }
1177 }
1178 fuse_put_request(fc, req);
1179 } while (!err && iov_iter_count(ii));
1180
1181 if (res > 0)
1182 fuse_write_update_size(inode, pos);
1183
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001184 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001185 fuse_invalidate_attr(inode);
1186
1187 return res > 0 ? res : err;
1188}
1189
Al Viro84c3d552014-04-03 14:33:23 -04001190static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001191{
1192 struct file *file = iocb->ki_filp;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301193 struct fuse_file *ff = file->private_data;
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001196 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001197 struct inode *inode = mapping->host;
1198 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001199 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001200
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001201 if (get_fuse_conn(inode)->writeback_cache) {
1202 /* Update size (EOF optimization) and mode (SUID clearing) */
1203 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1204 if (err)
1205 return err;
1206
Al Viro84c3d552014-04-03 14:33:23 -04001207 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001208 }
1209
Al Viro59551022016-01-22 15:40:57 -05001210 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
1212 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001213 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001214
Al Viro3309dd02015-04-09 12:55:47 -04001215 err = generic_write_checks(iocb, from);
1216 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001217 goto out;
1218
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001219 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001220 if (err)
1221 goto out;
1222
Josef Bacikc3b2da32012-03-26 09:59:21 -04001223 err = file_update_time(file);
1224 if (err)
1225 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001226
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301227 if (ff && ff->passthrough_enabled && ff->passthrough_filp) {
1228 written = fuse_passthrough_write_iter(iocb, from);
1229 goto out;
1230 }
1231
Al Viro2ba48ce2015-04-09 13:52:01 -04001232 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001233 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001234 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001235 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001236 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001237
Anand Avati4273b792012-02-17 12:46:25 -05001238 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001239
Al Viro84c3d552014-04-03 14:33:23 -04001240 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001241 if (written_buffered < 0) {
1242 err = written_buffered;
1243 goto out;
1244 }
1245 endbyte = pos + written_buffered - 1;
1246
1247 err = filemap_write_and_wait_range(file->f_mapping, pos,
1248 endbyte);
1249 if (err)
1250 goto out;
1251
1252 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001253 pos >> PAGE_SHIFT,
1254 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001255
1256 written += written_buffered;
1257 iocb->ki_pos = pos + written_buffered;
1258 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001259 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001260 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001261 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001262 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001263out:
1264 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001265 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001266
1267 return written ? written : err;
1268}
1269
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001270static inline void fuse_page_descs_length_init(struct fuse_req *req,
1271 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001272{
1273 int i;
1274
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001275 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001276 req->page_descs[i].length = PAGE_SIZE -
1277 req->page_descs[i].offset;
1278}
1279
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001280static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1281{
1282 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1283}
1284
1285static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1286 size_t max_size)
1287{
1288 return min(iov_iter_single_seg_count(ii), max_size);
1289}
1290
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001291static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001292 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001293{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001294 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001295 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001296
Miklos Szeredif4975c62009-04-02 14:25:34 +02001297 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001298 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001299 unsigned long user_addr = fuse_get_user_addr(ii);
1300 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1301
Miklos Szeredif4975c62009-04-02 14:25:34 +02001302 if (write)
1303 req->in.args[1].value = (void *) user_addr;
1304 else
1305 req->out.args[0].value = (void *) user_addr;
1306
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001307 iov_iter_advance(ii, frag_size);
1308 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001309 return 0;
1310 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001311
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001312 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001313 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001314 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001315 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001316 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001317 req->max_pages - req->num_pages,
1318 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001319 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001320 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001321
Al Viroc9c37e22014-03-16 16:08:30 -04001322 iov_iter_advance(ii, ret);
1323 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001324
Al Viroc9c37e22014-03-16 16:08:30 -04001325 ret += start;
1326 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1327
1328 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001329 fuse_page_descs_length_init(req, req->num_pages, npages);
1330
1331 req->num_pages += npages;
1332 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001333 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001334 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001335
1336 if (write)
1337 req->in.argpages = 1;
1338 else
1339 req->out.argpages = 1;
1340
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001341 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001342
Ashish Samant2c932d42016-03-25 10:53:41 -07001343 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001344}
1345
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001346static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1347{
Al Virof67da302014-03-19 01:16:16 -04001348 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001349}
1350
Al Virod22a9432014-03-16 15:50:47 -04001351ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1352 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001353{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001354 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001355 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001356 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001357 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001358 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001359 struct fuse_file *ff = file->private_data;
1360 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 size_t nmax = write ? fc->max_write : fc->max_read;
1362 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001363 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001364 pgoff_t idx_from = pos >> PAGE_SHIFT;
1365 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001366 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001367 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001368 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001369
Brian Fosterde82b922013-05-14 11:25:39 -04001370 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001371 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001372 else
Al Virod22a9432014-03-16 15:50:47 -04001373 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001374 if (IS_ERR(req))
1375 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001377 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1378 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001379 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001380 fuse_sync_writes(inode);
1381 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001382 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001383 }
1384
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001385 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001386 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001387 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001388 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001389 err = fuse_get_user_pages(req, iter, &nbytes, write);
1390 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001392
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001393 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001394 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001395 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001396 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001397
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001398 if (!io->async)
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001399 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001401 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001402 break;
1403 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001404 res = 0;
1405 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001406 break;
1407 }
1408 count -= nres;
1409 res += nres;
1410 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001411 if (nres != nbytes)
1412 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001413 if (count) {
1414 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001415 if (io->async)
1416 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001417 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001418 else
Al Virod22a9432014-03-16 15:50:47 -04001419 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001420 if (IS_ERR(req))
1421 break;
1422 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001423 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001424 if (!IS_ERR(req))
1425 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001426 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001428
Ashish Samant742f9922016-03-14 21:57:35 -07001429 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001430}
Tejun Heo08cbf542009-04-14 10:54:53 +09001431EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001432
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001433static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001434 struct iov_iter *iter,
1435 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001437 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001438 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001439 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001440
1441 if (is_bad_inode(inode))
1442 return -EIO;
1443
Al Virod22a9432014-03-16 15:50:47 -04001444 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001445
1446 fuse_invalidate_attr(inode);
1447
1448 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001449}
1450
Al Viro15316262015-03-30 22:08:36 -04001451static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001452{
Seth Forshee744742d2016-03-11 10:35:34 -06001453 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001454 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001455}
1456
Al Viro15316262015-03-30 22:08:36 -04001457static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001458{
Al Viro15316262015-03-30 22:08:36 -04001459 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001460 struct inode *inode = file_inode(file);
Seth Forshee744742d2016-03-11 10:35:34 -06001461 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001462 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001463
1464 if (is_bad_inode(inode))
1465 return -EIO;
1466
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001467 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001468 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001469 res = generic_write_checks(iocb, from);
1470 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001471 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001472 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001473 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001474 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001475 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001476
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001477 return res;
1478}
1479
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001480static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001481{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001482 int i;
1483
1484 for (i = 0; i < req->num_pages; i++)
1485 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001486
1487 if (req->ff)
1488 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001489}
1490
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001491static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001492{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493 struct inode *inode = req->inode;
1494 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001495 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001496 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497
1498 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001499 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001500 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001501 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001502 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001503 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 wake_up(&fi->page_waitq);
1505}
1506
1507/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001508static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1509 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001510__releases(fc->lock)
1511__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512{
1513 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001514 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001515 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001516
1517 if (!fc->connected)
1518 goto out_free;
1519
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001520 if (inarg->offset + data_size <= size) {
1521 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001522 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001523 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001524 } else {
1525 /* Got truncated off completely */
1526 goto out_free;
1527 }
1528
1529 req->in.args[1].size = inarg->size;
1530 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001531 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001532 return;
1533
1534 out_free:
1535 fuse_writepage_finish(fc, req);
1536 spin_unlock(&fc->lock);
1537 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001538 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539 spin_lock(&fc->lock);
1540}
1541
1542/*
1543 * If fi->writectr is positive (no truncate or fsync going on) send
1544 * all queued writepage requests.
1545 *
1546 * Called with fc->lock
1547 */
1548void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001549__releases(fc->lock)
1550__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001551{
1552 struct fuse_conn *fc = get_fuse_conn(inode);
1553 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001554 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001555 struct fuse_req *req;
1556
1557 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1558 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1559 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001560 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001561 }
1562}
1563
1564static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1565{
1566 struct inode *inode = req->inode;
1567 struct fuse_inode *fi = get_fuse_inode(inode);
1568
1569 mapping_set_error(inode->i_mapping, req->out.h.error);
1570 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001571 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001572 struct fuse_conn *fc = get_fuse_conn(inode);
1573 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001574 struct fuse_req *next = req->misc.write.next;
1575 req->misc.write.next = next->misc.write.next;
1576 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001577 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001578 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001579
1580 /*
1581 * Skip fuse_flush_writepages() to make it easy to crop requests
1582 * based on primary request size.
1583 *
1584 * 1st case (trivial): there are no concurrent activities using
1585 * fuse_set/release_nowrite. Then we're on safe side because
1586 * fuse_flush_writepages() would call fuse_send_writepage()
1587 * anyway.
1588 *
1589 * 2nd case: someone called fuse_set_nowrite and it is waiting
1590 * now for completion of all in-flight requests. This happens
1591 * rarely and no more than once per page, so this should be
1592 * okay.
1593 *
1594 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1595 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1596 * that fuse_set_nowrite returned implies that all in-flight
1597 * requests were completed along with all of their secondary
1598 * requests. Further primary requests are blocked by negative
1599 * writectr. Hence there cannot be any in-flight requests and
1600 * no invocations of fuse_writepage_end() while we're in
1601 * fuse_set_nowrite..fuse_release_nowrite section.
1602 */
1603 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001604 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001605 fi->writectr--;
1606 fuse_writepage_finish(fc, req);
1607 spin_unlock(&fc->lock);
1608 fuse_writepage_free(fc, req);
1609}
1610
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001611static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1612 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001613{
Miklos Szeredi72523422013-10-01 16:44:52 +02001614 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001615
1616 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001617 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001618 ff = list_entry(fi->write_files.next, struct fuse_file,
1619 write_entry);
1620 fuse_file_get(ff);
1621 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001622 spin_unlock(&fc->lock);
1623
1624 return ff;
1625}
1626
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001627static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1628 struct fuse_inode *fi)
1629{
1630 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1631 WARN_ON(!ff);
1632 return ff;
1633}
1634
1635int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1636{
1637 struct fuse_conn *fc = get_fuse_conn(inode);
1638 struct fuse_inode *fi = get_fuse_inode(inode);
1639 struct fuse_file *ff;
1640 int err;
1641
1642 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001643 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001644 if (ff)
1645 fuse_file_put(ff, 0);
1646
1647 return err;
1648}
1649
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650static int fuse_writepage_locked(struct page *page)
1651{
1652 struct address_space *mapping = page->mapping;
1653 struct inode *inode = mapping->host;
1654 struct fuse_conn *fc = get_fuse_conn(inode);
1655 struct fuse_inode *fi = get_fuse_inode(inode);
1656 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001658 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659
1660 set_page_writeback(page);
1661
Maxim Patlasov4250c062012-10-26 19:48:07 +04001662 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663 if (!req)
1664 goto err;
1665
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001666 /* writeback always goes to bg_queue */
1667 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001668 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1669 if (!tmp_page)
1670 goto err_free;
1671
Miklos Szeredi72523422013-10-01 16:44:52 +02001672 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001673 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001674 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001675 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001676
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001677 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001678
1679 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001680 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001681 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001682 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683 req->num_pages = 1;
1684 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001685 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001686 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001687 req->end = fuse_writepage_end;
1688 req->inode = inode;
1689
Tejun Heo93f78d82015-05-22 17:13:27 -04001690 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001691 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001692
1693 spin_lock(&fc->lock);
1694 list_add(&req->writepages_entry, &fi->writepages);
1695 list_add_tail(&req->list, &fi->queued_writes);
1696 fuse_flush_writepages(inode);
1697 spin_unlock(&fc->lock);
1698
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001699 end_page_writeback(page);
1700
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001701 return 0;
1702
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001703err_nofile:
1704 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001705err_free:
1706 fuse_request_free(req);
1707err:
1708 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001709 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001710}
1711
1712static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1713{
1714 int err;
1715
Miklos Szerediff17be02013-10-01 16:44:53 +02001716 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1717 /*
1718 * ->writepages() should be called for sync() and friends. We
1719 * should only get here on direct reclaim and then we are
1720 * allowed to skip a page which is already in flight
1721 */
1722 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1723
1724 redirty_page_for_writepage(wbc, page);
1725 return 0;
1726 }
1727
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001728 err = fuse_writepage_locked(page);
1729 unlock_page(page);
1730
1731 return err;
1732}
1733
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001734struct fuse_fill_wb_data {
1735 struct fuse_req *req;
1736 struct fuse_file *ff;
1737 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001738 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001739};
1740
1741static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1742{
1743 struct fuse_req *req = data->req;
1744 struct inode *inode = data->inode;
1745 struct fuse_conn *fc = get_fuse_conn(inode);
1746 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001747 int num_pages = req->num_pages;
1748 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001749
1750 req->ff = fuse_file_get(data->ff);
1751 spin_lock(&fc->lock);
1752 list_add_tail(&req->list, &fi->queued_writes);
1753 fuse_flush_writepages(inode);
1754 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001755
1756 for (i = 0; i < num_pages; i++)
1757 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001758}
1759
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001760static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1761 struct page *page)
1762{
1763 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1764 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1765 struct fuse_req *tmp;
1766 struct fuse_req *old_req;
1767 bool found = false;
1768 pgoff_t curr_index;
1769
1770 BUG_ON(new_req->num_pages != 0);
1771
1772 spin_lock(&fc->lock);
1773 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001774 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1775 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001776 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001777 if (curr_index <= page->index &&
1778 page->index < curr_index + old_req->num_pages) {
1779 found = true;
1780 break;
1781 }
1782 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001783 if (!found) {
1784 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001785 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001786 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001787
Maxim Patlasovf6011082013-10-02 15:01:07 +04001788 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001789 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1790 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001791 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001792 if (tmp->num_pages == 1 &&
1793 curr_index == page->index) {
1794 old_req = tmp;
1795 }
1796 }
1797
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001798 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001799 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001800
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001801 copy_highpage(old_req->pages[0], page);
1802 spin_unlock(&fc->lock);
1803
Tejun Heo93f78d82015-05-22 17:13:27 -04001804 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001805 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001806 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001807 fuse_writepage_free(fc, new_req);
1808 fuse_request_free(new_req);
1809 goto out;
1810 } else {
1811 new_req->misc.write.next = old_req->misc.write.next;
1812 old_req->misc.write.next = new_req;
1813 }
1814out_unlock:
1815 spin_unlock(&fc->lock);
1816out:
1817 return found;
1818}
1819
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001820static int fuse_writepages_fill(struct page *page,
1821 struct writeback_control *wbc, void *_data)
1822{
1823 struct fuse_fill_wb_data *data = _data;
1824 struct fuse_req *req = data->req;
1825 struct inode *inode = data->inode;
1826 struct fuse_conn *fc = get_fuse_conn(inode);
1827 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001828 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001829 int err;
1830
1831 if (!data->ff) {
1832 err = -EIO;
1833 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1834 if (!data->ff)
1835 goto out_unlock;
1836 }
1837
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001838 /*
1839 * Being under writeback is unlikely but possible. For example direct
1840 * read to an mmaped fuse file will set the page dirty twice; once when
1841 * the pages are faulted with get_user_pages(), and then after the read
1842 * completed.
1843 */
1844 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001845
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001846 if (req && req->num_pages &&
1847 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001848 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001849 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1850 fuse_writepages_send(data);
1851 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001852 }
1853 err = -ENOMEM;
1854 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1855 if (!tmp_page)
1856 goto out_unlock;
1857
1858 /*
1859 * The page must not be redirtied until the writeout is completed
1860 * (i.e. userspace has sent a reply to the write request). Otherwise
1861 * there could be more than one temporary page instance for each real
1862 * page.
1863 *
1864 * This is ensured by holding the page lock in page_mkwrite() while
1865 * checking fuse_page_is_writeback(). We already hold the page lock
1866 * since clear_page_dirty_for_io() and keep it held until we add the
1867 * request to the fi->writepages list and increment req->num_pages.
1868 * After this fuse_page_is_writeback() will indicate that the page is
1869 * under writeback, so we can release the page lock.
1870 */
1871 if (data->req == NULL) {
1872 struct fuse_inode *fi = get_fuse_inode(inode);
1873
1874 err = -ENOMEM;
1875 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1876 if (!req) {
1877 __free_page(tmp_page);
1878 goto out_unlock;
1879 }
1880
1881 fuse_write_fill(req, data->ff, page_offset(page), 0);
1882 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001883 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001884 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001885 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001886 req->num_pages = 0;
1887 req->end = fuse_writepage_end;
1888 req->inode = inode;
1889
1890 spin_lock(&fc->lock);
1891 list_add(&req->writepages_entry, &fi->writepages);
1892 spin_unlock(&fc->lock);
1893
1894 data->req = req;
1895 }
1896 set_page_writeback(page);
1897
1898 copy_highpage(tmp_page, page);
1899 req->pages[req->num_pages] = tmp_page;
1900 req->page_descs[req->num_pages].offset = 0;
1901 req->page_descs[req->num_pages].length = PAGE_SIZE;
1902
Tejun Heo93f78d82015-05-22 17:13:27 -04001903 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001904 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001905
1906 err = 0;
1907 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1908 end_page_writeback(page);
1909 data->req = NULL;
1910 goto out_unlock;
1911 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001912 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001913
1914 /*
1915 * Protected by fc->lock against concurrent access by
1916 * fuse_page_is_writeback().
1917 */
1918 spin_lock(&fc->lock);
1919 req->num_pages++;
1920 spin_unlock(&fc->lock);
1921
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001922out_unlock:
1923 unlock_page(page);
1924
1925 return err;
1926}
1927
1928static int fuse_writepages(struct address_space *mapping,
1929 struct writeback_control *wbc)
1930{
1931 struct inode *inode = mapping->host;
1932 struct fuse_fill_wb_data data;
1933 int err;
1934
1935 err = -EIO;
1936 if (is_bad_inode(inode))
1937 goto out;
1938
1939 data.inode = inode;
1940 data.req = NULL;
1941 data.ff = NULL;
1942
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001943 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001944 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1945 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001946 GFP_NOFS);
1947 if (!data.orig_pages)
1948 goto out;
1949
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001950 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1951 if (data.req) {
1952 /* Ignore errors if we can write at least one page */
1953 BUG_ON(!data.req->num_pages);
1954 fuse_writepages_send(&data);
1955 err = 0;
1956 }
1957 if (data.ff)
1958 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001959
1960 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001961out:
1962 return err;
1963}
1964
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001965/*
1966 * It's worthy to make sure that space is reserved on disk for the write,
1967 * but how to implement it without killing performance need more thinking.
1968 */
1969static int fuse_write_begin(struct file *file, struct address_space *mapping,
1970 loff_t pos, unsigned len, unsigned flags,
1971 struct page **pagep, void **fsdata)
1972{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001973 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001974 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001975 struct page *page;
1976 loff_t fsize;
1977 int err = -ENOMEM;
1978
1979 WARN_ON(!fc->writeback_cache);
1980
1981 page = grab_cache_page_write_begin(mapping, index, flags);
1982 if (!page)
1983 goto error;
1984
1985 fuse_wait_on_page_writeback(mapping->host, page->index);
1986
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001987 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001988 goto success;
1989 /*
1990 * Check if the start this page comes after the end of file, in which
1991 * case the readpage can be optimized away.
1992 */
1993 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001994 if (fsize <= (pos & PAGE_MASK)) {
1995 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001996 if (off)
1997 zero_user_segment(page, 0, off);
1998 goto success;
1999 }
2000 err = fuse_do_readpage(file, page);
2001 if (err)
2002 goto cleanup;
2003success:
2004 *pagep = page;
2005 return 0;
2006
2007cleanup:
2008 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002009 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002010error:
2011 return err;
2012}
2013
2014static int fuse_write_end(struct file *file, struct address_space *mapping,
2015 loff_t pos, unsigned len, unsigned copied,
2016 struct page *page, void *fsdata)
2017{
2018 struct inode *inode = page->mapping->host;
2019
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002020 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2021 if (!copied)
2022 goto unlock;
2023
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002024 if (!PageUptodate(page)) {
2025 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002026 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002027 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002029 SetPageUptodate(page);
2030 }
2031
2032 fuse_write_update_size(inode, pos + copied);
2033 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002034
2035unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002036 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002038
2039 return copied;
2040}
2041
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002042static int fuse_launder_page(struct page *page)
2043{
2044 int err = 0;
2045 if (clear_page_dirty_for_io(page)) {
2046 struct inode *inode = page->mapping->host;
2047 err = fuse_writepage_locked(page);
2048 if (!err)
2049 fuse_wait_on_page_writeback(inode, page->index);
2050 }
2051 return err;
2052}
2053
2054/*
2055 * Write back dirty pages now, because there may not be any suitable
2056 * open files later
2057 */
2058static void fuse_vma_close(struct vm_area_struct *vma)
2059{
2060 filemap_write_and_wait(vma->vm_file->f_mapping);
2061}
2062
2063/*
2064 * Wait for writeback against this page to complete before allowing it
2065 * to be marked dirty again, and hence written back again, possibly
2066 * before the previous writepage completed.
2067 *
2068 * Block here, instead of in ->writepage(), so that the userspace fs
2069 * can only block processes actually operating on the filesystem.
2070 *
2071 * Otherwise unprivileged userspace fs would be able to block
2072 * unrelated:
2073 *
2074 * - page migration
2075 * - sync(2)
2076 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2077 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002078static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002079{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002080 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002081 struct inode *inode = file_inode(vma->vm_file);
2082
2083 file_update_time(vma->vm_file);
2084 lock_page(page);
2085 if (page->mapping != inode->i_mapping) {
2086 unlock_page(page);
2087 return VM_FAULT_NOPAGE;
2088 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002089
2090 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002091 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002092}
2093
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002094static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002095 .close = fuse_vma_close,
2096 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002097 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002098 .page_mkwrite = fuse_page_mkwrite,
2099};
2100
2101static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2102{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05302103 struct fuse_file *ff = file->private_data;
2104
2105 ff->passthrough_enabled = 0;
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002106 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2107 fuse_link_write_file(file);
2108
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002109 file_accessed(file);
2110 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002111 return 0;
2112}
2113
Miklos Szeredifc280c92009-04-02 14:25:35 +02002114static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2115{
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05302116 struct fuse_file *ff = file->private_data;
2117
2118 ff->passthrough_enabled = 0;
Miklos Szeredifc280c92009-04-02 14:25:35 +02002119 /* Can't provide the coherency needed for MAP_SHARED */
2120 if (vma->vm_flags & VM_MAYSHARE)
2121 return -ENODEV;
2122
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002123 invalidate_inode_pages2(file->f_mapping);
2124
Miklos Szeredifc280c92009-04-02 14:25:35 +02002125 return generic_file_mmap(file, vma);
2126}
2127
Miklos Szeredi71421252006-06-25 05:48:52 -07002128static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2129 struct file_lock *fl)
2130{
2131 switch (ffl->type) {
2132 case F_UNLCK:
2133 break;
2134
2135 case F_RDLCK:
2136 case F_WRLCK:
2137 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2138 ffl->end < ffl->start)
2139 return -EIO;
2140
2141 fl->fl_start = ffl->start;
2142 fl->fl_end = ffl->end;
2143 fl->fl_pid = ffl->pid;
2144 break;
2145
2146 default:
2147 return -EIO;
2148 }
2149 fl->fl_type = ffl->type;
2150 return 0;
2151}
2152
Miklos Szeredi70781872014-12-12 09:49:05 +01002153static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002154 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002155 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002156{
Al Viro6131ffa2013-02-27 16:59:05 -05002157 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002158 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002159 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002160
Miklos Szeredi70781872014-12-12 09:49:05 +01002161 memset(inarg, 0, sizeof(*inarg));
2162 inarg->fh = ff->fh;
2163 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2164 inarg->lk.start = fl->fl_start;
2165 inarg->lk.end = fl->fl_end;
2166 inarg->lk.type = fl->fl_type;
2167 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002168 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002169 inarg->lk_flags |= FUSE_LK_FLOCK;
2170 args->in.h.opcode = opcode;
2171 args->in.h.nodeid = get_node_id(inode);
2172 args->in.numargs = 1;
2173 args->in.args[0].size = sizeof(*inarg);
2174 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002175}
2176
2177static int fuse_getlk(struct file *file, struct file_lock *fl)
2178{
Al Viro6131ffa2013-02-27 16:59:05 -05002179 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002180 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002181 FUSE_ARGS(args);
2182 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002183 struct fuse_lk_out outarg;
2184 int err;
2185
Miklos Szeredi70781872014-12-12 09:49:05 +01002186 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2187 args.out.numargs = 1;
2188 args.out.args[0].size = sizeof(outarg);
2189 args.out.args[0].value = &outarg;
2190 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 if (!err)
2192 err = convert_fuse_file_lock(&outarg.lk, fl);
2193
2194 return err;
2195}
2196
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002197static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002198{
Al Viro6131ffa2013-02-27 16:59:05 -05002199 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002201 FUSE_ARGS(args);
2202 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002203 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2204 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2205 int err;
2206
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002207 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002208 /* NLM needs asynchronous locks, which we don't support yet */
2209 return -ENOLCK;
2210 }
2211
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 /* Unlock on close is handled by the flush method */
2213 if (fl->fl_flags & FL_CLOSE)
2214 return 0;
2215
Miklos Szeredi70781872014-12-12 09:49:05 +01002216 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2217 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002218
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002219 /* locking is restartable */
2220 if (err == -EINTR)
2221 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002222
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 return err;
2224}
2225
2226static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2227{
Al Viro6131ffa2013-02-27 16:59:05 -05002228 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002229 struct fuse_conn *fc = get_fuse_conn(inode);
2230 int err;
2231
Miklos Szeredi48e90762008-07-25 01:49:02 -07002232 if (cmd == F_CANCELLK) {
2233 err = 0;
2234 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002235 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002236 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002237 err = 0;
2238 } else
2239 err = fuse_getlk(file, fl);
2240 } else {
2241 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002242 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002243 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002244 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002245 }
2246 return err;
2247}
2248
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002249static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2250{
Al Viro6131ffa2013-02-27 16:59:05 -05002251 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002252 struct fuse_conn *fc = get_fuse_conn(inode);
2253 int err;
2254
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002255 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002256 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002257 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002258 struct fuse_file *ff = file->private_data;
2259
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002260 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002261 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002262 err = fuse_setlk(file, fl, 1);
2263 }
2264
2265 return err;
2266}
2267
Miklos Szeredib2d22722006-12-06 20:35:51 -08002268static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2269{
2270 struct inode *inode = mapping->host;
2271 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002272 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002273 struct fuse_bmap_in inarg;
2274 struct fuse_bmap_out outarg;
2275 int err;
2276
2277 if (!inode->i_sb->s_bdev || fc->no_bmap)
2278 return 0;
2279
Miklos Szeredib2d22722006-12-06 20:35:51 -08002280 memset(&inarg, 0, sizeof(inarg));
2281 inarg.block = block;
2282 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002283 args.in.h.opcode = FUSE_BMAP;
2284 args.in.h.nodeid = get_node_id(inode);
2285 args.in.numargs = 1;
2286 args.in.args[0].size = sizeof(inarg);
2287 args.in.args[0].value = &inarg;
2288 args.out.numargs = 1;
2289 args.out.args[0].size = sizeof(outarg);
2290 args.out.args[0].value = &outarg;
2291 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002292 if (err == -ENOSYS)
2293 fc->no_bmap = 1;
2294
2295 return err ? 0 : outarg.block;
2296}
2297
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302298static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2299{
2300 struct inode *inode = file->f_mapping->host;
2301 struct fuse_conn *fc = get_fuse_conn(inode);
2302 struct fuse_file *ff = file->private_data;
2303 FUSE_ARGS(args);
2304 struct fuse_lseek_in inarg = {
2305 .fh = ff->fh,
2306 .offset = offset,
2307 .whence = whence
2308 };
2309 struct fuse_lseek_out outarg;
2310 int err;
2311
2312 if (fc->no_lseek)
2313 goto fallback;
2314
2315 args.in.h.opcode = FUSE_LSEEK;
2316 args.in.h.nodeid = ff->nodeid;
2317 args.in.numargs = 1;
2318 args.in.args[0].size = sizeof(inarg);
2319 args.in.args[0].value = &inarg;
2320 args.out.numargs = 1;
2321 args.out.args[0].size = sizeof(outarg);
2322 args.out.args[0].value = &outarg;
2323 err = fuse_simple_request(fc, &args);
2324 if (err) {
2325 if (err == -ENOSYS) {
2326 fc->no_lseek = 1;
2327 goto fallback;
2328 }
2329 return err;
2330 }
2331
2332 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2333
2334fallback:
2335 err = fuse_update_attributes(inode, NULL, file, NULL);
2336 if (!err)
2337 return generic_file_llseek(file, offset, whence);
2338 else
2339 return err;
2340}
2341
Andrew Morton965c8e52012-12-17 15:59:39 -08002342static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002343{
2344 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002345 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002346
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302347 switch (whence) {
2348 case SEEK_SET:
2349 case SEEK_CUR:
2350 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002351 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302352 break;
2353 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002354 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302355 retval = fuse_update_attributes(inode, NULL, file, NULL);
2356 if (!retval)
2357 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002358 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302359 break;
2360 case SEEK_HOLE:
2361 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002362 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302363 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002364 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302365 break;
2366 default:
2367 retval = -EINVAL;
2368 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002369
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002370 return retval;
2371}
2372
Tejun Heo59efec72008-11-26 12:03:55 +01002373/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002374 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2375 * ABI was defined to be 'struct iovec' which is different on 32bit
2376 * and 64bit. Fortunately we can determine which structure the server
2377 * used from the size of the reply.
2378 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002379static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2380 size_t transferred, unsigned count,
2381 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002382{
2383#ifdef CONFIG_COMPAT
2384 if (count * sizeof(struct compat_iovec) == transferred) {
2385 struct compat_iovec *ciov = src;
2386 unsigned i;
2387
2388 /*
2389 * With this interface a 32bit server cannot support
2390 * non-compat (i.e. ones coming from 64bit apps) ioctl
2391 * requests
2392 */
2393 if (!is_compat)
2394 return -EINVAL;
2395
2396 for (i = 0; i < count; i++) {
2397 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2398 dst[i].iov_len = ciov[i].iov_len;
2399 }
2400 return 0;
2401 }
2402#endif
2403
2404 if (count * sizeof(struct iovec) != transferred)
2405 return -EIO;
2406
2407 memcpy(dst, src, transferred);
2408 return 0;
2409}
2410
Miklos Szeredi75727772010-11-30 16:39:27 +01002411/* Make sure iov_length() won't overflow */
2412static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2413{
2414 size_t n;
2415 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2416
Zach Brownfb6ccff2012-07-24 12:10:11 -07002417 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002418 if (iov->iov_len > (size_t) max)
2419 return -ENOMEM;
2420 max -= iov->iov_len;
2421 }
2422 return 0;
2423}
2424
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002425static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2426 void *src, size_t transferred, unsigned count,
2427 bool is_compat)
2428{
2429 unsigned i;
2430 struct fuse_ioctl_iovec *fiov = src;
2431
2432 if (fc->minor < 16) {
2433 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2434 count, is_compat);
2435 }
2436
2437 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2438 return -EIO;
2439
2440 for (i = 0; i < count; i++) {
2441 /* Did the server supply an inappropriate value? */
2442 if (fiov[i].base != (unsigned long) fiov[i].base ||
2443 fiov[i].len != (unsigned long) fiov[i].len)
2444 return -EIO;
2445
2446 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2447 dst[i].iov_len = (size_t) fiov[i].len;
2448
2449#ifdef CONFIG_COMPAT
2450 if (is_compat &&
2451 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2452 (compat_size_t) dst[i].iov_len != fiov[i].len))
2453 return -EIO;
2454#endif
2455 }
2456
2457 return 0;
2458}
2459
2460
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002461/*
Tejun Heo59efec72008-11-26 12:03:55 +01002462 * For ioctls, there is no generic way to determine how much memory
2463 * needs to be read and/or written. Furthermore, ioctls are allowed
2464 * to dereference the passed pointer, so the parameter requires deep
2465 * copying but FUSE has no idea whatsoever about what to copy in or
2466 * out.
2467 *
2468 * This is solved by allowing FUSE server to retry ioctl with
2469 * necessary in/out iovecs. Let's assume the ioctl implementation
2470 * needs to read in the following structure.
2471 *
2472 * struct a {
2473 * char *buf;
2474 * size_t buflen;
2475 * }
2476 *
2477 * On the first callout to FUSE server, inarg->in_size and
2478 * inarg->out_size will be NULL; then, the server completes the ioctl
2479 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2480 * the actual iov array to
2481 *
2482 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2483 *
2484 * which tells FUSE to copy in the requested area and retry the ioctl.
2485 * On the second round, the server has access to the structure and
2486 * from that it can tell what to look for next, so on the invocation,
2487 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2488 *
2489 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2490 * { .iov_base = a.buf, .iov_len = a.buflen } }
2491 *
2492 * FUSE will copy both struct a and the pointed buffer from the
2493 * process doing the ioctl and retry ioctl with both struct a and the
2494 * buffer.
2495 *
2496 * This time, FUSE server has everything it needs and completes ioctl
2497 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2498 *
2499 * Copying data out works the same way.
2500 *
2501 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2502 * automatically initializes in and out iovs by decoding @cmd with
2503 * _IOC_* macros and the server is not allowed to request RETRY. This
2504 * limits ioctl data transfers to well-formed ioctls and is the forced
2505 * behavior for all FUSE servers.
2506 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002507long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2508 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002509{
Tejun Heo59efec72008-11-26 12:03:55 +01002510 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002511 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002512 struct fuse_ioctl_in inarg = {
2513 .fh = ff->fh,
2514 .cmd = cmd,
2515 .arg = arg,
2516 .flags = flags
2517 };
2518 struct fuse_ioctl_out outarg;
2519 struct fuse_req *req = NULL;
2520 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002521 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002522 struct iovec *in_iov = NULL, *out_iov = NULL;
2523 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002524 size_t in_size, out_size, transferred, c;
2525 int err, i;
2526 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002527
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002528#if BITS_PER_LONG == 32
2529 inarg.flags |= FUSE_IOCTL_32BIT;
2530#else
2531 if (flags & FUSE_IOCTL_COMPAT)
2532 inarg.flags |= FUSE_IOCTL_32BIT;
2533#endif
2534
Tejun Heo59efec72008-11-26 12:03:55 +01002535 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002536 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002537
Tejun Heo59efec72008-11-26 12:03:55 +01002538 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002539 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002540 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002541 if (!pages || !iov_page)
2542 goto out;
2543
2544 /*
2545 * If restricted, initialize IO parameters as encoded in @cmd.
2546 * RETRY from server is not allowed.
2547 */
2548 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002549 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002550
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002551 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002552 iov->iov_len = _IOC_SIZE(cmd);
2553
2554 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2555 in_iov = iov;
2556 in_iovs = 1;
2557 }
2558
2559 if (_IOC_DIR(cmd) & _IOC_READ) {
2560 out_iov = iov;
2561 out_iovs = 1;
2562 }
2563 }
2564
2565 retry:
2566 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2567 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2568
2569 /*
2570 * Out data can be used either for actual out data or iovs,
2571 * make sure there always is at least one page.
2572 */
2573 out_size = max_t(size_t, out_size, PAGE_SIZE);
2574 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2575
2576 /* make sure there are enough buffer pages and init request with them */
2577 err = -ENOMEM;
2578 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2579 goto out;
2580 while (num_pages < max_pages) {
2581 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2582 if (!pages[num_pages])
2583 goto out;
2584 num_pages++;
2585 }
2586
Maxim Patlasov54b96672012-10-26 19:49:13 +04002587 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002588 if (IS_ERR(req)) {
2589 err = PTR_ERR(req);
2590 req = NULL;
2591 goto out;
2592 }
2593 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2594 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002595 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002596
2597 /* okay, let's send it to the client */
2598 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002599 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002600 req->in.numargs = 1;
2601 req->in.args[0].size = sizeof(inarg);
2602 req->in.args[0].value = &inarg;
2603 if (in_size) {
2604 req->in.numargs++;
2605 req->in.args[1].size = in_size;
2606 req->in.argpages = 1;
2607
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002608 err = -EFAULT;
2609 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2610 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2611 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2612 if (c != PAGE_SIZE && iov_iter_count(&ii))
2613 goto out;
2614 }
Tejun Heo59efec72008-11-26 12:03:55 +01002615 }
2616
2617 req->out.numargs = 2;
2618 req->out.args[0].size = sizeof(outarg);
2619 req->out.args[0].value = &outarg;
2620 req->out.args[1].size = out_size;
2621 req->out.argpages = 1;
2622 req->out.argvar = 1;
2623
Tejun Heob93f8582008-11-26 12:03:55 +01002624 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002625 err = req->out.h.error;
2626 transferred = req->out.args[1].size;
2627 fuse_put_request(fc, req);
2628 req = NULL;
2629 if (err)
2630 goto out;
2631
2632 /* did it ask for retry? */
2633 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002634 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002635
2636 /* no retry if in restricted mode */
2637 err = -EIO;
2638 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2639 goto out;
2640
2641 in_iovs = outarg.in_iovs;
2642 out_iovs = outarg.out_iovs;
2643
2644 /*
2645 * Make sure things are in boundary, separate checks
2646 * are to protect against overflow.
2647 */
2648 err = -ENOMEM;
2649 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2650 out_iovs > FUSE_IOCTL_MAX_IOV ||
2651 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2652 goto out;
2653
Cong Wang2408f6e2011-11-25 23:14:30 +08002654 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002655 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002656 transferred, in_iovs + out_iovs,
2657 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002658 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002659 if (err)
2660 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002661
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002662 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002663 out_iov = in_iov + in_iovs;
2664
Miklos Szeredi75727772010-11-30 16:39:27 +01002665 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2666 if (err)
2667 goto out;
2668
2669 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2670 if (err)
2671 goto out;
2672
Tejun Heo59efec72008-11-26 12:03:55 +01002673 goto retry;
2674 }
2675
2676 err = -EIO;
2677 if (transferred > inarg.out_size)
2678 goto out;
2679
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002680 err = -EFAULT;
2681 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2682 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2683 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2684 if (c != PAGE_SIZE && iov_iter_count(&ii))
2685 goto out;
2686 }
2687 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002688 out:
2689 if (req)
2690 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002691 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002692 while (num_pages)
2693 __free_page(pages[--num_pages]);
2694 kfree(pages);
2695
2696 return err ? err : outarg.result;
2697}
Tejun Heo08cbf542009-04-14 10:54:53 +09002698EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002699
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002700long fuse_ioctl_common(struct file *file, unsigned int cmd,
2701 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002702{
Al Viro6131ffa2013-02-27 16:59:05 -05002703 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002704 struct fuse_conn *fc = get_fuse_conn(inode);
2705
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002706 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002707 return -EACCES;
2708
2709 if (is_bad_inode(inode))
2710 return -EIO;
2711
2712 return fuse_do_ioctl(file, cmd, arg, flags);
2713}
2714
Tejun Heo59efec72008-11-26 12:03:55 +01002715static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2716 unsigned long arg)
2717{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002718 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002719}
2720
2721static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2722 unsigned long arg)
2723{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002724 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002725}
2726
Tejun Heo95668a62008-11-26 12:03:55 +01002727/*
2728 * All files which have been polled are linked to RB tree
2729 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2730 * find the matching one.
2731 */
2732static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2733 struct rb_node **parent_out)
2734{
2735 struct rb_node **link = &fc->polled_files.rb_node;
2736 struct rb_node *last = NULL;
2737
2738 while (*link) {
2739 struct fuse_file *ff;
2740
2741 last = *link;
2742 ff = rb_entry(last, struct fuse_file, polled_node);
2743
2744 if (kh < ff->kh)
2745 link = &last->rb_left;
2746 else if (kh > ff->kh)
2747 link = &last->rb_right;
2748 else
2749 return link;
2750 }
2751
2752 if (parent_out)
2753 *parent_out = last;
2754 return link;
2755}
2756
2757/*
2758 * The file is about to be polled. Make sure it's on the polled_files
2759 * RB tree. Note that files once added to the polled_files tree are
2760 * not removed before the file is released. This is because a file
2761 * polled once is likely to be polled again.
2762 */
2763static void fuse_register_polled_file(struct fuse_conn *fc,
2764 struct fuse_file *ff)
2765{
2766 spin_lock(&fc->lock);
2767 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002768 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002769
2770 link = fuse_find_polled_node(fc, ff->kh, &parent);
2771 BUG_ON(*link);
2772 rb_link_node(&ff->polled_node, parent, link);
2773 rb_insert_color(&ff->polled_node, &fc->polled_files);
2774 }
2775 spin_unlock(&fc->lock);
2776}
2777
Tejun Heo08cbf542009-04-14 10:54:53 +09002778unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002779{
Tejun Heo95668a62008-11-26 12:03:55 +01002780 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002781 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002782 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2783 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002784 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002785 int err;
2786
2787 if (fc->no_poll)
2788 return DEFAULT_POLLMASK;
2789
2790 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002791 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002792
2793 /*
2794 * Ask for notification iff there's someone waiting for it.
2795 * The client may ignore the flag and always notify.
2796 */
2797 if (waitqueue_active(&ff->poll_wait)) {
2798 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2799 fuse_register_polled_file(fc, ff);
2800 }
2801
Miklos Szeredi70781872014-12-12 09:49:05 +01002802 args.in.h.opcode = FUSE_POLL;
2803 args.in.h.nodeid = ff->nodeid;
2804 args.in.numargs = 1;
2805 args.in.args[0].size = sizeof(inarg);
2806 args.in.args[0].value = &inarg;
2807 args.out.numargs = 1;
2808 args.out.args[0].size = sizeof(outarg);
2809 args.out.args[0].value = &outarg;
2810 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002811
2812 if (!err)
2813 return outarg.revents;
2814 if (err == -ENOSYS) {
2815 fc->no_poll = 1;
2816 return DEFAULT_POLLMASK;
2817 }
2818 return POLLERR;
2819}
Tejun Heo08cbf542009-04-14 10:54:53 +09002820EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002821
2822/*
2823 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2824 * wakes up the poll waiters.
2825 */
2826int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2827 struct fuse_notify_poll_wakeup_out *outarg)
2828{
2829 u64 kh = outarg->kh;
2830 struct rb_node **link;
2831
2832 spin_lock(&fc->lock);
2833
2834 link = fuse_find_polled_node(fc, kh, NULL);
2835 if (*link) {
2836 struct fuse_file *ff;
2837
2838 ff = rb_entry(*link, struct fuse_file, polled_node);
2839 wake_up_interruptible_sync(&ff->poll_wait);
2840 }
2841
2842 spin_unlock(&fc->lock);
2843 return 0;
2844}
2845
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002846static void fuse_do_truncate(struct file *file)
2847{
2848 struct inode *inode = file->f_mapping->host;
2849 struct iattr attr;
2850
2851 attr.ia_valid = ATTR_SIZE;
2852 attr.ia_size = i_size_read(inode);
2853
2854 attr.ia_file = file;
2855 attr.ia_valid |= ATTR_FILE;
2856
Jan Kara62490332016-05-26 17:12:41 +02002857 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002858}
2859
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002860static inline loff_t fuse_round_up(loff_t off)
2861{
2862 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2863}
2864
Anand Avati4273b792012-02-17 12:46:25 -05002865static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002866fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002867{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002868 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002869 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002870 struct file *file = iocb->ki_filp;
2871 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002872 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002873 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002874 struct inode *inode;
2875 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002876 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002877 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002878 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002879
Anand Avati4273b792012-02-17 12:46:25 -05002880 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002881 inode = file->f_mapping->host;
2882 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002883
Omar Sandoval6f673762015-03-16 04:33:52 -07002884 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002885 return 0;
2886
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002887 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002888 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002889 if (offset >= i_size)
2890 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002891 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2892 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002893 }
2894
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002896 if (!io)
2897 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002899 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002900 io->reqs = 1;
2901 io->bytes = -1;
2902 io->size = 0;
2903 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002904 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002905 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002906 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002907 /*
2908 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002909 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002910 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002911 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002912 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302913 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002914
2915 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302916 * We cannot asynchronously extend the size of a file.
2917 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002918 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302919 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2920 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002921
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302922 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002923 /*
2924 * Additional reference to keep io around after
2925 * calling fuse_aio_complete()
2926 */
2927 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002928 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002929 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002930
Omar Sandoval6f673762015-03-16 04:33:52 -07002931 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002932 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002933 fuse_invalidate_attr(inode);
2934 } else {
Al Virod22a9432014-03-16 15:50:47 -04002935 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002936 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002937
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002938 if (io->async) {
2939 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2940
2941 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302942 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002943 return -EIOCBQUEUED;
2944
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002945 wait_for_completion(&wait);
2946 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002947 }
2948
Seth Forshee744742d2016-03-11 10:35:34 -06002949 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002950
Omar Sandoval6f673762015-03-16 04:33:52 -07002951 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002952 if (ret > 0)
2953 fuse_write_update_size(inode, pos);
2954 else if (ret < 0 && offset + count > i_size)
2955 fuse_do_truncate(file);
2956 }
Anand Avati4273b792012-02-17 12:46:25 -05002957
2958 return ret;
2959}
2960
Miklos Szeredicdadb112012-11-10 16:55:56 +01002961static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2962 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002963{
2964 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002965 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002966 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002967 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002968 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002969 struct fuse_fallocate_in inarg = {
2970 .fh = ff->fh,
2971 .offset = offset,
2972 .length = length,
2973 .mode = mode
2974 };
2975 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002976 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2977 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002978
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002979 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2980 return -EOPNOTSUPP;
2981
Miklos Szeredi519c6042012-04-26 10:56:36 +02002982 if (fc->no_fallocate)
2983 return -EOPNOTSUPP;
2984
Maxim Patlasov14c14412013-06-13 12:16:39 +04002985 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002986 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002987 if (mode & FALLOC_FL_PUNCH_HOLE) {
2988 loff_t endbyte = offset + length - 1;
2989 err = filemap_write_and_wait_range(inode->i_mapping,
2990 offset, endbyte);
2991 if (err)
2992 goto out;
2993
2994 fuse_sync_writes(inode);
2995 }
Brian Foster3634a632013-05-17 09:30:32 -04002996 }
2997
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002998 if (!(mode & FALLOC_FL_KEEP_SIZE))
2999 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3000
Miklos Szeredi70781872014-12-12 09:49:05 +01003001 args.in.h.opcode = FUSE_FALLOCATE;
3002 args.in.h.nodeid = ff->nodeid;
3003 args.in.numargs = 1;
3004 args.in.args[0].size = sizeof(inarg);
3005 args.in.args[0].value = &inarg;
3006 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02003007 if (err == -ENOSYS) {
3008 fc->no_fallocate = 1;
3009 err = -EOPNOTSUPP;
3010 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003011 if (err)
3012 goto out;
3013
3014 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003015 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3016 bool changed = fuse_write_update_size(inode, offset + length);
3017
Miklos Szeredi93d22692014-04-28 14:19:22 +02003018 if (changed && fc->writeback_cache)
3019 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003020 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003021
3022 if (mode & FALLOC_FL_PUNCH_HOLE)
3023 truncate_pagecache_range(inode, offset, offset + length - 1);
3024
3025 fuse_invalidate_attr(inode);
3026
Brian Foster3634a632013-05-17 09:30:32 -04003027out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003028 if (!(mode & FALLOC_FL_KEEP_SIZE))
3029 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3030
Maxim Patlasovbde52782013-09-13 19:19:54 +04003031 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003032 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003033
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003034 return err;
3035}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003036
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003037static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003038 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003039 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003040 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003041 .mmap = fuse_file_mmap,
3042 .open = fuse_open,
3043 .flush = fuse_flush,
3044 .release = fuse_release,
3045 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003046 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003047 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003048 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003049 .unlocked_ioctl = fuse_file_ioctl,
3050 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003051 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003052 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003053};
3054
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003055static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003056 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003057 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003058 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003059 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003060 .open = fuse_open,
3061 .flush = fuse_flush,
3062 .release = fuse_release,
3063 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003064 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003065 .flock = fuse_file_flock,
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 Szeredifc280c92009-04-02 14:25:35 +02003070 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003071};
3072
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003073static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003074 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003075 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003076 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003077 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003078 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003079 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003080 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003081 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003082 .write_begin = fuse_write_begin,
3083 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003084};
3085
3086void fuse_init_file_inode(struct inode *inode)
3087{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003088 inode->i_fop = &fuse_file_operations;
3089 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003090}