blob: 68051f3bdf916601844db9e915b891341a403df5 [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
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700613static int fuse_buffered_write(struct file *file, struct inode *inode,
614 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700615{
616 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700617 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700618 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700619 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700620 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800621 struct fuse_req *req;
622
623 if (is_bad_inode(inode))
624 return -EIO;
625
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700626 /*
627 * Make sure writepages on the same page are not mixed up with
628 * plain writes.
629 */
630 fuse_wait_on_page_writeback(inode, page->index);
631
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700632 req = fuse_get_req(fc);
633 if (IS_ERR(req))
634 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700635
636 req->num_pages = 1;
637 req->pages[0] = page;
638 req->page_offset = offset;
Miklos Szeredif3332112007-10-18 03:07:04 -0700639 nres = fuse_send_write(req, file, inode, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700640 err = req->out.h.error;
641 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700642 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700643 err = -EIO;
644 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700645 pos += nres;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700646 spin_lock(&fc->lock);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700647 fi->attr_version = ++fc->attr_version;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700648 if (pos > inode->i_size)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700649 i_size_write(inode, pos);
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700650 spin_unlock(&fc->lock);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700651
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700652 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700653 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700654 }
655 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700656 return err ? err : nres;
657}
658
659static int fuse_write_end(struct file *file, struct address_space *mapping,
660 loff_t pos, unsigned len, unsigned copied,
661 struct page *page, void *fsdata)
662{
663 struct inode *inode = mapping->host;
664 int res = 0;
665
666 if (copied)
667 res = fuse_buffered_write(file, inode, pos, copied, page);
668
669 unlock_page(page);
670 page_cache_release(page);
671 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700672}
673
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700674static void fuse_release_user_pages(struct fuse_req *req, int write)
675{
676 unsigned i;
677
678 for (i = 0; i < req->num_pages; i++) {
679 struct page *page = req->pages[i];
680 if (write)
681 set_page_dirty_lock(page);
682 put_page(page);
683 }
684}
685
686static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
687 unsigned nbytes, int write)
688{
689 unsigned long user_addr = (unsigned long) buf;
690 unsigned offset = user_addr & ~PAGE_MASK;
691 int npages;
692
693 /* This doesn't work with nfsd */
694 if (!current->mm)
695 return -EPERM;
696
697 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
698 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800699 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700700 down_read(&current->mm->mmap_sem);
701 npages = get_user_pages(current, current->mm, user_addr, npages, write,
702 0, req->pages, NULL);
703 up_read(&current->mm->mmap_sem);
704 if (npages < 0)
705 return npages;
706
707 req->num_pages = npages;
708 req->page_offset = offset;
709 return 0;
710}
711
712static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
713 size_t count, loff_t *ppos, int write)
714{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800715 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700716 struct fuse_conn *fc = get_fuse_conn(inode);
717 size_t nmax = write ? fc->max_write : fc->max_read;
718 loff_t pos = *ppos;
719 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800720 struct fuse_req *req;
721
722 if (is_bad_inode(inode))
723 return -EIO;
724
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700725 req = fuse_get_req(fc);
726 if (IS_ERR(req))
727 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700728
729 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700730 size_t nres;
731 size_t nbytes = min(count, nmax);
732 int err = fuse_get_user_pages(req, buf, nbytes, !write);
733 if (err) {
734 res = err;
735 break;
736 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800737 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
738 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700739 if (write)
Miklos Szeredif3332112007-10-18 03:07:04 -0700740 nres = fuse_send_write(req, file, inode, pos, nbytes,
741 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700742 else
Miklos Szeredif3332112007-10-18 03:07:04 -0700743 nres = fuse_send_read(req, file, inode, pos, nbytes,
744 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700745 fuse_release_user_pages(req, !write);
746 if (req->out.h.error) {
747 if (!res)
748 res = req->out.h.error;
749 break;
750 } else if (nres > nbytes) {
751 res = -EIO;
752 break;
753 }
754 count -= nres;
755 res += nres;
756 pos += nres;
757 buf += nres;
758 if (nres != nbytes)
759 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200760 if (count) {
761 fuse_put_request(fc, req);
762 req = fuse_get_req(fc);
763 if (IS_ERR(req))
764 break;
765 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700766 }
767 fuse_put_request(fc, req);
768 if (res > 0) {
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700769 if (write) {
770 spin_lock(&fc->lock);
771 if (pos > inode->i_size)
772 i_size_write(inode, pos);
773 spin_unlock(&fc->lock);
774 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700775 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700776 }
777 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700778
779 return res;
780}
781
782static ssize_t fuse_direct_read(struct file *file, char __user *buf,
783 size_t count, loff_t *ppos)
784{
785 return fuse_direct_io(file, buf, count, ppos, 0);
786}
787
788static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
789 size_t count, loff_t *ppos)
790{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800791 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700792 ssize_t res;
793 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800794 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -0700795 res = generic_write_checks(file, ppos, &count, 0);
796 if (!res)
797 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800798 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700799 return res;
800}
801
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700802static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700803{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700804 __free_page(req->pages[0]);
805 fuse_file_put(req->ff);
806 fuse_put_request(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700807}
808
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700809static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700810{
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700811 struct inode *inode = req->inode;
812 struct fuse_inode *fi = get_fuse_inode(inode);
813 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
814
815 list_del(&req->writepages_entry);
816 dec_bdi_stat(bdi, BDI_WRITEBACK);
817 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
818 bdi_writeout_inc(bdi);
819 wake_up(&fi->page_waitq);
820}
821
822/* Called under fc->lock, may release and reacquire it */
823static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
824{
825 struct fuse_inode *fi = get_fuse_inode(req->inode);
826 loff_t size = i_size_read(req->inode);
827 struct fuse_write_in *inarg = &req->misc.write.in;
828
829 if (!fc->connected)
830 goto out_free;
831
832 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
833 inarg->size = PAGE_CACHE_SIZE;
834 } else if (inarg->offset < size) {
835 inarg->size = size & (PAGE_CACHE_SIZE - 1);
836 } else {
837 /* Got truncated off completely */
838 goto out_free;
839 }
840
841 req->in.args[1].size = inarg->size;
842 fi->writectr++;
843 request_send_background_locked(fc, req);
844 return;
845
846 out_free:
847 fuse_writepage_finish(fc, req);
848 spin_unlock(&fc->lock);
849 fuse_writepage_free(fc, req);
850 spin_lock(&fc->lock);
851}
852
853/*
854 * If fi->writectr is positive (no truncate or fsync going on) send
855 * all queued writepage requests.
856 *
857 * Called with fc->lock
858 */
859void fuse_flush_writepages(struct inode *inode)
860{
861 struct fuse_conn *fc = get_fuse_conn(inode);
862 struct fuse_inode *fi = get_fuse_inode(inode);
863 struct fuse_req *req;
864
865 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
866 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
867 list_del_init(&req->list);
868 fuse_send_writepage(fc, req);
869 }
870}
871
872static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
873{
874 struct inode *inode = req->inode;
875 struct fuse_inode *fi = get_fuse_inode(inode);
876
877 mapping_set_error(inode->i_mapping, req->out.h.error);
878 spin_lock(&fc->lock);
879 fi->writectr--;
880 fuse_writepage_finish(fc, req);
881 spin_unlock(&fc->lock);
882 fuse_writepage_free(fc, req);
883}
884
885static int fuse_writepage_locked(struct page *page)
886{
887 struct address_space *mapping = page->mapping;
888 struct inode *inode = mapping->host;
889 struct fuse_conn *fc = get_fuse_conn(inode);
890 struct fuse_inode *fi = get_fuse_inode(inode);
891 struct fuse_req *req;
892 struct fuse_file *ff;
893 struct page *tmp_page;
894
895 set_page_writeback(page);
896
897 req = fuse_request_alloc_nofs();
898 if (!req)
899 goto err;
900
901 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
902 if (!tmp_page)
903 goto err_free;
904
905 spin_lock(&fc->lock);
906 BUG_ON(list_empty(&fi->write_files));
907 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
908 req->ff = fuse_file_get(ff);
909 spin_unlock(&fc->lock);
910
911 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
912
913 copy_highpage(tmp_page, page);
914 req->num_pages = 1;
915 req->pages[0] = tmp_page;
916 req->page_offset = 0;
917 req->end = fuse_writepage_end;
918 req->inode = inode;
919
920 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
921 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
922 end_page_writeback(page);
923
924 spin_lock(&fc->lock);
925 list_add(&req->writepages_entry, &fi->writepages);
926 list_add_tail(&req->list, &fi->queued_writes);
927 fuse_flush_writepages(inode);
928 spin_unlock(&fc->lock);
929
930 return 0;
931
932err_free:
933 fuse_request_free(req);
934err:
935 end_page_writeback(page);
936 return -ENOMEM;
937}
938
939static int fuse_writepage(struct page *page, struct writeback_control *wbc)
940{
941 int err;
942
943 err = fuse_writepage_locked(page);
944 unlock_page(page);
945
946 return err;
947}
948
949static int fuse_launder_page(struct page *page)
950{
951 int err = 0;
952 if (clear_page_dirty_for_io(page)) {
953 struct inode *inode = page->mapping->host;
954 err = fuse_writepage_locked(page);
955 if (!err)
956 fuse_wait_on_page_writeback(inode, page->index);
957 }
958 return err;
959}
960
961/*
962 * Write back dirty pages now, because there may not be any suitable
963 * open files later
964 */
965static void fuse_vma_close(struct vm_area_struct *vma)
966{
967 filemap_write_and_wait(vma->vm_file->f_mapping);
968}
969
970/*
971 * Wait for writeback against this page to complete before allowing it
972 * to be marked dirty again, and hence written back again, possibly
973 * before the previous writepage completed.
974 *
975 * Block here, instead of in ->writepage(), so that the userspace fs
976 * can only block processes actually operating on the filesystem.
977 *
978 * Otherwise unprivileged userspace fs would be able to block
979 * unrelated:
980 *
981 * - page migration
982 * - sync(2)
983 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
984 */
985static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
986{
987 /*
988 * Don't use page->mapping as it may become NULL from a
989 * concurrent truncate.
990 */
991 struct inode *inode = vma->vm_file->f_mapping->host;
992
993 fuse_wait_on_page_writeback(inode, page->index);
994 return 0;
995}
996
997static struct vm_operations_struct fuse_file_vm_ops = {
998 .close = fuse_vma_close,
999 .fault = filemap_fault,
1000 .page_mkwrite = fuse_page_mkwrite,
1001};
1002
1003static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1004{
1005 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1006 struct inode *inode = file->f_dentry->d_inode;
1007 struct fuse_conn *fc = get_fuse_conn(inode);
1008 struct fuse_inode *fi = get_fuse_inode(inode);
1009 struct fuse_file *ff = file->private_data;
1010 /*
1011 * file may be written through mmap, so chain it onto the
1012 * inodes's write_file list
1013 */
1014 spin_lock(&fc->lock);
1015 if (list_empty(&ff->write_entry))
1016 list_add(&ff->write_entry, &fi->write_files);
1017 spin_unlock(&fc->lock);
1018 }
1019 file_accessed(file);
1020 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001021 return 0;
1022}
1023
Miklos Szeredi71421252006-06-25 05:48:52 -07001024static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1025 struct file_lock *fl)
1026{
1027 switch (ffl->type) {
1028 case F_UNLCK:
1029 break;
1030
1031 case F_RDLCK:
1032 case F_WRLCK:
1033 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1034 ffl->end < ffl->start)
1035 return -EIO;
1036
1037 fl->fl_start = ffl->start;
1038 fl->fl_end = ffl->end;
1039 fl->fl_pid = ffl->pid;
1040 break;
1041
1042 default:
1043 return -EIO;
1044 }
1045 fl->fl_type = ffl->type;
1046 return 0;
1047}
1048
1049static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001050 const struct file_lock *fl, int opcode, pid_t pid,
1051 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001052{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001053 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001054 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001055 struct fuse_file *ff = file->private_data;
1056 struct fuse_lk_in *arg = &req->misc.lk_in;
1057
1058 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001059 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001060 arg->lk.start = fl->fl_start;
1061 arg->lk.end = fl->fl_end;
1062 arg->lk.type = fl->fl_type;
1063 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001064 if (flock)
1065 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001066 req->in.h.opcode = opcode;
1067 req->in.h.nodeid = get_node_id(inode);
1068 req->in.numargs = 1;
1069 req->in.args[0].size = sizeof(*arg);
1070 req->in.args[0].value = arg;
1071}
1072
1073static int fuse_getlk(struct file *file, struct file_lock *fl)
1074{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001075 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001076 struct fuse_conn *fc = get_fuse_conn(inode);
1077 struct fuse_req *req;
1078 struct fuse_lk_out outarg;
1079 int err;
1080
1081 req = fuse_get_req(fc);
1082 if (IS_ERR(req))
1083 return PTR_ERR(req);
1084
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001085 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001086 req->out.numargs = 1;
1087 req->out.args[0].size = sizeof(outarg);
1088 req->out.args[0].value = &outarg;
1089 request_send(fc, req);
1090 err = req->out.h.error;
1091 fuse_put_request(fc, req);
1092 if (!err)
1093 err = convert_fuse_file_lock(&outarg.lk, fl);
1094
1095 return err;
1096}
1097
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001098static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001099{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001100 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001101 struct fuse_conn *fc = get_fuse_conn(inode);
1102 struct fuse_req *req;
1103 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1104 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1105 int err;
1106
1107 /* Unlock on close is handled by the flush method */
1108 if (fl->fl_flags & FL_CLOSE)
1109 return 0;
1110
1111 req = fuse_get_req(fc);
1112 if (IS_ERR(req))
1113 return PTR_ERR(req);
1114
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001115 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Miklos Szeredi71421252006-06-25 05:48:52 -07001116 request_send(fc, req);
1117 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001118 /* locking is restartable */
1119 if (err == -EINTR)
1120 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001121 fuse_put_request(fc, req);
1122 return err;
1123}
1124
1125static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1126{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001127 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001128 struct fuse_conn *fc = get_fuse_conn(inode);
1129 int err;
1130
1131 if (cmd == F_GETLK) {
1132 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001133 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001134 err = 0;
1135 } else
1136 err = fuse_getlk(file, fl);
1137 } else {
1138 if (fc->no_lock)
1139 err = posix_lock_file_wait(file, fl);
1140 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001141 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001142 }
1143 return err;
1144}
1145
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001146static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1147{
1148 struct inode *inode = file->f_path.dentry->d_inode;
1149 struct fuse_conn *fc = get_fuse_conn(inode);
1150 int err;
1151
1152 if (fc->no_lock) {
1153 err = flock_lock_file_wait(file, fl);
1154 } else {
1155 /* emulate flock with POSIX locks */
1156 fl->fl_owner = (fl_owner_t) file;
1157 err = fuse_setlk(file, fl, 1);
1158 }
1159
1160 return err;
1161}
1162
Miklos Szeredib2d22722006-12-06 20:35:51 -08001163static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1164{
1165 struct inode *inode = mapping->host;
1166 struct fuse_conn *fc = get_fuse_conn(inode);
1167 struct fuse_req *req;
1168 struct fuse_bmap_in inarg;
1169 struct fuse_bmap_out outarg;
1170 int err;
1171
1172 if (!inode->i_sb->s_bdev || fc->no_bmap)
1173 return 0;
1174
1175 req = fuse_get_req(fc);
1176 if (IS_ERR(req))
1177 return 0;
1178
1179 memset(&inarg, 0, sizeof(inarg));
1180 inarg.block = block;
1181 inarg.blocksize = inode->i_sb->s_blocksize;
1182 req->in.h.opcode = FUSE_BMAP;
1183 req->in.h.nodeid = get_node_id(inode);
1184 req->in.numargs = 1;
1185 req->in.args[0].size = sizeof(inarg);
1186 req->in.args[0].value = &inarg;
1187 req->out.numargs = 1;
1188 req->out.args[0].size = sizeof(outarg);
1189 req->out.args[0].value = &outarg;
1190 request_send(fc, req);
1191 err = req->out.h.error;
1192 fuse_put_request(fc, req);
1193 if (err == -ENOSYS)
1194 fc->no_bmap = 1;
1195
1196 return err ? 0 : outarg.block;
1197}
1198
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001199static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001200 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001201 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001202 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001203 .write = do_sync_write,
1204 .aio_write = generic_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001205 .mmap = fuse_file_mmap,
1206 .open = fuse_open,
1207 .flush = fuse_flush,
1208 .release = fuse_release,
1209 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001210 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001211 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001212 .splice_read = generic_file_splice_read,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001213};
1214
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001215static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001216 .llseek = generic_file_llseek,
1217 .read = fuse_direct_read,
1218 .write = fuse_direct_write,
1219 .open = fuse_open,
1220 .flush = fuse_flush,
1221 .release = fuse_release,
1222 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001223 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001224 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001225 /* no mmap and splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001226};
1227
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001228static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001229 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001230 .writepage = fuse_writepage,
1231 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07001232 .write_begin = fuse_write_begin,
1233 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07001234 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001235 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08001236 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001237};
1238
1239void fuse_init_file_inode(struct inode *inode)
1240{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07001241 inode->i_fop = &fuse_file_operations;
1242 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001243}