blob: b00a3f126a89a7deee198544987fba547814102f [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)) {
870 fuse_put_request(fc, req);
871 return -EIO;
872 }
873
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300874 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400876 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100877 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400878 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 return 0;
880}
881
882static int fuse_readpages(struct file *file, struct address_space *mapping,
883 struct list_head *pages, unsigned nr_pages)
884{
885 struct inode *inode = mapping->host;
886 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700887 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700888 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400889 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800890
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700891 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800892 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800893 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800894
Miklos Szeredia6643092007-11-28 16:22:00 -0800895 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700896 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400897 if (fc->async_read)
898 data.req = fuse_get_req_for_background(fc, nr_alloc);
899 else
900 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400901 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700902 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700903 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800904 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700905
906 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700907 if (!err) {
908 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200909 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700910 else
911 fuse_put_request(fc, data.req);
912 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800913out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700914 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915}
916
Al Viro37c20f12014-04-02 14:47:09 -0400917static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800918{
919 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400920 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800921
Brian Fostera8894272012-07-16 15:23:50 -0400922 /*
923 * In auto invalidate mode, always update attributes on read.
924 * Otherwise, only update if we attempt to read past EOF (to ensure
925 * i_size is up to date).
926 */
927 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400928 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800929 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200930 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931 if (err)
932 return err;
933 }
934
Al Viro37c20f12014-04-02 14:47:09 -0400935 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936}
937
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200938static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200939 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700940{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700941 struct fuse_write_in *inarg = &req->misc.write.in;
942 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700943
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700944 inarg->fh = ff->fh;
945 inarg->offset = pos;
946 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700947 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200948 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700949 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700951 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
952 else
953 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700954 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955 req->in.args[1].size = count;
956 req->out.numargs = 1;
957 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958 req->out.args[0].value = outarg;
959}
960
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400961static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200962 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700963{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200964 struct kiocb *iocb = io->iocb;
965 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 struct fuse_file *ff = file->private_data;
967 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200968 struct fuse_write_in *inarg = &req->misc.write.in;
969
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200972 if (iocb->ki_flags & IOCB_DSYNC)
973 inarg->flags |= O_DSYNC;
974 if (iocb->ki_flags & IOCB_SYNC)
975 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700976 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700977 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
978 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
979 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400980
981 if (io->async)
982 return fuse_async_req_send(fc, req, count, io);
983
Tejun Heob93f8582008-11-26 12:03:55 +0100984 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700985 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700986}
987
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400988bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700989{
990 struct fuse_conn *fc = get_fuse_conn(inode);
991 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400992 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700993
994 spin_lock(&fc->lock);
995 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400996 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700997 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400998 ret = true;
999 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001001
1002 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001003}
1004
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001005static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001006 struct inode *inode, loff_t pos,
1007 size_t count)
1008{
1009 size_t res;
1010 unsigned offset;
1011 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001012 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001013
1014 for (i = 0; i < req->num_pages; i++)
1015 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1016
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001017 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001018
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001019 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001020 count = res;
1021 for (i = 0; i < req->num_pages; i++) {
1022 struct page *page = req->pages[i];
1023
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001024 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001025 SetPageUptodate(page);
1026
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 if (count > PAGE_SIZE - offset)
1028 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001029 else
1030 count = 0;
1031 offset = 0;
1032
1033 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001034 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001035 }
1036
1037 return res;
1038}
1039
1040static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1041 struct address_space *mapping,
1042 struct iov_iter *ii, loff_t pos)
1043{
1044 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001045 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001046 size_t count = 0;
1047 int err;
1048
Miklos Szeredif4975c62009-04-02 14:25:34 +02001049 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001050 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001051
1052 do {
1053 size_t tmp;
1054 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 pgoff_t index = pos >> PAGE_SHIFT;
1056 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001057 iov_iter_count(ii));
1058
1059 bytes = min_t(size_t, bytes, fc->max_write - count);
1060
1061 again:
1062 err = -EFAULT;
1063 if (iov_iter_fault_in_readable(ii, bytes))
1064 break;
1065
1066 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001067 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001068 if (!page)
1069 break;
1070
anfei zhou931e80e2010-02-02 13:44:02 -08001071 if (mapping_writably_mapped(mapping))
1072 flush_dcache_page(page);
1073
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001075 flush_dcache_page(page);
1076
Roman Gushchin3ca81382015-10-12 16:33:44 +03001077 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001078 if (!tmp) {
1079 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001080 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 bytes = min(bytes, iov_iter_single_seg_count(ii));
1082 goto again;
1083 }
1084
1085 err = 0;
1086 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001087 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001088 req->num_pages++;
1089
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 count += tmp;
1091 pos += tmp;
1092 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001093 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 offset = 0;
1095
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001096 if (!fc->big_writes)
1097 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001098 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001099 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001100
1101 return count > 0 ? count : err;
1102}
1103
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001104static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1105{
1106 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001107 ((pos + len - 1) >> PAGE_SHIFT) -
1108 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001109 FUSE_MAX_PAGES_PER_REQ);
1110}
1111
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001112static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001113 struct address_space *mapping,
1114 struct iov_iter *ii, loff_t pos)
1115{
1116 struct inode *inode = mapping->host;
1117 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001118 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001119 int err = 0;
1120 ssize_t res = 0;
1121
1122 if (is_bad_inode(inode))
1123 return -EIO;
1124
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001125 if (inode->i_size < pos + iov_iter_count(ii))
1126 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1127
Nick Pigginea9b9902008-04-30 00:54:42 -07001128 do {
1129 struct fuse_req *req;
1130 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001131 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001132
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001133 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001134 if (IS_ERR(req)) {
1135 err = PTR_ERR(req);
1136 break;
1137 }
1138
1139 count = fuse_fill_write_pages(req, mapping, ii, pos);
1140 if (count <= 0) {
1141 err = count;
1142 } else {
1143 size_t num_written;
1144
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001145 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 pos, count);
1147 err = req->out.h.error;
1148 if (!err) {
1149 res += num_written;
1150 pos += num_written;
1151
1152 /* break out of the loop on short write */
1153 if (num_written != count)
1154 err = -EIO;
1155 }
1156 }
1157 fuse_put_request(fc, req);
1158 } while (!err && iov_iter_count(ii));
1159
1160 if (res > 0)
1161 fuse_write_update_size(inode, pos);
1162
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001163 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001164 fuse_invalidate_attr(inode);
1165
1166 return res > 0 ? res : err;
1167}
1168
Al Viro84c3d552014-04-03 14:33:23 -04001169static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001170{
1171 struct file *file = iocb->ki_filp;
1172 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001173 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001174 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001175 struct inode *inode = mapping->host;
1176 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001177 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001178
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001179 if (get_fuse_conn(inode)->writeback_cache) {
1180 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001181 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001182 if (err)
1183 return err;
1184
Al Viro84c3d552014-04-03 14:33:23 -04001185 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001186 }
1187
Al Viro59551022016-01-22 15:40:57 -05001188 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001189
1190 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001191 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001192
Al Viro3309dd02015-04-09 12:55:47 -04001193 err = generic_write_checks(iocb, from);
1194 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 goto out;
1196
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001197 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001198 if (err)
1199 goto out;
1200
Josef Bacikc3b2da32012-03-26 09:59:21 -04001201 err = file_update_time(file);
1202 if (err)
1203 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001204
Al Viro2ba48ce2015-04-09 13:52:01 -04001205 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001206 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001207 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001208 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001209 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001210
Anand Avati4273b792012-02-17 12:46:25 -05001211 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001212
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001213 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001214 if (written_buffered < 0) {
1215 err = written_buffered;
1216 goto out;
1217 }
1218 endbyte = pos + written_buffered - 1;
1219
1220 err = filemap_write_and_wait_range(file->f_mapping, pos,
1221 endbyte);
1222 if (err)
1223 goto out;
1224
1225 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001226 pos >> PAGE_SHIFT,
1227 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001228
1229 written += written_buffered;
1230 iocb->ki_pos = pos + written_buffered;
1231 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001232 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001233 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001234 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001235 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001236out:
1237 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001238 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001239 if (written > 0)
1240 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001241
1242 return written ? written : err;
1243}
1244
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001245static inline void fuse_page_descs_length_init(struct fuse_req *req,
1246 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001247{
1248 int i;
1249
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001250 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001251 req->page_descs[i].length = PAGE_SIZE -
1252 req->page_descs[i].offset;
1253}
1254
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001255static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1256{
1257 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1258}
1259
1260static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1261 size_t max_size)
1262{
1263 return min(iov_iter_single_seg_count(ii), max_size);
1264}
1265
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001266static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001267 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001268{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001270 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001271
Miklos Szeredif4975c62009-04-02 14:25:34 +02001272 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001273 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001274 unsigned long user_addr = fuse_get_user_addr(ii);
1275 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1276
Miklos Szeredif4975c62009-04-02 14:25:34 +02001277 if (write)
1278 req->in.args[1].value = (void *) user_addr;
1279 else
1280 req->out.args[0].value = (void *) user_addr;
1281
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001282 iov_iter_advance(ii, frag_size);
1283 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001284 return 0;
1285 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001286
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001287 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001289 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001290 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001291 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001292 req->max_pages - req->num_pages,
1293 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001294 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001295 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001296
Al Viroc9c37e22014-03-16 16:08:30 -04001297 iov_iter_advance(ii, ret);
1298 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001299
Al Viroc9c37e22014-03-16 16:08:30 -04001300 ret += start;
1301 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1302
1303 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001304 fuse_page_descs_length_init(req, req->num_pages, npages);
1305
1306 req->num_pages += npages;
1307 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001308 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001310
1311 if (write)
1312 req->in.argpages = 1;
1313 else
1314 req->out.argpages = 1;
1315
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001316 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001317
Ashish Samant2c932d42016-03-25 10:53:41 -07001318 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001319}
1320
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001321static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1322{
Al Virof67da302014-03-19 01:16:16 -04001323 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001324}
1325
Al Virod22a9432014-03-16 15:50:47 -04001326ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1327 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001328{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001329 int write = flags & FUSE_DIO_WRITE;
1330 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001331 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001332 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001333 struct fuse_file *ff = file->private_data;
1334 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 size_t nmax = write ? fc->max_write : fc->max_read;
1336 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001337 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001338 pgoff_t idx_from = pos >> PAGE_SHIFT;
1339 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001340 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001341 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001342 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001343
Brian Fosterde82b922013-05-14 11:25:39 -04001344 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001345 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001346 else
Al Virod22a9432014-03-16 15:50:47 -04001347 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001348 if (IS_ERR(req))
1349 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001350
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001351 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1352 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001353 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001354 fuse_sync_writes(inode);
1355 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001356 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001357 }
1358
Ashish Samant61c12b42017-07-12 19:26:58 -07001359 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001362 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001363 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001364 err = fuse_get_user_pages(req, iter, &nbytes, write);
1365 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001366 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001367
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001369 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001371 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001372
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001373 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001374 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001376 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001377 break;
1378 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001379 res = 0;
1380 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001381 break;
1382 }
1383 count -= nres;
1384 res += nres;
1385 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001386 if (nres != nbytes)
1387 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001388 if (count) {
1389 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001390 if (io->async)
1391 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001392 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001393 else
Al Virod22a9432014-03-16 15:50:47 -04001394 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001395 if (IS_ERR(req))
1396 break;
1397 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001399 if (!IS_ERR(req))
1400 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001401 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001402 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001403
Ashish Samant742f9922016-03-14 21:57:35 -07001404 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405}
Tejun Heo08cbf542009-04-14 10:54:53 +09001406EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001408static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001409 struct iov_iter *iter,
1410 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001411{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001412 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001413 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001414
1415 if (is_bad_inode(inode))
1416 return -EIO;
1417
Al Virod22a9432014-03-16 15:50:47 -04001418 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001419
1420 fuse_invalidate_attr(inode);
1421
1422 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001423}
1424
Al Viro15316262015-03-30 22:08:36 -04001425static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001426{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001427 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001428 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001429}
1430
Al Viro15316262015-03-30 22:08:36 -04001431static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001432{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001433 struct inode *inode = file_inode(iocb->ki_filp);
1434 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001435 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001436
1437 if (is_bad_inode(inode))
1438 return -EIO;
1439
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001441 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001442 res = generic_write_checks(iocb, from);
1443 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001444 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001445 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001446 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001447 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001448 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001449
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001450 return res;
1451}
1452
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001453static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001454{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001455 int i;
1456
1457 for (i = 0; i < req->num_pages; i++)
1458 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001459
1460 if (req->ff)
1461 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001462}
1463
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001464static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001465{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001466 struct inode *inode = req->inode;
1467 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001468 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001469 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001470
1471 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001472 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001473 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001474 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001475 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001476 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001477 wake_up(&fi->page_waitq);
1478}
1479
1480/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001481static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1482 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001483__releases(fc->lock)
1484__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001485{
1486 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001487 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001488 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001489
1490 if (!fc->connected)
1491 goto out_free;
1492
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001493 if (inarg->offset + data_size <= size) {
1494 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001495 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001496 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497 } else {
1498 /* Got truncated off completely */
1499 goto out_free;
1500 }
1501
1502 req->in.args[1].size = inarg->size;
1503 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001504 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001505 return;
1506
1507 out_free:
1508 fuse_writepage_finish(fc, req);
1509 spin_unlock(&fc->lock);
1510 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001511 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512 spin_lock(&fc->lock);
1513}
1514
1515/*
1516 * If fi->writectr is positive (no truncate or fsync going on) send
1517 * all queued writepage requests.
1518 *
1519 * Called with fc->lock
1520 */
1521void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001522__releases(fc->lock)
1523__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001524{
1525 struct fuse_conn *fc = get_fuse_conn(inode);
1526 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001527 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001528 struct fuse_req *req;
1529
1530 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1531 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1532 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001533 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001534 }
1535}
1536
1537static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1538{
1539 struct inode *inode = req->inode;
1540 struct fuse_inode *fi = get_fuse_inode(inode);
1541
1542 mapping_set_error(inode->i_mapping, req->out.h.error);
1543 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001544 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001545 struct fuse_conn *fc = get_fuse_conn(inode);
1546 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001547 struct fuse_req *next = req->misc.write.next;
1548 req->misc.write.next = next->misc.write.next;
1549 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001550 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001551 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001552
1553 /*
1554 * Skip fuse_flush_writepages() to make it easy to crop requests
1555 * based on primary request size.
1556 *
1557 * 1st case (trivial): there are no concurrent activities using
1558 * fuse_set/release_nowrite. Then we're on safe side because
1559 * fuse_flush_writepages() would call fuse_send_writepage()
1560 * anyway.
1561 *
1562 * 2nd case: someone called fuse_set_nowrite and it is waiting
1563 * now for completion of all in-flight requests. This happens
1564 * rarely and no more than once per page, so this should be
1565 * okay.
1566 *
1567 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1568 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1569 * that fuse_set_nowrite returned implies that all in-flight
1570 * requests were completed along with all of their secondary
1571 * requests. Further primary requests are blocked by negative
1572 * writectr. Hence there cannot be any in-flight requests and
1573 * no invocations of fuse_writepage_end() while we're in
1574 * fuse_set_nowrite..fuse_release_nowrite section.
1575 */
1576 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001577 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001578 fi->writectr--;
1579 fuse_writepage_finish(fc, req);
1580 spin_unlock(&fc->lock);
1581 fuse_writepage_free(fc, req);
1582}
1583
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001584static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1585 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001586{
Miklos Szeredi72523422013-10-01 16:44:52 +02001587 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001588
1589 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001590 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001591 ff = list_entry(fi->write_files.next, struct fuse_file,
1592 write_entry);
1593 fuse_file_get(ff);
1594 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001595 spin_unlock(&fc->lock);
1596
1597 return ff;
1598}
1599
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001600static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1601 struct fuse_inode *fi)
1602{
1603 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1604 WARN_ON(!ff);
1605 return ff;
1606}
1607
1608int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1609{
1610 struct fuse_conn *fc = get_fuse_conn(inode);
1611 struct fuse_inode *fi = get_fuse_inode(inode);
1612 struct fuse_file *ff;
1613 int err;
1614
1615 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001616 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001617 if (ff)
1618 fuse_file_put(ff, 0);
1619
1620 return err;
1621}
1622
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001623static int fuse_writepage_locked(struct page *page)
1624{
1625 struct address_space *mapping = page->mapping;
1626 struct inode *inode = mapping->host;
1627 struct fuse_conn *fc = get_fuse_conn(inode);
1628 struct fuse_inode *fi = get_fuse_inode(inode);
1629 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001630 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001631 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001632
1633 set_page_writeback(page);
1634
Maxim Patlasov4250c062012-10-26 19:48:07 +04001635 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636 if (!req)
1637 goto err;
1638
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001639 /* writeback always goes to bg_queue */
1640 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001641 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1642 if (!tmp_page)
1643 goto err_free;
1644
Miklos Szeredi72523422013-10-01 16:44:52 +02001645 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001646 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001647 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001648 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001649
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001650 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001651
1652 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001653 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001654 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001655 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001656 req->num_pages = 1;
1657 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001658 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001659 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660 req->end = fuse_writepage_end;
1661 req->inode = inode;
1662
Tejun Heo93f78d82015-05-22 17:13:27 -04001663 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001664 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001665
1666 spin_lock(&fc->lock);
1667 list_add(&req->writepages_entry, &fi->writepages);
1668 list_add_tail(&req->list, &fi->queued_writes);
1669 fuse_flush_writepages(inode);
1670 spin_unlock(&fc->lock);
1671
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001672 end_page_writeback(page);
1673
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674 return 0;
1675
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001676err_nofile:
1677 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001678err_free:
1679 fuse_request_free(req);
1680err:
Jeff Layton91839762017-05-25 06:57:50 -04001681 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001682 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001683 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001684}
1685
1686static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1687{
1688 int err;
1689
Miklos Szerediff17be02013-10-01 16:44:53 +02001690 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1691 /*
1692 * ->writepages() should be called for sync() and friends. We
1693 * should only get here on direct reclaim and then we are
1694 * allowed to skip a page which is already in flight
1695 */
1696 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1697
1698 redirty_page_for_writepage(wbc, page);
1699 return 0;
1700 }
1701
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001702 err = fuse_writepage_locked(page);
1703 unlock_page(page);
1704
1705 return err;
1706}
1707
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001708struct fuse_fill_wb_data {
1709 struct fuse_req *req;
1710 struct fuse_file *ff;
1711 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001712 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001713};
1714
1715static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1716{
1717 struct fuse_req *req = data->req;
1718 struct inode *inode = data->inode;
1719 struct fuse_conn *fc = get_fuse_conn(inode);
1720 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001721 int num_pages = req->num_pages;
1722 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001723
1724 req->ff = fuse_file_get(data->ff);
1725 spin_lock(&fc->lock);
1726 list_add_tail(&req->list, &fi->queued_writes);
1727 fuse_flush_writepages(inode);
1728 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001729
1730 for (i = 0; i < num_pages; i++)
1731 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001732}
1733
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001734static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1735 struct page *page)
1736{
1737 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1738 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1739 struct fuse_req *tmp;
1740 struct fuse_req *old_req;
1741 bool found = false;
1742 pgoff_t curr_index;
1743
1744 BUG_ON(new_req->num_pages != 0);
1745
1746 spin_lock(&fc->lock);
1747 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001748 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1749 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001750 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001751 if (curr_index <= page->index &&
1752 page->index < curr_index + old_req->num_pages) {
1753 found = true;
1754 break;
1755 }
1756 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001757 if (!found) {
1758 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001759 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001760 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001761
Maxim Patlasovf6011082013-10-02 15:01:07 +04001762 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001763 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1764 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001765 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001766 if (tmp->num_pages == 1 &&
1767 curr_index == page->index) {
1768 old_req = tmp;
1769 }
1770 }
1771
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001772 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001773 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001774
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001775 copy_highpage(old_req->pages[0], page);
1776 spin_unlock(&fc->lock);
1777
Tejun Heo93f78d82015-05-22 17:13:27 -04001778 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001779 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001780 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001781 fuse_writepage_free(fc, new_req);
1782 fuse_request_free(new_req);
1783 goto out;
1784 } else {
1785 new_req->misc.write.next = old_req->misc.write.next;
1786 old_req->misc.write.next = new_req;
1787 }
1788out_unlock:
1789 spin_unlock(&fc->lock);
1790out:
1791 return found;
1792}
1793
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001794static int fuse_writepages_fill(struct page *page,
1795 struct writeback_control *wbc, void *_data)
1796{
1797 struct fuse_fill_wb_data *data = _data;
1798 struct fuse_req *req = data->req;
1799 struct inode *inode = data->inode;
1800 struct fuse_conn *fc = get_fuse_conn(inode);
1801 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001802 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001803 int err;
1804
1805 if (!data->ff) {
1806 err = -EIO;
1807 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1808 if (!data->ff)
1809 goto out_unlock;
1810 }
1811
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001812 /*
1813 * Being under writeback is unlikely but possible. For example direct
1814 * read to an mmaped fuse file will set the page dirty twice; once when
1815 * the pages are faulted with get_user_pages(), and then after the read
1816 * completed.
1817 */
1818 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001819
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001820 if (req && req->num_pages &&
1821 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001822 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001823 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1824 fuse_writepages_send(data);
1825 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001826 }
1827 err = -ENOMEM;
1828 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1829 if (!tmp_page)
1830 goto out_unlock;
1831
1832 /*
1833 * The page must not be redirtied until the writeout is completed
1834 * (i.e. userspace has sent a reply to the write request). Otherwise
1835 * there could be more than one temporary page instance for each real
1836 * page.
1837 *
1838 * This is ensured by holding the page lock in page_mkwrite() while
1839 * checking fuse_page_is_writeback(). We already hold the page lock
1840 * since clear_page_dirty_for_io() and keep it held until we add the
1841 * request to the fi->writepages list and increment req->num_pages.
1842 * After this fuse_page_is_writeback() will indicate that the page is
1843 * under writeback, so we can release the page lock.
1844 */
1845 if (data->req == NULL) {
1846 struct fuse_inode *fi = get_fuse_inode(inode);
1847
1848 err = -ENOMEM;
1849 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1850 if (!req) {
1851 __free_page(tmp_page);
1852 goto out_unlock;
1853 }
1854
1855 fuse_write_fill(req, data->ff, page_offset(page), 0);
1856 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001857 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001858 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001859 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001860 req->num_pages = 0;
1861 req->end = fuse_writepage_end;
1862 req->inode = inode;
1863
1864 spin_lock(&fc->lock);
1865 list_add(&req->writepages_entry, &fi->writepages);
1866 spin_unlock(&fc->lock);
1867
1868 data->req = req;
1869 }
1870 set_page_writeback(page);
1871
1872 copy_highpage(tmp_page, page);
1873 req->pages[req->num_pages] = tmp_page;
1874 req->page_descs[req->num_pages].offset = 0;
1875 req->page_descs[req->num_pages].length = PAGE_SIZE;
1876
Tejun Heo93f78d82015-05-22 17:13:27 -04001877 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001878 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001879
1880 err = 0;
1881 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1882 end_page_writeback(page);
1883 data->req = NULL;
1884 goto out_unlock;
1885 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001886 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001887
1888 /*
1889 * Protected by fc->lock against concurrent access by
1890 * fuse_page_is_writeback().
1891 */
1892 spin_lock(&fc->lock);
1893 req->num_pages++;
1894 spin_unlock(&fc->lock);
1895
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001896out_unlock:
1897 unlock_page(page);
1898
1899 return err;
1900}
1901
1902static int fuse_writepages(struct address_space *mapping,
1903 struct writeback_control *wbc)
1904{
1905 struct inode *inode = mapping->host;
1906 struct fuse_fill_wb_data data;
1907 int err;
1908
1909 err = -EIO;
1910 if (is_bad_inode(inode))
1911 goto out;
1912
1913 data.inode = inode;
1914 data.req = NULL;
1915 data.ff = NULL;
1916
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001917 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001918 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1919 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001920 GFP_NOFS);
1921 if (!data.orig_pages)
1922 goto out;
1923
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001924 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1925 if (data.req) {
1926 /* Ignore errors if we can write at least one page */
1927 BUG_ON(!data.req->num_pages);
1928 fuse_writepages_send(&data);
1929 err = 0;
1930 }
1931 if (data.ff)
1932 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001933
1934 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001935out:
1936 return err;
1937}
1938
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001939/*
1940 * It's worthy to make sure that space is reserved on disk for the write,
1941 * but how to implement it without killing performance need more thinking.
1942 */
1943static int fuse_write_begin(struct file *file, struct address_space *mapping,
1944 loff_t pos, unsigned len, unsigned flags,
1945 struct page **pagep, void **fsdata)
1946{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001947 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001948 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001949 struct page *page;
1950 loff_t fsize;
1951 int err = -ENOMEM;
1952
1953 WARN_ON(!fc->writeback_cache);
1954
1955 page = grab_cache_page_write_begin(mapping, index, flags);
1956 if (!page)
1957 goto error;
1958
1959 fuse_wait_on_page_writeback(mapping->host, page->index);
1960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001961 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001962 goto success;
1963 /*
1964 * Check if the start this page comes after the end of file, in which
1965 * case the readpage can be optimized away.
1966 */
1967 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001968 if (fsize <= (pos & PAGE_MASK)) {
1969 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001970 if (off)
1971 zero_user_segment(page, 0, off);
1972 goto success;
1973 }
1974 err = fuse_do_readpage(file, page);
1975 if (err)
1976 goto cleanup;
1977success:
1978 *pagep = page;
1979 return 0;
1980
1981cleanup:
1982 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001983 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001984error:
1985 return err;
1986}
1987
1988static int fuse_write_end(struct file *file, struct address_space *mapping,
1989 loff_t pos, unsigned len, unsigned copied,
1990 struct page *page, void *fsdata)
1991{
1992 struct inode *inode = page->mapping->host;
1993
Miklos Szeredi59c3b762016-08-18 09:10:44 +02001994 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1995 if (!copied)
1996 goto unlock;
1997
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001998 if (!PageUptodate(page)) {
1999 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002000 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002001 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002003 SetPageUptodate(page);
2004 }
2005
2006 fuse_write_update_size(inode, pos + copied);
2007 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002008
2009unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002010 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002011 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002012
2013 return copied;
2014}
2015
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002016static int fuse_launder_page(struct page *page)
2017{
2018 int err = 0;
2019 if (clear_page_dirty_for_io(page)) {
2020 struct inode *inode = page->mapping->host;
2021 err = fuse_writepage_locked(page);
2022 if (!err)
2023 fuse_wait_on_page_writeback(inode, page->index);
2024 }
2025 return err;
2026}
2027
2028/*
2029 * Write back dirty pages now, because there may not be any suitable
2030 * open files later
2031 */
2032static void fuse_vma_close(struct vm_area_struct *vma)
2033{
2034 filemap_write_and_wait(vma->vm_file->f_mapping);
2035}
2036
2037/*
2038 * Wait for writeback against this page to complete before allowing it
2039 * to be marked dirty again, and hence written back again, possibly
2040 * before the previous writepage completed.
2041 *
2042 * Block here, instead of in ->writepage(), so that the userspace fs
2043 * can only block processes actually operating on the filesystem.
2044 *
2045 * Otherwise unprivileged userspace fs would be able to block
2046 * unrelated:
2047 *
2048 * - page migration
2049 * - sync(2)
2050 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2051 */
Dave Jiang11bac802017-02-24 14:56:41 -08002052static int fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002053{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002054 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002055 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002056
Dave Jiang11bac802017-02-24 14:56:41 -08002057 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002058 lock_page(page);
2059 if (page->mapping != inode->i_mapping) {
2060 unlock_page(page);
2061 return VM_FAULT_NOPAGE;
2062 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002063
2064 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002065 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002066}
2067
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002068static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002069 .close = fuse_vma_close,
2070 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002071 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002072 .page_mkwrite = fuse_page_mkwrite,
2073};
2074
2075static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2076{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002077 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2078 fuse_link_write_file(file);
2079
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002080 file_accessed(file);
2081 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002082 return 0;
2083}
2084
Miklos Szeredifc280c92009-04-02 14:25:35 +02002085static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2086{
2087 /* Can't provide the coherency needed for MAP_SHARED */
2088 if (vma->vm_flags & VM_MAYSHARE)
2089 return -ENODEV;
2090
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002091 invalidate_inode_pages2(file->f_mapping);
2092
Miklos Szeredifc280c92009-04-02 14:25:35 +02002093 return generic_file_mmap(file, vma);
2094}
2095
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002096static int convert_fuse_file_lock(struct fuse_conn *fc,
2097 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002098 struct file_lock *fl)
2099{
2100 switch (ffl->type) {
2101 case F_UNLCK:
2102 break;
2103
2104 case F_RDLCK:
2105 case F_WRLCK:
2106 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2107 ffl->end < ffl->start)
2108 return -EIO;
2109
2110 fl->fl_start = ffl->start;
2111 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002112
2113 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002114 * Convert pid into init's pid namespace. The locks API will
2115 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002116 */
2117 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002118 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002119 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002120 break;
2121
2122 default:
2123 return -EIO;
2124 }
2125 fl->fl_type = ffl->type;
2126 return 0;
2127}
2128
Miklos Szeredi70781872014-12-12 09:49:05 +01002129static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002130 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002131 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002132{
Al Viro6131ffa2013-02-27 16:59:05 -05002133 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002134 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002135 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002136
Miklos Szeredi70781872014-12-12 09:49:05 +01002137 memset(inarg, 0, sizeof(*inarg));
2138 inarg->fh = ff->fh;
2139 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2140 inarg->lk.start = fl->fl_start;
2141 inarg->lk.end = fl->fl_end;
2142 inarg->lk.type = fl->fl_type;
2143 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002144 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002145 inarg->lk_flags |= FUSE_LK_FLOCK;
2146 args->in.h.opcode = opcode;
2147 args->in.h.nodeid = get_node_id(inode);
2148 args->in.numargs = 1;
2149 args->in.args[0].size = sizeof(*inarg);
2150 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002151}
2152
2153static int fuse_getlk(struct file *file, struct file_lock *fl)
2154{
Al Viro6131ffa2013-02-27 16:59:05 -05002155 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002156 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002157 FUSE_ARGS(args);
2158 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002159 struct fuse_lk_out outarg;
2160 int err;
2161
Miklos Szeredi70781872014-12-12 09:49:05 +01002162 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2163 args.out.numargs = 1;
2164 args.out.args[0].size = sizeof(outarg);
2165 args.out.args[0].value = &outarg;
2166 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002167 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002168 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002169
2170 return err;
2171}
2172
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002173static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002174{
Al Viro6131ffa2013-02-27 16:59:05 -05002175 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002176 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002177 FUSE_ARGS(args);
2178 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002179 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002180 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2181 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002182 int err;
2183
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002184 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002185 /* NLM needs asynchronous locks, which we don't support yet */
2186 return -ENOLCK;
2187 }
2188
Miklos Szeredi71421252006-06-25 05:48:52 -07002189 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002190 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 return 0;
2192
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002193 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002194 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002195
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002196 /* locking is restartable */
2197 if (err == -EINTR)
2198 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002199
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 return err;
2201}
2202
2203static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2204{
Al Viro6131ffa2013-02-27 16:59:05 -05002205 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002206 struct fuse_conn *fc = get_fuse_conn(inode);
2207 int err;
2208
Miklos Szeredi48e90762008-07-25 01:49:02 -07002209 if (cmd == F_CANCELLK) {
2210 err = 0;
2211 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002213 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002214 err = 0;
2215 } else
2216 err = fuse_getlk(file, fl);
2217 } else {
2218 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002219 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002220 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002221 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002222 }
2223 return err;
2224}
2225
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002226static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2227{
Al Viro6131ffa2013-02-27 16:59:05 -05002228 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002229 struct fuse_conn *fc = get_fuse_conn(inode);
2230 int err;
2231
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002232 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002233 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002234 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002235 struct fuse_file *ff = file->private_data;
2236
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002237 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002238 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002239 err = fuse_setlk(file, fl, 1);
2240 }
2241
2242 return err;
2243}
2244
Miklos Szeredib2d22722006-12-06 20:35:51 -08002245static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2246{
2247 struct inode *inode = mapping->host;
2248 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002249 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002250 struct fuse_bmap_in inarg;
2251 struct fuse_bmap_out outarg;
2252 int err;
2253
2254 if (!inode->i_sb->s_bdev || fc->no_bmap)
2255 return 0;
2256
Miklos Szeredib2d22722006-12-06 20:35:51 -08002257 memset(&inarg, 0, sizeof(inarg));
2258 inarg.block = block;
2259 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002260 args.in.h.opcode = FUSE_BMAP;
2261 args.in.h.nodeid = get_node_id(inode);
2262 args.in.numargs = 1;
2263 args.in.args[0].size = sizeof(inarg);
2264 args.in.args[0].value = &inarg;
2265 args.out.numargs = 1;
2266 args.out.args[0].size = sizeof(outarg);
2267 args.out.args[0].value = &outarg;
2268 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002269 if (err == -ENOSYS)
2270 fc->no_bmap = 1;
2271
2272 return err ? 0 : outarg.block;
2273}
2274
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302275static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2276{
2277 struct inode *inode = file->f_mapping->host;
2278 struct fuse_conn *fc = get_fuse_conn(inode);
2279 struct fuse_file *ff = file->private_data;
2280 FUSE_ARGS(args);
2281 struct fuse_lseek_in inarg = {
2282 .fh = ff->fh,
2283 .offset = offset,
2284 .whence = whence
2285 };
2286 struct fuse_lseek_out outarg;
2287 int err;
2288
2289 if (fc->no_lseek)
2290 goto fallback;
2291
2292 args.in.h.opcode = FUSE_LSEEK;
2293 args.in.h.nodeid = ff->nodeid;
2294 args.in.numargs = 1;
2295 args.in.args[0].size = sizeof(inarg);
2296 args.in.args[0].value = &inarg;
2297 args.out.numargs = 1;
2298 args.out.args[0].size = sizeof(outarg);
2299 args.out.args[0].value = &outarg;
2300 err = fuse_simple_request(fc, &args);
2301 if (err) {
2302 if (err == -ENOSYS) {
2303 fc->no_lseek = 1;
2304 goto fallback;
2305 }
2306 return err;
2307 }
2308
2309 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2310
2311fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002312 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302313 if (!err)
2314 return generic_file_llseek(file, offset, whence);
2315 else
2316 return err;
2317}
2318
Andrew Morton965c8e52012-12-17 15:59:39 -08002319static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002320{
2321 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002322 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002323
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302324 switch (whence) {
2325 case SEEK_SET:
2326 case SEEK_CUR:
2327 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002328 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302329 break;
2330 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002331 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002332 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302333 if (!retval)
2334 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002335 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302336 break;
2337 case SEEK_HOLE:
2338 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002339 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302340 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002341 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302342 break;
2343 default:
2344 retval = -EINVAL;
2345 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002346
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002347 return retval;
2348}
2349
Tejun Heo59efec72008-11-26 12:03:55 +01002350/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002351 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2352 * ABI was defined to be 'struct iovec' which is different on 32bit
2353 * and 64bit. Fortunately we can determine which structure the server
2354 * used from the size of the reply.
2355 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002356static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2357 size_t transferred, unsigned count,
2358 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002359{
2360#ifdef CONFIG_COMPAT
2361 if (count * sizeof(struct compat_iovec) == transferred) {
2362 struct compat_iovec *ciov = src;
2363 unsigned i;
2364
2365 /*
2366 * With this interface a 32bit server cannot support
2367 * non-compat (i.e. ones coming from 64bit apps) ioctl
2368 * requests
2369 */
2370 if (!is_compat)
2371 return -EINVAL;
2372
2373 for (i = 0; i < count; i++) {
2374 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2375 dst[i].iov_len = ciov[i].iov_len;
2376 }
2377 return 0;
2378 }
2379#endif
2380
2381 if (count * sizeof(struct iovec) != transferred)
2382 return -EIO;
2383
2384 memcpy(dst, src, transferred);
2385 return 0;
2386}
2387
Miklos Szeredi75727772010-11-30 16:39:27 +01002388/* Make sure iov_length() won't overflow */
2389static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2390{
2391 size_t n;
2392 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2393
Zach Brownfb6ccff2012-07-24 12:10:11 -07002394 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002395 if (iov->iov_len > (size_t) max)
2396 return -ENOMEM;
2397 max -= iov->iov_len;
2398 }
2399 return 0;
2400}
2401
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002402static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2403 void *src, size_t transferred, unsigned count,
2404 bool is_compat)
2405{
2406 unsigned i;
2407 struct fuse_ioctl_iovec *fiov = src;
2408
2409 if (fc->minor < 16) {
2410 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2411 count, is_compat);
2412 }
2413
2414 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2415 return -EIO;
2416
2417 for (i = 0; i < count; i++) {
2418 /* Did the server supply an inappropriate value? */
2419 if (fiov[i].base != (unsigned long) fiov[i].base ||
2420 fiov[i].len != (unsigned long) fiov[i].len)
2421 return -EIO;
2422
2423 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2424 dst[i].iov_len = (size_t) fiov[i].len;
2425
2426#ifdef CONFIG_COMPAT
2427 if (is_compat &&
2428 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2429 (compat_size_t) dst[i].iov_len != fiov[i].len))
2430 return -EIO;
2431#endif
2432 }
2433
2434 return 0;
2435}
2436
2437
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002438/*
Tejun Heo59efec72008-11-26 12:03:55 +01002439 * For ioctls, there is no generic way to determine how much memory
2440 * needs to be read and/or written. Furthermore, ioctls are allowed
2441 * to dereference the passed pointer, so the parameter requires deep
2442 * copying but FUSE has no idea whatsoever about what to copy in or
2443 * out.
2444 *
2445 * This is solved by allowing FUSE server to retry ioctl with
2446 * necessary in/out iovecs. Let's assume the ioctl implementation
2447 * needs to read in the following structure.
2448 *
2449 * struct a {
2450 * char *buf;
2451 * size_t buflen;
2452 * }
2453 *
2454 * On the first callout to FUSE server, inarg->in_size and
2455 * inarg->out_size will be NULL; then, the server completes the ioctl
2456 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2457 * the actual iov array to
2458 *
2459 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2460 *
2461 * which tells FUSE to copy in the requested area and retry the ioctl.
2462 * On the second round, the server has access to the structure and
2463 * from that it can tell what to look for next, so on the invocation,
2464 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2465 *
2466 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2467 * { .iov_base = a.buf, .iov_len = a.buflen } }
2468 *
2469 * FUSE will copy both struct a and the pointed buffer from the
2470 * process doing the ioctl and retry ioctl with both struct a and the
2471 * buffer.
2472 *
2473 * This time, FUSE server has everything it needs and completes ioctl
2474 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2475 *
2476 * Copying data out works the same way.
2477 *
2478 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2479 * automatically initializes in and out iovs by decoding @cmd with
2480 * _IOC_* macros and the server is not allowed to request RETRY. This
2481 * limits ioctl data transfers to well-formed ioctls and is the forced
2482 * behavior for all FUSE servers.
2483 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002484long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2485 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002486{
Tejun Heo59efec72008-11-26 12:03:55 +01002487 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002488 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002489 struct fuse_ioctl_in inarg = {
2490 .fh = ff->fh,
2491 .cmd = cmd,
2492 .arg = arg,
2493 .flags = flags
2494 };
2495 struct fuse_ioctl_out outarg;
2496 struct fuse_req *req = NULL;
2497 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002498 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002499 struct iovec *in_iov = NULL, *out_iov = NULL;
2500 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002501 size_t in_size, out_size, transferred, c;
2502 int err, i;
2503 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002504
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002505#if BITS_PER_LONG == 32
2506 inarg.flags |= FUSE_IOCTL_32BIT;
2507#else
2508 if (flags & FUSE_IOCTL_COMPAT)
2509 inarg.flags |= FUSE_IOCTL_32BIT;
2510#endif
2511
Tejun Heo59efec72008-11-26 12:03:55 +01002512 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002513 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002514
Tejun Heo59efec72008-11-26 12:03:55 +01002515 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002516 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002517 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002518 if (!pages || !iov_page)
2519 goto out;
2520
2521 /*
2522 * If restricted, initialize IO parameters as encoded in @cmd.
2523 * RETRY from server is not allowed.
2524 */
2525 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002526 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002527
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002528 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002529 iov->iov_len = _IOC_SIZE(cmd);
2530
2531 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2532 in_iov = iov;
2533 in_iovs = 1;
2534 }
2535
2536 if (_IOC_DIR(cmd) & _IOC_READ) {
2537 out_iov = iov;
2538 out_iovs = 1;
2539 }
2540 }
2541
2542 retry:
2543 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2544 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2545
2546 /*
2547 * Out data can be used either for actual out data or iovs,
2548 * make sure there always is at least one page.
2549 */
2550 out_size = max_t(size_t, out_size, PAGE_SIZE);
2551 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2552
2553 /* make sure there are enough buffer pages and init request with them */
2554 err = -ENOMEM;
2555 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2556 goto out;
2557 while (num_pages < max_pages) {
2558 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2559 if (!pages[num_pages])
2560 goto out;
2561 num_pages++;
2562 }
2563
Maxim Patlasov54b96672012-10-26 19:49:13 +04002564 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002565 if (IS_ERR(req)) {
2566 err = PTR_ERR(req);
2567 req = NULL;
2568 goto out;
2569 }
2570 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2571 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002572 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002573
2574 /* okay, let's send it to the client */
2575 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002576 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002577 req->in.numargs = 1;
2578 req->in.args[0].size = sizeof(inarg);
2579 req->in.args[0].value = &inarg;
2580 if (in_size) {
2581 req->in.numargs++;
2582 req->in.args[1].size = in_size;
2583 req->in.argpages = 1;
2584
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002585 err = -EFAULT;
2586 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2587 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2588 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2589 if (c != PAGE_SIZE && iov_iter_count(&ii))
2590 goto out;
2591 }
Tejun Heo59efec72008-11-26 12:03:55 +01002592 }
2593
2594 req->out.numargs = 2;
2595 req->out.args[0].size = sizeof(outarg);
2596 req->out.args[0].value = &outarg;
2597 req->out.args[1].size = out_size;
2598 req->out.argpages = 1;
2599 req->out.argvar = 1;
2600
Tejun Heob93f8582008-11-26 12:03:55 +01002601 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002602 err = req->out.h.error;
2603 transferred = req->out.args[1].size;
2604 fuse_put_request(fc, req);
2605 req = NULL;
2606 if (err)
2607 goto out;
2608
2609 /* did it ask for retry? */
2610 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002611 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002612
2613 /* no retry if in restricted mode */
2614 err = -EIO;
2615 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2616 goto out;
2617
2618 in_iovs = outarg.in_iovs;
2619 out_iovs = outarg.out_iovs;
2620
2621 /*
2622 * Make sure things are in boundary, separate checks
2623 * are to protect against overflow.
2624 */
2625 err = -ENOMEM;
2626 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2627 out_iovs > FUSE_IOCTL_MAX_IOV ||
2628 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2629 goto out;
2630
Cong Wang2408f6e2011-11-25 23:14:30 +08002631 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002632 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002633 transferred, in_iovs + out_iovs,
2634 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002635 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002636 if (err)
2637 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002638
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002639 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002640 out_iov = in_iov + in_iovs;
2641
Miklos Szeredi75727772010-11-30 16:39:27 +01002642 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2643 if (err)
2644 goto out;
2645
2646 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2647 if (err)
2648 goto out;
2649
Tejun Heo59efec72008-11-26 12:03:55 +01002650 goto retry;
2651 }
2652
2653 err = -EIO;
2654 if (transferred > inarg.out_size)
2655 goto out;
2656
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002657 err = -EFAULT;
2658 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2659 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2660 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2661 if (c != PAGE_SIZE && iov_iter_count(&ii))
2662 goto out;
2663 }
2664 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002665 out:
2666 if (req)
2667 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002668 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002669 while (num_pages)
2670 __free_page(pages[--num_pages]);
2671 kfree(pages);
2672
2673 return err ? err : outarg.result;
2674}
Tejun Heo08cbf542009-04-14 10:54:53 +09002675EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002676
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002677long fuse_ioctl_common(struct file *file, unsigned int cmd,
2678 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002679{
Al Viro6131ffa2013-02-27 16:59:05 -05002680 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002681 struct fuse_conn *fc = get_fuse_conn(inode);
2682
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002683 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002684 return -EACCES;
2685
2686 if (is_bad_inode(inode))
2687 return -EIO;
2688
2689 return fuse_do_ioctl(file, cmd, arg, flags);
2690}
2691
Tejun Heo59efec72008-11-26 12:03:55 +01002692static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2693 unsigned long arg)
2694{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002695 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002696}
2697
2698static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2699 unsigned long arg)
2700{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002701 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002702}
2703
Tejun Heo95668a62008-11-26 12:03:55 +01002704/*
2705 * All files which have been polled are linked to RB tree
2706 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2707 * find the matching one.
2708 */
2709static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2710 struct rb_node **parent_out)
2711{
2712 struct rb_node **link = &fc->polled_files.rb_node;
2713 struct rb_node *last = NULL;
2714
2715 while (*link) {
2716 struct fuse_file *ff;
2717
2718 last = *link;
2719 ff = rb_entry(last, struct fuse_file, polled_node);
2720
2721 if (kh < ff->kh)
2722 link = &last->rb_left;
2723 else if (kh > ff->kh)
2724 link = &last->rb_right;
2725 else
2726 return link;
2727 }
2728
2729 if (parent_out)
2730 *parent_out = last;
2731 return link;
2732}
2733
2734/*
2735 * The file is about to be polled. Make sure it's on the polled_files
2736 * RB tree. Note that files once added to the polled_files tree are
2737 * not removed before the file is released. This is because a file
2738 * polled once is likely to be polled again.
2739 */
2740static void fuse_register_polled_file(struct fuse_conn *fc,
2741 struct fuse_file *ff)
2742{
2743 spin_lock(&fc->lock);
2744 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002745 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002746
2747 link = fuse_find_polled_node(fc, ff->kh, &parent);
2748 BUG_ON(*link);
2749 rb_link_node(&ff->polled_node, parent, link);
2750 rb_insert_color(&ff->polled_node, &fc->polled_files);
2751 }
2752 spin_unlock(&fc->lock);
2753}
2754
Al Viro076ccb72017-07-03 01:02:18 -04002755__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002756{
Tejun Heo95668a62008-11-26 12:03:55 +01002757 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002758 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002759 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2760 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002761 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002762 int err;
2763
2764 if (fc->no_poll)
2765 return DEFAULT_POLLMASK;
2766
2767 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002768 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002769
2770 /*
2771 * Ask for notification iff there's someone waiting for it.
2772 * The client may ignore the flag and always notify.
2773 */
2774 if (waitqueue_active(&ff->poll_wait)) {
2775 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2776 fuse_register_polled_file(fc, ff);
2777 }
2778
Miklos Szeredi70781872014-12-12 09:49:05 +01002779 args.in.h.opcode = FUSE_POLL;
2780 args.in.h.nodeid = ff->nodeid;
2781 args.in.numargs = 1;
2782 args.in.args[0].size = sizeof(inarg);
2783 args.in.args[0].value = &inarg;
2784 args.out.numargs = 1;
2785 args.out.args[0].size = sizeof(outarg);
2786 args.out.args[0].value = &outarg;
2787 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002788
2789 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002790 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002791 if (err == -ENOSYS) {
2792 fc->no_poll = 1;
2793 return DEFAULT_POLLMASK;
2794 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002795 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002796}
Tejun Heo08cbf542009-04-14 10:54:53 +09002797EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002798
2799/*
2800 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2801 * wakes up the poll waiters.
2802 */
2803int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2804 struct fuse_notify_poll_wakeup_out *outarg)
2805{
2806 u64 kh = outarg->kh;
2807 struct rb_node **link;
2808
2809 spin_lock(&fc->lock);
2810
2811 link = fuse_find_polled_node(fc, kh, NULL);
2812 if (*link) {
2813 struct fuse_file *ff;
2814
2815 ff = rb_entry(*link, struct fuse_file, polled_node);
2816 wake_up_interruptible_sync(&ff->poll_wait);
2817 }
2818
2819 spin_unlock(&fc->lock);
2820 return 0;
2821}
2822
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002823static void fuse_do_truncate(struct file *file)
2824{
2825 struct inode *inode = file->f_mapping->host;
2826 struct iattr attr;
2827
2828 attr.ia_valid = ATTR_SIZE;
2829 attr.ia_size = i_size_read(inode);
2830
2831 attr.ia_file = file;
2832 attr.ia_valid |= ATTR_FILE;
2833
Jan Kara62490332016-05-26 17:12:41 +02002834 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002835}
2836
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002837static inline loff_t fuse_round_up(loff_t off)
2838{
2839 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2840}
2841
Anand Avati4273b792012-02-17 12:46:25 -05002842static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002843fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002844{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002845 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002846 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002847 struct file *file = iocb->ki_filp;
2848 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002849 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002850 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002851 struct inode *inode;
2852 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002853 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002854 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002855 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002856
Anand Avati4273b792012-02-17 12:46:25 -05002857 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002858 inode = file->f_mapping->host;
2859 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002860
Omar Sandoval6f673762015-03-16 04:33:52 -07002861 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002862 return 0;
2863
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002864 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002865 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002866 if (offset >= i_size)
2867 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002868 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2869 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002870 }
2871
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002872 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002873 if (!io)
2874 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002875 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002876 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002877 io->reqs = 1;
2878 io->bytes = -1;
2879 io->size = 0;
2880 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002881 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002882 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002883 /*
2884 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002885 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002886 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002887 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002888 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302889 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890
2891 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302892 * We cannot asynchronously extend the size of a file.
2893 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302895 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2896 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002897
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302898 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002899 /*
2900 * Additional reference to keep io around after
2901 * calling fuse_aio_complete()
2902 */
2903 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002904 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002905 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002906
Omar Sandoval6f673762015-03-16 04:33:52 -07002907 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002908 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002909 fuse_invalidate_attr(inode);
2910 } else {
Al Virod22a9432014-03-16 15:50:47 -04002911 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002912 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002913
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002914 if (io->async) {
2915 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2916
2917 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302918 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002919 return -EIOCBQUEUED;
2920
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002921 wait_for_completion(&wait);
2922 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002923 }
2924
Seth Forshee744742d2016-03-11 10:35:34 -06002925 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002926
Omar Sandoval6f673762015-03-16 04:33:52 -07002927 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002928 if (ret > 0)
2929 fuse_write_update_size(inode, pos);
2930 else if (ret < 0 && offset + count > i_size)
2931 fuse_do_truncate(file);
2932 }
Anand Avati4273b792012-02-17 12:46:25 -05002933
2934 return ret;
2935}
2936
Miklos Szeredicdadb112012-11-10 16:55:56 +01002937static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2938 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002939{
2940 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002941 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002942 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002943 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002944 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002945 struct fuse_fallocate_in inarg = {
2946 .fh = ff->fh,
2947 .offset = offset,
2948 .length = length,
2949 .mode = mode
2950 };
2951 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002952 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2953 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002954
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002955 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2956 return -EOPNOTSUPP;
2957
Miklos Szeredi519c6042012-04-26 10:56:36 +02002958 if (fc->no_fallocate)
2959 return -EOPNOTSUPP;
2960
Maxim Patlasov14c14412013-06-13 12:16:39 +04002961 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002962 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002963 if (mode & FALLOC_FL_PUNCH_HOLE) {
2964 loff_t endbyte = offset + length - 1;
2965 err = filemap_write_and_wait_range(inode->i_mapping,
2966 offset, endbyte);
2967 if (err)
2968 goto out;
2969
2970 fuse_sync_writes(inode);
2971 }
Brian Foster3634a632013-05-17 09:30:32 -04002972 }
2973
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002974 if (!(mode & FALLOC_FL_KEEP_SIZE))
2975 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2976
Miklos Szeredi70781872014-12-12 09:49:05 +01002977 args.in.h.opcode = FUSE_FALLOCATE;
2978 args.in.h.nodeid = ff->nodeid;
2979 args.in.numargs = 1;
2980 args.in.args[0].size = sizeof(inarg);
2981 args.in.args[0].value = &inarg;
2982 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002983 if (err == -ENOSYS) {
2984 fc->no_fallocate = 1;
2985 err = -EOPNOTSUPP;
2986 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002987 if (err)
2988 goto out;
2989
2990 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002991 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2992 bool changed = fuse_write_update_size(inode, offset + length);
2993
Miklos Szeredi93d22692014-04-28 14:19:22 +02002994 if (changed && fc->writeback_cache)
2995 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002996 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002997
2998 if (mode & FALLOC_FL_PUNCH_HOLE)
2999 truncate_pagecache_range(inode, offset, offset + length - 1);
3000
3001 fuse_invalidate_attr(inode);
3002
Brian Foster3634a632013-05-17 09:30:32 -04003003out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003004 if (!(mode & FALLOC_FL_KEEP_SIZE))
3005 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3006
Maxim Patlasovbde52782013-09-13 19:19:54 +04003007 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003008 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003009
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003010 return err;
3011}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003012
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003013static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003014 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003015 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003016 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003017 .mmap = fuse_file_mmap,
3018 .open = fuse_open,
3019 .flush = fuse_flush,
3020 .release = fuse_release,
3021 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003022 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003023 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003024 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003025 .unlocked_ioctl = fuse_file_ioctl,
3026 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003027 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003028 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003029};
3030
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003031static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003032 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003033 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003034 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003035 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003036 .open = fuse_open,
3037 .flush = fuse_flush,
3038 .release = fuse_release,
3039 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003040 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003041 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003042 .unlocked_ioctl = fuse_file_ioctl,
3043 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003044 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003045 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003046 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003047};
3048
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003049static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003050 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003051 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003052 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003053 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003054 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003055 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003056 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003057 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003058 .write_begin = fuse_write_begin,
3059 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003060};
3061
3062void fuse_init_file_inode(struct inode *inode)
3063{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003064 inode->i_fop = &fuse_file_operations;
3065 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003066}