blob: 4408abf6675ba5d3eb7e9988b4796025f432f877 [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
842static int fuse_readpages_fill(void *_data, struct page *page)
843{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700844 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 struct fuse_req *req = data->req;
846 struct inode *inode = data->inode;
847 struct fuse_conn *fc = get_fuse_conn(inode);
848
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700849 fuse_wait_on_page_writeback(inode, page->index);
850
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 if (req->num_pages &&
852 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300853 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400855 int nr_alloc = min_t(unsigned, data->nr_pages,
856 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200857 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400858 if (fc->async_read)
859 req = fuse_get_req_for_background(fc, nr_alloc);
860 else
861 req = fuse_get_req(fc, nr_alloc);
862
863 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700864 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700865 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400869
870 if (WARN_ON(req->num_pages >= req->max_pages)) {
Kirill Tkhai01f07722018-07-19 15:49:39 +0300871 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400872 fuse_put_request(fc, req);
873 return -EIO;
874 }
875
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700877 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400878 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100879 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400880 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700881 return 0;
882}
883
884static int fuse_readpages(struct file *file, struct address_space *mapping,
885 struct list_head *pages, unsigned nr_pages)
886{
887 struct inode *inode = mapping->host;
888 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700889 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400891 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800892
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700893 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800894 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800895 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800896
Miklos Szeredia6643092007-11-28 16:22:00 -0800897 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700898 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400899 if (fc->async_read)
900 data.req = fuse_get_req_for_background(fc, nr_alloc);
901 else
902 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400903 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700904 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700905 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800906 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907
908 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700909 if (!err) {
910 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200911 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700912 else
913 fuse_put_request(fc, data.req);
914 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800915out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700916 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700917}
918
Al Viro37c20f12014-04-02 14:47:09 -0400919static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800920{
921 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400922 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800923
Brian Fostera8894272012-07-16 15:23:50 -0400924 /*
925 * In auto invalidate mode, always update attributes on read.
926 * Otherwise, only update if we attempt to read past EOF (to ensure
927 * i_size is up to date).
928 */
929 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400930 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
933 if (err)
934 return err;
935 }
936
Al Viro37c20f12014-04-02 14:47:09 -0400937 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800938}
939
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200940static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200941 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700942{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700943 struct fuse_write_in *inarg = &req->misc.write.in;
944 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700945
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700946 inarg->fh = ff->fh;
947 inarg->offset = pos;
948 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700949 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200952 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700953 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
954 else
955 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700956 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700957 req->in.args[1].size = count;
958 req->out.numargs = 1;
959 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700960 req->out.args[0].value = outarg;
961}
962
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400963static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200964 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700965{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400966 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200967 struct fuse_file *ff = file->private_data;
968 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200969 struct fuse_write_in *inarg = &req->misc.write.in;
970
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200971 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200972 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700973 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700974 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
975 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
976 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400977
978 if (io->async)
979 return fuse_async_req_send(fc, req, count, io);
980
Tejun Heob93f8582008-11-26 12:03:55 +0100981 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700982 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700983}
984
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400985bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700986{
987 struct fuse_conn *fc = get_fuse_conn(inode);
988 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400989 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700990
991 spin_lock(&fc->lock);
992 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400993 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700994 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400995 ret = true;
996 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700997 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400998
999 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000}
1001
Nick Pigginea9b9902008-04-30 00:54:42 -07001002static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1003 struct inode *inode, loff_t pos,
1004 size_t count)
1005{
1006 size_t res;
1007 unsigned offset;
1008 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001009 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001010
1011 for (i = 0; i < req->num_pages; i++)
1012 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1013
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001014 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001015
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001016 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001017 count = res;
1018 for (i = 0; i < req->num_pages; i++) {
1019 struct page *page = req->pages[i];
1020
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001021 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001022 SetPageUptodate(page);
1023
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001024 if (count > PAGE_SIZE - offset)
1025 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001026 else
1027 count = 0;
1028 offset = 0;
1029
1030 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001031 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001032 }
1033
1034 return res;
1035}
1036
1037static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1038 struct address_space *mapping,
1039 struct iov_iter *ii, loff_t pos)
1040{
1041 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001042 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001043 size_t count = 0;
1044 int err;
1045
Miklos Szeredif4975c62009-04-02 14:25:34 +02001046 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001047 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001048
1049 do {
1050 size_t tmp;
1051 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001052 pgoff_t index = pos >> PAGE_SHIFT;
1053 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001054 iov_iter_count(ii));
1055
1056 bytes = min_t(size_t, bytes, fc->max_write - count);
1057
1058 again:
1059 err = -EFAULT;
1060 if (iov_iter_fault_in_readable(ii, bytes))
1061 break;
1062
1063 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001064 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001065 if (!page)
1066 break;
1067
anfei zhou931e80e2010-02-02 13:44:02 -08001068 if (mapping_writably_mapped(mapping))
1069 flush_dcache_page(page);
1070
Nick Pigginea9b9902008-04-30 00:54:42 -07001071 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001072 flush_dcache_page(page);
1073
Roman Gushchin3ca81382015-10-12 16:33:44 +03001074 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001075 if (!tmp) {
1076 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001077 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001078 bytes = min(bytes, iov_iter_single_seg_count(ii));
1079 goto again;
1080 }
1081
1082 err = 0;
1083 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001084 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 req->num_pages++;
1086
Nick Pigginea9b9902008-04-30 00:54:42 -07001087 count += tmp;
1088 pos += tmp;
1089 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001090 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001091 offset = 0;
1092
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001093 if (!fc->big_writes)
1094 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001096 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001097
1098 return count > 0 ? count : err;
1099}
1100
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001101static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1102{
1103 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001104 ((pos + len - 1) >> PAGE_SHIFT) -
1105 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001106 FUSE_MAX_PAGES_PER_REQ);
1107}
1108
Nick Pigginea9b9902008-04-30 00:54:42 -07001109static ssize_t fuse_perform_write(struct file *file,
1110 struct address_space *mapping,
1111 struct iov_iter *ii, loff_t pos)
1112{
1113 struct inode *inode = mapping->host;
1114 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001115 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001116 int err = 0;
1117 ssize_t res = 0;
1118
1119 if (is_bad_inode(inode))
1120 return -EIO;
1121
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001122 if (inode->i_size < pos + iov_iter_count(ii))
1123 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1124
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 do {
1126 struct fuse_req *req;
1127 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001128 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001129
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001130 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001131 if (IS_ERR(req)) {
1132 err = PTR_ERR(req);
1133 break;
1134 }
1135
1136 count = fuse_fill_write_pages(req, mapping, ii, pos);
1137 if (count <= 0) {
1138 err = count;
1139 } else {
1140 size_t num_written;
1141
1142 num_written = fuse_send_write_pages(req, file, inode,
1143 pos, count);
1144 err = req->out.h.error;
1145 if (!err) {
1146 res += num_written;
1147 pos += num_written;
1148
1149 /* break out of the loop on short write */
1150 if (num_written != count)
1151 err = -EIO;
1152 }
1153 }
1154 fuse_put_request(fc, req);
1155 } while (!err && iov_iter_count(ii));
1156
1157 if (res > 0)
1158 fuse_write_update_size(inode, pos);
1159
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001160 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001161 fuse_invalidate_attr(inode);
1162
1163 return res > 0 ? res : err;
1164}
1165
Al Viro84c3d552014-04-03 14:33:23 -04001166static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001167{
1168 struct file *file = iocb->ki_filp;
1169 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001171 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001172 struct inode *inode = mapping->host;
1173 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001174 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001175
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001176 if (get_fuse_conn(inode)->writeback_cache) {
1177 /* Update size (EOF optimization) and mode (SUID clearing) */
1178 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1179 if (err)
1180 return err;
1181
Al Viro84c3d552014-04-03 14:33:23 -04001182 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001183 }
1184
Al Viro59551022016-01-22 15:40:57 -05001185 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001186
1187 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001188 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001189
Al Viro3309dd02015-04-09 12:55:47 -04001190 err = generic_write_checks(iocb, from);
1191 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001192 goto out;
1193
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001194 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 if (err)
1196 goto out;
1197
Josef Bacikc3b2da32012-03-26 09:59:21 -04001198 err = file_update_time(file);
1199 if (err)
1200 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001201
Al Viro2ba48ce2015-04-09 13:52:01 -04001202 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001203 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001204 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001205 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001206 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001207
Anand Avati4273b792012-02-17 12:46:25 -05001208 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001209
Al Viro84c3d552014-04-03 14:33:23 -04001210 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001211 if (written_buffered < 0) {
1212 err = written_buffered;
1213 goto out;
1214 }
1215 endbyte = pos + written_buffered - 1;
1216
1217 err = filemap_write_and_wait_range(file->f_mapping, pos,
1218 endbyte);
1219 if (err)
1220 goto out;
1221
1222 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001223 pos >> PAGE_SHIFT,
1224 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001225
1226 written += written_buffered;
1227 iocb->ki_pos = pos + written_buffered;
1228 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001229 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001230 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001231 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001232 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001233out:
1234 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001235 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001236
1237 return written ? written : err;
1238}
1239
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001240static inline void fuse_page_descs_length_init(struct fuse_req *req,
1241 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001242{
1243 int i;
1244
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001245 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001246 req->page_descs[i].length = PAGE_SIZE -
1247 req->page_descs[i].offset;
1248}
1249
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001250static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1251{
1252 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1253}
1254
1255static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1256 size_t max_size)
1257{
1258 return min(iov_iter_single_seg_count(ii), max_size);
1259}
1260
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001261static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001262 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001263{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001264 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001265 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001266
Miklos Szeredif4975c62009-04-02 14:25:34 +02001267 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001268 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269 unsigned long user_addr = fuse_get_user_addr(ii);
1270 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1271
Miklos Szeredif4975c62009-04-02 14:25:34 +02001272 if (write)
1273 req->in.args[1].value = (void *) user_addr;
1274 else
1275 req->out.args[0].value = (void *) user_addr;
1276
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001277 iov_iter_advance(ii, frag_size);
1278 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001279 return 0;
1280 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001281
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001282 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001283 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001284 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001285 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001286 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001287 req->max_pages - req->num_pages,
1288 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001289 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001290 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001291
Al Viroc9c37e22014-03-16 16:08:30 -04001292 iov_iter_advance(ii, ret);
1293 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001294
Al Viroc9c37e22014-03-16 16:08:30 -04001295 ret += start;
1296 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1297
1298 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001299 fuse_page_descs_length_init(req, req->num_pages, npages);
1300
1301 req->num_pages += npages;
1302 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001303 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001304 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001305
1306 if (write)
1307 req->in.argpages = 1;
1308 else
1309 req->out.argpages = 1;
1310
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001311 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001312
Ashish Samant2c932d42016-03-25 10:53:41 -07001313 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001314}
1315
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001316static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1317{
Al Virof67da302014-03-19 01:16:16 -04001318 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001319}
1320
Al Virod22a9432014-03-16 15:50:47 -04001321ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1322 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001323{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001324 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001325 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001326 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001327 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001328 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001329 struct fuse_file *ff = file->private_data;
1330 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001331 size_t nmax = write ? fc->max_write : fc->max_read;
1332 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001333 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001334 pgoff_t idx_from = pos >> PAGE_SHIFT;
1335 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001336 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001337 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001338 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001339
Brian Fosterde82b922013-05-14 11:25:39 -04001340 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001341 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001342 else
Al Virod22a9432014-03-16 15:50:47 -04001343 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001344 if (IS_ERR(req))
1345 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001346
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001347 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1348 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001349 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001350 fuse_sync_writes(inode);
1351 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001352 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001353 }
1354
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001355 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001356 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001357 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001358 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001359 err = fuse_get_user_pages(req, iter, &nbytes, write);
1360 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001362
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001364 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001365 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001366 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001367
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001368 if (!io->async)
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001369 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001371 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001372 break;
1373 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001374 res = 0;
1375 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 break;
1377 }
1378 count -= nres;
1379 res += nres;
1380 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001381 if (nres != nbytes)
1382 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001383 if (count) {
1384 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001385 if (io->async)
1386 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001387 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001388 else
Al Virod22a9432014-03-16 15:50:47 -04001389 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001390 if (IS_ERR(req))
1391 break;
1392 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001393 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001394 if (!IS_ERR(req))
1395 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001396 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001397 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398
Ashish Samant742f9922016-03-14 21:57:35 -07001399 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400}
Tejun Heo08cbf542009-04-14 10:54:53 +09001401EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001402
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001403static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001404 struct iov_iter *iter,
1405 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001406{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001407 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001408 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001409 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001410
1411 if (is_bad_inode(inode))
1412 return -EIO;
1413
Al Virod22a9432014-03-16 15:50:47 -04001414 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001415
1416 fuse_invalidate_attr(inode);
1417
1418 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419}
1420
Al Viro15316262015-03-30 22:08:36 -04001421static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001422{
Seth Forshee744742d2016-03-11 10:35:34 -06001423 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001424 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001425}
1426
Al Viro15316262015-03-30 22:08:36 -04001427static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001428{
Al Viro15316262015-03-30 22:08:36 -04001429 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001430 struct inode *inode = file_inode(file);
Seth Forshee744742d2016-03-11 10:35:34 -06001431 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001432 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001433
1434 if (is_bad_inode(inode))
1435 return -EIO;
1436
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001437 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001438 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001439 res = generic_write_checks(iocb, from);
1440 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001441 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001442 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001443 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001444 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001445 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001446
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001447 return res;
1448}
1449
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001450static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001451{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001452 int i;
1453
1454 for (i = 0; i < req->num_pages; i++)
1455 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001456
1457 if (req->ff)
1458 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001459}
1460
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001461static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001462{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001463 struct inode *inode = req->inode;
1464 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001465 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001466 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001467
1468 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001469 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001470 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001471 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001472 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001473 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001474 wake_up(&fi->page_waitq);
1475}
1476
1477/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001478static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1479 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001480__releases(fc->lock)
1481__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001482{
1483 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001484 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001485 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486
1487 if (!fc->connected)
1488 goto out_free;
1489
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001490 if (inarg->offset + data_size <= size) {
1491 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001492 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001493 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001494 } else {
1495 /* Got truncated off completely */
1496 goto out_free;
1497 }
1498
1499 req->in.args[1].size = inarg->size;
1500 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001501 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001502 return;
1503
1504 out_free:
1505 fuse_writepage_finish(fc, req);
1506 spin_unlock(&fc->lock);
1507 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001508 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001509 spin_lock(&fc->lock);
1510}
1511
1512/*
1513 * If fi->writectr is positive (no truncate or fsync going on) send
1514 * all queued writepage requests.
1515 *
1516 * Called with fc->lock
1517 */
1518void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001519__releases(fc->lock)
1520__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001521{
1522 struct fuse_conn *fc = get_fuse_conn(inode);
1523 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001524 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001525 struct fuse_req *req;
1526
1527 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1528 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1529 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001530 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001531 }
1532}
1533
1534static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1535{
1536 struct inode *inode = req->inode;
1537 struct fuse_inode *fi = get_fuse_inode(inode);
1538
1539 mapping_set_error(inode->i_mapping, req->out.h.error);
1540 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001541 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001542 struct fuse_conn *fc = get_fuse_conn(inode);
1543 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001544 struct fuse_req *next = req->misc.write.next;
1545 req->misc.write.next = next->misc.write.next;
1546 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001547 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001548 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001549
1550 /*
1551 * Skip fuse_flush_writepages() to make it easy to crop requests
1552 * based on primary request size.
1553 *
1554 * 1st case (trivial): there are no concurrent activities using
1555 * fuse_set/release_nowrite. Then we're on safe side because
1556 * fuse_flush_writepages() would call fuse_send_writepage()
1557 * anyway.
1558 *
1559 * 2nd case: someone called fuse_set_nowrite and it is waiting
1560 * now for completion of all in-flight requests. This happens
1561 * rarely and no more than once per page, so this should be
1562 * okay.
1563 *
1564 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1565 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1566 * that fuse_set_nowrite returned implies that all in-flight
1567 * requests were completed along with all of their secondary
1568 * requests. Further primary requests are blocked by negative
1569 * writectr. Hence there cannot be any in-flight requests and
1570 * no invocations of fuse_writepage_end() while we're in
1571 * fuse_set_nowrite..fuse_release_nowrite section.
1572 */
1573 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001574 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001575 fi->writectr--;
1576 fuse_writepage_finish(fc, req);
1577 spin_unlock(&fc->lock);
1578 fuse_writepage_free(fc, req);
1579}
1580
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001581static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1582 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001583{
Miklos Szeredi72523422013-10-01 16:44:52 +02001584 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001585
1586 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001587 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001588 ff = list_entry(fi->write_files.next, struct fuse_file,
1589 write_entry);
1590 fuse_file_get(ff);
1591 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001592 spin_unlock(&fc->lock);
1593
1594 return ff;
1595}
1596
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001597static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1598 struct fuse_inode *fi)
1599{
1600 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1601 WARN_ON(!ff);
1602 return ff;
1603}
1604
1605int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1606{
1607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_inode *fi = get_fuse_inode(inode);
1609 struct fuse_file *ff;
1610 int err;
1611
1612 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001613 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001614 if (ff)
1615 fuse_file_put(ff, 0);
1616
1617 return err;
1618}
1619
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001620static int fuse_writepage_locked(struct page *page)
1621{
1622 struct address_space *mapping = page->mapping;
1623 struct inode *inode = mapping->host;
1624 struct fuse_conn *fc = get_fuse_conn(inode);
1625 struct fuse_inode *fi = get_fuse_inode(inode);
1626 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001627 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001628 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001629
1630 set_page_writeback(page);
1631
Maxim Patlasov4250c062012-10-26 19:48:07 +04001632 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001633 if (!req)
1634 goto err;
1635
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001636 /* writeback always goes to bg_queue */
1637 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001638 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1639 if (!tmp_page)
1640 goto err_free;
1641
Miklos Szeredi72523422013-10-01 16:44:52 +02001642 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001643 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001644 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001645 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001646
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001647 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001648
1649 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001650 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001651 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001652 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001653 req->num_pages = 1;
1654 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001655 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001656 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657 req->end = fuse_writepage_end;
1658 req->inode = inode;
1659
Tejun Heo93f78d82015-05-22 17:13:27 -04001660 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001661 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001662
1663 spin_lock(&fc->lock);
1664 list_add(&req->writepages_entry, &fi->writepages);
1665 list_add_tail(&req->list, &fi->queued_writes);
1666 fuse_flush_writepages(inode);
1667 spin_unlock(&fc->lock);
1668
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001669 end_page_writeback(page);
1670
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001671 return 0;
1672
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001673err_nofile:
1674 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001675err_free:
1676 fuse_request_free(req);
1677err:
1678 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001679 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001680}
1681
1682static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1683{
1684 int err;
1685
Miklos Szerediff17be02013-10-01 16:44:53 +02001686 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1687 /*
1688 * ->writepages() should be called for sync() and friends. We
1689 * should only get here on direct reclaim and then we are
1690 * allowed to skip a page which is already in flight
1691 */
1692 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1693
1694 redirty_page_for_writepage(wbc, page);
1695 return 0;
1696 }
1697
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001698 err = fuse_writepage_locked(page);
1699 unlock_page(page);
1700
1701 return err;
1702}
1703
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001704struct fuse_fill_wb_data {
1705 struct fuse_req *req;
1706 struct fuse_file *ff;
1707 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001708 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001709};
1710
1711static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1712{
1713 struct fuse_req *req = data->req;
1714 struct inode *inode = data->inode;
1715 struct fuse_conn *fc = get_fuse_conn(inode);
1716 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001717 int num_pages = req->num_pages;
1718 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001719
1720 req->ff = fuse_file_get(data->ff);
1721 spin_lock(&fc->lock);
1722 list_add_tail(&req->list, &fi->queued_writes);
1723 fuse_flush_writepages(inode);
1724 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001725
1726 for (i = 0; i < num_pages; i++)
1727 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001728}
1729
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001730static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1731 struct page *page)
1732{
1733 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1734 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1735 struct fuse_req *tmp;
1736 struct fuse_req *old_req;
1737 bool found = false;
1738 pgoff_t curr_index;
1739
1740 BUG_ON(new_req->num_pages != 0);
1741
1742 spin_lock(&fc->lock);
1743 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001744 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1745 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001746 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001747 if (curr_index <= page->index &&
1748 page->index < curr_index + old_req->num_pages) {
1749 found = true;
1750 break;
1751 }
1752 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001753 if (!found) {
1754 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001755 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001756 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001757
Maxim Patlasovf6011082013-10-02 15:01:07 +04001758 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001759 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1760 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001761 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001762 if (tmp->num_pages == 1 &&
1763 curr_index == page->index) {
1764 old_req = tmp;
1765 }
1766 }
1767
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001768 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001769 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001770
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001771 copy_highpage(old_req->pages[0], page);
1772 spin_unlock(&fc->lock);
1773
Tejun Heo93f78d82015-05-22 17:13:27 -04001774 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001775 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001776 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001777 fuse_writepage_free(fc, new_req);
1778 fuse_request_free(new_req);
1779 goto out;
1780 } else {
1781 new_req->misc.write.next = old_req->misc.write.next;
1782 old_req->misc.write.next = new_req;
1783 }
1784out_unlock:
1785 spin_unlock(&fc->lock);
1786out:
1787 return found;
1788}
1789
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001790static int fuse_writepages_fill(struct page *page,
1791 struct writeback_control *wbc, void *_data)
1792{
1793 struct fuse_fill_wb_data *data = _data;
1794 struct fuse_req *req = data->req;
1795 struct inode *inode = data->inode;
1796 struct fuse_conn *fc = get_fuse_conn(inode);
1797 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001798 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001799 int err;
1800
1801 if (!data->ff) {
1802 err = -EIO;
1803 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1804 if (!data->ff)
1805 goto out_unlock;
1806 }
1807
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001808 /*
1809 * Being under writeback is unlikely but possible. For example direct
1810 * read to an mmaped fuse file will set the page dirty twice; once when
1811 * the pages are faulted with get_user_pages(), and then after the read
1812 * completed.
1813 */
1814 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001815
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001816 if (req && req->num_pages &&
1817 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001818 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001819 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1820 fuse_writepages_send(data);
1821 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001822 }
1823 err = -ENOMEM;
1824 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1825 if (!tmp_page)
1826 goto out_unlock;
1827
1828 /*
1829 * The page must not be redirtied until the writeout is completed
1830 * (i.e. userspace has sent a reply to the write request). Otherwise
1831 * there could be more than one temporary page instance for each real
1832 * page.
1833 *
1834 * This is ensured by holding the page lock in page_mkwrite() while
1835 * checking fuse_page_is_writeback(). We already hold the page lock
1836 * since clear_page_dirty_for_io() and keep it held until we add the
1837 * request to the fi->writepages list and increment req->num_pages.
1838 * After this fuse_page_is_writeback() will indicate that the page is
1839 * under writeback, so we can release the page lock.
1840 */
1841 if (data->req == NULL) {
1842 struct fuse_inode *fi = get_fuse_inode(inode);
1843
1844 err = -ENOMEM;
1845 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1846 if (!req) {
1847 __free_page(tmp_page);
1848 goto out_unlock;
1849 }
1850
1851 fuse_write_fill(req, data->ff, page_offset(page), 0);
1852 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001853 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001854 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001855 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001856 req->num_pages = 0;
1857 req->end = fuse_writepage_end;
1858 req->inode = inode;
1859
1860 spin_lock(&fc->lock);
1861 list_add(&req->writepages_entry, &fi->writepages);
1862 spin_unlock(&fc->lock);
1863
1864 data->req = req;
1865 }
1866 set_page_writeback(page);
1867
1868 copy_highpage(tmp_page, page);
1869 req->pages[req->num_pages] = tmp_page;
1870 req->page_descs[req->num_pages].offset = 0;
1871 req->page_descs[req->num_pages].length = PAGE_SIZE;
1872
Tejun Heo93f78d82015-05-22 17:13:27 -04001873 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001874 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001875
1876 err = 0;
1877 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1878 end_page_writeback(page);
1879 data->req = NULL;
1880 goto out_unlock;
1881 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001882 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001883
1884 /*
1885 * Protected by fc->lock against concurrent access by
1886 * fuse_page_is_writeback().
1887 */
1888 spin_lock(&fc->lock);
1889 req->num_pages++;
1890 spin_unlock(&fc->lock);
1891
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001892out_unlock:
1893 unlock_page(page);
1894
1895 return err;
1896}
1897
1898static int fuse_writepages(struct address_space *mapping,
1899 struct writeback_control *wbc)
1900{
1901 struct inode *inode = mapping->host;
1902 struct fuse_fill_wb_data data;
1903 int err;
1904
1905 err = -EIO;
1906 if (is_bad_inode(inode))
1907 goto out;
1908
1909 data.inode = inode;
1910 data.req = NULL;
1911 data.ff = NULL;
1912
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001913 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001914 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1915 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001916 GFP_NOFS);
1917 if (!data.orig_pages)
1918 goto out;
1919
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001920 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1921 if (data.req) {
1922 /* Ignore errors if we can write at least one page */
1923 BUG_ON(!data.req->num_pages);
1924 fuse_writepages_send(&data);
1925 err = 0;
1926 }
1927 if (data.ff)
1928 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001929
1930 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001931out:
1932 return err;
1933}
1934
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001935/*
1936 * It's worthy to make sure that space is reserved on disk for the write,
1937 * but how to implement it without killing performance need more thinking.
1938 */
1939static int fuse_write_begin(struct file *file, struct address_space *mapping,
1940 loff_t pos, unsigned len, unsigned flags,
1941 struct page **pagep, void **fsdata)
1942{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001943 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001944 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001945 struct page *page;
1946 loff_t fsize;
1947 int err = -ENOMEM;
1948
1949 WARN_ON(!fc->writeback_cache);
1950
1951 page = grab_cache_page_write_begin(mapping, index, flags);
1952 if (!page)
1953 goto error;
1954
1955 fuse_wait_on_page_writeback(mapping->host, page->index);
1956
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001957 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001958 goto success;
1959 /*
1960 * Check if the start this page comes after the end of file, in which
1961 * case the readpage can be optimized away.
1962 */
1963 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 if (fsize <= (pos & PAGE_MASK)) {
1965 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001966 if (off)
1967 zero_user_segment(page, 0, off);
1968 goto success;
1969 }
1970 err = fuse_do_readpage(file, page);
1971 if (err)
1972 goto cleanup;
1973success:
1974 *pagep = page;
1975 return 0;
1976
1977cleanup:
1978 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001979 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001980error:
1981 return err;
1982}
1983
1984static int fuse_write_end(struct file *file, struct address_space *mapping,
1985 loff_t pos, unsigned len, unsigned copied,
1986 struct page *page, void *fsdata)
1987{
1988 struct inode *inode = page->mapping->host;
1989
Miklos Szeredi59c3b762016-08-18 09:10:44 +02001990 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1991 if (!copied)
1992 goto unlock;
1993
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001994 if (!PageUptodate(page)) {
1995 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001996 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001997 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001998 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001999 SetPageUptodate(page);
2000 }
2001
2002 fuse_write_update_size(inode, pos + copied);
2003 set_page_dirty(page);
Miklos Szeredi59c3b762016-08-18 09:10:44 +02002004
2005unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002006 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002007 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002008
2009 return copied;
2010}
2011
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002012static int fuse_launder_page(struct page *page)
2013{
2014 int err = 0;
2015 if (clear_page_dirty_for_io(page)) {
2016 struct inode *inode = page->mapping->host;
2017 err = fuse_writepage_locked(page);
2018 if (!err)
2019 fuse_wait_on_page_writeback(inode, page->index);
2020 }
2021 return err;
2022}
2023
2024/*
2025 * Write back dirty pages now, because there may not be any suitable
2026 * open files later
2027 */
2028static void fuse_vma_close(struct vm_area_struct *vma)
2029{
2030 filemap_write_and_wait(vma->vm_file->f_mapping);
2031}
2032
2033/*
2034 * Wait for writeback against this page to complete before allowing it
2035 * to be marked dirty again, and hence written back again, possibly
2036 * before the previous writepage completed.
2037 *
2038 * Block here, instead of in ->writepage(), so that the userspace fs
2039 * can only block processes actually operating on the filesystem.
2040 *
2041 * Otherwise unprivileged userspace fs would be able to block
2042 * unrelated:
2043 *
2044 * - page migration
2045 * - sync(2)
2046 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2047 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002048static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002049{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002050 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002051 struct inode *inode = file_inode(vma->vm_file);
2052
2053 file_update_time(vma->vm_file);
2054 lock_page(page);
2055 if (page->mapping != inode->i_mapping) {
2056 unlock_page(page);
2057 return VM_FAULT_NOPAGE;
2058 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002059
2060 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002061 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002062}
2063
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002064static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002065 .close = fuse_vma_close,
2066 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002067 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002068 .page_mkwrite = fuse_page_mkwrite,
2069};
2070
2071static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2072{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002073 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2074 fuse_link_write_file(file);
2075
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002076 file_accessed(file);
2077 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002078 return 0;
2079}
2080
Miklos Szeredifc280c92009-04-02 14:25:35 +02002081static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2082{
2083 /* Can't provide the coherency needed for MAP_SHARED */
2084 if (vma->vm_flags & VM_MAYSHARE)
2085 return -ENODEV;
2086
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002087 invalidate_inode_pages2(file->f_mapping);
2088
Miklos Szeredifc280c92009-04-02 14:25:35 +02002089 return generic_file_mmap(file, vma);
2090}
2091
Miklos Szeredi71421252006-06-25 05:48:52 -07002092static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2093 struct file_lock *fl)
2094{
2095 switch (ffl->type) {
2096 case F_UNLCK:
2097 break;
2098
2099 case F_RDLCK:
2100 case F_WRLCK:
2101 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2102 ffl->end < ffl->start)
2103 return -EIO;
2104
2105 fl->fl_start = ffl->start;
2106 fl->fl_end = ffl->end;
2107 fl->fl_pid = ffl->pid;
2108 break;
2109
2110 default:
2111 return -EIO;
2112 }
2113 fl->fl_type = ffl->type;
2114 return 0;
2115}
2116
Miklos Szeredi70781872014-12-12 09:49:05 +01002117static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002118 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002119 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002120{
Al Viro6131ffa2013-02-27 16:59:05 -05002121 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002122 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002123 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002124
Miklos Szeredi70781872014-12-12 09:49:05 +01002125 memset(inarg, 0, sizeof(*inarg));
2126 inarg->fh = ff->fh;
2127 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2128 inarg->lk.start = fl->fl_start;
2129 inarg->lk.end = fl->fl_end;
2130 inarg->lk.type = fl->fl_type;
2131 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002132 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002133 inarg->lk_flags |= FUSE_LK_FLOCK;
2134 args->in.h.opcode = opcode;
2135 args->in.h.nodeid = get_node_id(inode);
2136 args->in.numargs = 1;
2137 args->in.args[0].size = sizeof(*inarg);
2138 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002139}
2140
2141static int fuse_getlk(struct file *file, struct file_lock *fl)
2142{
Al Viro6131ffa2013-02-27 16:59:05 -05002143 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002144 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002145 FUSE_ARGS(args);
2146 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002147 struct fuse_lk_out outarg;
2148 int err;
2149
Miklos Szeredi70781872014-12-12 09:49:05 +01002150 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2151 args.out.numargs = 1;
2152 args.out.args[0].size = sizeof(outarg);
2153 args.out.args[0].value = &outarg;
2154 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002155 if (!err)
2156 err = convert_fuse_file_lock(&outarg.lk, fl);
2157
2158 return err;
2159}
2160
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002161static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002162{
Al Viro6131ffa2013-02-27 16:59:05 -05002163 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002164 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002165 FUSE_ARGS(args);
2166 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002167 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2168 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2169 int err;
2170
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002171 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002172 /* NLM needs asynchronous locks, which we don't support yet */
2173 return -ENOLCK;
2174 }
2175
Miklos Szeredi71421252006-06-25 05:48:52 -07002176 /* Unlock on close is handled by the flush method */
2177 if (fl->fl_flags & FL_CLOSE)
2178 return 0;
2179
Miklos Szeredi70781872014-12-12 09:49:05 +01002180 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2181 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002182
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002183 /* locking is restartable */
2184 if (err == -EINTR)
2185 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002186
Miklos Szeredi71421252006-06-25 05:48:52 -07002187 return err;
2188}
2189
2190static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2191{
Al Viro6131ffa2013-02-27 16:59:05 -05002192 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002193 struct fuse_conn *fc = get_fuse_conn(inode);
2194 int err;
2195
Miklos Szeredi48e90762008-07-25 01:49:02 -07002196 if (cmd == F_CANCELLK) {
2197 err = 0;
2198 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002199 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002200 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002201 err = 0;
2202 } else
2203 err = fuse_getlk(file, fl);
2204 } else {
2205 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002206 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002207 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002208 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002209 }
2210 return err;
2211}
2212
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002213static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2214{
Al Viro6131ffa2013-02-27 16:59:05 -05002215 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002216 struct fuse_conn *fc = get_fuse_conn(inode);
2217 int err;
2218
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002219 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002220 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002221 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002222 struct fuse_file *ff = file->private_data;
2223
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002224 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002225 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002226 err = fuse_setlk(file, fl, 1);
2227 }
2228
2229 return err;
2230}
2231
Miklos Szeredib2d22722006-12-06 20:35:51 -08002232static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2233{
2234 struct inode *inode = mapping->host;
2235 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002236 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002237 struct fuse_bmap_in inarg;
2238 struct fuse_bmap_out outarg;
2239 int err;
2240
2241 if (!inode->i_sb->s_bdev || fc->no_bmap)
2242 return 0;
2243
Miklos Szeredib2d22722006-12-06 20:35:51 -08002244 memset(&inarg, 0, sizeof(inarg));
2245 inarg.block = block;
2246 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002247 args.in.h.opcode = FUSE_BMAP;
2248 args.in.h.nodeid = get_node_id(inode);
2249 args.in.numargs = 1;
2250 args.in.args[0].size = sizeof(inarg);
2251 args.in.args[0].value = &inarg;
2252 args.out.numargs = 1;
2253 args.out.args[0].size = sizeof(outarg);
2254 args.out.args[0].value = &outarg;
2255 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002256 if (err == -ENOSYS)
2257 fc->no_bmap = 1;
2258
2259 return err ? 0 : outarg.block;
2260}
2261
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302262static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2263{
2264 struct inode *inode = file->f_mapping->host;
2265 struct fuse_conn *fc = get_fuse_conn(inode);
2266 struct fuse_file *ff = file->private_data;
2267 FUSE_ARGS(args);
2268 struct fuse_lseek_in inarg = {
2269 .fh = ff->fh,
2270 .offset = offset,
2271 .whence = whence
2272 };
2273 struct fuse_lseek_out outarg;
2274 int err;
2275
2276 if (fc->no_lseek)
2277 goto fallback;
2278
2279 args.in.h.opcode = FUSE_LSEEK;
2280 args.in.h.nodeid = ff->nodeid;
2281 args.in.numargs = 1;
2282 args.in.args[0].size = sizeof(inarg);
2283 args.in.args[0].value = &inarg;
2284 args.out.numargs = 1;
2285 args.out.args[0].size = sizeof(outarg);
2286 args.out.args[0].value = &outarg;
2287 err = fuse_simple_request(fc, &args);
2288 if (err) {
2289 if (err == -ENOSYS) {
2290 fc->no_lseek = 1;
2291 goto fallback;
2292 }
2293 return err;
2294 }
2295
2296 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2297
2298fallback:
2299 err = fuse_update_attributes(inode, NULL, file, NULL);
2300 if (!err)
2301 return generic_file_llseek(file, offset, whence);
2302 else
2303 return err;
2304}
2305
Andrew Morton965c8e52012-12-17 15:59:39 -08002306static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002307{
2308 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002309 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002310
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302311 switch (whence) {
2312 case SEEK_SET:
2313 case SEEK_CUR:
2314 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002315 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302316 break;
2317 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002318 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302319 retval = fuse_update_attributes(inode, NULL, file, NULL);
2320 if (!retval)
2321 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002322 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302323 break;
2324 case SEEK_HOLE:
2325 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002326 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302327 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002328 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302329 break;
2330 default:
2331 retval = -EINVAL;
2332 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002333
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002334 return retval;
2335}
2336
Tejun Heo59efec72008-11-26 12:03:55 +01002337/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002338 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2339 * ABI was defined to be 'struct iovec' which is different on 32bit
2340 * and 64bit. Fortunately we can determine which structure the server
2341 * used from the size of the reply.
2342 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002343static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2344 size_t transferred, unsigned count,
2345 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002346{
2347#ifdef CONFIG_COMPAT
2348 if (count * sizeof(struct compat_iovec) == transferred) {
2349 struct compat_iovec *ciov = src;
2350 unsigned i;
2351
2352 /*
2353 * With this interface a 32bit server cannot support
2354 * non-compat (i.e. ones coming from 64bit apps) ioctl
2355 * requests
2356 */
2357 if (!is_compat)
2358 return -EINVAL;
2359
2360 for (i = 0; i < count; i++) {
2361 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2362 dst[i].iov_len = ciov[i].iov_len;
2363 }
2364 return 0;
2365 }
2366#endif
2367
2368 if (count * sizeof(struct iovec) != transferred)
2369 return -EIO;
2370
2371 memcpy(dst, src, transferred);
2372 return 0;
2373}
2374
Miklos Szeredi75727772010-11-30 16:39:27 +01002375/* Make sure iov_length() won't overflow */
2376static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2377{
2378 size_t n;
2379 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2380
Zach Brownfb6ccff2012-07-24 12:10:11 -07002381 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002382 if (iov->iov_len > (size_t) max)
2383 return -ENOMEM;
2384 max -= iov->iov_len;
2385 }
2386 return 0;
2387}
2388
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002389static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2390 void *src, size_t transferred, unsigned count,
2391 bool is_compat)
2392{
2393 unsigned i;
2394 struct fuse_ioctl_iovec *fiov = src;
2395
2396 if (fc->minor < 16) {
2397 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2398 count, is_compat);
2399 }
2400
2401 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2402 return -EIO;
2403
2404 for (i = 0; i < count; i++) {
2405 /* Did the server supply an inappropriate value? */
2406 if (fiov[i].base != (unsigned long) fiov[i].base ||
2407 fiov[i].len != (unsigned long) fiov[i].len)
2408 return -EIO;
2409
2410 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2411 dst[i].iov_len = (size_t) fiov[i].len;
2412
2413#ifdef CONFIG_COMPAT
2414 if (is_compat &&
2415 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2416 (compat_size_t) dst[i].iov_len != fiov[i].len))
2417 return -EIO;
2418#endif
2419 }
2420
2421 return 0;
2422}
2423
2424
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002425/*
Tejun Heo59efec72008-11-26 12:03:55 +01002426 * For ioctls, there is no generic way to determine how much memory
2427 * needs to be read and/or written. Furthermore, ioctls are allowed
2428 * to dereference the passed pointer, so the parameter requires deep
2429 * copying but FUSE has no idea whatsoever about what to copy in or
2430 * out.
2431 *
2432 * This is solved by allowing FUSE server to retry ioctl with
2433 * necessary in/out iovecs. Let's assume the ioctl implementation
2434 * needs to read in the following structure.
2435 *
2436 * struct a {
2437 * char *buf;
2438 * size_t buflen;
2439 * }
2440 *
2441 * On the first callout to FUSE server, inarg->in_size and
2442 * inarg->out_size will be NULL; then, the server completes the ioctl
2443 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2444 * the actual iov array to
2445 *
2446 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2447 *
2448 * which tells FUSE to copy in the requested area and retry the ioctl.
2449 * On the second round, the server has access to the structure and
2450 * from that it can tell what to look for next, so on the invocation,
2451 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2452 *
2453 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2454 * { .iov_base = a.buf, .iov_len = a.buflen } }
2455 *
2456 * FUSE will copy both struct a and the pointed buffer from the
2457 * process doing the ioctl and retry ioctl with both struct a and the
2458 * buffer.
2459 *
2460 * This time, FUSE server has everything it needs and completes ioctl
2461 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2462 *
2463 * Copying data out works the same way.
2464 *
2465 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2466 * automatically initializes in and out iovs by decoding @cmd with
2467 * _IOC_* macros and the server is not allowed to request RETRY. This
2468 * limits ioctl data transfers to well-formed ioctls and is the forced
2469 * behavior for all FUSE servers.
2470 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002471long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2472 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002473{
Tejun Heo59efec72008-11-26 12:03:55 +01002474 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002475 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002476 struct fuse_ioctl_in inarg = {
2477 .fh = ff->fh,
2478 .cmd = cmd,
2479 .arg = arg,
2480 .flags = flags
2481 };
2482 struct fuse_ioctl_out outarg;
2483 struct fuse_req *req = NULL;
2484 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002485 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002486 struct iovec *in_iov = NULL, *out_iov = NULL;
2487 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002488 size_t in_size, out_size, transferred, c;
2489 int err, i;
2490 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002491
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002492#if BITS_PER_LONG == 32
2493 inarg.flags |= FUSE_IOCTL_32BIT;
2494#else
2495 if (flags & FUSE_IOCTL_COMPAT)
2496 inarg.flags |= FUSE_IOCTL_32BIT;
2497#endif
2498
Tejun Heo59efec72008-11-26 12:03:55 +01002499 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002500 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002501
Tejun Heo59efec72008-11-26 12:03:55 +01002502 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002503 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002504 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002505 if (!pages || !iov_page)
2506 goto out;
2507
2508 /*
2509 * If restricted, initialize IO parameters as encoded in @cmd.
2510 * RETRY from server is not allowed.
2511 */
2512 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002513 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002514
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002515 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002516 iov->iov_len = _IOC_SIZE(cmd);
2517
2518 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2519 in_iov = iov;
2520 in_iovs = 1;
2521 }
2522
2523 if (_IOC_DIR(cmd) & _IOC_READ) {
2524 out_iov = iov;
2525 out_iovs = 1;
2526 }
2527 }
2528
2529 retry:
2530 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2531 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2532
2533 /*
2534 * Out data can be used either for actual out data or iovs,
2535 * make sure there always is at least one page.
2536 */
2537 out_size = max_t(size_t, out_size, PAGE_SIZE);
2538 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2539
2540 /* make sure there are enough buffer pages and init request with them */
2541 err = -ENOMEM;
2542 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2543 goto out;
2544 while (num_pages < max_pages) {
2545 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2546 if (!pages[num_pages])
2547 goto out;
2548 num_pages++;
2549 }
2550
Maxim Patlasov54b96672012-10-26 19:49:13 +04002551 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002552 if (IS_ERR(req)) {
2553 err = PTR_ERR(req);
2554 req = NULL;
2555 goto out;
2556 }
2557 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2558 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002559 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002560
2561 /* okay, let's send it to the client */
2562 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002563 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002564 req->in.numargs = 1;
2565 req->in.args[0].size = sizeof(inarg);
2566 req->in.args[0].value = &inarg;
2567 if (in_size) {
2568 req->in.numargs++;
2569 req->in.args[1].size = in_size;
2570 req->in.argpages = 1;
2571
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002572 err = -EFAULT;
2573 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2574 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2575 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2576 if (c != PAGE_SIZE && iov_iter_count(&ii))
2577 goto out;
2578 }
Tejun Heo59efec72008-11-26 12:03:55 +01002579 }
2580
2581 req->out.numargs = 2;
2582 req->out.args[0].size = sizeof(outarg);
2583 req->out.args[0].value = &outarg;
2584 req->out.args[1].size = out_size;
2585 req->out.argpages = 1;
2586 req->out.argvar = 1;
2587
Tejun Heob93f8582008-11-26 12:03:55 +01002588 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002589 err = req->out.h.error;
2590 transferred = req->out.args[1].size;
2591 fuse_put_request(fc, req);
2592 req = NULL;
2593 if (err)
2594 goto out;
2595
2596 /* did it ask for retry? */
2597 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002598 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002599
2600 /* no retry if in restricted mode */
2601 err = -EIO;
2602 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2603 goto out;
2604
2605 in_iovs = outarg.in_iovs;
2606 out_iovs = outarg.out_iovs;
2607
2608 /*
2609 * Make sure things are in boundary, separate checks
2610 * are to protect against overflow.
2611 */
2612 err = -ENOMEM;
2613 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2614 out_iovs > FUSE_IOCTL_MAX_IOV ||
2615 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2616 goto out;
2617
Cong Wang2408f6e2011-11-25 23:14:30 +08002618 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002619 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002620 transferred, in_iovs + out_iovs,
2621 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002622 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002623 if (err)
2624 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002625
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002626 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002627 out_iov = in_iov + in_iovs;
2628
Miklos Szeredi75727772010-11-30 16:39:27 +01002629 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2630 if (err)
2631 goto out;
2632
2633 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2634 if (err)
2635 goto out;
2636
Tejun Heo59efec72008-11-26 12:03:55 +01002637 goto retry;
2638 }
2639
2640 err = -EIO;
2641 if (transferred > inarg.out_size)
2642 goto out;
2643
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002644 err = -EFAULT;
2645 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2646 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2647 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2648 if (c != PAGE_SIZE && iov_iter_count(&ii))
2649 goto out;
2650 }
2651 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002652 out:
2653 if (req)
2654 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002655 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002656 while (num_pages)
2657 __free_page(pages[--num_pages]);
2658 kfree(pages);
2659
2660 return err ? err : outarg.result;
2661}
Tejun Heo08cbf542009-04-14 10:54:53 +09002662EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002663
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002664long fuse_ioctl_common(struct file *file, unsigned int cmd,
2665 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002666{
Al Viro6131ffa2013-02-27 16:59:05 -05002667 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002668 struct fuse_conn *fc = get_fuse_conn(inode);
2669
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002670 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002671 return -EACCES;
2672
2673 if (is_bad_inode(inode))
2674 return -EIO;
2675
2676 return fuse_do_ioctl(file, cmd, arg, flags);
2677}
2678
Tejun Heo59efec72008-11-26 12:03:55 +01002679static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2680 unsigned long arg)
2681{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002682 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002683}
2684
2685static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2686 unsigned long arg)
2687{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002688 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002689}
2690
Tejun Heo95668a62008-11-26 12:03:55 +01002691/*
2692 * All files which have been polled are linked to RB tree
2693 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2694 * find the matching one.
2695 */
2696static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2697 struct rb_node **parent_out)
2698{
2699 struct rb_node **link = &fc->polled_files.rb_node;
2700 struct rb_node *last = NULL;
2701
2702 while (*link) {
2703 struct fuse_file *ff;
2704
2705 last = *link;
2706 ff = rb_entry(last, struct fuse_file, polled_node);
2707
2708 if (kh < ff->kh)
2709 link = &last->rb_left;
2710 else if (kh > ff->kh)
2711 link = &last->rb_right;
2712 else
2713 return link;
2714 }
2715
2716 if (parent_out)
2717 *parent_out = last;
2718 return link;
2719}
2720
2721/*
2722 * The file is about to be polled. Make sure it's on the polled_files
2723 * RB tree. Note that files once added to the polled_files tree are
2724 * not removed before the file is released. This is because a file
2725 * polled once is likely to be polled again.
2726 */
2727static void fuse_register_polled_file(struct fuse_conn *fc,
2728 struct fuse_file *ff)
2729{
2730 spin_lock(&fc->lock);
2731 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002732 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002733
2734 link = fuse_find_polled_node(fc, ff->kh, &parent);
2735 BUG_ON(*link);
2736 rb_link_node(&ff->polled_node, parent, link);
2737 rb_insert_color(&ff->polled_node, &fc->polled_files);
2738 }
2739 spin_unlock(&fc->lock);
2740}
2741
Tejun Heo08cbf542009-04-14 10:54:53 +09002742unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002743{
Tejun Heo95668a62008-11-26 12:03:55 +01002744 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002745 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002746 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2747 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002748 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002749 int err;
2750
2751 if (fc->no_poll)
2752 return DEFAULT_POLLMASK;
2753
2754 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002755 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002756
2757 /*
2758 * Ask for notification iff there's someone waiting for it.
2759 * The client may ignore the flag and always notify.
2760 */
2761 if (waitqueue_active(&ff->poll_wait)) {
2762 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2763 fuse_register_polled_file(fc, ff);
2764 }
2765
Miklos Szeredi70781872014-12-12 09:49:05 +01002766 args.in.h.opcode = FUSE_POLL;
2767 args.in.h.nodeid = ff->nodeid;
2768 args.in.numargs = 1;
2769 args.in.args[0].size = sizeof(inarg);
2770 args.in.args[0].value = &inarg;
2771 args.out.numargs = 1;
2772 args.out.args[0].size = sizeof(outarg);
2773 args.out.args[0].value = &outarg;
2774 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002775
2776 if (!err)
2777 return outarg.revents;
2778 if (err == -ENOSYS) {
2779 fc->no_poll = 1;
2780 return DEFAULT_POLLMASK;
2781 }
2782 return POLLERR;
2783}
Tejun Heo08cbf542009-04-14 10:54:53 +09002784EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002785
2786/*
2787 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2788 * wakes up the poll waiters.
2789 */
2790int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2791 struct fuse_notify_poll_wakeup_out *outarg)
2792{
2793 u64 kh = outarg->kh;
2794 struct rb_node **link;
2795
2796 spin_lock(&fc->lock);
2797
2798 link = fuse_find_polled_node(fc, kh, NULL);
2799 if (*link) {
2800 struct fuse_file *ff;
2801
2802 ff = rb_entry(*link, struct fuse_file, polled_node);
2803 wake_up_interruptible_sync(&ff->poll_wait);
2804 }
2805
2806 spin_unlock(&fc->lock);
2807 return 0;
2808}
2809
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002810static void fuse_do_truncate(struct file *file)
2811{
2812 struct inode *inode = file->f_mapping->host;
2813 struct iattr attr;
2814
2815 attr.ia_valid = ATTR_SIZE;
2816 attr.ia_size = i_size_read(inode);
2817
2818 attr.ia_file = file;
2819 attr.ia_valid |= ATTR_FILE;
2820
Jan Kara62490332016-05-26 17:12:41 +02002821 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002822}
2823
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002824static inline loff_t fuse_round_up(loff_t off)
2825{
2826 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2827}
2828
Anand Avati4273b792012-02-17 12:46:25 -05002829static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002830fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002831{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002832 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002833 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002834 struct file *file = iocb->ki_filp;
2835 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002836 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002837 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002838 struct inode *inode;
2839 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002840 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002841 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002842 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002843
Anand Avati4273b792012-02-17 12:46:25 -05002844 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002845 inode = file->f_mapping->host;
2846 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002847
Omar Sandoval6f673762015-03-16 04:33:52 -07002848 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002849 return 0;
2850
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002851 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002852 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002853 if (offset >= i_size)
2854 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002855 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2856 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002857 }
2858
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002859 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002860 if (!io)
2861 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002862 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002863 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002864 io->reqs = 1;
2865 io->bytes = -1;
2866 io->size = 0;
2867 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002868 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002869 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002870 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002871 /*
2872 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002873 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002874 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002875 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002876 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302877 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002878
2879 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302880 * We cannot asynchronously extend the size of a file.
2881 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002882 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302883 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2884 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002885
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302886 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002887 /*
2888 * Additional reference to keep io around after
2889 * calling fuse_aio_complete()
2890 */
2891 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002892 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002893 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002894
Omar Sandoval6f673762015-03-16 04:33:52 -07002895 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002896 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002897 fuse_invalidate_attr(inode);
2898 } else {
Al Virod22a9432014-03-16 15:50:47 -04002899 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002900 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002901
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002902 if (io->async) {
2903 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2904
2905 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302906 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002907 return -EIOCBQUEUED;
2908
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002909 wait_for_completion(&wait);
2910 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002911 }
2912
Seth Forshee744742d2016-03-11 10:35:34 -06002913 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002914
Omar Sandoval6f673762015-03-16 04:33:52 -07002915 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002916 if (ret > 0)
2917 fuse_write_update_size(inode, pos);
2918 else if (ret < 0 && offset + count > i_size)
2919 fuse_do_truncate(file);
2920 }
Anand Avati4273b792012-02-17 12:46:25 -05002921
2922 return ret;
2923}
2924
Miklos Szeredicdadb112012-11-10 16:55:56 +01002925static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2926 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002927{
2928 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002929 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002930 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002931 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002932 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002933 struct fuse_fallocate_in inarg = {
2934 .fh = ff->fh,
2935 .offset = offset,
2936 .length = length,
2937 .mode = mode
2938 };
2939 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002940 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2941 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002942
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002943 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2944 return -EOPNOTSUPP;
2945
Miklos Szeredi519c6042012-04-26 10:56:36 +02002946 if (fc->no_fallocate)
2947 return -EOPNOTSUPP;
2948
Maxim Patlasov14c14412013-06-13 12:16:39 +04002949 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002950 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002951 if (mode & FALLOC_FL_PUNCH_HOLE) {
2952 loff_t endbyte = offset + length - 1;
2953 err = filemap_write_and_wait_range(inode->i_mapping,
2954 offset, endbyte);
2955 if (err)
2956 goto out;
2957
2958 fuse_sync_writes(inode);
2959 }
Brian Foster3634a632013-05-17 09:30:32 -04002960 }
2961
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002962 if (!(mode & FALLOC_FL_KEEP_SIZE))
2963 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2964
Miklos Szeredi70781872014-12-12 09:49:05 +01002965 args.in.h.opcode = FUSE_FALLOCATE;
2966 args.in.h.nodeid = ff->nodeid;
2967 args.in.numargs = 1;
2968 args.in.args[0].size = sizeof(inarg);
2969 args.in.args[0].value = &inarg;
2970 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002971 if (err == -ENOSYS) {
2972 fc->no_fallocate = 1;
2973 err = -EOPNOTSUPP;
2974 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002975 if (err)
2976 goto out;
2977
2978 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002979 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2980 bool changed = fuse_write_update_size(inode, offset + length);
2981
Miklos Szeredi93d22692014-04-28 14:19:22 +02002982 if (changed && fc->writeback_cache)
2983 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002984 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002985
2986 if (mode & FALLOC_FL_PUNCH_HOLE)
2987 truncate_pagecache_range(inode, offset, offset + length - 1);
2988
2989 fuse_invalidate_attr(inode);
2990
Brian Foster3634a632013-05-17 09:30:32 -04002991out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002992 if (!(mode & FALLOC_FL_KEEP_SIZE))
2993 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2994
Maxim Patlasovbde52782013-09-13 19:19:54 +04002995 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05002996 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04002997
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002998 return err;
2999}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003000
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003001static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003002 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003003 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003004 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003005 .mmap = fuse_file_mmap,
3006 .open = fuse_open,
3007 .flush = fuse_flush,
3008 .release = fuse_release,
3009 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003010 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003011 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003012 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003013 .unlocked_ioctl = fuse_file_ioctl,
3014 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003015 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003016 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003017};
3018
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003019static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003020 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003021 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003022 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003023 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003024 .open = fuse_open,
3025 .flush = fuse_flush,
3026 .release = fuse_release,
3027 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003028 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003029 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003030 .unlocked_ioctl = fuse_file_ioctl,
3031 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003032 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003033 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003034 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003035};
3036
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003037static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003038 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003039 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003040 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003041 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003042 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003043 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003044 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003045 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003046 .write_begin = fuse_write_begin,
3047 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003048};
3049
3050void fuse_init_file_inode(struct inode *inode)
3051{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003052 inode->i_fop = &fuse_file_operations;
3053 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003054}