blob: 32d0b883e74f340d442754a5a87b82629b96658e [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050015#include <linux/sched/signal.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090016#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010017#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020018#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080022static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070023
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020024static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
25 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070027 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010028 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080029
30 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070031 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010034 args.in.h.opcode = opcode;
35 args.in.h.nodeid = nodeid;
36 args.in.numargs = 1;
37 args.in.args[0].size = sizeof(inarg);
38 args.in.args[0].value = &inarg;
39 args.out.numargs = 1;
40 args.out.args[0].size = sizeof(*outargp);
41 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080042
Miklos Szeredi70781872014-12-12 09:49:05 +010043 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080044}
45
Tejun Heoacf99432008-11-26 12:03:55 +010046struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047{
48 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090049
Mateusz Jurczyk68227c02017-06-07 12:26:49 +020050 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090051 if (unlikely(!ff))
52 return NULL;
53
Miklos Szeredida5e4712009-04-28 16:56:36 +020054 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040055 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090056 if (unlikely(!ff->reserved_req)) {
57 kfree(ff);
58 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080059 }
Tejun Heo6b2db282009-04-14 10:54:49 +090060
61 INIT_LIST_HEAD(&ff->write_entry);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020062 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090063 RB_CLEAR_NODE(&ff->polled_node);
64 init_waitqueue_head(&ff->poll_wait);
65
66 spin_lock(&fc->lock);
67 ff->kh = ++fc->khctr;
68 spin_unlock(&fc->lock);
69
Miklos Szeredifd72faa2005-11-07 00:59:51 -080070 return ff;
71}
72
73void fuse_file_free(struct fuse_file *ff)
74{
Miklos Szeredi33649c92006-06-25 05:48:52 -070075 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080076 kfree(ff);
77}
78
Miklos Szeredi267d8442017-02-22 20:08:25 +010079static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070080{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020081 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070082 return ff;
83}
84
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010085static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
86{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010087 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010088}
89
90static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070091{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020092 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -070093 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020094
Andrew Gallagher7678ac52013-11-05 16:05:52 +010095 if (ff->fc->no_open) {
96 /*
97 * Drop the release request when client does not
98 * implement 'open'
99 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200100 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100101 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100102 fuse_put_request(ff->fc, req);
103 } else if (sync) {
Miklos Szeredi2e38bea2017-02-22 20:08:25 +0100104 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200105 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100107 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100108 fuse_put_request(ff->fc, req);
109 } else {
110 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200111 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100112 fuse_request_send_background(ff->fc, req);
113 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700114 kfree(ff);
115 }
116}
117
Tejun Heo08cbf542009-04-14 10:54:53 +0900118int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
119 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200120{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200121 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200122 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
123
124 ff = fuse_file_alloc(fc);
125 if (!ff)
126 return -ENOMEM;
127
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100128 ff->fh = 0;
129 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
130 if (!fc->no_open || isdir) {
131 struct fuse_open_out outarg;
132 int err;
133
134 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
135 if (!err) {
136 ff->fh = outarg.fh;
137 ff->open_flags = outarg.open_flags;
138
139 } else if (err != -ENOSYS || isdir) {
140 fuse_file_free(ff);
141 return err;
142 } else {
143 fc->no_open = 1;
144 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200145 }
146
147 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100148 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200149
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200150 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100151 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200152
153 return 0;
154}
Tejun Heo08cbf542009-04-14 10:54:53 +0900155EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200156
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400157static void fuse_link_write_file(struct file *file)
158{
159 struct inode *inode = file_inode(file);
160 struct fuse_conn *fc = get_fuse_conn(inode);
161 struct fuse_inode *fi = get_fuse_inode(inode);
162 struct fuse_file *ff = file->private_data;
163 /*
164 * file may be written through mmap, so chain it onto the
165 * inodes's write_file list
166 */
167 spin_lock(&fc->lock);
168 if (list_empty(&ff->write_entry))
169 list_add(&ff->write_entry, &fi->write_files);
170 spin_unlock(&fc->lock);
171}
172
Miklos Szeredic7b71432009-04-28 16:56:37 +0200173void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800174{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200175 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800176 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200177
178 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800179 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200180 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700181 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200182 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200183 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800184 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
185 struct fuse_inode *fi = get_fuse_inode(inode);
186
187 spin_lock(&fc->lock);
188 fi->attr_version = ++fc->attr_version;
189 i_size_write(inode, 0);
190 spin_unlock(&fc->lock);
191 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200192 if (fc->writeback_cache)
193 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800194 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400195 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
196 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800197}
198
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200199int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800200{
Tejun Heoacf99432008-11-26 12:03:55 +0100201 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700202 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200203 bool lock_inode = (file->f_flags & O_TRUNC) &&
204 fc->atomic_o_trunc &&
205 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700206
207 err = generic_file_open(inode, file);
208 if (err)
209 return err;
210
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200211 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500212 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200213
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200214 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700215
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200216 if (!err)
217 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200218
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200219 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500220 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200221
222 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700223}
224
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200225static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800226{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200227 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700228 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800229 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700230
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200231 spin_lock(&fc->lock);
232 list_del(&ff->write_entry);
233 if (!RB_EMPTY_NODE(&ff->polled_node))
234 rb_erase(&ff->polled_node, &fc->polled_files);
235 spin_unlock(&fc->lock);
236
Bryan Green357ccf22011-03-01 16:43:52 -0800237 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200238
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700239 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800240 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700241 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200242 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700243 req->in.numargs = 1;
244 req->in.args[0].size = sizeof(struct fuse_release_in);
245 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800246}
247
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200248void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800249{
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100250 struct fuse_file *ff = file->private_data;
251 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700252
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200253 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100254
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200255 if (ff->flock) {
256 struct fuse_release_in *inarg = &req->misc.release.in;
257 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
258 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
259 (fl_owner_t) file);
260 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100261 /* Hold inode until release is finished */
262 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700263
Tejun Heo6b2db282009-04-14 10:54:49 +0900264 /*
265 * Normally this will send the RELEASE request, however if
266 * some asynchronous READ or WRITE requests are outstanding,
267 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100268 *
269 * Make the release synchronous if this is a fuseblk mount,
270 * synchronous RELEASE is allowed (and desirable) in this case
271 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900272 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100273 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700274}
275
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700276static int fuse_open(struct inode *inode, struct file *file)
277{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200278 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700279}
280
281static int fuse_release(struct inode *inode, struct file *file)
282{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400283 struct fuse_conn *fc = get_fuse_conn(inode);
284
285 /* see fuse_vma_close() for !writeback_cache case */
286 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200287 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400288
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200289 fuse_release_common(file, FUSE_RELEASE);
290
291 /* return value is ignored by VFS */
292 return 0;
293}
294
295void fuse_sync_release(struct fuse_file *ff, int flags)
296{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200297 WARN_ON(refcount_read(&ff->count) > 1);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200298 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100299 /*
300 * iput(NULL) is a no-op and since the refcount is 1 and everything's
301 * synchronous, we are fine with not doing igrab() here"
302 */
303 fuse_file_put(ff, true);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700304}
Tejun Heo08cbf542009-04-14 10:54:53 +0900305EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700306
Miklos Szeredi71421252006-06-25 05:48:52 -0700307/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700308 * Scramble the ID space with XTEA, so that the value of the files_struct
309 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700310 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700311u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700312{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700313 u32 *k = fc->scramble_key;
314 u64 v = (unsigned long) id;
315 u32 v0 = v;
316 u32 v1 = v >> 32;
317 u32 sum = 0;
318 int i;
319
320 for (i = 0; i < 32; i++) {
321 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
322 sum += 0x9E3779B9;
323 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
324 }
325
326 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700327}
328
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700329/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400330 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700331 *
332 * This is currently done by walking the list of writepage requests
333 * for the inode, which can be pretty inefficient.
334 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400335static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
336 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700337{
338 struct fuse_conn *fc = get_fuse_conn(inode);
339 struct fuse_inode *fi = get_fuse_inode(inode);
340 struct fuse_req *req;
341 bool found = false;
342
343 spin_lock(&fc->lock);
344 list_for_each_entry(req, &fi->writepages, writepages_entry) {
345 pgoff_t curr_index;
346
347 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300348 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400349 if (idx_from < curr_index + req->num_pages &&
350 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700351 found = true;
352 break;
353 }
354 }
355 spin_unlock(&fc->lock);
356
357 return found;
358}
359
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400360static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
361{
362 return fuse_range_is_writeback(inode, index, index);
363}
364
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700365/*
366 * Wait for page writeback to be completed.
367 *
368 * Since fuse doesn't rely on the VM writeback tracking, this has to
369 * use some other means.
370 */
371static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
372{
373 struct fuse_inode *fi = get_fuse_inode(inode);
374
375 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
376 return 0;
377}
378
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400379/*
380 * Wait for all pending writepages on the inode to finish.
381 *
382 * This is currently done by blocking further writes with FUSE_NOWRITE
383 * and waiting for all sent writes to complete.
384 *
385 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
386 * could conflict with truncation.
387 */
388static void fuse_sync_writes(struct inode *inode)
389{
390 fuse_set_nowrite(inode);
391 fuse_release_nowrite(inode);
392}
393
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700394static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700395{
Al Viro6131ffa2013-02-27 16:59:05 -0500396 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700397 struct fuse_conn *fc = get_fuse_conn(inode);
398 struct fuse_file *ff = file->private_data;
399 struct fuse_req *req;
400 struct fuse_flush_in inarg;
401 int err;
402
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800403 if (is_bad_inode(inode))
404 return -EIO;
405
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700406 if (fc->no_flush)
407 return 0;
408
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200409 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400410 if (err)
411 return err;
412
Al Viro59551022016-01-22 15:40:57 -0500413 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400414 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500415 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400416
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200417 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700418 if (err)
419 return err;
420
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400421 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700422 memset(&inarg, 0, sizeof(inarg));
423 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700424 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 req->in.h.opcode = FUSE_FLUSH;
426 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 req->in.numargs = 1;
428 req->in.args[0].size = sizeof(inarg);
429 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200430 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100431 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700432 err = req->out.h.error;
433 fuse_put_request(fc, req);
434 if (err == -ENOSYS) {
435 fc->no_flush = 1;
436 err = 0;
437 }
438 return err;
439}
440
Josef Bacik02c24a82011-07-16 20:44:56 -0400441int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
442 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700443{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200444 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700445 struct fuse_conn *fc = get_fuse_conn(inode);
446 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100447 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 struct fuse_fsync_in inarg;
449 int err;
450
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800451 if (is_bad_inode(inode))
452 return -EIO;
453
Al Viro59551022016-01-22 15:40:57 -0500454 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400455
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700456 /*
457 * Start writeback against all dirty pages of the inode, then
458 * wait for all outstanding writes, before sending the FSYNC
459 * request.
460 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400461 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700462 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400463 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700464
465 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700466
467 /*
468 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400469 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700470 * We have to do this directly after fuse_sync_writes()
471 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400472 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700473 if (err)
474 goto out;
475
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200476 err = sync_inode_metadata(inode, 1);
477 if (err)
478 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700479
Miklos Szeredi22401e72014-04-28 14:19:23 +0200480 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
481 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400482
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483 memset(&inarg, 0, sizeof(inarg));
484 inarg.fh = ff->fh;
485 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100486 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
487 args.in.h.nodeid = get_node_id(inode);
488 args.in.numargs = 1;
489 args.in.args[0].size = sizeof(inarg);
490 args.in.args[0].value = &inarg;
491 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700492 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700493 if (isdir)
494 fc->no_fsyncdir = 1;
495 else
496 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700497 err = 0;
498 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400499out:
Al Viro59551022016-01-22 15:40:57 -0500500 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501 return err;
502}
503
Josef Bacik02c24a82011-07-16 20:44:56 -0400504static int fuse_fsync(struct file *file, loff_t start, loff_t end,
505 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700506{
Josef Bacik02c24a82011-07-16 20:44:56 -0400507 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700508}
509
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200510void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
511 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700512{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700513 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800514 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800516 inarg->fh = ff->fh;
517 inarg->offset = pos;
518 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800519 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800520 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200521 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700522 req->in.numargs = 1;
523 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800524 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 req->out.argvar = 1;
526 req->out.numargs = 1;
527 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528}
529
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200530static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400531{
532 unsigned i;
533
534 for (i = 0; i < req->num_pages; i++) {
535 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200536 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400537 set_page_dirty_lock(page);
538 put_page(page);
539 }
540}
541
Seth Forshee744742d2016-03-11 10:35:34 -0600542static void fuse_io_release(struct kref *kref)
543{
544 kfree(container_of(kref, struct fuse_io_priv, refcnt));
545}
546
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100547static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
548{
549 if (io->err)
550 return io->err;
551
552 if (io->bytes >= 0 && io->write)
553 return -EIO;
554
555 return io->bytes < 0 ? io->size : io->bytes;
556}
557
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400558/**
559 * In case of short read, the caller sets 'pos' to the position of
560 * actual end of fuse request in IO request. Otherwise, if bytes_requested
561 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
562 *
563 * An example:
564 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
565 * both submitted asynchronously. The first of them was ACKed by userspace as
566 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
567 * second request was ACKed as short, e.g. only 1K was read, resulting in
568 * pos == 33K.
569 *
570 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
571 * will be equal to the length of the longest contiguous fragment of
572 * transferred data starting from the beginning of IO request.
573 */
574static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
575{
576 int left;
577
578 spin_lock(&io->lock);
579 if (err)
580 io->err = io->err ? : err;
581 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
582 io->bytes = pos;
583
584 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530585 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100586 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400587 spin_unlock(&io->lock);
588
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530589 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100590 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400591
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100592 if (res >= 0) {
593 struct inode *inode = file_inode(io->iocb->ki_filp);
594 struct fuse_conn *fc = get_fuse_conn(inode);
595 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400596
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100597 spin_lock(&fc->lock);
598 fi->attr_version = ++fc->attr_version;
599 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400600 }
601
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100602 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400603 }
Seth Forshee744742d2016-03-11 10:35:34 -0600604
605 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400606}
607
608static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
609{
610 struct fuse_io_priv *io = req->io;
611 ssize_t pos = -1;
612
Ashish Samant61c12b42017-07-12 19:26:58 -0700613 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400614
615 if (io->write) {
616 if (req->misc.write.in.size != req->misc.write.out.size)
617 pos = req->misc.write.in.offset - io->offset +
618 req->misc.write.out.size;
619 } else {
620 if (req->misc.read.in.size != req->out.args[0].size)
621 pos = req->misc.read.in.offset - io->offset +
622 req->out.args[0].size;
623 }
624
625 fuse_aio_complete(io, req->out.h.error, pos);
626}
627
628static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
629 size_t num_bytes, struct fuse_io_priv *io)
630{
631 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600632 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400633 io->size += num_bytes;
634 io->reqs++;
635 spin_unlock(&io->lock);
636
637 req->io = io;
638 req->end = fuse_aio_complete_req;
639
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400640 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400641 fuse_request_send_background(fc, req);
642
643 return num_bytes;
644}
645
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400646static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200647 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700648{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200649 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200650 struct fuse_file *ff = file->private_data;
651 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700652
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200653 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700654 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700655 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700656
657 inarg->read_flags |= FUSE_READ_LOCKOWNER;
658 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
659 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400660
661 if (io->async)
662 return fuse_async_req_send(fc, req, count, io);
663
Tejun Heob93f8582008-11-26 12:03:55 +0100664 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800665 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700666}
667
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700668static void fuse_read_update_size(struct inode *inode, loff_t size,
669 u64 attr_ver)
670{
671 struct fuse_conn *fc = get_fuse_conn(inode);
672 struct fuse_inode *fi = get_fuse_inode(inode);
673
674 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400675 if (attr_ver == fi->attr_version && size < inode->i_size &&
676 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700677 fi->attr_version = ++fc->attr_version;
678 i_size_write(inode, size);
679 }
680 spin_unlock(&fc->lock);
681}
682
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400683static void fuse_short_read(struct fuse_req *req, struct inode *inode,
684 u64 attr_ver)
685{
686 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400687 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400688
Pavel Emelyanov83732002013-10-10 17:10:46 +0400689 if (fc->writeback_cache) {
690 /*
691 * A hole in a file. Some data after the hole are in page cache,
692 * but have not reached the client fs yet. So, the hole is not
693 * present there.
694 */
695 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300696 int start_idx = num_read >> PAGE_SHIFT;
697 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400698
699 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300700 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701 off = 0;
702 }
703 } else {
704 loff_t pos = page_offset(req->pages[0]) + num_read;
705 fuse_read_update_size(inode, pos, attr_ver);
706 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400707}
708
Maxim Patlasov482fce52013-10-10 17:11:25 +0400709static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700710{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200711 struct kiocb iocb;
712 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700713 struct inode *inode = page->mapping->host;
714 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800715 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700716 size_t num_read;
717 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300718 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700719 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800720 int err;
721
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700722 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300723 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700724 * page-cache page, so make sure we read a properly synced
725 * page.
726 */
727 fuse_wait_on_page_writeback(inode, page->index);
728
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400729 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700730 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400731 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700732
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700733 attr_ver = fuse_get_attr_version(fc);
734
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700735 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200736 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737 req->num_pages = 1;
738 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400739 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200740 init_sync_kiocb(&iocb, file);
741 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400742 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700743 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700744
745 if (!err) {
746 /*
747 * Short read means EOF. If file size is larger, truncate it
748 */
749 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400750 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700751
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700752 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700753 }
754
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400755 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400756
757 return err;
758}
759
760static int fuse_readpage(struct file *file, struct page *page)
761{
762 struct inode *inode = page->mapping->host;
763 int err;
764
765 err = -EIO;
766 if (is_bad_inode(inode))
767 goto out;
768
769 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800770 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700771 out:
772 unlock_page(page);
773 return err;
774}
775
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800776static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700777{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800778 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700779 size_t count = req->misc.read.in.size;
780 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200781 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800782
Miklos Szeredice534fb2010-05-25 15:06:07 +0200783 for (i = 0; mapping == NULL && i < req->num_pages; i++)
784 mapping = req->pages[i]->mapping;
785
786 if (mapping) {
787 struct inode *inode = mapping->host;
788
789 /*
790 * Short read means EOF. If file size is larger, truncate it
791 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400792 if (!req->out.h.error && num_read < count)
793 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794
Andrew Gallagher451418f2013-11-05 03:55:43 -0800795 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700796 }
797
Miklos Szeredidb50b962005-09-09 13:10:33 -0700798 for (i = 0; i < req->num_pages; i++) {
799 struct page *page = req->pages[i];
800 if (!req->out.h.error)
801 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800802 else
803 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700804 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300805 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700806 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700807 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100808 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800809}
810
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200811static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800812{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200813 struct fuse_file *ff = file->private_data;
814 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800815 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300816 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200817
818 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800819 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200821 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700822 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800823 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700824 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800825 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100826 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800827 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100828 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800829 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100830 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800831 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700832}
833
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700834struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700835 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800836 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700837 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400838 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700839};
840
841static int fuse_readpages_fill(void *_data, struct page *page)
842{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700843 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700844 struct fuse_req *req = data->req;
845 struct inode *inode = data->inode;
846 struct fuse_conn *fc = get_fuse_conn(inode);
847
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700848 fuse_wait_on_page_writeback(inode, page->index);
849
Miklos Szeredidb50b962005-09-09 13:10:33 -0700850 if (req->num_pages &&
851 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300852 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400854 int nr_alloc = min_t(unsigned, data->nr_pages,
855 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200856 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400857 if (fc->async_read)
858 req = fuse_get_req_for_background(fc, nr_alloc);
859 else
860 req = fuse_get_req(fc, nr_alloc);
861
862 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700863 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700865 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700866 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400868
869 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300870 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400871 fuse_put_request(fc, req);
872 return -EIO;
873 }
874
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300875 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400877 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100878 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 return 0;
881}
882
883static int fuse_readpages(struct file *file, struct address_space *mapping,
884 struct list_head *pages, unsigned nr_pages)
885{
886 struct inode *inode = mapping->host;
887 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700888 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700889 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400890 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800891
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700892 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800893 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800894 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895
Miklos Szeredia6643092007-11-28 16:22:00 -0800896 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700897 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400898 if (fc->async_read)
899 data.req = fuse_get_req_for_background(fc, nr_alloc);
900 else
901 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400902 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700903 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700904 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800905 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700906
907 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700908 if (!err) {
909 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200910 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700911 else
912 fuse_put_request(fc, data.req);
913 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800914out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700915 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916}
917
Al Viro37c20f12014-04-02 14:47:09 -0400918static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800919{
920 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400921 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800922
Brian Fostera8894272012-07-16 15:23:50 -0400923 /*
924 * In auto invalidate mode, always update attributes on read.
925 * Otherwise, only update if we attempt to read past EOF (to ensure
926 * i_size is up to date).
927 */
928 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400929 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800930 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200931 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932 if (err)
933 return err;
934 }
935
Al Viro37c20f12014-04-02 14:47:09 -0400936 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800937}
938
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200939static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200940 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700941{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700942 struct fuse_write_in *inarg = &req->misc.write.in;
943 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700945 inarg->fh = ff->fh;
946 inarg->offset = pos;
947 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700948 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200949 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700950 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700952 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
953 else
954 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700955 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700956 req->in.args[1].size = count;
957 req->out.numargs = 1;
958 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700959 req->out.args[0].value = outarg;
960}
961
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400962static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200963 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700964{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200965 struct kiocb *iocb = io->iocb;
966 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200967 struct fuse_file *ff = file->private_data;
968 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200969 struct fuse_write_in *inarg = &req->misc.write.in;
970
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200971 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200972 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200973 if (iocb->ki_flags & IOCB_DSYNC)
974 inarg->flags |= O_DSYNC;
975 if (iocb->ki_flags & IOCB_SYNC)
976 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700977 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700978 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
979 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
980 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400981
982 if (io->async)
983 return fuse_async_req_send(fc, req, count, io);
984
Tejun Heob93f8582008-11-26 12:03:55 +0100985 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700986 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700987}
988
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400989bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700990{
991 struct fuse_conn *fc = get_fuse_conn(inode);
992 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400993 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700994
995 spin_lock(&fc->lock);
996 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400997 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700998 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400999 ret = true;
1000 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001001 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001002
1003 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004}
1005
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001006static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001007 struct inode *inode, loff_t pos,
1008 size_t count)
1009{
1010 size_t res;
1011 unsigned offset;
1012 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001013 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001014
1015 for (i = 0; i < req->num_pages; i++)
1016 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1017
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001018 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001019
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001020 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001021 count = res;
1022 for (i = 0; i < req->num_pages; i++) {
1023 struct page *page = req->pages[i];
1024
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001025 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001026 SetPageUptodate(page);
1027
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 if (count > PAGE_SIZE - offset)
1029 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001030 else
1031 count = 0;
1032 offset = 0;
1033
1034 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001036 }
1037
1038 return res;
1039}
1040
1041static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1042 struct address_space *mapping,
1043 struct iov_iter *ii, loff_t pos)
1044{
1045 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001046 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001047 size_t count = 0;
1048 int err;
1049
Miklos Szeredif4975c62009-04-02 14:25:34 +02001050 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001051 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001052
1053 do {
1054 size_t tmp;
1055 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001056 pgoff_t index = pos >> PAGE_SHIFT;
1057 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001058 iov_iter_count(ii));
1059
1060 bytes = min_t(size_t, bytes, fc->max_write - count);
1061
1062 again:
1063 err = -EFAULT;
1064 if (iov_iter_fault_in_readable(ii, bytes))
1065 break;
1066
1067 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001068 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001069 if (!page)
1070 break;
1071
anfei zhou931e80e2010-02-02 13:44:02 -08001072 if (mapping_writably_mapped(mapping))
1073 flush_dcache_page(page);
1074
Nick Pigginea9b9902008-04-30 00:54:42 -07001075 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001076 flush_dcache_page(page);
1077
Roman Gushchin3ca81382015-10-12 16:33:44 +03001078 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001079 if (!tmp) {
1080 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001081 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001082 bytes = min(bytes, iov_iter_single_seg_count(ii));
1083 goto again;
1084 }
1085
1086 err = 0;
1087 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001088 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 req->num_pages++;
1090
Nick Pigginea9b9902008-04-30 00:54:42 -07001091 count += tmp;
1092 pos += tmp;
1093 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001094 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 offset = 0;
1096
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001097 if (!fc->big_writes)
1098 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001100 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001101
1102 return count > 0 ? count : err;
1103}
1104
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001105static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1106{
1107 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001108 ((pos + len - 1) >> PAGE_SHIFT) -
1109 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001110 FUSE_MAX_PAGES_PER_REQ);
1111}
1112
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001113static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001114 struct address_space *mapping,
1115 struct iov_iter *ii, loff_t pos)
1116{
1117 struct inode *inode = mapping->host;
1118 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001119 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001120 int err = 0;
1121 ssize_t res = 0;
1122
1123 if (is_bad_inode(inode))
1124 return -EIO;
1125
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001126 if (inode->i_size < pos + iov_iter_count(ii))
1127 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1128
Nick Pigginea9b9902008-04-30 00:54:42 -07001129 do {
1130 struct fuse_req *req;
1131 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001132 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001133
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001134 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001135 if (IS_ERR(req)) {
1136 err = PTR_ERR(req);
1137 break;
1138 }
1139
1140 count = fuse_fill_write_pages(req, mapping, ii, pos);
1141 if (count <= 0) {
1142 err = count;
1143 } else {
1144 size_t num_written;
1145
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001146 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001147 pos, count);
1148 err = req->out.h.error;
1149 if (!err) {
1150 res += num_written;
1151 pos += num_written;
1152
1153 /* break out of the loop on short write */
1154 if (num_written != count)
1155 err = -EIO;
1156 }
1157 }
1158 fuse_put_request(fc, req);
1159 } while (!err && iov_iter_count(ii));
1160
1161 if (res > 0)
1162 fuse_write_update_size(inode, pos);
1163
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001164 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001165 fuse_invalidate_attr(inode);
1166
1167 return res > 0 ? res : err;
1168}
1169
Al Viro84c3d552014-04-03 14:33:23 -04001170static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001171{
1172 struct file *file = iocb->ki_filp;
1173 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001174 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001175 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001176 struct inode *inode = mapping->host;
1177 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001178 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001179
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001180 if (get_fuse_conn(inode)->writeback_cache) {
1181 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001182 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001183 if (err)
1184 return err;
1185
Al Viro84c3d552014-04-03 14:33:23 -04001186 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001187 }
1188
Al Viro59551022016-01-22 15:40:57 -05001189 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001190
1191 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001192 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001193
Al Viro3309dd02015-04-09 12:55:47 -04001194 err = generic_write_checks(iocb, from);
1195 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001196 goto out;
1197
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001198 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001199 if (err)
1200 goto out;
1201
Josef Bacikc3b2da32012-03-26 09:59:21 -04001202 err = file_update_time(file);
1203 if (err)
1204 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001205
Al Viro2ba48ce2015-04-09 13:52:01 -04001206 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001207 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001208 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001209 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001210 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
Anand Avati4273b792012-02-17 12:46:25 -05001212 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001213
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001214 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001215 if (written_buffered < 0) {
1216 err = written_buffered;
1217 goto out;
1218 }
1219 endbyte = pos + written_buffered - 1;
1220
1221 err = filemap_write_and_wait_range(file->f_mapping, pos,
1222 endbyte);
1223 if (err)
1224 goto out;
1225
1226 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001227 pos >> PAGE_SHIFT,
1228 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001229
1230 written += written_buffered;
1231 iocb->ki_pos = pos + written_buffered;
1232 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001233 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001234 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001235 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001236 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001237out:
1238 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001239 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001240 if (written > 0)
1241 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001242
1243 return written ? written : err;
1244}
1245
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001246static inline void fuse_page_descs_length_init(struct fuse_req *req,
1247 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001248{
1249 int i;
1250
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001251 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001252 req->page_descs[i].length = PAGE_SIZE -
1253 req->page_descs[i].offset;
1254}
1255
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001256static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1257{
1258 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1259}
1260
1261static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1262 size_t max_size)
1263{
1264 return min(iov_iter_single_seg_count(ii), max_size);
1265}
1266
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001267static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001268 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001269{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001270 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001271 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001272
Miklos Szeredif4975c62009-04-02 14:25:34 +02001273 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001274 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001275 unsigned long user_addr = fuse_get_user_addr(ii);
1276 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1277
Miklos Szeredif4975c62009-04-02 14:25:34 +02001278 if (write)
1279 req->in.args[1].value = (void *) user_addr;
1280 else
1281 req->out.args[0].value = (void *) user_addr;
1282
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001283 iov_iter_advance(ii, frag_size);
1284 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001285 return 0;
1286 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001287
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001288 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001289 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001290 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001291 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001292 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001293 req->max_pages - req->num_pages,
1294 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001295 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001296 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001297
Al Viroc9c37e22014-03-16 16:08:30 -04001298 iov_iter_advance(ii, ret);
1299 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001300
Al Viroc9c37e22014-03-16 16:08:30 -04001301 ret += start;
1302 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1303
1304 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001305 fuse_page_descs_length_init(req, req->num_pages, npages);
1306
1307 req->num_pages += npages;
1308 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001309 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001311
1312 if (write)
1313 req->in.argpages = 1;
1314 else
1315 req->out.argpages = 1;
1316
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001317 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001318
Ashish Samant2c932d42016-03-25 10:53:41 -07001319 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001320}
1321
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001322static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1323{
Al Virof67da302014-03-19 01:16:16 -04001324 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001325}
1326
Al Virod22a9432014-03-16 15:50:47 -04001327ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1328 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001329{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001330 int write = flags & FUSE_DIO_WRITE;
1331 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001332 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001333 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001334 struct fuse_file *ff = file->private_data;
1335 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001336 size_t nmax = write ? fc->max_write : fc->max_read;
1337 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001338 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 pgoff_t idx_from = pos >> PAGE_SHIFT;
1340 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001341 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001342 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001343 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001344
Brian Fosterde82b922013-05-14 11:25:39 -04001345 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001346 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001347 else
Al Virod22a9432014-03-16 15:50:47 -04001348 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001349 if (IS_ERR(req))
1350 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001351
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001352 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1353 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001354 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001355 fuse_sync_writes(inode);
1356 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001357 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001358 }
1359
Ashish Samant61c12b42017-07-12 19:26:58 -07001360 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001363 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001364 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001365 err = fuse_get_user_pages(req, iter, &nbytes, write);
1366 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001367 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001368
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001370 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001372 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001373
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001374 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001375 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001377 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378 break;
1379 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001380 res = 0;
1381 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001382 break;
1383 }
1384 count -= nres;
1385 res += nres;
1386 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387 if (nres != nbytes)
1388 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001389 if (count) {
1390 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001391 if (io->async)
1392 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001393 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001394 else
Al Virod22a9432014-03-16 15:50:47 -04001395 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001396 if (IS_ERR(req))
1397 break;
1398 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001399 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001400 if (!IS_ERR(req))
1401 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001402 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001403 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404
Ashish Samant742f9922016-03-14 21:57:35 -07001405 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001406}
Tejun Heo08cbf542009-04-14 10:54:53 +09001407EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001409static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001410 struct iov_iter *iter,
1411 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001412{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001413 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001414 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001415
1416 if (is_bad_inode(inode))
1417 return -EIO;
1418
Al Virod22a9432014-03-16 15:50:47 -04001419 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001420
1421 fuse_invalidate_attr(inode);
1422
1423 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424}
1425
Al Viro15316262015-03-30 22:08:36 -04001426static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001427{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001428 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001429 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001430}
1431
Al Viro15316262015-03-30 22:08:36 -04001432static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001433{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001434 struct inode *inode = file_inode(iocb->ki_filp);
1435 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001436 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001437
1438 if (is_bad_inode(inode))
1439 return -EIO;
1440
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001441 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001442 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001443 res = generic_write_checks(iocb, from);
1444 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001445 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001446 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001447 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001448 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001449 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001450
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001451 return res;
1452}
1453
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001454static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001455{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001456 int i;
1457
1458 for (i = 0; i < req->num_pages; i++)
1459 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001460
1461 if (req->ff)
1462 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001463}
1464
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001465static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001466{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001467 struct inode *inode = req->inode;
1468 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001469 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001470 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001471
1472 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001473 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001474 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001475 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001476 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001477 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001478 wake_up(&fi->page_waitq);
1479}
1480
1481/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001482static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1483 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001484__releases(fc->lock)
1485__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486{
1487 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001488 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001489 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001490
1491 if (!fc->connected)
1492 goto out_free;
1493
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001494 if (inarg->offset + data_size <= size) {
1495 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001496 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001497 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001498 } else {
1499 /* Got truncated off completely */
1500 goto out_free;
1501 }
1502
1503 req->in.args[1].size = inarg->size;
1504 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001505 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001506 return;
1507
1508 out_free:
1509 fuse_writepage_finish(fc, req);
1510 spin_unlock(&fc->lock);
1511 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001512 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001513 spin_lock(&fc->lock);
1514}
1515
1516/*
1517 * If fi->writectr is positive (no truncate or fsync going on) send
1518 * all queued writepage requests.
1519 *
1520 * Called with fc->lock
1521 */
1522void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001523__releases(fc->lock)
1524__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001525{
1526 struct fuse_conn *fc = get_fuse_conn(inode);
1527 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001528 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529 struct fuse_req *req;
1530
1531 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1532 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1533 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001534 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001535 }
1536}
1537
1538static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1539{
1540 struct inode *inode = req->inode;
1541 struct fuse_inode *fi = get_fuse_inode(inode);
1542
1543 mapping_set_error(inode->i_mapping, req->out.h.error);
1544 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001545 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001546 struct fuse_conn *fc = get_fuse_conn(inode);
1547 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001548 struct fuse_req *next = req->misc.write.next;
1549 req->misc.write.next = next->misc.write.next;
1550 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001551 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001552 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001553
1554 /*
1555 * Skip fuse_flush_writepages() to make it easy to crop requests
1556 * based on primary request size.
1557 *
1558 * 1st case (trivial): there are no concurrent activities using
1559 * fuse_set/release_nowrite. Then we're on safe side because
1560 * fuse_flush_writepages() would call fuse_send_writepage()
1561 * anyway.
1562 *
1563 * 2nd case: someone called fuse_set_nowrite and it is waiting
1564 * now for completion of all in-flight requests. This happens
1565 * rarely and no more than once per page, so this should be
1566 * okay.
1567 *
1568 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1569 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1570 * that fuse_set_nowrite returned implies that all in-flight
1571 * requests were completed along with all of their secondary
1572 * requests. Further primary requests are blocked by negative
1573 * writectr. Hence there cannot be any in-flight requests and
1574 * no invocations of fuse_writepage_end() while we're in
1575 * fuse_set_nowrite..fuse_release_nowrite section.
1576 */
1577 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001578 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001579 fi->writectr--;
1580 fuse_writepage_finish(fc, req);
1581 spin_unlock(&fc->lock);
1582 fuse_writepage_free(fc, req);
1583}
1584
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001585static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1586 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001587{
Miklos Szeredi72523422013-10-01 16:44:52 +02001588 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001589
1590 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001591 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001592 ff = list_entry(fi->write_files.next, struct fuse_file,
1593 write_entry);
1594 fuse_file_get(ff);
1595 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001596 spin_unlock(&fc->lock);
1597
1598 return ff;
1599}
1600
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001601static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1602 struct fuse_inode *fi)
1603{
1604 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1605 WARN_ON(!ff);
1606 return ff;
1607}
1608
1609int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1610{
1611 struct fuse_conn *fc = get_fuse_conn(inode);
1612 struct fuse_inode *fi = get_fuse_inode(inode);
1613 struct fuse_file *ff;
1614 int err;
1615
1616 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001617 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001618 if (ff)
1619 fuse_file_put(ff, 0);
1620
1621 return err;
1622}
1623
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001624static int fuse_writepage_locked(struct page *page)
1625{
1626 struct address_space *mapping = page->mapping;
1627 struct inode *inode = mapping->host;
1628 struct fuse_conn *fc = get_fuse_conn(inode);
1629 struct fuse_inode *fi = get_fuse_inode(inode);
1630 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001632 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001633
1634 set_page_writeback(page);
1635
Maxim Patlasov4250c062012-10-26 19:48:07 +04001636 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001637 if (!req)
1638 goto err;
1639
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001640 /* writeback always goes to bg_queue */
1641 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1643 if (!tmp_page)
1644 goto err_free;
1645
Miklos Szeredi72523422013-10-01 16:44:52 +02001646 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001647 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001648 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001649 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001650
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001651 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001652
1653 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001654 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001655 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001656 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657 req->num_pages = 1;
1658 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001659 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001660 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001661 req->end = fuse_writepage_end;
1662 req->inode = inode;
1663
Tejun Heo93f78d82015-05-22 17:13:27 -04001664 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001665 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001666
1667 spin_lock(&fc->lock);
1668 list_add(&req->writepages_entry, &fi->writepages);
1669 list_add_tail(&req->list, &fi->queued_writes);
1670 fuse_flush_writepages(inode);
1671 spin_unlock(&fc->lock);
1672
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001673 end_page_writeback(page);
1674
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001675 return 0;
1676
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001677err_nofile:
1678 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001679err_free:
1680 fuse_request_free(req);
1681err:
Jeff Layton91839762017-05-25 06:57:50 -04001682 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001684 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001685}
1686
1687static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1688{
1689 int err;
1690
Miklos Szerediff17be02013-10-01 16:44:53 +02001691 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1692 /*
1693 * ->writepages() should be called for sync() and friends. We
1694 * should only get here on direct reclaim and then we are
1695 * allowed to skip a page which is already in flight
1696 */
1697 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1698
1699 redirty_page_for_writepage(wbc, page);
1700 return 0;
1701 }
1702
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001703 err = fuse_writepage_locked(page);
1704 unlock_page(page);
1705
1706 return err;
1707}
1708
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001709struct fuse_fill_wb_data {
1710 struct fuse_req *req;
1711 struct fuse_file *ff;
1712 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001713 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001714};
1715
1716static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1717{
1718 struct fuse_req *req = data->req;
1719 struct inode *inode = data->inode;
1720 struct fuse_conn *fc = get_fuse_conn(inode);
1721 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001722 int num_pages = req->num_pages;
1723 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001724
1725 req->ff = fuse_file_get(data->ff);
1726 spin_lock(&fc->lock);
1727 list_add_tail(&req->list, &fi->queued_writes);
1728 fuse_flush_writepages(inode);
1729 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001730
1731 for (i = 0; i < num_pages; i++)
1732 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001733}
1734
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001735static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1736 struct page *page)
1737{
1738 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1739 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1740 struct fuse_req *tmp;
1741 struct fuse_req *old_req;
1742 bool found = false;
1743 pgoff_t curr_index;
1744
1745 BUG_ON(new_req->num_pages != 0);
1746
1747 spin_lock(&fc->lock);
1748 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001749 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1750 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001751 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001752 if (curr_index <= page->index &&
1753 page->index < curr_index + old_req->num_pages) {
1754 found = true;
1755 break;
1756 }
1757 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001758 if (!found) {
1759 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001760 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001761 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001762
Maxim Patlasovf6011082013-10-02 15:01:07 +04001763 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001764 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1765 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001766 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767 if (tmp->num_pages == 1 &&
1768 curr_index == page->index) {
1769 old_req = tmp;
1770 }
1771 }
1772
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001773 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001774 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001775
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001776 copy_highpage(old_req->pages[0], page);
1777 spin_unlock(&fc->lock);
1778
Tejun Heo93f78d82015-05-22 17:13:27 -04001779 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001780 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001781 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001782 fuse_writepage_free(fc, new_req);
1783 fuse_request_free(new_req);
1784 goto out;
1785 } else {
1786 new_req->misc.write.next = old_req->misc.write.next;
1787 old_req->misc.write.next = new_req;
1788 }
1789out_unlock:
1790 spin_unlock(&fc->lock);
1791out:
1792 return found;
1793}
1794
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001795static int fuse_writepages_fill(struct page *page,
1796 struct writeback_control *wbc, void *_data)
1797{
1798 struct fuse_fill_wb_data *data = _data;
1799 struct fuse_req *req = data->req;
1800 struct inode *inode = data->inode;
1801 struct fuse_conn *fc = get_fuse_conn(inode);
1802 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001803 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001804 int err;
1805
1806 if (!data->ff) {
1807 err = -EIO;
1808 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1809 if (!data->ff)
1810 goto out_unlock;
1811 }
1812
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001813 /*
1814 * Being under writeback is unlikely but possible. For example direct
1815 * read to an mmaped fuse file will set the page dirty twice; once when
1816 * the pages are faulted with get_user_pages(), and then after the read
1817 * completed.
1818 */
1819 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001820
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001821 if (req && req->num_pages &&
1822 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001823 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001824 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1825 fuse_writepages_send(data);
1826 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001827 }
1828 err = -ENOMEM;
1829 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1830 if (!tmp_page)
1831 goto out_unlock;
1832
1833 /*
1834 * The page must not be redirtied until the writeout is completed
1835 * (i.e. userspace has sent a reply to the write request). Otherwise
1836 * there could be more than one temporary page instance for each real
1837 * page.
1838 *
1839 * This is ensured by holding the page lock in page_mkwrite() while
1840 * checking fuse_page_is_writeback(). We already hold the page lock
1841 * since clear_page_dirty_for_io() and keep it held until we add the
1842 * request to the fi->writepages list and increment req->num_pages.
1843 * After this fuse_page_is_writeback() will indicate that the page is
1844 * under writeback, so we can release the page lock.
1845 */
1846 if (data->req == NULL) {
1847 struct fuse_inode *fi = get_fuse_inode(inode);
1848
1849 err = -ENOMEM;
1850 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1851 if (!req) {
1852 __free_page(tmp_page);
1853 goto out_unlock;
1854 }
1855
1856 fuse_write_fill(req, data->ff, page_offset(page), 0);
1857 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001858 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001859 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001860 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001861 req->num_pages = 0;
1862 req->end = fuse_writepage_end;
1863 req->inode = inode;
1864
1865 spin_lock(&fc->lock);
1866 list_add(&req->writepages_entry, &fi->writepages);
1867 spin_unlock(&fc->lock);
1868
1869 data->req = req;
1870 }
1871 set_page_writeback(page);
1872
1873 copy_highpage(tmp_page, page);
1874 req->pages[req->num_pages] = tmp_page;
1875 req->page_descs[req->num_pages].offset = 0;
1876 req->page_descs[req->num_pages].length = PAGE_SIZE;
1877
Tejun Heo93f78d82015-05-22 17:13:27 -04001878 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001879 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001880
1881 err = 0;
1882 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1883 end_page_writeback(page);
1884 data->req = NULL;
1885 goto out_unlock;
1886 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001887 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001888
1889 /*
1890 * Protected by fc->lock against concurrent access by
1891 * fuse_page_is_writeback().
1892 */
1893 spin_lock(&fc->lock);
1894 req->num_pages++;
1895 spin_unlock(&fc->lock);
1896
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001897out_unlock:
1898 unlock_page(page);
1899
1900 return err;
1901}
1902
1903static int fuse_writepages(struct address_space *mapping,
1904 struct writeback_control *wbc)
1905{
1906 struct inode *inode = mapping->host;
1907 struct fuse_fill_wb_data data;
1908 int err;
1909
1910 err = -EIO;
1911 if (is_bad_inode(inode))
1912 goto out;
1913
1914 data.inode = inode;
1915 data.req = NULL;
1916 data.ff = NULL;
1917
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001918 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001919 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1920 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001921 GFP_NOFS);
1922 if (!data.orig_pages)
1923 goto out;
1924
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001925 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1926 if (data.req) {
1927 /* Ignore errors if we can write at least one page */
1928 BUG_ON(!data.req->num_pages);
1929 fuse_writepages_send(&data);
1930 err = 0;
1931 }
1932 if (data.ff)
1933 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001934
1935 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001936out:
1937 return err;
1938}
1939
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001940/*
1941 * It's worthy to make sure that space is reserved on disk for the write,
1942 * but how to implement it without killing performance need more thinking.
1943 */
1944static int fuse_write_begin(struct file *file, struct address_space *mapping,
1945 loff_t pos, unsigned len, unsigned flags,
1946 struct page **pagep, void **fsdata)
1947{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001948 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001949 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001950 struct page *page;
1951 loff_t fsize;
1952 int err = -ENOMEM;
1953
1954 WARN_ON(!fc->writeback_cache);
1955
1956 page = grab_cache_page_write_begin(mapping, index, flags);
1957 if (!page)
1958 goto error;
1959
1960 fuse_wait_on_page_writeback(mapping->host, page->index);
1961
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001962 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001963 goto success;
1964 /*
1965 * Check if the start this page comes after the end of file, in which
1966 * case the readpage can be optimized away.
1967 */
1968 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001969 if (fsize <= (pos & PAGE_MASK)) {
1970 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001971 if (off)
1972 zero_user_segment(page, 0, off);
1973 goto success;
1974 }
1975 err = fuse_do_readpage(file, page);
1976 if (err)
1977 goto cleanup;
1978success:
1979 *pagep = page;
1980 return 0;
1981
1982cleanup:
1983 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001984 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001985error:
1986 return err;
1987}
1988
1989static int fuse_write_end(struct file *file, struct address_space *mapping,
1990 loff_t pos, unsigned len, unsigned copied,
1991 struct page *page, void *fsdata)
1992{
1993 struct inode *inode = page->mapping->host;
1994
Miklos Szeredi59c3b762016-08-18 09:10:44 +02001995 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1996 if (!copied)
1997 goto unlock;
1998
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001999 if (!PageUptodate(page)) {
2000 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002001 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002002 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002003 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002004 SetPageUptodate(page);
2005 }
2006
2007 fuse_write_update_size(inode, pos + copied);
2008 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002009
2010unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002011 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002012 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002013
2014 return copied;
2015}
2016
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002017static int fuse_launder_page(struct page *page)
2018{
2019 int err = 0;
2020 if (clear_page_dirty_for_io(page)) {
2021 struct inode *inode = page->mapping->host;
2022 err = fuse_writepage_locked(page);
2023 if (!err)
2024 fuse_wait_on_page_writeback(inode, page->index);
2025 }
2026 return err;
2027}
2028
2029/*
2030 * Write back dirty pages now, because there may not be any suitable
2031 * open files later
2032 */
2033static void fuse_vma_close(struct vm_area_struct *vma)
2034{
2035 filemap_write_and_wait(vma->vm_file->f_mapping);
2036}
2037
2038/*
2039 * Wait for writeback against this page to complete before allowing it
2040 * to be marked dirty again, and hence written back again, possibly
2041 * before the previous writepage completed.
2042 *
2043 * Block here, instead of in ->writepage(), so that the userspace fs
2044 * can only block processes actually operating on the filesystem.
2045 *
2046 * Otherwise unprivileged userspace fs would be able to block
2047 * unrelated:
2048 *
2049 * - page migration
2050 * - sync(2)
2051 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2052 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302053static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002054{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002055 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002056 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002057
Dave Jiang11bac802017-02-24 14:56:41 -08002058 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002059 lock_page(page);
2060 if (page->mapping != inode->i_mapping) {
2061 unlock_page(page);
2062 return VM_FAULT_NOPAGE;
2063 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002064
2065 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002066 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002067}
2068
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002069static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002070 .close = fuse_vma_close,
2071 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002072 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002073 .page_mkwrite = fuse_page_mkwrite,
2074};
2075
2076static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2077{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002078 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2079 fuse_link_write_file(file);
2080
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002081 file_accessed(file);
2082 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002083 return 0;
2084}
2085
Miklos Szeredifc280c92009-04-02 14:25:35 +02002086static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2087{
2088 /* Can't provide the coherency needed for MAP_SHARED */
2089 if (vma->vm_flags & VM_MAYSHARE)
2090 return -ENODEV;
2091
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002092 invalidate_inode_pages2(file->f_mapping);
2093
Miklos Szeredifc280c92009-04-02 14:25:35 +02002094 return generic_file_mmap(file, vma);
2095}
2096
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002097static int convert_fuse_file_lock(struct fuse_conn *fc,
2098 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002099 struct file_lock *fl)
2100{
2101 switch (ffl->type) {
2102 case F_UNLCK:
2103 break;
2104
2105 case F_RDLCK:
2106 case F_WRLCK:
2107 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2108 ffl->end < ffl->start)
2109 return -EIO;
2110
2111 fl->fl_start = ffl->start;
2112 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002113
2114 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002115 * Convert pid into init's pid namespace. The locks API will
2116 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002117 */
2118 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002119 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002120 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002121 break;
2122
2123 default:
2124 return -EIO;
2125 }
2126 fl->fl_type = ffl->type;
2127 return 0;
2128}
2129
Miklos Szeredi70781872014-12-12 09:49:05 +01002130static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002131 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002132 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002133{
Al Viro6131ffa2013-02-27 16:59:05 -05002134 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002135 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002136 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002137
Miklos Szeredi70781872014-12-12 09:49:05 +01002138 memset(inarg, 0, sizeof(*inarg));
2139 inarg->fh = ff->fh;
2140 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2141 inarg->lk.start = fl->fl_start;
2142 inarg->lk.end = fl->fl_end;
2143 inarg->lk.type = fl->fl_type;
2144 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002145 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002146 inarg->lk_flags |= FUSE_LK_FLOCK;
2147 args->in.h.opcode = opcode;
2148 args->in.h.nodeid = get_node_id(inode);
2149 args->in.numargs = 1;
2150 args->in.args[0].size = sizeof(*inarg);
2151 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002152}
2153
2154static int fuse_getlk(struct file *file, struct file_lock *fl)
2155{
Al Viro6131ffa2013-02-27 16:59:05 -05002156 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002157 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002158 FUSE_ARGS(args);
2159 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002160 struct fuse_lk_out outarg;
2161 int err;
2162
Miklos Szeredi70781872014-12-12 09:49:05 +01002163 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2164 args.out.numargs = 1;
2165 args.out.args[0].size = sizeof(outarg);
2166 args.out.args[0].value = &outarg;
2167 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002169 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002170
2171 return err;
2172}
2173
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002174static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002175{
Al Viro6131ffa2013-02-27 16:59:05 -05002176 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002177 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002178 FUSE_ARGS(args);
2179 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002180 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002181 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2182 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002183 int err;
2184
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002185 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002186 /* NLM needs asynchronous locks, which we don't support yet */
2187 return -ENOLCK;
2188 }
2189
Miklos Szeredi71421252006-06-25 05:48:52 -07002190 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002191 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002192 return 0;
2193
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002194 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002195 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002196
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002197 /* locking is restartable */
2198 if (err == -EINTR)
2199 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002200
Miklos Szeredi71421252006-06-25 05:48:52 -07002201 return err;
2202}
2203
2204static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2205{
Al Viro6131ffa2013-02-27 16:59:05 -05002206 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002207 struct fuse_conn *fc = get_fuse_conn(inode);
2208 int err;
2209
Miklos Szeredi48e90762008-07-25 01:49:02 -07002210 if (cmd == F_CANCELLK) {
2211 err = 0;
2212 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002213 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002214 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002215 err = 0;
2216 } else
2217 err = fuse_getlk(file, fl);
2218 } else {
2219 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002220 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002221 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002222 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 }
2224 return err;
2225}
2226
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002227static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2228{
Al Viro6131ffa2013-02-27 16:59:05 -05002229 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002230 struct fuse_conn *fc = get_fuse_conn(inode);
2231 int err;
2232
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002233 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002234 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002235 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002236 struct fuse_file *ff = file->private_data;
2237
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002239 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002240 err = fuse_setlk(file, fl, 1);
2241 }
2242
2243 return err;
2244}
2245
Miklos Szeredib2d22722006-12-06 20:35:51 -08002246static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2247{
2248 struct inode *inode = mapping->host;
2249 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002250 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002251 struct fuse_bmap_in inarg;
2252 struct fuse_bmap_out outarg;
2253 int err;
2254
2255 if (!inode->i_sb->s_bdev || fc->no_bmap)
2256 return 0;
2257
Miklos Szeredib2d22722006-12-06 20:35:51 -08002258 memset(&inarg, 0, sizeof(inarg));
2259 inarg.block = block;
2260 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002261 args.in.h.opcode = FUSE_BMAP;
2262 args.in.h.nodeid = get_node_id(inode);
2263 args.in.numargs = 1;
2264 args.in.args[0].size = sizeof(inarg);
2265 args.in.args[0].value = &inarg;
2266 args.out.numargs = 1;
2267 args.out.args[0].size = sizeof(outarg);
2268 args.out.args[0].value = &outarg;
2269 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002270 if (err == -ENOSYS)
2271 fc->no_bmap = 1;
2272
2273 return err ? 0 : outarg.block;
2274}
2275
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302276static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2277{
2278 struct inode *inode = file->f_mapping->host;
2279 struct fuse_conn *fc = get_fuse_conn(inode);
2280 struct fuse_file *ff = file->private_data;
2281 FUSE_ARGS(args);
2282 struct fuse_lseek_in inarg = {
2283 .fh = ff->fh,
2284 .offset = offset,
2285 .whence = whence
2286 };
2287 struct fuse_lseek_out outarg;
2288 int err;
2289
2290 if (fc->no_lseek)
2291 goto fallback;
2292
2293 args.in.h.opcode = FUSE_LSEEK;
2294 args.in.h.nodeid = ff->nodeid;
2295 args.in.numargs = 1;
2296 args.in.args[0].size = sizeof(inarg);
2297 args.in.args[0].value = &inarg;
2298 args.out.numargs = 1;
2299 args.out.args[0].size = sizeof(outarg);
2300 args.out.args[0].value = &outarg;
2301 err = fuse_simple_request(fc, &args);
2302 if (err) {
2303 if (err == -ENOSYS) {
2304 fc->no_lseek = 1;
2305 goto fallback;
2306 }
2307 return err;
2308 }
2309
2310 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2311
2312fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002313 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302314 if (!err)
2315 return generic_file_llseek(file, offset, whence);
2316 else
2317 return err;
2318}
2319
Andrew Morton965c8e52012-12-17 15:59:39 -08002320static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002321{
2322 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002323 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002324
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302325 switch (whence) {
2326 case SEEK_SET:
2327 case SEEK_CUR:
2328 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002329 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302330 break;
2331 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002332 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002333 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302334 if (!retval)
2335 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002336 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302337 break;
2338 case SEEK_HOLE:
2339 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002340 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302341 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002342 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302343 break;
2344 default:
2345 retval = -EINVAL;
2346 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002347
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002348 return retval;
2349}
2350
Tejun Heo59efec72008-11-26 12:03:55 +01002351/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002352 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2353 * ABI was defined to be 'struct iovec' which is different on 32bit
2354 * and 64bit. Fortunately we can determine which structure the server
2355 * used from the size of the reply.
2356 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002357static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2358 size_t transferred, unsigned count,
2359 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002360{
2361#ifdef CONFIG_COMPAT
2362 if (count * sizeof(struct compat_iovec) == transferred) {
2363 struct compat_iovec *ciov = src;
2364 unsigned i;
2365
2366 /*
2367 * With this interface a 32bit server cannot support
2368 * non-compat (i.e. ones coming from 64bit apps) ioctl
2369 * requests
2370 */
2371 if (!is_compat)
2372 return -EINVAL;
2373
2374 for (i = 0; i < count; i++) {
2375 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2376 dst[i].iov_len = ciov[i].iov_len;
2377 }
2378 return 0;
2379 }
2380#endif
2381
2382 if (count * sizeof(struct iovec) != transferred)
2383 return -EIO;
2384
2385 memcpy(dst, src, transferred);
2386 return 0;
2387}
2388
Miklos Szeredi75727772010-11-30 16:39:27 +01002389/* Make sure iov_length() won't overflow */
2390static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2391{
2392 size_t n;
2393 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2394
Zach Brownfb6ccff2012-07-24 12:10:11 -07002395 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002396 if (iov->iov_len > (size_t) max)
2397 return -ENOMEM;
2398 max -= iov->iov_len;
2399 }
2400 return 0;
2401}
2402
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002403static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2404 void *src, size_t transferred, unsigned count,
2405 bool is_compat)
2406{
2407 unsigned i;
2408 struct fuse_ioctl_iovec *fiov = src;
2409
2410 if (fc->minor < 16) {
2411 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2412 count, is_compat);
2413 }
2414
2415 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2416 return -EIO;
2417
2418 for (i = 0; i < count; i++) {
2419 /* Did the server supply an inappropriate value? */
2420 if (fiov[i].base != (unsigned long) fiov[i].base ||
2421 fiov[i].len != (unsigned long) fiov[i].len)
2422 return -EIO;
2423
2424 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2425 dst[i].iov_len = (size_t) fiov[i].len;
2426
2427#ifdef CONFIG_COMPAT
2428 if (is_compat &&
2429 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2430 (compat_size_t) dst[i].iov_len != fiov[i].len))
2431 return -EIO;
2432#endif
2433 }
2434
2435 return 0;
2436}
2437
2438
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002439/*
Tejun Heo59efec72008-11-26 12:03:55 +01002440 * For ioctls, there is no generic way to determine how much memory
2441 * needs to be read and/or written. Furthermore, ioctls are allowed
2442 * to dereference the passed pointer, so the parameter requires deep
2443 * copying but FUSE has no idea whatsoever about what to copy in or
2444 * out.
2445 *
2446 * This is solved by allowing FUSE server to retry ioctl with
2447 * necessary in/out iovecs. Let's assume the ioctl implementation
2448 * needs to read in the following structure.
2449 *
2450 * struct a {
2451 * char *buf;
2452 * size_t buflen;
2453 * }
2454 *
2455 * On the first callout to FUSE server, inarg->in_size and
2456 * inarg->out_size will be NULL; then, the server completes the ioctl
2457 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2458 * the actual iov array to
2459 *
2460 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2461 *
2462 * which tells FUSE to copy in the requested area and retry the ioctl.
2463 * On the second round, the server has access to the structure and
2464 * from that it can tell what to look for next, so on the invocation,
2465 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2466 *
2467 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2468 * { .iov_base = a.buf, .iov_len = a.buflen } }
2469 *
2470 * FUSE will copy both struct a and the pointed buffer from the
2471 * process doing the ioctl and retry ioctl with both struct a and the
2472 * buffer.
2473 *
2474 * This time, FUSE server has everything it needs and completes ioctl
2475 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2476 *
2477 * Copying data out works the same way.
2478 *
2479 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2480 * automatically initializes in and out iovs by decoding @cmd with
2481 * _IOC_* macros and the server is not allowed to request RETRY. This
2482 * limits ioctl data transfers to well-formed ioctls and is the forced
2483 * behavior for all FUSE servers.
2484 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002485long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2486 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002487{
Tejun Heo59efec72008-11-26 12:03:55 +01002488 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002489 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002490 struct fuse_ioctl_in inarg = {
2491 .fh = ff->fh,
2492 .cmd = cmd,
2493 .arg = arg,
2494 .flags = flags
2495 };
2496 struct fuse_ioctl_out outarg;
2497 struct fuse_req *req = NULL;
2498 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002499 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002500 struct iovec *in_iov = NULL, *out_iov = NULL;
2501 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002502 size_t in_size, out_size, transferred, c;
2503 int err, i;
2504 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002505
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002506#if BITS_PER_LONG == 32
2507 inarg.flags |= FUSE_IOCTL_32BIT;
2508#else
2509 if (flags & FUSE_IOCTL_COMPAT)
2510 inarg.flags |= FUSE_IOCTL_32BIT;
2511#endif
2512
Tejun Heo59efec72008-11-26 12:03:55 +01002513 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002514 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002515
Tejun Heo59efec72008-11-26 12:03:55 +01002516 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002517 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002518 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002519 if (!pages || !iov_page)
2520 goto out;
2521
2522 /*
2523 * If restricted, initialize IO parameters as encoded in @cmd.
2524 * RETRY from server is not allowed.
2525 */
2526 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002527 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002528
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002529 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002530 iov->iov_len = _IOC_SIZE(cmd);
2531
2532 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2533 in_iov = iov;
2534 in_iovs = 1;
2535 }
2536
2537 if (_IOC_DIR(cmd) & _IOC_READ) {
2538 out_iov = iov;
2539 out_iovs = 1;
2540 }
2541 }
2542
2543 retry:
2544 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2545 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2546
2547 /*
2548 * Out data can be used either for actual out data or iovs,
2549 * make sure there always is at least one page.
2550 */
2551 out_size = max_t(size_t, out_size, PAGE_SIZE);
2552 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2553
2554 /* make sure there are enough buffer pages and init request with them */
2555 err = -ENOMEM;
2556 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2557 goto out;
2558 while (num_pages < max_pages) {
2559 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2560 if (!pages[num_pages])
2561 goto out;
2562 num_pages++;
2563 }
2564
Maxim Patlasov54b96672012-10-26 19:49:13 +04002565 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002566 if (IS_ERR(req)) {
2567 err = PTR_ERR(req);
2568 req = NULL;
2569 goto out;
2570 }
2571 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2572 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002573 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002574
2575 /* okay, let's send it to the client */
2576 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002577 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002578 req->in.numargs = 1;
2579 req->in.args[0].size = sizeof(inarg);
2580 req->in.args[0].value = &inarg;
2581 if (in_size) {
2582 req->in.numargs++;
2583 req->in.args[1].size = in_size;
2584 req->in.argpages = 1;
2585
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002586 err = -EFAULT;
2587 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2588 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2589 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2590 if (c != PAGE_SIZE && iov_iter_count(&ii))
2591 goto out;
2592 }
Tejun Heo59efec72008-11-26 12:03:55 +01002593 }
2594
2595 req->out.numargs = 2;
2596 req->out.args[0].size = sizeof(outarg);
2597 req->out.args[0].value = &outarg;
2598 req->out.args[1].size = out_size;
2599 req->out.argpages = 1;
2600 req->out.argvar = 1;
2601
Tejun Heob93f8582008-11-26 12:03:55 +01002602 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002603 err = req->out.h.error;
2604 transferred = req->out.args[1].size;
2605 fuse_put_request(fc, req);
2606 req = NULL;
2607 if (err)
2608 goto out;
2609
2610 /* did it ask for retry? */
2611 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002612 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002613
2614 /* no retry if in restricted mode */
2615 err = -EIO;
2616 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2617 goto out;
2618
2619 in_iovs = outarg.in_iovs;
2620 out_iovs = outarg.out_iovs;
2621
2622 /*
2623 * Make sure things are in boundary, separate checks
2624 * are to protect against overflow.
2625 */
2626 err = -ENOMEM;
2627 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2628 out_iovs > FUSE_IOCTL_MAX_IOV ||
2629 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2630 goto out;
2631
Cong Wang2408f6e2011-11-25 23:14:30 +08002632 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002633 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002634 transferred, in_iovs + out_iovs,
2635 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002636 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002637 if (err)
2638 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002639
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002640 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002641 out_iov = in_iov + in_iovs;
2642
Miklos Szeredi75727772010-11-30 16:39:27 +01002643 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2644 if (err)
2645 goto out;
2646
2647 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2648 if (err)
2649 goto out;
2650
Tejun Heo59efec72008-11-26 12:03:55 +01002651 goto retry;
2652 }
2653
2654 err = -EIO;
2655 if (transferred > inarg.out_size)
2656 goto out;
2657
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002658 err = -EFAULT;
2659 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2660 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2661 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2662 if (c != PAGE_SIZE && iov_iter_count(&ii))
2663 goto out;
2664 }
2665 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002666 out:
2667 if (req)
2668 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002669 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002670 while (num_pages)
2671 __free_page(pages[--num_pages]);
2672 kfree(pages);
2673
2674 return err ? err : outarg.result;
2675}
Tejun Heo08cbf542009-04-14 10:54:53 +09002676EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002677
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002678long fuse_ioctl_common(struct file *file, unsigned int cmd,
2679 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002680{
Al Viro6131ffa2013-02-27 16:59:05 -05002681 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002682 struct fuse_conn *fc = get_fuse_conn(inode);
2683
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002684 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002685 return -EACCES;
2686
2687 if (is_bad_inode(inode))
2688 return -EIO;
2689
2690 return fuse_do_ioctl(file, cmd, arg, flags);
2691}
2692
Tejun Heo59efec72008-11-26 12:03:55 +01002693static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2694 unsigned long arg)
2695{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002696 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002697}
2698
2699static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2700 unsigned long arg)
2701{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002702 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002703}
2704
Tejun Heo95668a62008-11-26 12:03:55 +01002705/*
2706 * All files which have been polled are linked to RB tree
2707 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2708 * find the matching one.
2709 */
2710static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2711 struct rb_node **parent_out)
2712{
2713 struct rb_node **link = &fc->polled_files.rb_node;
2714 struct rb_node *last = NULL;
2715
2716 while (*link) {
2717 struct fuse_file *ff;
2718
2719 last = *link;
2720 ff = rb_entry(last, struct fuse_file, polled_node);
2721
2722 if (kh < ff->kh)
2723 link = &last->rb_left;
2724 else if (kh > ff->kh)
2725 link = &last->rb_right;
2726 else
2727 return link;
2728 }
2729
2730 if (parent_out)
2731 *parent_out = last;
2732 return link;
2733}
2734
2735/*
2736 * The file is about to be polled. Make sure it's on the polled_files
2737 * RB tree. Note that files once added to the polled_files tree are
2738 * not removed before the file is released. This is because a file
2739 * polled once is likely to be polled again.
2740 */
2741static void fuse_register_polled_file(struct fuse_conn *fc,
2742 struct fuse_file *ff)
2743{
2744 spin_lock(&fc->lock);
2745 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002746 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002747
2748 link = fuse_find_polled_node(fc, ff->kh, &parent);
2749 BUG_ON(*link);
2750 rb_link_node(&ff->polled_node, parent, link);
2751 rb_insert_color(&ff->polled_node, &fc->polled_files);
2752 }
2753 spin_unlock(&fc->lock);
2754}
2755
Al Viro076ccb72017-07-03 01:02:18 -04002756__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002757{
Tejun Heo95668a62008-11-26 12:03:55 +01002758 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002759 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002760 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2761 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002762 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002763 int err;
2764
2765 if (fc->no_poll)
2766 return DEFAULT_POLLMASK;
2767
2768 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002769 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002770
2771 /*
2772 * Ask for notification iff there's someone waiting for it.
2773 * The client may ignore the flag and always notify.
2774 */
2775 if (waitqueue_active(&ff->poll_wait)) {
2776 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2777 fuse_register_polled_file(fc, ff);
2778 }
2779
Miklos Szeredi70781872014-12-12 09:49:05 +01002780 args.in.h.opcode = FUSE_POLL;
2781 args.in.h.nodeid = ff->nodeid;
2782 args.in.numargs = 1;
2783 args.in.args[0].size = sizeof(inarg);
2784 args.in.args[0].value = &inarg;
2785 args.out.numargs = 1;
2786 args.out.args[0].size = sizeof(outarg);
2787 args.out.args[0].value = &outarg;
2788 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002789
2790 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002791 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002792 if (err == -ENOSYS) {
2793 fc->no_poll = 1;
2794 return DEFAULT_POLLMASK;
2795 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002796 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002797}
Tejun Heo08cbf542009-04-14 10:54:53 +09002798EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002799
2800/*
2801 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2802 * wakes up the poll waiters.
2803 */
2804int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2805 struct fuse_notify_poll_wakeup_out *outarg)
2806{
2807 u64 kh = outarg->kh;
2808 struct rb_node **link;
2809
2810 spin_lock(&fc->lock);
2811
2812 link = fuse_find_polled_node(fc, kh, NULL);
2813 if (*link) {
2814 struct fuse_file *ff;
2815
2816 ff = rb_entry(*link, struct fuse_file, polled_node);
2817 wake_up_interruptible_sync(&ff->poll_wait);
2818 }
2819
2820 spin_unlock(&fc->lock);
2821 return 0;
2822}
2823
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002824static void fuse_do_truncate(struct file *file)
2825{
2826 struct inode *inode = file->f_mapping->host;
2827 struct iattr attr;
2828
2829 attr.ia_valid = ATTR_SIZE;
2830 attr.ia_size = i_size_read(inode);
2831
2832 attr.ia_file = file;
2833 attr.ia_valid |= ATTR_FILE;
2834
Jan Kara62490332016-05-26 17:12:41 +02002835 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002836}
2837
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002838static inline loff_t fuse_round_up(loff_t off)
2839{
2840 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2841}
2842
Anand Avati4273b792012-02-17 12:46:25 -05002843static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002844fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002845{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002846 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002847 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002848 struct file *file = iocb->ki_filp;
2849 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002850 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002851 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002852 struct inode *inode;
2853 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002854 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002855 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002856 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002857
Anand Avati4273b792012-02-17 12:46:25 -05002858 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002859 inode = file->f_mapping->host;
2860 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002861
Omar Sandoval6f673762015-03-16 04:33:52 -07002862 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002863 return 0;
2864
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002865 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002866 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002867 if (offset >= i_size)
2868 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002869 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2870 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002871 }
2872
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002873 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002874 if (!io)
2875 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002876 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002877 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002878 io->reqs = 1;
2879 io->bytes = -1;
2880 io->size = 0;
2881 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002882 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002883 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002884 /*
2885 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002886 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002887 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002888 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302890 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002891
2892 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302893 * We cannot asynchronously extend the size of a file.
2894 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302896 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2897 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002898
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302899 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002900 /*
2901 * Additional reference to keep io around after
2902 * calling fuse_aio_complete()
2903 */
2904 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002905 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002906 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002907
Omar Sandoval6f673762015-03-16 04:33:52 -07002908 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002909 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002910 fuse_invalidate_attr(inode);
2911 } else {
Al Virod22a9432014-03-16 15:50:47 -04002912 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002913 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002914
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002915 if (io->async) {
2916 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2917
2918 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302919 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002920 return -EIOCBQUEUED;
2921
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002922 wait_for_completion(&wait);
2923 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002924 }
2925
Seth Forshee744742d2016-03-11 10:35:34 -06002926 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002927
Omar Sandoval6f673762015-03-16 04:33:52 -07002928 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002929 if (ret > 0)
2930 fuse_write_update_size(inode, pos);
2931 else if (ret < 0 && offset + count > i_size)
2932 fuse_do_truncate(file);
2933 }
Anand Avati4273b792012-02-17 12:46:25 -05002934
2935 return ret;
2936}
2937
Miklos Szeredicdadb112012-11-10 16:55:56 +01002938static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2939 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002940{
2941 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002942 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002943 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002944 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002945 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002946 struct fuse_fallocate_in inarg = {
2947 .fh = ff->fh,
2948 .offset = offset,
2949 .length = length,
2950 .mode = mode
2951 };
2952 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002953 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2954 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002955
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002956 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2957 return -EOPNOTSUPP;
2958
Miklos Szeredi519c6042012-04-26 10:56:36 +02002959 if (fc->no_fallocate)
2960 return -EOPNOTSUPP;
2961
Maxim Patlasov14c14412013-06-13 12:16:39 +04002962 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002963 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002964 if (mode & FALLOC_FL_PUNCH_HOLE) {
2965 loff_t endbyte = offset + length - 1;
2966 err = filemap_write_and_wait_range(inode->i_mapping,
2967 offset, endbyte);
2968 if (err)
2969 goto out;
2970
2971 fuse_sync_writes(inode);
2972 }
Brian Foster3634a632013-05-17 09:30:32 -04002973 }
2974
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002975 if (!(mode & FALLOC_FL_KEEP_SIZE))
2976 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2977
Miklos Szeredi70781872014-12-12 09:49:05 +01002978 args.in.h.opcode = FUSE_FALLOCATE;
2979 args.in.h.nodeid = ff->nodeid;
2980 args.in.numargs = 1;
2981 args.in.args[0].size = sizeof(inarg);
2982 args.in.args[0].value = &inarg;
2983 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002984 if (err == -ENOSYS) {
2985 fc->no_fallocate = 1;
2986 err = -EOPNOTSUPP;
2987 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002988 if (err)
2989 goto out;
2990
2991 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002992 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2993 bool changed = fuse_write_update_size(inode, offset + length);
2994
Miklos Szeredi93d22692014-04-28 14:19:22 +02002995 if (changed && fc->writeback_cache)
2996 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002997 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002998
2999 if (mode & FALLOC_FL_PUNCH_HOLE)
3000 truncate_pagecache_range(inode, offset, offset + length - 1);
3001
3002 fuse_invalidate_attr(inode);
3003
Brian Foster3634a632013-05-17 09:30:32 -04003004out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003005 if (!(mode & FALLOC_FL_KEEP_SIZE))
3006 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3007
Maxim Patlasovbde52782013-09-13 19:19:54 +04003008 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003009 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003010
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003011 return err;
3012}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003013
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003014static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003015 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003016 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003017 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003018 .mmap = fuse_file_mmap,
3019 .open = fuse_open,
3020 .flush = fuse_flush,
3021 .release = fuse_release,
3022 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003023 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003024 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003025 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003026 .unlocked_ioctl = fuse_file_ioctl,
3027 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003028 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003029 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003030};
3031
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003032static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003033 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003034 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003035 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003036 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003037 .open = fuse_open,
3038 .flush = fuse_flush,
3039 .release = fuse_release,
3040 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003041 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003042 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003043 .unlocked_ioctl = fuse_file_ioctl,
3044 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003045 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003046 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003047 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003048};
3049
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003050static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003051 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003052 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003053 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003054 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003055 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003056 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003057 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003058 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003059 .write_begin = fuse_write_begin,
3060 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003061};
3062
3063void fuse_init_file_inode(struct inode *inode)
3064{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003065 inode->i_fop = &fuse_file_operations;
3066 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003067}