blob: 11538a8be9f05b2859a261c2302c38733571dcf8 [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
Miklos Szeredifd72faa2005-11-07 00:59:51 -080049 ff = kmalloc(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 Szeredid4a8db62017-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)
211 mutex_lock(&inode->i_mutex);
212
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)
219 mutex_unlock(&inode->i_mutex);
220
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);
352 curr_index = req->misc.write.in.offset >> PAGE_CACHE_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
417 mutex_lock(&inode->i_mutex);
418 fuse_sync_writes(inode);
419 mutex_unlock(&inode->i_mutex);
420
Maxim Patlasov9ca5f112016-07-19 18:12:26 -0700421 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
422 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
423 err = -ENOSPC;
424 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
425 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
426 err = -EIO;
427 if (err)
428 return err;
429
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400430 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 memset(&inarg, 0, sizeof(inarg));
432 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700433 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 req->in.h.opcode = FUSE_FLUSH;
435 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700436 req->in.numargs = 1;
437 req->in.args[0].size = sizeof(inarg);
438 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200439 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100440 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700441 err = req->out.h.error;
442 fuse_put_request(fc, req);
443 if (err == -ENOSYS) {
444 fc->no_flush = 1;
445 err = 0;
446 }
447 return err;
448}
449
Josef Bacik02c24a82011-07-16 20:44:56 -0400450int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
451 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700452{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200453 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700454 struct fuse_conn *fc = get_fuse_conn(inode);
455 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100456 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700457 struct fuse_fsync_in inarg;
458 int err;
459
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800460 if (is_bad_inode(inode))
461 return -EIO;
462
Josef Bacik02c24a82011-07-16 20:44:56 -0400463 mutex_lock(&inode->i_mutex);
464
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700465 /*
466 * Start writeback against all dirty pages of the inode, then
467 * wait for all outstanding writes, before sending the FSYNC
468 * request.
469 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200470 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700471 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400472 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700473
474 fuse_sync_writes(inode);
Alexey Kuznetsov3d1c64d2016-07-19 12:48:01 -0700475
476 /*
477 * Due to implementation of fuse writeback
478 * filemap_write_and_wait_range() does not catch errors.
479 * We have to do this directly after fuse_sync_writes()
480 */
481 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
482 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
483 err = -ENOSPC;
484 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
485 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
486 err = -EIO;
487 if (err)
488 goto out;
489
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200490 err = sync_inode_metadata(inode, 1);
491 if (err)
492 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700493
Miklos Szeredi22401e72014-04-28 14:19:23 +0200494 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
495 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400496
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700497 memset(&inarg, 0, sizeof(inarg));
498 inarg.fh = ff->fh;
499 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100500 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
501 args.in.h.nodeid = get_node_id(inode);
502 args.in.numargs = 1;
503 args.in.args[0].size = sizeof(inarg);
504 args.in.args[0].value = &inarg;
505 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700506 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700507 if (isdir)
508 fc->no_fsyncdir = 1;
509 else
510 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 err = 0;
512 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400513out:
514 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515 return err;
516}
517
Josef Bacik02c24a82011-07-16 20:44:56 -0400518static int fuse_fsync(struct file *file, loff_t start, loff_t end,
519 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700520{
Josef Bacik02c24a82011-07-16 20:44:56 -0400521 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700522}
523
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200524void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
525 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700527 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800528 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800530 inarg->fh = ff->fh;
531 inarg->offset = pos;
532 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800533 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800534 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200535 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700536 req->in.numargs = 1;
537 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800538 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700539 req->out.argvar = 1;
540 req->out.numargs = 1;
541 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700542}
543
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +0200544static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400545{
546 unsigned i;
547
548 for (i = 0; i < req->num_pages; i++) {
549 struct page *page = req->pages[i];
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +0200550 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400551 set_page_dirty_lock(page);
552 put_page(page);
553 }
554}
555
Seth Forshee37bd8c82016-03-11 10:35:34 -0600556static void fuse_io_release(struct kref *kref)
557{
558 kfree(container_of(kref, struct fuse_io_priv, refcnt));
559}
560
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100561static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
562{
563 if (io->err)
564 return io->err;
565
566 if (io->bytes >= 0 && io->write)
567 return -EIO;
568
569 return io->bytes < 0 ? io->size : io->bytes;
570}
571
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400572/**
573 * In case of short read, the caller sets 'pos' to the position of
574 * actual end of fuse request in IO request. Otherwise, if bytes_requested
575 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
576 *
577 * An example:
578 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
579 * both submitted asynchronously. The first of them was ACKed by userspace as
580 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
581 * second request was ACKed as short, e.g. only 1K was read, resulting in
582 * pos == 33K.
583 *
584 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
585 * will be equal to the length of the longest contiguous fragment of
586 * transferred data starting from the beginning of IO request.
587 */
588static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
589{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100590 bool is_sync = is_sync_kiocb(io->iocb);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400591 int left;
592
593 spin_lock(&io->lock);
594 if (err)
595 io->err = io->err ? : err;
596 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
597 io->bytes = pos;
598
599 left = --io->reqs;
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100600 if (!left && is_sync)
601 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400602 spin_unlock(&io->lock);
603
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100604 if (!left && !is_sync) {
605 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400606
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100607 if (res >= 0) {
608 struct inode *inode = file_inode(io->iocb->ki_filp);
609 struct fuse_conn *fc = get_fuse_conn(inode);
610 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400611
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100612 spin_lock(&fc->lock);
613 fi->attr_version = ++fc->attr_version;
614 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400615 }
616
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100617 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400618 }
Seth Forshee37bd8c82016-03-11 10:35:34 -0600619
620 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400621}
622
623static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
624{
625 struct fuse_io_priv *io = req->io;
626 ssize_t pos = -1;
627
628 fuse_release_user_pages(req, !io->write);
629
630 if (io->write) {
631 if (req->misc.write.in.size != req->misc.write.out.size)
632 pos = req->misc.write.in.offset - io->offset +
633 req->misc.write.out.size;
634 } else {
635 if (req->misc.read.in.size != req->out.args[0].size)
636 pos = req->misc.read.in.offset - io->offset +
637 req->out.args[0].size;
638 }
639
640 fuse_aio_complete(io, req->out.h.error, pos);
641}
642
643static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
644 size_t num_bytes, struct fuse_io_priv *io)
645{
646 spin_lock(&io->lock);
Seth Forshee37bd8c82016-03-11 10:35:34 -0600647 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400648 io->size += num_bytes;
649 io->reqs++;
650 spin_unlock(&io->lock);
651
652 req->io = io;
653 req->end = fuse_aio_complete_req;
654
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400655 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400656 fuse_request_send_background(fc, req);
657
658 return num_bytes;
659}
660
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400661static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200662 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700663{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400664 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200665 struct fuse_file *ff = file->private_data;
666 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700667
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200668 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700669 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700670 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700671
672 inarg->read_flags |= FUSE_READ_LOCKOWNER;
673 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
674 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400675
676 if (io->async)
677 return fuse_async_req_send(fc, req, count, io);
678
Tejun Heob93f8582008-11-26 12:03:55 +0100679 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800680 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700681}
682
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700683static void fuse_read_update_size(struct inode *inode, loff_t size,
684 u64 attr_ver)
685{
686 struct fuse_conn *fc = get_fuse_conn(inode);
687 struct fuse_inode *fi = get_fuse_inode(inode);
688
689 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400690 if (attr_ver == fi->attr_version && size < inode->i_size &&
691 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700692 fi->attr_version = ++fc->attr_version;
693 i_size_write(inode, size);
694 }
695 spin_unlock(&fc->lock);
696}
697
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400698static void fuse_short_read(struct fuse_req *req, struct inode *inode,
699 u64 attr_ver)
700{
701 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400702 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400703
Pavel Emelyanov83732002013-10-10 17:10:46 +0400704 if (fc->writeback_cache) {
705 /*
706 * A hole in a file. Some data after the hole are in page cache,
707 * but have not reached the client fs yet. So, the hole is not
708 * present there.
709 */
710 int i;
711 int start_idx = num_read >> PAGE_CACHE_SHIFT;
712 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
713
714 for (i = start_idx; i < req->num_pages; i++) {
715 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
716 off = 0;
717 }
718 } else {
719 loff_t pos = page_offset(req->pages[0]) + num_read;
720 fuse_read_update_size(inode, pos, attr_ver);
721 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400722}
723
Maxim Patlasov482fce52013-10-10 17:11:25 +0400724static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700725{
Seth Forshee37bd8c82016-03-11 10:35:34 -0600726 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700727 struct inode *inode = page->mapping->host;
728 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800729 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700730 size_t num_read;
731 loff_t pos = page_offset(page);
732 size_t count = PAGE_CACHE_SIZE;
733 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800734 int err;
735
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700736 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300737 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700738 * page-cache page, so make sure we read a properly synced
739 * page.
740 */
741 fuse_wait_on_page_writeback(inode, page->index);
742
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400743 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700744 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400745 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700746
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747 attr_ver = fuse_get_attr_version(fc);
748
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700749 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200750 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700751 req->num_pages = 1;
752 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400753 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400754 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700755 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700756
757 if (!err) {
758 /*
759 * Short read means EOF. If file size is larger, truncate it
760 */
761 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400762 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700763
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700764 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700765 }
766
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400767 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400768
769 return err;
770}
771
772static int fuse_readpage(struct file *file, struct page *page)
773{
774 struct inode *inode = page->mapping->host;
775 int err;
776
777 err = -EIO;
778 if (is_bad_inode(inode))
779 goto out;
780
781 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800782 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700783 out:
784 unlock_page(page);
785 return err;
786}
787
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800788static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700789{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800790 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700791 size_t count = req->misc.read.in.size;
792 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200793 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800794
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795 for (i = 0; mapping == NULL && i < req->num_pages; i++)
796 mapping = req->pages[i]->mapping;
797
798 if (mapping) {
799 struct inode *inode = mapping->host;
800
801 /*
802 * Short read means EOF. If file size is larger, truncate it
803 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400804 if (!req->out.h.error && num_read < count)
805 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200806
Andrew Gallagher451418f2013-11-05 03:55:43 -0800807 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700808 }
809
Miklos Szeredidb50b962005-09-09 13:10:33 -0700810 for (i = 0; i < req->num_pages; i++) {
811 struct page *page = req->pages[i];
812 if (!req->out.h.error)
813 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800814 else
815 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700816 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200817 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700818 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700819 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100820 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800821}
822
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200823static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800824{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200825 struct fuse_file *ff = file->private_data;
826 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800827 loff_t pos = page_offset(req->pages[0]);
828 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200829
830 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200832 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200833 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700834 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800835 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700836 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800837 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100838 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800839 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100840 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800841 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100842 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800843 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700844}
845
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700846struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700847 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800848 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700849 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400850 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851};
852
853static int fuse_readpages_fill(void *_data, struct page *page)
854{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700855 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856 struct fuse_req *req = data->req;
857 struct inode *inode = data->inode;
858 struct fuse_conn *fc = get_fuse_conn(inode);
859
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700860 fuse_wait_on_page_writeback(inode, page->index);
861
Miklos Szeredidb50b962005-09-09 13:10:33 -0700862 if (req->num_pages &&
863 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
864 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
865 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400866 int nr_alloc = min_t(unsigned, data->nr_pages,
867 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200868 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400869 if (fc->async_read)
870 req = fuse_get_req_for_background(fc, nr_alloc);
871 else
872 req = fuse_get_req(fc, nr_alloc);
873
874 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700875 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700877 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400880
881 if (WARN_ON(req->num_pages >= req->max_pages)) {
882 fuse_put_request(fc, req);
883 return -EIO;
884 }
885
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200886 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700887 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400888 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100889 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400890 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700891 return 0;
892}
893
894static int fuse_readpages(struct file *file, struct address_space *mapping,
895 struct list_head *pages, unsigned nr_pages)
896{
897 struct inode *inode = mapping->host;
898 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700899 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700900 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400901 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800902
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700903 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800904 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800905 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800906
Miklos Szeredia6643092007-11-28 16:22:00 -0800907 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700908 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400909 if (fc->async_read)
910 data.req = fuse_get_req_for_background(fc, nr_alloc);
911 else
912 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400913 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700914 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700915 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800916 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700917
918 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700919 if (!err) {
920 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200921 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700922 else
923 fuse_put_request(fc, data.req);
924 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800925out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700926 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700927}
928
Al Viro37c20f12014-04-02 14:47:09 -0400929static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800930{
931 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400932 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933
Brian Fostera8894272012-07-16 15:23:50 -0400934 /*
935 * In auto invalidate mode, always update attributes on read.
936 * Otherwise, only update if we attempt to read past EOF (to ensure
937 * i_size is up to date).
938 */
939 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400940 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
943 if (err)
944 return err;
945 }
946
Al Viro37c20f12014-04-02 14:47:09 -0400947 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800948}
949
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200950static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700952{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700953 struct fuse_write_in *inarg = &req->misc.write.in;
954 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700956 inarg->fh = ff->fh;
957 inarg->offset = pos;
958 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700959 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200960 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700961 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200962 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700963 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
964 else
965 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700966 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700967 req->in.args[1].size = count;
968 req->out.numargs = 1;
969 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700970 req->out.args[0].value = outarg;
971}
972
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400973static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200974 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700975{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400976 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200977 struct fuse_file *ff = file->private_data;
978 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200979 struct fuse_write_in *inarg = &req->misc.write.in;
980
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200981 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200982 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700983 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700984 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
985 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
986 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400987
988 if (io->async)
989 return fuse_async_req_send(fc, req, count, io);
990
Tejun Heob93f8582008-11-26 12:03:55 +0100991 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700992 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700993}
994
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400995bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996{
997 struct fuse_conn *fc = get_fuse_conn(inode);
998 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400999 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000
1001 spin_lock(&fc->lock);
1002 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001005 ret = true;
1006 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001007 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001008
1009 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001010}
1011
Nick Pigginea9b9902008-04-30 00:54:42 -07001012static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1013 struct inode *inode, loff_t pos,
1014 size_t count)
1015{
1016 size_t res;
1017 unsigned offset;
1018 unsigned i;
Seth Forshee37bd8c82016-03-11 10:35:34 -06001019 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001020
1021 for (i = 0; i < req->num_pages; i++)
1022 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1023
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001024 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001025
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001026 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001027 count = res;
1028 for (i = 0; i < req->num_pages; i++) {
1029 struct page *page = req->pages[i];
1030
1031 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1032 SetPageUptodate(page);
1033
1034 if (count > PAGE_CACHE_SIZE - offset)
1035 count -= PAGE_CACHE_SIZE - offset;
1036 else
1037 count = 0;
1038 offset = 0;
1039
1040 unlock_page(page);
1041 page_cache_release(page);
1042 }
1043
1044 return res;
1045}
1046
1047static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1048 struct address_space *mapping,
1049 struct iov_iter *ii, loff_t pos)
1050{
1051 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1052 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1053 size_t count = 0;
1054 int err;
1055
Miklos Szeredif4975c62009-04-02 14:25:34 +02001056 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001057 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001058
1059 do {
1060 size_t tmp;
1061 struct page *page;
1062 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1063 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1064 iov_iter_count(ii));
1065
1066 bytes = min_t(size_t, bytes, fc->max_write - count);
1067
1068 again:
1069 err = -EFAULT;
1070 if (iov_iter_fault_in_readable(ii, bytes))
1071 break;
1072
1073 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001074 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001075 if (!page)
1076 break;
1077
anfei zhou931e80e2010-02-02 13:44:02 -08001078 if (mapping_writably_mapped(mapping))
1079 flush_dcache_page(page);
1080
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001082 flush_dcache_page(page);
1083
Roman Gushchin3ca81382015-10-12 16:33:44 +03001084 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 if (!tmp) {
1086 unlock_page(page);
1087 page_cache_release(page);
1088 bytes = min(bytes, iov_iter_single_seg_count(ii));
1089 goto again;
1090 }
1091
1092 err = 0;
1093 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001094 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 req->num_pages++;
1096
Nick Pigginea9b9902008-04-30 00:54:42 -07001097 count += tmp;
1098 pos += tmp;
1099 offset += tmp;
1100 if (offset == PAGE_CACHE_SIZE)
1101 offset = 0;
1102
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001103 if (!fc->big_writes)
1104 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001105 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001106 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001107
1108 return count > 0 ? count : err;
1109}
1110
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001111static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1112{
1113 return min_t(unsigned,
1114 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1115 (pos >> PAGE_CACHE_SHIFT) + 1,
1116 FUSE_MAX_PAGES_PER_REQ);
1117}
1118
Nick Pigginea9b9902008-04-30 00:54:42 -07001119static ssize_t fuse_perform_write(struct file *file,
1120 struct address_space *mapping,
1121 struct iov_iter *ii, loff_t pos)
1122{
1123 struct inode *inode = mapping->host;
1124 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001125 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001126 int err = 0;
1127 ssize_t res = 0;
1128
1129 if (is_bad_inode(inode))
1130 return -EIO;
1131
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001132 if (inode->i_size < pos + iov_iter_count(ii))
1133 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1134
Nick Pigginea9b9902008-04-30 00:54:42 -07001135 do {
1136 struct fuse_req *req;
1137 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001138 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001139
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001140 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001141 if (IS_ERR(req)) {
1142 err = PTR_ERR(req);
1143 break;
1144 }
1145
1146 count = fuse_fill_write_pages(req, mapping, ii, pos);
1147 if (count <= 0) {
1148 err = count;
1149 } else {
1150 size_t num_written;
1151
1152 num_written = fuse_send_write_pages(req, file, inode,
1153 pos, count);
1154 err = req->out.h.error;
1155 if (!err) {
1156 res += num_written;
1157 pos += num_written;
1158
1159 /* break out of the loop on short write */
1160 if (num_written != count)
1161 err = -EIO;
1162 }
1163 }
1164 fuse_put_request(fc, req);
1165 } while (!err && iov_iter_count(ii));
1166
1167 if (res > 0)
1168 fuse_write_update_size(inode, pos);
1169
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001170 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001171 fuse_invalidate_attr(inode);
1172
1173 return res > 0 ? res : err;
1174}
1175
Al Viro84c3d552014-04-03 14:33:23 -04001176static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001177{
1178 struct file *file = iocb->ki_filp;
1179 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001180 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001181 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001182 struct inode *inode = mapping->host;
1183 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001184 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001185
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001186 if (get_fuse_conn(inode)->writeback_cache) {
1187 /* Update size (EOF optimization) and mode (SUID clearing) */
1188 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1189 if (err)
1190 return err;
1191
Al Viro84c3d552014-04-03 14:33:23 -04001192 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001193 }
1194
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001196
1197 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001198 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001199
Al Viro3309dd02015-04-09 12:55:47 -04001200 err = generic_write_checks(iocb, from);
1201 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001202 goto out;
1203
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001204 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001205 if (err)
1206 goto out;
1207
Josef Bacikc3b2da32012-03-26 09:59:21 -04001208 err = file_update_time(file);
1209 if (err)
1210 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
Al Viro2ba48ce2015-04-09 13:52:01 -04001212 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001213 loff_t pos = iocb->ki_pos;
Al Viro84c3d552014-04-03 14:33:23 -04001214 written = generic_file_direct_write(iocb, from, pos);
1215 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001216 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001217
Anand Avati4273b792012-02-17 12:46:25 -05001218 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001219
Al Viro84c3d552014-04-03 14:33:23 -04001220 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001221 if (written_buffered < 0) {
1222 err = written_buffered;
1223 goto out;
1224 }
1225 endbyte = pos + written_buffered - 1;
1226
1227 err = filemap_write_and_wait_range(file->f_mapping, pos,
1228 endbyte);
1229 if (err)
1230 goto out;
1231
1232 invalidate_mapping_pages(file->f_mapping,
1233 pos >> PAGE_CACHE_SHIFT,
1234 endbyte >> PAGE_CACHE_SHIFT);
1235
1236 written += written_buffered;
1237 iocb->ki_pos = pos + written_buffered;
1238 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001239 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001240 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001241 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001242 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001243out:
1244 current->backing_dev_info = NULL;
1245 mutex_unlock(&inode->i_mutex);
1246
1247 return written ? written : err;
1248}
1249
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001250static inline void fuse_page_descs_length_init(struct fuse_req *req,
1251 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001252{
1253 int i;
1254
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001255 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001256 req->page_descs[i].length = PAGE_SIZE -
1257 req->page_descs[i].offset;
1258}
1259
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001260static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1261{
1262 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1263}
1264
1265static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1266 size_t max_size)
1267{
1268 return min(iov_iter_single_seg_count(ii), max_size);
1269}
1270
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001271static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001272 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001273{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001274 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001275
Miklos Szeredif4975c62009-04-02 14:25:34 +02001276 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001277 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001278 unsigned long user_addr = fuse_get_user_addr(ii);
1279 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1280
Miklos Szeredif4975c62009-04-02 14:25:34 +02001281 if (write)
1282 req->in.args[1].value = (void *) user_addr;
1283 else
1284 req->out.args[0].value = (void *) user_addr;
1285
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001286 iov_iter_advance(ii, frag_size);
1287 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001288 return 0;
1289 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001290
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001291 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001292 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001293 size_t start;
Al Viroc9c37e22014-03-16 16:08:30 -04001294 ssize_t ret = iov_iter_get_pages(ii,
1295 &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001296 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001297 req->max_pages - req->num_pages,
1298 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001299 if (ret < 0)
1300 return ret;
1301
Al Viroc9c37e22014-03-16 16:08:30 -04001302 iov_iter_advance(ii, ret);
1303 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001304
Al Viroc9c37e22014-03-16 16:08:30 -04001305 ret += start;
1306 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1307
1308 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309 fuse_page_descs_length_init(req, req->num_pages, npages);
1310
1311 req->num_pages += npages;
1312 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001313 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001314 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001315
1316 if (write)
1317 req->in.argpages = 1;
1318 else
1319 req->out.argpages = 1;
1320
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001321 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001322
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001323 return 0;
1324}
1325
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001326static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1327{
Al Virof67da302014-03-19 01:16:16 -04001328 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001329}
1330
Al Virod22a9432014-03-16 15:50:47 -04001331ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1332 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001333{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001334 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +02001335 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001336 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001337 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001338 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001339 struct fuse_file *ff = file->private_data;
1340 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001341 size_t nmax = write ? fc->max_write : fc->max_read;
1342 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001343 size_t count = iov_iter_count(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001344 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1345 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001346 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001347 struct fuse_req *req;
1348
Brian Fosterde82b922013-05-14 11:25:39 -04001349 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001350 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001351 else
Al Virod22a9432014-03-16 15:50:47 -04001352 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001353 if (IS_ERR(req))
1354 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001355
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001356 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1357 if (!write)
1358 mutex_lock(&inode->i_mutex);
1359 fuse_sync_writes(inode);
1360 if (!write)
1361 mutex_unlock(&inode->i_mutex);
1362 }
1363
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001365 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001366 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001367 size_t nbytes = min(count, nmax);
Al Virod22a9432014-03-16 15:50:47 -04001368 int err = fuse_get_user_pages(req, iter, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 if (err) {
1370 res = err;
1371 break;
1372 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001373
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001374 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001375 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001376 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001377 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001378
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001379 if (!io->async)
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +02001380 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001381 if (req->out.h.error) {
1382 if (!res)
1383 res = req->out.h.error;
1384 break;
1385 } else if (nres > nbytes) {
1386 res = -EIO;
1387 break;
1388 }
1389 count -= nres;
1390 res += nres;
1391 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 if (nres != nbytes)
1393 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001394 if (count) {
1395 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001396 if (io->async)
1397 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001398 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001399 else
Al Virod22a9432014-03-16 15:50:47 -04001400 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001401 if (IS_ERR(req))
1402 break;
1403 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001405 if (!IS_ERR(req))
1406 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001407 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409
1410 return res;
1411}
Tejun Heo08cbf542009-04-14 10:54:53 +09001412EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001414static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001415 struct iov_iter *iter,
1416 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001418 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001419 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001420 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001421
1422 if (is_bad_inode(inode))
1423 return -EIO;
1424
Al Virod22a9432014-03-16 15:50:47 -04001425 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001426
1427 fuse_invalidate_attr(inode);
1428
1429 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001430}
1431
Al Viro15316262015-03-30 22:08:36 -04001432static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001433{
Seth Forshee37bd8c82016-03-11 10:35:34 -06001434 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001435 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001436}
1437
Al Viro15316262015-03-30 22:08:36 -04001438static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001439{
Al Viro15316262015-03-30 22:08:36 -04001440 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001441 struct inode *inode = file_inode(file);
Seth Forshee37bd8c82016-03-11 10:35:34 -06001442 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001443 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001444
1445 if (is_bad_inode(inode))
1446 return -EIO;
1447
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001448 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001449 mutex_lock(&inode->i_mutex);
Al Viro3309dd02015-04-09 12:55:47 -04001450 res = generic_write_checks(iocb, from);
1451 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001452 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001453 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001454 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001455 fuse_write_update_size(inode, iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001456 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001457
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001458 return res;
1459}
1460
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001461static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001462{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001463 int i;
1464
1465 for (i = 0; i < req->num_pages; i++)
1466 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001467
1468 if (req->ff)
1469 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001470}
1471
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001472static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001473{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001474 struct inode *inode = req->inode;
1475 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001476 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001477 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001478
1479 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001480 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001481 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001482 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001483 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001484 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001485 wake_up(&fi->page_waitq);
1486}
1487
1488/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001489static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1490 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001491__releases(fc->lock)
1492__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493{
1494 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001495 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001496 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497
1498 if (!fc->connected)
1499 goto out_free;
1500
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001501 if (inarg->offset + data_size <= size) {
1502 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001503 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001504 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001505 } else {
1506 /* Got truncated off completely */
1507 goto out_free;
1508 }
1509
1510 req->in.args[1].size = inarg->size;
1511 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001512 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001513 return;
1514
1515 out_free:
1516 fuse_writepage_finish(fc, req);
1517 spin_unlock(&fc->lock);
1518 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001519 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001520 spin_lock(&fc->lock);
1521}
1522
1523/*
1524 * If fi->writectr is positive (no truncate or fsync going on) send
1525 * all queued writepage requests.
1526 *
1527 * Called with fc->lock
1528 */
1529void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001530__releases(fc->lock)
1531__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001532{
1533 struct fuse_conn *fc = get_fuse_conn(inode);
1534 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001535 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001536 struct fuse_req *req;
1537
1538 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1539 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1540 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001541 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001542 }
1543}
1544
1545static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1546{
1547 struct inode *inode = req->inode;
1548 struct fuse_inode *fi = get_fuse_inode(inode);
1549
1550 mapping_set_error(inode->i_mapping, req->out.h.error);
1551 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001552 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001553 struct fuse_conn *fc = get_fuse_conn(inode);
1554 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001555 struct fuse_req *next = req->misc.write.next;
1556 req->misc.write.next = next->misc.write.next;
1557 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001558 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001559 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001560
1561 /*
1562 * Skip fuse_flush_writepages() to make it easy to crop requests
1563 * based on primary request size.
1564 *
1565 * 1st case (trivial): there are no concurrent activities using
1566 * fuse_set/release_nowrite. Then we're on safe side because
1567 * fuse_flush_writepages() would call fuse_send_writepage()
1568 * anyway.
1569 *
1570 * 2nd case: someone called fuse_set_nowrite and it is waiting
1571 * now for completion of all in-flight requests. This happens
1572 * rarely and no more than once per page, so this should be
1573 * okay.
1574 *
1575 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1576 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1577 * that fuse_set_nowrite returned implies that all in-flight
1578 * requests were completed along with all of their secondary
1579 * requests. Further primary requests are blocked by negative
1580 * writectr. Hence there cannot be any in-flight requests and
1581 * no invocations of fuse_writepage_end() while we're in
1582 * fuse_set_nowrite..fuse_release_nowrite section.
1583 */
1584 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001585 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001586 fi->writectr--;
1587 fuse_writepage_finish(fc, req);
1588 spin_unlock(&fc->lock);
1589 fuse_writepage_free(fc, req);
1590}
1591
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001592static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1593 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001594{
Miklos Szeredi72523422013-10-01 16:44:52 +02001595 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001596
1597 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001598 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001599 ff = list_entry(fi->write_files.next, struct fuse_file,
1600 write_entry);
1601 fuse_file_get(ff);
1602 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001603 spin_unlock(&fc->lock);
1604
1605 return ff;
1606}
1607
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001608static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1609 struct fuse_inode *fi)
1610{
1611 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1612 WARN_ON(!ff);
1613 return ff;
1614}
1615
1616int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1617{
1618 struct fuse_conn *fc = get_fuse_conn(inode);
1619 struct fuse_inode *fi = get_fuse_inode(inode);
1620 struct fuse_file *ff;
1621 int err;
1622
1623 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001624 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001625 if (ff)
1626 fuse_file_put(ff, 0);
1627
1628 return err;
1629}
1630
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631static int fuse_writepage_locked(struct page *page)
1632{
1633 struct address_space *mapping = page->mapping;
1634 struct inode *inode = mapping->host;
1635 struct fuse_conn *fc = get_fuse_conn(inode);
1636 struct fuse_inode *fi = get_fuse_inode(inode);
1637 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001638 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001639 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640
1641 set_page_writeback(page);
1642
Maxim Patlasov4250c062012-10-26 19:48:07 +04001643 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001644 if (!req)
1645 goto err;
1646
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001647 /* writeback always goes to bg_queue */
1648 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001649 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1650 if (!tmp_page)
1651 goto err_free;
1652
Miklos Szeredi72523422013-10-01 16:44:52 +02001653 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001654 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001655 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001656 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001657
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001658 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659
1660 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001661 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001662 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001663 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001664 req->num_pages = 1;
1665 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001666 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001667 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001668 req->end = fuse_writepage_end;
1669 req->inode = inode;
1670
Tejun Heo93f78d82015-05-22 17:13:27 -04001671 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001672 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001673
1674 spin_lock(&fc->lock);
1675 list_add(&req->writepages_entry, &fi->writepages);
1676 list_add_tail(&req->list, &fi->queued_writes);
1677 fuse_flush_writepages(inode);
1678 spin_unlock(&fc->lock);
1679
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001680 end_page_writeback(page);
1681
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001682 return 0;
1683
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001684err_nofile:
1685 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001686err_free:
1687 fuse_request_free(req);
1688err:
1689 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001690 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001691}
1692
1693static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1694{
1695 int err;
1696
Miklos Szerediff17be02013-10-01 16:44:53 +02001697 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1698 /*
1699 * ->writepages() should be called for sync() and friends. We
1700 * should only get here on direct reclaim and then we are
1701 * allowed to skip a page which is already in flight
1702 */
1703 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1704
1705 redirty_page_for_writepage(wbc, page);
1706 return 0;
1707 }
1708
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001709 err = fuse_writepage_locked(page);
1710 unlock_page(page);
1711
1712 return err;
1713}
1714
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001715struct fuse_fill_wb_data {
1716 struct fuse_req *req;
1717 struct fuse_file *ff;
1718 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001719 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001720};
1721
1722static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1723{
1724 struct fuse_req *req = data->req;
1725 struct inode *inode = data->inode;
1726 struct fuse_conn *fc = get_fuse_conn(inode);
1727 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001728 int num_pages = req->num_pages;
1729 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001730
1731 req->ff = fuse_file_get(data->ff);
1732 spin_lock(&fc->lock);
1733 list_add_tail(&req->list, &fi->queued_writes);
1734 fuse_flush_writepages(inode);
1735 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001736
1737 for (i = 0; i < num_pages; i++)
1738 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001739}
1740
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001741static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1742 struct page *page)
1743{
1744 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1745 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1746 struct fuse_req *tmp;
1747 struct fuse_req *old_req;
1748 bool found = false;
1749 pgoff_t curr_index;
1750
1751 BUG_ON(new_req->num_pages != 0);
1752
1753 spin_lock(&fc->lock);
1754 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001755 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1756 BUG_ON(old_req->inode != new_req->inode);
1757 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1758 if (curr_index <= page->index &&
1759 page->index < curr_index + old_req->num_pages) {
1760 found = true;
1761 break;
1762 }
1763 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001764 if (!found) {
1765 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001766 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001767 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768
Maxim Patlasovf6011082013-10-02 15:01:07 +04001769 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001770 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1771 BUG_ON(tmp->inode != new_req->inode);
1772 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1773 if (tmp->num_pages == 1 &&
1774 curr_index == page->index) {
1775 old_req = tmp;
1776 }
1777 }
1778
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001779 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001780 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001781
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001782 copy_highpage(old_req->pages[0], page);
1783 spin_unlock(&fc->lock);
1784
Tejun Heo93f78d82015-05-22 17:13:27 -04001785 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001786 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001787 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001788 fuse_writepage_free(fc, new_req);
1789 fuse_request_free(new_req);
1790 goto out;
1791 } else {
1792 new_req->misc.write.next = old_req->misc.write.next;
1793 old_req->misc.write.next = new_req;
1794 }
1795out_unlock:
1796 spin_unlock(&fc->lock);
1797out:
1798 return found;
1799}
1800
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001801static int fuse_writepages_fill(struct page *page,
1802 struct writeback_control *wbc, void *_data)
1803{
1804 struct fuse_fill_wb_data *data = _data;
1805 struct fuse_req *req = data->req;
1806 struct inode *inode = data->inode;
1807 struct fuse_conn *fc = get_fuse_conn(inode);
1808 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001810 int err;
1811
1812 if (!data->ff) {
1813 err = -EIO;
1814 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1815 if (!data->ff)
1816 goto out_unlock;
1817 }
1818
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001819 /*
1820 * Being under writeback is unlikely but possible. For example direct
1821 * read to an mmaped fuse file will set the page dirty twice; once when
1822 * the pages are faulted with get_user_pages(), and then after the read
1823 * completed.
1824 */
1825 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001826
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001827 if (req && req->num_pages &&
1828 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1829 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1830 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1831 fuse_writepages_send(data);
1832 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001833 }
1834 err = -ENOMEM;
1835 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1836 if (!tmp_page)
1837 goto out_unlock;
1838
1839 /*
1840 * The page must not be redirtied until the writeout is completed
1841 * (i.e. userspace has sent a reply to the write request). Otherwise
1842 * there could be more than one temporary page instance for each real
1843 * page.
1844 *
1845 * This is ensured by holding the page lock in page_mkwrite() while
1846 * checking fuse_page_is_writeback(). We already hold the page lock
1847 * since clear_page_dirty_for_io() and keep it held until we add the
1848 * request to the fi->writepages list and increment req->num_pages.
1849 * After this fuse_page_is_writeback() will indicate that the page is
1850 * under writeback, so we can release the page lock.
1851 */
1852 if (data->req == NULL) {
1853 struct fuse_inode *fi = get_fuse_inode(inode);
1854
1855 err = -ENOMEM;
1856 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1857 if (!req) {
1858 __free_page(tmp_page);
1859 goto out_unlock;
1860 }
1861
1862 fuse_write_fill(req, data->ff, page_offset(page), 0);
1863 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001864 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001865 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001866 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001867 req->num_pages = 0;
1868 req->end = fuse_writepage_end;
1869 req->inode = inode;
1870
1871 spin_lock(&fc->lock);
1872 list_add(&req->writepages_entry, &fi->writepages);
1873 spin_unlock(&fc->lock);
1874
1875 data->req = req;
1876 }
1877 set_page_writeback(page);
1878
1879 copy_highpage(tmp_page, page);
1880 req->pages[req->num_pages] = tmp_page;
1881 req->page_descs[req->num_pages].offset = 0;
1882 req->page_descs[req->num_pages].length = PAGE_SIZE;
1883
Tejun Heo93f78d82015-05-22 17:13:27 -04001884 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001885 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001886
1887 err = 0;
1888 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1889 end_page_writeback(page);
1890 data->req = NULL;
1891 goto out_unlock;
1892 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001893 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894
1895 /*
1896 * Protected by fc->lock against concurrent access by
1897 * fuse_page_is_writeback().
1898 */
1899 spin_lock(&fc->lock);
1900 req->num_pages++;
1901 spin_unlock(&fc->lock);
1902
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001903out_unlock:
1904 unlock_page(page);
1905
1906 return err;
1907}
1908
1909static int fuse_writepages(struct address_space *mapping,
1910 struct writeback_control *wbc)
1911{
1912 struct inode *inode = mapping->host;
1913 struct fuse_fill_wb_data data;
1914 int err;
1915
1916 err = -EIO;
1917 if (is_bad_inode(inode))
1918 goto out;
1919
1920 data.inode = inode;
1921 data.req = NULL;
1922 data.ff = NULL;
1923
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001924 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001925 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1926 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001927 GFP_NOFS);
1928 if (!data.orig_pages)
1929 goto out;
1930
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001931 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1932 if (data.req) {
1933 /* Ignore errors if we can write at least one page */
1934 BUG_ON(!data.req->num_pages);
1935 fuse_writepages_send(&data);
1936 err = 0;
1937 }
1938 if (data.ff)
1939 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001940
1941 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001942out:
1943 return err;
1944}
1945
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001946/*
1947 * It's worthy to make sure that space is reserved on disk for the write,
1948 * but how to implement it without killing performance need more thinking.
1949 */
1950static int fuse_write_begin(struct file *file, struct address_space *mapping,
1951 loff_t pos, unsigned len, unsigned flags,
1952 struct page **pagep, void **fsdata)
1953{
1954 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001955 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001956 struct page *page;
1957 loff_t fsize;
1958 int err = -ENOMEM;
1959
1960 WARN_ON(!fc->writeback_cache);
1961
1962 page = grab_cache_page_write_begin(mapping, index, flags);
1963 if (!page)
1964 goto error;
1965
1966 fuse_wait_on_page_writeback(mapping->host, page->index);
1967
1968 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1969 goto success;
1970 /*
1971 * Check if the start this page comes after the end of file, in which
1972 * case the readpage can be optimized away.
1973 */
1974 fsize = i_size_read(mapping->host);
1975 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1976 size_t off = pos & ~PAGE_CACHE_MASK;
1977 if (off)
1978 zero_user_segment(page, 0, off);
1979 goto success;
1980 }
1981 err = fuse_do_readpage(file, page);
1982 if (err)
1983 goto cleanup;
1984success:
1985 *pagep = page;
1986 return 0;
1987
1988cleanup:
1989 unlock_page(page);
1990 page_cache_release(page);
1991error:
1992 return err;
1993}
1994
1995static int fuse_write_end(struct file *file, struct address_space *mapping,
1996 loff_t pos, unsigned len, unsigned copied,
1997 struct page *page, void *fsdata)
1998{
1999 struct inode *inode = page->mapping->host;
2000
Miklos Szeredib7321bc2016-08-18 09:10:44 +02002001 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2002 if (!copied)
2003 goto unlock;
2004
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002005 if (!PageUptodate(page)) {
2006 /* Zero any unwritten bytes at the end of the page */
2007 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2008 if (endoff)
2009 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2010 SetPageUptodate(page);
2011 }
2012
2013 fuse_write_update_size(inode, pos + copied);
2014 set_page_dirty(page);
Miklos Szeredib7321bc2016-08-18 09:10:44 +02002015
2016unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002017 unlock_page(page);
2018 page_cache_release(page);
2019
2020 return copied;
2021}
2022
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002023static int fuse_launder_page(struct page *page)
2024{
2025 int err = 0;
2026 if (clear_page_dirty_for_io(page)) {
2027 struct inode *inode = page->mapping->host;
2028 err = fuse_writepage_locked(page);
2029 if (!err)
2030 fuse_wait_on_page_writeback(inode, page->index);
2031 }
2032 return err;
2033}
2034
2035/*
2036 * Write back dirty pages now, because there may not be any suitable
2037 * open files later
2038 */
2039static void fuse_vma_close(struct vm_area_struct *vma)
2040{
2041 filemap_write_and_wait(vma->vm_file->f_mapping);
2042}
2043
2044/*
2045 * Wait for writeback against this page to complete before allowing it
2046 * to be marked dirty again, and hence written back again, possibly
2047 * before the previous writepage completed.
2048 *
2049 * Block here, instead of in ->writepage(), so that the userspace fs
2050 * can only block processes actually operating on the filesystem.
2051 *
2052 * Otherwise unprivileged userspace fs would be able to block
2053 * unrelated:
2054 *
2055 * - page migration
2056 * - sync(2)
2057 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2058 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002059static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002060{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002061 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002062 struct inode *inode = file_inode(vma->vm_file);
2063
2064 file_update_time(vma->vm_file);
2065 lock_page(page);
2066 if (page->mapping != inode->i_mapping) {
2067 unlock_page(page);
2068 return VM_FAULT_NOPAGE;
2069 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002070
2071 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002072 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002073}
2074
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002075static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002076 .close = fuse_vma_close,
2077 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002078 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002079 .page_mkwrite = fuse_page_mkwrite,
2080};
2081
2082static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2083{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002084 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2085 fuse_link_write_file(file);
2086
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002087 file_accessed(file);
2088 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002089 return 0;
2090}
2091
Miklos Szeredifc280c92009-04-02 14:25:35 +02002092static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2093{
2094 /* Can't provide the coherency needed for MAP_SHARED */
2095 if (vma->vm_flags & VM_MAYSHARE)
2096 return -ENODEV;
2097
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002098 invalidate_inode_pages2(file->f_mapping);
2099
Miklos Szeredifc280c92009-04-02 14:25:35 +02002100 return generic_file_mmap(file, vma);
2101}
2102
Miklos Szeredi71421252006-06-25 05:48:52 -07002103static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2104 struct file_lock *fl)
2105{
2106 switch (ffl->type) {
2107 case F_UNLCK:
2108 break;
2109
2110 case F_RDLCK:
2111 case F_WRLCK:
2112 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2113 ffl->end < ffl->start)
2114 return -EIO;
2115
2116 fl->fl_start = ffl->start;
2117 fl->fl_end = ffl->end;
2118 fl->fl_pid = ffl->pid;
2119 break;
2120
2121 default:
2122 return -EIO;
2123 }
2124 fl->fl_type = ffl->type;
2125 return 0;
2126}
2127
Miklos Szeredi70781872014-12-12 09:49:05 +01002128static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002129 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002130 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002131{
Al Viro6131ffa2013-02-27 16:59:05 -05002132 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002133 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002134 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002135
Miklos Szeredi70781872014-12-12 09:49:05 +01002136 memset(inarg, 0, sizeof(*inarg));
2137 inarg->fh = ff->fh;
2138 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2139 inarg->lk.start = fl->fl_start;
2140 inarg->lk.end = fl->fl_end;
2141 inarg->lk.type = fl->fl_type;
2142 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002143 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002144 inarg->lk_flags |= FUSE_LK_FLOCK;
2145 args->in.h.opcode = opcode;
2146 args->in.h.nodeid = get_node_id(inode);
2147 args->in.numargs = 1;
2148 args->in.args[0].size = sizeof(*inarg);
2149 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002150}
2151
2152static int fuse_getlk(struct file *file, struct file_lock *fl)
2153{
Al Viro6131ffa2013-02-27 16:59:05 -05002154 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002155 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002156 FUSE_ARGS(args);
2157 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002158 struct fuse_lk_out outarg;
2159 int err;
2160
Miklos Szeredi70781872014-12-12 09:49:05 +01002161 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2162 args.out.numargs = 1;
2163 args.out.args[0].size = sizeof(outarg);
2164 args.out.args[0].value = &outarg;
2165 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002166 if (!err)
2167 err = convert_fuse_file_lock(&outarg.lk, fl);
2168
2169 return err;
2170}
2171
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002172static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002173{
Al Viro6131ffa2013-02-27 16:59:05 -05002174 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002175 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002176 FUSE_ARGS(args);
2177 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002178 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2179 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2180 int err;
2181
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002182 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002183 /* NLM needs asynchronous locks, which we don't support yet */
2184 return -ENOLCK;
2185 }
2186
Miklos Szeredi71421252006-06-25 05:48:52 -07002187 /* Unlock on close is handled by the flush method */
2188 if (fl->fl_flags & FL_CLOSE)
2189 return 0;
2190
Miklos Szeredi70781872014-12-12 09:49:05 +01002191 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2192 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002193
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002194 /* locking is restartable */
2195 if (err == -EINTR)
2196 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002197
Miklos Szeredi71421252006-06-25 05:48:52 -07002198 return err;
2199}
2200
2201static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2202{
Al Viro6131ffa2013-02-27 16:59:05 -05002203 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002204 struct fuse_conn *fc = get_fuse_conn(inode);
2205 int err;
2206
Miklos Szeredi48e90762008-07-25 01:49:02 -07002207 if (cmd == F_CANCELLK) {
2208 err = 0;
2209 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002210 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002211 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 err = 0;
2213 } else
2214 err = fuse_getlk(file, fl);
2215 } else {
2216 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002217 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002218 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002219 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002220 }
2221 return err;
2222}
2223
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002224static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2225{
Al Viro6131ffa2013-02-27 16:59:05 -05002226 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002227 struct fuse_conn *fc = get_fuse_conn(inode);
2228 int err;
2229
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002230 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002231 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002232 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002233 struct fuse_file *ff = file->private_data;
2234
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002235 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002236 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002237 err = fuse_setlk(file, fl, 1);
2238 }
2239
2240 return err;
2241}
2242
Miklos Szeredib2d22722006-12-06 20:35:51 -08002243static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2244{
2245 struct inode *inode = mapping->host;
2246 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002247 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002248 struct fuse_bmap_in inarg;
2249 struct fuse_bmap_out outarg;
2250 int err;
2251
2252 if (!inode->i_sb->s_bdev || fc->no_bmap)
2253 return 0;
2254
Miklos Szeredib2d22722006-12-06 20:35:51 -08002255 memset(&inarg, 0, sizeof(inarg));
2256 inarg.block = block;
2257 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002258 args.in.h.opcode = FUSE_BMAP;
2259 args.in.h.nodeid = get_node_id(inode);
2260 args.in.numargs = 1;
2261 args.in.args[0].size = sizeof(inarg);
2262 args.in.args[0].value = &inarg;
2263 args.out.numargs = 1;
2264 args.out.args[0].size = sizeof(outarg);
2265 args.out.args[0].value = &outarg;
2266 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002267 if (err == -ENOSYS)
2268 fc->no_bmap = 1;
2269
2270 return err ? 0 : outarg.block;
2271}
2272
Andrew Morton965c8e52012-12-17 15:59:39 -08002273static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002274{
2275 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002276 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002277
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002278 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002279 if (whence == SEEK_CUR || whence == SEEK_SET)
2280 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002281
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002282 mutex_lock(&inode->i_mutex);
2283 retval = fuse_update_attributes(inode, NULL, file, NULL);
2284 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002285 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002286 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002287
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002288 return retval;
2289}
2290
Tejun Heo59efec72008-11-26 12:03:55 +01002291static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2292 unsigned int nr_segs, size_t bytes, bool to_user)
2293{
2294 struct iov_iter ii;
2295 int page_idx = 0;
2296
2297 if (!bytes)
2298 return 0;
2299
Al Viro71d8e532014-03-05 19:28:09 -05002300 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
Tejun Heo59efec72008-11-26 12:03:55 +01002301
2302 while (iov_iter_count(&ii)) {
2303 struct page *page = pages[page_idx++];
2304 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002305 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002306
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002307 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002308
2309 while (todo) {
2310 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2311 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2312 size_t copy = min(todo, iov_len);
2313 size_t left;
2314
2315 if (!to_user)
2316 left = copy_from_user(kaddr, uaddr, copy);
2317 else
2318 left = copy_to_user(uaddr, kaddr, copy);
2319
2320 if (unlikely(left))
2321 return -EFAULT;
2322
2323 iov_iter_advance(&ii, copy);
2324 todo -= copy;
2325 kaddr += copy;
2326 }
2327
Jens Axboe0bd87182009-11-03 11:40:44 +01002328 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002329 }
2330
2331 return 0;
2332}
2333
2334/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002335 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2336 * ABI was defined to be 'struct iovec' which is different on 32bit
2337 * and 64bit. Fortunately we can determine which structure the server
2338 * used from the size of the reply.
2339 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002340static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2341 size_t transferred, unsigned count,
2342 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002343{
2344#ifdef CONFIG_COMPAT
2345 if (count * sizeof(struct compat_iovec) == transferred) {
2346 struct compat_iovec *ciov = src;
2347 unsigned i;
2348
2349 /*
2350 * With this interface a 32bit server cannot support
2351 * non-compat (i.e. ones coming from 64bit apps) ioctl
2352 * requests
2353 */
2354 if (!is_compat)
2355 return -EINVAL;
2356
2357 for (i = 0; i < count; i++) {
2358 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2359 dst[i].iov_len = ciov[i].iov_len;
2360 }
2361 return 0;
2362 }
2363#endif
2364
2365 if (count * sizeof(struct iovec) != transferred)
2366 return -EIO;
2367
2368 memcpy(dst, src, transferred);
2369 return 0;
2370}
2371
Miklos Szeredi75727772010-11-30 16:39:27 +01002372/* Make sure iov_length() won't overflow */
2373static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2374{
2375 size_t n;
2376 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2377
Zach Brownfb6ccff2012-07-24 12:10:11 -07002378 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002379 if (iov->iov_len > (size_t) max)
2380 return -ENOMEM;
2381 max -= iov->iov_len;
2382 }
2383 return 0;
2384}
2385
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002386static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2387 void *src, size_t transferred, unsigned count,
2388 bool is_compat)
2389{
2390 unsigned i;
2391 struct fuse_ioctl_iovec *fiov = src;
2392
2393 if (fc->minor < 16) {
2394 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2395 count, is_compat);
2396 }
2397
2398 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2399 return -EIO;
2400
2401 for (i = 0; i < count; i++) {
2402 /* Did the server supply an inappropriate value? */
2403 if (fiov[i].base != (unsigned long) fiov[i].base ||
2404 fiov[i].len != (unsigned long) fiov[i].len)
2405 return -EIO;
2406
2407 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2408 dst[i].iov_len = (size_t) fiov[i].len;
2409
2410#ifdef CONFIG_COMPAT
2411 if (is_compat &&
2412 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2413 (compat_size_t) dst[i].iov_len != fiov[i].len))
2414 return -EIO;
2415#endif
2416 }
2417
2418 return 0;
2419}
2420
2421
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002422/*
Tejun Heo59efec72008-11-26 12:03:55 +01002423 * For ioctls, there is no generic way to determine how much memory
2424 * needs to be read and/or written. Furthermore, ioctls are allowed
2425 * to dereference the passed pointer, so the parameter requires deep
2426 * copying but FUSE has no idea whatsoever about what to copy in or
2427 * out.
2428 *
2429 * This is solved by allowing FUSE server to retry ioctl with
2430 * necessary in/out iovecs. Let's assume the ioctl implementation
2431 * needs to read in the following structure.
2432 *
2433 * struct a {
2434 * char *buf;
2435 * size_t buflen;
2436 * }
2437 *
2438 * On the first callout to FUSE server, inarg->in_size and
2439 * inarg->out_size will be NULL; then, the server completes the ioctl
2440 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2441 * the actual iov array to
2442 *
2443 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2444 *
2445 * which tells FUSE to copy in the requested area and retry the ioctl.
2446 * On the second round, the server has access to the structure and
2447 * from that it can tell what to look for next, so on the invocation,
2448 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2449 *
2450 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2451 * { .iov_base = a.buf, .iov_len = a.buflen } }
2452 *
2453 * FUSE will copy both struct a and the pointed buffer from the
2454 * process doing the ioctl and retry ioctl with both struct a and the
2455 * buffer.
2456 *
2457 * This time, FUSE server has everything it needs and completes ioctl
2458 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2459 *
2460 * Copying data out works the same way.
2461 *
2462 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2463 * automatically initializes in and out iovs by decoding @cmd with
2464 * _IOC_* macros and the server is not allowed to request RETRY. This
2465 * limits ioctl data transfers to well-formed ioctls and is the forced
2466 * behavior for all FUSE servers.
2467 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002468long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2469 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002470{
Tejun Heo59efec72008-11-26 12:03:55 +01002471 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002472 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002473 struct fuse_ioctl_in inarg = {
2474 .fh = ff->fh,
2475 .cmd = cmd,
2476 .arg = arg,
2477 .flags = flags
2478 };
2479 struct fuse_ioctl_out outarg;
2480 struct fuse_req *req = NULL;
2481 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002482 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002483 struct iovec *in_iov = NULL, *out_iov = NULL;
2484 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2485 size_t in_size, out_size, transferred;
2486 int err;
2487
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002488#if BITS_PER_LONG == 32
2489 inarg.flags |= FUSE_IOCTL_32BIT;
2490#else
2491 if (flags & FUSE_IOCTL_COMPAT)
2492 inarg.flags |= FUSE_IOCTL_32BIT;
2493#endif
2494
Tejun Heo59efec72008-11-26 12:03:55 +01002495 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002496 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002497
Tejun Heo59efec72008-11-26 12:03:55 +01002498 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002499 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002500 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002501 if (!pages || !iov_page)
2502 goto out;
2503
2504 /*
2505 * If restricted, initialize IO parameters as encoded in @cmd.
2506 * RETRY from server is not allowed.
2507 */
2508 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002509 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002510
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002511 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002512 iov->iov_len = _IOC_SIZE(cmd);
2513
2514 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2515 in_iov = iov;
2516 in_iovs = 1;
2517 }
2518
2519 if (_IOC_DIR(cmd) & _IOC_READ) {
2520 out_iov = iov;
2521 out_iovs = 1;
2522 }
2523 }
2524
2525 retry:
2526 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2527 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2528
2529 /*
2530 * Out data can be used either for actual out data or iovs,
2531 * make sure there always is at least one page.
2532 */
2533 out_size = max_t(size_t, out_size, PAGE_SIZE);
2534 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2535
2536 /* make sure there are enough buffer pages and init request with them */
2537 err = -ENOMEM;
2538 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2539 goto out;
2540 while (num_pages < max_pages) {
2541 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2542 if (!pages[num_pages])
2543 goto out;
2544 num_pages++;
2545 }
2546
Maxim Patlasov54b96672012-10-26 19:49:13 +04002547 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002548 if (IS_ERR(req)) {
2549 err = PTR_ERR(req);
2550 req = NULL;
2551 goto out;
2552 }
2553 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2554 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002555 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002556
2557 /* okay, let's send it to the client */
2558 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002559 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002560 req->in.numargs = 1;
2561 req->in.args[0].size = sizeof(inarg);
2562 req->in.args[0].value = &inarg;
2563 if (in_size) {
2564 req->in.numargs++;
2565 req->in.args[1].size = in_size;
2566 req->in.argpages = 1;
2567
2568 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2569 false);
2570 if (err)
2571 goto out;
2572 }
2573
2574 req->out.numargs = 2;
2575 req->out.args[0].size = sizeof(outarg);
2576 req->out.args[0].value = &outarg;
2577 req->out.args[1].size = out_size;
2578 req->out.argpages = 1;
2579 req->out.argvar = 1;
2580
Tejun Heob93f8582008-11-26 12:03:55 +01002581 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002582 err = req->out.h.error;
2583 transferred = req->out.args[1].size;
2584 fuse_put_request(fc, req);
2585 req = NULL;
2586 if (err)
2587 goto out;
2588
2589 /* did it ask for retry? */
2590 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002591 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002592
2593 /* no retry if in restricted mode */
2594 err = -EIO;
2595 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2596 goto out;
2597
2598 in_iovs = outarg.in_iovs;
2599 out_iovs = outarg.out_iovs;
2600
2601 /*
2602 * Make sure things are in boundary, separate checks
2603 * are to protect against overflow.
2604 */
2605 err = -ENOMEM;
2606 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2607 out_iovs > FUSE_IOCTL_MAX_IOV ||
2608 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2609 goto out;
2610
Cong Wang2408f6e2011-11-25 23:14:30 +08002611 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002612 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002613 transferred, in_iovs + out_iovs,
2614 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002615 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002616 if (err)
2617 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002618
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002619 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002620 out_iov = in_iov + in_iovs;
2621
Miklos Szeredi75727772010-11-30 16:39:27 +01002622 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2623 if (err)
2624 goto out;
2625
2626 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2627 if (err)
2628 goto out;
2629
Tejun Heo59efec72008-11-26 12:03:55 +01002630 goto retry;
2631 }
2632
2633 err = -EIO;
2634 if (transferred > inarg.out_size)
2635 goto out;
2636
2637 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2638 out:
2639 if (req)
2640 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002641 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002642 while (num_pages)
2643 __free_page(pages[--num_pages]);
2644 kfree(pages);
2645
2646 return err ? err : outarg.result;
2647}
Tejun Heo08cbf542009-04-14 10:54:53 +09002648EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002649
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002650long fuse_ioctl_common(struct file *file, unsigned int cmd,
2651 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002652{
Al Viro6131ffa2013-02-27 16:59:05 -05002653 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002654 struct fuse_conn *fc = get_fuse_conn(inode);
2655
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002656 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002657 return -EACCES;
2658
2659 if (is_bad_inode(inode))
2660 return -EIO;
2661
2662 return fuse_do_ioctl(file, cmd, arg, flags);
2663}
2664
Tejun Heo59efec72008-11-26 12:03:55 +01002665static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2666 unsigned long arg)
2667{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002668 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002669}
2670
2671static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2672 unsigned long arg)
2673{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002674 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002675}
2676
Tejun Heo95668a62008-11-26 12:03:55 +01002677/*
2678 * All files which have been polled are linked to RB tree
2679 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2680 * find the matching one.
2681 */
2682static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2683 struct rb_node **parent_out)
2684{
2685 struct rb_node **link = &fc->polled_files.rb_node;
2686 struct rb_node *last = NULL;
2687
2688 while (*link) {
2689 struct fuse_file *ff;
2690
2691 last = *link;
2692 ff = rb_entry(last, struct fuse_file, polled_node);
2693
2694 if (kh < ff->kh)
2695 link = &last->rb_left;
2696 else if (kh > ff->kh)
2697 link = &last->rb_right;
2698 else
2699 return link;
2700 }
2701
2702 if (parent_out)
2703 *parent_out = last;
2704 return link;
2705}
2706
2707/*
2708 * The file is about to be polled. Make sure it's on the polled_files
2709 * RB tree. Note that files once added to the polled_files tree are
2710 * not removed before the file is released. This is because a file
2711 * polled once is likely to be polled again.
2712 */
2713static void fuse_register_polled_file(struct fuse_conn *fc,
2714 struct fuse_file *ff)
2715{
2716 spin_lock(&fc->lock);
2717 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002718 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002719
2720 link = fuse_find_polled_node(fc, ff->kh, &parent);
2721 BUG_ON(*link);
2722 rb_link_node(&ff->polled_node, parent, link);
2723 rb_insert_color(&ff->polled_node, &fc->polled_files);
2724 }
2725 spin_unlock(&fc->lock);
2726}
2727
Tejun Heo08cbf542009-04-14 10:54:53 +09002728unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002729{
Tejun Heo95668a62008-11-26 12:03:55 +01002730 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002731 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002732 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2733 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002734 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002735 int err;
2736
2737 if (fc->no_poll)
2738 return DEFAULT_POLLMASK;
2739
2740 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002741 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002742
2743 /*
2744 * Ask for notification iff there's someone waiting for it.
2745 * The client may ignore the flag and always notify.
2746 */
2747 if (waitqueue_active(&ff->poll_wait)) {
2748 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2749 fuse_register_polled_file(fc, ff);
2750 }
2751
Miklos Szeredi70781872014-12-12 09:49:05 +01002752 args.in.h.opcode = FUSE_POLL;
2753 args.in.h.nodeid = ff->nodeid;
2754 args.in.numargs = 1;
2755 args.in.args[0].size = sizeof(inarg);
2756 args.in.args[0].value = &inarg;
2757 args.out.numargs = 1;
2758 args.out.args[0].size = sizeof(outarg);
2759 args.out.args[0].value = &outarg;
2760 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002761
2762 if (!err)
2763 return outarg.revents;
2764 if (err == -ENOSYS) {
2765 fc->no_poll = 1;
2766 return DEFAULT_POLLMASK;
2767 }
2768 return POLLERR;
2769}
Tejun Heo08cbf542009-04-14 10:54:53 +09002770EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002771
2772/*
2773 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2774 * wakes up the poll waiters.
2775 */
2776int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2777 struct fuse_notify_poll_wakeup_out *outarg)
2778{
2779 u64 kh = outarg->kh;
2780 struct rb_node **link;
2781
2782 spin_lock(&fc->lock);
2783
2784 link = fuse_find_polled_node(fc, kh, NULL);
2785 if (*link) {
2786 struct fuse_file *ff;
2787
2788 ff = rb_entry(*link, struct fuse_file, polled_node);
2789 wake_up_interruptible_sync(&ff->poll_wait);
2790 }
2791
2792 spin_unlock(&fc->lock);
2793 return 0;
2794}
2795
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002796static void fuse_do_truncate(struct file *file)
2797{
2798 struct inode *inode = file->f_mapping->host;
2799 struct iattr attr;
2800
2801 attr.ia_valid = ATTR_SIZE;
2802 attr.ia_size = i_size_read(inode);
2803
2804 attr.ia_file = file;
2805 attr.ia_valid |= ATTR_FILE;
2806
2807 fuse_do_setattr(inode, &attr, file);
2808}
2809
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002810static inline loff_t fuse_round_up(loff_t off)
2811{
2812 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2813}
2814
Anand Avati4273b792012-02-17 12:46:25 -05002815static ssize_t
Omar Sandoval22c61862015-03-16 04:33:53 -07002816fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
Anand Avati4273b792012-02-17 12:46:25 -05002817{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002818 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002819 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002820 struct file *file = iocb->ki_filp;
2821 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002822 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002823 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002824 struct inode *inode;
2825 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002826 size_t count = iov_iter_count(iter);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002827 struct fuse_io_priv *io;
Robert Doebbelin32b98072016-03-07 09:50:56 +01002828 bool is_sync = is_sync_kiocb(iocb);
Anand Avati4273b792012-02-17 12:46:25 -05002829
Anand Avati4273b792012-02-17 12:46:25 -05002830 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002831 inode = file->f_mapping->host;
2832 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002833
Omar Sandoval6f673762015-03-16 04:33:52 -07002834 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002835 return 0;
2836
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002837 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002838 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002839 if (offset >= i_size)
2840 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002841 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2842 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002843 }
2844
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002845 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002846 if (!io)
2847 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002848 spin_lock_init(&io->lock);
Seth Forshee37bd8c82016-03-11 10:35:34 -06002849 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002850 io->reqs = 1;
2851 io->bytes = -1;
2852 io->size = 0;
2853 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002854 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002855 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002856 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002857 /*
2858 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002859 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002860 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002861 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002862 io->iocb = iocb;
2863
2864 /*
2865 * We cannot asynchronously extend the size of a file. We have no method
2866 * to wait on real async I/O requests, so we must submit this request
2867 * synchronously.
2868 */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002869 if (!is_sync && (offset + count > i_size) &&
Omar Sandoval6f673762015-03-16 04:33:52 -07002870 iov_iter_rw(iter) == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002871 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002872
Seth Forshee37bd8c82016-03-11 10:35:34 -06002873 if (io->async && is_sync) {
2874 /*
2875 * Additional reference to keep io around after
2876 * calling fuse_aio_complete()
2877 */
2878 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002879 io->done = &wait;
Seth Forshee37bd8c82016-03-11 10:35:34 -06002880 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002881
Omar Sandoval6f673762015-03-16 04:33:52 -07002882 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002883 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002884 fuse_invalidate_attr(inode);
2885 } else {
Al Virod22a9432014-03-16 15:50:47 -04002886 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002887 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002888
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 if (io->async) {
2890 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2891
2892 /* we have a non-extending, async request, so return */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002893 if (!is_sync)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 return -EIOCBQUEUED;
2895
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002896 wait_for_completion(&wait);
2897 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 }
2899
Seth Forshee37bd8c82016-03-11 10:35:34 -06002900 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002901
Omar Sandoval6f673762015-03-16 04:33:52 -07002902 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002903 if (ret > 0)
2904 fuse_write_update_size(inode, pos);
2905 else if (ret < 0 && offset + count > i_size)
2906 fuse_do_truncate(file);
2907 }
Anand Avati4273b792012-02-17 12:46:25 -05002908
2909 return ret;
2910}
2911
Miklos Szeredicdadb112012-11-10 16:55:56 +01002912static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2913 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002914{
2915 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002916 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002917 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002918 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002919 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002920 struct fuse_fallocate_in inarg = {
2921 .fh = ff->fh,
2922 .offset = offset,
2923 .length = length,
2924 .mode = mode
2925 };
2926 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002927 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2928 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002929
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002930 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2931 return -EOPNOTSUPP;
2932
Miklos Szeredi519c6042012-04-26 10:56:36 +02002933 if (fc->no_fallocate)
2934 return -EOPNOTSUPP;
2935
Maxim Patlasov14c14412013-06-13 12:16:39 +04002936 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002937 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002938 if (mode & FALLOC_FL_PUNCH_HOLE) {
2939 loff_t endbyte = offset + length - 1;
2940 err = filemap_write_and_wait_range(inode->i_mapping,
2941 offset, endbyte);
2942 if (err)
2943 goto out;
2944
2945 fuse_sync_writes(inode);
2946 }
Brian Foster3634a632013-05-17 09:30:32 -04002947 }
2948
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002949 if (!(mode & FALLOC_FL_KEEP_SIZE))
2950 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2951
Miklos Szeredi70781872014-12-12 09:49:05 +01002952 args.in.h.opcode = FUSE_FALLOCATE;
2953 args.in.h.nodeid = ff->nodeid;
2954 args.in.numargs = 1;
2955 args.in.args[0].size = sizeof(inarg);
2956 args.in.args[0].value = &inarg;
2957 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002958 if (err == -ENOSYS) {
2959 fc->no_fallocate = 1;
2960 err = -EOPNOTSUPP;
2961 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002962 if (err)
2963 goto out;
2964
2965 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002966 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2967 bool changed = fuse_write_update_size(inode, offset + length);
2968
Miklos Szeredi93d22692014-04-28 14:19:22 +02002969 if (changed && fc->writeback_cache)
2970 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002971 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002972
2973 if (mode & FALLOC_FL_PUNCH_HOLE)
2974 truncate_pagecache_range(inode, offset, offset + length - 1);
2975
2976 fuse_invalidate_attr(inode);
2977
Brian Foster3634a632013-05-17 09:30:32 -04002978out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002979 if (!(mode & FALLOC_FL_KEEP_SIZE))
2980 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2981
Maxim Patlasovbde52782013-09-13 19:19:54 +04002982 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002983 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002984
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002985 return err;
2986}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002987
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002988static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002989 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04002990 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04002991 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002992 .mmap = fuse_file_mmap,
2993 .open = fuse_open,
2994 .flush = fuse_flush,
2995 .release = fuse_release,
2996 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002997 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002998 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002999 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003000 .unlocked_ioctl = fuse_file_ioctl,
3001 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003002 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003003 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003004};
3005
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003006static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003007 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003008 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003009 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003010 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003011 .open = fuse_open,
3012 .flush = fuse_flush,
3013 .release = fuse_release,
3014 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003015 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003016 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003017 .unlocked_ioctl = fuse_file_ioctl,
3018 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003019 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003020 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003021 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003022};
3023
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003024static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003025 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003026 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003027 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003028 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003029 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003030 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003031 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003032 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003033 .write_begin = fuse_write_begin,
3034 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003035};
3036
3037void fuse_init_file_inode(struct inode *inode)
3038{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003039 inode->i_fop = &fuse_file_operations;
3040 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003041}