blob: d40c2451487cb2772bc72a3c5128aaef38599595 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040018#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070022
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020023static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010027 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070030 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010033 args.in.h.opcode = opcode;
34 args.in.h.nodeid = nodeid;
35 args.in.numargs = 1;
36 args.in.args[0].size = sizeof(inarg);
37 args.in.args[0].value = &inarg;
38 args.out.numargs = 1;
39 args.out.args[0].size = sizeof(*outargp);
40 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080041
Miklos Szeredi70781872014-12-12 09:49:05 +010042 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080043}
44
Tejun Heoacf99432008-11-26 12:03:55 +010045struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080046{
47 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090048
Mateusz Jurczyk7271d132017-06-07 12:26:49 +020049 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090050 if (unlikely(!ff))
51 return NULL;
52
Miklos Szeredida5e4712009-04-28 16:56:36 +020053 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040054 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090055 if (unlikely(!ff->reserved_req)) {
56 kfree(ff);
57 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080058 }
Tejun Heo6b2db282009-04-14 10:54:49 +090059
60 INIT_LIST_HEAD(&ff->write_entry);
61 atomic_set(&ff->count, 0);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
64
65 spin_lock(&fc->lock);
66 ff->kh = ++fc->khctr;
67 spin_unlock(&fc->lock);
68
Miklos Szeredifd72faa2005-11-07 00:59:51 -080069 return ff;
70}
71
72void fuse_file_free(struct fuse_file *ff)
73{
Miklos Szeredi33649c92006-06-25 05:48:52 -070074 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 kfree(ff);
76}
77
Miklos Szeredic7b71432009-04-28 16:56:37 +020078struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070079{
80 atomic_inc(&ff->count);
81 return ff;
82}
83
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010084static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010086 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010087}
88
89static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070090{
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020093
Andrew Gallagher7678ac52013-11-05 16:05:52 +010094 if (ff->fc->no_open) {
95 /*
96 * Drop the release request when client does not
97 * implement 'open'
98 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +020099 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100100 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100101 fuse_put_request(ff->fc, req);
102 } else if (sync) {
Miklos 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
Ashish Samant1b6a8632017-07-12 19:26:58 -0700628 fuse_release_user_pages(req, io->should_dirty);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400629
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)) {
Kirill Tkhai84c7c902018-07-19 15:49:39 +0300882 unlock_page(page);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400883 fuse_put_request(fc, req);
884 return -EIO;
885 }
886
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200887 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700888 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400889 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100890 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400891 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700892 return 0;
893}
894
895static int fuse_readpages(struct file *file, struct address_space *mapping,
896 struct list_head *pages, unsigned nr_pages)
897{
898 struct inode *inode = mapping->host;
899 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700900 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700901 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400902 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800903
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700904 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800905 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800906 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800907
Miklos Szeredia6643092007-11-28 16:22:00 -0800908 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700909 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400910 if (fc->async_read)
911 data.req = fuse_get_req_for_background(fc, nr_alloc);
912 else
913 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400914 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700915 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700916 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800917 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700918
919 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700920 if (!err) {
921 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200922 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700923 else
924 fuse_put_request(fc, data.req);
925 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800926out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700927 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700928}
929
Al Viro37c20f12014-04-02 14:47:09 -0400930static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931{
932 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400933 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800934
Brian Fostera8894272012-07-16 15:23:50 -0400935 /*
936 * In auto invalidate mode, always update attributes on read.
937 * Otherwise, only update if we attempt to read past EOF (to ensure
938 * i_size is up to date).
939 */
940 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400941 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800943 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
944 if (err)
945 return err;
946 }
947
Al Viro37c20f12014-04-02 14:47:09 -0400948 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800949}
950
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200951static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200952 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700953{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700954 struct fuse_write_in *inarg = &req->misc.write.in;
955 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700956
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700957 inarg->fh = ff->fh;
958 inarg->offset = pos;
959 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200961 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700962 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200963 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700964 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
965 else
966 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700967 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700968 req->in.args[1].size = count;
969 req->out.numargs = 1;
970 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700971 req->out.args[0].value = outarg;
972}
973
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400974static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200975 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700976{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400977 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200978 struct fuse_file *ff = file->private_data;
979 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200980 struct fuse_write_in *inarg = &req->misc.write.in;
981
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200982 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200983 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700984 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700985 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
986 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
987 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400988
989 if (io->async)
990 return fuse_async_req_send(fc, req, count, io);
991
Tejun Heob93f8582008-11-26 12:03:55 +0100992 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700993 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700994}
995
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400996bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700997{
998 struct fuse_conn *fc = get_fuse_conn(inode);
999 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001000 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001001
1002 spin_lock(&fc->lock);
1003 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001004 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001005 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001006 ret = true;
1007 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001009
1010 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001011}
1012
Nick Pigginea9b9902008-04-30 00:54:42 -07001013static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1014 struct inode *inode, loff_t pos,
1015 size_t count)
1016{
1017 size_t res;
1018 unsigned offset;
1019 unsigned i;
Seth Forshee37bd8c82016-03-11 10:35:34 -06001020 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001021
1022 for (i = 0; i < req->num_pages; i++)
1023 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1024
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001025 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001026
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001027 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001028 count = res;
1029 for (i = 0; i < req->num_pages; i++) {
1030 struct page *page = req->pages[i];
1031
1032 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1033 SetPageUptodate(page);
1034
1035 if (count > PAGE_CACHE_SIZE - offset)
1036 count -= PAGE_CACHE_SIZE - offset;
1037 else
1038 count = 0;
1039 offset = 0;
1040
1041 unlock_page(page);
1042 page_cache_release(page);
1043 }
1044
1045 return res;
1046}
1047
1048static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1049 struct address_space *mapping,
1050 struct iov_iter *ii, loff_t pos)
1051{
1052 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1053 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1054 size_t count = 0;
1055 int err;
1056
Miklos Szeredif4975c62009-04-02 14:25:34 +02001057 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001058 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001059
1060 do {
1061 size_t tmp;
1062 struct page *page;
1063 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1064 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1065 iov_iter_count(ii));
1066
1067 bytes = min_t(size_t, bytes, fc->max_write - count);
1068
1069 again:
1070 err = -EFAULT;
1071 if (iov_iter_fault_in_readable(ii, bytes))
1072 break;
1073
1074 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001075 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001076 if (!page)
1077 break;
1078
anfei zhou931e80e2010-02-02 13:44:02 -08001079 if (mapping_writably_mapped(mapping))
1080 flush_dcache_page(page);
1081
Nick Pigginea9b9902008-04-30 00:54:42 -07001082 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 flush_dcache_page(page);
1084
Roman Gushchin3ca81382015-10-12 16:33:44 +03001085 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001086 if (!tmp) {
1087 unlock_page(page);
1088 page_cache_release(page);
1089 bytes = min(bytes, iov_iter_single_seg_count(ii));
1090 goto again;
1091 }
1092
1093 err = 0;
1094 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001095 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 req->num_pages++;
1097
Nick Pigginea9b9902008-04-30 00:54:42 -07001098 count += tmp;
1099 pos += tmp;
1100 offset += tmp;
1101 if (offset == PAGE_CACHE_SIZE)
1102 offset = 0;
1103
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001104 if (!fc->big_writes)
1105 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001106 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001107 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001108
1109 return count > 0 ? count : err;
1110}
1111
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001112static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1113{
1114 return min_t(unsigned,
1115 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1116 (pos >> PAGE_CACHE_SHIFT) + 1,
1117 FUSE_MAX_PAGES_PER_REQ);
1118}
1119
Nick Pigginea9b9902008-04-30 00:54:42 -07001120static ssize_t fuse_perform_write(struct file *file,
1121 struct address_space *mapping,
1122 struct iov_iter *ii, loff_t pos)
1123{
1124 struct inode *inode = mapping->host;
1125 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001126 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001127 int err = 0;
1128 ssize_t res = 0;
1129
1130 if (is_bad_inode(inode))
1131 return -EIO;
1132
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001133 if (inode->i_size < pos + iov_iter_count(ii))
1134 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1135
Nick Pigginea9b9902008-04-30 00:54:42 -07001136 do {
1137 struct fuse_req *req;
1138 ssize_t count;
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001139 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001140
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001141 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001142 if (IS_ERR(req)) {
1143 err = PTR_ERR(req);
1144 break;
1145 }
1146
1147 count = fuse_fill_write_pages(req, mapping, ii, pos);
1148 if (count <= 0) {
1149 err = count;
1150 } else {
1151 size_t num_written;
1152
1153 num_written = fuse_send_write_pages(req, file, inode,
1154 pos, count);
1155 err = req->out.h.error;
1156 if (!err) {
1157 res += num_written;
1158 pos += num_written;
1159
1160 /* break out of the loop on short write */
1161 if (num_written != count)
1162 err = -EIO;
1163 }
1164 }
1165 fuse_put_request(fc, req);
1166 } while (!err && iov_iter_count(ii));
1167
1168 if (res > 0)
1169 fuse_write_update_size(inode, pos);
1170
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001171 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001172 fuse_invalidate_attr(inode);
1173
1174 return res > 0 ? res : err;
1175}
1176
Al Viro84c3d552014-04-03 14:33:23 -04001177static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001178{
1179 struct file *file = iocb->ki_filp;
1180 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001182 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001183 struct inode *inode = mapping->host;
1184 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001185 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001186
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001187 if (get_fuse_conn(inode)->writeback_cache) {
1188 /* Update size (EOF optimization) and mode (SUID clearing) */
1189 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1190 if (err)
1191 return err;
1192
Al Viro84c3d552014-04-03 14:33:23 -04001193 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001194 }
1195
Nick Pigginea9b9902008-04-30 00:54:42 -07001196 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001197
1198 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001199 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001200
Al Viro3309dd02015-04-09 12:55:47 -04001201 err = generic_write_checks(iocb, from);
1202 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001203 goto out;
1204
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001205 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001206 if (err)
1207 goto out;
1208
Josef Bacikc3b2da32012-03-26 09:59:21 -04001209 err = file_update_time(file);
1210 if (err)
1211 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001212
Al Viro2ba48ce2015-04-09 13:52:01 -04001213 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001214 loff_t pos = iocb->ki_pos;
Al Viro84c3d552014-04-03 14:33:23 -04001215 written = generic_file_direct_write(iocb, from, pos);
1216 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001217 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001218
Anand Avati4273b792012-02-17 12:46:25 -05001219 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001220
Al Viro84c3d552014-04-03 14:33:23 -04001221 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001222 if (written_buffered < 0) {
1223 err = written_buffered;
1224 goto out;
1225 }
1226 endbyte = pos + written_buffered - 1;
1227
1228 err = filemap_write_and_wait_range(file->f_mapping, pos,
1229 endbyte);
1230 if (err)
1231 goto out;
1232
1233 invalidate_mapping_pages(file->f_mapping,
1234 pos >> PAGE_CACHE_SHIFT,
1235 endbyte >> PAGE_CACHE_SHIFT);
1236
1237 written += written_buffered;
1238 iocb->ki_pos = pos + written_buffered;
1239 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001240 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001241 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001242 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001243 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001244out:
1245 current->backing_dev_info = NULL;
1246 mutex_unlock(&inode->i_mutex);
1247
1248 return written ? written : err;
1249}
1250
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001251static inline void fuse_page_descs_length_init(struct fuse_req *req,
1252 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001253{
1254 int i;
1255
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001256 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001257 req->page_descs[i].length = PAGE_SIZE -
1258 req->page_descs[i].offset;
1259}
1260
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001261static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1262{
1263 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1264}
1265
1266static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1267 size_t max_size)
1268{
1269 return min(iov_iter_single_seg_count(ii), max_size);
1270}
1271
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001272static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001273 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001274{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001275 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001276
Miklos Szeredif4975c62009-04-02 14:25:34 +02001277 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001278 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001279 unsigned long user_addr = fuse_get_user_addr(ii);
1280 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1281
Miklos Szeredif4975c62009-04-02 14:25:34 +02001282 if (write)
1283 req->in.args[1].value = (void *) user_addr;
1284 else
1285 req->out.args[0].value = (void *) user_addr;
1286
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001287 iov_iter_advance(ii, frag_size);
1288 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001289 return 0;
1290 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001291
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001292 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001294 size_t start;
Al Viroc9c37e22014-03-16 16:08:30 -04001295 ssize_t ret = iov_iter_get_pages(ii,
1296 &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001297 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001298 req->max_pages - req->num_pages,
1299 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001300 if (ret < 0)
1301 return ret;
1302
Al Viroc9c37e22014-03-16 16:08:30 -04001303 iov_iter_advance(ii, ret);
1304 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001305
Al Viroc9c37e22014-03-16 16:08:30 -04001306 ret += start;
1307 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1308
1309 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310 fuse_page_descs_length_init(req, req->num_pages, npages);
1311
1312 req->num_pages += npages;
1313 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001314 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001315 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001316
1317 if (write)
1318 req->in.argpages = 1;
1319 else
1320 req->out.argpages = 1;
1321
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001322 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001323
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001324 return 0;
1325}
1326
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001327static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1328{
Al Virof67da302014-03-19 01:16:16 -04001329 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001330}
1331
Al Virod22a9432014-03-16 15:50:47 -04001332ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1333 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001334{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001335 int write = flags & FUSE_DIO_WRITE;
1336 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
Ashish Samant1b6a8632017-07-12 19:26:58 -07001364 io->should_dirty = !write && iter_is_iovec(iter);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001365 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001366 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001367 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001368 size_t nbytes = min(count, nmax);
Al Virod22a9432014-03-16 15:50:47 -04001369 int err = fuse_get_user_pages(req, iter, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 if (err) {
1371 res = err;
1372 break;
1373 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001374
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001376 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001377 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001378 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001379
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001380 if (!io->async)
Ashish Samant1b6a8632017-07-12 19:26:58 -07001381 fuse_release_user_pages(req, io->should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001382 if (req->out.h.error) {
1383 if (!res)
1384 res = req->out.h.error;
1385 break;
1386 } else if (nres > nbytes) {
1387 res = -EIO;
1388 break;
1389 }
1390 count -= nres;
1391 res += nres;
1392 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001393 if (nres != nbytes)
1394 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001395 if (count) {
1396 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001397 if (io->async)
1398 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001399 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001400 else
Al Virod22a9432014-03-16 15:50:47 -04001401 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001402 if (IS_ERR(req))
1403 break;
1404 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001405 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001406 if (!IS_ERR(req))
1407 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001408 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001409 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001410
1411 return res;
1412}
Tejun Heo08cbf542009-04-14 10:54:53 +09001413EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001414
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001415static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001416 struct iov_iter *iter,
1417 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001419 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001420 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001421 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001422
1423 if (is_bad_inode(inode))
1424 return -EIO;
1425
Al Virod22a9432014-03-16 15:50:47 -04001426 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001427
1428 fuse_invalidate_attr(inode);
1429
1430 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001431}
1432
Al Viro15316262015-03-30 22:08:36 -04001433static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001434{
Seth Forshee37bd8c82016-03-11 10:35:34 -06001435 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001436 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001437}
1438
Al Viro15316262015-03-30 22:08:36 -04001439static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440{
Al Viro15316262015-03-30 22:08:36 -04001441 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001442 struct inode *inode = file_inode(file);
Seth Forshee37bd8c82016-03-11 10:35:34 -06001443 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001444 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001445
1446 if (is_bad_inode(inode))
1447 return -EIO;
1448
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001449 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001450 mutex_lock(&inode->i_mutex);
Al Viro3309dd02015-04-09 12:55:47 -04001451 res = generic_write_checks(iocb, from);
1452 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001453 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001454 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001455 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001456 fuse_write_update_size(inode, iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001457 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001458
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001459 return res;
1460}
1461
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001462static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001463{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001464 int i;
1465
1466 for (i = 0; i < req->num_pages; i++)
1467 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001468
1469 if (req->ff)
1470 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001471}
1472
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001473static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001474{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001475 struct inode *inode = req->inode;
1476 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001477 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001478 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001479
1480 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001481 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001482 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001483 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001484 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001485 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486 wake_up(&fi->page_waitq);
1487}
1488
1489/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001490static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1491 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001492__releases(fc->lock)
1493__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001494{
1495 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001496 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001497 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001498
1499 if (!fc->connected)
1500 goto out_free;
1501
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001502 if (inarg->offset + data_size <= size) {
1503 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001505 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001506 } else {
1507 /* Got truncated off completely */
1508 goto out_free;
1509 }
1510
1511 req->in.args[1].size = inarg->size;
1512 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001513 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001514 return;
1515
1516 out_free:
1517 fuse_writepage_finish(fc, req);
1518 spin_unlock(&fc->lock);
1519 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001520 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001521 spin_lock(&fc->lock);
1522}
1523
1524/*
1525 * If fi->writectr is positive (no truncate or fsync going on) send
1526 * all queued writepage requests.
1527 *
1528 * Called with fc->lock
1529 */
1530void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001531__releases(fc->lock)
1532__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001533{
1534 struct fuse_conn *fc = get_fuse_conn(inode);
1535 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001536 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537 struct fuse_req *req;
1538
1539 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1540 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1541 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001542 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001543 }
1544}
1545
1546static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1547{
1548 struct inode *inode = req->inode;
1549 struct fuse_inode *fi = get_fuse_inode(inode);
1550
1551 mapping_set_error(inode->i_mapping, req->out.h.error);
1552 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001553 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001554 struct fuse_conn *fc = get_fuse_conn(inode);
1555 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001556 struct fuse_req *next = req->misc.write.next;
1557 req->misc.write.next = next->misc.write.next;
1558 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001559 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001560 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001561
1562 /*
1563 * Skip fuse_flush_writepages() to make it easy to crop requests
1564 * based on primary request size.
1565 *
1566 * 1st case (trivial): there are no concurrent activities using
1567 * fuse_set/release_nowrite. Then we're on safe side because
1568 * fuse_flush_writepages() would call fuse_send_writepage()
1569 * anyway.
1570 *
1571 * 2nd case: someone called fuse_set_nowrite and it is waiting
1572 * now for completion of all in-flight requests. This happens
1573 * rarely and no more than once per page, so this should be
1574 * okay.
1575 *
1576 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1577 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1578 * that fuse_set_nowrite returned implies that all in-flight
1579 * requests were completed along with all of their secondary
1580 * requests. Further primary requests are blocked by negative
1581 * writectr. Hence there cannot be any in-flight requests and
1582 * no invocations of fuse_writepage_end() while we're in
1583 * fuse_set_nowrite..fuse_release_nowrite section.
1584 */
1585 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001586 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001587 fi->writectr--;
1588 fuse_writepage_finish(fc, req);
1589 spin_unlock(&fc->lock);
1590 fuse_writepage_free(fc, req);
1591}
1592
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001593static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1594 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001595{
Miklos Szeredi72523422013-10-01 16:44:52 +02001596 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001597
1598 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001599 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001600 ff = list_entry(fi->write_files.next, struct fuse_file,
1601 write_entry);
1602 fuse_file_get(ff);
1603 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001604 spin_unlock(&fc->lock);
1605
1606 return ff;
1607}
1608
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001609static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1610 struct fuse_inode *fi)
1611{
1612 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1613 WARN_ON(!ff);
1614 return ff;
1615}
1616
1617int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1618{
1619 struct fuse_conn *fc = get_fuse_conn(inode);
1620 struct fuse_inode *fi = get_fuse_inode(inode);
1621 struct fuse_file *ff;
1622 int err;
1623
1624 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001625 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001626 if (ff)
1627 fuse_file_put(ff, 0);
1628
1629 return err;
1630}
1631
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001632static int fuse_writepage_locked(struct page *page)
1633{
1634 struct address_space *mapping = page->mapping;
1635 struct inode *inode = mapping->host;
1636 struct fuse_conn *fc = get_fuse_conn(inode);
1637 struct fuse_inode *fi = get_fuse_inode(inode);
1638 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001639 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001640 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001641
1642 set_page_writeback(page);
1643
Maxim Patlasov4250c062012-10-26 19:48:07 +04001644 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001645 if (!req)
1646 goto err;
1647
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001648 /* writeback always goes to bg_queue */
1649 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1651 if (!tmp_page)
1652 goto err_free;
1653
Miklos Szeredi72523422013-10-01 16:44:52 +02001654 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001655 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001656 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001657 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001658
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001659 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660
1661 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001662 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001663 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001664 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001665 req->num_pages = 1;
1666 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001667 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001668 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669 req->end = fuse_writepage_end;
1670 req->inode = inode;
1671
Tejun Heo93f78d82015-05-22 17:13:27 -04001672 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001673 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674
1675 spin_lock(&fc->lock);
1676 list_add(&req->writepages_entry, &fi->writepages);
1677 list_add_tail(&req->list, &fi->queued_writes);
1678 fuse_flush_writepages(inode);
1679 spin_unlock(&fc->lock);
1680
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001681 end_page_writeback(page);
1682
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683 return 0;
1684
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001685err_nofile:
1686 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001687err_free:
1688 fuse_request_free(req);
1689err:
1690 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001691 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001692}
1693
1694static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1695{
1696 int err;
1697
Miklos Szerediff17be02013-10-01 16:44:53 +02001698 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1699 /*
1700 * ->writepages() should be called for sync() and friends. We
1701 * should only get here on direct reclaim and then we are
1702 * allowed to skip a page which is already in flight
1703 */
1704 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1705
1706 redirty_page_for_writepage(wbc, page);
1707 return 0;
1708 }
1709
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001710 err = fuse_writepage_locked(page);
1711 unlock_page(page);
1712
1713 return err;
1714}
1715
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001716struct fuse_fill_wb_data {
1717 struct fuse_req *req;
1718 struct fuse_file *ff;
1719 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001720 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001721};
1722
1723static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1724{
1725 struct fuse_req *req = data->req;
1726 struct inode *inode = data->inode;
1727 struct fuse_conn *fc = get_fuse_conn(inode);
1728 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001729 int num_pages = req->num_pages;
1730 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001731
1732 req->ff = fuse_file_get(data->ff);
1733 spin_lock(&fc->lock);
1734 list_add_tail(&req->list, &fi->queued_writes);
1735 fuse_flush_writepages(inode);
1736 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001737
1738 for (i = 0; i < num_pages; i++)
1739 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001740}
1741
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001742static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1743 struct page *page)
1744{
1745 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1746 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1747 struct fuse_req *tmp;
1748 struct fuse_req *old_req;
1749 bool found = false;
1750 pgoff_t curr_index;
1751
1752 BUG_ON(new_req->num_pages != 0);
1753
1754 spin_lock(&fc->lock);
1755 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001756 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1757 BUG_ON(old_req->inode != new_req->inode);
1758 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1759 if (curr_index <= page->index &&
1760 page->index < curr_index + old_req->num_pages) {
1761 found = true;
1762 break;
1763 }
1764 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001765 if (!found) {
1766 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001768 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001769
Maxim Patlasovf6011082013-10-02 15:01:07 +04001770 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001771 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1772 BUG_ON(tmp->inode != new_req->inode);
1773 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1774 if (tmp->num_pages == 1 &&
1775 curr_index == page->index) {
1776 old_req = tmp;
1777 }
1778 }
1779
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001780 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001781 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001782
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001783 copy_highpage(old_req->pages[0], page);
1784 spin_unlock(&fc->lock);
1785
Tejun Heo93f78d82015-05-22 17:13:27 -04001786 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredibade8e52019-01-16 10:27:59 +01001787 dec_zone_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001788 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001789 fuse_writepage_free(fc, new_req);
1790 fuse_request_free(new_req);
1791 goto out;
1792 } else {
1793 new_req->misc.write.next = old_req->misc.write.next;
1794 old_req->misc.write.next = new_req;
1795 }
1796out_unlock:
1797 spin_unlock(&fc->lock);
1798out:
1799 return found;
1800}
1801
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001802static int fuse_writepages_fill(struct page *page,
1803 struct writeback_control *wbc, void *_data)
1804{
1805 struct fuse_fill_wb_data *data = _data;
1806 struct fuse_req *req = data->req;
1807 struct inode *inode = data->inode;
1808 struct fuse_conn *fc = get_fuse_conn(inode);
1809 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001810 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001811 int err;
1812
1813 if (!data->ff) {
1814 err = -EIO;
1815 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1816 if (!data->ff)
1817 goto out_unlock;
1818 }
1819
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001820 /*
1821 * Being under writeback is unlikely but possible. For example direct
1822 * read to an mmaped fuse file will set the page dirty twice; once when
1823 * the pages are faulted with get_user_pages(), and then after the read
1824 * completed.
1825 */
1826 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001827
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001828 if (req && req->num_pages &&
1829 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1830 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1831 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1832 fuse_writepages_send(data);
1833 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001834 }
1835 err = -ENOMEM;
1836 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1837 if (!tmp_page)
1838 goto out_unlock;
1839
1840 /*
1841 * The page must not be redirtied until the writeout is completed
1842 * (i.e. userspace has sent a reply to the write request). Otherwise
1843 * there could be more than one temporary page instance for each real
1844 * page.
1845 *
1846 * This is ensured by holding the page lock in page_mkwrite() while
1847 * checking fuse_page_is_writeback(). We already hold the page lock
1848 * since clear_page_dirty_for_io() and keep it held until we add the
1849 * request to the fi->writepages list and increment req->num_pages.
1850 * After this fuse_page_is_writeback() will indicate that the page is
1851 * under writeback, so we can release the page lock.
1852 */
1853 if (data->req == NULL) {
1854 struct fuse_inode *fi = get_fuse_inode(inode);
1855
1856 err = -ENOMEM;
1857 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1858 if (!req) {
1859 __free_page(tmp_page);
1860 goto out_unlock;
1861 }
1862
1863 fuse_write_fill(req, data->ff, page_offset(page), 0);
1864 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001865 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001866 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001867 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001868 req->num_pages = 0;
1869 req->end = fuse_writepage_end;
1870 req->inode = inode;
1871
1872 spin_lock(&fc->lock);
1873 list_add(&req->writepages_entry, &fi->writepages);
1874 spin_unlock(&fc->lock);
1875
1876 data->req = req;
1877 }
1878 set_page_writeback(page);
1879
1880 copy_highpage(tmp_page, page);
1881 req->pages[req->num_pages] = tmp_page;
1882 req->page_descs[req->num_pages].offset = 0;
1883 req->page_descs[req->num_pages].length = PAGE_SIZE;
1884
Tejun Heo93f78d82015-05-22 17:13:27 -04001885 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001886 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001887
1888 err = 0;
1889 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1890 end_page_writeback(page);
1891 data->req = NULL;
1892 goto out_unlock;
1893 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001894 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001895
1896 /*
1897 * Protected by fc->lock against concurrent access by
1898 * fuse_page_is_writeback().
1899 */
1900 spin_lock(&fc->lock);
1901 req->num_pages++;
1902 spin_unlock(&fc->lock);
1903
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001904out_unlock:
1905 unlock_page(page);
1906
1907 return err;
1908}
1909
1910static int fuse_writepages(struct address_space *mapping,
1911 struct writeback_control *wbc)
1912{
1913 struct inode *inode = mapping->host;
1914 struct fuse_fill_wb_data data;
1915 int err;
1916
1917 err = -EIO;
1918 if (is_bad_inode(inode))
1919 goto out;
1920
1921 data.inode = inode;
1922 data.req = NULL;
1923 data.ff = NULL;
1924
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001925 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001926 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1927 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001928 GFP_NOFS);
1929 if (!data.orig_pages)
1930 goto out;
1931
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001932 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1933 if (data.req) {
1934 /* Ignore errors if we can write at least one page */
1935 BUG_ON(!data.req->num_pages);
1936 fuse_writepages_send(&data);
1937 err = 0;
1938 }
1939 if (data.ff)
1940 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001941
1942 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001943out:
1944 return err;
1945}
1946
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001947/*
1948 * It's worthy to make sure that space is reserved on disk for the write,
1949 * but how to implement it without killing performance need more thinking.
1950 */
1951static int fuse_write_begin(struct file *file, struct address_space *mapping,
1952 loff_t pos, unsigned len, unsigned flags,
1953 struct page **pagep, void **fsdata)
1954{
1955 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001956 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001957 struct page *page;
1958 loff_t fsize;
1959 int err = -ENOMEM;
1960
1961 WARN_ON(!fc->writeback_cache);
1962
1963 page = grab_cache_page_write_begin(mapping, index, flags);
1964 if (!page)
1965 goto error;
1966
1967 fuse_wait_on_page_writeback(mapping->host, page->index);
1968
1969 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1970 goto success;
1971 /*
1972 * Check if the start this page comes after the end of file, in which
1973 * case the readpage can be optimized away.
1974 */
1975 fsize = i_size_read(mapping->host);
1976 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1977 size_t off = pos & ~PAGE_CACHE_MASK;
1978 if (off)
1979 zero_user_segment(page, 0, off);
1980 goto success;
1981 }
1982 err = fuse_do_readpage(file, page);
1983 if (err)
1984 goto cleanup;
1985success:
1986 *pagep = page;
1987 return 0;
1988
1989cleanup:
1990 unlock_page(page);
1991 page_cache_release(page);
1992error:
1993 return err;
1994}
1995
1996static int fuse_write_end(struct file *file, struct address_space *mapping,
1997 loff_t pos, unsigned len, unsigned copied,
1998 struct page *page, void *fsdata)
1999{
2000 struct inode *inode = page->mapping->host;
2001
Miklos Szeredib7321bc2016-08-18 09:10:44 +02002002 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2003 if (!copied)
2004 goto unlock;
2005
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002006 if (!PageUptodate(page)) {
2007 /* Zero any unwritten bytes at the end of the page */
2008 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2009 if (endoff)
2010 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2011 SetPageUptodate(page);
2012 }
2013
2014 fuse_write_update_size(inode, pos + copied);
2015 set_page_dirty(page);
Miklos Szeredib7321bc2016-08-18 09:10:44 +02002016
2017unlock:
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002018 unlock_page(page);
2019 page_cache_release(page);
2020
2021 return copied;
2022}
2023
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002024static int fuse_launder_page(struct page *page)
2025{
2026 int err = 0;
2027 if (clear_page_dirty_for_io(page)) {
2028 struct inode *inode = page->mapping->host;
2029 err = fuse_writepage_locked(page);
2030 if (!err)
2031 fuse_wait_on_page_writeback(inode, page->index);
2032 }
2033 return err;
2034}
2035
2036/*
2037 * Write back dirty pages now, because there may not be any suitable
2038 * open files later
2039 */
2040static void fuse_vma_close(struct vm_area_struct *vma)
2041{
2042 filemap_write_and_wait(vma->vm_file->f_mapping);
2043}
2044
2045/*
2046 * Wait for writeback against this page to complete before allowing it
2047 * to be marked dirty again, and hence written back again, possibly
2048 * before the previous writepage completed.
2049 *
2050 * Block here, instead of in ->writepage(), so that the userspace fs
2051 * can only block processes actually operating on the filesystem.
2052 *
2053 * Otherwise unprivileged userspace fs would be able to block
2054 * unrelated:
2055 *
2056 * - page migration
2057 * - sync(2)
2058 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2059 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002060static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002061{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002062 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002063 struct inode *inode = file_inode(vma->vm_file);
2064
2065 file_update_time(vma->vm_file);
2066 lock_page(page);
2067 if (page->mapping != inode->i_mapping) {
2068 unlock_page(page);
2069 return VM_FAULT_NOPAGE;
2070 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002071
2072 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002073 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002074}
2075
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002076static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002077 .close = fuse_vma_close,
2078 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002079 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002080 .page_mkwrite = fuse_page_mkwrite,
2081};
2082
2083static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2084{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002085 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2086 fuse_link_write_file(file);
2087
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002088 file_accessed(file);
2089 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002090 return 0;
2091}
2092
Miklos Szeredifc280c92009-04-02 14:25:35 +02002093static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2094{
2095 /* Can't provide the coherency needed for MAP_SHARED */
2096 if (vma->vm_flags & VM_MAYSHARE)
2097 return -ENODEV;
2098
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002099 invalidate_inode_pages2(file->f_mapping);
2100
Miklos Szeredifc280c92009-04-02 14:25:35 +02002101 return generic_file_mmap(file, vma);
2102}
2103
Miklos Szeredi71421252006-06-25 05:48:52 -07002104static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2105 struct file_lock *fl)
2106{
2107 switch (ffl->type) {
2108 case F_UNLCK:
2109 break;
2110
2111 case F_RDLCK:
2112 case F_WRLCK:
2113 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2114 ffl->end < ffl->start)
2115 return -EIO;
2116
2117 fl->fl_start = ffl->start;
2118 fl->fl_end = ffl->end;
2119 fl->fl_pid = ffl->pid;
2120 break;
2121
2122 default:
2123 return -EIO;
2124 }
2125 fl->fl_type = ffl->type;
2126 return 0;
2127}
2128
Miklos Szeredi70781872014-12-12 09:49:05 +01002129static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002130 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002131 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002132{
Al Viro6131ffa2013-02-27 16:59:05 -05002133 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002134 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002135 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002136
Miklos Szeredi70781872014-12-12 09:49:05 +01002137 memset(inarg, 0, sizeof(*inarg));
2138 inarg->fh = ff->fh;
2139 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2140 inarg->lk.start = fl->fl_start;
2141 inarg->lk.end = fl->fl_end;
2142 inarg->lk.type = fl->fl_type;
2143 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002144 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002145 inarg->lk_flags |= FUSE_LK_FLOCK;
2146 args->in.h.opcode = opcode;
2147 args->in.h.nodeid = get_node_id(inode);
2148 args->in.numargs = 1;
2149 args->in.args[0].size = sizeof(*inarg);
2150 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002151}
2152
2153static int fuse_getlk(struct file *file, struct file_lock *fl)
2154{
Al Viro6131ffa2013-02-27 16:59:05 -05002155 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002156 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002157 FUSE_ARGS(args);
2158 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002159 struct fuse_lk_out outarg;
2160 int err;
2161
Miklos Szeredi70781872014-12-12 09:49:05 +01002162 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2163 args.out.numargs = 1;
2164 args.out.args[0].size = sizeof(outarg);
2165 args.out.args[0].value = &outarg;
2166 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002167 if (!err)
2168 err = convert_fuse_file_lock(&outarg.lk, fl);
2169
2170 return err;
2171}
2172
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002173static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002174{
Al Viro6131ffa2013-02-27 16:59:05 -05002175 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002176 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002177 FUSE_ARGS(args);
2178 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002179 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2180 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2181 int err;
2182
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002183 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002184 /* NLM needs asynchronous locks, which we don't support yet */
2185 return -ENOLCK;
2186 }
2187
Miklos Szeredi71421252006-06-25 05:48:52 -07002188 /* Unlock on close is handled by the flush method */
2189 if (fl->fl_flags & FL_CLOSE)
2190 return 0;
2191
Miklos Szeredi70781872014-12-12 09:49:05 +01002192 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2193 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002194
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002195 /* locking is restartable */
2196 if (err == -EINTR)
2197 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002198
Miklos Szeredi71421252006-06-25 05:48:52 -07002199 return err;
2200}
2201
2202static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2203{
Al Viro6131ffa2013-02-27 16:59:05 -05002204 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002205 struct fuse_conn *fc = get_fuse_conn(inode);
2206 int err;
2207
Miklos Szeredi48e90762008-07-25 01:49:02 -07002208 if (cmd == F_CANCELLK) {
2209 err = 0;
2210 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002212 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002213 err = 0;
2214 } else
2215 err = fuse_getlk(file, fl);
2216 } else {
2217 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002218 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002219 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002220 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002221 }
2222 return err;
2223}
2224
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002225static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2226{
Al Viro6131ffa2013-02-27 16:59:05 -05002227 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002228 struct fuse_conn *fc = get_fuse_conn(inode);
2229 int err;
2230
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002231 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002232 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002233 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002234 struct fuse_file *ff = file->private_data;
2235
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002236 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002237 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238 err = fuse_setlk(file, fl, 1);
2239 }
2240
2241 return err;
2242}
2243
Miklos Szeredib2d22722006-12-06 20:35:51 -08002244static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2245{
2246 struct inode *inode = mapping->host;
2247 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002248 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002249 struct fuse_bmap_in inarg;
2250 struct fuse_bmap_out outarg;
2251 int err;
2252
2253 if (!inode->i_sb->s_bdev || fc->no_bmap)
2254 return 0;
2255
Miklos Szeredib2d22722006-12-06 20:35:51 -08002256 memset(&inarg, 0, sizeof(inarg));
2257 inarg.block = block;
2258 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002259 args.in.h.opcode = FUSE_BMAP;
2260 args.in.h.nodeid = get_node_id(inode);
2261 args.in.numargs = 1;
2262 args.in.args[0].size = sizeof(inarg);
2263 args.in.args[0].value = &inarg;
2264 args.out.numargs = 1;
2265 args.out.args[0].size = sizeof(outarg);
2266 args.out.args[0].value = &outarg;
2267 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002268 if (err == -ENOSYS)
2269 fc->no_bmap = 1;
2270
2271 return err ? 0 : outarg.block;
2272}
2273
Andrew Morton965c8e52012-12-17 15:59:39 -08002274static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002275{
2276 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002277 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002278
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002279 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002280 if (whence == SEEK_CUR || whence == SEEK_SET)
2281 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002282
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002283 mutex_lock(&inode->i_mutex);
2284 retval = fuse_update_attributes(inode, NULL, file, NULL);
2285 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002286 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002287 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002288
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002289 return retval;
2290}
2291
Tejun Heo59efec72008-11-26 12:03:55 +01002292static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2293 unsigned int nr_segs, size_t bytes, bool to_user)
2294{
2295 struct iov_iter ii;
2296 int page_idx = 0;
2297
2298 if (!bytes)
2299 return 0;
2300
Al Viro71d8e532014-03-05 19:28:09 -05002301 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
Tejun Heo59efec72008-11-26 12:03:55 +01002302
2303 while (iov_iter_count(&ii)) {
2304 struct page *page = pages[page_idx++];
2305 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002306 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002307
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002308 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002309
2310 while (todo) {
2311 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2312 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2313 size_t copy = min(todo, iov_len);
2314 size_t left;
2315
2316 if (!to_user)
2317 left = copy_from_user(kaddr, uaddr, copy);
2318 else
2319 left = copy_to_user(uaddr, kaddr, copy);
2320
2321 if (unlikely(left))
2322 return -EFAULT;
2323
2324 iov_iter_advance(&ii, copy);
2325 todo -= copy;
2326 kaddr += copy;
2327 }
2328
Jens Axboe0bd87182009-11-03 11:40:44 +01002329 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002330 }
2331
2332 return 0;
2333}
2334
2335/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002336 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2337 * ABI was defined to be 'struct iovec' which is different on 32bit
2338 * and 64bit. Fortunately we can determine which structure the server
2339 * used from the size of the reply.
2340 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002341static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2342 size_t transferred, unsigned count,
2343 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002344{
2345#ifdef CONFIG_COMPAT
2346 if (count * sizeof(struct compat_iovec) == transferred) {
2347 struct compat_iovec *ciov = src;
2348 unsigned i;
2349
2350 /*
2351 * With this interface a 32bit server cannot support
2352 * non-compat (i.e. ones coming from 64bit apps) ioctl
2353 * requests
2354 */
2355 if (!is_compat)
2356 return -EINVAL;
2357
2358 for (i = 0; i < count; i++) {
2359 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2360 dst[i].iov_len = ciov[i].iov_len;
2361 }
2362 return 0;
2363 }
2364#endif
2365
2366 if (count * sizeof(struct iovec) != transferred)
2367 return -EIO;
2368
2369 memcpy(dst, src, transferred);
2370 return 0;
2371}
2372
Miklos Szeredi75727772010-11-30 16:39:27 +01002373/* Make sure iov_length() won't overflow */
2374static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2375{
2376 size_t n;
2377 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2378
Zach Brownfb6ccff2012-07-24 12:10:11 -07002379 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002380 if (iov->iov_len > (size_t) max)
2381 return -ENOMEM;
2382 max -= iov->iov_len;
2383 }
2384 return 0;
2385}
2386
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002387static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2388 void *src, size_t transferred, unsigned count,
2389 bool is_compat)
2390{
2391 unsigned i;
2392 struct fuse_ioctl_iovec *fiov = src;
2393
2394 if (fc->minor < 16) {
2395 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2396 count, is_compat);
2397 }
2398
2399 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2400 return -EIO;
2401
2402 for (i = 0; i < count; i++) {
2403 /* Did the server supply an inappropriate value? */
2404 if (fiov[i].base != (unsigned long) fiov[i].base ||
2405 fiov[i].len != (unsigned long) fiov[i].len)
2406 return -EIO;
2407
2408 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2409 dst[i].iov_len = (size_t) fiov[i].len;
2410
2411#ifdef CONFIG_COMPAT
2412 if (is_compat &&
2413 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2414 (compat_size_t) dst[i].iov_len != fiov[i].len))
2415 return -EIO;
2416#endif
2417 }
2418
2419 return 0;
2420}
2421
2422
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002423/*
Tejun Heo59efec72008-11-26 12:03:55 +01002424 * For ioctls, there is no generic way to determine how much memory
2425 * needs to be read and/or written. Furthermore, ioctls are allowed
2426 * to dereference the passed pointer, so the parameter requires deep
2427 * copying but FUSE has no idea whatsoever about what to copy in or
2428 * out.
2429 *
2430 * This is solved by allowing FUSE server to retry ioctl with
2431 * necessary in/out iovecs. Let's assume the ioctl implementation
2432 * needs to read in the following structure.
2433 *
2434 * struct a {
2435 * char *buf;
2436 * size_t buflen;
2437 * }
2438 *
2439 * On the first callout to FUSE server, inarg->in_size and
2440 * inarg->out_size will be NULL; then, the server completes the ioctl
2441 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2442 * the actual iov array to
2443 *
2444 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2445 *
2446 * which tells FUSE to copy in the requested area and retry the ioctl.
2447 * On the second round, the server has access to the structure and
2448 * from that it can tell what to look for next, so on the invocation,
2449 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2450 *
2451 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2452 * { .iov_base = a.buf, .iov_len = a.buflen } }
2453 *
2454 * FUSE will copy both struct a and the pointed buffer from the
2455 * process doing the ioctl and retry ioctl with both struct a and the
2456 * buffer.
2457 *
2458 * This time, FUSE server has everything it needs and completes ioctl
2459 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2460 *
2461 * Copying data out works the same way.
2462 *
2463 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2464 * automatically initializes in and out iovs by decoding @cmd with
2465 * _IOC_* macros and the server is not allowed to request RETRY. This
2466 * limits ioctl data transfers to well-formed ioctls and is the forced
2467 * behavior for all FUSE servers.
2468 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002469long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2470 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002471{
Tejun Heo59efec72008-11-26 12:03:55 +01002472 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002473 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002474 struct fuse_ioctl_in inarg = {
2475 .fh = ff->fh,
2476 .cmd = cmd,
2477 .arg = arg,
2478 .flags = flags
2479 };
2480 struct fuse_ioctl_out outarg;
2481 struct fuse_req *req = NULL;
2482 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002483 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002484 struct iovec *in_iov = NULL, *out_iov = NULL;
2485 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2486 size_t in_size, out_size, transferred;
2487 int err;
2488
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002489#if BITS_PER_LONG == 32
2490 inarg.flags |= FUSE_IOCTL_32BIT;
2491#else
2492 if (flags & FUSE_IOCTL_COMPAT)
2493 inarg.flags |= FUSE_IOCTL_32BIT;
2494#endif
2495
Tejun Heo59efec72008-11-26 12:03:55 +01002496 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002497 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002498
Tejun Heo59efec72008-11-26 12:03:55 +01002499 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002500 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002501 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002502 if (!pages || !iov_page)
2503 goto out;
2504
2505 /*
2506 * If restricted, initialize IO parameters as encoded in @cmd.
2507 * RETRY from server is not allowed.
2508 */
2509 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002510 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002511
Miklos Szeredic9f05232008-12-02 14:49:42 +01002512 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002513 iov->iov_len = _IOC_SIZE(cmd);
2514
2515 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2516 in_iov = iov;
2517 in_iovs = 1;
2518 }
2519
2520 if (_IOC_DIR(cmd) & _IOC_READ) {
2521 out_iov = iov;
2522 out_iovs = 1;
2523 }
2524 }
2525
2526 retry:
2527 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2528 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2529
2530 /*
2531 * Out data can be used either for actual out data or iovs,
2532 * make sure there always is at least one page.
2533 */
2534 out_size = max_t(size_t, out_size, PAGE_SIZE);
2535 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2536
2537 /* make sure there are enough buffer pages and init request with them */
2538 err = -ENOMEM;
2539 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2540 goto out;
2541 while (num_pages < max_pages) {
2542 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2543 if (!pages[num_pages])
2544 goto out;
2545 num_pages++;
2546 }
2547
Maxim Patlasov54b96672012-10-26 19:49:13 +04002548 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002549 if (IS_ERR(req)) {
2550 err = PTR_ERR(req);
2551 req = NULL;
2552 goto out;
2553 }
2554 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2555 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002556 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002557
2558 /* okay, let's send it to the client */
2559 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002560 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002561 req->in.numargs = 1;
2562 req->in.args[0].size = sizeof(inarg);
2563 req->in.args[0].value = &inarg;
2564 if (in_size) {
2565 req->in.numargs++;
2566 req->in.args[1].size = in_size;
2567 req->in.argpages = 1;
2568
2569 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2570 false);
2571 if (err)
2572 goto out;
2573 }
2574
2575 req->out.numargs = 2;
2576 req->out.args[0].size = sizeof(outarg);
2577 req->out.args[0].value = &outarg;
2578 req->out.args[1].size = out_size;
2579 req->out.argpages = 1;
2580 req->out.argvar = 1;
2581
Tejun Heob93f8582008-11-26 12:03:55 +01002582 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002583 err = req->out.h.error;
2584 transferred = req->out.args[1].size;
2585 fuse_put_request(fc, req);
2586 req = NULL;
2587 if (err)
2588 goto out;
2589
2590 /* did it ask for retry? */
2591 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002592 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002593
2594 /* no retry if in restricted mode */
2595 err = -EIO;
2596 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2597 goto out;
2598
2599 in_iovs = outarg.in_iovs;
2600 out_iovs = outarg.out_iovs;
2601
2602 /*
2603 * Make sure things are in boundary, separate checks
2604 * are to protect against overflow.
2605 */
2606 err = -ENOMEM;
2607 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2608 out_iovs > FUSE_IOCTL_MAX_IOV ||
2609 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2610 goto out;
2611
Cong Wang2408f6e2011-11-25 23:14:30 +08002612 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002613 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002614 transferred, in_iovs + out_iovs,
2615 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002616 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002617 if (err)
2618 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002619
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002620 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002621 out_iov = in_iov + in_iovs;
2622
Miklos Szeredi75727772010-11-30 16:39:27 +01002623 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2624 if (err)
2625 goto out;
2626
2627 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2628 if (err)
2629 goto out;
2630
Tejun Heo59efec72008-11-26 12:03:55 +01002631 goto retry;
2632 }
2633
2634 err = -EIO;
2635 if (transferred > inarg.out_size)
2636 goto out;
2637
2638 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2639 out:
2640 if (req)
2641 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002642 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002643 while (num_pages)
2644 __free_page(pages[--num_pages]);
2645 kfree(pages);
2646
2647 return err ? err : outarg.result;
2648}
Tejun Heo08cbf542009-04-14 10:54:53 +09002649EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002650
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002651long fuse_ioctl_common(struct file *file, unsigned int cmd,
2652 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002653{
Al Viro6131ffa2013-02-27 16:59:05 -05002654 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002655 struct fuse_conn *fc = get_fuse_conn(inode);
2656
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002657 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002658 return -EACCES;
2659
2660 if (is_bad_inode(inode))
2661 return -EIO;
2662
2663 return fuse_do_ioctl(file, cmd, arg, flags);
2664}
2665
Tejun Heo59efec72008-11-26 12:03:55 +01002666static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2667 unsigned long arg)
2668{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002669 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002670}
2671
2672static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2673 unsigned long arg)
2674{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002675 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002676}
2677
Tejun Heo95668a62008-11-26 12:03:55 +01002678/*
2679 * All files which have been polled are linked to RB tree
2680 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2681 * find the matching one.
2682 */
2683static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2684 struct rb_node **parent_out)
2685{
2686 struct rb_node **link = &fc->polled_files.rb_node;
2687 struct rb_node *last = NULL;
2688
2689 while (*link) {
2690 struct fuse_file *ff;
2691
2692 last = *link;
2693 ff = rb_entry(last, struct fuse_file, polled_node);
2694
2695 if (kh < ff->kh)
2696 link = &last->rb_left;
2697 else if (kh > ff->kh)
2698 link = &last->rb_right;
2699 else
2700 return link;
2701 }
2702
2703 if (parent_out)
2704 *parent_out = last;
2705 return link;
2706}
2707
2708/*
2709 * The file is about to be polled. Make sure it's on the polled_files
2710 * RB tree. Note that files once added to the polled_files tree are
2711 * not removed before the file is released. This is because a file
2712 * polled once is likely to be polled again.
2713 */
2714static void fuse_register_polled_file(struct fuse_conn *fc,
2715 struct fuse_file *ff)
2716{
2717 spin_lock(&fc->lock);
2718 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002719 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002720
2721 link = fuse_find_polled_node(fc, ff->kh, &parent);
2722 BUG_ON(*link);
2723 rb_link_node(&ff->polled_node, parent, link);
2724 rb_insert_color(&ff->polled_node, &fc->polled_files);
2725 }
2726 spin_unlock(&fc->lock);
2727}
2728
Tejun Heo08cbf542009-04-14 10:54:53 +09002729unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002730{
Tejun Heo95668a62008-11-26 12:03:55 +01002731 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002732 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002733 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2734 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002735 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002736 int err;
2737
2738 if (fc->no_poll)
2739 return DEFAULT_POLLMASK;
2740
2741 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002742 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002743
2744 /*
2745 * Ask for notification iff there's someone waiting for it.
2746 * The client may ignore the flag and always notify.
2747 */
2748 if (waitqueue_active(&ff->poll_wait)) {
2749 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2750 fuse_register_polled_file(fc, ff);
2751 }
2752
Miklos Szeredi70781872014-12-12 09:49:05 +01002753 args.in.h.opcode = FUSE_POLL;
2754 args.in.h.nodeid = ff->nodeid;
2755 args.in.numargs = 1;
2756 args.in.args[0].size = sizeof(inarg);
2757 args.in.args[0].value = &inarg;
2758 args.out.numargs = 1;
2759 args.out.args[0].size = sizeof(outarg);
2760 args.out.args[0].value = &outarg;
2761 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002762
2763 if (!err)
2764 return outarg.revents;
2765 if (err == -ENOSYS) {
2766 fc->no_poll = 1;
2767 return DEFAULT_POLLMASK;
2768 }
2769 return POLLERR;
2770}
Tejun Heo08cbf542009-04-14 10:54:53 +09002771EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002772
2773/*
2774 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2775 * wakes up the poll waiters.
2776 */
2777int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2778 struct fuse_notify_poll_wakeup_out *outarg)
2779{
2780 u64 kh = outarg->kh;
2781 struct rb_node **link;
2782
2783 spin_lock(&fc->lock);
2784
2785 link = fuse_find_polled_node(fc, kh, NULL);
2786 if (*link) {
2787 struct fuse_file *ff;
2788
2789 ff = rb_entry(*link, struct fuse_file, polled_node);
2790 wake_up_interruptible_sync(&ff->poll_wait);
2791 }
2792
2793 spin_unlock(&fc->lock);
2794 return 0;
2795}
2796
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002797static void fuse_do_truncate(struct file *file)
2798{
2799 struct inode *inode = file->f_mapping->host;
2800 struct iattr attr;
2801
2802 attr.ia_valid = ATTR_SIZE;
2803 attr.ia_size = i_size_read(inode);
2804
2805 attr.ia_file = file;
2806 attr.ia_valid |= ATTR_FILE;
2807
2808 fuse_do_setattr(inode, &attr, file);
2809}
2810
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002811static inline loff_t fuse_round_up(loff_t off)
2812{
2813 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2814}
2815
Anand Avati4273b792012-02-17 12:46:25 -05002816static ssize_t
Omar Sandoval22c61862015-03-16 04:33:53 -07002817fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
Anand Avati4273b792012-02-17 12:46:25 -05002818{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002819 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002820 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002821 struct file *file = iocb->ki_filp;
2822 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002823 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002824 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002825 struct inode *inode;
2826 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002827 size_t count = iov_iter_count(iter);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002828 struct fuse_io_priv *io;
Robert Doebbelin32b98072016-03-07 09:50:56 +01002829 bool is_sync = is_sync_kiocb(iocb);
Anand Avati4273b792012-02-17 12:46:25 -05002830
Anand Avati4273b792012-02-17 12:46:25 -05002831 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002832 inode = file->f_mapping->host;
2833 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002834
Omar Sandoval6f673762015-03-16 04:33:52 -07002835 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002836 return 0;
2837
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002838 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002839 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002840 if (offset >= i_size)
2841 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002842 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2843 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002844 }
2845
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002846 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002847 if (!io)
2848 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002849 spin_lock_init(&io->lock);
Seth Forshee37bd8c82016-03-11 10:35:34 -06002850 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002851 io->reqs = 1;
2852 io->bytes = -1;
2853 io->size = 0;
2854 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002855 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002856 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002857 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002858 /*
2859 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002860 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002861 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002862 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002863 io->iocb = iocb;
2864
2865 /*
2866 * We cannot asynchronously extend the size of a file. We have no method
2867 * to wait on real async I/O requests, so we must submit this request
2868 * synchronously.
2869 */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002870 if (!is_sync && (offset + count > i_size) &&
Omar Sandoval6f673762015-03-16 04:33:52 -07002871 iov_iter_rw(iter) == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002872 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002873
Seth Forshee37bd8c82016-03-11 10:35:34 -06002874 if (io->async && is_sync) {
2875 /*
2876 * Additional reference to keep io around after
2877 * calling fuse_aio_complete()
2878 */
2879 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002880 io->done = &wait;
Seth Forshee37bd8c82016-03-11 10:35:34 -06002881 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002882
Omar Sandoval6f673762015-03-16 04:33:52 -07002883 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002884 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002885 fuse_invalidate_attr(inode);
2886 } else {
Al Virod22a9432014-03-16 15:50:47 -04002887 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002888 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002889
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890 if (io->async) {
2891 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2892
2893 /* we have a non-extending, async request, so return */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002894 if (!is_sync)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 return -EIOCBQUEUED;
2896
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002897 wait_for_completion(&wait);
2898 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002899 }
2900
Seth Forshee37bd8c82016-03-11 10:35:34 -06002901 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002902
Omar Sandoval6f673762015-03-16 04:33:52 -07002903 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002904 if (ret > 0)
2905 fuse_write_update_size(inode, pos);
2906 else if (ret < 0 && offset + count > i_size)
2907 fuse_do_truncate(file);
2908 }
Anand Avati4273b792012-02-17 12:46:25 -05002909
2910 return ret;
2911}
2912
Miklos Szeredicdadb112012-11-10 16:55:56 +01002913static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2914 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002915{
2916 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002917 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002918 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002919 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002920 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002921 struct fuse_fallocate_in inarg = {
2922 .fh = ff->fh,
2923 .offset = offset,
2924 .length = length,
2925 .mode = mode
2926 };
2927 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002928 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2929 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002930
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002931 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2932 return -EOPNOTSUPP;
2933
Miklos Szeredi519c6042012-04-26 10:56:36 +02002934 if (fc->no_fallocate)
2935 return -EOPNOTSUPP;
2936
Maxim Patlasov14c14412013-06-13 12:16:39 +04002937 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002938 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002939 if (mode & FALLOC_FL_PUNCH_HOLE) {
2940 loff_t endbyte = offset + length - 1;
2941 err = filemap_write_and_wait_range(inode->i_mapping,
2942 offset, endbyte);
2943 if (err)
2944 goto out;
2945
2946 fuse_sync_writes(inode);
2947 }
Brian Foster3634a632013-05-17 09:30:32 -04002948 }
2949
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002950 if (!(mode & FALLOC_FL_KEEP_SIZE))
2951 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2952
Miklos Szeredi70781872014-12-12 09:49:05 +01002953 args.in.h.opcode = FUSE_FALLOCATE;
2954 args.in.h.nodeid = ff->nodeid;
2955 args.in.numargs = 1;
2956 args.in.args[0].size = sizeof(inarg);
2957 args.in.args[0].value = &inarg;
2958 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002959 if (err == -ENOSYS) {
2960 fc->no_fallocate = 1;
2961 err = -EOPNOTSUPP;
2962 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002963 if (err)
2964 goto out;
2965
2966 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002967 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2968 bool changed = fuse_write_update_size(inode, offset + length);
2969
Miklos Szeredi93d22692014-04-28 14:19:22 +02002970 if (changed && fc->writeback_cache)
2971 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002972 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002973
2974 if (mode & FALLOC_FL_PUNCH_HOLE)
2975 truncate_pagecache_range(inode, offset, offset + length - 1);
2976
2977 fuse_invalidate_attr(inode);
2978
Brian Foster3634a632013-05-17 09:30:32 -04002979out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002980 if (!(mode & FALLOC_FL_KEEP_SIZE))
2981 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2982
Maxim Patlasovbde52782013-09-13 19:19:54 +04002983 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002984 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002985
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002986 return err;
2987}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002988
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002989static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002990 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04002991 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04002992 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002993 .mmap = fuse_file_mmap,
2994 .open = fuse_open,
2995 .flush = fuse_flush,
2996 .release = fuse_release,
2997 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002998 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002999 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003000 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003001 .unlocked_ioctl = fuse_file_ioctl,
3002 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003003 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003004 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003005};
3006
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003007static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003008 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003009 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003010 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003011 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003012 .open = fuse_open,
3013 .flush = fuse_flush,
3014 .release = fuse_release,
3015 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003016 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003017 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003018 .unlocked_ioctl = fuse_file_ioctl,
3019 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003020 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003021 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003022 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003023};
3024
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003025static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003026 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003027 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003028 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003029 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003030 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003031 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003032 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003033 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003034 .write_begin = fuse_write_begin,
3035 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003036};
3037
3038void fuse_init_file_inode(struct inode *inode)
3039{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003040 inode->i_fop = &fuse_file_operations;
3041 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003042}