blob: 3be0301053547e92a38a1a6f775d31ac179ee469 [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 Szeredic756e0a2007-10-16 23:31:00 -070082static struct fuse_file *fuse_file_get(struct fuse_file *ff)
83{
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 Szeredifd72faa2005-11-07 00:59:51 -0800105void fuse_finish_open(struct inode *inode, struct file *file,
106 struct fuse_file *ff, struct fuse_open_out *outarg)
107{
108 if (outarg->open_flags & FOPEN_DIRECT_IO)
109 file->f_op = &fuse_direct_io_file_operations;
110 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700111 invalidate_inode_pages2(inode->i_mapping);
Tejun Heoa7c1b992008-10-16 16:08:57 +0200112 if (outarg->open_flags & FOPEN_NONSEEKABLE)
113 nonseekable_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800114 ff->fh = outarg->fh;
Miklos Szeredida5e4712009-04-28 16:56:36 +0200115 ff->nodeid = get_node_id(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700116 file->private_data = fuse_file_get(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800117}
118
119int fuse_open_common(struct inode *inode, struct file *file, int isdir)
120{
Tejun Heoacf99432008-11-26 12:03:55 +0100121 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700122 struct fuse_open_out outarg;
123 struct fuse_file *ff;
124 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700125
Miklos Szeredidd190d02005-09-30 11:59:02 -0700126 /* VFS checks this, but only _after_ ->open() */
127 if (file->f_flags & O_DIRECT)
128 return -EINVAL;
129
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700130 err = generic_file_open(inode, file);
131 if (err)
132 return err;
133
Tejun Heoacf99432008-11-26 12:03:55 +0100134 ff = fuse_file_alloc(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700135 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800136 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700137
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800138 err = fuse_send_open(inode, file, isdir, &outarg);
139 if (err)
140 fuse_file_free(ff);
141 else {
142 if (isdir)
143 outarg.open_flags &= ~FOPEN_DIRECT_IO;
144 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700145 }
146
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700147 return err;
148}
149
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700150void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800151{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700152 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800153 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700154
155 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800156 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700157 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800158 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700159 req->in.numargs = 1;
160 req->in.args[0].size = sizeof(struct fuse_release_in);
161 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800162}
163
164int fuse_release_common(struct inode *inode, struct file *file, int isdir)
165{
Tejun Heo6b2db282009-04-14 10:54:49 +0900166 struct fuse_conn *fc;
167 struct fuse_file *ff;
168 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700169
Tejun Heo6b2db282009-04-14 10:54:49 +0900170 ff = file->private_data;
171 if (unlikely(!ff))
172 return 0; /* return value is ignored by VFS */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700173
Tejun Heo6b2db282009-04-14 10:54:49 +0900174 fc = get_fuse_conn(inode);
175 req = ff->reserved_req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700176
Tejun Heo6b2db282009-04-14 10:54:49 +0900177 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
178 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
Tejun Heo95668a62008-11-26 12:03:55 +0100179
Tejun Heo6b2db282009-04-14 10:54:49 +0900180 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200181 path_get(&file->f_path);
182 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700183
Tejun Heo6b2db282009-04-14 10:54:49 +0900184 spin_lock(&fc->lock);
185 list_del(&ff->write_entry);
186 if (!RB_EMPTY_NODE(&ff->polled_node))
187 rb_erase(&ff->polled_node, &fc->polled_files);
188 spin_unlock(&fc->lock);
189
190 wake_up_interruptible_sync(&ff->poll_wait);
191 /*
192 * Normally this will send the RELEASE request, however if
193 * some asynchronous READ or WRITE requests are outstanding,
194 * the sending will be delayed.
195 */
196 fuse_file_put(ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197 return 0;
198}
199
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700200static int fuse_open(struct inode *inode, struct file *file)
201{
202 return fuse_open_common(inode, file, 0);
203}
204
205static int fuse_release(struct inode *inode, struct file *file)
206{
207 return fuse_release_common(inode, file, 0);
208}
209
Miklos Szeredi71421252006-06-25 05:48:52 -0700210/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700211 * Scramble the ID space with XTEA, so that the value of the files_struct
212 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700213 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700214u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700215{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700216 u32 *k = fc->scramble_key;
217 u64 v = (unsigned long) id;
218 u32 v0 = v;
219 u32 v1 = v >> 32;
220 u32 sum = 0;
221 int i;
222
223 for (i = 0; i < 32; i++) {
224 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
225 sum += 0x9E3779B9;
226 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
227 }
228
229 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700230}
231
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700232/*
233 * Check if page is under writeback
234 *
235 * This is currently done by walking the list of writepage requests
236 * for the inode, which can be pretty inefficient.
237 */
238static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
239{
240 struct fuse_conn *fc = get_fuse_conn(inode);
241 struct fuse_inode *fi = get_fuse_inode(inode);
242 struct fuse_req *req;
243 bool found = false;
244
245 spin_lock(&fc->lock);
246 list_for_each_entry(req, &fi->writepages, writepages_entry) {
247 pgoff_t curr_index;
248
249 BUG_ON(req->inode != inode);
250 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
251 if (curr_index == index) {
252 found = true;
253 break;
254 }
255 }
256 spin_unlock(&fc->lock);
257
258 return found;
259}
260
261/*
262 * Wait for page writeback to be completed.
263 *
264 * Since fuse doesn't rely on the VM writeback tracking, this has to
265 * use some other means.
266 */
267static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
268{
269 struct fuse_inode *fi = get_fuse_inode(inode);
270
271 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
272 return 0;
273}
274
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700275static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700276{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800277 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700278 struct fuse_conn *fc = get_fuse_conn(inode);
279 struct fuse_file *ff = file->private_data;
280 struct fuse_req *req;
281 struct fuse_flush_in inarg;
282 int err;
283
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800284 if (is_bad_inode(inode))
285 return -EIO;
286
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700287 if (fc->no_flush)
288 return 0;
289
Miklos Szeredi33649c92006-06-25 05:48:52 -0700290 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700291 memset(&inarg, 0, sizeof(inarg));
292 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700293 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700294 req->in.h.opcode = FUSE_FLUSH;
295 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296 req->in.numargs = 1;
297 req->in.args[0].size = sizeof(inarg);
298 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700299 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100300 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700301 err = req->out.h.error;
302 fuse_put_request(fc, req);
303 if (err == -ENOSYS) {
304 fc->no_flush = 1;
305 err = 0;
306 }
307 return err;
308}
309
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700310/*
311 * Wait for all pending writepages on the inode to finish.
312 *
313 * This is currently done by blocking further writes with FUSE_NOWRITE
314 * and waiting for all sent writes to complete.
315 *
316 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
317 * could conflict with truncation.
318 */
319static void fuse_sync_writes(struct inode *inode)
320{
321 fuse_set_nowrite(inode);
322 fuse_release_nowrite(inode);
323}
324
Miklos Szeredi82547982005-09-09 13:10:38 -0700325int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
326 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700327{
328 struct inode *inode = de->d_inode;
329 struct fuse_conn *fc = get_fuse_conn(inode);
330 struct fuse_file *ff = file->private_data;
331 struct fuse_req *req;
332 struct fuse_fsync_in inarg;
333 int err;
334
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800335 if (is_bad_inode(inode))
336 return -EIO;
337
Miklos Szeredi82547982005-09-09 13:10:38 -0700338 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700339 return 0;
340
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700341 /*
342 * Start writeback against all dirty pages of the inode, then
343 * wait for all outstanding writes, before sending the FSYNC
344 * request.
345 */
346 err = write_inode_now(inode, 0);
347 if (err)
348 return err;
349
350 fuse_sync_writes(inode);
351
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700352 req = fuse_get_req(fc);
353 if (IS_ERR(req))
354 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700355
356 memset(&inarg, 0, sizeof(inarg));
357 inarg.fh = ff->fh;
358 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700359 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700360 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700361 req->in.numargs = 1;
362 req->in.args[0].size = sizeof(inarg);
363 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100364 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700365 err = req->out.h.error;
366 fuse_put_request(fc, req);
367 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700368 if (isdir)
369 fc->no_fsyncdir = 1;
370 else
371 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700372 err = 0;
373 }
374 return err;
375}
376
Miklos Szeredi82547982005-09-09 13:10:38 -0700377static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
378{
379 return fuse_fsync_common(file, de, datasync, 0);
380}
381
Miklos Szeredia6643092007-11-28 16:22:00 -0800382void fuse_read_fill(struct fuse_req *req, struct file *file,
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800383 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700384{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700385 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800386 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700387
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800388 inarg->fh = ff->fh;
389 inarg->offset = pos;
390 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800391 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800392 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700393 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700394 req->in.numargs = 1;
395 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800396 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700397 req->out.argvar = 1;
398 req->out.numargs = 1;
399 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700400}
401
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800402static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredif3332112007-10-18 03:07:04 -0700403 struct inode *inode, loff_t pos, size_t count,
404 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700405{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800406 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredif3332112007-10-18 03:07:04 -0700407
Miklos Szeredia6643092007-11-28 16:22:00 -0800408 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700409 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700410 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700411
412 inarg->read_flags |= FUSE_READ_LOCKOWNER;
413 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
414 }
Tejun Heob93f8582008-11-26 12:03:55 +0100415 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800416 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700417}
418
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700419static void fuse_read_update_size(struct inode *inode, loff_t size,
420 u64 attr_ver)
421{
422 struct fuse_conn *fc = get_fuse_conn(inode);
423 struct fuse_inode *fi = get_fuse_inode(inode);
424
425 spin_lock(&fc->lock);
426 if (attr_ver == fi->attr_version && size < inode->i_size) {
427 fi->attr_version = ++fc->attr_version;
428 i_size_write(inode, size);
429 }
430 spin_unlock(&fc->lock);
431}
432
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700433static int fuse_readpage(struct file *file, struct page *page)
434{
435 struct inode *inode = page->mapping->host;
436 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800437 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700438 size_t num_read;
439 loff_t pos = page_offset(page);
440 size_t count = PAGE_CACHE_SIZE;
441 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800442 int err;
443
444 err = -EIO;
445 if (is_bad_inode(inode))
446 goto out;
447
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700448 /*
449 * Page writeback can extend beyond the liftime of the
450 * page-cache page, so make sure we read a properly synced
451 * page.
452 */
453 fuse_wait_on_page_writeback(inode, page->index);
454
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700455 req = fuse_get_req(fc);
456 err = PTR_ERR(req);
457 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 goto out;
459
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700460 attr_ver = fuse_get_attr_version(fc);
461
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200463 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464 req->num_pages = 1;
465 req->pages[0] = page;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700466 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467 err = req->out.h.error;
468 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700469
470 if (!err) {
471 /*
472 * Short read means EOF. If file size is larger, truncate it
473 */
474 if (num_read < count)
475 fuse_read_update_size(inode, pos + num_read, attr_ver);
476
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700477 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700478 }
479
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700480 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 out:
482 unlock_page(page);
483 return err;
484}
485
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800486static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700487{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800488 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700489 size_t count = req->misc.read.in.size;
490 size_t num_read = req->out.args[0].size;
491 struct inode *inode = req->pages[0]->mapping->host;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800492
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700493 /*
494 * Short read means EOF. If file size is larger, truncate it
495 */
496 if (!req->out.h.error && num_read < count) {
497 loff_t pos = page_offset(req->pages[0]) + num_read;
498 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
499 }
500
501 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800502
Miklos Szeredidb50b962005-09-09 13:10:33 -0700503 for (i = 0; i < req->num_pages; i++) {
504 struct page *page = req->pages[i];
505 if (!req->out.h.error)
506 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800507 else
508 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700509 unlock_page(page);
510 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700511 if (req->ff)
512 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800513}
514
Miklos Szeredia6643092007-11-28 16:22:00 -0800515static void fuse_send_readpages(struct fuse_req *req, struct file *file,
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800516 struct inode *inode)
517{
518 struct fuse_conn *fc = get_fuse_conn(inode);
519 loff_t pos = page_offset(req->pages[0]);
520 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200521
522 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800523 req->out.page_zeroing = 1;
Miklos Szeredia6643092007-11-28 16:22:00 -0800524 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700525 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800526 if (fc->async_read) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800527 struct fuse_file *ff = file->private_data;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700528 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800529 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100530 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800531 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100532 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800533 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100534 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800535 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700536}
537
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700538struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700539 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800540 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700541 struct inode *inode;
542};
543
544static int fuse_readpages_fill(void *_data, struct page *page)
545{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700546 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700547 struct fuse_req *req = data->req;
548 struct inode *inode = data->inode;
549 struct fuse_conn *fc = get_fuse_conn(inode);
550
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700551 fuse_wait_on_page_writeback(inode, page->index);
552
Miklos Szeredidb50b962005-09-09 13:10:33 -0700553 if (req->num_pages &&
554 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
555 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
556 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800557 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700558 data->req = req = fuse_get_req(fc);
559 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700560 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700561 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700562 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700563 }
564 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100565 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700566 return 0;
567}
568
569static int fuse_readpages(struct file *file, struct address_space *mapping,
570 struct list_head *pages, unsigned nr_pages)
571{
572 struct inode *inode = mapping->host;
573 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700574 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700575 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800576
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700577 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800578 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800579 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800580
Miklos Szeredia6643092007-11-28 16:22:00 -0800581 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700582 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700583 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700584 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700585 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800586 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700587
588 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700589 if (!err) {
590 if (data.req->num_pages)
Miklos Szeredia6643092007-11-28 16:22:00 -0800591 fuse_send_readpages(data.req, file, inode);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700592 else
593 fuse_put_request(fc, data.req);
594 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800595out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700596 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700597}
598
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800599static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
600 unsigned long nr_segs, loff_t pos)
601{
602 struct inode *inode = iocb->ki_filp->f_mapping->host;
603
604 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
605 int err;
606 /*
607 * If trying to read past EOF, make sure the i_size
608 * attribute is up-to-date.
609 */
610 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
611 if (err)
612 return err;
613 }
614
615 return generic_file_aio_read(iocb, iov, nr_segs, pos);
616}
617
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200618static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
619 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700620{
Miklos Szeredif3332112007-10-18 03:07:04 -0700621 struct fuse_conn *fc = get_fuse_conn(inode);
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;
629 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700630 req->in.numargs = 2;
Miklos Szeredif3332112007-10-18 03:07:04 -0700631 if (fc->minor < 9)
632 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 Szeredif3332112007-10-18 03:07:04 -0700643 struct inode *inode, loff_t pos, size_t count,
644 fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700645{
646 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200647 struct fuse_write_in *inarg = &req->misc.write.in;
648
649 fuse_write_fill(req, file->private_data, inode, pos, count);
650 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 Szeredif3332112007-10-18 03:07:04 -0700709 nres = fuse_send_write(req, file, inode, 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
750 res = fuse_send_write(req, file, inode, pos, count, NULL);
751
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{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800991 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700992 struct fuse_conn *fc = get_fuse_conn(inode);
993 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 Szeredif4975c62009-04-02 14:25:34 +02001004 size_t nbytes = min(count, nmax);
1005 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001006 if (err) {
1007 res = err;
1008 break;
1009 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001010
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001011 if (write)
Miklos Szeredif3332112007-10-18 03:07:04 -07001012 nres = fuse_send_write(req, file, inode, pos, nbytes,
1013 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001014 else
Miklos Szeredif3332112007-10-18 03:07:04 -07001015 nres = fuse_send_read(req, file, inode, pos, nbytes,
1016 current->files);
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 Szeredi2d698b02009-04-28 16:56:36 +02001199 fuse_write_fill(req, ff, inode, 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 Szeredic9f0523d2008-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}