blob: c4353da0591dbeb005210b4bdd02b277bbb2b7ce [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>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040018#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070022
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020023static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010027 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070030 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010033 args.in.h.opcode = opcode;
34 args.in.h.nodeid = nodeid;
35 args.in.numargs = 1;
36 args.in.args[0].size = sizeof(inarg);
37 args.in.args[0].value = &inarg;
38 args.out.numargs = 1;
39 args.out.args[0].size = sizeof(*outargp);
40 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080041
Miklos Szeredi70781872014-12-12 09:49:05 +010042 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080043}
44
Tejun Heoacf99432008-11-26 12:03:55 +010045struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080046{
47 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090048
Mateusz Jurczyk227559e2017-06-07 12:26:49 +020049 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090050 if (unlikely(!ff))
51 return NULL;
52
Miklos Szeredida5e4712009-04-28 16:56:36 +020053 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040054 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090055 if (unlikely(!ff->reserved_req)) {
56 kfree(ff);
57 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080058 }
Tejun Heo6b2db282009-04-14 10:54:49 +090059
60 INIT_LIST_HEAD(&ff->write_entry);
61 atomic_set(&ff->count, 0);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
64
65 spin_lock(&fc->lock);
66 ff->kh = ++fc->khctr;
67 spin_unlock(&fc->lock);
68
Miklos Szeredifd72faa2005-11-07 00:59:51 -080069 return ff;
70}
71
72void fuse_file_free(struct fuse_file *ff)
73{
Miklos Szeredi33649c92006-06-25 05:48:52 -070074 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 kfree(ff);
76}
77
Miklos Szeredic7b71432009-04-28 16:56:37 +020078struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070079{
80 atomic_inc(&ff->count);
81 return ff;
82}
83
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010084static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010086 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010087}
88
89static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070090{
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020093
Andrew Gallagher7678ac52013-11-05 16:05:52 +010094 if (ff->fc->no_open) {
95 /*
96 * Drop the release request when client does not
97 * implement 'open'
98 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +020099 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100100 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100101 fuse_put_request(ff->fc, req);
102 } else if (sync) {
Miklos Szeredi1e6be9c2017-02-22 20:08:25 +0100103 __set_bit(FR_FORCE, &req->flags);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200104 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100105 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100106 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100107 fuse_put_request(ff->fc, req);
108 } else {
109 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200110 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100111 fuse_request_send_background(ff->fc, req);
112 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700113 kfree(ff);
114 }
115}
116
Tejun Heo08cbf542009-04-14 10:54:53 +0900117int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
118 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200119{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200120 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200121 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
122
123 ff = fuse_file_alloc(fc);
124 if (!ff)
125 return -ENOMEM;
126
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100127 ff->fh = 0;
128 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
129 if (!fc->no_open || isdir) {
130 struct fuse_open_out outarg;
131 int err;
132
133 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
134 if (!err) {
135 ff->fh = outarg.fh;
136 ff->open_flags = outarg.open_flags;
137
138 } else if (err != -ENOSYS || isdir) {
139 fuse_file_free(ff);
140 return err;
141 } else {
142 fc->no_open = 1;
143 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200144 }
145
146 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100147 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200148
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200149 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200150 file->private_data = fuse_file_get(ff);
151
152 return 0;
153}
Tejun Heo08cbf542009-04-14 10:54:53 +0900154EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200155
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400156static void fuse_link_write_file(struct file *file)
157{
158 struct inode *inode = file_inode(file);
159 struct fuse_conn *fc = get_fuse_conn(inode);
160 struct fuse_inode *fi = get_fuse_inode(inode);
161 struct fuse_file *ff = file->private_data;
162 /*
163 * file may be written through mmap, so chain it onto the
164 * inodes's write_file list
165 */
166 spin_lock(&fc->lock);
167 if (list_empty(&ff->write_entry))
168 list_add(&ff->write_entry, &fi->write_files);
169 spin_unlock(&fc->lock);
170}
171
Miklos Szeredic7b71432009-04-28 16:56:37 +0200172void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800173{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800175 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176
177 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800178 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700180 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200181 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200182 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800183 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
184 struct fuse_inode *fi = get_fuse_inode(inode);
185
186 spin_lock(&fc->lock);
187 fi->attr_version = ++fc->attr_version;
188 i_size_write(inode, 0);
189 spin_unlock(&fc->lock);
190 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200191 if (fc->writeback_cache)
192 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800193 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400194 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
195 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800196}
197
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200198int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800199{
Tejun Heoacf99432008-11-26 12:03:55 +0100200 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200202 bool lock_inode = (file->f_flags & O_TRUNC) &&
203 fc->atomic_o_trunc &&
204 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700205
206 err = generic_file_open(inode, file);
207 if (err)
208 return err;
209
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200210 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500211 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200212
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200213 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700214
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200215 if (!err)
216 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200217
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200218 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500219 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200220
221 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222}
223
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200224static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800225{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200226 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700227 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800228 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200230 spin_lock(&fc->lock);
231 list_del(&ff->write_entry);
232 if (!RB_EMPTY_NODE(&ff->polled_node))
233 rb_erase(&ff->polled_node, &fc->polled_files);
234 spin_unlock(&fc->lock);
235
Bryan Green357ccf22011-03-01 16:43:52 -0800236 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200237
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700238 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800239 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700240 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200241 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700242 req->in.numargs = 1;
243 req->in.args[0].size = sizeof(struct fuse_release_in);
244 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800245}
246
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200247void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800248{
Tejun Heo6b2db282009-04-14 10:54:49 +0900249 struct fuse_file *ff;
250 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700251
Tejun Heo6b2db282009-04-14 10:54:49 +0900252 ff = file->private_data;
253 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200254 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700255
Tejun Heo6b2db282009-04-14 10:54:49 +0900256 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200257 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100258
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200259 if (ff->flock) {
260 struct fuse_release_in *inarg = &req->misc.release.in;
261 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
262 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
263 (fl_owner_t) file);
264 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100265 /* Hold inode until release is finished */
266 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700267
Tejun Heo6b2db282009-04-14 10:54:49 +0900268 /*
269 * Normally this will send the RELEASE request, however if
270 * some asynchronous READ or WRITE requests are outstanding,
271 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100272 *
273 * Make the release synchronous if this is a fuseblk mount,
274 * synchronous RELEASE is allowed (and desirable) in this case
275 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900276 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100277 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700278}
279
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700280static int fuse_open(struct inode *inode, struct file *file)
281{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200282 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700283}
284
285static int fuse_release(struct inode *inode, struct file *file)
286{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400287 struct fuse_conn *fc = get_fuse_conn(inode);
288
289 /* see fuse_vma_close() for !writeback_cache case */
290 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200291 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400292
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200293 fuse_release_common(file, FUSE_RELEASE);
294
295 /* return value is ignored by VFS */
296 return 0;
297}
298
299void fuse_sync_release(struct fuse_file *ff, int flags)
300{
301 WARN_ON(atomic_read(&ff->count) > 1);
302 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200303 __set_bit(FR_FORCE, &ff->reserved_req->flags);
304 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200305 fuse_request_send(ff->fc, ff->reserved_req);
306 fuse_put_request(ff->fc, ff->reserved_req);
307 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700308}
Tejun Heo08cbf542009-04-14 10:54:53 +0900309EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700310
Miklos Szeredi71421252006-06-25 05:48:52 -0700311/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700312 * Scramble the ID space with XTEA, so that the value of the files_struct
313 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700314 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700315u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700316{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700317 u32 *k = fc->scramble_key;
318 u64 v = (unsigned long) id;
319 u32 v0 = v;
320 u32 v1 = v >> 32;
321 u32 sum = 0;
322 int i;
323
324 for (i = 0; i < 32; i++) {
325 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
326 sum += 0x9E3779B9;
327 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
328 }
329
330 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700331}
332
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700333/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400334 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700335 *
336 * This is currently done by walking the list of writepage requests
337 * for the inode, which can be pretty inefficient.
338 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400339static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
340 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700341{
342 struct fuse_conn *fc = get_fuse_conn(inode);
343 struct fuse_inode *fi = get_fuse_inode(inode);
344 struct fuse_req *req;
345 bool found = false;
346
347 spin_lock(&fc->lock);
348 list_for_each_entry(req, &fi->writepages, writepages_entry) {
349 pgoff_t curr_index;
350
351 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300352 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400353 if (idx_from < curr_index + req->num_pages &&
354 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700355 found = true;
356 break;
357 }
358 }
359 spin_unlock(&fc->lock);
360
361 return found;
362}
363
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400364static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
365{
366 return fuse_range_is_writeback(inode, index, index);
367}
368
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700369/*
370 * Wait for page writeback to be completed.
371 *
372 * Since fuse doesn't rely on the VM writeback tracking, this has to
373 * use some other means.
374 */
375static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
376{
377 struct fuse_inode *fi = get_fuse_inode(inode);
378
379 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
380 return 0;
381}
382
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400383/*
384 * Wait for all pending writepages on the inode to finish.
385 *
386 * This is currently done by blocking further writes with FUSE_NOWRITE
387 * and waiting for all sent writes to complete.
388 *
389 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
390 * could conflict with truncation.
391 */
392static void fuse_sync_writes(struct inode *inode)
393{
394 fuse_set_nowrite(inode);
395 fuse_release_nowrite(inode);
396}
397
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700398static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700399{
Al Viro6131ffa2013-02-27 16:59:05 -0500400 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700401 struct fuse_conn *fc = get_fuse_conn(inode);
402 struct fuse_file *ff = file->private_data;
403 struct fuse_req *req;
404 struct fuse_flush_in inarg;
405 int err;
406
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800407 if (is_bad_inode(inode))
408 return -EIO;
409
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700410 if (fc->no_flush)
411 return 0;
412
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200413 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400414 if (err)
415 return err;
416
Al Viro59551022016-01-22 15:40:57 -0500417 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400418 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500419 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400420
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200421 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700422 if (err)
423 return err;
424
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400425 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426 memset(&inarg, 0, sizeof(inarg));
427 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700428 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700429 req->in.h.opcode = FUSE_FLUSH;
430 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 req->in.numargs = 1;
432 req->in.args[0].size = sizeof(inarg);
433 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200434 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100435 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700436 err = req->out.h.error;
437 fuse_put_request(fc, req);
438 if (err == -ENOSYS) {
439 fc->no_flush = 1;
440 err = 0;
441 }
442 return err;
443}
444
Josef Bacik02c24a82011-07-16 20:44:56 -0400445int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
446 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200448 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 struct fuse_conn *fc = get_fuse_conn(inode);
450 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100451 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700452 struct fuse_fsync_in inarg;
453 int err;
454
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800455 if (is_bad_inode(inode))
456 return -EIO;
457
Al Viro59551022016-01-22 15:40:57 -0500458 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400459
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700460 /*
461 * Start writeback against all dirty pages of the inode, then
462 * wait for all outstanding writes, before sending the FSYNC
463 * request.
464 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200465 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700466 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400467 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700468
469 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700470
471 /*
472 * Due to implementation of fuse writeback
473 * filemap_write_and_wait_range() does not catch errors.
474 * We have to do this directly after fuse_sync_writes()
475 */
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200476 err = filemap_check_errors(file->f_mapping);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700477 if (err)
478 goto out;
479
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200480 err = sync_inode_metadata(inode, 1);
481 if (err)
482 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700483
Miklos Szeredi22401e72014-04-28 14:19:23 +0200484 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
485 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400486
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700487 memset(&inarg, 0, sizeof(inarg));
488 inarg.fh = ff->fh;
489 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100490 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
491 args.in.h.nodeid = get_node_id(inode);
492 args.in.numargs = 1;
493 args.in.args[0].size = sizeof(inarg);
494 args.in.args[0].value = &inarg;
495 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700496 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700497 if (isdir)
498 fc->no_fsyncdir = 1;
499 else
500 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501 err = 0;
502 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400503out:
Al Viro59551022016-01-22 15:40:57 -0500504 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505 return err;
506}
507
Josef Bacik02c24a82011-07-16 20:44:56 -0400508static int fuse_fsync(struct file *file, loff_t start, loff_t end,
509 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700510{
Josef Bacik02c24a82011-07-16 20:44:56 -0400511 return fuse_fsync_common(file, start, end, datasync, 0);
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
617 fuse_release_user_pages(req, !io->write);
618
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{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400653 struct file *file = io->file;
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{
Seth Forshee744742d2016-03-11 10:35:34 -0600715 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700716 struct inode *inode = page->mapping->host;
717 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800718 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700719 size_t num_read;
720 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300721 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700722 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800723 int err;
724
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700725 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300726 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700727 * page-cache page, so make sure we read a properly synced
728 * page.
729 */
730 fuse_wait_on_page_writeback(inode, page->index);
731
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400732 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700733 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400734 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700735
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700736 attr_ver = fuse_get_attr_version(fc);
737
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200739 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700740 req->num_pages = 1;
741 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400742 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400743 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700744 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700745
746 if (!err) {
747 /*
748 * Short read means EOF. If file size is larger, truncate it
749 */
750 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400751 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700752
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 }
755
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400756 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400757
758 return err;
759}
760
761static int fuse_readpage(struct file *file, struct page *page)
762{
763 struct inode *inode = page->mapping->host;
764 int err;
765
766 err = -EIO;
767 if (is_bad_inode(inode))
768 goto out;
769
770 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800771 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700772 out:
773 unlock_page(page);
774 return err;
775}
776
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800777static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700778{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800779 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700780 size_t count = req->misc.read.in.size;
781 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200782 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800783
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 for (i = 0; mapping == NULL && i < req->num_pages; i++)
785 mapping = req->pages[i]->mapping;
786
787 if (mapping) {
788 struct inode *inode = mapping->host;
789
790 /*
791 * Short read means EOF. If file size is larger, truncate it
792 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400793 if (!req->out.h.error && num_read < count)
794 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795
Andrew Gallagher451418f2013-11-05 03:55:43 -0800796 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700797 }
798
Miklos Szeredidb50b962005-09-09 13:10:33 -0700799 for (i = 0; i < req->num_pages; i++) {
800 struct page *page = req->pages[i];
801 if (!req->out.h.error)
802 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800803 else
804 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700805 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300806 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700807 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700808 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100809 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800810}
811
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200812static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200814 struct fuse_file *ff = file->private_data;
815 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800816 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300817 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200818
819 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800820 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200821 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200822 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700823 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800824 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700825 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800826 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100827 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800828 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100829 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800830 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100831 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800832 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700833}
834
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700835struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700836 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800837 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400839 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840};
841
Sami Tolvanen4fd840d2017-08-16 14:38:13 -0700842static int fuse_readpages_fill(struct file *_data, struct page *page)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843{
Sami Tolvanen4fd840d2017-08-16 14:38:13 -0700844 struct fuse_fill_data *data = (struct fuse_fill_data *)_data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 struct fuse_req *req = data->req;
846 struct inode *inode = data->inode;
847 struct fuse_conn *fc = get_fuse_conn(inode);
848
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700849 fuse_wait_on_page_writeback(inode, page->index);
850
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 if (req->num_pages &&
852 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300853 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400855 int nr_alloc = min_t(unsigned, data->nr_pages,
856 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200857 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400858 if (fc->async_read)
859 req = fuse_get_req_for_background(fc, nr_alloc);
860 else
861 req = fuse_get_req(fc, nr_alloc);
862
863 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700864 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700865 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400869
870 if (WARN_ON(req->num_pages >= req->max_pages)) {
871 fuse_put_request(fc, req);
872 return -EIO;
873 }
874
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300875 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400877 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100878 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 return 0;
881}
882
883static int fuse_readpages(struct file *file, struct address_space *mapping,
884 struct list_head *pages, unsigned nr_pages)
885{
886 struct inode *inode = mapping->host;
887 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700888 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700889 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400890 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800891
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700892 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800893 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800894 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895
Miklos Szeredia6643092007-11-28 16:22:00 -0800896 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700897 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400898 if (fc->async_read)
899 data.req = fuse_get_req_for_background(fc, nr_alloc);
900 else
901 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400902 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700903 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700904 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800905 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700906
907 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700908 if (!err) {
909 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200910 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700911 else
912 fuse_put_request(fc, data.req);
913 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800914out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700915 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916}
917
Al Viro37c20f12014-04-02 14:47:09 -0400918static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800919{
920 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400921 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800922
Brian Fostera8894272012-07-16 15:23:50 -0400923 /*
924 * In auto invalidate mode, always update attributes on read.
925 * Otherwise, only update if we attempt to read past EOF (to ensure
926 * i_size is up to date).
927 */
928 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400929 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800930 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
932 if (err)
933 return err;
934 }
935
Al Viro37c20f12014-04-02 14:47:09 -0400936 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800937}
938
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200939static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200940 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700941{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700942 struct fuse_write_in *inarg = &req->misc.write.in;
943 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700945 inarg->fh = ff->fh;
946 inarg->offset = pos;
947 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700948 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200949 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700950 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700952 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
953 else
954 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700955 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700956 req->in.args[1].size = count;
957 req->out.numargs = 1;
958 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700959 req->out.args[0].value = outarg;
960}
961
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400962static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200963 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700964{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400965 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 struct fuse_file *ff = file->private_data;
967 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200968 struct fuse_write_in *inarg = &req->misc.write.in;
969
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700972 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700973 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
974 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
975 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400976
977 if (io->async)
978 return fuse_async_req_send(fc, req, count, io);
979
Tejun Heob93f8582008-11-26 12:03:55 +0100980 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700981 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700982}
983
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400984bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700985{
986 struct fuse_conn *fc = get_fuse_conn(inode);
987 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400988 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700989
990 spin_lock(&fc->lock);
991 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400992 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700993 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400994 ret = true;
995 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400997
998 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700999}
1000
Nick Pigginea9b9902008-04-30 00:54:42 -07001001static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1002 struct inode *inode, loff_t pos,
1003 size_t count)
1004{
1005 size_t res;
1006 unsigned offset;
1007 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001008 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001009
1010 for (i = 0; i < req->num_pages; i++)
1011 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1012
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001013 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001014
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001015 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001016 count = res;
1017 for (i = 0; i < req->num_pages; i++) {
1018 struct page *page = req->pages[i];
1019
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001020 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001021 SetPageUptodate(page);
1022
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001023 if (count > PAGE_SIZE - offset)
1024 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001025 else
1026 count = 0;
1027 offset = 0;
1028
1029 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001030 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001031 }
1032
1033 return res;
1034}
1035
1036static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1037 struct address_space *mapping,
1038 struct iov_iter *ii, loff_t pos)
1039{
1040 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001042 size_t count = 0;
1043 int err;
1044
Miklos Szeredif4975c62009-04-02 14:25:34 +02001045 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001046 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001047
1048 do {
1049 size_t tmp;
1050 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001051 pgoff_t index = pos >> PAGE_SHIFT;
1052 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001053 iov_iter_count(ii));
1054
1055 bytes = min_t(size_t, bytes, fc->max_write - count);
1056
1057 again:
1058 err = -EFAULT;
1059 if (iov_iter_fault_in_readable(ii, bytes))
1060 break;
1061
1062 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001063 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001064 if (!page)
1065 break;
1066
anfei zhou931e80e2010-02-02 13:44:02 -08001067 if (mapping_writably_mapped(mapping))
1068 flush_dcache_page(page);
1069
Nick Pigginea9b9902008-04-30 00:54:42 -07001070 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001071 flush_dcache_page(page);
1072
Roman Gushchin3ca81382015-10-12 16:33:44 +03001073 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 if (!tmp) {
1075 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001076 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001077 bytes = min(bytes, iov_iter_single_seg_count(ii));
1078 goto again;
1079 }
1080
1081 err = 0;
1082 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001083 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 req->num_pages++;
1085
Nick Pigginea9b9902008-04-30 00:54:42 -07001086 count += tmp;
1087 pos += tmp;
1088 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001089 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 offset = 0;
1091
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001092 if (!fc->big_writes)
1093 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001095 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001096
1097 return count > 0 ? count : err;
1098}
1099
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001100static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1101{
1102 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001103 ((pos + len - 1) >> PAGE_SHIFT) -
1104 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001105 FUSE_MAX_PAGES_PER_REQ);
1106}
1107
Nick Pigginea9b9902008-04-30 00:54:42 -07001108static ssize_t fuse_perform_write(struct file *file,
1109 struct address_space *mapping,
1110 struct iov_iter *ii, loff_t pos)
1111{
1112 struct inode *inode = mapping->host;
1113 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001114 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 int err = 0;
1116 ssize_t res = 0;
1117
1118 if (is_bad_inode(inode))
1119 return -EIO;
1120
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001121 if (inode->i_size < pos + iov_iter_count(ii))
1122 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1123
Nick Pigginea9b9902008-04-30 00:54:42 -07001124 do {
1125 struct fuse_req *req;
1126 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001127 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001128
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001129 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001130 if (IS_ERR(req)) {
1131 err = PTR_ERR(req);
1132 break;
1133 }
1134
1135 count = fuse_fill_write_pages(req, mapping, ii, pos);
1136 if (count <= 0) {
1137 err = count;
1138 } else {
1139 size_t num_written;
1140
1141 num_written = fuse_send_write_pages(req, file, inode,
1142 pos, count);
1143 err = req->out.h.error;
1144 if (!err) {
1145 res += num_written;
1146 pos += num_written;
1147
1148 /* break out of the loop on short write */
1149 if (num_written != count)
1150 err = -EIO;
1151 }
1152 }
1153 fuse_put_request(fc, req);
1154 } while (!err && iov_iter_count(ii));
1155
1156 if (res > 0)
1157 fuse_write_update_size(inode, pos);
1158
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001159 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001160 fuse_invalidate_attr(inode);
1161
1162 return res > 0 ? res : err;
1163}
1164
Al Viro84c3d552014-04-03 14:33:23 -04001165static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001166{
1167 struct file *file = iocb->ki_filp;
1168 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001169 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001170 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001171 struct inode *inode = mapping->host;
1172 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001173 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001174
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001175 if (get_fuse_conn(inode)->writeback_cache) {
1176 /* Update size (EOF optimization) and mode (SUID clearing) */
1177 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1178 if (err)
1179 return err;
1180
Al Viro84c3d552014-04-03 14:33:23 -04001181 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001182 }
1183
Al Viro59551022016-01-22 15:40:57 -05001184 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001185
1186 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001187 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001188
Al Viro3309dd02015-04-09 12:55:47 -04001189 err = generic_write_checks(iocb, from);
1190 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001191 goto out;
1192
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001193 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 if (err)
1195 goto out;
1196
Josef Bacikc3b2da32012-03-26 09:59:21 -04001197 err = file_update_time(file);
1198 if (err)
1199 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001200
Al Viro2ba48ce2015-04-09 13:52:01 -04001201 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001202 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001203 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001204 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001205 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001206
Anand Avati4273b792012-02-17 12:46:25 -05001207 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001208
Al Viro84c3d552014-04-03 14:33:23 -04001209 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001210 if (written_buffered < 0) {
1211 err = written_buffered;
1212 goto out;
1213 }
1214 endbyte = pos + written_buffered - 1;
1215
1216 err = filemap_write_and_wait_range(file->f_mapping, pos,
1217 endbyte);
1218 if (err)
1219 goto out;
1220
1221 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001222 pos >> PAGE_SHIFT,
1223 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001224
1225 written += written_buffered;
1226 iocb->ki_pos = pos + written_buffered;
1227 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001228 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001229 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001230 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001231 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001232out:
1233 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001234 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001235
1236 return written ? written : err;
1237}
1238
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001239static inline void fuse_page_descs_length_init(struct fuse_req *req,
1240 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001241{
1242 int i;
1243
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001244 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001245 req->page_descs[i].length = PAGE_SIZE -
1246 req->page_descs[i].offset;
1247}
1248
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001249static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1250{
1251 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1252}
1253
1254static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1255 size_t max_size)
1256{
1257 return min(iov_iter_single_seg_count(ii), max_size);
1258}
1259
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001260static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001261 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001262{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001263 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001264 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001265
Miklos Szeredif4975c62009-04-02 14:25:34 +02001266 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001267 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001268 unsigned long user_addr = fuse_get_user_addr(ii);
1269 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1270
Miklos Szeredif4975c62009-04-02 14:25:34 +02001271 if (write)
1272 req->in.args[1].value = (void *) user_addr;
1273 else
1274 req->out.args[0].value = (void *) user_addr;
1275
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001276 iov_iter_advance(ii, frag_size);
1277 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001278 return 0;
1279 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001280
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001281 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001282 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001283 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001284 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001285 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001286 req->max_pages - req->num_pages,
1287 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001289 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001290
Al Viroc9c37e22014-03-16 16:08:30 -04001291 iov_iter_advance(ii, ret);
1292 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293
Al Viroc9c37e22014-03-16 16:08:30 -04001294 ret += start;
1295 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1296
1297 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298 fuse_page_descs_length_init(req, req->num_pages, npages);
1299
1300 req->num_pages += npages;
1301 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001302 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001303 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001304
1305 if (write)
1306 req->in.argpages = 1;
1307 else
1308 req->out.argpages = 1;
1309
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001311
Ashish Samant2c932d42016-03-25 10:53:41 -07001312 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001313}
1314
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001315static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1316{
Al Virof67da302014-03-19 01:16:16 -04001317 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001318}
1319
Al Virod22a9432014-03-16 15:50:47 -04001320ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1321 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001322{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001323 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001324 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001325 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001326 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001327 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001328 struct fuse_file *ff = file->private_data;
1329 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001330 size_t nmax = write ? fc->max_write : fc->max_read;
1331 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001332 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001333 pgoff_t idx_from = pos >> PAGE_SHIFT;
1334 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001336 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001337 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001338
Brian Fosterde82b922013-05-14 11:25:39 -04001339 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001340 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001341 else
Al Virod22a9432014-03-16 15:50:47 -04001342 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001343 if (IS_ERR(req))
1344 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001345
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001346 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1347 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001348 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001349 fuse_sync_writes(inode);
1350 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001351 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001352 }
1353
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001354 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001355 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001356 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001357 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001358 err = fuse_get_user_pages(req, iter, &nbytes, write);
1359 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001361
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001363 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001365 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001366
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001367 if (!io->async)
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001368 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001370 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 break;
1372 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001373 res = 0;
1374 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 break;
1376 }
1377 count -= nres;
1378 res += nres;
1379 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 if (nres != nbytes)
1381 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001382 if (count) {
1383 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001384 if (io->async)
1385 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001386 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001387 else
Al Virod22a9432014-03-16 15:50:47 -04001388 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001389 if (IS_ERR(req))
1390 break;
1391 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001393 if (!IS_ERR(req))
1394 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001395 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001397
Ashish Samant742f9922016-03-14 21:57:35 -07001398 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001399}
Tejun Heo08cbf542009-04-14 10:54:53 +09001400EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001401
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001402static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001403 struct iov_iter *iter,
1404 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001406 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001407 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001408 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001409
1410 if (is_bad_inode(inode))
1411 return -EIO;
1412
Al Virod22a9432014-03-16 15:50:47 -04001413 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001414
1415 fuse_invalidate_attr(inode);
1416
1417 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418}
1419
Al Viro15316262015-03-30 22:08:36 -04001420static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001421{
Seth Forshee744742d2016-03-11 10:35:34 -06001422 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001423 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001424}
1425
Al Viro15316262015-03-30 22:08:36 -04001426static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427{
Al Viro15316262015-03-30 22:08:36 -04001428 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001429 struct inode *inode = file_inode(file);
Seth Forshee744742d2016-03-11 10:35:34 -06001430 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001431 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001432
1433 if (is_bad_inode(inode))
1434 return -EIO;
1435
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001437 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001438 res = generic_write_checks(iocb, from);
1439 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001440 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001441 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001442 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001443 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001444 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001445
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001446 return res;
1447}
1448
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001449static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001450{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001451 int i;
1452
1453 for (i = 0; i < req->num_pages; i++)
1454 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001455
1456 if (req->ff)
1457 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001458}
1459
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001460static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001461{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001462 struct inode *inode = req->inode;
1463 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001464 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001465 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001466
1467 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001468 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001469 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001470 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001471 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001472 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001473 wake_up(&fi->page_waitq);
1474}
1475
1476/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001477static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1478 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001479__releases(fc->lock)
1480__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001481{
1482 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001483 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001484 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001485
1486 if (!fc->connected)
1487 goto out_free;
1488
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001489 if (inarg->offset + data_size <= size) {
1490 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001491 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001492 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493 } else {
1494 /* Got truncated off completely */
1495 goto out_free;
1496 }
1497
1498 req->in.args[1].size = inarg->size;
1499 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001500 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001501 return;
1502
1503 out_free:
1504 fuse_writepage_finish(fc, req);
1505 spin_unlock(&fc->lock);
1506 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001507 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001508 spin_lock(&fc->lock);
1509}
1510
1511/*
1512 * If fi->writectr is positive (no truncate or fsync going on) send
1513 * all queued writepage requests.
1514 *
1515 * Called with fc->lock
1516 */
1517void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001518__releases(fc->lock)
1519__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001520{
1521 struct fuse_conn *fc = get_fuse_conn(inode);
1522 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001523 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001524 struct fuse_req *req;
1525
1526 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1527 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1528 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001529 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001530 }
1531}
1532
1533static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1534{
1535 struct inode *inode = req->inode;
1536 struct fuse_inode *fi = get_fuse_inode(inode);
1537
1538 mapping_set_error(inode->i_mapping, req->out.h.error);
1539 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001540 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001541 struct fuse_conn *fc = get_fuse_conn(inode);
1542 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001543 struct fuse_req *next = req->misc.write.next;
1544 req->misc.write.next = next->misc.write.next;
1545 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001546 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001547 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001548
1549 /*
1550 * Skip fuse_flush_writepages() to make it easy to crop requests
1551 * based on primary request size.
1552 *
1553 * 1st case (trivial): there are no concurrent activities using
1554 * fuse_set/release_nowrite. Then we're on safe side because
1555 * fuse_flush_writepages() would call fuse_send_writepage()
1556 * anyway.
1557 *
1558 * 2nd case: someone called fuse_set_nowrite and it is waiting
1559 * now for completion of all in-flight requests. This happens
1560 * rarely and no more than once per page, so this should be
1561 * okay.
1562 *
1563 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1564 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1565 * that fuse_set_nowrite returned implies that all in-flight
1566 * requests were completed along with all of their secondary
1567 * requests. Further primary requests are blocked by negative
1568 * writectr. Hence there cannot be any in-flight requests and
1569 * no invocations of fuse_writepage_end() while we're in
1570 * fuse_set_nowrite..fuse_release_nowrite section.
1571 */
1572 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001573 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001574 fi->writectr--;
1575 fuse_writepage_finish(fc, req);
1576 spin_unlock(&fc->lock);
1577 fuse_writepage_free(fc, req);
1578}
1579
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001580static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1581 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001582{
Miklos Szeredi72523422013-10-01 16:44:52 +02001583 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001584
1585 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001586 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001587 ff = list_entry(fi->write_files.next, struct fuse_file,
1588 write_entry);
1589 fuse_file_get(ff);
1590 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001591 spin_unlock(&fc->lock);
1592
1593 return ff;
1594}
1595
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001596static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1597 struct fuse_inode *fi)
1598{
1599 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1600 WARN_ON(!ff);
1601 return ff;
1602}
1603
1604int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1605{
1606 struct fuse_conn *fc = get_fuse_conn(inode);
1607 struct fuse_inode *fi = get_fuse_inode(inode);
1608 struct fuse_file *ff;
1609 int err;
1610
1611 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001612 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001613 if (ff)
1614 fuse_file_put(ff, 0);
1615
1616 return err;
1617}
1618
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001619static int fuse_writepage_locked(struct page *page)
1620{
1621 struct address_space *mapping = page->mapping;
1622 struct inode *inode = mapping->host;
1623 struct fuse_conn *fc = get_fuse_conn(inode);
1624 struct fuse_inode *fi = get_fuse_inode(inode);
1625 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001626 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001627 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001628
1629 set_page_writeback(page);
1630
Maxim Patlasov4250c062012-10-26 19:48:07 +04001631 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001632 if (!req)
1633 goto err;
1634
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001635 /* writeback always goes to bg_queue */
1636 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001637 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1638 if (!tmp_page)
1639 goto err_free;
1640
Miklos Szeredi72523422013-10-01 16:44:52 +02001641 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001642 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001643 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001644 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001645
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001646 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001647
1648 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001649 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001650 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001651 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001652 req->num_pages = 1;
1653 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001654 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001655 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001656 req->end = fuse_writepage_end;
1657 req->inode = inode;
1658
Tejun Heo93f78d82015-05-22 17:13:27 -04001659 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001660 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001661
1662 spin_lock(&fc->lock);
1663 list_add(&req->writepages_entry, &fi->writepages);
1664 list_add_tail(&req->list, &fi->queued_writes);
1665 fuse_flush_writepages(inode);
1666 spin_unlock(&fc->lock);
1667
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001668 end_page_writeback(page);
1669
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001670 return 0;
1671
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001672err_nofile:
1673 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674err_free:
1675 fuse_request_free(req);
1676err:
1677 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001678 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001679}
1680
1681static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1682{
1683 int err;
1684
Miklos Szerediff17be02013-10-01 16:44:53 +02001685 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1686 /*
1687 * ->writepages() should be called for sync() and friends. We
1688 * should only get here on direct reclaim and then we are
1689 * allowed to skip a page which is already in flight
1690 */
1691 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1692
1693 redirty_page_for_writepage(wbc, page);
1694 return 0;
1695 }
1696
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001697 err = fuse_writepage_locked(page);
1698 unlock_page(page);
1699
1700 return err;
1701}
1702
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001703struct fuse_fill_wb_data {
1704 struct fuse_req *req;
1705 struct fuse_file *ff;
1706 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001707 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001708};
1709
1710static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1711{
1712 struct fuse_req *req = data->req;
1713 struct inode *inode = data->inode;
1714 struct fuse_conn *fc = get_fuse_conn(inode);
1715 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001716 int num_pages = req->num_pages;
1717 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001718
1719 req->ff = fuse_file_get(data->ff);
1720 spin_lock(&fc->lock);
1721 list_add_tail(&req->list, &fi->queued_writes);
1722 fuse_flush_writepages(inode);
1723 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001724
1725 for (i = 0; i < num_pages; i++)
1726 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001727}
1728
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001729static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1730 struct page *page)
1731{
1732 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1733 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1734 struct fuse_req *tmp;
1735 struct fuse_req *old_req;
1736 bool found = false;
1737 pgoff_t curr_index;
1738
1739 BUG_ON(new_req->num_pages != 0);
1740
1741 spin_lock(&fc->lock);
1742 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001743 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1744 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001745 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001746 if (curr_index <= page->index &&
1747 page->index < curr_index + old_req->num_pages) {
1748 found = true;
1749 break;
1750 }
1751 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001752 if (!found) {
1753 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001754 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001755 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001756
Maxim Patlasovf6011082013-10-02 15:01:07 +04001757 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001758 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1759 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001760 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001761 if (tmp->num_pages == 1 &&
1762 curr_index == page->index) {
1763 old_req = tmp;
1764 }
1765 }
1766
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001767 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001768 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001769
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001770 copy_highpage(old_req->pages[0], page);
1771 spin_unlock(&fc->lock);
1772
Tejun Heo93f78d82015-05-22 17:13:27 -04001773 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001774 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001775 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001776 fuse_writepage_free(fc, new_req);
1777 fuse_request_free(new_req);
1778 goto out;
1779 } else {
1780 new_req->misc.write.next = old_req->misc.write.next;
1781 old_req->misc.write.next = new_req;
1782 }
1783out_unlock:
1784 spin_unlock(&fc->lock);
1785out:
1786 return found;
1787}
1788
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001789static int fuse_writepages_fill(struct page *page,
1790 struct writeback_control *wbc, void *_data)
1791{
1792 struct fuse_fill_wb_data *data = _data;
1793 struct fuse_req *req = data->req;
1794 struct inode *inode = data->inode;
1795 struct fuse_conn *fc = get_fuse_conn(inode);
1796 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001797 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001798 int err;
1799
1800 if (!data->ff) {
1801 err = -EIO;
1802 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1803 if (!data->ff)
1804 goto out_unlock;
1805 }
1806
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001807 /*
1808 * Being under writeback is unlikely but possible. For example direct
1809 * read to an mmaped fuse file will set the page dirty twice; once when
1810 * the pages are faulted with get_user_pages(), and then after the read
1811 * completed.
1812 */
1813 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001814
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001815 if (req && req->num_pages &&
1816 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001817 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001818 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1819 fuse_writepages_send(data);
1820 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001821 }
1822 err = -ENOMEM;
1823 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1824 if (!tmp_page)
1825 goto out_unlock;
1826
1827 /*
1828 * The page must not be redirtied until the writeout is completed
1829 * (i.e. userspace has sent a reply to the write request). Otherwise
1830 * there could be more than one temporary page instance for each real
1831 * page.
1832 *
1833 * This is ensured by holding the page lock in page_mkwrite() while
1834 * checking fuse_page_is_writeback(). We already hold the page lock
1835 * since clear_page_dirty_for_io() and keep it held until we add the
1836 * request to the fi->writepages list and increment req->num_pages.
1837 * After this fuse_page_is_writeback() will indicate that the page is
1838 * under writeback, so we can release the page lock.
1839 */
1840 if (data->req == NULL) {
1841 struct fuse_inode *fi = get_fuse_inode(inode);
1842
1843 err = -ENOMEM;
1844 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1845 if (!req) {
1846 __free_page(tmp_page);
1847 goto out_unlock;
1848 }
1849
1850 fuse_write_fill(req, data->ff, page_offset(page), 0);
1851 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001852 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001853 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001854 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001855 req->num_pages = 0;
1856 req->end = fuse_writepage_end;
1857 req->inode = inode;
1858
1859 spin_lock(&fc->lock);
1860 list_add(&req->writepages_entry, &fi->writepages);
1861 spin_unlock(&fc->lock);
1862
1863 data->req = req;
1864 }
1865 set_page_writeback(page);
1866
1867 copy_highpage(tmp_page, page);
1868 req->pages[req->num_pages] = tmp_page;
1869 req->page_descs[req->num_pages].offset = 0;
1870 req->page_descs[req->num_pages].length = PAGE_SIZE;
1871
Tejun Heo93f78d82015-05-22 17:13:27 -04001872 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001873 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001874
1875 err = 0;
1876 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1877 end_page_writeback(page);
1878 data->req = NULL;
1879 goto out_unlock;
1880 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001881 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001882
1883 /*
1884 * Protected by fc->lock against concurrent access by
1885 * fuse_page_is_writeback().
1886 */
1887 spin_lock(&fc->lock);
1888 req->num_pages++;
1889 spin_unlock(&fc->lock);
1890
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001891out_unlock:
1892 unlock_page(page);
1893
1894 return err;
1895}
1896
1897static int fuse_writepages(struct address_space *mapping,
1898 struct writeback_control *wbc)
1899{
1900 struct inode *inode = mapping->host;
1901 struct fuse_fill_wb_data data;
1902 int err;
1903
1904 err = -EIO;
1905 if (is_bad_inode(inode))
1906 goto out;
1907
1908 data.inode = inode;
1909 data.req = NULL;
1910 data.ff = NULL;
1911
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001912 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001913 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1914 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001915 GFP_NOFS);
1916 if (!data.orig_pages)
1917 goto out;
1918
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001919 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1920 if (data.req) {
1921 /* Ignore errors if we can write at least one page */
1922 BUG_ON(!data.req->num_pages);
1923 fuse_writepages_send(&data);
1924 err = 0;
1925 }
1926 if (data.ff)
1927 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001928
1929 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001930out:
1931 return err;
1932}
1933
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001934/*
1935 * It's worthy to make sure that space is reserved on disk for the write,
1936 * but how to implement it without killing performance need more thinking.
1937 */
1938static int fuse_write_begin(struct file *file, struct address_space *mapping,
1939 loff_t pos, unsigned len, unsigned flags,
1940 struct page **pagep, void **fsdata)
1941{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001942 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001943 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001944 struct page *page;
1945 loff_t fsize;
1946 int err = -ENOMEM;
1947
1948 WARN_ON(!fc->writeback_cache);
1949
1950 page = grab_cache_page_write_begin(mapping, index, flags);
1951 if (!page)
1952 goto error;
1953
1954 fuse_wait_on_page_writeback(mapping->host, page->index);
1955
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001956 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001957 goto success;
1958 /*
1959 * Check if the start this page comes after the end of file, in which
1960 * case the readpage can be optimized away.
1961 */
1962 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001963 if (fsize <= (pos & PAGE_MASK)) {
1964 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001965 if (off)
1966 zero_user_segment(page, 0, off);
1967 goto success;
1968 }
1969 err = fuse_do_readpage(file, page);
1970 if (err)
1971 goto cleanup;
1972success:
1973 *pagep = page;
1974 return 0;
1975
1976cleanup:
1977 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001978 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001979error:
1980 return err;
1981}
1982
1983static int fuse_write_end(struct file *file, struct address_space *mapping,
1984 loff_t pos, unsigned len, unsigned copied,
1985 struct page *page, void *fsdata)
1986{
1987 struct inode *inode = page->mapping->host;
1988
Miklos Szeredi59c3b762016-08-18 09:10:44 +02001989 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1990 if (!copied)
1991 goto unlock;
1992
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001993 if (!PageUptodate(page)) {
1994 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001996 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001997 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001998 SetPageUptodate(page);
1999 }
2000
2001 fuse_write_update_size(inode, pos + copied);
2002 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002003
2004unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002005 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002006 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002007
2008 return copied;
2009}
2010
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002011static int fuse_launder_page(struct page *page)
2012{
2013 int err = 0;
2014 if (clear_page_dirty_for_io(page)) {
2015 struct inode *inode = page->mapping->host;
2016 err = fuse_writepage_locked(page);
2017 if (!err)
2018 fuse_wait_on_page_writeback(inode, page->index);
2019 }
2020 return err;
2021}
2022
2023/*
2024 * Write back dirty pages now, because there may not be any suitable
2025 * open files later
2026 */
2027static void fuse_vma_close(struct vm_area_struct *vma)
2028{
2029 filemap_write_and_wait(vma->vm_file->f_mapping);
2030}
2031
2032/*
2033 * Wait for writeback against this page to complete before allowing it
2034 * to be marked dirty again, and hence written back again, possibly
2035 * before the previous writepage completed.
2036 *
2037 * Block here, instead of in ->writepage(), so that the userspace fs
2038 * can only block processes actually operating on the filesystem.
2039 *
2040 * Otherwise unprivileged userspace fs would be able to block
2041 * unrelated:
2042 *
2043 * - page migration
2044 * - sync(2)
2045 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2046 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002047static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002048{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002049 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002050 struct inode *inode = file_inode(vma->vm_file);
2051
2052 file_update_time(vma->vm_file);
2053 lock_page(page);
2054 if (page->mapping != inode->i_mapping) {
2055 unlock_page(page);
2056 return VM_FAULT_NOPAGE;
2057 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002058
2059 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002060 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002061}
2062
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002063static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002064 .close = fuse_vma_close,
2065 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002066 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002067 .page_mkwrite = fuse_page_mkwrite,
2068};
2069
2070static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2071{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002072 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2073 fuse_link_write_file(file);
2074
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002075 file_accessed(file);
2076 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002077 return 0;
2078}
2079
Miklos Szeredifc280c92009-04-02 14:25:35 +02002080static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2081{
2082 /* Can't provide the coherency needed for MAP_SHARED */
2083 if (vma->vm_flags & VM_MAYSHARE)
2084 return -ENODEV;
2085
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002086 invalidate_inode_pages2(file->f_mapping);
2087
Miklos Szeredifc280c92009-04-02 14:25:35 +02002088 return generic_file_mmap(file, vma);
2089}
2090
Miklos Szeredi71421252006-06-25 05:48:52 -07002091static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2092 struct file_lock *fl)
2093{
2094 switch (ffl->type) {
2095 case F_UNLCK:
2096 break;
2097
2098 case F_RDLCK:
2099 case F_WRLCK:
2100 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2101 ffl->end < ffl->start)
2102 return -EIO;
2103
2104 fl->fl_start = ffl->start;
2105 fl->fl_end = ffl->end;
2106 fl->fl_pid = ffl->pid;
2107 break;
2108
2109 default:
2110 return -EIO;
2111 }
2112 fl->fl_type = ffl->type;
2113 return 0;
2114}
2115
Miklos Szeredi70781872014-12-12 09:49:05 +01002116static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002117 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002118 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002119{
Al Viro6131ffa2013-02-27 16:59:05 -05002120 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002121 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002122 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002123
Miklos Szeredi70781872014-12-12 09:49:05 +01002124 memset(inarg, 0, sizeof(*inarg));
2125 inarg->fh = ff->fh;
2126 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2127 inarg->lk.start = fl->fl_start;
2128 inarg->lk.end = fl->fl_end;
2129 inarg->lk.type = fl->fl_type;
2130 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002131 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002132 inarg->lk_flags |= FUSE_LK_FLOCK;
2133 args->in.h.opcode = opcode;
2134 args->in.h.nodeid = get_node_id(inode);
2135 args->in.numargs = 1;
2136 args->in.args[0].size = sizeof(*inarg);
2137 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002138}
2139
2140static int fuse_getlk(struct file *file, struct file_lock *fl)
2141{
Al Viro6131ffa2013-02-27 16:59:05 -05002142 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002143 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002144 FUSE_ARGS(args);
2145 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002146 struct fuse_lk_out outarg;
2147 int err;
2148
Miklos Szeredi70781872014-12-12 09:49:05 +01002149 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2150 args.out.numargs = 1;
2151 args.out.args[0].size = sizeof(outarg);
2152 args.out.args[0].value = &outarg;
2153 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002154 if (!err)
2155 err = convert_fuse_file_lock(&outarg.lk, fl);
2156
2157 return err;
2158}
2159
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002160static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002161{
Al Viro6131ffa2013-02-27 16:59:05 -05002162 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002163 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002164 FUSE_ARGS(args);
2165 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002166 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2167 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2168 int err;
2169
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002170 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002171 /* NLM needs asynchronous locks, which we don't support yet */
2172 return -ENOLCK;
2173 }
2174
Miklos Szeredi71421252006-06-25 05:48:52 -07002175 /* Unlock on close is handled by the flush method */
2176 if (fl->fl_flags & FL_CLOSE)
2177 return 0;
2178
Miklos Szeredi70781872014-12-12 09:49:05 +01002179 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2180 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002181
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002182 /* locking is restartable */
2183 if (err == -EINTR)
2184 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002185
Miklos Szeredi71421252006-06-25 05:48:52 -07002186 return err;
2187}
2188
2189static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2190{
Al Viro6131ffa2013-02-27 16:59:05 -05002191 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002192 struct fuse_conn *fc = get_fuse_conn(inode);
2193 int err;
2194
Miklos Szeredi48e90762008-07-25 01:49:02 -07002195 if (cmd == F_CANCELLK) {
2196 err = 0;
2197 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002198 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002199 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 err = 0;
2201 } else
2202 err = fuse_getlk(file, fl);
2203 } else {
2204 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002205 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002206 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002207 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002208 }
2209 return err;
2210}
2211
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002212static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2213{
Al Viro6131ffa2013-02-27 16:59:05 -05002214 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002215 struct fuse_conn *fc = get_fuse_conn(inode);
2216 int err;
2217
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002218 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002219 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002220 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002221 struct fuse_file *ff = file->private_data;
2222
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002223 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002224 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002225 err = fuse_setlk(file, fl, 1);
2226 }
2227
2228 return err;
2229}
2230
Miklos Szeredib2d22722006-12-06 20:35:51 -08002231static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2232{
2233 struct inode *inode = mapping->host;
2234 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002235 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002236 struct fuse_bmap_in inarg;
2237 struct fuse_bmap_out outarg;
2238 int err;
2239
2240 if (!inode->i_sb->s_bdev || fc->no_bmap)
2241 return 0;
2242
Miklos Szeredib2d22722006-12-06 20:35:51 -08002243 memset(&inarg, 0, sizeof(inarg));
2244 inarg.block = block;
2245 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002246 args.in.h.opcode = FUSE_BMAP;
2247 args.in.h.nodeid = get_node_id(inode);
2248 args.in.numargs = 1;
2249 args.in.args[0].size = sizeof(inarg);
2250 args.in.args[0].value = &inarg;
2251 args.out.numargs = 1;
2252 args.out.args[0].size = sizeof(outarg);
2253 args.out.args[0].value = &outarg;
2254 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002255 if (err == -ENOSYS)
2256 fc->no_bmap = 1;
2257
2258 return err ? 0 : outarg.block;
2259}
2260
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302261static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2262{
2263 struct inode *inode = file->f_mapping->host;
2264 struct fuse_conn *fc = get_fuse_conn(inode);
2265 struct fuse_file *ff = file->private_data;
2266 FUSE_ARGS(args);
2267 struct fuse_lseek_in inarg = {
2268 .fh = ff->fh,
2269 .offset = offset,
2270 .whence = whence
2271 };
2272 struct fuse_lseek_out outarg;
2273 int err;
2274
2275 if (fc->no_lseek)
2276 goto fallback;
2277
2278 args.in.h.opcode = FUSE_LSEEK;
2279 args.in.h.nodeid = ff->nodeid;
2280 args.in.numargs = 1;
2281 args.in.args[0].size = sizeof(inarg);
2282 args.in.args[0].value = &inarg;
2283 args.out.numargs = 1;
2284 args.out.args[0].size = sizeof(outarg);
2285 args.out.args[0].value = &outarg;
2286 err = fuse_simple_request(fc, &args);
2287 if (err) {
2288 if (err == -ENOSYS) {
2289 fc->no_lseek = 1;
2290 goto fallback;
2291 }
2292 return err;
2293 }
2294
2295 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2296
2297fallback:
2298 err = fuse_update_attributes(inode, NULL, file, NULL);
2299 if (!err)
2300 return generic_file_llseek(file, offset, whence);
2301 else
2302 return err;
2303}
2304
Andrew Morton965c8e52012-12-17 15:59:39 -08002305static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002306{
2307 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002308 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002309
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302310 switch (whence) {
2311 case SEEK_SET:
2312 case SEEK_CUR:
2313 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002314 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302315 break;
2316 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002317 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302318 retval = fuse_update_attributes(inode, NULL, file, NULL);
2319 if (!retval)
2320 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002321 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302322 break;
2323 case SEEK_HOLE:
2324 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002325 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302326 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002327 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302328 break;
2329 default:
2330 retval = -EINVAL;
2331 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002332
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002333 return retval;
2334}
2335
Tejun Heo59efec72008-11-26 12:03:55 +01002336/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002337 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2338 * ABI was defined to be 'struct iovec' which is different on 32bit
2339 * and 64bit. Fortunately we can determine which structure the server
2340 * used from the size of the reply.
2341 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002342static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2343 size_t transferred, unsigned count,
2344 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002345{
2346#ifdef CONFIG_COMPAT
2347 if (count * sizeof(struct compat_iovec) == transferred) {
2348 struct compat_iovec *ciov = src;
2349 unsigned i;
2350
2351 /*
2352 * With this interface a 32bit server cannot support
2353 * non-compat (i.e. ones coming from 64bit apps) ioctl
2354 * requests
2355 */
2356 if (!is_compat)
2357 return -EINVAL;
2358
2359 for (i = 0; i < count; i++) {
2360 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2361 dst[i].iov_len = ciov[i].iov_len;
2362 }
2363 return 0;
2364 }
2365#endif
2366
2367 if (count * sizeof(struct iovec) != transferred)
2368 return -EIO;
2369
2370 memcpy(dst, src, transferred);
2371 return 0;
2372}
2373
Miklos Szeredi75727772010-11-30 16:39:27 +01002374/* Make sure iov_length() won't overflow */
2375static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2376{
2377 size_t n;
2378 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2379
Zach Brownfb6ccff2012-07-24 12:10:11 -07002380 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002381 if (iov->iov_len > (size_t) max)
2382 return -ENOMEM;
2383 max -= iov->iov_len;
2384 }
2385 return 0;
2386}
2387
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002388static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2389 void *src, size_t transferred, unsigned count,
2390 bool is_compat)
2391{
2392 unsigned i;
2393 struct fuse_ioctl_iovec *fiov = src;
2394
2395 if (fc->minor < 16) {
2396 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2397 count, is_compat);
2398 }
2399
2400 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2401 return -EIO;
2402
2403 for (i = 0; i < count; i++) {
2404 /* Did the server supply an inappropriate value? */
2405 if (fiov[i].base != (unsigned long) fiov[i].base ||
2406 fiov[i].len != (unsigned long) fiov[i].len)
2407 return -EIO;
2408
2409 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2410 dst[i].iov_len = (size_t) fiov[i].len;
2411
2412#ifdef CONFIG_COMPAT
2413 if (is_compat &&
2414 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2415 (compat_size_t) dst[i].iov_len != fiov[i].len))
2416 return -EIO;
2417#endif
2418 }
2419
2420 return 0;
2421}
2422
2423
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002424/*
Tejun Heo59efec72008-11-26 12:03:55 +01002425 * For ioctls, there is no generic way to determine how much memory
2426 * needs to be read and/or written. Furthermore, ioctls are allowed
2427 * to dereference the passed pointer, so the parameter requires deep
2428 * copying but FUSE has no idea whatsoever about what to copy in or
2429 * out.
2430 *
2431 * This is solved by allowing FUSE server to retry ioctl with
2432 * necessary in/out iovecs. Let's assume the ioctl implementation
2433 * needs to read in the following structure.
2434 *
2435 * struct a {
2436 * char *buf;
2437 * size_t buflen;
2438 * }
2439 *
2440 * On the first callout to FUSE server, inarg->in_size and
2441 * inarg->out_size will be NULL; then, the server completes the ioctl
2442 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2443 * the actual iov array to
2444 *
2445 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2446 *
2447 * which tells FUSE to copy in the requested area and retry the ioctl.
2448 * On the second round, the server has access to the structure and
2449 * from that it can tell what to look for next, so on the invocation,
2450 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2451 *
2452 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2453 * { .iov_base = a.buf, .iov_len = a.buflen } }
2454 *
2455 * FUSE will copy both struct a and the pointed buffer from the
2456 * process doing the ioctl and retry ioctl with both struct a and the
2457 * buffer.
2458 *
2459 * This time, FUSE server has everything it needs and completes ioctl
2460 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2461 *
2462 * Copying data out works the same way.
2463 *
2464 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2465 * automatically initializes in and out iovs by decoding @cmd with
2466 * _IOC_* macros and the server is not allowed to request RETRY. This
2467 * limits ioctl data transfers to well-formed ioctls and is the forced
2468 * behavior for all FUSE servers.
2469 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002470long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2471 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002472{
Tejun Heo59efec72008-11-26 12:03:55 +01002473 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002474 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002475 struct fuse_ioctl_in inarg = {
2476 .fh = ff->fh,
2477 .cmd = cmd,
2478 .arg = arg,
2479 .flags = flags
2480 };
2481 struct fuse_ioctl_out outarg;
2482 struct fuse_req *req = NULL;
2483 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002484 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002485 struct iovec *in_iov = NULL, *out_iov = NULL;
2486 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002487 size_t in_size, out_size, transferred, c;
2488 int err, i;
2489 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002490
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002491#if BITS_PER_LONG == 32
2492 inarg.flags |= FUSE_IOCTL_32BIT;
2493#else
2494 if (flags & FUSE_IOCTL_COMPAT)
2495 inarg.flags |= FUSE_IOCTL_32BIT;
2496#endif
2497
Tejun Heo59efec72008-11-26 12:03:55 +01002498 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002499 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002500
Tejun Heo59efec72008-11-26 12:03:55 +01002501 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002502 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002503 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002504 if (!pages || !iov_page)
2505 goto out;
2506
2507 /*
2508 * If restricted, initialize IO parameters as encoded in @cmd.
2509 * RETRY from server is not allowed.
2510 */
2511 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002512 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002513
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002514 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002515 iov->iov_len = _IOC_SIZE(cmd);
2516
2517 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2518 in_iov = iov;
2519 in_iovs = 1;
2520 }
2521
2522 if (_IOC_DIR(cmd) & _IOC_READ) {
2523 out_iov = iov;
2524 out_iovs = 1;
2525 }
2526 }
2527
2528 retry:
2529 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2530 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2531
2532 /*
2533 * Out data can be used either for actual out data or iovs,
2534 * make sure there always is at least one page.
2535 */
2536 out_size = max_t(size_t, out_size, PAGE_SIZE);
2537 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2538
2539 /* make sure there are enough buffer pages and init request with them */
2540 err = -ENOMEM;
2541 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2542 goto out;
2543 while (num_pages < max_pages) {
2544 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2545 if (!pages[num_pages])
2546 goto out;
2547 num_pages++;
2548 }
2549
Maxim Patlasov54b96672012-10-26 19:49:13 +04002550 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002551 if (IS_ERR(req)) {
2552 err = PTR_ERR(req);
2553 req = NULL;
2554 goto out;
2555 }
2556 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2557 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002558 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002559
2560 /* okay, let's send it to the client */
2561 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002562 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002563 req->in.numargs = 1;
2564 req->in.args[0].size = sizeof(inarg);
2565 req->in.args[0].value = &inarg;
2566 if (in_size) {
2567 req->in.numargs++;
2568 req->in.args[1].size = in_size;
2569 req->in.argpages = 1;
2570
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002571 err = -EFAULT;
2572 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2573 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2574 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2575 if (c != PAGE_SIZE && iov_iter_count(&ii))
2576 goto out;
2577 }
Tejun Heo59efec72008-11-26 12:03:55 +01002578 }
2579
2580 req->out.numargs = 2;
2581 req->out.args[0].size = sizeof(outarg);
2582 req->out.args[0].value = &outarg;
2583 req->out.args[1].size = out_size;
2584 req->out.argpages = 1;
2585 req->out.argvar = 1;
2586
Tejun Heob93f8582008-11-26 12:03:55 +01002587 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002588 err = req->out.h.error;
2589 transferred = req->out.args[1].size;
2590 fuse_put_request(fc, req);
2591 req = NULL;
2592 if (err)
2593 goto out;
2594
2595 /* did it ask for retry? */
2596 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002597 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002598
2599 /* no retry if in restricted mode */
2600 err = -EIO;
2601 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2602 goto out;
2603
2604 in_iovs = outarg.in_iovs;
2605 out_iovs = outarg.out_iovs;
2606
2607 /*
2608 * Make sure things are in boundary, separate checks
2609 * are to protect against overflow.
2610 */
2611 err = -ENOMEM;
2612 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2613 out_iovs > FUSE_IOCTL_MAX_IOV ||
2614 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2615 goto out;
2616
Cong Wang2408f6e2011-11-25 23:14:30 +08002617 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002618 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002619 transferred, in_iovs + out_iovs,
2620 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002621 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002622 if (err)
2623 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002624
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002625 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002626 out_iov = in_iov + in_iovs;
2627
Miklos Szeredi75727772010-11-30 16:39:27 +01002628 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2629 if (err)
2630 goto out;
2631
2632 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2633 if (err)
2634 goto out;
2635
Tejun Heo59efec72008-11-26 12:03:55 +01002636 goto retry;
2637 }
2638
2639 err = -EIO;
2640 if (transferred > inarg.out_size)
2641 goto out;
2642
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002643 err = -EFAULT;
2644 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2645 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2646 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2647 if (c != PAGE_SIZE && iov_iter_count(&ii))
2648 goto out;
2649 }
2650 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002651 out:
2652 if (req)
2653 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002654 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002655 while (num_pages)
2656 __free_page(pages[--num_pages]);
2657 kfree(pages);
2658
2659 return err ? err : outarg.result;
2660}
Tejun Heo08cbf542009-04-14 10:54:53 +09002661EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002662
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002663long fuse_ioctl_common(struct file *file, unsigned int cmd,
2664 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002665{
Al Viro6131ffa2013-02-27 16:59:05 -05002666 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002667 struct fuse_conn *fc = get_fuse_conn(inode);
2668
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002669 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002670 return -EACCES;
2671
2672 if (is_bad_inode(inode))
2673 return -EIO;
2674
2675 return fuse_do_ioctl(file, cmd, arg, flags);
2676}
2677
Tejun Heo59efec72008-11-26 12:03:55 +01002678static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2679 unsigned long arg)
2680{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002681 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002682}
2683
2684static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2685 unsigned long arg)
2686{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002687 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002688}
2689
Tejun Heo95668a62008-11-26 12:03:55 +01002690/*
2691 * All files which have been polled are linked to RB tree
2692 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2693 * find the matching one.
2694 */
2695static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2696 struct rb_node **parent_out)
2697{
2698 struct rb_node **link = &fc->polled_files.rb_node;
2699 struct rb_node *last = NULL;
2700
2701 while (*link) {
2702 struct fuse_file *ff;
2703
2704 last = *link;
2705 ff = rb_entry(last, struct fuse_file, polled_node);
2706
2707 if (kh < ff->kh)
2708 link = &last->rb_left;
2709 else if (kh > ff->kh)
2710 link = &last->rb_right;
2711 else
2712 return link;
2713 }
2714
2715 if (parent_out)
2716 *parent_out = last;
2717 return link;
2718}
2719
2720/*
2721 * The file is about to be polled. Make sure it's on the polled_files
2722 * RB tree. Note that files once added to the polled_files tree are
2723 * not removed before the file is released. This is because a file
2724 * polled once is likely to be polled again.
2725 */
2726static void fuse_register_polled_file(struct fuse_conn *fc,
2727 struct fuse_file *ff)
2728{
2729 spin_lock(&fc->lock);
2730 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002731 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002732
2733 link = fuse_find_polled_node(fc, ff->kh, &parent);
2734 BUG_ON(*link);
2735 rb_link_node(&ff->polled_node, parent, link);
2736 rb_insert_color(&ff->polled_node, &fc->polled_files);
2737 }
2738 spin_unlock(&fc->lock);
2739}
2740
Tejun Heo08cbf542009-04-14 10:54:53 +09002741unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002742{
Tejun Heo95668a62008-11-26 12:03:55 +01002743 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002744 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002745 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2746 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002747 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002748 int err;
2749
2750 if (fc->no_poll)
2751 return DEFAULT_POLLMASK;
2752
2753 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002754 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002755
2756 /*
2757 * Ask for notification iff there's someone waiting for it.
2758 * The client may ignore the flag and always notify.
2759 */
2760 if (waitqueue_active(&ff->poll_wait)) {
2761 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2762 fuse_register_polled_file(fc, ff);
2763 }
2764
Miklos Szeredi70781872014-12-12 09:49:05 +01002765 args.in.h.opcode = FUSE_POLL;
2766 args.in.h.nodeid = ff->nodeid;
2767 args.in.numargs = 1;
2768 args.in.args[0].size = sizeof(inarg);
2769 args.in.args[0].value = &inarg;
2770 args.out.numargs = 1;
2771 args.out.args[0].size = sizeof(outarg);
2772 args.out.args[0].value = &outarg;
2773 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002774
2775 if (!err)
2776 return outarg.revents;
2777 if (err == -ENOSYS) {
2778 fc->no_poll = 1;
2779 return DEFAULT_POLLMASK;
2780 }
2781 return POLLERR;
2782}
Tejun Heo08cbf542009-04-14 10:54:53 +09002783EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002784
2785/*
2786 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2787 * wakes up the poll waiters.
2788 */
2789int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2790 struct fuse_notify_poll_wakeup_out *outarg)
2791{
2792 u64 kh = outarg->kh;
2793 struct rb_node **link;
2794
2795 spin_lock(&fc->lock);
2796
2797 link = fuse_find_polled_node(fc, kh, NULL);
2798 if (*link) {
2799 struct fuse_file *ff;
2800
2801 ff = rb_entry(*link, struct fuse_file, polled_node);
2802 wake_up_interruptible_sync(&ff->poll_wait);
2803 }
2804
2805 spin_unlock(&fc->lock);
2806 return 0;
2807}
2808
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002809static void fuse_do_truncate(struct file *file)
2810{
2811 struct inode *inode = file->f_mapping->host;
2812 struct iattr attr;
2813
2814 attr.ia_valid = ATTR_SIZE;
2815 attr.ia_size = i_size_read(inode);
2816
2817 attr.ia_file = file;
2818 attr.ia_valid |= ATTR_FILE;
2819
Jan Kara62490332016-05-26 17:12:41 +02002820 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002821}
2822
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002823static inline loff_t fuse_round_up(loff_t off)
2824{
2825 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2826}
2827
Anand Avati4273b792012-02-17 12:46:25 -05002828static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002829fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002830{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002831 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002832 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002833 struct file *file = iocb->ki_filp;
2834 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002835 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002836 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002837 struct inode *inode;
2838 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002839 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002840 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002841 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002842
Anand Avati4273b792012-02-17 12:46:25 -05002843 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002844 inode = file->f_mapping->host;
2845 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002846
Omar Sandoval6f673762015-03-16 04:33:52 -07002847 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002848 return 0;
2849
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002850 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002851 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002852 if (offset >= i_size)
2853 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002854 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2855 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002856 }
2857
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002858 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002859 if (!io)
2860 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002861 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002862 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002863 io->reqs = 1;
2864 io->bytes = -1;
2865 io->size = 0;
2866 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002867 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002868 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002869 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002870 /*
2871 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002872 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002873 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002874 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002875 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302876 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002877
2878 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302879 * We cannot asynchronously extend the size of a file.
2880 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002881 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302882 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2883 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002884
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302885 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002886 /*
2887 * Additional reference to keep io around after
2888 * calling fuse_aio_complete()
2889 */
2890 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002891 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002892 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002893
Omar Sandoval6f673762015-03-16 04:33:52 -07002894 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002895 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002896 fuse_invalidate_attr(inode);
2897 } else {
Al Virod22a9432014-03-16 15:50:47 -04002898 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002899 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002900
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002901 if (io->async) {
2902 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2903
2904 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302905 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 return -EIOCBQUEUED;
2907
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002908 wait_for_completion(&wait);
2909 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002910 }
2911
Seth Forshee744742d2016-03-11 10:35:34 -06002912 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002913
Omar Sandoval6f673762015-03-16 04:33:52 -07002914 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002915 if (ret > 0)
2916 fuse_write_update_size(inode, pos);
2917 else if (ret < 0 && offset + count > i_size)
2918 fuse_do_truncate(file);
2919 }
Anand Avati4273b792012-02-17 12:46:25 -05002920
2921 return ret;
2922}
2923
Miklos Szeredicdadb112012-11-10 16:55:56 +01002924static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2925 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002926{
2927 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002928 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002929 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002930 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002931 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002932 struct fuse_fallocate_in inarg = {
2933 .fh = ff->fh,
2934 .offset = offset,
2935 .length = length,
2936 .mode = mode
2937 };
2938 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002939 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2940 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002941
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002942 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2943 return -EOPNOTSUPP;
2944
Miklos Szeredi519c6042012-04-26 10:56:36 +02002945 if (fc->no_fallocate)
2946 return -EOPNOTSUPP;
2947
Maxim Patlasov14c14412013-06-13 12:16:39 +04002948 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002949 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002950 if (mode & FALLOC_FL_PUNCH_HOLE) {
2951 loff_t endbyte = offset + length - 1;
2952 err = filemap_write_and_wait_range(inode->i_mapping,
2953 offset, endbyte);
2954 if (err)
2955 goto out;
2956
2957 fuse_sync_writes(inode);
2958 }
Brian Foster3634a632013-05-17 09:30:32 -04002959 }
2960
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002961 if (!(mode & FALLOC_FL_KEEP_SIZE))
2962 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2963
Miklos Szeredi70781872014-12-12 09:49:05 +01002964 args.in.h.opcode = FUSE_FALLOCATE;
2965 args.in.h.nodeid = ff->nodeid;
2966 args.in.numargs = 1;
2967 args.in.args[0].size = sizeof(inarg);
2968 args.in.args[0].value = &inarg;
2969 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002970 if (err == -ENOSYS) {
2971 fc->no_fallocate = 1;
2972 err = -EOPNOTSUPP;
2973 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002974 if (err)
2975 goto out;
2976
2977 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002978 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2979 bool changed = fuse_write_update_size(inode, offset + length);
2980
Miklos Szeredi93d22692014-04-28 14:19:22 +02002981 if (changed && fc->writeback_cache)
2982 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002983 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002984
2985 if (mode & FALLOC_FL_PUNCH_HOLE)
2986 truncate_pagecache_range(inode, offset, offset + length - 1);
2987
2988 fuse_invalidate_attr(inode);
2989
Brian Foster3634a632013-05-17 09:30:32 -04002990out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002991 if (!(mode & FALLOC_FL_KEEP_SIZE))
2992 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2993
Maxim Patlasovbde52782013-09-13 19:19:54 +04002994 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05002995 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04002996
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002997 return err;
2998}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002999
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003000static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003001 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003002 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003003 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003004 .mmap = fuse_file_mmap,
3005 .open = fuse_open,
3006 .flush = fuse_flush,
3007 .release = fuse_release,
3008 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003009 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003010 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003011 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003012 .unlocked_ioctl = fuse_file_ioctl,
3013 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003014 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003015 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003016};
3017
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003018static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003019 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003020 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003021 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003022 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003023 .open = fuse_open,
3024 .flush = fuse_flush,
3025 .release = fuse_release,
3026 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003027 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003028 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003029 .unlocked_ioctl = fuse_file_ioctl,
3030 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003031 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003032 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003033 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003034};
3035
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003036static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003037 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003038 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003039 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003040 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003041 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003042 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003043 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003044 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003045 .write_begin = fuse_write_begin,
3046 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003047};
3048
3049void fuse_init_file_inode(struct inode *inode)
3050{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003051 inode->i_fop = &fuse_file_operations;
3052 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003053}