blob: abc66a6237fd0ebd9bbafbf893b21b97874d01dc [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Brian Foster3634a632013-05-17 09:30:32 -040018#include <linux/falloc.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070022
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020023static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredi70781872014-12-12 09:49:05 +010027 FUSE_ARGS(args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070030 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
Miklos Szeredi70781872014-12-12 09:49:05 +010033 args.in.h.opcode = opcode;
34 args.in.h.nodeid = nodeid;
35 args.in.numargs = 1;
36 args.in.args[0].size = sizeof(inarg);
37 args.in.args[0].value = &inarg;
38 args.out.numargs = 1;
39 args.out.args[0].size = sizeof(*outargp);
40 args.out.args[0].value = outargp;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080041
Miklos Szeredi70781872014-12-12 09:49:05 +010042 return fuse_simple_request(fc, &args);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080043}
44
Tejun Heoacf99432008-11-26 12:03:55 +010045struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080046{
47 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090048
Miklos Szeredifd72faa2005-11-07 00:59:51 -080049 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090050 if (unlikely(!ff))
51 return NULL;
52
Miklos Szeredida5e4712009-04-28 16:56:36 +020053 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040054 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090055 if (unlikely(!ff->reserved_req)) {
56 kfree(ff);
57 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080058 }
Tejun Heo6b2db282009-04-14 10:54:49 +090059
60 INIT_LIST_HEAD(&ff->write_entry);
61 atomic_set(&ff->count, 0);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
64
65 spin_lock(&fc->lock);
66 ff->kh = ++fc->khctr;
67 spin_unlock(&fc->lock);
68
Miklos Szeredifd72faa2005-11-07 00:59:51 -080069 return ff;
70}
71
72void fuse_file_free(struct fuse_file *ff)
73{
Miklos Szeredi33649c92006-06-25 05:48:52 -070074 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 kfree(ff);
76}
77
Miklos Szeredic7b71432009-04-28 16:56:37 +020078struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070079{
80 atomic_inc(&ff->count);
81 return ff;
82}
83
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010084static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85{
Miklos Szeredibaebccb2014-12-12 09:49:04 +010086 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010087}
88
89static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070090{
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020093
Andrew Gallagher7678ac52013-11-05 16:05:52 +010094 if (ff->fc->no_open) {
95 /*
96 * Drop the release request when client does not
97 * implement 'open'
98 */
Miklos Szeredi825d6d32015-07-01 16:25:58 +020099 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100100 iput(req->misc.release.inode);
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100101 fuse_put_request(ff->fc, req);
102 } else if (sync) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200103 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104 fuse_request_send(ff->fc, req);
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100105 iput(req->misc.release.inode);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106 fuse_put_request(ff->fc, req);
107 } else {
108 req->end = fuse_release_end;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200109 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100110 fuse_request_send_background(ff->fc, req);
111 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700112 kfree(ff);
113 }
114}
115
Tejun Heo08cbf542009-04-14 10:54:53 +0900116int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
117 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200118{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200119 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
121
122 ff = fuse_file_alloc(fc);
123 if (!ff)
124 return -ENOMEM;
125
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100126 ff->fh = 0;
127 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
128 if (!fc->no_open || isdir) {
129 struct fuse_open_out outarg;
130 int err;
131
132 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
133 if (!err) {
134 ff->fh = outarg.fh;
135 ff->open_flags = outarg.open_flags;
136
137 } else if (err != -ENOSYS || isdir) {
138 fuse_file_free(ff);
139 return err;
140 } else {
141 fc->no_open = 1;
142 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200143 }
144
145 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100146 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200147
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200148 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200149 file->private_data = fuse_file_get(ff);
150
151 return 0;
152}
Tejun Heo08cbf542009-04-14 10:54:53 +0900153EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400155static void fuse_link_write_file(struct file *file)
156{
157 struct inode *inode = file_inode(file);
158 struct fuse_conn *fc = get_fuse_conn(inode);
159 struct fuse_inode *fi = get_fuse_inode(inode);
160 struct fuse_file *ff = file->private_data;
161 /*
162 * file may be written through mmap, so chain it onto the
163 * inodes's write_file list
164 */
165 spin_lock(&fc->lock);
166 if (list_empty(&ff->write_entry))
167 list_add(&ff->write_entry, &fi->write_files);
168 spin_unlock(&fc->lock);
169}
170
Miklos Szeredic7b71432009-04-28 16:56:37 +0200171void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800172{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200173 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800174 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200175
176 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800177 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700179 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200180 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200181 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
184
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200190 if (fc->writeback_cache)
191 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800192 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400193 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
194 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800195}
196
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200197int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800198{
Tejun Heoacf99432008-11-26 12:03:55 +0100199 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700200 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200201 bool lock_inode = (file->f_flags & O_TRUNC) &&
202 fc->atomic_o_trunc &&
203 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204
205 err = generic_file_open(inode, file);
206 if (err)
207 return err;
208
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200209 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500210 inode_lock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200211
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200212 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700213
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200214 if (!err)
215 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200216
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200217 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -0500218 inode_unlock(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200219
220 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700221}
222
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200223static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800224{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200225 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700226 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800227 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700228
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200229 spin_lock(&fc->lock);
230 list_del(&ff->write_entry);
231 if (!RB_EMPTY_NODE(&ff->polled_node))
232 rb_erase(&ff->polled_node, &fc->polled_files);
233 spin_unlock(&fc->lock);
234
Bryan Green357ccf22011-03-01 16:43:52 -0800235 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200236
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700237 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800238 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700239 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200240 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700241 req->in.numargs = 1;
242 req->in.args[0].size = sizeof(struct fuse_release_in);
243 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800244}
245
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200246void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800247{
Tejun Heo6b2db282009-04-14 10:54:49 +0900248 struct fuse_file *ff;
249 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700250
Tejun Heo6b2db282009-04-14 10:54:49 +0900251 ff = file->private_data;
252 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200253 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700254
Tejun Heo6b2db282009-04-14 10:54:49 +0900255 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200256 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100257
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200258 if (ff->flock) {
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
262 (fl_owner_t) file);
263 }
Miklos Szeredibaebccb2014-12-12 09:49:04 +0100264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266
Tejun Heo6b2db282009-04-14 10:54:49 +0900267 /*
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100271 *
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900275 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100276 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277}
278
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700279static int fuse_open(struct inode *inode, struct file *file)
280{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200281 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700282}
283
284static int fuse_release(struct inode *inode, struct file *file)
285{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400286 struct fuse_conn *fc = get_fuse_conn(inode);
287
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200290 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400291
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200292 fuse_release_common(file, FUSE_RELEASE);
293
294 /* return value is ignored by VFS */
295 return 0;
296}
297
298void fuse_sync_release(struct fuse_file *ff, int flags)
299{
300 WARN_ON(atomic_read(&ff->count) > 1);
301 fuse_prepare_release(ff, flags, FUSE_RELEASE);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200302 __set_bit(FR_FORCE, &ff->reserved_req->flags);
303 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200304 fuse_request_send(ff->fc, ff->reserved_req);
305 fuse_put_request(ff->fc, ff->reserved_req);
306 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307}
Tejun Heo08cbf542009-04-14 10:54:53 +0900308EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700309
Miklos Szeredi71421252006-06-25 05:48:52 -0700310/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700313 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700314u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700315{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
318 u32 v0 = v;
319 u32 v1 = v >> 32;
320 u32 sum = 0;
321 int i;
322
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
325 sum += 0x9E3779B9;
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
327 }
328
329 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700330}
331
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700332/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400333 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700334 *
335 * This is currently done by walking the list of writepage requests
336 * for the inode, which can be pretty inefficient.
337 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400338static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
339 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700340{
341 struct fuse_conn *fc = get_fuse_conn(inode);
342 struct fuse_inode *fi = get_fuse_inode(inode);
343 struct fuse_req *req;
344 bool found = false;
345
346 spin_lock(&fc->lock);
347 list_for_each_entry(req, &fi->writepages, writepages_entry) {
348 pgoff_t curr_index;
349
350 BUG_ON(req->inode != inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300351 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400352 if (idx_from < curr_index + req->num_pages &&
353 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700354 found = true;
355 break;
356 }
357 }
358 spin_unlock(&fc->lock);
359
360 return found;
361}
362
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400363static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
364{
365 return fuse_range_is_writeback(inode, index, index);
366}
367
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700368/*
369 * Wait for page writeback to be completed.
370 *
371 * Since fuse doesn't rely on the VM writeback tracking, this has to
372 * use some other means.
373 */
374static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
375{
376 struct fuse_inode *fi = get_fuse_inode(inode);
377
378 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
379 return 0;
380}
381
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400382/*
383 * Wait for all pending writepages on the inode to finish.
384 *
385 * This is currently done by blocking further writes with FUSE_NOWRITE
386 * and waiting for all sent writes to complete.
387 *
388 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
389 * could conflict with truncation.
390 */
391static void fuse_sync_writes(struct inode *inode)
392{
393 fuse_set_nowrite(inode);
394 fuse_release_nowrite(inode);
395}
396
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700397static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700398{
Al Viro6131ffa2013-02-27 16:59:05 -0500399 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700400 struct fuse_conn *fc = get_fuse_conn(inode);
401 struct fuse_file *ff = file->private_data;
402 struct fuse_req *req;
403 struct fuse_flush_in inarg;
404 int err;
405
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800406 if (is_bad_inode(inode))
407 return -EIO;
408
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700409 if (fc->no_flush)
410 return 0;
411
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200412 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400413 if (err)
414 return err;
415
Al Viro59551022016-01-22 15:40:57 -0500416 inode_lock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400417 fuse_sync_writes(inode);
Al Viro59551022016-01-22 15:40:57 -0500418 inode_unlock(inode);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400419
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200420 err = filemap_check_errors(file->f_mapping);
Maxim Patlasov9ebce592016-07-19 18:12:26 -0700421 if (err)
422 return err;
423
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400424 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 memset(&inarg, 0, sizeof(inarg));
426 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700427 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700428 req->in.h.opcode = FUSE_FLUSH;
429 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 req->in.numargs = 1;
431 req->in.args[0].size = sizeof(inarg);
432 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200433 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100434 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435 err = req->out.h.error;
436 fuse_put_request(fc, req);
437 if (err == -ENOSYS) {
438 fc->no_flush = 1;
439 err = 0;
440 }
441 return err;
442}
443
Josef Bacik02c24a82011-07-16 20:44:56 -0400444int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
445 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200447 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 struct fuse_conn *fc = get_fuse_conn(inode);
449 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100450 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 struct fuse_fsync_in inarg;
452 int err;
453
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800454 if (is_bad_inode(inode))
455 return -EIO;
456
Al Viro59551022016-01-22 15:40:57 -0500457 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400458
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700459 /*
460 * Start writeback against all dirty pages of the inode, then
461 * wait for all outstanding writes, before sending the FSYNC
462 * request.
463 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200464 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700465 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400466 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700467
468 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700469
470 /*
471 * Due to implementation of fuse writeback
472 * filemap_write_and_wait_range() does not catch errors.
473 * We have to do this directly after fuse_sync_writes()
474 */
Miklos Szeredi4a7f4e82016-07-29 14:10:57 +0200475 err = filemap_check_errors(file->f_mapping);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700476 if (err)
477 goto out;
478
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200479 err = sync_inode_metadata(inode, 1);
480 if (err)
481 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700482
Miklos Szeredi22401e72014-04-28 14:19:23 +0200483 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
484 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400485
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700486 memset(&inarg, 0, sizeof(inarg));
487 inarg.fh = ff->fh;
488 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100489 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
490 args.in.h.nodeid = get_node_id(inode);
491 args.in.numargs = 1;
492 args.in.args[0].size = sizeof(inarg);
493 args.in.args[0].value = &inarg;
494 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700495 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700496 if (isdir)
497 fc->no_fsyncdir = 1;
498 else
499 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700500 err = 0;
501 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400502out:
Al Viro59551022016-01-22 15:40:57 -0500503 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700504 return err;
505}
506
Josef Bacik02c24a82011-07-16 20:44:56 -0400507static int fuse_fsync(struct file *file, loff_t start, loff_t end,
508 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700509{
Josef Bacik02c24a82011-07-16 20:44:56 -0400510 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700511}
512
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200513void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
514 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700516 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800517 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700518
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800519 inarg->fh = ff->fh;
520 inarg->offset = pos;
521 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800522 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800523 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200524 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 req->in.numargs = 1;
526 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800527 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528 req->out.argvar = 1;
529 req->out.numargs = 1;
530 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531}
532
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200533static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400534{
535 unsigned i;
536
537 for (i = 0; i < req->num_pages; i++) {
538 struct page *page = req->pages[i];
Miklos Szeredi8fba54a2016-08-24 18:17:04 +0200539 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400540 set_page_dirty_lock(page);
541 put_page(page);
542 }
543}
544
Seth Forshee744742d2016-03-11 10:35:34 -0600545static void fuse_io_release(struct kref *kref)
546{
547 kfree(container_of(kref, struct fuse_io_priv, refcnt));
548}
549
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100550static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
551{
552 if (io->err)
553 return io->err;
554
555 if (io->bytes >= 0 && io->write)
556 return -EIO;
557
558 return io->bytes < 0 ? io->size : io->bytes;
559}
560
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400561/**
562 * In case of short read, the caller sets 'pos' to the position of
563 * actual end of fuse request in IO request. Otherwise, if bytes_requested
564 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
565 *
566 * An example:
567 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
568 * both submitted asynchronously. The first of them was ACKed by userspace as
569 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
570 * second request was ACKed as short, e.g. only 1K was read, resulting in
571 * pos == 33K.
572 *
573 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
574 * will be equal to the length of the longest contiguous fragment of
575 * transferred data starting from the beginning of IO request.
576 */
577static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
578{
579 int left;
580
581 spin_lock(&io->lock);
582 if (err)
583 io->err = io->err ? : err;
584 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
585 io->bytes = pos;
586
587 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530588 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100589 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400590 spin_unlock(&io->lock);
591
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530592 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100593 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400594
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100595 if (res >= 0) {
596 struct inode *inode = file_inode(io->iocb->ki_filp);
597 struct fuse_conn *fc = get_fuse_conn(inode);
598 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400599
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100600 spin_lock(&fc->lock);
601 fi->attr_version = ++fc->attr_version;
602 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400603 }
604
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100605 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400606 }
Seth Forshee744742d2016-03-11 10:35:34 -0600607
608 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400609}
610
611static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
612{
613 struct fuse_io_priv *io = req->io;
614 ssize_t pos = -1;
615
616 fuse_release_user_pages(req, !io->write);
617
618 if (io->write) {
619 if (req->misc.write.in.size != req->misc.write.out.size)
620 pos = req->misc.write.in.offset - io->offset +
621 req->misc.write.out.size;
622 } else {
623 if (req->misc.read.in.size != req->out.args[0].size)
624 pos = req->misc.read.in.offset - io->offset +
625 req->out.args[0].size;
626 }
627
628 fuse_aio_complete(io, req->out.h.error, pos);
629}
630
631static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
632 size_t num_bytes, struct fuse_io_priv *io)
633{
634 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600635 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400636 io->size += num_bytes;
637 io->reqs++;
638 spin_unlock(&io->lock);
639
640 req->io = io;
641 req->end = fuse_aio_complete_req;
642
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400643 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400644 fuse_request_send_background(fc, req);
645
646 return num_bytes;
647}
648
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400649static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200650 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700651{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400652 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200653 struct fuse_file *ff = file->private_data;
654 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700655
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200656 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700657 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700658 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700659
660 inarg->read_flags |= FUSE_READ_LOCKOWNER;
661 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
662 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400663
664 if (io->async)
665 return fuse_async_req_send(fc, req, count, io);
666
Tejun Heob93f8582008-11-26 12:03:55 +0100667 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800668 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700669}
670
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700671static void fuse_read_update_size(struct inode *inode, loff_t size,
672 u64 attr_ver)
673{
674 struct fuse_conn *fc = get_fuse_conn(inode);
675 struct fuse_inode *fi = get_fuse_inode(inode);
676
677 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400678 if (attr_ver == fi->attr_version && size < inode->i_size &&
679 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700680 fi->attr_version = ++fc->attr_version;
681 i_size_write(inode, size);
682 }
683 spin_unlock(&fc->lock);
684}
685
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400686static void fuse_short_read(struct fuse_req *req, struct inode *inode,
687 u64 attr_ver)
688{
689 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400690 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400691
Pavel Emelyanov83732002013-10-10 17:10:46 +0400692 if (fc->writeback_cache) {
693 /*
694 * A hole in a file. Some data after the hole are in page cache,
695 * but have not reached the client fs yet. So, the hole is not
696 * present there.
697 */
698 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300699 int start_idx = num_read >> PAGE_SHIFT;
700 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701
702 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300703 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400704 off = 0;
705 }
706 } else {
707 loff_t pos = page_offset(req->pages[0]) + num_read;
708 fuse_read_update_size(inode, pos, attr_ver);
709 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710}
711
Maxim Patlasov482fce52013-10-10 17:11:25 +0400712static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700713{
Seth Forshee744742d2016-03-11 10:35:34 -0600714 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700715 struct inode *inode = page->mapping->host;
716 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800717 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700718 size_t num_read;
719 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300720 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700721 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800722 int err;
723
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700724 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300725 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700726 * page-cache page, so make sure we read a properly synced
727 * page.
728 */
729 fuse_wait_on_page_writeback(inode, page->index);
730
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400731 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700732 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400733 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700735 attr_ver = fuse_get_attr_version(fc);
736
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200738 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700739 req->num_pages = 1;
740 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400741 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400742 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700743 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700744
745 if (!err) {
746 /*
747 * Short read means EOF. If file size is larger, truncate it
748 */
749 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400750 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700751
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700752 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700753 }
754
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400755 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400756
757 return err;
758}
759
760static int fuse_readpage(struct file *file, struct page *page)
761{
762 struct inode *inode = page->mapping->host;
763 int err;
764
765 err = -EIO;
766 if (is_bad_inode(inode))
767 goto out;
768
769 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800770 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700771 out:
772 unlock_page(page);
773 return err;
774}
775
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800776static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700777{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800778 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700779 size_t count = req->misc.read.in.size;
780 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200781 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800782
Miklos Szeredice534fb2010-05-25 15:06:07 +0200783 for (i = 0; mapping == NULL && i < req->num_pages; i++)
784 mapping = req->pages[i]->mapping;
785
786 if (mapping) {
787 struct inode *inode = mapping->host;
788
789 /*
790 * Short read means EOF. If file size is larger, truncate it
791 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400792 if (!req->out.h.error && num_read < count)
793 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794
Andrew Gallagher451418f2013-11-05 03:55:43 -0800795 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700796 }
797
Miklos Szeredidb50b962005-09-09 13:10:33 -0700798 for (i = 0; i < req->num_pages; i++) {
799 struct page *page = req->pages[i];
800 if (!req->out.h.error)
801 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800802 else
803 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700804 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300805 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700806 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700807 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100808 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800809}
810
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200811static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800812{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200813 struct fuse_file *ff = file->private_data;
814 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800815 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300816 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200817
818 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800819 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200821 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700822 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800823 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700824 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800825 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100826 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800827 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100828 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800829 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100830 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800831 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700832}
833
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700834struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700835 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800836 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700837 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400838 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700839};
840
841static int fuse_readpages_fill(void *_data, struct page *page)
842{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700843 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700844 struct fuse_req *req = data->req;
845 struct inode *inode = data->inode;
846 struct fuse_conn *fc = get_fuse_conn(inode);
847
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700848 fuse_wait_on_page_writeback(inode, page->index);
849
Miklos Szeredidb50b962005-09-09 13:10:33 -0700850 if (req->num_pages &&
851 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300852 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400854 int nr_alloc = min_t(unsigned, data->nr_pages,
855 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200856 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400857 if (fc->async_read)
858 req = fuse_get_req_for_background(fc, nr_alloc);
859 else
860 req = fuse_get_req(fc, nr_alloc);
861
862 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700863 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700865 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700866 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400868
869 if (WARN_ON(req->num_pages >= req->max_pages)) {
870 fuse_put_request(fc, req);
871 return -EIO;
872 }
873
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300874 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400876 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100877 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400878 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 return 0;
880}
881
882static int fuse_readpages(struct file *file, struct address_space *mapping,
883 struct list_head *pages, unsigned nr_pages)
884{
885 struct inode *inode = mapping->host;
886 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700887 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700888 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400889 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800890
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700891 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800892 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800893 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800894
Miklos Szeredia6643092007-11-28 16:22:00 -0800895 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700896 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400897 if (fc->async_read)
898 data.req = fuse_get_req_for_background(fc, nr_alloc);
899 else
900 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400901 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700902 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700903 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800904 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700905
906 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700907 if (!err) {
908 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200909 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700910 else
911 fuse_put_request(fc, data.req);
912 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800913out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700914 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915}
916
Al Viro37c20f12014-04-02 14:47:09 -0400917static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800918{
919 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400920 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800921
Brian Fostera8894272012-07-16 15:23:50 -0400922 /*
923 * In auto invalidate mode, always update attributes on read.
924 * Otherwise, only update if we attempt to read past EOF (to ensure
925 * i_size is up to date).
926 */
927 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400928 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800929 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800930 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
931 if (err)
932 return err;
933 }
934
Al Viro37c20f12014-04-02 14:47:09 -0400935 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936}
937
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200938static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200939 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700940{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700941 struct fuse_write_in *inarg = &req->misc.write.in;
942 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700943
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700944 inarg->fh = ff->fh;
945 inarg->offset = pos;
946 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700947 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200948 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700949 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700951 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
952 else
953 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700954 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955 req->in.args[1].size = count;
956 req->out.numargs = 1;
957 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958 req->out.args[0].value = outarg;
959}
960
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400961static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200962 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700963{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400964 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200965 struct fuse_file *ff = file->private_data;
966 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200967 struct fuse_write_in *inarg = &req->misc.write.in;
968
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200969 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200970 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700971 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700972 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
973 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
974 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400975
976 if (io->async)
977 return fuse_async_req_send(fc, req, count, io);
978
Tejun Heob93f8582008-11-26 12:03:55 +0100979 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700980 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700981}
982
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400983bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700984{
985 struct fuse_conn *fc = get_fuse_conn(inode);
986 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400987 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700988
989 spin_lock(&fc->lock);
990 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400991 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700992 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400993 ret = true;
994 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700995 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400996
997 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700998}
999
Nick Pigginea9b9902008-04-30 00:54:42 -07001000static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1001 struct inode *inode, loff_t pos,
1002 size_t count)
1003{
1004 size_t res;
1005 unsigned offset;
1006 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001007 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001008
1009 for (i = 0; i < req->num_pages; i++)
1010 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1011
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001012 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001013
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001014 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001015 count = res;
1016 for (i = 0; i < req->num_pages; i++) {
1017 struct page *page = req->pages[i];
1018
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001019 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001020 SetPageUptodate(page);
1021
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001022 if (count > PAGE_SIZE - offset)
1023 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001024 else
1025 count = 0;
1026 offset = 0;
1027
1028 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001029 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001030 }
1031
1032 return res;
1033}
1034
1035static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1036 struct address_space *mapping,
1037 struct iov_iter *ii, loff_t pos)
1038{
1039 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001040 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001041 size_t count = 0;
1042 int err;
1043
Miklos Szeredif4975c62009-04-02 14:25:34 +02001044 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001045 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001046
1047 do {
1048 size_t tmp;
1049 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001050 pgoff_t index = pos >> PAGE_SHIFT;
1051 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001052 iov_iter_count(ii));
1053
1054 bytes = min_t(size_t, bytes, fc->max_write - count);
1055
1056 again:
1057 err = -EFAULT;
1058 if (iov_iter_fault_in_readable(ii, bytes))
1059 break;
1060
1061 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001062 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001063 if (!page)
1064 break;
1065
anfei zhou931e80e2010-02-02 13:44:02 -08001066 if (mapping_writably_mapped(mapping))
1067 flush_dcache_page(page);
1068
Nick Pigginea9b9902008-04-30 00:54:42 -07001069 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001070 flush_dcache_page(page);
1071
Roman Gushchin3ca81382015-10-12 16:33:44 +03001072 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001073 if (!tmp) {
1074 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001075 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001076 bytes = min(bytes, iov_iter_single_seg_count(ii));
1077 goto again;
1078 }
1079
1080 err = 0;
1081 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001082 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 req->num_pages++;
1084
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 count += tmp;
1086 pos += tmp;
1087 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 offset = 0;
1090
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001091 if (!fc->big_writes)
1092 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001094 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001095
1096 return count > 0 ? count : err;
1097}
1098
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001099static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1100{
1101 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001102 ((pos + len - 1) >> PAGE_SHIFT) -
1103 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001104 FUSE_MAX_PAGES_PER_REQ);
1105}
1106
Nick Pigginea9b9902008-04-30 00:54:42 -07001107static ssize_t fuse_perform_write(struct file *file,
1108 struct address_space *mapping,
1109 struct iov_iter *ii, loff_t pos)
1110{
1111 struct inode *inode = mapping->host;
1112 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001113 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001114 int err = 0;
1115 ssize_t res = 0;
1116
1117 if (is_bad_inode(inode))
1118 return -EIO;
1119
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001120 if (inode->i_size < pos + iov_iter_count(ii))
1121 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1122
Nick Pigginea9b9902008-04-30 00:54:42 -07001123 do {
1124 struct fuse_req *req;
1125 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001126 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001127
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001128 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001129 if (IS_ERR(req)) {
1130 err = PTR_ERR(req);
1131 break;
1132 }
1133
1134 count = fuse_fill_write_pages(req, mapping, ii, pos);
1135 if (count <= 0) {
1136 err = count;
1137 } else {
1138 size_t num_written;
1139
1140 num_written = fuse_send_write_pages(req, file, inode,
1141 pos, count);
1142 err = req->out.h.error;
1143 if (!err) {
1144 res += num_written;
1145 pos += num_written;
1146
1147 /* break out of the loop on short write */
1148 if (num_written != count)
1149 err = -EIO;
1150 }
1151 }
1152 fuse_put_request(fc, req);
1153 } while (!err && iov_iter_count(ii));
1154
1155 if (res > 0)
1156 fuse_write_update_size(inode, pos);
1157
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001158 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001159 fuse_invalidate_attr(inode);
1160
1161 return res > 0 ? res : err;
1162}
1163
Al Viro84c3d552014-04-03 14:33:23 -04001164static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001165{
1166 struct file *file = iocb->ki_filp;
1167 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001168 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001169 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 struct inode *inode = mapping->host;
1171 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001172 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001173
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001174 if (get_fuse_conn(inode)->writeback_cache) {
1175 /* Update size (EOF optimization) and mode (SUID clearing) */
1176 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1177 if (err)
1178 return err;
1179
Al Viro84c3d552014-04-03 14:33:23 -04001180 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001181 }
1182
Al Viro59551022016-01-22 15:40:57 -05001183 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001184
1185 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001186 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001187
Al Viro3309dd02015-04-09 12:55:47 -04001188 err = generic_write_checks(iocb, from);
1189 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001190 goto out;
1191
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001192 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001193 if (err)
1194 goto out;
1195
Josef Bacikc3b2da32012-03-26 09:59:21 -04001196 err = file_update_time(file);
1197 if (err)
1198 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001199
Al Viro2ba48ce2015-04-09 13:52:01 -04001200 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001201 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001202 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001203 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001204 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001205
Anand Avati4273b792012-02-17 12:46:25 -05001206 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001207
Al Viro84c3d552014-04-03 14:33:23 -04001208 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001209 if (written_buffered < 0) {
1210 err = written_buffered;
1211 goto out;
1212 }
1213 endbyte = pos + written_buffered - 1;
1214
1215 err = filemap_write_and_wait_range(file->f_mapping, pos,
1216 endbyte);
1217 if (err)
1218 goto out;
1219
1220 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001221 pos >> PAGE_SHIFT,
1222 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001223
1224 written += written_buffered;
1225 iocb->ki_pos = pos + written_buffered;
1226 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001227 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001228 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001229 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001230 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001231out:
1232 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001233 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001234
1235 return written ? written : err;
1236}
1237
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001238static inline void fuse_page_descs_length_init(struct fuse_req *req,
1239 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001240{
1241 int i;
1242
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001243 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001244 req->page_descs[i].length = PAGE_SIZE -
1245 req->page_descs[i].offset;
1246}
1247
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001248static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1249{
1250 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1251}
1252
1253static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1254 size_t max_size)
1255{
1256 return min(iov_iter_single_seg_count(ii), max_size);
1257}
1258
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001259static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001260 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001261{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001262 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001263 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001264
Miklos Szeredif4975c62009-04-02 14:25:34 +02001265 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001266 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001267 unsigned long user_addr = fuse_get_user_addr(ii);
1268 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1269
Miklos Szeredif4975c62009-04-02 14:25:34 +02001270 if (write)
1271 req->in.args[1].value = (void *) user_addr;
1272 else
1273 req->out.args[0].value = (void *) user_addr;
1274
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001275 iov_iter_advance(ii, frag_size);
1276 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001277 return 0;
1278 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001279
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001280 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001282 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001283 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001284 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001285 req->max_pages - req->num_pages,
1286 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001287 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001288 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001289
Al Viroc9c37e22014-03-16 16:08:30 -04001290 iov_iter_advance(ii, ret);
1291 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001292
Al Viroc9c37e22014-03-16 16:08:30 -04001293 ret += start;
1294 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1295
1296 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001297 fuse_page_descs_length_init(req, req->num_pages, npages);
1298
1299 req->num_pages += npages;
1300 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001301 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001302 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001303
1304 if (write)
1305 req->in.argpages = 1;
1306 else
1307 req->out.argpages = 1;
1308
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001310
Ashish Samant2c932d42016-03-25 10:53:41 -07001311 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001312}
1313
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001314static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1315{
Al Virof67da302014-03-19 01:16:16 -04001316 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001317}
1318
Al Virod22a9432014-03-16 15:50:47 -04001319ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1320 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001321{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001322 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001323 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001324 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001325 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001326 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001327 struct fuse_file *ff = file->private_data;
1328 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001329 size_t nmax = write ? fc->max_write : fc->max_read;
1330 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001331 size_t count = iov_iter_count(iter);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001332 pgoff_t idx_from = pos >> PAGE_SHIFT;
1333 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001334 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001335 struct fuse_req *req;
Ashish Samant742f9922016-03-14 21:57:35 -07001336 int err = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001337
Brian Fosterde82b922013-05-14 11:25:39 -04001338 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001339 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001340 else
Al Virod22a9432014-03-16 15:50:47 -04001341 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001342 if (IS_ERR(req))
1343 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001344
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001345 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1346 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001347 inode_lock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001348 fuse_sync_writes(inode);
1349 if (!write)
Al Viro59551022016-01-22 15:40:57 -05001350 inode_unlock(inode);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001351 }
1352
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001353 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001354 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001355 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001356 size_t nbytes = min(count, nmax);
Ashish Samant742f9922016-03-14 21:57:35 -07001357 err = fuse_get_user_pages(req, iter, &nbytes, write);
1358 if (err && !nbytes)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001359 break;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001360
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001361 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001362 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001364 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001365
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001366 if (!io->async)
Miklos Szeredi8fba54a2016-08-24 18:17:04 +02001367 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 if (req->out.h.error) {
Ashish Samant742f9922016-03-14 21:57:35 -07001369 err = req->out.h.error;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 break;
1371 } else if (nres > nbytes) {
Ashish Samant742f9922016-03-14 21:57:35 -07001372 res = 0;
1373 err = -EIO;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001374 break;
1375 }
1376 count -= nres;
1377 res += nres;
1378 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379 if (nres != nbytes)
1380 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001381 if (count) {
1382 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001383 if (io->async)
1384 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001385 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001386 else
Al Virod22a9432014-03-16 15:50:47 -04001387 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001388 if (IS_ERR(req))
1389 break;
1390 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001392 if (!IS_ERR(req))
1393 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001394 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001395 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396
Ashish Samant742f9922016-03-14 21:57:35 -07001397 return res > 0 ? res : err;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398}
Tejun Heo08cbf542009-04-14 10:54:53 +09001399EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001400
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001401static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001402 struct iov_iter *iter,
1403 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001405 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001406 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001407 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001408
1409 if (is_bad_inode(inode))
1410 return -EIO;
1411
Al Virod22a9432014-03-16 15:50:47 -04001412 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001413
1414 fuse_invalidate_attr(inode);
1415
1416 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417}
1418
Al Viro15316262015-03-30 22:08:36 -04001419static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001420{
Seth Forshee744742d2016-03-11 10:35:34 -06001421 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001422 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001423}
1424
Al Viro15316262015-03-30 22:08:36 -04001425static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001426{
Al Viro15316262015-03-30 22:08:36 -04001427 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001428 struct inode *inode = file_inode(file);
Seth Forshee744742d2016-03-11 10:35:34 -06001429 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001430 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001431
1432 if (is_bad_inode(inode))
1433 return -EIO;
1434
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001435 /* Don't allow parallel writes to the same file */
Al Viro59551022016-01-22 15:40:57 -05001436 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001437 res = generic_write_checks(iocb, from);
1438 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001439 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001440 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001441 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001442 fuse_write_update_size(inode, iocb->ki_pos);
Al Viro59551022016-01-22 15:40:57 -05001443 inode_unlock(inode);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001444
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001445 return res;
1446}
1447
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001448static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001449{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001450 int i;
1451
1452 for (i = 0; i < req->num_pages; i++)
1453 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001454
1455 if (req->ff)
1456 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001457}
1458
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001459static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001460{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001461 struct inode *inode = req->inode;
1462 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001463 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001464 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001465
1466 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001467 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001468 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001469 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001470 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001471 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001472 wake_up(&fi->page_waitq);
1473}
1474
1475/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001476static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1477 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001478__releases(fc->lock)
1479__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001480{
1481 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001482 struct fuse_write_in *inarg = &req->misc.write.in;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001483 __u64 data_size = req->num_pages * PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001484
1485 if (!fc->connected)
1486 goto out_free;
1487
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001488 if (inarg->offset + data_size <= size) {
1489 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001490 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001491 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001492 } else {
1493 /* Got truncated off completely */
1494 goto out_free;
1495 }
1496
1497 req->in.args[1].size = inarg->size;
1498 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001499 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001500 return;
1501
1502 out_free:
1503 fuse_writepage_finish(fc, req);
1504 spin_unlock(&fc->lock);
1505 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001506 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001507 spin_lock(&fc->lock);
1508}
1509
1510/*
1511 * If fi->writectr is positive (no truncate or fsync going on) send
1512 * all queued writepage requests.
1513 *
1514 * Called with fc->lock
1515 */
1516void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001517__releases(fc->lock)
1518__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001519{
1520 struct fuse_conn *fc = get_fuse_conn(inode);
1521 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001522 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001523 struct fuse_req *req;
1524
1525 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1526 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1527 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001528 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529 }
1530}
1531
1532static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1533{
1534 struct inode *inode = req->inode;
1535 struct fuse_inode *fi = get_fuse_inode(inode);
1536
1537 mapping_set_error(inode->i_mapping, req->out.h.error);
1538 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001539 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001540 struct fuse_conn *fc = get_fuse_conn(inode);
1541 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001542 struct fuse_req *next = req->misc.write.next;
1543 req->misc.write.next = next->misc.write.next;
1544 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001545 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001546 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001547
1548 /*
1549 * Skip fuse_flush_writepages() to make it easy to crop requests
1550 * based on primary request size.
1551 *
1552 * 1st case (trivial): there are no concurrent activities using
1553 * fuse_set/release_nowrite. Then we're on safe side because
1554 * fuse_flush_writepages() would call fuse_send_writepage()
1555 * anyway.
1556 *
1557 * 2nd case: someone called fuse_set_nowrite and it is waiting
1558 * now for completion of all in-flight requests. This happens
1559 * rarely and no more than once per page, so this should be
1560 * okay.
1561 *
1562 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1563 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1564 * that fuse_set_nowrite returned implies that all in-flight
1565 * requests were completed along with all of their secondary
1566 * requests. Further primary requests are blocked by negative
1567 * writectr. Hence there cannot be any in-flight requests and
1568 * no invocations of fuse_writepage_end() while we're in
1569 * fuse_set_nowrite..fuse_release_nowrite section.
1570 */
1571 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001572 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001573 fi->writectr--;
1574 fuse_writepage_finish(fc, req);
1575 spin_unlock(&fc->lock);
1576 fuse_writepage_free(fc, req);
1577}
1578
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001579static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1580 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001581{
Miklos Szeredi72523422013-10-01 16:44:52 +02001582 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001583
1584 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001585 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001586 ff = list_entry(fi->write_files.next, struct fuse_file,
1587 write_entry);
1588 fuse_file_get(ff);
1589 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001590 spin_unlock(&fc->lock);
1591
1592 return ff;
1593}
1594
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001595static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1596 struct fuse_inode *fi)
1597{
1598 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1599 WARN_ON(!ff);
1600 return ff;
1601}
1602
1603int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1604{
1605 struct fuse_conn *fc = get_fuse_conn(inode);
1606 struct fuse_inode *fi = get_fuse_inode(inode);
1607 struct fuse_file *ff;
1608 int err;
1609
1610 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001611 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001612 if (ff)
1613 fuse_file_put(ff, 0);
1614
1615 return err;
1616}
1617
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001618static int fuse_writepage_locked(struct page *page)
1619{
1620 struct address_space *mapping = page->mapping;
1621 struct inode *inode = mapping->host;
1622 struct fuse_conn *fc = get_fuse_conn(inode);
1623 struct fuse_inode *fi = get_fuse_inode(inode);
1624 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001625 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001626 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001627
1628 set_page_writeback(page);
1629
Maxim Patlasov4250c062012-10-26 19:48:07 +04001630 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631 if (!req)
1632 goto err;
1633
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001634 /* writeback always goes to bg_queue */
1635 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1637 if (!tmp_page)
1638 goto err_free;
1639
Miklos Szeredi72523422013-10-01 16:44:52 +02001640 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001641 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001642 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001643 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001644
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001645 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001646
1647 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001648 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001649 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001650 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001651 req->num_pages = 1;
1652 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001653 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001654 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001655 req->end = fuse_writepage_end;
1656 req->inode = inode;
1657
Tejun Heo93f78d82015-05-22 17:13:27 -04001658 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001659 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660
1661 spin_lock(&fc->lock);
1662 list_add(&req->writepages_entry, &fi->writepages);
1663 list_add_tail(&req->list, &fi->queued_writes);
1664 fuse_flush_writepages(inode);
1665 spin_unlock(&fc->lock);
1666
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001667 end_page_writeback(page);
1668
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669 return 0;
1670
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001671err_nofile:
1672 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001673err_free:
1674 fuse_request_free(req);
1675err:
1676 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001677 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001678}
1679
1680static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1681{
1682 int err;
1683
Miklos Szerediff17be02013-10-01 16:44:53 +02001684 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1685 /*
1686 * ->writepages() should be called for sync() and friends. We
1687 * should only get here on direct reclaim and then we are
1688 * allowed to skip a page which is already in flight
1689 */
1690 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1691
1692 redirty_page_for_writepage(wbc, page);
1693 return 0;
1694 }
1695
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001696 err = fuse_writepage_locked(page);
1697 unlock_page(page);
1698
1699 return err;
1700}
1701
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001702struct fuse_fill_wb_data {
1703 struct fuse_req *req;
1704 struct fuse_file *ff;
1705 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001706 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001707};
1708
1709static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1710{
1711 struct fuse_req *req = data->req;
1712 struct inode *inode = data->inode;
1713 struct fuse_conn *fc = get_fuse_conn(inode);
1714 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001715 int num_pages = req->num_pages;
1716 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001717
1718 req->ff = fuse_file_get(data->ff);
1719 spin_lock(&fc->lock);
1720 list_add_tail(&req->list, &fi->queued_writes);
1721 fuse_flush_writepages(inode);
1722 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001723
1724 for (i = 0; i < num_pages; i++)
1725 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001726}
1727
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001728static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1729 struct page *page)
1730{
1731 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1732 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1733 struct fuse_req *tmp;
1734 struct fuse_req *old_req;
1735 bool found = false;
1736 pgoff_t curr_index;
1737
1738 BUG_ON(new_req->num_pages != 0);
1739
1740 spin_lock(&fc->lock);
1741 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001742 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1743 BUG_ON(old_req->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001744 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001745 if (curr_index <= page->index &&
1746 page->index < curr_index + old_req->num_pages) {
1747 found = true;
1748 break;
1749 }
1750 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001751 if (!found) {
1752 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001753 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001754 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001755
Maxim Patlasovf6011082013-10-02 15:01:07 +04001756 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001757 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1758 BUG_ON(tmp->inode != new_req->inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001759 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001760 if (tmp->num_pages == 1 &&
1761 curr_index == page->index) {
1762 old_req = tmp;
1763 }
1764 }
1765
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001766 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001767 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001768
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001769 copy_highpage(old_req->pages[0], page);
1770 spin_unlock(&fc->lock);
1771
Tejun Heo93f78d82015-05-22 17:13:27 -04001772 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001773 dec_node_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001774 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001775 fuse_writepage_free(fc, new_req);
1776 fuse_request_free(new_req);
1777 goto out;
1778 } else {
1779 new_req->misc.write.next = old_req->misc.write.next;
1780 old_req->misc.write.next = new_req;
1781 }
1782out_unlock:
1783 spin_unlock(&fc->lock);
1784out:
1785 return found;
1786}
1787
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001788static int fuse_writepages_fill(struct page *page,
1789 struct writeback_control *wbc, void *_data)
1790{
1791 struct fuse_fill_wb_data *data = _data;
1792 struct fuse_req *req = data->req;
1793 struct inode *inode = data->inode;
1794 struct fuse_conn *fc = get_fuse_conn(inode);
1795 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001796 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001797 int err;
1798
1799 if (!data->ff) {
1800 err = -EIO;
1801 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1802 if (!data->ff)
1803 goto out_unlock;
1804 }
1805
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001806 /*
1807 * Being under writeback is unlikely but possible. For example direct
1808 * read to an mmaped fuse file will set the page dirty twice; once when
1809 * the pages are faulted with get_user_pages(), and then after the read
1810 * completed.
1811 */
1812 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001813
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001814 if (req && req->num_pages &&
1815 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001816 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001817 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1818 fuse_writepages_send(data);
1819 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001820 }
1821 err = -ENOMEM;
1822 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1823 if (!tmp_page)
1824 goto out_unlock;
1825
1826 /*
1827 * The page must not be redirtied until the writeout is completed
1828 * (i.e. userspace has sent a reply to the write request). Otherwise
1829 * there could be more than one temporary page instance for each real
1830 * page.
1831 *
1832 * This is ensured by holding the page lock in page_mkwrite() while
1833 * checking fuse_page_is_writeback(). We already hold the page lock
1834 * since clear_page_dirty_for_io() and keep it held until we add the
1835 * request to the fi->writepages list and increment req->num_pages.
1836 * After this fuse_page_is_writeback() will indicate that the page is
1837 * under writeback, so we can release the page lock.
1838 */
1839 if (data->req == NULL) {
1840 struct fuse_inode *fi = get_fuse_inode(inode);
1841
1842 err = -ENOMEM;
1843 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1844 if (!req) {
1845 __free_page(tmp_page);
1846 goto out_unlock;
1847 }
1848
1849 fuse_write_fill(req, data->ff, page_offset(page), 0);
1850 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001851 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001852 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001853 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001854 req->num_pages = 0;
1855 req->end = fuse_writepage_end;
1856 req->inode = inode;
1857
1858 spin_lock(&fc->lock);
1859 list_add(&req->writepages_entry, &fi->writepages);
1860 spin_unlock(&fc->lock);
1861
1862 data->req = req;
1863 }
1864 set_page_writeback(page);
1865
1866 copy_highpage(tmp_page, page);
1867 req->pages[req->num_pages] = tmp_page;
1868 req->page_descs[req->num_pages].offset = 0;
1869 req->page_descs[req->num_pages].length = PAGE_SIZE;
1870
Tejun Heo93f78d82015-05-22 17:13:27 -04001871 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Mel Gorman11fb9982016-07-28 15:46:20 -07001872 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001873
1874 err = 0;
1875 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1876 end_page_writeback(page);
1877 data->req = NULL;
1878 goto out_unlock;
1879 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001880 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001881
1882 /*
1883 * Protected by fc->lock against concurrent access by
1884 * fuse_page_is_writeback().
1885 */
1886 spin_lock(&fc->lock);
1887 req->num_pages++;
1888 spin_unlock(&fc->lock);
1889
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001890out_unlock:
1891 unlock_page(page);
1892
1893 return err;
1894}
1895
1896static int fuse_writepages(struct address_space *mapping,
1897 struct writeback_control *wbc)
1898{
1899 struct inode *inode = mapping->host;
1900 struct fuse_fill_wb_data data;
1901 int err;
1902
1903 err = -EIO;
1904 if (is_bad_inode(inode))
1905 goto out;
1906
1907 data.inode = inode;
1908 data.req = NULL;
1909 data.ff = NULL;
1910
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001911 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001912 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1913 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001914 GFP_NOFS);
1915 if (!data.orig_pages)
1916 goto out;
1917
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001918 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1919 if (data.req) {
1920 /* Ignore errors if we can write at least one page */
1921 BUG_ON(!data.req->num_pages);
1922 fuse_writepages_send(&data);
1923 err = 0;
1924 }
1925 if (data.ff)
1926 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001927
1928 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001929out:
1930 return err;
1931}
1932
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001933/*
1934 * It's worthy to make sure that space is reserved on disk for the write,
1935 * but how to implement it without killing performance need more thinking.
1936 */
1937static int fuse_write_begin(struct file *file, struct address_space *mapping,
1938 loff_t pos, unsigned len, unsigned flags,
1939 struct page **pagep, void **fsdata)
1940{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001941 pgoff_t index = pos >> PAGE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001942 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001943 struct page *page;
1944 loff_t fsize;
1945 int err = -ENOMEM;
1946
1947 WARN_ON(!fc->writeback_cache);
1948
1949 page = grab_cache_page_write_begin(mapping, index, flags);
1950 if (!page)
1951 goto error;
1952
1953 fuse_wait_on_page_writeback(mapping->host, page->index);
1954
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001955 if (PageUptodate(page) || len == PAGE_SIZE)
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001956 goto success;
1957 /*
1958 * Check if the start this page comes after the end of file, in which
1959 * case the readpage can be optimized away.
1960 */
1961 fsize = i_size_read(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001962 if (fsize <= (pos & PAGE_MASK)) {
1963 size_t off = pos & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001964 if (off)
1965 zero_user_segment(page, 0, off);
1966 goto success;
1967 }
1968 err = fuse_do_readpage(file, page);
1969 if (err)
1970 goto cleanup;
1971success:
1972 *pagep = page;
1973 return 0;
1974
1975cleanup:
1976 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001977 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001978error:
1979 return err;
1980}
1981
1982static int fuse_write_end(struct file *file, struct address_space *mapping,
1983 loff_t pos, unsigned len, unsigned copied,
1984 struct page *page, void *fsdata)
1985{
1986 struct inode *inode = page->mapping->host;
1987
1988 if (!PageUptodate(page)) {
1989 /* Zero any unwritten bytes at the end of the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001990 size_t endoff = (pos + copied) & ~PAGE_MASK;
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001991 if (endoff)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001992 zero_user_segment(page, endoff, PAGE_SIZE);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001993 SetPageUptodate(page);
1994 }
1995
1996 fuse_write_update_size(inode, pos + copied);
1997 set_page_dirty(page);
1998 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001999 put_page(page);
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002000
2001 return copied;
2002}
2003
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002004static int fuse_launder_page(struct page *page)
2005{
2006 int err = 0;
2007 if (clear_page_dirty_for_io(page)) {
2008 struct inode *inode = page->mapping->host;
2009 err = fuse_writepage_locked(page);
2010 if (!err)
2011 fuse_wait_on_page_writeback(inode, page->index);
2012 }
2013 return err;
2014}
2015
2016/*
2017 * Write back dirty pages now, because there may not be any suitable
2018 * open files later
2019 */
2020static void fuse_vma_close(struct vm_area_struct *vma)
2021{
2022 filemap_write_and_wait(vma->vm_file->f_mapping);
2023}
2024
2025/*
2026 * Wait for writeback against this page to complete before allowing it
2027 * to be marked dirty again, and hence written back again, possibly
2028 * before the previous writepage completed.
2029 *
2030 * Block here, instead of in ->writepage(), so that the userspace fs
2031 * can only block processes actually operating on the filesystem.
2032 *
2033 * Otherwise unprivileged userspace fs would be able to block
2034 * unrelated:
2035 *
2036 * - page migration
2037 * - sync(2)
2038 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2039 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002040static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002041{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002042 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002043 struct inode *inode = file_inode(vma->vm_file);
2044
2045 file_update_time(vma->vm_file);
2046 lock_page(page);
2047 if (page->mapping != inode->i_mapping) {
2048 unlock_page(page);
2049 return VM_FAULT_NOPAGE;
2050 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002051
2052 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002053 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002054}
2055
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002056static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002057 .close = fuse_vma_close,
2058 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002059 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002060 .page_mkwrite = fuse_page_mkwrite,
2061};
2062
2063static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2064{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002065 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2066 fuse_link_write_file(file);
2067
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002068 file_accessed(file);
2069 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002070 return 0;
2071}
2072
Miklos Szeredifc280c92009-04-02 14:25:35 +02002073static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2074{
2075 /* Can't provide the coherency needed for MAP_SHARED */
2076 if (vma->vm_flags & VM_MAYSHARE)
2077 return -ENODEV;
2078
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002079 invalidate_inode_pages2(file->f_mapping);
2080
Miklos Szeredifc280c92009-04-02 14:25:35 +02002081 return generic_file_mmap(file, vma);
2082}
2083
Miklos Szeredi71421252006-06-25 05:48:52 -07002084static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2085 struct file_lock *fl)
2086{
2087 switch (ffl->type) {
2088 case F_UNLCK:
2089 break;
2090
2091 case F_RDLCK:
2092 case F_WRLCK:
2093 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2094 ffl->end < ffl->start)
2095 return -EIO;
2096
2097 fl->fl_start = ffl->start;
2098 fl->fl_end = ffl->end;
2099 fl->fl_pid = ffl->pid;
2100 break;
2101
2102 default:
2103 return -EIO;
2104 }
2105 fl->fl_type = ffl->type;
2106 return 0;
2107}
2108
Miklos Szeredi70781872014-12-12 09:49:05 +01002109static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002110 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002111 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002112{
Al Viro6131ffa2013-02-27 16:59:05 -05002113 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002114 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002115 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002116
Miklos Szeredi70781872014-12-12 09:49:05 +01002117 memset(inarg, 0, sizeof(*inarg));
2118 inarg->fh = ff->fh;
2119 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2120 inarg->lk.start = fl->fl_start;
2121 inarg->lk.end = fl->fl_end;
2122 inarg->lk.type = fl->fl_type;
2123 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002124 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002125 inarg->lk_flags |= FUSE_LK_FLOCK;
2126 args->in.h.opcode = opcode;
2127 args->in.h.nodeid = get_node_id(inode);
2128 args->in.numargs = 1;
2129 args->in.args[0].size = sizeof(*inarg);
2130 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002131}
2132
2133static int fuse_getlk(struct file *file, struct file_lock *fl)
2134{
Al Viro6131ffa2013-02-27 16:59:05 -05002135 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002136 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002137 FUSE_ARGS(args);
2138 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002139 struct fuse_lk_out outarg;
2140 int err;
2141
Miklos Szeredi70781872014-12-12 09:49:05 +01002142 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2143 args.out.numargs = 1;
2144 args.out.args[0].size = sizeof(outarg);
2145 args.out.args[0].value = &outarg;
2146 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002147 if (!err)
2148 err = convert_fuse_file_lock(&outarg.lk, fl);
2149
2150 return err;
2151}
2152
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002153static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002154{
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 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2160 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2161 int err;
2162
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002163 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002164 /* NLM needs asynchronous locks, which we don't support yet */
2165 return -ENOLCK;
2166 }
2167
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 /* Unlock on close is handled by the flush method */
2169 if (fl->fl_flags & FL_CLOSE)
2170 return 0;
2171
Miklos Szeredi70781872014-12-12 09:49:05 +01002172 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2173 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002174
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002175 /* locking is restartable */
2176 if (err == -EINTR)
2177 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002178
Miklos Szeredi71421252006-06-25 05:48:52 -07002179 return err;
2180}
2181
2182static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2183{
Al Viro6131ffa2013-02-27 16:59:05 -05002184 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002185 struct fuse_conn *fc = get_fuse_conn(inode);
2186 int err;
2187
Miklos Szeredi48e90762008-07-25 01:49:02 -07002188 if (cmd == F_CANCELLK) {
2189 err = 0;
2190 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002192 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002193 err = 0;
2194 } else
2195 err = fuse_getlk(file, fl);
2196 } else {
2197 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002198 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002199 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002200 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002201 }
2202 return err;
2203}
2204
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002205static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2206{
Al Viro6131ffa2013-02-27 16:59:05 -05002207 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002208 struct fuse_conn *fc = get_fuse_conn(inode);
2209 int err;
2210
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002211 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002212 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002213 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002214 struct fuse_file *ff = file->private_data;
2215
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002216 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002217 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002218 err = fuse_setlk(file, fl, 1);
2219 }
2220
2221 return err;
2222}
2223
Miklos Szeredib2d22722006-12-06 20:35:51 -08002224static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2225{
2226 struct inode *inode = mapping->host;
2227 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002228 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002229 struct fuse_bmap_in inarg;
2230 struct fuse_bmap_out outarg;
2231 int err;
2232
2233 if (!inode->i_sb->s_bdev || fc->no_bmap)
2234 return 0;
2235
Miklos Szeredib2d22722006-12-06 20:35:51 -08002236 memset(&inarg, 0, sizeof(inarg));
2237 inarg.block = block;
2238 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002239 args.in.h.opcode = FUSE_BMAP;
2240 args.in.h.nodeid = get_node_id(inode);
2241 args.in.numargs = 1;
2242 args.in.args[0].size = sizeof(inarg);
2243 args.in.args[0].value = &inarg;
2244 args.out.numargs = 1;
2245 args.out.args[0].size = sizeof(outarg);
2246 args.out.args[0].value = &outarg;
2247 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002248 if (err == -ENOSYS)
2249 fc->no_bmap = 1;
2250
2251 return err ? 0 : outarg.block;
2252}
2253
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302254static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2255{
2256 struct inode *inode = file->f_mapping->host;
2257 struct fuse_conn *fc = get_fuse_conn(inode);
2258 struct fuse_file *ff = file->private_data;
2259 FUSE_ARGS(args);
2260 struct fuse_lseek_in inarg = {
2261 .fh = ff->fh,
2262 .offset = offset,
2263 .whence = whence
2264 };
2265 struct fuse_lseek_out outarg;
2266 int err;
2267
2268 if (fc->no_lseek)
2269 goto fallback;
2270
2271 args.in.h.opcode = FUSE_LSEEK;
2272 args.in.h.nodeid = ff->nodeid;
2273 args.in.numargs = 1;
2274 args.in.args[0].size = sizeof(inarg);
2275 args.in.args[0].value = &inarg;
2276 args.out.numargs = 1;
2277 args.out.args[0].size = sizeof(outarg);
2278 args.out.args[0].value = &outarg;
2279 err = fuse_simple_request(fc, &args);
2280 if (err) {
2281 if (err == -ENOSYS) {
2282 fc->no_lseek = 1;
2283 goto fallback;
2284 }
2285 return err;
2286 }
2287
2288 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2289
2290fallback:
2291 err = fuse_update_attributes(inode, NULL, file, NULL);
2292 if (!err)
2293 return generic_file_llseek(file, offset, whence);
2294 else
2295 return err;
2296}
2297
Andrew Morton965c8e52012-12-17 15:59:39 -08002298static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002299{
2300 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002301 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002302
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302303 switch (whence) {
2304 case SEEK_SET:
2305 case SEEK_CUR:
2306 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002307 retval = generic_file_llseek(file, offset, whence);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302308 break;
2309 case SEEK_END:
Al Viro59551022016-01-22 15:40:57 -05002310 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302311 retval = fuse_update_attributes(inode, NULL, file, NULL);
2312 if (!retval)
2313 retval = generic_file_llseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002314 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302315 break;
2316 case SEEK_HOLE:
2317 case SEEK_DATA:
Al Viro59551022016-01-22 15:40:57 -05002318 inode_lock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302319 retval = fuse_lseek(file, offset, whence);
Al Viro59551022016-01-22 15:40:57 -05002320 inode_unlock(inode);
Ravishankar N0b5da8d2015-06-30 23:40:22 +05302321 break;
2322 default:
2323 retval = -EINVAL;
2324 }
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002325
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002326 return retval;
2327}
2328
Tejun Heo59efec72008-11-26 12:03:55 +01002329/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002330 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2331 * ABI was defined to be 'struct iovec' which is different on 32bit
2332 * and 64bit. Fortunately we can determine which structure the server
2333 * used from the size of the reply.
2334 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002335static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2336 size_t transferred, unsigned count,
2337 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002338{
2339#ifdef CONFIG_COMPAT
2340 if (count * sizeof(struct compat_iovec) == transferred) {
2341 struct compat_iovec *ciov = src;
2342 unsigned i;
2343
2344 /*
2345 * With this interface a 32bit server cannot support
2346 * non-compat (i.e. ones coming from 64bit apps) ioctl
2347 * requests
2348 */
2349 if (!is_compat)
2350 return -EINVAL;
2351
2352 for (i = 0; i < count; i++) {
2353 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2354 dst[i].iov_len = ciov[i].iov_len;
2355 }
2356 return 0;
2357 }
2358#endif
2359
2360 if (count * sizeof(struct iovec) != transferred)
2361 return -EIO;
2362
2363 memcpy(dst, src, transferred);
2364 return 0;
2365}
2366
Miklos Szeredi75727772010-11-30 16:39:27 +01002367/* Make sure iov_length() won't overflow */
2368static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2369{
2370 size_t n;
2371 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2372
Zach Brownfb6ccff2012-07-24 12:10:11 -07002373 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002374 if (iov->iov_len > (size_t) max)
2375 return -ENOMEM;
2376 max -= iov->iov_len;
2377 }
2378 return 0;
2379}
2380
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002381static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2382 void *src, size_t transferred, unsigned count,
2383 bool is_compat)
2384{
2385 unsigned i;
2386 struct fuse_ioctl_iovec *fiov = src;
2387
2388 if (fc->minor < 16) {
2389 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2390 count, is_compat);
2391 }
2392
2393 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2394 return -EIO;
2395
2396 for (i = 0; i < count; i++) {
2397 /* Did the server supply an inappropriate value? */
2398 if (fiov[i].base != (unsigned long) fiov[i].base ||
2399 fiov[i].len != (unsigned long) fiov[i].len)
2400 return -EIO;
2401
2402 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2403 dst[i].iov_len = (size_t) fiov[i].len;
2404
2405#ifdef CONFIG_COMPAT
2406 if (is_compat &&
2407 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2408 (compat_size_t) dst[i].iov_len != fiov[i].len))
2409 return -EIO;
2410#endif
2411 }
2412
2413 return 0;
2414}
2415
2416
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002417/*
Tejun Heo59efec72008-11-26 12:03:55 +01002418 * For ioctls, there is no generic way to determine how much memory
2419 * needs to be read and/or written. Furthermore, ioctls are allowed
2420 * to dereference the passed pointer, so the parameter requires deep
2421 * copying but FUSE has no idea whatsoever about what to copy in or
2422 * out.
2423 *
2424 * This is solved by allowing FUSE server to retry ioctl with
2425 * necessary in/out iovecs. Let's assume the ioctl implementation
2426 * needs to read in the following structure.
2427 *
2428 * struct a {
2429 * char *buf;
2430 * size_t buflen;
2431 * }
2432 *
2433 * On the first callout to FUSE server, inarg->in_size and
2434 * inarg->out_size will be NULL; then, the server completes the ioctl
2435 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2436 * the actual iov array to
2437 *
2438 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2439 *
2440 * which tells FUSE to copy in the requested area and retry the ioctl.
2441 * On the second round, the server has access to the structure and
2442 * from that it can tell what to look for next, so on the invocation,
2443 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2444 *
2445 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2446 * { .iov_base = a.buf, .iov_len = a.buflen } }
2447 *
2448 * FUSE will copy both struct a and the pointed buffer from the
2449 * process doing the ioctl and retry ioctl with both struct a and the
2450 * buffer.
2451 *
2452 * This time, FUSE server has everything it needs and completes ioctl
2453 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2454 *
2455 * Copying data out works the same way.
2456 *
2457 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2458 * automatically initializes in and out iovs by decoding @cmd with
2459 * _IOC_* macros and the server is not allowed to request RETRY. This
2460 * limits ioctl data transfers to well-formed ioctls and is the forced
2461 * behavior for all FUSE servers.
2462 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002463long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2464 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002465{
Tejun Heo59efec72008-11-26 12:03:55 +01002466 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002467 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002468 struct fuse_ioctl_in inarg = {
2469 .fh = ff->fh,
2470 .cmd = cmd,
2471 .arg = arg,
2472 .flags = flags
2473 };
2474 struct fuse_ioctl_out outarg;
2475 struct fuse_req *req = NULL;
2476 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002477 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002478 struct iovec *in_iov = NULL, *out_iov = NULL;
2479 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002480 size_t in_size, out_size, transferred, c;
2481 int err, i;
2482 struct iov_iter ii;
Tejun Heo59efec72008-11-26 12:03:55 +01002483
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002484#if BITS_PER_LONG == 32
2485 inarg.flags |= FUSE_IOCTL_32BIT;
2486#else
2487 if (flags & FUSE_IOCTL_COMPAT)
2488 inarg.flags |= FUSE_IOCTL_32BIT;
2489#endif
2490
Tejun Heo59efec72008-11-26 12:03:55 +01002491 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002492 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002493
Tejun Heo59efec72008-11-26 12:03:55 +01002494 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002495 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002496 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002497 if (!pages || !iov_page)
2498 goto out;
2499
2500 /*
2501 * If restricted, initialize IO parameters as encoded in @cmd.
2502 * RETRY from server is not allowed.
2503 */
2504 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002505 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002506
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002507 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002508 iov->iov_len = _IOC_SIZE(cmd);
2509
2510 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2511 in_iov = iov;
2512 in_iovs = 1;
2513 }
2514
2515 if (_IOC_DIR(cmd) & _IOC_READ) {
2516 out_iov = iov;
2517 out_iovs = 1;
2518 }
2519 }
2520
2521 retry:
2522 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2523 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2524
2525 /*
2526 * Out data can be used either for actual out data or iovs,
2527 * make sure there always is at least one page.
2528 */
2529 out_size = max_t(size_t, out_size, PAGE_SIZE);
2530 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2531
2532 /* make sure there are enough buffer pages and init request with them */
2533 err = -ENOMEM;
2534 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2535 goto out;
2536 while (num_pages < max_pages) {
2537 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2538 if (!pages[num_pages])
2539 goto out;
2540 num_pages++;
2541 }
2542
Maxim Patlasov54b96672012-10-26 19:49:13 +04002543 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002544 if (IS_ERR(req)) {
2545 err = PTR_ERR(req);
2546 req = NULL;
2547 goto out;
2548 }
2549 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2550 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002551 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002552
2553 /* okay, let's send it to the client */
2554 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002555 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002556 req->in.numargs = 1;
2557 req->in.args[0].size = sizeof(inarg);
2558 req->in.args[0].value = &inarg;
2559 if (in_size) {
2560 req->in.numargs++;
2561 req->in.args[1].size = in_size;
2562 req->in.argpages = 1;
2563
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002564 err = -EFAULT;
2565 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2566 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2567 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2568 if (c != PAGE_SIZE && iov_iter_count(&ii))
2569 goto out;
2570 }
Tejun Heo59efec72008-11-26 12:03:55 +01002571 }
2572
2573 req->out.numargs = 2;
2574 req->out.args[0].size = sizeof(outarg);
2575 req->out.args[0].value = &outarg;
2576 req->out.args[1].size = out_size;
2577 req->out.argpages = 1;
2578 req->out.argvar = 1;
2579
Tejun Heob93f8582008-11-26 12:03:55 +01002580 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002581 err = req->out.h.error;
2582 transferred = req->out.args[1].size;
2583 fuse_put_request(fc, req);
2584 req = NULL;
2585 if (err)
2586 goto out;
2587
2588 /* did it ask for retry? */
2589 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002590 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002591
2592 /* no retry if in restricted mode */
2593 err = -EIO;
2594 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2595 goto out;
2596
2597 in_iovs = outarg.in_iovs;
2598 out_iovs = outarg.out_iovs;
2599
2600 /*
2601 * Make sure things are in boundary, separate checks
2602 * are to protect against overflow.
2603 */
2604 err = -ENOMEM;
2605 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2606 out_iovs > FUSE_IOCTL_MAX_IOV ||
2607 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2608 goto out;
2609
Cong Wang2408f6e2011-11-25 23:14:30 +08002610 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002611 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002612 transferred, in_iovs + out_iovs,
2613 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002614 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002615 if (err)
2616 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002617
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002618 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002619 out_iov = in_iov + in_iovs;
2620
Miklos Szeredi75727772010-11-30 16:39:27 +01002621 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2622 if (err)
2623 goto out;
2624
2625 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2626 if (err)
2627 goto out;
2628
Tejun Heo59efec72008-11-26 12:03:55 +01002629 goto retry;
2630 }
2631
2632 err = -EIO;
2633 if (transferred > inarg.out_size)
2634 goto out;
2635
Miklos Szerediacbe5fd2016-10-01 07:32:33 +02002636 err = -EFAULT;
2637 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2638 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2639 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2640 if (c != PAGE_SIZE && iov_iter_count(&ii))
2641 goto out;
2642 }
2643 err = 0;
Tejun Heo59efec72008-11-26 12:03:55 +01002644 out:
2645 if (req)
2646 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002647 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002648 while (num_pages)
2649 __free_page(pages[--num_pages]);
2650 kfree(pages);
2651
2652 return err ? err : outarg.result;
2653}
Tejun Heo08cbf542009-04-14 10:54:53 +09002654EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002655
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002656long fuse_ioctl_common(struct file *file, unsigned int cmd,
2657 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002658{
Al Viro6131ffa2013-02-27 16:59:05 -05002659 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002660 struct fuse_conn *fc = get_fuse_conn(inode);
2661
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002662 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002663 return -EACCES;
2664
2665 if (is_bad_inode(inode))
2666 return -EIO;
2667
2668 return fuse_do_ioctl(file, cmd, arg, flags);
2669}
2670
Tejun Heo59efec72008-11-26 12:03:55 +01002671static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2672 unsigned long arg)
2673{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002674 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002675}
2676
2677static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2678 unsigned long arg)
2679{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002680 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002681}
2682
Tejun Heo95668a62008-11-26 12:03:55 +01002683/*
2684 * All files which have been polled are linked to RB tree
2685 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2686 * find the matching one.
2687 */
2688static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2689 struct rb_node **parent_out)
2690{
2691 struct rb_node **link = &fc->polled_files.rb_node;
2692 struct rb_node *last = NULL;
2693
2694 while (*link) {
2695 struct fuse_file *ff;
2696
2697 last = *link;
2698 ff = rb_entry(last, struct fuse_file, polled_node);
2699
2700 if (kh < ff->kh)
2701 link = &last->rb_left;
2702 else if (kh > ff->kh)
2703 link = &last->rb_right;
2704 else
2705 return link;
2706 }
2707
2708 if (parent_out)
2709 *parent_out = last;
2710 return link;
2711}
2712
2713/*
2714 * The file is about to be polled. Make sure it's on the polled_files
2715 * RB tree. Note that files once added to the polled_files tree are
2716 * not removed before the file is released. This is because a file
2717 * polled once is likely to be polled again.
2718 */
2719static void fuse_register_polled_file(struct fuse_conn *fc,
2720 struct fuse_file *ff)
2721{
2722 spin_lock(&fc->lock);
2723 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002724 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002725
2726 link = fuse_find_polled_node(fc, ff->kh, &parent);
2727 BUG_ON(*link);
2728 rb_link_node(&ff->polled_node, parent, link);
2729 rb_insert_color(&ff->polled_node, &fc->polled_files);
2730 }
2731 spin_unlock(&fc->lock);
2732}
2733
Tejun Heo08cbf542009-04-14 10:54:53 +09002734unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002735{
Tejun Heo95668a62008-11-26 12:03:55 +01002736 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002737 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002738 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2739 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002740 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002741 int err;
2742
2743 if (fc->no_poll)
2744 return DEFAULT_POLLMASK;
2745
2746 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002747 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002748
2749 /*
2750 * Ask for notification iff there's someone waiting for it.
2751 * The client may ignore the flag and always notify.
2752 */
2753 if (waitqueue_active(&ff->poll_wait)) {
2754 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2755 fuse_register_polled_file(fc, ff);
2756 }
2757
Miklos Szeredi70781872014-12-12 09:49:05 +01002758 args.in.h.opcode = FUSE_POLL;
2759 args.in.h.nodeid = ff->nodeid;
2760 args.in.numargs = 1;
2761 args.in.args[0].size = sizeof(inarg);
2762 args.in.args[0].value = &inarg;
2763 args.out.numargs = 1;
2764 args.out.args[0].size = sizeof(outarg);
2765 args.out.args[0].value = &outarg;
2766 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002767
2768 if (!err)
2769 return outarg.revents;
2770 if (err == -ENOSYS) {
2771 fc->no_poll = 1;
2772 return DEFAULT_POLLMASK;
2773 }
2774 return POLLERR;
2775}
Tejun Heo08cbf542009-04-14 10:54:53 +09002776EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002777
2778/*
2779 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2780 * wakes up the poll waiters.
2781 */
2782int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2783 struct fuse_notify_poll_wakeup_out *outarg)
2784{
2785 u64 kh = outarg->kh;
2786 struct rb_node **link;
2787
2788 spin_lock(&fc->lock);
2789
2790 link = fuse_find_polled_node(fc, kh, NULL);
2791 if (*link) {
2792 struct fuse_file *ff;
2793
2794 ff = rb_entry(*link, struct fuse_file, polled_node);
2795 wake_up_interruptible_sync(&ff->poll_wait);
2796 }
2797
2798 spin_unlock(&fc->lock);
2799 return 0;
2800}
2801
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002802static void fuse_do_truncate(struct file *file)
2803{
2804 struct inode *inode = file->f_mapping->host;
2805 struct iattr attr;
2806
2807 attr.ia_valid = ATTR_SIZE;
2808 attr.ia_size = i_size_read(inode);
2809
2810 attr.ia_file = file;
2811 attr.ia_valid |= ATTR_FILE;
2812
Jan Kara62490332016-05-26 17:12:41 +02002813 fuse_do_setattr(file_dentry(file), &attr, file);
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002814}
2815
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002816static inline loff_t fuse_round_up(loff_t off)
2817{
2818 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2819}
2820
Anand Avati4273b792012-02-17 12:46:25 -05002821static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002822fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002823{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002824 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002825 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002826 struct file *file = iocb->ki_filp;
2827 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002828 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002829 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002830 struct inode *inode;
2831 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002832 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002833 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002834 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002835
Anand Avati4273b792012-02-17 12:46:25 -05002836 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002837 inode = file->f_mapping->host;
2838 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002839
Omar Sandoval6f673762015-03-16 04:33:52 -07002840 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002841 return 0;
2842
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002843 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002844 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002845 if (offset >= i_size)
2846 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002847 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2848 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002849 }
2850
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002851 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002852 if (!io)
2853 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002854 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002855 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002856 io->reqs = 1;
2857 io->bytes = -1;
2858 io->size = 0;
2859 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002860 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002861 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002862 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002863 /*
2864 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002865 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002866 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002867 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002868 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302869 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002870
2871 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302872 * We cannot asynchronously extend the size of a file.
2873 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002874 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302875 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2876 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002877
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302878 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002879 /*
2880 * Additional reference to keep io around after
2881 * calling fuse_aio_complete()
2882 */
2883 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002884 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002885 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002886
Omar Sandoval6f673762015-03-16 04:33:52 -07002887 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002888 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002889 fuse_invalidate_attr(inode);
2890 } else {
Al Virod22a9432014-03-16 15:50:47 -04002891 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002892 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002893
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 if (io->async) {
2895 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2896
2897 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302898 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002899 return -EIOCBQUEUED;
2900
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002901 wait_for_completion(&wait);
2902 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002903 }
2904
Seth Forshee744742d2016-03-11 10:35:34 -06002905 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002906
Omar Sandoval6f673762015-03-16 04:33:52 -07002907 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002908 if (ret > 0)
2909 fuse_write_update_size(inode, pos);
2910 else if (ret < 0 && offset + count > i_size)
2911 fuse_do_truncate(file);
2912 }
Anand Avati4273b792012-02-17 12:46:25 -05002913
2914 return ret;
2915}
2916
Miklos Szeredicdadb112012-11-10 16:55:56 +01002917static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2918 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002919{
2920 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002921 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002922 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002923 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002924 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002925 struct fuse_fallocate_in inarg = {
2926 .fh = ff->fh,
2927 .offset = offset,
2928 .length = length,
2929 .mode = mode
2930 };
2931 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002932 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2933 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002934
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002935 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2936 return -EOPNOTSUPP;
2937
Miklos Szeredi519c6042012-04-26 10:56:36 +02002938 if (fc->no_fallocate)
2939 return -EOPNOTSUPP;
2940
Maxim Patlasov14c14412013-06-13 12:16:39 +04002941 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002942 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002943 if (mode & FALLOC_FL_PUNCH_HOLE) {
2944 loff_t endbyte = offset + length - 1;
2945 err = filemap_write_and_wait_range(inode->i_mapping,
2946 offset, endbyte);
2947 if (err)
2948 goto out;
2949
2950 fuse_sync_writes(inode);
2951 }
Brian Foster3634a632013-05-17 09:30:32 -04002952 }
2953
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002954 if (!(mode & FALLOC_FL_KEEP_SIZE))
2955 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2956
Miklos Szeredi70781872014-12-12 09:49:05 +01002957 args.in.h.opcode = FUSE_FALLOCATE;
2958 args.in.h.nodeid = ff->nodeid;
2959 args.in.numargs = 1;
2960 args.in.args[0].size = sizeof(inarg);
2961 args.in.args[0].value = &inarg;
2962 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002963 if (err == -ENOSYS) {
2964 fc->no_fallocate = 1;
2965 err = -EOPNOTSUPP;
2966 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002967 if (err)
2968 goto out;
2969
2970 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002971 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2972 bool changed = fuse_write_update_size(inode, offset + length);
2973
Miklos Szeredi93d22692014-04-28 14:19:22 +02002974 if (changed && fc->writeback_cache)
2975 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002976 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002977
2978 if (mode & FALLOC_FL_PUNCH_HOLE)
2979 truncate_pagecache_range(inode, offset, offset + length - 1);
2980
2981 fuse_invalidate_attr(inode);
2982
Brian Foster3634a632013-05-17 09:30:32 -04002983out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002984 if (!(mode & FALLOC_FL_KEEP_SIZE))
2985 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2986
Maxim Patlasovbde52782013-09-13 19:19:54 +04002987 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05002988 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04002989
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002990 return err;
2991}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002992
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002993static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002994 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04002995 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04002996 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002997 .mmap = fuse_file_mmap,
2998 .open = fuse_open,
2999 .flush = fuse_flush,
3000 .release = fuse_release,
3001 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003002 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003003 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003004 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003005 .unlocked_ioctl = fuse_file_ioctl,
3006 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003007 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003008 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003009};
3010
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003011static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003012 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003013 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003014 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003015 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003016 .open = fuse_open,
3017 .flush = fuse_flush,
3018 .release = fuse_release,
3019 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003020 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003021 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003022 .unlocked_ioctl = fuse_file_ioctl,
3023 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003024 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003025 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003026 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003027};
3028
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003029static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003030 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003031 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003032 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003033 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003034 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003035 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003036 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003037 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003038 .write_begin = fuse_write_begin,
3039 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003040};
3041
3042void fuse_init_file_inode(struct inode *inode)
3043{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003044 inode->i_fop = &fuse_file_operations;
3045 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003046}