blob: 2491f388358baa0c44db0b460bfb2e0ac735428f [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
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400420 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421 memset(&inarg, 0, sizeof(inarg));
422 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700423 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700424 req->in.h.opcode = FUSE_FLUSH;
425 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426 req->in.numargs = 1;
427 req->in.args[0].size = sizeof(inarg);
428 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200429 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100430 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 err = req->out.h.error;
432 fuse_put_request(fc, req);
433 if (err == -ENOSYS) {
434 fc->no_flush = 1;
435 err = 0;
436 }
437 return err;
438}
439
Josef Bacik02c24a82011-07-16 20:44:56 -0400440int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
441 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700442{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200443 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 struct fuse_conn *fc = get_fuse_conn(inode);
445 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100446 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 struct fuse_fsync_in inarg;
448 int err;
449
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800450 if (is_bad_inode(inode))
451 return -EIO;
452
Al Viro59551022016-01-22 15:40:57 -0500453 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400454
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700455 /*
456 * Start writeback against all dirty pages of the inode, then
457 * wait for all outstanding writes, before sending the FSYNC
458 * request.
459 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200460 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700461 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400462 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700463
464 fuse_sync_writes(inode);
Alexey Kuznetsovac7f0522016-07-19 12:48:01 -0700465
466 /*
467 * Due to implementation of fuse writeback
468 * filemap_write_and_wait_range() does not catch errors.
469 * We have to do this directly after fuse_sync_writes()
470 */
471 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
472 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
473 err = -ENOSPC;
474 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
475 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
476 err = -EIO;
477 if (err)
478 goto out;
479
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200480 err = sync_inode_metadata(inode, 1);
481 if (err)
482 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700483
Miklos Szeredi22401e72014-04-28 14:19:23 +0200484 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
485 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400486
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700487 memset(&inarg, 0, sizeof(inarg));
488 inarg.fh = ff->fh;
489 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100490 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
491 args.in.h.nodeid = get_node_id(inode);
492 args.in.numargs = 1;
493 args.in.args[0].size = sizeof(inarg);
494 args.in.args[0].value = &inarg;
495 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700496 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700497 if (isdir)
498 fc->no_fsyncdir = 1;
499 else
500 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501 err = 0;
502 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400503out:
Al Viro59551022016-01-22 15:40:57 -0500504 inode_unlock(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505 return err;
506}
507
Josef Bacik02c24a82011-07-16 20:44:56 -0400508static int fuse_fsync(struct file *file, loff_t start, loff_t end,
509 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700510{
Josef Bacik02c24a82011-07-16 20:44:56 -0400511 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700512}
513
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200514void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
515 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700517 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800518 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800520 inarg->fh = ff->fh;
521 inarg->offset = pos;
522 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800523 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800524 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200525 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526 req->in.numargs = 1;
527 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800528 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 req->out.argvar = 1;
530 req->out.numargs = 1;
531 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700532}
533
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400534static void fuse_release_user_pages(struct fuse_req *req, int write)
535{
536 unsigned i;
537
538 for (i = 0; i < req->num_pages; i++) {
539 struct page *page = req->pages[i];
540 if (write)
541 set_page_dirty_lock(page);
542 put_page(page);
543 }
544}
545
Seth Forshee744742d2016-03-11 10:35:34 -0600546static void fuse_io_release(struct kref *kref)
547{
548 kfree(container_of(kref, struct fuse_io_priv, refcnt));
549}
550
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100551static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
552{
553 if (io->err)
554 return io->err;
555
556 if (io->bytes >= 0 && io->write)
557 return -EIO;
558
559 return io->bytes < 0 ? io->size : io->bytes;
560}
561
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400562/**
563 * In case of short read, the caller sets 'pos' to the position of
564 * actual end of fuse request in IO request. Otherwise, if bytes_requested
565 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
566 *
567 * An example:
568 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
569 * both submitted asynchronously. The first of them was ACKed by userspace as
570 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
571 * second request was ACKed as short, e.g. only 1K was read, resulting in
572 * pos == 33K.
573 *
574 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
575 * will be equal to the length of the longest contiguous fragment of
576 * transferred data starting from the beginning of IO request.
577 */
578static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
579{
580 int left;
581
582 spin_lock(&io->lock);
583 if (err)
584 io->err = io->err ? : err;
585 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
586 io->bytes = pos;
587
588 left = --io->reqs;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530589 if (!left && io->blocking)
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100590 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400591 spin_unlock(&io->lock);
592
Ashish Sangwan7879c4e2016-04-07 17:18:11 +0530593 if (!left && !io->blocking) {
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100594 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400595
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100596 if (res >= 0) {
597 struct inode *inode = file_inode(io->iocb->ki_filp);
598 struct fuse_conn *fc = get_fuse_conn(inode);
599 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400600
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100601 spin_lock(&fc->lock);
602 fi->attr_version = ++fc->attr_version;
603 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400604 }
605
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100606 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400607 }
Seth Forshee744742d2016-03-11 10:35:34 -0600608
609 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400610}
611
612static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
613{
614 struct fuse_io_priv *io = req->io;
615 ssize_t pos = -1;
616
617 fuse_release_user_pages(req, !io->write);
618
619 if (io->write) {
620 if (req->misc.write.in.size != req->misc.write.out.size)
621 pos = req->misc.write.in.offset - io->offset +
622 req->misc.write.out.size;
623 } else {
624 if (req->misc.read.in.size != req->out.args[0].size)
625 pos = req->misc.read.in.offset - io->offset +
626 req->out.args[0].size;
627 }
628
629 fuse_aio_complete(io, req->out.h.error, pos);
630}
631
632static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
633 size_t num_bytes, struct fuse_io_priv *io)
634{
635 spin_lock(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -0600636 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400637 io->size += num_bytes;
638 io->reqs++;
639 spin_unlock(&io->lock);
640
641 req->io = io;
642 req->end = fuse_aio_complete_req;
643
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400644 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400645 fuse_request_send_background(fc, req);
646
647 return num_bytes;
648}
649
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400650static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200651 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700652{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400653 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200654 struct fuse_file *ff = file->private_data;
655 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700656
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200657 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700658 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700659 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700660
661 inarg->read_flags |= FUSE_READ_LOCKOWNER;
662 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
663 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400664
665 if (io->async)
666 return fuse_async_req_send(fc, req, count, io);
667
Tejun Heob93f8582008-11-26 12:03:55 +0100668 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800669 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700670}
671
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700672static void fuse_read_update_size(struct inode *inode, loff_t size,
673 u64 attr_ver)
674{
675 struct fuse_conn *fc = get_fuse_conn(inode);
676 struct fuse_inode *fi = get_fuse_inode(inode);
677
678 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400679 if (attr_ver == fi->attr_version && size < inode->i_size &&
680 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700681 fi->attr_version = ++fc->attr_version;
682 i_size_write(inode, size);
683 }
684 spin_unlock(&fc->lock);
685}
686
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400687static void fuse_short_read(struct fuse_req *req, struct inode *inode,
688 u64 attr_ver)
689{
690 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400691 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400692
Pavel Emelyanov83732002013-10-10 17:10:46 +0400693 if (fc->writeback_cache) {
694 /*
695 * A hole in a file. Some data after the hole are in page cache,
696 * but have not reached the client fs yet. So, the hole is not
697 * present there.
698 */
699 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300700 int start_idx = num_read >> PAGE_SHIFT;
701 size_t off = num_read & (PAGE_SIZE - 1);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400702
703 for (i = start_idx; i < req->num_pages; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300704 zero_user_segment(req->pages[i], off, PAGE_SIZE);
Pavel Emelyanov83732002013-10-10 17:10:46 +0400705 off = 0;
706 }
707 } else {
708 loff_t pos = page_offset(req->pages[0]) + num_read;
709 fuse_read_update_size(inode, pos, attr_ver);
710 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400711}
712
Maxim Patlasov482fce52013-10-10 17:11:25 +0400713static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700714{
Seth Forshee744742d2016-03-11 10:35:34 -0600715 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700716 struct inode *inode = page->mapping->host;
717 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800718 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700719 size_t num_read;
720 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300721 size_t count = PAGE_SIZE;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700722 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800723 int err;
724
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700725 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300726 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700727 * page-cache page, so make sure we read a properly synced
728 * page.
729 */
730 fuse_wait_on_page_writeback(inode, page->index);
731
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400732 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700733 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400734 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700735
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700736 attr_ver = fuse_get_attr_version(fc);
737
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200739 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700740 req->num_pages = 1;
741 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400742 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400743 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700744 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700745
746 if (!err) {
747 /*
748 * Short read means EOF. If file size is larger, truncate it
749 */
750 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400751 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700752
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 }
755
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400756 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400757
758 return err;
759}
760
761static int fuse_readpage(struct file *file, struct page *page)
762{
763 struct inode *inode = page->mapping->host;
764 int err;
765
766 err = -EIO;
767 if (is_bad_inode(inode))
768 goto out;
769
770 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800771 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700772 out:
773 unlock_page(page);
774 return err;
775}
776
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800777static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700778{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800779 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700780 size_t count = req->misc.read.in.size;
781 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200782 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800783
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 for (i = 0; mapping == NULL && i < req->num_pages; i++)
785 mapping = req->pages[i]->mapping;
786
787 if (mapping) {
788 struct inode *inode = mapping->host;
789
790 /*
791 * Short read means EOF. If file size is larger, truncate it
792 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400793 if (!req->out.h.error && num_read < count)
794 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795
Andrew Gallagher451418f2013-11-05 03:55:43 -0800796 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700797 }
798
Miklos Szeredidb50b962005-09-09 13:10:33 -0700799 for (i = 0; i < req->num_pages; i++) {
800 struct page *page = req->pages[i];
801 if (!req->out.h.error)
802 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800803 else
804 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700805 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300806 put_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700807 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700808 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100809 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800810}
811
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200812static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200814 struct fuse_file *ff = file->private_data;
815 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800816 loff_t pos = page_offset(req->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300817 size_t count = req->num_pages << PAGE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200818
819 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800820 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200821 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200822 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700823 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800824 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700825 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800826 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100827 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800828 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100829 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800830 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100831 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800832 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700833}
834
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700835struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700836 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800837 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400839 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840};
841
842static int fuse_readpages_fill(void *_data, struct page *page)
843{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700844 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 struct fuse_req *req = data->req;
846 struct inode *inode = data->inode;
847 struct fuse_conn *fc = get_fuse_conn(inode);
848
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700849 fuse_wait_on_page_writeback(inode, page->index);
850
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851 if (req->num_pages &&
852 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300853 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400855 int nr_alloc = min_t(unsigned, data->nr_pages,
856 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200857 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400858 if (fc->async_read)
859 req = fuse_get_req_for_background(fc, nr_alloc);
860 else
861 req = fuse_get_req(fc, nr_alloc);
862
863 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700864 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700865 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400869
870 if (WARN_ON(req->num_pages >= req->max_pages)) {
871 fuse_put_request(fc, req);
872 return -EIO;
873 }
874
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300875 get_page(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700876 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400877 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100878 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700880 return 0;
881}
882
883static int fuse_readpages(struct file *file, struct address_space *mapping,
884 struct list_head *pages, unsigned nr_pages)
885{
886 struct inode *inode = mapping->host;
887 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700888 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700889 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400890 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800891
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700892 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800893 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800894 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895
Miklos Szeredia6643092007-11-28 16:22:00 -0800896 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700897 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400898 if (fc->async_read)
899 data.req = fuse_get_req_for_background(fc, nr_alloc);
900 else
901 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400902 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700903 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700904 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800905 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700906
907 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700908 if (!err) {
909 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200910 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700911 else
912 fuse_put_request(fc, data.req);
913 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800914out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700915 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916}
917
Al Viro37c20f12014-04-02 14:47:09 -0400918static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800919{
920 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400921 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800922
Brian Fostera8894272012-07-16 15:23:50 -0400923 /*
924 * In auto invalidate mode, always update attributes on read.
925 * Otherwise, only update if we attempt to read past EOF (to ensure
926 * i_size is up to date).
927 */
928 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400929 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800930 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800931 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
932 if (err)
933 return err;
934 }
935
Al Viro37c20f12014-04-02 14:47:09 -0400936 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800937}
938
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200939static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200940 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700941{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700942 struct fuse_write_in *inarg = &req->misc.write.in;
943 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700945 inarg->fh = ff->fh;
946 inarg->offset = pos;
947 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700948 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200949 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700950 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700952 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
953 else
954 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700955 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700956 req->in.args[1].size = count;
957 req->out.numargs = 1;
958 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700959 req->out.args[0].value = outarg;
960}
961
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400962static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200963 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700964{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400965 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 struct fuse_file *ff = file->private_data;
967 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200968 struct fuse_write_in *inarg = &req->misc.write.in;
969
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700972 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700973 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
974 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
975 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400976
977 if (io->async)
978 return fuse_async_req_send(fc, req, count, io);
979
Tejun Heob93f8582008-11-26 12:03:55 +0100980 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700981 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700982}
983
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400984bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700985{
986 struct fuse_conn *fc = get_fuse_conn(inode);
987 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400988 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700989
990 spin_lock(&fc->lock);
991 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400992 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700993 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400994 ret = true;
995 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400997
998 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700999}
1000
Nick Pigginea9b9902008-04-30 00:54:42 -07001001static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1002 struct inode *inode, loff_t pos,
1003 size_t count)
1004{
1005 size_t res;
1006 unsigned offset;
1007 unsigned i;
Seth Forshee744742d2016-03-11 10:35:34 -06001008 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001009
1010 for (i = 0; i < req->num_pages; i++)
1011 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1012
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001013 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001014
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001015 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001016 count = res;
1017 for (i = 0; i < req->num_pages; i++) {
1018 struct page *page = req->pages[i];
1019
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001020 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001021 SetPageUptodate(page);
1022
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001023 if (count > PAGE_SIZE - offset)
1024 count -= PAGE_SIZE - offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001025 else
1026 count = 0;
1027 offset = 0;
1028
1029 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001030 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001031 }
1032
1033 return res;
1034}
1035
1036static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1037 struct address_space *mapping,
1038 struct iov_iter *ii, loff_t pos)
1039{
1040 struct fuse_conn *fc = get_fuse_conn(mapping->host);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 unsigned offset = pos & (PAGE_SIZE - 1);
Nick Pigginea9b9902008-04-30 00:54:42 -07001042 size_t count = 0;
1043 int err;
1044
Miklos Szeredif4975c62009-04-02 14:25:34 +02001045 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001046 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001047
1048 do {
1049 size_t tmp;
1050 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001051 pgoff_t index = pos >> PAGE_SHIFT;
1052 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
Nick Pigginea9b9902008-04-30 00:54:42 -07001053 iov_iter_count(ii));
1054
1055 bytes = min_t(size_t, bytes, fc->max_write - count);
1056
1057 again:
1058 err = -EFAULT;
1059 if (iov_iter_fault_in_readable(ii, bytes))
1060 break;
1061
1062 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001063 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001064 if (!page)
1065 break;
1066
anfei zhou931e80e2010-02-02 13:44:02 -08001067 if (mapping_writably_mapped(mapping))
1068 flush_dcache_page(page);
1069
Nick Pigginea9b9902008-04-30 00:54:42 -07001070 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001071 flush_dcache_page(page);
1072
Roman Gushchin3ca81382015-10-12 16:33:44 +03001073 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 if (!tmp) {
1075 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001076 put_page(page);
Nick Pigginea9b9902008-04-30 00:54:42 -07001077 bytes = min(bytes, iov_iter_single_seg_count(ii));
1078 goto again;
1079 }
1080
1081 err = 0;
1082 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001083 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 req->num_pages++;
1085
Nick Pigginea9b9902008-04-30 00:54:42 -07001086 count += tmp;
1087 pos += tmp;
1088 offset += tmp;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001089 if (offset == PAGE_SIZE)
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 offset = 0;
1091
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001092 if (!fc->big_writes)
1093 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001095 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001096
1097 return count > 0 ? count : err;
1098}
1099
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001100static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1101{
1102 return min_t(unsigned,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001103 ((pos + len - 1) >> PAGE_SHIFT) -
1104 (pos >> PAGE_SHIFT) + 1,
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001105 FUSE_MAX_PAGES_PER_REQ);
1106}
1107
Nick Pigginea9b9902008-04-30 00:54:42 -07001108static ssize_t fuse_perform_write(struct file *file,
1109 struct address_space *mapping,
1110 struct iov_iter *ii, loff_t pos)
1111{
1112 struct inode *inode = mapping->host;
1113 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001114 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 int err = 0;
1116 ssize_t res = 0;
1117
1118 if (is_bad_inode(inode))
1119 return -EIO;
1120
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001121 if (inode->i_size < pos + iov_iter_count(ii))
1122 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1123
Nick Pigginea9b9902008-04-30 00:54:42 -07001124 do {
1125 struct fuse_req *req;
1126 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001127 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001128
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001129 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001130 if (IS_ERR(req)) {
1131 err = PTR_ERR(req);
1132 break;
1133 }
1134
1135 count = fuse_fill_write_pages(req, mapping, ii, pos);
1136 if (count <= 0) {
1137 err = count;
1138 } else {
1139 size_t num_written;
1140
1141 num_written = fuse_send_write_pages(req, file, inode,
1142 pos, count);
1143 err = req->out.h.error;
1144 if (!err) {
1145 res += num_written;
1146 pos += num_written;
1147
1148 /* break out of the loop on short write */
1149 if (num_written != count)
1150 err = -EIO;
1151 }
1152 }
1153 fuse_put_request(fc, req);
1154 } while (!err && iov_iter_count(ii));
1155
1156 if (res > 0)
1157 fuse_write_update_size(inode, pos);
1158
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001159 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001160 fuse_invalidate_attr(inode);
1161
1162 return res > 0 ? res : err;
1163}
1164
Al Viro84c3d552014-04-03 14:33:23 -04001165static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001166{
1167 struct file *file = iocb->ki_filp;
1168 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001169 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001170 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001171 struct inode *inode = mapping->host;
1172 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001173 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001174
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001175 if (get_fuse_conn(inode)->writeback_cache) {
1176 /* Update size (EOF optimization) and mode (SUID clearing) */
1177 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1178 if (err)
1179 return err;
1180
Al Viro84c3d552014-04-03 14:33:23 -04001181 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001182 }
1183
Al Viro59551022016-01-22 15:40:57 -05001184 inode_lock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001185
1186 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001187 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001188
Al Viro3309dd02015-04-09 12:55:47 -04001189 err = generic_write_checks(iocb, from);
1190 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001191 goto out;
1192
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001193 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 if (err)
1195 goto out;
1196
Josef Bacikc3b2da32012-03-26 09:59:21 -04001197 err = file_update_time(file);
1198 if (err)
1199 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001200
Al Viro2ba48ce2015-04-09 13:52:01 -04001201 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001202 loff_t pos = iocb->ki_pos;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001203 written = generic_file_direct_write(iocb, from);
Al Viro84c3d552014-04-03 14:33:23 -04001204 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001205 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001206
Anand Avati4273b792012-02-17 12:46:25 -05001207 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001208
Al Viro84c3d552014-04-03 14:33:23 -04001209 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001210 if (written_buffered < 0) {
1211 err = written_buffered;
1212 goto out;
1213 }
1214 endbyte = pos + written_buffered - 1;
1215
1216 err = filemap_write_and_wait_range(file->f_mapping, pos,
1217 endbyte);
1218 if (err)
1219 goto out;
1220
1221 invalidate_mapping_pages(file->f_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001222 pos >> PAGE_SHIFT,
1223 endbyte >> PAGE_SHIFT);
Anand Avati4273b792012-02-17 12:46:25 -05001224
1225 written += written_buffered;
1226 iocb->ki_pos = pos + written_buffered;
1227 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001228 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001229 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001230 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001231 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001232out:
1233 current->backing_dev_info = NULL;
Al Viro59551022016-01-22 15:40:57 -05001234 inode_unlock(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001235
1236 return written ? written : err;
1237}
1238
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001239static inline void fuse_page_descs_length_init(struct fuse_req *req,
1240 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001241{
1242 int i;
1243
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001244 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001245 req->page_descs[i].length = PAGE_SIZE -
1246 req->page_descs[i].offset;
1247}
1248
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001249static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1250{
1251 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1252}
1253
1254static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1255 size_t max_size)
1256{
1257 return min(iov_iter_single_seg_count(ii), max_size);
1258}
1259
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001260static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001261 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001262{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001263 size_t nbytes = 0; /* # bytes already packed in req */
Ashish Samant742f9922016-03-14 21:57:35 -07001264 ssize_t ret = 0;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001265
Miklos Szeredif4975c62009-04-02 14:25:34 +02001266 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001267 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001268 unsigned long user_addr = fuse_get_user_addr(ii);
1269 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1270
Miklos Szeredif4975c62009-04-02 14:25:34 +02001271 if (write)
1272 req->in.args[1].value = (void *) user_addr;
1273 else
1274 req->out.args[0].value = (void *) user_addr;
1275
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001276 iov_iter_advance(ii, frag_size);
1277 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001278 return 0;
1279 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001280
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001281 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001282 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001283 size_t start;
Ashish Samant742f9922016-03-14 21:57:35 -07001284 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001285 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001286 req->max_pages - req->num_pages,
1287 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 if (ret < 0)
Ashish Samant742f9922016-03-14 21:57:35 -07001289 break;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001290
Al Viroc9c37e22014-03-16 16:08:30 -04001291 iov_iter_advance(ii, ret);
1292 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293
Al Viroc9c37e22014-03-16 16:08:30 -04001294 ret += start;
1295 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1296
1297 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298 fuse_page_descs_length_init(req, req->num_pages, npages);
1299
1300 req->num_pages += npages;
1301 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001302 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001303 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001304
1305 if (write)
1306 req->in.argpages = 1;
1307 else
1308 req->out.argpages = 1;
1309
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001310 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001311
Ashish Samant2c932d42016-03-25 10:53:41 -07001312 return ret < 0 ? ret : 0;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001313}
1314
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001315static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1316{
Al Virof67da302014-03-19 01:16:16 -04001317 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001318}
1319
Al Virod22a9432014-03-16 15:50:47 -04001320ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1321 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001322{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001323 int write = flags & FUSE_DIO_WRITE;
1324 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)
1367 fuse_release_user_pages(req, !write);
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);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001469 dec_zone_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);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659 inc_zone_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);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001773 dec_zone_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);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001872 inc_zone_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 +01002329static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2330 unsigned int nr_segs, size_t bytes, bool to_user)
2331{
2332 struct iov_iter ii;
2333 int page_idx = 0;
2334
2335 if (!bytes)
2336 return 0;
2337
Al Viro71d8e532014-03-05 19:28:09 -05002338 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
Tejun Heo59efec72008-11-26 12:03:55 +01002339
2340 while (iov_iter_count(&ii)) {
2341 struct page *page = pages[page_idx++];
2342 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002343 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002344
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002345 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002346
2347 while (todo) {
2348 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2349 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2350 size_t copy = min(todo, iov_len);
2351 size_t left;
2352
2353 if (!to_user)
2354 left = copy_from_user(kaddr, uaddr, copy);
2355 else
2356 left = copy_to_user(uaddr, kaddr, copy);
2357
2358 if (unlikely(left))
2359 return -EFAULT;
2360
2361 iov_iter_advance(&ii, copy);
2362 todo -= copy;
2363 kaddr += copy;
2364 }
2365
Jens Axboe0bd87182009-11-03 11:40:44 +01002366 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002367 }
2368
2369 return 0;
2370}
2371
2372/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002373 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2374 * ABI was defined to be 'struct iovec' which is different on 32bit
2375 * and 64bit. Fortunately we can determine which structure the server
2376 * used from the size of the reply.
2377 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002378static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2379 size_t transferred, unsigned count,
2380 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002381{
2382#ifdef CONFIG_COMPAT
2383 if (count * sizeof(struct compat_iovec) == transferred) {
2384 struct compat_iovec *ciov = src;
2385 unsigned i;
2386
2387 /*
2388 * With this interface a 32bit server cannot support
2389 * non-compat (i.e. ones coming from 64bit apps) ioctl
2390 * requests
2391 */
2392 if (!is_compat)
2393 return -EINVAL;
2394
2395 for (i = 0; i < count; i++) {
2396 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2397 dst[i].iov_len = ciov[i].iov_len;
2398 }
2399 return 0;
2400 }
2401#endif
2402
2403 if (count * sizeof(struct iovec) != transferred)
2404 return -EIO;
2405
2406 memcpy(dst, src, transferred);
2407 return 0;
2408}
2409
Miklos Szeredi75727772010-11-30 16:39:27 +01002410/* Make sure iov_length() won't overflow */
2411static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2412{
2413 size_t n;
2414 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2415
Zach Brownfb6ccff2012-07-24 12:10:11 -07002416 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002417 if (iov->iov_len > (size_t) max)
2418 return -ENOMEM;
2419 max -= iov->iov_len;
2420 }
2421 return 0;
2422}
2423
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002424static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2425 void *src, size_t transferred, unsigned count,
2426 bool is_compat)
2427{
2428 unsigned i;
2429 struct fuse_ioctl_iovec *fiov = src;
2430
2431 if (fc->minor < 16) {
2432 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2433 count, is_compat);
2434 }
2435
2436 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2437 return -EIO;
2438
2439 for (i = 0; i < count; i++) {
2440 /* Did the server supply an inappropriate value? */
2441 if (fiov[i].base != (unsigned long) fiov[i].base ||
2442 fiov[i].len != (unsigned long) fiov[i].len)
2443 return -EIO;
2444
2445 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2446 dst[i].iov_len = (size_t) fiov[i].len;
2447
2448#ifdef CONFIG_COMPAT
2449 if (is_compat &&
2450 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2451 (compat_size_t) dst[i].iov_len != fiov[i].len))
2452 return -EIO;
2453#endif
2454 }
2455
2456 return 0;
2457}
2458
2459
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002460/*
Tejun Heo59efec72008-11-26 12:03:55 +01002461 * For ioctls, there is no generic way to determine how much memory
2462 * needs to be read and/or written. Furthermore, ioctls are allowed
2463 * to dereference the passed pointer, so the parameter requires deep
2464 * copying but FUSE has no idea whatsoever about what to copy in or
2465 * out.
2466 *
2467 * This is solved by allowing FUSE server to retry ioctl with
2468 * necessary in/out iovecs. Let's assume the ioctl implementation
2469 * needs to read in the following structure.
2470 *
2471 * struct a {
2472 * char *buf;
2473 * size_t buflen;
2474 * }
2475 *
2476 * On the first callout to FUSE server, inarg->in_size and
2477 * inarg->out_size will be NULL; then, the server completes the ioctl
2478 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2479 * the actual iov array to
2480 *
2481 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2482 *
2483 * which tells FUSE to copy in the requested area and retry the ioctl.
2484 * On the second round, the server has access to the structure and
2485 * from that it can tell what to look for next, so on the invocation,
2486 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2487 *
2488 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2489 * { .iov_base = a.buf, .iov_len = a.buflen } }
2490 *
2491 * FUSE will copy both struct a and the pointed buffer from the
2492 * process doing the ioctl and retry ioctl with both struct a and the
2493 * buffer.
2494 *
2495 * This time, FUSE server has everything it needs and completes ioctl
2496 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2497 *
2498 * Copying data out works the same way.
2499 *
2500 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2501 * automatically initializes in and out iovs by decoding @cmd with
2502 * _IOC_* macros and the server is not allowed to request RETRY. This
2503 * limits ioctl data transfers to well-formed ioctls and is the forced
2504 * behavior for all FUSE servers.
2505 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002506long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2507 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002508{
Tejun Heo59efec72008-11-26 12:03:55 +01002509 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002510 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002511 struct fuse_ioctl_in inarg = {
2512 .fh = ff->fh,
2513 .cmd = cmd,
2514 .arg = arg,
2515 .flags = flags
2516 };
2517 struct fuse_ioctl_out outarg;
2518 struct fuse_req *req = NULL;
2519 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002520 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002521 struct iovec *in_iov = NULL, *out_iov = NULL;
2522 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2523 size_t in_size, out_size, transferred;
2524 int err;
2525
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002526#if BITS_PER_LONG == 32
2527 inarg.flags |= FUSE_IOCTL_32BIT;
2528#else
2529 if (flags & FUSE_IOCTL_COMPAT)
2530 inarg.flags |= FUSE_IOCTL_32BIT;
2531#endif
2532
Tejun Heo59efec72008-11-26 12:03:55 +01002533 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002534 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002535
Tejun Heo59efec72008-11-26 12:03:55 +01002536 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002537 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002538 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002539 if (!pages || !iov_page)
2540 goto out;
2541
2542 /*
2543 * If restricted, initialize IO parameters as encoded in @cmd.
2544 * RETRY from server is not allowed.
2545 */
2546 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002547 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002548
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002549 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002550 iov->iov_len = _IOC_SIZE(cmd);
2551
2552 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2553 in_iov = iov;
2554 in_iovs = 1;
2555 }
2556
2557 if (_IOC_DIR(cmd) & _IOC_READ) {
2558 out_iov = iov;
2559 out_iovs = 1;
2560 }
2561 }
2562
2563 retry:
2564 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2565 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2566
2567 /*
2568 * Out data can be used either for actual out data or iovs,
2569 * make sure there always is at least one page.
2570 */
2571 out_size = max_t(size_t, out_size, PAGE_SIZE);
2572 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2573
2574 /* make sure there are enough buffer pages and init request with them */
2575 err = -ENOMEM;
2576 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2577 goto out;
2578 while (num_pages < max_pages) {
2579 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2580 if (!pages[num_pages])
2581 goto out;
2582 num_pages++;
2583 }
2584
Maxim Patlasov54b96672012-10-26 19:49:13 +04002585 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002586 if (IS_ERR(req)) {
2587 err = PTR_ERR(req);
2588 req = NULL;
2589 goto out;
2590 }
2591 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2592 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002593 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002594
2595 /* okay, let's send it to the client */
2596 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002597 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002598 req->in.numargs = 1;
2599 req->in.args[0].size = sizeof(inarg);
2600 req->in.args[0].value = &inarg;
2601 if (in_size) {
2602 req->in.numargs++;
2603 req->in.args[1].size = in_size;
2604 req->in.argpages = 1;
2605
2606 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2607 false);
2608 if (err)
2609 goto out;
2610 }
2611
2612 req->out.numargs = 2;
2613 req->out.args[0].size = sizeof(outarg);
2614 req->out.args[0].value = &outarg;
2615 req->out.args[1].size = out_size;
2616 req->out.argpages = 1;
2617 req->out.argvar = 1;
2618
Tejun Heob93f8582008-11-26 12:03:55 +01002619 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002620 err = req->out.h.error;
2621 transferred = req->out.args[1].size;
2622 fuse_put_request(fc, req);
2623 req = NULL;
2624 if (err)
2625 goto out;
2626
2627 /* did it ask for retry? */
2628 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002629 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002630
2631 /* no retry if in restricted mode */
2632 err = -EIO;
2633 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2634 goto out;
2635
2636 in_iovs = outarg.in_iovs;
2637 out_iovs = outarg.out_iovs;
2638
2639 /*
2640 * Make sure things are in boundary, separate checks
2641 * are to protect against overflow.
2642 */
2643 err = -ENOMEM;
2644 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2645 out_iovs > FUSE_IOCTL_MAX_IOV ||
2646 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2647 goto out;
2648
Cong Wang2408f6e2011-11-25 23:14:30 +08002649 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002650 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002651 transferred, in_iovs + out_iovs,
2652 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002653 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002654 if (err)
2655 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002656
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002657 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002658 out_iov = in_iov + in_iovs;
2659
Miklos Szeredi75727772010-11-30 16:39:27 +01002660 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2661 if (err)
2662 goto out;
2663
2664 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2665 if (err)
2666 goto out;
2667
Tejun Heo59efec72008-11-26 12:03:55 +01002668 goto retry;
2669 }
2670
2671 err = -EIO;
2672 if (transferred > inarg.out_size)
2673 goto out;
2674
2675 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2676 out:
2677 if (req)
2678 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002679 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002680 while (num_pages)
2681 __free_page(pages[--num_pages]);
2682 kfree(pages);
2683
2684 return err ? err : outarg.result;
2685}
Tejun Heo08cbf542009-04-14 10:54:53 +09002686EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002687
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002688long fuse_ioctl_common(struct file *file, unsigned int cmd,
2689 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002690{
Al Viro6131ffa2013-02-27 16:59:05 -05002691 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002692 struct fuse_conn *fc = get_fuse_conn(inode);
2693
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002694 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002695 return -EACCES;
2696
2697 if (is_bad_inode(inode))
2698 return -EIO;
2699
2700 return fuse_do_ioctl(file, cmd, arg, flags);
2701}
2702
Tejun Heo59efec72008-11-26 12:03:55 +01002703static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2704 unsigned long arg)
2705{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002706 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002707}
2708
2709static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2710 unsigned long arg)
2711{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002712 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002713}
2714
Tejun Heo95668a62008-11-26 12:03:55 +01002715/*
2716 * All files which have been polled are linked to RB tree
2717 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2718 * find the matching one.
2719 */
2720static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2721 struct rb_node **parent_out)
2722{
2723 struct rb_node **link = &fc->polled_files.rb_node;
2724 struct rb_node *last = NULL;
2725
2726 while (*link) {
2727 struct fuse_file *ff;
2728
2729 last = *link;
2730 ff = rb_entry(last, struct fuse_file, polled_node);
2731
2732 if (kh < ff->kh)
2733 link = &last->rb_left;
2734 else if (kh > ff->kh)
2735 link = &last->rb_right;
2736 else
2737 return link;
2738 }
2739
2740 if (parent_out)
2741 *parent_out = last;
2742 return link;
2743}
2744
2745/*
2746 * The file is about to be polled. Make sure it's on the polled_files
2747 * RB tree. Note that files once added to the polled_files tree are
2748 * not removed before the file is released. This is because a file
2749 * polled once is likely to be polled again.
2750 */
2751static void fuse_register_polled_file(struct fuse_conn *fc,
2752 struct fuse_file *ff)
2753{
2754 spin_lock(&fc->lock);
2755 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002756 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002757
2758 link = fuse_find_polled_node(fc, ff->kh, &parent);
2759 BUG_ON(*link);
2760 rb_link_node(&ff->polled_node, parent, link);
2761 rb_insert_color(&ff->polled_node, &fc->polled_files);
2762 }
2763 spin_unlock(&fc->lock);
2764}
2765
Tejun Heo08cbf542009-04-14 10:54:53 +09002766unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002767{
Tejun Heo95668a62008-11-26 12:03:55 +01002768 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002769 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002770 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2771 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002772 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002773 int err;
2774
2775 if (fc->no_poll)
2776 return DEFAULT_POLLMASK;
2777
2778 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002779 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002780
2781 /*
2782 * Ask for notification iff there's someone waiting for it.
2783 * The client may ignore the flag and always notify.
2784 */
2785 if (waitqueue_active(&ff->poll_wait)) {
2786 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2787 fuse_register_polled_file(fc, ff);
2788 }
2789
Miklos Szeredi70781872014-12-12 09:49:05 +01002790 args.in.h.opcode = FUSE_POLL;
2791 args.in.h.nodeid = ff->nodeid;
2792 args.in.numargs = 1;
2793 args.in.args[0].size = sizeof(inarg);
2794 args.in.args[0].value = &inarg;
2795 args.out.numargs = 1;
2796 args.out.args[0].size = sizeof(outarg);
2797 args.out.args[0].value = &outarg;
2798 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002799
2800 if (!err)
2801 return outarg.revents;
2802 if (err == -ENOSYS) {
2803 fc->no_poll = 1;
2804 return DEFAULT_POLLMASK;
2805 }
2806 return POLLERR;
2807}
Tejun Heo08cbf542009-04-14 10:54:53 +09002808EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002809
2810/*
2811 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2812 * wakes up the poll waiters.
2813 */
2814int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2815 struct fuse_notify_poll_wakeup_out *outarg)
2816{
2817 u64 kh = outarg->kh;
2818 struct rb_node **link;
2819
2820 spin_lock(&fc->lock);
2821
2822 link = fuse_find_polled_node(fc, kh, NULL);
2823 if (*link) {
2824 struct fuse_file *ff;
2825
2826 ff = rb_entry(*link, struct fuse_file, polled_node);
2827 wake_up_interruptible_sync(&ff->poll_wait);
2828 }
2829
2830 spin_unlock(&fc->lock);
2831 return 0;
2832}
2833
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002834static void fuse_do_truncate(struct file *file)
2835{
2836 struct inode *inode = file->f_mapping->host;
2837 struct iattr attr;
2838
2839 attr.ia_valid = ATTR_SIZE;
2840 attr.ia_size = i_size_read(inode);
2841
2842 attr.ia_file = file;
2843 attr.ia_valid |= ATTR_FILE;
2844
2845 fuse_do_setattr(inode, &attr, file);
2846}
2847
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002848static inline loff_t fuse_round_up(loff_t off)
2849{
2850 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2851}
2852
Anand Avati4273b792012-02-17 12:46:25 -05002853static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002854fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Anand Avati4273b792012-02-17 12:46:25 -05002855{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002856 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002857 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002858 struct file *file = iocb->ki_filp;
2859 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002860 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002861 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002862 struct inode *inode;
2863 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002864 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002865 loff_t offset = iocb->ki_pos;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002866 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002867
Anand Avati4273b792012-02-17 12:46:25 -05002868 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002869 inode = file->f_mapping->host;
2870 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002871
Omar Sandoval6f673762015-03-16 04:33:52 -07002872 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002873 return 0;
2874
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002875 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002876 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002877 if (offset >= i_size)
2878 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002879 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2880 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002881 }
2882
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002883 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002884 if (!io)
2885 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002886 spin_lock_init(&io->lock);
Seth Forshee744742d2016-03-11 10:35:34 -06002887 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002888 io->reqs = 1;
2889 io->bytes = -1;
2890 io->size = 0;
2891 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002892 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002893 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002894 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 /*
2896 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002897 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002899 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002900 io->iocb = iocb;
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302901 io->blocking = is_sync_kiocb(iocb);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002902
2903 /*
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302904 * We cannot asynchronously extend the size of a file.
2905 * In such case the aio will behave exactly like sync io.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302907 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2908 io->blocking = true;
Anand Avati4273b792012-02-17 12:46:25 -05002909
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302910 if (io->async && io->blocking) {
Seth Forshee744742d2016-03-11 10:35:34 -06002911 /*
2912 * Additional reference to keep io around after
2913 * calling fuse_aio_complete()
2914 */
2915 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002916 io->done = &wait;
Seth Forshee744742d2016-03-11 10:35:34 -06002917 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002918
Omar Sandoval6f673762015-03-16 04:33:52 -07002919 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002920 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002921 fuse_invalidate_attr(inode);
2922 } else {
Al Virod22a9432014-03-16 15:50:47 -04002923 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002924 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002925
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002926 if (io->async) {
2927 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2928
2929 /* we have a non-extending, async request, so return */
Ashish Sangwan7879c4e2016-04-07 17:18:11 +05302930 if (!io->blocking)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002931 return -EIOCBQUEUED;
2932
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002933 wait_for_completion(&wait);
2934 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002935 }
2936
Seth Forshee744742d2016-03-11 10:35:34 -06002937 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002938
Omar Sandoval6f673762015-03-16 04:33:52 -07002939 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002940 if (ret > 0)
2941 fuse_write_update_size(inode, pos);
2942 else if (ret < 0 && offset + count > i_size)
2943 fuse_do_truncate(file);
2944 }
Anand Avati4273b792012-02-17 12:46:25 -05002945
2946 return ret;
2947}
2948
Miklos Szeredicdadb112012-11-10 16:55:56 +01002949static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2950 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002951{
2952 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002953 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002954 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002955 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002956 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002957 struct fuse_fallocate_in inarg = {
2958 .fh = ff->fh,
2959 .offset = offset,
2960 .length = length,
2961 .mode = mode
2962 };
2963 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002964 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2965 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002966
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002967 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2968 return -EOPNOTSUPP;
2969
Miklos Szeredi519c6042012-04-26 10:56:36 +02002970 if (fc->no_fallocate)
2971 return -EOPNOTSUPP;
2972
Maxim Patlasov14c14412013-06-13 12:16:39 +04002973 if (lock_inode) {
Al Viro59551022016-01-22 15:40:57 -05002974 inode_lock(inode);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002975 if (mode & FALLOC_FL_PUNCH_HOLE) {
2976 loff_t endbyte = offset + length - 1;
2977 err = filemap_write_and_wait_range(inode->i_mapping,
2978 offset, endbyte);
2979 if (err)
2980 goto out;
2981
2982 fuse_sync_writes(inode);
2983 }
Brian Foster3634a632013-05-17 09:30:32 -04002984 }
2985
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002986 if (!(mode & FALLOC_FL_KEEP_SIZE))
2987 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2988
Miklos Szeredi70781872014-12-12 09:49:05 +01002989 args.in.h.opcode = FUSE_FALLOCATE;
2990 args.in.h.nodeid = ff->nodeid;
2991 args.in.numargs = 1;
2992 args.in.args[0].size = sizeof(inarg);
2993 args.in.args[0].value = &inarg;
2994 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002995 if (err == -ENOSYS) {
2996 fc->no_fallocate = 1;
2997 err = -EOPNOTSUPP;
2998 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002999 if (err)
3000 goto out;
3001
3002 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003003 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3004 bool changed = fuse_write_update_size(inode, offset + length);
3005
Miklos Szeredi93d22692014-04-28 14:19:22 +02003006 if (changed && fc->writeback_cache)
3007 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003008 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003009
3010 if (mode & FALLOC_FL_PUNCH_HOLE)
3011 truncate_pagecache_range(inode, offset, offset + length - 1);
3012
3013 fuse_invalidate_attr(inode);
3014
Brian Foster3634a632013-05-17 09:30:32 -04003015out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003016 if (!(mode & FALLOC_FL_KEEP_SIZE))
3017 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3018
Maxim Patlasovbde52782013-09-13 19:19:54 +04003019 if (lock_inode)
Al Viro59551022016-01-22 15:40:57 -05003020 inode_unlock(inode);
Brian Foster3634a632013-05-17 09:30:32 -04003021
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003022 return err;
3023}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003024
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003025static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003026 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04003027 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04003028 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003029 .mmap = fuse_file_mmap,
3030 .open = fuse_open,
3031 .flush = fuse_flush,
3032 .release = fuse_release,
3033 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003034 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003035 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003036 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003037 .unlocked_ioctl = fuse_file_ioctl,
3038 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003039 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003040 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003041};
3042
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003043static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003044 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003045 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003046 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003047 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003048 .open = fuse_open,
3049 .flush = fuse_flush,
3050 .release = fuse_release,
3051 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003052 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003053 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003054 .unlocked_ioctl = fuse_file_ioctl,
3055 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003056 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003057 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003058 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003059};
3060
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003061static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003062 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003063 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003064 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003065 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003066 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003067 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003068 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003069 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003070 .write_begin = fuse_write_begin,
3071 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003072};
3073
3074void fuse_init_file_inode(struct inode *inode)
3075{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003076 inode->i_fop = &fuse_file_operations;
3077 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003078}