blob: e68d8c3f063a4668ec19d85565da2463c09697b7 [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 Szeredifd72faa2005-11-07 00:59:51 -080027 struct fuse_req *req;
28 int err;
29
Maxim Patlasovb111c8c2012-10-26 19:48:30 +040030 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -070031 if (IS_ERR(req))
32 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080033
34 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070035 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
36 if (!fc->atomic_o_trunc)
37 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020038 req->in.h.opcode = opcode;
39 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080040 req->in.numargs = 1;
41 req->in.args[0].size = sizeof(inarg);
42 req->in.args[0].value = &inarg;
43 req->out.numargs = 1;
44 req->out.args[0].size = sizeof(*outargp);
45 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010046 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047 err = req->out.h.error;
48 fuse_put_request(fc, req);
49
50 return err;
51}
52
Tejun Heoacf99432008-11-26 12:03:55 +010053struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054{
55 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090056
Miklos Szeredifd72faa2005-11-07 00:59:51 -080057 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090058 if (unlikely(!ff))
59 return NULL;
60
Miklos Szeredida5e4712009-04-28 16:56:36 +020061 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090063 if (unlikely(!ff->reserved_req)) {
64 kfree(ff);
65 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080066 }
Tejun Heo6b2db282009-04-14 10:54:49 +090067
68 INIT_LIST_HEAD(&ff->write_entry);
69 atomic_set(&ff->count, 0);
70 RB_CLEAR_NODE(&ff->polled_node);
71 init_waitqueue_head(&ff->poll_wait);
72
73 spin_lock(&fc->lock);
74 ff->kh = ++fc->khctr;
75 spin_unlock(&fc->lock);
76
Miklos Szeredifd72faa2005-11-07 00:59:51 -080077 return ff;
78}
79
80void fuse_file_free(struct fuse_file *ff)
81{
Miklos Szeredi33649c92006-06-25 05:48:52 -070082 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080083 kfree(ff);
84}
85
Miklos Szeredic7b71432009-04-28 16:56:37 +020086struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070087{
88 atomic_inc(&ff->count);
89 return ff;
90}
91
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010092static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070093{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010094 struct fuse_req *req;
95 struct fuse_conn *fc;
96 struct path path;
97
98 req = container_of(work, struct fuse_req, misc.release.work);
99 path = req->misc.release.path;
100 fc = get_fuse_conn(path.dentry->d_inode);
101
102 fuse_put_request(fc, req);
103 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700104}
105
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
107{
108 if (fc->destroy_req) {
109 /*
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
115 * thread.
116 */
117 atomic_inc(&req->count);
118 INIT_WORK(&req->misc.release.work, fuse_release_async);
119 schedule_work(&req->misc.release.work);
120 } else {
121 path_put(&req->misc.release.path);
122 }
123}
124
125static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700126{
127 if (atomic_dec_and_test(&ff->count)) {
128 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200129
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 if (ff->fc->no_open) {
131 /*
132 * Drop the release request when client does not
133 * implement 'open'
134 */
135 req->background = 0;
136 path_put(&req->misc.release.path);
137 fuse_put_request(ff->fc, req);
138 } else if (sync) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400139 req->background = 0;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100140 fuse_request_send(ff->fc, req);
141 path_put(&req->misc.release.path);
142 fuse_put_request(ff->fc, req);
143 } else {
144 req->end = fuse_release_end;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145 req->background = 1;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100146 fuse_request_send_background(ff->fc, req);
147 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700148 kfree(ff);
149 }
150}
151
Tejun Heo08cbf542009-04-14 10:54:53 +0900152int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
153 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200155 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200156 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
157
158 ff = fuse_file_alloc(fc);
159 if (!ff)
160 return -ENOMEM;
161
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 ff->fh = 0;
163 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
164 if (!fc->no_open || isdir) {
165 struct fuse_open_out outarg;
166 int err;
167
168 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
169 if (!err) {
170 ff->fh = outarg.fh;
171 ff->open_flags = outarg.open_flags;
172
173 } else if (err != -ENOSYS || isdir) {
174 fuse_file_free(ff);
175 return err;
176 } else {
177 fc->no_open = 1;
178 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200179 }
180
181 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100182 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200183
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200184 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200185 file->private_data = fuse_file_get(ff);
186
187 return 0;
188}
Tejun Heo08cbf542009-04-14 10:54:53 +0900189EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200190
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400191static void fuse_link_write_file(struct file *file)
192{
193 struct inode *inode = file_inode(file);
194 struct fuse_conn *fc = get_fuse_conn(inode);
195 struct fuse_inode *fi = get_fuse_inode(inode);
196 struct fuse_file *ff = file->private_data;
197 /*
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
200 */
201 spin_lock(&fc->lock);
202 if (list_empty(&ff->write_entry))
203 list_add(&ff->write_entry, &fi->write_files);
204 spin_unlock(&fc->lock);
205}
206
Miklos Szeredic7b71432009-04-28 16:56:37 +0200207void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800208{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200209 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800210 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200211
212 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800213 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200214 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700215 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200216 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200217 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800218 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
219 struct fuse_inode *fi = get_fuse_inode(inode);
220
221 spin_lock(&fc->lock);
222 fi->attr_version = ++fc->attr_version;
223 i_size_write(inode, 0);
224 spin_unlock(&fc->lock);
225 fuse_invalidate_attr(inode);
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200226 if (fc->writeback_cache)
227 file_update_time(file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800228 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400229 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
230 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800231}
232
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200233int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800234{
Tejun Heoacf99432008-11-26 12:03:55 +0100235 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700236 int err;
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200237 bool lock_inode = (file->f_flags & O_TRUNC) &&
238 fc->atomic_o_trunc &&
239 fc->writeback_cache;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700240
241 err = generic_file_open(inode, file);
242 if (err)
243 return err;
244
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200245 if (lock_inode)
246 mutex_lock(&inode->i_mutex);
247
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200248 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700249
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200250 if (!err)
251 fuse_finish_open(inode, file);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200252
Maxim Patlasov75caeec2014-04-28 14:19:22 +0200253 if (lock_inode)
254 mutex_unlock(&inode->i_mutex);
255
256 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700257}
258
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200259static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800260{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200261 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700262 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800263 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700264
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200265 spin_lock(&fc->lock);
266 list_del(&ff->write_entry);
267 if (!RB_EMPTY_NODE(&ff->polled_node))
268 rb_erase(&ff->polled_node, &fc->polled_files);
269 spin_unlock(&fc->lock);
270
Bryan Green357ccf22011-03-01 16:43:52 -0800271 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200272
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700273 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800274 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700275 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200276 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277 req->in.numargs = 1;
278 req->in.args[0].size = sizeof(struct fuse_release_in);
279 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800280}
281
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200282void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800283{
Tejun Heo6b2db282009-04-14 10:54:49 +0900284 struct fuse_file *ff;
285 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700286
Tejun Heo6b2db282009-04-14 10:54:49 +0900287 ff = file->private_data;
288 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200289 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700290
Tejun Heo6b2db282009-04-14 10:54:49 +0900291 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200292 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100293
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200294 if (ff->flock) {
295 struct fuse_release_in *inarg = &req->misc.release.in;
296 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
297 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
298 (fl_owner_t) file);
299 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900300 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200301 path_get(&file->f_path);
302 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700303
Tejun Heo6b2db282009-04-14 10:54:49 +0900304 /*
305 * Normally this will send the RELEASE request, however if
306 * some asynchronous READ or WRITE requests are outstanding,
307 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100308 *
309 * Make the release synchronous if this is a fuseblk mount,
310 * synchronous RELEASE is allowed (and desirable) in this case
311 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900312 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100313 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700314}
315
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700316static int fuse_open(struct inode *inode, struct file *file)
317{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200318 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700319}
320
321static int fuse_release(struct inode *inode, struct file *file)
322{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400323 struct fuse_conn *fc = get_fuse_conn(inode);
324
325 /* see fuse_vma_close() for !writeback_cache case */
326 if (fc->writeback_cache)
327 filemap_write_and_wait(file->f_mapping);
328
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400329 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
330 fuse_flush_mtime(file, true);
331
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200332 fuse_release_common(file, FUSE_RELEASE);
333
334 /* return value is ignored by VFS */
335 return 0;
336}
337
338void fuse_sync_release(struct fuse_file *ff, int flags)
339{
340 WARN_ON(atomic_read(&ff->count) > 1);
341 fuse_prepare_release(ff, flags, FUSE_RELEASE);
342 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400343 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200344 fuse_request_send(ff->fc, ff->reserved_req);
345 fuse_put_request(ff->fc, ff->reserved_req);
346 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700347}
Tejun Heo08cbf542009-04-14 10:54:53 +0900348EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700349
Miklos Szeredi71421252006-06-25 05:48:52 -0700350/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700351 * Scramble the ID space with XTEA, so that the value of the files_struct
352 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700353 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700354u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700355{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700356 u32 *k = fc->scramble_key;
357 u64 v = (unsigned long) id;
358 u32 v0 = v;
359 u32 v1 = v >> 32;
360 u32 sum = 0;
361 int i;
362
363 for (i = 0; i < 32; i++) {
364 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
365 sum += 0x9E3779B9;
366 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
367 }
368
369 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700370}
371
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700372/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400373 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700374 *
375 * This is currently done by walking the list of writepage requests
376 * for the inode, which can be pretty inefficient.
377 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400378static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
379 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700380{
381 struct fuse_conn *fc = get_fuse_conn(inode);
382 struct fuse_inode *fi = get_fuse_inode(inode);
383 struct fuse_req *req;
384 bool found = false;
385
386 spin_lock(&fc->lock);
387 list_for_each_entry(req, &fi->writepages, writepages_entry) {
388 pgoff_t curr_index;
389
390 BUG_ON(req->inode != inode);
391 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400392 if (idx_from < curr_index + req->num_pages &&
393 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700394 found = true;
395 break;
396 }
397 }
398 spin_unlock(&fc->lock);
399
400 return found;
401}
402
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400403static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
404{
405 return fuse_range_is_writeback(inode, index, index);
406}
407
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700408/*
409 * Wait for page writeback to be completed.
410 *
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
413 */
414static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
415{
416 struct fuse_inode *fi = get_fuse_inode(inode);
417
418 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
419 return 0;
420}
421
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400422/*
423 * Wait for all pending writepages on the inode to finish.
424 *
425 * This is currently done by blocking further writes with FUSE_NOWRITE
426 * and waiting for all sent writes to complete.
427 *
428 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
429 * could conflict with truncation.
430 */
431static void fuse_sync_writes(struct inode *inode)
432{
433 fuse_set_nowrite(inode);
434 fuse_release_nowrite(inode);
435}
436
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700437static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700438{
Al Viro6131ffa2013-02-27 16:59:05 -0500439 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 struct fuse_conn *fc = get_fuse_conn(inode);
441 struct fuse_file *ff = file->private_data;
442 struct fuse_req *req;
443 struct fuse_flush_in inarg;
444 int err;
445
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800446 if (is_bad_inode(inode))
447 return -EIO;
448
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 if (fc->no_flush)
450 return 0;
451
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400452 err = filemap_write_and_wait(file->f_mapping);
453 if (err)
454 return err;
455
456 mutex_lock(&inode->i_mutex);
457 fuse_sync_writes(inode);
458 mutex_unlock(&inode->i_mutex);
459
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400460 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461 memset(&inarg, 0, sizeof(inarg));
462 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700463 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464 req->in.h.opcode = FUSE_FLUSH;
465 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700466 req->in.numargs = 1;
467 req->in.args[0].size = sizeof(inarg);
468 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700469 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100470 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471 err = req->out.h.error;
472 fuse_put_request(fc, req);
473 if (err == -ENOSYS) {
474 fc->no_flush = 1;
475 err = 0;
476 }
477 return err;
478}
479
Josef Bacik02c24a82011-07-16 20:44:56 -0400480int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
481 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700482{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200483 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700484 struct fuse_conn *fc = get_fuse_conn(inode);
485 struct fuse_file *ff = file->private_data;
486 struct fuse_req *req;
487 struct fuse_fsync_in inarg;
488 int err;
489
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800490 if (is_bad_inode(inode))
491 return -EIO;
492
Josef Bacik02c24a82011-07-16 20:44:56 -0400493 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
494 if (err)
495 return err;
496
Miklos Szeredi82547982005-09-09 13:10:38 -0700497 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498 return 0;
499
Josef Bacik02c24a82011-07-16 20:44:56 -0400500 mutex_lock(&inode->i_mutex);
501
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700502 /*
503 * Start writeback against all dirty pages of the inode, then
504 * wait for all outstanding writes, before sending the FSYNC
505 * request.
506 */
507 err = write_inode_now(inode, 0);
508 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400509 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700510
511 fuse_sync_writes(inode);
512
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400513 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
Miklos Szerediaeb4eb62014-04-28 14:19:21 +0200514 err = fuse_flush_mtime(file, false);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400515 if (err)
516 goto out;
517 }
518
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400519 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400520 if (IS_ERR(req)) {
521 err = PTR_ERR(req);
522 goto out;
523 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700524
525 memset(&inarg, 0, sizeof(inarg));
526 inarg.fh = ff->fh;
527 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700528 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700530 req->in.numargs = 1;
531 req->in.args[0].size = sizeof(inarg);
532 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100533 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700534 err = req->out.h.error;
535 fuse_put_request(fc, req);
536 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700537 if (isdir)
538 fc->no_fsyncdir = 1;
539 else
540 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541 err = 0;
542 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400543out:
544 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700545 return err;
546}
547
Josef Bacik02c24a82011-07-16 20:44:56 -0400548static int fuse_fsync(struct file *file, loff_t start, loff_t end,
549 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700550{
Josef Bacik02c24a82011-07-16 20:44:56 -0400551 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700552}
553
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200554void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
555 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700556{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700557 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800558 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700559
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800560 inarg->fh = ff->fh;
561 inarg->offset = pos;
562 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800563 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800564 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200565 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700566 req->in.numargs = 1;
567 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800568 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700569 req->out.argvar = 1;
570 req->out.numargs = 1;
571 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700572}
573
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400574static void fuse_release_user_pages(struct fuse_req *req, int write)
575{
576 unsigned i;
577
578 for (i = 0; i < req->num_pages; i++) {
579 struct page *page = req->pages[i];
580 if (write)
581 set_page_dirty_lock(page);
582 put_page(page);
583 }
584}
585
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400586/**
587 * In case of short read, the caller sets 'pos' to the position of
588 * actual end of fuse request in IO request. Otherwise, if bytes_requested
589 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
590 *
591 * An example:
592 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
593 * both submitted asynchronously. The first of them was ACKed by userspace as
594 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
595 * second request was ACKed as short, e.g. only 1K was read, resulting in
596 * pos == 33K.
597 *
598 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
599 * will be equal to the length of the longest contiguous fragment of
600 * transferred data starting from the beginning of IO request.
601 */
602static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
603{
604 int left;
605
606 spin_lock(&io->lock);
607 if (err)
608 io->err = io->err ? : err;
609 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
610 io->bytes = pos;
611
612 left = --io->reqs;
613 spin_unlock(&io->lock);
614
615 if (!left) {
616 long res;
617
618 if (io->err)
619 res = io->err;
620 else if (io->bytes >= 0 && io->write)
621 res = -EIO;
622 else {
623 res = io->bytes < 0 ? io->size : io->bytes;
624
625 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400626 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400627 struct fuse_conn *fc = get_fuse_conn(inode);
628 struct fuse_inode *fi = get_fuse_inode(inode);
629
630 spin_lock(&fc->lock);
631 fi->attr_version = ++fc->attr_version;
632 spin_unlock(&fc->lock);
633 }
634 }
635
636 aio_complete(io->iocb, res, 0);
637 kfree(io);
638 }
639}
640
641static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
642{
643 struct fuse_io_priv *io = req->io;
644 ssize_t pos = -1;
645
646 fuse_release_user_pages(req, !io->write);
647
648 if (io->write) {
649 if (req->misc.write.in.size != req->misc.write.out.size)
650 pos = req->misc.write.in.offset - io->offset +
651 req->misc.write.out.size;
652 } else {
653 if (req->misc.read.in.size != req->out.args[0].size)
654 pos = req->misc.read.in.offset - io->offset +
655 req->out.args[0].size;
656 }
657
658 fuse_aio_complete(io, req->out.h.error, pos);
659}
660
661static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
662 size_t num_bytes, struct fuse_io_priv *io)
663{
664 spin_lock(&io->lock);
665 io->size += num_bytes;
666 io->reqs++;
667 spin_unlock(&io->lock);
668
669 req->io = io;
670 req->end = fuse_aio_complete_req;
671
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400672 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400673 fuse_request_send_background(fc, req);
674
675 return num_bytes;
676}
677
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400678static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200679 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700680{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400681 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200682 struct fuse_file *ff = file->private_data;
683 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700684
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200685 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700686 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700687 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700688
689 inarg->read_flags |= FUSE_READ_LOCKOWNER;
690 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
691 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400692
693 if (io->async)
694 return fuse_async_req_send(fc, req, count, io);
695
Tejun Heob93f8582008-11-26 12:03:55 +0100696 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800697 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700698}
699
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700700static void fuse_read_update_size(struct inode *inode, loff_t size,
701 u64 attr_ver)
702{
703 struct fuse_conn *fc = get_fuse_conn(inode);
704 struct fuse_inode *fi = get_fuse_inode(inode);
705
706 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400707 if (attr_ver == fi->attr_version && size < inode->i_size &&
708 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700709 fi->attr_version = ++fc->attr_version;
710 i_size_write(inode, size);
711 }
712 spin_unlock(&fc->lock);
713}
714
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400715static void fuse_short_read(struct fuse_req *req, struct inode *inode,
716 u64 attr_ver)
717{
718 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400719 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400720
Pavel Emelyanov83732002013-10-10 17:10:46 +0400721 if (fc->writeback_cache) {
722 /*
723 * A hole in a file. Some data after the hole are in page cache,
724 * but have not reached the client fs yet. So, the hole is not
725 * present there.
726 */
727 int i;
728 int start_idx = num_read >> PAGE_CACHE_SHIFT;
729 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
730
731 for (i = start_idx; i < req->num_pages; i++) {
732 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
733 off = 0;
734 }
735 } else {
736 loff_t pos = page_offset(req->pages[0]) + num_read;
737 fuse_read_update_size(inode, pos, attr_ver);
738 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400739}
740
Maxim Patlasov482fce52013-10-10 17:11:25 +0400741static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700742{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400743 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700744 struct inode *inode = page->mapping->host;
745 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800746 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747 size_t num_read;
748 loff_t pos = page_offset(page);
749 size_t count = PAGE_CACHE_SIZE;
750 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800751 int err;
752
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700753 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300754 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700755 * page-cache page, so make sure we read a properly synced
756 * page.
757 */
758 fuse_wait_on_page_writeback(inode, page->index);
759
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400760 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700761 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400762 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700763
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700764 attr_ver = fuse_get_attr_version(fc);
765
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700766 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200767 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700768 req->num_pages = 1;
769 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400770 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400771 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700772 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700773
774 if (!err) {
775 /*
776 * Short read means EOF. If file size is larger, truncate it
777 */
778 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400779 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700780
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700781 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700782 }
783
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400784 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400785
786 return err;
787}
788
789static int fuse_readpage(struct file *file, struct page *page)
790{
791 struct inode *inode = page->mapping->host;
792 int err;
793
794 err = -EIO;
795 if (is_bad_inode(inode))
796 goto out;
797
798 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800799 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700800 out:
801 unlock_page(page);
802 return err;
803}
804
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800805static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700806{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800807 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700808 size_t count = req->misc.read.in.size;
809 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200810 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800811
Miklos Szeredice534fb2010-05-25 15:06:07 +0200812 for (i = 0; mapping == NULL && i < req->num_pages; i++)
813 mapping = req->pages[i]->mapping;
814
815 if (mapping) {
816 struct inode *inode = mapping->host;
817
818 /*
819 * Short read means EOF. If file size is larger, truncate it
820 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400821 if (!req->out.h.error && num_read < count)
822 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200823
Andrew Gallagher451418f2013-11-05 03:55:43 -0800824 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700825 }
826
Miklos Szeredidb50b962005-09-09 13:10:33 -0700827 for (i = 0; i < req->num_pages; i++) {
828 struct page *page = req->pages[i];
829 if (!req->out.h.error)
830 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831 else
832 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700833 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200834 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700835 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700836 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100837 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800838}
839
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200840static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800841{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200842 struct fuse_file *ff = file->private_data;
843 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800844 loff_t pos = page_offset(req->pages[0]);
845 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200846
847 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800848 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200849 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200850 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700851 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800852 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700853 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800854 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100855 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800856 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100857 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800858 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100859 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800860 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861}
862
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700863struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700864 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800865 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700866 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400867 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868};
869
870static int fuse_readpages_fill(void *_data, struct page *page)
871{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700872 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700873 struct fuse_req *req = data->req;
874 struct inode *inode = data->inode;
875 struct fuse_conn *fc = get_fuse_conn(inode);
876
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700877 fuse_wait_on_page_writeback(inode, page->index);
878
Miklos Szeredidb50b962005-09-09 13:10:33 -0700879 if (req->num_pages &&
880 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
881 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
882 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400883 int nr_alloc = min_t(unsigned, data->nr_pages,
884 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200885 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400886 if (fc->async_read)
887 req = fuse_get_req_for_background(fc, nr_alloc);
888 else
889 req = fuse_get_req(fc, nr_alloc);
890
891 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700892 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700893 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700894 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700895 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700896 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400897
898 if (WARN_ON(req->num_pages >= req->max_pages)) {
899 fuse_put_request(fc, req);
900 return -EIO;
901 }
902
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200903 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700904 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400905 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100906 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400907 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700908 return 0;
909}
910
911static int fuse_readpages(struct file *file, struct address_space *mapping,
912 struct list_head *pages, unsigned nr_pages)
913{
914 struct inode *inode = mapping->host;
915 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700916 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700917 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400918 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800919
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700920 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800921 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800922 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800923
Miklos Szeredia6643092007-11-28 16:22:00 -0800924 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700925 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400926 if (fc->async_read)
927 data.req = fuse_get_req_for_background(fc, nr_alloc);
928 else
929 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400930 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700931 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700932 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800933 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700934
935 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700936 if (!err) {
937 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200938 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700939 else
940 fuse_put_request(fc, data.req);
941 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800942out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700943 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700944}
945
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800946static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
947 unsigned long nr_segs, loff_t pos)
948{
949 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400950 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800951
Brian Fostera8894272012-07-16 15:23:50 -0400952 /*
953 * In auto invalidate mode, always update attributes on read.
954 * Otherwise, only update if we attempt to read past EOF (to ensure
955 * i_size is up to date).
956 */
957 if (fc->auto_inval_data ||
958 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800959 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800960 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
961 if (err)
962 return err;
963 }
964
965 return generic_file_aio_read(iocb, iov, nr_segs, pos);
966}
967
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200968static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200969 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700970{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700971 struct fuse_write_in *inarg = &req->misc.write.in;
972 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700973
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974 inarg->fh = ff->fh;
975 inarg->offset = pos;
976 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700977 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200978 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700979 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200980 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700981 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
982 else
983 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700984 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700985 req->in.args[1].size = count;
986 req->out.numargs = 1;
987 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700988 req->out.args[0].value = outarg;
989}
990
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400991static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200992 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700993{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400994 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200995 struct fuse_file *ff = file->private_data;
996 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200997 struct fuse_write_in *inarg = &req->misc.write.in;
998
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200999 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001000 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -07001001 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -07001002 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1003 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1004 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001005
1006 if (io->async)
1007 return fuse_async_req_send(fc, req, count, io);
1008
Tejun Heob93f8582008-11-26 12:03:55 +01001009 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001010 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001011}
1012
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001014{
1015 struct fuse_conn *fc = get_fuse_conn(inode);
1016 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001017 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001018
1019 spin_lock(&fc->lock);
1020 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001021 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001022 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001023 ret = true;
1024 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001025 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001026
1027 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001028}
1029
Nick Pigginea9b9902008-04-30 00:54:42 -07001030static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1031 struct inode *inode, loff_t pos,
1032 size_t count)
1033{
1034 size_t res;
1035 unsigned offset;
1036 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001037 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001038
1039 for (i = 0; i < req->num_pages; i++)
1040 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1041
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001042 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001043
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001044 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001045 count = res;
1046 for (i = 0; i < req->num_pages; i++) {
1047 struct page *page = req->pages[i];
1048
1049 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1050 SetPageUptodate(page);
1051
1052 if (count > PAGE_CACHE_SIZE - offset)
1053 count -= PAGE_CACHE_SIZE - offset;
1054 else
1055 count = 0;
1056 offset = 0;
1057
1058 unlock_page(page);
1059 page_cache_release(page);
1060 }
1061
1062 return res;
1063}
1064
1065static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1066 struct address_space *mapping,
1067 struct iov_iter *ii, loff_t pos)
1068{
1069 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1070 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1071 size_t count = 0;
1072 int err;
1073
Miklos Szeredif4975c62009-04-02 14:25:34 +02001074 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001075 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001076
1077 do {
1078 size_t tmp;
1079 struct page *page;
1080 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1081 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1082 iov_iter_count(ii));
1083
1084 bytes = min_t(size_t, bytes, fc->max_write - count);
1085
1086 again:
1087 err = -EFAULT;
1088 if (iov_iter_fault_in_readable(ii, bytes))
1089 break;
1090
1091 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001092 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 if (!page)
1094 break;
1095
anfei zhou931e80e2010-02-02 13:44:02 -08001096 if (mapping_writably_mapped(mapping))
1097 flush_dcache_page(page);
1098
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001100 flush_dcache_page(page);
1101
Johannes Weiner478e0842011-07-25 22:35:35 +02001102 mark_page_accessed(page);
1103
Nick Pigginea9b9902008-04-30 00:54:42 -07001104 if (!tmp) {
1105 unlock_page(page);
1106 page_cache_release(page);
1107 bytes = min(bytes, iov_iter_single_seg_count(ii));
1108 goto again;
1109 }
1110
1111 err = 0;
1112 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001113 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001114 req->num_pages++;
1115
1116 iov_iter_advance(ii, tmp);
1117 count += tmp;
1118 pos += tmp;
1119 offset += tmp;
1120 if (offset == PAGE_CACHE_SIZE)
1121 offset = 0;
1122
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001123 if (!fc->big_writes)
1124 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001125 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001126 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001127
1128 return count > 0 ? count : err;
1129}
1130
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001131static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1132{
1133 return min_t(unsigned,
1134 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1135 (pos >> PAGE_CACHE_SHIFT) + 1,
1136 FUSE_MAX_PAGES_PER_REQ);
1137}
1138
Nick Pigginea9b9902008-04-30 00:54:42 -07001139static ssize_t fuse_perform_write(struct file *file,
1140 struct address_space *mapping,
1141 struct iov_iter *ii, loff_t pos)
1142{
1143 struct inode *inode = mapping->host;
1144 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001145 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001146 int err = 0;
1147 ssize_t res = 0;
1148
1149 if (is_bad_inode(inode))
1150 return -EIO;
1151
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001152 if (inode->i_size < pos + iov_iter_count(ii))
1153 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1154
Nick Pigginea9b9902008-04-30 00:54:42 -07001155 do {
1156 struct fuse_req *req;
1157 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001158 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001159
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001160 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001161 if (IS_ERR(req)) {
1162 err = PTR_ERR(req);
1163 break;
1164 }
1165
1166 count = fuse_fill_write_pages(req, mapping, ii, pos);
1167 if (count <= 0) {
1168 err = count;
1169 } else {
1170 size_t num_written;
1171
1172 num_written = fuse_send_write_pages(req, file, inode,
1173 pos, count);
1174 err = req->out.h.error;
1175 if (!err) {
1176 res += num_written;
1177 pos += num_written;
1178
1179 /* break out of the loop on short write */
1180 if (num_written != count)
1181 err = -EIO;
1182 }
1183 }
1184 fuse_put_request(fc, req);
1185 } while (!err && iov_iter_count(ii));
1186
1187 if (res > 0)
1188 fuse_write_update_size(inode, pos);
1189
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001190 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001191 fuse_invalidate_attr(inode);
1192
1193 return res > 0 ? res : err;
1194}
1195
1196static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1197 unsigned long nr_segs, loff_t pos)
1198{
1199 struct file *file = iocb->ki_filp;
1200 struct address_space *mapping = file->f_mapping;
1201 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001202 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001203 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001204 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001205 struct inode *inode = mapping->host;
1206 ssize_t err;
1207 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001208 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001209
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001210 if (get_fuse_conn(inode)->writeback_cache) {
1211 /* Update size (EOF optimization) and mode (SUID clearing) */
1212 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1213 if (err)
1214 return err;
1215
1216 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1217 }
1218
Nick Pigginea9b9902008-04-30 00:54:42 -07001219 WARN_ON(iocb->ki_pos != pos);
1220
Anand Avati4273b792012-02-17 12:46:25 -05001221 ocount = 0;
1222 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001223 if (err)
1224 return err;
1225
Anand Avati4273b792012-02-17 12:46:25 -05001226 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001227 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001228
1229 /* We can write back this queue in page reclaim */
1230 current->backing_dev_info = mapping->backing_dev_info;
1231
1232 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1233 if (err)
1234 goto out;
1235
1236 if (count == 0)
1237 goto out;
1238
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001239 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001240 if (err)
1241 goto out;
1242
Josef Bacikc3b2da32012-03-26 09:59:21 -04001243 err = file_update_time(file);
1244 if (err)
1245 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001246
Anand Avati4273b792012-02-17 12:46:25 -05001247 if (file->f_flags & O_DIRECT) {
Al Viro5cb6c6c2014-02-11 20:58:20 -05001248 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
Anand Avati4273b792012-02-17 12:46:25 -05001249 count, ocount);
1250 if (written < 0 || written == count)
1251 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001252
Anand Avati4273b792012-02-17 12:46:25 -05001253 pos += written;
1254 count -= written;
1255
1256 iov_iter_init(&i, iov, nr_segs, count, written);
1257 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1258 if (written_buffered < 0) {
1259 err = written_buffered;
1260 goto out;
1261 }
1262 endbyte = pos + written_buffered - 1;
1263
1264 err = filemap_write_and_wait_range(file->f_mapping, pos,
1265 endbyte);
1266 if (err)
1267 goto out;
1268
1269 invalidate_mapping_pages(file->f_mapping,
1270 pos >> PAGE_CACHE_SHIFT,
1271 endbyte >> PAGE_CACHE_SHIFT);
1272
1273 written += written_buffered;
1274 iocb->ki_pos = pos + written_buffered;
1275 } else {
1276 iov_iter_init(&i, iov, nr_segs, count, 0);
1277 written = fuse_perform_write(file, mapping, &i, pos);
1278 if (written >= 0)
1279 iocb->ki_pos = pos + written;
1280 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001281out:
1282 current->backing_dev_info = NULL;
1283 mutex_unlock(&inode->i_mutex);
1284
1285 return written ? written : err;
1286}
1287
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288static inline void fuse_page_descs_length_init(struct fuse_req *req,
1289 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001290{
1291 int i;
1292
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001294 req->page_descs[i].length = PAGE_SIZE -
1295 req->page_descs[i].offset;
1296}
1297
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1299{
1300 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1301}
1302
1303static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1304 size_t max_size)
1305{
1306 return min(iov_iter_single_seg_count(ii), max_size);
1307}
1308
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001309static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001310 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001311{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001312 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001313
Miklos Szeredif4975c62009-04-02 14:25:34 +02001314 /* Special case for kernel I/O: can copy directly into the buffer */
1315 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001316 unsigned long user_addr = fuse_get_user_addr(ii);
1317 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1318
Miklos Szeredif4975c62009-04-02 14:25:34 +02001319 if (write)
1320 req->in.args[1].value = (void *) user_addr;
1321 else
1322 req->out.args[0].value = (void *) user_addr;
1323
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001324 iov_iter_advance(ii, frag_size);
1325 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001326 return 0;
1327 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001328
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001329 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001330 unsigned npages;
1331 unsigned long user_addr = fuse_get_user_addr(ii);
1332 unsigned offset = user_addr & ~PAGE_MASK;
1333 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1334 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001336 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001337 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1338
1339 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1340 npages = clamp(npages, 1U, n);
1341
1342 ret = get_user_pages_fast(user_addr, npages, !write,
1343 &req->pages[req->num_pages]);
1344 if (ret < 0)
1345 return ret;
1346
1347 npages = ret;
1348 frag_size = min_t(size_t, frag_size,
1349 (npages << PAGE_SHIFT) - offset);
1350 iov_iter_advance(ii, frag_size);
1351
1352 req->page_descs[req->num_pages].offset = offset;
1353 fuse_page_descs_length_init(req, req->num_pages, npages);
1354
1355 req->num_pages += npages;
1356 req->page_descs[req->num_pages - 1].length -=
1357 (npages << PAGE_SHIFT) - offset - frag_size;
1358
1359 nbytes += frag_size;
1360 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001361
1362 if (write)
1363 req->in.argpages = 1;
1364 else
1365 req->out.argpages = 1;
1366
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001367 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001368
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369 return 0;
1370}
1371
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001372static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1373{
1374 struct iov_iter ii = *ii_p;
1375 int npages = 0;
1376
1377 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1378 unsigned long user_addr = fuse_get_user_addr(&ii);
1379 unsigned offset = user_addr & ~PAGE_MASK;
1380 size_t frag_size = iov_iter_single_seg_count(&ii);
1381
1382 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1383 iov_iter_advance(&ii, frag_size);
1384 }
1385
1386 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1387}
1388
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001389ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001390 unsigned long nr_segs, size_t count, loff_t *ppos,
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001391 int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001393 int write = flags & FUSE_DIO_WRITE;
1394 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001395 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001396 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001397 struct fuse_file *ff = file->private_data;
1398 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001399 size_t nmax = write ? fc->max_write : fc->max_read;
1400 loff_t pos = *ppos;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001401 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1402 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001403 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001404 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001405 struct iov_iter ii;
1406
1407 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001408
Brian Fosterde82b922013-05-14 11:25:39 -04001409 if (io->async)
1410 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1411 else
1412 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001413 if (IS_ERR(req))
1414 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001416 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1417 if (!write)
1418 mutex_lock(&inode->i_mutex);
1419 fuse_sync_writes(inode);
1420 if (!write)
1421 mutex_unlock(&inode->i_mutex);
1422 }
1423
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001425 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001426 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001427 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001428 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429 if (err) {
1430 res = err;
1431 break;
1432 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001433
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001435 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001437 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001438
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001439 if (!io->async)
1440 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001441 if (req->out.h.error) {
1442 if (!res)
1443 res = req->out.h.error;
1444 break;
1445 } else if (nres > nbytes) {
1446 res = -EIO;
1447 break;
1448 }
1449 count -= nres;
1450 res += nres;
1451 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001452 if (nres != nbytes)
1453 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001454 if (count) {
1455 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001456 if (io->async)
1457 req = fuse_get_req_for_background(fc,
1458 fuse_iter_npages(&ii));
1459 else
1460 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001461 if (IS_ERR(req))
1462 break;
1463 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001464 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001465 if (!IS_ERR(req))
1466 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001467 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001468 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001469
1470 return res;
1471}
Tejun Heo08cbf542009-04-14 10:54:53 +09001472EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001473
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001474static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1475 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001476 unsigned long nr_segs, loff_t *ppos,
1477 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001478{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001479 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001480 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001481 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001482
1483 if (is_bad_inode(inode))
1484 return -EIO;
1485
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001486 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001487
1488 fuse_invalidate_attr(inode);
1489
1490 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001491}
1492
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001493static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1494 size_t count, loff_t *ppos)
1495{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001496 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001497 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001498 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001499}
1500
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001501static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1502 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001503 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001504{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001505 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001506 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001507 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001508 ssize_t res;
1509
1510 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001511 if (!res)
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001512 res = fuse_direct_io(io, iov, nr_segs, count, ppos,
1513 FUSE_DIO_WRITE);
Anand Avati4273b792012-02-17 12:46:25 -05001514
1515 fuse_invalidate_attr(inode);
1516
1517 return res;
1518}
1519
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001520static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1521 size_t count, loff_t *ppos)
1522{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001523 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001524 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001525 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001526 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001527
1528 if (is_bad_inode(inode))
1529 return -EIO;
1530
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001531 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001532 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001533 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001534 if (res > 0)
1535 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001536 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001537
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001538 return res;
1539}
1540
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001542{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001543 int i;
1544
1545 for (i = 0; i < req->num_pages; i++)
1546 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001547
1548 if (req->ff)
1549 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001550}
1551
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001552static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001553{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001554 struct inode *inode = req->inode;
1555 struct fuse_inode *fi = get_fuse_inode(inode);
1556 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001557 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001558
1559 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001560 for (i = 0; i < req->num_pages; i++) {
1561 dec_bdi_stat(bdi, BDI_WRITEBACK);
1562 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1563 bdi_writeout_inc(bdi);
1564 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001565 wake_up(&fi->page_waitq);
1566}
1567
1568/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001569static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1570 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001571__releases(fc->lock)
1572__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001573{
1574 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001575 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001576 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001577
1578 if (!fc->connected)
1579 goto out_free;
1580
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001581 if (inarg->offset + data_size <= size) {
1582 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001583 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001584 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001585 } else {
1586 /* Got truncated off completely */
1587 goto out_free;
1588 }
1589
1590 req->in.args[1].size = inarg->size;
1591 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001592 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001593 return;
1594
1595 out_free:
1596 fuse_writepage_finish(fc, req);
1597 spin_unlock(&fc->lock);
1598 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001599 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001600 spin_lock(&fc->lock);
1601}
1602
1603/*
1604 * If fi->writectr is positive (no truncate or fsync going on) send
1605 * all queued writepage requests.
1606 *
1607 * Called with fc->lock
1608 */
1609void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001610__releases(fc->lock)
1611__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001612{
1613 struct fuse_conn *fc = get_fuse_conn(inode);
1614 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001615 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001616 struct fuse_req *req;
1617
1618 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1619 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1620 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001621 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001622 }
1623}
1624
1625static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1626{
1627 struct inode *inode = req->inode;
1628 struct fuse_inode *fi = get_fuse_inode(inode);
1629
1630 mapping_set_error(inode->i_mapping, req->out.h.error);
1631 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001632 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001633 struct fuse_conn *fc = get_fuse_conn(inode);
1634 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001635 struct fuse_req *next = req->misc.write.next;
1636 req->misc.write.next = next->misc.write.next;
1637 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001638 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001639 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001640
1641 /*
1642 * Skip fuse_flush_writepages() to make it easy to crop requests
1643 * based on primary request size.
1644 *
1645 * 1st case (trivial): there are no concurrent activities using
1646 * fuse_set/release_nowrite. Then we're on safe side because
1647 * fuse_flush_writepages() would call fuse_send_writepage()
1648 * anyway.
1649 *
1650 * 2nd case: someone called fuse_set_nowrite and it is waiting
1651 * now for completion of all in-flight requests. This happens
1652 * rarely and no more than once per page, so this should be
1653 * okay.
1654 *
1655 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1656 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1657 * that fuse_set_nowrite returned implies that all in-flight
1658 * requests were completed along with all of their secondary
1659 * requests. Further primary requests are blocked by negative
1660 * writectr. Hence there cannot be any in-flight requests and
1661 * no invocations of fuse_writepage_end() while we're in
1662 * fuse_set_nowrite..fuse_release_nowrite section.
1663 */
1664 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001665 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001666 fi->writectr--;
1667 fuse_writepage_finish(fc, req);
1668 spin_unlock(&fc->lock);
1669 fuse_writepage_free(fc, req);
1670}
1671
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001672static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1673 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001674{
Miklos Szeredi72523422013-10-01 16:44:52 +02001675 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001676
1677 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001678 if (!WARN_ON(list_empty(&fi->write_files))) {
1679 ff = list_entry(fi->write_files.next, struct fuse_file,
1680 write_entry);
1681 fuse_file_get(ff);
1682 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001683 spin_unlock(&fc->lock);
1684
1685 return ff;
1686}
1687
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688static int fuse_writepage_locked(struct page *page)
1689{
1690 struct address_space *mapping = page->mapping;
1691 struct inode *inode = mapping->host;
1692 struct fuse_conn *fc = get_fuse_conn(inode);
1693 struct fuse_inode *fi = get_fuse_inode(inode);
1694 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001695 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001696 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001697
1698 set_page_writeback(page);
1699
Maxim Patlasov4250c062012-10-26 19:48:07 +04001700 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001701 if (!req)
1702 goto err;
1703
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001704 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001705 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1706 if (!tmp_page)
1707 goto err_free;
1708
Miklos Szeredi72523422013-10-01 16:44:52 +02001709 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001710 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001711 if (!req->ff)
1712 goto err_free;
1713
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001714 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001715
1716 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001717 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001718 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001719 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001720 req->num_pages = 1;
1721 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001722 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001723 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001724 req->end = fuse_writepage_end;
1725 req->inode = inode;
1726
1727 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1728 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001729
1730 spin_lock(&fc->lock);
1731 list_add(&req->writepages_entry, &fi->writepages);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fc->lock);
1735
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001736 end_page_writeback(page);
1737
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001738 return 0;
1739
1740err_free:
1741 fuse_request_free(req);
1742err:
1743 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001744 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001745}
1746
1747static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1748{
1749 int err;
1750
Miklos Szerediff17be02013-10-01 16:44:53 +02001751 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1752 /*
1753 * ->writepages() should be called for sync() and friends. We
1754 * should only get here on direct reclaim and then we are
1755 * allowed to skip a page which is already in flight
1756 */
1757 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1758
1759 redirty_page_for_writepage(wbc, page);
1760 return 0;
1761 }
1762
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001763 err = fuse_writepage_locked(page);
1764 unlock_page(page);
1765
1766 return err;
1767}
1768
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001769struct fuse_fill_wb_data {
1770 struct fuse_req *req;
1771 struct fuse_file *ff;
1772 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001773 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001774};
1775
1776static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1777{
1778 struct fuse_req *req = data->req;
1779 struct inode *inode = data->inode;
1780 struct fuse_conn *fc = get_fuse_conn(inode);
1781 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001782 int num_pages = req->num_pages;
1783 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001784
1785 req->ff = fuse_file_get(data->ff);
1786 spin_lock(&fc->lock);
1787 list_add_tail(&req->list, &fi->queued_writes);
1788 fuse_flush_writepages(inode);
1789 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001790
1791 for (i = 0; i < num_pages; i++)
1792 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001793}
1794
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001795static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1796 struct page *page)
1797{
1798 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1799 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1800 struct fuse_req *tmp;
1801 struct fuse_req *old_req;
1802 bool found = false;
1803 pgoff_t curr_index;
1804
1805 BUG_ON(new_req->num_pages != 0);
1806
1807 spin_lock(&fc->lock);
1808 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1810 BUG_ON(old_req->inode != new_req->inode);
1811 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1812 if (curr_index <= page->index &&
1813 page->index < curr_index + old_req->num_pages) {
1814 found = true;
1815 break;
1816 }
1817 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001818 if (!found) {
1819 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001820 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001821 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001822
Maxim Patlasovf6011082013-10-02 15:01:07 +04001823 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001824 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1825 BUG_ON(tmp->inode != new_req->inode);
1826 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1827 if (tmp->num_pages == 1 &&
1828 curr_index == page->index) {
1829 old_req = tmp;
1830 }
1831 }
1832
1833 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1834 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001835 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1836
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001837 copy_highpage(old_req->pages[0], page);
1838 spin_unlock(&fc->lock);
1839
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001840 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001841 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001842 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001843 fuse_writepage_free(fc, new_req);
1844 fuse_request_free(new_req);
1845 goto out;
1846 } else {
1847 new_req->misc.write.next = old_req->misc.write.next;
1848 old_req->misc.write.next = new_req;
1849 }
1850out_unlock:
1851 spin_unlock(&fc->lock);
1852out:
1853 return found;
1854}
1855
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001856static int fuse_writepages_fill(struct page *page,
1857 struct writeback_control *wbc, void *_data)
1858{
1859 struct fuse_fill_wb_data *data = _data;
1860 struct fuse_req *req = data->req;
1861 struct inode *inode = data->inode;
1862 struct fuse_conn *fc = get_fuse_conn(inode);
1863 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001864 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001865 int err;
1866
1867 if (!data->ff) {
1868 err = -EIO;
1869 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1870 if (!data->ff)
1871 goto out_unlock;
1872 }
1873
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001874 /*
1875 * Being under writeback is unlikely but possible. For example direct
1876 * read to an mmaped fuse file will set the page dirty twice; once when
1877 * the pages are faulted with get_user_pages(), and then after the read
1878 * completed.
1879 */
1880 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001881
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001882 if (req && req->num_pages &&
1883 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1884 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1885 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1886 fuse_writepages_send(data);
1887 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001888 }
1889 err = -ENOMEM;
1890 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1891 if (!tmp_page)
1892 goto out_unlock;
1893
1894 /*
1895 * The page must not be redirtied until the writeout is completed
1896 * (i.e. userspace has sent a reply to the write request). Otherwise
1897 * there could be more than one temporary page instance for each real
1898 * page.
1899 *
1900 * This is ensured by holding the page lock in page_mkwrite() while
1901 * checking fuse_page_is_writeback(). We already hold the page lock
1902 * since clear_page_dirty_for_io() and keep it held until we add the
1903 * request to the fi->writepages list and increment req->num_pages.
1904 * After this fuse_page_is_writeback() will indicate that the page is
1905 * under writeback, so we can release the page lock.
1906 */
1907 if (data->req == NULL) {
1908 struct fuse_inode *fi = get_fuse_inode(inode);
1909
1910 err = -ENOMEM;
1911 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1912 if (!req) {
1913 __free_page(tmp_page);
1914 goto out_unlock;
1915 }
1916
1917 fuse_write_fill(req, data->ff, page_offset(page), 0);
1918 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001919 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001920 req->in.argpages = 1;
1921 req->background = 1;
1922 req->num_pages = 0;
1923 req->end = fuse_writepage_end;
1924 req->inode = inode;
1925
1926 spin_lock(&fc->lock);
1927 list_add(&req->writepages_entry, &fi->writepages);
1928 spin_unlock(&fc->lock);
1929
1930 data->req = req;
1931 }
1932 set_page_writeback(page);
1933
1934 copy_highpage(tmp_page, page);
1935 req->pages[req->num_pages] = tmp_page;
1936 req->page_descs[req->num_pages].offset = 0;
1937 req->page_descs[req->num_pages].length = PAGE_SIZE;
1938
1939 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1940 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001941
1942 err = 0;
1943 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1944 end_page_writeback(page);
1945 data->req = NULL;
1946 goto out_unlock;
1947 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001948 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001949
1950 /*
1951 * Protected by fc->lock against concurrent access by
1952 * fuse_page_is_writeback().
1953 */
1954 spin_lock(&fc->lock);
1955 req->num_pages++;
1956 spin_unlock(&fc->lock);
1957
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001958out_unlock:
1959 unlock_page(page);
1960
1961 return err;
1962}
1963
1964static int fuse_writepages(struct address_space *mapping,
1965 struct writeback_control *wbc)
1966{
1967 struct inode *inode = mapping->host;
1968 struct fuse_fill_wb_data data;
1969 int err;
1970
1971 err = -EIO;
1972 if (is_bad_inode(inode))
1973 goto out;
1974
1975 data.inode = inode;
1976 data.req = NULL;
1977 data.ff = NULL;
1978
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001979 err = -ENOMEM;
1980 data.orig_pages = kzalloc(sizeof(struct page *) *
1981 FUSE_MAX_PAGES_PER_REQ,
1982 GFP_NOFS);
1983 if (!data.orig_pages)
1984 goto out;
1985
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001986 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1987 if (data.req) {
1988 /* Ignore errors if we can write at least one page */
1989 BUG_ON(!data.req->num_pages);
1990 fuse_writepages_send(&data);
1991 err = 0;
1992 }
1993 if (data.ff)
1994 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001995
1996 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001997out:
1998 return err;
1999}
2000
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002001/*
2002 * It's worthy to make sure that space is reserved on disk for the write,
2003 * but how to implement it without killing performance need more thinking.
2004 */
2005static int fuse_write_begin(struct file *file, struct address_space *mapping,
2006 loff_t pos, unsigned len, unsigned flags,
2007 struct page **pagep, void **fsdata)
2008{
2009 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2010 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
2011 struct page *page;
2012 loff_t fsize;
2013 int err = -ENOMEM;
2014
2015 WARN_ON(!fc->writeback_cache);
2016
2017 page = grab_cache_page_write_begin(mapping, index, flags);
2018 if (!page)
2019 goto error;
2020
2021 fuse_wait_on_page_writeback(mapping->host, page->index);
2022
2023 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
2024 goto success;
2025 /*
2026 * Check if the start this page comes after the end of file, in which
2027 * case the readpage can be optimized away.
2028 */
2029 fsize = i_size_read(mapping->host);
2030 if (fsize <= (pos & PAGE_CACHE_MASK)) {
2031 size_t off = pos & ~PAGE_CACHE_MASK;
2032 if (off)
2033 zero_user_segment(page, 0, off);
2034 goto success;
2035 }
2036 err = fuse_do_readpage(file, page);
2037 if (err)
2038 goto cleanup;
2039success:
2040 *pagep = page;
2041 return 0;
2042
2043cleanup:
2044 unlock_page(page);
2045 page_cache_release(page);
2046error:
2047 return err;
2048}
2049
2050static int fuse_write_end(struct file *file, struct address_space *mapping,
2051 loff_t pos, unsigned len, unsigned copied,
2052 struct page *page, void *fsdata)
2053{
2054 struct inode *inode = page->mapping->host;
2055
2056 if (!PageUptodate(page)) {
2057 /* Zero any unwritten bytes at the end of the page */
2058 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2059 if (endoff)
2060 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2061 SetPageUptodate(page);
2062 }
2063
2064 fuse_write_update_size(inode, pos + copied);
2065 set_page_dirty(page);
2066 unlock_page(page);
2067 page_cache_release(page);
2068
2069 return copied;
2070}
2071
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002072static int fuse_launder_page(struct page *page)
2073{
2074 int err = 0;
2075 if (clear_page_dirty_for_io(page)) {
2076 struct inode *inode = page->mapping->host;
2077 err = fuse_writepage_locked(page);
2078 if (!err)
2079 fuse_wait_on_page_writeback(inode, page->index);
2080 }
2081 return err;
2082}
2083
2084/*
2085 * Write back dirty pages now, because there may not be any suitable
2086 * open files later
2087 */
2088static void fuse_vma_close(struct vm_area_struct *vma)
2089{
2090 filemap_write_and_wait(vma->vm_file->f_mapping);
2091}
2092
2093/*
2094 * Wait for writeback against this page to complete before allowing it
2095 * to be marked dirty again, and hence written back again, possibly
2096 * before the previous writepage completed.
2097 *
2098 * Block here, instead of in ->writepage(), so that the userspace fs
2099 * can only block processes actually operating on the filesystem.
2100 *
2101 * Otherwise unprivileged userspace fs would be able to block
2102 * unrelated:
2103 *
2104 * - page migration
2105 * - sync(2)
2106 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2107 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002108static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002109{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002110 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002111 struct inode *inode = file_inode(vma->vm_file);
2112
2113 file_update_time(vma->vm_file);
2114 lock_page(page);
2115 if (page->mapping != inode->i_mapping) {
2116 unlock_page(page);
2117 return VM_FAULT_NOPAGE;
2118 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002119
2120 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002121 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002122}
2123
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002124static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002125 .close = fuse_vma_close,
2126 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002127 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002128 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002129 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002130};
2131
2132static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2133{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002134 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2135 fuse_link_write_file(file);
2136
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002137 file_accessed(file);
2138 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002139 return 0;
2140}
2141
Miklos Szeredifc280c92009-04-02 14:25:35 +02002142static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2143{
2144 /* Can't provide the coherency needed for MAP_SHARED */
2145 if (vma->vm_flags & VM_MAYSHARE)
2146 return -ENODEV;
2147
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002148 invalidate_inode_pages2(file->f_mapping);
2149
Miklos Szeredifc280c92009-04-02 14:25:35 +02002150 return generic_file_mmap(file, vma);
2151}
2152
Miklos Szeredi71421252006-06-25 05:48:52 -07002153static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2154 struct file_lock *fl)
2155{
2156 switch (ffl->type) {
2157 case F_UNLCK:
2158 break;
2159
2160 case F_RDLCK:
2161 case F_WRLCK:
2162 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2163 ffl->end < ffl->start)
2164 return -EIO;
2165
2166 fl->fl_start = ffl->start;
2167 fl->fl_end = ffl->end;
2168 fl->fl_pid = ffl->pid;
2169 break;
2170
2171 default:
2172 return -EIO;
2173 }
2174 fl->fl_type = ffl->type;
2175 return 0;
2176}
2177
2178static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002179 const struct file_lock *fl, int opcode, pid_t pid,
2180 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002181{
Al Viro6131ffa2013-02-27 16:59:05 -05002182 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002183 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002184 struct fuse_file *ff = file->private_data;
2185 struct fuse_lk_in *arg = &req->misc.lk_in;
2186
2187 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002188 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002189 arg->lk.start = fl->fl_start;
2190 arg->lk.end = fl->fl_end;
2191 arg->lk.type = fl->fl_type;
2192 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002193 if (flock)
2194 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002195 req->in.h.opcode = opcode;
2196 req->in.h.nodeid = get_node_id(inode);
2197 req->in.numargs = 1;
2198 req->in.args[0].size = sizeof(*arg);
2199 req->in.args[0].value = arg;
2200}
2201
2202static int fuse_getlk(struct file *file, struct file_lock *fl)
2203{
Al Viro6131ffa2013-02-27 16:59:05 -05002204 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002205 struct fuse_conn *fc = get_fuse_conn(inode);
2206 struct fuse_req *req;
2207 struct fuse_lk_out outarg;
2208 int err;
2209
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002210 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002211 if (IS_ERR(req))
2212 return PTR_ERR(req);
2213
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002214 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002215 req->out.numargs = 1;
2216 req->out.args[0].size = sizeof(outarg);
2217 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002218 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002219 err = req->out.h.error;
2220 fuse_put_request(fc, req);
2221 if (!err)
2222 err = convert_fuse_file_lock(&outarg.lk, fl);
2223
2224 return err;
2225}
2226
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002227static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002228{
Al Viro6131ffa2013-02-27 16:59:05 -05002229 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002230 struct fuse_conn *fc = get_fuse_conn(inode);
2231 struct fuse_req *req;
2232 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2233 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2234 int err;
2235
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002236 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002237 /* NLM needs asynchronous locks, which we don't support yet */
2238 return -ENOLCK;
2239 }
2240
Miklos Szeredi71421252006-06-25 05:48:52 -07002241 /* Unlock on close is handled by the flush method */
2242 if (fl->fl_flags & FL_CLOSE)
2243 return 0;
2244
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002245 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002246 if (IS_ERR(req))
2247 return PTR_ERR(req);
2248
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002249 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002250 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002251 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002252 /* locking is restartable */
2253 if (err == -EINTR)
2254 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002255 fuse_put_request(fc, req);
2256 return err;
2257}
2258
2259static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2260{
Al Viro6131ffa2013-02-27 16:59:05 -05002261 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002262 struct fuse_conn *fc = get_fuse_conn(inode);
2263 int err;
2264
Miklos Szeredi48e90762008-07-25 01:49:02 -07002265 if (cmd == F_CANCELLK) {
2266 err = 0;
2267 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002268 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002269 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002270 err = 0;
2271 } else
2272 err = fuse_getlk(file, fl);
2273 } else {
2274 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002275 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002276 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002277 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002278 }
2279 return err;
2280}
2281
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002282static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2283{
Al Viro6131ffa2013-02-27 16:59:05 -05002284 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002285 struct fuse_conn *fc = get_fuse_conn(inode);
2286 int err;
2287
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002288 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002289 err = flock_lock_file_wait(file, fl);
2290 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002291 struct fuse_file *ff = file->private_data;
2292
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002293 /* emulate flock with POSIX locks */
2294 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002295 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002296 err = fuse_setlk(file, fl, 1);
2297 }
2298
2299 return err;
2300}
2301
Miklos Szeredib2d22722006-12-06 20:35:51 -08002302static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2303{
2304 struct inode *inode = mapping->host;
2305 struct fuse_conn *fc = get_fuse_conn(inode);
2306 struct fuse_req *req;
2307 struct fuse_bmap_in inarg;
2308 struct fuse_bmap_out outarg;
2309 int err;
2310
2311 if (!inode->i_sb->s_bdev || fc->no_bmap)
2312 return 0;
2313
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002314 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002315 if (IS_ERR(req))
2316 return 0;
2317
2318 memset(&inarg, 0, sizeof(inarg));
2319 inarg.block = block;
2320 inarg.blocksize = inode->i_sb->s_blocksize;
2321 req->in.h.opcode = FUSE_BMAP;
2322 req->in.h.nodeid = get_node_id(inode);
2323 req->in.numargs = 1;
2324 req->in.args[0].size = sizeof(inarg);
2325 req->in.args[0].value = &inarg;
2326 req->out.numargs = 1;
2327 req->out.args[0].size = sizeof(outarg);
2328 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002329 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002330 err = req->out.h.error;
2331 fuse_put_request(fc, req);
2332 if (err == -ENOSYS)
2333 fc->no_bmap = 1;
2334
2335 return err ? 0 : outarg.block;
2336}
2337
Andrew Morton965c8e52012-12-17 15:59:39 -08002338static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002339{
2340 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002341 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002342
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002343 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002344 if (whence == SEEK_CUR || whence == SEEK_SET)
2345 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002346
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002347 mutex_lock(&inode->i_mutex);
2348 retval = fuse_update_attributes(inode, NULL, file, NULL);
2349 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002350 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002351 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002352
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002353 return retval;
2354}
2355
Tejun Heo59efec72008-11-26 12:03:55 +01002356static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2357 unsigned int nr_segs, size_t bytes, bool to_user)
2358{
2359 struct iov_iter ii;
2360 int page_idx = 0;
2361
2362 if (!bytes)
2363 return 0;
2364
2365 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2366
2367 while (iov_iter_count(&ii)) {
2368 struct page *page = pages[page_idx++];
2369 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002370 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002371
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002372 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002373
2374 while (todo) {
2375 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2376 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2377 size_t copy = min(todo, iov_len);
2378 size_t left;
2379
2380 if (!to_user)
2381 left = copy_from_user(kaddr, uaddr, copy);
2382 else
2383 left = copy_to_user(uaddr, kaddr, copy);
2384
2385 if (unlikely(left))
2386 return -EFAULT;
2387
2388 iov_iter_advance(&ii, copy);
2389 todo -= copy;
2390 kaddr += copy;
2391 }
2392
Jens Axboe0bd87182009-11-03 11:40:44 +01002393 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002394 }
2395
2396 return 0;
2397}
2398
2399/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002400 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2401 * ABI was defined to be 'struct iovec' which is different on 32bit
2402 * and 64bit. Fortunately we can determine which structure the server
2403 * used from the size of the reply.
2404 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002405static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2406 size_t transferred, unsigned count,
2407 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002408{
2409#ifdef CONFIG_COMPAT
2410 if (count * sizeof(struct compat_iovec) == transferred) {
2411 struct compat_iovec *ciov = src;
2412 unsigned i;
2413
2414 /*
2415 * With this interface a 32bit server cannot support
2416 * non-compat (i.e. ones coming from 64bit apps) ioctl
2417 * requests
2418 */
2419 if (!is_compat)
2420 return -EINVAL;
2421
2422 for (i = 0; i < count; i++) {
2423 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2424 dst[i].iov_len = ciov[i].iov_len;
2425 }
2426 return 0;
2427 }
2428#endif
2429
2430 if (count * sizeof(struct iovec) != transferred)
2431 return -EIO;
2432
2433 memcpy(dst, src, transferred);
2434 return 0;
2435}
2436
Miklos Szeredi75727772010-11-30 16:39:27 +01002437/* Make sure iov_length() won't overflow */
2438static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2439{
2440 size_t n;
2441 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2442
Zach Brownfb6ccff2012-07-24 12:10:11 -07002443 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002444 if (iov->iov_len > (size_t) max)
2445 return -ENOMEM;
2446 max -= iov->iov_len;
2447 }
2448 return 0;
2449}
2450
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002451static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2452 void *src, size_t transferred, unsigned count,
2453 bool is_compat)
2454{
2455 unsigned i;
2456 struct fuse_ioctl_iovec *fiov = src;
2457
2458 if (fc->minor < 16) {
2459 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2460 count, is_compat);
2461 }
2462
2463 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2464 return -EIO;
2465
2466 for (i = 0; i < count; i++) {
2467 /* Did the server supply an inappropriate value? */
2468 if (fiov[i].base != (unsigned long) fiov[i].base ||
2469 fiov[i].len != (unsigned long) fiov[i].len)
2470 return -EIO;
2471
2472 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2473 dst[i].iov_len = (size_t) fiov[i].len;
2474
2475#ifdef CONFIG_COMPAT
2476 if (is_compat &&
2477 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2478 (compat_size_t) dst[i].iov_len != fiov[i].len))
2479 return -EIO;
2480#endif
2481 }
2482
2483 return 0;
2484}
2485
2486
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002487/*
Tejun Heo59efec72008-11-26 12:03:55 +01002488 * For ioctls, there is no generic way to determine how much memory
2489 * needs to be read and/or written. Furthermore, ioctls are allowed
2490 * to dereference the passed pointer, so the parameter requires deep
2491 * copying but FUSE has no idea whatsoever about what to copy in or
2492 * out.
2493 *
2494 * This is solved by allowing FUSE server to retry ioctl with
2495 * necessary in/out iovecs. Let's assume the ioctl implementation
2496 * needs to read in the following structure.
2497 *
2498 * struct a {
2499 * char *buf;
2500 * size_t buflen;
2501 * }
2502 *
2503 * On the first callout to FUSE server, inarg->in_size and
2504 * inarg->out_size will be NULL; then, the server completes the ioctl
2505 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2506 * the actual iov array to
2507 *
2508 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2509 *
2510 * which tells FUSE to copy in the requested area and retry the ioctl.
2511 * On the second round, the server has access to the structure and
2512 * from that it can tell what to look for next, so on the invocation,
2513 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2514 *
2515 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2516 * { .iov_base = a.buf, .iov_len = a.buflen } }
2517 *
2518 * FUSE will copy both struct a and the pointed buffer from the
2519 * process doing the ioctl and retry ioctl with both struct a and the
2520 * buffer.
2521 *
2522 * This time, FUSE server has everything it needs and completes ioctl
2523 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2524 *
2525 * Copying data out works the same way.
2526 *
2527 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2528 * automatically initializes in and out iovs by decoding @cmd with
2529 * _IOC_* macros and the server is not allowed to request RETRY. This
2530 * limits ioctl data transfers to well-formed ioctls and is the forced
2531 * behavior for all FUSE servers.
2532 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002533long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2534 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002535{
Tejun Heo59efec72008-11-26 12:03:55 +01002536 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002537 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002538 struct fuse_ioctl_in inarg = {
2539 .fh = ff->fh,
2540 .cmd = cmd,
2541 .arg = arg,
2542 .flags = flags
2543 };
2544 struct fuse_ioctl_out outarg;
2545 struct fuse_req *req = NULL;
2546 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002547 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002548 struct iovec *in_iov = NULL, *out_iov = NULL;
2549 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2550 size_t in_size, out_size, transferred;
2551 int err;
2552
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002553#if BITS_PER_LONG == 32
2554 inarg.flags |= FUSE_IOCTL_32BIT;
2555#else
2556 if (flags & FUSE_IOCTL_COMPAT)
2557 inarg.flags |= FUSE_IOCTL_32BIT;
2558#endif
2559
Tejun Heo59efec72008-11-26 12:03:55 +01002560 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002561 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002562
Tejun Heo59efec72008-11-26 12:03:55 +01002563 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002564 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002565 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002566 if (!pages || !iov_page)
2567 goto out;
2568
2569 /*
2570 * If restricted, initialize IO parameters as encoded in @cmd.
2571 * RETRY from server is not allowed.
2572 */
2573 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002574 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002575
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002576 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002577 iov->iov_len = _IOC_SIZE(cmd);
2578
2579 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2580 in_iov = iov;
2581 in_iovs = 1;
2582 }
2583
2584 if (_IOC_DIR(cmd) & _IOC_READ) {
2585 out_iov = iov;
2586 out_iovs = 1;
2587 }
2588 }
2589
2590 retry:
2591 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2592 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2593
2594 /*
2595 * Out data can be used either for actual out data or iovs,
2596 * make sure there always is at least one page.
2597 */
2598 out_size = max_t(size_t, out_size, PAGE_SIZE);
2599 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2600
2601 /* make sure there are enough buffer pages and init request with them */
2602 err = -ENOMEM;
2603 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2604 goto out;
2605 while (num_pages < max_pages) {
2606 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2607 if (!pages[num_pages])
2608 goto out;
2609 num_pages++;
2610 }
2611
Maxim Patlasov54b96672012-10-26 19:49:13 +04002612 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002613 if (IS_ERR(req)) {
2614 err = PTR_ERR(req);
2615 req = NULL;
2616 goto out;
2617 }
2618 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2619 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002620 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002621
2622 /* okay, let's send it to the client */
2623 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002624 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002625 req->in.numargs = 1;
2626 req->in.args[0].size = sizeof(inarg);
2627 req->in.args[0].value = &inarg;
2628 if (in_size) {
2629 req->in.numargs++;
2630 req->in.args[1].size = in_size;
2631 req->in.argpages = 1;
2632
2633 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2634 false);
2635 if (err)
2636 goto out;
2637 }
2638
2639 req->out.numargs = 2;
2640 req->out.args[0].size = sizeof(outarg);
2641 req->out.args[0].value = &outarg;
2642 req->out.args[1].size = out_size;
2643 req->out.argpages = 1;
2644 req->out.argvar = 1;
2645
Tejun Heob93f8582008-11-26 12:03:55 +01002646 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002647 err = req->out.h.error;
2648 transferred = req->out.args[1].size;
2649 fuse_put_request(fc, req);
2650 req = NULL;
2651 if (err)
2652 goto out;
2653
2654 /* did it ask for retry? */
2655 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002656 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002657
2658 /* no retry if in restricted mode */
2659 err = -EIO;
2660 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2661 goto out;
2662
2663 in_iovs = outarg.in_iovs;
2664 out_iovs = outarg.out_iovs;
2665
2666 /*
2667 * Make sure things are in boundary, separate checks
2668 * are to protect against overflow.
2669 */
2670 err = -ENOMEM;
2671 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2672 out_iovs > FUSE_IOCTL_MAX_IOV ||
2673 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2674 goto out;
2675
Cong Wang2408f6e2011-11-25 23:14:30 +08002676 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002677 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002678 transferred, in_iovs + out_iovs,
2679 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002680 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002681 if (err)
2682 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002683
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002684 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002685 out_iov = in_iov + in_iovs;
2686
Miklos Szeredi75727772010-11-30 16:39:27 +01002687 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2688 if (err)
2689 goto out;
2690
2691 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2692 if (err)
2693 goto out;
2694
Tejun Heo59efec72008-11-26 12:03:55 +01002695 goto retry;
2696 }
2697
2698 err = -EIO;
2699 if (transferred > inarg.out_size)
2700 goto out;
2701
2702 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2703 out:
2704 if (req)
2705 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002706 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002707 while (num_pages)
2708 __free_page(pages[--num_pages]);
2709 kfree(pages);
2710
2711 return err ? err : outarg.result;
2712}
Tejun Heo08cbf542009-04-14 10:54:53 +09002713EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002714
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002715long fuse_ioctl_common(struct file *file, unsigned int cmd,
2716 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002717{
Al Viro6131ffa2013-02-27 16:59:05 -05002718 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002719 struct fuse_conn *fc = get_fuse_conn(inode);
2720
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002721 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002722 return -EACCES;
2723
2724 if (is_bad_inode(inode))
2725 return -EIO;
2726
2727 return fuse_do_ioctl(file, cmd, arg, flags);
2728}
2729
Tejun Heo59efec72008-11-26 12:03:55 +01002730static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2731 unsigned long arg)
2732{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002733 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002734}
2735
2736static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2737 unsigned long arg)
2738{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002739 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002740}
2741
Tejun Heo95668a62008-11-26 12:03:55 +01002742/*
2743 * All files which have been polled are linked to RB tree
2744 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2745 * find the matching one.
2746 */
2747static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2748 struct rb_node **parent_out)
2749{
2750 struct rb_node **link = &fc->polled_files.rb_node;
2751 struct rb_node *last = NULL;
2752
2753 while (*link) {
2754 struct fuse_file *ff;
2755
2756 last = *link;
2757 ff = rb_entry(last, struct fuse_file, polled_node);
2758
2759 if (kh < ff->kh)
2760 link = &last->rb_left;
2761 else if (kh > ff->kh)
2762 link = &last->rb_right;
2763 else
2764 return link;
2765 }
2766
2767 if (parent_out)
2768 *parent_out = last;
2769 return link;
2770}
2771
2772/*
2773 * The file is about to be polled. Make sure it's on the polled_files
2774 * RB tree. Note that files once added to the polled_files tree are
2775 * not removed before the file is released. This is because a file
2776 * polled once is likely to be polled again.
2777 */
2778static void fuse_register_polled_file(struct fuse_conn *fc,
2779 struct fuse_file *ff)
2780{
2781 spin_lock(&fc->lock);
2782 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002783 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002784
2785 link = fuse_find_polled_node(fc, ff->kh, &parent);
2786 BUG_ON(*link);
2787 rb_link_node(&ff->polled_node, parent, link);
2788 rb_insert_color(&ff->polled_node, &fc->polled_files);
2789 }
2790 spin_unlock(&fc->lock);
2791}
2792
Tejun Heo08cbf542009-04-14 10:54:53 +09002793unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002794{
Tejun Heo95668a62008-11-26 12:03:55 +01002795 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002796 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002797 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2798 struct fuse_poll_out outarg;
2799 struct fuse_req *req;
2800 int err;
2801
2802 if (fc->no_poll)
2803 return DEFAULT_POLLMASK;
2804
2805 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002806 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002807
2808 /*
2809 * Ask for notification iff there's someone waiting for it.
2810 * The client may ignore the flag and always notify.
2811 */
2812 if (waitqueue_active(&ff->poll_wait)) {
2813 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2814 fuse_register_polled_file(fc, ff);
2815 }
2816
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002817 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002818 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002819 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002820
2821 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002822 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002823 req->in.numargs = 1;
2824 req->in.args[0].size = sizeof(inarg);
2825 req->in.args[0].value = &inarg;
2826 req->out.numargs = 1;
2827 req->out.args[0].size = sizeof(outarg);
2828 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002829 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002830 err = req->out.h.error;
2831 fuse_put_request(fc, req);
2832
2833 if (!err)
2834 return outarg.revents;
2835 if (err == -ENOSYS) {
2836 fc->no_poll = 1;
2837 return DEFAULT_POLLMASK;
2838 }
2839 return POLLERR;
2840}
Tejun Heo08cbf542009-04-14 10:54:53 +09002841EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002842
2843/*
2844 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2845 * wakes up the poll waiters.
2846 */
2847int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2848 struct fuse_notify_poll_wakeup_out *outarg)
2849{
2850 u64 kh = outarg->kh;
2851 struct rb_node **link;
2852
2853 spin_lock(&fc->lock);
2854
2855 link = fuse_find_polled_node(fc, kh, NULL);
2856 if (*link) {
2857 struct fuse_file *ff;
2858
2859 ff = rb_entry(*link, struct fuse_file, polled_node);
2860 wake_up_interruptible_sync(&ff->poll_wait);
2861 }
2862
2863 spin_unlock(&fc->lock);
2864 return 0;
2865}
2866
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002867static void fuse_do_truncate(struct file *file)
2868{
2869 struct inode *inode = file->f_mapping->host;
2870 struct iattr attr;
2871
2872 attr.ia_valid = ATTR_SIZE;
2873 attr.ia_size = i_size_read(inode);
2874
2875 attr.ia_file = file;
2876 attr.ia_valid |= ATTR_FILE;
2877
2878 fuse_do_setattr(inode, &attr, file);
2879}
2880
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002881static inline loff_t fuse_round_up(loff_t off)
2882{
2883 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2884}
2885
Anand Avati4273b792012-02-17 12:46:25 -05002886static ssize_t
2887fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2888 loff_t offset, unsigned long nr_segs)
2889{
2890 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002891 struct file *file = iocb->ki_filp;
2892 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002893 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002894 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002895 struct inode *inode;
2896 loff_t i_size;
2897 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002898 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002899
Anand Avati4273b792012-02-17 12:46:25 -05002900 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002901 inode = file->f_mapping->host;
2902 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002903
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002904 if ((rw == READ) && (offset > i_size))
2905 return 0;
2906
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002907 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002908 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002909 if (offset >= i_size)
2910 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002911 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002912 }
2913
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002914 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002915 if (!io)
2916 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002917 spin_lock_init(&io->lock);
2918 io->reqs = 1;
2919 io->bytes = -1;
2920 io->size = 0;
2921 io->offset = offset;
2922 io->write = (rw == WRITE);
2923 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002924 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002925 /*
2926 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002927 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002928 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002929 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002930 io->iocb = iocb;
2931
2932 /*
2933 * We cannot asynchronously extend the size of a file. We have no method
2934 * to wait on real async I/O requests, so we must submit this request
2935 * synchronously.
2936 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002937 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002938 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002939
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002940 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002941 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002942 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002943 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002944
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002945 if (io->async) {
2946 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2947
2948 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002949 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002950 return -EIOCBQUEUED;
2951
2952 ret = wait_on_sync_kiocb(iocb);
2953 } else {
2954 kfree(io);
2955 }
2956
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002957 if (rw == WRITE) {
2958 if (ret > 0)
2959 fuse_write_update_size(inode, pos);
2960 else if (ret < 0 && offset + count > i_size)
2961 fuse_do_truncate(file);
2962 }
Anand Avati4273b792012-02-17 12:46:25 -05002963
2964 return ret;
2965}
2966
Miklos Szeredicdadb112012-11-10 16:55:56 +01002967static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2968 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002969{
2970 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002971 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002972 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002973 struct fuse_conn *fc = ff->fc;
2974 struct fuse_req *req;
2975 struct fuse_fallocate_in inarg = {
2976 .fh = ff->fh,
2977 .offset = offset,
2978 .length = length,
2979 .mode = mode
2980 };
2981 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002982 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2983 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002984
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002985 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2986 return -EOPNOTSUPP;
2987
Miklos Szeredi519c6042012-04-26 10:56:36 +02002988 if (fc->no_fallocate)
2989 return -EOPNOTSUPP;
2990
Maxim Patlasov14c14412013-06-13 12:16:39 +04002991 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002992 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002993 if (mode & FALLOC_FL_PUNCH_HOLE) {
2994 loff_t endbyte = offset + length - 1;
2995 err = filemap_write_and_wait_range(inode->i_mapping,
2996 offset, endbyte);
2997 if (err)
2998 goto out;
2999
3000 fuse_sync_writes(inode);
3001 }
Brian Foster3634a632013-05-17 09:30:32 -04003002 }
3003
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003004 if (!(mode & FALLOC_FL_KEEP_SIZE))
3005 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3006
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04003007 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04003008 if (IS_ERR(req)) {
3009 err = PTR_ERR(req);
3010 goto out;
3011 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003012
3013 req->in.h.opcode = FUSE_FALLOCATE;
3014 req->in.h.nodeid = ff->nodeid;
3015 req->in.numargs = 1;
3016 req->in.args[0].size = sizeof(inarg);
3017 req->in.args[0].value = &inarg;
3018 fuse_request_send(fc, req);
3019 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02003020 if (err == -ENOSYS) {
3021 fc->no_fallocate = 1;
3022 err = -EOPNOTSUPP;
3023 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003024 fuse_put_request(fc, req);
3025
Brian Fosterbee6c302013-05-17 15:27:34 -04003026 if (err)
3027 goto out;
3028
3029 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003030 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3031 bool changed = fuse_write_update_size(inode, offset + length);
3032
3033 if (changed && fc->writeback_cache) {
3034 struct fuse_inode *fi = get_fuse_inode(inode);
3035
3036 inode->i_mtime = current_fs_time(inode->i_sb);
3037 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
3038 }
3039 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003040
3041 if (mode & FALLOC_FL_PUNCH_HOLE)
3042 truncate_pagecache_range(inode, offset, offset + length - 1);
3043
3044 fuse_invalidate_attr(inode);
3045
Brian Foster3634a632013-05-17 09:30:32 -04003046out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003047 if (!(mode & FALLOC_FL_KEEP_SIZE))
3048 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3049
Maxim Patlasovbde52782013-09-13 19:19:54 +04003050 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003051 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003052
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003053 return err;
3054}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003055
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003056static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003057 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003058 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003059 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003060 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003061 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003062 .mmap = fuse_file_mmap,
3063 .open = fuse_open,
3064 .flush = fuse_flush,
3065 .release = fuse_release,
3066 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003067 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003068 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003069 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003070 .unlocked_ioctl = fuse_file_ioctl,
3071 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003072 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003073 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003074};
3075
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003076static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003077 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003078 .read = fuse_direct_read,
3079 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003080 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003081 .open = fuse_open,
3082 .flush = fuse_flush,
3083 .release = fuse_release,
3084 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003085 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003086 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003087 .unlocked_ioctl = fuse_file_ioctl,
3088 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003089 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003090 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003091 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003092};
3093
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003094static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003095 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003096 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003097 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003098 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003099 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003100 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003101 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003102 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003103 .write_begin = fuse_write_begin,
3104 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003105};
3106
3107void fuse_init_file_inode(struct inode *inode)
3108{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003109 inode->i_fop = &fuse_file_operations;
3110 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003111}