blob: a02418c89d4bbc52a9670d6a91cc2b1e68503a63 [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 Szeredi5c5c5e52008-04-30 00:54:43 -0700366 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) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700392 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700393
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 Szeredi5c5c5e52008-04-30 00:54:43 -0700401static void fuse_read_update_size(struct inode *inode, loff_t size,
402 u64 attr_ver)
403{
404 struct fuse_conn *fc = get_fuse_conn(inode);
405 struct fuse_inode *fi = get_fuse_inode(inode);
406
407 spin_lock(&fc->lock);
408 if (attr_ver == fi->attr_version && size < inode->i_size) {
409 fi->attr_version = ++fc->attr_version;
410 i_size_write(inode, size);
411 }
412 spin_unlock(&fc->lock);
413}
414
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700415static int fuse_readpage(struct file *file, struct page *page)
416{
417 struct inode *inode = page->mapping->host;
418 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800419 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700420 size_t num_read;
421 loff_t pos = page_offset(page);
422 size_t count = PAGE_CACHE_SIZE;
423 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800424 int err;
425
426 err = -EIO;
427 if (is_bad_inode(inode))
428 goto out;
429
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700430 /*
431 * Page writeback can extend beyond the liftime of the
432 * page-cache page, so make sure we read a properly synced
433 * page.
434 */
435 fuse_wait_on_page_writeback(inode, page->index);
436
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700437 req = fuse_get_req(fc);
438 err = PTR_ERR(req);
439 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 goto out;
441
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700442 attr_ver = fuse_get_attr_version(fc);
443
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 req->out.page_zeroing = 1;
445 req->num_pages = 1;
446 req->pages[0] = page;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700447 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 err = req->out.h.error;
449 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700450
451 if (!err) {
452 /*
453 * Short read means EOF. If file size is larger, truncate it
454 */
455 if (num_read < count)
456 fuse_read_update_size(inode, pos + num_read, attr_ver);
457
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700459 }
460
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700461 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 out:
463 unlock_page(page);
464 return err;
465}
466
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800467static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700468{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800469 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700470 size_t count = req->misc.read.in.size;
471 size_t num_read = req->out.args[0].size;
472 struct inode *inode = req->pages[0]->mapping->host;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800473
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700474 /*
475 * Short read means EOF. If file size is larger, truncate it
476 */
477 if (!req->out.h.error && num_read < count) {
478 loff_t pos = page_offset(req->pages[0]) + num_read;
479 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
480 }
481
482 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800483
Miklos Szeredidb50b962005-09-09 13:10:33 -0700484 for (i = 0; i < req->num_pages; i++) {
485 struct page *page = req->pages[i];
486 if (!req->out.h.error)
487 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800488 else
489 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700490 unlock_page(page);
491 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700492 if (req->ff)
493 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800494 fuse_put_request(fc, req);
495}
496
Miklos Szeredia6643092007-11-28 16:22:00 -0800497static void fuse_send_readpages(struct fuse_req *req, struct file *file,
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800498 struct inode *inode)
499{
500 struct fuse_conn *fc = get_fuse_conn(inode);
501 loff_t pos = page_offset(req->pages[0]);
502 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
503 req->out.page_zeroing = 1;
Miklos Szeredia6643092007-11-28 16:22:00 -0800504 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700505 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800506 if (fc->async_read) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800507 struct fuse_file *ff = file->private_data;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700508 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800509 req->end = fuse_readpages_end;
510 request_send_background(fc, req);
511 } else {
512 request_send(fc, req);
513 fuse_readpages_end(fc, req);
514 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700515}
516
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700517struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700518 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800519 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700520 struct inode *inode;
521};
522
523static int fuse_readpages_fill(void *_data, struct page *page)
524{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700525 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700526 struct fuse_req *req = data->req;
527 struct inode *inode = data->inode;
528 struct fuse_conn *fc = get_fuse_conn(inode);
529
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700530 fuse_wait_on_page_writeback(inode, page->index);
531
Miklos Szeredidb50b962005-09-09 13:10:33 -0700532 if (req->num_pages &&
533 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
534 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
535 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredia6643092007-11-28 16:22:00 -0800536 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700537 data->req = req = fuse_get_req(fc);
538 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700539 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700540 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700541 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700542 }
543 req->pages[req->num_pages] = page;
544 req->num_pages ++;
545 return 0;
546}
547
548static int fuse_readpages(struct file *file, struct address_space *mapping,
549 struct list_head *pages, unsigned nr_pages)
550{
551 struct inode *inode = mapping->host;
552 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700553 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700554 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800555
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700556 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800557 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800558 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800559
Miklos Szeredia6643092007-11-28 16:22:00 -0800560 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700561 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700562 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700563 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700564 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800565 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700566
567 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700568 if (!err) {
569 if (data.req->num_pages)
Miklos Szeredia6643092007-11-28 16:22:00 -0800570 fuse_send_readpages(data.req, file, inode);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700571 else
572 fuse_put_request(fc, data.req);
573 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800574out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700575 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700576}
577
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800578static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
579 unsigned long nr_segs, loff_t pos)
580{
581 struct inode *inode = iocb->ki_filp->f_mapping->host;
582
583 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
584 int err;
585 /*
586 * If trying to read past EOF, make sure the i_size
587 * attribute is up-to-date.
588 */
589 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
590 if (err)
591 return err;
592 }
593
594 return generic_file_aio_read(iocb, iov, nr_segs, pos);
595}
596
Miklos Szeredia6643092007-11-28 16:22:00 -0800597static void fuse_write_fill(struct fuse_req *req, struct file *file,
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700598 struct fuse_file *ff, struct inode *inode,
599 loff_t pos, size_t count, int writepage)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700600{
Miklos Szeredif3332112007-10-18 03:07:04 -0700601 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700602 struct fuse_write_in *inarg = &req->misc.write.in;
603 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700604
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700605 memset(inarg, 0, sizeof(struct fuse_write_in));
606 inarg->fh = ff->fh;
607 inarg->offset = pos;
608 inarg->size = count;
609 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700610 inarg->flags = file ? file->f_flags : 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700611 req->in.h.opcode = FUSE_WRITE;
612 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700613 req->in.argpages = 1;
614 req->in.numargs = 2;
Miklos Szeredif3332112007-10-18 03:07:04 -0700615 if (fc->minor < 9)
616 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
617 else
618 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700619 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700620 req->in.args[1].size = count;
621 req->out.numargs = 1;
622 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700623 req->out.args[0].value = outarg;
624}
625
626static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredif3332112007-10-18 03:07:04 -0700627 struct inode *inode, loff_t pos, size_t count,
628 fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700629{
630 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700631 fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
Miklos Szeredif3332112007-10-18 03:07:04 -0700632 if (owner != NULL) {
633 struct fuse_write_in *inarg = &req->misc.write.in;
634 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
635 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
636 }
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700637 request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700638 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700639}
640
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700641static int fuse_write_begin(struct file *file, struct address_space *mapping,
642 loff_t pos, unsigned len, unsigned flags,
643 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700644{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700645 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
646
647 *pagep = __grab_cache_page(mapping, index);
648 if (!*pagep)
649 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700650 return 0;
651}
652
Miklos Szeredi854512e2008-04-30 00:54:41 -0700653static void fuse_write_update_size(struct inode *inode, loff_t pos)
654{
655 struct fuse_conn *fc = get_fuse_conn(inode);
656 struct fuse_inode *fi = get_fuse_inode(inode);
657
658 spin_lock(&fc->lock);
659 fi->attr_version = ++fc->attr_version;
660 if (pos > inode->i_size)
661 i_size_write(inode, pos);
662 spin_unlock(&fc->lock);
663}
664
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700665static int fuse_buffered_write(struct file *file, struct inode *inode,
666 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700667{
668 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700669 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700670 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700671 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800672 struct fuse_req *req;
673
674 if (is_bad_inode(inode))
675 return -EIO;
676
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700677 /*
678 * Make sure writepages on the same page are not mixed up with
679 * plain writes.
680 */
681 fuse_wait_on_page_writeback(inode, page->index);
682
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700683 req = fuse_get_req(fc);
684 if (IS_ERR(req))
685 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700686
687 req->num_pages = 1;
688 req->pages[0] = page;
689 req->page_offset = offset;
Miklos Szeredif3332112007-10-18 03:07:04 -0700690 nres = fuse_send_write(req, file, inode, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700691 err = req->out.h.error;
692 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700693 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700694 err = -EIO;
695 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700696 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700697 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700698 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700699 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700700 }
701 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700702 return err ? err : nres;
703}
704
705static int fuse_write_end(struct file *file, struct address_space *mapping,
706 loff_t pos, unsigned len, unsigned copied,
707 struct page *page, void *fsdata)
708{
709 struct inode *inode = mapping->host;
710 int res = 0;
711
712 if (copied)
713 res = fuse_buffered_write(file, inode, pos, copied, page);
714
715 unlock_page(page);
716 page_cache_release(page);
717 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700718}
719
Nick Pigginea9b9902008-04-30 00:54:42 -0700720static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
721 struct inode *inode, loff_t pos,
722 size_t count)
723{
724 size_t res;
725 unsigned offset;
726 unsigned i;
727
728 for (i = 0; i < req->num_pages; i++)
729 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
730
731 res = fuse_send_write(req, file, inode, pos, count, NULL);
732
733 offset = req->page_offset;
734 count = res;
735 for (i = 0; i < req->num_pages; i++) {
736 struct page *page = req->pages[i];
737
738 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
739 SetPageUptodate(page);
740
741 if (count > PAGE_CACHE_SIZE - offset)
742 count -= PAGE_CACHE_SIZE - offset;
743 else
744 count = 0;
745 offset = 0;
746
747 unlock_page(page);
748 page_cache_release(page);
749 }
750
751 return res;
752}
753
754static ssize_t fuse_fill_write_pages(struct fuse_req *req,
755 struct address_space *mapping,
756 struct iov_iter *ii, loff_t pos)
757{
758 struct fuse_conn *fc = get_fuse_conn(mapping->host);
759 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
760 size_t count = 0;
761 int err;
762
763 req->page_offset = offset;
764
765 do {
766 size_t tmp;
767 struct page *page;
768 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
769 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
770 iov_iter_count(ii));
771
772 bytes = min_t(size_t, bytes, fc->max_write - count);
773
774 again:
775 err = -EFAULT;
776 if (iov_iter_fault_in_readable(ii, bytes))
777 break;
778
779 err = -ENOMEM;
780 page = __grab_cache_page(mapping, index);
781 if (!page)
782 break;
783
784 pagefault_disable();
785 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
786 pagefault_enable();
787 flush_dcache_page(page);
788
789 if (!tmp) {
790 unlock_page(page);
791 page_cache_release(page);
792 bytes = min(bytes, iov_iter_single_seg_count(ii));
793 goto again;
794 }
795
796 err = 0;
797 req->pages[req->num_pages] = page;
798 req->num_pages++;
799
800 iov_iter_advance(ii, tmp);
801 count += tmp;
802 pos += tmp;
803 offset += tmp;
804 if (offset == PAGE_CACHE_SIZE)
805 offset = 0;
806
807 } while (iov_iter_count(ii) && count < fc->max_write &&
808 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
809
810 return count > 0 ? count : err;
811}
812
813static ssize_t fuse_perform_write(struct file *file,
814 struct address_space *mapping,
815 struct iov_iter *ii, loff_t pos)
816{
817 struct inode *inode = mapping->host;
818 struct fuse_conn *fc = get_fuse_conn(inode);
819 int err = 0;
820 ssize_t res = 0;
821
822 if (is_bad_inode(inode))
823 return -EIO;
824
825 do {
826 struct fuse_req *req;
827 ssize_t count;
828
829 req = fuse_get_req(fc);
830 if (IS_ERR(req)) {
831 err = PTR_ERR(req);
832 break;
833 }
834
835 count = fuse_fill_write_pages(req, mapping, ii, pos);
836 if (count <= 0) {
837 err = count;
838 } else {
839 size_t num_written;
840
841 num_written = fuse_send_write_pages(req, file, inode,
842 pos, count);
843 err = req->out.h.error;
844 if (!err) {
845 res += num_written;
846 pos += num_written;
847
848 /* break out of the loop on short write */
849 if (num_written != count)
850 err = -EIO;
851 }
852 }
853 fuse_put_request(fc, req);
854 } while (!err && iov_iter_count(ii));
855
856 if (res > 0)
857 fuse_write_update_size(inode, pos);
858
859 fuse_invalidate_attr(inode);
860
861 return res > 0 ? res : err;
862}
863
864static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
865 unsigned long nr_segs, loff_t pos)
866{
867 struct file *file = iocb->ki_filp;
868 struct address_space *mapping = file->f_mapping;
869 size_t count = 0;
870 ssize_t written = 0;
871 struct inode *inode = mapping->host;
872 ssize_t err;
873 struct iov_iter i;
874
875 WARN_ON(iocb->ki_pos != pos);
876
877 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
878 if (err)
879 return err;
880
881 mutex_lock(&inode->i_mutex);
882 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
883
884 /* We can write back this queue in page reclaim */
885 current->backing_dev_info = mapping->backing_dev_info;
886
887 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
888 if (err)
889 goto out;
890
891 if (count == 0)
892 goto out;
893
894 err = remove_suid(file->f_path.dentry);
895 if (err)
896 goto out;
897
898 file_update_time(file);
899
900 iov_iter_init(&i, iov, nr_segs, count, 0);
901 written = fuse_perform_write(file, mapping, &i, pos);
902 if (written >= 0)
903 iocb->ki_pos = pos + written;
904
905out:
906 current->backing_dev_info = NULL;
907 mutex_unlock(&inode->i_mutex);
908
909 return written ? written : err;
910}
911
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700912static void fuse_release_user_pages(struct fuse_req *req, int write)
913{
914 unsigned i;
915
916 for (i = 0; i < req->num_pages; i++) {
917 struct page *page = req->pages[i];
918 if (write)
919 set_page_dirty_lock(page);
920 put_page(page);
921 }
922}
923
924static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
925 unsigned nbytes, int write)
926{
927 unsigned long user_addr = (unsigned long) buf;
928 unsigned offset = user_addr & ~PAGE_MASK;
929 int npages;
930
931 /* This doesn't work with nfsd */
932 if (!current->mm)
933 return -EPERM;
934
935 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
936 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800937 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700938 down_read(&current->mm->mmap_sem);
939 npages = get_user_pages(current, current->mm, user_addr, npages, write,
940 0, req->pages, NULL);
941 up_read(&current->mm->mmap_sem);
942 if (npages < 0)
943 return npages;
944
945 req->num_pages = npages;
946 req->page_offset = offset;
947 return 0;
948}
949
950static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
951 size_t count, loff_t *ppos, int write)
952{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800953 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700954 struct fuse_conn *fc = get_fuse_conn(inode);
955 size_t nmax = write ? fc->max_write : fc->max_read;
956 loff_t pos = *ppos;
957 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800958 struct fuse_req *req;
959
960 if (is_bad_inode(inode))
961 return -EIO;
962
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700963 req = fuse_get_req(fc);
964 if (IS_ERR(req))
965 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700966
967 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700968 size_t nres;
969 size_t nbytes = min(count, nmax);
970 int err = fuse_get_user_pages(req, buf, nbytes, !write);
971 if (err) {
972 res = err;
973 break;
974 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800975 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
976 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700977 if (write)
Miklos Szeredif3332112007-10-18 03:07:04 -0700978 nres = fuse_send_write(req, file, inode, pos, nbytes,
979 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700980 else
Miklos Szeredif3332112007-10-18 03:07:04 -0700981 nres = fuse_send_read(req, file, inode, pos, nbytes,
982 current->files);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700983 fuse_release_user_pages(req, !write);
984 if (req->out.h.error) {
985 if (!res)
986 res = req->out.h.error;
987 break;
988 } else if (nres > nbytes) {
989 res = -EIO;
990 break;
991 }
992 count -= nres;
993 res += nres;
994 pos += nres;
995 buf += nres;
996 if (nres != nbytes)
997 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200998 if (count) {
999 fuse_put_request(fc, req);
1000 req = fuse_get_req(fc);
1001 if (IS_ERR(req))
1002 break;
1003 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001004 }
1005 fuse_put_request(fc, req);
1006 if (res > 0) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001007 if (write)
1008 fuse_write_update_size(inode, pos);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001009 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001010 }
1011 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001012
1013 return res;
1014}
1015
1016static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1017 size_t count, loff_t *ppos)
1018{
1019 return fuse_direct_io(file, buf, count, ppos, 0);
1020}
1021
1022static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1023 size_t count, loff_t *ppos)
1024{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001025 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001026 ssize_t res;
1027 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001028 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001029 res = generic_write_checks(file, ppos, &count, 0);
1030 if (!res)
1031 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001032 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001033 return res;
1034}
1035
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001036static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001037{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001038 __free_page(req->pages[0]);
1039 fuse_file_put(req->ff);
1040 fuse_put_request(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001041}
1042
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001043static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001044{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001045 struct inode *inode = req->inode;
1046 struct fuse_inode *fi = get_fuse_inode(inode);
1047 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1048
1049 list_del(&req->writepages_entry);
1050 dec_bdi_stat(bdi, BDI_WRITEBACK);
1051 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1052 bdi_writeout_inc(bdi);
1053 wake_up(&fi->page_waitq);
1054}
1055
1056/* Called under fc->lock, may release and reacquire it */
1057static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1058{
1059 struct fuse_inode *fi = get_fuse_inode(req->inode);
1060 loff_t size = i_size_read(req->inode);
1061 struct fuse_write_in *inarg = &req->misc.write.in;
1062
1063 if (!fc->connected)
1064 goto out_free;
1065
1066 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1067 inarg->size = PAGE_CACHE_SIZE;
1068 } else if (inarg->offset < size) {
1069 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1070 } else {
1071 /* Got truncated off completely */
1072 goto out_free;
1073 }
1074
1075 req->in.args[1].size = inarg->size;
1076 fi->writectr++;
1077 request_send_background_locked(fc, req);
1078 return;
1079
1080 out_free:
1081 fuse_writepage_finish(fc, req);
1082 spin_unlock(&fc->lock);
1083 fuse_writepage_free(fc, req);
1084 spin_lock(&fc->lock);
1085}
1086
1087/*
1088 * If fi->writectr is positive (no truncate or fsync going on) send
1089 * all queued writepage requests.
1090 *
1091 * Called with fc->lock
1092 */
1093void fuse_flush_writepages(struct inode *inode)
1094{
1095 struct fuse_conn *fc = get_fuse_conn(inode);
1096 struct fuse_inode *fi = get_fuse_inode(inode);
1097 struct fuse_req *req;
1098
1099 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1100 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1101 list_del_init(&req->list);
1102 fuse_send_writepage(fc, req);
1103 }
1104}
1105
1106static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1107{
1108 struct inode *inode = req->inode;
1109 struct fuse_inode *fi = get_fuse_inode(inode);
1110
1111 mapping_set_error(inode->i_mapping, req->out.h.error);
1112 spin_lock(&fc->lock);
1113 fi->writectr--;
1114 fuse_writepage_finish(fc, req);
1115 spin_unlock(&fc->lock);
1116 fuse_writepage_free(fc, req);
1117}
1118
1119static int fuse_writepage_locked(struct page *page)
1120{
1121 struct address_space *mapping = page->mapping;
1122 struct inode *inode = mapping->host;
1123 struct fuse_conn *fc = get_fuse_conn(inode);
1124 struct fuse_inode *fi = get_fuse_inode(inode);
1125 struct fuse_req *req;
1126 struct fuse_file *ff;
1127 struct page *tmp_page;
1128
1129 set_page_writeback(page);
1130
1131 req = fuse_request_alloc_nofs();
1132 if (!req)
1133 goto err;
1134
1135 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1136 if (!tmp_page)
1137 goto err_free;
1138
1139 spin_lock(&fc->lock);
1140 BUG_ON(list_empty(&fi->write_files));
1141 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1142 req->ff = fuse_file_get(ff);
1143 spin_unlock(&fc->lock);
1144
1145 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
1146
1147 copy_highpage(tmp_page, page);
1148 req->num_pages = 1;
1149 req->pages[0] = tmp_page;
1150 req->page_offset = 0;
1151 req->end = fuse_writepage_end;
1152 req->inode = inode;
1153
1154 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1155 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1156 end_page_writeback(page);
1157
1158 spin_lock(&fc->lock);
1159 list_add(&req->writepages_entry, &fi->writepages);
1160 list_add_tail(&req->list, &fi->queued_writes);
1161 fuse_flush_writepages(inode);
1162 spin_unlock(&fc->lock);
1163
1164 return 0;
1165
1166err_free:
1167 fuse_request_free(req);
1168err:
1169 end_page_writeback(page);
1170 return -ENOMEM;
1171}
1172
1173static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1174{
1175 int err;
1176
1177 err = fuse_writepage_locked(page);
1178 unlock_page(page);
1179
1180 return err;
1181}
1182
1183static int fuse_launder_page(struct page *page)
1184{
1185 int err = 0;
1186 if (clear_page_dirty_for_io(page)) {
1187 struct inode *inode = page->mapping->host;
1188 err = fuse_writepage_locked(page);
1189 if (!err)
1190 fuse_wait_on_page_writeback(inode, page->index);
1191 }
1192 return err;
1193}
1194
1195/*
1196 * Write back dirty pages now, because there may not be any suitable
1197 * open files later
1198 */
1199static void fuse_vma_close(struct vm_area_struct *vma)
1200{
1201 filemap_write_and_wait(vma->vm_file->f_mapping);
1202}
1203
1204/*
1205 * Wait for writeback against this page to complete before allowing it
1206 * to be marked dirty again, and hence written back again, possibly
1207 * before the previous writepage completed.
1208 *
1209 * Block here, instead of in ->writepage(), so that the userspace fs
1210 * can only block processes actually operating on the filesystem.
1211 *
1212 * Otherwise unprivileged userspace fs would be able to block
1213 * unrelated:
1214 *
1215 * - page migration
1216 * - sync(2)
1217 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1218 */
1219static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1220{
1221 /*
1222 * Don't use page->mapping as it may become NULL from a
1223 * concurrent truncate.
1224 */
1225 struct inode *inode = vma->vm_file->f_mapping->host;
1226
1227 fuse_wait_on_page_writeback(inode, page->index);
1228 return 0;
1229}
1230
1231static struct vm_operations_struct fuse_file_vm_ops = {
1232 .close = fuse_vma_close,
1233 .fault = filemap_fault,
1234 .page_mkwrite = fuse_page_mkwrite,
1235};
1236
1237static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1238{
1239 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1240 struct inode *inode = file->f_dentry->d_inode;
1241 struct fuse_conn *fc = get_fuse_conn(inode);
1242 struct fuse_inode *fi = get_fuse_inode(inode);
1243 struct fuse_file *ff = file->private_data;
1244 /*
1245 * file may be written through mmap, so chain it onto the
1246 * inodes's write_file list
1247 */
1248 spin_lock(&fc->lock);
1249 if (list_empty(&ff->write_entry))
1250 list_add(&ff->write_entry, &fi->write_files);
1251 spin_unlock(&fc->lock);
1252 }
1253 file_accessed(file);
1254 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001255 return 0;
1256}
1257
Miklos Szeredi71421252006-06-25 05:48:52 -07001258static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1259 struct file_lock *fl)
1260{
1261 switch (ffl->type) {
1262 case F_UNLCK:
1263 break;
1264
1265 case F_RDLCK:
1266 case F_WRLCK:
1267 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1268 ffl->end < ffl->start)
1269 return -EIO;
1270
1271 fl->fl_start = ffl->start;
1272 fl->fl_end = ffl->end;
1273 fl->fl_pid = ffl->pid;
1274 break;
1275
1276 default:
1277 return -EIO;
1278 }
1279 fl->fl_type = ffl->type;
1280 return 0;
1281}
1282
1283static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001284 const struct file_lock *fl, int opcode, pid_t pid,
1285 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001286{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001287 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001288 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001289 struct fuse_file *ff = file->private_data;
1290 struct fuse_lk_in *arg = &req->misc.lk_in;
1291
1292 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001293 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001294 arg->lk.start = fl->fl_start;
1295 arg->lk.end = fl->fl_end;
1296 arg->lk.type = fl->fl_type;
1297 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001298 if (flock)
1299 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001300 req->in.h.opcode = opcode;
1301 req->in.h.nodeid = get_node_id(inode);
1302 req->in.numargs = 1;
1303 req->in.args[0].size = sizeof(*arg);
1304 req->in.args[0].value = arg;
1305}
1306
1307static int fuse_getlk(struct file *file, struct file_lock *fl)
1308{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001309 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001310 struct fuse_conn *fc = get_fuse_conn(inode);
1311 struct fuse_req *req;
1312 struct fuse_lk_out outarg;
1313 int err;
1314
1315 req = fuse_get_req(fc);
1316 if (IS_ERR(req))
1317 return PTR_ERR(req);
1318
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001319 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001320 req->out.numargs = 1;
1321 req->out.args[0].size = sizeof(outarg);
1322 req->out.args[0].value = &outarg;
1323 request_send(fc, req);
1324 err = req->out.h.error;
1325 fuse_put_request(fc, req);
1326 if (!err)
1327 err = convert_fuse_file_lock(&outarg.lk, fl);
1328
1329 return err;
1330}
1331
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001332static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001333{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001334 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001335 struct fuse_conn *fc = get_fuse_conn(inode);
1336 struct fuse_req *req;
1337 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1338 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1339 int err;
1340
1341 /* Unlock on close is handled by the flush method */
1342 if (fl->fl_flags & FL_CLOSE)
1343 return 0;
1344
1345 req = fuse_get_req(fc);
1346 if (IS_ERR(req))
1347 return PTR_ERR(req);
1348
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001349 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Miklos Szeredi71421252006-06-25 05:48:52 -07001350 request_send(fc, req);
1351 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001352 /* locking is restartable */
1353 if (err == -EINTR)
1354 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001355 fuse_put_request(fc, req);
1356 return err;
1357}
1358
1359static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1360{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001361 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001362 struct fuse_conn *fc = get_fuse_conn(inode);
1363 int err;
1364
1365 if (cmd == F_GETLK) {
1366 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001367 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001368 err = 0;
1369 } else
1370 err = fuse_getlk(file, fl);
1371 } else {
1372 if (fc->no_lock)
1373 err = posix_lock_file_wait(file, fl);
1374 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001375 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001376 }
1377 return err;
1378}
1379
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001380static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1381{
1382 struct inode *inode = file->f_path.dentry->d_inode;
1383 struct fuse_conn *fc = get_fuse_conn(inode);
1384 int err;
1385
1386 if (fc->no_lock) {
1387 err = flock_lock_file_wait(file, fl);
1388 } else {
1389 /* emulate flock with POSIX locks */
1390 fl->fl_owner = (fl_owner_t) file;
1391 err = fuse_setlk(file, fl, 1);
1392 }
1393
1394 return err;
1395}
1396
Miklos Szeredib2d22722006-12-06 20:35:51 -08001397static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1398{
1399 struct inode *inode = mapping->host;
1400 struct fuse_conn *fc = get_fuse_conn(inode);
1401 struct fuse_req *req;
1402 struct fuse_bmap_in inarg;
1403 struct fuse_bmap_out outarg;
1404 int err;
1405
1406 if (!inode->i_sb->s_bdev || fc->no_bmap)
1407 return 0;
1408
1409 req = fuse_get_req(fc);
1410 if (IS_ERR(req))
1411 return 0;
1412
1413 memset(&inarg, 0, sizeof(inarg));
1414 inarg.block = block;
1415 inarg.blocksize = inode->i_sb->s_blocksize;
1416 req->in.h.opcode = FUSE_BMAP;
1417 req->in.h.nodeid = get_node_id(inode);
1418 req->in.numargs = 1;
1419 req->in.args[0].size = sizeof(inarg);
1420 req->in.args[0].value = &inarg;
1421 req->out.numargs = 1;
1422 req->out.args[0].size = sizeof(outarg);
1423 req->out.args[0].value = &outarg;
1424 request_send(fc, req);
1425 err = req->out.h.error;
1426 fuse_put_request(fc, req);
1427 if (err == -ENOSYS)
1428 fc->no_bmap = 1;
1429
1430 return err ? 0 : outarg.block;
1431}
1432
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001433static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001434 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001435 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001436 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001437 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07001438 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001439 .mmap = fuse_file_mmap,
1440 .open = fuse_open,
1441 .flush = fuse_flush,
1442 .release = fuse_release,
1443 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001444 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001445 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001446 .splice_read = generic_file_splice_read,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001447};
1448
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001449static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001450 .llseek = generic_file_llseek,
1451 .read = fuse_direct_read,
1452 .write = fuse_direct_write,
1453 .open = fuse_open,
1454 .flush = fuse_flush,
1455 .release = fuse_release,
1456 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07001457 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001458 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02001459 /* no mmap and splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001460};
1461
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001462static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001463 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001464 .writepage = fuse_writepage,
1465 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07001466 .write_begin = fuse_write_begin,
1467 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07001468 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001469 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08001470 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001471};
1472
1473void fuse_init_file_inode(struct inode *inode)
1474{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07001475 inode->i_fop = &fuse_file_operations;
1476 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001477}