blob: 760b2c55219741a48e0e444d482cb9e08535f938 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070018#include <linux/aio.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.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 */
99 req->background = 0;
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) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400103 req->background = 0;
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;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400109 req->background = 1;
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);
302 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400303 ff->reserved_req->background = 0;
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 Patlasovb111c8c2012-10-26 19:48:30 +0400420 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421 memset(&inarg, 0, sizeof(inarg));
422 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700423 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700424 req->in.h.opcode = FUSE_FLUSH;
425 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426 req->in.numargs = 1;
427 req->in.args[0].size = sizeof(inarg);
428 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700429 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100430 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 err = req->out.h.error;
432 fuse_put_request(fc, req);
433 if (err == -ENOSYS) {
434 fc->no_flush = 1;
435 err = 0;
436 }
437 return err;
438}
439
Josef Bacik02c24a82011-07-16 20:44:56 -0400440int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
441 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700442{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200443 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 struct fuse_conn *fc = get_fuse_conn(inode);
445 struct fuse_file *ff = file->private_data;
Miklos Szeredi70781872014-12-12 09:49:05 +0100446 FUSE_ARGS(args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 struct fuse_fsync_in inarg;
448 int err;
449
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800450 if (is_bad_inode(inode))
451 return -EIO;
452
Josef Bacik02c24a82011-07-16 20:44:56 -0400453 mutex_lock(&inode->i_mutex);
454
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700455 /*
456 * Start writeback against all dirty pages of the inode, then
457 * wait for all outstanding writes, before sending the FSYNC
458 * request.
459 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200460 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700461 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400462 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700463
464 fuse_sync_writes(inode);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200465 err = sync_inode_metadata(inode, 1);
466 if (err)
467 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700468
Miklos Szeredi22401e72014-04-28 14:19:23 +0200469 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
470 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400471
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472 memset(&inarg, 0, sizeof(inarg));
473 inarg.fh = ff->fh;
474 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi70781872014-12-12 09:49:05 +0100475 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
476 args.in.h.nodeid = get_node_id(inode);
477 args.in.numargs = 1;
478 args.in.args[0].size = sizeof(inarg);
479 args.in.args[0].value = &inarg;
480 err = fuse_simple_request(fc, &args);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700482 if (isdir)
483 fc->no_fsyncdir = 1;
484 else
485 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700486 err = 0;
487 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400488out:
489 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700490 return err;
491}
492
Josef Bacik02c24a82011-07-16 20:44:56 -0400493static int fuse_fsync(struct file *file, loff_t start, loff_t end,
494 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700495{
Josef Bacik02c24a82011-07-16 20:44:56 -0400496 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700497}
498
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200499void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
500 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700502 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800503 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700504
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800505 inarg->fh = ff->fh;
506 inarg->offset = pos;
507 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800508 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800509 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200510 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 req->in.numargs = 1;
512 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800513 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514 req->out.argvar = 1;
515 req->out.numargs = 1;
516 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700517}
518
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400519static void fuse_release_user_pages(struct fuse_req *req, int write)
520{
521 unsigned i;
522
523 for (i = 0; i < req->num_pages; i++) {
524 struct page *page = req->pages[i];
525 if (write)
526 set_page_dirty_lock(page);
527 put_page(page);
528 }
529}
530
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400531/**
532 * In case of short read, the caller sets 'pos' to the position of
533 * actual end of fuse request in IO request. Otherwise, if bytes_requested
534 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
535 *
536 * An example:
537 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
538 * both submitted asynchronously. The first of them was ACKed by userspace as
539 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
540 * second request was ACKed as short, e.g. only 1K was read, resulting in
541 * pos == 33K.
542 *
543 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
544 * will be equal to the length of the longest contiguous fragment of
545 * transferred data starting from the beginning of IO request.
546 */
547static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
548{
549 int left;
550
551 spin_lock(&io->lock);
552 if (err)
553 io->err = io->err ? : err;
554 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
555 io->bytes = pos;
556
557 left = --io->reqs;
558 spin_unlock(&io->lock);
559
560 if (!left) {
561 long res;
562
563 if (io->err)
564 res = io->err;
565 else if (io->bytes >= 0 && io->write)
566 res = -EIO;
567 else {
568 res = io->bytes < 0 ? io->size : io->bytes;
569
570 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400571 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400572 struct fuse_conn *fc = get_fuse_conn(inode);
573 struct fuse_inode *fi = get_fuse_inode(inode);
574
575 spin_lock(&fc->lock);
576 fi->attr_version = ++fc->attr_version;
577 spin_unlock(&fc->lock);
578 }
579 }
580
581 aio_complete(io->iocb, res, 0);
582 kfree(io);
583 }
584}
585
586static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
587{
588 struct fuse_io_priv *io = req->io;
589 ssize_t pos = -1;
590
591 fuse_release_user_pages(req, !io->write);
592
593 if (io->write) {
594 if (req->misc.write.in.size != req->misc.write.out.size)
595 pos = req->misc.write.in.offset - io->offset +
596 req->misc.write.out.size;
597 } else {
598 if (req->misc.read.in.size != req->out.args[0].size)
599 pos = req->misc.read.in.offset - io->offset +
600 req->out.args[0].size;
601 }
602
603 fuse_aio_complete(io, req->out.h.error, pos);
604}
605
606static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
607 size_t num_bytes, struct fuse_io_priv *io)
608{
609 spin_lock(&io->lock);
610 io->size += num_bytes;
611 io->reqs++;
612 spin_unlock(&io->lock);
613
614 req->io = io;
615 req->end = fuse_aio_complete_req;
616
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400617 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400618 fuse_request_send_background(fc, req);
619
620 return num_bytes;
621}
622
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400623static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200624 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700625{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400626 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200627 struct fuse_file *ff = file->private_data;
628 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700629
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200630 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700631 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700632 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700633
634 inarg->read_flags |= FUSE_READ_LOCKOWNER;
635 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
636 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400637
638 if (io->async)
639 return fuse_async_req_send(fc, req, count, io);
640
Tejun Heob93f8582008-11-26 12:03:55 +0100641 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800642 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700643}
644
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700645static void fuse_read_update_size(struct inode *inode, loff_t size,
646 u64 attr_ver)
647{
648 struct fuse_conn *fc = get_fuse_conn(inode);
649 struct fuse_inode *fi = get_fuse_inode(inode);
650
651 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400652 if (attr_ver == fi->attr_version && size < inode->i_size &&
653 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700654 fi->attr_version = ++fc->attr_version;
655 i_size_write(inode, size);
656 }
657 spin_unlock(&fc->lock);
658}
659
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400660static void fuse_short_read(struct fuse_req *req, struct inode *inode,
661 u64 attr_ver)
662{
663 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400664 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400665
Pavel Emelyanov83732002013-10-10 17:10:46 +0400666 if (fc->writeback_cache) {
667 /*
668 * A hole in a file. Some data after the hole are in page cache,
669 * but have not reached the client fs yet. So, the hole is not
670 * present there.
671 */
672 int i;
673 int start_idx = num_read >> PAGE_CACHE_SHIFT;
674 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
675
676 for (i = start_idx; i < req->num_pages; i++) {
677 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
678 off = 0;
679 }
680 } else {
681 loff_t pos = page_offset(req->pages[0]) + num_read;
682 fuse_read_update_size(inode, pos, attr_ver);
683 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400684}
685
Maxim Patlasov482fce52013-10-10 17:11:25 +0400686static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700687{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400688 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700689 struct inode *inode = page->mapping->host;
690 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800691 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700692 size_t num_read;
693 loff_t pos = page_offset(page);
694 size_t count = PAGE_CACHE_SIZE;
695 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800696 int err;
697
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700698 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300699 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700700 * page-cache page, so make sure we read a properly synced
701 * page.
702 */
703 fuse_wait_on_page_writeback(inode, page->index);
704
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400705 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700706 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400707 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700708
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700709 attr_ver = fuse_get_attr_version(fc);
710
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700711 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200712 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700713 req->num_pages = 1;
714 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400715 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400716 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700717 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700718
719 if (!err) {
720 /*
721 * Short read means EOF. If file size is larger, truncate it
722 */
723 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400724 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700725
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700726 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700727 }
728
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400729 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400730
731 return err;
732}
733
734static int fuse_readpage(struct file *file, struct page *page)
735{
736 struct inode *inode = page->mapping->host;
737 int err;
738
739 err = -EIO;
740 if (is_bad_inode(inode))
741 goto out;
742
743 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800744 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 out:
746 unlock_page(page);
747 return err;
748}
749
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800750static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700751{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800752 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700753 size_t count = req->misc.read.in.size;
754 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200755 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800756
Miklos Szeredice534fb2010-05-25 15:06:07 +0200757 for (i = 0; mapping == NULL && i < req->num_pages; i++)
758 mapping = req->pages[i]->mapping;
759
760 if (mapping) {
761 struct inode *inode = mapping->host;
762
763 /*
764 * Short read means EOF. If file size is larger, truncate it
765 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400766 if (!req->out.h.error && num_read < count)
767 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200768
Andrew Gallagher451418f2013-11-05 03:55:43 -0800769 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700770 }
771
Miklos Szeredidb50b962005-09-09 13:10:33 -0700772 for (i = 0; i < req->num_pages; i++) {
773 struct page *page = req->pages[i];
774 if (!req->out.h.error)
775 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800776 else
777 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700778 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200779 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700780 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700781 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100782 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800783}
784
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200785static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800786{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200787 struct fuse_file *ff = file->private_data;
788 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800789 loff_t pos = page_offset(req->pages[0]);
790 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200791
792 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800793 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200795 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700796 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800797 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700798 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800799 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100800 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800801 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100802 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800803 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100804 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800805 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700806}
807
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700808struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700809 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800810 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700811 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400812 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700813};
814
815static int fuse_readpages_fill(void *_data, struct page *page)
816{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700817 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700818 struct fuse_req *req = data->req;
819 struct inode *inode = data->inode;
820 struct fuse_conn *fc = get_fuse_conn(inode);
821
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700822 fuse_wait_on_page_writeback(inode, page->index);
823
Miklos Szeredidb50b962005-09-09 13:10:33 -0700824 if (req->num_pages &&
825 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
826 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
827 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400828 int nr_alloc = min_t(unsigned, data->nr_pages,
829 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200830 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400831 if (fc->async_read)
832 req = fuse_get_req_for_background(fc, nr_alloc);
833 else
834 req = fuse_get_req(fc, nr_alloc);
835
836 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700837 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700839 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700841 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400842
843 if (WARN_ON(req->num_pages >= req->max_pages)) {
844 fuse_put_request(fc, req);
845 return -EIO;
846 }
847
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200848 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700849 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400850 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100851 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400852 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 return 0;
854}
855
856static int fuse_readpages(struct file *file, struct address_space *mapping,
857 struct list_head *pages, unsigned nr_pages)
858{
859 struct inode *inode = mapping->host;
860 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700861 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700862 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400863 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800864
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700865 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800866 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800867 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800868
Miklos Szeredia6643092007-11-28 16:22:00 -0800869 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400871 if (fc->async_read)
872 data.req = fuse_get_req_for_background(fc, nr_alloc);
873 else
874 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400875 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700876 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700877 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800878 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879
880 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700881 if (!err) {
882 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200883 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700884 else
885 fuse_put_request(fc, data.req);
886 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800887out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700888 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700889}
890
Al Viro37c20f12014-04-02 14:47:09 -0400891static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800892{
893 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400894 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800895
Brian Fostera8894272012-07-16 15:23:50 -0400896 /*
897 * In auto invalidate mode, always update attributes on read.
898 * Otherwise, only update if we attempt to read past EOF (to ensure
899 * i_size is up to date).
900 */
901 if (fc->auto_inval_data ||
Al Viro37c20f12014-04-02 14:47:09 -0400902 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800903 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800904 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
905 if (err)
906 return err;
907 }
908
Al Viro37c20f12014-04-02 14:47:09 -0400909 return generic_file_read_iter(iocb, to);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800910}
911
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200912static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200913 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700914{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700915 struct fuse_write_in *inarg = &req->misc.write.in;
916 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700917
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700918 inarg->fh = ff->fh;
919 inarg->offset = pos;
920 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700921 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200922 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700923 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200924 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700925 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
926 else
927 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700928 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700929 req->in.args[1].size = count;
930 req->out.numargs = 1;
931 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700932 req->out.args[0].value = outarg;
933}
934
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400935static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200936 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700937{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400938 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200939 struct fuse_file *ff = file->private_data;
940 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200941 struct fuse_write_in *inarg = &req->misc.write.in;
942
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200943 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200944 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700945 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700946 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
947 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
948 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400949
950 if (io->async)
951 return fuse_async_req_send(fc, req, count, io);
952
Tejun Heob93f8582008-11-26 12:03:55 +0100953 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700954 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955}
956
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400957bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700958{
959 struct fuse_conn *fc = get_fuse_conn(inode);
960 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400961 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700962
963 spin_lock(&fc->lock);
964 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400965 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700966 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400967 ret = true;
968 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700969 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400970
971 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700972}
973
Nick Pigginea9b9902008-04-30 00:54:42 -0700974static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
975 struct inode *inode, loff_t pos,
976 size_t count)
977{
978 size_t res;
979 unsigned offset;
980 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400981 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -0700982
983 for (i = 0; i < req->num_pages; i++)
984 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
985
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400986 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700987
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400988 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700989 count = res;
990 for (i = 0; i < req->num_pages; i++) {
991 struct page *page = req->pages[i];
992
993 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
994 SetPageUptodate(page);
995
996 if (count > PAGE_CACHE_SIZE - offset)
997 count -= PAGE_CACHE_SIZE - offset;
998 else
999 count = 0;
1000 offset = 0;
1001
1002 unlock_page(page);
1003 page_cache_release(page);
1004 }
1005
1006 return res;
1007}
1008
1009static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1010 struct address_space *mapping,
1011 struct iov_iter *ii, loff_t pos)
1012{
1013 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1014 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1015 size_t count = 0;
1016 int err;
1017
Miklos Szeredif4975c62009-04-02 14:25:34 +02001018 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001019 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001020
1021 do {
1022 size_t tmp;
1023 struct page *page;
1024 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1025 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1026 iov_iter_count(ii));
1027
1028 bytes = min_t(size_t, bytes, fc->max_write - count);
1029
1030 again:
1031 err = -EFAULT;
1032 if (iov_iter_fault_in_readable(ii, bytes))
1033 break;
1034
1035 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001036 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001037 if (!page)
1038 break;
1039
anfei zhou931e80e2010-02-02 13:44:02 -08001040 if (mapping_writably_mapped(mapping))
1041 flush_dcache_page(page);
1042
Nick Pigginea9b9902008-04-30 00:54:42 -07001043 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001044 flush_dcache_page(page);
1045
1046 if (!tmp) {
1047 unlock_page(page);
1048 page_cache_release(page);
1049 bytes = min(bytes, iov_iter_single_seg_count(ii));
1050 goto again;
1051 }
1052
1053 err = 0;
1054 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001055 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001056 req->num_pages++;
1057
1058 iov_iter_advance(ii, tmp);
1059 count += tmp;
1060 pos += tmp;
1061 offset += tmp;
1062 if (offset == PAGE_CACHE_SIZE)
1063 offset = 0;
1064
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001065 if (!fc->big_writes)
1066 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001067 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001068 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001069
1070 return count > 0 ? count : err;
1071}
1072
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001073static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1074{
1075 return min_t(unsigned,
1076 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1077 (pos >> PAGE_CACHE_SHIFT) + 1,
1078 FUSE_MAX_PAGES_PER_REQ);
1079}
1080
Nick Pigginea9b9902008-04-30 00:54:42 -07001081static ssize_t fuse_perform_write(struct file *file,
1082 struct address_space *mapping,
1083 struct iov_iter *ii, loff_t pos)
1084{
1085 struct inode *inode = mapping->host;
1086 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001087 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001088 int err = 0;
1089 ssize_t res = 0;
1090
1091 if (is_bad_inode(inode))
1092 return -EIO;
1093
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001094 if (inode->i_size < pos + iov_iter_count(ii))
1095 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1096
Nick Pigginea9b9902008-04-30 00:54:42 -07001097 do {
1098 struct fuse_req *req;
1099 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001100 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001101
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001102 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001103 if (IS_ERR(req)) {
1104 err = PTR_ERR(req);
1105 break;
1106 }
1107
1108 count = fuse_fill_write_pages(req, mapping, ii, pos);
1109 if (count <= 0) {
1110 err = count;
1111 } else {
1112 size_t num_written;
1113
1114 num_written = fuse_send_write_pages(req, file, inode,
1115 pos, count);
1116 err = req->out.h.error;
1117 if (!err) {
1118 res += num_written;
1119 pos += num_written;
1120
1121 /* break out of the loop on short write */
1122 if (num_written != count)
1123 err = -EIO;
1124 }
1125 }
1126 fuse_put_request(fc, req);
1127 } while (!err && iov_iter_count(ii));
1128
1129 if (res > 0)
1130 fuse_write_update_size(inode, pos);
1131
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001132 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001133 fuse_invalidate_attr(inode);
1134
1135 return res > 0 ? res : err;
1136}
1137
Al Viro84c3d552014-04-03 14:33:23 -04001138static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Nick Pigginea9b9902008-04-30 00:54:42 -07001139{
1140 struct file *file = iocb->ki_filp;
1141 struct address_space *mapping = file->f_mapping;
Al Viro84c3d552014-04-03 14:33:23 -04001142 size_t count = iov_iter_count(from);
Nick Pigginea9b9902008-04-30 00:54:42 -07001143 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001144 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001145 struct inode *inode = mapping->host;
1146 ssize_t err;
Anand Avati4273b792012-02-17 12:46:25 -05001147 loff_t endbyte = 0;
Al Viro84c3d552014-04-03 14:33:23 -04001148 loff_t pos = iocb->ki_pos;
Nick Pigginea9b9902008-04-30 00:54:42 -07001149
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001150 if (get_fuse_conn(inode)->writeback_cache) {
1151 /* Update size (EOF optimization) and mode (SUID clearing) */
1152 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1153 if (err)
1154 return err;
1155
Al Viro84c3d552014-04-03 14:33:23 -04001156 return generic_file_write_iter(iocb, from);
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001157 }
1158
Nick Pigginea9b9902008-04-30 00:54:42 -07001159 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001160
1161 /* We can write back this queue in page reclaim */
1162 current->backing_dev_info = mapping->backing_dev_info;
1163
1164 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1165 if (err)
1166 goto out;
1167
1168 if (count == 0)
1169 goto out;
1170
Al Viro84c3d552014-04-03 14:33:23 -04001171 iov_iter_truncate(from, count);
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001172 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001173 if (err)
1174 goto out;
1175
Josef Bacikc3b2da32012-03-26 09:59:21 -04001176 err = file_update_time(file);
1177 if (err)
1178 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001179
Anand Avati4273b792012-02-17 12:46:25 -05001180 if (file->f_flags & O_DIRECT) {
Al Viro84c3d552014-04-03 14:33:23 -04001181 written = generic_file_direct_write(iocb, from, pos);
1182 if (written < 0 || !iov_iter_count(from))
Anand Avati4273b792012-02-17 12:46:25 -05001183 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001184
Anand Avati4273b792012-02-17 12:46:25 -05001185 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001186
Al Viro84c3d552014-04-03 14:33:23 -04001187 written_buffered = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001188 if (written_buffered < 0) {
1189 err = written_buffered;
1190 goto out;
1191 }
1192 endbyte = pos + written_buffered - 1;
1193
1194 err = filemap_write_and_wait_range(file->f_mapping, pos,
1195 endbyte);
1196 if (err)
1197 goto out;
1198
1199 invalidate_mapping_pages(file->f_mapping,
1200 pos >> PAGE_CACHE_SHIFT,
1201 endbyte >> PAGE_CACHE_SHIFT);
1202
1203 written += written_buffered;
1204 iocb->ki_pos = pos + written_buffered;
1205 } else {
Al Viro84c3d552014-04-03 14:33:23 -04001206 written = fuse_perform_write(file, mapping, from, pos);
Anand Avati4273b792012-02-17 12:46:25 -05001207 if (written >= 0)
1208 iocb->ki_pos = pos + written;
1209 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001210out:
1211 current->backing_dev_info = NULL;
1212 mutex_unlock(&inode->i_mutex);
1213
1214 return written ? written : err;
1215}
1216
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001217static inline void fuse_page_descs_length_init(struct fuse_req *req,
1218 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001219{
1220 int i;
1221
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001222 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001223 req->page_descs[i].length = PAGE_SIZE -
1224 req->page_descs[i].offset;
1225}
1226
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001227static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1228{
1229 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1230}
1231
1232static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1233 size_t max_size)
1234{
1235 return min(iov_iter_single_seg_count(ii), max_size);
1236}
1237
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001238static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001239 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001240{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001241 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001242
Miklos Szeredif4975c62009-04-02 14:25:34 +02001243 /* Special case for kernel I/O: can copy directly into the buffer */
Al Viro62a80672014-04-04 23:12:29 -04001244 if (ii->type & ITER_KVEC) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001245 unsigned long user_addr = fuse_get_user_addr(ii);
1246 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1247
Miklos Szeredif4975c62009-04-02 14:25:34 +02001248 if (write)
1249 req->in.args[1].value = (void *) user_addr;
1250 else
1251 req->out.args[0].value = (void *) user_addr;
1252
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001253 iov_iter_advance(ii, frag_size);
1254 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001255 return 0;
1256 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001257
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001258 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001259 unsigned npages;
Al Virof67da302014-03-19 01:16:16 -04001260 size_t start;
Al Viroc9c37e22014-03-16 16:08:30 -04001261 ssize_t ret = iov_iter_get_pages(ii,
1262 &req->pages[req->num_pages],
Miklos Szeredi2c809292014-09-24 17:09:11 +02001263 *nbytesp - nbytes,
Al Viroc7f38882014-06-18 20:34:33 -04001264 req->max_pages - req->num_pages,
1265 &start);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001266 if (ret < 0)
1267 return ret;
1268
Al Viroc9c37e22014-03-16 16:08:30 -04001269 iov_iter_advance(ii, ret);
1270 nbytes += ret;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001271
Al Viroc9c37e22014-03-16 16:08:30 -04001272 ret += start;
1273 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1274
1275 req->page_descs[req->num_pages].offset = start;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001276 fuse_page_descs_length_init(req, req->num_pages, npages);
1277
1278 req->num_pages += npages;
1279 req->page_descs[req->num_pages - 1].length -=
Al Viroc9c37e22014-03-16 16:08:30 -04001280 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001282
1283 if (write)
1284 req->in.argpages = 1;
1285 else
1286 req->out.argpages = 1;
1287
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001289
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001290 return 0;
1291}
1292
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001293static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1294{
Al Virof67da302014-03-19 01:16:16 -04001295 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001296}
1297
Al Virod22a9432014-03-16 15:50:47 -04001298ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1299 loff_t *ppos, int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001300{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001301 int write = flags & FUSE_DIO_WRITE;
1302 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001303 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001304 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001305 struct fuse_file *ff = file->private_data;
1306 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001307 size_t nmax = write ? fc->max_write : fc->max_read;
1308 loff_t pos = *ppos;
Al Virod22a9432014-03-16 15:50:47 -04001309 size_t count = iov_iter_count(iter);
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001310 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1311 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001312 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001313 struct fuse_req *req;
1314
Brian Fosterde82b922013-05-14 11:25:39 -04001315 if (io->async)
Al Virod22a9432014-03-16 15:50:47 -04001316 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001317 else
Al Virod22a9432014-03-16 15:50:47 -04001318 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001319 if (IS_ERR(req))
1320 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001321
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001322 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1323 if (!write)
1324 mutex_lock(&inode->i_mutex);
1325 fuse_sync_writes(inode);
1326 if (!write)
1327 mutex_unlock(&inode->i_mutex);
1328 }
1329
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001330 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001331 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001332 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001333 size_t nbytes = min(count, nmax);
Al Virod22a9432014-03-16 15:50:47 -04001334 int err = fuse_get_user_pages(req, iter, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 if (err) {
1336 res = err;
1337 break;
1338 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001339
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001340 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001341 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001342 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001343 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001344
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001345 if (!io->async)
1346 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001347 if (req->out.h.error) {
1348 if (!res)
1349 res = req->out.h.error;
1350 break;
1351 } else if (nres > nbytes) {
1352 res = -EIO;
1353 break;
1354 }
1355 count -= nres;
1356 res += nres;
1357 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001358 if (nres != nbytes)
1359 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001360 if (count) {
1361 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001362 if (io->async)
1363 req = fuse_get_req_for_background(fc,
Al Virod22a9432014-03-16 15:50:47 -04001364 fuse_iter_npages(iter));
Brian Fosterde82b922013-05-14 11:25:39 -04001365 else
Al Virod22a9432014-03-16 15:50:47 -04001366 req = fuse_get_req(fc, fuse_iter_npages(iter));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001367 if (IS_ERR(req))
1368 break;
1369 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001371 if (!IS_ERR(req))
1372 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001373 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001374 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375
1376 return res;
1377}
Tejun Heo08cbf542009-04-14 10:54:53 +09001378EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001379
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001380static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001381 struct iov_iter *iter,
1382 loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001383{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001384 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001385 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001386 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001387
1388 if (is_bad_inode(inode))
1389 return -EIO;
1390
Al Virod22a9432014-03-16 15:50:47 -04001391 res = fuse_direct_io(io, iter, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001392
1393 fuse_invalidate_attr(inode);
1394
1395 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396}
1397
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001398static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1399 size_t count, loff_t *ppos)
1400{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001401 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001402 struct iovec iov = { .iov_base = buf, .iov_len = count };
Al Virod22a9432014-03-16 15:50:47 -04001403 struct iov_iter ii;
1404 iov_iter_init(&ii, READ, &iov, 1, count);
1405 return __fuse_direct_read(&io, &ii, ppos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001406}
1407
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001408static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
Al Virod22a9432014-03-16 15:50:47 -04001409 struct iov_iter *iter,
1410 loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001411{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001412 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001413 struct inode *inode = file_inode(file);
Al Virod22a9432014-03-16 15:50:47 -04001414 size_t count = iov_iter_count(iter);
Anand Avati4273b792012-02-17 12:46:25 -05001415 ssize_t res;
1416
Al Virod22a9432014-03-16 15:50:47 -04001417
Anand Avati4273b792012-02-17 12:46:25 -05001418 res = generic_write_checks(file, ppos, &count, 0);
Al Virod22a9432014-03-16 15:50:47 -04001419 if (!res) {
Al Viro0c949332014-03-22 06:51:37 -04001420 iov_iter_truncate(iter, count);
Al Virod22a9432014-03-16 15:50:47 -04001421 res = fuse_direct_io(io, iter, ppos, FUSE_DIO_WRITE);
1422 }
Anand Avati4273b792012-02-17 12:46:25 -05001423
1424 fuse_invalidate_attr(inode);
1425
1426 return res;
1427}
1428
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1430 size_t count, loff_t *ppos)
1431{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001432 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001433 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001435 struct fuse_io_priv io = { .async = 0, .file = file };
Al Virod22a9432014-03-16 15:50:47 -04001436 struct iov_iter ii;
1437 iov_iter_init(&ii, WRITE, &iov, 1, count);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001438
1439 if (is_bad_inode(inode))
1440 return -EIO;
1441
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001442 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001443 mutex_lock(&inode->i_mutex);
Al Virod22a9432014-03-16 15:50:47 -04001444 res = __fuse_direct_write(&io, &ii, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001445 if (res > 0)
1446 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001447 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001448
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001449 return res;
1450}
1451
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001452static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001453{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001454 int i;
1455
1456 for (i = 0; i < req->num_pages; i++)
1457 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001458
1459 if (req->ff)
1460 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001461}
1462
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001463static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001464{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001465 struct inode *inode = req->inode;
1466 struct fuse_inode *fi = get_fuse_inode(inode);
1467 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001468 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001469
1470 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001471 for (i = 0; i < req->num_pages; i++) {
1472 dec_bdi_stat(bdi, BDI_WRITEBACK);
1473 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1474 bdi_writeout_inc(bdi);
1475 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001476 wake_up(&fi->page_waitq);
1477}
1478
1479/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001480static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1481 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001482__releases(fc->lock)
1483__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001484{
1485 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001487 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001488
1489 if (!fc->connected)
1490 goto out_free;
1491
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001492 if (inarg->offset + data_size <= size) {
1493 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001494 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001495 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001496 } else {
1497 /* Got truncated off completely */
1498 goto out_free;
1499 }
1500
1501 req->in.args[1].size = inarg->size;
1502 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001503 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 return;
1505
1506 out_free:
1507 fuse_writepage_finish(fc, req);
1508 spin_unlock(&fc->lock);
1509 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001510 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001511 spin_lock(&fc->lock);
1512}
1513
1514/*
1515 * If fi->writectr is positive (no truncate or fsync going on) send
1516 * all queued writepage requests.
1517 *
1518 * Called with fc->lock
1519 */
1520void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001521__releases(fc->lock)
1522__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001523{
1524 struct fuse_conn *fc = get_fuse_conn(inode);
1525 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001526 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001527 struct fuse_req *req;
1528
1529 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1530 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1531 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001532 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001533 }
1534}
1535
1536static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1537{
1538 struct inode *inode = req->inode;
1539 struct fuse_inode *fi = get_fuse_inode(inode);
1540
1541 mapping_set_error(inode->i_mapping, req->out.h.error);
1542 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001543 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001544 struct fuse_conn *fc = get_fuse_conn(inode);
1545 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001546 struct fuse_req *next = req->misc.write.next;
1547 req->misc.write.next = next->misc.write.next;
1548 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001549 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001550 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001551
1552 /*
1553 * Skip fuse_flush_writepages() to make it easy to crop requests
1554 * based on primary request size.
1555 *
1556 * 1st case (trivial): there are no concurrent activities using
1557 * fuse_set/release_nowrite. Then we're on safe side because
1558 * fuse_flush_writepages() would call fuse_send_writepage()
1559 * anyway.
1560 *
1561 * 2nd case: someone called fuse_set_nowrite and it is waiting
1562 * now for completion of all in-flight requests. This happens
1563 * rarely and no more than once per page, so this should be
1564 * okay.
1565 *
1566 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1567 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1568 * that fuse_set_nowrite returned implies that all in-flight
1569 * requests were completed along with all of their secondary
1570 * requests. Further primary requests are blocked by negative
1571 * writectr. Hence there cannot be any in-flight requests and
1572 * no invocations of fuse_writepage_end() while we're in
1573 * fuse_set_nowrite..fuse_release_nowrite section.
1574 */
1575 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001576 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001577 fi->writectr--;
1578 fuse_writepage_finish(fc, req);
1579 spin_unlock(&fc->lock);
1580 fuse_writepage_free(fc, req);
1581}
1582
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001583static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1584 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001585{
Miklos Szeredi72523422013-10-01 16:44:52 +02001586 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001587
1588 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001589 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001590 ff = list_entry(fi->write_files.next, struct fuse_file,
1591 write_entry);
1592 fuse_file_get(ff);
1593 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001594 spin_unlock(&fc->lock);
1595
1596 return ff;
1597}
1598
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001599static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1600 struct fuse_inode *fi)
1601{
1602 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1603 WARN_ON(!ff);
1604 return ff;
1605}
1606
1607int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1608{
1609 struct fuse_conn *fc = get_fuse_conn(inode);
1610 struct fuse_inode *fi = get_fuse_inode(inode);
1611 struct fuse_file *ff;
1612 int err;
1613
1614 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001615 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001616 if (ff)
1617 fuse_file_put(ff, 0);
1618
1619 return err;
1620}
1621
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001622static int fuse_writepage_locked(struct page *page)
1623{
1624 struct address_space *mapping = page->mapping;
1625 struct inode *inode = mapping->host;
1626 struct fuse_conn *fc = get_fuse_conn(inode);
1627 struct fuse_inode *fi = get_fuse_inode(inode);
1628 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001629 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001630 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631
1632 set_page_writeback(page);
1633
Maxim Patlasov4250c062012-10-26 19:48:07 +04001634 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001635 if (!req)
1636 goto err;
1637
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001638 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001639 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1640 if (!tmp_page)
1641 goto err_free;
1642
Miklos Szeredi72523422013-10-01 16:44:52 +02001643 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001644 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001645 if (!req->ff)
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001646 goto err_nofile;
Miklos Szeredi72523422013-10-01 16:44:52 +02001647
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001648 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001649
1650 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001651 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001652 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001653 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001654 req->num_pages = 1;
1655 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001656 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001657 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658 req->end = fuse_writepage_end;
1659 req->inode = inode;
1660
1661 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1662 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663
1664 spin_lock(&fc->lock);
1665 list_add(&req->writepages_entry, &fi->writepages);
1666 list_add_tail(&req->list, &fi->queued_writes);
1667 fuse_flush_writepages(inode);
1668 spin_unlock(&fc->lock);
1669
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001670 end_page_writeback(page);
1671
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001672 return 0;
1673
Maxim Patlasov27f1b362014-07-10 15:32:43 +04001674err_nofile:
1675 __free_page(tmp_page);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001676err_free:
1677 fuse_request_free(req);
1678err:
1679 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001680 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001681}
1682
1683static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1684{
1685 int err;
1686
Miklos Szerediff17be02013-10-01 16:44:53 +02001687 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1688 /*
1689 * ->writepages() should be called for sync() and friends. We
1690 * should only get here on direct reclaim and then we are
1691 * allowed to skip a page which is already in flight
1692 */
1693 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1694
1695 redirty_page_for_writepage(wbc, page);
1696 return 0;
1697 }
1698
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699 err = fuse_writepage_locked(page);
1700 unlock_page(page);
1701
1702 return err;
1703}
1704
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001705struct fuse_fill_wb_data {
1706 struct fuse_req *req;
1707 struct fuse_file *ff;
1708 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001709 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001710};
1711
1712static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1713{
1714 struct fuse_req *req = data->req;
1715 struct inode *inode = data->inode;
1716 struct fuse_conn *fc = get_fuse_conn(inode);
1717 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001718 int num_pages = req->num_pages;
1719 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001720
1721 req->ff = fuse_file_get(data->ff);
1722 spin_lock(&fc->lock);
1723 list_add_tail(&req->list, &fi->queued_writes);
1724 fuse_flush_writepages(inode);
1725 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001726
1727 for (i = 0; i < num_pages; i++)
1728 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001729}
1730
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001731static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1732 struct page *page)
1733{
1734 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1735 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1736 struct fuse_req *tmp;
1737 struct fuse_req *old_req;
1738 bool found = false;
1739 pgoff_t curr_index;
1740
1741 BUG_ON(new_req->num_pages != 0);
1742
1743 spin_lock(&fc->lock);
1744 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001745 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1746 BUG_ON(old_req->inode != new_req->inode);
1747 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1748 if (curr_index <= page->index &&
1749 page->index < curr_index + old_req->num_pages) {
1750 found = true;
1751 break;
1752 }
1753 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001754 if (!found) {
1755 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001756 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001757 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001758
Maxim Patlasovf6011082013-10-02 15:01:07 +04001759 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001760 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1761 BUG_ON(tmp->inode != new_req->inode);
1762 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1763 if (tmp->num_pages == 1 &&
1764 curr_index == page->index) {
1765 old_req = tmp;
1766 }
1767 }
1768
1769 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1770 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001771 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1772
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001773 copy_highpage(old_req->pages[0], page);
1774 spin_unlock(&fc->lock);
1775
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001776 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001777 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001778 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001779 fuse_writepage_free(fc, new_req);
1780 fuse_request_free(new_req);
1781 goto out;
1782 } else {
1783 new_req->misc.write.next = old_req->misc.write.next;
1784 old_req->misc.write.next = new_req;
1785 }
1786out_unlock:
1787 spin_unlock(&fc->lock);
1788out:
1789 return found;
1790}
1791
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001792static int fuse_writepages_fill(struct page *page,
1793 struct writeback_control *wbc, void *_data)
1794{
1795 struct fuse_fill_wb_data *data = _data;
1796 struct fuse_req *req = data->req;
1797 struct inode *inode = data->inode;
1798 struct fuse_conn *fc = get_fuse_conn(inode);
1799 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001800 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001801 int err;
1802
1803 if (!data->ff) {
1804 err = -EIO;
1805 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1806 if (!data->ff)
1807 goto out_unlock;
1808 }
1809
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001810 /*
1811 * Being under writeback is unlikely but possible. For example direct
1812 * read to an mmaped fuse file will set the page dirty twice; once when
1813 * the pages are faulted with get_user_pages(), and then after the read
1814 * completed.
1815 */
1816 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001817
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001818 if (req && req->num_pages &&
1819 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1820 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1821 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1822 fuse_writepages_send(data);
1823 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001824 }
1825 err = -ENOMEM;
1826 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1827 if (!tmp_page)
1828 goto out_unlock;
1829
1830 /*
1831 * The page must not be redirtied until the writeout is completed
1832 * (i.e. userspace has sent a reply to the write request). Otherwise
1833 * there could be more than one temporary page instance for each real
1834 * page.
1835 *
1836 * This is ensured by holding the page lock in page_mkwrite() while
1837 * checking fuse_page_is_writeback(). We already hold the page lock
1838 * since clear_page_dirty_for_io() and keep it held until we add the
1839 * request to the fi->writepages list and increment req->num_pages.
1840 * After this fuse_page_is_writeback() will indicate that the page is
1841 * under writeback, so we can release the page lock.
1842 */
1843 if (data->req == NULL) {
1844 struct fuse_inode *fi = get_fuse_inode(inode);
1845
1846 err = -ENOMEM;
1847 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1848 if (!req) {
1849 __free_page(tmp_page);
1850 goto out_unlock;
1851 }
1852
1853 fuse_write_fill(req, data->ff, page_offset(page), 0);
1854 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001855 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001856 req->in.argpages = 1;
1857 req->background = 1;
1858 req->num_pages = 0;
1859 req->end = fuse_writepage_end;
1860 req->inode = inode;
1861
1862 spin_lock(&fc->lock);
1863 list_add(&req->writepages_entry, &fi->writepages);
1864 spin_unlock(&fc->lock);
1865
1866 data->req = req;
1867 }
1868 set_page_writeback(page);
1869
1870 copy_highpage(tmp_page, page);
1871 req->pages[req->num_pages] = tmp_page;
1872 req->page_descs[req->num_pages].offset = 0;
1873 req->page_descs[req->num_pages].length = PAGE_SIZE;
1874
1875 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1876 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001877
1878 err = 0;
1879 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1880 end_page_writeback(page);
1881 data->req = NULL;
1882 goto out_unlock;
1883 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001884 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001885
1886 /*
1887 * Protected by fc->lock against concurrent access by
1888 * fuse_page_is_writeback().
1889 */
1890 spin_lock(&fc->lock);
1891 req->num_pages++;
1892 spin_unlock(&fc->lock);
1893
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894out_unlock:
1895 unlock_page(page);
1896
1897 return err;
1898}
1899
1900static int fuse_writepages(struct address_space *mapping,
1901 struct writeback_control *wbc)
1902{
1903 struct inode *inode = mapping->host;
1904 struct fuse_fill_wb_data data;
1905 int err;
1906
1907 err = -EIO;
1908 if (is_bad_inode(inode))
1909 goto out;
1910
1911 data.inode = inode;
1912 data.req = NULL;
1913 data.ff = NULL;
1914
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001915 err = -ENOMEM;
Fabian Frederickf2b34552014-06-23 18:35:15 +02001916 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1917 sizeof(struct page *),
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001918 GFP_NOFS);
1919 if (!data.orig_pages)
1920 goto out;
1921
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001922 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1923 if (data.req) {
1924 /* Ignore errors if we can write at least one page */
1925 BUG_ON(!data.req->num_pages);
1926 fuse_writepages_send(&data);
1927 err = 0;
1928 }
1929 if (data.ff)
1930 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001931
1932 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001933out:
1934 return err;
1935}
1936
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001937/*
1938 * It's worthy to make sure that space is reserved on disk for the write,
1939 * but how to implement it without killing performance need more thinking.
1940 */
1941static int fuse_write_begin(struct file *file, struct address_space *mapping,
1942 loff_t pos, unsigned len, unsigned flags,
1943 struct page **pagep, void **fsdata)
1944{
1945 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Al Viroa4555892014-10-21 20:11:25 -04001946 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001947 struct page *page;
1948 loff_t fsize;
1949 int err = -ENOMEM;
1950
1951 WARN_ON(!fc->writeback_cache);
1952
1953 page = grab_cache_page_write_begin(mapping, index, flags);
1954 if (!page)
1955 goto error;
1956
1957 fuse_wait_on_page_writeback(mapping->host, page->index);
1958
1959 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1960 goto success;
1961 /*
1962 * Check if the start this page comes after the end of file, in which
1963 * case the readpage can be optimized away.
1964 */
1965 fsize = i_size_read(mapping->host);
1966 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1967 size_t off = pos & ~PAGE_CACHE_MASK;
1968 if (off)
1969 zero_user_segment(page, 0, off);
1970 goto success;
1971 }
1972 err = fuse_do_readpage(file, page);
1973 if (err)
1974 goto cleanup;
1975success:
1976 *pagep = page;
1977 return 0;
1978
1979cleanup:
1980 unlock_page(page);
1981 page_cache_release(page);
1982error:
1983 return err;
1984}
1985
1986static int fuse_write_end(struct file *file, struct address_space *mapping,
1987 loff_t pos, unsigned len, unsigned copied,
1988 struct page *page, void *fsdata)
1989{
1990 struct inode *inode = page->mapping->host;
1991
1992 if (!PageUptodate(page)) {
1993 /* Zero any unwritten bytes at the end of the page */
1994 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
1995 if (endoff)
1996 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
1997 SetPageUptodate(page);
1998 }
1999
2000 fuse_write_update_size(inode, pos + copied);
2001 set_page_dirty(page);
2002 unlock_page(page);
2003 page_cache_release(page);
2004
2005 return copied;
2006}
2007
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002008static int fuse_launder_page(struct page *page)
2009{
2010 int err = 0;
2011 if (clear_page_dirty_for_io(page)) {
2012 struct inode *inode = page->mapping->host;
2013 err = fuse_writepage_locked(page);
2014 if (!err)
2015 fuse_wait_on_page_writeback(inode, page->index);
2016 }
2017 return err;
2018}
2019
2020/*
2021 * Write back dirty pages now, because there may not be any suitable
2022 * open files later
2023 */
2024static void fuse_vma_close(struct vm_area_struct *vma)
2025{
2026 filemap_write_and_wait(vma->vm_file->f_mapping);
2027}
2028
2029/*
2030 * Wait for writeback against this page to complete before allowing it
2031 * to be marked dirty again, and hence written back again, possibly
2032 * before the previous writepage completed.
2033 *
2034 * Block here, instead of in ->writepage(), so that the userspace fs
2035 * can only block processes actually operating on the filesystem.
2036 *
2037 * Otherwise unprivileged userspace fs would be able to block
2038 * unrelated:
2039 *
2040 * - page migration
2041 * - sync(2)
2042 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2043 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002044static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002045{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002046 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002047 struct inode *inode = file_inode(vma->vm_file);
2048
2049 file_update_time(vma->vm_file);
2050 lock_page(page);
2051 if (page->mapping != inode->i_mapping) {
2052 unlock_page(page);
2053 return VM_FAULT_NOPAGE;
2054 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002055
2056 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002057 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002058}
2059
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002060static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002061 .close = fuse_vma_close,
2062 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002063 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002064 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002065 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002066};
2067
2068static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2069{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002070 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2071 fuse_link_write_file(file);
2072
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002073 file_accessed(file);
2074 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002075 return 0;
2076}
2077
Miklos Szeredifc280c92009-04-02 14:25:35 +02002078static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2079{
2080 /* Can't provide the coherency needed for MAP_SHARED */
2081 if (vma->vm_flags & VM_MAYSHARE)
2082 return -ENODEV;
2083
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002084 invalidate_inode_pages2(file->f_mapping);
2085
Miklos Szeredifc280c92009-04-02 14:25:35 +02002086 return generic_file_mmap(file, vma);
2087}
2088
Miklos Szeredi71421252006-06-25 05:48:52 -07002089static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2090 struct file_lock *fl)
2091{
2092 switch (ffl->type) {
2093 case F_UNLCK:
2094 break;
2095
2096 case F_RDLCK:
2097 case F_WRLCK:
2098 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2099 ffl->end < ffl->start)
2100 return -EIO;
2101
2102 fl->fl_start = ffl->start;
2103 fl->fl_end = ffl->end;
2104 fl->fl_pid = ffl->pid;
2105 break;
2106
2107 default:
2108 return -EIO;
2109 }
2110 fl->fl_type = ffl->type;
2111 return 0;
2112}
2113
Miklos Szeredi70781872014-12-12 09:49:05 +01002114static void fuse_lk_fill(struct fuse_args *args, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002115 const struct file_lock *fl, int opcode, pid_t pid,
Miklos Szeredi70781872014-12-12 09:49:05 +01002116 int flock, struct fuse_lk_in *inarg)
Miklos Szeredi71421252006-06-25 05:48:52 -07002117{
Al Viro6131ffa2013-02-27 16:59:05 -05002118 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002119 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002120 struct fuse_file *ff = file->private_data;
Miklos Szeredi71421252006-06-25 05:48:52 -07002121
Miklos Szeredi70781872014-12-12 09:49:05 +01002122 memset(inarg, 0, sizeof(*inarg));
2123 inarg->fh = ff->fh;
2124 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2125 inarg->lk.start = fl->fl_start;
2126 inarg->lk.end = fl->fl_end;
2127 inarg->lk.type = fl->fl_type;
2128 inarg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002129 if (flock)
Miklos Szeredi70781872014-12-12 09:49:05 +01002130 inarg->lk_flags |= FUSE_LK_FLOCK;
2131 args->in.h.opcode = opcode;
2132 args->in.h.nodeid = get_node_id(inode);
2133 args->in.numargs = 1;
2134 args->in.args[0].size = sizeof(*inarg);
2135 args->in.args[0].value = inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002136}
2137
2138static int fuse_getlk(struct file *file, struct file_lock *fl)
2139{
Al Viro6131ffa2013-02-27 16:59:05 -05002140 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002141 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002142 FUSE_ARGS(args);
2143 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002144 struct fuse_lk_out outarg;
2145 int err;
2146
Miklos Szeredi70781872014-12-12 09:49:05 +01002147 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2148 args.out.numargs = 1;
2149 args.out.args[0].size = sizeof(outarg);
2150 args.out.args[0].value = &outarg;
2151 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002152 if (!err)
2153 err = convert_fuse_file_lock(&outarg.lk, fl);
2154
2155 return err;
2156}
2157
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002158static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002159{
Al Viro6131ffa2013-02-27 16:59:05 -05002160 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002161 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002162 FUSE_ARGS(args);
2163 struct fuse_lk_in inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -07002164 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2165 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2166 int err;
2167
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002168 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002169 /* NLM needs asynchronous locks, which we don't support yet */
2170 return -ENOLCK;
2171 }
2172
Miklos Szeredi71421252006-06-25 05:48:52 -07002173 /* Unlock on close is handled by the flush method */
2174 if (fl->fl_flags & FL_CLOSE)
2175 return 0;
2176
Miklos Szeredi70781872014-12-12 09:49:05 +01002177 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2178 err = fuse_simple_request(fc, &args);
Miklos Szeredi71421252006-06-25 05:48:52 -07002179
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002180 /* locking is restartable */
2181 if (err == -EINTR)
2182 err = -ERESTARTSYS;
Miklos Szeredi70781872014-12-12 09:49:05 +01002183
Miklos Szeredi71421252006-06-25 05:48:52 -07002184 return err;
2185}
2186
2187static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2188{
Al Viro6131ffa2013-02-27 16:59:05 -05002189 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002190 struct fuse_conn *fc = get_fuse_conn(inode);
2191 int err;
2192
Miklos Szeredi48e90762008-07-25 01:49:02 -07002193 if (cmd == F_CANCELLK) {
2194 err = 0;
2195 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002196 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002197 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002198 err = 0;
2199 } else
2200 err = fuse_getlk(file, fl);
2201 } else {
2202 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002203 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002204 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002205 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002206 }
2207 return err;
2208}
2209
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002210static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2211{
Al Viro6131ffa2013-02-27 16:59:05 -05002212 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002213 struct fuse_conn *fc = get_fuse_conn(inode);
2214 int err;
2215
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002216 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002217 err = flock_lock_file_wait(file, fl);
2218 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002219 struct fuse_file *ff = file->private_data;
2220
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002221 /* emulate flock with POSIX locks */
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002222 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002223 err = fuse_setlk(file, fl, 1);
2224 }
2225
2226 return err;
2227}
2228
Miklos Szeredib2d22722006-12-06 20:35:51 -08002229static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2230{
2231 struct inode *inode = mapping->host;
2232 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01002233 FUSE_ARGS(args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002234 struct fuse_bmap_in inarg;
2235 struct fuse_bmap_out outarg;
2236 int err;
2237
2238 if (!inode->i_sb->s_bdev || fc->no_bmap)
2239 return 0;
2240
Miklos Szeredib2d22722006-12-06 20:35:51 -08002241 memset(&inarg, 0, sizeof(inarg));
2242 inarg.block = block;
2243 inarg.blocksize = inode->i_sb->s_blocksize;
Miklos Szeredi70781872014-12-12 09:49:05 +01002244 args.in.h.opcode = FUSE_BMAP;
2245 args.in.h.nodeid = get_node_id(inode);
2246 args.in.numargs = 1;
2247 args.in.args[0].size = sizeof(inarg);
2248 args.in.args[0].value = &inarg;
2249 args.out.numargs = 1;
2250 args.out.args[0].size = sizeof(outarg);
2251 args.out.args[0].value = &outarg;
2252 err = fuse_simple_request(fc, &args);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002253 if (err == -ENOSYS)
2254 fc->no_bmap = 1;
2255
2256 return err ? 0 : outarg.block;
2257}
2258
Andrew Morton965c8e52012-12-17 15:59:39 -08002259static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002260{
2261 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002262 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002263
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002264 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002265 if (whence == SEEK_CUR || whence == SEEK_SET)
2266 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002267
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002268 mutex_lock(&inode->i_mutex);
2269 retval = fuse_update_attributes(inode, NULL, file, NULL);
2270 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002271 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002272 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002273
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002274 return retval;
2275}
2276
Tejun Heo59efec72008-11-26 12:03:55 +01002277static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2278 unsigned int nr_segs, size_t bytes, bool to_user)
2279{
2280 struct iov_iter ii;
2281 int page_idx = 0;
2282
2283 if (!bytes)
2284 return 0;
2285
Al Viro71d8e532014-03-05 19:28:09 -05002286 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
Tejun Heo59efec72008-11-26 12:03:55 +01002287
2288 while (iov_iter_count(&ii)) {
2289 struct page *page = pages[page_idx++];
2290 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002291 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002292
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002293 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002294
2295 while (todo) {
2296 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2297 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2298 size_t copy = min(todo, iov_len);
2299 size_t left;
2300
2301 if (!to_user)
2302 left = copy_from_user(kaddr, uaddr, copy);
2303 else
2304 left = copy_to_user(uaddr, kaddr, copy);
2305
2306 if (unlikely(left))
2307 return -EFAULT;
2308
2309 iov_iter_advance(&ii, copy);
2310 todo -= copy;
2311 kaddr += copy;
2312 }
2313
Jens Axboe0bd87182009-11-03 11:40:44 +01002314 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002315 }
2316
2317 return 0;
2318}
2319
2320/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002321 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2322 * ABI was defined to be 'struct iovec' which is different on 32bit
2323 * and 64bit. Fortunately we can determine which structure the server
2324 * used from the size of the reply.
2325 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002326static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2327 size_t transferred, unsigned count,
2328 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002329{
2330#ifdef CONFIG_COMPAT
2331 if (count * sizeof(struct compat_iovec) == transferred) {
2332 struct compat_iovec *ciov = src;
2333 unsigned i;
2334
2335 /*
2336 * With this interface a 32bit server cannot support
2337 * non-compat (i.e. ones coming from 64bit apps) ioctl
2338 * requests
2339 */
2340 if (!is_compat)
2341 return -EINVAL;
2342
2343 for (i = 0; i < count; i++) {
2344 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2345 dst[i].iov_len = ciov[i].iov_len;
2346 }
2347 return 0;
2348 }
2349#endif
2350
2351 if (count * sizeof(struct iovec) != transferred)
2352 return -EIO;
2353
2354 memcpy(dst, src, transferred);
2355 return 0;
2356}
2357
Miklos Szeredi75727772010-11-30 16:39:27 +01002358/* Make sure iov_length() won't overflow */
2359static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2360{
2361 size_t n;
2362 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2363
Zach Brownfb6ccff2012-07-24 12:10:11 -07002364 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002365 if (iov->iov_len > (size_t) max)
2366 return -ENOMEM;
2367 max -= iov->iov_len;
2368 }
2369 return 0;
2370}
2371
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002372static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2373 void *src, size_t transferred, unsigned count,
2374 bool is_compat)
2375{
2376 unsigned i;
2377 struct fuse_ioctl_iovec *fiov = src;
2378
2379 if (fc->minor < 16) {
2380 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2381 count, is_compat);
2382 }
2383
2384 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2385 return -EIO;
2386
2387 for (i = 0; i < count; i++) {
2388 /* Did the server supply an inappropriate value? */
2389 if (fiov[i].base != (unsigned long) fiov[i].base ||
2390 fiov[i].len != (unsigned long) fiov[i].len)
2391 return -EIO;
2392
2393 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2394 dst[i].iov_len = (size_t) fiov[i].len;
2395
2396#ifdef CONFIG_COMPAT
2397 if (is_compat &&
2398 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2399 (compat_size_t) dst[i].iov_len != fiov[i].len))
2400 return -EIO;
2401#endif
2402 }
2403
2404 return 0;
2405}
2406
2407
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002408/*
Tejun Heo59efec72008-11-26 12:03:55 +01002409 * For ioctls, there is no generic way to determine how much memory
2410 * needs to be read and/or written. Furthermore, ioctls are allowed
2411 * to dereference the passed pointer, so the parameter requires deep
2412 * copying but FUSE has no idea whatsoever about what to copy in or
2413 * out.
2414 *
2415 * This is solved by allowing FUSE server to retry ioctl with
2416 * necessary in/out iovecs. Let's assume the ioctl implementation
2417 * needs to read in the following structure.
2418 *
2419 * struct a {
2420 * char *buf;
2421 * size_t buflen;
2422 * }
2423 *
2424 * On the first callout to FUSE server, inarg->in_size and
2425 * inarg->out_size will be NULL; then, the server completes the ioctl
2426 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2427 * the actual iov array to
2428 *
2429 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2430 *
2431 * which tells FUSE to copy in the requested area and retry the ioctl.
2432 * On the second round, the server has access to the structure and
2433 * from that it can tell what to look for next, so on the invocation,
2434 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2435 *
2436 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2437 * { .iov_base = a.buf, .iov_len = a.buflen } }
2438 *
2439 * FUSE will copy both struct a and the pointed buffer from the
2440 * process doing the ioctl and retry ioctl with both struct a and the
2441 * buffer.
2442 *
2443 * This time, FUSE server has everything it needs and completes ioctl
2444 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2445 *
2446 * Copying data out works the same way.
2447 *
2448 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2449 * automatically initializes in and out iovs by decoding @cmd with
2450 * _IOC_* macros and the server is not allowed to request RETRY. This
2451 * limits ioctl data transfers to well-formed ioctls and is the forced
2452 * behavior for all FUSE servers.
2453 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002454long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2455 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002456{
Tejun Heo59efec72008-11-26 12:03:55 +01002457 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002458 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002459 struct fuse_ioctl_in inarg = {
2460 .fh = ff->fh,
2461 .cmd = cmd,
2462 .arg = arg,
2463 .flags = flags
2464 };
2465 struct fuse_ioctl_out outarg;
2466 struct fuse_req *req = NULL;
2467 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002468 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002469 struct iovec *in_iov = NULL, *out_iov = NULL;
2470 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2471 size_t in_size, out_size, transferred;
2472 int err;
2473
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002474#if BITS_PER_LONG == 32
2475 inarg.flags |= FUSE_IOCTL_32BIT;
2476#else
2477 if (flags & FUSE_IOCTL_COMPAT)
2478 inarg.flags |= FUSE_IOCTL_32BIT;
2479#endif
2480
Tejun Heo59efec72008-11-26 12:03:55 +01002481 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002482 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002483
Tejun Heo59efec72008-11-26 12:03:55 +01002484 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002485 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002486 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002487 if (!pages || !iov_page)
2488 goto out;
2489
2490 /*
2491 * If restricted, initialize IO parameters as encoded in @cmd.
2492 * RETRY from server is not allowed.
2493 */
2494 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002495 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002496
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002497 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002498 iov->iov_len = _IOC_SIZE(cmd);
2499
2500 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2501 in_iov = iov;
2502 in_iovs = 1;
2503 }
2504
2505 if (_IOC_DIR(cmd) & _IOC_READ) {
2506 out_iov = iov;
2507 out_iovs = 1;
2508 }
2509 }
2510
2511 retry:
2512 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2513 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2514
2515 /*
2516 * Out data can be used either for actual out data or iovs,
2517 * make sure there always is at least one page.
2518 */
2519 out_size = max_t(size_t, out_size, PAGE_SIZE);
2520 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2521
2522 /* make sure there are enough buffer pages and init request with them */
2523 err = -ENOMEM;
2524 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2525 goto out;
2526 while (num_pages < max_pages) {
2527 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2528 if (!pages[num_pages])
2529 goto out;
2530 num_pages++;
2531 }
2532
Maxim Patlasov54b96672012-10-26 19:49:13 +04002533 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002534 if (IS_ERR(req)) {
2535 err = PTR_ERR(req);
2536 req = NULL;
2537 goto out;
2538 }
2539 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2540 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002541 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002542
2543 /* okay, let's send it to the client */
2544 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002545 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002546 req->in.numargs = 1;
2547 req->in.args[0].size = sizeof(inarg);
2548 req->in.args[0].value = &inarg;
2549 if (in_size) {
2550 req->in.numargs++;
2551 req->in.args[1].size = in_size;
2552 req->in.argpages = 1;
2553
2554 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2555 false);
2556 if (err)
2557 goto out;
2558 }
2559
2560 req->out.numargs = 2;
2561 req->out.args[0].size = sizeof(outarg);
2562 req->out.args[0].value = &outarg;
2563 req->out.args[1].size = out_size;
2564 req->out.argpages = 1;
2565 req->out.argvar = 1;
2566
Tejun Heob93f8582008-11-26 12:03:55 +01002567 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002568 err = req->out.h.error;
2569 transferred = req->out.args[1].size;
2570 fuse_put_request(fc, req);
2571 req = NULL;
2572 if (err)
2573 goto out;
2574
2575 /* did it ask for retry? */
2576 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002577 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002578
2579 /* no retry if in restricted mode */
2580 err = -EIO;
2581 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2582 goto out;
2583
2584 in_iovs = outarg.in_iovs;
2585 out_iovs = outarg.out_iovs;
2586
2587 /*
2588 * Make sure things are in boundary, separate checks
2589 * are to protect against overflow.
2590 */
2591 err = -ENOMEM;
2592 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2593 out_iovs > FUSE_IOCTL_MAX_IOV ||
2594 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2595 goto out;
2596
Cong Wang2408f6e2011-11-25 23:14:30 +08002597 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002598 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002599 transferred, in_iovs + out_iovs,
2600 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002601 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002602 if (err)
2603 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002604
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002605 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002606 out_iov = in_iov + in_iovs;
2607
Miklos Szeredi75727772010-11-30 16:39:27 +01002608 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2609 if (err)
2610 goto out;
2611
2612 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2613 if (err)
2614 goto out;
2615
Tejun Heo59efec72008-11-26 12:03:55 +01002616 goto retry;
2617 }
2618
2619 err = -EIO;
2620 if (transferred > inarg.out_size)
2621 goto out;
2622
2623 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2624 out:
2625 if (req)
2626 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002627 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002628 while (num_pages)
2629 __free_page(pages[--num_pages]);
2630 kfree(pages);
2631
2632 return err ? err : outarg.result;
2633}
Tejun Heo08cbf542009-04-14 10:54:53 +09002634EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002635
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002636long fuse_ioctl_common(struct file *file, unsigned int cmd,
2637 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002638{
Al Viro6131ffa2013-02-27 16:59:05 -05002639 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002640 struct fuse_conn *fc = get_fuse_conn(inode);
2641
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002642 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002643 return -EACCES;
2644
2645 if (is_bad_inode(inode))
2646 return -EIO;
2647
2648 return fuse_do_ioctl(file, cmd, arg, flags);
2649}
2650
Tejun Heo59efec72008-11-26 12:03:55 +01002651static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2652 unsigned long arg)
2653{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002654 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002655}
2656
2657static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2658 unsigned long arg)
2659{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002660 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002661}
2662
Tejun Heo95668a62008-11-26 12:03:55 +01002663/*
2664 * All files which have been polled are linked to RB tree
2665 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2666 * find the matching one.
2667 */
2668static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2669 struct rb_node **parent_out)
2670{
2671 struct rb_node **link = &fc->polled_files.rb_node;
2672 struct rb_node *last = NULL;
2673
2674 while (*link) {
2675 struct fuse_file *ff;
2676
2677 last = *link;
2678 ff = rb_entry(last, struct fuse_file, polled_node);
2679
2680 if (kh < ff->kh)
2681 link = &last->rb_left;
2682 else if (kh > ff->kh)
2683 link = &last->rb_right;
2684 else
2685 return link;
2686 }
2687
2688 if (parent_out)
2689 *parent_out = last;
2690 return link;
2691}
2692
2693/*
2694 * The file is about to be polled. Make sure it's on the polled_files
2695 * RB tree. Note that files once added to the polled_files tree are
2696 * not removed before the file is released. This is because a file
2697 * polled once is likely to be polled again.
2698 */
2699static void fuse_register_polled_file(struct fuse_conn *fc,
2700 struct fuse_file *ff)
2701{
2702 spin_lock(&fc->lock);
2703 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002704 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002705
2706 link = fuse_find_polled_node(fc, ff->kh, &parent);
2707 BUG_ON(*link);
2708 rb_link_node(&ff->polled_node, parent, link);
2709 rb_insert_color(&ff->polled_node, &fc->polled_files);
2710 }
2711 spin_unlock(&fc->lock);
2712}
2713
Tejun Heo08cbf542009-04-14 10:54:53 +09002714unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002715{
Tejun Heo95668a62008-11-26 12:03:55 +01002716 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002717 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002718 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2719 struct fuse_poll_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +01002720 FUSE_ARGS(args);
Tejun Heo95668a62008-11-26 12:03:55 +01002721 int err;
2722
2723 if (fc->no_poll)
2724 return DEFAULT_POLLMASK;
2725
2726 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002727 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002728
2729 /*
2730 * Ask for notification iff there's someone waiting for it.
2731 * The client may ignore the flag and always notify.
2732 */
2733 if (waitqueue_active(&ff->poll_wait)) {
2734 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2735 fuse_register_polled_file(fc, ff);
2736 }
2737
Miklos Szeredi70781872014-12-12 09:49:05 +01002738 args.in.h.opcode = FUSE_POLL;
2739 args.in.h.nodeid = ff->nodeid;
2740 args.in.numargs = 1;
2741 args.in.args[0].size = sizeof(inarg);
2742 args.in.args[0].value = &inarg;
2743 args.out.numargs = 1;
2744 args.out.args[0].size = sizeof(outarg);
2745 args.out.args[0].value = &outarg;
2746 err = fuse_simple_request(fc, &args);
Tejun Heo95668a62008-11-26 12:03:55 +01002747
2748 if (!err)
2749 return outarg.revents;
2750 if (err == -ENOSYS) {
2751 fc->no_poll = 1;
2752 return DEFAULT_POLLMASK;
2753 }
2754 return POLLERR;
2755}
Tejun Heo08cbf542009-04-14 10:54:53 +09002756EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002757
2758/*
2759 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2760 * wakes up the poll waiters.
2761 */
2762int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2763 struct fuse_notify_poll_wakeup_out *outarg)
2764{
2765 u64 kh = outarg->kh;
2766 struct rb_node **link;
2767
2768 spin_lock(&fc->lock);
2769
2770 link = fuse_find_polled_node(fc, kh, NULL);
2771 if (*link) {
2772 struct fuse_file *ff;
2773
2774 ff = rb_entry(*link, struct fuse_file, polled_node);
2775 wake_up_interruptible_sync(&ff->poll_wait);
2776 }
2777
2778 spin_unlock(&fc->lock);
2779 return 0;
2780}
2781
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002782static void fuse_do_truncate(struct file *file)
2783{
2784 struct inode *inode = file->f_mapping->host;
2785 struct iattr attr;
2786
2787 attr.ia_valid = ATTR_SIZE;
2788 attr.ia_size = i_size_read(inode);
2789
2790 attr.ia_file = file;
2791 attr.ia_valid |= ATTR_FILE;
2792
2793 fuse_do_setattr(inode, &attr, file);
2794}
2795
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002796static inline loff_t fuse_round_up(loff_t off)
2797{
2798 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2799}
2800
Anand Avati4273b792012-02-17 12:46:25 -05002801static ssize_t
Al Virod8d3d942014-03-04 21:27:34 -05002802fuse_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
2803 loff_t offset)
Anand Avati4273b792012-02-17 12:46:25 -05002804{
2805 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002806 struct file *file = iocb->ki_filp;
2807 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002808 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002809 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002810 struct inode *inode;
2811 loff_t i_size;
Al Viroa6cbcd42014-03-04 22:38:00 -05002812 size_t count = iov_iter_count(iter);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002813 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002814
Anand Avati4273b792012-02-17 12:46:25 -05002815 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002816 inode = file->f_mapping->host;
2817 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002818
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002819 if ((rw == READ) && (offset > i_size))
2820 return 0;
2821
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002822 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002823 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002824 if (offset >= i_size)
2825 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002826 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Al Viro0c949332014-03-22 06:51:37 -04002827 iov_iter_truncate(iter, count);
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002828 }
2829
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002830 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002831 if (!io)
2832 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002833 spin_lock_init(&io->lock);
2834 io->reqs = 1;
2835 io->bytes = -1;
2836 io->size = 0;
2837 io->offset = offset;
2838 io->write = (rw == WRITE);
2839 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002840 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002841 /*
2842 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002843 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002844 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002845 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002846 io->iocb = iocb;
2847
2848 /*
2849 * We cannot asynchronously extend the size of a file. We have no method
2850 * to wait on real async I/O requests, so we must submit this request
2851 * synchronously.
2852 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002853 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002854 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002855
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002856 if (rw == WRITE)
Al Virod22a9432014-03-16 15:50:47 -04002857 ret = __fuse_direct_write(io, iter, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002858 else
Al Virod22a9432014-03-16 15:50:47 -04002859 ret = __fuse_direct_read(io, iter, &pos);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002860
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002861 if (io->async) {
2862 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2863
2864 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002865 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002866 return -EIOCBQUEUED;
2867
2868 ret = wait_on_sync_kiocb(iocb);
2869 } else {
2870 kfree(io);
2871 }
2872
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002873 if (rw == WRITE) {
2874 if (ret > 0)
2875 fuse_write_update_size(inode, pos);
2876 else if (ret < 0 && offset + count > i_size)
2877 fuse_do_truncate(file);
2878 }
Anand Avati4273b792012-02-17 12:46:25 -05002879
2880 return ret;
2881}
2882
Miklos Szeredicdadb112012-11-10 16:55:56 +01002883static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2884 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002885{
2886 struct fuse_file *ff = file->private_data;
Miklos Szeredi1c682712014-12-12 10:04:51 +01002887 struct inode *inode = file_inode(file);
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002888 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002889 struct fuse_conn *fc = ff->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +01002890 FUSE_ARGS(args);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002891 struct fuse_fallocate_in inarg = {
2892 .fh = ff->fh,
2893 .offset = offset,
2894 .length = length,
2895 .mode = mode
2896 };
2897 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002898 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2899 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002900
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002901 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2902 return -EOPNOTSUPP;
2903
Miklos Szeredi519c6042012-04-26 10:56:36 +02002904 if (fc->no_fallocate)
2905 return -EOPNOTSUPP;
2906
Maxim Patlasov14c14412013-06-13 12:16:39 +04002907 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002908 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002909 if (mode & FALLOC_FL_PUNCH_HOLE) {
2910 loff_t endbyte = offset + length - 1;
2911 err = filemap_write_and_wait_range(inode->i_mapping,
2912 offset, endbyte);
2913 if (err)
2914 goto out;
2915
2916 fuse_sync_writes(inode);
2917 }
Brian Foster3634a632013-05-17 09:30:32 -04002918 }
2919
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002920 if (!(mode & FALLOC_FL_KEEP_SIZE))
2921 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2922
Miklos Szeredi70781872014-12-12 09:49:05 +01002923 args.in.h.opcode = FUSE_FALLOCATE;
2924 args.in.h.nodeid = ff->nodeid;
2925 args.in.numargs = 1;
2926 args.in.args[0].size = sizeof(inarg);
2927 args.in.args[0].value = &inarg;
2928 err = fuse_simple_request(fc, &args);
Miklos Szeredi519c6042012-04-26 10:56:36 +02002929 if (err == -ENOSYS) {
2930 fc->no_fallocate = 1;
2931 err = -EOPNOTSUPP;
2932 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002933 if (err)
2934 goto out;
2935
2936 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002937 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2938 bool changed = fuse_write_update_size(inode, offset + length);
2939
Miklos Szeredi93d22692014-04-28 14:19:22 +02002940 if (changed && fc->writeback_cache)
2941 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002942 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002943
2944 if (mode & FALLOC_FL_PUNCH_HOLE)
2945 truncate_pagecache_range(inode, offset, offset + length - 1);
2946
2947 fuse_invalidate_attr(inode);
2948
Brian Foster3634a632013-05-17 09:30:32 -04002949out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002950 if (!(mode & FALLOC_FL_KEEP_SIZE))
2951 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2952
Maxim Patlasovbde52782013-09-13 19:19:54 +04002953 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002954 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002955
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002956 return err;
2957}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002958
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002959static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002960 .llseek = fuse_file_llseek,
Al Viro37c20f12014-04-02 14:47:09 -04002961 .read = new_sync_read,
2962 .read_iter = fuse_file_read_iter,
Al Viro84c3d552014-04-03 14:33:23 -04002963 .write = new_sync_write,
2964 .write_iter = fuse_file_write_iter,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002965 .mmap = fuse_file_mmap,
2966 .open = fuse_open,
2967 .flush = fuse_flush,
2968 .release = fuse_release,
2969 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002970 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002971 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002972 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002973 .unlocked_ioctl = fuse_file_ioctl,
2974 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002975 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002976 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002977};
2978
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002979static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002980 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002981 .read = fuse_direct_read,
2982 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002983 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002984 .open = fuse_open,
2985 .flush = fuse_flush,
2986 .release = fuse_release,
2987 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002988 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002989 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002990 .unlocked_ioctl = fuse_file_ioctl,
2991 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002992 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002993 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002994 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002995};
2996
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002997static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002998 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002999 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003000 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003001 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003002 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003003 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003004 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003005 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003006 .write_begin = fuse_write_begin,
3007 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003008};
3009
3010void fuse_init_file_inode(struct inode *inode)
3011{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003012 inode->i_fop = &fuse_file_operations;
3013 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003014}