blob: 65586a567d0d4ebad99204e28951daf515e42bff [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 mutex_lock(&inode->i_mutex);
494
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700495 /*
496 * Start writeback against all dirty pages of the inode, then
497 * wait for all outstanding writes, before sending the FSYNC
498 * request.
499 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200500 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700501 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400502 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700503
504 fuse_sync_writes(inode);
505
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400506 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
Miklos Szerediaeb4eb62014-04-28 14:19:21 +0200507 err = fuse_flush_mtime(file, false);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400508 if (err)
509 goto out;
510 }
Miklos Szeredi22401e72014-04-28 14:19:23 +0200511 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
512 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400513
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400514 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400515 if (IS_ERR(req)) {
516 err = PTR_ERR(req);
517 goto out;
518 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519
520 memset(&inarg, 0, sizeof(inarg));
521 inarg.fh = ff->fh;
522 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700523 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700524 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 req->in.numargs = 1;
526 req->in.args[0].size = sizeof(inarg);
527 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100528 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700529 err = req->out.h.error;
530 fuse_put_request(fc, req);
531 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700532 if (isdir)
533 fc->no_fsyncdir = 1;
534 else
535 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700536 err = 0;
537 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400538out:
539 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700540 return err;
541}
542
Josef Bacik02c24a82011-07-16 20:44:56 -0400543static int fuse_fsync(struct file *file, loff_t start, loff_t end,
544 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700545{
Josef Bacik02c24a82011-07-16 20:44:56 -0400546 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700547}
548
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200549void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
550 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700551{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700552 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800553 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700554
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800555 inarg->fh = ff->fh;
556 inarg->offset = pos;
557 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800558 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800559 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200560 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700561 req->in.numargs = 1;
562 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800563 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700564 req->out.argvar = 1;
565 req->out.numargs = 1;
566 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700567}
568
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400569static void fuse_release_user_pages(struct fuse_req *req, int write)
570{
571 unsigned i;
572
573 for (i = 0; i < req->num_pages; i++) {
574 struct page *page = req->pages[i];
575 if (write)
576 set_page_dirty_lock(page);
577 put_page(page);
578 }
579}
580
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400581/**
582 * In case of short read, the caller sets 'pos' to the position of
583 * actual end of fuse request in IO request. Otherwise, if bytes_requested
584 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
585 *
586 * An example:
587 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
588 * both submitted asynchronously. The first of them was ACKed by userspace as
589 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
590 * second request was ACKed as short, e.g. only 1K was read, resulting in
591 * pos == 33K.
592 *
593 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
594 * will be equal to the length of the longest contiguous fragment of
595 * transferred data starting from the beginning of IO request.
596 */
597static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
598{
599 int left;
600
601 spin_lock(&io->lock);
602 if (err)
603 io->err = io->err ? : err;
604 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
605 io->bytes = pos;
606
607 left = --io->reqs;
608 spin_unlock(&io->lock);
609
610 if (!left) {
611 long res;
612
613 if (io->err)
614 res = io->err;
615 else if (io->bytes >= 0 && io->write)
616 res = -EIO;
617 else {
618 res = io->bytes < 0 ? io->size : io->bytes;
619
620 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400621 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400622 struct fuse_conn *fc = get_fuse_conn(inode);
623 struct fuse_inode *fi = get_fuse_inode(inode);
624
625 spin_lock(&fc->lock);
626 fi->attr_version = ++fc->attr_version;
627 spin_unlock(&fc->lock);
628 }
629 }
630
631 aio_complete(io->iocb, res, 0);
632 kfree(io);
633 }
634}
635
636static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
637{
638 struct fuse_io_priv *io = req->io;
639 ssize_t pos = -1;
640
641 fuse_release_user_pages(req, !io->write);
642
643 if (io->write) {
644 if (req->misc.write.in.size != req->misc.write.out.size)
645 pos = req->misc.write.in.offset - io->offset +
646 req->misc.write.out.size;
647 } else {
648 if (req->misc.read.in.size != req->out.args[0].size)
649 pos = req->misc.read.in.offset - io->offset +
650 req->out.args[0].size;
651 }
652
653 fuse_aio_complete(io, req->out.h.error, pos);
654}
655
656static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
657 size_t num_bytes, struct fuse_io_priv *io)
658{
659 spin_lock(&io->lock);
660 io->size += num_bytes;
661 io->reqs++;
662 spin_unlock(&io->lock);
663
664 req->io = io;
665 req->end = fuse_aio_complete_req;
666
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400667 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400668 fuse_request_send_background(fc, req);
669
670 return num_bytes;
671}
672
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400673static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200674 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700675{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400676 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200677 struct fuse_file *ff = file->private_data;
678 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700679
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200680 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700681 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700682 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700683
684 inarg->read_flags |= FUSE_READ_LOCKOWNER;
685 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
686 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400687
688 if (io->async)
689 return fuse_async_req_send(fc, req, count, io);
690
Tejun Heob93f8582008-11-26 12:03:55 +0100691 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800692 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700693}
694
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700695static void fuse_read_update_size(struct inode *inode, loff_t size,
696 u64 attr_ver)
697{
698 struct fuse_conn *fc = get_fuse_conn(inode);
699 struct fuse_inode *fi = get_fuse_inode(inode);
700
701 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400702 if (attr_ver == fi->attr_version && size < inode->i_size &&
703 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700704 fi->attr_version = ++fc->attr_version;
705 i_size_write(inode, size);
706 }
707 spin_unlock(&fc->lock);
708}
709
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710static void fuse_short_read(struct fuse_req *req, struct inode *inode,
711 u64 attr_ver)
712{
713 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400714 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400715
Pavel Emelyanov83732002013-10-10 17:10:46 +0400716 if (fc->writeback_cache) {
717 /*
718 * A hole in a file. Some data after the hole are in page cache,
719 * but have not reached the client fs yet. So, the hole is not
720 * present there.
721 */
722 int i;
723 int start_idx = num_read >> PAGE_CACHE_SHIFT;
724 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
725
726 for (i = start_idx; i < req->num_pages; i++) {
727 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
728 off = 0;
729 }
730 } else {
731 loff_t pos = page_offset(req->pages[0]) + num_read;
732 fuse_read_update_size(inode, pos, attr_ver);
733 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400734}
735
Maxim Patlasov482fce52013-10-10 17:11:25 +0400736static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400738 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700739 struct inode *inode = page->mapping->host;
740 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800741 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700742 size_t num_read;
743 loff_t pos = page_offset(page);
744 size_t count = PAGE_CACHE_SIZE;
745 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800746 int err;
747
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700748 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300749 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700750 * page-cache page, so make sure we read a properly synced
751 * page.
752 */
753 fuse_wait_on_page_writeback(inode, page->index);
754
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400755 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700756 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400757 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700758
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700759 attr_ver = fuse_get_attr_version(fc);
760
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700761 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200762 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700763 req->num_pages = 1;
764 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400765 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400766 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700767 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700768
769 if (!err) {
770 /*
771 * Short read means EOF. If file size is larger, truncate it
772 */
773 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400774 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700775
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700776 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700777 }
778
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400779 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400780
781 return err;
782}
783
784static int fuse_readpage(struct file *file, struct page *page)
785{
786 struct inode *inode = page->mapping->host;
787 int err;
788
789 err = -EIO;
790 if (is_bad_inode(inode))
791 goto out;
792
793 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800794 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700795 out:
796 unlock_page(page);
797 return err;
798}
799
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800800static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700801{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800802 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700803 size_t count = req->misc.read.in.size;
804 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200805 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800806
Miklos Szeredice534fb2010-05-25 15:06:07 +0200807 for (i = 0; mapping == NULL && i < req->num_pages; i++)
808 mapping = req->pages[i]->mapping;
809
810 if (mapping) {
811 struct inode *inode = mapping->host;
812
813 /*
814 * Short read means EOF. If file size is larger, truncate it
815 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400816 if (!req->out.h.error && num_read < count)
817 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200818
Andrew Gallagher451418f2013-11-05 03:55:43 -0800819 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700820 }
821
Miklos Szeredidb50b962005-09-09 13:10:33 -0700822 for (i = 0; i < req->num_pages; i++) {
823 struct page *page = req->pages[i];
824 if (!req->out.h.error)
825 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800826 else
827 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700828 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200829 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700830 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700831 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100832 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800833}
834
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200835static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800836{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200837 struct fuse_file *ff = file->private_data;
838 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800839 loff_t pos = page_offset(req->pages[0]);
840 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200841
842 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800843 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200844 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200845 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700846 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800847 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700848 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800849 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100850 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800851 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100852 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800853 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100854 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800855 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856}
857
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700858struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700859 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800860 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400862 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863};
864
865static int fuse_readpages_fill(void *_data, struct page *page)
866{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700867 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700868 struct fuse_req *req = data->req;
869 struct inode *inode = data->inode;
870 struct fuse_conn *fc = get_fuse_conn(inode);
871
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700872 fuse_wait_on_page_writeback(inode, page->index);
873
Miklos Szeredidb50b962005-09-09 13:10:33 -0700874 if (req->num_pages &&
875 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
876 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
877 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400878 int nr_alloc = min_t(unsigned, data->nr_pages,
879 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200880 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400881 if (fc->async_read)
882 req = fuse_get_req_for_background(fc, nr_alloc);
883 else
884 req = fuse_get_req(fc, nr_alloc);
885
886 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700887 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700888 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700889 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700891 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400892
893 if (WARN_ON(req->num_pages >= req->max_pages)) {
894 fuse_put_request(fc, req);
895 return -EIO;
896 }
897
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200898 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400900 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100901 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400902 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700903 return 0;
904}
905
906static int fuse_readpages(struct file *file, struct address_space *mapping,
907 struct list_head *pages, unsigned nr_pages)
908{
909 struct inode *inode = mapping->host;
910 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700911 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700912 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400913 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800914
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700915 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800916 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800917 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800918
Miklos Szeredia6643092007-11-28 16:22:00 -0800919 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700920 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400921 if (fc->async_read)
922 data.req = fuse_get_req_for_background(fc, nr_alloc);
923 else
924 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400925 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700926 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700927 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800928 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700929
930 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700931 if (!err) {
932 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200933 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700934 else
935 fuse_put_request(fc, data.req);
936 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800937out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700938 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700939}
940
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
942 unsigned long nr_segs, loff_t pos)
943{
944 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400945 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800946
Brian Fostera8894272012-07-16 15:23:50 -0400947 /*
948 * In auto invalidate mode, always update attributes on read.
949 * Otherwise, only update if we attempt to read past EOF (to ensure
950 * i_size is up to date).
951 */
952 if (fc->auto_inval_data ||
953 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800954 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800955 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
956 if (err)
957 return err;
958 }
959
960 return generic_file_aio_read(iocb, iov, nr_segs, pos);
961}
962
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200963static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200964 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700965{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700966 struct fuse_write_in *inarg = &req->misc.write.in;
967 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700968
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700969 inarg->fh = ff->fh;
970 inarg->offset = pos;
971 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700972 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200973 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700974 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200975 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700976 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
977 else
978 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700979 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700980 req->in.args[1].size = count;
981 req->out.numargs = 1;
982 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700983 req->out.args[0].value = outarg;
984}
985
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400986static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200987 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700988{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400989 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200990 struct fuse_file *ff = file->private_data;
991 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200992 struct fuse_write_in *inarg = &req->misc.write.in;
993
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200994 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200995 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700996 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700997 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
998 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
999 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001000
1001 if (io->async)
1002 return fuse_async_req_send(fc, req, count, io);
1003
Tejun Heob93f8582008-11-26 12:03:55 +01001004 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001005 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001006}
1007
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001008bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001009{
1010 struct fuse_conn *fc = get_fuse_conn(inode);
1011 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001012 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001013
1014 spin_lock(&fc->lock);
1015 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001016 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001017 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001018 ret = true;
1019 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001020 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001021
1022 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001023}
1024
Nick Pigginea9b9902008-04-30 00:54:42 -07001025static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1026 struct inode *inode, loff_t pos,
1027 size_t count)
1028{
1029 size_t res;
1030 unsigned offset;
1031 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001032 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001033
1034 for (i = 0; i < req->num_pages; i++)
1035 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1036
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001037 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001038
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001039 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001040 count = res;
1041 for (i = 0; i < req->num_pages; i++) {
1042 struct page *page = req->pages[i];
1043
1044 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1045 SetPageUptodate(page);
1046
1047 if (count > PAGE_CACHE_SIZE - offset)
1048 count -= PAGE_CACHE_SIZE - offset;
1049 else
1050 count = 0;
1051 offset = 0;
1052
1053 unlock_page(page);
1054 page_cache_release(page);
1055 }
1056
1057 return res;
1058}
1059
1060static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1061 struct address_space *mapping,
1062 struct iov_iter *ii, loff_t pos)
1063{
1064 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1065 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1066 size_t count = 0;
1067 int err;
1068
Miklos Szeredif4975c62009-04-02 14:25:34 +02001069 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001070 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001071
1072 do {
1073 size_t tmp;
1074 struct page *page;
1075 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1076 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1077 iov_iter_count(ii));
1078
1079 bytes = min_t(size_t, bytes, fc->max_write - count);
1080
1081 again:
1082 err = -EFAULT;
1083 if (iov_iter_fault_in_readable(ii, bytes))
1084 break;
1085
1086 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001087 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001088 if (!page)
1089 break;
1090
anfei zhou931e80e2010-02-02 13:44:02 -08001091 if (mapping_writably_mapped(mapping))
1092 flush_dcache_page(page);
1093
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 flush_dcache_page(page);
1096
Johannes Weiner478e0842011-07-25 22:35:35 +02001097 mark_page_accessed(page);
1098
Nick Pigginea9b9902008-04-30 00:54:42 -07001099 if (!tmp) {
1100 unlock_page(page);
1101 page_cache_release(page);
1102 bytes = min(bytes, iov_iter_single_seg_count(ii));
1103 goto again;
1104 }
1105
1106 err = 0;
1107 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001108 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 req->num_pages++;
1110
1111 iov_iter_advance(ii, tmp);
1112 count += tmp;
1113 pos += tmp;
1114 offset += tmp;
1115 if (offset == PAGE_CACHE_SIZE)
1116 offset = 0;
1117
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001118 if (!fc->big_writes)
1119 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001120 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001121 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001122
1123 return count > 0 ? count : err;
1124}
1125
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001126static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1127{
1128 return min_t(unsigned,
1129 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1130 (pos >> PAGE_CACHE_SHIFT) + 1,
1131 FUSE_MAX_PAGES_PER_REQ);
1132}
1133
Nick Pigginea9b9902008-04-30 00:54:42 -07001134static ssize_t fuse_perform_write(struct file *file,
1135 struct address_space *mapping,
1136 struct iov_iter *ii, loff_t pos)
1137{
1138 struct inode *inode = mapping->host;
1139 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001140 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001141 int err = 0;
1142 ssize_t res = 0;
1143
1144 if (is_bad_inode(inode))
1145 return -EIO;
1146
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001147 if (inode->i_size < pos + iov_iter_count(ii))
1148 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1149
Nick Pigginea9b9902008-04-30 00:54:42 -07001150 do {
1151 struct fuse_req *req;
1152 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001153 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001154
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001155 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001156 if (IS_ERR(req)) {
1157 err = PTR_ERR(req);
1158 break;
1159 }
1160
1161 count = fuse_fill_write_pages(req, mapping, ii, pos);
1162 if (count <= 0) {
1163 err = count;
1164 } else {
1165 size_t num_written;
1166
1167 num_written = fuse_send_write_pages(req, file, inode,
1168 pos, count);
1169 err = req->out.h.error;
1170 if (!err) {
1171 res += num_written;
1172 pos += num_written;
1173
1174 /* break out of the loop on short write */
1175 if (num_written != count)
1176 err = -EIO;
1177 }
1178 }
1179 fuse_put_request(fc, req);
1180 } while (!err && iov_iter_count(ii));
1181
1182 if (res > 0)
1183 fuse_write_update_size(inode, pos);
1184
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001185 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001186 fuse_invalidate_attr(inode);
1187
1188 return res > 0 ? res : err;
1189}
1190
1191static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1192 unsigned long nr_segs, loff_t pos)
1193{
1194 struct file *file = iocb->ki_filp;
1195 struct address_space *mapping = file->f_mapping;
1196 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001197 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001198 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001199 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001200 struct inode *inode = mapping->host;
1201 ssize_t err;
1202 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001203 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001204
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001205 if (get_fuse_conn(inode)->writeback_cache) {
1206 /* Update size (EOF optimization) and mode (SUID clearing) */
1207 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1208 if (err)
1209 return err;
1210
1211 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1212 }
1213
Nick Pigginea9b9902008-04-30 00:54:42 -07001214 WARN_ON(iocb->ki_pos != pos);
1215
Anand Avati4273b792012-02-17 12:46:25 -05001216 ocount = 0;
1217 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001218 if (err)
1219 return err;
1220
Anand Avati4273b792012-02-17 12:46:25 -05001221 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001222 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001223
1224 /* We can write back this queue in page reclaim */
1225 current->backing_dev_info = mapping->backing_dev_info;
1226
1227 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1228 if (err)
1229 goto out;
1230
1231 if (count == 0)
1232 goto out;
1233
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001234 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001235 if (err)
1236 goto out;
1237
Josef Bacikc3b2da32012-03-26 09:59:21 -04001238 err = file_update_time(file);
1239 if (err)
1240 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001241
Anand Avati4273b792012-02-17 12:46:25 -05001242 if (file->f_flags & O_DIRECT) {
Al Viro5cb6c6c2014-02-11 20:58:20 -05001243 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
Anand Avati4273b792012-02-17 12:46:25 -05001244 count, ocount);
1245 if (written < 0 || written == count)
1246 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001247
Anand Avati4273b792012-02-17 12:46:25 -05001248 pos += written;
1249 count -= written;
1250
1251 iov_iter_init(&i, iov, nr_segs, count, written);
1252 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1253 if (written_buffered < 0) {
1254 err = written_buffered;
1255 goto out;
1256 }
1257 endbyte = pos + written_buffered - 1;
1258
1259 err = filemap_write_and_wait_range(file->f_mapping, pos,
1260 endbyte);
1261 if (err)
1262 goto out;
1263
1264 invalidate_mapping_pages(file->f_mapping,
1265 pos >> PAGE_CACHE_SHIFT,
1266 endbyte >> PAGE_CACHE_SHIFT);
1267
1268 written += written_buffered;
1269 iocb->ki_pos = pos + written_buffered;
1270 } else {
1271 iov_iter_init(&i, iov, nr_segs, count, 0);
1272 written = fuse_perform_write(file, mapping, &i, pos);
1273 if (written >= 0)
1274 iocb->ki_pos = pos + written;
1275 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001276out:
1277 current->backing_dev_info = NULL;
1278 mutex_unlock(&inode->i_mutex);
1279
1280 return written ? written : err;
1281}
1282
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001283static inline void fuse_page_descs_length_init(struct fuse_req *req,
1284 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001285{
1286 int i;
1287
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001289 req->page_descs[i].length = PAGE_SIZE -
1290 req->page_descs[i].offset;
1291}
1292
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001293static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1294{
1295 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1296}
1297
1298static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1299 size_t max_size)
1300{
1301 return min(iov_iter_single_seg_count(ii), max_size);
1302}
1303
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001304static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001305 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001306{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001307 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001308
Miklos Szeredif4975c62009-04-02 14:25:34 +02001309 /* Special case for kernel I/O: can copy directly into the buffer */
1310 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001311 unsigned long user_addr = fuse_get_user_addr(ii);
1312 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1313
Miklos Szeredif4975c62009-04-02 14:25:34 +02001314 if (write)
1315 req->in.args[1].value = (void *) user_addr;
1316 else
1317 req->out.args[0].value = (void *) user_addr;
1318
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001319 iov_iter_advance(ii, frag_size);
1320 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001321 return 0;
1322 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001323
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001324 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001325 unsigned npages;
1326 unsigned long user_addr = fuse_get_user_addr(ii);
1327 unsigned offset = user_addr & ~PAGE_MASK;
1328 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1329 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001330
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001331 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001332 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1333
1334 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1335 npages = clamp(npages, 1U, n);
1336
1337 ret = get_user_pages_fast(user_addr, npages, !write,
1338 &req->pages[req->num_pages]);
1339 if (ret < 0)
1340 return ret;
1341
1342 npages = ret;
1343 frag_size = min_t(size_t, frag_size,
1344 (npages << PAGE_SHIFT) - offset);
1345 iov_iter_advance(ii, frag_size);
1346
1347 req->page_descs[req->num_pages].offset = offset;
1348 fuse_page_descs_length_init(req, req->num_pages, npages);
1349
1350 req->num_pages += npages;
1351 req->page_descs[req->num_pages - 1].length -=
1352 (npages << PAGE_SHIFT) - offset - frag_size;
1353
1354 nbytes += frag_size;
1355 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001356
1357 if (write)
1358 req->in.argpages = 1;
1359 else
1360 req->out.argpages = 1;
1361
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001362 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001363
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 return 0;
1365}
1366
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001367static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1368{
1369 struct iov_iter ii = *ii_p;
1370 int npages = 0;
1371
1372 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1373 unsigned long user_addr = fuse_get_user_addr(&ii);
1374 unsigned offset = user_addr & ~PAGE_MASK;
1375 size_t frag_size = iov_iter_single_seg_count(&ii);
1376
1377 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1378 iov_iter_advance(&ii, frag_size);
1379 }
1380
1381 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1382}
1383
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001384ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001385 unsigned long nr_segs, size_t count, loff_t *ppos,
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001386 int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001388 int write = flags & FUSE_DIO_WRITE;
1389 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001390 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001391 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001392 struct fuse_file *ff = file->private_data;
1393 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001394 size_t nmax = write ? fc->max_write : fc->max_read;
1395 loff_t pos = *ppos;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001396 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1397 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001399 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001400 struct iov_iter ii;
1401
1402 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001403
Brian Fosterde82b922013-05-14 11:25:39 -04001404 if (io->async)
1405 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1406 else
1407 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001408 if (IS_ERR(req))
1409 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001410
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001411 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1412 if (!write)
1413 mutex_lock(&inode->i_mutex);
1414 fuse_sync_writes(inode);
1415 if (!write)
1416 mutex_unlock(&inode->i_mutex);
1417 }
1418
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001420 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001421 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001422 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001423 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424 if (err) {
1425 res = err;
1426 break;
1427 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001428
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001430 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001431 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001432 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001433
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001434 if (!io->async)
1435 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436 if (req->out.h.error) {
1437 if (!res)
1438 res = req->out.h.error;
1439 break;
1440 } else if (nres > nbytes) {
1441 res = -EIO;
1442 break;
1443 }
1444 count -= nres;
1445 res += nres;
1446 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001447 if (nres != nbytes)
1448 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001449 if (count) {
1450 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001451 if (io->async)
1452 req = fuse_get_req_for_background(fc,
1453 fuse_iter_npages(&ii));
1454 else
1455 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001456 if (IS_ERR(req))
1457 break;
1458 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001459 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001460 if (!IS_ERR(req))
1461 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001462 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001463 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001464
1465 return res;
1466}
Tejun Heo08cbf542009-04-14 10:54:53 +09001467EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001468
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001469static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1470 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001471 unsigned long nr_segs, loff_t *ppos,
1472 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001473{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001474 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001475 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001476 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001477
1478 if (is_bad_inode(inode))
1479 return -EIO;
1480
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001481 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001482
1483 fuse_invalidate_attr(inode);
1484
1485 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001486}
1487
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001488static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1489 size_t count, loff_t *ppos)
1490{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001491 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001492 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001493 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001494}
1495
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001496static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1497 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001498 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001499{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001500 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001501 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001502 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001503 ssize_t res;
1504
1505 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001506 if (!res)
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001507 res = fuse_direct_io(io, iov, nr_segs, count, ppos,
1508 FUSE_DIO_WRITE);
Anand Avati4273b792012-02-17 12:46:25 -05001509
1510 fuse_invalidate_attr(inode);
1511
1512 return res;
1513}
1514
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001515static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1516 size_t count, loff_t *ppos)
1517{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001518 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001519 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001520 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001521 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001522
1523 if (is_bad_inode(inode))
1524 return -EIO;
1525
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001526 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001527 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001528 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001529 if (res > 0)
1530 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001531 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001532
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001533 return res;
1534}
1535
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001536static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001537{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001538 int i;
1539
1540 for (i = 0; i < req->num_pages; i++)
1541 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001542
1543 if (req->ff)
1544 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001545}
1546
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001548{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001549 struct inode *inode = req->inode;
1550 struct fuse_inode *fi = get_fuse_inode(inode);
1551 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001552 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001553
1554 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001555 for (i = 0; i < req->num_pages; i++) {
1556 dec_bdi_stat(bdi, BDI_WRITEBACK);
1557 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1558 bdi_writeout_inc(bdi);
1559 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001560 wake_up(&fi->page_waitq);
1561}
1562
1563/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001564static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1565 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001566__releases(fc->lock)
1567__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001568{
1569 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001570 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001571 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001572
1573 if (!fc->connected)
1574 goto out_free;
1575
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001576 if (inarg->offset + data_size <= size) {
1577 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001578 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001579 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001580 } else {
1581 /* Got truncated off completely */
1582 goto out_free;
1583 }
1584
1585 req->in.args[1].size = inarg->size;
1586 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001587 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001588 return;
1589
1590 out_free:
1591 fuse_writepage_finish(fc, req);
1592 spin_unlock(&fc->lock);
1593 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001594 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001595 spin_lock(&fc->lock);
1596}
1597
1598/*
1599 * If fi->writectr is positive (no truncate or fsync going on) send
1600 * all queued writepage requests.
1601 *
1602 * Called with fc->lock
1603 */
1604void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001605__releases(fc->lock)
1606__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001607{
1608 struct fuse_conn *fc = get_fuse_conn(inode);
1609 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001610 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001611 struct fuse_req *req;
1612
1613 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1614 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1615 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001616 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001617 }
1618}
1619
1620static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1621{
1622 struct inode *inode = req->inode;
1623 struct fuse_inode *fi = get_fuse_inode(inode);
1624
1625 mapping_set_error(inode->i_mapping, req->out.h.error);
1626 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001627 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001628 struct fuse_conn *fc = get_fuse_conn(inode);
1629 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001630 struct fuse_req *next = req->misc.write.next;
1631 req->misc.write.next = next->misc.write.next;
1632 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001633 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001634 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001635
1636 /*
1637 * Skip fuse_flush_writepages() to make it easy to crop requests
1638 * based on primary request size.
1639 *
1640 * 1st case (trivial): there are no concurrent activities using
1641 * fuse_set/release_nowrite. Then we're on safe side because
1642 * fuse_flush_writepages() would call fuse_send_writepage()
1643 * anyway.
1644 *
1645 * 2nd case: someone called fuse_set_nowrite and it is waiting
1646 * now for completion of all in-flight requests. This happens
1647 * rarely and no more than once per page, so this should be
1648 * okay.
1649 *
1650 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1651 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1652 * that fuse_set_nowrite returned implies that all in-flight
1653 * requests were completed along with all of their secondary
1654 * requests. Further primary requests are blocked by negative
1655 * writectr. Hence there cannot be any in-flight requests and
1656 * no invocations of fuse_writepage_end() while we're in
1657 * fuse_set_nowrite..fuse_release_nowrite section.
1658 */
1659 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001660 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001661 fi->writectr--;
1662 fuse_writepage_finish(fc, req);
1663 spin_unlock(&fc->lock);
1664 fuse_writepage_free(fc, req);
1665}
1666
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001667static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1668 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001669{
Miklos Szeredi72523422013-10-01 16:44:52 +02001670 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001671
1672 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001673 if (!WARN_ON(list_empty(&fi->write_files))) {
1674 ff = list_entry(fi->write_files.next, struct fuse_file,
1675 write_entry);
1676 fuse_file_get(ff);
1677 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001678 spin_unlock(&fc->lock);
1679
1680 return ff;
1681}
1682
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683static int fuse_writepage_locked(struct page *page)
1684{
1685 struct address_space *mapping = page->mapping;
1686 struct inode *inode = mapping->host;
1687 struct fuse_conn *fc = get_fuse_conn(inode);
1688 struct fuse_inode *fi = get_fuse_inode(inode);
1689 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001691 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001692
1693 set_page_writeback(page);
1694
Maxim Patlasov4250c062012-10-26 19:48:07 +04001695 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001696 if (!req)
1697 goto err;
1698
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001699 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1701 if (!tmp_page)
1702 goto err_free;
1703
Miklos Szeredi72523422013-10-01 16:44:52 +02001704 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001705 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001706 if (!req->ff)
1707 goto err_free;
1708
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001709 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001710
1711 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001712 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001713 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001714 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001715 req->num_pages = 1;
1716 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001717 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001718 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001719 req->end = fuse_writepage_end;
1720 req->inode = inode;
1721
1722 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1723 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001724
1725 spin_lock(&fc->lock);
1726 list_add(&req->writepages_entry, &fi->writepages);
1727 list_add_tail(&req->list, &fi->queued_writes);
1728 fuse_flush_writepages(inode);
1729 spin_unlock(&fc->lock);
1730
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001731 end_page_writeback(page);
1732
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001733 return 0;
1734
1735err_free:
1736 fuse_request_free(req);
1737err:
1738 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001739 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001740}
1741
1742static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1743{
1744 int err;
1745
Miklos Szerediff17be02013-10-01 16:44:53 +02001746 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1747 /*
1748 * ->writepages() should be called for sync() and friends. We
1749 * should only get here on direct reclaim and then we are
1750 * allowed to skip a page which is already in flight
1751 */
1752 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1753
1754 redirty_page_for_writepage(wbc, page);
1755 return 0;
1756 }
1757
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001758 err = fuse_writepage_locked(page);
1759 unlock_page(page);
1760
1761 return err;
1762}
1763
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001764struct fuse_fill_wb_data {
1765 struct fuse_req *req;
1766 struct fuse_file *ff;
1767 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001768 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001769};
1770
1771static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1772{
1773 struct fuse_req *req = data->req;
1774 struct inode *inode = data->inode;
1775 struct fuse_conn *fc = get_fuse_conn(inode);
1776 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001777 int num_pages = req->num_pages;
1778 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001779
1780 req->ff = fuse_file_get(data->ff);
1781 spin_lock(&fc->lock);
1782 list_add_tail(&req->list, &fi->queued_writes);
1783 fuse_flush_writepages(inode);
1784 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001785
1786 for (i = 0; i < num_pages; i++)
1787 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001788}
1789
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001790static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1791 struct page *page)
1792{
1793 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1794 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1795 struct fuse_req *tmp;
1796 struct fuse_req *old_req;
1797 bool found = false;
1798 pgoff_t curr_index;
1799
1800 BUG_ON(new_req->num_pages != 0);
1801
1802 spin_lock(&fc->lock);
1803 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001804 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1805 BUG_ON(old_req->inode != new_req->inode);
1806 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1807 if (curr_index <= page->index &&
1808 page->index < curr_index + old_req->num_pages) {
1809 found = true;
1810 break;
1811 }
1812 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001813 if (!found) {
1814 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001815 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001816 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001817
Maxim Patlasovf6011082013-10-02 15:01:07 +04001818 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001819 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1820 BUG_ON(tmp->inode != new_req->inode);
1821 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1822 if (tmp->num_pages == 1 &&
1823 curr_index == page->index) {
1824 old_req = tmp;
1825 }
1826 }
1827
1828 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1829 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001830 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1831
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001832 copy_highpage(old_req->pages[0], page);
1833 spin_unlock(&fc->lock);
1834
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001835 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001836 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001837 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001838 fuse_writepage_free(fc, new_req);
1839 fuse_request_free(new_req);
1840 goto out;
1841 } else {
1842 new_req->misc.write.next = old_req->misc.write.next;
1843 old_req->misc.write.next = new_req;
1844 }
1845out_unlock:
1846 spin_unlock(&fc->lock);
1847out:
1848 return found;
1849}
1850
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001851static int fuse_writepages_fill(struct page *page,
1852 struct writeback_control *wbc, void *_data)
1853{
1854 struct fuse_fill_wb_data *data = _data;
1855 struct fuse_req *req = data->req;
1856 struct inode *inode = data->inode;
1857 struct fuse_conn *fc = get_fuse_conn(inode);
1858 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001859 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001860 int err;
1861
1862 if (!data->ff) {
1863 err = -EIO;
1864 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1865 if (!data->ff)
1866 goto out_unlock;
1867 }
1868
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001869 /*
1870 * Being under writeback is unlikely but possible. For example direct
1871 * read to an mmaped fuse file will set the page dirty twice; once when
1872 * the pages are faulted with get_user_pages(), and then after the read
1873 * completed.
1874 */
1875 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001876
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001877 if (req && req->num_pages &&
1878 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1879 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1880 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1881 fuse_writepages_send(data);
1882 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001883 }
1884 err = -ENOMEM;
1885 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1886 if (!tmp_page)
1887 goto out_unlock;
1888
1889 /*
1890 * The page must not be redirtied until the writeout is completed
1891 * (i.e. userspace has sent a reply to the write request). Otherwise
1892 * there could be more than one temporary page instance for each real
1893 * page.
1894 *
1895 * This is ensured by holding the page lock in page_mkwrite() while
1896 * checking fuse_page_is_writeback(). We already hold the page lock
1897 * since clear_page_dirty_for_io() and keep it held until we add the
1898 * request to the fi->writepages list and increment req->num_pages.
1899 * After this fuse_page_is_writeback() will indicate that the page is
1900 * under writeback, so we can release the page lock.
1901 */
1902 if (data->req == NULL) {
1903 struct fuse_inode *fi = get_fuse_inode(inode);
1904
1905 err = -ENOMEM;
1906 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1907 if (!req) {
1908 __free_page(tmp_page);
1909 goto out_unlock;
1910 }
1911
1912 fuse_write_fill(req, data->ff, page_offset(page), 0);
1913 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001914 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001915 req->in.argpages = 1;
1916 req->background = 1;
1917 req->num_pages = 0;
1918 req->end = fuse_writepage_end;
1919 req->inode = inode;
1920
1921 spin_lock(&fc->lock);
1922 list_add(&req->writepages_entry, &fi->writepages);
1923 spin_unlock(&fc->lock);
1924
1925 data->req = req;
1926 }
1927 set_page_writeback(page);
1928
1929 copy_highpage(tmp_page, page);
1930 req->pages[req->num_pages] = tmp_page;
1931 req->page_descs[req->num_pages].offset = 0;
1932 req->page_descs[req->num_pages].length = PAGE_SIZE;
1933
1934 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1935 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001936
1937 err = 0;
1938 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1939 end_page_writeback(page);
1940 data->req = NULL;
1941 goto out_unlock;
1942 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001943 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001944
1945 /*
1946 * Protected by fc->lock against concurrent access by
1947 * fuse_page_is_writeback().
1948 */
1949 spin_lock(&fc->lock);
1950 req->num_pages++;
1951 spin_unlock(&fc->lock);
1952
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001953out_unlock:
1954 unlock_page(page);
1955
1956 return err;
1957}
1958
1959static int fuse_writepages(struct address_space *mapping,
1960 struct writeback_control *wbc)
1961{
1962 struct inode *inode = mapping->host;
1963 struct fuse_fill_wb_data data;
1964 int err;
1965
1966 err = -EIO;
1967 if (is_bad_inode(inode))
1968 goto out;
1969
1970 data.inode = inode;
1971 data.req = NULL;
1972 data.ff = NULL;
1973
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001974 err = -ENOMEM;
1975 data.orig_pages = kzalloc(sizeof(struct page *) *
1976 FUSE_MAX_PAGES_PER_REQ,
1977 GFP_NOFS);
1978 if (!data.orig_pages)
1979 goto out;
1980
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001981 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1982 if (data.req) {
1983 /* Ignore errors if we can write at least one page */
1984 BUG_ON(!data.req->num_pages);
1985 fuse_writepages_send(&data);
1986 err = 0;
1987 }
1988 if (data.ff)
1989 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001990
1991 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001992out:
1993 return err;
1994}
1995
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001996/*
1997 * It's worthy to make sure that space is reserved on disk for the write,
1998 * but how to implement it without killing performance need more thinking.
1999 */
2000static int fuse_write_begin(struct file *file, struct address_space *mapping,
2001 loff_t pos, unsigned len, unsigned flags,
2002 struct page **pagep, void **fsdata)
2003{
2004 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2005 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
2006 struct page *page;
2007 loff_t fsize;
2008 int err = -ENOMEM;
2009
2010 WARN_ON(!fc->writeback_cache);
2011
2012 page = grab_cache_page_write_begin(mapping, index, flags);
2013 if (!page)
2014 goto error;
2015
2016 fuse_wait_on_page_writeback(mapping->host, page->index);
2017
2018 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
2019 goto success;
2020 /*
2021 * Check if the start this page comes after the end of file, in which
2022 * case the readpage can be optimized away.
2023 */
2024 fsize = i_size_read(mapping->host);
2025 if (fsize <= (pos & PAGE_CACHE_MASK)) {
2026 size_t off = pos & ~PAGE_CACHE_MASK;
2027 if (off)
2028 zero_user_segment(page, 0, off);
2029 goto success;
2030 }
2031 err = fuse_do_readpage(file, page);
2032 if (err)
2033 goto cleanup;
2034success:
2035 *pagep = page;
2036 return 0;
2037
2038cleanup:
2039 unlock_page(page);
2040 page_cache_release(page);
2041error:
2042 return err;
2043}
2044
2045static int fuse_write_end(struct file *file, struct address_space *mapping,
2046 loff_t pos, unsigned len, unsigned copied,
2047 struct page *page, void *fsdata)
2048{
2049 struct inode *inode = page->mapping->host;
2050
2051 if (!PageUptodate(page)) {
2052 /* Zero any unwritten bytes at the end of the page */
2053 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2054 if (endoff)
2055 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2056 SetPageUptodate(page);
2057 }
2058
2059 fuse_write_update_size(inode, pos + copied);
2060 set_page_dirty(page);
2061 unlock_page(page);
2062 page_cache_release(page);
2063
2064 return copied;
2065}
2066
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002067static int fuse_launder_page(struct page *page)
2068{
2069 int err = 0;
2070 if (clear_page_dirty_for_io(page)) {
2071 struct inode *inode = page->mapping->host;
2072 err = fuse_writepage_locked(page);
2073 if (!err)
2074 fuse_wait_on_page_writeback(inode, page->index);
2075 }
2076 return err;
2077}
2078
2079/*
2080 * Write back dirty pages now, because there may not be any suitable
2081 * open files later
2082 */
2083static void fuse_vma_close(struct vm_area_struct *vma)
2084{
2085 filemap_write_and_wait(vma->vm_file->f_mapping);
2086}
2087
2088/*
2089 * Wait for writeback against this page to complete before allowing it
2090 * to be marked dirty again, and hence written back again, possibly
2091 * before the previous writepage completed.
2092 *
2093 * Block here, instead of in ->writepage(), so that the userspace fs
2094 * can only block processes actually operating on the filesystem.
2095 *
2096 * Otherwise unprivileged userspace fs would be able to block
2097 * unrelated:
2098 *
2099 * - page migration
2100 * - sync(2)
2101 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2102 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002103static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002104{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002105 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002106 struct inode *inode = file_inode(vma->vm_file);
2107
2108 file_update_time(vma->vm_file);
2109 lock_page(page);
2110 if (page->mapping != inode->i_mapping) {
2111 unlock_page(page);
2112 return VM_FAULT_NOPAGE;
2113 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002114
2115 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002116 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002117}
2118
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002119static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002120 .close = fuse_vma_close,
2121 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002122 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002123 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002124 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002125};
2126
2127static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2128{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002129 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2130 fuse_link_write_file(file);
2131
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002132 file_accessed(file);
2133 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002134 return 0;
2135}
2136
Miklos Szeredifc280c92009-04-02 14:25:35 +02002137static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2138{
2139 /* Can't provide the coherency needed for MAP_SHARED */
2140 if (vma->vm_flags & VM_MAYSHARE)
2141 return -ENODEV;
2142
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002143 invalidate_inode_pages2(file->f_mapping);
2144
Miklos Szeredifc280c92009-04-02 14:25:35 +02002145 return generic_file_mmap(file, vma);
2146}
2147
Miklos Szeredi71421252006-06-25 05:48:52 -07002148static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2149 struct file_lock *fl)
2150{
2151 switch (ffl->type) {
2152 case F_UNLCK:
2153 break;
2154
2155 case F_RDLCK:
2156 case F_WRLCK:
2157 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2158 ffl->end < ffl->start)
2159 return -EIO;
2160
2161 fl->fl_start = ffl->start;
2162 fl->fl_end = ffl->end;
2163 fl->fl_pid = ffl->pid;
2164 break;
2165
2166 default:
2167 return -EIO;
2168 }
2169 fl->fl_type = ffl->type;
2170 return 0;
2171}
2172
2173static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002174 const struct file_lock *fl, int opcode, pid_t pid,
2175 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002176{
Al Viro6131ffa2013-02-27 16:59:05 -05002177 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002178 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002179 struct fuse_file *ff = file->private_data;
2180 struct fuse_lk_in *arg = &req->misc.lk_in;
2181
2182 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002183 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002184 arg->lk.start = fl->fl_start;
2185 arg->lk.end = fl->fl_end;
2186 arg->lk.type = fl->fl_type;
2187 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002188 if (flock)
2189 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002190 req->in.h.opcode = opcode;
2191 req->in.h.nodeid = get_node_id(inode);
2192 req->in.numargs = 1;
2193 req->in.args[0].size = sizeof(*arg);
2194 req->in.args[0].value = arg;
2195}
2196
2197static int fuse_getlk(struct file *file, struct file_lock *fl)
2198{
Al Viro6131ffa2013-02-27 16:59:05 -05002199 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 struct fuse_conn *fc = get_fuse_conn(inode);
2201 struct fuse_req *req;
2202 struct fuse_lk_out outarg;
2203 int err;
2204
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002205 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002206 if (IS_ERR(req))
2207 return PTR_ERR(req);
2208
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002209 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002210 req->out.numargs = 1;
2211 req->out.args[0].size = sizeof(outarg);
2212 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002213 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002214 err = req->out.h.error;
2215 fuse_put_request(fc, req);
2216 if (!err)
2217 err = convert_fuse_file_lock(&outarg.lk, fl);
2218
2219 return err;
2220}
2221
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002222static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002223{
Al Viro6131ffa2013-02-27 16:59:05 -05002224 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002225 struct fuse_conn *fc = get_fuse_conn(inode);
2226 struct fuse_req *req;
2227 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2228 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2229 int err;
2230
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002231 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002232 /* NLM needs asynchronous locks, which we don't support yet */
2233 return -ENOLCK;
2234 }
2235
Miklos Szeredi71421252006-06-25 05:48:52 -07002236 /* Unlock on close is handled by the flush method */
2237 if (fl->fl_flags & FL_CLOSE)
2238 return 0;
2239
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002240 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002241 if (IS_ERR(req))
2242 return PTR_ERR(req);
2243
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002244 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002245 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002246 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002247 /* locking is restartable */
2248 if (err == -EINTR)
2249 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002250 fuse_put_request(fc, req);
2251 return err;
2252}
2253
2254static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2255{
Al Viro6131ffa2013-02-27 16:59:05 -05002256 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002257 struct fuse_conn *fc = get_fuse_conn(inode);
2258 int err;
2259
Miklos Szeredi48e90762008-07-25 01:49:02 -07002260 if (cmd == F_CANCELLK) {
2261 err = 0;
2262 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002263 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002264 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002265 err = 0;
2266 } else
2267 err = fuse_getlk(file, fl);
2268 } else {
2269 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002270 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002271 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002272 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002273 }
2274 return err;
2275}
2276
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002277static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2278{
Al Viro6131ffa2013-02-27 16:59:05 -05002279 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002280 struct fuse_conn *fc = get_fuse_conn(inode);
2281 int err;
2282
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002283 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002284 err = flock_lock_file_wait(file, fl);
2285 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002286 struct fuse_file *ff = file->private_data;
2287
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002288 /* emulate flock with POSIX locks */
2289 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002290 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002291 err = fuse_setlk(file, fl, 1);
2292 }
2293
2294 return err;
2295}
2296
Miklos Szeredib2d22722006-12-06 20:35:51 -08002297static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2298{
2299 struct inode *inode = mapping->host;
2300 struct fuse_conn *fc = get_fuse_conn(inode);
2301 struct fuse_req *req;
2302 struct fuse_bmap_in inarg;
2303 struct fuse_bmap_out outarg;
2304 int err;
2305
2306 if (!inode->i_sb->s_bdev || fc->no_bmap)
2307 return 0;
2308
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002309 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002310 if (IS_ERR(req))
2311 return 0;
2312
2313 memset(&inarg, 0, sizeof(inarg));
2314 inarg.block = block;
2315 inarg.blocksize = inode->i_sb->s_blocksize;
2316 req->in.h.opcode = FUSE_BMAP;
2317 req->in.h.nodeid = get_node_id(inode);
2318 req->in.numargs = 1;
2319 req->in.args[0].size = sizeof(inarg);
2320 req->in.args[0].value = &inarg;
2321 req->out.numargs = 1;
2322 req->out.args[0].size = sizeof(outarg);
2323 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002324 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002325 err = req->out.h.error;
2326 fuse_put_request(fc, req);
2327 if (err == -ENOSYS)
2328 fc->no_bmap = 1;
2329
2330 return err ? 0 : outarg.block;
2331}
2332
Andrew Morton965c8e52012-12-17 15:59:39 -08002333static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002334{
2335 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002336 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002337
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002338 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002339 if (whence == SEEK_CUR || whence == SEEK_SET)
2340 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002341
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002342 mutex_lock(&inode->i_mutex);
2343 retval = fuse_update_attributes(inode, NULL, file, NULL);
2344 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002345 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002346 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002347
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002348 return retval;
2349}
2350
Tejun Heo59efec72008-11-26 12:03:55 +01002351static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2352 unsigned int nr_segs, size_t bytes, bool to_user)
2353{
2354 struct iov_iter ii;
2355 int page_idx = 0;
2356
2357 if (!bytes)
2358 return 0;
2359
2360 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2361
2362 while (iov_iter_count(&ii)) {
2363 struct page *page = pages[page_idx++];
2364 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002365 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002366
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002367 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002368
2369 while (todo) {
2370 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2371 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2372 size_t copy = min(todo, iov_len);
2373 size_t left;
2374
2375 if (!to_user)
2376 left = copy_from_user(kaddr, uaddr, copy);
2377 else
2378 left = copy_to_user(uaddr, kaddr, copy);
2379
2380 if (unlikely(left))
2381 return -EFAULT;
2382
2383 iov_iter_advance(&ii, copy);
2384 todo -= copy;
2385 kaddr += copy;
2386 }
2387
Jens Axboe0bd87182009-11-03 11:40:44 +01002388 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002389 }
2390
2391 return 0;
2392}
2393
2394/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002395 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2396 * ABI was defined to be 'struct iovec' which is different on 32bit
2397 * and 64bit. Fortunately we can determine which structure the server
2398 * used from the size of the reply.
2399 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002400static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2401 size_t transferred, unsigned count,
2402 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002403{
2404#ifdef CONFIG_COMPAT
2405 if (count * sizeof(struct compat_iovec) == transferred) {
2406 struct compat_iovec *ciov = src;
2407 unsigned i;
2408
2409 /*
2410 * With this interface a 32bit server cannot support
2411 * non-compat (i.e. ones coming from 64bit apps) ioctl
2412 * requests
2413 */
2414 if (!is_compat)
2415 return -EINVAL;
2416
2417 for (i = 0; i < count; i++) {
2418 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2419 dst[i].iov_len = ciov[i].iov_len;
2420 }
2421 return 0;
2422 }
2423#endif
2424
2425 if (count * sizeof(struct iovec) != transferred)
2426 return -EIO;
2427
2428 memcpy(dst, src, transferred);
2429 return 0;
2430}
2431
Miklos Szeredi75727772010-11-30 16:39:27 +01002432/* Make sure iov_length() won't overflow */
2433static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2434{
2435 size_t n;
2436 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2437
Zach Brownfb6ccff2012-07-24 12:10:11 -07002438 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002439 if (iov->iov_len > (size_t) max)
2440 return -ENOMEM;
2441 max -= iov->iov_len;
2442 }
2443 return 0;
2444}
2445
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002446static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2447 void *src, size_t transferred, unsigned count,
2448 bool is_compat)
2449{
2450 unsigned i;
2451 struct fuse_ioctl_iovec *fiov = src;
2452
2453 if (fc->minor < 16) {
2454 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2455 count, is_compat);
2456 }
2457
2458 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2459 return -EIO;
2460
2461 for (i = 0; i < count; i++) {
2462 /* Did the server supply an inappropriate value? */
2463 if (fiov[i].base != (unsigned long) fiov[i].base ||
2464 fiov[i].len != (unsigned long) fiov[i].len)
2465 return -EIO;
2466
2467 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2468 dst[i].iov_len = (size_t) fiov[i].len;
2469
2470#ifdef CONFIG_COMPAT
2471 if (is_compat &&
2472 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2473 (compat_size_t) dst[i].iov_len != fiov[i].len))
2474 return -EIO;
2475#endif
2476 }
2477
2478 return 0;
2479}
2480
2481
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002482/*
Tejun Heo59efec72008-11-26 12:03:55 +01002483 * For ioctls, there is no generic way to determine how much memory
2484 * needs to be read and/or written. Furthermore, ioctls are allowed
2485 * to dereference the passed pointer, so the parameter requires deep
2486 * copying but FUSE has no idea whatsoever about what to copy in or
2487 * out.
2488 *
2489 * This is solved by allowing FUSE server to retry ioctl with
2490 * necessary in/out iovecs. Let's assume the ioctl implementation
2491 * needs to read in the following structure.
2492 *
2493 * struct a {
2494 * char *buf;
2495 * size_t buflen;
2496 * }
2497 *
2498 * On the first callout to FUSE server, inarg->in_size and
2499 * inarg->out_size will be NULL; then, the server completes the ioctl
2500 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2501 * the actual iov array to
2502 *
2503 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2504 *
2505 * which tells FUSE to copy in the requested area and retry the ioctl.
2506 * On the second round, the server has access to the structure and
2507 * from that it can tell what to look for next, so on the invocation,
2508 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2509 *
2510 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2511 * { .iov_base = a.buf, .iov_len = a.buflen } }
2512 *
2513 * FUSE will copy both struct a and the pointed buffer from the
2514 * process doing the ioctl and retry ioctl with both struct a and the
2515 * buffer.
2516 *
2517 * This time, FUSE server has everything it needs and completes ioctl
2518 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2519 *
2520 * Copying data out works the same way.
2521 *
2522 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2523 * automatically initializes in and out iovs by decoding @cmd with
2524 * _IOC_* macros and the server is not allowed to request RETRY. This
2525 * limits ioctl data transfers to well-formed ioctls and is the forced
2526 * behavior for all FUSE servers.
2527 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002528long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2529 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002530{
Tejun Heo59efec72008-11-26 12:03:55 +01002531 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002532 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002533 struct fuse_ioctl_in inarg = {
2534 .fh = ff->fh,
2535 .cmd = cmd,
2536 .arg = arg,
2537 .flags = flags
2538 };
2539 struct fuse_ioctl_out outarg;
2540 struct fuse_req *req = NULL;
2541 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002542 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002543 struct iovec *in_iov = NULL, *out_iov = NULL;
2544 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2545 size_t in_size, out_size, transferred;
2546 int err;
2547
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002548#if BITS_PER_LONG == 32
2549 inarg.flags |= FUSE_IOCTL_32BIT;
2550#else
2551 if (flags & FUSE_IOCTL_COMPAT)
2552 inarg.flags |= FUSE_IOCTL_32BIT;
2553#endif
2554
Tejun Heo59efec72008-11-26 12:03:55 +01002555 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002556 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002557
Tejun Heo59efec72008-11-26 12:03:55 +01002558 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002559 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002560 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002561 if (!pages || !iov_page)
2562 goto out;
2563
2564 /*
2565 * If restricted, initialize IO parameters as encoded in @cmd.
2566 * RETRY from server is not allowed.
2567 */
2568 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002569 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002570
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002571 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002572 iov->iov_len = _IOC_SIZE(cmd);
2573
2574 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2575 in_iov = iov;
2576 in_iovs = 1;
2577 }
2578
2579 if (_IOC_DIR(cmd) & _IOC_READ) {
2580 out_iov = iov;
2581 out_iovs = 1;
2582 }
2583 }
2584
2585 retry:
2586 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2587 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2588
2589 /*
2590 * Out data can be used either for actual out data or iovs,
2591 * make sure there always is at least one page.
2592 */
2593 out_size = max_t(size_t, out_size, PAGE_SIZE);
2594 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2595
2596 /* make sure there are enough buffer pages and init request with them */
2597 err = -ENOMEM;
2598 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2599 goto out;
2600 while (num_pages < max_pages) {
2601 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2602 if (!pages[num_pages])
2603 goto out;
2604 num_pages++;
2605 }
2606
Maxim Patlasov54b96672012-10-26 19:49:13 +04002607 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002608 if (IS_ERR(req)) {
2609 err = PTR_ERR(req);
2610 req = NULL;
2611 goto out;
2612 }
2613 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2614 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002615 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002616
2617 /* okay, let's send it to the client */
2618 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002619 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002620 req->in.numargs = 1;
2621 req->in.args[0].size = sizeof(inarg);
2622 req->in.args[0].value = &inarg;
2623 if (in_size) {
2624 req->in.numargs++;
2625 req->in.args[1].size = in_size;
2626 req->in.argpages = 1;
2627
2628 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2629 false);
2630 if (err)
2631 goto out;
2632 }
2633
2634 req->out.numargs = 2;
2635 req->out.args[0].size = sizeof(outarg);
2636 req->out.args[0].value = &outarg;
2637 req->out.args[1].size = out_size;
2638 req->out.argpages = 1;
2639 req->out.argvar = 1;
2640
Tejun Heob93f8582008-11-26 12:03:55 +01002641 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002642 err = req->out.h.error;
2643 transferred = req->out.args[1].size;
2644 fuse_put_request(fc, req);
2645 req = NULL;
2646 if (err)
2647 goto out;
2648
2649 /* did it ask for retry? */
2650 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002651 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002652
2653 /* no retry if in restricted mode */
2654 err = -EIO;
2655 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2656 goto out;
2657
2658 in_iovs = outarg.in_iovs;
2659 out_iovs = outarg.out_iovs;
2660
2661 /*
2662 * Make sure things are in boundary, separate checks
2663 * are to protect against overflow.
2664 */
2665 err = -ENOMEM;
2666 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2667 out_iovs > FUSE_IOCTL_MAX_IOV ||
2668 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2669 goto out;
2670
Cong Wang2408f6e2011-11-25 23:14:30 +08002671 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002672 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002673 transferred, in_iovs + out_iovs,
2674 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002675 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002676 if (err)
2677 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002678
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002679 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002680 out_iov = in_iov + in_iovs;
2681
Miklos Szeredi75727772010-11-30 16:39:27 +01002682 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2683 if (err)
2684 goto out;
2685
2686 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2687 if (err)
2688 goto out;
2689
Tejun Heo59efec72008-11-26 12:03:55 +01002690 goto retry;
2691 }
2692
2693 err = -EIO;
2694 if (transferred > inarg.out_size)
2695 goto out;
2696
2697 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2698 out:
2699 if (req)
2700 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002701 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002702 while (num_pages)
2703 __free_page(pages[--num_pages]);
2704 kfree(pages);
2705
2706 return err ? err : outarg.result;
2707}
Tejun Heo08cbf542009-04-14 10:54:53 +09002708EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002709
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002710long fuse_ioctl_common(struct file *file, unsigned int cmd,
2711 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002712{
Al Viro6131ffa2013-02-27 16:59:05 -05002713 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002714 struct fuse_conn *fc = get_fuse_conn(inode);
2715
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002716 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002717 return -EACCES;
2718
2719 if (is_bad_inode(inode))
2720 return -EIO;
2721
2722 return fuse_do_ioctl(file, cmd, arg, flags);
2723}
2724
Tejun Heo59efec72008-11-26 12:03:55 +01002725static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2726 unsigned long arg)
2727{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002728 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002729}
2730
2731static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2732 unsigned long arg)
2733{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002734 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002735}
2736
Tejun Heo95668a62008-11-26 12:03:55 +01002737/*
2738 * All files which have been polled are linked to RB tree
2739 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2740 * find the matching one.
2741 */
2742static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2743 struct rb_node **parent_out)
2744{
2745 struct rb_node **link = &fc->polled_files.rb_node;
2746 struct rb_node *last = NULL;
2747
2748 while (*link) {
2749 struct fuse_file *ff;
2750
2751 last = *link;
2752 ff = rb_entry(last, struct fuse_file, polled_node);
2753
2754 if (kh < ff->kh)
2755 link = &last->rb_left;
2756 else if (kh > ff->kh)
2757 link = &last->rb_right;
2758 else
2759 return link;
2760 }
2761
2762 if (parent_out)
2763 *parent_out = last;
2764 return link;
2765}
2766
2767/*
2768 * The file is about to be polled. Make sure it's on the polled_files
2769 * RB tree. Note that files once added to the polled_files tree are
2770 * not removed before the file is released. This is because a file
2771 * polled once is likely to be polled again.
2772 */
2773static void fuse_register_polled_file(struct fuse_conn *fc,
2774 struct fuse_file *ff)
2775{
2776 spin_lock(&fc->lock);
2777 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002778 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002779
2780 link = fuse_find_polled_node(fc, ff->kh, &parent);
2781 BUG_ON(*link);
2782 rb_link_node(&ff->polled_node, parent, link);
2783 rb_insert_color(&ff->polled_node, &fc->polled_files);
2784 }
2785 spin_unlock(&fc->lock);
2786}
2787
Tejun Heo08cbf542009-04-14 10:54:53 +09002788unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002789{
Tejun Heo95668a62008-11-26 12:03:55 +01002790 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002791 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002792 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2793 struct fuse_poll_out outarg;
2794 struct fuse_req *req;
2795 int err;
2796
2797 if (fc->no_poll)
2798 return DEFAULT_POLLMASK;
2799
2800 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002801 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002802
2803 /*
2804 * Ask for notification iff there's someone waiting for it.
2805 * The client may ignore the flag and always notify.
2806 */
2807 if (waitqueue_active(&ff->poll_wait)) {
2808 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2809 fuse_register_polled_file(fc, ff);
2810 }
2811
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002812 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002813 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002814 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002815
2816 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002817 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002818 req->in.numargs = 1;
2819 req->in.args[0].size = sizeof(inarg);
2820 req->in.args[0].value = &inarg;
2821 req->out.numargs = 1;
2822 req->out.args[0].size = sizeof(outarg);
2823 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002824 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002825 err = req->out.h.error;
2826 fuse_put_request(fc, req);
2827
2828 if (!err)
2829 return outarg.revents;
2830 if (err == -ENOSYS) {
2831 fc->no_poll = 1;
2832 return DEFAULT_POLLMASK;
2833 }
2834 return POLLERR;
2835}
Tejun Heo08cbf542009-04-14 10:54:53 +09002836EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002837
2838/*
2839 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2840 * wakes up the poll waiters.
2841 */
2842int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2843 struct fuse_notify_poll_wakeup_out *outarg)
2844{
2845 u64 kh = outarg->kh;
2846 struct rb_node **link;
2847
2848 spin_lock(&fc->lock);
2849
2850 link = fuse_find_polled_node(fc, kh, NULL);
2851 if (*link) {
2852 struct fuse_file *ff;
2853
2854 ff = rb_entry(*link, struct fuse_file, polled_node);
2855 wake_up_interruptible_sync(&ff->poll_wait);
2856 }
2857
2858 spin_unlock(&fc->lock);
2859 return 0;
2860}
2861
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002862static void fuse_do_truncate(struct file *file)
2863{
2864 struct inode *inode = file->f_mapping->host;
2865 struct iattr attr;
2866
2867 attr.ia_valid = ATTR_SIZE;
2868 attr.ia_size = i_size_read(inode);
2869
2870 attr.ia_file = file;
2871 attr.ia_valid |= ATTR_FILE;
2872
2873 fuse_do_setattr(inode, &attr, file);
2874}
2875
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002876static inline loff_t fuse_round_up(loff_t off)
2877{
2878 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2879}
2880
Anand Avati4273b792012-02-17 12:46:25 -05002881static ssize_t
2882fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2883 loff_t offset, unsigned long nr_segs)
2884{
2885 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002886 struct file *file = iocb->ki_filp;
2887 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002888 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002889 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002890 struct inode *inode;
2891 loff_t i_size;
2892 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002893 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002894
Anand Avati4273b792012-02-17 12:46:25 -05002895 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002896 inode = file->f_mapping->host;
2897 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002898
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002899 if ((rw == READ) && (offset > i_size))
2900 return 0;
2901
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002902 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002903 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002904 if (offset >= i_size)
2905 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002906 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002907 }
2908
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002909 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002910 if (!io)
2911 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002912 spin_lock_init(&io->lock);
2913 io->reqs = 1;
2914 io->bytes = -1;
2915 io->size = 0;
2916 io->offset = offset;
2917 io->write = (rw == WRITE);
2918 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002919 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002920 /*
2921 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002922 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002923 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002924 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002925 io->iocb = iocb;
2926
2927 /*
2928 * We cannot asynchronously extend the size of a file. We have no method
2929 * to wait on real async I/O requests, so we must submit this request
2930 * synchronously.
2931 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002932 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002933 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002934
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002935 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002936 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002937 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002938 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002939
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002940 if (io->async) {
2941 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2942
2943 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002944 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002945 return -EIOCBQUEUED;
2946
2947 ret = wait_on_sync_kiocb(iocb);
2948 } else {
2949 kfree(io);
2950 }
2951
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002952 if (rw == WRITE) {
2953 if (ret > 0)
2954 fuse_write_update_size(inode, pos);
2955 else if (ret < 0 && offset + count > i_size)
2956 fuse_do_truncate(file);
2957 }
Anand Avati4273b792012-02-17 12:46:25 -05002958
2959 return ret;
2960}
2961
Miklos Szeredicdadb112012-11-10 16:55:56 +01002962static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2963 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002964{
2965 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002966 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002967 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002968 struct fuse_conn *fc = ff->fc;
2969 struct fuse_req *req;
2970 struct fuse_fallocate_in inarg = {
2971 .fh = ff->fh,
2972 .offset = offset,
2973 .length = length,
2974 .mode = mode
2975 };
2976 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002977 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2978 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002979
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002980 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2981 return -EOPNOTSUPP;
2982
Miklos Szeredi519c6042012-04-26 10:56:36 +02002983 if (fc->no_fallocate)
2984 return -EOPNOTSUPP;
2985
Maxim Patlasov14c14412013-06-13 12:16:39 +04002986 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002987 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002988 if (mode & FALLOC_FL_PUNCH_HOLE) {
2989 loff_t endbyte = offset + length - 1;
2990 err = filemap_write_and_wait_range(inode->i_mapping,
2991 offset, endbyte);
2992 if (err)
2993 goto out;
2994
2995 fuse_sync_writes(inode);
2996 }
Brian Foster3634a632013-05-17 09:30:32 -04002997 }
2998
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002999 if (!(mode & FALLOC_FL_KEEP_SIZE))
3000 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3001
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04003002 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04003003 if (IS_ERR(req)) {
3004 err = PTR_ERR(req);
3005 goto out;
3006 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003007
3008 req->in.h.opcode = FUSE_FALLOCATE;
3009 req->in.h.nodeid = ff->nodeid;
3010 req->in.numargs = 1;
3011 req->in.args[0].size = sizeof(inarg);
3012 req->in.args[0].value = &inarg;
3013 fuse_request_send(fc, req);
3014 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02003015 if (err == -ENOSYS) {
3016 fc->no_fallocate = 1;
3017 err = -EOPNOTSUPP;
3018 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003019 fuse_put_request(fc, req);
3020
Brian Fosterbee6c302013-05-17 15:27:34 -04003021 if (err)
3022 goto out;
3023
3024 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003025 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3026 bool changed = fuse_write_update_size(inode, offset + length);
3027
Miklos Szeredi93d22692014-04-28 14:19:22 +02003028 if (changed && fc->writeback_cache)
3029 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003030 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003031
3032 if (mode & FALLOC_FL_PUNCH_HOLE)
3033 truncate_pagecache_range(inode, offset, offset + length - 1);
3034
3035 fuse_invalidate_attr(inode);
3036
Brian Foster3634a632013-05-17 09:30:32 -04003037out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003038 if (!(mode & FALLOC_FL_KEEP_SIZE))
3039 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3040
Maxim Patlasovbde52782013-09-13 19:19:54 +04003041 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003042 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003043
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003044 return err;
3045}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003046
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003047static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003048 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003049 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003050 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003051 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003052 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003053 .mmap = fuse_file_mmap,
3054 .open = fuse_open,
3055 .flush = fuse_flush,
3056 .release = fuse_release,
3057 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003058 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003059 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003060 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003061 .unlocked_ioctl = fuse_file_ioctl,
3062 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003063 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003064 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003065};
3066
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003067static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003068 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003069 .read = fuse_direct_read,
3070 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003071 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003072 .open = fuse_open,
3073 .flush = fuse_flush,
3074 .release = fuse_release,
3075 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003076 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003077 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003078 .unlocked_ioctl = fuse_file_ioctl,
3079 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003080 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003081 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003082 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003083};
3084
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003085static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003086 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003087 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003088 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003089 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003090 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003091 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003092 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003093 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003094 .write_begin = fuse_write_begin,
3095 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003096};
3097
3098void fuse_init_file_inode(struct inode *inode)
3099{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003100 inode->i_fop = &fuse_file_operations;
3101 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003102}