blob: f0f0f278b4eae6cc150daa07f6ad220131d222f4 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02003 Copyright (C) 2001-2006 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;
42 request_send(fc, req);
43 err = req->out.h.error;
44 fuse_put_request(fc, req);
45
46 return err;
47}
48
49struct fuse_file *fuse_file_alloc(void)
50{
51 struct fuse_file *ff;
52 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
53 if (ff) {
Miklos Szeredi33649c92006-06-25 05:48:52 -070054 ff->reserved_req = fuse_request_alloc();
55 if (!ff->reserved_req) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080056 kfree(ff);
57 ff = NULL;
Adrian Bunk87449692007-11-14 17:00:02 -080058 } else {
59 INIT_LIST_HEAD(&ff->write_entry);
60 atomic_set(&ff->count, 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080061 }
62 }
63 return ff;
64}
65
66void fuse_file_free(struct fuse_file *ff)
67{
Miklos Szeredi33649c92006-06-25 05:48:52 -070068 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080069 kfree(ff);
70}
71
Miklos Szeredic756e0a2007-10-16 23:31:00 -070072static struct fuse_file *fuse_file_get(struct fuse_file *ff)
73{
74 atomic_inc(&ff->count);
75 return ff;
76}
77
Miklos Szeredi819c4b32007-10-16 23:31:04 -070078static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
79{
Miklos Szeredib57d4262008-02-06 01:38:39 -080080 dput(req->misc.release.dentry);
81 mntput(req->misc.release.vfsmount);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070082 fuse_put_request(fc, req);
83}
84
Miklos Szeredic756e0a2007-10-16 23:31:00 -070085static void fuse_file_put(struct fuse_file *ff)
86{
87 if (atomic_dec_and_test(&ff->count)) {
88 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -080089 struct inode *inode = req->misc.release.dentry->d_inode;
90 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070091 req->end = fuse_release_end;
Miklos Szeredic756e0a2007-10-16 23:31:00 -070092 request_send_background(fc, req);
93 kfree(ff);
94 }
95}
96
Miklos Szeredifd72faa2005-11-07 00:59:51 -080097void fuse_finish_open(struct inode *inode, struct file *file,
98 struct fuse_file *ff, struct fuse_open_out *outarg)
99{
100 if (outarg->open_flags & FOPEN_DIRECT_IO)
101 file->f_op = &fuse_direct_io_file_operations;
102 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700103 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800104 ff->fh = outarg->fh;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700105 file->private_data = fuse_file_get(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800106}
107
108int fuse_open_common(struct inode *inode, struct file *file, int isdir)
109{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700110 struct fuse_open_out outarg;
111 struct fuse_file *ff;
112 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700113
Miklos Szeredidd190d02005-09-30 11:59:02 -0700114 /* VFS checks this, but only _after_ ->open() */
115 if (file->f_flags & O_DIRECT)
116 return -EINVAL;
117
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700118 err = generic_file_open(inode, file);
119 if (err)
120 return err;
121
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800122 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700123 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800124 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700125
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800126 err = fuse_send_open(inode, file, isdir, &outarg);
127 if (err)
128 fuse_file_free(ff);
129 else {
130 if (isdir)
131 outarg.open_flags &= ~FOPEN_DIRECT_IO;
132 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700133 }
134
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700135 return err;
136}
137
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700138void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800139{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700140 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800141 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700142
143 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800144 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700145 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800146 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700147 req->in.numargs = 1;
148 req->in.args[0].size = sizeof(struct fuse_release_in);
149 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800150}
151
152int fuse_release_common(struct inode *inode, struct file *file, int isdir)
153{
154 struct fuse_file *ff = file->private_data;
155 if (ff) {
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700156 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib57d4262008-02-06 01:38:39 -0800157 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700158
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700159 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
160 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700161
162 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib57d4262008-02-06 01:38:39 -0800163 req->misc.release.vfsmount = mntget(file->f_path.mnt);
164 req->misc.release.dentry = dget(file->f_path.dentry);
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700165
166 spin_lock(&fc->lock);
167 list_del(&ff->write_entry);
168 spin_unlock(&fc->lock);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700169 /*
170 * Normally this will send the RELEASE request,
171 * however if some asynchronous READ or WRITE requests
172 * are outstanding, the sending will be delayed
173 */
174 fuse_file_put(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800175 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700176
177 /* Return value is ignored by VFS */
178 return 0;
179}
180
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700181static int fuse_open(struct inode *inode, struct file *file)
182{
183 return fuse_open_common(inode, file, 0);
184}
185
186static int fuse_release(struct inode *inode, struct file *file)
187{
188 return fuse_release_common(inode, file, 0);
189}
190
Miklos Szeredi71421252006-06-25 05:48:52 -0700191/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700192 * Scramble the ID space with XTEA, so that the value of the files_struct
193 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700194 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700195u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700196{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700197 u32 *k = fc->scramble_key;
198 u64 v = (unsigned long) id;
199 u32 v0 = v;
200 u32 v1 = v >> 32;
201 u32 sum = 0;
202 int i;
203
204 for (i = 0; i < 32; i++) {
205 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
206 sum += 0x9E3779B9;
207 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
208 }
209
210 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700211}
212
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700213/*
214 * Check if page is under writeback
215 *
216 * This is currently done by walking the list of writepage requests
217 * for the inode, which can be pretty inefficient.
218 */
219static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
220{
221 struct fuse_conn *fc = get_fuse_conn(inode);
222 struct fuse_inode *fi = get_fuse_inode(inode);
223 struct fuse_req *req;
224 bool found = false;
225
226 spin_lock(&fc->lock);
227 list_for_each_entry(req, &fi->writepages, writepages_entry) {
228 pgoff_t curr_index;
229
230 BUG_ON(req->inode != inode);
231 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
232 if (curr_index == index) {
233 found = true;
234 break;
235 }
236 }
237 spin_unlock(&fc->lock);
238
239 return found;
240}
241
242/*
243 * Wait for page writeback to be completed.
244 *
245 * Since fuse doesn't rely on the VM writeback tracking, this has to
246 * use some other means.
247 */
248static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
249{
250 struct fuse_inode *fi = get_fuse_inode(inode);
251
252 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
253 return 0;
254}
255
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700256static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700257{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800258 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700259 struct fuse_conn *fc = get_fuse_conn(inode);
260 struct fuse_file *ff = file->private_data;
261 struct fuse_req *req;
262 struct fuse_flush_in inarg;
263 int err;
264
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800265 if (is_bad_inode(inode))
266 return -EIO;
267
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700268 if (fc->no_flush)
269 return 0;
270
Miklos Szeredi33649c92006-06-25 05:48:52 -0700271 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700272 memset(&inarg, 0, sizeof(inarg));
273 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700274 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700275 req->in.h.opcode = FUSE_FLUSH;
276 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277 req->in.numargs = 1;
278 req->in.args[0].size = sizeof(inarg);
279 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700280 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700281 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700282 err = req->out.h.error;
283 fuse_put_request(fc, req);
284 if (err == -ENOSYS) {
285 fc->no_flush = 1;
286 err = 0;
287 }
288 return err;
289}
290
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700291/*
292 * Wait for all pending writepages on the inode to finish.
293 *
294 * This is currently done by blocking further writes with FUSE_NOWRITE
295 * and waiting for all sent writes to complete.
296 *
297 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
298 * could conflict with truncation.
299 */
300static void fuse_sync_writes(struct inode *inode)
301{
302 fuse_set_nowrite(inode);
303 fuse_release_nowrite(inode);
304}
305
Miklos Szeredi82547982005-09-09 13:10:38 -0700306int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
307 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700308{
309 struct inode *inode = de->d_inode;
310 struct fuse_conn *fc = get_fuse_conn(inode);
311 struct fuse_file *ff = file->private_data;
312 struct fuse_req *req;
313 struct fuse_fsync_in inarg;
314 int err;
315
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800316 if (is_bad_inode(inode))
317 return -EIO;
318
Miklos Szeredi82547982005-09-09 13:10:38 -0700319 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700320 return 0;
321
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700322 /*
323 * Start writeback against all dirty pages of the inode, then
324 * wait for all outstanding writes, before sending the FSYNC
325 * request.
326 */
327 err = write_inode_now(inode, 0);
328 if (err)
329 return err;
330
331 fuse_sync_writes(inode);
332
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700333 req = fuse_get_req(fc);
334 if (IS_ERR(req))
335 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700336
337 memset(&inarg, 0, sizeof(inarg));
338 inarg.fh = ff->fh;
339 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700340 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700341 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700342 req->in.numargs = 1;
343 req->in.args[0].size = sizeof(inarg);
344 req->in.args[0].value = &inarg;
345 request_send(fc, req);
346 err = req->out.h.error;
347 fuse_put_request(fc, req);
348 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700349 if (isdir)
350 fc->no_fsyncdir = 1;
351 else
352 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700353 err = 0;
354 }
355 return err;
356}
357
Miklos Szeredi82547982005-09-09 13:10:38 -0700358static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
359{
360 return fuse_fsync_common(file, de, datasync, 0);
361}
362
Miklos Szeredia6643092007-11-28 16:22:00 -0800363void fuse_read_fill(struct fuse_req *req, struct file *file,
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800364 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700365{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800366 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800367 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700368
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800369 inarg->fh = ff->fh;
370 inarg->offset = pos;
371 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800372 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800373 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700374 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700375 req->in.numargs = 1;
376 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800377 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700378 req->out.argpages = 1;
379 req->out.argvar = 1;
380 req->out.numargs = 1;
381 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700382}
383
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800384static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredif3332112007-10-18 03:07:04 -0700385 struct inode *inode, loff_t pos, size_t count,
386 fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700387{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800388 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredif3332112007-10-18 03:07:04 -0700389
Miklos Szeredia6643092007-11-28 16:22:00 -0800390 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700391 if (owner != NULL) {
392 struct fuse_read_in *inarg = &req->misc.read_in;
393
394 inarg->read_flags |= FUSE_READ_LOCKOWNER;
395 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
396 }
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800397 request_send(fc, req);
398 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700399}
400
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700401static int fuse_readpage(struct file *file, struct page *page)
402{
403 struct inode *inode = page->mapping->host;
404 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800405 struct fuse_req *req;
406 int err;
407
408 err = -EIO;
409 if (is_bad_inode(inode))
410 goto out;
411
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700412 /*
413 * Page writeback can extend beyond the liftime of the
414 * page-cache page, so make sure we read a properly synced
415 * page.
416 */
417 fuse_wait_on_page_writeback(inode, page->index);
418
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700419 req = fuse_get_req(fc);
420 err = PTR_ERR(req);
421 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700422 goto out;
423
424 req->out.page_zeroing = 1;
425 req->num_pages = 1;
426 req->pages[0] = page;
Miklos Szeredif3332112007-10-18 03:07:04 -0700427 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE,
428 NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700429 err = req->out.h.error;
430 fuse_put_request(fc, req);
431 if (!err)
432 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700433 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 out:
435 unlock_page(page);
436 return err;
437}
438
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800439static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700440{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800441 int i;
442
443 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
444
Miklos Szeredidb50b962005-09-09 13:10:33 -0700445 for (i = 0; i < req->num_pages; i++) {
446 struct page *page = req->pages[i];
447 if (!req->out.h.error)
448 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800449 else
450 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700451 unlock_page(page);
452 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700453 if (req->ff)
454 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800455 fuse_put_request(fc, req);
456}
457
Miklos Szeredia6643092007-11-28 16:22:00 -0800458static void fuse_send_readpages(struct fuse_req *req, struct file *file,
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800459 struct inode *inode)
460{
461 struct fuse_conn *fc = get_fuse_conn(inode);
462 loff_t pos = page_offset(req->pages[0]);
463 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
464 req->out.page_zeroing = 1;
Miklos Szeredia6643092007-11-28 16:22:00 -0800465 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800466 if (fc->async_read) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800467 struct fuse_file *ff = file->private_data;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700468 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800469 req->end = fuse_readpages_end;
470 request_send_background(fc, req);
471 } else {
472 request_send(fc, req);
473 fuse_readpages_end(fc, req);
474 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700475}
476
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700477struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700478 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800479 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700480 struct inode *inode;
481};
482
483static int fuse_readpages_fill(void *_data, struct page *page)
484{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700485 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700486 struct fuse_req *req = data->req;
487 struct inode *inode = data->inode;
488 struct fuse_conn *fc = get_fuse_conn(inode);
489
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700490 fuse_wait_on_page_writeback(inode, page->index);
491
Miklos Szeredidb50b962005-09-09 13:10:33 -0700492 if (req->num_pages &&
493 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
494 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
495 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800496 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700497 data->req = req = fuse_get_req(fc);
498 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700499 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700500 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700501 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700502 }
503 req->pages[req->num_pages] = page;
504 req->num_pages ++;
505 return 0;
506}
507
508static int fuse_readpages(struct file *file, struct address_space *mapping,
509 struct list_head *pages, unsigned nr_pages)
510{
511 struct inode *inode = mapping->host;
512 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700513 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700514 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800515
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700516 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800517 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800518 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800519
Miklos Szeredia6643092007-11-28 16:22:00 -0800520 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700521 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700522 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700523 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700524 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800525 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700526
527 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700528 if (!err) {
529 if (data.req->num_pages)
Miklos Szeredia6643092007-11-28 16:22:00 -0800530 fuse_send_readpages(data.req, file, inode);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700531 else
532 fuse_put_request(fc, data.req);
533 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800534out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700535 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700536}
537
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800538static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
539 unsigned long nr_segs, loff_t pos)
540{
541 struct inode *inode = iocb->ki_filp->f_mapping->host;
542
543 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
544 int err;
545 /*
546 * If trying to read past EOF, make sure the i_size
547 * attribute is up-to-date.
548 */
549 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
550 if (err)
551 return err;
552 }
553
554 return generic_file_aio_read(iocb, iov, nr_segs, pos);
555}
556
Miklos Szeredia6643092007-11-28 16:22:00 -0800557static void fuse_write_fill(struct fuse_req *req, struct file *file,
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700558 struct fuse_file *ff, struct inode *inode,
559 loff_t pos, size_t count, int writepage)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700560{
Miklos Szeredif3332112007-10-18 03:07:04 -0700561 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700562 struct fuse_write_in *inarg = &req->misc.write.in;
563 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700564
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700565 memset(inarg, 0, sizeof(struct fuse_write_in));
566 inarg->fh = ff->fh;
567 inarg->offset = pos;
568 inarg->size = count;
569 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700570 inarg->flags = file ? file->f_flags : 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700571 req->in.h.opcode = FUSE_WRITE;
572 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700573 req->in.argpages = 1;
574 req->in.numargs = 2;
Miklos Szeredif3332112007-10-18 03:07:04 -0700575 if (fc->minor < 9)
576 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
577 else
578 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700579 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700580 req->in.args[1].size = count;
581 req->out.numargs = 1;
582 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700583 req->out.args[0].value = outarg;
584}
585
586static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredif3332112007-10-18 03:07:04 -0700587 struct inode *inode, loff_t pos, size_t count,
588 fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700589{
590 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700591 fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
Miklos Szeredif3332112007-10-18 03:07:04 -0700592 if (owner != NULL) {
593 struct fuse_write_in *inarg = &req->misc.write.in;
594 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
595 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
596 }
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700597 request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700598 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700599}
600
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700601static int fuse_write_begin(struct file *file, struct address_space *mapping,
602 loff_t pos, unsigned len, unsigned flags,
603 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700604{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700605 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
606
607 *pagep = __grab_cache_page(mapping, index);
608 if (!*pagep)
609 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700610 return 0;
611}
612
Miklos Szeredi854512e2008-04-30 00:54:41 -0700613static void fuse_write_update_size(struct inode *inode, loff_t pos)
614{
615 struct fuse_conn *fc = get_fuse_conn(inode);
616 struct fuse_inode *fi = get_fuse_inode(inode);
617
618 spin_lock(&fc->lock);
619 fi->attr_version = ++fc->attr_version;
620 if (pos > inode->i_size)
621 i_size_write(inode, pos);
622 spin_unlock(&fc->lock);
623}
624
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700625static int fuse_buffered_write(struct file *file, struct inode *inode,
626 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700627{
628 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700629 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700630 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700631 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800632 struct fuse_req *req;
633
634 if (is_bad_inode(inode))
635 return -EIO;
636
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700637 /*
638 * Make sure writepages on the same page are not mixed up with
639 * plain writes.
640 */
641 fuse_wait_on_page_writeback(inode, page->index);
642
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700643 req = fuse_get_req(fc);
644 if (IS_ERR(req))
645 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700646
647 req->num_pages = 1;
648 req->pages[0] = page;
649 req->page_offset = offset;
Miklos Szeredif3332112007-10-18 03:07:04 -0700650 nres = fuse_send_write(req, file, inode, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700651 err = req->out.h.error;
652 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700653 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700654 err = -EIO;
655 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700656 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700657 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700658 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700659 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700660 }
661 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700662 return err ? err : nres;
663}
664
665static int fuse_write_end(struct file *file, struct address_space *mapping,
666 loff_t pos, unsigned len, unsigned copied,
667 struct page *page, void *fsdata)
668{
669 struct inode *inode = mapping->host;
670 int res = 0;
671
672 if (copied)
673 res = fuse_buffered_write(file, inode, pos, copied, page);
674
675 unlock_page(page);
676 page_cache_release(page);
677 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700678}
679
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700680static void fuse_release_user_pages(struct fuse_req *req, int write)
681{
682 unsigned i;
683
684 for (i = 0; i < req->num_pages; i++) {
685 struct page *page = req->pages[i];
686 if (write)
687 set_page_dirty_lock(page);
688 put_page(page);
689 }
690}
691
692static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
693 unsigned nbytes, int write)
694{
695 unsigned long user_addr = (unsigned long) buf;
696 unsigned offset = user_addr & ~PAGE_MASK;
697 int npages;
698
699 /* This doesn't work with nfsd */
700 if (!current->mm)
701 return -EPERM;
702
703 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
704 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800705 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700706 down_read(&current->mm->mmap_sem);
707 npages = get_user_pages(current, current->mm, user_addr, npages, write,
708 0, req->pages, NULL);
709 up_read(&current->mm->mmap_sem);
710 if (npages < 0)
711 return npages;
712
713 req->num_pages = npages;
714 req->page_offset = offset;
715 return 0;
716}
717
718static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
719 size_t count, loff_t *ppos, int write)
720{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800721 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700722 struct fuse_conn *fc = get_fuse_conn(inode);
723 size_t nmax = write ? fc->max_write : fc->max_read;
724 loff_t pos = *ppos;
725 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800726 struct fuse_req *req;
727
728 if (is_bad_inode(inode))
729 return -EIO;
730
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700731 req = fuse_get_req(fc);
732 if (IS_ERR(req))
733 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700734
735 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700736 size_t nres;
737 size_t nbytes = min(count, nmax);
738 int err = fuse_get_user_pages(req, buf, nbytes, !write);
739 if (err) {
740 res = err;
741 break;
742 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800743 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
744 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700745 if (write)
Miklos Szeredif3332112007-10-18 03:07:04 -0700746 nres = fuse_send_write(req, file, inode, pos, nbytes,
747 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700748 else
Miklos Szeredif3332112007-10-18 03:07:04 -0700749 nres = fuse_send_read(req, file, inode, pos, nbytes,
750 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700751 fuse_release_user_pages(req, !write);
752 if (req->out.h.error) {
753 if (!res)
754 res = req->out.h.error;
755 break;
756 } else if (nres > nbytes) {
757 res = -EIO;
758 break;
759 }
760 count -= nres;
761 res += nres;
762 pos += nres;
763 buf += nres;
764 if (nres != nbytes)
765 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200766 if (count) {
767 fuse_put_request(fc, req);
768 req = fuse_get_req(fc);
769 if (IS_ERR(req))
770 break;
771 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700772 }
773 fuse_put_request(fc, req);
774 if (res > 0) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700775 if (write)
776 fuse_write_update_size(inode, pos);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700777 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700778 }
779 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700780
781 return res;
782}
783
784static ssize_t fuse_direct_read(struct file *file, char __user *buf,
785 size_t count, loff_t *ppos)
786{
787 return fuse_direct_io(file, buf, count, ppos, 0);
788}
789
790static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
791 size_t count, loff_t *ppos)
792{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800793 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700794 ssize_t res;
795 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800796 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -0700797 res = generic_write_checks(file, ppos, &count, 0);
798 if (!res)
799 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800800 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700801 return res;
802}
803
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700804static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700805{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700806 __free_page(req->pages[0]);
807 fuse_file_put(req->ff);
808 fuse_put_request(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700809}
810
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700811static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700812{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700813 struct inode *inode = req->inode;
814 struct fuse_inode *fi = get_fuse_inode(inode);
815 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
816
817 list_del(&req->writepages_entry);
818 dec_bdi_stat(bdi, BDI_WRITEBACK);
819 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
820 bdi_writeout_inc(bdi);
821 wake_up(&fi->page_waitq);
822}
823
824/* Called under fc->lock, may release and reacquire it */
825static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
826{
827 struct fuse_inode *fi = get_fuse_inode(req->inode);
828 loff_t size = i_size_read(req->inode);
829 struct fuse_write_in *inarg = &req->misc.write.in;
830
831 if (!fc->connected)
832 goto out_free;
833
834 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
835 inarg->size = PAGE_CACHE_SIZE;
836 } else if (inarg->offset < size) {
837 inarg->size = size & (PAGE_CACHE_SIZE - 1);
838 } else {
839 /* Got truncated off completely */
840 goto out_free;
841 }
842
843 req->in.args[1].size = inarg->size;
844 fi->writectr++;
845 request_send_background_locked(fc, req);
846 return;
847
848 out_free:
849 fuse_writepage_finish(fc, req);
850 spin_unlock(&fc->lock);
851 fuse_writepage_free(fc, req);
852 spin_lock(&fc->lock);
853}
854
855/*
856 * If fi->writectr is positive (no truncate or fsync going on) send
857 * all queued writepage requests.
858 *
859 * Called with fc->lock
860 */
861void fuse_flush_writepages(struct inode *inode)
862{
863 struct fuse_conn *fc = get_fuse_conn(inode);
864 struct fuse_inode *fi = get_fuse_inode(inode);
865 struct fuse_req *req;
866
867 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
868 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
869 list_del_init(&req->list);
870 fuse_send_writepage(fc, req);
871 }
872}
873
874static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
875{
876 struct inode *inode = req->inode;
877 struct fuse_inode *fi = get_fuse_inode(inode);
878
879 mapping_set_error(inode->i_mapping, req->out.h.error);
880 spin_lock(&fc->lock);
881 fi->writectr--;
882 fuse_writepage_finish(fc, req);
883 spin_unlock(&fc->lock);
884 fuse_writepage_free(fc, req);
885}
886
887static int fuse_writepage_locked(struct page *page)
888{
889 struct address_space *mapping = page->mapping;
890 struct inode *inode = mapping->host;
891 struct fuse_conn *fc = get_fuse_conn(inode);
892 struct fuse_inode *fi = get_fuse_inode(inode);
893 struct fuse_req *req;
894 struct fuse_file *ff;
895 struct page *tmp_page;
896
897 set_page_writeback(page);
898
899 req = fuse_request_alloc_nofs();
900 if (!req)
901 goto err;
902
903 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
904 if (!tmp_page)
905 goto err_free;
906
907 spin_lock(&fc->lock);
908 BUG_ON(list_empty(&fi->write_files));
909 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
910 req->ff = fuse_file_get(ff);
911 spin_unlock(&fc->lock);
912
913 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
914
915 copy_highpage(tmp_page, page);
916 req->num_pages = 1;
917 req->pages[0] = tmp_page;
918 req->page_offset = 0;
919 req->end = fuse_writepage_end;
920 req->inode = inode;
921
922 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
923 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
924 end_page_writeback(page);
925
926 spin_lock(&fc->lock);
927 list_add(&req->writepages_entry, &fi->writepages);
928 list_add_tail(&req->list, &fi->queued_writes);
929 fuse_flush_writepages(inode);
930 spin_unlock(&fc->lock);
931
932 return 0;
933
934err_free:
935 fuse_request_free(req);
936err:
937 end_page_writeback(page);
938 return -ENOMEM;
939}
940
941static int fuse_writepage(struct page *page, struct writeback_control *wbc)
942{
943 int err;
944
945 err = fuse_writepage_locked(page);
946 unlock_page(page);
947
948 return err;
949}
950
951static int fuse_launder_page(struct page *page)
952{
953 int err = 0;
954 if (clear_page_dirty_for_io(page)) {
955 struct inode *inode = page->mapping->host;
956 err = fuse_writepage_locked(page);
957 if (!err)
958 fuse_wait_on_page_writeback(inode, page->index);
959 }
960 return err;
961}
962
963/*
964 * Write back dirty pages now, because there may not be any suitable
965 * open files later
966 */
967static void fuse_vma_close(struct vm_area_struct *vma)
968{
969 filemap_write_and_wait(vma->vm_file->f_mapping);
970}
971
972/*
973 * Wait for writeback against this page to complete before allowing it
974 * to be marked dirty again, and hence written back again, possibly
975 * before the previous writepage completed.
976 *
977 * Block here, instead of in ->writepage(), so that the userspace fs
978 * can only block processes actually operating on the filesystem.
979 *
980 * Otherwise unprivileged userspace fs would be able to block
981 * unrelated:
982 *
983 * - page migration
984 * - sync(2)
985 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
986 */
987static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
988{
989 /*
990 * Don't use page->mapping as it may become NULL from a
991 * concurrent truncate.
992 */
993 struct inode *inode = vma->vm_file->f_mapping->host;
994
995 fuse_wait_on_page_writeback(inode, page->index);
996 return 0;
997}
998
999static struct vm_operations_struct fuse_file_vm_ops = {
1000 .close = fuse_vma_close,
1001 .fault = filemap_fault,
1002 .page_mkwrite = fuse_page_mkwrite,
1003};
1004
1005static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1006{
1007 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1008 struct inode *inode = file->f_dentry->d_inode;
1009 struct fuse_conn *fc = get_fuse_conn(inode);
1010 struct fuse_inode *fi = get_fuse_inode(inode);
1011 struct fuse_file *ff = file->private_data;
1012 /*
1013 * file may be written through mmap, so chain it onto the
1014 * inodes's write_file list
1015 */
1016 spin_lock(&fc->lock);
1017 if (list_empty(&ff->write_entry))
1018 list_add(&ff->write_entry, &fi->write_files);
1019 spin_unlock(&fc->lock);
1020 }
1021 file_accessed(file);
1022 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001023 return 0;
1024}
1025
Miklos Szeredi71421252006-06-25 05:48:52 -07001026static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1027 struct file_lock *fl)
1028{
1029 switch (ffl->type) {
1030 case F_UNLCK:
1031 break;
1032
1033 case F_RDLCK:
1034 case F_WRLCK:
1035 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1036 ffl->end < ffl->start)
1037 return -EIO;
1038
1039 fl->fl_start = ffl->start;
1040 fl->fl_end = ffl->end;
1041 fl->fl_pid = ffl->pid;
1042 break;
1043
1044 default:
1045 return -EIO;
1046 }
1047 fl->fl_type = ffl->type;
1048 return 0;
1049}
1050
1051static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001052 const struct file_lock *fl, int opcode, pid_t pid,
1053 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001054{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001055 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001056 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001057 struct fuse_file *ff = file->private_data;
1058 struct fuse_lk_in *arg = &req->misc.lk_in;
1059
1060 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001061 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001062 arg->lk.start = fl->fl_start;
1063 arg->lk.end = fl->fl_end;
1064 arg->lk.type = fl->fl_type;
1065 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001066 if (flock)
1067 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001068 req->in.h.opcode = opcode;
1069 req->in.h.nodeid = get_node_id(inode);
1070 req->in.numargs = 1;
1071 req->in.args[0].size = sizeof(*arg);
1072 req->in.args[0].value = arg;
1073}
1074
1075static int fuse_getlk(struct file *file, struct file_lock *fl)
1076{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001077 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001078 struct fuse_conn *fc = get_fuse_conn(inode);
1079 struct fuse_req *req;
1080 struct fuse_lk_out outarg;
1081 int err;
1082
1083 req = fuse_get_req(fc);
1084 if (IS_ERR(req))
1085 return PTR_ERR(req);
1086
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001087 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001088 req->out.numargs = 1;
1089 req->out.args[0].size = sizeof(outarg);
1090 req->out.args[0].value = &outarg;
1091 request_send(fc, req);
1092 err = req->out.h.error;
1093 fuse_put_request(fc, req);
1094 if (!err)
1095 err = convert_fuse_file_lock(&outarg.lk, fl);
1096
1097 return err;
1098}
1099
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001100static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001101{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001102 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001103 struct fuse_conn *fc = get_fuse_conn(inode);
1104 struct fuse_req *req;
1105 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1106 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1107 int err;
1108
1109 /* Unlock on close is handled by the flush method */
1110 if (fl->fl_flags & FL_CLOSE)
1111 return 0;
1112
1113 req = fuse_get_req(fc);
1114 if (IS_ERR(req))
1115 return PTR_ERR(req);
1116
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001117 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Miklos Szeredi71421252006-06-25 05:48:52 -07001118 request_send(fc, req);
1119 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001120 /* locking is restartable */
1121 if (err == -EINTR)
1122 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001123 fuse_put_request(fc, req);
1124 return err;
1125}
1126
1127static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1128{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001129 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001130 struct fuse_conn *fc = get_fuse_conn(inode);
1131 int err;
1132
1133 if (cmd == F_GETLK) {
1134 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001135 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001136 err = 0;
1137 } else
1138 err = fuse_getlk(file, fl);
1139 } else {
1140 if (fc->no_lock)
1141 err = posix_lock_file_wait(file, fl);
1142 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001143 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001144 }
1145 return err;
1146}
1147
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001148static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1149{
1150 struct inode *inode = file->f_path.dentry->d_inode;
1151 struct fuse_conn *fc = get_fuse_conn(inode);
1152 int err;
1153
1154 if (fc->no_lock) {
1155 err = flock_lock_file_wait(file, fl);
1156 } else {
1157 /* emulate flock with POSIX locks */
1158 fl->fl_owner = (fl_owner_t) file;
1159 err = fuse_setlk(file, fl, 1);
1160 }
1161
1162 return err;
1163}
1164
Miklos Szeredib2d22722006-12-06 20:35:51 -08001165static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1166{
1167 struct inode *inode = mapping->host;
1168 struct fuse_conn *fc = get_fuse_conn(inode);
1169 struct fuse_req *req;
1170 struct fuse_bmap_in inarg;
1171 struct fuse_bmap_out outarg;
1172 int err;
1173
1174 if (!inode->i_sb->s_bdev || fc->no_bmap)
1175 return 0;
1176
1177 req = fuse_get_req(fc);
1178 if (IS_ERR(req))
1179 return 0;
1180
1181 memset(&inarg, 0, sizeof(inarg));
1182 inarg.block = block;
1183 inarg.blocksize = inode->i_sb->s_blocksize;
1184 req->in.h.opcode = FUSE_BMAP;
1185 req->in.h.nodeid = get_node_id(inode);
1186 req->in.numargs = 1;
1187 req->in.args[0].size = sizeof(inarg);
1188 req->in.args[0].value = &inarg;
1189 req->out.numargs = 1;
1190 req->out.args[0].size = sizeof(outarg);
1191 req->out.args[0].value = &outarg;
1192 request_send(fc, req);
1193 err = req->out.h.error;
1194 fuse_put_request(fc, req);
1195 if (err == -ENOSYS)
1196 fc->no_bmap = 1;
1197
1198 return err ? 0 : outarg.block;
1199}
1200
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001201static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001202 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001203 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001204 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001205 .write = do_sync_write,
1206 .aio_write = generic_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001207 .mmap = fuse_file_mmap,
1208 .open = fuse_open,
1209 .flush = fuse_flush,
1210 .release = fuse_release,
1211 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001212 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001213 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001214 .splice_read = generic_file_splice_read,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001215};
1216
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001217static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001218 .llseek = generic_file_llseek,
1219 .read = fuse_direct_read,
1220 .write = fuse_direct_write,
1221 .open = fuse_open,
1222 .flush = fuse_flush,
1223 .release = fuse_release,
1224 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001225 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001226 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001227 /* no mmap and splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001228};
1229
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001230static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001231 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001232 .writepage = fuse_writepage,
1233 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07001234 .write_begin = fuse_write_begin,
1235 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07001236 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001237 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08001238 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001239};
1240
1241void fuse_init_file_inode(struct inode *inode)
1242{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07001243 inode->i_fop = &fuse_file_operations;
1244 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001245}