blob: fbd6978479cb656f8c6bc9f228aca74a8690bf7d [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
Chad Austind9267e12018-12-10 10:54:52 -080090static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
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
Chad Austind9267e12018-12-10 10:54:52 -080095 if (ff->fc->no_open && !isdir) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +010096 /*
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
Chad Austind9267e12018-12-10 10:54:52 -0800248void fuse_release_common(struct file *file, bool isdir)
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;
Chad Austind9267e12018-12-10 10:54:52 -0800252 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700253
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200254 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100255
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200256 if (ff->flock) {
257 struct fuse_release_in *inarg = &req->misc.release.in;
258 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
259 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
260 (fl_owner_t) file);
261 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100262 /* Hold inode until release is finished */
263 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700264
Tejun Heo6b2db282009-04-14 10:54:49 +0900265 /*
266 * Normally this will send the RELEASE request, however if
267 * some asynchronous READ or WRITE requests are outstanding,
268 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100269 *
270 * Make the release synchronous if this is a fuseblk mount,
271 * synchronous RELEASE is allowed (and desirable) in this case
272 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900273 */
Chad Austind9267e12018-12-10 10:54:52 -0800274 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700275}
276
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700277static int fuse_open(struct inode *inode, struct file *file)
278{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200279 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700280}
281
282static int fuse_release(struct inode *inode, struct file *file)
283{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400284 struct fuse_conn *fc = get_fuse_conn(inode);
285
286 /* see fuse_vma_close() for !writeback_cache case */
287 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200288 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400289
Chad Austind9267e12018-12-10 10:54:52 -0800290 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200291
292 /* return value is ignored by VFS */
293 return 0;
294}
295
296void fuse_sync_release(struct fuse_file *ff, int flags)
297{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200298 WARN_ON(refcount_read(&ff->count) > 1);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200299 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100300 /*
301 * iput(NULL) is a no-op and since the refcount is 1 and everything's
302 * synchronous, we are fine with not doing igrab() here"
303 */
Chad Austind9267e12018-12-10 10:54:52 -0800304 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700305}
Tejun Heo08cbf542009-04-14 10:54:53 +0900306EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307
Miklos Szeredi71421252006-06-25 05:48:52 -0700308/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700309 * Scramble the ID space with XTEA, so that the value of the files_struct
310 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700311 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700312u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700313{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700314 u32 *k = fc->scramble_key;
315 u64 v = (unsigned long) id;
316 u32 v0 = v;
317 u32 v1 = v >> 32;
318 u32 sum = 0;
319 int i;
320
321 for (i = 0; i < 32; i++) {
322 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
323 sum += 0x9E3779B9;
324 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
325 }
326
327 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700328}
329
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700330/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400331 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700332 *
333 * This is currently done by walking the list of writepage requests
334 * for the inode, which can be pretty inefficient.
335 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400336static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
337 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700338{
339 struct fuse_conn *fc = get_fuse_conn(inode);
340 struct fuse_inode *fi = get_fuse_inode(inode);
341 struct fuse_req *req;
342 bool found = false;
343
344 spin_lock(&fc->lock);
345 list_for_each_entry(req, &fi->writepages, writepages_entry) {
346 pgoff_t curr_index;
347
348 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300349 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400350 if (idx_from < curr_index + req->num_pages &&
351 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700352 found = true;
353 break;
354 }
355 }
356 spin_unlock(&fc->lock);
357
358 return found;
359}
360
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400361static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
362{
363 return fuse_range_is_writeback(inode, index, index);
364}
365
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700366/*
367 * Wait for page writeback to be completed.
368 *
369 * Since fuse doesn't rely on the VM writeback tracking, this has to
370 * use some other means.
371 */
372static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
373{
374 struct fuse_inode *fi = get_fuse_inode(inode);
375
376 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
377 return 0;
378}
379
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400380/*
381 * Wait for all pending writepages on the inode to finish.
382 *
383 * This is currently done by blocking further writes with FUSE_NOWRITE
384 * and waiting for all sent writes to complete.
385 *
386 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
387 * could conflict with truncation.
388 */
389static void fuse_sync_writes(struct inode *inode)
390{
391 fuse_set_nowrite(inode);
392 fuse_release_nowrite(inode);
393}
394
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700395static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700396{
Al Viro6131ffa2013-02-27 16:59:05 -0500397 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700398 struct fuse_conn *fc = get_fuse_conn(inode);
399 struct fuse_file *ff = file->private_data;
400 struct fuse_req *req;
401 struct fuse_flush_in inarg;
402 int err;
403
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800404 if (is_bad_inode(inode))
405 return -EIO;
406
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700407 if (fc->no_flush)
408 return 0;
409
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200410 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400411 if (err)
412 return err;
413
Al Viro59551022016-01-22 15:40:57 -0500414 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400415 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500416 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400417
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200418 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700419 if (err)
420 return err;
421
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400422 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 memset(&inarg, 0, sizeof(inarg));
424 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700425 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426 req->in.h.opcode = FUSE_FLUSH;
427 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700428 req->in.numargs = 1;
429 req->in.args[0].size = sizeof(inarg);
430 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200431 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100432 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700433 err = req->out.h.error;
434 fuse_put_request(fc, req);
435 if (err == -ENOSYS) {
436 fc->no_flush = 1;
437 err = 0;
438 }
439 return err;
440}
441
Josef Bacik02c24a82011-07-16 20:44:56 -0400442int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
443 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200445 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446 struct fuse_conn *fc = get_fuse_conn(inode);
447 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100448 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 struct fuse_fsync_in inarg;
450 int err;
451
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800452 if (is_bad_inode(inode))
453 return -EIO;
454
Al Viro59551022016-01-22 15:40:57 -0500455 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400456
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700457 /*
458 * Start writeback against all dirty pages of the inode, then
459 * wait for all outstanding writes, before sending the FSYNC
460 * request.
461 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400462 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700463 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400464 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700465
466 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700467
468 /*
469 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400470 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700471 * We have to do this directly after fuse_sync_writes()
472 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400473 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700474 if (err)
475 goto out;
476
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200477 err = sync_inode_metadata(inode, 1);
478 if (err)
479 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700480
Miklos Szeredi22401e72014-04-28 14:19:23 +0200481 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
482 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400483
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700484 memset(&inarg, 0, sizeof(inarg));
485 inarg.fh = ff->fh;
486 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100487 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
488 args.in.h.nodeid = get_node_id(inode);
489 args.in.numargs = 1;
490 args.in.args[0].size = sizeof(inarg);
491 args.in.args[0].value = &inarg;
492 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700494 if (isdir)
495 fc->no_fsyncdir = 1;
496 else
497 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498 err = 0;
499 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400500out:
Al Viro59551022016-01-22 15:40:57 -0500501 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700502 return err;
503}
504
Josef Bacik02c24a82011-07-16 20:44:56 -0400505static int fuse_fsync(struct file *file, loff_t start, loff_t end,
506 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700507{
Josef Bacik02c24a82011-07-16 20:44:56 -0400508 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700509}
510
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200511void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
512 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700513{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700514 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800515 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800517 inarg->fh = ff->fh;
518 inarg->offset = pos;
519 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800520 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800521 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200522 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700523 req->in.numargs = 1;
524 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800525 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526 req->out.argvar = 1;
527 req->out.numargs = 1;
528 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529}
530
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200531static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400532{
533 unsigned i;
534
535 for (i = 0; i < req->num_pages; i++) {
536 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200537 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400538 set_page_dirty_lock(page);
539 put_page(page);
540 }
541}
542
Seth Forshee744742d2016-03-11 10:35:34 -0600543static void fuse_io_release(struct kref *kref)
544{
545 kfree(container_of(kref, struct fuse_io_priv, refcnt));
546}
547
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100548static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
549{
550 if (io->err)
551 return io->err;
552
553 if (io->bytes >= 0 && io->write)
554 return -EIO;
555
556 return io->bytes < 0 ? io->size : io->bytes;
557}
558
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400559/**
560 * In case of short read, the caller sets 'pos' to the position of
561 * actual end of fuse request in IO request. Otherwise, if bytes_requested
562 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
563 *
564 * An example:
565 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
566 * both submitted asynchronously. The first of them was ACKed by userspace as
567 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
568 * second request was ACKed as short, e.g. only 1K was read, resulting in
569 * pos == 33K.
570 *
571 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
572 * will be equal to the length of the longest contiguous fragment of
573 * transferred data starting from the beginning of IO request.
574 */
575static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
576{
577 int left;
578
579 spin_lock(&io->lock);
580 if (err)
581 io->err = io->err ? : err;
582 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
583 io->bytes = pos;
584
585 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530586 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100587 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400588 spin_unlock(&io->lock);
589
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530590 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100591 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400592
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100593 if (res >= 0) {
594 struct inode *inode = file_inode(io->iocb->ki_filp);
595 struct fuse_conn *fc = get_fuse_conn(inode);
596 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400597
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100598 spin_lock(&fc->lock);
599 fi->attr_version = ++fc->attr_version;
600 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400601 }
602
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100603 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400604 }
Seth Forshee744742d2016-03-11 10:35:34 -0600605
606 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400607}
608
609static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
610{
611 struct fuse_io_priv *io = req->io;
612 ssize_t pos = -1;
613
Ashish Samant61c12b42017-07-12 19:26:58 -0700614 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400615
616 if (io->write) {
617 if (req->misc.write.in.size != req->misc.write.out.size)
618 pos = req->misc.write.in.offset - io->offset +
619 req->misc.write.out.size;
620 } else {
621 if (req->misc.read.in.size != req->out.args[0].size)
622 pos = req->misc.read.in.offset - io->offset +
623 req->out.args[0].size;
624 }
625
626 fuse_aio_complete(io, req->out.h.error, pos);
627}
628
629static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
630 size_t num_bytes, struct fuse_io_priv *io)
631{
632 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600633 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400634 io->size += num_bytes;
635 io->reqs++;
636 spin_unlock(&io->lock);
637
638 req->io = io;
639 req->end = fuse_aio_complete_req;
640
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400641 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400642 fuse_request_send_background(fc, req);
643
644 return num_bytes;
645}
646
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400647static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200648 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700649{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200650 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200651 struct fuse_file *ff = file->private_data;
652 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700653
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200654 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700655 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700656 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700657
658 inarg->read_flags |= FUSE_READ_LOCKOWNER;
659 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
660 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400661
662 if (io->async)
663 return fuse_async_req_send(fc, req, count, io);
664
Tejun Heob93f8582008-11-26 12:03:55 +0100665 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800666 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700667}
668
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669static void fuse_read_update_size(struct inode *inode, loff_t size,
670 u64 attr_ver)
671{
672 struct fuse_conn *fc = get_fuse_conn(inode);
673 struct fuse_inode *fi = get_fuse_inode(inode);
674
675 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400676 if (attr_ver == fi->attr_version && size < inode->i_size &&
677 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700678 fi->attr_version = ++fc->attr_version;
679 i_size_write(inode, size);
680 }
681 spin_unlock(&fc->lock);
682}
683
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400684static void fuse_short_read(struct fuse_req *req, struct inode *inode,
685 u64 attr_ver)
686{
687 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400688 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400689
Pavel Emelyanov83732002013-10-10 17:10:46 +0400690 if (fc->writeback_cache) {
691 /*
692 * A hole in a file. Some data after the hole are in page cache,
693 * but have not reached the client fs yet. So, the hole is not
694 * present there.
695 */
696 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 int start_idx = num_read >> PAGE_SHIFT;
698 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400699
700 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300701 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400702 off = 0;
703 }
704 } else {
705 loff_t pos = page_offset(req->pages[0]) + num_read;
706 fuse_read_update_size(inode, pos, attr_ver);
707 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400708}
709
Maxim Patlasov482fce52013-10-10 17:11:25 +0400710static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700711{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200712 struct kiocb iocb;
713 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700714 struct inode *inode = page->mapping->host;
715 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800716 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700717 size_t num_read;
718 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300719 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700720 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800721 int err;
722
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700723 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300724 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700725 * page-cache page, so make sure we read a properly synced
726 * page.
727 */
728 fuse_wait_on_page_writeback(inode, page->index);
729
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400730 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700731 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400732 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700733
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700734 attr_ver = fuse_get_attr_version(fc);
735
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700736 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200737 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738 req->num_pages = 1;
739 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400740 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200741 init_sync_kiocb(&iocb, file);
742 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400743 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700744 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700745
746 if (!err) {
747 /*
748 * Short read means EOF. If file size is larger, truncate it
749 */
750 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400751 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700752
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 }
755
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400756 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400757
758 return err;
759}
760
761static int fuse_readpage(struct file *file, struct page *page)
762{
763 struct inode *inode = page->mapping->host;
764 int err;
765
766 err = -EIO;
767 if (is_bad_inode(inode))
768 goto out;
769
770 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800771 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700772 out:
773 unlock_page(page);
774 return err;
775}
776
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800777static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700778{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800779 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700780 size_t count = req->misc.read.in.size;
781 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200782 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800783
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 for (i = 0; mapping == NULL && i < req->num_pages; i++)
785 mapping = req->pages[i]->mapping;
786
787 if (mapping) {
788 struct inode *inode = mapping->host;
789
790 /*
791 * Short read means EOF. If file size is larger, truncate it
792 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400793 if (!req->out.h.error && num_read < count)
794 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795
Andrew Gallagher451418f2013-11-05 03:55:43 -0800796 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700797 }
798
Miklos Szeredidb50b962005-09-09 13:10:33 -0700799 for (i = 0; i < req->num_pages; i++) {
800 struct page *page = req->pages[i];
801 if (!req->out.h.error)
802 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800803 else
804 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700805 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300806 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700807 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700808 if (req->ff)
Chad Austind9267e12018-12-10 10:54:52 -0800809 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800810}
811
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200812static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200814 struct fuse_file *ff = file->private_data;
815 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800816 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300817 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200818
819 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800820 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200821 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200822 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700823 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800824 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700825 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800826 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100827 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800828 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100829 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800830 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100831 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800832 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700833}
834
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700835struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700836 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800837 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400839 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840};
841
842static int fuse_readpages_fill(void *_data, struct page *page)
843{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700844 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 struct fuse_req *req = data->req;
846 struct inode *inode = data->inode;
847 struct fuse_conn *fc = get_fuse_conn(inode);
848
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700849 fuse_wait_on_page_writeback(inode, page->index);
850
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 if (req->num_pages &&
852 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300853 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400855 int nr_alloc = min_t(unsigned, data->nr_pages,
856 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200857 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400858 if (fc->async_read)
859 req = fuse_get_req_for_background(fc, nr_alloc);
860 else
861 req = fuse_get_req(fc, nr_alloc);
862
863 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700864 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700865 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400869
870 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300871 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400872 fuse_put_request(fc, req);
873 return -EIO;
874 }
875
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700877 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400878 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100879 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400880 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700881 return 0;
882}
883
884static int fuse_readpages(struct file *file, struct address_space *mapping,
885 struct list_head *pages, unsigned nr_pages)
886{
887 struct inode *inode = mapping->host;
888 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700889 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400891 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800892
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700893 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800894 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800895 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800896
Miklos Szeredia6643092007-11-28 16:22:00 -0800897 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700898 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400899 if (fc->async_read)
900 data.req = fuse_get_req_for_background(fc, nr_alloc);
901 else
902 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400903 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700904 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700905 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800906 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907
908 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700909 if (!err) {
910 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200911 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700912 else
913 fuse_put_request(fc, data.req);
914 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800915out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700916 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700917}
918
Al Viro37c20f12014-04-02 14:47:09 -0400919static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800920{
921 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400922 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800923
Brian Fostera8894272012-07-16 15:23:50 -0400924 /*
925 * In auto invalidate mode, always update attributes on read.
926 * Otherwise, only update if we attempt to read past EOF (to ensure
927 * i_size is up to date).
928 */
929 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400930 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200932 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933 if (err)
934 return err;
935 }
936
Al Viro37c20f12014-04-02 14:47:09 -0400937 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800938}
939
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200940static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200941 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700942{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700943 struct fuse_write_in *inarg = &req->misc.write.in;
944 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700945
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700946 inarg->fh = ff->fh;
947 inarg->offset = pos;
948 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700949 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200952 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700953 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
954 else
955 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700956 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700957 req->in.args[1].size = count;
958 req->out.numargs = 1;
959 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700960 req->out.args[0].value = outarg;
961}
962
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400963static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200964 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700965{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200966 struct kiocb *iocb = io->iocb;
967 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200968 struct fuse_file *ff = file->private_data;
969 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200970 struct fuse_write_in *inarg = &req->misc.write.in;
971
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200972 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200973 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200974 if (iocb->ki_flags & IOCB_DSYNC)
975 inarg->flags |= O_DSYNC;
976 if (iocb->ki_flags & IOCB_SYNC)
977 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700978 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700979 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
980 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
981 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400982
983 if (io->async)
984 return fuse_async_req_send(fc, req, count, io);
985
Tejun Heob93f8582008-11-26 12:03:55 +0100986 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700987 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700988}
989
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400990bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700991{
992 struct fuse_conn *fc = get_fuse_conn(inode);
993 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400994 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700995
996 spin_lock(&fc->lock);
997 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400998 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700999 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001000 ret = true;
1001 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001002 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003
1004 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001005}
1006
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001007static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001008 struct inode *inode, loff_t pos,
1009 size_t count)
1010{
1011 size_t res;
1012 unsigned offset;
1013 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001014 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001015
1016 for (i = 0; i < req->num_pages; i++)
1017 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1018
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001019 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001020
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001021 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001022 count = res;
1023 for (i = 0; i < req->num_pages; i++) {
1024 struct page *page = req->pages[i];
1025
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001026 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001027 SetPageUptodate(page);
1028
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001029 if (count > PAGE_SIZE - offset)
1030 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001031 else
1032 count = 0;
1033 offset = 0;
1034
1035 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001036 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001037 }
1038
1039 return res;
1040}
1041
1042static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1043 struct address_space *mapping,
1044 struct iov_iter *ii, loff_t pos)
1045{
1046 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001047 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001048 size_t count = 0;
1049 int err;
1050
Miklos Szeredif4975c62009-04-02 14:25:34 +02001051 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001052 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001053
1054 do {
1055 size_t tmp;
1056 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001057 pgoff_t index = pos >> PAGE_SHIFT;
1058 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001059 iov_iter_count(ii));
1060
1061 bytes = min_t(size_t, bytes, fc->max_write - count);
1062
1063 again:
1064 err = -EFAULT;
1065 if (iov_iter_fault_in_readable(ii, bytes))
1066 break;
1067
1068 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001069 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001070 if (!page)
1071 break;
1072
anfei zhou931e80e2010-02-02 13:44:02 -08001073 if (mapping_writably_mapped(mapping))
1074 flush_dcache_page(page);
1075
Nick Pigginea9b9902008-04-30 00:54:42 -07001076 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001077 flush_dcache_page(page);
1078
Roman Gushchin3ca81382015-10-12 16:33:44 +03001079 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001080 if (!tmp) {
1081 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001082 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 bytes = min(bytes, iov_iter_single_seg_count(ii));
1084 goto again;
1085 }
1086
1087 err = 0;
1088 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001089 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 req->num_pages++;
1091
Nick Pigginea9b9902008-04-30 00:54:42 -07001092 count += tmp;
1093 pos += tmp;
1094 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001095 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 offset = 0;
1097
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001098 if (!fc->big_writes)
1099 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001100 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001101 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001102
1103 return count > 0 ? count : err;
1104}
1105
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001106static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1107{
1108 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001109 ((pos + len - 1) >> PAGE_SHIFT) -
1110 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001111 FUSE_MAX_PAGES_PER_REQ);
1112}
1113
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001114static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 struct address_space *mapping,
1116 struct iov_iter *ii, loff_t pos)
1117{
1118 struct inode *inode = mapping->host;
1119 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001120 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001121 int err = 0;
1122 ssize_t res = 0;
1123
1124 if (is_bad_inode(inode))
1125 return -EIO;
1126
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001127 if (inode->i_size < pos + iov_iter_count(ii))
1128 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1129
Nick Pigginea9b9902008-04-30 00:54:42 -07001130 do {
1131 struct fuse_req *req;
1132 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001133 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001134
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001135 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001136 if (IS_ERR(req)) {
1137 err = PTR_ERR(req);
1138 break;
1139 }
1140
1141 count = fuse_fill_write_pages(req, mapping, ii, pos);
1142 if (count <= 0) {
1143 err = count;
1144 } else {
1145 size_t num_written;
1146
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001147 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001148 pos, count);
1149 err = req->out.h.error;
1150 if (!err) {
1151 res += num_written;
1152 pos += num_written;
1153
1154 /* break out of the loop on short write */
1155 if (num_written != count)
1156 err = -EIO;
1157 }
1158 }
1159 fuse_put_request(fc, req);
1160 } while (!err && iov_iter_count(ii));
1161
1162 if (res > 0)
1163 fuse_write_update_size(inode, pos);
1164
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001165 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001166 fuse_invalidate_attr(inode);
1167
1168 return res > 0 ? res : err;
1169}
1170
Al Viro84c3d552014-04-03 14:33:23 -04001171static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001172{
1173 struct file *file = iocb->ki_filp;
1174 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001175 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001176 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001177 struct inode *inode = mapping->host;
1178 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001179 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001180
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001181 if (get_fuse_conn(inode)->writeback_cache) {
1182 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001183 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001184 if (err)
1185 return err;
1186
Al Viro84c3d552014-04-03 14:33:23 -04001187 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001188 }
1189
Al Viro59551022016-01-22 15:40:57 -05001190 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001191
1192 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001193 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001194
Al Viro3309dd02015-04-09 12:55:47 -04001195 err = generic_write_checks(iocb, from);
1196 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001197 goto out;
1198
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001199 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001200 if (err)
1201 goto out;
1202
Josef Bacikc3b2da32012-03-26 09:59:21 -04001203 err = file_update_time(file);
1204 if (err)
1205 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001206
Al Viro2ba48ce2015-04-09 13:52:01 -04001207 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001208 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001209 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001210 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001211 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001212
Anand Avati4273b792012-02-17 12:46:25 -05001213 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001214
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001215 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001216 if (written_buffered < 0) {
1217 err = written_buffered;
1218 goto out;
1219 }
1220 endbyte = pos + written_buffered - 1;
1221
1222 err = filemap_write_and_wait_range(file->f_mapping, pos,
1223 endbyte);
1224 if (err)
1225 goto out;
1226
1227 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 pos >> PAGE_SHIFT,
1229 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001230
1231 written += written_buffered;
1232 iocb->ki_pos = pos + written_buffered;
1233 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001234 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001235 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001236 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001237 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001238out:
1239 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001240 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001241 if (written > 0)
1242 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001243
1244 return written ? written : err;
1245}
1246
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001247static inline void fuse_page_descs_length_init(struct fuse_req *req,
1248 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001249{
1250 int i;
1251
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001252 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001253 req->page_descs[i].length = PAGE_SIZE -
1254 req->page_descs[i].offset;
1255}
1256
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001257static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1258{
1259 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1260}
1261
1262static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1263 size_t max_size)
1264{
1265 return min(iov_iter_single_seg_count(ii), max_size);
1266}
1267
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001268static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001269 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001270{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001271 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001272 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001273
Miklos Szeredif4975c62009-04-02 14:25:34 +02001274 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001275 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001276 unsigned long user_addr = fuse_get_user_addr(ii);
1277 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1278
Miklos Szeredif4975c62009-04-02 14:25:34 +02001279 if (write)
1280 req->in.args[1].value = (void *) user_addr;
1281 else
1282 req->out.args[0].value = (void *) user_addr;
1283
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001284 iov_iter_advance(ii, frag_size);
1285 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001286 return 0;
1287 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001288
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001289 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001290 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001291 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001292 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001293 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001294 req->max_pages - req->num_pages,
1295 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001296 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001297 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298
Al Viroc9c37e22014-03-16 16:08:30 -04001299 iov_iter_advance(ii, ret);
1300 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001301
Al Viroc9c37e22014-03-16 16:08:30 -04001302 ret += start;
1303 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1304
1305 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306 fuse_page_descs_length_init(req, req->num_pages, npages);
1307
1308 req->num_pages += npages;
1309 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001310 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001311 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001312
1313 if (write)
1314 req->in.argpages = 1;
1315 else
1316 req->out.argpages = 1;
1317
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001318 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001319
Ashish Samant2c932d42016-03-25 10:53:41 -07001320 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001321}
1322
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001323static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1324{
Al Virof67da302014-03-19 01:16:16 -04001325 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001326}
1327
Al Virod22a9432014-03-16 15:50:47 -04001328ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1329 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001330{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001331 int write = flags & FUSE_DIO_WRITE;
1332 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001333 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001334 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001335 struct fuse_file *ff = file->private_data;
1336 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001337 size_t nmax = write ? fc->max_write : fc->max_read;
1338 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001339 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 pgoff_t idx_from = pos >> PAGE_SHIFT;
1341 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001342 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001343 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001344 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001345
Brian Fosterde82b922013-05-14 11:25:39 -04001346 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001347 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001348 else
Al Virod22a9432014-03-16 15:50:47 -04001349 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001350 if (IS_ERR(req))
1351 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001352
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001353 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1354 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001355 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001356 fuse_sync_writes(inode);
1357 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001358 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001359 }
1360
Ashish Samant61c12b42017-07-12 19:26:58 -07001361 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001364 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001365 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001366 err = fuse_get_user_pages(req, iter, &nbytes, write);
1367 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001369
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001371 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001372 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001373 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001374
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001375 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001376 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001377 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001378 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379 break;
1380 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001381 res = 0;
1382 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383 break;
1384 }
1385 count -= nres;
1386 res += nres;
1387 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001388 if (nres != nbytes)
1389 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001390 if (count) {
1391 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001392 if (io->async)
1393 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001394 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001395 else
Al Virod22a9432014-03-16 15:50:47 -04001396 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001397 if (IS_ERR(req))
1398 break;
1399 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001401 if (!IS_ERR(req))
1402 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001403 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405
Ashish Samant742f9922016-03-14 21:57:35 -07001406 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407}
Tejun Heo08cbf542009-04-14 10:54:53 +09001408EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001410static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001411 struct iov_iter *iter,
1412 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001414 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001415 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001416
1417 if (is_bad_inode(inode))
1418 return -EIO;
1419
Al Virod22a9432014-03-16 15:50:47 -04001420 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001421
1422 fuse_invalidate_attr(inode);
1423
1424 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001425}
1426
Al Viro15316262015-03-30 22:08:36 -04001427static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001428{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001429 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001430 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001431}
1432
Al Viro15316262015-03-30 22:08:36 -04001433static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001435 struct inode *inode = file_inode(iocb->ki_filp);
1436 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro15316262015-03-30 22:08:36 -04001437 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001438
1439 if (is_bad_inode(inode))
1440 return -EIO;
1441
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001442 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001443 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001444 res = generic_write_checks(iocb, from);
1445 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001446 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001447 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001448 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001449 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001450 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001451
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001452 return res;
1453}
1454
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001455static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001456{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001457 int i;
1458
1459 for (i = 0; i < req->num_pages; i++)
1460 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001461
1462 if (req->ff)
Chad Austind9267e12018-12-10 10:54:52 -08001463 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001464}
1465
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001466static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001467{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001468 struct inode *inode = req->inode;
1469 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001470 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001471 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001472
1473 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001474 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001475 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001476 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001477 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001478 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001479 wake_up(&fi->page_waitq);
1480}
1481
1482/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001483static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1484 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001485__releases(fc->lock)
1486__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001487{
1488 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001489 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001490 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001491
1492 if (!fc->connected)
1493 goto out_free;
1494
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001495 if (inarg->offset + data_size <= size) {
1496 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001498 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001499 } else {
1500 /* Got truncated off completely */
1501 goto out_free;
1502 }
1503
1504 req->in.args[1].size = inarg->size;
1505 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001506 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001507 return;
1508
1509 out_free:
1510 fuse_writepage_finish(fc, req);
1511 spin_unlock(&fc->lock);
1512 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001513 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001514 spin_lock(&fc->lock);
1515}
1516
1517/*
1518 * If fi->writectr is positive (no truncate or fsync going on) send
1519 * all queued writepage requests.
1520 *
1521 * Called with fc->lock
1522 */
1523void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001524__releases(fc->lock)
1525__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001526{
1527 struct fuse_conn *fc = get_fuse_conn(inode);
1528 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001529 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001530 struct fuse_req *req;
1531
1532 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1533 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1534 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001535 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001536 }
1537}
1538
1539static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1540{
1541 struct inode *inode = req->inode;
1542 struct fuse_inode *fi = get_fuse_inode(inode);
1543
1544 mapping_set_error(inode->i_mapping, req->out.h.error);
1545 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001546 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001547 struct fuse_conn *fc = get_fuse_conn(inode);
1548 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001549 struct fuse_req *next = req->misc.write.next;
1550 req->misc.write.next = next->misc.write.next;
1551 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001552 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001553 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001554
1555 /*
1556 * Skip fuse_flush_writepages() to make it easy to crop requests
1557 * based on primary request size.
1558 *
1559 * 1st case (trivial): there are no concurrent activities using
1560 * fuse_set/release_nowrite. Then we're on safe side because
1561 * fuse_flush_writepages() would call fuse_send_writepage()
1562 * anyway.
1563 *
1564 * 2nd case: someone called fuse_set_nowrite and it is waiting
1565 * now for completion of all in-flight requests. This happens
1566 * rarely and no more than once per page, so this should be
1567 * okay.
1568 *
1569 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1570 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1571 * that fuse_set_nowrite returned implies that all in-flight
1572 * requests were completed along with all of their secondary
1573 * requests. Further primary requests are blocked by negative
1574 * writectr. Hence there cannot be any in-flight requests and
1575 * no invocations of fuse_writepage_end() while we're in
1576 * fuse_set_nowrite..fuse_release_nowrite section.
1577 */
1578 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001579 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001580 fi->writectr--;
1581 fuse_writepage_finish(fc, req);
1582 spin_unlock(&fc->lock);
1583 fuse_writepage_free(fc, req);
1584}
1585
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001586static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1587 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001588{
Miklos Szeredi72523422013-10-01 16:44:52 +02001589 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001590
1591 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001592 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001593 ff = list_entry(fi->write_files.next, struct fuse_file,
1594 write_entry);
1595 fuse_file_get(ff);
1596 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001597 spin_unlock(&fc->lock);
1598
1599 return ff;
1600}
1601
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001602static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1603 struct fuse_inode *fi)
1604{
1605 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1606 WARN_ON(!ff);
1607 return ff;
1608}
1609
1610int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1611{
1612 struct fuse_conn *fc = get_fuse_conn(inode);
1613 struct fuse_inode *fi = get_fuse_inode(inode);
1614 struct fuse_file *ff;
1615 int err;
1616
1617 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001618 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001619 if (ff)
Chad Austind9267e12018-12-10 10:54:52 -08001620 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001621
1622 return err;
1623}
1624
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001625static int fuse_writepage_locked(struct page *page)
1626{
1627 struct address_space *mapping = page->mapping;
1628 struct inode *inode = mapping->host;
1629 struct fuse_conn *fc = get_fuse_conn(inode);
1630 struct fuse_inode *fi = get_fuse_inode(inode);
1631 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001632 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001633 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001634
1635 set_page_writeback(page);
1636
Maxim Patlasov4250c062012-10-26 19:48:07 +04001637 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001638 if (!req)
1639 goto err;
1640
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001641 /* writeback always goes to bg_queue */
1642 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001643 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1644 if (!tmp_page)
1645 goto err_free;
1646
Miklos Szeredi72523422013-10-01 16:44:52 +02001647 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001648 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001649 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001650 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001651
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001652 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001653
1654 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001655 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001656 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001657 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658 req->num_pages = 1;
1659 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001660 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001661 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001662 req->end = fuse_writepage_end;
1663 req->inode = inode;
1664
Tejun Heo93f78d82015-05-22 17:13:27 -04001665 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001666 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001667
1668 spin_lock(&fc->lock);
1669 list_add(&req->writepages_entry, &fi->writepages);
1670 list_add_tail(&req->list, &fi->queued_writes);
1671 fuse_flush_writepages(inode);
1672 spin_unlock(&fc->lock);
1673
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001674 end_page_writeback(page);
1675
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001676 return 0;
1677
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001678err_nofile:
1679 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001680err_free:
1681 fuse_request_free(req);
1682err:
Jeff Layton91839762017-05-25 06:57:50 -04001683 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001684 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001685 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001686}
1687
1688static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1689{
1690 int err;
1691
Miklos Szerediff17be02013-10-01 16:44:53 +02001692 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1693 /*
1694 * ->writepages() should be called for sync() and friends. We
1695 * should only get here on direct reclaim and then we are
1696 * allowed to skip a page which is already in flight
1697 */
1698 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1699
1700 redirty_page_for_writepage(wbc, page);
1701 return 0;
1702 }
1703
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001704 err = fuse_writepage_locked(page);
1705 unlock_page(page);
1706
1707 return err;
1708}
1709
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001710struct fuse_fill_wb_data {
1711 struct fuse_req *req;
1712 struct fuse_file *ff;
1713 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001714 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001715};
1716
1717static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1718{
1719 struct fuse_req *req = data->req;
1720 struct inode *inode = data->inode;
1721 struct fuse_conn *fc = get_fuse_conn(inode);
1722 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001723 int num_pages = req->num_pages;
1724 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001725
1726 req->ff = fuse_file_get(data->ff);
1727 spin_lock(&fc->lock);
1728 list_add_tail(&req->list, &fi->queued_writes);
1729 fuse_flush_writepages(inode);
1730 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001731
1732 for (i = 0; i < num_pages; i++)
1733 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001734}
1735
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001736static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1737 struct page *page)
1738{
1739 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1740 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1741 struct fuse_req *tmp;
1742 struct fuse_req *old_req;
1743 bool found = false;
1744 pgoff_t curr_index;
1745
1746 BUG_ON(new_req->num_pages != 0);
1747
1748 spin_lock(&fc->lock);
1749 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001750 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1751 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001752 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001753 if (curr_index <= page->index &&
1754 page->index < curr_index + old_req->num_pages) {
1755 found = true;
1756 break;
1757 }
1758 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001759 if (!found) {
1760 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001761 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001762 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001763
Maxim Patlasovf6011082013-10-02 15:01:07 +04001764 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001765 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1766 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001767 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768 if (tmp->num_pages == 1 &&
1769 curr_index == page->index) {
1770 old_req = tmp;
1771 }
1772 }
1773
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001774 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001775 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001776
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001777 copy_highpage(old_req->pages[0], page);
1778 spin_unlock(&fc->lock);
1779
Tejun Heo93f78d82015-05-22 17:13:27 -04001780 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001781 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001782 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001783 fuse_writepage_free(fc, new_req);
1784 fuse_request_free(new_req);
1785 goto out;
1786 } else {
1787 new_req->misc.write.next = old_req->misc.write.next;
1788 old_req->misc.write.next = new_req;
1789 }
1790out_unlock:
1791 spin_unlock(&fc->lock);
1792out:
1793 return found;
1794}
1795
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001796static int fuse_writepages_fill(struct page *page,
1797 struct writeback_control *wbc, void *_data)
1798{
1799 struct fuse_fill_wb_data *data = _data;
1800 struct fuse_req *req = data->req;
1801 struct inode *inode = data->inode;
1802 struct fuse_conn *fc = get_fuse_conn(inode);
1803 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001804 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001805 int err;
1806
1807 if (!data->ff) {
1808 err = -EIO;
1809 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1810 if (!data->ff)
1811 goto out_unlock;
1812 }
1813
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001814 /*
1815 * Being under writeback is unlikely but possible. For example direct
1816 * read to an mmaped fuse file will set the page dirty twice; once when
1817 * the pages are faulted with get_user_pages(), and then after the read
1818 * completed.
1819 */
1820 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001821
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001822 if (req && req->num_pages &&
1823 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001824 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001825 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1826 fuse_writepages_send(data);
1827 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001828 }
1829 err = -ENOMEM;
1830 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1831 if (!tmp_page)
1832 goto out_unlock;
1833
1834 /*
1835 * The page must not be redirtied until the writeout is completed
1836 * (i.e. userspace has sent a reply to the write request). Otherwise
1837 * there could be more than one temporary page instance for each real
1838 * page.
1839 *
1840 * This is ensured by holding the page lock in page_mkwrite() while
1841 * checking fuse_page_is_writeback(). We already hold the page lock
1842 * since clear_page_dirty_for_io() and keep it held until we add the
1843 * request to the fi->writepages list and increment req->num_pages.
1844 * After this fuse_page_is_writeback() will indicate that the page is
1845 * under writeback, so we can release the page lock.
1846 */
1847 if (data->req == NULL) {
1848 struct fuse_inode *fi = get_fuse_inode(inode);
1849
1850 err = -ENOMEM;
1851 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1852 if (!req) {
1853 __free_page(tmp_page);
1854 goto out_unlock;
1855 }
1856
1857 fuse_write_fill(req, data->ff, page_offset(page), 0);
1858 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001859 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001860 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001861 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001862 req->num_pages = 0;
1863 req->end = fuse_writepage_end;
1864 req->inode = inode;
1865
1866 spin_lock(&fc->lock);
1867 list_add(&req->writepages_entry, &fi->writepages);
1868 spin_unlock(&fc->lock);
1869
1870 data->req = req;
1871 }
1872 set_page_writeback(page);
1873
1874 copy_highpage(tmp_page, page);
1875 req->pages[req->num_pages] = tmp_page;
1876 req->page_descs[req->num_pages].offset = 0;
1877 req->page_descs[req->num_pages].length = PAGE_SIZE;
1878
Tejun Heo93f78d82015-05-22 17:13:27 -04001879 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001880 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001881
1882 err = 0;
1883 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1884 end_page_writeback(page);
1885 data->req = NULL;
1886 goto out_unlock;
1887 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001888 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001889
1890 /*
1891 * Protected by fc->lock against concurrent access by
1892 * fuse_page_is_writeback().
1893 */
1894 spin_lock(&fc->lock);
1895 req->num_pages++;
1896 spin_unlock(&fc->lock);
1897
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001898out_unlock:
1899 unlock_page(page);
1900
1901 return err;
1902}
1903
1904static int fuse_writepages(struct address_space *mapping,
1905 struct writeback_control *wbc)
1906{
1907 struct inode *inode = mapping->host;
1908 struct fuse_fill_wb_data data;
1909 int err;
1910
1911 err = -EIO;
1912 if (is_bad_inode(inode))
1913 goto out;
1914
1915 data.inode = inode;
1916 data.req = NULL;
1917 data.ff = NULL;
1918
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001919 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001920 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1921 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001922 GFP_NOFS);
1923 if (!data.orig_pages)
1924 goto out;
1925
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001926 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1927 if (data.req) {
1928 /* Ignore errors if we can write at least one page */
1929 BUG_ON(!data.req->num_pages);
1930 fuse_writepages_send(&data);
1931 err = 0;
1932 }
1933 if (data.ff)
Chad Austind9267e12018-12-10 10:54:52 -08001934 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001935
1936 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001937out:
1938 return err;
1939}
1940
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001941/*
1942 * It's worthy to make sure that space is reserved on disk for the write,
1943 * but how to implement it without killing performance need more thinking.
1944 */
1945static int fuse_write_begin(struct file *file, struct address_space *mapping,
1946 loff_t pos, unsigned len, unsigned flags,
1947 struct page **pagep, void **fsdata)
1948{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001949 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001950 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001951 struct page *page;
1952 loff_t fsize;
1953 int err = -ENOMEM;
1954
1955 WARN_ON(!fc->writeback_cache);
1956
1957 page = grab_cache_page_write_begin(mapping, index, flags);
1958 if (!page)
1959 goto error;
1960
1961 fuse_wait_on_page_writeback(mapping->host, page->index);
1962
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001963 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001964 goto success;
1965 /*
1966 * Check if the start this page comes after the end of file, in which
1967 * case the readpage can be optimized away.
1968 */
1969 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 if (fsize <= (pos & PAGE_MASK)) {
1971 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001972 if (off)
1973 zero_user_segment(page, 0, off);
1974 goto success;
1975 }
1976 err = fuse_do_readpage(file, page);
1977 if (err)
1978 goto cleanup;
1979success:
1980 *pagep = page;
1981 return 0;
1982
1983cleanup:
1984 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001985 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001986error:
1987 return err;
1988}
1989
1990static int fuse_write_end(struct file *file, struct address_space *mapping,
1991 loff_t pos, unsigned len, unsigned copied,
1992 struct page *page, void *fsdata)
1993{
1994 struct inode *inode = page->mapping->host;
1995
Miklos Szeredi59c3b762016-08-18 09:10:44 +02001996 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1997 if (!copied)
1998 goto unlock;
1999
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002000 if (!PageUptodate(page)) {
2001 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002003 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002004 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002005 SetPageUptodate(page);
2006 }
2007
2008 fuse_write_update_size(inode, pos + copied);
2009 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002010
2011unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002012 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002014
2015 return copied;
2016}
2017
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002018static int fuse_launder_page(struct page *page)
2019{
2020 int err = 0;
2021 if (clear_page_dirty_for_io(page)) {
2022 struct inode *inode = page->mapping->host;
2023 err = fuse_writepage_locked(page);
2024 if (!err)
2025 fuse_wait_on_page_writeback(inode, page->index);
2026 }
2027 return err;
2028}
2029
2030/*
2031 * Write back dirty pages now, because there may not be any suitable
2032 * open files later
2033 */
2034static void fuse_vma_close(struct vm_area_struct *vma)
2035{
2036 filemap_write_and_wait(vma->vm_file->f_mapping);
2037}
2038
2039/*
2040 * Wait for writeback against this page to complete before allowing it
2041 * to be marked dirty again, and hence written back again, possibly
2042 * before the previous writepage completed.
2043 *
2044 * Block here, instead of in ->writepage(), so that the userspace fs
2045 * can only block processes actually operating on the filesystem.
2046 *
2047 * Otherwise unprivileged userspace fs would be able to block
2048 * unrelated:
2049 *
2050 * - page migration
2051 * - sync(2)
2052 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2053 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302054static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002055{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002056 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002057 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002058
Dave Jiang11bac802017-02-24 14:56:41 -08002059 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002060 lock_page(page);
2061 if (page->mapping != inode->i_mapping) {
2062 unlock_page(page);
2063 return VM_FAULT_NOPAGE;
2064 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002065
2066 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002067 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002068}
2069
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002070static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002071 .close = fuse_vma_close,
2072 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002073 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002074 .page_mkwrite = fuse_page_mkwrite,
2075};
2076
2077static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2078{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002079 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2080 fuse_link_write_file(file);
2081
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002082 file_accessed(file);
2083 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002084 return 0;
2085}
2086
Miklos Szeredifc280c92009-04-02 14:25:35 +02002087static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2088{
2089 /* Can't provide the coherency needed for MAP_SHARED */
2090 if (vma->vm_flags & VM_MAYSHARE)
2091 return -ENODEV;
2092
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002093 invalidate_inode_pages2(file->f_mapping);
2094
Miklos Szeredifc280c92009-04-02 14:25:35 +02002095 return generic_file_mmap(file, vma);
2096}
2097
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002098static int convert_fuse_file_lock(struct fuse_conn *fc,
2099 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002100 struct file_lock *fl)
2101{
2102 switch (ffl->type) {
2103 case F_UNLCK:
2104 break;
2105
2106 case F_RDLCK:
2107 case F_WRLCK:
2108 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2109 ffl->end < ffl->start)
2110 return -EIO;
2111
2112 fl->fl_start = ffl->start;
2113 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002114
2115 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002116 * Convert pid into init's pid namespace. The locks API will
2117 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002118 */
2119 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002120 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002121 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002122 break;
2123
2124 default:
2125 return -EIO;
2126 }
2127 fl->fl_type = ffl->type;
2128 return 0;
2129}
2130
Miklos Szeredi70781872014-12-12 09:49:05 +01002131static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002132 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002133 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002134{
Al Viro6131ffa2013-02-27 16:59:05 -05002135 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002136 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002137 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002138
Miklos Szeredi70781872014-12-12 09:49:05 +01002139 memset(inarg, 0, sizeof(*inarg));
2140 inarg->fh = ff->fh;
2141 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2142 inarg->lk.start = fl->fl_start;
2143 inarg->lk.end = fl->fl_end;
2144 inarg->lk.type = fl->fl_type;
2145 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002146 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002147 inarg->lk_flags |= FUSE_LK_FLOCK;
2148 args->in.h.opcode = opcode;
2149 args->in.h.nodeid = get_node_id(inode);
2150 args->in.numargs = 1;
2151 args->in.args[0].size = sizeof(*inarg);
2152 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002153}
2154
2155static int fuse_getlk(struct file *file, struct file_lock *fl)
2156{
Al Viro6131ffa2013-02-27 16:59:05 -05002157 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002158 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002159 FUSE_ARGS(args);
2160 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002161 struct fuse_lk_out outarg;
2162 int err;
2163
Miklos Szeredi70781872014-12-12 09:49:05 +01002164 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2165 args.out.numargs = 1;
2166 args.out.args[0].size = sizeof(outarg);
2167 args.out.args[0].value = &outarg;
2168 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002169 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002170 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002171
2172 return err;
2173}
2174
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002175static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002176{
Al Viro6131ffa2013-02-27 16:59:05 -05002177 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002178 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002179 FUSE_ARGS(args);
2180 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002181 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002182 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2183 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002184 int err;
2185
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002186 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002187 /* NLM needs asynchronous locks, which we don't support yet */
2188 return -ENOLCK;
2189 }
2190
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002192 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002193 return 0;
2194
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002195 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002196 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002197
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002198 /* locking is restartable */
2199 if (err == -EINTR)
2200 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002201
Miklos Szeredi71421252006-06-25 05:48:52 -07002202 return err;
2203}
2204
2205static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2206{
Al Viro6131ffa2013-02-27 16:59:05 -05002207 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002208 struct fuse_conn *fc = get_fuse_conn(inode);
2209 int err;
2210
Miklos Szeredi48e90762008-07-25 01:49:02 -07002211 if (cmd == F_CANCELLK) {
2212 err = 0;
2213 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002214 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002215 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002216 err = 0;
2217 } else
2218 err = fuse_getlk(file, fl);
2219 } else {
2220 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002221 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002222 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002223 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002224 }
2225 return err;
2226}
2227
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002228static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2229{
Al Viro6131ffa2013-02-27 16:59:05 -05002230 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002231 struct fuse_conn *fc = get_fuse_conn(inode);
2232 int err;
2233
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002234 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002235 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002236 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002237 struct fuse_file *ff = file->private_data;
2238
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002239 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002240 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002241 err = fuse_setlk(file, fl, 1);
2242 }
2243
2244 return err;
2245}
2246
Miklos Szeredib2d22722006-12-06 20:35:51 -08002247static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2248{
2249 struct inode *inode = mapping->host;
2250 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002251 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002252 struct fuse_bmap_in inarg;
2253 struct fuse_bmap_out outarg;
2254 int err;
2255
2256 if (!inode->i_sb->s_bdev || fc->no_bmap)
2257 return 0;
2258
Miklos Szeredib2d22722006-12-06 20:35:51 -08002259 memset(&inarg, 0, sizeof(inarg));
2260 inarg.block = block;
2261 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002262 args.in.h.opcode = FUSE_BMAP;
2263 args.in.h.nodeid = get_node_id(inode);
2264 args.in.numargs = 1;
2265 args.in.args[0].size = sizeof(inarg);
2266 args.in.args[0].value = &inarg;
2267 args.out.numargs = 1;
2268 args.out.args[0].size = sizeof(outarg);
2269 args.out.args[0].value = &outarg;
2270 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002271 if (err == -ENOSYS)
2272 fc->no_bmap = 1;
2273
2274 return err ? 0 : outarg.block;
2275}
2276
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302277static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2278{
2279 struct inode *inode = file->f_mapping->host;
2280 struct fuse_conn *fc = get_fuse_conn(inode);
2281 struct fuse_file *ff = file->private_data;
2282 FUSE_ARGS(args);
2283 struct fuse_lseek_in inarg = {
2284 .fh = ff->fh,
2285 .offset = offset,
2286 .whence = whence
2287 };
2288 struct fuse_lseek_out outarg;
2289 int err;
2290
2291 if (fc->no_lseek)
2292 goto fallback;
2293
2294 args.in.h.opcode = FUSE_LSEEK;
2295 args.in.h.nodeid = ff->nodeid;
2296 args.in.numargs = 1;
2297 args.in.args[0].size = sizeof(inarg);
2298 args.in.args[0].value = &inarg;
2299 args.out.numargs = 1;
2300 args.out.args[0].size = sizeof(outarg);
2301 args.out.args[0].value = &outarg;
2302 err = fuse_simple_request(fc, &args);
2303 if (err) {
2304 if (err == -ENOSYS) {
2305 fc->no_lseek = 1;
2306 goto fallback;
2307 }
2308 return err;
2309 }
2310
2311 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2312
2313fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002314 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302315 if (!err)
2316 return generic_file_llseek(file, offset, whence);
2317 else
2318 return err;
2319}
2320
Andrew Morton965c8e52012-12-17 15:59:39 -08002321static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002322{
2323 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002324 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002325
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302326 switch (whence) {
2327 case SEEK_SET:
2328 case SEEK_CUR:
2329 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002330 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302331 break;
2332 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002333 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002334 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302335 if (!retval)
2336 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002337 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302338 break;
2339 case SEEK_HOLE:
2340 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002341 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302342 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002343 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302344 break;
2345 default:
2346 retval = -EINVAL;
2347 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002348
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002349 return retval;
2350}
2351
Tejun Heo59efec72008-11-26 12:03:55 +01002352/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002353 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2354 * ABI was defined to be 'struct iovec' which is different on 32bit
2355 * and 64bit. Fortunately we can determine which structure the server
2356 * used from the size of the reply.
2357 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002358static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2359 size_t transferred, unsigned count,
2360 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002361{
2362#ifdef CONFIG_COMPAT
2363 if (count * sizeof(struct compat_iovec) == transferred) {
2364 struct compat_iovec *ciov = src;
2365 unsigned i;
2366
2367 /*
2368 * With this interface a 32bit server cannot support
2369 * non-compat (i.e. ones coming from 64bit apps) ioctl
2370 * requests
2371 */
2372 if (!is_compat)
2373 return -EINVAL;
2374
2375 for (i = 0; i < count; i++) {
2376 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2377 dst[i].iov_len = ciov[i].iov_len;
2378 }
2379 return 0;
2380 }
2381#endif
2382
2383 if (count * sizeof(struct iovec) != transferred)
2384 return -EIO;
2385
2386 memcpy(dst, src, transferred);
2387 return 0;
2388}
2389
Miklos Szeredi75727772010-11-30 16:39:27 +01002390/* Make sure iov_length() won't overflow */
2391static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2392{
2393 size_t n;
2394 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2395
Zach Brownfb6ccff2012-07-24 12:10:11 -07002396 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002397 if (iov->iov_len > (size_t) max)
2398 return -ENOMEM;
2399 max -= iov->iov_len;
2400 }
2401 return 0;
2402}
2403
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002404static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2405 void *src, size_t transferred, unsigned count,
2406 bool is_compat)
2407{
2408 unsigned i;
2409 struct fuse_ioctl_iovec *fiov = src;
2410
2411 if (fc->minor < 16) {
2412 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2413 count, is_compat);
2414 }
2415
2416 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2417 return -EIO;
2418
2419 for (i = 0; i < count; i++) {
2420 /* Did the server supply an inappropriate value? */
2421 if (fiov[i].base != (unsigned long) fiov[i].base ||
2422 fiov[i].len != (unsigned long) fiov[i].len)
2423 return -EIO;
2424
2425 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2426 dst[i].iov_len = (size_t) fiov[i].len;
2427
2428#ifdef CONFIG_COMPAT
2429 if (is_compat &&
2430 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2431 (compat_size_t) dst[i].iov_len != fiov[i].len))
2432 return -EIO;
2433#endif
2434 }
2435
2436 return 0;
2437}
2438
2439
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002440/*
Tejun Heo59efec72008-11-26 12:03:55 +01002441 * For ioctls, there is no generic way to determine how much memory
2442 * needs to be read and/or written. Furthermore, ioctls are allowed
2443 * to dereference the passed pointer, so the parameter requires deep
2444 * copying but FUSE has no idea whatsoever about what to copy in or
2445 * out.
2446 *
2447 * This is solved by allowing FUSE server to retry ioctl with
2448 * necessary in/out iovecs. Let's assume the ioctl implementation
2449 * needs to read in the following structure.
2450 *
2451 * struct a {
2452 * char *buf;
2453 * size_t buflen;
2454 * }
2455 *
2456 * On the first callout to FUSE server, inarg->in_size and
2457 * inarg->out_size will be NULL; then, the server completes the ioctl
2458 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2459 * the actual iov array to
2460 *
2461 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2462 *
2463 * which tells FUSE to copy in the requested area and retry the ioctl.
2464 * On the second round, the server has access to the structure and
2465 * from that it can tell what to look for next, so on the invocation,
2466 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2467 *
2468 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2469 * { .iov_base = a.buf, .iov_len = a.buflen } }
2470 *
2471 * FUSE will copy both struct a and the pointed buffer from the
2472 * process doing the ioctl and retry ioctl with both struct a and the
2473 * buffer.
2474 *
2475 * This time, FUSE server has everything it needs and completes ioctl
2476 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2477 *
2478 * Copying data out works the same way.
2479 *
2480 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2481 * automatically initializes in and out iovs by decoding @cmd with
2482 * _IOC_* macros and the server is not allowed to request RETRY. This
2483 * limits ioctl data transfers to well-formed ioctls and is the forced
2484 * behavior for all FUSE servers.
2485 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002486long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2487 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002488{
Tejun Heo59efec72008-11-26 12:03:55 +01002489 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002490 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002491 struct fuse_ioctl_in inarg = {
2492 .fh = ff->fh,
2493 .cmd = cmd,
2494 .arg = arg,
2495 .flags = flags
2496 };
2497 struct fuse_ioctl_out outarg;
2498 struct fuse_req *req = NULL;
2499 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002500 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002501 struct iovec *in_iov = NULL, *out_iov = NULL;
2502 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002503 size_t in_size, out_size, transferred, c;
2504 int err, i;
2505 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002506
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002507#if BITS_PER_LONG == 32
2508 inarg.flags |= FUSE_IOCTL_32BIT;
2509#else
2510 if (flags & FUSE_IOCTL_COMPAT)
2511 inarg.flags |= FUSE_IOCTL_32BIT;
2512#endif
2513
Tejun Heo59efec72008-11-26 12:03:55 +01002514 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002515 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002516
Tejun Heo59efec72008-11-26 12:03:55 +01002517 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002518 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002519 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002520 if (!pages || !iov_page)
2521 goto out;
2522
2523 /*
2524 * If restricted, initialize IO parameters as encoded in @cmd.
2525 * RETRY from server is not allowed.
2526 */
2527 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002528 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002529
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002530 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002531 iov->iov_len = _IOC_SIZE(cmd);
2532
2533 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2534 in_iov = iov;
2535 in_iovs = 1;
2536 }
2537
2538 if (_IOC_DIR(cmd) & _IOC_READ) {
2539 out_iov = iov;
2540 out_iovs = 1;
2541 }
2542 }
2543
2544 retry:
2545 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2546 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2547
2548 /*
2549 * Out data can be used either for actual out data or iovs,
2550 * make sure there always is at least one page.
2551 */
2552 out_size = max_t(size_t, out_size, PAGE_SIZE);
2553 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2554
2555 /* make sure there are enough buffer pages and init request with them */
2556 err = -ENOMEM;
2557 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2558 goto out;
2559 while (num_pages < max_pages) {
2560 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2561 if (!pages[num_pages])
2562 goto out;
2563 num_pages++;
2564 }
2565
Maxim Patlasov54b96672012-10-26 19:49:13 +04002566 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002567 if (IS_ERR(req)) {
2568 err = PTR_ERR(req);
2569 req = NULL;
2570 goto out;
2571 }
2572 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2573 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002574 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002575
2576 /* okay, let's send it to the client */
2577 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002578 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002579 req->in.numargs = 1;
2580 req->in.args[0].size = sizeof(inarg);
2581 req->in.args[0].value = &inarg;
2582 if (in_size) {
2583 req->in.numargs++;
2584 req->in.args[1].size = in_size;
2585 req->in.argpages = 1;
2586
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002587 err = -EFAULT;
2588 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2589 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2590 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2591 if (c != PAGE_SIZE && iov_iter_count(&ii))
2592 goto out;
2593 }
Tejun Heo59efec72008-11-26 12:03:55 +01002594 }
2595
2596 req->out.numargs = 2;
2597 req->out.args[0].size = sizeof(outarg);
2598 req->out.args[0].value = &outarg;
2599 req->out.args[1].size = out_size;
2600 req->out.argpages = 1;
2601 req->out.argvar = 1;
2602
Tejun Heob93f8582008-11-26 12:03:55 +01002603 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002604 err = req->out.h.error;
2605 transferred = req->out.args[1].size;
2606 fuse_put_request(fc, req);
2607 req = NULL;
2608 if (err)
2609 goto out;
2610
2611 /* did it ask for retry? */
2612 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002613 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002614
2615 /* no retry if in restricted mode */
2616 err = -EIO;
2617 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2618 goto out;
2619
2620 in_iovs = outarg.in_iovs;
2621 out_iovs = outarg.out_iovs;
2622
2623 /*
2624 * Make sure things are in boundary, separate checks
2625 * are to protect against overflow.
2626 */
2627 err = -ENOMEM;
2628 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2629 out_iovs > FUSE_IOCTL_MAX_IOV ||
2630 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2631 goto out;
2632
Cong Wang2408f6e2011-11-25 23:14:30 +08002633 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002634 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002635 transferred, in_iovs + out_iovs,
2636 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002637 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002638 if (err)
2639 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002640
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002641 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002642 out_iov = in_iov + in_iovs;
2643
Miklos Szeredi75727772010-11-30 16:39:27 +01002644 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2645 if (err)
2646 goto out;
2647
2648 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2649 if (err)
2650 goto out;
2651
Tejun Heo59efec72008-11-26 12:03:55 +01002652 goto retry;
2653 }
2654
2655 err = -EIO;
2656 if (transferred > inarg.out_size)
2657 goto out;
2658
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002659 err = -EFAULT;
2660 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2661 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2662 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2663 if (c != PAGE_SIZE && iov_iter_count(&ii))
2664 goto out;
2665 }
2666 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002667 out:
2668 if (req)
2669 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002670 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002671 while (num_pages)
2672 __free_page(pages[--num_pages]);
2673 kfree(pages);
2674
2675 return err ? err : outarg.result;
2676}
Tejun Heo08cbf542009-04-14 10:54:53 +09002677EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002678
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002679long fuse_ioctl_common(struct file *file, unsigned int cmd,
2680 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002681{
Al Viro6131ffa2013-02-27 16:59:05 -05002682 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002683 struct fuse_conn *fc = get_fuse_conn(inode);
2684
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002685 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002686 return -EACCES;
2687
2688 if (is_bad_inode(inode))
2689 return -EIO;
2690
2691 return fuse_do_ioctl(file, cmd, arg, flags);
2692}
2693
Tejun Heo59efec72008-11-26 12:03:55 +01002694static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2695 unsigned long arg)
2696{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002697 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002698}
2699
2700static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2701 unsigned long arg)
2702{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002703 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002704}
2705
Tejun Heo95668a62008-11-26 12:03:55 +01002706/*
2707 * All files which have been polled are linked to RB tree
2708 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2709 * find the matching one.
2710 */
2711static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2712 struct rb_node **parent_out)
2713{
2714 struct rb_node **link = &fc->polled_files.rb_node;
2715 struct rb_node *last = NULL;
2716
2717 while (*link) {
2718 struct fuse_file *ff;
2719
2720 last = *link;
2721 ff = rb_entry(last, struct fuse_file, polled_node);
2722
2723 if (kh < ff->kh)
2724 link = &last->rb_left;
2725 else if (kh > ff->kh)
2726 link = &last->rb_right;
2727 else
2728 return link;
2729 }
2730
2731 if (parent_out)
2732 *parent_out = last;
2733 return link;
2734}
2735
2736/*
2737 * The file is about to be polled. Make sure it's on the polled_files
2738 * RB tree. Note that files once added to the polled_files tree are
2739 * not removed before the file is released. This is because a file
2740 * polled once is likely to be polled again.
2741 */
2742static void fuse_register_polled_file(struct fuse_conn *fc,
2743 struct fuse_file *ff)
2744{
2745 spin_lock(&fc->lock);
2746 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002747 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002748
2749 link = fuse_find_polled_node(fc, ff->kh, &parent);
2750 BUG_ON(*link);
2751 rb_link_node(&ff->polled_node, parent, link);
2752 rb_insert_color(&ff->polled_node, &fc->polled_files);
2753 }
2754 spin_unlock(&fc->lock);
2755}
2756
Al Viro076ccb72017-07-03 01:02:18 -04002757__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002758{
Tejun Heo95668a62008-11-26 12:03:55 +01002759 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002760 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002761 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2762 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002763 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002764 int err;
2765
2766 if (fc->no_poll)
2767 return DEFAULT_POLLMASK;
2768
2769 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002770 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002771
2772 /*
2773 * Ask for notification iff there's someone waiting for it.
2774 * The client may ignore the flag and always notify.
2775 */
2776 if (waitqueue_active(&ff->poll_wait)) {
2777 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2778 fuse_register_polled_file(fc, ff);
2779 }
2780
Miklos Szeredi70781872014-12-12 09:49:05 +01002781 args.in.h.opcode = FUSE_POLL;
2782 args.in.h.nodeid = ff->nodeid;
2783 args.in.numargs = 1;
2784 args.in.args[0].size = sizeof(inarg);
2785 args.in.args[0].value = &inarg;
2786 args.out.numargs = 1;
2787 args.out.args[0].size = sizeof(outarg);
2788 args.out.args[0].value = &outarg;
2789 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002790
2791 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002792 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002793 if (err == -ENOSYS) {
2794 fc->no_poll = 1;
2795 return DEFAULT_POLLMASK;
2796 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002797 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002798}
Tejun Heo08cbf542009-04-14 10:54:53 +09002799EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002800
2801/*
2802 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2803 * wakes up the poll waiters.
2804 */
2805int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2806 struct fuse_notify_poll_wakeup_out *outarg)
2807{
2808 u64 kh = outarg->kh;
2809 struct rb_node **link;
2810
2811 spin_lock(&fc->lock);
2812
2813 link = fuse_find_polled_node(fc, kh, NULL);
2814 if (*link) {
2815 struct fuse_file *ff;
2816
2817 ff = rb_entry(*link, struct fuse_file, polled_node);
2818 wake_up_interruptible_sync(&ff->poll_wait);
2819 }
2820
2821 spin_unlock(&fc->lock);
2822 return 0;
2823}
2824
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002825static void fuse_do_truncate(struct file *file)
2826{
2827 struct inode *inode = file->f_mapping->host;
2828 struct iattr attr;
2829
2830 attr.ia_valid = ATTR_SIZE;
2831 attr.ia_size = i_size_read(inode);
2832
2833 attr.ia_file = file;
2834 attr.ia_valid |= ATTR_FILE;
2835
Jan Kara62490332016-05-26 17:12:41 +02002836 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002837}
2838
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002839static inline loff_t fuse_round_up(loff_t off)
2840{
2841 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2842}
2843
Anand Avati4273b792012-02-17 12:46:25 -05002844static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002845fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002846{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002847 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002848 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002849 struct file *file = iocb->ki_filp;
2850 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002851 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002852 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002853 struct inode *inode;
2854 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002855 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002856 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002857 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002858
Anand Avati4273b792012-02-17 12:46:25 -05002859 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002860 inode = file->f_mapping->host;
2861 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002862
Omar Sandoval6f673762015-03-16 04:33:52 -07002863 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002864 return 0;
2865
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002866 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002867 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002868 if (offset >= i_size)
2869 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002870 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2871 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002872 }
2873
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002874 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002875 if (!io)
2876 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002877 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002878 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002879 io->reqs = 1;
2880 io->bytes = -1;
2881 io->size = 0;
2882 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002883 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002884 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002885 /*
2886 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002887 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002888 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002889 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302891 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002892
2893 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302894 * We cannot asynchronously extend the size of a file.
2895 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002896 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302897 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2898 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002899
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302900 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002901 /*
2902 * Additional reference to keep io around after
2903 * calling fuse_aio_complete()
2904 */
2905 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002906 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002907 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002908
Omar Sandoval6f673762015-03-16 04:33:52 -07002909 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002910 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002911 fuse_invalidate_attr(inode);
2912 } else {
Al Virod22a9432014-03-16 15:50:47 -04002913 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002914 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002915
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002916 if (io->async) {
Lukas Czernerc0f52982018-11-09 14:51:46 +01002917 bool blocking = io->blocking;
2918
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002919 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2920
2921 /* we have a non-extending, async request, so return */
Lukas Czernerc0f52982018-11-09 14:51:46 +01002922 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002923 return -EIOCBQUEUED;
2924
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002925 wait_for_completion(&wait);
2926 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002927 }
2928
Seth Forshee744742d2016-03-11 10:35:34 -06002929 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002930
Omar Sandoval6f673762015-03-16 04:33:52 -07002931 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002932 if (ret > 0)
2933 fuse_write_update_size(inode, pos);
2934 else if (ret < 0 && offset + count > i_size)
2935 fuse_do_truncate(file);
2936 }
Anand Avati4273b792012-02-17 12:46:25 -05002937
2938 return ret;
2939}
2940
Miklos Szeredicdadb112012-11-10 16:55:56 +01002941static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2942 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002943{
2944 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002945 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002946 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002947 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002948 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002949 struct fuse_fallocate_in inarg = {
2950 .fh = ff->fh,
2951 .offset = offset,
2952 .length = length,
2953 .mode = mode
2954 };
2955 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002956 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2957 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002958
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002959 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2960 return -EOPNOTSUPP;
2961
Miklos Szeredi519c6042012-04-26 10:56:36 +02002962 if (fc->no_fallocate)
2963 return -EOPNOTSUPP;
2964
Maxim Patlasov14c14412013-06-13 12:16:39 +04002965 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002966 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002967 if (mode & FALLOC_FL_PUNCH_HOLE) {
2968 loff_t endbyte = offset + length - 1;
2969 err = filemap_write_and_wait_range(inode->i_mapping,
2970 offset, endbyte);
2971 if (err)
2972 goto out;
2973
2974 fuse_sync_writes(inode);
2975 }
Brian Foster3634a632013-05-17 09:30:32 -04002976 }
2977
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002978 if (!(mode & FALLOC_FL_KEEP_SIZE))
2979 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2980
Miklos Szeredi70781872014-12-12 09:49:05 +01002981 args.in.h.opcode = FUSE_FALLOCATE;
2982 args.in.h.nodeid = ff->nodeid;
2983 args.in.numargs = 1;
2984 args.in.args[0].size = sizeof(inarg);
2985 args.in.args[0].value = &inarg;
2986 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002987 if (err == -ENOSYS) {
2988 fc->no_fallocate = 1;
2989 err = -EOPNOTSUPP;
2990 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002991 if (err)
2992 goto out;
2993
2994 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002995 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2996 bool changed = fuse_write_update_size(inode, offset + length);
2997
Miklos Szeredi93d22692014-04-28 14:19:22 +02002998 if (changed && fc->writeback_cache)
2999 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003000 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003001
3002 if (mode & FALLOC_FL_PUNCH_HOLE)
3003 truncate_pagecache_range(inode, offset, offset + length - 1);
3004
3005 fuse_invalidate_attr(inode);
3006
Brian Foster3634a632013-05-17 09:30:32 -04003007out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003008 if (!(mode & FALLOC_FL_KEEP_SIZE))
3009 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3010
Maxim Patlasovbde52782013-09-13 19:19:54 +04003011 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003012 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003013
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003014 return err;
3015}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003016
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003017static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003018 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003019 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003020 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003021 .mmap = fuse_file_mmap,
3022 .open = fuse_open,
3023 .flush = fuse_flush,
3024 .release = fuse_release,
3025 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003026 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003027 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003028 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003029 .unlocked_ioctl = fuse_file_ioctl,
3030 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003031 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003032 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003033};
3034
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003035static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003036 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003037 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003038 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003039 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003040 .open = fuse_open,
3041 .flush = fuse_flush,
3042 .release = fuse_release,
3043 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003044 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003045 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003046 .unlocked_ioctl = fuse_file_ioctl,
3047 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003048 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003049 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003050 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003051};
3052
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003053static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003054 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003055 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003056 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003057 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003058 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003059 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003060 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003061 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003062 .write_begin = fuse_write_begin,
3063 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003064};
3065
3066void fuse_init_file_inode(struct inode *inode)
3067{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003068 inode->i_fop = &fuse_file_operations;
3069 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003070}