blob: 126deb5d0a9c2cf89139d818095406848336eaa0 [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)
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200327 write_inode_now(inode, 1);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400328
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200329 fuse_release_common(file, FUSE_RELEASE);
330
331 /* return value is ignored by VFS */
332 return 0;
333}
334
335void fuse_sync_release(struct fuse_file *ff, int flags)
336{
337 WARN_ON(atomic_read(&ff->count) > 1);
338 fuse_prepare_release(ff, flags, FUSE_RELEASE);
339 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400340 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200341 fuse_request_send(ff->fc, ff->reserved_req);
342 fuse_put_request(ff->fc, ff->reserved_req);
343 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700344}
Tejun Heo08cbf542009-04-14 10:54:53 +0900345EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700346
Miklos Szeredi71421252006-06-25 05:48:52 -0700347/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700348 * Scramble the ID space with XTEA, so that the value of the files_struct
349 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700350 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700351u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700352{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700353 u32 *k = fc->scramble_key;
354 u64 v = (unsigned long) id;
355 u32 v0 = v;
356 u32 v1 = v >> 32;
357 u32 sum = 0;
358 int i;
359
360 for (i = 0; i < 32; i++) {
361 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
362 sum += 0x9E3779B9;
363 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
364 }
365
366 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700367}
368
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700369/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400370 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700371 *
372 * This is currently done by walking the list of writepage requests
373 * for the inode, which can be pretty inefficient.
374 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400375static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
376 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700377{
378 struct fuse_conn *fc = get_fuse_conn(inode);
379 struct fuse_inode *fi = get_fuse_inode(inode);
380 struct fuse_req *req;
381 bool found = false;
382
383 spin_lock(&fc->lock);
384 list_for_each_entry(req, &fi->writepages, writepages_entry) {
385 pgoff_t curr_index;
386
387 BUG_ON(req->inode != inode);
388 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400389 if (idx_from < curr_index + req->num_pages &&
390 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700391 found = true;
392 break;
393 }
394 }
395 spin_unlock(&fc->lock);
396
397 return found;
398}
399
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400400static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
401{
402 return fuse_range_is_writeback(inode, index, index);
403}
404
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700405/*
406 * Wait for page writeback to be completed.
407 *
408 * Since fuse doesn't rely on the VM writeback tracking, this has to
409 * use some other means.
410 */
411static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
412{
413 struct fuse_inode *fi = get_fuse_inode(inode);
414
415 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
416 return 0;
417}
418
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400419/*
420 * Wait for all pending writepages on the inode to finish.
421 *
422 * This is currently done by blocking further writes with FUSE_NOWRITE
423 * and waiting for all sent writes to complete.
424 *
425 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
426 * could conflict with truncation.
427 */
428static void fuse_sync_writes(struct inode *inode)
429{
430 fuse_set_nowrite(inode);
431 fuse_release_nowrite(inode);
432}
433
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700434static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435{
Al Viro6131ffa2013-02-27 16:59:05 -0500436 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700437 struct fuse_conn *fc = get_fuse_conn(inode);
438 struct fuse_file *ff = file->private_data;
439 struct fuse_req *req;
440 struct fuse_flush_in inarg;
441 int err;
442
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800443 if (is_bad_inode(inode))
444 return -EIO;
445
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446 if (fc->no_flush)
447 return 0;
448
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200449 err = write_inode_now(inode, 1);
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400450 if (err)
451 return err;
452
453 mutex_lock(&inode->i_mutex);
454 fuse_sync_writes(inode);
455 mutex_unlock(&inode->i_mutex);
456
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400457 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 memset(&inarg, 0, sizeof(inarg));
459 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700460 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461 req->in.h.opcode = FUSE_FLUSH;
462 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700463 req->in.numargs = 1;
464 req->in.args[0].size = sizeof(inarg);
465 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700466 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100467 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468 err = req->out.h.error;
469 fuse_put_request(fc, req);
470 if (err == -ENOSYS) {
471 fc->no_flush = 1;
472 err = 0;
473 }
474 return err;
475}
476
Josef Bacik02c24a82011-07-16 20:44:56 -0400477int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
478 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700479{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200480 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 struct fuse_conn *fc = get_fuse_conn(inode);
482 struct fuse_file *ff = file->private_data;
483 struct fuse_req *req;
484 struct fuse_fsync_in inarg;
485 int err;
486
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800487 if (is_bad_inode(inode))
488 return -EIO;
489
Josef Bacik02c24a82011-07-16 20:44:56 -0400490 mutex_lock(&inode->i_mutex);
491
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492 /*
493 * Start writeback against all dirty pages of the inode, then
494 * wait for all outstanding writes, before sending the FSYNC
495 * request.
496 */
Miklos Szeredi22401e72014-04-28 14:19:23 +0200497 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700498 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400499 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700500
501 fuse_sync_writes(inode);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +0200502 err = sync_inode_metadata(inode, 1);
503 if (err)
504 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700505
Miklos Szeredi22401e72014-04-28 14:19:23 +0200506 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
507 goto out;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400508
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400509 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400510 if (IS_ERR(req)) {
511 err = PTR_ERR(req);
512 goto out;
513 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514
515 memset(&inarg, 0, sizeof(inarg));
516 inarg.fh = ff->fh;
517 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700518 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700520 req->in.numargs = 1;
521 req->in.args[0].size = sizeof(inarg);
522 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100523 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700524 err = req->out.h.error;
525 fuse_put_request(fc, req);
526 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700527 if (isdir)
528 fc->no_fsyncdir = 1;
529 else
530 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531 err = 0;
532 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400533out:
534 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700535 return err;
536}
537
Josef Bacik02c24a82011-07-16 20:44:56 -0400538static int fuse_fsync(struct file *file, loff_t start, loff_t end,
539 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700540{
Josef Bacik02c24a82011-07-16 20:44:56 -0400541 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700542}
543
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200544void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
545 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700546{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700547 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800548 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700549
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800550 inarg->fh = ff->fh;
551 inarg->offset = pos;
552 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800553 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800554 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200555 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700556 req->in.numargs = 1;
557 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800558 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700559 req->out.argvar = 1;
560 req->out.numargs = 1;
561 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700562}
563
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400564static void fuse_release_user_pages(struct fuse_req *req, int write)
565{
566 unsigned i;
567
568 for (i = 0; i < req->num_pages; i++) {
569 struct page *page = req->pages[i];
570 if (write)
571 set_page_dirty_lock(page);
572 put_page(page);
573 }
574}
575
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400576/**
577 * In case of short read, the caller sets 'pos' to the position of
578 * actual end of fuse request in IO request. Otherwise, if bytes_requested
579 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
580 *
581 * An example:
582 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
583 * both submitted asynchronously. The first of them was ACKed by userspace as
584 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
585 * second request was ACKed as short, e.g. only 1K was read, resulting in
586 * pos == 33K.
587 *
588 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
589 * will be equal to the length of the longest contiguous fragment of
590 * transferred data starting from the beginning of IO request.
591 */
592static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
593{
594 int left;
595
596 spin_lock(&io->lock);
597 if (err)
598 io->err = io->err ? : err;
599 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
600 io->bytes = pos;
601
602 left = --io->reqs;
603 spin_unlock(&io->lock);
604
605 if (!left) {
606 long res;
607
608 if (io->err)
609 res = io->err;
610 else if (io->bytes >= 0 && io->write)
611 res = -EIO;
612 else {
613 res = io->bytes < 0 ? io->size : io->bytes;
614
615 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400616 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400617 struct fuse_conn *fc = get_fuse_conn(inode);
618 struct fuse_inode *fi = get_fuse_inode(inode);
619
620 spin_lock(&fc->lock);
621 fi->attr_version = ++fc->attr_version;
622 spin_unlock(&fc->lock);
623 }
624 }
625
626 aio_complete(io->iocb, res, 0);
627 kfree(io);
628 }
629}
630
631static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
632{
633 struct fuse_io_priv *io = req->io;
634 ssize_t pos = -1;
635
636 fuse_release_user_pages(req, !io->write);
637
638 if (io->write) {
639 if (req->misc.write.in.size != req->misc.write.out.size)
640 pos = req->misc.write.in.offset - io->offset +
641 req->misc.write.out.size;
642 } else {
643 if (req->misc.read.in.size != req->out.args[0].size)
644 pos = req->misc.read.in.offset - io->offset +
645 req->out.args[0].size;
646 }
647
648 fuse_aio_complete(io, req->out.h.error, pos);
649}
650
651static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
652 size_t num_bytes, struct fuse_io_priv *io)
653{
654 spin_lock(&io->lock);
655 io->size += num_bytes;
656 io->reqs++;
657 spin_unlock(&io->lock);
658
659 req->io = io;
660 req->end = fuse_aio_complete_req;
661
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400662 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400663 fuse_request_send_background(fc, req);
664
665 return num_bytes;
666}
667
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400668static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200669 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700670{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400671 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200672 struct fuse_file *ff = file->private_data;
673 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700674
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200675 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700676 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700677 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700678
679 inarg->read_flags |= FUSE_READ_LOCKOWNER;
680 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
681 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400682
683 if (io->async)
684 return fuse_async_req_send(fc, req, count, io);
685
Tejun Heob93f8582008-11-26 12:03:55 +0100686 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800687 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700688}
689
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700690static void fuse_read_update_size(struct inode *inode, loff_t size,
691 u64 attr_ver)
692{
693 struct fuse_conn *fc = get_fuse_conn(inode);
694 struct fuse_inode *fi = get_fuse_inode(inode);
695
696 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400697 if (attr_ver == fi->attr_version && size < inode->i_size &&
698 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700699 fi->attr_version = ++fc->attr_version;
700 i_size_write(inode, size);
701 }
702 spin_unlock(&fc->lock);
703}
704
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400705static void fuse_short_read(struct fuse_req *req, struct inode *inode,
706 u64 attr_ver)
707{
708 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400709 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710
Pavel Emelyanov83732002013-10-10 17:10:46 +0400711 if (fc->writeback_cache) {
712 /*
713 * A hole in a file. Some data after the hole are in page cache,
714 * but have not reached the client fs yet. So, the hole is not
715 * present there.
716 */
717 int i;
718 int start_idx = num_read >> PAGE_CACHE_SHIFT;
719 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
720
721 for (i = start_idx; i < req->num_pages; i++) {
722 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
723 off = 0;
724 }
725 } else {
726 loff_t pos = page_offset(req->pages[0]) + num_read;
727 fuse_read_update_size(inode, pos, attr_ver);
728 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400729}
730
Maxim Patlasov482fce52013-10-10 17:11:25 +0400731static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700732{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400733 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734 struct inode *inode = page->mapping->host;
735 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800736 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700737 size_t num_read;
738 loff_t pos = page_offset(page);
739 size_t count = PAGE_CACHE_SIZE;
740 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800741 int err;
742
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700743 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300744 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700745 * page-cache page, so make sure we read a properly synced
746 * page.
747 */
748 fuse_wait_on_page_writeback(inode, page->index);
749
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400750 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700751 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400752 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 attr_ver = fuse_get_attr_version(fc);
755
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200757 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700758 req->num_pages = 1;
759 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400760 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400761 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700763
764 if (!err) {
765 /*
766 * Short read means EOF. If file size is larger, truncate it
767 */
768 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400769 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700770
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700771 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700772 }
773
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400774 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400775
776 return err;
777}
778
779static int fuse_readpage(struct file *file, struct page *page)
780{
781 struct inode *inode = page->mapping->host;
782 int err;
783
784 err = -EIO;
785 if (is_bad_inode(inode))
786 goto out;
787
788 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800789 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700790 out:
791 unlock_page(page);
792 return err;
793}
794
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800795static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700796{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800797 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700798 size_t count = req->misc.read.in.size;
799 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200800 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800801
Miklos Szeredice534fb2010-05-25 15:06:07 +0200802 for (i = 0; mapping == NULL && i < req->num_pages; i++)
803 mapping = req->pages[i]->mapping;
804
805 if (mapping) {
806 struct inode *inode = mapping->host;
807
808 /*
809 * Short read means EOF. If file size is larger, truncate it
810 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400811 if (!req->out.h.error && num_read < count)
812 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200813
Andrew Gallagher451418f2013-11-05 03:55:43 -0800814 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700815 }
816
Miklos Szeredidb50b962005-09-09 13:10:33 -0700817 for (i = 0; i < req->num_pages; i++) {
818 struct page *page = req->pages[i];
819 if (!req->out.h.error)
820 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800821 else
822 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700823 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200824 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700825 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700826 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100827 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800828}
829
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200830static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200832 struct fuse_file *ff = file->private_data;
833 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800834 loff_t pos = page_offset(req->pages[0]);
835 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200836
837 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800838 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200839 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200840 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700841 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800842 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700843 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800844 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100845 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800846 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100847 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800848 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100849 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800850 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851}
852
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700853struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800855 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400857 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858};
859
860static int fuse_readpages_fill(void *_data, struct page *page)
861{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700862 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863 struct fuse_req *req = data->req;
864 struct inode *inode = data->inode;
865 struct fuse_conn *fc = get_fuse_conn(inode);
866
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700867 fuse_wait_on_page_writeback(inode, page->index);
868
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 if (req->num_pages &&
870 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
871 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
872 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400873 int nr_alloc = min_t(unsigned, data->nr_pages,
874 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200875 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400876 if (fc->async_read)
877 req = fuse_get_req_for_background(fc, nr_alloc);
878 else
879 req = fuse_get_req(fc, nr_alloc);
880
881 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700882 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700883 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700884 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700885 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400887
888 if (WARN_ON(req->num_pages >= req->max_pages)) {
889 fuse_put_request(fc, req);
890 return -EIO;
891 }
892
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200893 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700894 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400895 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100896 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400897 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700898 return 0;
899}
900
901static int fuse_readpages(struct file *file, struct address_space *mapping,
902 struct list_head *pages, unsigned nr_pages)
903{
904 struct inode *inode = mapping->host;
905 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700906 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400908 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800909
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700910 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800911 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800912 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800913
Miklos Szeredia6643092007-11-28 16:22:00 -0800914 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400916 if (fc->async_read)
917 data.req = fuse_get_req_for_background(fc, nr_alloc);
918 else
919 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400920 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700921 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700922 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800923 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700924
925 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700926 if (!err) {
927 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200928 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700929 else
930 fuse_put_request(fc, data.req);
931 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800932out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700933 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700934}
935
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
937 unsigned long nr_segs, loff_t pos)
938{
939 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400940 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941
Brian Fostera8894272012-07-16 15:23:50 -0400942 /*
943 * In auto invalidate mode, always update attributes on read.
944 * Otherwise, only update if we attempt to read past EOF (to ensure
945 * i_size is up to date).
946 */
947 if (fc->auto_inval_data ||
948 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800949 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800950 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
951 if (err)
952 return err;
953 }
954
955 return generic_file_aio_read(iocb, iov, nr_segs, pos);
956}
957
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200958static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200959 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700961 struct fuse_write_in *inarg = &req->misc.write.in;
962 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700963
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700964 inarg->fh = ff->fh;
965 inarg->offset = pos;
966 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700967 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200968 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700969 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700971 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
972 else
973 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700975 req->in.args[1].size = count;
976 req->out.numargs = 1;
977 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700978 req->out.args[0].value = outarg;
979}
980
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400981static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200982 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700983{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400984 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200985 struct fuse_file *ff = file->private_data;
986 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200987 struct fuse_write_in *inarg = &req->misc.write.in;
988
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200989 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200990 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700991 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700992 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
993 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
994 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400995
996 if (io->async)
997 return fuse_async_req_send(fc, req, count, io);
998
Tejun Heob93f8582008-11-26 12:03:55 +0100999 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001000 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001001}
1002
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004{
1005 struct fuse_conn *fc = get_fuse_conn(inode);
1006 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001007 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008
1009 spin_lock(&fc->lock);
1010 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001011 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001012 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013 ret = true;
1014 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001015 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001016
1017 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001018}
1019
Nick Pigginea9b9902008-04-30 00:54:42 -07001020static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1021 struct inode *inode, loff_t pos,
1022 size_t count)
1023{
1024 size_t res;
1025 unsigned offset;
1026 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001027 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001028
1029 for (i = 0; i < req->num_pages; i++)
1030 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1031
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001032 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001033
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001034 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001035 count = res;
1036 for (i = 0; i < req->num_pages; i++) {
1037 struct page *page = req->pages[i];
1038
1039 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1040 SetPageUptodate(page);
1041
1042 if (count > PAGE_CACHE_SIZE - offset)
1043 count -= PAGE_CACHE_SIZE - offset;
1044 else
1045 count = 0;
1046 offset = 0;
1047
1048 unlock_page(page);
1049 page_cache_release(page);
1050 }
1051
1052 return res;
1053}
1054
1055static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1056 struct address_space *mapping,
1057 struct iov_iter *ii, loff_t pos)
1058{
1059 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1060 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1061 size_t count = 0;
1062 int err;
1063
Miklos Szeredif4975c62009-04-02 14:25:34 +02001064 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001065 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001066
1067 do {
1068 size_t tmp;
1069 struct page *page;
1070 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1071 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1072 iov_iter_count(ii));
1073
1074 bytes = min_t(size_t, bytes, fc->max_write - count);
1075
1076 again:
1077 err = -EFAULT;
1078 if (iov_iter_fault_in_readable(ii, bytes))
1079 break;
1080
1081 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001082 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 if (!page)
1084 break;
1085
anfei zhou931e80e2010-02-02 13:44:02 -08001086 if (mapping_writably_mapped(mapping))
1087 flush_dcache_page(page);
1088
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 flush_dcache_page(page);
1091
Johannes Weiner478e0842011-07-25 22:35:35 +02001092 mark_page_accessed(page);
1093
Nick Pigginea9b9902008-04-30 00:54:42 -07001094 if (!tmp) {
1095 unlock_page(page);
1096 page_cache_release(page);
1097 bytes = min(bytes, iov_iter_single_seg_count(ii));
1098 goto again;
1099 }
1100
1101 err = 0;
1102 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001103 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001104 req->num_pages++;
1105
1106 iov_iter_advance(ii, tmp);
1107 count += tmp;
1108 pos += tmp;
1109 offset += tmp;
1110 if (offset == PAGE_CACHE_SIZE)
1111 offset = 0;
1112
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001113 if (!fc->big_writes)
1114 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001115 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001116 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001117
1118 return count > 0 ? count : err;
1119}
1120
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001121static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1122{
1123 return min_t(unsigned,
1124 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1125 (pos >> PAGE_CACHE_SHIFT) + 1,
1126 FUSE_MAX_PAGES_PER_REQ);
1127}
1128
Nick Pigginea9b9902008-04-30 00:54:42 -07001129static ssize_t fuse_perform_write(struct file *file,
1130 struct address_space *mapping,
1131 struct iov_iter *ii, loff_t pos)
1132{
1133 struct inode *inode = mapping->host;
1134 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001135 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001136 int err = 0;
1137 ssize_t res = 0;
1138
1139 if (is_bad_inode(inode))
1140 return -EIO;
1141
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001142 if (inode->i_size < pos + iov_iter_count(ii))
1143 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1144
Nick Pigginea9b9902008-04-30 00:54:42 -07001145 do {
1146 struct fuse_req *req;
1147 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001148 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001149
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001150 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001151 if (IS_ERR(req)) {
1152 err = PTR_ERR(req);
1153 break;
1154 }
1155
1156 count = fuse_fill_write_pages(req, mapping, ii, pos);
1157 if (count <= 0) {
1158 err = count;
1159 } else {
1160 size_t num_written;
1161
1162 num_written = fuse_send_write_pages(req, file, inode,
1163 pos, count);
1164 err = req->out.h.error;
1165 if (!err) {
1166 res += num_written;
1167 pos += num_written;
1168
1169 /* break out of the loop on short write */
1170 if (num_written != count)
1171 err = -EIO;
1172 }
1173 }
1174 fuse_put_request(fc, req);
1175 } while (!err && iov_iter_count(ii));
1176
1177 if (res > 0)
1178 fuse_write_update_size(inode, pos);
1179
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001180 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 fuse_invalidate_attr(inode);
1182
1183 return res > 0 ? res : err;
1184}
1185
1186static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1187 unsigned long nr_segs, loff_t pos)
1188{
1189 struct file *file = iocb->ki_filp;
1190 struct address_space *mapping = file->f_mapping;
1191 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001192 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001193 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001194 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 struct inode *inode = mapping->host;
1196 ssize_t err;
1197 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001198 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001199
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001200 if (get_fuse_conn(inode)->writeback_cache) {
1201 /* Update size (EOF optimization) and mode (SUID clearing) */
1202 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1203 if (err)
1204 return err;
1205
1206 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1207 }
1208
Nick Pigginea9b9902008-04-30 00:54:42 -07001209 WARN_ON(iocb->ki_pos != pos);
1210
Anand Avati4273b792012-02-17 12:46:25 -05001211 ocount = 0;
1212 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001213 if (err)
1214 return err;
1215
Anand Avati4273b792012-02-17 12:46:25 -05001216 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001217 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001218
1219 /* We can write back this queue in page reclaim */
1220 current->backing_dev_info = mapping->backing_dev_info;
1221
1222 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1223 if (err)
1224 goto out;
1225
1226 if (count == 0)
1227 goto out;
1228
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001229 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001230 if (err)
1231 goto out;
1232
Josef Bacikc3b2da32012-03-26 09:59:21 -04001233 err = file_update_time(file);
1234 if (err)
1235 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001236
Anand Avati4273b792012-02-17 12:46:25 -05001237 if (file->f_flags & O_DIRECT) {
Al Virof8579f82014-03-03 22:03:20 -05001238 iov_iter_init(&i, iov, nr_segs, count, 0);
1239 written = generic_file_direct_write(iocb, &i, pos, count, ocount);
Anand Avati4273b792012-02-17 12:46:25 -05001240 if (written < 0 || written == count)
1241 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001242
Anand Avati4273b792012-02-17 12:46:25 -05001243 pos += written;
Anand Avati4273b792012-02-17 12:46:25 -05001244
Anand Avati4273b792012-02-17 12:46:25 -05001245 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1246 if (written_buffered < 0) {
1247 err = written_buffered;
1248 goto out;
1249 }
1250 endbyte = pos + written_buffered - 1;
1251
1252 err = filemap_write_and_wait_range(file->f_mapping, pos,
1253 endbyte);
1254 if (err)
1255 goto out;
1256
1257 invalidate_mapping_pages(file->f_mapping,
1258 pos >> PAGE_CACHE_SHIFT,
1259 endbyte >> PAGE_CACHE_SHIFT);
1260
1261 written += written_buffered;
1262 iocb->ki_pos = pos + written_buffered;
1263 } else {
1264 iov_iter_init(&i, iov, nr_segs, count, 0);
1265 written = fuse_perform_write(file, mapping, &i, pos);
1266 if (written >= 0)
1267 iocb->ki_pos = pos + written;
1268 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001269out:
1270 current->backing_dev_info = NULL;
1271 mutex_unlock(&inode->i_mutex);
1272
1273 return written ? written : err;
1274}
1275
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001276static inline void fuse_page_descs_length_init(struct fuse_req *req,
1277 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001278{
1279 int i;
1280
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001282 req->page_descs[i].length = PAGE_SIZE -
1283 req->page_descs[i].offset;
1284}
1285
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001286static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1287{
1288 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1289}
1290
1291static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1292 size_t max_size)
1293{
1294 return min(iov_iter_single_seg_count(ii), max_size);
1295}
1296
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001297static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001298 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001299{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001300 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001301
Miklos Szeredif4975c62009-04-02 14:25:34 +02001302 /* Special case for kernel I/O: can copy directly into the buffer */
1303 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001304 unsigned long user_addr = fuse_get_user_addr(ii);
1305 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1306
Miklos Szeredif4975c62009-04-02 14:25:34 +02001307 if (write)
1308 req->in.args[1].value = (void *) user_addr;
1309 else
1310 req->out.args[0].value = (void *) user_addr;
1311
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001312 iov_iter_advance(ii, frag_size);
1313 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001314 return 0;
1315 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001316
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001317 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001318 unsigned npages;
1319 unsigned long user_addr = fuse_get_user_addr(ii);
1320 unsigned offset = user_addr & ~PAGE_MASK;
1321 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1322 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001323
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001324 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001325 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1326
1327 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1328 npages = clamp(npages, 1U, n);
1329
1330 ret = get_user_pages_fast(user_addr, npages, !write,
1331 &req->pages[req->num_pages]);
1332 if (ret < 0)
1333 return ret;
1334
1335 npages = ret;
1336 frag_size = min_t(size_t, frag_size,
1337 (npages << PAGE_SHIFT) - offset);
1338 iov_iter_advance(ii, frag_size);
1339
1340 req->page_descs[req->num_pages].offset = offset;
1341 fuse_page_descs_length_init(req, req->num_pages, npages);
1342
1343 req->num_pages += npages;
1344 req->page_descs[req->num_pages - 1].length -=
1345 (npages << PAGE_SHIFT) - offset - frag_size;
1346
1347 nbytes += frag_size;
1348 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001349
1350 if (write)
1351 req->in.argpages = 1;
1352 else
1353 req->out.argpages = 1;
1354
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001355 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001356
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001357 return 0;
1358}
1359
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001360static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1361{
1362 struct iov_iter ii = *ii_p;
1363 int npages = 0;
1364
1365 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1366 unsigned long user_addr = fuse_get_user_addr(&ii);
1367 unsigned offset = user_addr & ~PAGE_MASK;
1368 size_t frag_size = iov_iter_single_seg_count(&ii);
1369
1370 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1371 iov_iter_advance(&ii, frag_size);
1372 }
1373
1374 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1375}
1376
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001377ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001378 unsigned long nr_segs, size_t count, loff_t *ppos,
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001379 int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001381 int write = flags & FUSE_DIO_WRITE;
1382 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001383 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001384 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001385 struct fuse_file *ff = file->private_data;
1386 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387 size_t nmax = write ? fc->max_write : fc->max_read;
1388 loff_t pos = *ppos;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001389 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1390 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001392 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001393 struct iov_iter ii;
1394
1395 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001396
Brian Fosterde82b922013-05-14 11:25:39 -04001397 if (io->async)
1398 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1399 else
1400 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001401 if (IS_ERR(req))
1402 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001403
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001404 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1405 if (!write)
1406 mutex_lock(&inode->i_mutex);
1407 fuse_sync_writes(inode);
1408 if (!write)
1409 mutex_unlock(&inode->i_mutex);
1410 }
1411
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001412 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001413 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001414 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001415 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001416 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417 if (err) {
1418 res = err;
1419 break;
1420 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001421
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001422 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001423 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001425 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001426
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001427 if (!io->async)
1428 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429 if (req->out.h.error) {
1430 if (!res)
1431 res = req->out.h.error;
1432 break;
1433 } else if (nres > nbytes) {
1434 res = -EIO;
1435 break;
1436 }
1437 count -= nres;
1438 res += nres;
1439 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001440 if (nres != nbytes)
1441 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001442 if (count) {
1443 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001444 if (io->async)
1445 req = fuse_get_req_for_background(fc,
1446 fuse_iter_npages(&ii));
1447 else
1448 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001449 if (IS_ERR(req))
1450 break;
1451 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001452 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001453 if (!IS_ERR(req))
1454 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001455 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001456 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001457
1458 return res;
1459}
Tejun Heo08cbf542009-04-14 10:54:53 +09001460EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001461
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001462static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1463 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001464 unsigned long nr_segs, loff_t *ppos,
1465 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001466{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001467 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001468 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001469 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001470
1471 if (is_bad_inode(inode))
1472 return -EIO;
1473
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001474 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001475
1476 fuse_invalidate_attr(inode);
1477
1478 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001479}
1480
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001481static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1482 size_t count, loff_t *ppos)
1483{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001484 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001485 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001486 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001487}
1488
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001489static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1490 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001491 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001492{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001493 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001494 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001495 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001496 ssize_t res;
1497
1498 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001499 if (!res)
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001500 res = fuse_direct_io(io, iov, nr_segs, count, ppos,
1501 FUSE_DIO_WRITE);
Anand Avati4273b792012-02-17 12:46:25 -05001502
1503 fuse_invalidate_attr(inode);
1504
1505 return res;
1506}
1507
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001508static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1509 size_t count, loff_t *ppos)
1510{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001511 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001512 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001513 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001514 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001515
1516 if (is_bad_inode(inode))
1517 return -EIO;
1518
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001519 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001520 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001521 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001522 if (res > 0)
1523 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001524 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001525
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001526 return res;
1527}
1528
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001530{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001531 int i;
1532
1533 for (i = 0; i < req->num_pages; i++)
1534 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001535
1536 if (req->ff)
1537 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001538}
1539
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001540static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001541{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001542 struct inode *inode = req->inode;
1543 struct fuse_inode *fi = get_fuse_inode(inode);
1544 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001545 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001546
1547 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001548 for (i = 0; i < req->num_pages; i++) {
1549 dec_bdi_stat(bdi, BDI_WRITEBACK);
1550 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1551 bdi_writeout_inc(bdi);
1552 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001553 wake_up(&fi->page_waitq);
1554}
1555
1556/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001557static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1558 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001559__releases(fc->lock)
1560__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001561{
1562 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001563 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001564 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001565
1566 if (!fc->connected)
1567 goto out_free;
1568
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001569 if (inarg->offset + data_size <= size) {
1570 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001571 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001572 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001573 } else {
1574 /* Got truncated off completely */
1575 goto out_free;
1576 }
1577
1578 req->in.args[1].size = inarg->size;
1579 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001580 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001581 return;
1582
1583 out_free:
1584 fuse_writepage_finish(fc, req);
1585 spin_unlock(&fc->lock);
1586 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001587 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001588 spin_lock(&fc->lock);
1589}
1590
1591/*
1592 * If fi->writectr is positive (no truncate or fsync going on) send
1593 * all queued writepage requests.
1594 *
1595 * Called with fc->lock
1596 */
1597void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001598__releases(fc->lock)
1599__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001600{
1601 struct fuse_conn *fc = get_fuse_conn(inode);
1602 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001603 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001604 struct fuse_req *req;
1605
1606 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1607 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1608 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001609 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001610 }
1611}
1612
1613static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1614{
1615 struct inode *inode = req->inode;
1616 struct fuse_inode *fi = get_fuse_inode(inode);
1617
1618 mapping_set_error(inode->i_mapping, req->out.h.error);
1619 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001620 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001621 struct fuse_conn *fc = get_fuse_conn(inode);
1622 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001623 struct fuse_req *next = req->misc.write.next;
1624 req->misc.write.next = next->misc.write.next;
1625 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001626 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001627 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001628
1629 /*
1630 * Skip fuse_flush_writepages() to make it easy to crop requests
1631 * based on primary request size.
1632 *
1633 * 1st case (trivial): there are no concurrent activities using
1634 * fuse_set/release_nowrite. Then we're on safe side because
1635 * fuse_flush_writepages() would call fuse_send_writepage()
1636 * anyway.
1637 *
1638 * 2nd case: someone called fuse_set_nowrite and it is waiting
1639 * now for completion of all in-flight requests. This happens
1640 * rarely and no more than once per page, so this should be
1641 * okay.
1642 *
1643 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1644 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1645 * that fuse_set_nowrite returned implies that all in-flight
1646 * requests were completed along with all of their secondary
1647 * requests. Further primary requests are blocked by negative
1648 * writectr. Hence there cannot be any in-flight requests and
1649 * no invocations of fuse_writepage_end() while we're in
1650 * fuse_set_nowrite..fuse_release_nowrite section.
1651 */
1652 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001653 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001654 fi->writectr--;
1655 fuse_writepage_finish(fc, req);
1656 spin_unlock(&fc->lock);
1657 fuse_writepage_free(fc, req);
1658}
1659
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001660static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1661 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001662{
Miklos Szeredi72523422013-10-01 16:44:52 +02001663 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001664
1665 spin_lock(&fc->lock);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001666 if (!list_empty(&fi->write_files)) {
Miklos Szeredi72523422013-10-01 16:44:52 +02001667 ff = list_entry(fi->write_files.next, struct fuse_file,
1668 write_entry);
1669 fuse_file_get(ff);
1670 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001671 spin_unlock(&fc->lock);
1672
1673 return ff;
1674}
1675
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001676static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1677 struct fuse_inode *fi)
1678{
1679 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1680 WARN_ON(!ff);
1681 return ff;
1682}
1683
1684int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1685{
1686 struct fuse_conn *fc = get_fuse_conn(inode);
1687 struct fuse_inode *fi = get_fuse_inode(inode);
1688 struct fuse_file *ff;
1689 int err;
1690
1691 ff = __fuse_write_file_get(fc, fi);
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001692 err = fuse_flush_times(inode, ff);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001693 if (ff)
1694 fuse_file_put(ff, 0);
1695
1696 return err;
1697}
1698
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699static int fuse_writepage_locked(struct page *page)
1700{
1701 struct address_space *mapping = page->mapping;
1702 struct inode *inode = mapping->host;
1703 struct fuse_conn *fc = get_fuse_conn(inode);
1704 struct fuse_inode *fi = get_fuse_inode(inode);
1705 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001706 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001707 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708
1709 set_page_writeback(page);
1710
Maxim Patlasov4250c062012-10-26 19:48:07 +04001711 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001712 if (!req)
1713 goto err;
1714
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001715 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001716 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1717 if (!tmp_page)
1718 goto err_free;
1719
Miklos Szeredi72523422013-10-01 16:44:52 +02001720 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001721 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001722 if (!req->ff)
1723 goto err_free;
1724
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001725 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001726
1727 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001728 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001729 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001730 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001731 req->num_pages = 1;
1732 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001733 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001734 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001735 req->end = fuse_writepage_end;
1736 req->inode = inode;
1737
1738 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1739 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001740
1741 spin_lock(&fc->lock);
1742 list_add(&req->writepages_entry, &fi->writepages);
1743 list_add_tail(&req->list, &fi->queued_writes);
1744 fuse_flush_writepages(inode);
1745 spin_unlock(&fc->lock);
1746
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001747 end_page_writeback(page);
1748
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001749 return 0;
1750
1751err_free:
1752 fuse_request_free(req);
1753err:
1754 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001755 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001756}
1757
1758static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1759{
1760 int err;
1761
Miklos Szerediff17be02013-10-01 16:44:53 +02001762 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1763 /*
1764 * ->writepages() should be called for sync() and friends. We
1765 * should only get here on direct reclaim and then we are
1766 * allowed to skip a page which is already in flight
1767 */
1768 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1769
1770 redirty_page_for_writepage(wbc, page);
1771 return 0;
1772 }
1773
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001774 err = fuse_writepage_locked(page);
1775 unlock_page(page);
1776
1777 return err;
1778}
1779
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001780struct fuse_fill_wb_data {
1781 struct fuse_req *req;
1782 struct fuse_file *ff;
1783 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001784 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001785};
1786
1787static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1788{
1789 struct fuse_req *req = data->req;
1790 struct inode *inode = data->inode;
1791 struct fuse_conn *fc = get_fuse_conn(inode);
1792 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001793 int num_pages = req->num_pages;
1794 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001795
1796 req->ff = fuse_file_get(data->ff);
1797 spin_lock(&fc->lock);
1798 list_add_tail(&req->list, &fi->queued_writes);
1799 fuse_flush_writepages(inode);
1800 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001801
1802 for (i = 0; i < num_pages; i++)
1803 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001804}
1805
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001806static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1807 struct page *page)
1808{
1809 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1810 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1811 struct fuse_req *tmp;
1812 struct fuse_req *old_req;
1813 bool found = false;
1814 pgoff_t curr_index;
1815
1816 BUG_ON(new_req->num_pages != 0);
1817
1818 spin_lock(&fc->lock);
1819 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001820 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1821 BUG_ON(old_req->inode != new_req->inode);
1822 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1823 if (curr_index <= page->index &&
1824 page->index < curr_index + old_req->num_pages) {
1825 found = true;
1826 break;
1827 }
1828 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001829 if (!found) {
1830 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001831 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001832 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001833
Maxim Patlasovf6011082013-10-02 15:01:07 +04001834 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001835 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1836 BUG_ON(tmp->inode != new_req->inode);
1837 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1838 if (tmp->num_pages == 1 &&
1839 curr_index == page->index) {
1840 old_req = tmp;
1841 }
1842 }
1843
1844 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1845 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001846 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1847
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001848 copy_highpage(old_req->pages[0], page);
1849 spin_unlock(&fc->lock);
1850
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001851 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001852 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001853 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001854 fuse_writepage_free(fc, new_req);
1855 fuse_request_free(new_req);
1856 goto out;
1857 } else {
1858 new_req->misc.write.next = old_req->misc.write.next;
1859 old_req->misc.write.next = new_req;
1860 }
1861out_unlock:
1862 spin_unlock(&fc->lock);
1863out:
1864 return found;
1865}
1866
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001867static int fuse_writepages_fill(struct page *page,
1868 struct writeback_control *wbc, void *_data)
1869{
1870 struct fuse_fill_wb_data *data = _data;
1871 struct fuse_req *req = data->req;
1872 struct inode *inode = data->inode;
1873 struct fuse_conn *fc = get_fuse_conn(inode);
1874 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001875 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001876 int err;
1877
1878 if (!data->ff) {
1879 err = -EIO;
1880 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1881 if (!data->ff)
1882 goto out_unlock;
1883 }
1884
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001885 /*
1886 * Being under writeback is unlikely but possible. For example direct
1887 * read to an mmaped fuse file will set the page dirty twice; once when
1888 * the pages are faulted with get_user_pages(), and then after the read
1889 * completed.
1890 */
1891 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001892
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001893 if (req && req->num_pages &&
1894 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1895 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1896 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1897 fuse_writepages_send(data);
1898 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001899 }
1900 err = -ENOMEM;
1901 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1902 if (!tmp_page)
1903 goto out_unlock;
1904
1905 /*
1906 * The page must not be redirtied until the writeout is completed
1907 * (i.e. userspace has sent a reply to the write request). Otherwise
1908 * there could be more than one temporary page instance for each real
1909 * page.
1910 *
1911 * This is ensured by holding the page lock in page_mkwrite() while
1912 * checking fuse_page_is_writeback(). We already hold the page lock
1913 * since clear_page_dirty_for_io() and keep it held until we add the
1914 * request to the fi->writepages list and increment req->num_pages.
1915 * After this fuse_page_is_writeback() will indicate that the page is
1916 * under writeback, so we can release the page lock.
1917 */
1918 if (data->req == NULL) {
1919 struct fuse_inode *fi = get_fuse_inode(inode);
1920
1921 err = -ENOMEM;
1922 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1923 if (!req) {
1924 __free_page(tmp_page);
1925 goto out_unlock;
1926 }
1927
1928 fuse_write_fill(req, data->ff, page_offset(page), 0);
1929 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001930 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001931 req->in.argpages = 1;
1932 req->background = 1;
1933 req->num_pages = 0;
1934 req->end = fuse_writepage_end;
1935 req->inode = inode;
1936
1937 spin_lock(&fc->lock);
1938 list_add(&req->writepages_entry, &fi->writepages);
1939 spin_unlock(&fc->lock);
1940
1941 data->req = req;
1942 }
1943 set_page_writeback(page);
1944
1945 copy_highpage(tmp_page, page);
1946 req->pages[req->num_pages] = tmp_page;
1947 req->page_descs[req->num_pages].offset = 0;
1948 req->page_descs[req->num_pages].length = PAGE_SIZE;
1949
1950 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1951 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001952
1953 err = 0;
1954 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1955 end_page_writeback(page);
1956 data->req = NULL;
1957 goto out_unlock;
1958 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001959 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001960
1961 /*
1962 * Protected by fc->lock against concurrent access by
1963 * fuse_page_is_writeback().
1964 */
1965 spin_lock(&fc->lock);
1966 req->num_pages++;
1967 spin_unlock(&fc->lock);
1968
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001969out_unlock:
1970 unlock_page(page);
1971
1972 return err;
1973}
1974
1975static int fuse_writepages(struct address_space *mapping,
1976 struct writeback_control *wbc)
1977{
1978 struct inode *inode = mapping->host;
1979 struct fuse_fill_wb_data data;
1980 int err;
1981
1982 err = -EIO;
1983 if (is_bad_inode(inode))
1984 goto out;
1985
1986 data.inode = inode;
1987 data.req = NULL;
1988 data.ff = NULL;
1989
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001990 err = -ENOMEM;
1991 data.orig_pages = kzalloc(sizeof(struct page *) *
1992 FUSE_MAX_PAGES_PER_REQ,
1993 GFP_NOFS);
1994 if (!data.orig_pages)
1995 goto out;
1996
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001997 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1998 if (data.req) {
1999 /* Ignore errors if we can write at least one page */
2000 BUG_ON(!data.req->num_pages);
2001 fuse_writepages_send(&data);
2002 err = 0;
2003 }
2004 if (data.ff)
2005 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04002006
2007 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002008out:
2009 return err;
2010}
2011
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04002012/*
2013 * It's worthy to make sure that space is reserved on disk for the write,
2014 * but how to implement it without killing performance need more thinking.
2015 */
2016static int fuse_write_begin(struct file *file, struct address_space *mapping,
2017 loff_t pos, unsigned len, unsigned flags,
2018 struct page **pagep, void **fsdata)
2019{
2020 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2021 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
2022 struct page *page;
2023 loff_t fsize;
2024 int err = -ENOMEM;
2025
2026 WARN_ON(!fc->writeback_cache);
2027
2028 page = grab_cache_page_write_begin(mapping, index, flags);
2029 if (!page)
2030 goto error;
2031
2032 fuse_wait_on_page_writeback(mapping->host, page->index);
2033
2034 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
2035 goto success;
2036 /*
2037 * Check if the start this page comes after the end of file, in which
2038 * case the readpage can be optimized away.
2039 */
2040 fsize = i_size_read(mapping->host);
2041 if (fsize <= (pos & PAGE_CACHE_MASK)) {
2042 size_t off = pos & ~PAGE_CACHE_MASK;
2043 if (off)
2044 zero_user_segment(page, 0, off);
2045 goto success;
2046 }
2047 err = fuse_do_readpage(file, page);
2048 if (err)
2049 goto cleanup;
2050success:
2051 *pagep = page;
2052 return 0;
2053
2054cleanup:
2055 unlock_page(page);
2056 page_cache_release(page);
2057error:
2058 return err;
2059}
2060
2061static int fuse_write_end(struct file *file, struct address_space *mapping,
2062 loff_t pos, unsigned len, unsigned copied,
2063 struct page *page, void *fsdata)
2064{
2065 struct inode *inode = page->mapping->host;
2066
2067 if (!PageUptodate(page)) {
2068 /* Zero any unwritten bytes at the end of the page */
2069 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2070 if (endoff)
2071 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2072 SetPageUptodate(page);
2073 }
2074
2075 fuse_write_update_size(inode, pos + copied);
2076 set_page_dirty(page);
2077 unlock_page(page);
2078 page_cache_release(page);
2079
2080 return copied;
2081}
2082
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002083static int fuse_launder_page(struct page *page)
2084{
2085 int err = 0;
2086 if (clear_page_dirty_for_io(page)) {
2087 struct inode *inode = page->mapping->host;
2088 err = fuse_writepage_locked(page);
2089 if (!err)
2090 fuse_wait_on_page_writeback(inode, page->index);
2091 }
2092 return err;
2093}
2094
2095/*
2096 * Write back dirty pages now, because there may not be any suitable
2097 * open files later
2098 */
2099static void fuse_vma_close(struct vm_area_struct *vma)
2100{
2101 filemap_write_and_wait(vma->vm_file->f_mapping);
2102}
2103
2104/*
2105 * Wait for writeback against this page to complete before allowing it
2106 * to be marked dirty again, and hence written back again, possibly
2107 * before the previous writepage completed.
2108 *
2109 * Block here, instead of in ->writepage(), so that the userspace fs
2110 * can only block processes actually operating on the filesystem.
2111 *
2112 * Otherwise unprivileged userspace fs would be able to block
2113 * unrelated:
2114 *
2115 * - page migration
2116 * - sync(2)
2117 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2118 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002119static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002120{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002121 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002122 struct inode *inode = file_inode(vma->vm_file);
2123
2124 file_update_time(vma->vm_file);
2125 lock_page(page);
2126 if (page->mapping != inode->i_mapping) {
2127 unlock_page(page);
2128 return VM_FAULT_NOPAGE;
2129 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002130
2131 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002132 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002133}
2134
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002135static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002136 .close = fuse_vma_close,
2137 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002138 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002139 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002140 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002141};
2142
2143static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2144{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002145 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2146 fuse_link_write_file(file);
2147
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002148 file_accessed(file);
2149 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002150 return 0;
2151}
2152
Miklos Szeredifc280c92009-04-02 14:25:35 +02002153static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2154{
2155 /* Can't provide the coherency needed for MAP_SHARED */
2156 if (vma->vm_flags & VM_MAYSHARE)
2157 return -ENODEV;
2158
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002159 invalidate_inode_pages2(file->f_mapping);
2160
Miklos Szeredifc280c92009-04-02 14:25:35 +02002161 return generic_file_mmap(file, vma);
2162}
2163
Miklos Szeredi71421252006-06-25 05:48:52 -07002164static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2165 struct file_lock *fl)
2166{
2167 switch (ffl->type) {
2168 case F_UNLCK:
2169 break;
2170
2171 case F_RDLCK:
2172 case F_WRLCK:
2173 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2174 ffl->end < ffl->start)
2175 return -EIO;
2176
2177 fl->fl_start = ffl->start;
2178 fl->fl_end = ffl->end;
2179 fl->fl_pid = ffl->pid;
2180 break;
2181
2182 default:
2183 return -EIO;
2184 }
2185 fl->fl_type = ffl->type;
2186 return 0;
2187}
2188
2189static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002190 const struct file_lock *fl, int opcode, pid_t pid,
2191 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002192{
Al Viro6131ffa2013-02-27 16:59:05 -05002193 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002194 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002195 struct fuse_file *ff = file->private_data;
2196 struct fuse_lk_in *arg = &req->misc.lk_in;
2197
2198 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002199 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002200 arg->lk.start = fl->fl_start;
2201 arg->lk.end = fl->fl_end;
2202 arg->lk.type = fl->fl_type;
2203 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002204 if (flock)
2205 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002206 req->in.h.opcode = opcode;
2207 req->in.h.nodeid = get_node_id(inode);
2208 req->in.numargs = 1;
2209 req->in.args[0].size = sizeof(*arg);
2210 req->in.args[0].value = arg;
2211}
2212
2213static int fuse_getlk(struct file *file, struct file_lock *fl)
2214{
Al Viro6131ffa2013-02-27 16:59:05 -05002215 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002216 struct fuse_conn *fc = get_fuse_conn(inode);
2217 struct fuse_req *req;
2218 struct fuse_lk_out outarg;
2219 int err;
2220
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002221 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002222 if (IS_ERR(req))
2223 return PTR_ERR(req);
2224
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002225 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002226 req->out.numargs = 1;
2227 req->out.args[0].size = sizeof(outarg);
2228 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002229 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002230 err = req->out.h.error;
2231 fuse_put_request(fc, req);
2232 if (!err)
2233 err = convert_fuse_file_lock(&outarg.lk, fl);
2234
2235 return err;
2236}
2237
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002239{
Al Viro6131ffa2013-02-27 16:59:05 -05002240 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002241 struct fuse_conn *fc = get_fuse_conn(inode);
2242 struct fuse_req *req;
2243 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2244 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2245 int err;
2246
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002247 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002248 /* NLM needs asynchronous locks, which we don't support yet */
2249 return -ENOLCK;
2250 }
2251
Miklos Szeredi71421252006-06-25 05:48:52 -07002252 /* Unlock on close is handled by the flush method */
2253 if (fl->fl_flags & FL_CLOSE)
2254 return 0;
2255
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002256 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002257 if (IS_ERR(req))
2258 return PTR_ERR(req);
2259
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002260 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002261 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002262 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002263 /* locking is restartable */
2264 if (err == -EINTR)
2265 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002266 fuse_put_request(fc, req);
2267 return err;
2268}
2269
2270static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2271{
Al Viro6131ffa2013-02-27 16:59:05 -05002272 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002273 struct fuse_conn *fc = get_fuse_conn(inode);
2274 int err;
2275
Miklos Szeredi48e90762008-07-25 01:49:02 -07002276 if (cmd == F_CANCELLK) {
2277 err = 0;
2278 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002279 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002280 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002281 err = 0;
2282 } else
2283 err = fuse_getlk(file, fl);
2284 } else {
2285 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002286 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002287 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002288 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002289 }
2290 return err;
2291}
2292
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002293static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2294{
Al Viro6131ffa2013-02-27 16:59:05 -05002295 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002296 struct fuse_conn *fc = get_fuse_conn(inode);
2297 int err;
2298
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002299 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002300 err = flock_lock_file_wait(file, fl);
2301 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002302 struct fuse_file *ff = file->private_data;
2303
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002304 /* emulate flock with POSIX locks */
2305 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002306 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002307 err = fuse_setlk(file, fl, 1);
2308 }
2309
2310 return err;
2311}
2312
Miklos Szeredib2d22722006-12-06 20:35:51 -08002313static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2314{
2315 struct inode *inode = mapping->host;
2316 struct fuse_conn *fc = get_fuse_conn(inode);
2317 struct fuse_req *req;
2318 struct fuse_bmap_in inarg;
2319 struct fuse_bmap_out outarg;
2320 int err;
2321
2322 if (!inode->i_sb->s_bdev || fc->no_bmap)
2323 return 0;
2324
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002325 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002326 if (IS_ERR(req))
2327 return 0;
2328
2329 memset(&inarg, 0, sizeof(inarg));
2330 inarg.block = block;
2331 inarg.blocksize = inode->i_sb->s_blocksize;
2332 req->in.h.opcode = FUSE_BMAP;
2333 req->in.h.nodeid = get_node_id(inode);
2334 req->in.numargs = 1;
2335 req->in.args[0].size = sizeof(inarg);
2336 req->in.args[0].value = &inarg;
2337 req->out.numargs = 1;
2338 req->out.args[0].size = sizeof(outarg);
2339 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002340 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002341 err = req->out.h.error;
2342 fuse_put_request(fc, req);
2343 if (err == -ENOSYS)
2344 fc->no_bmap = 1;
2345
2346 return err ? 0 : outarg.block;
2347}
2348
Andrew Morton965c8e52012-12-17 15:59:39 -08002349static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002350{
2351 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002352 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002353
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002354 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002355 if (whence == SEEK_CUR || whence == SEEK_SET)
2356 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002357
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002358 mutex_lock(&inode->i_mutex);
2359 retval = fuse_update_attributes(inode, NULL, file, NULL);
2360 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002361 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002362 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002363
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002364 return retval;
2365}
2366
Tejun Heo59efec72008-11-26 12:03:55 +01002367static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2368 unsigned int nr_segs, size_t bytes, bool to_user)
2369{
2370 struct iov_iter ii;
2371 int page_idx = 0;
2372
2373 if (!bytes)
2374 return 0;
2375
2376 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2377
2378 while (iov_iter_count(&ii)) {
2379 struct page *page = pages[page_idx++];
2380 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002381 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002382
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002383 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002384
2385 while (todo) {
2386 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2387 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2388 size_t copy = min(todo, iov_len);
2389 size_t left;
2390
2391 if (!to_user)
2392 left = copy_from_user(kaddr, uaddr, copy);
2393 else
2394 left = copy_to_user(uaddr, kaddr, copy);
2395
2396 if (unlikely(left))
2397 return -EFAULT;
2398
2399 iov_iter_advance(&ii, copy);
2400 todo -= copy;
2401 kaddr += copy;
2402 }
2403
Jens Axboe0bd87182009-11-03 11:40:44 +01002404 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002405 }
2406
2407 return 0;
2408}
2409
2410/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002411 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2412 * ABI was defined to be 'struct iovec' which is different on 32bit
2413 * and 64bit. Fortunately we can determine which structure the server
2414 * used from the size of the reply.
2415 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002416static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2417 size_t transferred, unsigned count,
2418 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002419{
2420#ifdef CONFIG_COMPAT
2421 if (count * sizeof(struct compat_iovec) == transferred) {
2422 struct compat_iovec *ciov = src;
2423 unsigned i;
2424
2425 /*
2426 * With this interface a 32bit server cannot support
2427 * non-compat (i.e. ones coming from 64bit apps) ioctl
2428 * requests
2429 */
2430 if (!is_compat)
2431 return -EINVAL;
2432
2433 for (i = 0; i < count; i++) {
2434 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2435 dst[i].iov_len = ciov[i].iov_len;
2436 }
2437 return 0;
2438 }
2439#endif
2440
2441 if (count * sizeof(struct iovec) != transferred)
2442 return -EIO;
2443
2444 memcpy(dst, src, transferred);
2445 return 0;
2446}
2447
Miklos Szeredi75727772010-11-30 16:39:27 +01002448/* Make sure iov_length() won't overflow */
2449static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2450{
2451 size_t n;
2452 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2453
Zach Brownfb6ccff2012-07-24 12:10:11 -07002454 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002455 if (iov->iov_len > (size_t) max)
2456 return -ENOMEM;
2457 max -= iov->iov_len;
2458 }
2459 return 0;
2460}
2461
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002462static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2463 void *src, size_t transferred, unsigned count,
2464 bool is_compat)
2465{
2466 unsigned i;
2467 struct fuse_ioctl_iovec *fiov = src;
2468
2469 if (fc->minor < 16) {
2470 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2471 count, is_compat);
2472 }
2473
2474 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2475 return -EIO;
2476
2477 for (i = 0; i < count; i++) {
2478 /* Did the server supply an inappropriate value? */
2479 if (fiov[i].base != (unsigned long) fiov[i].base ||
2480 fiov[i].len != (unsigned long) fiov[i].len)
2481 return -EIO;
2482
2483 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2484 dst[i].iov_len = (size_t) fiov[i].len;
2485
2486#ifdef CONFIG_COMPAT
2487 if (is_compat &&
2488 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2489 (compat_size_t) dst[i].iov_len != fiov[i].len))
2490 return -EIO;
2491#endif
2492 }
2493
2494 return 0;
2495}
2496
2497
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002498/*
Tejun Heo59efec72008-11-26 12:03:55 +01002499 * For ioctls, there is no generic way to determine how much memory
2500 * needs to be read and/or written. Furthermore, ioctls are allowed
2501 * to dereference the passed pointer, so the parameter requires deep
2502 * copying but FUSE has no idea whatsoever about what to copy in or
2503 * out.
2504 *
2505 * This is solved by allowing FUSE server to retry ioctl with
2506 * necessary in/out iovecs. Let's assume the ioctl implementation
2507 * needs to read in the following structure.
2508 *
2509 * struct a {
2510 * char *buf;
2511 * size_t buflen;
2512 * }
2513 *
2514 * On the first callout to FUSE server, inarg->in_size and
2515 * inarg->out_size will be NULL; then, the server completes the ioctl
2516 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2517 * the actual iov array to
2518 *
2519 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2520 *
2521 * which tells FUSE to copy in the requested area and retry the ioctl.
2522 * On the second round, the server has access to the structure and
2523 * from that it can tell what to look for next, so on the invocation,
2524 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2525 *
2526 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2527 * { .iov_base = a.buf, .iov_len = a.buflen } }
2528 *
2529 * FUSE will copy both struct a and the pointed buffer from the
2530 * process doing the ioctl and retry ioctl with both struct a and the
2531 * buffer.
2532 *
2533 * This time, FUSE server has everything it needs and completes ioctl
2534 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2535 *
2536 * Copying data out works the same way.
2537 *
2538 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2539 * automatically initializes in and out iovs by decoding @cmd with
2540 * _IOC_* macros and the server is not allowed to request RETRY. This
2541 * limits ioctl data transfers to well-formed ioctls and is the forced
2542 * behavior for all FUSE servers.
2543 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002544long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2545 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002546{
Tejun Heo59efec72008-11-26 12:03:55 +01002547 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002548 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002549 struct fuse_ioctl_in inarg = {
2550 .fh = ff->fh,
2551 .cmd = cmd,
2552 .arg = arg,
2553 .flags = flags
2554 };
2555 struct fuse_ioctl_out outarg;
2556 struct fuse_req *req = NULL;
2557 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002558 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002559 struct iovec *in_iov = NULL, *out_iov = NULL;
2560 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2561 size_t in_size, out_size, transferred;
2562 int err;
2563
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002564#if BITS_PER_LONG == 32
2565 inarg.flags |= FUSE_IOCTL_32BIT;
2566#else
2567 if (flags & FUSE_IOCTL_COMPAT)
2568 inarg.flags |= FUSE_IOCTL_32BIT;
2569#endif
2570
Tejun Heo59efec72008-11-26 12:03:55 +01002571 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002572 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002573
Tejun Heo59efec72008-11-26 12:03:55 +01002574 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002575 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002576 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002577 if (!pages || !iov_page)
2578 goto out;
2579
2580 /*
2581 * If restricted, initialize IO parameters as encoded in @cmd.
2582 * RETRY from server is not allowed.
2583 */
2584 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002585 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002586
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002587 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002588 iov->iov_len = _IOC_SIZE(cmd);
2589
2590 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2591 in_iov = iov;
2592 in_iovs = 1;
2593 }
2594
2595 if (_IOC_DIR(cmd) & _IOC_READ) {
2596 out_iov = iov;
2597 out_iovs = 1;
2598 }
2599 }
2600
2601 retry:
2602 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2603 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2604
2605 /*
2606 * Out data can be used either for actual out data or iovs,
2607 * make sure there always is at least one page.
2608 */
2609 out_size = max_t(size_t, out_size, PAGE_SIZE);
2610 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2611
2612 /* make sure there are enough buffer pages and init request with them */
2613 err = -ENOMEM;
2614 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2615 goto out;
2616 while (num_pages < max_pages) {
2617 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2618 if (!pages[num_pages])
2619 goto out;
2620 num_pages++;
2621 }
2622
Maxim Patlasov54b96672012-10-26 19:49:13 +04002623 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002624 if (IS_ERR(req)) {
2625 err = PTR_ERR(req);
2626 req = NULL;
2627 goto out;
2628 }
2629 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2630 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002631 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002632
2633 /* okay, let's send it to the client */
2634 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002635 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002636 req->in.numargs = 1;
2637 req->in.args[0].size = sizeof(inarg);
2638 req->in.args[0].value = &inarg;
2639 if (in_size) {
2640 req->in.numargs++;
2641 req->in.args[1].size = in_size;
2642 req->in.argpages = 1;
2643
2644 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2645 false);
2646 if (err)
2647 goto out;
2648 }
2649
2650 req->out.numargs = 2;
2651 req->out.args[0].size = sizeof(outarg);
2652 req->out.args[0].value = &outarg;
2653 req->out.args[1].size = out_size;
2654 req->out.argpages = 1;
2655 req->out.argvar = 1;
2656
Tejun Heob93f8582008-11-26 12:03:55 +01002657 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002658 err = req->out.h.error;
2659 transferred = req->out.args[1].size;
2660 fuse_put_request(fc, req);
2661 req = NULL;
2662 if (err)
2663 goto out;
2664
2665 /* did it ask for retry? */
2666 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002667 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002668
2669 /* no retry if in restricted mode */
2670 err = -EIO;
2671 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2672 goto out;
2673
2674 in_iovs = outarg.in_iovs;
2675 out_iovs = outarg.out_iovs;
2676
2677 /*
2678 * Make sure things are in boundary, separate checks
2679 * are to protect against overflow.
2680 */
2681 err = -ENOMEM;
2682 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2683 out_iovs > FUSE_IOCTL_MAX_IOV ||
2684 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2685 goto out;
2686
Cong Wang2408f6e2011-11-25 23:14:30 +08002687 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002688 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002689 transferred, in_iovs + out_iovs,
2690 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002691 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002692 if (err)
2693 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002694
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002695 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002696 out_iov = in_iov + in_iovs;
2697
Miklos Szeredi75727772010-11-30 16:39:27 +01002698 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2699 if (err)
2700 goto out;
2701
2702 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2703 if (err)
2704 goto out;
2705
Tejun Heo59efec72008-11-26 12:03:55 +01002706 goto retry;
2707 }
2708
2709 err = -EIO;
2710 if (transferred > inarg.out_size)
2711 goto out;
2712
2713 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2714 out:
2715 if (req)
2716 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002717 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002718 while (num_pages)
2719 __free_page(pages[--num_pages]);
2720 kfree(pages);
2721
2722 return err ? err : outarg.result;
2723}
Tejun Heo08cbf542009-04-14 10:54:53 +09002724EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002725
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002726long fuse_ioctl_common(struct file *file, unsigned int cmd,
2727 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002728{
Al Viro6131ffa2013-02-27 16:59:05 -05002729 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002730 struct fuse_conn *fc = get_fuse_conn(inode);
2731
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002732 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002733 return -EACCES;
2734
2735 if (is_bad_inode(inode))
2736 return -EIO;
2737
2738 return fuse_do_ioctl(file, cmd, arg, flags);
2739}
2740
Tejun Heo59efec72008-11-26 12:03:55 +01002741static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2742 unsigned long arg)
2743{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002744 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002745}
2746
2747static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2748 unsigned long arg)
2749{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002750 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002751}
2752
Tejun Heo95668a62008-11-26 12:03:55 +01002753/*
2754 * All files which have been polled are linked to RB tree
2755 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2756 * find the matching one.
2757 */
2758static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2759 struct rb_node **parent_out)
2760{
2761 struct rb_node **link = &fc->polled_files.rb_node;
2762 struct rb_node *last = NULL;
2763
2764 while (*link) {
2765 struct fuse_file *ff;
2766
2767 last = *link;
2768 ff = rb_entry(last, struct fuse_file, polled_node);
2769
2770 if (kh < ff->kh)
2771 link = &last->rb_left;
2772 else if (kh > ff->kh)
2773 link = &last->rb_right;
2774 else
2775 return link;
2776 }
2777
2778 if (parent_out)
2779 *parent_out = last;
2780 return link;
2781}
2782
2783/*
2784 * The file is about to be polled. Make sure it's on the polled_files
2785 * RB tree. Note that files once added to the polled_files tree are
2786 * not removed before the file is released. This is because a file
2787 * polled once is likely to be polled again.
2788 */
2789static void fuse_register_polled_file(struct fuse_conn *fc,
2790 struct fuse_file *ff)
2791{
2792 spin_lock(&fc->lock);
2793 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002794 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002795
2796 link = fuse_find_polled_node(fc, ff->kh, &parent);
2797 BUG_ON(*link);
2798 rb_link_node(&ff->polled_node, parent, link);
2799 rb_insert_color(&ff->polled_node, &fc->polled_files);
2800 }
2801 spin_unlock(&fc->lock);
2802}
2803
Tejun Heo08cbf542009-04-14 10:54:53 +09002804unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002805{
Tejun Heo95668a62008-11-26 12:03:55 +01002806 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002807 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002808 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2809 struct fuse_poll_out outarg;
2810 struct fuse_req *req;
2811 int err;
2812
2813 if (fc->no_poll)
2814 return DEFAULT_POLLMASK;
2815
2816 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002817 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002818
2819 /*
2820 * Ask for notification iff there's someone waiting for it.
2821 * The client may ignore the flag and always notify.
2822 */
2823 if (waitqueue_active(&ff->poll_wait)) {
2824 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2825 fuse_register_polled_file(fc, ff);
2826 }
2827
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002828 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002829 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002830 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002831
2832 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002833 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002834 req->in.numargs = 1;
2835 req->in.args[0].size = sizeof(inarg);
2836 req->in.args[0].value = &inarg;
2837 req->out.numargs = 1;
2838 req->out.args[0].size = sizeof(outarg);
2839 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002840 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002841 err = req->out.h.error;
2842 fuse_put_request(fc, req);
2843
2844 if (!err)
2845 return outarg.revents;
2846 if (err == -ENOSYS) {
2847 fc->no_poll = 1;
2848 return DEFAULT_POLLMASK;
2849 }
2850 return POLLERR;
2851}
Tejun Heo08cbf542009-04-14 10:54:53 +09002852EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002853
2854/*
2855 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2856 * wakes up the poll waiters.
2857 */
2858int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2859 struct fuse_notify_poll_wakeup_out *outarg)
2860{
2861 u64 kh = outarg->kh;
2862 struct rb_node **link;
2863
2864 spin_lock(&fc->lock);
2865
2866 link = fuse_find_polled_node(fc, kh, NULL);
2867 if (*link) {
2868 struct fuse_file *ff;
2869
2870 ff = rb_entry(*link, struct fuse_file, polled_node);
2871 wake_up_interruptible_sync(&ff->poll_wait);
2872 }
2873
2874 spin_unlock(&fc->lock);
2875 return 0;
2876}
2877
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002878static void fuse_do_truncate(struct file *file)
2879{
2880 struct inode *inode = file->f_mapping->host;
2881 struct iattr attr;
2882
2883 attr.ia_valid = ATTR_SIZE;
2884 attr.ia_size = i_size_read(inode);
2885
2886 attr.ia_file = file;
2887 attr.ia_valid |= ATTR_FILE;
2888
2889 fuse_do_setattr(inode, &attr, file);
2890}
2891
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002892static inline loff_t fuse_round_up(loff_t off)
2893{
2894 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2895}
2896
Anand Avati4273b792012-02-17 12:46:25 -05002897static ssize_t
2898fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2899 loff_t offset, unsigned long nr_segs)
2900{
2901 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002902 struct file *file = iocb->ki_filp;
2903 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002904 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002905 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 struct inode *inode;
2907 loff_t i_size;
2908 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002909 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002910
Anand Avati4273b792012-02-17 12:46:25 -05002911 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002912 inode = file->f_mapping->host;
2913 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002914
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002915 if ((rw == READ) && (offset > i_size))
2916 return 0;
2917
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002918 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002919 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002920 if (offset >= i_size)
2921 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002922 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002923 }
2924
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002925 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002926 if (!io)
2927 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002928 spin_lock_init(&io->lock);
2929 io->reqs = 1;
2930 io->bytes = -1;
2931 io->size = 0;
2932 io->offset = offset;
2933 io->write = (rw == WRITE);
2934 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002935 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002936 /*
2937 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002938 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002939 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002940 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002941 io->iocb = iocb;
2942
2943 /*
2944 * We cannot asynchronously extend the size of a file. We have no method
2945 * to wait on real async I/O requests, so we must submit this request
2946 * synchronously.
2947 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002948 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002949 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002950
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002951 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002952 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002953 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002954 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002955
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002956 if (io->async) {
2957 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2958
2959 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002960 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002961 return -EIOCBQUEUED;
2962
2963 ret = wait_on_sync_kiocb(iocb);
2964 } else {
2965 kfree(io);
2966 }
2967
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002968 if (rw == WRITE) {
2969 if (ret > 0)
2970 fuse_write_update_size(inode, pos);
2971 else if (ret < 0 && offset + count > i_size)
2972 fuse_do_truncate(file);
2973 }
Anand Avati4273b792012-02-17 12:46:25 -05002974
2975 return ret;
2976}
2977
Miklos Szeredicdadb112012-11-10 16:55:56 +01002978static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2979 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002980{
2981 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002982 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002983 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002984 struct fuse_conn *fc = ff->fc;
2985 struct fuse_req *req;
2986 struct fuse_fallocate_in inarg = {
2987 .fh = ff->fh,
2988 .offset = offset,
2989 .length = length,
2990 .mode = mode
2991 };
2992 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002993 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2994 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002995
Miklos Szeredi4adb8302014-04-28 14:19:21 +02002996 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2997 return -EOPNOTSUPP;
2998
Miklos Szeredi519c6042012-04-26 10:56:36 +02002999 if (fc->no_fallocate)
3000 return -EOPNOTSUPP;
3001
Maxim Patlasov14c14412013-06-13 12:16:39 +04003002 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04003003 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04003004 if (mode & FALLOC_FL_PUNCH_HOLE) {
3005 loff_t endbyte = offset + length - 1;
3006 err = filemap_write_and_wait_range(inode->i_mapping,
3007 offset, endbyte);
3008 if (err)
3009 goto out;
3010
3011 fuse_sync_writes(inode);
3012 }
Brian Foster3634a632013-05-17 09:30:32 -04003013 }
3014
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003015 if (!(mode & FALLOC_FL_KEEP_SIZE))
3016 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3017
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04003018 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04003019 if (IS_ERR(req)) {
3020 err = PTR_ERR(req);
3021 goto out;
3022 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003023
3024 req->in.h.opcode = FUSE_FALLOCATE;
3025 req->in.h.nodeid = ff->nodeid;
3026 req->in.numargs = 1;
3027 req->in.args[0].size = sizeof(inarg);
3028 req->in.args[0].value = &inarg;
3029 fuse_request_send(fc, req);
3030 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02003031 if (err == -ENOSYS) {
3032 fc->no_fallocate = 1;
3033 err = -EOPNOTSUPP;
3034 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003035 fuse_put_request(fc, req);
3036
Brian Fosterbee6c302013-05-17 15:27:34 -04003037 if (err)
3038 goto out;
3039
3040 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003041 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3042 bool changed = fuse_write_update_size(inode, offset + length);
3043
Miklos Szeredi93d22692014-04-28 14:19:22 +02003044 if (changed && fc->writeback_cache)
3045 file_update_time(file);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003046 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003047
3048 if (mode & FALLOC_FL_PUNCH_HOLE)
3049 truncate_pagecache_range(inode, offset, offset + length - 1);
3050
3051 fuse_invalidate_attr(inode);
3052
Brian Foster3634a632013-05-17 09:30:32 -04003053out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003054 if (!(mode & FALLOC_FL_KEEP_SIZE))
3055 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3056
Maxim Patlasovbde52782013-09-13 19:19:54 +04003057 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003058 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003059
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003060 return err;
3061}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003062
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003063static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003064 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003065 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003066 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003067 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003068 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003069 .mmap = fuse_file_mmap,
3070 .open = fuse_open,
3071 .flush = fuse_flush,
3072 .release = fuse_release,
3073 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003074 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003075 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003076 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003077 .unlocked_ioctl = fuse_file_ioctl,
3078 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003079 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003080 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003081};
3082
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003083static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003084 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003085 .read = fuse_direct_read,
3086 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003087 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003088 .open = fuse_open,
3089 .flush = fuse_flush,
3090 .release = fuse_release,
3091 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003092 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003093 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003094 .unlocked_ioctl = fuse_file_ioctl,
3095 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003096 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003097 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003098 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003099};
3100
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003101static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003102 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003103 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003104 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003105 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003106 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003107 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003108 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003109 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003110 .write_begin = fuse_write_begin,
3111 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003112};
3113
3114void fuse_init_file_inode(struct inode *inode)
3115{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003116 inode->i_fop = &fuse_file_operations;
3117 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003118}