blob: a59c16bd90accaa2f49d924fd232dfd265be1f98 [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
Chad Austin2e64ff12018-12-10 10:54:52 -080092static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
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
Chad Austin2e64ff12018-12-10 10:54:52 -080097 if (ff->fc->no_open && !isdir) {
Andrew Gallagher7678ac52013-11-05 16:05:52 +010098 /*
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 Patlasov75caeec2014-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 Patlasov75caeec2014-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 Patlasov75caeec2014-04-28 14:19:22 +0200213 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500214 inode_lock(inode);
Maxim Patlasov75caeec2014-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 Patlasov75caeec2014-04-28 14:19:22 +0200218 if (!err)
219 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200220
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200221 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500222 inode_unlock(inode);
Maxim Patlasov75caeec2014-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
Chad Austin2e64ff12018-12-10 10:54:52 -0800250void fuse_release_common(struct file *file, bool isdir)
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;
Chad Austin2e64ff12018-12-10 10:54:52 -0800254 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700255
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200256 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100257
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200258 if (ff->flock) {
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
262 (fl_owner_t) file);
263 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266
Tejun Heo6b2db282009-04-14 10:54:49 +0900267 /*
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100271 *
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900275 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800276 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277}
278
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700279static int fuse_open(struct inode *inode, struct file *file)
280{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200281 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700282}
283
284static int fuse_release(struct inode *inode, struct file *file)
285{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400286 struct fuse_conn *fc = get_fuse_conn(inode);
287
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200290 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400291
Chad Austin2e64ff12018-12-10 10:54:52 -0800292 fuse_release_common(file, false);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200293
294 /* return value is ignored by VFS */
295 return 0;
296}
297
298void fuse_sync_release(struct fuse_file *ff, int flags)
299{
Elena Reshetova4e8c2eb2017-03-03 11:04:03 +0200300 WARN_ON(refcount_read(&ff->count) > 1);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200301 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi267d8442017-02-22 20:08:25 +0100302 /*
303 * iput(NULL) is a no-op and since the refcount is 1 and everything's
304 * synchronous, we are fine with not doing igrab() here"
305 */
Chad Austin2e64ff12018-12-10 10:54:52 -0800306 fuse_file_put(ff, true, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307}
Tejun Heo08cbf542009-04-14 10:54:53 +0900308EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700309
Miklos Szeredi71421252006-06-25 05:48:52 -0700310/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700313 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700314u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700315{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
318 u32 v0 = v;
319 u32 v1 = v >> 32;
320 u32 sum = 0;
321 int i;
322
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
325 sum += 0x9E3779B9;
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
327 }
328
329 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700330}
331
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700332/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400333 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700334 *
335 * This is currently done by walking the list of writepage requests
336 * for the inode, which can be pretty inefficient.
337 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400338static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
339 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700340{
341 struct fuse_conn *fc = get_fuse_conn(inode);
342 struct fuse_inode *fi = get_fuse_inode(inode);
343 struct fuse_req *req;
344 bool found = false;
345
346 spin_lock(&fc->lock);
347 list_for_each_entry(req, &fi->writepages, writepages_entry) {
348 pgoff_t curr_index;
349
350 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300351 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400352 if (idx_from < curr_index + req->num_pages &&
353 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700354 found = true;
355 break;
356 }
357 }
358 spin_unlock(&fc->lock);
359
360 return found;
361}
362
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400363static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
364{
365 return fuse_range_is_writeback(inode, index, index);
366}
367
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700368/*
369 * Wait for page writeback to be completed.
370 *
371 * Since fuse doesn't rely on the VM writeback tracking, this has to
372 * use some other means.
373 */
374static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
375{
376 struct fuse_inode *fi = get_fuse_inode(inode);
377
378 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
379 return 0;
380}
381
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400382/*
383 * Wait for all pending writepages on the inode to finish.
384 *
385 * This is currently done by blocking further writes with FUSE_NOWRITE
386 * and waiting for all sent writes to complete.
387 *
388 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
389 * could conflict with truncation.
390 */
391static void fuse_sync_writes(struct inode *inode)
392{
393 fuse_set_nowrite(inode);
394 fuse_release_nowrite(inode);
395}
396
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700397static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700398{
Al Viro6131ffa2013-02-27 16:59:05 -0500399 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700400 struct fuse_conn *fc = get_fuse_conn(inode);
401 struct fuse_file *ff = file->private_data;
402 struct fuse_req *req;
403 struct fuse_flush_in inarg;
404 int err;
405
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800406 if (is_bad_inode(inode))
407 return -EIO;
408
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700409 if (fc->no_flush)
410 return 0;
411
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200412 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400413 if (err)
414 return err;
415
Al Viro59551022016-01-22 15:40:57 -0500416 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400417 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500418 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400419
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200420 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700421 if (err)
422 return err;
423
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400424 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 memset(&inarg, 0, sizeof(inarg));
426 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700427 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700428 req->in.h.opcode = FUSE_FLUSH;
429 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 req->in.numargs = 1;
431 req->in.args[0].size = sizeof(inarg);
432 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200433 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100434 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435 err = req->out.h.error;
436 fuse_put_request(fc, req);
437 if (err == -ENOSYS) {
438 fc->no_flush = 1;
439 err = 0;
440 }
441 return err;
442}
443
Josef Bacik02c24a82011-07-16 20:44:56 -0400444int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100445 int datasync, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200447 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 struct fuse_conn *fc = get_fuse_conn(inode);
449 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100450 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 struct fuse_fsync_in inarg;
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100452
453 memset(&inarg, 0, sizeof(inarg));
454 inarg.fh = ff->fh;
455 inarg.fsync_flags = datasync ? 1 : 0;
456 args.in.h.opcode = opcode;
457 args.in.h.nodeid = get_node_id(inode);
458 args.in.numargs = 1;
459 args.in.args[0].size = sizeof(inarg);
460 args.in.args[0].value = &inarg;
461 return fuse_simple_request(fc, &args);
462}
463
464static int fuse_fsync(struct file *file, loff_t start, loff_t end,
465 int datasync)
466{
467 struct inode *inode = file->f_mapping->host;
468 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700469 int err;
470
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800471 if (is_bad_inode(inode))
472 return -EIO;
473
Al Viro59551022016-01-22 15:40:57 -0500474 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400475
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700476 /*
477 * Start writeback against all dirty pages of the inode, then
478 * wait for all outstanding writes, before sending the FSYNC
479 * request.
480 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400481 err = file_write_and_wait_range(file, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700482 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400483 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700484
485 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700486
487 /*
488 * Due to implementation of fuse writeback
Jeff Layton7e51fe12017-07-22 09:27:43 -0400489 * file_write_and_wait_range() does not catch errors.
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700490 * We have to do this directly after fuse_sync_writes()
491 */
Jeff Layton7e51fe12017-07-22 09:27:43 -0400492 err = file_check_and_advance_wb_err(file);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700493 if (err)
494 goto out;
495
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200496 err = sync_inode_metadata(inode, 1);
497 if (err)
498 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700499
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100500 if (fc->no_fsync)
Miklos Szeredi22401e72014-04-28 14:19:23 +0200501 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400502
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100503 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700504 if (err == -ENOSYS) {
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100505 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700506 err = 0;
507 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400508out:
Al Viro59551022016-01-22 15:40:57 -0500509 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700510
Miklos Szeredia9c2d1e2018-12-03 10:14:43 +0100511 return err;
Miklos Szeredi82547982005-09-09 13:10:38 -0700512}
513
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200514void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
515 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700517 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800518 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800520 inarg->fh = ff->fh;
521 inarg->offset = pos;
522 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800523 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800524 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200525 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526 req->in.numargs = 1;
527 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800528 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 req->out.argvar = 1;
530 req->out.numargs = 1;
531 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700532}
533
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200534static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400535{
536 unsigned i;
537
538 for (i = 0; i < req->num_pages; i++) {
539 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200540 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400541 set_page_dirty_lock(page);
542 put_page(page);
543 }
544}
545
Seth Forshee744742d2016-03-11 10:35:34 -0600546static void fuse_io_release(struct kref *kref)
547{
548 kfree(container_of(kref, struct fuse_io_priv, refcnt));
549}
550
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100551static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
552{
553 if (io->err)
554 return io->err;
555
556 if (io->bytes >= 0 && io->write)
557 return -EIO;
558
559 return io->bytes < 0 ? io->size : io->bytes;
560}
561
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400562/**
563 * In case of short read, the caller sets 'pos' to the position of
564 * actual end of fuse request in IO request. Otherwise, if bytes_requested
565 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
566 *
567 * An example:
568 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
569 * both submitted asynchronously. The first of them was ACKed by userspace as
570 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
571 * second request was ACKed as short, e.g. only 1K was read, resulting in
572 * pos == 33K.
573 *
574 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
575 * will be equal to the length of the longest contiguous fragment of
576 * transferred data starting from the beginning of IO request.
577 */
578static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
579{
580 int left;
581
582 spin_lock(&io->lock);
583 if (err)
584 io->err = io->err ? : err;
585 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
586 io->bytes = pos;
587
588 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530589 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100590 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400591 spin_unlock(&io->lock);
592
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530593 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100594 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400595
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100596 if (res >= 0) {
597 struct inode *inode = file_inode(io->iocb->ki_filp);
598 struct fuse_conn *fc = get_fuse_conn(inode);
599 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400600
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100601 spin_lock(&fc->lock);
602 fi->attr_version = ++fc->attr_version;
603 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400604 }
605
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100606 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400607 }
Seth Forshee744742d2016-03-11 10:35:34 -0600608
609 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400610}
611
612static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
613{
614 struct fuse_io_priv *io = req->io;
615 ssize_t pos = -1;
616
Ashish Samant61c12b42017-07-12 19:26:58 -0700617 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400618
619 if (io->write) {
620 if (req->misc.write.in.size != req->misc.write.out.size)
621 pos = req->misc.write.in.offset - io->offset +
622 req->misc.write.out.size;
623 } else {
624 if (req->misc.read.in.size != req->out.args[0].size)
625 pos = req->misc.read.in.offset - io->offset +
626 req->out.args[0].size;
627 }
628
629 fuse_aio_complete(io, req->out.h.error, pos);
630}
631
632static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
633 size_t num_bytes, struct fuse_io_priv *io)
634{
635 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600636 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400637 io->size += num_bytes;
638 io->reqs++;
639 spin_unlock(&io->lock);
640
641 req->io = io;
642 req->end = fuse_aio_complete_req;
643
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400644 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400645 fuse_request_send_background(fc, req);
646
647 return num_bytes;
648}
649
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400650static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200651 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700652{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200653 struct file *file = io->iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200654 struct fuse_file *ff = file->private_data;
655 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700656
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200657 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700658 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700659 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700660
661 inarg->read_flags |= FUSE_READ_LOCKOWNER;
662 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
663 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400664
665 if (io->async)
666 return fuse_async_req_send(fc, req, count, io);
667
Tejun Heob93f8582008-11-26 12:03:55 +0100668 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800669 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700670}
671
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700672static void fuse_read_update_size(struct inode *inode, loff_t size,
673 u64 attr_ver)
674{
675 struct fuse_conn *fc = get_fuse_conn(inode);
676 struct fuse_inode *fi = get_fuse_inode(inode);
677
678 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400679 if (attr_ver == fi->attr_version && size < inode->i_size &&
680 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700681 fi->attr_version = ++fc->attr_version;
682 i_size_write(inode, size);
683 }
684 spin_unlock(&fc->lock);
685}
686
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400687static void fuse_short_read(struct fuse_req *req, struct inode *inode,
688 u64 attr_ver)
689{
690 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400691 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400692
Pavel Emelyanov83732002013-10-10 17:10:46 +0400693 if (fc->writeback_cache) {
694 /*
695 * A hole in a file. Some data after the hole are in page cache,
696 * but have not reached the client fs yet. So, the hole is not
697 * present there.
698 */
699 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300700 int start_idx = num_read >> PAGE_SHIFT;
701 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400702
703 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300704 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400705 off = 0;
706 }
707 } else {
708 loff_t pos = page_offset(req->pages[0]) + num_read;
709 fuse_read_update_size(inode, pos, attr_ver);
710 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400711}
712
Maxim Patlasov482fce52013-10-10 17:11:25 +0400713static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700714{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200715 struct kiocb iocb;
716 struct fuse_io_priv io;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700717 struct inode *inode = page->mapping->host;
718 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800719 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700720 size_t num_read;
721 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300722 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700723 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800724 int err;
725
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700726 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300727 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700728 * page-cache page, so make sure we read a properly synced
729 * page.
730 */
731 fuse_wait_on_page_writeback(inode, page->index);
732
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400733 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700734 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400735 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700736
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700737 attr_ver = fuse_get_attr_version(fc);
738
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700739 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200740 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700741 req->num_pages = 1;
742 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400743 req->page_descs[0].length = count;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200744 init_sync_kiocb(&iocb, file);
745 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400746 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700747 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700748
749 if (!err) {
750 /*
751 * Short read means EOF. If file size is larger, truncate it
752 */
753 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400754 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700755
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700757 }
758
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400759 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400760
761 return err;
762}
763
764static int fuse_readpage(struct file *file, struct page *page)
765{
766 struct inode *inode = page->mapping->host;
767 int err;
768
769 err = -EIO;
770 if (is_bad_inode(inode))
771 goto out;
772
773 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800774 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700775 out:
776 unlock_page(page);
777 return err;
778}
779
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800780static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700781{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800782 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700783 size_t count = req->misc.read.in.size;
784 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200785 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800786
Miklos Szeredice534fb2010-05-25 15:06:07 +0200787 for (i = 0; mapping == NULL && i < req->num_pages; i++)
788 mapping = req->pages[i]->mapping;
789
790 if (mapping) {
791 struct inode *inode = mapping->host;
792
793 /*
794 * Short read means EOF. If file size is larger, truncate it
795 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400796 if (!req->out.h.error && num_read < count)
797 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200798
Andrew Gallagher451418f2013-11-05 03:55:43 -0800799 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700800 }
801
Miklos Szeredidb50b962005-09-09 13:10:33 -0700802 for (i = 0; i < req->num_pages; i++) {
803 struct page *page = req->pages[i];
804 if (!req->out.h.error)
805 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800806 else
807 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700808 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300809 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700810 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700811 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -0800812 fuse_file_put(req->ff, false, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813}
814
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200815static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800816{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200817 struct fuse_file *ff = file->private_data;
818 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800819 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300820 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200821
822 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800823 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200824 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200825 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700826 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800827 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700828 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800829 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100830 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800831 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100832 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800833 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100834 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800835 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700836}
837
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700838struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700839 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800840 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700841 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400842 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843};
844
845static int fuse_readpages_fill(void *_data, struct page *page)
846{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700847 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700848 struct fuse_req *req = data->req;
849 struct inode *inode = data->inode;
850 struct fuse_conn *fc = get_fuse_conn(inode);
851
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700852 fuse_wait_on_page_writeback(inode, page->index);
853
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 if (req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300855 (req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300856 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700857 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300858 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
859 fc->max_pages);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200860 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400861 if (fc->async_read)
862 req = fuse_get_req_for_background(fc, nr_alloc);
863 else
864 req = fuse_get_req(fc, nr_alloc);
865
866 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700867 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700869 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700871 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400872
873 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai109728c2018-07-19 15:49:39 +0300874 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400875 fuse_put_request(fc, req);
876 return -EIO;
877 }
878
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300879 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400881 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100882 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400883 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700884 return 0;
885}
886
887static int fuse_readpages(struct file *file, struct address_space *mapping,
888 struct list_head *pages, unsigned nr_pages)
889{
890 struct inode *inode = mapping->host;
891 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700892 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700893 int err;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +0300894 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700896 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800897 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800898 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800899
Miklos Szeredia6643092007-11-28 16:22:00 -0800900 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700901 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400902 if (fc->async_read)
903 data.req = fuse_get_req_for_background(fc, nr_alloc);
904 else
905 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400906 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700907 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700908 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800909 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700910
911 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700912 if (!err) {
913 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200914 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700915 else
916 fuse_put_request(fc, data.req);
917 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800918out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700919 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700920}
921
Al Viro37c20f12014-04-02 14:47:09 -0400922static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800923{
924 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400925 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800926
Brian Fostera8894272012-07-16 15:23:50 -0400927 /*
928 * In auto invalidate mode, always update attributes on read.
929 * Otherwise, only update if we attempt to read past EOF (to ensure
930 * i_size is up to date).
931 */
932 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400933 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800934 int err;
Miklos Szeredi5b97eea2017-09-12 16:57:54 +0200935 err = fuse_update_attributes(inode, iocb->ki_filp);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936 if (err)
937 return err;
938 }
939
Al Viro37c20f12014-04-02 14:47:09 -0400940 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941}
942
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200943static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200944 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700945{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700946 struct fuse_write_in *inarg = &req->misc.write.in;
947 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700948
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700949 inarg->fh = ff->fh;
950 inarg->offset = pos;
951 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700952 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200953 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700954 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200955 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700956 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
957 else
958 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700959 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960 req->in.args[1].size = count;
961 req->out.numargs = 1;
962 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700963 req->out.args[0].value = outarg;
964}
965
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400966static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200967 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700968{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200969 struct kiocb *iocb = io->iocb;
970 struct file *file = iocb->ki_filp;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200971 struct fuse_file *ff = file->private_data;
972 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200973 struct fuse_write_in *inarg = &req->misc.write.in;
974
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200975 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200976 inarg->flags = file->f_flags;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +0200977 if (iocb->ki_flags & IOCB_DSYNC)
978 inarg->flags |= O_DSYNC;
979 if (iocb->ki_flags & IOCB_SYNC)
980 inarg->flags |= O_SYNC;
Miklos Szeredif3332112007-10-18 03:07:04 -0700981 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700982 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
983 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
984 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400985
986 if (io->async)
987 return fuse_async_req_send(fc, req, count, io);
988
Tejun Heob93f8582008-11-26 12:03:55 +0100989 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700990 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700991}
992
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400993bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700994{
995 struct fuse_conn *fc = get_fuse_conn(inode);
996 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400997 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700998
999 spin_lock(&fc->lock);
1000 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001001 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001002 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003 ret = true;
1004 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001005 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001006
1007 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008}
1009
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001010static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001011 struct inode *inode, loff_t pos,
1012 size_t count)
1013{
1014 size_t res;
1015 unsigned offset;
1016 unsigned i;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001017 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001018
1019 for (i = 0; i < req->num_pages; i++)
1020 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1021
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001022 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001023
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001024 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001025 count = res;
1026 for (i = 0; i < req->num_pages; i++) {
1027 struct page *page = req->pages[i];
1028
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001029 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001030 SetPageUptodate(page);
1031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 if (count > PAGE_SIZE - offset)
1033 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001034 else
1035 count = 0;
1036 offset = 0;
1037
1038 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001039 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001040 }
1041
1042 return res;
1043}
1044
1045static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1046 struct address_space *mapping,
1047 struct iov_iter *ii, loff_t pos)
1048{
1049 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001050 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001051 size_t count = 0;
1052 int err;
1053
Miklos Szeredif4975c62009-04-02 14:25:34 +02001054 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001055 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001056
1057 do {
1058 size_t tmp;
1059 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001060 pgoff_t index = pos >> PAGE_SHIFT;
1061 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001062 iov_iter_count(ii));
1063
1064 bytes = min_t(size_t, bytes, fc->max_write - count);
1065
1066 again:
1067 err = -EFAULT;
1068 if (iov_iter_fault_in_readable(ii, bytes))
1069 break;
1070
1071 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001072 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001073 if (!page)
1074 break;
1075
anfei zhou931e80e2010-02-02 13:44:02 -08001076 if (mapping_writably_mapped(mapping))
1077 flush_dcache_page(page);
1078
Nick Pigginea9b9902008-04-30 00:54:42 -07001079 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001080 flush_dcache_page(page);
1081
Roman Gushchin3ca81382015-10-12 16:33:44 +03001082 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 if (!tmp) {
1084 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001086 bytes = min(bytes, iov_iter_single_seg_count(ii));
1087 goto again;
1088 }
1089
1090 err = 0;
1091 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001092 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 req->num_pages++;
1094
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 count += tmp;
1096 pos += tmp;
1097 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001098 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 offset = 0;
1100
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001101 if (!fc->big_writes)
1102 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001103 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001104 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001105
1106 return count > 0 ? count : err;
1107}
1108
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001109static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1110 unsigned int max_pages)
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001111{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001112 return min_t(unsigned int,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001113 ((pos + len - 1) >> PAGE_SHIFT) -
1114 (pos >> PAGE_SHIFT) + 1,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001115 max_pages);
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001116}
1117
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001118static ssize_t fuse_perform_write(struct kiocb *iocb,
Nick Pigginea9b9902008-04-30 00:54:42 -07001119 struct address_space *mapping,
1120 struct iov_iter *ii, loff_t pos)
1121{
1122 struct inode *inode = mapping->host;
1123 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001124 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 int err = 0;
1126 ssize_t res = 0;
1127
1128 if (is_bad_inode(inode))
1129 return -EIO;
1130
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001131 if (inode->i_size < pos + iov_iter_count(ii))
1132 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1133
Nick Pigginea9b9902008-04-30 00:54:42 -07001134 do {
1135 struct fuse_req *req;
1136 ssize_t count;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001137 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1138 fc->max_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001139
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001140 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001141 if (IS_ERR(req)) {
1142 err = PTR_ERR(req);
1143 break;
1144 }
1145
1146 count = fuse_fill_write_pages(req, mapping, ii, pos);
1147 if (count <= 0) {
1148 err = count;
1149 } else {
1150 size_t num_written;
1151
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001152 num_written = fuse_send_write_pages(req, iocb, inode,
Nick Pigginea9b9902008-04-30 00:54:42 -07001153 pos, count);
1154 err = req->out.h.error;
1155 if (!err) {
1156 res += num_written;
1157 pos += num_written;
1158
1159 /* break out of the loop on short write */
1160 if (num_written != count)
1161 err = -EIO;
1162 }
1163 }
1164 fuse_put_request(fc, req);
1165 } while (!err && iov_iter_count(ii));
1166
1167 if (res > 0)
1168 fuse_write_update_size(inode, pos);
1169
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001170 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001171 fuse_invalidate_attr(inode);
1172
1173 return res > 0 ? res : err;
1174}
1175
Al Viro84c3d552014-04-03 14:33:23 -04001176static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001177{
1178 struct file *file = iocb->ki_filp;
1179 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001180 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001181 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001182 struct inode *inode = mapping->host;
1183 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001184 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001185
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001186 if (get_fuse_conn(inode)->writeback_cache) {
1187 /* Update size (EOF optimization) and mode (SUID clearing) */
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02001188 err = fuse_update_attributes(mapping->host, file);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001189 if (err)
1190 return err;
1191
Al Viro84c3d552014-04-03 14:33:23 -04001192 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001193 }
1194
Al Viro59551022016-01-22 15:40:57 -05001195 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001196
1197 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001198 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001199
Al Viro3309dd02015-04-09 12:55:47 -04001200 err = generic_write_checks(iocb, from);
1201 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001202 goto out;
1203
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001204 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001205 if (err)
1206 goto out;
1207
Josef Bacikc3b2da32012-03-26 09:59:21 -04001208 err = file_update_time(file);
1209 if (err)
1210 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
Al Viro2ba48ce2015-04-09 13:52:01 -04001212 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001213 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001214 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001215 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001216 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001217
Anand Avati4273b792012-02-17 12:46:25 -05001218 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001219
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001220 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001221 if (written_buffered < 0) {
1222 err = written_buffered;
1223 goto out;
1224 }
1225 endbyte = pos + written_buffered - 1;
1226
1227 err = filemap_write_and_wait_range(file->f_mapping, pos,
1228 endbyte);
1229 if (err)
1230 goto out;
1231
1232 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 pos >> PAGE_SHIFT,
1234 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001235
1236 written += written_buffered;
1237 iocb->ki_pos = pos + written_buffered;
1238 } else {
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001239 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001240 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001241 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001242 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001243out:
1244 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001245 inode_unlock(inode);
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001246 if (written > 0)
1247 written = generic_write_sync(iocb, written);
Nick Pigginea9b9902008-04-30 00:54:42 -07001248
1249 return written ? written : err;
1250}
1251
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001252static inline void fuse_page_descs_length_init(struct fuse_req *req,
1253 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001254{
1255 int i;
1256
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001257 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001258 req->page_descs[i].length = PAGE_SIZE -
1259 req->page_descs[i].offset;
1260}
1261
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001262static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1263{
1264 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1265}
1266
1267static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1268 size_t max_size)
1269{
1270 return min(iov_iter_single_seg_count(ii), max_size);
1271}
1272
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001273static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001274 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001275{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001276 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001277 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001278
Miklos Szeredif4975c62009-04-02 14:25:34 +02001279 /* Special case for kernel I/O: can copy directly into the buffer */
David Howells00e23702018-10-22 13:07:28 +01001280 if (iov_iter_is_kvec(ii)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281 unsigned long user_addr = fuse_get_user_addr(ii);
1282 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1283
Miklos Szeredif4975c62009-04-02 14:25:34 +02001284 if (write)
1285 req->in.args[1].value = (void *) user_addr;
1286 else
1287 req->out.args[0].value = (void *) user_addr;
1288
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001289 iov_iter_advance(ii, frag_size);
1290 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001291 return 0;
1292 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001293
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001294 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001295 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001296 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001297 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001298 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001299 req->max_pages - req->num_pages,
1300 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001301 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001302 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001303
Al Viroc9c37e22014-03-16 16:08:30 -04001304 iov_iter_advance(ii, ret);
1305 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306
Al Viroc9c37e22014-03-16 16:08:30 -04001307 ret += start;
1308 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1309
1310 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001311 fuse_page_descs_length_init(req, req->num_pages, npages);
1312
1313 req->num_pages += npages;
1314 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001315 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001316 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001317
1318 if (write)
1319 req->in.argpages = 1;
1320 else
1321 req->out.argpages = 1;
1322
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001323 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001324
Ashish Samant2c932d42016-03-25 10:53:41 -07001325 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001326}
1327
Al Virod22a9432014-03-16 15:50:47 -04001328ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1329 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001330{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001331 int write = flags & FUSE_DIO_WRITE;
1332 int cuse = flags & FUSE_DIO_CUSE;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001333 struct file *file = io->iocb->ki_filp;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001334 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001335 struct fuse_file *ff = file->private_data;
1336 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001337 size_t nmax = write ? fc->max_write : fc->max_read;
1338 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001339 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 pgoff_t idx_from = pos >> PAGE_SHIFT;
1341 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001342 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001343 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001344 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001345
Brian Fosterde82b922013-05-14 11:25:39 -04001346 if (io->async)
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001347 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1348 fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001349 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001350 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001351 if (IS_ERR(req))
1352 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001353
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001354 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1355 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001356 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001357 fuse_sync_writes(inode);
1358 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001359 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001360 }
1361
Ashish Samant61c12b42017-07-12 19:26:58 -07001362 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001365 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001366 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001367 err = fuse_get_user_pages(req, iter, &nbytes, write);
1368 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001370
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001372 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001373 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001374 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001375
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001376 if (!io->async)
Ashish Samant61c12b42017-07-12 19:26:58 -07001377 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001379 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 break;
1381 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001382 res = 0;
1383 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001384 break;
1385 }
1386 count -= nres;
1387 res += nres;
1388 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001389 if (nres != nbytes)
1390 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001391 if (count) {
1392 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001393 if (io->async)
1394 req = fuse_get_req_for_background(fc,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001395 iov_iter_npages(iter, fc->max_pages));
Brian Fosterde82b922013-05-14 11:25:39 -04001396 else
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001397 req = fuse_get_req(fc, iov_iter_npages(iter,
1398 fc->max_pages));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001399 if (IS_ERR(req))
1400 break;
1401 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001402 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001403 if (!IS_ERR(req))
1404 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001405 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001406 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407
Ashish Samant742f9922016-03-14 21:57:35 -07001408 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409}
Tejun Heo08cbf542009-04-14 10:54:53 +09001410EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001411
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001412static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001413 struct iov_iter *iter,
1414 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001416 ssize_t res;
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001417 struct inode *inode = file_inode(io->iocb->ki_filp);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001418
1419 if (is_bad_inode(inode))
1420 return -EIO;
1421
Al Virod22a9432014-03-16 15:50:47 -04001422 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001423
Miklos Szeredi9a2eb242018-10-15 15:43:06 +02001424 fuse_invalidate_atime(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001425
1426 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427}
1428
Al Viro153162632015-03-30 22:08:36 -04001429static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001430{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001431 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001432 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001433}
1434
Al Viro153162632015-03-30 22:08:36 -04001435static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436{
Miklos Szeredie1c0eec2017-09-12 16:57:53 +02001437 struct inode *inode = file_inode(iocb->ki_filp);
1438 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
Al Viro153162632015-03-30 22:08:36 -04001439 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001440
1441 if (is_bad_inode(inode))
1442 return -EIO;
1443
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001444 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001445 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001446 res = generic_write_checks(iocb, from);
1447 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001448 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001449 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001450 if (res > 0)
Al Viro153162632015-03-30 22:08:36 -04001451 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001452 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001453
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001454 return res;
1455}
1456
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001457static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001458{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001459 int i;
1460
1461 for (i = 0; i < req->num_pages; i++)
1462 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001463
1464 if (req->ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001465 fuse_file_put(req->ff, false, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001466}
1467
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001468static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001469{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001470 struct inode *inode = req->inode;
1471 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001472 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001473 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001474
1475 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001476 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001477 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001478 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001479 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001480 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001481 wake_up(&fi->page_waitq);
1482}
1483
1484/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001485static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1486 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001487__releases(fc->lock)
1488__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001489{
1490 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001491 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001492 __u64 data_size = req->num_pages * PAGE_SIZE;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001493 bool queued;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001494
1495 if (!fc->connected)
1496 goto out_free;
1497
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001498 if (inarg->offset + data_size <= size) {
1499 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001500 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001501 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001502 } else {
1503 /* Got truncated off completely */
1504 goto out_free;
1505 }
1506
1507 req->in.args[1].size = inarg->size;
1508 fi->writectr++;
Kirill Tkhai63825b42018-08-27 18:29:56 +03001509 queued = fuse_request_queue_background(fc, req);
1510 WARN_ON(!queued);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001511 return;
1512
1513 out_free:
1514 fuse_writepage_finish(fc, req);
1515 spin_unlock(&fc->lock);
1516 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001517 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001518 spin_lock(&fc->lock);
1519}
1520
1521/*
1522 * If fi->writectr is positive (no truncate or fsync going on) send
1523 * all queued writepage requests.
1524 *
1525 * Called with fc->lock
1526 */
1527void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001528__releases(fc->lock)
1529__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001530{
1531 struct fuse_conn *fc = get_fuse_conn(inode);
1532 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001533 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001534 struct fuse_req *req;
1535
1536 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1537 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1538 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001539 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001540 }
1541}
1542
1543static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1544{
1545 struct inode *inode = req->inode;
1546 struct fuse_inode *fi = get_fuse_inode(inode);
1547
1548 mapping_set_error(inode->i_mapping, req->out.h.error);
1549 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001550 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001551 struct fuse_conn *fc = get_fuse_conn(inode);
1552 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001553 struct fuse_req *next = req->misc.write.next;
1554 req->misc.write.next = next->misc.write.next;
1555 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001556 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001557 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001558
1559 /*
1560 * Skip fuse_flush_writepages() to make it easy to crop requests
1561 * based on primary request size.
1562 *
1563 * 1st case (trivial): there are no concurrent activities using
1564 * fuse_set/release_nowrite. Then we're on safe side because
1565 * fuse_flush_writepages() would call fuse_send_writepage()
1566 * anyway.
1567 *
1568 * 2nd case: someone called fuse_set_nowrite and it is waiting
1569 * now for completion of all in-flight requests. This happens
1570 * rarely and no more than once per page, so this should be
1571 * okay.
1572 *
1573 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1574 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1575 * that fuse_set_nowrite returned implies that all in-flight
1576 * requests were completed along with all of their secondary
1577 * requests. Further primary requests are blocked by negative
1578 * writectr. Hence there cannot be any in-flight requests and
1579 * no invocations of fuse_writepage_end() while we're in
1580 * fuse_set_nowrite..fuse_release_nowrite section.
1581 */
1582 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001583 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001584 fi->writectr--;
1585 fuse_writepage_finish(fc, req);
1586 spin_unlock(&fc->lock);
1587 fuse_writepage_free(fc, req);
1588}
1589
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001590static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1591 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001592{
Miklos Szeredi72523422013-10-01 16:44:52 +02001593 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001594
1595 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001596 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001597 ff = list_entry(fi->write_files.next, struct fuse_file,
1598 write_entry);
1599 fuse_file_get(ff);
1600 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001601 spin_unlock(&fc->lock);
1602
1603 return ff;
1604}
1605
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001606static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1607 struct fuse_inode *fi)
1608{
1609 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1610 WARN_ON(!ff);
1611 return ff;
1612}
1613
1614int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1615{
1616 struct fuse_conn *fc = get_fuse_conn(inode);
1617 struct fuse_inode *fi = get_fuse_inode(inode);
1618 struct fuse_file *ff;
1619 int err;
1620
1621 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001622 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001623 if (ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001624 fuse_file_put(ff, false, false);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001625
1626 return err;
1627}
1628
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001629static int fuse_writepage_locked(struct page *page)
1630{
1631 struct address_space *mapping = page->mapping;
1632 struct inode *inode = mapping->host;
1633 struct fuse_conn *fc = get_fuse_conn(inode);
1634 struct fuse_inode *fi = get_fuse_inode(inode);
1635 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001637 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001638
1639 set_page_writeback(page);
1640
Maxim Patlasov4250c062012-10-26 19:48:07 +04001641 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642 if (!req)
1643 goto err;
1644
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001645 /* writeback always goes to bg_queue */
1646 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001647 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1648 if (!tmp_page)
1649 goto err_free;
1650
Miklos Szeredi72523422013-10-01 16:44:52 +02001651 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001652 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001653 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001654 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001655
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001656 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657
1658 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001659 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001660 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001661 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001662 req->num_pages = 1;
1663 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001664 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001665 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001666 req->end = fuse_writepage_end;
1667 req->inode = inode;
1668
Tejun Heo93f78d82015-05-22 17:13:27 -04001669 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001670 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001671
1672 spin_lock(&fc->lock);
1673 list_add(&req->writepages_entry, &fi->writepages);
1674 list_add_tail(&req->list, &fi->queued_writes);
1675 fuse_flush_writepages(inode);
1676 spin_unlock(&fc->lock);
1677
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001678 end_page_writeback(page);
1679
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001680 return 0;
1681
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001682err_nofile:
1683 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001684err_free:
1685 fuse_request_free(req);
1686err:
Jeff Layton91839762017-05-25 06:57:50 -04001687 mapping_set_error(page->mapping, error);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001689 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690}
1691
1692static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1693{
1694 int err;
1695
Miklos Szerediff17be02013-10-01 16:44:53 +02001696 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1697 /*
1698 * ->writepages() should be called for sync() and friends. We
1699 * should only get here on direct reclaim and then we are
1700 * allowed to skip a page which is already in flight
1701 */
1702 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1703
1704 redirty_page_for_writepage(wbc, page);
1705 return 0;
1706 }
1707
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708 err = fuse_writepage_locked(page);
1709 unlock_page(page);
1710
1711 return err;
1712}
1713
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001714struct fuse_fill_wb_data {
1715 struct fuse_req *req;
1716 struct fuse_file *ff;
1717 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001718 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001719};
1720
1721static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1722{
1723 struct fuse_req *req = data->req;
1724 struct inode *inode = data->inode;
1725 struct fuse_conn *fc = get_fuse_conn(inode);
1726 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001727 int num_pages = req->num_pages;
1728 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001729
1730 req->ff = fuse_file_get(data->ff);
1731 spin_lock(&fc->lock);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001735
1736 for (i = 0; i < num_pages; i++)
1737 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001738}
1739
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001740static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1741 struct page *page)
1742{
1743 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1744 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1745 struct fuse_req *tmp;
1746 struct fuse_req *old_req;
1747 bool found = false;
1748 pgoff_t curr_index;
1749
1750 BUG_ON(new_req->num_pages != 0);
1751
1752 spin_lock(&fc->lock);
1753 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001754 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1755 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001756 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001757 if (curr_index <= page->index &&
1758 page->index < curr_index + old_req->num_pages) {
1759 found = true;
1760 break;
1761 }
1762 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001763 if (!found) {
1764 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001765 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001766 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767
Maxim Patlasovf6011082013-10-02 15:01:07 +04001768 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001769 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1770 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001771 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001772 if (tmp->num_pages == 1 &&
1773 curr_index == page->index) {
1774 old_req = tmp;
1775 }
1776 }
1777
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001778 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001779 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001780
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001781 copy_highpage(old_req->pages[0], page);
1782 spin_unlock(&fc->lock);
1783
Tejun Heo93f78d82015-05-22 17:13:27 -04001784 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredia2ebba82019-01-16 10:27:59 +01001785 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001786 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001787 fuse_writepage_free(fc, new_req);
1788 fuse_request_free(new_req);
1789 goto out;
1790 } else {
1791 new_req->misc.write.next = old_req->misc.write.next;
1792 old_req->misc.write.next = new_req;
1793 }
1794out_unlock:
1795 spin_unlock(&fc->lock);
1796out:
1797 return found;
1798}
1799
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001800static int fuse_writepages_fill(struct page *page,
1801 struct writeback_control *wbc, void *_data)
1802{
1803 struct fuse_fill_wb_data *data = _data;
1804 struct fuse_req *req = data->req;
1805 struct inode *inode = data->inode;
1806 struct fuse_conn *fc = get_fuse_conn(inode);
1807 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001808 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001809 int err;
1810
1811 if (!data->ff) {
1812 err = -EIO;
1813 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1814 if (!data->ff)
1815 goto out_unlock;
1816 }
1817
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001818 /*
1819 * Being under writeback is unlikely but possible. For example direct
1820 * read to an mmaped fuse file will set the page dirty twice; once when
1821 * the pages are faulted with get_user_pages(), and then after the read
1822 * completed.
1823 */
1824 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001825
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001826 if (req && req->num_pages &&
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001827 (is_writeback || req->num_pages == fc->max_pages ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001828 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001829 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1830 fuse_writepages_send(data);
1831 data->req = NULL;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001832 } else if (req && req->num_pages == req->max_pages) {
1833 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1834 fuse_writepages_send(data);
1835 req = data->req = NULL;
1836 }
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001837 }
Miklos Szeredie52a82502018-10-01 10:07:06 +02001838
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001839 err = -ENOMEM;
1840 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1841 if (!tmp_page)
1842 goto out_unlock;
1843
1844 /*
1845 * The page must not be redirtied until the writeout is completed
1846 * (i.e. userspace has sent a reply to the write request). Otherwise
1847 * there could be more than one temporary page instance for each real
1848 * page.
1849 *
1850 * This is ensured by holding the page lock in page_mkwrite() while
1851 * checking fuse_page_is_writeback(). We already hold the page lock
1852 * since clear_page_dirty_for_io() and keep it held until we add the
1853 * request to the fi->writepages list and increment req->num_pages.
1854 * After this fuse_page_is_writeback() will indicate that the page is
1855 * under writeback, so we can release the page lock.
1856 */
1857 if (data->req == NULL) {
1858 struct fuse_inode *fi = get_fuse_inode(inode);
1859
1860 err = -ENOMEM;
Miklos Szeredie52a82502018-10-01 10:07:06 +02001861 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001862 if (!req) {
1863 __free_page(tmp_page);
1864 goto out_unlock;
1865 }
1866
1867 fuse_write_fill(req, data->ff, page_offset(page), 0);
1868 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001869 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001870 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001871 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001872 req->num_pages = 0;
1873 req->end = fuse_writepage_end;
1874 req->inode = inode;
1875
1876 spin_lock(&fc->lock);
1877 list_add(&req->writepages_entry, &fi->writepages);
1878 spin_unlock(&fc->lock);
1879
1880 data->req = req;
1881 }
1882 set_page_writeback(page);
1883
1884 copy_highpage(tmp_page, page);
1885 req->pages[req->num_pages] = tmp_page;
1886 req->page_descs[req->num_pages].offset = 0;
1887 req->page_descs[req->num_pages].length = PAGE_SIZE;
1888
Tejun Heo93f78d82015-05-22 17:13:27 -04001889 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001890 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001891
1892 err = 0;
1893 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1894 end_page_writeback(page);
1895 data->req = NULL;
1896 goto out_unlock;
1897 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001898 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001899
1900 /*
1901 * Protected by fc->lock against concurrent access by
1902 * fuse_page_is_writeback().
1903 */
1904 spin_lock(&fc->lock);
1905 req->num_pages++;
1906 spin_unlock(&fc->lock);
1907
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001908out_unlock:
1909 unlock_page(page);
1910
1911 return err;
1912}
1913
1914static int fuse_writepages(struct address_space *mapping,
1915 struct writeback_control *wbc)
1916{
1917 struct inode *inode = mapping->host;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001918 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001919 struct fuse_fill_wb_data data;
1920 int err;
1921
1922 err = -EIO;
1923 if (is_bad_inode(inode))
1924 goto out;
1925
1926 data.inode = inode;
1927 data.req = NULL;
1928 data.ff = NULL;
1929
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001930 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001931 data.orig_pages = kcalloc(fc->max_pages,
Fabian Frederickf2b34552014-06-23 18:35:15 +02001932 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001933 GFP_NOFS);
1934 if (!data.orig_pages)
1935 goto out;
1936
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001937 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1938 if (data.req) {
1939 /* Ignore errors if we can write at least one page */
1940 BUG_ON(!data.req->num_pages);
1941 fuse_writepages_send(&data);
1942 err = 0;
1943 }
1944 if (data.ff)
Chad Austin2e64ff12018-12-10 10:54:52 -08001945 fuse_file_put(data.ff, false, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001946
1947 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001948out:
1949 return err;
1950}
1951
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001952/*
1953 * It's worthy to make sure that space is reserved on disk for the write,
1954 * but how to implement it without killing performance need more thinking.
1955 */
1956static int fuse_write_begin(struct file *file, struct address_space *mapping,
1957 loff_t pos, unsigned len, unsigned flags,
1958 struct page **pagep, void **fsdata)
1959{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001961 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001962 struct page *page;
1963 loff_t fsize;
1964 int err = -ENOMEM;
1965
1966 WARN_ON(!fc->writeback_cache);
1967
1968 page = grab_cache_page_write_begin(mapping, index, flags);
1969 if (!page)
1970 goto error;
1971
1972 fuse_wait_on_page_writeback(mapping->host, page->index);
1973
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001974 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001975 goto success;
1976 /*
1977 * Check if the start this page comes after the end of file, in which
1978 * case the readpage can be optimized away.
1979 */
1980 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 if (fsize <= (pos & PAGE_MASK)) {
1982 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001983 if (off)
1984 zero_user_segment(page, 0, off);
1985 goto success;
1986 }
1987 err = fuse_do_readpage(file, page);
1988 if (err)
1989 goto cleanup;
1990success:
1991 *pagep = page;
1992 return 0;
1993
1994cleanup:
1995 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001996 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001997error:
1998 return err;
1999}
2000
2001static int fuse_write_end(struct file *file, struct address_space *mapping,
2002 loff_t pos, unsigned len, unsigned copied,
2003 struct page *page, void *fsdata)
2004{
2005 struct inode *inode = page->mapping->host;
2006
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002007 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2008 if (!copied)
2009 goto unlock;
2010
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002011 if (!PageUptodate(page)) {
2012 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002014 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002015 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002016 SetPageUptodate(page);
2017 }
2018
2019 fuse_write_update_size(inode, pos + copied);
2020 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002021
2022unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002023 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002024 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002025
2026 return copied;
2027}
2028
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002029static int fuse_launder_page(struct page *page)
2030{
2031 int err = 0;
2032 if (clear_page_dirty_for_io(page)) {
2033 struct inode *inode = page->mapping->host;
2034 err = fuse_writepage_locked(page);
2035 if (!err)
2036 fuse_wait_on_page_writeback(inode, page->index);
2037 }
2038 return err;
2039}
2040
2041/*
2042 * Write back dirty pages now, because there may not be any suitable
2043 * open files later
2044 */
2045static void fuse_vma_close(struct vm_area_struct *vma)
2046{
2047 filemap_write_and_wait(vma->vm_file->f_mapping);
2048}
2049
2050/*
2051 * Wait for writeback against this page to complete before allowing it
2052 * to be marked dirty again, and hence written back again, possibly
2053 * before the previous writepage completed.
2054 *
2055 * Block here, instead of in ->writepage(), so that the userspace fs
2056 * can only block processes actually operating on the filesystem.
2057 *
2058 * Otherwise unprivileged userspace fs would be able to block
2059 * unrelated:
2060 *
2061 * - page migration
2062 * - sync(2)
2063 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2064 */
Souptick Joarder46fb5042018-05-12 10:25:37 +05302065static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002066{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002067 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08002068 struct inode *inode = file_inode(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002069
Dave Jiang11bac802017-02-24 14:56:41 -08002070 file_update_time(vmf->vma->vm_file);
Miklos Szeredicca24372013-10-01 16:44:51 +02002071 lock_page(page);
2072 if (page->mapping != inode->i_mapping) {
2073 unlock_page(page);
2074 return VM_FAULT_NOPAGE;
2075 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002076
2077 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002078 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002079}
2080
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002081static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002082 .close = fuse_vma_close,
2083 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002084 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002085 .page_mkwrite = fuse_page_mkwrite,
2086};
2087
2088static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2089{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002090 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2091 fuse_link_write_file(file);
2092
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002093 file_accessed(file);
2094 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002095 return 0;
2096}
2097
Miklos Szeredifc280c92009-04-02 14:25:35 +02002098static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2099{
2100 /* Can't provide the coherency needed for MAP_SHARED */
2101 if (vma->vm_flags & VM_MAYSHARE)
2102 return -ENODEV;
2103
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002104 invalidate_inode_pages2(file->f_mapping);
2105
Miklos Szeredifc280c92009-04-02 14:25:35 +02002106 return generic_file_mmap(file, vma);
2107}
2108
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002109static int convert_fuse_file_lock(struct fuse_conn *fc,
2110 const struct fuse_file_lock *ffl,
Miklos Szeredi71421252006-06-25 05:48:52 -07002111 struct file_lock *fl)
2112{
2113 switch (ffl->type) {
2114 case F_UNLCK:
2115 break;
2116
2117 case F_RDLCK:
2118 case F_WRLCK:
2119 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2120 ffl->end < ffl->start)
2121 return -EIO;
2122
2123 fl->fl_start = ffl->start;
2124 fl->fl_end = ffl->end;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002125
2126 /*
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002127 * Convert pid into init's pid namespace. The locks API will
2128 * translate it into the caller's pid namespace.
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002129 */
2130 rcu_read_lock();
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002131 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002132 rcu_read_unlock();
Miklos Szeredi71421252006-06-25 05:48:52 -07002133 break;
2134
2135 default:
2136 return -EIO;
2137 }
2138 fl->fl_type = ffl->type;
2139 return 0;
2140}
2141
Miklos Szeredi70781872014-12-12 09:49:05 +01002142static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002143 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002144 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002145{
Al Viro6131ffa2013-02-27 16:59:05 -05002146 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002147 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002148 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002149
Miklos Szeredi70781872014-12-12 09:49:05 +01002150 memset(inarg, 0, sizeof(*inarg));
2151 inarg->fh = ff->fh;
2152 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2153 inarg->lk.start = fl->fl_start;
2154 inarg->lk.end = fl->fl_end;
2155 inarg->lk.type = fl->fl_type;
2156 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002157 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002158 inarg->lk_flags |= FUSE_LK_FLOCK;
2159 args->in.h.opcode = opcode;
2160 args->in.h.nodeid = get_node_id(inode);
2161 args->in.numargs = 1;
2162 args->in.args[0].size = sizeof(*inarg);
2163 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002164}
2165
2166static int fuse_getlk(struct file *file, struct file_lock *fl)
2167{
Al Viro6131ffa2013-02-27 16:59:05 -05002168 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002169 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002170 FUSE_ARGS(args);
2171 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002172 struct fuse_lk_out outarg;
2173 int err;
2174
Miklos Szeredi70781872014-12-12 09:49:05 +01002175 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2176 args.out.numargs = 1;
2177 args.out.args[0].size = sizeof(outarg);
2178 args.out.args[0].value = &outarg;
2179 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002180 if (!err)
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002181 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002182
2183 return err;
2184}
2185
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002186static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002187{
Al Viro6131ffa2013-02-27 16:59:05 -05002188 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002189 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002190 FUSE_ARGS(args);
2191 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002192 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002193 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2194 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
Miklos Szeredi71421252006-06-25 05:48:52 -07002195 int err;
2196
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002197 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002198 /* NLM needs asynchronous locks, which we don't support yet */
2199 return -ENOLCK;
2200 }
2201
Miklos Szeredi71421252006-06-25 05:48:52 -07002202 /* Unlock on close is handled by the flush method */
Benjamin Coddington50f21122017-04-11 12:50:09 -04002203 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
Miklos Szeredi71421252006-06-25 05:48:52 -07002204 return 0;
2205
Seth Forshee0b6e9ea2014-07-02 16:29:19 -05002206 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +01002207 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002208
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002209 /* locking is restartable */
2210 if (err == -EINTR)
2211 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002212
Miklos Szeredi71421252006-06-25 05:48:52 -07002213 return err;
2214}
2215
2216static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2217{
Al Viro6131ffa2013-02-27 16:59:05 -05002218 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002219 struct fuse_conn *fc = get_fuse_conn(inode);
2220 int err;
2221
Miklos Szeredi48e90762008-07-25 01:49:02 -07002222 if (cmd == F_CANCELLK) {
2223 err = 0;
2224 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002225 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002226 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002227 err = 0;
2228 } else
2229 err = fuse_getlk(file, fl);
2230 } else {
2231 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002232 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002233 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002234 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002235 }
2236 return err;
2237}
2238
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002239static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2240{
Al Viro6131ffa2013-02-27 16:59:05 -05002241 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002242 struct fuse_conn *fc = get_fuse_conn(inode);
2243 int err;
2244
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002245 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002246 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002247 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002248 struct fuse_file *ff = file->private_data;
2249
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002250 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002251 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002252 err = fuse_setlk(file, fl, 1);
2253 }
2254
2255 return err;
2256}
2257
Miklos Szeredib2d22722006-12-06 20:35:51 -08002258static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2259{
2260 struct inode *inode = mapping->host;
2261 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002262 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002263 struct fuse_bmap_in inarg;
2264 struct fuse_bmap_out outarg;
2265 int err;
2266
2267 if (!inode->i_sb->s_bdev || fc->no_bmap)
2268 return 0;
2269
Miklos Szeredib2d22722006-12-06 20:35:51 -08002270 memset(&inarg, 0, sizeof(inarg));
2271 inarg.block = block;
2272 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002273 args.in.h.opcode = FUSE_BMAP;
2274 args.in.h.nodeid = get_node_id(inode);
2275 args.in.numargs = 1;
2276 args.in.args[0].size = sizeof(inarg);
2277 args.in.args[0].value = &inarg;
2278 args.out.numargs = 1;
2279 args.out.args[0].size = sizeof(outarg);
2280 args.out.args[0].value = &outarg;
2281 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002282 if (err == -ENOSYS)
2283 fc->no_bmap = 1;
2284
2285 return err ? 0 : outarg.block;
2286}
2287
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302288static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2289{
2290 struct inode *inode = file->f_mapping->host;
2291 struct fuse_conn *fc = get_fuse_conn(inode);
2292 struct fuse_file *ff = file->private_data;
2293 FUSE_ARGS(args);
2294 struct fuse_lseek_in inarg = {
2295 .fh = ff->fh,
2296 .offset = offset,
2297 .whence = whence
2298 };
2299 struct fuse_lseek_out outarg;
2300 int err;
2301
2302 if (fc->no_lseek)
2303 goto fallback;
2304
2305 args.in.h.opcode = FUSE_LSEEK;
2306 args.in.h.nodeid = ff->nodeid;
2307 args.in.numargs = 1;
2308 args.in.args[0].size = sizeof(inarg);
2309 args.in.args[0].value = &inarg;
2310 args.out.numargs = 1;
2311 args.out.args[0].size = sizeof(outarg);
2312 args.out.args[0].value = &outarg;
2313 err = fuse_simple_request(fc, &args);
2314 if (err) {
2315 if (err == -ENOSYS) {
2316 fc->no_lseek = 1;
2317 goto fallback;
2318 }
2319 return err;
2320 }
2321
2322 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2323
2324fallback:
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002325 err = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302326 if (!err)
2327 return generic_file_llseek(file, offset, whence);
2328 else
2329 return err;
2330}
2331
Andrew Morton965c8e52012-12-17 15:59:39 -08002332static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002333{
2334 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002335 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002336
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302337 switch (whence) {
2338 case SEEK_SET:
2339 case SEEK_CUR:
2340 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002341 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302342 break;
2343 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002344 inode_lock(inode);
Miklos Szeredi5b97eea2017-09-12 16:57:54 +02002345 retval = fuse_update_attributes(inode, file);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302346 if (!retval)
2347 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002348 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302349 break;
2350 case SEEK_HOLE:
2351 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002352 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302353 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002354 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302355 break;
2356 default:
2357 retval = -EINVAL;
2358 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002359
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002360 return retval;
2361}
2362
Tejun Heo59efec72008-11-26 12:03:55 +01002363/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002364 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2365 * ABI was defined to be 'struct iovec' which is different on 32bit
2366 * and 64bit. Fortunately we can determine which structure the server
2367 * used from the size of the reply.
2368 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002369static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2370 size_t transferred, unsigned count,
2371 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002372{
2373#ifdef CONFIG_COMPAT
2374 if (count * sizeof(struct compat_iovec) == transferred) {
2375 struct compat_iovec *ciov = src;
2376 unsigned i;
2377
2378 /*
2379 * With this interface a 32bit server cannot support
2380 * non-compat (i.e. ones coming from 64bit apps) ioctl
2381 * requests
2382 */
2383 if (!is_compat)
2384 return -EINVAL;
2385
2386 for (i = 0; i < count; i++) {
2387 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2388 dst[i].iov_len = ciov[i].iov_len;
2389 }
2390 return 0;
2391 }
2392#endif
2393
2394 if (count * sizeof(struct iovec) != transferred)
2395 return -EIO;
2396
2397 memcpy(dst, src, transferred);
2398 return 0;
2399}
2400
Miklos Szeredi75727772010-11-30 16:39:27 +01002401/* Make sure iov_length() won't overflow */
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002402static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2403 size_t count)
Miklos Szeredi75727772010-11-30 16:39:27 +01002404{
2405 size_t n;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002406 u32 max = fc->max_pages << PAGE_SHIFT;
Miklos Szeredi75727772010-11-30 16:39:27 +01002407
Zach Brownfb6ccff2012-07-24 12:10:11 -07002408 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002409 if (iov->iov_len > (size_t) max)
2410 return -ENOMEM;
2411 max -= iov->iov_len;
2412 }
2413 return 0;
2414}
2415
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002416static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2417 void *src, size_t transferred, unsigned count,
2418 bool is_compat)
2419{
2420 unsigned i;
2421 struct fuse_ioctl_iovec *fiov = src;
2422
2423 if (fc->minor < 16) {
2424 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2425 count, is_compat);
2426 }
2427
2428 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2429 return -EIO;
2430
2431 for (i = 0; i < count; i++) {
2432 /* Did the server supply an inappropriate value? */
2433 if (fiov[i].base != (unsigned long) fiov[i].base ||
2434 fiov[i].len != (unsigned long) fiov[i].len)
2435 return -EIO;
2436
2437 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2438 dst[i].iov_len = (size_t) fiov[i].len;
2439
2440#ifdef CONFIG_COMPAT
2441 if (is_compat &&
2442 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2443 (compat_size_t) dst[i].iov_len != fiov[i].len))
2444 return -EIO;
2445#endif
2446 }
2447
2448 return 0;
2449}
2450
2451
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002452/*
Tejun Heo59efec72008-11-26 12:03:55 +01002453 * For ioctls, there is no generic way to determine how much memory
2454 * needs to be read and/or written. Furthermore, ioctls are allowed
2455 * to dereference the passed pointer, so the parameter requires deep
2456 * copying but FUSE has no idea whatsoever about what to copy in or
2457 * out.
2458 *
2459 * This is solved by allowing FUSE server to retry ioctl with
2460 * necessary in/out iovecs. Let's assume the ioctl implementation
2461 * needs to read in the following structure.
2462 *
2463 * struct a {
2464 * char *buf;
2465 * size_t buflen;
2466 * }
2467 *
2468 * On the first callout to FUSE server, inarg->in_size and
2469 * inarg->out_size will be NULL; then, the server completes the ioctl
2470 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2471 * the actual iov array to
2472 *
2473 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2474 *
2475 * which tells FUSE to copy in the requested area and retry the ioctl.
2476 * On the second round, the server has access to the structure and
2477 * from that it can tell what to look for next, so on the invocation,
2478 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2479 *
2480 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2481 * { .iov_base = a.buf, .iov_len = a.buflen } }
2482 *
2483 * FUSE will copy both struct a and the pointed buffer from the
2484 * process doing the ioctl and retry ioctl with both struct a and the
2485 * buffer.
2486 *
2487 * This time, FUSE server has everything it needs and completes ioctl
2488 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2489 *
2490 * Copying data out works the same way.
2491 *
2492 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2493 * automatically initializes in and out iovs by decoding @cmd with
2494 * _IOC_* macros and the server is not allowed to request RETRY. This
2495 * limits ioctl data transfers to well-formed ioctls and is the forced
2496 * behavior for all FUSE servers.
2497 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002498long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2499 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002500{
Tejun Heo59efec72008-11-26 12:03:55 +01002501 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002502 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002503 struct fuse_ioctl_in inarg = {
2504 .fh = ff->fh,
2505 .cmd = cmd,
2506 .arg = arg,
2507 .flags = flags
2508 };
2509 struct fuse_ioctl_out outarg;
2510 struct fuse_req *req = NULL;
2511 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002512 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002513 struct iovec *in_iov = NULL, *out_iov = NULL;
2514 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002515 size_t in_size, out_size, transferred, c;
2516 int err, i;
2517 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002518
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002519#if BITS_PER_LONG == 32
2520 inarg.flags |= FUSE_IOCTL_32BIT;
2521#else
2522 if (flags & FUSE_IOCTL_COMPAT)
2523 inarg.flags |= FUSE_IOCTL_32BIT;
2524#endif
2525
Tejun Heo59efec72008-11-26 12:03:55 +01002526 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002527 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002528
Tejun Heo59efec72008-11-26 12:03:55 +01002529 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002530 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002531 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002532 if (!pages || !iov_page)
2533 goto out;
2534
2535 /*
2536 * If restricted, initialize IO parameters as encoded in @cmd.
2537 * RETRY from server is not allowed.
2538 */
2539 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002540 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002541
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002542 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002543 iov->iov_len = _IOC_SIZE(cmd);
2544
2545 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2546 in_iov = iov;
2547 in_iovs = 1;
2548 }
2549
2550 if (_IOC_DIR(cmd) & _IOC_READ) {
2551 out_iov = iov;
2552 out_iovs = 1;
2553 }
2554 }
2555
2556 retry:
2557 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2558 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2559
2560 /*
2561 * Out data can be used either for actual out data or iovs,
2562 * make sure there always is at least one page.
2563 */
2564 out_size = max_t(size_t, out_size, PAGE_SIZE);
2565 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2566
2567 /* make sure there are enough buffer pages and init request with them */
2568 err = -ENOMEM;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002569 if (max_pages > fc->max_pages)
Tejun Heo59efec72008-11-26 12:03:55 +01002570 goto out;
2571 while (num_pages < max_pages) {
2572 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2573 if (!pages[num_pages])
2574 goto out;
2575 num_pages++;
2576 }
2577
Maxim Patlasov54b96672012-10-26 19:49:13 +04002578 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002579 if (IS_ERR(req)) {
2580 err = PTR_ERR(req);
2581 req = NULL;
2582 goto out;
2583 }
2584 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2585 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002586 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002587
2588 /* okay, let's send it to the client */
2589 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002590 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002591 req->in.numargs = 1;
2592 req->in.args[0].size = sizeof(inarg);
2593 req->in.args[0].value = &inarg;
2594 if (in_size) {
2595 req->in.numargs++;
2596 req->in.args[1].size = in_size;
2597 req->in.argpages = 1;
2598
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002599 err = -EFAULT;
2600 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2601 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2602 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2603 if (c != PAGE_SIZE && iov_iter_count(&ii))
2604 goto out;
2605 }
Tejun Heo59efec72008-11-26 12:03:55 +01002606 }
2607
2608 req->out.numargs = 2;
2609 req->out.args[0].size = sizeof(outarg);
2610 req->out.args[0].value = &outarg;
2611 req->out.args[1].size = out_size;
2612 req->out.argpages = 1;
2613 req->out.argvar = 1;
2614
Tejun Heob93f8582008-11-26 12:03:55 +01002615 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002616 err = req->out.h.error;
2617 transferred = req->out.args[1].size;
2618 fuse_put_request(fc, req);
2619 req = NULL;
2620 if (err)
2621 goto out;
2622
2623 /* did it ask for retry? */
2624 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002625 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002626
2627 /* no retry if in restricted mode */
2628 err = -EIO;
2629 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2630 goto out;
2631
2632 in_iovs = outarg.in_iovs;
2633 out_iovs = outarg.out_iovs;
2634
2635 /*
2636 * Make sure things are in boundary, separate checks
2637 * are to protect against overflow.
2638 */
2639 err = -ENOMEM;
2640 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2641 out_iovs > FUSE_IOCTL_MAX_IOV ||
2642 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2643 goto out;
2644
Cong Wang2408f6e2011-11-25 23:14:30 +08002645 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002646 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002647 transferred, in_iovs + out_iovs,
2648 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002649 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002650 if (err)
2651 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002652
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002653 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002654 out_iov = in_iov + in_iovs;
2655
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002656 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002657 if (err)
2658 goto out;
2659
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002660 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
Miklos Szeredi75727772010-11-30 16:39:27 +01002661 if (err)
2662 goto out;
2663
Tejun Heo59efec72008-11-26 12:03:55 +01002664 goto retry;
2665 }
2666
2667 err = -EIO;
2668 if (transferred > inarg.out_size)
2669 goto out;
2670
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002671 err = -EFAULT;
2672 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2673 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2674 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2675 if (c != PAGE_SIZE && iov_iter_count(&ii))
2676 goto out;
2677 }
2678 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002679 out:
2680 if (req)
2681 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002682 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002683 while (num_pages)
2684 __free_page(pages[--num_pages]);
2685 kfree(pages);
2686
2687 return err ? err : outarg.result;
2688}
Tejun Heo08cbf542009-04-14 10:54:53 +09002689EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002690
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002691long fuse_ioctl_common(struct file *file, unsigned int cmd,
2692 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002693{
Al Viro6131ffa2013-02-27 16:59:05 -05002694 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002695 struct fuse_conn *fc = get_fuse_conn(inode);
2696
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002697 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002698 return -EACCES;
2699
2700 if (is_bad_inode(inode))
2701 return -EIO;
2702
2703 return fuse_do_ioctl(file, cmd, arg, flags);
2704}
2705
Tejun Heo59efec72008-11-26 12:03:55 +01002706static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2707 unsigned long arg)
2708{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002709 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002710}
2711
2712static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2713 unsigned long arg)
2714{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002715 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002716}
2717
Tejun Heo95668a62008-11-26 12:03:55 +01002718/*
2719 * All files which have been polled are linked to RB tree
2720 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2721 * find the matching one.
2722 */
2723static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2724 struct rb_node **parent_out)
2725{
2726 struct rb_node **link = &fc->polled_files.rb_node;
2727 struct rb_node *last = NULL;
2728
2729 while (*link) {
2730 struct fuse_file *ff;
2731
2732 last = *link;
2733 ff = rb_entry(last, struct fuse_file, polled_node);
2734
2735 if (kh < ff->kh)
2736 link = &last->rb_left;
2737 else if (kh > ff->kh)
2738 link = &last->rb_right;
2739 else
2740 return link;
2741 }
2742
2743 if (parent_out)
2744 *parent_out = last;
2745 return link;
2746}
2747
2748/*
2749 * The file is about to be polled. Make sure it's on the polled_files
2750 * RB tree. Note that files once added to the polled_files tree are
2751 * not removed before the file is released. This is because a file
2752 * polled once is likely to be polled again.
2753 */
2754static void fuse_register_polled_file(struct fuse_conn *fc,
2755 struct fuse_file *ff)
2756{
2757 spin_lock(&fc->lock);
2758 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002759 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002760
2761 link = fuse_find_polled_node(fc, ff->kh, &parent);
2762 BUG_ON(*link);
2763 rb_link_node(&ff->polled_node, parent, link);
2764 rb_insert_color(&ff->polled_node, &fc->polled_files);
2765 }
2766 spin_unlock(&fc->lock);
2767}
2768
Al Viro076ccb72017-07-03 01:02:18 -04002769__poll_t fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002770{
Tejun Heo95668a62008-11-26 12:03:55 +01002771 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002772 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002773 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2774 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002775 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002776 int err;
2777
2778 if (fc->no_poll)
2779 return DEFAULT_POLLMASK;
2780
2781 poll_wait(file, &ff->poll_wait, wait);
Al Viroc71d2272017-11-29 19:00:41 -05002782 inarg.events = mangle_poll(poll_requested_events(wait));
Tejun Heo95668a62008-11-26 12:03:55 +01002783
2784 /*
2785 * Ask for notification iff there's someone waiting for it.
2786 * The client may ignore the flag and always notify.
2787 */
2788 if (waitqueue_active(&ff->poll_wait)) {
2789 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2790 fuse_register_polled_file(fc, ff);
2791 }
2792
Miklos Szeredi70781872014-12-12 09:49:05 +01002793 args.in.h.opcode = FUSE_POLL;
2794 args.in.h.nodeid = ff->nodeid;
2795 args.in.numargs = 1;
2796 args.in.args[0].size = sizeof(inarg);
2797 args.in.args[0].value = &inarg;
2798 args.out.numargs = 1;
2799 args.out.args[0].size = sizeof(outarg);
2800 args.out.args[0].value = &outarg;
2801 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002802
2803 if (!err)
Al Viroc71d2272017-11-29 19:00:41 -05002804 return demangle_poll(outarg.revents);
Tejun Heo95668a62008-11-26 12:03:55 +01002805 if (err == -ENOSYS) {
2806 fc->no_poll = 1;
2807 return DEFAULT_POLLMASK;
2808 }
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002809 return EPOLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002810}
Tejun Heo08cbf542009-04-14 10:54:53 +09002811EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002812
2813/*
2814 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2815 * wakes up the poll waiters.
2816 */
2817int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2818 struct fuse_notify_poll_wakeup_out *outarg)
2819{
2820 u64 kh = outarg->kh;
2821 struct rb_node **link;
2822
2823 spin_lock(&fc->lock);
2824
2825 link = fuse_find_polled_node(fc, kh, NULL);
2826 if (*link) {
2827 struct fuse_file *ff;
2828
2829 ff = rb_entry(*link, struct fuse_file, polled_node);
2830 wake_up_interruptible_sync(&ff->poll_wait);
2831 }
2832
2833 spin_unlock(&fc->lock);
2834 return 0;
2835}
2836
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002837static void fuse_do_truncate(struct file *file)
2838{
2839 struct inode *inode = file->f_mapping->host;
2840 struct iattr attr;
2841
2842 attr.ia_valid = ATTR_SIZE;
2843 attr.ia_size = i_size_read(inode);
2844
2845 attr.ia_file = file;
2846 attr.ia_valid |= ATTR_FILE;
2847
Jan Kara62490332016-05-26 17:12:41 +02002848 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002849}
2850
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002851static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002852{
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002853 return round_up(off, fc->max_pages << PAGE_SHIFT);
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002854}
2855
Anand Avati4273b792012-02-17 12:46:25 -05002856static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002857fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002858{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002859 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002860 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002861 struct file *file = iocb->ki_filp;
2862 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002863 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002864 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002865 struct inode *inode;
2866 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002867 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002868 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002869 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002870
Anand Avati4273b792012-02-17 12:46:25 -05002871 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002872 inode = file->f_mapping->host;
2873 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002874
Omar Sandoval6f673762015-03-16 04:33:52 -07002875 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002876 return 0;
2877
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002878 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002879 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002880 if (offset >= i_size)
2881 return 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03002882 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
Al Viro6b775b12015-04-07 15:06:19 -04002883 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002884 }
2885
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002886 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002887 if (!io)
2888 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002890 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002891 io->reqs = 1;
2892 io->bytes = -1;
2893 io->size = 0;
2894 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002895 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002896 io->err = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002897 /*
2898 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002899 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002900 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002901 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002902 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302903 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002904
2905 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302906 * We cannot asynchronously extend the size of a file.
2907 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002908 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302909 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2910 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002911
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302912 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002913 /*
2914 * Additional reference to keep io around after
2915 * calling fuse_aio_complete()
2916 */
2917 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002918 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002919 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002920
Omar Sandoval6f673762015-03-16 04:33:52 -07002921 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002922 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002923 fuse_invalidate_attr(inode);
2924 } else {
Al Virod22a9432014-03-16 15:50:47 -04002925 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002926 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002927
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002928 if (io->async) {
Lukas Czernerebacb812018-11-09 14:51:46 +01002929 bool blocking = io->blocking;
2930
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002931 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2932
2933 /* we have a non-extending, async request, so return */
Lukas Czernerebacb812018-11-09 14:51:46 +01002934 if (!blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002935 return -EIOCBQUEUED;
2936
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002937 wait_for_completion(&wait);
2938 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002939 }
2940
Seth Forshee744742d2016-03-11 10:35:34 -06002941 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002942
Omar Sandoval6f673762015-03-16 04:33:52 -07002943 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002944 if (ret > 0)
2945 fuse_write_update_size(inode, pos);
2946 else if (ret < 0 && offset + count > i_size)
2947 fuse_do_truncate(file);
2948 }
Anand Avati4273b792012-02-17 12:46:25 -05002949
2950 return ret;
2951}
2952
Miklos Szeredicdadb112012-11-10 16:55:56 +01002953static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2954 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002955{
2956 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002957 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002958 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002959 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002960 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002961 struct fuse_fallocate_in inarg = {
2962 .fh = ff->fh,
2963 .offset = offset,
2964 .length = length,
2965 .mode = mode
2966 };
2967 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002968 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2969 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002970
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002971 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2972 return -EOPNOTSUPP;
2973
Miklos Szeredi519c6042012-04-26 10:56:36 +02002974 if (fc->no_fallocate)
2975 return -EOPNOTSUPP;
2976
Maxim Patlasov14c14412013-06-13 12:16:39 +04002977 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002978 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002979 if (mode & FALLOC_FL_PUNCH_HOLE) {
2980 loff_t endbyte = offset + length - 1;
2981 err = filemap_write_and_wait_range(inode->i_mapping,
2982 offset, endbyte);
2983 if (err)
2984 goto out;
2985
2986 fuse_sync_writes(inode);
2987 }
Brian Foster3634a632013-05-17 09:30:32 -04002988 }
2989
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002990 if (!(mode & FALLOC_FL_KEEP_SIZE))
2991 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2992
Miklos Szeredi70781872014-12-12 09:49:05 +01002993 args.in.h.opcode = FUSE_FALLOCATE;
2994 args.in.h.nodeid = ff->nodeid;
2995 args.in.numargs = 1;
2996 args.in.args[0].size = sizeof(inarg);
2997 args.in.args[0].value = &inarg;
2998 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002999 if (err == -ENOSYS) {
3000 fc->no_fallocate = 1;
3001 err = -EOPNOTSUPP;
3002 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003003 if (err)
3004 goto out;
3005
3006 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003007 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3008 bool changed = fuse_write_update_size(inode, offset + length);
3009
Miklos Szeredi93d22692014-04-28 14:19:22 +02003010 if (changed && fc->writeback_cache)
3011 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003012 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003013
3014 if (mode & FALLOC_FL_PUNCH_HOLE)
3015 truncate_pagecache_range(inode, offset, offset + length - 1);
3016
3017 fuse_invalidate_attr(inode);
3018
Brian Foster3634a632013-05-17 09:30:32 -04003019out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003020 if (!(mode & FALLOC_FL_KEEP_SIZE))
3021 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3022
Maxim Patlasovbde52782013-09-13 19:19:54 +04003023 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003024 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003025
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003026 return err;
3027}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003028
Niels de Vos88bc7d52018-08-21 14:36:31 +02003029static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3030 struct file *file_out, loff_t pos_out,
3031 size_t len, unsigned int flags)
3032{
3033 struct fuse_file *ff_in = file_in->private_data;
3034 struct fuse_file *ff_out = file_out->private_data;
3035 struct inode *inode_out = file_inode(file_out);
3036 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3037 struct fuse_conn *fc = ff_in->fc;
3038 FUSE_ARGS(args);
3039 struct fuse_copy_file_range_in inarg = {
3040 .fh_in = ff_in->fh,
3041 .off_in = pos_in,
3042 .nodeid_out = ff_out->nodeid,
3043 .fh_out = ff_out->fh,
3044 .off_out = pos_out,
3045 .len = len,
3046 .flags = flags
3047 };
3048 struct fuse_write_out outarg;
3049 ssize_t err;
3050 /* mark unstable when write-back is not used, and file_out gets
3051 * extended */
3052 bool is_unstable = (!fc->writeback_cache) &&
3053 ((pos_out + len) > inode_out->i_size);
3054
3055 if (fc->no_copy_file_range)
3056 return -EOPNOTSUPP;
3057
3058 inode_lock(inode_out);
3059
3060 if (fc->writeback_cache) {
3061 err = filemap_write_and_wait_range(inode_out->i_mapping,
3062 pos_out, pos_out + len);
3063 if (err)
3064 goto out;
3065
3066 fuse_sync_writes(inode_out);
3067 }
3068
3069 if (is_unstable)
3070 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3071
3072 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3073 args.in.h.nodeid = ff_in->nodeid;
3074 args.in.numargs = 1;
3075 args.in.args[0].size = sizeof(inarg);
3076 args.in.args[0].value = &inarg;
3077 args.out.numargs = 1;
3078 args.out.args[0].size = sizeof(outarg);
3079 args.out.args[0].value = &outarg;
3080 err = fuse_simple_request(fc, &args);
3081 if (err == -ENOSYS) {
3082 fc->no_copy_file_range = 1;
3083 err = -EOPNOTSUPP;
3084 }
3085 if (err)
3086 goto out;
3087
3088 if (fc->writeback_cache) {
3089 fuse_write_update_size(inode_out, pos_out + outarg.size);
3090 file_update_time(file_out);
3091 }
3092
3093 fuse_invalidate_attr(inode_out);
3094
3095 err = outarg.size;
3096out:
3097 if (is_unstable)
3098 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3099
3100 inode_unlock(inode_out);
3101
3102 return err;
3103}
3104
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003105static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003106 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003107 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003108 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003109 .mmap = fuse_file_mmap,
3110 .open = fuse_open,
3111 .flush = fuse_flush,
3112 .release = fuse_release,
3113 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003114 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003115 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003116 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003117 .unlocked_ioctl = fuse_file_ioctl,
3118 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003119 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003120 .fallocate = fuse_file_fallocate,
Niels de Vos88bc7d52018-08-21 14:36:31 +02003121 .copy_file_range = fuse_copy_file_range,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003122};
3123
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003124static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003125 .llseek = fuse_file_llseek,
Al Viro153162632015-03-30 22:08:36 -04003126 .read_iter = fuse_direct_read_iter,
Al Viro153162632015-03-30 22:08:36 -04003127 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003128 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003129 .open = fuse_open,
3130 .flush = fuse_flush,
3131 .release = fuse_release,
3132 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003133 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003134 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003135 .unlocked_ioctl = fuse_file_ioctl,
3136 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003137 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003138 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003139 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003140};
3141
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003142static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003143 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003144 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003145 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003146 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003147 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003148 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003149 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003150 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003151 .write_begin = fuse_write_begin,
3152 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003153};
3154
3155void fuse_init_file_inode(struct inode *inode)
3156{
Miklos Szerediab2257e2018-10-01 10:07:05 +02003157 struct fuse_inode *fi = get_fuse_inode(inode);
3158
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003159 inode->i_fop = &fuse_file_operations;
3160 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szerediab2257e2018-10-01 10:07:05 +02003161
3162 INIT_LIST_HEAD(&fi->write_files);
3163 INIT_LIST_HEAD(&fi->queued_writes);
3164 fi->writectr = 0;
3165 init_waitqueue_head(&fi->page_waitq);
3166 INIT_LIST_HEAD(&fi->writepages);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003167}