blob: 682e79965c164663a94bac2c46b9303f21a1eba8 [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)
210 mutex_lock(&inode->i_mutex);
211
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)
218 mutex_unlock(&inode->i_mutex);
219
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);
351 curr_index = req->misc.write.in.offset >> PAGE_CACHE_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
416 mutex_lock(&inode->i_mutex);
417 fuse_sync_writes(inode);
418 mutex_unlock(&inode->i_mutex);
419
Maxim Patlasov9ca5f112016-07-19 18:12:26 -0700420 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
421 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
422 err = -ENOSPC;
423 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
424 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
425 err = -EIO;
426 if (err)
427 return err;
428
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400429 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 memset(&inarg, 0, sizeof(inarg));
431 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700432 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700433 req->in.h.opcode = FUSE_FLUSH;
434 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435 req->in.numargs = 1;
436 req->in.args[0].size = sizeof(inarg);
437 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200438 __set_bit(FR_FORCE, &req->flags);
Tejun Heob93f8582008-11-26 12:03:55 +0100439 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 err = req->out.h.error;
441 fuse_put_request(fc, req);
442 if (err == -ENOSYS) {
443 fc->no_flush = 1;
444 err = 0;
445 }
446 return err;
447}
448
Josef Bacik02c24a82011-07-16 20:44:56 -0400449int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
450 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200452 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700453 struct fuse_conn *fc = get_fuse_conn(inode);
454 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100455 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456 struct fuse_fsync_in inarg;
457 int err;
458
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800459 if (is_bad_inode(inode))
460 return -EIO;
461
Josef Bacik02c24a82011-07-16 20:44:56 -0400462 mutex_lock(&inode->i_mutex);
463
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700464 /*
465 * Start writeback against all dirty pages of the inode, then
466 * wait for all outstanding writes, before sending the FSYNC
467 * request.
468 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200469 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700470 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400471 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700472
473 fuse_sync_writes(inode);
Alexey Kuznetsov3d1c64d2016-07-19 12:48:01 -0700474
475 /*
476 * Due to implementation of fuse writeback
477 * filemap_write_and_wait_range() does not catch errors.
478 * We have to do this directly after fuse_sync_writes()
479 */
480 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
481 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
482 err = -ENOSPC;
483 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
484 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
485 err = -EIO;
486 if (err)
487 goto out;
488
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200489 err = sync_inode_metadata(inode, 1);
490 if (err)
491 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492
Miklos Szeredi22401e72014-04-28 14:19:23 +0200493 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
494 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400495
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700496 memset(&inarg, 0, sizeof(inarg));
497 inarg.fh = ff->fh;
498 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100499 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
500 args.in.h.nodeid = get_node_id(inode);
501 args.in.numargs = 1;
502 args.in.args[0].size = sizeof(inarg);
503 args.in.args[0].value = &inarg;
504 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700506 if (isdir)
507 fc->no_fsyncdir = 1;
508 else
509 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700510 err = 0;
511 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400512out:
513 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514 return err;
515}
516
Josef Bacik02c24a82011-07-16 20:44:56 -0400517static int fuse_fsync(struct file *file, loff_t start, loff_t end,
518 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700519{
Josef Bacik02c24a82011-07-16 20:44:56 -0400520 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700521}
522
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200523void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
524 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700526 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800527 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800529 inarg->fh = ff->fh;
530 inarg->offset = pos;
531 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800532 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800533 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200534 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700535 req->in.numargs = 1;
536 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800537 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700538 req->out.argvar = 1;
539 req->out.numargs = 1;
540 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541}
542
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +0200543static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400544{
545 unsigned i;
546
547 for (i = 0; i < req->num_pages; i++) {
548 struct page *page = req->pages[i];
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +0200549 if (should_dirty)
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400550 set_page_dirty_lock(page);
551 put_page(page);
552 }
553}
554
Seth Forshee37bd8c82016-03-11 10:35:34 -0600555static void fuse_io_release(struct kref *kref)
556{
557 kfree(container_of(kref, struct fuse_io_priv, refcnt));
558}
559
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100560static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
561{
562 if (io->err)
563 return io->err;
564
565 if (io->bytes >= 0 && io->write)
566 return -EIO;
567
568 return io->bytes < 0 ? io->size : io->bytes;
569}
570
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400571/**
572 * In case of short read, the caller sets 'pos' to the position of
573 * actual end of fuse request in IO request. Otherwise, if bytes_requested
574 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
575 *
576 * An example:
577 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
578 * both submitted asynchronously. The first of them was ACKed by userspace as
579 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
580 * second request was ACKed as short, e.g. only 1K was read, resulting in
581 * pos == 33K.
582 *
583 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
584 * will be equal to the length of the longest contiguous fragment of
585 * transferred data starting from the beginning of IO request.
586 */
587static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
588{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100589 bool is_sync = is_sync_kiocb(io->iocb);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400590 int left;
591
592 spin_lock(&io->lock);
593 if (err)
594 io->err = io->err ? : err;
595 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
596 io->bytes = pos;
597
598 left = --io->reqs;
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100599 if (!left && is_sync)
600 complete(io->done);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400601 spin_unlock(&io->lock);
602
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100603 if (!left && !is_sync) {
604 ssize_t res = fuse_get_res_by_io(io);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400605
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100606 if (res >= 0) {
607 struct inode *inode = file_inode(io->iocb->ki_filp);
608 struct fuse_conn *fc = get_fuse_conn(inode);
609 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400610
Christoph Hellwig9d5722b2015-02-02 14:59:43 +0100611 spin_lock(&fc->lock);
612 fi->attr_version = ++fc->attr_version;
613 spin_unlock(&fc->lock);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400614 }
615
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100616 io->iocb->ki_complete(io->iocb, res, 0);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400617 }
Seth Forshee37bd8c82016-03-11 10:35:34 -0600618
619 kref_put(&io->refcnt, fuse_io_release);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400620}
621
622static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
623{
624 struct fuse_io_priv *io = req->io;
625 ssize_t pos = -1;
626
627 fuse_release_user_pages(req, !io->write);
628
629 if (io->write) {
630 if (req->misc.write.in.size != req->misc.write.out.size)
631 pos = req->misc.write.in.offset - io->offset +
632 req->misc.write.out.size;
633 } else {
634 if (req->misc.read.in.size != req->out.args[0].size)
635 pos = req->misc.read.in.offset - io->offset +
636 req->out.args[0].size;
637 }
638
639 fuse_aio_complete(io, req->out.h.error, pos);
640}
641
642static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
643 size_t num_bytes, struct fuse_io_priv *io)
644{
645 spin_lock(&io->lock);
Seth Forshee37bd8c82016-03-11 10:35:34 -0600646 kref_get(&io->refcnt);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400647 io->size += num_bytes;
648 io->reqs++;
649 spin_unlock(&io->lock);
650
651 req->io = io;
652 req->end = fuse_aio_complete_req;
653
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400654 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400655 fuse_request_send_background(fc, req);
656
657 return num_bytes;
658}
659
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400660static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200661 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700662{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400663 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200664 struct fuse_file *ff = file->private_data;
665 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700666
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200667 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700668 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700670
671 inarg->read_flags |= FUSE_READ_LOCKOWNER;
672 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
673 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400674
675 if (io->async)
676 return fuse_async_req_send(fc, req, count, io);
677
Tejun Heob93f8582008-11-26 12:03:55 +0100678 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800679 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700680}
681
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700682static void fuse_read_update_size(struct inode *inode, loff_t size,
683 u64 attr_ver)
684{
685 struct fuse_conn *fc = get_fuse_conn(inode);
686 struct fuse_inode *fi = get_fuse_inode(inode);
687
688 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400689 if (attr_ver == fi->attr_version && size < inode->i_size &&
690 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700691 fi->attr_version = ++fc->attr_version;
692 i_size_write(inode, size);
693 }
694 spin_unlock(&fc->lock);
695}
696
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400697static void fuse_short_read(struct fuse_req *req, struct inode *inode,
698 u64 attr_ver)
699{
700 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400702
Pavel Emelyanov83732002013-10-10 17:10:46 +0400703 if (fc->writeback_cache) {
704 /*
705 * A hole in a file. Some data after the hole are in page cache,
706 * but have not reached the client fs yet. So, the hole is not
707 * present there.
708 */
709 int i;
710 int start_idx = num_read >> PAGE_CACHE_SHIFT;
711 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
712
713 for (i = start_idx; i < req->num_pages; i++) {
714 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
715 off = 0;
716 }
717 } else {
718 loff_t pos = page_offset(req->pages[0]) + num_read;
719 fuse_read_update_size(inode, pos, attr_ver);
720 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400721}
722
Maxim Patlasov482fce52013-10-10 17:11:25 +0400723static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700724{
Seth Forshee37bd8c82016-03-11 10:35:34 -0600725 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700726 struct inode *inode = page->mapping->host;
727 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800728 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700729 size_t num_read;
730 loff_t pos = page_offset(page);
731 size_t count = PAGE_CACHE_SIZE;
732 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800733 int err;
734
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700735 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300736 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700737 * page-cache page, so make sure we read a properly synced
738 * page.
739 */
740 fuse_wait_on_page_writeback(inode, page->index);
741
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400742 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700743 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400744 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746 attr_ver = fuse_get_attr_version(fc);
747
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700748 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200749 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700750 req->num_pages = 1;
751 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400752 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400753 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700754 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700755
756 if (!err) {
757 /*
758 * Short read means EOF. If file size is larger, truncate it
759 */
760 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400761 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700762
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700763 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700764 }
765
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400766 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400767
768 return err;
769}
770
771static int fuse_readpage(struct file *file, struct page *page)
772{
773 struct inode *inode = page->mapping->host;
774 int err;
775
776 err = -EIO;
777 if (is_bad_inode(inode))
778 goto out;
779
780 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800781 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700782 out:
783 unlock_page(page);
784 return err;
785}
786
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800787static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700788{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800789 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700790 size_t count = req->misc.read.in.size;
791 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200792 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800793
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794 for (i = 0; mapping == NULL && i < req->num_pages; i++)
795 mapping = req->pages[i]->mapping;
796
797 if (mapping) {
798 struct inode *inode = mapping->host;
799
800 /*
801 * Short read means EOF. If file size is larger, truncate it
802 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400803 if (!req->out.h.error && num_read < count)
804 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200805
Andrew Gallagher451418f2013-11-05 03:55:43 -0800806 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700807 }
808
Miklos Szeredidb50b962005-09-09 13:10:33 -0700809 for (i = 0; i < req->num_pages; i++) {
810 struct page *page = req->pages[i];
811 if (!req->out.h.error)
812 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813 else
814 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700815 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200816 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700817 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700818 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100819 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800820}
821
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200822static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800823{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200824 struct fuse_file *ff = file->private_data;
825 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800826 loff_t pos = page_offset(req->pages[0]);
827 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200828
829 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800830 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200831 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200832 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700833 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800834 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700835 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800836 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100837 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800838 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100839 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800840 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100841 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800842 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843}
844
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700845struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700846 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800847 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700848 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400849 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700850};
851
852static int fuse_readpages_fill(void *_data, struct page *page)
853{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700854 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700855 struct fuse_req *req = data->req;
856 struct inode *inode = data->inode;
857 struct fuse_conn *fc = get_fuse_conn(inode);
858
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700859 fuse_wait_on_page_writeback(inode, page->index);
860
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861 if (req->num_pages &&
862 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
863 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
864 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400865 int nr_alloc = min_t(unsigned, data->nr_pages,
866 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200867 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400868 if (fc->async_read)
869 req = fuse_get_req_for_background(fc, nr_alloc);
870 else
871 req = fuse_get_req(fc, nr_alloc);
872
873 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700874 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700876 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700877 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879
880 if (WARN_ON(req->num_pages >= req->max_pages)) {
881 fuse_put_request(fc, req);
882 return -EIO;
883 }
884
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200885 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400887 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100888 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400889 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 return 0;
891}
892
893static int fuse_readpages(struct file *file, struct address_space *mapping,
894 struct list_head *pages, unsigned nr_pages)
895{
896 struct inode *inode = mapping->host;
897 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700898 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400900 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800901
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700902 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800903 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800904 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800905
Miklos Szeredia6643092007-11-28 16:22:00 -0800906 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400908 if (fc->async_read)
909 data.req = fuse_get_req_for_background(fc, nr_alloc);
910 else
911 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400912 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700913 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700914 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800915 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916
917 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700918 if (!err) {
919 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200920 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700921 else
922 fuse_put_request(fc, data.req);
923 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800924out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700925 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700926}
927
Al Viro37c20f12014-04-02 14:47:09 -0400928static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800929{
930 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400931 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932
Brian Fostera8894272012-07-16 15:23:50 -0400933 /*
934 * In auto invalidate mode, always update attributes on read.
935 * Otherwise, only update if we attempt to read past EOF (to ensure
936 * i_size is up to date).
937 */
938 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400939 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800940 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
942 if (err)
943 return err;
944 }
945
Al Viro37c20f12014-04-02 14:47:09 -0400946 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800947}
948
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200949static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200950 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700952 struct fuse_write_in *inarg = &req->misc.write.in;
953 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700954
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700955 inarg->fh = ff->fh;
956 inarg->offset = pos;
957 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700958 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200959 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200961 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700962 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
963 else
964 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700965 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700966 req->in.args[1].size = count;
967 req->out.numargs = 1;
968 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700969 req->out.args[0].value = outarg;
970}
971
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400972static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200973 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400975 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200976 struct fuse_file *ff = file->private_data;
977 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200978 struct fuse_write_in *inarg = &req->misc.write.in;
979
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200980 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200981 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700982 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700983 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
984 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
985 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400986
987 if (io->async)
988 return fuse_async_req_send(fc, req, count, io);
989
Tejun Heob93f8582008-11-26 12:03:55 +0100990 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700991 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700992}
993
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400994bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700995{
996 struct fuse_conn *fc = get_fuse_conn(inode);
997 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400998 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700999
1000 spin_lock(&fc->lock);
1001 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001002 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001003 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001004 ret = true;
1005 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001006 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001007
1008 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001009}
1010
Nick Pigginea9b9902008-04-30 00:54:42 -07001011static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1012 struct inode *inode, loff_t pos,
1013 size_t count)
1014{
1015 size_t res;
1016 unsigned offset;
1017 unsigned i;
Seth Forshee37bd8c82016-03-11 10:35:34 -06001018 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001019
1020 for (i = 0; i < req->num_pages; i++)
1021 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1022
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001023 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001024
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001025 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001026 count = res;
1027 for (i = 0; i < req->num_pages; i++) {
1028 struct page *page = req->pages[i];
1029
1030 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1031 SetPageUptodate(page);
1032
1033 if (count > PAGE_CACHE_SIZE - offset)
1034 count -= PAGE_CACHE_SIZE - offset;
1035 else
1036 count = 0;
1037 offset = 0;
1038
1039 unlock_page(page);
1040 page_cache_release(page);
1041 }
1042
1043 return res;
1044}
1045
1046static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1047 struct address_space *mapping,
1048 struct iov_iter *ii, loff_t pos)
1049{
1050 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1051 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1052 size_t count = 0;
1053 int err;
1054
Miklos Szeredif4975c62009-04-02 14:25:34 +02001055 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001056 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001057
1058 do {
1059 size_t tmp;
1060 struct page *page;
1061 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1062 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1063 iov_iter_count(ii));
1064
1065 bytes = min_t(size_t, bytes, fc->max_write - count);
1066
1067 again:
1068 err = -EFAULT;
1069 if (iov_iter_fault_in_readable(ii, bytes))
1070 break;
1071
1072 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001073 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 if (!page)
1075 break;
1076
anfei zhou931e80e2010-02-02 13:44:02 -08001077 if (mapping_writably_mapped(mapping))
1078 flush_dcache_page(page);
1079
Nick Pigginea9b9902008-04-30 00:54:42 -07001080 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 flush_dcache_page(page);
1082
Roman Gushchin3ca81382015-10-12 16:33:44 +03001083 iov_iter_advance(ii, tmp);
Nick Pigginea9b9902008-04-30 00:54:42 -07001084 if (!tmp) {
1085 unlock_page(page);
1086 page_cache_release(page);
1087 bytes = min(bytes, iov_iter_single_seg_count(ii));
1088 goto again;
1089 }
1090
1091 err = 0;
1092 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001093 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 req->num_pages++;
1095
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 count += tmp;
1097 pos += tmp;
1098 offset += tmp;
1099 if (offset == PAGE_CACHE_SIZE)
1100 offset = 0;
1101
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001102 if (!fc->big_writes)
1103 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001104 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001105 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001106
1107 return count > 0 ? count : err;
1108}
1109
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001110static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1111{
1112 return min_t(unsigned,
1113 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1114 (pos >> PAGE_CACHE_SHIFT) + 1,
1115 FUSE_MAX_PAGES_PER_REQ);
1116}
1117
Nick Pigginea9b9902008-04-30 00:54:42 -07001118static ssize_t fuse_perform_write(struct file *file,
1119 struct address_space *mapping,
1120 struct iov_iter *ii, loff_t pos)
1121{
1122 struct inode *inode = mapping->host;
1123 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001124 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 int err = 0;
1126 ssize_t res = 0;
1127
1128 if (is_bad_inode(inode))
1129 return -EIO;
1130
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001131 if (inode->i_size < pos + iov_iter_count(ii))
1132 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1133
Nick Pigginea9b9902008-04-30 00:54:42 -07001134 do {
1135 struct fuse_req *req;
1136 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001137 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001138
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001139 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001140 if (IS_ERR(req)) {
1141 err = PTR_ERR(req);
1142 break;
1143 }
1144
1145 count = fuse_fill_write_pages(req, mapping, ii, pos);
1146 if (count <= 0) {
1147 err = count;
1148 } else {
1149 size_t num_written;
1150
1151 num_written = fuse_send_write_pages(req, file, inode,
1152 pos, count);
1153 err = req->out.h.error;
1154 if (!err) {
1155 res += num_written;
1156 pos += num_written;
1157
1158 /* break out of the loop on short write */
1159 if (num_written != count)
1160 err = -EIO;
1161 }
1162 }
1163 fuse_put_request(fc, req);
1164 } while (!err && iov_iter_count(ii));
1165
1166 if (res > 0)
1167 fuse_write_update_size(inode, pos);
1168
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001169 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 fuse_invalidate_attr(inode);
1171
1172 return res > 0 ? res : err;
1173}
1174
Al Viro84c3d552014-04-03 14:33:23 -04001175static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001176{
1177 struct file *file = iocb->ki_filp;
1178 struct address_space *mapping = file->f_mapping;
Nick Pigginea9b9902008-04-30 00:54:42 -07001179 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001180 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 struct inode *inode = mapping->host;
1182 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001183 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001184
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001185 if (get_fuse_conn(inode)->writeback_cache) {
1186 /* Update size (EOF optimization) and mode (SUID clearing) */
1187 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1188 if (err)
1189 return err;
1190
Al Viro84c3d552014-04-03 14:33:23 -04001191 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001192 }
1193
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001195
1196 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001197 current->backing_dev_info = inode_to_bdi(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001198
Al Viro3309dd02015-04-09 12:55:47 -04001199 err = generic_write_checks(iocb, from);
1200 if (err <= 0)
Nick Pigginea9b9902008-04-30 00:54:42 -07001201 goto out;
1202
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001203 err = file_remove_privs(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001204 if (err)
1205 goto out;
1206
Josef Bacikc3b2da32012-03-26 09:59:21 -04001207 err = file_update_time(file);
1208 if (err)
1209 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001210
Al Viro2ba48ce2015-04-09 13:52:01 -04001211 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro3309dd02015-04-09 12:55:47 -04001212 loff_t pos = iocb->ki_pos;
Al Viro84c3d552014-04-03 14:33:23 -04001213 written = generic_file_direct_write(iocb, from, pos);
1214 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001215 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001216
Anand Avati4273b792012-02-17 12:46:25 -05001217 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001218
Al Viro84c3d552014-04-03 14:33:23 -04001219 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001220 if (written_buffered < 0) {
1221 err = written_buffered;
1222 goto out;
1223 }
1224 endbyte = pos + written_buffered - 1;
1225
1226 err = filemap_write_and_wait_range(file->f_mapping, pos,
1227 endbyte);
1228 if (err)
1229 goto out;
1230
1231 invalidate_mapping_pages(file->f_mapping,
1232 pos >> PAGE_CACHE_SHIFT,
1233 endbyte >> PAGE_CACHE_SHIFT);
1234
1235 written += written_buffered;
1236 iocb->ki_pos = pos + written_buffered;
1237 } else {
Al Viro3309dd02015-04-09 12:55:47 -04001238 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
Anand Avati4273b792012-02-17 12:46:25 -05001239 if (written >= 0)
Al Viro3309dd02015-04-09 12:55:47 -04001240 iocb->ki_pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001241 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001242out:
1243 current->backing_dev_info = NULL;
1244 mutex_unlock(&inode->i_mutex);
1245
1246 return written ? written : err;
1247}
1248
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001249static inline void fuse_page_descs_length_init(struct fuse_req *req,
1250 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001251{
1252 int i;
1253
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001254 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001255 req->page_descs[i].length = PAGE_SIZE -
1256 req->page_descs[i].offset;
1257}
1258
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001259static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1260{
1261 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1262}
1263
1264static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1265 size_t max_size)
1266{
1267 return min(iov_iter_single_seg_count(ii), max_size);
1268}
1269
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001270static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001271 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001272{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001273 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001274
Miklos Szeredif4975c62009-04-02 14:25:34 +02001275 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001276 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001277 unsigned long user_addr = fuse_get_user_addr(ii);
1278 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1279
Miklos Szeredif4975c62009-04-02 14:25:34 +02001280 if (write)
1281 req->in.args[1].value = (void *) user_addr;
1282 else
1283 req->out.args[0].value = (void *) user_addr;
1284
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001285 iov_iter_advance(ii, frag_size);
1286 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001287 return 0;
1288 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001289
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001290 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001291 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001292 size_t start;
Al Viroc9c37e22014-03-16 16:08:30 -04001293 ssize_t ret = iov_iter_get_pages(ii,
1294 &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001295 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001296 req->max_pages - req->num_pages,
1297 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298 if (ret < 0)
1299 return ret;
1300
Al Viroc9c37e22014-03-16 16:08:30 -04001301 iov_iter_advance(ii, ret);
1302 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001303
Al Viroc9c37e22014-03-16 16:08:30 -04001304 ret += start;
1305 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1306
1307 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001308 fuse_page_descs_length_init(req, req->num_pages, npages);
1309
1310 req->num_pages += npages;
1311 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001312 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001313 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001314
1315 if (write)
1316 req->in.argpages = 1;
1317 else
1318 req->out.argpages = 1;
1319
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001320 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001321
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001322 return 0;
1323}
1324
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001325static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1326{
Al Virof67da302014-03-19 01:16:16 -04001327 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001328}
1329
Al Virod22a9432014-03-16 15:50:47 -04001330ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1331 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001332{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001333 int write = flags & FUSE_DIO_WRITE;
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +02001334 bool should_dirty = !write && iter_is_iovec(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001335 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001336 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001337 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001338 struct fuse_file *ff = file->private_data;
1339 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001340 size_t nmax = write ? fc->max_write : fc->max_read;
1341 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001342 size_t count = iov_iter_count(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001343 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1344 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001345 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001346 struct fuse_req *req;
1347
Brian Fosterde82b922013-05-14 11:25:39 -04001348 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001349 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001350 else
Al Virod22a9432014-03-16 15:50:47 -04001351 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001352 if (IS_ERR(req))
1353 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001354
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001355 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1356 if (!write)
1357 mutex_lock(&inode->i_mutex);
1358 fuse_sync_writes(inode);
1359 if (!write)
1360 mutex_unlock(&inode->i_mutex);
1361 }
1362
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001363 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001365 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001366 size_t nbytes = min(count, nmax);
Al Virod22a9432014-03-16 15:50:47 -04001367 int err = fuse_get_user_pages(req, iter, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368 if (err) {
1369 res = err;
1370 break;
1371 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001372
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001373 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001374 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001376 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001377
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001378 if (!io->async)
Miklos Szeredi8aa6a2a2016-08-24 18:17:04 +02001379 fuse_release_user_pages(req, should_dirty);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 if (req->out.h.error) {
1381 if (!res)
1382 res = req->out.h.error;
1383 break;
1384 } else if (nres > nbytes) {
1385 res = -EIO;
1386 break;
1387 }
1388 count -= nres;
1389 res += nres;
1390 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 if (nres != nbytes)
1392 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001393 if (count) {
1394 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001395 if (io->async)
1396 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001397 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001398 else
Al Virod22a9432014-03-16 15:50:47 -04001399 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001400 if (IS_ERR(req))
1401 break;
1402 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001403 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001404 if (!IS_ERR(req))
1405 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001406 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408
1409 return res;
1410}
Tejun Heo08cbf542009-04-14 10:54:53 +09001411EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001412
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001413static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001414 struct iov_iter *iter,
1415 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001416{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001417 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001418 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001419 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001420
1421 if (is_bad_inode(inode))
1422 return -EIO;
1423
Al Virod22a9432014-03-16 15:50:47 -04001424 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001425
1426 fuse_invalidate_attr(inode);
1427
1428 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429}
1430
Al Viro15316262015-03-30 22:08:36 -04001431static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001432{
Seth Forshee37bd8c82016-03-11 10:35:34 -06001433 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
Al Viro15316262015-03-30 22:08:36 -04001434 return __fuse_direct_read(&io, to, &iocb->ki_pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001435}
1436
Al Viro15316262015-03-30 22:08:36 -04001437static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001438{
Al Viro15316262015-03-30 22:08:36 -04001439 struct file *file = iocb->ki_filp;
Al Viro6131ffa2013-02-27 16:59:05 -05001440 struct inode *inode = file_inode(file);
Seth Forshee37bd8c82016-03-11 10:35:34 -06001441 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
Al Viro15316262015-03-30 22:08:36 -04001442 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001443
1444 if (is_bad_inode(inode))
1445 return -EIO;
1446
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001447 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001448 mutex_lock(&inode->i_mutex);
Al Viro3309dd02015-04-09 12:55:47 -04001449 res = generic_write_checks(iocb, from);
1450 if (res > 0)
Al Viro812408f2015-03-30 22:15:58 -04001451 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04001452 fuse_invalidate_attr(inode);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001453 if (res > 0)
Al Viro15316262015-03-30 22:08:36 -04001454 fuse_write_update_size(inode, iocb->ki_pos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001455 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001456
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001457 return res;
1458}
1459
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001460static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001461{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001462 int i;
1463
1464 for (i = 0; i < req->num_pages; i++)
1465 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001466
1467 if (req->ff)
1468 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001469}
1470
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001471static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001472{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001473 struct inode *inode = req->inode;
1474 struct fuse_inode *fi = get_fuse_inode(inode);
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001475 struct backing_dev_info *bdi = inode_to_bdi(inode);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001476 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001477
1478 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001479 for (i = 0; i < req->num_pages; i++) {
Tejun Heo93f78d82015-05-22 17:13:27 -04001480 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001481 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001482 wb_writeout_inc(&bdi->wb);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001483 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001484 wake_up(&fi->page_waitq);
1485}
1486
1487/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001488static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1489 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001490__releases(fc->lock)
1491__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001492{
1493 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001494 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001495 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001496
1497 if (!fc->connected)
1498 goto out_free;
1499
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001500 if (inarg->offset + data_size <= size) {
1501 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001502 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001503 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 } else {
1505 /* Got truncated off completely */
1506 goto out_free;
1507 }
1508
1509 req->in.args[1].size = inarg->size;
1510 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001511 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512 return;
1513
1514 out_free:
1515 fuse_writepage_finish(fc, req);
1516 spin_unlock(&fc->lock);
1517 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001518 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001519 spin_lock(&fc->lock);
1520}
1521
1522/*
1523 * If fi->writectr is positive (no truncate or fsync going on) send
1524 * all queued writepage requests.
1525 *
1526 * Called with fc->lock
1527 */
1528void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001529__releases(fc->lock)
1530__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001531{
1532 struct fuse_conn *fc = get_fuse_conn(inode);
1533 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001534 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001535 struct fuse_req *req;
1536
1537 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1538 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1539 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001540 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541 }
1542}
1543
1544static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1545{
1546 struct inode *inode = req->inode;
1547 struct fuse_inode *fi = get_fuse_inode(inode);
1548
1549 mapping_set_error(inode->i_mapping, req->out.h.error);
1550 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001551 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001552 struct fuse_conn *fc = get_fuse_conn(inode);
1553 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001554 struct fuse_req *next = req->misc.write.next;
1555 req->misc.write.next = next->misc.write.next;
1556 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001557 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001558 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001559
1560 /*
1561 * Skip fuse_flush_writepages() to make it easy to crop requests
1562 * based on primary request size.
1563 *
1564 * 1st case (trivial): there are no concurrent activities using
1565 * fuse_set/release_nowrite. Then we're on safe side because
1566 * fuse_flush_writepages() would call fuse_send_writepage()
1567 * anyway.
1568 *
1569 * 2nd case: someone called fuse_set_nowrite and it is waiting
1570 * now for completion of all in-flight requests. This happens
1571 * rarely and no more than once per page, so this should be
1572 * okay.
1573 *
1574 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1575 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1576 * that fuse_set_nowrite returned implies that all in-flight
1577 * requests were completed along with all of their secondary
1578 * requests. Further primary requests are blocked by negative
1579 * writectr. Hence there cannot be any in-flight requests and
1580 * no invocations of fuse_writepage_end() while we're in
1581 * fuse_set_nowrite..fuse_release_nowrite section.
1582 */
1583 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001584 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001585 fi->writectr--;
1586 fuse_writepage_finish(fc, req);
1587 spin_unlock(&fc->lock);
1588 fuse_writepage_free(fc, req);
1589}
1590
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001591static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1592 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001593{
Miklos Szeredi72523422013-10-01 16:44:52 +02001594 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001595
1596 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001597 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001598 ff = list_entry(fi->write_files.next, struct fuse_file,
1599 write_entry);
1600 fuse_file_get(ff);
1601 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001602 spin_unlock(&fc->lock);
1603
1604 return ff;
1605}
1606
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001607static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1608 struct fuse_inode *fi)
1609{
1610 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1611 WARN_ON(!ff);
1612 return ff;
1613}
1614
1615int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1616{
1617 struct fuse_conn *fc = get_fuse_conn(inode);
1618 struct fuse_inode *fi = get_fuse_inode(inode);
1619 struct fuse_file *ff;
1620 int err;
1621
1622 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001623 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001624 if (ff)
1625 fuse_file_put(ff, 0);
1626
1627 return err;
1628}
1629
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001630static int fuse_writepage_locked(struct page *page)
1631{
1632 struct address_space *mapping = page->mapping;
1633 struct inode *inode = mapping->host;
1634 struct fuse_conn *fc = get_fuse_conn(inode);
1635 struct fuse_inode *fi = get_fuse_inode(inode);
1636 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001637 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001638 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001639
1640 set_page_writeback(page);
1641
Maxim Patlasov4250c062012-10-26 19:48:07 +04001642 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001643 if (!req)
1644 goto err;
1645
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001646 /* writeback always goes to bg_queue */
1647 __set_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001648 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1649 if (!tmp_page)
1650 goto err_free;
1651
Miklos Szeredi72523422013-10-01 16:44:52 +02001652 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001653 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001654 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001655 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001656
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001657 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658
1659 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001660 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001661 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001662 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663 req->num_pages = 1;
1664 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001665 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001666 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001667 req->end = fuse_writepage_end;
1668 req->inode = inode;
1669
Tejun Heo93f78d82015-05-22 17:13:27 -04001670 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001671 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001672
1673 spin_lock(&fc->lock);
1674 list_add(&req->writepages_entry, &fi->writepages);
1675 list_add_tail(&req->list, &fi->queued_writes);
1676 fuse_flush_writepages(inode);
1677 spin_unlock(&fc->lock);
1678
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001679 end_page_writeback(page);
1680
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001681 return 0;
1682
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001683err_nofile:
1684 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001685err_free:
1686 fuse_request_free(req);
1687err:
1688 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001689 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690}
1691
1692static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1693{
1694 int err;
1695
Miklos Szerediff17be02013-10-01 16:44:53 +02001696 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1697 /*
1698 * ->writepages() should be called for sync() and friends. We
1699 * should only get here on direct reclaim and then we are
1700 * allowed to skip a page which is already in flight
1701 */
1702 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1703
1704 redirty_page_for_writepage(wbc, page);
1705 return 0;
1706 }
1707
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708 err = fuse_writepage_locked(page);
1709 unlock_page(page);
1710
1711 return err;
1712}
1713
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001714struct fuse_fill_wb_data {
1715 struct fuse_req *req;
1716 struct fuse_file *ff;
1717 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001718 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001719};
1720
1721static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1722{
1723 struct fuse_req *req = data->req;
1724 struct inode *inode = data->inode;
1725 struct fuse_conn *fc = get_fuse_conn(inode);
1726 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001727 int num_pages = req->num_pages;
1728 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001729
1730 req->ff = fuse_file_get(data->ff);
1731 spin_lock(&fc->lock);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001735
1736 for (i = 0; i < num_pages; i++)
1737 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001738}
1739
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001740static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1741 struct page *page)
1742{
1743 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1744 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1745 struct fuse_req *tmp;
1746 struct fuse_req *old_req;
1747 bool found = false;
1748 pgoff_t curr_index;
1749
1750 BUG_ON(new_req->num_pages != 0);
1751
1752 spin_lock(&fc->lock);
1753 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001754 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1755 BUG_ON(old_req->inode != new_req->inode);
1756 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1757 if (curr_index <= page->index &&
1758 page->index < curr_index + old_req->num_pages) {
1759 found = true;
1760 break;
1761 }
1762 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001763 if (!found) {
1764 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001765 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001766 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767
Maxim Patlasovf6011082013-10-02 15:01:07 +04001768 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001769 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1770 BUG_ON(tmp->inode != new_req->inode);
1771 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1772 if (tmp->num_pages == 1 &&
1773 curr_index == page->index) {
1774 old_req = tmp;
1775 }
1776 }
1777
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001778 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001779 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001780
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001781 copy_highpage(old_req->pages[0], page);
1782 spin_unlock(&fc->lock);
1783
Tejun Heo93f78d82015-05-22 17:13:27 -04001784 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001785 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Tejun Heo93f78d82015-05-22 17:13:27 -04001786 wb_writeout_inc(&bdi->wb);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001787 fuse_writepage_free(fc, new_req);
1788 fuse_request_free(new_req);
1789 goto out;
1790 } else {
1791 new_req->misc.write.next = old_req->misc.write.next;
1792 old_req->misc.write.next = new_req;
1793 }
1794out_unlock:
1795 spin_unlock(&fc->lock);
1796out:
1797 return found;
1798}
1799
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001800static int fuse_writepages_fill(struct page *page,
1801 struct writeback_control *wbc, void *_data)
1802{
1803 struct fuse_fill_wb_data *data = _data;
1804 struct fuse_req *req = data->req;
1805 struct inode *inode = data->inode;
1806 struct fuse_conn *fc = get_fuse_conn(inode);
1807 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001808 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001809 int err;
1810
1811 if (!data->ff) {
1812 err = -EIO;
1813 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1814 if (!data->ff)
1815 goto out_unlock;
1816 }
1817
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001818 /*
1819 * Being under writeback is unlikely but possible. For example direct
1820 * read to an mmaped fuse file will set the page dirty twice; once when
1821 * the pages are faulted with get_user_pages(), and then after the read
1822 * completed.
1823 */
1824 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001825
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001826 if (req && req->num_pages &&
1827 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1828 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1829 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1830 fuse_writepages_send(data);
1831 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001832 }
1833 err = -ENOMEM;
1834 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1835 if (!tmp_page)
1836 goto out_unlock;
1837
1838 /*
1839 * The page must not be redirtied until the writeout is completed
1840 * (i.e. userspace has sent a reply to the write request). Otherwise
1841 * there could be more than one temporary page instance for each real
1842 * page.
1843 *
1844 * This is ensured by holding the page lock in page_mkwrite() while
1845 * checking fuse_page_is_writeback(). We already hold the page lock
1846 * since clear_page_dirty_for_io() and keep it held until we add the
1847 * request to the fi->writepages list and increment req->num_pages.
1848 * After this fuse_page_is_writeback() will indicate that the page is
1849 * under writeback, so we can release the page lock.
1850 */
1851 if (data->req == NULL) {
1852 struct fuse_inode *fi = get_fuse_inode(inode);
1853
1854 err = -ENOMEM;
1855 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1856 if (!req) {
1857 __free_page(tmp_page);
1858 goto out_unlock;
1859 }
1860
1861 fuse_write_fill(req, data->ff, page_offset(page), 0);
1862 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001863 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001864 req->in.argpages = 1;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001865 __set_bit(FR_BACKGROUND, &req->flags);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001866 req->num_pages = 0;
1867 req->end = fuse_writepage_end;
1868 req->inode = inode;
1869
1870 spin_lock(&fc->lock);
1871 list_add(&req->writepages_entry, &fi->writepages);
1872 spin_unlock(&fc->lock);
1873
1874 data->req = req;
1875 }
1876 set_page_writeback(page);
1877
1878 copy_highpage(tmp_page, page);
1879 req->pages[req->num_pages] = tmp_page;
1880 req->page_descs[req->num_pages].offset = 0;
1881 req->page_descs[req->num_pages].length = PAGE_SIZE;
1882
Tejun Heo93f78d82015-05-22 17:13:27 -04001883 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001884 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001885
1886 err = 0;
1887 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1888 end_page_writeback(page);
1889 data->req = NULL;
1890 goto out_unlock;
1891 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001892 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001893
1894 /*
1895 * Protected by fc->lock against concurrent access by
1896 * fuse_page_is_writeback().
1897 */
1898 spin_lock(&fc->lock);
1899 req->num_pages++;
1900 spin_unlock(&fc->lock);
1901
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001902out_unlock:
1903 unlock_page(page);
1904
1905 return err;
1906}
1907
1908static int fuse_writepages(struct address_space *mapping,
1909 struct writeback_control *wbc)
1910{
1911 struct inode *inode = mapping->host;
1912 struct fuse_fill_wb_data data;
1913 int err;
1914
1915 err = -EIO;
1916 if (is_bad_inode(inode))
1917 goto out;
1918
1919 data.inode = inode;
1920 data.req = NULL;
1921 data.ff = NULL;
1922
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001923 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001924 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1925 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001926 GFP_NOFS);
1927 if (!data.orig_pages)
1928 goto out;
1929
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001930 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1931 if (data.req) {
1932 /* Ignore errors if we can write at least one page */
1933 BUG_ON(!data.req->num_pages);
1934 fuse_writepages_send(&data);
1935 err = 0;
1936 }
1937 if (data.ff)
1938 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001939
1940 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001941out:
1942 return err;
1943}
1944
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001945/*
1946 * It's worthy to make sure that space is reserved on disk for the write,
1947 * but how to implement it without killing performance need more thinking.
1948 */
1949static int fuse_write_begin(struct file *file, struct address_space *mapping,
1950 loff_t pos, unsigned len, unsigned flags,
1951 struct page **pagep, void **fsdata)
1952{
1953 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001954 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001955 struct page *page;
1956 loff_t fsize;
1957 int err = -ENOMEM;
1958
1959 WARN_ON(!fc->writeback_cache);
1960
1961 page = grab_cache_page_write_begin(mapping, index, flags);
1962 if (!page)
1963 goto error;
1964
1965 fuse_wait_on_page_writeback(mapping->host, page->index);
1966
1967 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1968 goto success;
1969 /*
1970 * Check if the start this page comes after the end of file, in which
1971 * case the readpage can be optimized away.
1972 */
1973 fsize = i_size_read(mapping->host);
1974 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1975 size_t off = pos & ~PAGE_CACHE_MASK;
1976 if (off)
1977 zero_user_segment(page, 0, off);
1978 goto success;
1979 }
1980 err = fuse_do_readpage(file, page);
1981 if (err)
1982 goto cleanup;
1983success:
1984 *pagep = page;
1985 return 0;
1986
1987cleanup:
1988 unlock_page(page);
1989 page_cache_release(page);
1990error:
1991 return err;
1992}
1993
1994static int fuse_write_end(struct file *file, struct address_space *mapping,
1995 loff_t pos, unsigned len, unsigned copied,
1996 struct page *page, void *fsdata)
1997{
1998 struct inode *inode = page->mapping->host;
1999
2000 if (!PageUptodate(page)) {
2001 /* Zero any unwritten bytes at the end of the page */
2002 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2003 if (endoff)
2004 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2005 SetPageUptodate(page);
2006 }
2007
2008 fuse_write_update_size(inode, pos + copied);
2009 set_page_dirty(page);
2010 unlock_page(page);
2011 page_cache_release(page);
2012
2013 return copied;
2014}
2015
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002016static int fuse_launder_page(struct page *page)
2017{
2018 int err = 0;
2019 if (clear_page_dirty_for_io(page)) {
2020 struct inode *inode = page->mapping->host;
2021 err = fuse_writepage_locked(page);
2022 if (!err)
2023 fuse_wait_on_page_writeback(inode, page->index);
2024 }
2025 return err;
2026}
2027
2028/*
2029 * Write back dirty pages now, because there may not be any suitable
2030 * open files later
2031 */
2032static void fuse_vma_close(struct vm_area_struct *vma)
2033{
2034 filemap_write_and_wait(vma->vm_file->f_mapping);
2035}
2036
2037/*
2038 * Wait for writeback against this page to complete before allowing it
2039 * to be marked dirty again, and hence written back again, possibly
2040 * before the previous writepage completed.
2041 *
2042 * Block here, instead of in ->writepage(), so that the userspace fs
2043 * can only block processes actually operating on the filesystem.
2044 *
2045 * Otherwise unprivileged userspace fs would be able to block
2046 * unrelated:
2047 *
2048 * - page migration
2049 * - sync(2)
2050 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2051 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002052static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002053{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002054 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002055 struct inode *inode = file_inode(vma->vm_file);
2056
2057 file_update_time(vma->vm_file);
2058 lock_page(page);
2059 if (page->mapping != inode->i_mapping) {
2060 unlock_page(page);
2061 return VM_FAULT_NOPAGE;
2062 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002063
2064 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002065 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002066}
2067
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002068static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002069 .close = fuse_vma_close,
2070 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002071 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002072 .page_mkwrite = fuse_page_mkwrite,
2073};
2074
2075static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2076{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002077 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2078 fuse_link_write_file(file);
2079
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002080 file_accessed(file);
2081 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002082 return 0;
2083}
2084
Miklos Szeredifc280c92009-04-02 14:25:35 +02002085static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2086{
2087 /* Can't provide the coherency needed for MAP_SHARED */
2088 if (vma->vm_flags & VM_MAYSHARE)
2089 return -ENODEV;
2090
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002091 invalidate_inode_pages2(file->f_mapping);
2092
Miklos Szeredifc280c92009-04-02 14:25:35 +02002093 return generic_file_mmap(file, vma);
2094}
2095
Miklos Szeredi71421252006-06-25 05:48:52 -07002096static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2097 struct file_lock *fl)
2098{
2099 switch (ffl->type) {
2100 case F_UNLCK:
2101 break;
2102
2103 case F_RDLCK:
2104 case F_WRLCK:
2105 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2106 ffl->end < ffl->start)
2107 return -EIO;
2108
2109 fl->fl_start = ffl->start;
2110 fl->fl_end = ffl->end;
2111 fl->fl_pid = ffl->pid;
2112 break;
2113
2114 default:
2115 return -EIO;
2116 }
2117 fl->fl_type = ffl->type;
2118 return 0;
2119}
2120
Miklos Szeredi70781872014-12-12 09:49:05 +01002121static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002122 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002123 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002124{
Al Viro6131ffa2013-02-27 16:59:05 -05002125 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002126 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002127 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002128
Miklos Szeredi70781872014-12-12 09:49:05 +01002129 memset(inarg, 0, sizeof(*inarg));
2130 inarg->fh = ff->fh;
2131 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2132 inarg->lk.start = fl->fl_start;
2133 inarg->lk.end = fl->fl_end;
2134 inarg->lk.type = fl->fl_type;
2135 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002136 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002137 inarg->lk_flags |= FUSE_LK_FLOCK;
2138 args->in.h.opcode = opcode;
2139 args->in.h.nodeid = get_node_id(inode);
2140 args->in.numargs = 1;
2141 args->in.args[0].size = sizeof(*inarg);
2142 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002143}
2144
2145static int fuse_getlk(struct file *file, struct file_lock *fl)
2146{
Al Viro6131ffa2013-02-27 16:59:05 -05002147 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002148 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002149 FUSE_ARGS(args);
2150 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002151 struct fuse_lk_out outarg;
2152 int err;
2153
Miklos Szeredi70781872014-12-12 09:49:05 +01002154 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2155 args.out.numargs = 1;
2156 args.out.args[0].size = sizeof(outarg);
2157 args.out.args[0].value = &outarg;
2158 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002159 if (!err)
2160 err = convert_fuse_file_lock(&outarg.lk, fl);
2161
2162 return err;
2163}
2164
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002165static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002166{
Al Viro6131ffa2013-02-27 16:59:05 -05002167 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002169 FUSE_ARGS(args);
2170 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002171 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2172 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2173 int err;
2174
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002175 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002176 /* NLM needs asynchronous locks, which we don't support yet */
2177 return -ENOLCK;
2178 }
2179
Miklos Szeredi71421252006-06-25 05:48:52 -07002180 /* Unlock on close is handled by the flush method */
2181 if (fl->fl_flags & FL_CLOSE)
2182 return 0;
2183
Miklos Szeredi70781872014-12-12 09:49:05 +01002184 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2185 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002186
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002187 /* locking is restartable */
2188 if (err == -EINTR)
2189 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002190
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 return err;
2192}
2193
2194static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2195{
Al Viro6131ffa2013-02-27 16:59:05 -05002196 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002197 struct fuse_conn *fc = get_fuse_conn(inode);
2198 int err;
2199
Miklos Szeredi48e90762008-07-25 01:49:02 -07002200 if (cmd == F_CANCELLK) {
2201 err = 0;
2202 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002203 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002204 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002205 err = 0;
2206 } else
2207 err = fuse_getlk(file, fl);
2208 } else {
2209 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002210 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002212 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002213 }
2214 return err;
2215}
2216
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002217static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2218{
Al Viro6131ffa2013-02-27 16:59:05 -05002219 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002220 struct fuse_conn *fc = get_fuse_conn(inode);
2221 int err;
2222
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002223 if (fc->no_flock) {
Benjamin Coddington4f656362015-10-22 13:38:14 -04002224 err = locks_lock_file_wait(file, fl);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002225 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002226 struct fuse_file *ff = file->private_data;
2227
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002228 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002229 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002230 err = fuse_setlk(file, fl, 1);
2231 }
2232
2233 return err;
2234}
2235
Miklos Szeredib2d22722006-12-06 20:35:51 -08002236static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2237{
2238 struct inode *inode = mapping->host;
2239 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002240 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002241 struct fuse_bmap_in inarg;
2242 struct fuse_bmap_out outarg;
2243 int err;
2244
2245 if (!inode->i_sb->s_bdev || fc->no_bmap)
2246 return 0;
2247
Miklos Szeredib2d22722006-12-06 20:35:51 -08002248 memset(&inarg, 0, sizeof(inarg));
2249 inarg.block = block;
2250 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002251 args.in.h.opcode = FUSE_BMAP;
2252 args.in.h.nodeid = get_node_id(inode);
2253 args.in.numargs = 1;
2254 args.in.args[0].size = sizeof(inarg);
2255 args.in.args[0].value = &inarg;
2256 args.out.numargs = 1;
2257 args.out.args[0].size = sizeof(outarg);
2258 args.out.args[0].value = &outarg;
2259 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002260 if (err == -ENOSYS)
2261 fc->no_bmap = 1;
2262
2263 return err ? 0 : outarg.block;
2264}
2265
Andrew Morton965c8e52012-12-17 15:59:39 -08002266static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002267{
2268 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002269 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002270
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002271 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002272 if (whence == SEEK_CUR || whence == SEEK_SET)
2273 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002274
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002275 mutex_lock(&inode->i_mutex);
2276 retval = fuse_update_attributes(inode, NULL, file, NULL);
2277 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002278 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002279 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002280
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002281 return retval;
2282}
2283
Tejun Heo59efec72008-11-26 12:03:55 +01002284static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2285 unsigned int nr_segs, size_t bytes, bool to_user)
2286{
2287 struct iov_iter ii;
2288 int page_idx = 0;
2289
2290 if (!bytes)
2291 return 0;
2292
Al Viro71d8e532014-03-05 19:28:09 -05002293 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
Tejun Heo59efec72008-11-26 12:03:55 +01002294
2295 while (iov_iter_count(&ii)) {
2296 struct page *page = pages[page_idx++];
2297 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002298 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002299
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002300 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002301
2302 while (todo) {
2303 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2304 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2305 size_t copy = min(todo, iov_len);
2306 size_t left;
2307
2308 if (!to_user)
2309 left = copy_from_user(kaddr, uaddr, copy);
2310 else
2311 left = copy_to_user(uaddr, kaddr, copy);
2312
2313 if (unlikely(left))
2314 return -EFAULT;
2315
2316 iov_iter_advance(&ii, copy);
2317 todo -= copy;
2318 kaddr += copy;
2319 }
2320
Jens Axboe0bd87182009-11-03 11:40:44 +01002321 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002322 }
2323
2324 return 0;
2325}
2326
2327/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002328 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2329 * ABI was defined to be 'struct iovec' which is different on 32bit
2330 * and 64bit. Fortunately we can determine which structure the server
2331 * used from the size of the reply.
2332 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002333static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2334 size_t transferred, unsigned count,
2335 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002336{
2337#ifdef CONFIG_COMPAT
2338 if (count * sizeof(struct compat_iovec) == transferred) {
2339 struct compat_iovec *ciov = src;
2340 unsigned i;
2341
2342 /*
2343 * With this interface a 32bit server cannot support
2344 * non-compat (i.e. ones coming from 64bit apps) ioctl
2345 * requests
2346 */
2347 if (!is_compat)
2348 return -EINVAL;
2349
2350 for (i = 0; i < count; i++) {
2351 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2352 dst[i].iov_len = ciov[i].iov_len;
2353 }
2354 return 0;
2355 }
2356#endif
2357
2358 if (count * sizeof(struct iovec) != transferred)
2359 return -EIO;
2360
2361 memcpy(dst, src, transferred);
2362 return 0;
2363}
2364
Miklos Szeredi75727772010-11-30 16:39:27 +01002365/* Make sure iov_length() won't overflow */
2366static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2367{
2368 size_t n;
2369 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2370
Zach Brownfb6ccff2012-07-24 12:10:11 -07002371 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002372 if (iov->iov_len > (size_t) max)
2373 return -ENOMEM;
2374 max -= iov->iov_len;
2375 }
2376 return 0;
2377}
2378
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002379static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2380 void *src, size_t transferred, unsigned count,
2381 bool is_compat)
2382{
2383 unsigned i;
2384 struct fuse_ioctl_iovec *fiov = src;
2385
2386 if (fc->minor < 16) {
2387 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2388 count, is_compat);
2389 }
2390
2391 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2392 return -EIO;
2393
2394 for (i = 0; i < count; i++) {
2395 /* Did the server supply an inappropriate value? */
2396 if (fiov[i].base != (unsigned long) fiov[i].base ||
2397 fiov[i].len != (unsigned long) fiov[i].len)
2398 return -EIO;
2399
2400 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2401 dst[i].iov_len = (size_t) fiov[i].len;
2402
2403#ifdef CONFIG_COMPAT
2404 if (is_compat &&
2405 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2406 (compat_size_t) dst[i].iov_len != fiov[i].len))
2407 return -EIO;
2408#endif
2409 }
2410
2411 return 0;
2412}
2413
2414
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002415/*
Tejun Heo59efec72008-11-26 12:03:55 +01002416 * For ioctls, there is no generic way to determine how much memory
2417 * needs to be read and/or written. Furthermore, ioctls are allowed
2418 * to dereference the passed pointer, so the parameter requires deep
2419 * copying but FUSE has no idea whatsoever about what to copy in or
2420 * out.
2421 *
2422 * This is solved by allowing FUSE server to retry ioctl with
2423 * necessary in/out iovecs. Let's assume the ioctl implementation
2424 * needs to read in the following structure.
2425 *
2426 * struct a {
2427 * char *buf;
2428 * size_t buflen;
2429 * }
2430 *
2431 * On the first callout to FUSE server, inarg->in_size and
2432 * inarg->out_size will be NULL; then, the server completes the ioctl
2433 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2434 * the actual iov array to
2435 *
2436 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2437 *
2438 * which tells FUSE to copy in the requested area and retry the ioctl.
2439 * On the second round, the server has access to the structure and
2440 * from that it can tell what to look for next, so on the invocation,
2441 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2442 *
2443 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2444 * { .iov_base = a.buf, .iov_len = a.buflen } }
2445 *
2446 * FUSE will copy both struct a and the pointed buffer from the
2447 * process doing the ioctl and retry ioctl with both struct a and the
2448 * buffer.
2449 *
2450 * This time, FUSE server has everything it needs and completes ioctl
2451 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2452 *
2453 * Copying data out works the same way.
2454 *
2455 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2456 * automatically initializes in and out iovs by decoding @cmd with
2457 * _IOC_* macros and the server is not allowed to request RETRY. This
2458 * limits ioctl data transfers to well-formed ioctls and is the forced
2459 * behavior for all FUSE servers.
2460 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002461long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2462 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002463{
Tejun Heo59efec72008-11-26 12:03:55 +01002464 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002465 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002466 struct fuse_ioctl_in inarg = {
2467 .fh = ff->fh,
2468 .cmd = cmd,
2469 .arg = arg,
2470 .flags = flags
2471 };
2472 struct fuse_ioctl_out outarg;
2473 struct fuse_req *req = NULL;
2474 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002475 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002476 struct iovec *in_iov = NULL, *out_iov = NULL;
2477 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2478 size_t in_size, out_size, transferred;
2479 int err;
2480
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002481#if BITS_PER_LONG == 32
2482 inarg.flags |= FUSE_IOCTL_32BIT;
2483#else
2484 if (flags & FUSE_IOCTL_COMPAT)
2485 inarg.flags |= FUSE_IOCTL_32BIT;
2486#endif
2487
Tejun Heo59efec72008-11-26 12:03:55 +01002488 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002489 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002490
Tejun Heo59efec72008-11-26 12:03:55 +01002491 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002492 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002493 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002494 if (!pages || !iov_page)
2495 goto out;
2496
2497 /*
2498 * If restricted, initialize IO parameters as encoded in @cmd.
2499 * RETRY from server is not allowed.
2500 */
2501 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002502 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002503
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002504 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002505 iov->iov_len = _IOC_SIZE(cmd);
2506
2507 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2508 in_iov = iov;
2509 in_iovs = 1;
2510 }
2511
2512 if (_IOC_DIR(cmd) & _IOC_READ) {
2513 out_iov = iov;
2514 out_iovs = 1;
2515 }
2516 }
2517
2518 retry:
2519 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2520 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2521
2522 /*
2523 * Out data can be used either for actual out data or iovs,
2524 * make sure there always is at least one page.
2525 */
2526 out_size = max_t(size_t, out_size, PAGE_SIZE);
2527 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2528
2529 /* make sure there are enough buffer pages and init request with them */
2530 err = -ENOMEM;
2531 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2532 goto out;
2533 while (num_pages < max_pages) {
2534 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2535 if (!pages[num_pages])
2536 goto out;
2537 num_pages++;
2538 }
2539
Maxim Patlasov54b96672012-10-26 19:49:13 +04002540 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002541 if (IS_ERR(req)) {
2542 err = PTR_ERR(req);
2543 req = NULL;
2544 goto out;
2545 }
2546 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2547 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002548 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002549
2550 /* okay, let's send it to the client */
2551 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002552 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002553 req->in.numargs = 1;
2554 req->in.args[0].size = sizeof(inarg);
2555 req->in.args[0].value = &inarg;
2556 if (in_size) {
2557 req->in.numargs++;
2558 req->in.args[1].size = in_size;
2559 req->in.argpages = 1;
2560
2561 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2562 false);
2563 if (err)
2564 goto out;
2565 }
2566
2567 req->out.numargs = 2;
2568 req->out.args[0].size = sizeof(outarg);
2569 req->out.args[0].value = &outarg;
2570 req->out.args[1].size = out_size;
2571 req->out.argpages = 1;
2572 req->out.argvar = 1;
2573
Tejun Heob93f8582008-11-26 12:03:55 +01002574 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002575 err = req->out.h.error;
2576 transferred = req->out.args[1].size;
2577 fuse_put_request(fc, req);
2578 req = NULL;
2579 if (err)
2580 goto out;
2581
2582 /* did it ask for retry? */
2583 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002584 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002585
2586 /* no retry if in restricted mode */
2587 err = -EIO;
2588 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2589 goto out;
2590
2591 in_iovs = outarg.in_iovs;
2592 out_iovs = outarg.out_iovs;
2593
2594 /*
2595 * Make sure things are in boundary, separate checks
2596 * are to protect against overflow.
2597 */
2598 err = -ENOMEM;
2599 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2600 out_iovs > FUSE_IOCTL_MAX_IOV ||
2601 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2602 goto out;
2603
Cong Wang2408f6e2011-11-25 23:14:30 +08002604 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002605 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002606 transferred, in_iovs + out_iovs,
2607 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002608 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002609 if (err)
2610 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002611
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002612 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002613 out_iov = in_iov + in_iovs;
2614
Miklos Szeredi75727772010-11-30 16:39:27 +01002615 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2616 if (err)
2617 goto out;
2618
2619 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2620 if (err)
2621 goto out;
2622
Tejun Heo59efec72008-11-26 12:03:55 +01002623 goto retry;
2624 }
2625
2626 err = -EIO;
2627 if (transferred > inarg.out_size)
2628 goto out;
2629
2630 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2631 out:
2632 if (req)
2633 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002634 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002635 while (num_pages)
2636 __free_page(pages[--num_pages]);
2637 kfree(pages);
2638
2639 return err ? err : outarg.result;
2640}
Tejun Heo08cbf542009-04-14 10:54:53 +09002641EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002642
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002643long fuse_ioctl_common(struct file *file, unsigned int cmd,
2644 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002645{
Al Viro6131ffa2013-02-27 16:59:05 -05002646 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002647 struct fuse_conn *fc = get_fuse_conn(inode);
2648
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002649 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002650 return -EACCES;
2651
2652 if (is_bad_inode(inode))
2653 return -EIO;
2654
2655 return fuse_do_ioctl(file, cmd, arg, flags);
2656}
2657
Tejun Heo59efec72008-11-26 12:03:55 +01002658static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2659 unsigned long arg)
2660{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002661 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002662}
2663
2664static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2665 unsigned long arg)
2666{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002667 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002668}
2669
Tejun Heo95668a62008-11-26 12:03:55 +01002670/*
2671 * All files which have been polled are linked to RB tree
2672 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2673 * find the matching one.
2674 */
2675static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2676 struct rb_node **parent_out)
2677{
2678 struct rb_node **link = &fc->polled_files.rb_node;
2679 struct rb_node *last = NULL;
2680
2681 while (*link) {
2682 struct fuse_file *ff;
2683
2684 last = *link;
2685 ff = rb_entry(last, struct fuse_file, polled_node);
2686
2687 if (kh < ff->kh)
2688 link = &last->rb_left;
2689 else if (kh > ff->kh)
2690 link = &last->rb_right;
2691 else
2692 return link;
2693 }
2694
2695 if (parent_out)
2696 *parent_out = last;
2697 return link;
2698}
2699
2700/*
2701 * The file is about to be polled. Make sure it's on the polled_files
2702 * RB tree. Note that files once added to the polled_files tree are
2703 * not removed before the file is released. This is because a file
2704 * polled once is likely to be polled again.
2705 */
2706static void fuse_register_polled_file(struct fuse_conn *fc,
2707 struct fuse_file *ff)
2708{
2709 spin_lock(&fc->lock);
2710 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002711 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002712
2713 link = fuse_find_polled_node(fc, ff->kh, &parent);
2714 BUG_ON(*link);
2715 rb_link_node(&ff->polled_node, parent, link);
2716 rb_insert_color(&ff->polled_node, &fc->polled_files);
2717 }
2718 spin_unlock(&fc->lock);
2719}
2720
Tejun Heo08cbf542009-04-14 10:54:53 +09002721unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002722{
Tejun Heo95668a62008-11-26 12:03:55 +01002723 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002724 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002725 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2726 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002727 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002728 int err;
2729
2730 if (fc->no_poll)
2731 return DEFAULT_POLLMASK;
2732
2733 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002734 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002735
2736 /*
2737 * Ask for notification iff there's someone waiting for it.
2738 * The client may ignore the flag and always notify.
2739 */
2740 if (waitqueue_active(&ff->poll_wait)) {
2741 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2742 fuse_register_polled_file(fc, ff);
2743 }
2744
Miklos Szeredi70781872014-12-12 09:49:05 +01002745 args.in.h.opcode = FUSE_POLL;
2746 args.in.h.nodeid = ff->nodeid;
2747 args.in.numargs = 1;
2748 args.in.args[0].size = sizeof(inarg);
2749 args.in.args[0].value = &inarg;
2750 args.out.numargs = 1;
2751 args.out.args[0].size = sizeof(outarg);
2752 args.out.args[0].value = &outarg;
2753 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002754
2755 if (!err)
2756 return outarg.revents;
2757 if (err == -ENOSYS) {
2758 fc->no_poll = 1;
2759 return DEFAULT_POLLMASK;
2760 }
2761 return POLLERR;
2762}
Tejun Heo08cbf542009-04-14 10:54:53 +09002763EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002764
2765/*
2766 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2767 * wakes up the poll waiters.
2768 */
2769int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2770 struct fuse_notify_poll_wakeup_out *outarg)
2771{
2772 u64 kh = outarg->kh;
2773 struct rb_node **link;
2774
2775 spin_lock(&fc->lock);
2776
2777 link = fuse_find_polled_node(fc, kh, NULL);
2778 if (*link) {
2779 struct fuse_file *ff;
2780
2781 ff = rb_entry(*link, struct fuse_file, polled_node);
2782 wake_up_interruptible_sync(&ff->poll_wait);
2783 }
2784
2785 spin_unlock(&fc->lock);
2786 return 0;
2787}
2788
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002789static void fuse_do_truncate(struct file *file)
2790{
2791 struct inode *inode = file->f_mapping->host;
2792 struct iattr attr;
2793
2794 attr.ia_valid = ATTR_SIZE;
2795 attr.ia_size = i_size_read(inode);
2796
2797 attr.ia_file = file;
2798 attr.ia_valid |= ATTR_FILE;
2799
2800 fuse_do_setattr(inode, &attr, file);
2801}
2802
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002803static inline loff_t fuse_round_up(loff_t off)
2804{
2805 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2806}
2807
Anand Avati4273b792012-02-17 12:46:25 -05002808static ssize_t
Omar Sandoval22c61862015-03-16 04:33:53 -07002809fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
Anand Avati4273b792012-02-17 12:46:25 -05002810{
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002811 DECLARE_COMPLETION_ONSTACK(wait);
Anand Avati4273b792012-02-17 12:46:25 -05002812 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002813 struct file *file = iocb->ki_filp;
2814 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002815 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002816 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002817 struct inode *inode;
2818 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002819 size_t count = iov_iter_count(iter);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002820 struct fuse_io_priv *io;
Robert Doebbelin32b98072016-03-07 09:50:56 +01002821 bool is_sync = is_sync_kiocb(iocb);
Anand Avati4273b792012-02-17 12:46:25 -05002822
Anand Avati4273b792012-02-17 12:46:25 -05002823 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002824 inode = file->f_mapping->host;
2825 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002826
Omar Sandoval6f673762015-03-16 04:33:52 -07002827 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002828 return 0;
2829
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002830 /* optimization for short read */
Omar Sandoval6f673762015-03-16 04:33:52 -07002831 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002832 if (offset >= i_size)
2833 return 0;
Al Viro6b775b12015-04-07 15:06:19 -04002834 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2835 count = iov_iter_count(iter);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002836 }
2837
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002838 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002839 if (!io)
2840 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002841 spin_lock_init(&io->lock);
Seth Forshee37bd8c82016-03-11 10:35:34 -06002842 kref_init(&io->refcnt);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002843 io->reqs = 1;
2844 io->bytes = -1;
2845 io->size = 0;
2846 io->offset = offset;
Omar Sandoval6f673762015-03-16 04:33:52 -07002847 io->write = (iov_iter_rw(iter) == WRITE);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002848 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002849 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002850 /*
2851 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002852 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002853 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002854 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002855 io->iocb = iocb;
2856
2857 /*
2858 * We cannot asynchronously extend the size of a file. We have no method
2859 * to wait on real async I/O requests, so we must submit this request
2860 * synchronously.
2861 */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002862 if (!is_sync && (offset + count > i_size) &&
Omar Sandoval6f673762015-03-16 04:33:52 -07002863 iov_iter_rw(iter) == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002864 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002865
Seth Forshee37bd8c82016-03-11 10:35:34 -06002866 if (io->async && is_sync) {
2867 /*
2868 * Additional reference to keep io around after
2869 * calling fuse_aio_complete()
2870 */
2871 kref_get(&io->refcnt);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002872 io->done = &wait;
Seth Forshee37bd8c82016-03-11 10:35:34 -06002873 }
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002874
Omar Sandoval6f673762015-03-16 04:33:52 -07002875 if (iov_iter_rw(iter) == WRITE) {
Al Viro6b775b12015-04-07 15:06:19 -04002876 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
Al Viro812408f2015-03-30 22:15:58 -04002877 fuse_invalidate_attr(inode);
2878 } else {
Al Virod22a9432014-03-16 15:50:47 -04002879 ret = __fuse_direct_read(io, iter, &pos);
Al Viro812408f2015-03-30 22:15:58 -04002880 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002881
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002882 if (io->async) {
2883 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2884
2885 /* we have a non-extending, async request, so return */
Robert Doebbelin32b98072016-03-07 09:50:56 +01002886 if (!is_sync)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002887 return -EIOCBQUEUED;
2888
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002889 wait_for_completion(&wait);
2890 ret = fuse_get_res_by_io(io);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002891 }
2892
Seth Forshee37bd8c82016-03-11 10:35:34 -06002893 kref_put(&io->refcnt, fuse_io_release);
Christoph Hellwig9d5722b2015-02-02 14:59:43 +01002894
Omar Sandoval6f673762015-03-16 04:33:52 -07002895 if (iov_iter_rw(iter) == WRITE) {
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002896 if (ret > 0)
2897 fuse_write_update_size(inode, pos);
2898 else if (ret < 0 && offset + count > i_size)
2899 fuse_do_truncate(file);
2900 }
Anand Avati4273b792012-02-17 12:46:25 -05002901
2902 return ret;
2903}
2904
Miklos Szeredicdadb112012-11-10 16:55:56 +01002905static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2906 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002907{
2908 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002909 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002910 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002911 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002912 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002913 struct fuse_fallocate_in inarg = {
2914 .fh = ff->fh,
2915 .offset = offset,
2916 .length = length,
2917 .mode = mode
2918 };
2919 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002920 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2921 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002922
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002923 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2924 return -EOPNOTSUPP;
2925
Miklos Szeredi519c6042012-04-26 10:56:36 +02002926 if (fc->no_fallocate)
2927 return -EOPNOTSUPP;
2928
Maxim Patlasov14c14412013-06-13 12:16:39 +04002929 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002930 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002931 if (mode & FALLOC_FL_PUNCH_HOLE) {
2932 loff_t endbyte = offset + length - 1;
2933 err = filemap_write_and_wait_range(inode->i_mapping,
2934 offset, endbyte);
2935 if (err)
2936 goto out;
2937
2938 fuse_sync_writes(inode);
2939 }
Brian Foster3634a632013-05-17 09:30:32 -04002940 }
2941
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002942 if (!(mode & FALLOC_FL_KEEP_SIZE))
2943 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2944
Miklos Szeredi70781872014-12-12 09:49:05 +01002945 args.in.h.opcode = FUSE_FALLOCATE;
2946 args.in.h.nodeid = ff->nodeid;
2947 args.in.numargs = 1;
2948 args.in.args[0].size = sizeof(inarg);
2949 args.in.args[0].value = &inarg;
2950 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002951 if (err == -ENOSYS) {
2952 fc->no_fallocate = 1;
2953 err = -EOPNOTSUPP;
2954 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002955 if (err)
2956 goto out;
2957
2958 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002959 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2960 bool changed = fuse_write_update_size(inode, offset + length);
2961
Miklos Szeredi93d22692014-04-28 14:19:22 +02002962 if (changed && fc->writeback_cache)
2963 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002964 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002965
2966 if (mode & FALLOC_FL_PUNCH_HOLE)
2967 truncate_pagecache_range(inode, offset, offset + length - 1);
2968
2969 fuse_invalidate_attr(inode);
2970
Brian Foster3634a632013-05-17 09:30:32 -04002971out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002972 if (!(mode & FALLOC_FL_KEEP_SIZE))
2973 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2974
Maxim Patlasovbde52782013-09-13 19:19:54 +04002975 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002976 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002977
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002978 return err;
2979}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002980
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002981static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002982 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04002983 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04002984 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002985 .mmap = fuse_file_mmap,
2986 .open = fuse_open,
2987 .flush = fuse_flush,
2988 .release = fuse_release,
2989 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002990 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002991 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002992 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002993 .unlocked_ioctl = fuse_file_ioctl,
2994 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002995 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002996 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002997};
2998
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002999static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003000 .llseek = fuse_file_llseek,
Al Viro15316262015-03-30 22:08:36 -04003001 .read_iter = fuse_direct_read_iter,
Al Viro15316262015-03-30 22:08:36 -04003002 .write_iter = fuse_direct_write_iter,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003003 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003004 .open = fuse_open,
3005 .flush = fuse_flush,
3006 .release = fuse_release,
3007 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003008 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003009 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003010 .unlocked_ioctl = fuse_file_ioctl,
3011 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003012 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003013 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003014 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003015};
3016
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003017static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003018 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003019 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003020 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003021 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003022 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003023 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003024 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003025 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003026 .write_begin = fuse_write_begin,
3027 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003028};
3029
3030void fuse_init_file_inode(struct inode *inode)
3031{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003032 inode->i_fop = &fuse_file_operations;
3033 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003034}