blob: d9458cace4259773558d4c31cffb72c08309d394 [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 Szeredifd72faa2005-11-07 00:59:51 -080018static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19 struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020{
21 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070022 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080023 struct fuse_req *req;
24 int err;
25
Miklos Szeredice1d5a42006-04-10 22:54:58 -070026 req = fuse_get_req(fc);
27 if (IS_ERR(req))
28 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080029
30 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070031 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080034 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
35 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080036 req->in.numargs = 1;
37 req->in.args[0].size = sizeof(inarg);
38 req->in.args[0].value = &inarg;
39 req->out.numargs = 1;
40 req->out.args[0].size = sizeof(*outargp);
41 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010042 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080043 err = req->out.h.error;
44 fuse_put_request(fc, req);
45
46 return err;
47}
48
Tejun Heoacf99432008-11-26 12:03:55 +010049struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080050{
51 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090052
Miklos Szeredifd72faa2005-11-07 00:59:51 -080053 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090054 if (unlikely(!ff))
55 return NULL;
56
Miklos Szeredida5e4712009-04-28 16:56:36 +020057 ff->fc = fc;
Tejun Heo6b2db282009-04-14 10:54:49 +090058 ff->reserved_req = fuse_request_alloc();
59 if (unlikely(!ff->reserved_req)) {
60 kfree(ff);
61 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080062 }
Tejun Heo6b2db282009-04-14 10:54:49 +090063
64 INIT_LIST_HEAD(&ff->write_entry);
65 atomic_set(&ff->count, 0);
66 RB_CLEAR_NODE(&ff->polled_node);
67 init_waitqueue_head(&ff->poll_wait);
68
69 spin_lock(&fc->lock);
70 ff->kh = ++fc->khctr;
71 spin_unlock(&fc->lock);
72
Miklos Szeredifd72faa2005-11-07 00:59:51 -080073 return ff;
74}
75
76void fuse_file_free(struct fuse_file *ff)
77{
Miklos Szeredi33649c92006-06-25 05:48:52 -070078 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080079 kfree(ff);
80}
81
Miklos Szeredic7b71432009-04-28 16:56:37 +020082struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070083{
84 atomic_inc(&ff->count);
85 return ff;
86}
87
Miklos Szeredi819c4b32007-10-16 23:31:04 -070088static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
89{
Miklos Szeredib0be46e2009-04-28 16:56:36 +020090 path_put(&req->misc.release.path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070091}
92
Miklos Szeredic756e0a2007-10-16 23:31:00 -070093static void fuse_file_put(struct fuse_file *ff)
94{
95 if (atomic_dec_and_test(&ff->count)) {
96 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib0be46e2009-04-28 16:56:36 +020097 struct inode *inode = req->misc.release.path.dentry->d_inode;
Miklos Szeredib57d4262008-02-06 01:38:39 -080098 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070099 req->end = fuse_release_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100100 fuse_request_send_background(fc, req);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700101 kfree(ff);
102 }
103}
104
Miklos Szeredic7b71432009-04-28 16:56:37 +0200105void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800106{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200107 struct fuse_file *ff = file->private_data;
108
109 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800110 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200111 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700112 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200113 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200114 nonseekable_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800115}
116
117int fuse_open_common(struct inode *inode, struct file *file, int isdir)
118{
Tejun Heoacf99432008-11-26 12:03:55 +0100119 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700120 struct fuse_open_out outarg;
121 struct fuse_file *ff;
122 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700123
Miklos Szeredidd190d02005-09-30 11:59:02 -0700124 /* VFS checks this, but only _after_ ->open() */
125 if (file->f_flags & O_DIRECT)
126 return -EINVAL;
127
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700128 err = generic_file_open(inode, file);
129 if (err)
130 return err;
131
Tejun Heoacf99432008-11-26 12:03:55 +0100132 ff = fuse_file_alloc(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700133 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800134 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700135
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800136 err = fuse_send_open(inode, file, isdir, &outarg);
137 if (err)
138 fuse_file_free(ff);
139 else {
140 if (isdir)
141 outarg.open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200142 ff->fh = outarg.fh;
143 ff->nodeid = get_node_id(inode);
144 ff->open_flags = outarg.open_flags;
145 file->private_data = fuse_file_get(ff);
146 fuse_finish_open(inode, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700147 }
148
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700149 return err;
150}
151
Miklos Szeredic7b71432009-04-28 16:56:37 +0200152void fuse_release_fill(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800153{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700154 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800155 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700156
157 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800158 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700159 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200160 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700161 req->in.numargs = 1;
162 req->in.args[0].size = sizeof(struct fuse_release_in);
163 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800164}
165
166int fuse_release_common(struct inode *inode, struct file *file, int isdir)
167{
Tejun Heo6b2db282009-04-14 10:54:49 +0900168 struct fuse_conn *fc;
169 struct fuse_file *ff;
170 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700171
Tejun Heo6b2db282009-04-14 10:54:49 +0900172 ff = file->private_data;
173 if (unlikely(!ff))
174 return 0; /* return value is ignored by VFS */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700175
Tejun Heo6b2db282009-04-14 10:54:49 +0900176 fc = get_fuse_conn(inode);
177 req = ff->reserved_req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700178
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179 fuse_release_fill(ff, file->f_flags,
Tejun Heo6b2db282009-04-14 10:54:49 +0900180 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
Tejun Heo95668a62008-11-26 12:03:55 +0100181
Tejun Heo6b2db282009-04-14 10:54:49 +0900182 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200183 path_get(&file->f_path);
184 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700185
Tejun Heo6b2db282009-04-14 10:54:49 +0900186 spin_lock(&fc->lock);
187 list_del(&ff->write_entry);
188 if (!RB_EMPTY_NODE(&ff->polled_node))
189 rb_erase(&ff->polled_node, &fc->polled_files);
190 spin_unlock(&fc->lock);
191
192 wake_up_interruptible_sync(&ff->poll_wait);
193 /*
194 * Normally this will send the RELEASE request, however if
195 * some asynchronous READ or WRITE requests are outstanding,
196 * the sending will be delayed.
197 */
198 fuse_file_put(ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700199 return 0;
200}
201
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700202static int fuse_open(struct inode *inode, struct file *file)
203{
204 return fuse_open_common(inode, file, 0);
205}
206
207static int fuse_release(struct inode *inode, struct file *file)
208{
209 return fuse_release_common(inode, file, 0);
210}
211
Miklos Szeredi71421252006-06-25 05:48:52 -0700212/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700213 * Scramble the ID space with XTEA, so that the value of the files_struct
214 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700215 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700216u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700217{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700218 u32 *k = fc->scramble_key;
219 u64 v = (unsigned long) id;
220 u32 v0 = v;
221 u32 v1 = v >> 32;
222 u32 sum = 0;
223 int i;
224
225 for (i = 0; i < 32; i++) {
226 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
227 sum += 0x9E3779B9;
228 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
229 }
230
231 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700232}
233
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700234/*
235 * Check if page is under writeback
236 *
237 * This is currently done by walking the list of writepage requests
238 * for the inode, which can be pretty inefficient.
239 */
240static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
241{
242 struct fuse_conn *fc = get_fuse_conn(inode);
243 struct fuse_inode *fi = get_fuse_inode(inode);
244 struct fuse_req *req;
245 bool found = false;
246
247 spin_lock(&fc->lock);
248 list_for_each_entry(req, &fi->writepages, writepages_entry) {
249 pgoff_t curr_index;
250
251 BUG_ON(req->inode != inode);
252 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
253 if (curr_index == index) {
254 found = true;
255 break;
256 }
257 }
258 spin_unlock(&fc->lock);
259
260 return found;
261}
262
263/*
264 * Wait for page writeback to be completed.
265 *
266 * Since fuse doesn't rely on the VM writeback tracking, this has to
267 * use some other means.
268 */
269static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
270{
271 struct fuse_inode *fi = get_fuse_inode(inode);
272
273 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
274 return 0;
275}
276
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700277static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700278{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800279 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700280 struct fuse_conn *fc = get_fuse_conn(inode);
281 struct fuse_file *ff = file->private_data;
282 struct fuse_req *req;
283 struct fuse_flush_in inarg;
284 int err;
285
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800286 if (is_bad_inode(inode))
287 return -EIO;
288
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700289 if (fc->no_flush)
290 return 0;
291
Miklos Szeredi33649c92006-06-25 05:48:52 -0700292 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700293 memset(&inarg, 0, sizeof(inarg));
294 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700295 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296 req->in.h.opcode = FUSE_FLUSH;
297 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700298 req->in.numargs = 1;
299 req->in.args[0].size = sizeof(inarg);
300 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700301 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100302 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700303 err = req->out.h.error;
304 fuse_put_request(fc, req);
305 if (err == -ENOSYS) {
306 fc->no_flush = 1;
307 err = 0;
308 }
309 return err;
310}
311
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700312/*
313 * Wait for all pending writepages on the inode to finish.
314 *
315 * This is currently done by blocking further writes with FUSE_NOWRITE
316 * and waiting for all sent writes to complete.
317 *
318 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
319 * could conflict with truncation.
320 */
321static void fuse_sync_writes(struct inode *inode)
322{
323 fuse_set_nowrite(inode);
324 fuse_release_nowrite(inode);
325}
326
Miklos Szeredi82547982005-09-09 13:10:38 -0700327int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
328 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700329{
330 struct inode *inode = de->d_inode;
331 struct fuse_conn *fc = get_fuse_conn(inode);
332 struct fuse_file *ff = file->private_data;
333 struct fuse_req *req;
334 struct fuse_fsync_in inarg;
335 int err;
336
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800337 if (is_bad_inode(inode))
338 return -EIO;
339
Miklos Szeredi82547982005-09-09 13:10:38 -0700340 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700341 return 0;
342
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700343 /*
344 * Start writeback against all dirty pages of the inode, then
345 * wait for all outstanding writes, before sending the FSYNC
346 * request.
347 */
348 err = write_inode_now(inode, 0);
349 if (err)
350 return err;
351
352 fuse_sync_writes(inode);
353
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700354 req = fuse_get_req(fc);
355 if (IS_ERR(req))
356 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700357
358 memset(&inarg, 0, sizeof(inarg));
359 inarg.fh = ff->fh;
360 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700361 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700362 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700363 req->in.numargs = 1;
364 req->in.args[0].size = sizeof(inarg);
365 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100366 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700367 err = req->out.h.error;
368 fuse_put_request(fc, req);
369 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700370 if (isdir)
371 fc->no_fsyncdir = 1;
372 else
373 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700374 err = 0;
375 }
376 return err;
377}
378
Miklos Szeredi82547982005-09-09 13:10:38 -0700379static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
380{
381 return fuse_fsync_common(file, de, datasync, 0);
382}
383
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200384void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
385 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700386{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700387 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800388 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700389
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800390 inarg->fh = ff->fh;
391 inarg->offset = pos;
392 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800393 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800394 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200395 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700396 req->in.numargs = 1;
397 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800398 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700399 req->out.argvar = 1;
400 req->out.numargs = 1;
401 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700402}
403
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800404static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200405 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700406{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200407 struct fuse_file *ff = file->private_data;
408 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700409
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200410 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700411 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700412 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700413
414 inarg->read_flags |= FUSE_READ_LOCKOWNER;
415 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
416 }
Tejun Heob93f8582008-11-26 12:03:55 +0100417 fuse_request_send(fc, req);
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800418 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700419}
420
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700421static void fuse_read_update_size(struct inode *inode, loff_t size,
422 u64 attr_ver)
423{
424 struct fuse_conn *fc = get_fuse_conn(inode);
425 struct fuse_inode *fi = get_fuse_inode(inode);
426
427 spin_lock(&fc->lock);
428 if (attr_ver == fi->attr_version && size < inode->i_size) {
429 fi->attr_version = ++fc->attr_version;
430 i_size_write(inode, size);
431 }
432 spin_unlock(&fc->lock);
433}
434
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435static int fuse_readpage(struct file *file, struct page *page)
436{
437 struct inode *inode = page->mapping->host;
438 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800439 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700440 size_t num_read;
441 loff_t pos = page_offset(page);
442 size_t count = PAGE_CACHE_SIZE;
443 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800444 int err;
445
446 err = -EIO;
447 if (is_bad_inode(inode))
448 goto out;
449
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700450 /*
451 * Page writeback can extend beyond the liftime of the
452 * page-cache page, so make sure we read a properly synced
453 * page.
454 */
455 fuse_wait_on_page_writeback(inode, page->index);
456
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700457 req = fuse_get_req(fc);
458 err = PTR_ERR(req);
459 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460 goto out;
461
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700462 attr_ver = fuse_get_attr_version(fc);
463
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200465 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700466 req->num_pages = 1;
467 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200468 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700469 err = req->out.h.error;
470 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700471
472 if (!err) {
473 /*
474 * Short read means EOF. If file size is larger, truncate it
475 */
476 if (num_read < count)
477 fuse_read_update_size(inode, pos + num_read, attr_ver);
478
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700479 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700480 }
481
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700482 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483 out:
484 unlock_page(page);
485 return err;
486}
487
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800488static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700489{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800490 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700491 size_t count = req->misc.read.in.size;
492 size_t num_read = req->out.args[0].size;
493 struct inode *inode = req->pages[0]->mapping->host;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800494
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700495 /*
496 * Short read means EOF. If file size is larger, truncate it
497 */
498 if (!req->out.h.error && num_read < count) {
499 loff_t pos = page_offset(req->pages[0]) + num_read;
500 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
501 }
502
503 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800504
Miklos Szeredidb50b962005-09-09 13:10:33 -0700505 for (i = 0; i < req->num_pages; i++) {
506 struct page *page = req->pages[i];
507 if (!req->out.h.error)
508 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800509 else
510 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700511 unlock_page(page);
512 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700513 if (req->ff)
514 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800515}
516
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200517static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800518{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200519 struct fuse_file *ff = file->private_data;
520 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800521 loff_t pos = page_offset(req->pages[0]);
522 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200523
524 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800525 req->out.page_zeroing = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200526 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700527 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800528 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700529 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800530 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100531 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800532 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100533 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800534 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100535 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800536 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700537}
538
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700539struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700540 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800541 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700542 struct inode *inode;
543};
544
545static int fuse_readpages_fill(void *_data, struct page *page)
546{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700547 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700548 struct fuse_req *req = data->req;
549 struct inode *inode = data->inode;
550 struct fuse_conn *fc = get_fuse_conn(inode);
551
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700552 fuse_wait_on_page_writeback(inode, page->index);
553
Miklos Szeredidb50b962005-09-09 13:10:33 -0700554 if (req->num_pages &&
555 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
556 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
557 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200558 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700559 data->req = req = fuse_get_req(fc);
560 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700561 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700562 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700563 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700564 }
565 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100566 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700567 return 0;
568}
569
570static int fuse_readpages(struct file *file, struct address_space *mapping,
571 struct list_head *pages, unsigned nr_pages)
572{
573 struct inode *inode = mapping->host;
574 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700575 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700576 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800577
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700578 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800579 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800580 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800581
Miklos Szeredia6643092007-11-28 16:22:00 -0800582 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700583 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700584 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700585 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700586 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800587 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700588
589 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700590 if (!err) {
591 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200592 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700593 else
594 fuse_put_request(fc, data.req);
595 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800596out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700597 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700598}
599
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800600static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
601 unsigned long nr_segs, loff_t pos)
602{
603 struct inode *inode = iocb->ki_filp->f_mapping->host;
604
605 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
606 int err;
607 /*
608 * If trying to read past EOF, make sure the i_size
609 * attribute is up-to-date.
610 */
611 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
612 if (err)
613 return err;
614 }
615
616 return generic_file_aio_read(iocb, iov, nr_segs, pos);
617}
618
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200619static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200620 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700621{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700622 struct fuse_write_in *inarg = &req->misc.write.in;
623 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700624
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700625 inarg->fh = ff->fh;
626 inarg->offset = pos;
627 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700628 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200629 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700630 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200631 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700632 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
633 else
634 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700635 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700636 req->in.args[1].size = count;
637 req->out.numargs = 1;
638 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700639 req->out.args[0].value = outarg;
640}
641
642static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200643 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700644{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200645 struct fuse_file *ff = file->private_data;
646 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200647 struct fuse_write_in *inarg = &req->misc.write.in;
648
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200649 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200650 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700651 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700652 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
653 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
654 }
Tejun Heob93f8582008-11-26 12:03:55 +0100655 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700656 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700657}
658
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700659static int fuse_write_begin(struct file *file, struct address_space *mapping,
660 loff_t pos, unsigned len, unsigned flags,
661 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700662{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700663 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
664
Nick Piggin54566b22009-01-04 12:00:53 -0800665 *pagep = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700666 if (!*pagep)
667 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700668 return 0;
669}
670
Miklos Szeredi854512e2008-04-30 00:54:41 -0700671static void fuse_write_update_size(struct inode *inode, loff_t pos)
672{
673 struct fuse_conn *fc = get_fuse_conn(inode);
674 struct fuse_inode *fi = get_fuse_inode(inode);
675
676 spin_lock(&fc->lock);
677 fi->attr_version = ++fc->attr_version;
678 if (pos > inode->i_size)
679 i_size_write(inode, pos);
680 spin_unlock(&fc->lock);
681}
682
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700683static int fuse_buffered_write(struct file *file, struct inode *inode,
684 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700685{
686 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700687 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700688 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700689 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800690 struct fuse_req *req;
691
692 if (is_bad_inode(inode))
693 return -EIO;
694
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700695 /*
696 * Make sure writepages on the same page are not mixed up with
697 * plain writes.
698 */
699 fuse_wait_on_page_writeback(inode, page->index);
700
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700701 req = fuse_get_req(fc);
702 if (IS_ERR(req))
703 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700704
Miklos Szeredif4975c62009-04-02 14:25:34 +0200705 req->in.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700706 req->num_pages = 1;
707 req->pages[0] = page;
708 req->page_offset = offset;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200709 nres = fuse_send_write(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700710 err = req->out.h.error;
711 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700712 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700713 err = -EIO;
714 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700715 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700716 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700717 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700718 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700719 }
720 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700721 return err ? err : nres;
722}
723
724static int fuse_write_end(struct file *file, struct address_space *mapping,
725 loff_t pos, unsigned len, unsigned copied,
726 struct page *page, void *fsdata)
727{
728 struct inode *inode = mapping->host;
729 int res = 0;
730
731 if (copied)
732 res = fuse_buffered_write(file, inode, pos, copied, page);
733
734 unlock_page(page);
735 page_cache_release(page);
736 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737}
738
Nick Pigginea9b9902008-04-30 00:54:42 -0700739static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
740 struct inode *inode, loff_t pos,
741 size_t count)
742{
743 size_t res;
744 unsigned offset;
745 unsigned i;
746
747 for (i = 0; i < req->num_pages; i++)
748 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
749
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200750 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700751
752 offset = req->page_offset;
753 count = res;
754 for (i = 0; i < req->num_pages; i++) {
755 struct page *page = req->pages[i];
756
757 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
758 SetPageUptodate(page);
759
760 if (count > PAGE_CACHE_SIZE - offset)
761 count -= PAGE_CACHE_SIZE - offset;
762 else
763 count = 0;
764 offset = 0;
765
766 unlock_page(page);
767 page_cache_release(page);
768 }
769
770 return res;
771}
772
773static ssize_t fuse_fill_write_pages(struct fuse_req *req,
774 struct address_space *mapping,
775 struct iov_iter *ii, loff_t pos)
776{
777 struct fuse_conn *fc = get_fuse_conn(mapping->host);
778 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
779 size_t count = 0;
780 int err;
781
Miklos Szeredif4975c62009-04-02 14:25:34 +0200782 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700783 req->page_offset = offset;
784
785 do {
786 size_t tmp;
787 struct page *page;
788 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
789 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
790 iov_iter_count(ii));
791
792 bytes = min_t(size_t, bytes, fc->max_write - count);
793
794 again:
795 err = -EFAULT;
796 if (iov_iter_fault_in_readable(ii, bytes))
797 break;
798
799 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800800 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700801 if (!page)
802 break;
803
804 pagefault_disable();
805 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
806 pagefault_enable();
807 flush_dcache_page(page);
808
809 if (!tmp) {
810 unlock_page(page);
811 page_cache_release(page);
812 bytes = min(bytes, iov_iter_single_seg_count(ii));
813 goto again;
814 }
815
816 err = 0;
817 req->pages[req->num_pages] = page;
818 req->num_pages++;
819
820 iov_iter_advance(ii, tmp);
821 count += tmp;
822 pos += tmp;
823 offset += tmp;
824 if (offset == PAGE_CACHE_SIZE)
825 offset = 0;
826
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700827 if (!fc->big_writes)
828 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700829 } while (iov_iter_count(ii) && count < fc->max_write &&
830 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
831
832 return count > 0 ? count : err;
833}
834
835static ssize_t fuse_perform_write(struct file *file,
836 struct address_space *mapping,
837 struct iov_iter *ii, loff_t pos)
838{
839 struct inode *inode = mapping->host;
840 struct fuse_conn *fc = get_fuse_conn(inode);
841 int err = 0;
842 ssize_t res = 0;
843
844 if (is_bad_inode(inode))
845 return -EIO;
846
847 do {
848 struct fuse_req *req;
849 ssize_t count;
850
851 req = fuse_get_req(fc);
852 if (IS_ERR(req)) {
853 err = PTR_ERR(req);
854 break;
855 }
856
857 count = fuse_fill_write_pages(req, mapping, ii, pos);
858 if (count <= 0) {
859 err = count;
860 } else {
861 size_t num_written;
862
863 num_written = fuse_send_write_pages(req, file, inode,
864 pos, count);
865 err = req->out.h.error;
866 if (!err) {
867 res += num_written;
868 pos += num_written;
869
870 /* break out of the loop on short write */
871 if (num_written != count)
872 err = -EIO;
873 }
874 }
875 fuse_put_request(fc, req);
876 } while (!err && iov_iter_count(ii));
877
878 if (res > 0)
879 fuse_write_update_size(inode, pos);
880
881 fuse_invalidate_attr(inode);
882
883 return res > 0 ? res : err;
884}
885
886static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
887 unsigned long nr_segs, loff_t pos)
888{
889 struct file *file = iocb->ki_filp;
890 struct address_space *mapping = file->f_mapping;
891 size_t count = 0;
892 ssize_t written = 0;
893 struct inode *inode = mapping->host;
894 ssize_t err;
895 struct iov_iter i;
896
897 WARN_ON(iocb->ki_pos != pos);
898
899 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
900 if (err)
901 return err;
902
903 mutex_lock(&inode->i_mutex);
904 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
905
906 /* We can write back this queue in page reclaim */
907 current->backing_dev_info = mapping->backing_dev_info;
908
909 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
910 if (err)
911 goto out;
912
913 if (count == 0)
914 goto out;
915
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200916 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700917 if (err)
918 goto out;
919
920 file_update_time(file);
921
922 iov_iter_init(&i, iov, nr_segs, count, 0);
923 written = fuse_perform_write(file, mapping, &i, pos);
924 if (written >= 0)
925 iocb->ki_pos = pos + written;
926
927out:
928 current->backing_dev_info = NULL;
929 mutex_unlock(&inode->i_mutex);
930
931 return written ? written : err;
932}
933
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700934static void fuse_release_user_pages(struct fuse_req *req, int write)
935{
936 unsigned i;
937
938 for (i = 0; i < req->num_pages; i++) {
939 struct page *page = req->pages[i];
940 if (write)
941 set_page_dirty_lock(page);
942 put_page(page);
943 }
944}
945
946static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200947 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700948{
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200949 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700950 unsigned long user_addr = (unsigned long) buf;
951 unsigned offset = user_addr & ~PAGE_MASK;
952 int npages;
953
Miklos Szeredif4975c62009-04-02 14:25:34 +0200954 /* Special case for kernel I/O: can copy directly into the buffer */
955 if (segment_eq(get_fs(), KERNEL_DS)) {
956 if (write)
957 req->in.args[1].value = (void *) user_addr;
958 else
959 req->out.args[0].value = (void *) user_addr;
960
961 return 0;
962 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700963
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200964 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700965 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -0700966 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700967 down_read(&current->mm->mmap_sem);
Miklos Szeredif4975c62009-04-02 14:25:34 +0200968 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700969 0, req->pages, NULL);
970 up_read(&current->mm->mmap_sem);
971 if (npages < 0)
972 return npages;
973
974 req->num_pages = npages;
975 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200976
977 if (write)
978 req->in.argpages = 1;
979 else
980 req->out.argpages = 1;
981
982 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
983 *nbytesp = min(*nbytesp, nbytes);
984
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700985 return 0;
986}
987
988static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
989 size_t count, loff_t *ppos, int write)
990{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200991 struct fuse_file *ff = file->private_data;
992 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700993 size_t nmax = write ? fc->max_write : fc->max_read;
994 loff_t pos = *ppos;
995 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800996 struct fuse_req *req;
997
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700998 req = fuse_get_req(fc);
999 if (IS_ERR(req))
1000 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001001
1002 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001003 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001004 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001005 size_t nbytes = min(count, nmax);
1006 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001007 if (err) {
1008 res = err;
1009 break;
1010 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001011
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001012 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001013 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001014 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001015 nres = fuse_send_read(req, file, pos, nbytes, owner);
1016
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001017 fuse_release_user_pages(req, !write);
1018 if (req->out.h.error) {
1019 if (!res)
1020 res = req->out.h.error;
1021 break;
1022 } else if (nres > nbytes) {
1023 res = -EIO;
1024 break;
1025 }
1026 count -= nres;
1027 res += nres;
1028 pos += nres;
1029 buf += nres;
1030 if (nres != nbytes)
1031 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001032 if (count) {
1033 fuse_put_request(fc, req);
1034 req = fuse_get_req(fc);
1035 if (IS_ERR(req))
1036 break;
1037 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001038 }
1039 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001040 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001041 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001042
1043 return res;
1044}
1045
1046static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1047 size_t count, loff_t *ppos)
1048{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001049 ssize_t res;
1050 struct inode *inode = file->f_path.dentry->d_inode;
1051
1052 if (is_bad_inode(inode))
1053 return -EIO;
1054
1055 res = fuse_direct_io(file, buf, count, ppos, 0);
1056
1057 fuse_invalidate_attr(inode);
1058
1059 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001060}
1061
1062static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1063 size_t count, loff_t *ppos)
1064{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001065 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001066 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001067
1068 if (is_bad_inode(inode))
1069 return -EIO;
1070
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001071 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001072 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001073 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001074 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001075 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001076 if (res > 0)
1077 fuse_write_update_size(inode, *ppos);
1078 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001079 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001080
1081 fuse_invalidate_attr(inode);
1082
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001083 return res;
1084}
1085
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001086static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001087{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001088 __free_page(req->pages[0]);
1089 fuse_file_put(req->ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001090}
1091
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001092static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001093{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001094 struct inode *inode = req->inode;
1095 struct fuse_inode *fi = get_fuse_inode(inode);
1096 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1097
1098 list_del(&req->writepages_entry);
1099 dec_bdi_stat(bdi, BDI_WRITEBACK);
1100 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1101 bdi_writeout_inc(bdi);
1102 wake_up(&fi->page_waitq);
1103}
1104
1105/* Called under fc->lock, may release and reacquire it */
1106static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001107__releases(&fc->lock)
1108__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001109{
1110 struct fuse_inode *fi = get_fuse_inode(req->inode);
1111 loff_t size = i_size_read(req->inode);
1112 struct fuse_write_in *inarg = &req->misc.write.in;
1113
1114 if (!fc->connected)
1115 goto out_free;
1116
1117 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1118 inarg->size = PAGE_CACHE_SIZE;
1119 } else if (inarg->offset < size) {
1120 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1121 } else {
1122 /* Got truncated off completely */
1123 goto out_free;
1124 }
1125
1126 req->in.args[1].size = inarg->size;
1127 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001128 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001129 return;
1130
1131 out_free:
1132 fuse_writepage_finish(fc, req);
1133 spin_unlock(&fc->lock);
1134 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001135 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001136 spin_lock(&fc->lock);
1137}
1138
1139/*
1140 * If fi->writectr is positive (no truncate or fsync going on) send
1141 * all queued writepage requests.
1142 *
1143 * Called with fc->lock
1144 */
1145void fuse_flush_writepages(struct inode *inode)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001146__releases(&fc->lock)
1147__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001148{
1149 struct fuse_conn *fc = get_fuse_conn(inode);
1150 struct fuse_inode *fi = get_fuse_inode(inode);
1151 struct fuse_req *req;
1152
1153 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1154 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1155 list_del_init(&req->list);
1156 fuse_send_writepage(fc, req);
1157 }
1158}
1159
1160static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1161{
1162 struct inode *inode = req->inode;
1163 struct fuse_inode *fi = get_fuse_inode(inode);
1164
1165 mapping_set_error(inode->i_mapping, req->out.h.error);
1166 spin_lock(&fc->lock);
1167 fi->writectr--;
1168 fuse_writepage_finish(fc, req);
1169 spin_unlock(&fc->lock);
1170 fuse_writepage_free(fc, req);
1171}
1172
1173static int fuse_writepage_locked(struct page *page)
1174{
1175 struct address_space *mapping = page->mapping;
1176 struct inode *inode = mapping->host;
1177 struct fuse_conn *fc = get_fuse_conn(inode);
1178 struct fuse_inode *fi = get_fuse_inode(inode);
1179 struct fuse_req *req;
1180 struct fuse_file *ff;
1181 struct page *tmp_page;
1182
1183 set_page_writeback(page);
1184
1185 req = fuse_request_alloc_nofs();
1186 if (!req)
1187 goto err;
1188
1189 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1190 if (!tmp_page)
1191 goto err_free;
1192
1193 spin_lock(&fc->lock);
1194 BUG_ON(list_empty(&fi->write_files));
1195 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1196 req->ff = fuse_file_get(ff);
1197 spin_unlock(&fc->lock);
1198
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001199 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001200
1201 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001202 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001203 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001204 req->num_pages = 1;
1205 req->pages[0] = tmp_page;
1206 req->page_offset = 0;
1207 req->end = fuse_writepage_end;
1208 req->inode = inode;
1209
1210 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1211 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1212 end_page_writeback(page);
1213
1214 spin_lock(&fc->lock);
1215 list_add(&req->writepages_entry, &fi->writepages);
1216 list_add_tail(&req->list, &fi->queued_writes);
1217 fuse_flush_writepages(inode);
1218 spin_unlock(&fc->lock);
1219
1220 return 0;
1221
1222err_free:
1223 fuse_request_free(req);
1224err:
1225 end_page_writeback(page);
1226 return -ENOMEM;
1227}
1228
1229static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1230{
1231 int err;
1232
1233 err = fuse_writepage_locked(page);
1234 unlock_page(page);
1235
1236 return err;
1237}
1238
1239static int fuse_launder_page(struct page *page)
1240{
1241 int err = 0;
1242 if (clear_page_dirty_for_io(page)) {
1243 struct inode *inode = page->mapping->host;
1244 err = fuse_writepage_locked(page);
1245 if (!err)
1246 fuse_wait_on_page_writeback(inode, page->index);
1247 }
1248 return err;
1249}
1250
1251/*
1252 * Write back dirty pages now, because there may not be any suitable
1253 * open files later
1254 */
1255static void fuse_vma_close(struct vm_area_struct *vma)
1256{
1257 filemap_write_and_wait(vma->vm_file->f_mapping);
1258}
1259
1260/*
1261 * Wait for writeback against this page to complete before allowing it
1262 * to be marked dirty again, and hence written back again, possibly
1263 * before the previous writepage completed.
1264 *
1265 * Block here, instead of in ->writepage(), so that the userspace fs
1266 * can only block processes actually operating on the filesystem.
1267 *
1268 * Otherwise unprivileged userspace fs would be able to block
1269 * unrelated:
1270 *
1271 * - page migration
1272 * - sync(2)
1273 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1274 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001275static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001276{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001277 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001278 /*
1279 * Don't use page->mapping as it may become NULL from a
1280 * concurrent truncate.
1281 */
1282 struct inode *inode = vma->vm_file->f_mapping->host;
1283
1284 fuse_wait_on_page_writeback(inode, page->index);
1285 return 0;
1286}
1287
1288static struct vm_operations_struct fuse_file_vm_ops = {
1289 .close = fuse_vma_close,
1290 .fault = filemap_fault,
1291 .page_mkwrite = fuse_page_mkwrite,
1292};
1293
1294static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1295{
1296 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1297 struct inode *inode = file->f_dentry->d_inode;
1298 struct fuse_conn *fc = get_fuse_conn(inode);
1299 struct fuse_inode *fi = get_fuse_inode(inode);
1300 struct fuse_file *ff = file->private_data;
1301 /*
1302 * file may be written through mmap, so chain it onto the
1303 * inodes's write_file list
1304 */
1305 spin_lock(&fc->lock);
1306 if (list_empty(&ff->write_entry))
1307 list_add(&ff->write_entry, &fi->write_files);
1308 spin_unlock(&fc->lock);
1309 }
1310 file_accessed(file);
1311 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001312 return 0;
1313}
1314
Miklos Szeredifc280c92009-04-02 14:25:35 +02001315static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1316{
1317 /* Can't provide the coherency needed for MAP_SHARED */
1318 if (vma->vm_flags & VM_MAYSHARE)
1319 return -ENODEV;
1320
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001321 invalidate_inode_pages2(file->f_mapping);
1322
Miklos Szeredifc280c92009-04-02 14:25:35 +02001323 return generic_file_mmap(file, vma);
1324}
1325
Miklos Szeredi71421252006-06-25 05:48:52 -07001326static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1327 struct file_lock *fl)
1328{
1329 switch (ffl->type) {
1330 case F_UNLCK:
1331 break;
1332
1333 case F_RDLCK:
1334 case F_WRLCK:
1335 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1336 ffl->end < ffl->start)
1337 return -EIO;
1338
1339 fl->fl_start = ffl->start;
1340 fl->fl_end = ffl->end;
1341 fl->fl_pid = ffl->pid;
1342 break;
1343
1344 default:
1345 return -EIO;
1346 }
1347 fl->fl_type = ffl->type;
1348 return 0;
1349}
1350
1351static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001352 const struct file_lock *fl, int opcode, pid_t pid,
1353 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001354{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001355 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001356 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001357 struct fuse_file *ff = file->private_data;
1358 struct fuse_lk_in *arg = &req->misc.lk_in;
1359
1360 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001361 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001362 arg->lk.start = fl->fl_start;
1363 arg->lk.end = fl->fl_end;
1364 arg->lk.type = fl->fl_type;
1365 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001366 if (flock)
1367 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001368 req->in.h.opcode = opcode;
1369 req->in.h.nodeid = get_node_id(inode);
1370 req->in.numargs = 1;
1371 req->in.args[0].size = sizeof(*arg);
1372 req->in.args[0].value = arg;
1373}
1374
1375static int fuse_getlk(struct file *file, struct file_lock *fl)
1376{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001377 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001378 struct fuse_conn *fc = get_fuse_conn(inode);
1379 struct fuse_req *req;
1380 struct fuse_lk_out outarg;
1381 int err;
1382
1383 req = fuse_get_req(fc);
1384 if (IS_ERR(req))
1385 return PTR_ERR(req);
1386
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001387 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001388 req->out.numargs = 1;
1389 req->out.args[0].size = sizeof(outarg);
1390 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001391 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001392 err = req->out.h.error;
1393 fuse_put_request(fc, req);
1394 if (!err)
1395 err = convert_fuse_file_lock(&outarg.lk, fl);
1396
1397 return err;
1398}
1399
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001400static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001401{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001402 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001403 struct fuse_conn *fc = get_fuse_conn(inode);
1404 struct fuse_req *req;
1405 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1406 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1407 int err;
1408
Miklos Szeredi48e90762008-07-25 01:49:02 -07001409 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1410 /* NLM needs asynchronous locks, which we don't support yet */
1411 return -ENOLCK;
1412 }
1413
Miklos Szeredi71421252006-06-25 05:48:52 -07001414 /* Unlock on close is handled by the flush method */
1415 if (fl->fl_flags & FL_CLOSE)
1416 return 0;
1417
1418 req = fuse_get_req(fc);
1419 if (IS_ERR(req))
1420 return PTR_ERR(req);
1421
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001422 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001423 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001424 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001425 /* locking is restartable */
1426 if (err == -EINTR)
1427 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001428 fuse_put_request(fc, req);
1429 return err;
1430}
1431
1432static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1433{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001434 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001435 struct fuse_conn *fc = get_fuse_conn(inode);
1436 int err;
1437
Miklos Szeredi48e90762008-07-25 01:49:02 -07001438 if (cmd == F_CANCELLK) {
1439 err = 0;
1440 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001441 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001442 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001443 err = 0;
1444 } else
1445 err = fuse_getlk(file, fl);
1446 } else {
1447 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001448 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001449 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001450 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001451 }
1452 return err;
1453}
1454
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001455static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1456{
1457 struct inode *inode = file->f_path.dentry->d_inode;
1458 struct fuse_conn *fc = get_fuse_conn(inode);
1459 int err;
1460
1461 if (fc->no_lock) {
1462 err = flock_lock_file_wait(file, fl);
1463 } else {
1464 /* emulate flock with POSIX locks */
1465 fl->fl_owner = (fl_owner_t) file;
1466 err = fuse_setlk(file, fl, 1);
1467 }
1468
1469 return err;
1470}
1471
Miklos Szeredib2d22722006-12-06 20:35:51 -08001472static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1473{
1474 struct inode *inode = mapping->host;
1475 struct fuse_conn *fc = get_fuse_conn(inode);
1476 struct fuse_req *req;
1477 struct fuse_bmap_in inarg;
1478 struct fuse_bmap_out outarg;
1479 int err;
1480
1481 if (!inode->i_sb->s_bdev || fc->no_bmap)
1482 return 0;
1483
1484 req = fuse_get_req(fc);
1485 if (IS_ERR(req))
1486 return 0;
1487
1488 memset(&inarg, 0, sizeof(inarg));
1489 inarg.block = block;
1490 inarg.blocksize = inode->i_sb->s_blocksize;
1491 req->in.h.opcode = FUSE_BMAP;
1492 req->in.h.nodeid = get_node_id(inode);
1493 req->in.numargs = 1;
1494 req->in.args[0].size = sizeof(inarg);
1495 req->in.args[0].value = &inarg;
1496 req->out.numargs = 1;
1497 req->out.args[0].size = sizeof(outarg);
1498 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001499 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001500 err = req->out.h.error;
1501 fuse_put_request(fc, req);
1502 if (err == -ENOSYS)
1503 fc->no_bmap = 1;
1504
1505 return err ? 0 : outarg.block;
1506}
1507
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001508static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1509{
1510 loff_t retval;
1511 struct inode *inode = file->f_path.dentry->d_inode;
1512
1513 mutex_lock(&inode->i_mutex);
1514 switch (origin) {
1515 case SEEK_END:
Miklos Szeredi769415c2008-10-16 16:08:56 +02001516 retval = fuse_update_attributes(inode, NULL, file, NULL);
1517 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001518 goto exit;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001519 offset += i_size_read(inode);
1520 break;
1521 case SEEK_CUR:
1522 offset += file->f_pos;
1523 }
1524 retval = -EINVAL;
1525 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1526 if (offset != file->f_pos) {
1527 file->f_pos = offset;
1528 file->f_version = 0;
1529 }
1530 retval = offset;
1531 }
Dan Carpenter52916582009-03-27 13:36:10 +03001532exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001533 mutex_unlock(&inode->i_mutex);
1534 return retval;
1535}
1536
Tejun Heo59efec72008-11-26 12:03:55 +01001537static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1538 unsigned int nr_segs, size_t bytes, bool to_user)
1539{
1540 struct iov_iter ii;
1541 int page_idx = 0;
1542
1543 if (!bytes)
1544 return 0;
1545
1546 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1547
1548 while (iov_iter_count(&ii)) {
1549 struct page *page = pages[page_idx++];
1550 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1551 void *kaddr, *map;
1552
1553 kaddr = map = kmap(page);
1554
1555 while (todo) {
1556 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1557 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1558 size_t copy = min(todo, iov_len);
1559 size_t left;
1560
1561 if (!to_user)
1562 left = copy_from_user(kaddr, uaddr, copy);
1563 else
1564 left = copy_to_user(uaddr, kaddr, copy);
1565
1566 if (unlikely(left))
1567 return -EFAULT;
1568
1569 iov_iter_advance(&ii, copy);
1570 todo -= copy;
1571 kaddr += copy;
1572 }
1573
1574 kunmap(map);
1575 }
1576
1577 return 0;
1578}
1579
1580/*
1581 * For ioctls, there is no generic way to determine how much memory
1582 * needs to be read and/or written. Furthermore, ioctls are allowed
1583 * to dereference the passed pointer, so the parameter requires deep
1584 * copying but FUSE has no idea whatsoever about what to copy in or
1585 * out.
1586 *
1587 * This is solved by allowing FUSE server to retry ioctl with
1588 * necessary in/out iovecs. Let's assume the ioctl implementation
1589 * needs to read in the following structure.
1590 *
1591 * struct a {
1592 * char *buf;
1593 * size_t buflen;
1594 * }
1595 *
1596 * On the first callout to FUSE server, inarg->in_size and
1597 * inarg->out_size will be NULL; then, the server completes the ioctl
1598 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1599 * the actual iov array to
1600 *
1601 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1602 *
1603 * which tells FUSE to copy in the requested area and retry the ioctl.
1604 * On the second round, the server has access to the structure and
1605 * from that it can tell what to look for next, so on the invocation,
1606 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1607 *
1608 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1609 * { .iov_base = a.buf, .iov_len = a.buflen } }
1610 *
1611 * FUSE will copy both struct a and the pointed buffer from the
1612 * process doing the ioctl and retry ioctl with both struct a and the
1613 * buffer.
1614 *
1615 * This time, FUSE server has everything it needs and completes ioctl
1616 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1617 *
1618 * Copying data out works the same way.
1619 *
1620 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1621 * automatically initializes in and out iovs by decoding @cmd with
1622 * _IOC_* macros and the server is not allowed to request RETRY. This
1623 * limits ioctl data transfers to well-formed ioctls and is the forced
1624 * behavior for all FUSE servers.
1625 */
1626static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
1627 unsigned long arg, unsigned int flags)
1628{
1629 struct inode *inode = file->f_dentry->d_inode;
1630 struct fuse_file *ff = file->private_data;
1631 struct fuse_conn *fc = get_fuse_conn(inode);
1632 struct fuse_ioctl_in inarg = {
1633 .fh = ff->fh,
1634 .cmd = cmd,
1635 .arg = arg,
1636 .flags = flags
1637 };
1638 struct fuse_ioctl_out outarg;
1639 struct fuse_req *req = NULL;
1640 struct page **pages = NULL;
1641 struct page *iov_page = NULL;
1642 struct iovec *in_iov = NULL, *out_iov = NULL;
1643 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1644 size_t in_size, out_size, transferred;
1645 int err;
1646
1647 /* assume all the iovs returned by client always fits in a page */
1648 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1649
1650 if (!fuse_allow_task(fc, current))
1651 return -EACCES;
1652
1653 err = -EIO;
1654 if (is_bad_inode(inode))
1655 goto out;
1656
1657 err = -ENOMEM;
1658 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1659 iov_page = alloc_page(GFP_KERNEL);
1660 if (!pages || !iov_page)
1661 goto out;
1662
1663 /*
1664 * If restricted, initialize IO parameters as encoded in @cmd.
1665 * RETRY from server is not allowed.
1666 */
1667 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1668 struct iovec *iov = page_address(iov_page);
1669
Miklos Szeredic9f05232008-12-02 14:49:42 +01001670 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001671 iov->iov_len = _IOC_SIZE(cmd);
1672
1673 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1674 in_iov = iov;
1675 in_iovs = 1;
1676 }
1677
1678 if (_IOC_DIR(cmd) & _IOC_READ) {
1679 out_iov = iov;
1680 out_iovs = 1;
1681 }
1682 }
1683
1684 retry:
1685 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1686 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1687
1688 /*
1689 * Out data can be used either for actual out data or iovs,
1690 * make sure there always is at least one page.
1691 */
1692 out_size = max_t(size_t, out_size, PAGE_SIZE);
1693 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1694
1695 /* make sure there are enough buffer pages and init request with them */
1696 err = -ENOMEM;
1697 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1698 goto out;
1699 while (num_pages < max_pages) {
1700 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1701 if (!pages[num_pages])
1702 goto out;
1703 num_pages++;
1704 }
1705
1706 req = fuse_get_req(fc);
1707 if (IS_ERR(req)) {
1708 err = PTR_ERR(req);
1709 req = NULL;
1710 goto out;
1711 }
1712 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1713 req->num_pages = num_pages;
1714
1715 /* okay, let's send it to the client */
1716 req->in.h.opcode = FUSE_IOCTL;
1717 req->in.h.nodeid = get_node_id(inode);
1718 req->in.numargs = 1;
1719 req->in.args[0].size = sizeof(inarg);
1720 req->in.args[0].value = &inarg;
1721 if (in_size) {
1722 req->in.numargs++;
1723 req->in.args[1].size = in_size;
1724 req->in.argpages = 1;
1725
1726 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1727 false);
1728 if (err)
1729 goto out;
1730 }
1731
1732 req->out.numargs = 2;
1733 req->out.args[0].size = sizeof(outarg);
1734 req->out.args[0].value = &outarg;
1735 req->out.args[1].size = out_size;
1736 req->out.argpages = 1;
1737 req->out.argvar = 1;
1738
Tejun Heob93f8582008-11-26 12:03:55 +01001739 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001740 err = req->out.h.error;
1741 transferred = req->out.args[1].size;
1742 fuse_put_request(fc, req);
1743 req = NULL;
1744 if (err)
1745 goto out;
1746
1747 /* did it ask for retry? */
1748 if (outarg.flags & FUSE_IOCTL_RETRY) {
1749 char *vaddr;
1750
1751 /* no retry if in restricted mode */
1752 err = -EIO;
1753 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1754 goto out;
1755
1756 in_iovs = outarg.in_iovs;
1757 out_iovs = outarg.out_iovs;
1758
1759 /*
1760 * Make sure things are in boundary, separate checks
1761 * are to protect against overflow.
1762 */
1763 err = -ENOMEM;
1764 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1765 out_iovs > FUSE_IOCTL_MAX_IOV ||
1766 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1767 goto out;
1768
1769 err = -EIO;
1770 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1771 goto out;
1772
1773 /* okay, copy in iovs and retry */
1774 vaddr = kmap_atomic(pages[0], KM_USER0);
1775 memcpy(page_address(iov_page), vaddr, transferred);
1776 kunmap_atomic(vaddr, KM_USER0);
1777
1778 in_iov = page_address(iov_page);
1779 out_iov = in_iov + in_iovs;
1780
1781 goto retry;
1782 }
1783
1784 err = -EIO;
1785 if (transferred > inarg.out_size)
1786 goto out;
1787
1788 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1789 out:
1790 if (req)
1791 fuse_put_request(fc, req);
1792 if (iov_page)
1793 __free_page(iov_page);
1794 while (num_pages)
1795 __free_page(pages[--num_pages]);
1796 kfree(pages);
1797
1798 return err ? err : outarg.result;
1799}
1800
1801static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1802 unsigned long arg)
1803{
1804 return fuse_file_do_ioctl(file, cmd, arg, 0);
1805}
1806
1807static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1808 unsigned long arg)
1809{
1810 return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
1811}
1812
Tejun Heo95668a62008-11-26 12:03:55 +01001813/*
1814 * All files which have been polled are linked to RB tree
1815 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1816 * find the matching one.
1817 */
1818static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1819 struct rb_node **parent_out)
1820{
1821 struct rb_node **link = &fc->polled_files.rb_node;
1822 struct rb_node *last = NULL;
1823
1824 while (*link) {
1825 struct fuse_file *ff;
1826
1827 last = *link;
1828 ff = rb_entry(last, struct fuse_file, polled_node);
1829
1830 if (kh < ff->kh)
1831 link = &last->rb_left;
1832 else if (kh > ff->kh)
1833 link = &last->rb_right;
1834 else
1835 return link;
1836 }
1837
1838 if (parent_out)
1839 *parent_out = last;
1840 return link;
1841}
1842
1843/*
1844 * The file is about to be polled. Make sure it's on the polled_files
1845 * RB tree. Note that files once added to the polled_files tree are
1846 * not removed before the file is released. This is because a file
1847 * polled once is likely to be polled again.
1848 */
1849static void fuse_register_polled_file(struct fuse_conn *fc,
1850 struct fuse_file *ff)
1851{
1852 spin_lock(&fc->lock);
1853 if (RB_EMPTY_NODE(&ff->polled_node)) {
1854 struct rb_node **link, *parent;
1855
1856 link = fuse_find_polled_node(fc, ff->kh, &parent);
1857 BUG_ON(*link);
1858 rb_link_node(&ff->polled_node, parent, link);
1859 rb_insert_color(&ff->polled_node, &fc->polled_files);
1860 }
1861 spin_unlock(&fc->lock);
1862}
1863
1864static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1865{
1866 struct inode *inode = file->f_dentry->d_inode;
1867 struct fuse_file *ff = file->private_data;
1868 struct fuse_conn *fc = get_fuse_conn(inode);
1869 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1870 struct fuse_poll_out outarg;
1871 struct fuse_req *req;
1872 int err;
1873
1874 if (fc->no_poll)
1875 return DEFAULT_POLLMASK;
1876
1877 poll_wait(file, &ff->poll_wait, wait);
1878
1879 /*
1880 * Ask for notification iff there's someone waiting for it.
1881 * The client may ignore the flag and always notify.
1882 */
1883 if (waitqueue_active(&ff->poll_wait)) {
1884 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1885 fuse_register_polled_file(fc, ff);
1886 }
1887
1888 req = fuse_get_req(fc);
1889 if (IS_ERR(req))
1890 return PTR_ERR(req);
1891
1892 req->in.h.opcode = FUSE_POLL;
1893 req->in.h.nodeid = get_node_id(inode);
1894 req->in.numargs = 1;
1895 req->in.args[0].size = sizeof(inarg);
1896 req->in.args[0].value = &inarg;
1897 req->out.numargs = 1;
1898 req->out.args[0].size = sizeof(outarg);
1899 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001900 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01001901 err = req->out.h.error;
1902 fuse_put_request(fc, req);
1903
1904 if (!err)
1905 return outarg.revents;
1906 if (err == -ENOSYS) {
1907 fc->no_poll = 1;
1908 return DEFAULT_POLLMASK;
1909 }
1910 return POLLERR;
1911}
1912
1913/*
1914 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1915 * wakes up the poll waiters.
1916 */
1917int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1918 struct fuse_notify_poll_wakeup_out *outarg)
1919{
1920 u64 kh = outarg->kh;
1921 struct rb_node **link;
1922
1923 spin_lock(&fc->lock);
1924
1925 link = fuse_find_polled_node(fc, kh, NULL);
1926 if (*link) {
1927 struct fuse_file *ff;
1928
1929 ff = rb_entry(*link, struct fuse_file, polled_node);
1930 wake_up_interruptible_sync(&ff->poll_wait);
1931 }
1932
1933 spin_unlock(&fc->lock);
1934 return 0;
1935}
1936
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001937static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001938 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001939 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001940 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001941 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07001942 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001943 .mmap = fuse_file_mmap,
1944 .open = fuse_open,
1945 .flush = fuse_flush,
1946 .release = fuse_release,
1947 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001948 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001949 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001950 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01001951 .unlocked_ioctl = fuse_file_ioctl,
1952 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01001953 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001954};
1955
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001956static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001957 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001958 .read = fuse_direct_read,
1959 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02001960 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001961 .open = fuse_open,
1962 .flush = fuse_flush,
1963 .release = fuse_release,
1964 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001965 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001966 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01001967 .unlocked_ioctl = fuse_file_ioctl,
1968 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01001969 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02001970 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001971};
1972
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001973static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001974 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001975 .writepage = fuse_writepage,
1976 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07001977 .write_begin = fuse_write_begin,
1978 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07001979 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001980 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08001981 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001982};
1983
1984void fuse_init_file_inode(struct inode *inode)
1985{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07001986 inode->i_fop = &fuse_file_operations;
1987 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001988}