blob: 677c51341e96f80b9536356171ea1383004f24fc [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,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100444 int datasync, int opcode)
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;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100451
452 memset(&inarg, 0, sizeof(inarg));
453 inarg.fh = ff->fh;
454 inarg.fsync_flags = datasync ? 1 : 0;
455 args.in.h.opcode = opcode;
456 args.in.h.nodeid = get_node_id(inode);
457 args.in.numargs = 1;
458 args.in.args[0].size = sizeof(inarg);
459 args.in.args[0].value = &inarg;
460 return fuse_simple_request(fc, &args);
461}
462
463static int fuse_fsync(struct file *file, loff_t start, loff_t end,
464 int datasync)
465{
466 struct inode *inode = file->f_mapping->host;
467 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468 int err;
469
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800470 if (is_bad_inode(inode))
471 return -EIO;
472
Al Viro59551022016-01-22 15:40:57 -0500473 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400474
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700475 /*
476 * Start writeback against all dirty pages of the inode, then
477 * wait for all outstanding writes, before sending the FSYNC
478 * request.
479 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400480 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700481 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400482 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700483
484 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700485
486 /*
487 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400488 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700489 * We have to do this directly after fuse_sync_writes()
490 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400491 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700492 if (err)
493 goto out;
494
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200495 err = sync_inode_metadata(inode, 1);
496 if (err)
497 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700498
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100499 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200500 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400501
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100502 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700503 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100504 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505 err = 0;
506 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400507out:
Al Viro59551022016-01-22 15:40:57 -0500508 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700509
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100510 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700511}
512
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200513void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
514 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700516 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800517 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700518
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800519 inarg->fh = ff->fh;
520 inarg->offset = pos;
521 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800522 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800523 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200524 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 req->in.numargs = 1;
526 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800527 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528 req->out.argvar = 1;
529 req->out.numargs = 1;
530 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531}
532
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200533static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400534{
535 unsigned i;
536
537 for (i = 0; i < req->num_pages; i++) {
538 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200539 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400540 set_page_dirty_lock(page);
541 put_page(page);
542 }
543}
544
Seth Forshee744742d2016-03-11 10:35:34 -0600545static void fuse_io_release(struct kref *kref)
546{
547 kfree(container_of(kref, struct fuse_io_priv, refcnt));
548}
549
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100550static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
551{
552 if (io->err)
553 return io->err;
554
555 if (io->bytes >= 0 && io->write)
556 return -EIO;
557
558 return io->bytes < 0 ? io->size : io->bytes;
559}
560
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400561/**
562 * In case of short read, the caller sets 'pos' to the position of
563 * actual end of fuse request in IO request. Otherwise, if bytes_requested
564 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
565 *
566 * An example:
567 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
568 * both submitted asynchronously. The first of them was ACKed by userspace as
569 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
570 * second request was ACKed as short, e.g. only 1K was read, resulting in
571 * pos == 33K.
572 *
573 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
574 * will be equal to the length of the longest contiguous fragment of
575 * transferred data starting from the beginning of IO request.
576 */
577static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
578{
579 int left;
580
581 spin_lock(&io->lock);
582 if (err)
583 io->err = io->err ? : err;
584 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
585 io->bytes = pos;
586
587 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530588 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100589 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400590 spin_unlock(&io->lock);
591
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530592 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100593 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400594
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100595 if (res >= 0) {
596 struct inode *inode = file_inode(io->iocb->ki_filp);
597 struct fuse_conn *fc = get_fuse_conn(inode);
598 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400599
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100600 spin_lock(&fc->lock);
601 fi->attr_version = ++fc->attr_version;
602 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400603 }
604
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100605 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400606 }
Seth Forshee744742d2016-03-11 10:35:34 -0600607
608 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400609}
610
611static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
612{
613 struct fuse_io_priv *io = req->io;
614 ssize_t pos = -1;
615
Ashish Samant61c12b42017-07-12 19:26:58 -0700616 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400617
618 if (io->write) {
619 if (req->misc.write.in.size != req->misc.write.out.size)
620 pos = req->misc.write.in.offset - io->offset +
621 req->misc.write.out.size;
622 } else {
623 if (req->misc.read.in.size != req->out.args[0].size)
624 pos = req->misc.read.in.offset - io->offset +
625 req->out.args[0].size;
626 }
627
628 fuse_aio_complete(io, req->out.h.error, pos);
629}
630
631static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
632 size_t num_bytes, struct fuse_io_priv *io)
633{
634 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600635 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400636 io->size += num_bytes;
637 io->reqs++;
638 spin_unlock(&io->lock);
639
640 req->io = io;
641 req->end = fuse_aio_complete_req;
642
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400643 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400644 fuse_request_send_background(fc, req);
645
646 return num_bytes;
647}
648
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400649static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200650 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700651{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200652 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200653 struct fuse_file *ff = file->private_data;
654 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700655
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200656 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700657 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700658 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700659
660 inarg->read_flags |= FUSE_READ_LOCKOWNER;
661 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
662 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400663
664 if (io->async)
665 return fuse_async_req_send(fc, req, count, io);
666
Tejun Heob93f8582008-11-26 12:03:55 +0100667 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800668 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700669}
670
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700671static void fuse_read_update_size(struct inode *inode, loff_t size,
672 u64 attr_ver)
673{
674 struct fuse_conn *fc = get_fuse_conn(inode);
675 struct fuse_inode *fi = get_fuse_inode(inode);
676
677 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400678 if (attr_ver == fi->attr_version && size < inode->i_size &&
679 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700680 fi->attr_version = ++fc->attr_version;
681 i_size_write(inode, size);
682 }
683 spin_unlock(&fc->lock);
684}
685
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400686static void fuse_short_read(struct fuse_req *req, struct inode *inode,
687 u64 attr_ver)
688{
689 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400690 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400691
Pavel Emelyanov83732002013-10-10 17:10:46 +0400692 if (fc->writeback_cache) {
693 /*
694 * A hole in a file. Some data after the hole are in page cache,
695 * but have not reached the client fs yet. So, the hole is not
696 * present there.
697 */
698 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300699 int start_idx = num_read >> PAGE_SHIFT;
700 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701
702 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300703 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400704 off = 0;
705 }
706 } else {
707 loff_t pos = page_offset(req->pages[0]) + num_read;
708 fuse_read_update_size(inode, pos, attr_ver);
709 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710}
711
Maxim Patlasov482fce52013-10-10 17:11:25 +0400712static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700713{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200714 struct kiocb iocb;
715 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700716 struct inode *inode = page->mapping->host;
717 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800718 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700719 size_t num_read;
720 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300721 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700722 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800723 int err;
724
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700725 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300726 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700727 * page-cache page, so make sure we read a properly synced
728 * page.
729 */
730 fuse_wait_on_page_writeback(inode, page->index);
731
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400732 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700733 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400734 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700735
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700736 attr_ver = fuse_get_attr_version(fc);
737
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200739 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700740 req->num_pages = 1;
741 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400742 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200743 init_sync_kiocb(&iocb, file);
744 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400745 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700746 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747
748 if (!err) {
749 /*
750 * Short read means EOF. If file size is larger, truncate it
751 */
752 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400753 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700755 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700756 }
757
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400758 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400759
760 return err;
761}
762
763static int fuse_readpage(struct file *file, struct page *page)
764{
765 struct inode *inode = page->mapping->host;
766 int err;
767
768 err = -EIO;
769 if (is_bad_inode(inode))
770 goto out;
771
772 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800773 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700774 out:
775 unlock_page(page);
776 return err;
777}
778
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800779static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700780{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800781 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700782 size_t count = req->misc.read.in.size;
783 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800785
Miklos Szeredice534fb2010-05-25 15:06:07 +0200786 for (i = 0; mapping == NULL && i < req->num_pages; i++)
787 mapping = req->pages[i]->mapping;
788
789 if (mapping) {
790 struct inode *inode = mapping->host;
791
792 /*
793 * Short read means EOF. If file size is larger, truncate it
794 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400795 if (!req->out.h.error && num_read < count)
796 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200797
Andrew Gallagher451418f2013-11-05 03:55:43 -0800798 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700799 }
800
Miklos Szeredidb50b962005-09-09 13:10:33 -0700801 for (i = 0; i < req->num_pages; i++) {
802 struct page *page = req->pages[i];
803 if (!req->out.h.error)
804 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800805 else
806 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700807 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300808 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700809 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700810 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100811 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800812}
813
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200814static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800815{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200816 struct fuse_file *ff = file->private_data;
817 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800818 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200820
821 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800822 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200823 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200824 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700825 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800826 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700827 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800828 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100829 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800830 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100831 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800832 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100833 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800834 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700835}
836
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700837struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800839 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400841 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700842};
843
844static int fuse_readpages_fill(void *_data, struct page *page)
845{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700846 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700847 struct fuse_req *req = data->req;
848 struct inode *inode = data->inode;
849 struct fuse_conn *fc = get_fuse_conn(inode);
850
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700851 fuse_wait_on_page_writeback(inode, page->index);
852
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300854 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300855 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300857 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
858 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200859 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400860 if (fc->async_read)
861 req = fuse_get_req_for_background(fc, nr_alloc);
862 else
863 req = fuse_get_req(fc, nr_alloc);
864
865 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700868 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400871
872 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300873 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400874 fuse_put_request(fc, req);
875 return -EIO;
876 }
877
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300878 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400880 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100881 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400882 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700883 return 0;
884}
885
886static int fuse_readpages(struct file *file, struct address_space *mapping,
887 struct list_head *pages, unsigned nr_pages)
888{
889 struct inode *inode = mapping->host;
890 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700891 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700892 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300893 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800894
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700895 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800896 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800897 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800898
Miklos Szeredia6643092007-11-28 16:22:00 -0800899 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700900 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400901 if (fc->async_read)
902 data.req = fuse_get_req_for_background(fc, nr_alloc);
903 else
904 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400905 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700906 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700907 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800908 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700909
910 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700911 if (!err) {
912 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200913 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700914 else
915 fuse_put_request(fc, data.req);
916 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800917out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700918 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700919}
920
Al Viro37c20f12014-04-02 14:47:09 -0400921static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800922{
923 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400924 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800925
Brian Fostera8894272012-07-16 15:23:50 -0400926 /*
927 * In auto invalidate mode, always update attributes on read.
928 * Otherwise, only update if we attempt to read past EOF (to ensure
929 * i_size is up to date).
930 */
931 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400932 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200934 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800935 if (err)
936 return err;
937 }
938
Al Viro37c20f12014-04-02 14:47:09 -0400939 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800940}
941
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200942static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200943 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700945 struct fuse_write_in *inarg = &req->misc.write.in;
946 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700947
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700948 inarg->fh = ff->fh;
949 inarg->offset = pos;
950 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200952 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700953 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200954 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700955 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
956 else
957 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700959 req->in.args[1].size = count;
960 req->out.numargs = 1;
961 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700962 req->out.args[0].value = outarg;
963}
964
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400965static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700967{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200968 struct kiocb *iocb = io->iocb;
969 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 struct fuse_file *ff = file->private_data;
971 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200972 struct fuse_write_in *inarg = &req->misc.write.in;
973
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200974 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200975 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200976 if (iocb->ki_flags & IOCB_DSYNC)
977 inarg->flags |= O_DSYNC;
978 if (iocb->ki_flags & IOCB_SYNC)
979 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700980 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700981 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
982 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
983 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400984
985 if (io->async)
986 return fuse_async_req_send(fc, req, count, io);
987
Tejun Heob93f8582008-11-26 12:03:55 +0100988 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700989 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700990}
991
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400992bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700993{
994 struct fuse_conn *fc = get_fuse_conn(inode);
995 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400996 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700997
998 spin_lock(&fc->lock);
999 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001000 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001001 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001002 ret = true;
1003 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001005
1006 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001007}
1008
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001009static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001010 struct inode *inode, loff_t pos,
1011 size_t count)
1012{
1013 size_t res;
1014 unsigned offset;
1015 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001016 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001017
1018 for (i = 0; i < req->num_pages; i++)
1019 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1020
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001021 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001022
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001023 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001024 count = res;
1025 for (i = 0; i < req->num_pages; i++) {
1026 struct page *page = req->pages[i];
1027
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001029 SetPageUptodate(page);
1030
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001031 if (count > PAGE_SIZE - offset)
1032 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001033 else
1034 count = 0;
1035 offset = 0;
1036
1037 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001038 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001039 }
1040
1041 return res;
1042}
1043
1044static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1045 struct address_space *mapping,
1046 struct iov_iter *ii, loff_t pos)
1047{
1048 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001049 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001050 size_t count = 0;
1051 int err;
1052
Miklos Szeredif4975c62009-04-02 14:25:34 +02001053 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001054 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001055
1056 do {
1057 size_t tmp;
1058 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001059 pgoff_t index = pos >> PAGE_SHIFT;
1060 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001061 iov_iter_count(ii));
1062
1063 bytes = min_t(size_t, bytes, fc->max_write - count);
1064
1065 again:
1066 err = -EFAULT;
1067 if (iov_iter_fault_in_readable(ii, bytes))
1068 break;
1069
1070 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001071 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001072 if (!page)
1073 break;
1074
anfei zhou931e80e2010-02-02 13:44:02 -08001075 if (mapping_writably_mapped(mapping))
1076 flush_dcache_page(page);
1077
Nick Pigginea9b9902008-04-30 00:54:42 -07001078 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001079 flush_dcache_page(page);
1080
Roman Gushchin3ca81382015-10-12 16:33:44 +03001081 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001082 if (!tmp) {
1083 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001084 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 bytes = min(bytes, iov_iter_single_seg_count(ii));
1086 goto again;
1087 }
1088
1089 err = 0;
1090 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001091 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001092 req->num_pages++;
1093
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 count += tmp;
1095 pos += tmp;
1096 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001097 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001098 offset = 0;
1099
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001100 if (!fc->big_writes)
1101 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001103 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001104
1105 return count > 0 ? count : err;
1106}
1107
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001108static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1109 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001110{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001111 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001112 ((pos + len - 1) >> PAGE_SHIFT) -
1113 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001114 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001115}
1116
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001117static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001118 struct address_space *mapping,
1119 struct iov_iter *ii, loff_t pos)
1120{
1121 struct inode *inode = mapping->host;
1122 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001123 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001124 int err = 0;
1125 ssize_t res = 0;
1126
1127 if (is_bad_inode(inode))
1128 return -EIO;
1129
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001130 if (inode->i_size < pos + iov_iter_count(ii))
1131 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1132
Nick Pigginea9b9902008-04-30 00:54:42 -07001133 do {
1134 struct fuse_req *req;
1135 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001136 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1137 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001138
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001139 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001140 if (IS_ERR(req)) {
1141 err = PTR_ERR(req);
1142 break;
1143 }
1144
1145 count = fuse_fill_write_pages(req, mapping, ii, pos);
1146 if (count <= 0) {
1147 err = count;
1148 } else {
1149 size_t num_written;
1150
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001151 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001152 pos, count);
1153 err = req->out.h.error;
1154 if (!err) {
1155 res += num_written;
1156 pos += num_written;
1157
1158 /* break out of the loop on short write */
1159 if (num_written != count)
1160 err = -EIO;
1161 }
1162 }
1163 fuse_put_request(fc, req);
1164 } while (!err && iov_iter_count(ii));
1165
1166 if (res > 0)
1167 fuse_write_update_size(inode, pos);
1168
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001169 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 fuse_invalidate_attr(inode);
1171
1172 return res > 0 ? res : err;
1173}
1174
Al Viro84c3d552014-04-03 14:33:23 -04001175static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001176{
1177 struct file *file = iocb->ki_filp;
1178 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001179 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001180 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 struct inode *inode = mapping->host;
1182 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001183 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001184
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001185 if (get_fuse_conn(inode)->writeback_cache) {
1186 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001187 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001188 if (err)
1189 return err;
1190
Al Viro84c3d552014-04-03 14:33:23 -04001191 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001192 }
1193
Al Viro59551022016-01-22 15:40:57 -05001194 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001195
1196 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001197 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001198
Al Viro3309dd02015-04-09 12:55:47 -04001199 err = generic_write_checks(iocb, from);
1200 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001201 goto out;
1202
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001203 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001204 if (err)
1205 goto out;
1206
Josef Bacikc3b2da32012-03-26 09:59:21 -04001207 err = file_update_time(file);
1208 if (err)
1209 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001210
Al Viro2ba48ce2015-04-09 13:52:01 -04001211 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001212 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001213 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001214 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001215 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001216
Anand Avati4273b792012-02-17 12:46:25 -05001217 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001218
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001219 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001220 if (written_buffered < 0) {
1221 err = written_buffered;
1222 goto out;
1223 }
1224 endbyte = pos + written_buffered - 1;
1225
1226 err = filemap_write_and_wait_range(file->f_mapping, pos,
1227 endbyte);
1228 if (err)
1229 goto out;
1230
1231 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 pos >> PAGE_SHIFT,
1233 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001234
1235 written += written_buffered;
1236 iocb->ki_pos = pos + written_buffered;
1237 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001238 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001239 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001240 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001241 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001242out:
1243 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001244 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001245 if (written > 0)
1246 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001247
1248 return written ? written : err;
1249}
1250
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001251static inline void fuse_page_descs_length_init(struct fuse_req *req,
1252 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001253{
1254 int i;
1255
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001256 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001257 req->page_descs[i].length = PAGE_SIZE -
1258 req->page_descs[i].offset;
1259}
1260
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001261static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1262{
1263 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1264}
1265
1266static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1267 size_t max_size)
1268{
1269 return min(iov_iter_single_seg_count(ii), max_size);
1270}
1271
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001272static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001273 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001274{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001275 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001276 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001277
Miklos Szeredif4975c62009-04-02 14:25:34 +02001278 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001279 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001280 unsigned long user_addr = fuse_get_user_addr(ii);
1281 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1282
Miklos Szeredif4975c62009-04-02 14:25:34 +02001283 if (write)
1284 req->in.args[1].value = (void *) user_addr;
1285 else
1286 req->out.args[0].value = (void *) user_addr;
1287
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001288 iov_iter_advance(ii, frag_size);
1289 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001290 return 0;
1291 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001292
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001293 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001294 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001295 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001296 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001297 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001298 req->max_pages - req->num_pages,
1299 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001300 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001301 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001302
Al Viroc9c37e22014-03-16 16:08:30 -04001303 iov_iter_advance(ii, ret);
1304 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001305
Al Viroc9c37e22014-03-16 16:08:30 -04001306 ret += start;
1307 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1308
1309 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310 fuse_page_descs_length_init(req, req->num_pages, npages);
1311
1312 req->num_pages += npages;
1313 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001314 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001315 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001316
1317 if (write)
1318 req->in.argpages = 1;
1319 else
1320 req->out.argpages = 1;
1321
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001322 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001323
Ashish Samant2c932d42016-03-25 10:53:41 -07001324 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001325}
1326
Al Virod22a9432014-03-16 15:50:47 -04001327ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1328 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001329{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001330 int write = flags & FUSE_DIO_WRITE;
1331 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001332 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001333 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001334 struct fuse_file *ff = file->private_data;
1335 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001336 size_t nmax = write ? fc->max_write : fc->max_read;
1337 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001338 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 pgoff_t idx_from = pos >> PAGE_SHIFT;
1340 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001341 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001342 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001343 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001344
Brian Fosterde82b922013-05-14 11:25:39 -04001345 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001346 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1347 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001348 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001349 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001350 if (IS_ERR(req))
1351 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001352
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001353 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1354 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001355 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001356 fuse_sync_writes(inode);
1357 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001358 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001359 }
1360
Ashish Samant61c12b42017-07-12 19:26:58 -07001361 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001364 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001365 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001366 err = fuse_get_user_pages(req, iter, &nbytes, write);
1367 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001369
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001371 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001372 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001373 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001374
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001375 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001376 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001377 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001378 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379 break;
1380 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001381 res = 0;
1382 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383 break;
1384 }
1385 count -= nres;
1386 res += nres;
1387 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001388 if (nres != nbytes)
1389 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001390 if (count) {
1391 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001392 if (io->async)
1393 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001394 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001395 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001396 req = fuse_get_req(fc, iov_iter_npages(iter,
1397 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001398 if (IS_ERR(req))
1399 break;
1400 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001401 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001402 if (!IS_ERR(req))
1403 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001404 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001406
Ashish Samant742f9922016-03-14 21:57:35 -07001407 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408}
Tejun Heo08cbf542009-04-14 10:54:53 +09001409EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001410
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001411static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001412 struct iov_iter *iter,
1413 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001414{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001415 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001416 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001417
1418 if (is_bad_inode(inode))
1419 return -EIO;
1420
Al Virod22a9432014-03-16 15:50:47 -04001421 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001422
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001423 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001424
1425 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001426}
1427
Al Viro153162632015-03-30 22:08:36 -04001428static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001429{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001430 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001431 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001432}
1433
Al Viro153162632015-03-30 22:08:36 -04001434static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001435{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001436 struct inode *inode = file_inode(iocb->ki_filp);
1437 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001438 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001439
1440 if (is_bad_inode(inode))
1441 return -EIO;
1442
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001443 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001444 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001445 res = generic_write_checks(iocb, from);
1446 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001447 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001448 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001449 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001450 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001451 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001452
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001453 return res;
1454}
1455
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001456static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001457{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001458 int i;
1459
1460 for (i = 0; i < req->num_pages; i++)
1461 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001462
1463 if (req->ff)
1464 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001465}
1466
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001467static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001468{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001469 struct inode *inode = req->inode;
1470 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001471 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001472 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001473
1474 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001475 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001476 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001477 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001478 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001479 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001480 wake_up(&fi->page_waitq);
1481}
1482
1483/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001484static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1485 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001486__releases(fc->lock)
1487__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001488{
1489 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001490 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001491 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001492 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493
1494 if (!fc->connected)
1495 goto out_free;
1496
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001497 if (inarg->offset + data_size <= size) {
1498 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001499 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001500 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001501 } else {
1502 /* Got truncated off completely */
1503 goto out_free;
1504 }
1505
1506 req->in.args[1].size = inarg->size;
1507 fi->writectr++;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001508 queued = fuse_request_queue_background(fc, req);
1509 WARN_ON(!queued);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001510 return;
1511
1512 out_free:
1513 fuse_writepage_finish(fc, req);
1514 spin_unlock(&fc->lock);
1515 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001516 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001517 spin_lock(&fc->lock);
1518}
1519
1520/*
1521 * If fi->writectr is positive (no truncate or fsync going on) send
1522 * all queued writepage requests.
1523 *
1524 * Called with fc->lock
1525 */
1526void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001527__releases(fc->lock)
1528__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529{
1530 struct fuse_conn *fc = get_fuse_conn(inode);
1531 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001532 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001533 struct fuse_req *req;
1534
1535 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1536 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1537 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001538 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539 }
1540}
1541
1542static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1543{
1544 struct inode *inode = req->inode;
1545 struct fuse_inode *fi = get_fuse_inode(inode);
1546
1547 mapping_set_error(inode->i_mapping, req->out.h.error);
1548 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001549 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001550 struct fuse_conn *fc = get_fuse_conn(inode);
1551 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001552 struct fuse_req *next = req->misc.write.next;
1553 req->misc.write.next = next->misc.write.next;
1554 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001555 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001556 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001557
1558 /*
1559 * Skip fuse_flush_writepages() to make it easy to crop requests
1560 * based on primary request size.
1561 *
1562 * 1st case (trivial): there are no concurrent activities using
1563 * fuse_set/release_nowrite. Then we're on safe side because
1564 * fuse_flush_writepages() would call fuse_send_writepage()
1565 * anyway.
1566 *
1567 * 2nd case: someone called fuse_set_nowrite and it is waiting
1568 * now for completion of all in-flight requests. This happens
1569 * rarely and no more than once per page, so this should be
1570 * okay.
1571 *
1572 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1573 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1574 * that fuse_set_nowrite returned implies that all in-flight
1575 * requests were completed along with all of their secondary
1576 * requests. Further primary requests are blocked by negative
1577 * writectr. Hence there cannot be any in-flight requests and
1578 * no invocations of fuse_writepage_end() while we're in
1579 * fuse_set_nowrite..fuse_release_nowrite section.
1580 */
1581 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001582 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001583 fi->writectr--;
1584 fuse_writepage_finish(fc, req);
1585 spin_unlock(&fc->lock);
1586 fuse_writepage_free(fc, req);
1587}
1588
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001589static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1590 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001591{
Miklos Szeredi72523422013-10-01 16:44:52 +02001592 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001593
1594 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001595 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001596 ff = list_entry(fi->write_files.next, struct fuse_file,
1597 write_entry);
1598 fuse_file_get(ff);
1599 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001600 spin_unlock(&fc->lock);
1601
1602 return ff;
1603}
1604
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001605static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1606 struct fuse_inode *fi)
1607{
1608 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1609 WARN_ON(!ff);
1610 return ff;
1611}
1612
1613int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1614{
1615 struct fuse_conn *fc = get_fuse_conn(inode);
1616 struct fuse_inode *fi = get_fuse_inode(inode);
1617 struct fuse_file *ff;
1618 int err;
1619
1620 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001621 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001622 if (ff)
1623 fuse_file_put(ff, 0);
1624
1625 return err;
1626}
1627
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001628static int fuse_writepage_locked(struct page *page)
1629{
1630 struct address_space *mapping = page->mapping;
1631 struct inode *inode = mapping->host;
1632 struct fuse_conn *fc = get_fuse_conn(inode);
1633 struct fuse_inode *fi = get_fuse_inode(inode);
1634 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001635 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001636 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001637
1638 set_page_writeback(page);
1639
Maxim Patlasov4250c062012-10-26 19:48:07 +04001640 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001641 if (!req)
1642 goto err;
1643
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001644 /* writeback always goes to bg_queue */
1645 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001646 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1647 if (!tmp_page)
1648 goto err_free;
1649
Miklos Szeredi72523422013-10-01 16:44:52 +02001650 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001651 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001652 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001653 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001654
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001655 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001656
1657 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001658 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001659 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001660 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001661 req->num_pages = 1;
1662 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001663 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001664 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001665 req->end = fuse_writepage_end;
1666 req->inode = inode;
1667
Tejun Heo93f78d82015-05-22 17:13:27 -04001668 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001669 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001670
1671 spin_lock(&fc->lock);
1672 list_add(&req->writepages_entry, &fi->writepages);
1673 list_add_tail(&req->list, &fi->queued_writes);
1674 fuse_flush_writepages(inode);
1675 spin_unlock(&fc->lock);
1676
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001677 end_page_writeback(page);
1678
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001679 return 0;
1680
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001681err_nofile:
1682 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683err_free:
1684 fuse_request_free(req);
1685err:
Jeff Layton91839762017-05-25 06:57:50 -04001686 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001687 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001688 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001689}
1690
1691static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1692{
1693 int err;
1694
Miklos Szerediff17be02013-10-01 16:44:53 +02001695 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1696 /*
1697 * ->writepages() should be called for sync() and friends. We
1698 * should only get here on direct reclaim and then we are
1699 * allowed to skip a page which is already in flight
1700 */
1701 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1702
1703 redirty_page_for_writepage(wbc, page);
1704 return 0;
1705 }
1706
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001707 err = fuse_writepage_locked(page);
1708 unlock_page(page);
1709
1710 return err;
1711}
1712
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001713struct fuse_fill_wb_data {
1714 struct fuse_req *req;
1715 struct fuse_file *ff;
1716 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001717 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001718};
1719
1720static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1721{
1722 struct fuse_req *req = data->req;
1723 struct inode *inode = data->inode;
1724 struct fuse_conn *fc = get_fuse_conn(inode);
1725 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001726 int num_pages = req->num_pages;
1727 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001728
1729 req->ff = fuse_file_get(data->ff);
1730 spin_lock(&fc->lock);
1731 list_add_tail(&req->list, &fi->queued_writes);
1732 fuse_flush_writepages(inode);
1733 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001734
1735 for (i = 0; i < num_pages; i++)
1736 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001737}
1738
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001739static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1740 struct page *page)
1741{
1742 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1743 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1744 struct fuse_req *tmp;
1745 struct fuse_req *old_req;
1746 bool found = false;
1747 pgoff_t curr_index;
1748
1749 BUG_ON(new_req->num_pages != 0);
1750
1751 spin_lock(&fc->lock);
1752 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001753 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1754 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001755 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001756 if (curr_index <= page->index &&
1757 page->index < curr_index + old_req->num_pages) {
1758 found = true;
1759 break;
1760 }
1761 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001762 if (!found) {
1763 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001764 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001765 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001766
Maxim Patlasovf6011082013-10-02 15:01:07 +04001767 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1769 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001770 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001771 if (tmp->num_pages == 1 &&
1772 curr_index == page->index) {
1773 old_req = tmp;
1774 }
1775 }
1776
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001777 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001778 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001779
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001780 copy_highpage(old_req->pages[0], page);
1781 spin_unlock(&fc->lock);
1782
Tejun Heo93f78d82015-05-22 17:13:27 -04001783 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001784 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001785 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001786 fuse_writepage_free(fc, new_req);
1787 fuse_request_free(new_req);
1788 goto out;
1789 } else {
1790 new_req->misc.write.next = old_req->misc.write.next;
1791 old_req->misc.write.next = new_req;
1792 }
1793out_unlock:
1794 spin_unlock(&fc->lock);
1795out:
1796 return found;
1797}
1798
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001799static int fuse_writepages_fill(struct page *page,
1800 struct writeback_control *wbc, void *_data)
1801{
1802 struct fuse_fill_wb_data *data = _data;
1803 struct fuse_req *req = data->req;
1804 struct inode *inode = data->inode;
1805 struct fuse_conn *fc = get_fuse_conn(inode);
1806 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001807 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001808 int err;
1809
1810 if (!data->ff) {
1811 err = -EIO;
1812 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1813 if (!data->ff)
1814 goto out_unlock;
1815 }
1816
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001817 /*
1818 * Being under writeback is unlikely but possible. For example direct
1819 * read to an mmaped fuse file will set the page dirty twice; once when
1820 * the pages are faulted with get_user_pages(), and then after the read
1821 * completed.
1822 */
1823 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001824
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001825 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001826 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001827 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001828 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1829 fuse_writepages_send(data);
1830 data->req = NULL;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001831 } else if (req && req->num_pages == req->max_pages) {
1832 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1833 fuse_writepages_send(data);
1834 req = data->req = NULL;
1835 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001836 }
Miklos Szeredie52a8252018-10-01 10:07:06 +02001837
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001838 err = -ENOMEM;
1839 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1840 if (!tmp_page)
1841 goto out_unlock;
1842
1843 /*
1844 * The page must not be redirtied until the writeout is completed
1845 * (i.e. userspace has sent a reply to the write request). Otherwise
1846 * there could be more than one temporary page instance for each real
1847 * page.
1848 *
1849 * This is ensured by holding the page lock in page_mkwrite() while
1850 * checking fuse_page_is_writeback(). We already hold the page lock
1851 * since clear_page_dirty_for_io() and keep it held until we add the
1852 * request to the fi->writepages list and increment req->num_pages.
1853 * After this fuse_page_is_writeback() will indicate that the page is
1854 * under writeback, so we can release the page lock.
1855 */
1856 if (data->req == NULL) {
1857 struct fuse_inode *fi = get_fuse_inode(inode);
1858
1859 err = -ENOMEM;
Miklos Szeredie52a8252018-10-01 10:07:06 +02001860 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001861 if (!req) {
1862 __free_page(tmp_page);
1863 goto out_unlock;
1864 }
1865
1866 fuse_write_fill(req, data->ff, page_offset(page), 0);
1867 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001868 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001869 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001870 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001871 req->num_pages = 0;
1872 req->end = fuse_writepage_end;
1873 req->inode = inode;
1874
1875 spin_lock(&fc->lock);
1876 list_add(&req->writepages_entry, &fi->writepages);
1877 spin_unlock(&fc->lock);
1878
1879 data->req = req;
1880 }
1881 set_page_writeback(page);
1882
1883 copy_highpage(tmp_page, page);
1884 req->pages[req->num_pages] = tmp_page;
1885 req->page_descs[req->num_pages].offset = 0;
1886 req->page_descs[req->num_pages].length = PAGE_SIZE;
1887
Tejun Heo93f78d82015-05-22 17:13:27 -04001888 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001889 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001890
1891 err = 0;
1892 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1893 end_page_writeback(page);
1894 data->req = NULL;
1895 goto out_unlock;
1896 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001897 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001898
1899 /*
1900 * Protected by fc->lock against concurrent access by
1901 * fuse_page_is_writeback().
1902 */
1903 spin_lock(&fc->lock);
1904 req->num_pages++;
1905 spin_unlock(&fc->lock);
1906
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001907out_unlock:
1908 unlock_page(page);
1909
1910 return err;
1911}
1912
1913static int fuse_writepages(struct address_space *mapping,
1914 struct writeback_control *wbc)
1915{
1916 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001917 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001918 struct fuse_fill_wb_data data;
1919 int err;
1920
1921 err = -EIO;
1922 if (is_bad_inode(inode))
1923 goto out;
1924
1925 data.inode = inode;
1926 data.req = NULL;
1927 data.ff = NULL;
1928
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001929 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001930 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001931 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001932 GFP_NOFS);
1933 if (!data.orig_pages)
1934 goto out;
1935
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001936 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1937 if (data.req) {
1938 /* Ignore errors if we can write at least one page */
1939 BUG_ON(!data.req->num_pages);
1940 fuse_writepages_send(&data);
1941 err = 0;
1942 }
1943 if (data.ff)
1944 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001945
1946 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001947out:
1948 return err;
1949}
1950
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001951/*
1952 * It's worthy to make sure that space is reserved on disk for the write,
1953 * but how to implement it without killing performance need more thinking.
1954 */
1955static int fuse_write_begin(struct file *file, struct address_space *mapping,
1956 loff_t pos, unsigned len, unsigned flags,
1957 struct page **pagep, void **fsdata)
1958{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001959 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001960 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001961 struct page *page;
1962 loff_t fsize;
1963 int err = -ENOMEM;
1964
1965 WARN_ON(!fc->writeback_cache);
1966
1967 page = grab_cache_page_write_begin(mapping, index, flags);
1968 if (!page)
1969 goto error;
1970
1971 fuse_wait_on_page_writeback(mapping->host, page->index);
1972
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001973 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001974 goto success;
1975 /*
1976 * Check if the start this page comes after the end of file, in which
1977 * case the readpage can be optimized away.
1978 */
1979 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001980 if (fsize <= (pos & PAGE_MASK)) {
1981 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001982 if (off)
1983 zero_user_segment(page, 0, off);
1984 goto success;
1985 }
1986 err = fuse_do_readpage(file, page);
1987 if (err)
1988 goto cleanup;
1989success:
1990 *pagep = page;
1991 return 0;
1992
1993cleanup:
1994 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001996error:
1997 return err;
1998}
1999
2000static int fuse_write_end(struct file *file, struct address_space *mapping,
2001 loff_t pos, unsigned len, unsigned copied,
2002 struct page *page, void *fsdata)
2003{
2004 struct inode *inode = page->mapping->host;
2005
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002006 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2007 if (!copied)
2008 goto unlock;
2009
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002010 if (!PageUptodate(page)) {
2011 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002012 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002013 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002014 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002015 SetPageUptodate(page);
2016 }
2017
2018 fuse_write_update_size(inode, pos + copied);
2019 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002020
2021unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002022 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002023 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002024
2025 return copied;
2026}
2027
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002028static int fuse_launder_page(struct page *page)
2029{
2030 int err = 0;
2031 if (clear_page_dirty_for_io(page)) {
2032 struct inode *inode = page->mapping->host;
2033 err = fuse_writepage_locked(page);
2034 if (!err)
2035 fuse_wait_on_page_writeback(inode, page->index);
2036 }
2037 return err;
2038}
2039
2040/*
2041 * Write back dirty pages now, because there may not be any suitable
2042 * open files later
2043 */
2044static void fuse_vma_close(struct vm_area_struct *vma)
2045{
2046 filemap_write_and_wait(vma->vm_file->f_mapping);
2047}
2048
2049/*
2050 * Wait for writeback against this page to complete before allowing it
2051 * to be marked dirty again, and hence written back again, possibly
2052 * before the previous writepage completed.
2053 *
2054 * Block here, instead of in ->writepage(), so that the userspace fs
2055 * can only block processes actually operating on the filesystem.
2056 *
2057 * Otherwise unprivileged userspace fs would be able to block
2058 * unrelated:
2059 *
2060 * - page migration
2061 * - sync(2)
2062 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2063 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302064static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002065{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002066 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002067 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002068
Dave Jiang11bac802017-02-24 14:56:41 -08002069 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002070 lock_page(page);
2071 if (page->mapping != inode->i_mapping) {
2072 unlock_page(page);
2073 return VM_FAULT_NOPAGE;
2074 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002075
2076 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002077 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002078}
2079
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002080static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002081 .close = fuse_vma_close,
2082 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002083 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002084 .page_mkwrite = fuse_page_mkwrite,
2085};
2086
2087static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2088{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002089 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2090 fuse_link_write_file(file);
2091
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002092 file_accessed(file);
2093 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002094 return 0;
2095}
2096
Miklos Szeredifc280c92009-04-02 14:25:35 +02002097static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2098{
2099 /* Can't provide the coherency needed for MAP_SHARED */
2100 if (vma->vm_flags & VM_MAYSHARE)
2101 return -ENODEV;
2102
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002103 invalidate_inode_pages2(file->f_mapping);
2104
Miklos Szeredifc280c92009-04-02 14:25:35 +02002105 return generic_file_mmap(file, vma);
2106}
2107
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002108static int convert_fuse_file_lock(struct fuse_conn *fc,
2109 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002110 struct file_lock *fl)
2111{
2112 switch (ffl->type) {
2113 case F_UNLCK:
2114 break;
2115
2116 case F_RDLCK:
2117 case F_WRLCK:
2118 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2119 ffl->end < ffl->start)
2120 return -EIO;
2121
2122 fl->fl_start = ffl->start;
2123 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002124
2125 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002126 * Convert pid into init's pid namespace. The locks API will
2127 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002128 */
2129 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002130 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002131 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002132 break;
2133
2134 default:
2135 return -EIO;
2136 }
2137 fl->fl_type = ffl->type;
2138 return 0;
2139}
2140
Miklos Szeredi70781872014-12-12 09:49:05 +01002141static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002142 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002143 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002144{
Al Viro6131ffa2013-02-27 16:59:05 -05002145 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002146 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002147 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002148
Miklos Szeredi70781872014-12-12 09:49:05 +01002149 memset(inarg, 0, sizeof(*inarg));
2150 inarg->fh = ff->fh;
2151 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2152 inarg->lk.start = fl->fl_start;
2153 inarg->lk.end = fl->fl_end;
2154 inarg->lk.type = fl->fl_type;
2155 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002156 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002157 inarg->lk_flags |= FUSE_LK_FLOCK;
2158 args->in.h.opcode = opcode;
2159 args->in.h.nodeid = get_node_id(inode);
2160 args->in.numargs = 1;
2161 args->in.args[0].size = sizeof(*inarg);
2162 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002163}
2164
2165static int fuse_getlk(struct file *file, struct file_lock *fl)
2166{
Al Viro6131ffa2013-02-27 16:59:05 -05002167 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002169 FUSE_ARGS(args);
2170 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002171 struct fuse_lk_out outarg;
2172 int err;
2173
Miklos Szeredi70781872014-12-12 09:49:05 +01002174 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2175 args.out.numargs = 1;
2176 args.out.args[0].size = sizeof(outarg);
2177 args.out.args[0].value = &outarg;
2178 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002179 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002180 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002181
2182 return err;
2183}
2184
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002185static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002186{
Al Viro6131ffa2013-02-27 16:59:05 -05002187 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002188 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002189 FUSE_ARGS(args);
2190 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002192 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2193 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002194 int err;
2195
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002196 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002197 /* NLM needs asynchronous locks, which we don't support yet */
2198 return -ENOLCK;
2199 }
2200
Miklos Szeredi71421252006-06-25 05:48:52 -07002201 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002202 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002203 return 0;
2204
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002205 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002206 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002207
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002208 /* locking is restartable */
2209 if (err == -EINTR)
2210 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002211
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 return err;
2213}
2214
2215static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2216{
Al Viro6131ffa2013-02-27 16:59:05 -05002217 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002218 struct fuse_conn *fc = get_fuse_conn(inode);
2219 int err;
2220
Miklos Szeredi48e90762008-07-25 01:49:02 -07002221 if (cmd == F_CANCELLK) {
2222 err = 0;
2223 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002224 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002225 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002226 err = 0;
2227 } else
2228 err = fuse_getlk(file, fl);
2229 } else {
2230 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002231 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002232 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002233 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002234 }
2235 return err;
2236}
2237
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2239{
Al Viro6131ffa2013-02-27 16:59:05 -05002240 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002241 struct fuse_conn *fc = get_fuse_conn(inode);
2242 int err;
2243
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002244 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002245 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002246 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002247 struct fuse_file *ff = file->private_data;
2248
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002249 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002250 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002251 err = fuse_setlk(file, fl, 1);
2252 }
2253
2254 return err;
2255}
2256
Miklos Szeredib2d22722006-12-06 20:35:51 -08002257static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2258{
2259 struct inode *inode = mapping->host;
2260 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002261 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002262 struct fuse_bmap_in inarg;
2263 struct fuse_bmap_out outarg;
2264 int err;
2265
2266 if (!inode->i_sb->s_bdev || fc->no_bmap)
2267 return 0;
2268
Miklos Szeredib2d22722006-12-06 20:35:51 -08002269 memset(&inarg, 0, sizeof(inarg));
2270 inarg.block = block;
2271 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002272 args.in.h.opcode = FUSE_BMAP;
2273 args.in.h.nodeid = get_node_id(inode);
2274 args.in.numargs = 1;
2275 args.in.args[0].size = sizeof(inarg);
2276 args.in.args[0].value = &inarg;
2277 args.out.numargs = 1;
2278 args.out.args[0].size = sizeof(outarg);
2279 args.out.args[0].value = &outarg;
2280 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002281 if (err == -ENOSYS)
2282 fc->no_bmap = 1;
2283
2284 return err ? 0 : outarg.block;
2285}
2286
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302287static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2288{
2289 struct inode *inode = file->f_mapping->host;
2290 struct fuse_conn *fc = get_fuse_conn(inode);
2291 struct fuse_file *ff = file->private_data;
2292 FUSE_ARGS(args);
2293 struct fuse_lseek_in inarg = {
2294 .fh = ff->fh,
2295 .offset = offset,
2296 .whence = whence
2297 };
2298 struct fuse_lseek_out outarg;
2299 int err;
2300
2301 if (fc->no_lseek)
2302 goto fallback;
2303
2304 args.in.h.opcode = FUSE_LSEEK;
2305 args.in.h.nodeid = ff->nodeid;
2306 args.in.numargs = 1;
2307 args.in.args[0].size = sizeof(inarg);
2308 args.in.args[0].value = &inarg;
2309 args.out.numargs = 1;
2310 args.out.args[0].size = sizeof(outarg);
2311 args.out.args[0].value = &outarg;
2312 err = fuse_simple_request(fc, &args);
2313 if (err) {
2314 if (err == -ENOSYS) {
2315 fc->no_lseek = 1;
2316 goto fallback;
2317 }
2318 return err;
2319 }
2320
2321 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2322
2323fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002324 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302325 if (!err)
2326 return generic_file_llseek(file, offset, whence);
2327 else
2328 return err;
2329}
2330
Andrew Morton965c8e52012-12-17 15:59:39 -08002331static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002332{
2333 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002334 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002335
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302336 switch (whence) {
2337 case SEEK_SET:
2338 case SEEK_CUR:
2339 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002340 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302341 break;
2342 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002343 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002344 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302345 if (!retval)
2346 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002347 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302348 break;
2349 case SEEK_HOLE:
2350 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002351 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302352 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002353 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302354 break;
2355 default:
2356 retval = -EINVAL;
2357 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002358
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002359 return retval;
2360}
2361
Tejun Heo59efec72008-11-26 12:03:55 +01002362/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002363 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2364 * ABI was defined to be 'struct iovec' which is different on 32bit
2365 * and 64bit. Fortunately we can determine which structure the server
2366 * used from the size of the reply.
2367 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002368static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2369 size_t transferred, unsigned count,
2370 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002371{
2372#ifdef CONFIG_COMPAT
2373 if (count * sizeof(struct compat_iovec) == transferred) {
2374 struct compat_iovec *ciov = src;
2375 unsigned i;
2376
2377 /*
2378 * With this interface a 32bit server cannot support
2379 * non-compat (i.e. ones coming from 64bit apps) ioctl
2380 * requests
2381 */
2382 if (!is_compat)
2383 return -EINVAL;
2384
2385 for (i = 0; i < count; i++) {
2386 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2387 dst[i].iov_len = ciov[i].iov_len;
2388 }
2389 return 0;
2390 }
2391#endif
2392
2393 if (count * sizeof(struct iovec) != transferred)
2394 return -EIO;
2395
2396 memcpy(dst, src, transferred);
2397 return 0;
2398}
2399
Miklos Szeredi75727772010-11-30 16:39:27 +01002400/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002401static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2402 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002403{
2404 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002405 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002406
Zach Brownfb6ccff2012-07-24 12:10:11 -07002407 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002408 if (iov->iov_len > (size_t) max)
2409 return -ENOMEM;
2410 max -= iov->iov_len;
2411 }
2412 return 0;
2413}
2414
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002415static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2416 void *src, size_t transferred, unsigned count,
2417 bool is_compat)
2418{
2419 unsigned i;
2420 struct fuse_ioctl_iovec *fiov = src;
2421
2422 if (fc->minor < 16) {
2423 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2424 count, is_compat);
2425 }
2426
2427 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2428 return -EIO;
2429
2430 for (i = 0; i < count; i++) {
2431 /* Did the server supply an inappropriate value? */
2432 if (fiov[i].base != (unsigned long) fiov[i].base ||
2433 fiov[i].len != (unsigned long) fiov[i].len)
2434 return -EIO;
2435
2436 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2437 dst[i].iov_len = (size_t) fiov[i].len;
2438
2439#ifdef CONFIG_COMPAT
2440 if (is_compat &&
2441 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2442 (compat_size_t) dst[i].iov_len != fiov[i].len))
2443 return -EIO;
2444#endif
2445 }
2446
2447 return 0;
2448}
2449
2450
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002451/*
Tejun Heo59efec72008-11-26 12:03:55 +01002452 * For ioctls, there is no generic way to determine how much memory
2453 * needs to be read and/or written. Furthermore, ioctls are allowed
2454 * to dereference the passed pointer, so the parameter requires deep
2455 * copying but FUSE has no idea whatsoever about what to copy in or
2456 * out.
2457 *
2458 * This is solved by allowing FUSE server to retry ioctl with
2459 * necessary in/out iovecs. Let's assume the ioctl implementation
2460 * needs to read in the following structure.
2461 *
2462 * struct a {
2463 * char *buf;
2464 * size_t buflen;
2465 * }
2466 *
2467 * On the first callout to FUSE server, inarg->in_size and
2468 * inarg->out_size will be NULL; then, the server completes the ioctl
2469 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2470 * the actual iov array to
2471 *
2472 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2473 *
2474 * which tells FUSE to copy in the requested area and retry the ioctl.
2475 * On the second round, the server has access to the structure and
2476 * from that it can tell what to look for next, so on the invocation,
2477 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2478 *
2479 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2480 * { .iov_base = a.buf, .iov_len = a.buflen } }
2481 *
2482 * FUSE will copy both struct a and the pointed buffer from the
2483 * process doing the ioctl and retry ioctl with both struct a and the
2484 * buffer.
2485 *
2486 * This time, FUSE server has everything it needs and completes ioctl
2487 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2488 *
2489 * Copying data out works the same way.
2490 *
2491 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2492 * automatically initializes in and out iovs by decoding @cmd with
2493 * _IOC_* macros and the server is not allowed to request RETRY. This
2494 * limits ioctl data transfers to well-formed ioctls and is the forced
2495 * behavior for all FUSE servers.
2496 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002497long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2498 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002499{
Tejun Heo59efec72008-11-26 12:03:55 +01002500 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002501 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002502 struct fuse_ioctl_in inarg = {
2503 .fh = ff->fh,
2504 .cmd = cmd,
2505 .arg = arg,
2506 .flags = flags
2507 };
2508 struct fuse_ioctl_out outarg;
2509 struct fuse_req *req = NULL;
2510 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002511 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002512 struct iovec *in_iov = NULL, *out_iov = NULL;
2513 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002514 size_t in_size, out_size, transferred, c;
2515 int err, i;
2516 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002517
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002518#if BITS_PER_LONG == 32
2519 inarg.flags |= FUSE_IOCTL_32BIT;
2520#else
2521 if (flags & FUSE_IOCTL_COMPAT)
2522 inarg.flags |= FUSE_IOCTL_32BIT;
2523#endif
2524
Tejun Heo59efec72008-11-26 12:03:55 +01002525 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002526 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002527
Tejun Heo59efec72008-11-26 12:03:55 +01002528 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002529 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002530 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002531 if (!pages || !iov_page)
2532 goto out;
2533
2534 /*
2535 * If restricted, initialize IO parameters as encoded in @cmd.
2536 * RETRY from server is not allowed.
2537 */
2538 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002539 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002540
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002541 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002542 iov->iov_len = _IOC_SIZE(cmd);
2543
2544 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2545 in_iov = iov;
2546 in_iovs = 1;
2547 }
2548
2549 if (_IOC_DIR(cmd) & _IOC_READ) {
2550 out_iov = iov;
2551 out_iovs = 1;
2552 }
2553 }
2554
2555 retry:
2556 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2557 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2558
2559 /*
2560 * Out data can be used either for actual out data or iovs,
2561 * make sure there always is at least one page.
2562 */
2563 out_size = max_t(size_t, out_size, PAGE_SIZE);
2564 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2565
2566 /* make sure there are enough buffer pages and init request with them */
2567 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002568 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002569 goto out;
2570 while (num_pages < max_pages) {
2571 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2572 if (!pages[num_pages])
2573 goto out;
2574 num_pages++;
2575 }
2576
Maxim Patlasov54b96672012-10-26 19:49:13 +04002577 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002578 if (IS_ERR(req)) {
2579 err = PTR_ERR(req);
2580 req = NULL;
2581 goto out;
2582 }
2583 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2584 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002585 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002586
2587 /* okay, let's send it to the client */
2588 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002589 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002590 req->in.numargs = 1;
2591 req->in.args[0].size = sizeof(inarg);
2592 req->in.args[0].value = &inarg;
2593 if (in_size) {
2594 req->in.numargs++;
2595 req->in.args[1].size = in_size;
2596 req->in.argpages = 1;
2597
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002598 err = -EFAULT;
2599 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2600 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2601 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2602 if (c != PAGE_SIZE && iov_iter_count(&ii))
2603 goto out;
2604 }
Tejun Heo59efec72008-11-26 12:03:55 +01002605 }
2606
2607 req->out.numargs = 2;
2608 req->out.args[0].size = sizeof(outarg);
2609 req->out.args[0].value = &outarg;
2610 req->out.args[1].size = out_size;
2611 req->out.argpages = 1;
2612 req->out.argvar = 1;
2613
Tejun Heob93f8582008-11-26 12:03:55 +01002614 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002615 err = req->out.h.error;
2616 transferred = req->out.args[1].size;
2617 fuse_put_request(fc, req);
2618 req = NULL;
2619 if (err)
2620 goto out;
2621
2622 /* did it ask for retry? */
2623 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002624 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002625
2626 /* no retry if in restricted mode */
2627 err = -EIO;
2628 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2629 goto out;
2630
2631 in_iovs = outarg.in_iovs;
2632 out_iovs = outarg.out_iovs;
2633
2634 /*
2635 * Make sure things are in boundary, separate checks
2636 * are to protect against overflow.
2637 */
2638 err = -ENOMEM;
2639 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2640 out_iovs > FUSE_IOCTL_MAX_IOV ||
2641 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2642 goto out;
2643
Cong Wang2408f6e2011-11-25 23:14:30 +08002644 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002645 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002646 transferred, in_iovs + out_iovs,
2647 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002648 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002649 if (err)
2650 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002651
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002652 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002653 out_iov = in_iov + in_iovs;
2654
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002655 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002656 if (err)
2657 goto out;
2658
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002659 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002660 if (err)
2661 goto out;
2662
Tejun Heo59efec72008-11-26 12:03:55 +01002663 goto retry;
2664 }
2665
2666 err = -EIO;
2667 if (transferred > inarg.out_size)
2668 goto out;
2669
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002670 err = -EFAULT;
2671 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2672 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2673 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2674 if (c != PAGE_SIZE && iov_iter_count(&ii))
2675 goto out;
2676 }
2677 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002678 out:
2679 if (req)
2680 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002681 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002682 while (num_pages)
2683 __free_page(pages[--num_pages]);
2684 kfree(pages);
2685
2686 return err ? err : outarg.result;
2687}
Tejun Heo08cbf542009-04-14 10:54:53 +09002688EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002689
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002690long fuse_ioctl_common(struct file *file, unsigned int cmd,
2691 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002692{
Al Viro6131ffa2013-02-27 16:59:05 -05002693 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002694 struct fuse_conn *fc = get_fuse_conn(inode);
2695
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002696 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002697 return -EACCES;
2698
2699 if (is_bad_inode(inode))
2700 return -EIO;
2701
2702 return fuse_do_ioctl(file, cmd, arg, flags);
2703}
2704
Tejun Heo59efec72008-11-26 12:03:55 +01002705static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2706 unsigned long arg)
2707{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002708 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002709}
2710
2711static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2712 unsigned long arg)
2713{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002714 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002715}
2716
Tejun Heo95668a62008-11-26 12:03:55 +01002717/*
2718 * All files which have been polled are linked to RB tree
2719 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2720 * find the matching one.
2721 */
2722static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2723 struct rb_node **parent_out)
2724{
2725 struct rb_node **link = &fc->polled_files.rb_node;
2726 struct rb_node *last = NULL;
2727
2728 while (*link) {
2729 struct fuse_file *ff;
2730
2731 last = *link;
2732 ff = rb_entry(last, struct fuse_file, polled_node);
2733
2734 if (kh < ff->kh)
2735 link = &last->rb_left;
2736 else if (kh > ff->kh)
2737 link = &last->rb_right;
2738 else
2739 return link;
2740 }
2741
2742 if (parent_out)
2743 *parent_out = last;
2744 return link;
2745}
2746
2747/*
2748 * The file is about to be polled. Make sure it's on the polled_files
2749 * RB tree. Note that files once added to the polled_files tree are
2750 * not removed before the file is released. This is because a file
2751 * polled once is likely to be polled again.
2752 */
2753static void fuse_register_polled_file(struct fuse_conn *fc,
2754 struct fuse_file *ff)
2755{
2756 spin_lock(&fc->lock);
2757 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002758 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002759
2760 link = fuse_find_polled_node(fc, ff->kh, &parent);
2761 BUG_ON(*link);
2762 rb_link_node(&ff->polled_node, parent, link);
2763 rb_insert_color(&ff->polled_node, &fc->polled_files);
2764 }
2765 spin_unlock(&fc->lock);
2766}
2767
Al Viro076ccb72017-07-03 01:02:18 -04002768__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002769{
Tejun Heo95668a62008-11-26 12:03:55 +01002770 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002771 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002772 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2773 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002774 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002775 int err;
2776
2777 if (fc->no_poll)
2778 return DEFAULT_POLLMASK;
2779
2780 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002781 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002782
2783 /*
2784 * Ask for notification iff there's someone waiting for it.
2785 * The client may ignore the flag and always notify.
2786 */
2787 if (waitqueue_active(&ff->poll_wait)) {
2788 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2789 fuse_register_polled_file(fc, ff);
2790 }
2791
Miklos Szeredi70781872014-12-12 09:49:05 +01002792 args.in.h.opcode = FUSE_POLL;
2793 args.in.h.nodeid = ff->nodeid;
2794 args.in.numargs = 1;
2795 args.in.args[0].size = sizeof(inarg);
2796 args.in.args[0].value = &inarg;
2797 args.out.numargs = 1;
2798 args.out.args[0].size = sizeof(outarg);
2799 args.out.args[0].value = &outarg;
2800 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002801
2802 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002803 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002804 if (err == -ENOSYS) {
2805 fc->no_poll = 1;
2806 return DEFAULT_POLLMASK;
2807 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002808 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002809}
Tejun Heo08cbf542009-04-14 10:54:53 +09002810EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002811
2812/*
2813 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2814 * wakes up the poll waiters.
2815 */
2816int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2817 struct fuse_notify_poll_wakeup_out *outarg)
2818{
2819 u64 kh = outarg->kh;
2820 struct rb_node **link;
2821
2822 spin_lock(&fc->lock);
2823
2824 link = fuse_find_polled_node(fc, kh, NULL);
2825 if (*link) {
2826 struct fuse_file *ff;
2827
2828 ff = rb_entry(*link, struct fuse_file, polled_node);
2829 wake_up_interruptible_sync(&ff->poll_wait);
2830 }
2831
2832 spin_unlock(&fc->lock);
2833 return 0;
2834}
2835
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002836static void fuse_do_truncate(struct file *file)
2837{
2838 struct inode *inode = file->f_mapping->host;
2839 struct iattr attr;
2840
2841 attr.ia_valid = ATTR_SIZE;
2842 attr.ia_size = i_size_read(inode);
2843
2844 attr.ia_file = file;
2845 attr.ia_valid |= ATTR_FILE;
2846
Jan Kara62490332016-05-26 17:12:41 +02002847 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002848}
2849
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002850static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002851{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002852 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002853}
2854
Anand Avati4273b792012-02-17 12:46:25 -05002855static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002856fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002857{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002858 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002859 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002860 struct file *file = iocb->ki_filp;
2861 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002862 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002863 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002864 struct inode *inode;
2865 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002866 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002867 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002868 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002869
Anand Avati4273b792012-02-17 12:46:25 -05002870 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002871 inode = file->f_mapping->host;
2872 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002873
Omar Sandoval6f673762015-03-16 04:33:52 -07002874 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002875 return 0;
2876
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002877 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002878 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002879 if (offset >= i_size)
2880 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002881 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002882 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002883 }
2884
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002885 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002886 if (!io)
2887 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002888 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002889 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890 io->reqs = 1;
2891 io->bytes = -1;
2892 io->size = 0;
2893 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002894 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002896 /*
2897 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002898 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002899 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002900 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002901 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302902 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002903
2904 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302905 * We cannot asynchronously extend the size of a file.
2906 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002907 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302908 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2909 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002910
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302911 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002912 /*
2913 * Additional reference to keep io around after
2914 * calling fuse_aio_complete()
2915 */
2916 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002917 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002918 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002919
Omar Sandoval6f673762015-03-16 04:33:52 -07002920 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002921 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002922 fuse_invalidate_attr(inode);
2923 } else {
Al Virod22a9432014-03-16 15:50:47 -04002924 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002925 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002926
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002927 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002928 bool blocking = io->blocking;
2929
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002930 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2931
2932 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002933 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002934 return -EIOCBQUEUED;
2935
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002936 wait_for_completion(&wait);
2937 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002938 }
2939
Seth Forshee744742d2016-03-11 10:35:34 -06002940 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002941
Omar Sandoval6f673762015-03-16 04:33:52 -07002942 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002943 if (ret > 0)
2944 fuse_write_update_size(inode, pos);
2945 else if (ret < 0 && offset + count > i_size)
2946 fuse_do_truncate(file);
2947 }
Anand Avati4273b792012-02-17 12:46:25 -05002948
2949 return ret;
2950}
2951
Miklos Szeredicdadb112012-11-10 16:55:56 +01002952static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2953 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002954{
2955 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002956 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002957 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002958 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002959 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002960 struct fuse_fallocate_in inarg = {
2961 .fh = ff->fh,
2962 .offset = offset,
2963 .length = length,
2964 .mode = mode
2965 };
2966 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002967 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2968 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002969
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002970 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2971 return -EOPNOTSUPP;
2972
Miklos Szeredi519c6042012-04-26 10:56:36 +02002973 if (fc->no_fallocate)
2974 return -EOPNOTSUPP;
2975
Maxim Patlasov14c14412013-06-13 12:16:39 +04002976 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002977 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002978 if (mode & FALLOC_FL_PUNCH_HOLE) {
2979 loff_t endbyte = offset + length - 1;
2980 err = filemap_write_and_wait_range(inode->i_mapping,
2981 offset, endbyte);
2982 if (err)
2983 goto out;
2984
2985 fuse_sync_writes(inode);
2986 }
Brian Foster3634a632013-05-17 09:30:32 -04002987 }
2988
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002989 if (!(mode & FALLOC_FL_KEEP_SIZE))
2990 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2991
Miklos Szeredi70781872014-12-12 09:49:05 +01002992 args.in.h.opcode = FUSE_FALLOCATE;
2993 args.in.h.nodeid = ff->nodeid;
2994 args.in.numargs = 1;
2995 args.in.args[0].size = sizeof(inarg);
2996 args.in.args[0].value = &inarg;
2997 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002998 if (err == -ENOSYS) {
2999 fc->no_fallocate = 1;
3000 err = -EOPNOTSUPP;
3001 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003002 if (err)
3003 goto out;
3004
3005 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003006 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3007 bool changed = fuse_write_update_size(inode, offset + length);
3008
Miklos Szeredi93d22692014-04-28 14:19:22 +02003009 if (changed && fc->writeback_cache)
3010 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003011 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003012
3013 if (mode & FALLOC_FL_PUNCH_HOLE)
3014 truncate_pagecache_range(inode, offset, offset + length - 1);
3015
3016 fuse_invalidate_attr(inode);
3017
Brian Foster3634a632013-05-17 09:30:32 -04003018out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003019 if (!(mode & FALLOC_FL_KEEP_SIZE))
3020 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3021
Maxim Patlasovbde52782013-09-13 19:19:54 +04003022 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003023 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003024
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003025 return err;
3026}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003027
Niels de Vos88bc7d52018-08-21 14:36:31 +02003028static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3029 struct file *file_out, loff_t pos_out,
3030 size_t len, unsigned int flags)
3031{
3032 struct fuse_file *ff_in = file_in->private_data;
3033 struct fuse_file *ff_out = file_out->private_data;
3034 struct inode *inode_out = file_inode(file_out);
3035 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3036 struct fuse_conn *fc = ff_in->fc;
3037 FUSE_ARGS(args);
3038 struct fuse_copy_file_range_in inarg = {
3039 .fh_in = ff_in->fh,
3040 .off_in = pos_in,
3041 .nodeid_out = ff_out->nodeid,
3042 .fh_out = ff_out->fh,
3043 .off_out = pos_out,
3044 .len = len,
3045 .flags = flags
3046 };
3047 struct fuse_write_out outarg;
3048 ssize_t err;
3049 /* mark unstable when write-back is not used, and file_out gets
3050 * extended */
3051 bool is_unstable = (!fc->writeback_cache) &&
3052 ((pos_out + len) > inode_out->i_size);
3053
3054 if (fc->no_copy_file_range)
3055 return -EOPNOTSUPP;
3056
3057 inode_lock(inode_out);
3058
3059 if (fc->writeback_cache) {
3060 err = filemap_write_and_wait_range(inode_out->i_mapping,
3061 pos_out, pos_out + len);
3062 if (err)
3063 goto out;
3064
3065 fuse_sync_writes(inode_out);
3066 }
3067
3068 if (is_unstable)
3069 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3070
3071 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3072 args.in.h.nodeid = ff_in->nodeid;
3073 args.in.numargs = 1;
3074 args.in.args[0].size = sizeof(inarg);
3075 args.in.args[0].value = &inarg;
3076 args.out.numargs = 1;
3077 args.out.args[0].size = sizeof(outarg);
3078 args.out.args[0].value = &outarg;
3079 err = fuse_simple_request(fc, &args);
3080 if (err == -ENOSYS) {
3081 fc->no_copy_file_range = 1;
3082 err = -EOPNOTSUPP;
3083 }
3084 if (err)
3085 goto out;
3086
3087 if (fc->writeback_cache) {
3088 fuse_write_update_size(inode_out, pos_out + outarg.size);
3089 file_update_time(file_out);
3090 }
3091
3092 fuse_invalidate_attr(inode_out);
3093
3094 err = outarg.size;
3095out:
3096 if (is_unstable)
3097 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3098
3099 inode_unlock(inode_out);
3100
3101 return err;
3102}
3103
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003104static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003105 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003106 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003107 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003108 .mmap = fuse_file_mmap,
3109 .open = fuse_open,
3110 .flush = fuse_flush,
3111 .release = fuse_release,
3112 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003113 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003114 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003115 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003116 .unlocked_ioctl = fuse_file_ioctl,
3117 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003118 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003119 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003120 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003121};
3122
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003123static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003124 .llseek = fuse_file_llseek,
Al Viro153162632015-03-30 22:08:36 -04003125 .read_iter = fuse_direct_read_iter,
Al Viro153162632015-03-30 22:08:36 -04003126 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003127 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003128 .open = fuse_open,
3129 .flush = fuse_flush,
3130 .release = fuse_release,
3131 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003132 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003133 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003134 .unlocked_ioctl = fuse_file_ioctl,
3135 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003136 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003137 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003138 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003139};
3140
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003141static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003142 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003143 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003144 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003145 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003146 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003147 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003148 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003149 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003150 .write_begin = fuse_write_begin,
3151 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003152};
3153
3154void fuse_init_file_inode(struct inode *inode)
3155{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003156 struct fuse_inode *fi = get_fuse_inode(inode);
3157
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003158 inode->i_fop = &fuse_file_operations;
3159 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003160
3161 INIT_LIST_HEAD(&fi->write_files);
3162 INIT_LIST_HEAD(&fi->queued_writes);
3163 fi->writectr = 0;
3164 init_waitqueue_head(&fi->page_waitq);
3165 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003166}