blob: c5de60e873cb463601ada0de59ff51f45772f804 [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>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070015
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080016static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070017
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020018static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
19 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080022 struct fuse_req *req;
23 int err;
24
Miklos Szeredice1d5a42006-04-10 22:54:58 -070025 req = fuse_get_req(fc);
26 if (IS_ERR(req))
27 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070030 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020033 req->in.h.opcode = opcode;
34 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080035 req->in.numargs = 1;
36 req->in.args[0].size = sizeof(inarg);
37 req->in.args[0].value = &inarg;
38 req->out.numargs = 1;
39 req->out.args[0].size = sizeof(*outargp);
40 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010041 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080042 err = req->out.h.error;
43 fuse_put_request(fc, req);
44
45 return err;
46}
47
Tejun Heoacf99432008-11-26 12:03:55 +010048struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080049{
50 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090051
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090053 if (unlikely(!ff))
54 return NULL;
55
Miklos Szeredida5e4712009-04-28 16:56:36 +020056 ff->fc = fc;
Tejun Heo6b2db282009-04-14 10:54:49 +090057 ff->reserved_req = fuse_request_alloc();
58 if (unlikely(!ff->reserved_req)) {
59 kfree(ff);
60 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080061 }
Tejun Heo6b2db282009-04-14 10:54:49 +090062
63 INIT_LIST_HEAD(&ff->write_entry);
64 atomic_set(&ff->count, 0);
65 RB_CLEAR_NODE(&ff->polled_node);
66 init_waitqueue_head(&ff->poll_wait);
67
68 spin_lock(&fc->lock);
69 ff->kh = ++fc->khctr;
70 spin_unlock(&fc->lock);
71
Miklos Szeredifd72faa2005-11-07 00:59:51 -080072 return ff;
73}
74
75void fuse_file_free(struct fuse_file *ff)
76{
Miklos Szeredi33649c92006-06-25 05:48:52 -070077 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080078 kfree(ff);
79}
80
Miklos Szeredic7b71432009-04-28 16:56:37 +020081struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070082{
83 atomic_inc(&ff->count);
84 return ff;
85}
86
Miklos Szeredi819c4b32007-10-16 23:31:04 -070087static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
88{
Miklos Szeredib0be46e2009-04-28 16:56:36 +020089 path_put(&req->misc.release.path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070090}
91
Miklos Szeredic756e0a2007-10-16 23:31:00 -070092static void fuse_file_put(struct fuse_file *ff)
93{
94 if (atomic_dec_and_test(&ff->count)) {
95 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020096
Miklos Szeredi819c4b32007-10-16 23:31:04 -070097 req->end = fuse_release_end;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020098 fuse_request_send_background(ff->fc, req);
Miklos Szeredic756e0a2007-10-16 23:31:00 -070099 kfree(ff);
100 }
101}
102
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200103static int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
104 bool isdir)
105{
106 struct fuse_open_out outarg;
107 struct fuse_file *ff;
108 int err;
109 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
110
111 ff = fuse_file_alloc(fc);
112 if (!ff)
113 return -ENOMEM;
114
115 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
116 if (err) {
117 fuse_file_free(ff);
118 return err;
119 }
120
121 if (isdir)
122 outarg.open_flags &= ~FOPEN_DIRECT_IO;
123
124 ff->fh = outarg.fh;
125 ff->nodeid = nodeid;
126 ff->open_flags = outarg.open_flags;
127 file->private_data = fuse_file_get(ff);
128
129 return 0;
130}
131
Miklos Szeredic7b71432009-04-28 16:56:37 +0200132void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800133{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200134 struct fuse_file *ff = file->private_data;
135
136 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800137 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200138 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700139 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200140 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200141 nonseekable_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800142}
143
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200144int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800145{
Tejun Heoacf99432008-11-26 12:03:55 +0100146 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700147 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700148
Miklos Szeredidd190d02005-09-30 11:59:02 -0700149 /* VFS checks this, but only _after_ ->open() */
150 if (file->f_flags & O_DIRECT)
151 return -EINVAL;
152
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700153 err = generic_file_open(inode, file);
154 if (err)
155 return err;
156
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200157 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800158 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200159 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700160
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200161 fuse_finish_open(inode, file);
162
163 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700164}
165
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200166static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800167{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200168 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700169 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800170 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700171
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200172 spin_lock(&fc->lock);
173 list_del(&ff->write_entry);
174 if (!RB_EMPTY_NODE(&ff->polled_node))
175 rb_erase(&ff->polled_node, &fc->polled_files);
176 spin_unlock(&fc->lock);
177
178 wake_up_interruptible_sync(&ff->poll_wait);
179
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700180 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800181 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700182 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200183 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700184 req->in.numargs = 1;
185 req->in.args[0].size = sizeof(struct fuse_release_in);
186 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800187}
188
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200189void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800190{
Tejun Heo6b2db282009-04-14 10:54:49 +0900191 struct fuse_file *ff;
192 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700193
Tejun Heo6b2db282009-04-14 10:54:49 +0900194 ff = file->private_data;
195 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200196 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700197
Tejun Heo6b2db282009-04-14 10:54:49 +0900198 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200199 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100200
Tejun Heo6b2db282009-04-14 10:54:49 +0900201 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200202 path_get(&file->f_path);
203 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204
Tejun Heo6b2db282009-04-14 10:54:49 +0900205 /*
206 * Normally this will send the RELEASE request, however if
207 * some asynchronous READ or WRITE requests are outstanding,
208 * the sending will be delayed.
209 */
210 fuse_file_put(ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700211}
212
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700213static int fuse_open(struct inode *inode, struct file *file)
214{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200215 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700216}
217
218static int fuse_release(struct inode *inode, struct file *file)
219{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200220 fuse_release_common(file, FUSE_RELEASE);
221
222 /* return value is ignored by VFS */
223 return 0;
224}
225
226void fuse_sync_release(struct fuse_file *ff, int flags)
227{
228 WARN_ON(atomic_read(&ff->count) > 1);
229 fuse_prepare_release(ff, flags, FUSE_RELEASE);
230 ff->reserved_req->force = 1;
231 fuse_request_send(ff->fc, ff->reserved_req);
232 fuse_put_request(ff->fc, ff->reserved_req);
233 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700234}
235
Miklos Szeredi71421252006-06-25 05:48:52 -0700236/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700237 * Scramble the ID space with XTEA, so that the value of the files_struct
238 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700239 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700240u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700241{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700242 u32 *k = fc->scramble_key;
243 u64 v = (unsigned long) id;
244 u32 v0 = v;
245 u32 v1 = v >> 32;
246 u32 sum = 0;
247 int i;
248
249 for (i = 0; i < 32; i++) {
250 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
251 sum += 0x9E3779B9;
252 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
253 }
254
255 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700256}
257
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700258/*
259 * Check if page is under writeback
260 *
261 * This is currently done by walking the list of writepage requests
262 * for the inode, which can be pretty inefficient.
263 */
264static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
265{
266 struct fuse_conn *fc = get_fuse_conn(inode);
267 struct fuse_inode *fi = get_fuse_inode(inode);
268 struct fuse_req *req;
269 bool found = false;
270
271 spin_lock(&fc->lock);
272 list_for_each_entry(req, &fi->writepages, writepages_entry) {
273 pgoff_t curr_index;
274
275 BUG_ON(req->inode != inode);
276 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
277 if (curr_index == index) {
278 found = true;
279 break;
280 }
281 }
282 spin_unlock(&fc->lock);
283
284 return found;
285}
286
287/*
288 * Wait for page writeback to be completed.
289 *
290 * Since fuse doesn't rely on the VM writeback tracking, this has to
291 * use some other means.
292 */
293static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
294{
295 struct fuse_inode *fi = get_fuse_inode(inode);
296
297 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
298 return 0;
299}
300
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700301static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700302{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800303 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700304 struct fuse_conn *fc = get_fuse_conn(inode);
305 struct fuse_file *ff = file->private_data;
306 struct fuse_req *req;
307 struct fuse_flush_in inarg;
308 int err;
309
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800310 if (is_bad_inode(inode))
311 return -EIO;
312
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700313 if (fc->no_flush)
314 return 0;
315
Miklos Szeredi33649c92006-06-25 05:48:52 -0700316 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700317 memset(&inarg, 0, sizeof(inarg));
318 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700319 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700320 req->in.h.opcode = FUSE_FLUSH;
321 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700322 req->in.numargs = 1;
323 req->in.args[0].size = sizeof(inarg);
324 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700325 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100326 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700327 err = req->out.h.error;
328 fuse_put_request(fc, req);
329 if (err == -ENOSYS) {
330 fc->no_flush = 1;
331 err = 0;
332 }
333 return err;
334}
335
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700336/*
337 * Wait for all pending writepages on the inode to finish.
338 *
339 * This is currently done by blocking further writes with FUSE_NOWRITE
340 * and waiting for all sent writes to complete.
341 *
342 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
343 * could conflict with truncation.
344 */
345static void fuse_sync_writes(struct inode *inode)
346{
347 fuse_set_nowrite(inode);
348 fuse_release_nowrite(inode);
349}
350
Miklos Szeredi82547982005-09-09 13:10:38 -0700351int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
352 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700353{
354 struct inode *inode = de->d_inode;
355 struct fuse_conn *fc = get_fuse_conn(inode);
356 struct fuse_file *ff = file->private_data;
357 struct fuse_req *req;
358 struct fuse_fsync_in inarg;
359 int err;
360
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800361 if (is_bad_inode(inode))
362 return -EIO;
363
Miklos Szeredi82547982005-09-09 13:10:38 -0700364 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700365 return 0;
366
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700367 /*
368 * Start writeback against all dirty pages of the inode, then
369 * wait for all outstanding writes, before sending the FSYNC
370 * request.
371 */
372 err = write_inode_now(inode, 0);
373 if (err)
374 return err;
375
376 fuse_sync_writes(inode);
377
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700378 req = fuse_get_req(fc);
379 if (IS_ERR(req))
380 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700381
382 memset(&inarg, 0, sizeof(inarg));
383 inarg.fh = ff->fh;
384 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700385 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700386 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700387 req->in.numargs = 1;
388 req->in.args[0].size = sizeof(inarg);
389 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100390 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700391 err = req->out.h.error;
392 fuse_put_request(fc, req);
393 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700394 if (isdir)
395 fc->no_fsyncdir = 1;
396 else
397 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700398 err = 0;
399 }
400 return err;
401}
402
Miklos Szeredi82547982005-09-09 13:10:38 -0700403static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
404{
405 return fuse_fsync_common(file, de, datasync, 0);
406}
407
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200408void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
409 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700410{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700411 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800412 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700413
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800414 inarg->fh = ff->fh;
415 inarg->offset = pos;
416 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800417 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800418 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200419 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700420 req->in.numargs = 1;
421 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800422 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 req->out.argvar = 1;
424 req->out.numargs = 1;
425 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426}
427
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800428static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200429 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700430{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200431 struct fuse_file *ff = file->private_data;
432 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700433
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200434 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700435 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700436 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700437
438 inarg->read_flags |= FUSE_READ_LOCKOWNER;
439 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
440 }
Tejun Heob93f8582008-11-26 12:03:55 +0100441 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800442 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700443}
444
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700445static void fuse_read_update_size(struct inode *inode, loff_t size,
446 u64 attr_ver)
447{
448 struct fuse_conn *fc = get_fuse_conn(inode);
449 struct fuse_inode *fi = get_fuse_inode(inode);
450
451 spin_lock(&fc->lock);
452 if (attr_ver == fi->attr_version && size < inode->i_size) {
453 fi->attr_version = ++fc->attr_version;
454 i_size_write(inode, size);
455 }
456 spin_unlock(&fc->lock);
457}
458
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700459static int fuse_readpage(struct file *file, struct page *page)
460{
461 struct inode *inode = page->mapping->host;
462 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800463 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700464 size_t num_read;
465 loff_t pos = page_offset(page);
466 size_t count = PAGE_CACHE_SIZE;
467 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800468 int err;
469
470 err = -EIO;
471 if (is_bad_inode(inode))
472 goto out;
473
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700474 /*
475 * Page writeback can extend beyond the liftime of the
476 * page-cache page, so make sure we read a properly synced
477 * page.
478 */
479 fuse_wait_on_page_writeback(inode, page->index);
480
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700481 req = fuse_get_req(fc);
482 err = PTR_ERR(req);
483 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700484 goto out;
485
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700486 attr_ver = fuse_get_attr_version(fc);
487
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700488 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200489 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700490 req->num_pages = 1;
491 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200492 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493 err = req->out.h.error;
494 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700495
496 if (!err) {
497 /*
498 * Short read means EOF. If file size is larger, truncate it
499 */
500 if (num_read < count)
501 fuse_read_update_size(inode, pos + num_read, attr_ver);
502
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700503 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700504 }
505
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700506 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700507 out:
508 unlock_page(page);
509 return err;
510}
511
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800512static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700513{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800514 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700515 size_t count = req->misc.read.in.size;
516 size_t num_read = req->out.args[0].size;
517 struct inode *inode = req->pages[0]->mapping->host;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800518
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700519 /*
520 * Short read means EOF. If file size is larger, truncate it
521 */
522 if (!req->out.h.error && num_read < count) {
523 loff_t pos = page_offset(req->pages[0]) + num_read;
524 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
525 }
526
527 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800528
Miklos Szeredidb50b962005-09-09 13:10:33 -0700529 for (i = 0; i < req->num_pages; i++) {
530 struct page *page = req->pages[i];
531 if (!req->out.h.error)
532 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800533 else
534 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700535 unlock_page(page);
536 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700537 if (req->ff)
538 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800539}
540
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200541static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800542{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200543 struct fuse_file *ff = file->private_data;
544 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800545 loff_t pos = page_offset(req->pages[0]);
546 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200547
548 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800549 req->out.page_zeroing = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200550 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700551 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800552 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700553 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800554 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100555 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800556 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100557 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800558 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100559 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800560 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700561}
562
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700563struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700564 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800565 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700566 struct inode *inode;
567};
568
569static int fuse_readpages_fill(void *_data, struct page *page)
570{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700571 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700572 struct fuse_req *req = data->req;
573 struct inode *inode = data->inode;
574 struct fuse_conn *fc = get_fuse_conn(inode);
575
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700576 fuse_wait_on_page_writeback(inode, page->index);
577
Miklos Szeredidb50b962005-09-09 13:10:33 -0700578 if (req->num_pages &&
579 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
580 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
581 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200582 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700583 data->req = req = fuse_get_req(fc);
584 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700585 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700586 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700587 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700588 }
589 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100590 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700591 return 0;
592}
593
594static int fuse_readpages(struct file *file, struct address_space *mapping,
595 struct list_head *pages, unsigned nr_pages)
596{
597 struct inode *inode = mapping->host;
598 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700599 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700600 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800601
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700602 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800603 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800604 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800605
Miklos Szeredia6643092007-11-28 16:22:00 -0800606 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700607 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700608 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700609 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700610 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800611 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700612
613 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700614 if (!err) {
615 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200616 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700617 else
618 fuse_put_request(fc, data.req);
619 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800620out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700621 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700622}
623
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800624static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
625 unsigned long nr_segs, loff_t pos)
626{
627 struct inode *inode = iocb->ki_filp->f_mapping->host;
628
629 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
630 int err;
631 /*
632 * If trying to read past EOF, make sure the i_size
633 * attribute is up-to-date.
634 */
635 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
636 if (err)
637 return err;
638 }
639
640 return generic_file_aio_read(iocb, iov, nr_segs, pos);
641}
642
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200643static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200644 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700645{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700646 struct fuse_write_in *inarg = &req->misc.write.in;
647 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700648
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700649 inarg->fh = ff->fh;
650 inarg->offset = pos;
651 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700652 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200653 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700654 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200655 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700656 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
657 else
658 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700659 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700660 req->in.args[1].size = count;
661 req->out.numargs = 1;
662 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700663 req->out.args[0].value = outarg;
664}
665
666static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200667 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700668{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200669 struct fuse_file *ff = file->private_data;
670 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200671 struct fuse_write_in *inarg = &req->misc.write.in;
672
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200673 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200674 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700675 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700676 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
677 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
678 }
Tejun Heob93f8582008-11-26 12:03:55 +0100679 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700680 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700681}
682
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700683static int fuse_write_begin(struct file *file, struct address_space *mapping,
684 loff_t pos, unsigned len, unsigned flags,
685 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700686{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700687 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
688
Nick Piggin54566b22009-01-04 12:00:53 -0800689 *pagep = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700690 if (!*pagep)
691 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700692 return 0;
693}
694
Miklos Szeredi854512e2008-04-30 00:54:41 -0700695static void fuse_write_update_size(struct inode *inode, loff_t pos)
696{
697 struct fuse_conn *fc = get_fuse_conn(inode);
698 struct fuse_inode *fi = get_fuse_inode(inode);
699
700 spin_lock(&fc->lock);
701 fi->attr_version = ++fc->attr_version;
702 if (pos > inode->i_size)
703 i_size_write(inode, pos);
704 spin_unlock(&fc->lock);
705}
706
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700707static int fuse_buffered_write(struct file *file, struct inode *inode,
708 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700709{
710 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700711 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700712 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700713 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800714 struct fuse_req *req;
715
716 if (is_bad_inode(inode))
717 return -EIO;
718
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700719 /*
720 * Make sure writepages on the same page are not mixed up with
721 * plain writes.
722 */
723 fuse_wait_on_page_writeback(inode, page->index);
724
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700725 req = fuse_get_req(fc);
726 if (IS_ERR(req))
727 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700728
Miklos Szeredif4975c62009-04-02 14:25:34 +0200729 req->in.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700730 req->num_pages = 1;
731 req->pages[0] = page;
732 req->page_offset = offset;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200733 nres = fuse_send_write(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734 err = req->out.h.error;
735 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700736 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737 err = -EIO;
738 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700739 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700740 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700741 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700742 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700743 }
744 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700745 return err ? err : nres;
746}
747
748static int fuse_write_end(struct file *file, struct address_space *mapping,
749 loff_t pos, unsigned len, unsigned copied,
750 struct page *page, void *fsdata)
751{
752 struct inode *inode = mapping->host;
753 int res = 0;
754
755 if (copied)
756 res = fuse_buffered_write(file, inode, pos, copied, page);
757
758 unlock_page(page);
759 page_cache_release(page);
760 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700761}
762
Nick Pigginea9b9902008-04-30 00:54:42 -0700763static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
764 struct inode *inode, loff_t pos,
765 size_t count)
766{
767 size_t res;
768 unsigned offset;
769 unsigned i;
770
771 for (i = 0; i < req->num_pages; i++)
772 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
773
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200774 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700775
776 offset = req->page_offset;
777 count = res;
778 for (i = 0; i < req->num_pages; i++) {
779 struct page *page = req->pages[i];
780
781 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
782 SetPageUptodate(page);
783
784 if (count > PAGE_CACHE_SIZE - offset)
785 count -= PAGE_CACHE_SIZE - offset;
786 else
787 count = 0;
788 offset = 0;
789
790 unlock_page(page);
791 page_cache_release(page);
792 }
793
794 return res;
795}
796
797static ssize_t fuse_fill_write_pages(struct fuse_req *req,
798 struct address_space *mapping,
799 struct iov_iter *ii, loff_t pos)
800{
801 struct fuse_conn *fc = get_fuse_conn(mapping->host);
802 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
803 size_t count = 0;
804 int err;
805
Miklos Szeredif4975c62009-04-02 14:25:34 +0200806 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700807 req->page_offset = offset;
808
809 do {
810 size_t tmp;
811 struct page *page;
812 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
813 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
814 iov_iter_count(ii));
815
816 bytes = min_t(size_t, bytes, fc->max_write - count);
817
818 again:
819 err = -EFAULT;
820 if (iov_iter_fault_in_readable(ii, bytes))
821 break;
822
823 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800824 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700825 if (!page)
826 break;
827
828 pagefault_disable();
829 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
830 pagefault_enable();
831 flush_dcache_page(page);
832
833 if (!tmp) {
834 unlock_page(page);
835 page_cache_release(page);
836 bytes = min(bytes, iov_iter_single_seg_count(ii));
837 goto again;
838 }
839
840 err = 0;
841 req->pages[req->num_pages] = page;
842 req->num_pages++;
843
844 iov_iter_advance(ii, tmp);
845 count += tmp;
846 pos += tmp;
847 offset += tmp;
848 if (offset == PAGE_CACHE_SIZE)
849 offset = 0;
850
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700851 if (!fc->big_writes)
852 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700853 } while (iov_iter_count(ii) && count < fc->max_write &&
854 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
855
856 return count > 0 ? count : err;
857}
858
859static ssize_t fuse_perform_write(struct file *file,
860 struct address_space *mapping,
861 struct iov_iter *ii, loff_t pos)
862{
863 struct inode *inode = mapping->host;
864 struct fuse_conn *fc = get_fuse_conn(inode);
865 int err = 0;
866 ssize_t res = 0;
867
868 if (is_bad_inode(inode))
869 return -EIO;
870
871 do {
872 struct fuse_req *req;
873 ssize_t count;
874
875 req = fuse_get_req(fc);
876 if (IS_ERR(req)) {
877 err = PTR_ERR(req);
878 break;
879 }
880
881 count = fuse_fill_write_pages(req, mapping, ii, pos);
882 if (count <= 0) {
883 err = count;
884 } else {
885 size_t num_written;
886
887 num_written = fuse_send_write_pages(req, file, inode,
888 pos, count);
889 err = req->out.h.error;
890 if (!err) {
891 res += num_written;
892 pos += num_written;
893
894 /* break out of the loop on short write */
895 if (num_written != count)
896 err = -EIO;
897 }
898 }
899 fuse_put_request(fc, req);
900 } while (!err && iov_iter_count(ii));
901
902 if (res > 0)
903 fuse_write_update_size(inode, pos);
904
905 fuse_invalidate_attr(inode);
906
907 return res > 0 ? res : err;
908}
909
910static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
911 unsigned long nr_segs, loff_t pos)
912{
913 struct file *file = iocb->ki_filp;
914 struct address_space *mapping = file->f_mapping;
915 size_t count = 0;
916 ssize_t written = 0;
917 struct inode *inode = mapping->host;
918 ssize_t err;
919 struct iov_iter i;
920
921 WARN_ON(iocb->ki_pos != pos);
922
923 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
924 if (err)
925 return err;
926
927 mutex_lock(&inode->i_mutex);
928 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
929
930 /* We can write back this queue in page reclaim */
931 current->backing_dev_info = mapping->backing_dev_info;
932
933 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
934 if (err)
935 goto out;
936
937 if (count == 0)
938 goto out;
939
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200940 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700941 if (err)
942 goto out;
943
944 file_update_time(file);
945
946 iov_iter_init(&i, iov, nr_segs, count, 0);
947 written = fuse_perform_write(file, mapping, &i, pos);
948 if (written >= 0)
949 iocb->ki_pos = pos + written;
950
951out:
952 current->backing_dev_info = NULL;
953 mutex_unlock(&inode->i_mutex);
954
955 return written ? written : err;
956}
957
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700958static void fuse_release_user_pages(struct fuse_req *req, int write)
959{
960 unsigned i;
961
962 for (i = 0; i < req->num_pages; i++) {
963 struct page *page = req->pages[i];
964 if (write)
965 set_page_dirty_lock(page);
966 put_page(page);
967 }
968}
969
970static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200971 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700972{
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200973 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700974 unsigned long user_addr = (unsigned long) buf;
975 unsigned offset = user_addr & ~PAGE_MASK;
976 int npages;
977
Miklos Szeredif4975c62009-04-02 14:25:34 +0200978 /* Special case for kernel I/O: can copy directly into the buffer */
979 if (segment_eq(get_fs(), KERNEL_DS)) {
980 if (write)
981 req->in.args[1].value = (void *) user_addr;
982 else
983 req->out.args[0].value = (void *) user_addr;
984
985 return 0;
986 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700987
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200988 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700989 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -0700990 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700991 down_read(&current->mm->mmap_sem);
Miklos Szeredif4975c62009-04-02 14:25:34 +0200992 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700993 0, req->pages, NULL);
994 up_read(&current->mm->mmap_sem);
995 if (npages < 0)
996 return npages;
997
998 req->num_pages = npages;
999 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001000
1001 if (write)
1002 req->in.argpages = 1;
1003 else
1004 req->out.argpages = 1;
1005
1006 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1007 *nbytesp = min(*nbytesp, nbytes);
1008
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001009 return 0;
1010}
1011
1012static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1013 size_t count, loff_t *ppos, int write)
1014{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001015 struct fuse_file *ff = file->private_data;
1016 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001017 size_t nmax = write ? fc->max_write : fc->max_read;
1018 loff_t pos = *ppos;
1019 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001020 struct fuse_req *req;
1021
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001022 req = fuse_get_req(fc);
1023 if (IS_ERR(req))
1024 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001025
1026 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001027 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001028 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001029 size_t nbytes = min(count, nmax);
1030 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001031 if (err) {
1032 res = err;
1033 break;
1034 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001035
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001036 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001037 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001038 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001039 nres = fuse_send_read(req, file, pos, nbytes, owner);
1040
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001041 fuse_release_user_pages(req, !write);
1042 if (req->out.h.error) {
1043 if (!res)
1044 res = req->out.h.error;
1045 break;
1046 } else if (nres > nbytes) {
1047 res = -EIO;
1048 break;
1049 }
1050 count -= nres;
1051 res += nres;
1052 pos += nres;
1053 buf += nres;
1054 if (nres != nbytes)
1055 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001056 if (count) {
1057 fuse_put_request(fc, req);
1058 req = fuse_get_req(fc);
1059 if (IS_ERR(req))
1060 break;
1061 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001062 }
1063 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001064 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001065 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001066
1067 return res;
1068}
1069
1070static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1071 size_t count, loff_t *ppos)
1072{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001073 ssize_t res;
1074 struct inode *inode = file->f_path.dentry->d_inode;
1075
1076 if (is_bad_inode(inode))
1077 return -EIO;
1078
1079 res = fuse_direct_io(file, buf, count, ppos, 0);
1080
1081 fuse_invalidate_attr(inode);
1082
1083 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001084}
1085
1086static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1087 size_t count, loff_t *ppos)
1088{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001089 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001090 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001091
1092 if (is_bad_inode(inode))
1093 return -EIO;
1094
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001095 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001096 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001097 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001098 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001099 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001100 if (res > 0)
1101 fuse_write_update_size(inode, *ppos);
1102 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001103 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001104
1105 fuse_invalidate_attr(inode);
1106
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001107 return res;
1108}
1109
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001110static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001111{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001112 __free_page(req->pages[0]);
1113 fuse_file_put(req->ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001114}
1115
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001116static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001117{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001118 struct inode *inode = req->inode;
1119 struct fuse_inode *fi = get_fuse_inode(inode);
1120 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1121
1122 list_del(&req->writepages_entry);
1123 dec_bdi_stat(bdi, BDI_WRITEBACK);
1124 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1125 bdi_writeout_inc(bdi);
1126 wake_up(&fi->page_waitq);
1127}
1128
1129/* Called under fc->lock, may release and reacquire it */
1130static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001131__releases(&fc->lock)
1132__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001133{
1134 struct fuse_inode *fi = get_fuse_inode(req->inode);
1135 loff_t size = i_size_read(req->inode);
1136 struct fuse_write_in *inarg = &req->misc.write.in;
1137
1138 if (!fc->connected)
1139 goto out_free;
1140
1141 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1142 inarg->size = PAGE_CACHE_SIZE;
1143 } else if (inarg->offset < size) {
1144 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1145 } else {
1146 /* Got truncated off completely */
1147 goto out_free;
1148 }
1149
1150 req->in.args[1].size = inarg->size;
1151 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001152 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001153 return;
1154
1155 out_free:
1156 fuse_writepage_finish(fc, req);
1157 spin_unlock(&fc->lock);
1158 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001159 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001160 spin_lock(&fc->lock);
1161}
1162
1163/*
1164 * If fi->writectr is positive (no truncate or fsync going on) send
1165 * all queued writepage requests.
1166 *
1167 * Called with fc->lock
1168 */
1169void fuse_flush_writepages(struct inode *inode)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001170__releases(&fc->lock)
1171__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001172{
1173 struct fuse_conn *fc = get_fuse_conn(inode);
1174 struct fuse_inode *fi = get_fuse_inode(inode);
1175 struct fuse_req *req;
1176
1177 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1178 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1179 list_del_init(&req->list);
1180 fuse_send_writepage(fc, req);
1181 }
1182}
1183
1184static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1185{
1186 struct inode *inode = req->inode;
1187 struct fuse_inode *fi = get_fuse_inode(inode);
1188
1189 mapping_set_error(inode->i_mapping, req->out.h.error);
1190 spin_lock(&fc->lock);
1191 fi->writectr--;
1192 fuse_writepage_finish(fc, req);
1193 spin_unlock(&fc->lock);
1194 fuse_writepage_free(fc, req);
1195}
1196
1197static int fuse_writepage_locked(struct page *page)
1198{
1199 struct address_space *mapping = page->mapping;
1200 struct inode *inode = mapping->host;
1201 struct fuse_conn *fc = get_fuse_conn(inode);
1202 struct fuse_inode *fi = get_fuse_inode(inode);
1203 struct fuse_req *req;
1204 struct fuse_file *ff;
1205 struct page *tmp_page;
1206
1207 set_page_writeback(page);
1208
1209 req = fuse_request_alloc_nofs();
1210 if (!req)
1211 goto err;
1212
1213 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1214 if (!tmp_page)
1215 goto err_free;
1216
1217 spin_lock(&fc->lock);
1218 BUG_ON(list_empty(&fi->write_files));
1219 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1220 req->ff = fuse_file_get(ff);
1221 spin_unlock(&fc->lock);
1222
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001223 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001224
1225 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001226 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001227 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001228 req->num_pages = 1;
1229 req->pages[0] = tmp_page;
1230 req->page_offset = 0;
1231 req->end = fuse_writepage_end;
1232 req->inode = inode;
1233
1234 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1235 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1236 end_page_writeback(page);
1237
1238 spin_lock(&fc->lock);
1239 list_add(&req->writepages_entry, &fi->writepages);
1240 list_add_tail(&req->list, &fi->queued_writes);
1241 fuse_flush_writepages(inode);
1242 spin_unlock(&fc->lock);
1243
1244 return 0;
1245
1246err_free:
1247 fuse_request_free(req);
1248err:
1249 end_page_writeback(page);
1250 return -ENOMEM;
1251}
1252
1253static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1254{
1255 int err;
1256
1257 err = fuse_writepage_locked(page);
1258 unlock_page(page);
1259
1260 return err;
1261}
1262
1263static int fuse_launder_page(struct page *page)
1264{
1265 int err = 0;
1266 if (clear_page_dirty_for_io(page)) {
1267 struct inode *inode = page->mapping->host;
1268 err = fuse_writepage_locked(page);
1269 if (!err)
1270 fuse_wait_on_page_writeback(inode, page->index);
1271 }
1272 return err;
1273}
1274
1275/*
1276 * Write back dirty pages now, because there may not be any suitable
1277 * open files later
1278 */
1279static void fuse_vma_close(struct vm_area_struct *vma)
1280{
1281 filemap_write_and_wait(vma->vm_file->f_mapping);
1282}
1283
1284/*
1285 * Wait for writeback against this page to complete before allowing it
1286 * to be marked dirty again, and hence written back again, possibly
1287 * before the previous writepage completed.
1288 *
1289 * Block here, instead of in ->writepage(), so that the userspace fs
1290 * can only block processes actually operating on the filesystem.
1291 *
1292 * Otherwise unprivileged userspace fs would be able to block
1293 * unrelated:
1294 *
1295 * - page migration
1296 * - sync(2)
1297 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1298 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001299static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001300{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001301 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001302 /*
1303 * Don't use page->mapping as it may become NULL from a
1304 * concurrent truncate.
1305 */
1306 struct inode *inode = vma->vm_file->f_mapping->host;
1307
1308 fuse_wait_on_page_writeback(inode, page->index);
1309 return 0;
1310}
1311
1312static struct vm_operations_struct fuse_file_vm_ops = {
1313 .close = fuse_vma_close,
1314 .fault = filemap_fault,
1315 .page_mkwrite = fuse_page_mkwrite,
1316};
1317
1318static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1319{
1320 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1321 struct inode *inode = file->f_dentry->d_inode;
1322 struct fuse_conn *fc = get_fuse_conn(inode);
1323 struct fuse_inode *fi = get_fuse_inode(inode);
1324 struct fuse_file *ff = file->private_data;
1325 /*
1326 * file may be written through mmap, so chain it onto the
1327 * inodes's write_file list
1328 */
1329 spin_lock(&fc->lock);
1330 if (list_empty(&ff->write_entry))
1331 list_add(&ff->write_entry, &fi->write_files);
1332 spin_unlock(&fc->lock);
1333 }
1334 file_accessed(file);
1335 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001336 return 0;
1337}
1338
Miklos Szeredifc280c92009-04-02 14:25:35 +02001339static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1340{
1341 /* Can't provide the coherency needed for MAP_SHARED */
1342 if (vma->vm_flags & VM_MAYSHARE)
1343 return -ENODEV;
1344
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001345 invalidate_inode_pages2(file->f_mapping);
1346
Miklos Szeredifc280c92009-04-02 14:25:35 +02001347 return generic_file_mmap(file, vma);
1348}
1349
Miklos Szeredi71421252006-06-25 05:48:52 -07001350static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1351 struct file_lock *fl)
1352{
1353 switch (ffl->type) {
1354 case F_UNLCK:
1355 break;
1356
1357 case F_RDLCK:
1358 case F_WRLCK:
1359 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1360 ffl->end < ffl->start)
1361 return -EIO;
1362
1363 fl->fl_start = ffl->start;
1364 fl->fl_end = ffl->end;
1365 fl->fl_pid = ffl->pid;
1366 break;
1367
1368 default:
1369 return -EIO;
1370 }
1371 fl->fl_type = ffl->type;
1372 return 0;
1373}
1374
1375static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001376 const struct file_lock *fl, int opcode, pid_t pid,
1377 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001378{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001379 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001380 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001381 struct fuse_file *ff = file->private_data;
1382 struct fuse_lk_in *arg = &req->misc.lk_in;
1383
1384 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001385 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001386 arg->lk.start = fl->fl_start;
1387 arg->lk.end = fl->fl_end;
1388 arg->lk.type = fl->fl_type;
1389 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001390 if (flock)
1391 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001392 req->in.h.opcode = opcode;
1393 req->in.h.nodeid = get_node_id(inode);
1394 req->in.numargs = 1;
1395 req->in.args[0].size = sizeof(*arg);
1396 req->in.args[0].value = arg;
1397}
1398
1399static int fuse_getlk(struct file *file, struct file_lock *fl)
1400{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001401 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001402 struct fuse_conn *fc = get_fuse_conn(inode);
1403 struct fuse_req *req;
1404 struct fuse_lk_out outarg;
1405 int err;
1406
1407 req = fuse_get_req(fc);
1408 if (IS_ERR(req))
1409 return PTR_ERR(req);
1410
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001411 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001412 req->out.numargs = 1;
1413 req->out.args[0].size = sizeof(outarg);
1414 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001415 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001416 err = req->out.h.error;
1417 fuse_put_request(fc, req);
1418 if (!err)
1419 err = convert_fuse_file_lock(&outarg.lk, fl);
1420
1421 return err;
1422}
1423
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001424static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001425{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001426 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001427 struct fuse_conn *fc = get_fuse_conn(inode);
1428 struct fuse_req *req;
1429 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1430 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1431 int err;
1432
Miklos Szeredi48e90762008-07-25 01:49:02 -07001433 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1434 /* NLM needs asynchronous locks, which we don't support yet */
1435 return -ENOLCK;
1436 }
1437
Miklos Szeredi71421252006-06-25 05:48:52 -07001438 /* Unlock on close is handled by the flush method */
1439 if (fl->fl_flags & FL_CLOSE)
1440 return 0;
1441
1442 req = fuse_get_req(fc);
1443 if (IS_ERR(req))
1444 return PTR_ERR(req);
1445
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001446 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001447 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001448 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001449 /* locking is restartable */
1450 if (err == -EINTR)
1451 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001452 fuse_put_request(fc, req);
1453 return err;
1454}
1455
1456static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1457{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001458 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001459 struct fuse_conn *fc = get_fuse_conn(inode);
1460 int err;
1461
Miklos Szeredi48e90762008-07-25 01:49:02 -07001462 if (cmd == F_CANCELLK) {
1463 err = 0;
1464 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001465 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001466 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001467 err = 0;
1468 } else
1469 err = fuse_getlk(file, fl);
1470 } else {
1471 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001472 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001473 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001474 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001475 }
1476 return err;
1477}
1478
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001479static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1480{
1481 struct inode *inode = file->f_path.dentry->d_inode;
1482 struct fuse_conn *fc = get_fuse_conn(inode);
1483 int err;
1484
1485 if (fc->no_lock) {
1486 err = flock_lock_file_wait(file, fl);
1487 } else {
1488 /* emulate flock with POSIX locks */
1489 fl->fl_owner = (fl_owner_t) file;
1490 err = fuse_setlk(file, fl, 1);
1491 }
1492
1493 return err;
1494}
1495
Miklos Szeredib2d22722006-12-06 20:35:51 -08001496static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1497{
1498 struct inode *inode = mapping->host;
1499 struct fuse_conn *fc = get_fuse_conn(inode);
1500 struct fuse_req *req;
1501 struct fuse_bmap_in inarg;
1502 struct fuse_bmap_out outarg;
1503 int err;
1504
1505 if (!inode->i_sb->s_bdev || fc->no_bmap)
1506 return 0;
1507
1508 req = fuse_get_req(fc);
1509 if (IS_ERR(req))
1510 return 0;
1511
1512 memset(&inarg, 0, sizeof(inarg));
1513 inarg.block = block;
1514 inarg.blocksize = inode->i_sb->s_blocksize;
1515 req->in.h.opcode = FUSE_BMAP;
1516 req->in.h.nodeid = get_node_id(inode);
1517 req->in.numargs = 1;
1518 req->in.args[0].size = sizeof(inarg);
1519 req->in.args[0].value = &inarg;
1520 req->out.numargs = 1;
1521 req->out.args[0].size = sizeof(outarg);
1522 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001523 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001524 err = req->out.h.error;
1525 fuse_put_request(fc, req);
1526 if (err == -ENOSYS)
1527 fc->no_bmap = 1;
1528
1529 return err ? 0 : outarg.block;
1530}
1531
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001532static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1533{
1534 loff_t retval;
1535 struct inode *inode = file->f_path.dentry->d_inode;
1536
1537 mutex_lock(&inode->i_mutex);
1538 switch (origin) {
1539 case SEEK_END:
Miklos Szeredi769415c2008-10-16 16:08:56 +02001540 retval = fuse_update_attributes(inode, NULL, file, NULL);
1541 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001542 goto exit;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001543 offset += i_size_read(inode);
1544 break;
1545 case SEEK_CUR:
1546 offset += file->f_pos;
1547 }
1548 retval = -EINVAL;
1549 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1550 if (offset != file->f_pos) {
1551 file->f_pos = offset;
1552 file->f_version = 0;
1553 }
1554 retval = offset;
1555 }
Dan Carpenter52916582009-03-27 13:36:10 +03001556exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001557 mutex_unlock(&inode->i_mutex);
1558 return retval;
1559}
1560
Tejun Heo59efec72008-11-26 12:03:55 +01001561static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1562 unsigned int nr_segs, size_t bytes, bool to_user)
1563{
1564 struct iov_iter ii;
1565 int page_idx = 0;
1566
1567 if (!bytes)
1568 return 0;
1569
1570 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1571
1572 while (iov_iter_count(&ii)) {
1573 struct page *page = pages[page_idx++];
1574 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1575 void *kaddr, *map;
1576
1577 kaddr = map = kmap(page);
1578
1579 while (todo) {
1580 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1581 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1582 size_t copy = min(todo, iov_len);
1583 size_t left;
1584
1585 if (!to_user)
1586 left = copy_from_user(kaddr, uaddr, copy);
1587 else
1588 left = copy_to_user(uaddr, kaddr, copy);
1589
1590 if (unlikely(left))
1591 return -EFAULT;
1592
1593 iov_iter_advance(&ii, copy);
1594 todo -= copy;
1595 kaddr += copy;
1596 }
1597
1598 kunmap(map);
1599 }
1600
1601 return 0;
1602}
1603
1604/*
1605 * For ioctls, there is no generic way to determine how much memory
1606 * needs to be read and/or written. Furthermore, ioctls are allowed
1607 * to dereference the passed pointer, so the parameter requires deep
1608 * copying but FUSE has no idea whatsoever about what to copy in or
1609 * out.
1610 *
1611 * This is solved by allowing FUSE server to retry ioctl with
1612 * necessary in/out iovecs. Let's assume the ioctl implementation
1613 * needs to read in the following structure.
1614 *
1615 * struct a {
1616 * char *buf;
1617 * size_t buflen;
1618 * }
1619 *
1620 * On the first callout to FUSE server, inarg->in_size and
1621 * inarg->out_size will be NULL; then, the server completes the ioctl
1622 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1623 * the actual iov array to
1624 *
1625 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1626 *
1627 * which tells FUSE to copy in the requested area and retry the ioctl.
1628 * On the second round, the server has access to the structure and
1629 * from that it can tell what to look for next, so on the invocation,
1630 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1631 *
1632 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1633 * { .iov_base = a.buf, .iov_len = a.buflen } }
1634 *
1635 * FUSE will copy both struct a and the pointed buffer from the
1636 * process doing the ioctl and retry ioctl with both struct a and the
1637 * buffer.
1638 *
1639 * This time, FUSE server has everything it needs and completes ioctl
1640 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1641 *
1642 * Copying data out works the same way.
1643 *
1644 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1645 * automatically initializes in and out iovs by decoding @cmd with
1646 * _IOC_* macros and the server is not allowed to request RETRY. This
1647 * limits ioctl data transfers to well-formed ioctls and is the forced
1648 * behavior for all FUSE servers.
1649 */
Miklos Szeredid36f2482009-04-28 16:56:39 +02001650static long fuse_do_ioctl(struct file *file, unsigned int cmd,
1651 unsigned long arg, unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001652{
Tejun Heo59efec72008-11-26 12:03:55 +01001653 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001654 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001655 struct fuse_ioctl_in inarg = {
1656 .fh = ff->fh,
1657 .cmd = cmd,
1658 .arg = arg,
1659 .flags = flags
1660 };
1661 struct fuse_ioctl_out outarg;
1662 struct fuse_req *req = NULL;
1663 struct page **pages = NULL;
1664 struct page *iov_page = NULL;
1665 struct iovec *in_iov = NULL, *out_iov = NULL;
1666 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1667 size_t in_size, out_size, transferred;
1668 int err;
1669
1670 /* assume all the iovs returned by client always fits in a page */
1671 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1672
Tejun Heo59efec72008-11-26 12:03:55 +01001673 err = -ENOMEM;
1674 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1675 iov_page = alloc_page(GFP_KERNEL);
1676 if (!pages || !iov_page)
1677 goto out;
1678
1679 /*
1680 * If restricted, initialize IO parameters as encoded in @cmd.
1681 * RETRY from server is not allowed.
1682 */
1683 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1684 struct iovec *iov = page_address(iov_page);
1685
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01001686 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001687 iov->iov_len = _IOC_SIZE(cmd);
1688
1689 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1690 in_iov = iov;
1691 in_iovs = 1;
1692 }
1693
1694 if (_IOC_DIR(cmd) & _IOC_READ) {
1695 out_iov = iov;
1696 out_iovs = 1;
1697 }
1698 }
1699
1700 retry:
1701 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1702 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1703
1704 /*
1705 * Out data can be used either for actual out data or iovs,
1706 * make sure there always is at least one page.
1707 */
1708 out_size = max_t(size_t, out_size, PAGE_SIZE);
1709 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1710
1711 /* make sure there are enough buffer pages and init request with them */
1712 err = -ENOMEM;
1713 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1714 goto out;
1715 while (num_pages < max_pages) {
1716 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1717 if (!pages[num_pages])
1718 goto out;
1719 num_pages++;
1720 }
1721
1722 req = fuse_get_req(fc);
1723 if (IS_ERR(req)) {
1724 err = PTR_ERR(req);
1725 req = NULL;
1726 goto out;
1727 }
1728 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1729 req->num_pages = num_pages;
1730
1731 /* okay, let's send it to the client */
1732 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001733 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001734 req->in.numargs = 1;
1735 req->in.args[0].size = sizeof(inarg);
1736 req->in.args[0].value = &inarg;
1737 if (in_size) {
1738 req->in.numargs++;
1739 req->in.args[1].size = in_size;
1740 req->in.argpages = 1;
1741
1742 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1743 false);
1744 if (err)
1745 goto out;
1746 }
1747
1748 req->out.numargs = 2;
1749 req->out.args[0].size = sizeof(outarg);
1750 req->out.args[0].value = &outarg;
1751 req->out.args[1].size = out_size;
1752 req->out.argpages = 1;
1753 req->out.argvar = 1;
1754
Tejun Heob93f8582008-11-26 12:03:55 +01001755 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001756 err = req->out.h.error;
1757 transferred = req->out.args[1].size;
1758 fuse_put_request(fc, req);
1759 req = NULL;
1760 if (err)
1761 goto out;
1762
1763 /* did it ask for retry? */
1764 if (outarg.flags & FUSE_IOCTL_RETRY) {
1765 char *vaddr;
1766
1767 /* no retry if in restricted mode */
1768 err = -EIO;
1769 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1770 goto out;
1771
1772 in_iovs = outarg.in_iovs;
1773 out_iovs = outarg.out_iovs;
1774
1775 /*
1776 * Make sure things are in boundary, separate checks
1777 * are to protect against overflow.
1778 */
1779 err = -ENOMEM;
1780 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1781 out_iovs > FUSE_IOCTL_MAX_IOV ||
1782 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1783 goto out;
1784
1785 err = -EIO;
1786 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1787 goto out;
1788
1789 /* okay, copy in iovs and retry */
1790 vaddr = kmap_atomic(pages[0], KM_USER0);
1791 memcpy(page_address(iov_page), vaddr, transferred);
1792 kunmap_atomic(vaddr, KM_USER0);
1793
1794 in_iov = page_address(iov_page);
1795 out_iov = in_iov + in_iovs;
1796
1797 goto retry;
1798 }
1799
1800 err = -EIO;
1801 if (transferred > inarg.out_size)
1802 goto out;
1803
1804 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1805 out:
1806 if (req)
1807 fuse_put_request(fc, req);
1808 if (iov_page)
1809 __free_page(iov_page);
1810 while (num_pages)
1811 __free_page(pages[--num_pages]);
1812 kfree(pages);
1813
1814 return err ? err : outarg.result;
1815}
1816
Miklos Szeredid36f2482009-04-28 16:56:39 +02001817static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
1818 unsigned long arg, unsigned int flags)
1819{
1820 struct inode *inode = file->f_dentry->d_inode;
1821 struct fuse_conn *fc = get_fuse_conn(inode);
1822
1823 if (!fuse_allow_task(fc, current))
1824 return -EACCES;
1825
1826 if (is_bad_inode(inode))
1827 return -EIO;
1828
1829 return fuse_do_ioctl(file, cmd, arg, flags);
1830}
1831
Tejun Heo59efec72008-11-26 12:03:55 +01001832static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1833 unsigned long arg)
1834{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001835 return fuse_file_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001836}
1837
1838static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1839 unsigned long arg)
1840{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001841 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01001842}
1843
Tejun Heo95668a62008-11-26 12:03:55 +01001844/*
1845 * All files which have been polled are linked to RB tree
1846 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1847 * find the matching one.
1848 */
1849static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1850 struct rb_node **parent_out)
1851{
1852 struct rb_node **link = &fc->polled_files.rb_node;
1853 struct rb_node *last = NULL;
1854
1855 while (*link) {
1856 struct fuse_file *ff;
1857
1858 last = *link;
1859 ff = rb_entry(last, struct fuse_file, polled_node);
1860
1861 if (kh < ff->kh)
1862 link = &last->rb_left;
1863 else if (kh > ff->kh)
1864 link = &last->rb_right;
1865 else
1866 return link;
1867 }
1868
1869 if (parent_out)
1870 *parent_out = last;
1871 return link;
1872}
1873
1874/*
1875 * The file is about to be polled. Make sure it's on the polled_files
1876 * RB tree. Note that files once added to the polled_files tree are
1877 * not removed before the file is released. This is because a file
1878 * polled once is likely to be polled again.
1879 */
1880static void fuse_register_polled_file(struct fuse_conn *fc,
1881 struct fuse_file *ff)
1882{
1883 spin_lock(&fc->lock);
1884 if (RB_EMPTY_NODE(&ff->polled_node)) {
1885 struct rb_node **link, *parent;
1886
1887 link = fuse_find_polled_node(fc, ff->kh, &parent);
1888 BUG_ON(*link);
1889 rb_link_node(&ff->polled_node, parent, link);
1890 rb_insert_color(&ff->polled_node, &fc->polled_files);
1891 }
1892 spin_unlock(&fc->lock);
1893}
1894
1895static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1896{
Tejun Heo95668a62008-11-26 12:03:55 +01001897 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02001898 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01001899 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1900 struct fuse_poll_out outarg;
1901 struct fuse_req *req;
1902 int err;
1903
1904 if (fc->no_poll)
1905 return DEFAULT_POLLMASK;
1906
1907 poll_wait(file, &ff->poll_wait, wait);
1908
1909 /*
1910 * Ask for notification iff there's someone waiting for it.
1911 * The client may ignore the flag and always notify.
1912 */
1913 if (waitqueue_active(&ff->poll_wait)) {
1914 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1915 fuse_register_polled_file(fc, ff);
1916 }
1917
1918 req = fuse_get_req(fc);
1919 if (IS_ERR(req))
1920 return PTR_ERR(req);
1921
1922 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02001923 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01001924 req->in.numargs = 1;
1925 req->in.args[0].size = sizeof(inarg);
1926 req->in.args[0].value = &inarg;
1927 req->out.numargs = 1;
1928 req->out.args[0].size = sizeof(outarg);
1929 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001930 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01001931 err = req->out.h.error;
1932 fuse_put_request(fc, req);
1933
1934 if (!err)
1935 return outarg.revents;
1936 if (err == -ENOSYS) {
1937 fc->no_poll = 1;
1938 return DEFAULT_POLLMASK;
1939 }
1940 return POLLERR;
1941}
1942
1943/*
1944 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1945 * wakes up the poll waiters.
1946 */
1947int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1948 struct fuse_notify_poll_wakeup_out *outarg)
1949{
1950 u64 kh = outarg->kh;
1951 struct rb_node **link;
1952
1953 spin_lock(&fc->lock);
1954
1955 link = fuse_find_polled_node(fc, kh, NULL);
1956 if (*link) {
1957 struct fuse_file *ff;
1958
1959 ff = rb_entry(*link, struct fuse_file, polled_node);
1960 wake_up_interruptible_sync(&ff->poll_wait);
1961 }
1962
1963 spin_unlock(&fc->lock);
1964 return 0;
1965}
1966
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001967static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001968 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001969 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001970 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001971 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07001972 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001973 .mmap = fuse_file_mmap,
1974 .open = fuse_open,
1975 .flush = fuse_flush,
1976 .release = fuse_release,
1977 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001978 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001979 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001980 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01001981 .unlocked_ioctl = fuse_file_ioctl,
1982 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01001983 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001984};
1985
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001986static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001987 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001988 .read = fuse_direct_read,
1989 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02001990 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001991 .open = fuse_open,
1992 .flush = fuse_flush,
1993 .release = fuse_release,
1994 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001995 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001996 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01001997 .unlocked_ioctl = fuse_file_ioctl,
1998 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01001999 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002000 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002001};
2002
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002003static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002004 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002005 .writepage = fuse_writepage,
2006 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07002007 .write_begin = fuse_write_begin,
2008 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002009 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002010 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002011 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002012};
2013
2014void fuse_init_file_inode(struct inode *inode)
2015{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002016 inode->i_fop = &fuse_file_operations;
2017 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002018}