blob: 06e3775b2282d96ca3cb5fa632cb44d726739dba [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070016
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080017static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070018
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020019static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
20 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021{
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 Szeredi91fe96b2009-04-28 16:56:37 +020034 req->in.h.opcode = opcode;
35 req->in.h.nodeid = nodeid;
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 Szeredi8b0797a2009-04-28 16:56:39 +020097
Miklos Szeredi819c4b32007-10-16 23:31:04 -070098 req->end = fuse_release_end;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +020099 fuse_request_send_background(ff->fc, req);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700100 kfree(ff);
101 }
102}
103
Tejun Heo08cbf542009-04-14 10:54:53 +0900104int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
105 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200106{
107 struct fuse_open_out outarg;
108 struct fuse_file *ff;
109 int err;
110 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
111
112 ff = fuse_file_alloc(fc);
113 if (!ff)
114 return -ENOMEM;
115
116 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
117 if (err) {
118 fuse_file_free(ff);
119 return err;
120 }
121
122 if (isdir)
123 outarg.open_flags &= ~FOPEN_DIRECT_IO;
124
125 ff->fh = outarg.fh;
126 ff->nodeid = nodeid;
127 ff->open_flags = outarg.open_flags;
128 file->private_data = fuse_file_get(ff);
129
130 return 0;
131}
Tejun Heo08cbf542009-04-14 10:54:53 +0900132EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200133
Miklos Szeredic7b71432009-04-28 16:56:37 +0200134void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800135{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200136 struct fuse_file *ff = file->private_data;
137
138 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800139 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200140 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700141 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200142 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200143 nonseekable_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800144}
145
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200146int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800147{
Tejun Heoacf99432008-11-26 12:03:55 +0100148 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700149 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700150
Miklos Szeredidd190d02005-09-30 11:59:02 -0700151 /* VFS checks this, but only _after_ ->open() */
152 if (file->f_flags & O_DIRECT)
153 return -EINVAL;
154
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700155 err = generic_file_open(inode, file);
156 if (err)
157 return err;
158
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200159 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800160 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200161 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700162
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200163 fuse_finish_open(inode, file);
164
165 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700166}
167
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200168static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800169{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200170 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700171 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800172 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700173
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200174 spin_lock(&fc->lock);
175 list_del(&ff->write_entry);
176 if (!RB_EMPTY_NODE(&ff->polled_node))
177 rb_erase(&ff->polled_node, &fc->polled_files);
178 spin_unlock(&fc->lock);
179
180 wake_up_interruptible_sync(&ff->poll_wait);
181
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700182 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800183 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700184 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200185 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700186 req->in.numargs = 1;
187 req->in.args[0].size = sizeof(struct fuse_release_in);
188 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800189}
190
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200191void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800192{
Tejun Heo6b2db282009-04-14 10:54:49 +0900193 struct fuse_file *ff;
194 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700195
Tejun Heo6b2db282009-04-14 10:54:49 +0900196 ff = file->private_data;
197 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200198 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700199
Tejun Heo6b2db282009-04-14 10:54:49 +0900200 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200201 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100202
Tejun Heo6b2db282009-04-14 10:54:49 +0900203 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200204 path_get(&file->f_path);
205 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700206
Tejun Heo6b2db282009-04-14 10:54:49 +0900207 /*
208 * Normally this will send the RELEASE request, however if
209 * some asynchronous READ or WRITE requests are outstanding,
210 * the sending will be delayed.
211 */
212 fuse_file_put(ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700213}
214
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700215static int fuse_open(struct inode *inode, struct file *file)
216{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200217 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700218}
219
220static int fuse_release(struct inode *inode, struct file *file)
221{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200222 fuse_release_common(file, FUSE_RELEASE);
223
224 /* return value is ignored by VFS */
225 return 0;
226}
227
228void fuse_sync_release(struct fuse_file *ff, int flags)
229{
230 WARN_ON(atomic_read(&ff->count) > 1);
231 fuse_prepare_release(ff, flags, FUSE_RELEASE);
232 ff->reserved_req->force = 1;
233 fuse_request_send(ff->fc, ff->reserved_req);
234 fuse_put_request(ff->fc, ff->reserved_req);
235 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700236}
Tejun Heo08cbf542009-04-14 10:54:53 +0900237EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700238
Miklos Szeredi71421252006-06-25 05:48:52 -0700239/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700240 * Scramble the ID space with XTEA, so that the value of the files_struct
241 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700242 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700243u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700244{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700245 u32 *k = fc->scramble_key;
246 u64 v = (unsigned long) id;
247 u32 v0 = v;
248 u32 v1 = v >> 32;
249 u32 sum = 0;
250 int i;
251
252 for (i = 0; i < 32; i++) {
253 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
254 sum += 0x9E3779B9;
255 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
256 }
257
258 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700259}
260
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700261/*
262 * Check if page is under writeback
263 *
264 * This is currently done by walking the list of writepage requests
265 * for the inode, which can be pretty inefficient.
266 */
267static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
268{
269 struct fuse_conn *fc = get_fuse_conn(inode);
270 struct fuse_inode *fi = get_fuse_inode(inode);
271 struct fuse_req *req;
272 bool found = false;
273
274 spin_lock(&fc->lock);
275 list_for_each_entry(req, &fi->writepages, writepages_entry) {
276 pgoff_t curr_index;
277
278 BUG_ON(req->inode != inode);
279 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
280 if (curr_index == index) {
281 found = true;
282 break;
283 }
284 }
285 spin_unlock(&fc->lock);
286
287 return found;
288}
289
290/*
291 * Wait for page writeback to be completed.
292 *
293 * Since fuse doesn't rely on the VM writeback tracking, this has to
294 * use some other means.
295 */
296static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
297{
298 struct fuse_inode *fi = get_fuse_inode(inode);
299
300 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
301 return 0;
302}
303
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700304static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700305{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800306 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700307 struct fuse_conn *fc = get_fuse_conn(inode);
308 struct fuse_file *ff = file->private_data;
309 struct fuse_req *req;
310 struct fuse_flush_in inarg;
311 int err;
312
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800313 if (is_bad_inode(inode))
314 return -EIO;
315
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700316 if (fc->no_flush)
317 return 0;
318
Miklos Szeredi33649c92006-06-25 05:48:52 -0700319 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700320 memset(&inarg, 0, sizeof(inarg));
321 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700322 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700323 req->in.h.opcode = FUSE_FLUSH;
324 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700325 req->in.numargs = 1;
326 req->in.args[0].size = sizeof(inarg);
327 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700328 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100329 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700330 err = req->out.h.error;
331 fuse_put_request(fc, req);
332 if (err == -ENOSYS) {
333 fc->no_flush = 1;
334 err = 0;
335 }
336 return err;
337}
338
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700339/*
340 * Wait for all pending writepages on the inode to finish.
341 *
342 * This is currently done by blocking further writes with FUSE_NOWRITE
343 * and waiting for all sent writes to complete.
344 *
345 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
346 * could conflict with truncation.
347 */
348static void fuse_sync_writes(struct inode *inode)
349{
350 fuse_set_nowrite(inode);
351 fuse_release_nowrite(inode);
352}
353
Miklos Szeredi82547982005-09-09 13:10:38 -0700354int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
355 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700356{
357 struct inode *inode = de->d_inode;
358 struct fuse_conn *fc = get_fuse_conn(inode);
359 struct fuse_file *ff = file->private_data;
360 struct fuse_req *req;
361 struct fuse_fsync_in inarg;
362 int err;
363
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800364 if (is_bad_inode(inode))
365 return -EIO;
366
Miklos Szeredi82547982005-09-09 13:10:38 -0700367 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700368 return 0;
369
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700370 /*
371 * Start writeback against all dirty pages of the inode, then
372 * wait for all outstanding writes, before sending the FSYNC
373 * request.
374 */
375 err = write_inode_now(inode, 0);
376 if (err)
377 return err;
378
379 fuse_sync_writes(inode);
380
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700381 req = fuse_get_req(fc);
382 if (IS_ERR(req))
383 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700384
385 memset(&inarg, 0, sizeof(inarg));
386 inarg.fh = ff->fh;
387 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700388 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700389 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700390 req->in.numargs = 1;
391 req->in.args[0].size = sizeof(inarg);
392 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100393 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700394 err = req->out.h.error;
395 fuse_put_request(fc, req);
396 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700397 if (isdir)
398 fc->no_fsyncdir = 1;
399 else
400 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700401 err = 0;
402 }
403 return err;
404}
405
Miklos Szeredi82547982005-09-09 13:10:38 -0700406static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
407{
408 return fuse_fsync_common(file, de, datasync, 0);
409}
410
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200411void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
412 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700413{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700414 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800415 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700416
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800417 inarg->fh = ff->fh;
418 inarg->offset = pos;
419 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800420 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800421 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200422 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 req->in.numargs = 1;
424 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800425 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700426 req->out.argvar = 1;
427 req->out.numargs = 1;
428 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700429}
430
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800431static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200432 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700433{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200434 struct fuse_file *ff = file->private_data;
435 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700436
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200437 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700438 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700439 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700440
441 inarg->read_flags |= FUSE_READ_LOCKOWNER;
442 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
443 }
Tejun Heob93f8582008-11-26 12:03:55 +0100444 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800445 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700446}
447
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700448static void fuse_read_update_size(struct inode *inode, loff_t size,
449 u64 attr_ver)
450{
451 struct fuse_conn *fc = get_fuse_conn(inode);
452 struct fuse_inode *fi = get_fuse_inode(inode);
453
454 spin_lock(&fc->lock);
455 if (attr_ver == fi->attr_version && size < inode->i_size) {
456 fi->attr_version = ++fc->attr_version;
457 i_size_write(inode, size);
458 }
459 spin_unlock(&fc->lock);
460}
461
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462static int fuse_readpage(struct file *file, struct page *page)
463{
464 struct inode *inode = page->mapping->host;
465 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800466 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700467 size_t num_read;
468 loff_t pos = page_offset(page);
469 size_t count = PAGE_CACHE_SIZE;
470 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800471 int err;
472
473 err = -EIO;
474 if (is_bad_inode(inode))
475 goto out;
476
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700477 /*
478 * Page writeback can extend beyond the liftime of the
479 * page-cache page, so make sure we read a properly synced
480 * page.
481 */
482 fuse_wait_on_page_writeback(inode, page->index);
483
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700484 req = fuse_get_req(fc);
485 err = PTR_ERR(req);
486 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700487 goto out;
488
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700489 attr_ver = fuse_get_attr_version(fc);
490
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700491 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200492 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493 req->num_pages = 1;
494 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200495 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700496 err = req->out.h.error;
497 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700498
499 if (!err) {
500 /*
501 * Short read means EOF. If file size is larger, truncate it
502 */
503 if (num_read < count)
504 fuse_read_update_size(inode, pos + num_read, attr_ver);
505
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700506 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700507 }
508
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700509 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700510 out:
511 unlock_page(page);
512 return err;
513}
514
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800515static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700516{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800517 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700518 size_t count = req->misc.read.in.size;
519 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200520 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800521
Miklos Szeredice534fb2010-05-25 15:06:07 +0200522 for (i = 0; mapping == NULL && i < req->num_pages; i++)
523 mapping = req->pages[i]->mapping;
524
525 if (mapping) {
526 struct inode *inode = mapping->host;
527
528 /*
529 * Short read means EOF. If file size is larger, truncate it
530 */
531 if (!req->out.h.error && num_read < count) {
532 loff_t pos;
533
534 pos = page_offset(req->pages[0]) + num_read;
535 fuse_read_update_size(inode, pos,
536 req->misc.read.attr_ver);
537 }
538 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700539 }
540
Miklos Szeredidb50b962005-09-09 13:10:33 -0700541 for (i = 0; i < req->num_pages; i++) {
542 struct page *page = req->pages[i];
543 if (!req->out.h.error)
544 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800545 else
546 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700547 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200548 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700549 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700550 if (req->ff)
551 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800552}
553
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200554static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800555{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200556 struct fuse_file *ff = file->private_data;
557 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800558 loff_t pos = page_offset(req->pages[0]);
559 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200560
561 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800562 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200563 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200564 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700565 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800566 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700567 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800568 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100569 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800570 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100571 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800572 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100573 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800574 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700575}
576
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700577struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700578 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800579 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700580 struct inode *inode;
581};
582
583static int fuse_readpages_fill(void *_data, struct page *page)
584{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700585 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700586 struct fuse_req *req = data->req;
587 struct inode *inode = data->inode;
588 struct fuse_conn *fc = get_fuse_conn(inode);
589
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700590 fuse_wait_on_page_writeback(inode, page->index);
591
Miklos Szeredidb50b962005-09-09 13:10:33 -0700592 if (req->num_pages &&
593 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
594 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
595 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200596 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700597 data->req = req = fuse_get_req(fc);
598 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700599 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700600 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700601 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700602 }
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200603 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700604 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100605 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700606 return 0;
607}
608
609static int fuse_readpages(struct file *file, struct address_space *mapping,
610 struct list_head *pages, unsigned nr_pages)
611{
612 struct inode *inode = mapping->host;
613 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700614 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700615 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800616
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700617 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800618 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800619 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800620
Miklos Szeredia6643092007-11-28 16:22:00 -0800621 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700622 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700623 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700624 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700625 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800626 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700627
628 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700629 if (!err) {
630 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200631 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700632 else
633 fuse_put_request(fc, data.req);
634 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800635out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700636 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700637}
638
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800639static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
640 unsigned long nr_segs, loff_t pos)
641{
642 struct inode *inode = iocb->ki_filp->f_mapping->host;
643
644 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
645 int err;
646 /*
647 * If trying to read past EOF, make sure the i_size
648 * attribute is up-to-date.
649 */
650 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
651 if (err)
652 return err;
653 }
654
655 return generic_file_aio_read(iocb, iov, nr_segs, pos);
656}
657
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200658static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200659 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700660{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700661 struct fuse_write_in *inarg = &req->misc.write.in;
662 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700663
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700664 inarg->fh = ff->fh;
665 inarg->offset = pos;
666 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700667 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200668 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700669 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200670 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700671 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
672 else
673 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700674 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700675 req->in.args[1].size = count;
676 req->out.numargs = 1;
677 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700678 req->out.args[0].value = outarg;
679}
680
681static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200682 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700683{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200684 struct fuse_file *ff = file->private_data;
685 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200686 struct fuse_write_in *inarg = &req->misc.write.in;
687
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200688 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200689 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700690 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700691 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
692 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
693 }
Tejun Heob93f8582008-11-26 12:03:55 +0100694 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700695 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700696}
697
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700698static int fuse_write_begin(struct file *file, struct address_space *mapping,
699 loff_t pos, unsigned len, unsigned flags,
700 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700701{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700702 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
703
Nick Piggin54566b22009-01-04 12:00:53 -0800704 *pagep = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700705 if (!*pagep)
706 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700707 return 0;
708}
709
Miklos Szeredi854512e2008-04-30 00:54:41 -0700710static void fuse_write_update_size(struct inode *inode, loff_t pos)
711{
712 struct fuse_conn *fc = get_fuse_conn(inode);
713 struct fuse_inode *fi = get_fuse_inode(inode);
714
715 spin_lock(&fc->lock);
716 fi->attr_version = ++fc->attr_version;
717 if (pos > inode->i_size)
718 i_size_write(inode, pos);
719 spin_unlock(&fc->lock);
720}
721
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700722static int fuse_buffered_write(struct file *file, struct inode *inode,
723 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700724{
725 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700726 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700727 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700728 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800729 struct fuse_req *req;
730
731 if (is_bad_inode(inode))
732 return -EIO;
733
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700734 /*
735 * Make sure writepages on the same page are not mixed up with
736 * plain writes.
737 */
738 fuse_wait_on_page_writeback(inode, page->index);
739
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700740 req = fuse_get_req(fc);
741 if (IS_ERR(req))
742 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700743
Miklos Szeredif4975c62009-04-02 14:25:34 +0200744 req->in.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 req->num_pages = 1;
746 req->pages[0] = page;
747 req->page_offset = offset;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200748 nres = fuse_send_write(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700749 err = req->out.h.error;
750 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700751 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700752 err = -EIO;
753 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700754 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700755 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700756 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700757 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700758 }
759 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700760 return err ? err : nres;
761}
762
763static int fuse_write_end(struct file *file, struct address_space *mapping,
764 loff_t pos, unsigned len, unsigned copied,
765 struct page *page, void *fsdata)
766{
767 struct inode *inode = mapping->host;
768 int res = 0;
769
770 if (copied)
771 res = fuse_buffered_write(file, inode, pos, copied, page);
772
773 unlock_page(page);
774 page_cache_release(page);
775 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700776}
777
Nick Pigginea9b9902008-04-30 00:54:42 -0700778static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
779 struct inode *inode, loff_t pos,
780 size_t count)
781{
782 size_t res;
783 unsigned offset;
784 unsigned i;
785
786 for (i = 0; i < req->num_pages; i++)
787 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
788
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200789 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700790
791 offset = req->page_offset;
792 count = res;
793 for (i = 0; i < req->num_pages; i++) {
794 struct page *page = req->pages[i];
795
796 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
797 SetPageUptodate(page);
798
799 if (count > PAGE_CACHE_SIZE - offset)
800 count -= PAGE_CACHE_SIZE - offset;
801 else
802 count = 0;
803 offset = 0;
804
805 unlock_page(page);
806 page_cache_release(page);
807 }
808
809 return res;
810}
811
812static ssize_t fuse_fill_write_pages(struct fuse_req *req,
813 struct address_space *mapping,
814 struct iov_iter *ii, loff_t pos)
815{
816 struct fuse_conn *fc = get_fuse_conn(mapping->host);
817 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
818 size_t count = 0;
819 int err;
820
Miklos Szeredif4975c62009-04-02 14:25:34 +0200821 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700822 req->page_offset = offset;
823
824 do {
825 size_t tmp;
826 struct page *page;
827 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
828 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
829 iov_iter_count(ii));
830
831 bytes = min_t(size_t, bytes, fc->max_write - count);
832
833 again:
834 err = -EFAULT;
835 if (iov_iter_fault_in_readable(ii, bytes))
836 break;
837
838 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800839 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700840 if (!page)
841 break;
842
anfei zhou931e80e2010-02-02 13:44:02 -0800843 if (mapping_writably_mapped(mapping))
844 flush_dcache_page(page);
845
Nick Pigginea9b9902008-04-30 00:54:42 -0700846 pagefault_disable();
847 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
848 pagefault_enable();
849 flush_dcache_page(page);
850
851 if (!tmp) {
852 unlock_page(page);
853 page_cache_release(page);
854 bytes = min(bytes, iov_iter_single_seg_count(ii));
855 goto again;
856 }
857
858 err = 0;
859 req->pages[req->num_pages] = page;
860 req->num_pages++;
861
862 iov_iter_advance(ii, tmp);
863 count += tmp;
864 pos += tmp;
865 offset += tmp;
866 if (offset == PAGE_CACHE_SIZE)
867 offset = 0;
868
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700869 if (!fc->big_writes)
870 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700871 } while (iov_iter_count(ii) && count < fc->max_write &&
872 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
873
874 return count > 0 ? count : err;
875}
876
877static ssize_t fuse_perform_write(struct file *file,
878 struct address_space *mapping,
879 struct iov_iter *ii, loff_t pos)
880{
881 struct inode *inode = mapping->host;
882 struct fuse_conn *fc = get_fuse_conn(inode);
883 int err = 0;
884 ssize_t res = 0;
885
886 if (is_bad_inode(inode))
887 return -EIO;
888
889 do {
890 struct fuse_req *req;
891 ssize_t count;
892
893 req = fuse_get_req(fc);
894 if (IS_ERR(req)) {
895 err = PTR_ERR(req);
896 break;
897 }
898
899 count = fuse_fill_write_pages(req, mapping, ii, pos);
900 if (count <= 0) {
901 err = count;
902 } else {
903 size_t num_written;
904
905 num_written = fuse_send_write_pages(req, file, inode,
906 pos, count);
907 err = req->out.h.error;
908 if (!err) {
909 res += num_written;
910 pos += num_written;
911
912 /* break out of the loop on short write */
913 if (num_written != count)
914 err = -EIO;
915 }
916 }
917 fuse_put_request(fc, req);
918 } while (!err && iov_iter_count(ii));
919
920 if (res > 0)
921 fuse_write_update_size(inode, pos);
922
923 fuse_invalidate_attr(inode);
924
925 return res > 0 ? res : err;
926}
927
928static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
929 unsigned long nr_segs, loff_t pos)
930{
931 struct file *file = iocb->ki_filp;
932 struct address_space *mapping = file->f_mapping;
933 size_t count = 0;
934 ssize_t written = 0;
935 struct inode *inode = mapping->host;
936 ssize_t err;
937 struct iov_iter i;
938
939 WARN_ON(iocb->ki_pos != pos);
940
941 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
942 if (err)
943 return err;
944
945 mutex_lock(&inode->i_mutex);
946 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
947
948 /* We can write back this queue in page reclaim */
949 current->backing_dev_info = mapping->backing_dev_info;
950
951 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
952 if (err)
953 goto out;
954
955 if (count == 0)
956 goto out;
957
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200958 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700959 if (err)
960 goto out;
961
962 file_update_time(file);
963
964 iov_iter_init(&i, iov, nr_segs, count, 0);
965 written = fuse_perform_write(file, mapping, &i, pos);
966 if (written >= 0)
967 iocb->ki_pos = pos + written;
968
969out:
970 current->backing_dev_info = NULL;
971 mutex_unlock(&inode->i_mutex);
972
973 return written ? written : err;
974}
975
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700976static void fuse_release_user_pages(struct fuse_req *req, int write)
977{
978 unsigned i;
979
980 for (i = 0; i < req->num_pages; i++) {
981 struct page *page = req->pages[i];
982 if (write)
983 set_page_dirty_lock(page);
984 put_page(page);
985 }
986}
987
988static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200989 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700990{
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200991 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700992 unsigned long user_addr = (unsigned long) buf;
993 unsigned offset = user_addr & ~PAGE_MASK;
994 int npages;
995
Miklos Szeredif4975c62009-04-02 14:25:34 +0200996 /* Special case for kernel I/O: can copy directly into the buffer */
997 if (segment_eq(get_fs(), KERNEL_DS)) {
998 if (write)
999 req->in.args[1].value = (void *) user_addr;
1000 else
1001 req->out.args[0].value = (void *) user_addr;
1002
1003 return 0;
1004 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001005
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001006 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001007 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -07001008 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi1bf94ca2010-05-25 15:06:06 +02001009 npages = get_user_pages_fast(user_addr, npages, !write, req->pages);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001010 if (npages < 0)
1011 return npages;
1012
1013 req->num_pages = npages;
1014 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001015
1016 if (write)
1017 req->in.argpages = 1;
1018 else
1019 req->out.argpages = 1;
1020
1021 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1022 *nbytesp = min(*nbytesp, nbytes);
1023
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001024 return 0;
1025}
1026
Tejun Heo08cbf542009-04-14 10:54:53 +09001027ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1028 size_t count, loff_t *ppos, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001029{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001030 struct fuse_file *ff = file->private_data;
1031 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001032 size_t nmax = write ? fc->max_write : fc->max_read;
1033 loff_t pos = *ppos;
1034 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001035 struct fuse_req *req;
1036
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001037 req = fuse_get_req(fc);
1038 if (IS_ERR(req))
1039 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001040
1041 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001042 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001043 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001044 size_t nbytes = min(count, nmax);
1045 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001046 if (err) {
1047 res = err;
1048 break;
1049 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001050
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001051 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001052 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001053 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001054 nres = fuse_send_read(req, file, pos, nbytes, owner);
1055
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001056 fuse_release_user_pages(req, !write);
1057 if (req->out.h.error) {
1058 if (!res)
1059 res = req->out.h.error;
1060 break;
1061 } else if (nres > nbytes) {
1062 res = -EIO;
1063 break;
1064 }
1065 count -= nres;
1066 res += nres;
1067 pos += nres;
1068 buf += nres;
1069 if (nres != nbytes)
1070 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001071 if (count) {
1072 fuse_put_request(fc, req);
1073 req = fuse_get_req(fc);
1074 if (IS_ERR(req))
1075 break;
1076 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001077 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001078 if (!IS_ERR(req))
1079 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001080 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001081 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001082
1083 return res;
1084}
Tejun Heo08cbf542009-04-14 10:54:53 +09001085EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001086
1087static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1088 size_t count, loff_t *ppos)
1089{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001090 ssize_t res;
1091 struct inode *inode = file->f_path.dentry->d_inode;
1092
1093 if (is_bad_inode(inode))
1094 return -EIO;
1095
1096 res = fuse_direct_io(file, buf, count, ppos, 0);
1097
1098 fuse_invalidate_attr(inode);
1099
1100 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001101}
1102
1103static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1104 size_t count, loff_t *ppos)
1105{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001106 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001107 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001108
1109 if (is_bad_inode(inode))
1110 return -EIO;
1111
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001112 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001113 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001114 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001115 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001116 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001117 if (res > 0)
1118 fuse_write_update_size(inode, *ppos);
1119 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001120 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001121
1122 fuse_invalidate_attr(inode);
1123
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001124 return res;
1125}
1126
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001127static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001128{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001129 __free_page(req->pages[0]);
1130 fuse_file_put(req->ff);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001131}
1132
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001133static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001134{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001135 struct inode *inode = req->inode;
1136 struct fuse_inode *fi = get_fuse_inode(inode);
1137 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1138
1139 list_del(&req->writepages_entry);
1140 dec_bdi_stat(bdi, BDI_WRITEBACK);
1141 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1142 bdi_writeout_inc(bdi);
1143 wake_up(&fi->page_waitq);
1144}
1145
1146/* Called under fc->lock, may release and reacquire it */
1147static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001148__releases(&fc->lock)
1149__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001150{
1151 struct fuse_inode *fi = get_fuse_inode(req->inode);
1152 loff_t size = i_size_read(req->inode);
1153 struct fuse_write_in *inarg = &req->misc.write.in;
1154
1155 if (!fc->connected)
1156 goto out_free;
1157
1158 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1159 inarg->size = PAGE_CACHE_SIZE;
1160 } else if (inarg->offset < size) {
1161 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1162 } else {
1163 /* Got truncated off completely */
1164 goto out_free;
1165 }
1166
1167 req->in.args[1].size = inarg->size;
1168 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001169 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001170 return;
1171
1172 out_free:
1173 fuse_writepage_finish(fc, req);
1174 spin_unlock(&fc->lock);
1175 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001176 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001177 spin_lock(&fc->lock);
1178}
1179
1180/*
1181 * If fi->writectr is positive (no truncate or fsync going on) send
1182 * all queued writepage requests.
1183 *
1184 * Called with fc->lock
1185 */
1186void fuse_flush_writepages(struct inode *inode)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001187__releases(&fc->lock)
1188__acquires(&fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001189{
1190 struct fuse_conn *fc = get_fuse_conn(inode);
1191 struct fuse_inode *fi = get_fuse_inode(inode);
1192 struct fuse_req *req;
1193
1194 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1195 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1196 list_del_init(&req->list);
1197 fuse_send_writepage(fc, req);
1198 }
1199}
1200
1201static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1202{
1203 struct inode *inode = req->inode;
1204 struct fuse_inode *fi = get_fuse_inode(inode);
1205
1206 mapping_set_error(inode->i_mapping, req->out.h.error);
1207 spin_lock(&fc->lock);
1208 fi->writectr--;
1209 fuse_writepage_finish(fc, req);
1210 spin_unlock(&fc->lock);
1211 fuse_writepage_free(fc, req);
1212}
1213
1214static int fuse_writepage_locked(struct page *page)
1215{
1216 struct address_space *mapping = page->mapping;
1217 struct inode *inode = mapping->host;
1218 struct fuse_conn *fc = get_fuse_conn(inode);
1219 struct fuse_inode *fi = get_fuse_inode(inode);
1220 struct fuse_req *req;
1221 struct fuse_file *ff;
1222 struct page *tmp_page;
1223
1224 set_page_writeback(page);
1225
1226 req = fuse_request_alloc_nofs();
1227 if (!req)
1228 goto err;
1229
1230 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1231 if (!tmp_page)
1232 goto err_free;
1233
1234 spin_lock(&fc->lock);
1235 BUG_ON(list_empty(&fi->write_files));
1236 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1237 req->ff = fuse_file_get(ff);
1238 spin_unlock(&fc->lock);
1239
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001240 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001241
1242 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001243 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001244 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001245 req->num_pages = 1;
1246 req->pages[0] = tmp_page;
1247 req->page_offset = 0;
1248 req->end = fuse_writepage_end;
1249 req->inode = inode;
1250
1251 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1252 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1253 end_page_writeback(page);
1254
1255 spin_lock(&fc->lock);
1256 list_add(&req->writepages_entry, &fi->writepages);
1257 list_add_tail(&req->list, &fi->queued_writes);
1258 fuse_flush_writepages(inode);
1259 spin_unlock(&fc->lock);
1260
1261 return 0;
1262
1263err_free:
1264 fuse_request_free(req);
1265err:
1266 end_page_writeback(page);
1267 return -ENOMEM;
1268}
1269
1270static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1271{
1272 int err;
1273
1274 err = fuse_writepage_locked(page);
1275 unlock_page(page);
1276
1277 return err;
1278}
1279
1280static int fuse_launder_page(struct page *page)
1281{
1282 int err = 0;
1283 if (clear_page_dirty_for_io(page)) {
1284 struct inode *inode = page->mapping->host;
1285 err = fuse_writepage_locked(page);
1286 if (!err)
1287 fuse_wait_on_page_writeback(inode, page->index);
1288 }
1289 return err;
1290}
1291
1292/*
1293 * Write back dirty pages now, because there may not be any suitable
1294 * open files later
1295 */
1296static void fuse_vma_close(struct vm_area_struct *vma)
1297{
1298 filemap_write_and_wait(vma->vm_file->f_mapping);
1299}
1300
1301/*
1302 * Wait for writeback against this page to complete before allowing it
1303 * to be marked dirty again, and hence written back again, possibly
1304 * before the previous writepage completed.
1305 *
1306 * Block here, instead of in ->writepage(), so that the userspace fs
1307 * can only block processes actually operating on the filesystem.
1308 *
1309 * Otherwise unprivileged userspace fs would be able to block
1310 * unrelated:
1311 *
1312 * - page migration
1313 * - sync(2)
1314 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1315 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001316static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001317{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001318 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001319 /*
1320 * Don't use page->mapping as it may become NULL from a
1321 * concurrent truncate.
1322 */
1323 struct inode *inode = vma->vm_file->f_mapping->host;
1324
1325 fuse_wait_on_page_writeback(inode, page->index);
1326 return 0;
1327}
1328
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001329static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001330 .close = fuse_vma_close,
1331 .fault = filemap_fault,
1332 .page_mkwrite = fuse_page_mkwrite,
1333};
1334
1335static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1336{
1337 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1338 struct inode *inode = file->f_dentry->d_inode;
1339 struct fuse_conn *fc = get_fuse_conn(inode);
1340 struct fuse_inode *fi = get_fuse_inode(inode);
1341 struct fuse_file *ff = file->private_data;
1342 /*
1343 * file may be written through mmap, so chain it onto the
1344 * inodes's write_file list
1345 */
1346 spin_lock(&fc->lock);
1347 if (list_empty(&ff->write_entry))
1348 list_add(&ff->write_entry, &fi->write_files);
1349 spin_unlock(&fc->lock);
1350 }
1351 file_accessed(file);
1352 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001353 return 0;
1354}
1355
Miklos Szeredifc280c92009-04-02 14:25:35 +02001356static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1357{
1358 /* Can't provide the coherency needed for MAP_SHARED */
1359 if (vma->vm_flags & VM_MAYSHARE)
1360 return -ENODEV;
1361
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001362 invalidate_inode_pages2(file->f_mapping);
1363
Miklos Szeredifc280c92009-04-02 14:25:35 +02001364 return generic_file_mmap(file, vma);
1365}
1366
Miklos Szeredi71421252006-06-25 05:48:52 -07001367static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1368 struct file_lock *fl)
1369{
1370 switch (ffl->type) {
1371 case F_UNLCK:
1372 break;
1373
1374 case F_RDLCK:
1375 case F_WRLCK:
1376 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1377 ffl->end < ffl->start)
1378 return -EIO;
1379
1380 fl->fl_start = ffl->start;
1381 fl->fl_end = ffl->end;
1382 fl->fl_pid = ffl->pid;
1383 break;
1384
1385 default:
1386 return -EIO;
1387 }
1388 fl->fl_type = ffl->type;
1389 return 0;
1390}
1391
1392static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001393 const struct file_lock *fl, int opcode, pid_t pid,
1394 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001395{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001396 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001397 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001398 struct fuse_file *ff = file->private_data;
1399 struct fuse_lk_in *arg = &req->misc.lk_in;
1400
1401 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001402 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001403 arg->lk.start = fl->fl_start;
1404 arg->lk.end = fl->fl_end;
1405 arg->lk.type = fl->fl_type;
1406 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001407 if (flock)
1408 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001409 req->in.h.opcode = opcode;
1410 req->in.h.nodeid = get_node_id(inode);
1411 req->in.numargs = 1;
1412 req->in.args[0].size = sizeof(*arg);
1413 req->in.args[0].value = arg;
1414}
1415
1416static int fuse_getlk(struct file *file, struct file_lock *fl)
1417{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001418 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001419 struct fuse_conn *fc = get_fuse_conn(inode);
1420 struct fuse_req *req;
1421 struct fuse_lk_out outarg;
1422 int err;
1423
1424 req = fuse_get_req(fc);
1425 if (IS_ERR(req))
1426 return PTR_ERR(req);
1427
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001428 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001429 req->out.numargs = 1;
1430 req->out.args[0].size = sizeof(outarg);
1431 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001432 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001433 err = req->out.h.error;
1434 fuse_put_request(fc, req);
1435 if (!err)
1436 err = convert_fuse_file_lock(&outarg.lk, fl);
1437
1438 return err;
1439}
1440
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001441static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001442{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001443 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001444 struct fuse_conn *fc = get_fuse_conn(inode);
1445 struct fuse_req *req;
1446 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1447 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1448 int err;
1449
Miklos Szeredi48e90762008-07-25 01:49:02 -07001450 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1451 /* NLM needs asynchronous locks, which we don't support yet */
1452 return -ENOLCK;
1453 }
1454
Miklos Szeredi71421252006-06-25 05:48:52 -07001455 /* Unlock on close is handled by the flush method */
1456 if (fl->fl_flags & FL_CLOSE)
1457 return 0;
1458
1459 req = fuse_get_req(fc);
1460 if (IS_ERR(req))
1461 return PTR_ERR(req);
1462
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001463 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001464 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001465 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001466 /* locking is restartable */
1467 if (err == -EINTR)
1468 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001469 fuse_put_request(fc, req);
1470 return err;
1471}
1472
1473static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1474{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001475 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001476 struct fuse_conn *fc = get_fuse_conn(inode);
1477 int err;
1478
Miklos Szeredi48e90762008-07-25 01:49:02 -07001479 if (cmd == F_CANCELLK) {
1480 err = 0;
1481 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001482 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001483 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001484 err = 0;
1485 } else
1486 err = fuse_getlk(file, fl);
1487 } else {
1488 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001489 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001490 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001491 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001492 }
1493 return err;
1494}
1495
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001496static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1497{
1498 struct inode *inode = file->f_path.dentry->d_inode;
1499 struct fuse_conn *fc = get_fuse_conn(inode);
1500 int err;
1501
1502 if (fc->no_lock) {
1503 err = flock_lock_file_wait(file, fl);
1504 } else {
1505 /* emulate flock with POSIX locks */
1506 fl->fl_owner = (fl_owner_t) file;
1507 err = fuse_setlk(file, fl, 1);
1508 }
1509
1510 return err;
1511}
1512
Miklos Szeredib2d22722006-12-06 20:35:51 -08001513static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1514{
1515 struct inode *inode = mapping->host;
1516 struct fuse_conn *fc = get_fuse_conn(inode);
1517 struct fuse_req *req;
1518 struct fuse_bmap_in inarg;
1519 struct fuse_bmap_out outarg;
1520 int err;
1521
1522 if (!inode->i_sb->s_bdev || fc->no_bmap)
1523 return 0;
1524
1525 req = fuse_get_req(fc);
1526 if (IS_ERR(req))
1527 return 0;
1528
1529 memset(&inarg, 0, sizeof(inarg));
1530 inarg.block = block;
1531 inarg.blocksize = inode->i_sb->s_blocksize;
1532 req->in.h.opcode = FUSE_BMAP;
1533 req->in.h.nodeid = get_node_id(inode);
1534 req->in.numargs = 1;
1535 req->in.args[0].size = sizeof(inarg);
1536 req->in.args[0].value = &inarg;
1537 req->out.numargs = 1;
1538 req->out.args[0].size = sizeof(outarg);
1539 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001540 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001541 err = req->out.h.error;
1542 fuse_put_request(fc, req);
1543 if (err == -ENOSYS)
1544 fc->no_bmap = 1;
1545
1546 return err ? 0 : outarg.block;
1547}
1548
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001549static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1550{
1551 loff_t retval;
1552 struct inode *inode = file->f_path.dentry->d_inode;
1553
1554 mutex_lock(&inode->i_mutex);
1555 switch (origin) {
1556 case SEEK_END:
Miklos Szeredi769415c2008-10-16 16:08:56 +02001557 retval = fuse_update_attributes(inode, NULL, file, NULL);
1558 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001559 goto exit;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001560 offset += i_size_read(inode);
1561 break;
1562 case SEEK_CUR:
1563 offset += file->f_pos;
1564 }
1565 retval = -EINVAL;
1566 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1567 if (offset != file->f_pos) {
1568 file->f_pos = offset;
1569 file->f_version = 0;
1570 }
1571 retval = offset;
1572 }
Dan Carpenter52916582009-03-27 13:36:10 +03001573exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001574 mutex_unlock(&inode->i_mutex);
1575 return retval;
1576}
1577
Tejun Heo59efec72008-11-26 12:03:55 +01001578static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1579 unsigned int nr_segs, size_t bytes, bool to_user)
1580{
1581 struct iov_iter ii;
1582 int page_idx = 0;
1583
1584 if (!bytes)
1585 return 0;
1586
1587 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1588
1589 while (iov_iter_count(&ii)) {
1590 struct page *page = pages[page_idx++];
1591 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001592 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001593
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001594 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001595
1596 while (todo) {
1597 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1598 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1599 size_t copy = min(todo, iov_len);
1600 size_t left;
1601
1602 if (!to_user)
1603 left = copy_from_user(kaddr, uaddr, copy);
1604 else
1605 left = copy_to_user(uaddr, kaddr, copy);
1606
1607 if (unlikely(left))
1608 return -EFAULT;
1609
1610 iov_iter_advance(&ii, copy);
1611 todo -= copy;
1612 kaddr += copy;
1613 }
1614
Jens Axboe0bd87182009-11-03 11:40:44 +01001615 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001616 }
1617
1618 return 0;
1619}
1620
1621/*
1622 * For ioctls, there is no generic way to determine how much memory
1623 * needs to be read and/or written. Furthermore, ioctls are allowed
1624 * to dereference the passed pointer, so the parameter requires deep
1625 * copying but FUSE has no idea whatsoever about what to copy in or
1626 * out.
1627 *
1628 * This is solved by allowing FUSE server to retry ioctl with
1629 * necessary in/out iovecs. Let's assume the ioctl implementation
1630 * needs to read in the following structure.
1631 *
1632 * struct a {
1633 * char *buf;
1634 * size_t buflen;
1635 * }
1636 *
1637 * On the first callout to FUSE server, inarg->in_size and
1638 * inarg->out_size will be NULL; then, the server completes the ioctl
1639 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1640 * the actual iov array to
1641 *
1642 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1643 *
1644 * which tells FUSE to copy in the requested area and retry the ioctl.
1645 * On the second round, the server has access to the structure and
1646 * from that it can tell what to look for next, so on the invocation,
1647 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1648 *
1649 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1650 * { .iov_base = a.buf, .iov_len = a.buflen } }
1651 *
1652 * FUSE will copy both struct a and the pointed buffer from the
1653 * process doing the ioctl and retry ioctl with both struct a and the
1654 * buffer.
1655 *
1656 * This time, FUSE server has everything it needs and completes ioctl
1657 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1658 *
1659 * Copying data out works the same way.
1660 *
1661 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1662 * automatically initializes in and out iovs by decoding @cmd with
1663 * _IOC_* macros and the server is not allowed to request RETRY. This
1664 * limits ioctl data transfers to well-formed ioctls and is the forced
1665 * behavior for all FUSE servers.
1666 */
Tejun Heo08cbf542009-04-14 10:54:53 +09001667long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1668 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001669{
Tejun Heo59efec72008-11-26 12:03:55 +01001670 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001671 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001672 struct fuse_ioctl_in inarg = {
1673 .fh = ff->fh,
1674 .cmd = cmd,
1675 .arg = arg,
1676 .flags = flags
1677 };
1678 struct fuse_ioctl_out outarg;
1679 struct fuse_req *req = NULL;
1680 struct page **pages = NULL;
1681 struct page *iov_page = NULL;
1682 struct iovec *in_iov = NULL, *out_iov = NULL;
1683 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1684 size_t in_size, out_size, transferred;
1685 int err;
1686
1687 /* assume all the iovs returned by client always fits in a page */
1688 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1689
Tejun Heo59efec72008-11-26 12:03:55 +01001690 err = -ENOMEM;
1691 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1692 iov_page = alloc_page(GFP_KERNEL);
1693 if (!pages || !iov_page)
1694 goto out;
1695
1696 /*
1697 * If restricted, initialize IO parameters as encoded in @cmd.
1698 * RETRY from server is not allowed.
1699 */
1700 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1701 struct iovec *iov = page_address(iov_page);
1702
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01001703 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001704 iov->iov_len = _IOC_SIZE(cmd);
1705
1706 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1707 in_iov = iov;
1708 in_iovs = 1;
1709 }
1710
1711 if (_IOC_DIR(cmd) & _IOC_READ) {
1712 out_iov = iov;
1713 out_iovs = 1;
1714 }
1715 }
1716
1717 retry:
1718 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1719 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1720
1721 /*
1722 * Out data can be used either for actual out data or iovs,
1723 * make sure there always is at least one page.
1724 */
1725 out_size = max_t(size_t, out_size, PAGE_SIZE);
1726 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1727
1728 /* make sure there are enough buffer pages and init request with them */
1729 err = -ENOMEM;
1730 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1731 goto out;
1732 while (num_pages < max_pages) {
1733 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1734 if (!pages[num_pages])
1735 goto out;
1736 num_pages++;
1737 }
1738
1739 req = fuse_get_req(fc);
1740 if (IS_ERR(req)) {
1741 err = PTR_ERR(req);
1742 req = NULL;
1743 goto out;
1744 }
1745 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1746 req->num_pages = num_pages;
1747
1748 /* okay, let's send it to the client */
1749 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001750 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001751 req->in.numargs = 1;
1752 req->in.args[0].size = sizeof(inarg);
1753 req->in.args[0].value = &inarg;
1754 if (in_size) {
1755 req->in.numargs++;
1756 req->in.args[1].size = in_size;
1757 req->in.argpages = 1;
1758
1759 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1760 false);
1761 if (err)
1762 goto out;
1763 }
1764
1765 req->out.numargs = 2;
1766 req->out.args[0].size = sizeof(outarg);
1767 req->out.args[0].value = &outarg;
1768 req->out.args[1].size = out_size;
1769 req->out.argpages = 1;
1770 req->out.argvar = 1;
1771
Tejun Heob93f8582008-11-26 12:03:55 +01001772 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001773 err = req->out.h.error;
1774 transferred = req->out.args[1].size;
1775 fuse_put_request(fc, req);
1776 req = NULL;
1777 if (err)
1778 goto out;
1779
1780 /* did it ask for retry? */
1781 if (outarg.flags & FUSE_IOCTL_RETRY) {
1782 char *vaddr;
1783
1784 /* no retry if in restricted mode */
1785 err = -EIO;
1786 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1787 goto out;
1788
1789 in_iovs = outarg.in_iovs;
1790 out_iovs = outarg.out_iovs;
1791
1792 /*
1793 * Make sure things are in boundary, separate checks
1794 * are to protect against overflow.
1795 */
1796 err = -ENOMEM;
1797 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1798 out_iovs > FUSE_IOCTL_MAX_IOV ||
1799 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1800 goto out;
1801
1802 err = -EIO;
1803 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1804 goto out;
1805
1806 /* okay, copy in iovs and retry */
1807 vaddr = kmap_atomic(pages[0], KM_USER0);
1808 memcpy(page_address(iov_page), vaddr, transferred);
1809 kunmap_atomic(vaddr, KM_USER0);
1810
1811 in_iov = page_address(iov_page);
1812 out_iov = in_iov + in_iovs;
1813
1814 goto retry;
1815 }
1816
1817 err = -EIO;
1818 if (transferred > inarg.out_size)
1819 goto out;
1820
1821 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1822 out:
1823 if (req)
1824 fuse_put_request(fc, req);
1825 if (iov_page)
1826 __free_page(iov_page);
1827 while (num_pages)
1828 __free_page(pages[--num_pages]);
1829 kfree(pages);
1830
1831 return err ? err : outarg.result;
1832}
Tejun Heo08cbf542009-04-14 10:54:53 +09001833EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01001834
Miklos Szeredid36f2482009-04-28 16:56:39 +02001835static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
1836 unsigned long arg, unsigned int flags)
1837{
1838 struct inode *inode = file->f_dentry->d_inode;
1839 struct fuse_conn *fc = get_fuse_conn(inode);
1840
1841 if (!fuse_allow_task(fc, current))
1842 return -EACCES;
1843
1844 if (is_bad_inode(inode))
1845 return -EIO;
1846
1847 return fuse_do_ioctl(file, cmd, arg, flags);
1848}
1849
Tejun Heo59efec72008-11-26 12:03:55 +01001850static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1851 unsigned long arg)
1852{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001853 return fuse_file_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001854}
1855
1856static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1857 unsigned long arg)
1858{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001859 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01001860}
1861
Tejun Heo95668a62008-11-26 12:03:55 +01001862/*
1863 * All files which have been polled are linked to RB tree
1864 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1865 * find the matching one.
1866 */
1867static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1868 struct rb_node **parent_out)
1869{
1870 struct rb_node **link = &fc->polled_files.rb_node;
1871 struct rb_node *last = NULL;
1872
1873 while (*link) {
1874 struct fuse_file *ff;
1875
1876 last = *link;
1877 ff = rb_entry(last, struct fuse_file, polled_node);
1878
1879 if (kh < ff->kh)
1880 link = &last->rb_left;
1881 else if (kh > ff->kh)
1882 link = &last->rb_right;
1883 else
1884 return link;
1885 }
1886
1887 if (parent_out)
1888 *parent_out = last;
1889 return link;
1890}
1891
1892/*
1893 * The file is about to be polled. Make sure it's on the polled_files
1894 * RB tree. Note that files once added to the polled_files tree are
1895 * not removed before the file is released. This is because a file
1896 * polled once is likely to be polled again.
1897 */
1898static void fuse_register_polled_file(struct fuse_conn *fc,
1899 struct fuse_file *ff)
1900{
1901 spin_lock(&fc->lock);
1902 if (RB_EMPTY_NODE(&ff->polled_node)) {
1903 struct rb_node **link, *parent;
1904
1905 link = fuse_find_polled_node(fc, ff->kh, &parent);
1906 BUG_ON(*link);
1907 rb_link_node(&ff->polled_node, parent, link);
1908 rb_insert_color(&ff->polled_node, &fc->polled_files);
1909 }
1910 spin_unlock(&fc->lock);
1911}
1912
Tejun Heo08cbf542009-04-14 10:54:53 +09001913unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01001914{
Tejun Heo95668a62008-11-26 12:03:55 +01001915 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02001916 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01001917 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1918 struct fuse_poll_out outarg;
1919 struct fuse_req *req;
1920 int err;
1921
1922 if (fc->no_poll)
1923 return DEFAULT_POLLMASK;
1924
1925 poll_wait(file, &ff->poll_wait, wait);
1926
1927 /*
1928 * Ask for notification iff there's someone waiting for it.
1929 * The client may ignore the flag and always notify.
1930 */
1931 if (waitqueue_active(&ff->poll_wait)) {
1932 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1933 fuse_register_polled_file(fc, ff);
1934 }
1935
1936 req = fuse_get_req(fc);
1937 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02001938 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01001939
1940 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02001941 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01001942 req->in.numargs = 1;
1943 req->in.args[0].size = sizeof(inarg);
1944 req->in.args[0].value = &inarg;
1945 req->out.numargs = 1;
1946 req->out.args[0].size = sizeof(outarg);
1947 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001948 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01001949 err = req->out.h.error;
1950 fuse_put_request(fc, req);
1951
1952 if (!err)
1953 return outarg.revents;
1954 if (err == -ENOSYS) {
1955 fc->no_poll = 1;
1956 return DEFAULT_POLLMASK;
1957 }
1958 return POLLERR;
1959}
Tejun Heo08cbf542009-04-14 10:54:53 +09001960EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01001961
1962/*
1963 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1964 * wakes up the poll waiters.
1965 */
1966int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1967 struct fuse_notify_poll_wakeup_out *outarg)
1968{
1969 u64 kh = outarg->kh;
1970 struct rb_node **link;
1971
1972 spin_lock(&fc->lock);
1973
1974 link = fuse_find_polled_node(fc, kh, NULL);
1975 if (*link) {
1976 struct fuse_file *ff;
1977
1978 ff = rb_entry(*link, struct fuse_file, polled_node);
1979 wake_up_interruptible_sync(&ff->poll_wait);
1980 }
1981
1982 spin_unlock(&fc->lock);
1983 return 0;
1984}
1985
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001986static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001987 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001988 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001989 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001990 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07001991 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001992 .mmap = fuse_file_mmap,
1993 .open = fuse_open,
1994 .flush = fuse_flush,
1995 .release = fuse_release,
1996 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001997 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001998 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001999 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002000 .unlocked_ioctl = fuse_file_ioctl,
2001 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002002 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002003};
2004
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002005static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002006 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002007 .read = fuse_direct_read,
2008 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002009 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002010 .open = fuse_open,
2011 .flush = fuse_flush,
2012 .release = fuse_release,
2013 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002014 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002015 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002016 .unlocked_ioctl = fuse_file_ioctl,
2017 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002018 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002019 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002020};
2021
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002022static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002023 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002024 .writepage = fuse_writepage,
2025 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07002026 .write_begin = fuse_write_begin,
2027 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002028 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002029 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002030 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002031};
2032
2033void fuse_init_file_inode(struct inode *inode)
2034{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002035 inode->i_fop = &fuse_file_operations;
2036 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002037}