blob: b52f9baaa3e7b9c98478a8c115748ae71fb7b0e1 [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);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020062 mutex_init(&ff->readdir.lock);
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020063 refcount_set(&ff->count, 1);
Tejun Heo6b2db282009-04-14 10:54:49 +090064 RB_CLEAR_NODE(&ff->polled_node);
65 init_waitqueue_head(&ff->poll_wait);
66
67 spin_lock(&fc->lock);
68 ff->kh = ++fc->khctr;
69 spin_unlock(&fc->lock);
70
Miklos Szeredifd72faa2005-11-07 00:59:51 -080071 return ff;
72}
73
74void fuse_file_free(struct fuse_file *ff)
75{
Miklos Szeredi33649c92006-06-25 05:48:52 -070076 fuse_request_free(ff->reserved_req);
Miklos Szeredi5d7bc7e2018-10-01 10:07:04 +020077 mutex_destroy(&ff->readdir.lock);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080078 kfree(ff);
79}
80
Miklos Szeredi267d8442017-02-22 20:08:25 +010081static struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070082{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020083 refcount_inc(&ff->count);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070084 return ff;
85}
86
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010087static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
88{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010089 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010090}
91
92static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070093{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +020094 if (refcount_dec_and_test(&ff->count)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -070095 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020096
Andrew Gallagher7678ac52013-11-05 16:05:52 +010097 if (ff->fc->no_open) {
98 /*
99 * Drop the release request when client does not
100 * implement 'open'
101 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200102 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100103 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100104 fuse_put_request(ff->fc, req);
105 } else if (sync) {
Miklos Szeredi2e38bea2017-02-22 20:08:25 +0100106 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200107 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100108 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100109 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100110 fuse_put_request(ff->fc, req);
111 } else {
112 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200113 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100114 fuse_request_send_background(ff->fc, req);
115 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700116 kfree(ff);
117 }
118}
119
Tejun Heo08cbf542009-04-14 10:54:53 +0900120int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
121 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200122{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200123 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200124 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
125
126 ff = fuse_file_alloc(fc);
127 if (!ff)
128 return -ENOMEM;
129
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 ff->fh = 0;
131 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
132 if (!fc->no_open || isdir) {
133 struct fuse_open_out outarg;
134 int err;
135
136 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
137 if (!err) {
138 ff->fh = outarg.fh;
139 ff->open_flags = outarg.open_flags;
140
141 } else if (err != -ENOSYS || isdir) {
142 fuse_file_free(ff);
143 return err;
144 } else {
145 fc->no_open = 1;
146 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200147 }
148
149 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100150 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200151
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200152 ff->nodeid = nodeid;
Miklos Szeredi267d8442017-02-22 20:08:25 +0100153 file->private_data = ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154
155 return 0;
156}
Tejun Heo08cbf542009-04-14 10:54:53 +0900157EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200158
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400159static void fuse_link_write_file(struct file *file)
160{
161 struct inode *inode = file_inode(file);
162 struct fuse_conn *fc = get_fuse_conn(inode);
163 struct fuse_inode *fi = get_fuse_inode(inode);
164 struct fuse_file *ff = file->private_data;
165 /*
166 * file may be written through mmap, so chain it onto the
167 * inodes's write_file list
168 */
169 spin_lock(&fc->lock);
170 if (list_empty(&ff->write_entry))
171 list_add(&ff->write_entry, &fi->write_files);
172 spin_unlock(&fc->lock);
173}
174
Miklos Szeredic7b71432009-04-28 16:56:37 +0200175void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800176{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200177 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800178 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179
180 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800181 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200182 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700183 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200184 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200185 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800186 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
187 struct fuse_inode *fi = get_fuse_inode(inode);
188
189 spin_lock(&fc->lock);
190 fi->attr_version = ++fc->attr_version;
191 i_size_write(inode, 0);
192 spin_unlock(&fc->lock);
193 fuse_invalidate_attr(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200194 if (fc->writeback_cache)
195 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800196 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400197 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
198 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800199}
200
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200201int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800202{
Tejun Heoacf99432008-11-26 12:03:55 +0100203 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204 int err;
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200205 bool lock_inode = (file->f_flags & O_TRUNC) &&
206 fc->atomic_o_trunc &&
207 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700208
209 err = generic_file_open(inode, file);
210 if (err)
211 return err;
212
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200213 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500214 inode_lock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200215
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200216 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700217
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200218 if (!err)
219 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200220
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200221 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500222 inode_unlock(inode);
Maxim Patlasov75caeecd2014-04-28 14:19:22 +0200223
224 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700225}
226
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200227static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800228{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200229 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700230 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800231 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700232
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200233 spin_lock(&fc->lock);
234 list_del(&ff->write_entry);
235 if (!RB_EMPTY_NODE(&ff->polled_node))
236 rb_erase(&ff->polled_node, &fc->polled_files);
237 spin_unlock(&fc->lock);
238
Bryan Green357ccf22011-03-01 16:43:52 -0800239 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200240
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700241 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800242 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700243 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200244 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700245 req->in.numargs = 1;
246 req->in.args[0].size = sizeof(struct fuse_release_in);
247 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800248}
249
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200250void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800251{
Miklos Szeredi9a87ad32017-02-22 20:08:25 +0100252 struct fuse_file *ff = file->private_data;
253 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700254
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200255 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100256
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200257 if (ff->flock) {
258 struct fuse_release_in *inarg = &req->misc.release.in;
259 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
260 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
261 (fl_owner_t) file);
262 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100263 /* Hold inode until release is finished */
264 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700265
Tejun Heo6b2db282009-04-14 10:54:49 +0900266 /*
267 * Normally this will send the RELEASE request, however if
268 * some asynchronous READ or WRITE requests are outstanding,
269 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100270 *
271 * Make the release synchronous if this is a fuseblk mount,
272 * synchronous RELEASE is allowed (and desirable) in this case
273 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900274 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100275 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700276}
277
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700278static int fuse_open(struct inode *inode, struct file *file)
279{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200280 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700281}
282
283static int fuse_release(struct inode *inode, struct file *file)
284{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400285 struct fuse_conn *fc = get_fuse_conn(inode);
286
287 /* see fuse_vma_close() for !writeback_cache case */
288 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200289 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400290
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200291 fuse_release_common(file, FUSE_RELEASE);
292
293 /* return value is ignored by VFS */
294 return 0;
295}
296
297void fuse_sync_release(struct fuse_file *ff, int flags)
298{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200299 WARN_ON(refcount_read(&ff->count) > 1);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200300 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100301 /*
302 * iput(NULL) is a no-op and since the refcount is 1 and everything's
303 * synchronous, we are fine with not doing igrab() here"
304 */
305 fuse_file_put(ff, true);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700306}
Tejun Heo08cbf542009-04-14 10:54:53 +0900307EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700308
Miklos Szeredi71421252006-06-25 05:48:52 -0700309/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700310 * Scramble the ID space with XTEA, so that the value of the files_struct
311 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700312 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700313u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700314{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700315 u32 *k = fc->scramble_key;
316 u64 v = (unsigned long) id;
317 u32 v0 = v;
318 u32 v1 = v >> 32;
319 u32 sum = 0;
320 int i;
321
322 for (i = 0; i < 32; i++) {
323 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
324 sum += 0x9E3779B9;
325 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
326 }
327
328 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700329}
330
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700331/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400332 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700333 *
334 * This is currently done by walking the list of writepage requests
335 * for the inode, which can be pretty inefficient.
336 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400337static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
338 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700339{
340 struct fuse_conn *fc = get_fuse_conn(inode);
341 struct fuse_inode *fi = get_fuse_inode(inode);
342 struct fuse_req *req;
343 bool found = false;
344
345 spin_lock(&fc->lock);
346 list_for_each_entry(req, &fi->writepages, writepages_entry) {
347 pgoff_t curr_index;
348
349 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300350 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400351 if (idx_from < curr_index + req->num_pages &&
352 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700353 found = true;
354 break;
355 }
356 }
357 spin_unlock(&fc->lock);
358
359 return found;
360}
361
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400362static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
363{
364 return fuse_range_is_writeback(inode, index, index);
365}
366
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700367/*
368 * Wait for page writeback to be completed.
369 *
370 * Since fuse doesn't rely on the VM writeback tracking, this has to
371 * use some other means.
372 */
373static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
374{
375 struct fuse_inode *fi = get_fuse_inode(inode);
376
377 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
378 return 0;
379}
380
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400381/*
382 * Wait for all pending writepages on the inode to finish.
383 *
384 * This is currently done by blocking further writes with FUSE_NOWRITE
385 * and waiting for all sent writes to complete.
386 *
387 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
388 * could conflict with truncation.
389 */
390static void fuse_sync_writes(struct inode *inode)
391{
392 fuse_set_nowrite(inode);
393 fuse_release_nowrite(inode);
394}
395
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700396static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700397{
Al Viro6131ffa2013-02-27 16:59:05 -0500398 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700399 struct fuse_conn *fc = get_fuse_conn(inode);
400 struct fuse_file *ff = file->private_data;
401 struct fuse_req *req;
402 struct fuse_flush_in inarg;
403 int err;
404
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800405 if (is_bad_inode(inode))
406 return -EIO;
407
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700408 if (fc->no_flush)
409 return 0;
410
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200411 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400412 if (err)
413 return err;
414
Al Viro59551022016-01-22 15:40:57 -0500415 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400416 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500417 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400418
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200419 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700420 if (err)
421 return err;
422
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400423 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700424 memset(&inarg, 0, sizeof(inarg));
425 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700426 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 req->in.h.opcode = FUSE_FLUSH;
428 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700429 req->in.numargs = 1;
430 req->in.args[0].size = sizeof(inarg);
431 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200432 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100433 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 err = req->out.h.error;
435 fuse_put_request(fc, req);
436 if (err == -ENOSYS) {
437 fc->no_flush = 1;
438 err = 0;
439 }
440 return err;
441}
442
Josef Bacik02c24a82011-07-16 20:44:56 -0400443int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
444 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700445{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200446 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 struct fuse_conn *fc = get_fuse_conn(inode);
448 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100449 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700450 struct fuse_fsync_in inarg;
451 int err;
452
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800453 if (is_bad_inode(inode))
454 return -EIO;
455
Al Viro59551022016-01-22 15:40:57 -0500456 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400457
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700458 /*
459 * Start writeback against all dirty pages of the inode, then
460 * wait for all outstanding writes, before sending the FSYNC
461 * request.
462 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400463 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700464 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400465 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700466
467 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700468
469 /*
470 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400471 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700472 * We have to do this directly after fuse_sync_writes()
473 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400474 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700475 if (err)
476 goto out;
477
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200478 err = sync_inode_metadata(inode, 1);
479 if (err)
480 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700481
Miklos Szeredi22401e72014-04-28 14:19:23 +0200482 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
483 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400484
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700485 memset(&inarg, 0, sizeof(inarg));
486 inarg.fh = ff->fh;
487 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100488 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
489 args.in.h.nodeid = get_node_id(inode);
490 args.in.numargs = 1;
491 args.in.args[0].size = sizeof(inarg);
492 args.in.args[0].value = &inarg;
493 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700494 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700495 if (isdir)
496 fc->no_fsyncdir = 1;
497 else
498 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700499 err = 0;
500 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400501out:
Al Viro59551022016-01-22 15:40:57 -0500502 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700503 return err;
504}
505
Josef Bacik02c24a82011-07-16 20:44:56 -0400506static int fuse_fsync(struct file *file, loff_t start, loff_t end,
507 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700508{
Josef Bacik02c24a82011-07-16 20:44:56 -0400509 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700510}
511
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200512void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
513 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700515 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800516 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700517
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800518 inarg->fh = ff->fh;
519 inarg->offset = pos;
520 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800521 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800522 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200523 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700524 req->in.numargs = 1;
525 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800526 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700527 req->out.argvar = 1;
528 req->out.numargs = 1;
529 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700530}
531
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200532static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400533{
534 unsigned i;
535
536 for (i = 0; i < req->num_pages; i++) {
537 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200538 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400539 set_page_dirty_lock(page);
540 put_page(page);
541 }
542}
543
Seth Forshee744742d2016-03-11 10:35:34 -0600544static void fuse_io_release(struct kref *kref)
545{
546 kfree(container_of(kref, struct fuse_io_priv, refcnt));
547}
548
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100549static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
550{
551 if (io->err)
552 return io->err;
553
554 if (io->bytes >= 0 && io->write)
555 return -EIO;
556
557 return io->bytes < 0 ? io->size : io->bytes;
558}
559
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400560/**
561 * In case of short read, the caller sets 'pos' to the position of
562 * actual end of fuse request in IO request. Otherwise, if bytes_requested
563 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
564 *
565 * An example:
566 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
567 * both submitted asynchronously. The first of them was ACKed by userspace as
568 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
569 * second request was ACKed as short, e.g. only 1K was read, resulting in
570 * pos == 33K.
571 *
572 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
573 * will be equal to the length of the longest contiguous fragment of
574 * transferred data starting from the beginning of IO request.
575 */
576static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
577{
578 int left;
579
580 spin_lock(&io->lock);
581 if (err)
582 io->err = io->err ? : err;
583 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
584 io->bytes = pos;
585
586 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530587 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100588 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400589 spin_unlock(&io->lock);
590
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530591 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100592 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400593
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100594 if (res >= 0) {
595 struct inode *inode = file_inode(io->iocb->ki_filp);
596 struct fuse_conn *fc = get_fuse_conn(inode);
597 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400598
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100599 spin_lock(&fc->lock);
600 fi->attr_version = ++fc->attr_version;
601 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400602 }
603
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100604 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400605 }
Seth Forshee744742d2016-03-11 10:35:34 -0600606
607 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400608}
609
610static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
611{
612 struct fuse_io_priv *io = req->io;
613 ssize_t pos = -1;
614
Ashish Samant61c12b42017-07-12 19:26:58 -0700615 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400616
617 if (io->write) {
618 if (req->misc.write.in.size != req->misc.write.out.size)
619 pos = req->misc.write.in.offset - io->offset +
620 req->misc.write.out.size;
621 } else {
622 if (req->misc.read.in.size != req->out.args[0].size)
623 pos = req->misc.read.in.offset - io->offset +
624 req->out.args[0].size;
625 }
626
627 fuse_aio_complete(io, req->out.h.error, pos);
628}
629
630static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
631 size_t num_bytes, struct fuse_io_priv *io)
632{
633 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600634 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400635 io->size += num_bytes;
636 io->reqs++;
637 spin_unlock(&io->lock);
638
639 req->io = io;
640 req->end = fuse_aio_complete_req;
641
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400642 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400643 fuse_request_send_background(fc, req);
644
645 return num_bytes;
646}
647
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400648static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200649 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700650{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200651 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200652 struct fuse_file *ff = file->private_data;
653 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700654
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200655 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700656 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700657 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700658
659 inarg->read_flags |= FUSE_READ_LOCKOWNER;
660 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
661 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400662
663 if (io->async)
664 return fuse_async_req_send(fc, req, count, io);
665
Tejun Heob93f8582008-11-26 12:03:55 +0100666 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800667 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700668}
669
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700670static void fuse_read_update_size(struct inode *inode, loff_t size,
671 u64 attr_ver)
672{
673 struct fuse_conn *fc = get_fuse_conn(inode);
674 struct fuse_inode *fi = get_fuse_inode(inode);
675
676 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400677 if (attr_ver == fi->attr_version && size < inode->i_size &&
678 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700679 fi->attr_version = ++fc->attr_version;
680 i_size_write(inode, size);
681 }
682 spin_unlock(&fc->lock);
683}
684
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400685static void fuse_short_read(struct fuse_req *req, struct inode *inode,
686 u64 attr_ver)
687{
688 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400689 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400690
Pavel Emelyanov83732002013-10-10 17:10:46 +0400691 if (fc->writeback_cache) {
692 /*
693 * A hole in a file. Some data after the hole are in page cache,
694 * but have not reached the client fs yet. So, the hole is not
695 * present there.
696 */
697 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300698 int start_idx = num_read >> PAGE_SHIFT;
699 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400700
701 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300702 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400703 off = 0;
704 }
705 } else {
706 loff_t pos = page_offset(req->pages[0]) + num_read;
707 fuse_read_update_size(inode, pos, attr_ver);
708 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400709}
710
Maxim Patlasov482fce52013-10-10 17:11:25 +0400711static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700712{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200713 struct kiocb iocb;
714 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700715 struct inode *inode = page->mapping->host;
716 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800717 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700718 size_t num_read;
719 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300720 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700721 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800722 int err;
723
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700724 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300725 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700726 * page-cache page, so make sure we read a properly synced
727 * page.
728 */
729 fuse_wait_on_page_writeback(inode, page->index);
730
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400731 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700732 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400733 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700735 attr_ver = fuse_get_attr_version(fc);
736
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200738 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700739 req->num_pages = 1;
740 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400741 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200742 init_sync_kiocb(&iocb, file);
743 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400744 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746
747 if (!err) {
748 /*
749 * Short read means EOF. If file size is larger, truncate it
750 */
751 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400752 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700753
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700754 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700755 }
756
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400757 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400758
759 return err;
760}
761
762static int fuse_readpage(struct file *file, struct page *page)
763{
764 struct inode *inode = page->mapping->host;
765 int err;
766
767 err = -EIO;
768 if (is_bad_inode(inode))
769 goto out;
770
771 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800772 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700773 out:
774 unlock_page(page);
775 return err;
776}
777
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800778static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700779{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800780 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700781 size_t count = req->misc.read.in.size;
782 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200783 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800784
Miklos Szeredice534fb2010-05-25 15:06:07 +0200785 for (i = 0; mapping == NULL && i < req->num_pages; i++)
786 mapping = req->pages[i]->mapping;
787
788 if (mapping) {
789 struct inode *inode = mapping->host;
790
791 /*
792 * Short read means EOF. If file size is larger, truncate it
793 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400794 if (!req->out.h.error && num_read < count)
795 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200796
Andrew Gallagher451418f2013-11-05 03:55:43 -0800797 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700798 }
799
Miklos Szeredidb50b962005-09-09 13:10:33 -0700800 for (i = 0; i < req->num_pages; i++) {
801 struct page *page = req->pages[i];
802 if (!req->out.h.error)
803 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800804 else
805 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700806 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300807 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700808 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700809 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100810 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800811}
812
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200813static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800814{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200815 struct fuse_file *ff = file->private_data;
816 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800817 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300818 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200819
820 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800821 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200822 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200823 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700824 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800825 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700826 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800827 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100828 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800829 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100830 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800831 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100832 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800833 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700834}
835
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700836struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700837 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800838 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700839 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400840 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700841};
842
843static int fuse_readpages_fill(void *_data, struct page *page)
844{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700845 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700846 struct fuse_req *req = data->req;
847 struct inode *inode = data->inode;
848 struct fuse_conn *fc = get_fuse_conn(inode);
849
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700850 fuse_wait_on_page_writeback(inode, page->index);
851
Miklos Szeredidb50b962005-09-09 13:10:33 -0700852 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300853 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300854 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700855 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300856 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
857 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200858 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400859 if (fc->async_read)
860 req = fuse_get_req_for_background(fc, nr_alloc);
861 else
862 req = fuse_get_req(fc, nr_alloc);
863
864 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700865 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700866 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700867 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400870
871 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300872 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400873 fuse_put_request(fc, req);
874 return -EIO;
875 }
876
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300877 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400879 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100880 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400881 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700882 return 0;
883}
884
885static int fuse_readpages(struct file *file, struct address_space *mapping,
886 struct list_head *pages, unsigned nr_pages)
887{
888 struct inode *inode = mapping->host;
889 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700890 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700891 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300892 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800893
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700894 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800896 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800897
Miklos Szeredia6643092007-11-28 16:22:00 -0800898 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400900 if (fc->async_read)
901 data.req = fuse_get_req_for_background(fc, nr_alloc);
902 else
903 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400904 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700905 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700906 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800907 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700908
909 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700910 if (!err) {
911 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200912 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700913 else
914 fuse_put_request(fc, data.req);
915 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800916out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700917 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700918}
919
Al Viro37c20f12014-04-02 14:47:09 -0400920static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800921{
922 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400923 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800924
Brian Fostera8894272012-07-16 15:23:50 -0400925 /*
926 * In auto invalidate mode, always update attributes on read.
927 * Otherwise, only update if we attempt to read past EOF (to ensure
928 * i_size is up to date).
929 */
930 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400931 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200933 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800934 if (err)
935 return err;
936 }
937
Al Viro37c20f12014-04-02 14:47:09 -0400938 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800939}
940
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200941static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200942 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700943{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700944 struct fuse_write_in *inarg = &req->misc.write.in;
945 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700946
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700947 inarg->fh = ff->fh;
948 inarg->offset = pos;
949 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700950 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700952 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200953 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700954 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
955 else
956 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700957 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700958 req->in.args[1].size = count;
959 req->out.numargs = 1;
960 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700961 req->out.args[0].value = outarg;
962}
963
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400964static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200965 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700966{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200967 struct kiocb *iocb = io->iocb;
968 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200969 struct fuse_file *ff = file->private_data;
970 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971 struct fuse_write_in *inarg = &req->misc.write.in;
972
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200973 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200974 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200975 if (iocb->ki_flags & IOCB_DSYNC)
976 inarg->flags |= O_DSYNC;
977 if (iocb->ki_flags & IOCB_SYNC)
978 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700979 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700980 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
981 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
982 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400983
984 if (io->async)
985 return fuse_async_req_send(fc, req, count, io);
986
Tejun Heob93f8582008-11-26 12:03:55 +0100987 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700988 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700989}
990
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400991bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700992{
993 struct fuse_conn *fc = get_fuse_conn(inode);
994 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400995 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996
997 spin_lock(&fc->lock);
998 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400999 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001001 ret = true;
1002 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001003 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001004
1005 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001006}
1007
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001008static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001009 struct inode *inode, loff_t pos,
1010 size_t count)
1011{
1012 size_t res;
1013 unsigned offset;
1014 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001015 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001016
1017 for (i = 0; i < req->num_pages; i++)
1018 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1019
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001020 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001021
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001022 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001023 count = res;
1024 for (i = 0; i < req->num_pages; i++) {
1025 struct page *page = req->pages[i];
1026
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001028 SetPageUptodate(page);
1029
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001030 if (count > PAGE_SIZE - offset)
1031 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001032 else
1033 count = 0;
1034 offset = 0;
1035
1036 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001037 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001038 }
1039
1040 return res;
1041}
1042
1043static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1044 struct address_space *mapping,
1045 struct iov_iter *ii, loff_t pos)
1046{
1047 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001048 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001049 size_t count = 0;
1050 int err;
1051
Miklos Szeredif4975c62009-04-02 14:25:34 +02001052 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001053 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001054
1055 do {
1056 size_t tmp;
1057 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001058 pgoff_t index = pos >> PAGE_SHIFT;
1059 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001060 iov_iter_count(ii));
1061
1062 bytes = min_t(size_t, bytes, fc->max_write - count);
1063
1064 again:
1065 err = -EFAULT;
1066 if (iov_iter_fault_in_readable(ii, bytes))
1067 break;
1068
1069 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001070 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001071 if (!page)
1072 break;
1073
anfei zhou931e80e2010-02-02 13:44:02 -08001074 if (mapping_writably_mapped(mapping))
1075 flush_dcache_page(page);
1076
Nick Pigginea9b9902008-04-30 00:54:42 -07001077 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001078 flush_dcache_page(page);
1079
Roman Gushchin3ca81382015-10-12 16:33:44 +03001080 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 if (!tmp) {
1082 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001083 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 bytes = min(bytes, iov_iter_single_seg_count(ii));
1085 goto again;
1086 }
1087
1088 err = 0;
1089 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001090 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001091 req->num_pages++;
1092
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 count += tmp;
1094 pos += tmp;
1095 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001096 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001097 offset = 0;
1098
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001099 if (!fc->big_writes)
1100 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001101 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001102 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001103
1104 return count > 0 ? count : err;
1105}
1106
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001107static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1108 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001109{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001110 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001111 ((pos + len - 1) >> PAGE_SHIFT) -
1112 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001113 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001114}
1115
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001116static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001117 struct address_space *mapping,
1118 struct iov_iter *ii, loff_t pos)
1119{
1120 struct inode *inode = mapping->host;
1121 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001122 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001123 int err = 0;
1124 ssize_t res = 0;
1125
1126 if (is_bad_inode(inode))
1127 return -EIO;
1128
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001129 if (inode->i_size < pos + iov_iter_count(ii))
1130 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1131
Nick Pigginea9b9902008-04-30 00:54:42 -07001132 do {
1133 struct fuse_req *req;
1134 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001135 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1136 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001137
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001138 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001139 if (IS_ERR(req)) {
1140 err = PTR_ERR(req);
1141 break;
1142 }
1143
1144 count = fuse_fill_write_pages(req, mapping, ii, pos);
1145 if (count <= 0) {
1146 err = count;
1147 } else {
1148 size_t num_written;
1149
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001150 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001151 pos, count);
1152 err = req->out.h.error;
1153 if (!err) {
1154 res += num_written;
1155 pos += num_written;
1156
1157 /* break out of the loop on short write */
1158 if (num_written != count)
1159 err = -EIO;
1160 }
1161 }
1162 fuse_put_request(fc, req);
1163 } while (!err && iov_iter_count(ii));
1164
1165 if (res > 0)
1166 fuse_write_update_size(inode, pos);
1167
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001168 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001169 fuse_invalidate_attr(inode);
1170
1171 return res > 0 ? res : err;
1172}
1173
Al Viro84c3d552014-04-03 14:33:23 -04001174static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001175{
1176 struct file *file = iocb->ki_filp;
1177 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001178 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001179 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001180 struct inode *inode = mapping->host;
1181 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001182 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001183
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001184 if (get_fuse_conn(inode)->writeback_cache) {
1185 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001186 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001187 if (err)
1188 return err;
1189
Al Viro84c3d552014-04-03 14:33:23 -04001190 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001191 }
1192
Al Viro59551022016-01-22 15:40:57 -05001193 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001194
1195 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001196 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001197
Al Viro3309dd02015-04-09 12:55:47 -04001198 err = generic_write_checks(iocb, from);
1199 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001200 goto out;
1201
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001202 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001203 if (err)
1204 goto out;
1205
Josef Bacikc3b2da32012-03-26 09:59:21 -04001206 err = file_update_time(file);
1207 if (err)
1208 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001209
Al Viro2ba48ce2015-04-09 13:52:01 -04001210 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001211 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001212 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001213 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001214 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001215
Anand Avati4273b792012-02-17 12:46:25 -05001216 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001217
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001218 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001219 if (written_buffered < 0) {
1220 err = written_buffered;
1221 goto out;
1222 }
1223 endbyte = pos + written_buffered - 1;
1224
1225 err = filemap_write_and_wait_range(file->f_mapping, pos,
1226 endbyte);
1227 if (err)
1228 goto out;
1229
1230 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 pos >> PAGE_SHIFT,
1232 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001233
1234 written += written_buffered;
1235 iocb->ki_pos = pos + written_buffered;
1236 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001237 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001238 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001239 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001240 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001241out:
1242 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001243 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001244 if (written > 0)
1245 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001246
1247 return written ? written : err;
1248}
1249
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001250static inline void fuse_page_descs_length_init(struct fuse_req *req,
1251 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001252{
1253 int i;
1254
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001255 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001256 req->page_descs[i].length = PAGE_SIZE -
1257 req->page_descs[i].offset;
1258}
1259
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001260static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1261{
1262 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1263}
1264
1265static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1266 size_t max_size)
1267{
1268 return min(iov_iter_single_seg_count(ii), max_size);
1269}
1270
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001271static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001272 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001273{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001274 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001275 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001276
Miklos Szeredif4975c62009-04-02 14:25:34 +02001277 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001278 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001279 unsigned long user_addr = fuse_get_user_addr(ii);
1280 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1281
Miklos Szeredif4975c62009-04-02 14:25:34 +02001282 if (write)
1283 req->in.args[1].value = (void *) user_addr;
1284 else
1285 req->out.args[0].value = (void *) user_addr;
1286
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001287 iov_iter_advance(ii, frag_size);
1288 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001289 return 0;
1290 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001291
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001292 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001294 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001295 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001296 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001297 req->max_pages - req->num_pages,
1298 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001299 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001300 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001301
Al Viroc9c37e22014-03-16 16:08:30 -04001302 iov_iter_advance(ii, ret);
1303 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001304
Al Viroc9c37e22014-03-16 16:08:30 -04001305 ret += start;
1306 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1307
1308 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309 fuse_page_descs_length_init(req, req->num_pages, npages);
1310
1311 req->num_pages += npages;
1312 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001313 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001314 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001315
1316 if (write)
1317 req->in.argpages = 1;
1318 else
1319 req->out.argpages = 1;
1320
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001321 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001322
Ashish Samant2c932d42016-03-25 10:53:41 -07001323 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001324}
1325
Al Virod22a9432014-03-16 15:50:47 -04001326ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1327 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001328{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001329 int write = flags & FUSE_DIO_WRITE;
1330 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001331 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001332 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001333 struct fuse_file *ff = file->private_data;
1334 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 size_t nmax = write ? fc->max_write : fc->max_read;
1336 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001337 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001338 pgoff_t idx_from = pos >> PAGE_SHIFT;
1339 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001340 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001341 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001342 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001343
Brian Fosterde82b922013-05-14 11:25:39 -04001344 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001345 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1346 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001347 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001348 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001349 if (IS_ERR(req))
1350 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001351
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001352 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1353 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001354 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001355 fuse_sync_writes(inode);
1356 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001357 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001358 }
1359
Ashish Samant61c12b42017-07-12 19:26:58 -07001360 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001363 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001364 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001365 err = fuse_get_user_pages(req, iter, &nbytes, write);
1366 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001367 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001368
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001370 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001372 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001373
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001374 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001375 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001377 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378 break;
1379 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001380 res = 0;
1381 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001382 break;
1383 }
1384 count -= nres;
1385 res += nres;
1386 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387 if (nres != nbytes)
1388 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001389 if (count) {
1390 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001391 if (io->async)
1392 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001393 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001394 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001395 req = fuse_get_req(fc, iov_iter_npages(iter,
1396 fc->max_pages));
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
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001422 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001423
1424 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001425}
1426
Al Viro153162632015-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 Viro153162632015-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 Viro153162632015-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 Viro153162632015-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 Viro153162632015-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)
1463 fuse_file_put(req->ff, 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;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001491 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001492
1493 if (!fc->connected)
1494 goto out_free;
1495
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001496 if (inarg->offset + data_size <= size) {
1497 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001498 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001499 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001500 } else {
1501 /* Got truncated off completely */
1502 goto out_free;
1503 }
1504
1505 req->in.args[1].size = inarg->size;
1506 fi->writectr++;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001507 queued = fuse_request_queue_background(fc, req);
1508 WARN_ON(!queued);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001509 return;
1510
1511 out_free:
1512 fuse_writepage_finish(fc, req);
1513 spin_unlock(&fc->lock);
1514 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001515 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001516 spin_lock(&fc->lock);
1517}
1518
1519/*
1520 * If fi->writectr is positive (no truncate or fsync going on) send
1521 * all queued writepage requests.
1522 *
1523 * Called with fc->lock
1524 */
1525void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001526__releases(fc->lock)
1527__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001528{
1529 struct fuse_conn *fc = get_fuse_conn(inode);
1530 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001531 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001532 struct fuse_req *req;
1533
1534 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1535 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1536 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001537 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001538 }
1539}
1540
1541static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1542{
1543 struct inode *inode = req->inode;
1544 struct fuse_inode *fi = get_fuse_inode(inode);
1545
1546 mapping_set_error(inode->i_mapping, req->out.h.error);
1547 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001548 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001549 struct fuse_conn *fc = get_fuse_conn(inode);
1550 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001551 struct fuse_req *next = req->misc.write.next;
1552 req->misc.write.next = next->misc.write.next;
1553 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001554 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001555 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001556
1557 /*
1558 * Skip fuse_flush_writepages() to make it easy to crop requests
1559 * based on primary request size.
1560 *
1561 * 1st case (trivial): there are no concurrent activities using
1562 * fuse_set/release_nowrite. Then we're on safe side because
1563 * fuse_flush_writepages() would call fuse_send_writepage()
1564 * anyway.
1565 *
1566 * 2nd case: someone called fuse_set_nowrite and it is waiting
1567 * now for completion of all in-flight requests. This happens
1568 * rarely and no more than once per page, so this should be
1569 * okay.
1570 *
1571 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1572 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1573 * that fuse_set_nowrite returned implies that all in-flight
1574 * requests were completed along with all of their secondary
1575 * requests. Further primary requests are blocked by negative
1576 * writectr. Hence there cannot be any in-flight requests and
1577 * no invocations of fuse_writepage_end() while we're in
1578 * fuse_set_nowrite..fuse_release_nowrite section.
1579 */
1580 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001581 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001582 fi->writectr--;
1583 fuse_writepage_finish(fc, req);
1584 spin_unlock(&fc->lock);
1585 fuse_writepage_free(fc, req);
1586}
1587
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001588static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1589 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001590{
Miklos Szeredi72523422013-10-01 16:44:52 +02001591 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001592
1593 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001594 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001595 ff = list_entry(fi->write_files.next, struct fuse_file,
1596 write_entry);
1597 fuse_file_get(ff);
1598 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001599 spin_unlock(&fc->lock);
1600
1601 return ff;
1602}
1603
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001604static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1605 struct fuse_inode *fi)
1606{
1607 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1608 WARN_ON(!ff);
1609 return ff;
1610}
1611
1612int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1613{
1614 struct fuse_conn *fc = get_fuse_conn(inode);
1615 struct fuse_inode *fi = get_fuse_inode(inode);
1616 struct fuse_file *ff;
1617 int err;
1618
1619 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001620 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001621 if (ff)
1622 fuse_file_put(ff, 0);
1623
1624 return err;
1625}
1626
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001627static int fuse_writepage_locked(struct page *page)
1628{
1629 struct address_space *mapping = page->mapping;
1630 struct inode *inode = mapping->host;
1631 struct fuse_conn *fc = get_fuse_conn(inode);
1632 struct fuse_inode *fi = get_fuse_inode(inode);
1633 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001634 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001635 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636
1637 set_page_writeback(page);
1638
Maxim Patlasov4250c062012-10-26 19:48:07 +04001639 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640 if (!req)
1641 goto err;
1642
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001643 /* writeback always goes to bg_queue */
1644 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001645 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1646 if (!tmp_page)
1647 goto err_free;
1648
Miklos Szeredi72523422013-10-01 16:44:52 +02001649 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001650 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001651 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001652 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001653
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001654 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001655
1656 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001657 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001658 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001659 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660 req->num_pages = 1;
1661 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001662 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001663 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001664 req->end = fuse_writepage_end;
1665 req->inode = inode;
1666
Tejun Heo93f78d82015-05-22 17:13:27 -04001667 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001668 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669
1670 spin_lock(&fc->lock);
1671 list_add(&req->writepages_entry, &fi->writepages);
1672 list_add_tail(&req->list, &fi->queued_writes);
1673 fuse_flush_writepages(inode);
1674 spin_unlock(&fc->lock);
1675
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001676 end_page_writeback(page);
1677
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001678 return 0;
1679
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001680err_nofile:
1681 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001682err_free:
1683 fuse_request_free(req);
1684err:
Jeff Layton91839762017-05-25 06:57:50 -04001685 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001686 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001687 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688}
1689
1690static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1691{
1692 int err;
1693
Miklos Szerediff17be02013-10-01 16:44:53 +02001694 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1695 /*
1696 * ->writepages() should be called for sync() and friends. We
1697 * should only get here on direct reclaim and then we are
1698 * allowed to skip a page which is already in flight
1699 */
1700 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1701
1702 redirty_page_for_writepage(wbc, page);
1703 return 0;
1704 }
1705
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706 err = fuse_writepage_locked(page);
1707 unlock_page(page);
1708
1709 return err;
1710}
1711
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001712struct fuse_fill_wb_data {
1713 struct fuse_req *req;
1714 struct fuse_file *ff;
1715 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001716 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001717};
1718
1719static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1720{
1721 struct fuse_req *req = data->req;
1722 struct inode *inode = data->inode;
1723 struct fuse_conn *fc = get_fuse_conn(inode);
1724 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001725 int num_pages = req->num_pages;
1726 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001727
1728 req->ff = fuse_file_get(data->ff);
1729 spin_lock(&fc->lock);
1730 list_add_tail(&req->list, &fi->queued_writes);
1731 fuse_flush_writepages(inode);
1732 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001733
1734 for (i = 0; i < num_pages; i++)
1735 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001736}
1737
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001738static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1739 struct page *page)
1740{
1741 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1742 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1743 struct fuse_req *tmp;
1744 struct fuse_req *old_req;
1745 bool found = false;
1746 pgoff_t curr_index;
1747
1748 BUG_ON(new_req->num_pages != 0);
1749
1750 spin_lock(&fc->lock);
1751 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001752 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1753 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001754 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001755 if (curr_index <= page->index &&
1756 page->index < curr_index + old_req->num_pages) {
1757 found = true;
1758 break;
1759 }
1760 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001761 if (!found) {
1762 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001763 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001764 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001765
Maxim Patlasovf6011082013-10-02 15:01:07 +04001766 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1768 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001769 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001770 if (tmp->num_pages == 1 &&
1771 curr_index == page->index) {
1772 old_req = tmp;
1773 }
1774 }
1775
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001776 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001777 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001778
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001779 copy_highpage(old_req->pages[0], page);
1780 spin_unlock(&fc->lock);
1781
Tejun Heo93f78d82015-05-22 17:13:27 -04001782 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001783 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001784 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001785 fuse_writepage_free(fc, new_req);
1786 fuse_request_free(new_req);
1787 goto out;
1788 } else {
1789 new_req->misc.write.next = old_req->misc.write.next;
1790 old_req->misc.write.next = new_req;
1791 }
1792out_unlock:
1793 spin_unlock(&fc->lock);
1794out:
1795 return found;
1796}
1797
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001798static int fuse_writepages_fill(struct page *page,
1799 struct writeback_control *wbc, void *_data)
1800{
1801 struct fuse_fill_wb_data *data = _data;
1802 struct fuse_req *req = data->req;
1803 struct inode *inode = data->inode;
1804 struct fuse_conn *fc = get_fuse_conn(inode);
1805 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001806 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001807 int err;
1808
1809 if (!data->ff) {
1810 err = -EIO;
1811 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1812 if (!data->ff)
1813 goto out_unlock;
1814 }
1815
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001816 /*
1817 * Being under writeback is unlikely but possible. For example direct
1818 * read to an mmaped fuse file will set the page dirty twice; once when
1819 * the pages are faulted with get_user_pages(), and then after the read
1820 * completed.
1821 */
1822 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001823
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001824 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001825 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001826 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001827 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1828 fuse_writepages_send(data);
1829 data->req = NULL;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001830 } else if (req && req->num_pages == req->max_pages) {
1831 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1832 fuse_writepages_send(data);
1833 req = data->req = NULL;
1834 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001835 }
Miklos Szeredie52a8252018-10-01 10:07:06 +02001836
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001837 err = -ENOMEM;
1838 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1839 if (!tmp_page)
1840 goto out_unlock;
1841
1842 /*
1843 * The page must not be redirtied until the writeout is completed
1844 * (i.e. userspace has sent a reply to the write request). Otherwise
1845 * there could be more than one temporary page instance for each real
1846 * page.
1847 *
1848 * This is ensured by holding the page lock in page_mkwrite() while
1849 * checking fuse_page_is_writeback(). We already hold the page lock
1850 * since clear_page_dirty_for_io() and keep it held until we add the
1851 * request to the fi->writepages list and increment req->num_pages.
1852 * After this fuse_page_is_writeback() will indicate that the page is
1853 * under writeback, so we can release the page lock.
1854 */
1855 if (data->req == NULL) {
1856 struct fuse_inode *fi = get_fuse_inode(inode);
1857
1858 err = -ENOMEM;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001859 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001860 if (!req) {
1861 __free_page(tmp_page);
1862 goto out_unlock;
1863 }
1864
1865 fuse_write_fill(req, data->ff, page_offset(page), 0);
1866 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001867 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001868 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001869 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001870 req->num_pages = 0;
1871 req->end = fuse_writepage_end;
1872 req->inode = inode;
1873
1874 spin_lock(&fc->lock);
1875 list_add(&req->writepages_entry, &fi->writepages);
1876 spin_unlock(&fc->lock);
1877
1878 data->req = req;
1879 }
1880 set_page_writeback(page);
1881
1882 copy_highpage(tmp_page, page);
1883 req->pages[req->num_pages] = tmp_page;
1884 req->page_descs[req->num_pages].offset = 0;
1885 req->page_descs[req->num_pages].length = PAGE_SIZE;
1886
Tejun Heo93f78d82015-05-22 17:13:27 -04001887 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001888 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001889
1890 err = 0;
1891 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1892 end_page_writeback(page);
1893 data->req = NULL;
1894 goto out_unlock;
1895 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001896 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001897
1898 /*
1899 * Protected by fc->lock against concurrent access by
1900 * fuse_page_is_writeback().
1901 */
1902 spin_lock(&fc->lock);
1903 req->num_pages++;
1904 spin_unlock(&fc->lock);
1905
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001906out_unlock:
1907 unlock_page(page);
1908
1909 return err;
1910}
1911
1912static int fuse_writepages(struct address_space *mapping,
1913 struct writeback_control *wbc)
1914{
1915 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001916 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001917 struct fuse_fill_wb_data data;
1918 int err;
1919
1920 err = -EIO;
1921 if (is_bad_inode(inode))
1922 goto out;
1923
1924 data.inode = inode;
1925 data.req = NULL;
1926 data.ff = NULL;
1927
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001928 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001929 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001930 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001931 GFP_NOFS);
1932 if (!data.orig_pages)
1933 goto out;
1934
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001935 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1936 if (data.req) {
1937 /* Ignore errors if we can write at least one page */
1938 BUG_ON(!data.req->num_pages);
1939 fuse_writepages_send(&data);
1940 err = 0;
1941 }
1942 if (data.ff)
1943 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001944
1945 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001946out:
1947 return err;
1948}
1949
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001950/*
1951 * It's worthy to make sure that space is reserved on disk for the write,
1952 * but how to implement it without killing performance need more thinking.
1953 */
1954static int fuse_write_begin(struct file *file, struct address_space *mapping,
1955 loff_t pos, unsigned len, unsigned flags,
1956 struct page **pagep, void **fsdata)
1957{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001958 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001959 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001960 struct page *page;
1961 loff_t fsize;
1962 int err = -ENOMEM;
1963
1964 WARN_ON(!fc->writeback_cache);
1965
1966 page = grab_cache_page_write_begin(mapping, index, flags);
1967 if (!page)
1968 goto error;
1969
1970 fuse_wait_on_page_writeback(mapping->host, page->index);
1971
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001972 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001973 goto success;
1974 /*
1975 * Check if the start this page comes after the end of file, in which
1976 * case the readpage can be optimized away.
1977 */
1978 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001979 if (fsize <= (pos & PAGE_MASK)) {
1980 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001981 if (off)
1982 zero_user_segment(page, 0, off);
1983 goto success;
1984 }
1985 err = fuse_do_readpage(file, page);
1986 if (err)
1987 goto cleanup;
1988success:
1989 *pagep = page;
1990 return 0;
1991
1992cleanup:
1993 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001994 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001995error:
1996 return err;
1997}
1998
1999static int fuse_write_end(struct file *file, struct address_space *mapping,
2000 loff_t pos, unsigned len, unsigned copied,
2001 struct page *page, void *fsdata)
2002{
2003 struct inode *inode = page->mapping->host;
2004
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002005 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2006 if (!copied)
2007 goto unlock;
2008
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002009 if (!PageUptodate(page)) {
2010 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002011 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002012 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002014 SetPageUptodate(page);
2015 }
2016
2017 fuse_write_update_size(inode, pos + copied);
2018 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002019
2020unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002021 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002022 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002023
2024 return copied;
2025}
2026
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002027static int fuse_launder_page(struct page *page)
2028{
2029 int err = 0;
2030 if (clear_page_dirty_for_io(page)) {
2031 struct inode *inode = page->mapping->host;
2032 err = fuse_writepage_locked(page);
2033 if (!err)
2034 fuse_wait_on_page_writeback(inode, page->index);
2035 }
2036 return err;
2037}
2038
2039/*
2040 * Write back dirty pages now, because there may not be any suitable
2041 * open files later
2042 */
2043static void fuse_vma_close(struct vm_area_struct *vma)
2044{
2045 filemap_write_and_wait(vma->vm_file->f_mapping);
2046}
2047
2048/*
2049 * Wait for writeback against this page to complete before allowing it
2050 * to be marked dirty again, and hence written back again, possibly
2051 * before the previous writepage completed.
2052 *
2053 * Block here, instead of in ->writepage(), so that the userspace fs
2054 * can only block processes actually operating on the filesystem.
2055 *
2056 * Otherwise unprivileged userspace fs would be able to block
2057 * unrelated:
2058 *
2059 * - page migration
2060 * - sync(2)
2061 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2062 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302063static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002064{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002065 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002066 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002067
Dave Jiang11bac802017-02-24 14:56:41 -08002068 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002069 lock_page(page);
2070 if (page->mapping != inode->i_mapping) {
2071 unlock_page(page);
2072 return VM_FAULT_NOPAGE;
2073 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002074
2075 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002076 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002077}
2078
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002079static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002080 .close = fuse_vma_close,
2081 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002082 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002083 .page_mkwrite = fuse_page_mkwrite,
2084};
2085
2086static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2087{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002088 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2089 fuse_link_write_file(file);
2090
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002091 file_accessed(file);
2092 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002093 return 0;
2094}
2095
Miklos Szeredifc280c92009-04-02 14:25:35 +02002096static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2097{
2098 /* Can't provide the coherency needed for MAP_SHARED */
2099 if (vma->vm_flags & VM_MAYSHARE)
2100 return -ENODEV;
2101
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002102 invalidate_inode_pages2(file->f_mapping);
2103
Miklos Szeredifc280c92009-04-02 14:25:35 +02002104 return generic_file_mmap(file, vma);
2105}
2106
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002107static int convert_fuse_file_lock(struct fuse_conn *fc,
2108 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002109 struct file_lock *fl)
2110{
2111 switch (ffl->type) {
2112 case F_UNLCK:
2113 break;
2114
2115 case F_RDLCK:
2116 case F_WRLCK:
2117 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2118 ffl->end < ffl->start)
2119 return -EIO;
2120
2121 fl->fl_start = ffl->start;
2122 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002123
2124 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002125 * Convert pid into init's pid namespace. The locks API will
2126 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002127 */
2128 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002129 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002130 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002131 break;
2132
2133 default:
2134 return -EIO;
2135 }
2136 fl->fl_type = ffl->type;
2137 return 0;
2138}
2139
Miklos Szeredi70781872014-12-12 09:49:05 +01002140static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002141 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002142 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002143{
Al Viro6131ffa2013-02-27 16:59:05 -05002144 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002145 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002146 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002147
Miklos Szeredi70781872014-12-12 09:49:05 +01002148 memset(inarg, 0, sizeof(*inarg));
2149 inarg->fh = ff->fh;
2150 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2151 inarg->lk.start = fl->fl_start;
2152 inarg->lk.end = fl->fl_end;
2153 inarg->lk.type = fl->fl_type;
2154 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002155 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002156 inarg->lk_flags |= FUSE_LK_FLOCK;
2157 args->in.h.opcode = opcode;
2158 args->in.h.nodeid = get_node_id(inode);
2159 args->in.numargs = 1;
2160 args->in.args[0].size = sizeof(*inarg);
2161 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002162}
2163
2164static int fuse_getlk(struct file *file, struct file_lock *fl)
2165{
Al Viro6131ffa2013-02-27 16:59:05 -05002166 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002167 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002168 FUSE_ARGS(args);
2169 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002170 struct fuse_lk_out outarg;
2171 int err;
2172
Miklos Szeredi70781872014-12-12 09:49:05 +01002173 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2174 args.out.numargs = 1;
2175 args.out.args[0].size = sizeof(outarg);
2176 args.out.args[0].value = &outarg;
2177 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002178 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002179 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002180
2181 return err;
2182}
2183
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002184static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002185{
Al Viro6131ffa2013-02-27 16:59:05 -05002186 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002187 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002188 FUSE_ARGS(args);
2189 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002190 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002191 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2192 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002193 int err;
2194
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002195 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002196 /* NLM needs asynchronous locks, which we don't support yet */
2197 return -ENOLCK;
2198 }
2199
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002201 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002202 return 0;
2203
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002204 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002205 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002206
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002207 /* locking is restartable */
2208 if (err == -EINTR)
2209 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002210
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 return err;
2212}
2213
2214static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2215{
Al Viro6131ffa2013-02-27 16:59:05 -05002216 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002217 struct fuse_conn *fc = get_fuse_conn(inode);
2218 int err;
2219
Miklos Szeredi48e90762008-07-25 01:49:02 -07002220 if (cmd == F_CANCELLK) {
2221 err = 0;
2222 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002224 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002225 err = 0;
2226 } else
2227 err = fuse_getlk(file, fl);
2228 } else {
2229 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002230 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002231 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002232 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002233 }
2234 return err;
2235}
2236
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002237static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2238{
Al Viro6131ffa2013-02-27 16:59:05 -05002239 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002240 struct fuse_conn *fc = get_fuse_conn(inode);
2241 int err;
2242
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002243 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002244 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002245 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002246 struct fuse_file *ff = file->private_data;
2247
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002248 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002249 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002250 err = fuse_setlk(file, fl, 1);
2251 }
2252
2253 return err;
2254}
2255
Miklos Szeredib2d22722006-12-06 20:35:51 -08002256static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2257{
2258 struct inode *inode = mapping->host;
2259 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002260 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002261 struct fuse_bmap_in inarg;
2262 struct fuse_bmap_out outarg;
2263 int err;
2264
2265 if (!inode->i_sb->s_bdev || fc->no_bmap)
2266 return 0;
2267
Miklos Szeredib2d22722006-12-06 20:35:51 -08002268 memset(&inarg, 0, sizeof(inarg));
2269 inarg.block = block;
2270 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002271 args.in.h.opcode = FUSE_BMAP;
2272 args.in.h.nodeid = get_node_id(inode);
2273 args.in.numargs = 1;
2274 args.in.args[0].size = sizeof(inarg);
2275 args.in.args[0].value = &inarg;
2276 args.out.numargs = 1;
2277 args.out.args[0].size = sizeof(outarg);
2278 args.out.args[0].value = &outarg;
2279 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002280 if (err == -ENOSYS)
2281 fc->no_bmap = 1;
2282
2283 return err ? 0 : outarg.block;
2284}
2285
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302286static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2287{
2288 struct inode *inode = file->f_mapping->host;
2289 struct fuse_conn *fc = get_fuse_conn(inode);
2290 struct fuse_file *ff = file->private_data;
2291 FUSE_ARGS(args);
2292 struct fuse_lseek_in inarg = {
2293 .fh = ff->fh,
2294 .offset = offset,
2295 .whence = whence
2296 };
2297 struct fuse_lseek_out outarg;
2298 int err;
2299
2300 if (fc->no_lseek)
2301 goto fallback;
2302
2303 args.in.h.opcode = FUSE_LSEEK;
2304 args.in.h.nodeid = ff->nodeid;
2305 args.in.numargs = 1;
2306 args.in.args[0].size = sizeof(inarg);
2307 args.in.args[0].value = &inarg;
2308 args.out.numargs = 1;
2309 args.out.args[0].size = sizeof(outarg);
2310 args.out.args[0].value = &outarg;
2311 err = fuse_simple_request(fc, &args);
2312 if (err) {
2313 if (err == -ENOSYS) {
2314 fc->no_lseek = 1;
2315 goto fallback;
2316 }
2317 return err;
2318 }
2319
2320 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2321
2322fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002323 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302324 if (!err)
2325 return generic_file_llseek(file, offset, whence);
2326 else
2327 return err;
2328}
2329
Andrew Morton965c8e52012-12-17 15:59:39 -08002330static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002331{
2332 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002333 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002334
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302335 switch (whence) {
2336 case SEEK_SET:
2337 case SEEK_CUR:
2338 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002339 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302340 break;
2341 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002342 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002343 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302344 if (!retval)
2345 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002346 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302347 break;
2348 case SEEK_HOLE:
2349 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002350 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302351 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002352 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302353 break;
2354 default:
2355 retval = -EINVAL;
2356 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002357
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002358 return retval;
2359}
2360
Tejun Heo59efec72008-11-26 12:03:55 +01002361/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002362 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2363 * ABI was defined to be 'struct iovec' which is different on 32bit
2364 * and 64bit. Fortunately we can determine which structure the server
2365 * used from the size of the reply.
2366 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002367static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2368 size_t transferred, unsigned count,
2369 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002370{
2371#ifdef CONFIG_COMPAT
2372 if (count * sizeof(struct compat_iovec) == transferred) {
2373 struct compat_iovec *ciov = src;
2374 unsigned i;
2375
2376 /*
2377 * With this interface a 32bit server cannot support
2378 * non-compat (i.e. ones coming from 64bit apps) ioctl
2379 * requests
2380 */
2381 if (!is_compat)
2382 return -EINVAL;
2383
2384 for (i = 0; i < count; i++) {
2385 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2386 dst[i].iov_len = ciov[i].iov_len;
2387 }
2388 return 0;
2389 }
2390#endif
2391
2392 if (count * sizeof(struct iovec) != transferred)
2393 return -EIO;
2394
2395 memcpy(dst, src, transferred);
2396 return 0;
2397}
2398
Miklos Szeredi75727772010-11-30 16:39:27 +01002399/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002400static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2401 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002402{
2403 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002404 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002405
Zach Brownfb6ccff2012-07-24 12:10:11 -07002406 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002407 if (iov->iov_len > (size_t) max)
2408 return -ENOMEM;
2409 max -= iov->iov_len;
2410 }
2411 return 0;
2412}
2413
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002414static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2415 void *src, size_t transferred, unsigned count,
2416 bool is_compat)
2417{
2418 unsigned i;
2419 struct fuse_ioctl_iovec *fiov = src;
2420
2421 if (fc->minor < 16) {
2422 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2423 count, is_compat);
2424 }
2425
2426 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2427 return -EIO;
2428
2429 for (i = 0; i < count; i++) {
2430 /* Did the server supply an inappropriate value? */
2431 if (fiov[i].base != (unsigned long) fiov[i].base ||
2432 fiov[i].len != (unsigned long) fiov[i].len)
2433 return -EIO;
2434
2435 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2436 dst[i].iov_len = (size_t) fiov[i].len;
2437
2438#ifdef CONFIG_COMPAT
2439 if (is_compat &&
2440 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2441 (compat_size_t) dst[i].iov_len != fiov[i].len))
2442 return -EIO;
2443#endif
2444 }
2445
2446 return 0;
2447}
2448
2449
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002450/*
Tejun Heo59efec72008-11-26 12:03:55 +01002451 * For ioctls, there is no generic way to determine how much memory
2452 * needs to be read and/or written. Furthermore, ioctls are allowed
2453 * to dereference the passed pointer, so the parameter requires deep
2454 * copying but FUSE has no idea whatsoever about what to copy in or
2455 * out.
2456 *
2457 * This is solved by allowing FUSE server to retry ioctl with
2458 * necessary in/out iovecs. Let's assume the ioctl implementation
2459 * needs to read in the following structure.
2460 *
2461 * struct a {
2462 * char *buf;
2463 * size_t buflen;
2464 * }
2465 *
2466 * On the first callout to FUSE server, inarg->in_size and
2467 * inarg->out_size will be NULL; then, the server completes the ioctl
2468 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2469 * the actual iov array to
2470 *
2471 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2472 *
2473 * which tells FUSE to copy in the requested area and retry the ioctl.
2474 * On the second round, the server has access to the structure and
2475 * from that it can tell what to look for next, so on the invocation,
2476 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2477 *
2478 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2479 * { .iov_base = a.buf, .iov_len = a.buflen } }
2480 *
2481 * FUSE will copy both struct a and the pointed buffer from the
2482 * process doing the ioctl and retry ioctl with both struct a and the
2483 * buffer.
2484 *
2485 * This time, FUSE server has everything it needs and completes ioctl
2486 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2487 *
2488 * Copying data out works the same way.
2489 *
2490 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2491 * automatically initializes in and out iovs by decoding @cmd with
2492 * _IOC_* macros and the server is not allowed to request RETRY. This
2493 * limits ioctl data transfers to well-formed ioctls and is the forced
2494 * behavior for all FUSE servers.
2495 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002496long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2497 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002498{
Tejun Heo59efec72008-11-26 12:03:55 +01002499 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002500 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002501 struct fuse_ioctl_in inarg = {
2502 .fh = ff->fh,
2503 .cmd = cmd,
2504 .arg = arg,
2505 .flags = flags
2506 };
2507 struct fuse_ioctl_out outarg;
2508 struct fuse_req *req = NULL;
2509 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002510 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002511 struct iovec *in_iov = NULL, *out_iov = NULL;
2512 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002513 size_t in_size, out_size, transferred, c;
2514 int err, i;
2515 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002516
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002517#if BITS_PER_LONG == 32
2518 inarg.flags |= FUSE_IOCTL_32BIT;
2519#else
2520 if (flags & FUSE_IOCTL_COMPAT)
2521 inarg.flags |= FUSE_IOCTL_32BIT;
2522#endif
2523
Tejun Heo59efec72008-11-26 12:03:55 +01002524 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002525 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002526
Tejun Heo59efec72008-11-26 12:03:55 +01002527 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002528 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002529 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002530 if (!pages || !iov_page)
2531 goto out;
2532
2533 /*
2534 * If restricted, initialize IO parameters as encoded in @cmd.
2535 * RETRY from server is not allowed.
2536 */
2537 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002538 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002539
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002540 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002541 iov->iov_len = _IOC_SIZE(cmd);
2542
2543 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2544 in_iov = iov;
2545 in_iovs = 1;
2546 }
2547
2548 if (_IOC_DIR(cmd) & _IOC_READ) {
2549 out_iov = iov;
2550 out_iovs = 1;
2551 }
2552 }
2553
2554 retry:
2555 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2556 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2557
2558 /*
2559 * Out data can be used either for actual out data or iovs,
2560 * make sure there always is at least one page.
2561 */
2562 out_size = max_t(size_t, out_size, PAGE_SIZE);
2563 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2564
2565 /* make sure there are enough buffer pages and init request with them */
2566 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002567 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002568 goto out;
2569 while (num_pages < max_pages) {
2570 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2571 if (!pages[num_pages])
2572 goto out;
2573 num_pages++;
2574 }
2575
Maxim Patlasov54b96672012-10-26 19:49:13 +04002576 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002577 if (IS_ERR(req)) {
2578 err = PTR_ERR(req);
2579 req = NULL;
2580 goto out;
2581 }
2582 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2583 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002584 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002585
2586 /* okay, let's send it to the client */
2587 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002588 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002589 req->in.numargs = 1;
2590 req->in.args[0].size = sizeof(inarg);
2591 req->in.args[0].value = &inarg;
2592 if (in_size) {
2593 req->in.numargs++;
2594 req->in.args[1].size = in_size;
2595 req->in.argpages = 1;
2596
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002597 err = -EFAULT;
2598 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2599 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2600 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2601 if (c != PAGE_SIZE && iov_iter_count(&ii))
2602 goto out;
2603 }
Tejun Heo59efec72008-11-26 12:03:55 +01002604 }
2605
2606 req->out.numargs = 2;
2607 req->out.args[0].size = sizeof(outarg);
2608 req->out.args[0].value = &outarg;
2609 req->out.args[1].size = out_size;
2610 req->out.argpages = 1;
2611 req->out.argvar = 1;
2612
Tejun Heob93f8582008-11-26 12:03:55 +01002613 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002614 err = req->out.h.error;
2615 transferred = req->out.args[1].size;
2616 fuse_put_request(fc, req);
2617 req = NULL;
2618 if (err)
2619 goto out;
2620
2621 /* did it ask for retry? */
2622 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002623 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002624
2625 /* no retry if in restricted mode */
2626 err = -EIO;
2627 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2628 goto out;
2629
2630 in_iovs = outarg.in_iovs;
2631 out_iovs = outarg.out_iovs;
2632
2633 /*
2634 * Make sure things are in boundary, separate checks
2635 * are to protect against overflow.
2636 */
2637 err = -ENOMEM;
2638 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2639 out_iovs > FUSE_IOCTL_MAX_IOV ||
2640 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2641 goto out;
2642
Cong Wang2408f6e2011-11-25 23:14:30 +08002643 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002644 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002645 transferred, in_iovs + out_iovs,
2646 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002647 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002648 if (err)
2649 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002650
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002651 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002652 out_iov = in_iov + in_iovs;
2653
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002654 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002655 if (err)
2656 goto out;
2657
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002658 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002659 if (err)
2660 goto out;
2661
Tejun Heo59efec72008-11-26 12:03:55 +01002662 goto retry;
2663 }
2664
2665 err = -EIO;
2666 if (transferred > inarg.out_size)
2667 goto out;
2668
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002669 err = -EFAULT;
2670 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2671 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2672 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2673 if (c != PAGE_SIZE && iov_iter_count(&ii))
2674 goto out;
2675 }
2676 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002677 out:
2678 if (req)
2679 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002680 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002681 while (num_pages)
2682 __free_page(pages[--num_pages]);
2683 kfree(pages);
2684
2685 return err ? err : outarg.result;
2686}
Tejun Heo08cbf542009-04-14 10:54:53 +09002687EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002688
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002689long fuse_ioctl_common(struct file *file, unsigned int cmd,
2690 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002691{
Al Viro6131ffa2013-02-27 16:59:05 -05002692 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002693 struct fuse_conn *fc = get_fuse_conn(inode);
2694
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002695 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002696 return -EACCES;
2697
2698 if (is_bad_inode(inode))
2699 return -EIO;
2700
2701 return fuse_do_ioctl(file, cmd, arg, flags);
2702}
2703
Tejun Heo59efec72008-11-26 12:03:55 +01002704static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2705 unsigned long arg)
2706{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002707 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002708}
2709
2710static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2711 unsigned long arg)
2712{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002713 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002714}
2715
Tejun Heo95668a62008-11-26 12:03:55 +01002716/*
2717 * All files which have been polled are linked to RB tree
2718 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2719 * find the matching one.
2720 */
2721static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2722 struct rb_node **parent_out)
2723{
2724 struct rb_node **link = &fc->polled_files.rb_node;
2725 struct rb_node *last = NULL;
2726
2727 while (*link) {
2728 struct fuse_file *ff;
2729
2730 last = *link;
2731 ff = rb_entry(last, struct fuse_file, polled_node);
2732
2733 if (kh < ff->kh)
2734 link = &last->rb_left;
2735 else if (kh > ff->kh)
2736 link = &last->rb_right;
2737 else
2738 return link;
2739 }
2740
2741 if (parent_out)
2742 *parent_out = last;
2743 return link;
2744}
2745
2746/*
2747 * The file is about to be polled. Make sure it's on the polled_files
2748 * RB tree. Note that files once added to the polled_files tree are
2749 * not removed before the file is released. This is because a file
2750 * polled once is likely to be polled again.
2751 */
2752static void fuse_register_polled_file(struct fuse_conn *fc,
2753 struct fuse_file *ff)
2754{
2755 spin_lock(&fc->lock);
2756 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002757 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002758
2759 link = fuse_find_polled_node(fc, ff->kh, &parent);
2760 BUG_ON(*link);
2761 rb_link_node(&ff->polled_node, parent, link);
2762 rb_insert_color(&ff->polled_node, &fc->polled_files);
2763 }
2764 spin_unlock(&fc->lock);
2765}
2766
Al Viro076ccb72017-07-03 01:02:18 -04002767__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002768{
Tejun Heo95668a62008-11-26 12:03:55 +01002769 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002770 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002771 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2772 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002773 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002774 int err;
2775
2776 if (fc->no_poll)
2777 return DEFAULT_POLLMASK;
2778
2779 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002780 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002781
2782 /*
2783 * Ask for notification iff there's someone waiting for it.
2784 * The client may ignore the flag and always notify.
2785 */
2786 if (waitqueue_active(&ff->poll_wait)) {
2787 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2788 fuse_register_polled_file(fc, ff);
2789 }
2790
Miklos Szeredi70781872014-12-12 09:49:05 +01002791 args.in.h.opcode = FUSE_POLL;
2792 args.in.h.nodeid = ff->nodeid;
2793 args.in.numargs = 1;
2794 args.in.args[0].size = sizeof(inarg);
2795 args.in.args[0].value = &inarg;
2796 args.out.numargs = 1;
2797 args.out.args[0].size = sizeof(outarg);
2798 args.out.args[0].value = &outarg;
2799 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002800
2801 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002802 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002803 if (err == -ENOSYS) {
2804 fc->no_poll = 1;
2805 return DEFAULT_POLLMASK;
2806 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002807 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002808}
Tejun Heo08cbf542009-04-14 10:54:53 +09002809EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002810
2811/*
2812 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2813 * wakes up the poll waiters.
2814 */
2815int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2816 struct fuse_notify_poll_wakeup_out *outarg)
2817{
2818 u64 kh = outarg->kh;
2819 struct rb_node **link;
2820
2821 spin_lock(&fc->lock);
2822
2823 link = fuse_find_polled_node(fc, kh, NULL);
2824 if (*link) {
2825 struct fuse_file *ff;
2826
2827 ff = rb_entry(*link, struct fuse_file, polled_node);
2828 wake_up_interruptible_sync(&ff->poll_wait);
2829 }
2830
2831 spin_unlock(&fc->lock);
2832 return 0;
2833}
2834
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002835static void fuse_do_truncate(struct file *file)
2836{
2837 struct inode *inode = file->f_mapping->host;
2838 struct iattr attr;
2839
2840 attr.ia_valid = ATTR_SIZE;
2841 attr.ia_size = i_size_read(inode);
2842
2843 attr.ia_file = file;
2844 attr.ia_valid |= ATTR_FILE;
2845
Jan Kara62490332016-05-26 17:12:41 +02002846 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002847}
2848
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002849static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002850{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002851 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002852}
2853
Anand Avati4273b792012-02-17 12:46:25 -05002854static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002855fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002856{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002857 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002858 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002859 struct file *file = iocb->ki_filp;
2860 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002861 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002862 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002863 struct inode *inode;
2864 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002865 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002866 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002867 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002868
Anand Avati4273b792012-02-17 12:46:25 -05002869 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002870 inode = file->f_mapping->host;
2871 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002872
Omar Sandoval6f673762015-03-16 04:33:52 -07002873 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002874 return 0;
2875
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002876 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002877 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002878 if (offset >= i_size)
2879 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002880 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002881 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002882 }
2883
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002884 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002885 if (!io)
2886 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002887 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002888 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 io->reqs = 1;
2890 io->bytes = -1;
2891 io->size = 0;
2892 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002893 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 /*
2896 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002897 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002899 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002900 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302901 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002902
2903 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302904 * We cannot asynchronously extend the size of a file.
2905 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302907 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2908 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002909
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302910 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002911 /*
2912 * Additional reference to keep io around after
2913 * calling fuse_aio_complete()
2914 */
2915 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002916 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002917 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002918
Omar Sandoval6f673762015-03-16 04:33:52 -07002919 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002920 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002921 fuse_invalidate_attr(inode);
2922 } else {
Al Virod22a9432014-03-16 15:50:47 -04002923 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002924 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002925
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002926 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002927 bool blocking = io->blocking;
2928
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002929 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2930
2931 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002932 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002933 return -EIOCBQUEUED;
2934
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002935 wait_for_completion(&wait);
2936 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002937 }
2938
Seth Forshee744742d2016-03-11 10:35:34 -06002939 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002940
Omar Sandoval6f673762015-03-16 04:33:52 -07002941 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002942 if (ret > 0)
2943 fuse_write_update_size(inode, pos);
2944 else if (ret < 0 && offset + count > i_size)
2945 fuse_do_truncate(file);
2946 }
Anand Avati4273b792012-02-17 12:46:25 -05002947
2948 return ret;
2949}
2950
Miklos Szeredicdadb112012-11-10 16:55:56 +01002951static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2952 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002953{
2954 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002955 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002956 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002957 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002958 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002959 struct fuse_fallocate_in inarg = {
2960 .fh = ff->fh,
2961 .offset = offset,
2962 .length = length,
2963 .mode = mode
2964 };
2965 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002966 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2967 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002968
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002969 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2970 return -EOPNOTSUPP;
2971
Miklos Szeredi519c6042012-04-26 10:56:36 +02002972 if (fc->no_fallocate)
2973 return -EOPNOTSUPP;
2974
Maxim Patlasov14c14412013-06-13 12:16:39 +04002975 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002976 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002977 if (mode & FALLOC_FL_PUNCH_HOLE) {
2978 loff_t endbyte = offset + length - 1;
2979 err = filemap_write_and_wait_range(inode->i_mapping,
2980 offset, endbyte);
2981 if (err)
2982 goto out;
2983
2984 fuse_sync_writes(inode);
2985 }
Brian Foster3634a632013-05-17 09:30:32 -04002986 }
2987
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002988 if (!(mode & FALLOC_FL_KEEP_SIZE))
2989 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2990
Miklos Szeredi70781872014-12-12 09:49:05 +01002991 args.in.h.opcode = FUSE_FALLOCATE;
2992 args.in.h.nodeid = ff->nodeid;
2993 args.in.numargs = 1;
2994 args.in.args[0].size = sizeof(inarg);
2995 args.in.args[0].value = &inarg;
2996 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002997 if (err == -ENOSYS) {
2998 fc->no_fallocate = 1;
2999 err = -EOPNOTSUPP;
3000 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003001 if (err)
3002 goto out;
3003
3004 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003005 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3006 bool changed = fuse_write_update_size(inode, offset + length);
3007
Miklos Szeredi93d22692014-04-28 14:19:22 +02003008 if (changed && fc->writeback_cache)
3009 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003010 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003011
3012 if (mode & FALLOC_FL_PUNCH_HOLE)
3013 truncate_pagecache_range(inode, offset, offset + length - 1);
3014
3015 fuse_invalidate_attr(inode);
3016
Brian Foster3634a632013-05-17 09:30:32 -04003017out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003018 if (!(mode & FALLOC_FL_KEEP_SIZE))
3019 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3020
Maxim Patlasovbde52782013-09-13 19:19:54 +04003021 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003022 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003023
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003024 return err;
3025}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003026
Niels de Vos88bc7d52018-08-21 14:36:31 +02003027static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3028 struct file *file_out, loff_t pos_out,
3029 size_t len, unsigned int flags)
3030{
3031 struct fuse_file *ff_in = file_in->private_data;
3032 struct fuse_file *ff_out = file_out->private_data;
3033 struct inode *inode_out = file_inode(file_out);
3034 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3035 struct fuse_conn *fc = ff_in->fc;
3036 FUSE_ARGS(args);
3037 struct fuse_copy_file_range_in inarg = {
3038 .fh_in = ff_in->fh,
3039 .off_in = pos_in,
3040 .nodeid_out = ff_out->nodeid,
3041 .fh_out = ff_out->fh,
3042 .off_out = pos_out,
3043 .len = len,
3044 .flags = flags
3045 };
3046 struct fuse_write_out outarg;
3047 ssize_t err;
3048 /* mark unstable when write-back is not used, and file_out gets
3049 * extended */
3050 bool is_unstable = (!fc->writeback_cache) &&
3051 ((pos_out + len) > inode_out->i_size);
3052
3053 if (fc->no_copy_file_range)
3054 return -EOPNOTSUPP;
3055
3056 inode_lock(inode_out);
3057
3058 if (fc->writeback_cache) {
3059 err = filemap_write_and_wait_range(inode_out->i_mapping,
3060 pos_out, pos_out + len);
3061 if (err)
3062 goto out;
3063
3064 fuse_sync_writes(inode_out);
3065 }
3066
3067 if (is_unstable)
3068 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3069
3070 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3071 args.in.h.nodeid = ff_in->nodeid;
3072 args.in.numargs = 1;
3073 args.in.args[0].size = sizeof(inarg);
3074 args.in.args[0].value = &inarg;
3075 args.out.numargs = 1;
3076 args.out.args[0].size = sizeof(outarg);
3077 args.out.args[0].value = &outarg;
3078 err = fuse_simple_request(fc, &args);
3079 if (err == -ENOSYS) {
3080 fc->no_copy_file_range = 1;
3081 err = -EOPNOTSUPP;
3082 }
3083 if (err)
3084 goto out;
3085
3086 if (fc->writeback_cache) {
3087 fuse_write_update_size(inode_out, pos_out + outarg.size);
3088 file_update_time(file_out);
3089 }
3090
3091 fuse_invalidate_attr(inode_out);
3092
3093 err = outarg.size;
3094out:
3095 if (is_unstable)
3096 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3097
3098 inode_unlock(inode_out);
3099
3100 return err;
3101}
3102
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003103static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003104 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003105 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003106 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003107 .mmap = fuse_file_mmap,
3108 .open = fuse_open,
3109 .flush = fuse_flush,
3110 .release = fuse_release,
3111 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003112 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003113 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003114 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003115 .unlocked_ioctl = fuse_file_ioctl,
3116 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003117 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003118 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003119 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003120};
3121
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003122static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003123 .llseek = fuse_file_llseek,
Al Viro153162632015-03-30 22:08:36 -04003124 .read_iter = fuse_direct_read_iter,
Al Viro153162632015-03-30 22:08:36 -04003125 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003126 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003127 .open = fuse_open,
3128 .flush = fuse_flush,
3129 .release = fuse_release,
3130 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003131 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003132 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003133 .unlocked_ioctl = fuse_file_ioctl,
3134 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003135 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003136 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003137 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003138};
3139
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003140static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003141 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003142 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003143 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003144 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003145 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003146 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003147 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003148 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003149 .write_begin = fuse_write_begin,
3150 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003151};
3152
3153void fuse_init_file_inode(struct inode *inode)
3154{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003155 struct fuse_inode *fi = get_fuse_inode(inode);
3156
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003157 inode->i_fop = &fuse_file_operations;
3158 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003159
3160 INIT_LIST_HEAD(&fi->write_files);
3161 INIT_LIST_HEAD(&fi->queued_writes);
3162 fi->writectr = 0;
3163 init_waitqueue_head(&fi->page_waitq);
3164 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003165}