blob: 471ea70be48daadca5619dc7957d8cc319f1e507 [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;
58 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -070059 atomic_set(&ff->count, 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080060 }
61 return ff;
62}
63
64void fuse_file_free(struct fuse_file *ff)
65{
Miklos Szeredi33649c92006-06-25 05:48:52 -070066 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080067 kfree(ff);
68}
69
Miklos Szeredic756e0a2007-10-16 23:31:00 -070070static struct fuse_file *fuse_file_get(struct fuse_file *ff)
71{
72 atomic_inc(&ff->count);
73 return ff;
74}
75
Miklos Szeredi819c4b32007-10-16 23:31:04 -070076static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
77{
78 dput(req->dentry);
79 mntput(req->vfsmount);
80 fuse_put_request(fc, req);
81}
82
Miklos Szeredic756e0a2007-10-16 23:31:00 -070083static void fuse_file_put(struct fuse_file *ff)
84{
85 if (atomic_dec_and_test(&ff->count)) {
86 struct fuse_req *req = ff->reserved_req;
87 struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode);
Miklos Szeredi819c4b32007-10-16 23:31:04 -070088 req->end = fuse_release_end;
Miklos Szeredic756e0a2007-10-16 23:31:00 -070089 request_send_background(fc, req);
90 kfree(ff);
91 }
92}
93
Miklos Szeredifd72faa2005-11-07 00:59:51 -080094void fuse_finish_open(struct inode *inode, struct file *file,
95 struct fuse_file *ff, struct fuse_open_out *outarg)
96{
97 if (outarg->open_flags & FOPEN_DIRECT_IO)
98 file->f_op = &fuse_direct_io_file_operations;
99 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700100 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800101 ff->fh = outarg->fh;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700102 file->private_data = fuse_file_get(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800103}
104
105int fuse_open_common(struct inode *inode, struct file *file, int isdir)
106{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700107 struct fuse_open_out outarg;
108 struct fuse_file *ff;
109 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700110
Miklos Szeredidd190d02005-09-30 11:59:02 -0700111 /* VFS checks this, but only _after_ ->open() */
112 if (file->f_flags & O_DIRECT)
113 return -EINVAL;
114
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700115 err = generic_file_open(inode, file);
116 if (err)
117 return err;
118
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800119 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700120 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800121 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700122
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800123 err = fuse_send_open(inode, file, isdir, &outarg);
124 if (err)
125 fuse_file_free(ff);
126 else {
127 if (isdir)
128 outarg.open_flags &= ~FOPEN_DIRECT_IO;
129 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700130 }
131
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700132 return err;
133}
134
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700135void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800136{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700137 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700138 struct fuse_release_in *inarg = &req->misc.release_in;
139
140 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800141 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700142 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800143 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700144 req->in.numargs = 1;
145 req->in.args[0].size = sizeof(struct fuse_release_in);
146 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800147}
148
149int fuse_release_common(struct inode *inode, struct file *file, int isdir)
150{
151 struct fuse_file *ff = file->private_data;
152 if (ff) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700153 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
154 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700155
156 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700157 ff->reserved_req->vfsmount = mntget(file->f_path.mnt);
158 ff->reserved_req->dentry = dget(file->f_path.dentry);
159 /*
160 * Normally this will send the RELEASE request,
161 * however if some asynchronous READ or WRITE requests
162 * are outstanding, the sending will be delayed
163 */
164 fuse_file_put(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800165 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700166
167 /* Return value is ignored by VFS */
168 return 0;
169}
170
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700171static int fuse_open(struct inode *inode, struct file *file)
172{
173 return fuse_open_common(inode, file, 0);
174}
175
176static int fuse_release(struct inode *inode, struct file *file)
177{
178 return fuse_release_common(inode, file, 0);
179}
180
Miklos Szeredi71421252006-06-25 05:48:52 -0700181/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700182 * Scramble the ID space with XTEA, so that the value of the files_struct
183 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700184 */
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700185static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700186{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700187 u32 *k = fc->scramble_key;
188 u64 v = (unsigned long) id;
189 u32 v0 = v;
190 u32 v1 = v >> 32;
191 u32 sum = 0;
192 int i;
193
194 for (i = 0; i < 32; i++) {
195 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
196 sum += 0x9E3779B9;
197 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
198 }
199
200 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700201}
202
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700203static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800205 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700206 struct fuse_conn *fc = get_fuse_conn(inode);
207 struct fuse_file *ff = file->private_data;
208 struct fuse_req *req;
209 struct fuse_flush_in inarg;
210 int err;
211
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800212 if (is_bad_inode(inode))
213 return -EIO;
214
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700215 if (fc->no_flush)
216 return 0;
217
Miklos Szeredi33649c92006-06-25 05:48:52 -0700218 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219 memset(&inarg, 0, sizeof(inarg));
220 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700221 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222 req->in.h.opcode = FUSE_FLUSH;
223 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700224 req->in.numargs = 1;
225 req->in.args[0].size = sizeof(inarg);
226 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700227 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700228 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229 err = req->out.h.error;
230 fuse_put_request(fc, req);
231 if (err == -ENOSYS) {
232 fc->no_flush = 1;
233 err = 0;
234 }
235 return err;
236}
237
Miklos Szeredi82547982005-09-09 13:10:38 -0700238int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
239 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700240{
241 struct inode *inode = de->d_inode;
242 struct fuse_conn *fc = get_fuse_conn(inode);
243 struct fuse_file *ff = file->private_data;
244 struct fuse_req *req;
245 struct fuse_fsync_in inarg;
246 int err;
247
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800248 if (is_bad_inode(inode))
249 return -EIO;
250
Miklos Szeredi82547982005-09-09 13:10:38 -0700251 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700252 return 0;
253
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700254 req = fuse_get_req(fc);
255 if (IS_ERR(req))
256 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700257
258 memset(&inarg, 0, sizeof(inarg));
259 inarg.fh = ff->fh;
260 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700261 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700262 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700263 req->in.numargs = 1;
264 req->in.args[0].size = sizeof(inarg);
265 req->in.args[0].value = &inarg;
266 request_send(fc, req);
267 err = req->out.h.error;
268 fuse_put_request(fc, req);
269 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700270 if (isdir)
271 fc->no_fsyncdir = 1;
272 else
273 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700274 err = 0;
275 }
276 return err;
277}
278
Miklos Szeredi82547982005-09-09 13:10:38 -0700279static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
280{
281 return fuse_fsync_common(file, de, datasync, 0);
282}
283
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700284void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800285 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700286{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800287 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700288
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800289 inarg->fh = ff->fh;
290 inarg->offset = pos;
291 inarg->size = count;
292 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700293 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700294 req->in.numargs = 1;
295 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800296 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700297 req->out.argpages = 1;
298 req->out.argvar = 1;
299 req->out.numargs = 1;
300 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700301}
302
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800303static size_t fuse_send_read(struct fuse_req *req, struct file *file,
304 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700305{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800306 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700307 struct fuse_file *ff = file->private_data;
308 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800309 request_send(fc, req);
310 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700311}
312
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700313static int fuse_readpage(struct file *file, struct page *page)
314{
315 struct inode *inode = page->mapping->host;
316 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800317 struct fuse_req *req;
318 int err;
319
320 err = -EIO;
321 if (is_bad_inode(inode))
322 goto out;
323
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700324 req = fuse_get_req(fc);
325 err = PTR_ERR(req);
326 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700327 goto out;
328
329 req->out.page_zeroing = 1;
330 req->num_pages = 1;
331 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800332 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700333 err = req->out.h.error;
334 fuse_put_request(fc, req);
335 if (!err)
336 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700337 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700338 out:
339 unlock_page(page);
340 return err;
341}
342
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800343static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700344{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800345 int i;
346
347 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
348
Miklos Szeredidb50b962005-09-09 13:10:33 -0700349 for (i = 0; i < req->num_pages; i++) {
350 struct page *page = req->pages[i];
351 if (!req->out.h.error)
352 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800353 else
354 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700355 unlock_page(page);
356 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700357 if (req->ff)
358 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800359 fuse_put_request(fc, req);
360}
361
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700362static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800363 struct inode *inode)
364{
365 struct fuse_conn *fc = get_fuse_conn(inode);
366 loff_t pos = page_offset(req->pages[0]);
367 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
368 req->out.page_zeroing = 1;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700369 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800370 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700371 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800372 req->end = fuse_readpages_end;
373 request_send_background(fc, req);
374 } else {
375 request_send(fc, req);
376 fuse_readpages_end(fc, req);
377 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700378}
379
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700380struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700381 struct fuse_req *req;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700382 struct fuse_file *ff;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700383 struct inode *inode;
384};
385
386static int fuse_readpages_fill(void *_data, struct page *page)
387{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700388 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700389 struct fuse_req *req = data->req;
390 struct inode *inode = data->inode;
391 struct fuse_conn *fc = get_fuse_conn(inode);
392
393 if (req->num_pages &&
394 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
395 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
396 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700397 fuse_send_readpages(req, data->ff, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700398 data->req = req = fuse_get_req(fc);
399 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700400 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700401 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700402 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700403 }
404 req->pages[req->num_pages] = page;
405 req->num_pages ++;
406 return 0;
407}
408
409static int fuse_readpages(struct file *file, struct address_space *mapping,
410 struct list_head *pages, unsigned nr_pages)
411{
412 struct inode *inode = mapping->host;
413 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700414 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700415 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800416
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700417 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800418 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800419 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800420
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700421 data.ff = file->private_data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700422 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700423 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700424 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700425 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800426 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700427
428 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700429 if (!err) {
430 if (data.req->num_pages)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700431 fuse_send_readpages(data.req, data.ff, inode);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700432 else
433 fuse_put_request(fc, data.req);
434 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800435out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700436 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700437}
438
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700439static size_t fuse_send_write(struct fuse_req *req, struct file *file,
440 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700441{
442 struct fuse_conn *fc = get_fuse_conn(inode);
443 struct fuse_file *ff = file->private_data;
444 struct fuse_write_in inarg;
445 struct fuse_write_out outarg;
446
447 memset(&inarg, 0, sizeof(struct fuse_write_in));
448 inarg.fh = ff->fh;
449 inarg.offset = pos;
450 inarg.size = count;
451 req->in.h.opcode = FUSE_WRITE;
452 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700453 req->in.argpages = 1;
454 req->in.numargs = 2;
455 req->in.args[0].size = sizeof(struct fuse_write_in);
456 req->in.args[0].value = &inarg;
457 req->in.args[1].size = count;
458 req->out.numargs = 1;
459 req->out.args[0].size = sizeof(struct fuse_write_out);
460 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700461 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 return outarg.size;
463}
464
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700465static int fuse_write_begin(struct file *file, struct address_space *mapping,
466 loff_t pos, unsigned len, unsigned flags,
467 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700469 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
470
471 *pagep = __grab_cache_page(mapping, index);
472 if (!*pagep)
473 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700474 return 0;
475}
476
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700477static int fuse_buffered_write(struct file *file, struct inode *inode,
478 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700479{
480 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700481 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700482 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700483 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700484 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800485 struct fuse_req *req;
486
487 if (is_bad_inode(inode))
488 return -EIO;
489
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700490 req = fuse_get_req(fc);
491 if (IS_ERR(req))
492 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493
494 req->num_pages = 1;
495 req->pages[0] = page;
496 req->page_offset = offset;
497 nres = fuse_send_write(req, file, inode, pos, count);
498 err = req->out.h.error;
499 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700500 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700501 err = -EIO;
502 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700503 pos += nres;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700504 spin_lock(&fc->lock);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700505 fi->attr_version = ++fc->attr_version;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700506 if (pos > inode->i_size)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700507 i_size_write(inode, pos);
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700508 spin_unlock(&fc->lock);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700509
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700510 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700512 }
513 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700514 return err ? err : nres;
515}
516
517static int fuse_write_end(struct file *file, struct address_space *mapping,
518 loff_t pos, unsigned len, unsigned copied,
519 struct page *page, void *fsdata)
520{
521 struct inode *inode = mapping->host;
522 int res = 0;
523
524 if (copied)
525 res = fuse_buffered_write(file, inode, pos, copied, page);
526
527 unlock_page(page);
528 page_cache_release(page);
529 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700530}
531
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700532static void fuse_release_user_pages(struct fuse_req *req, int write)
533{
534 unsigned i;
535
536 for (i = 0; i < req->num_pages; i++) {
537 struct page *page = req->pages[i];
538 if (write)
539 set_page_dirty_lock(page);
540 put_page(page);
541 }
542}
543
544static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
545 unsigned nbytes, int write)
546{
547 unsigned long user_addr = (unsigned long) buf;
548 unsigned offset = user_addr & ~PAGE_MASK;
549 int npages;
550
551 /* This doesn't work with nfsd */
552 if (!current->mm)
553 return -EPERM;
554
555 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
556 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800557 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700558 down_read(&current->mm->mmap_sem);
559 npages = get_user_pages(current, current->mm, user_addr, npages, write,
560 0, req->pages, NULL);
561 up_read(&current->mm->mmap_sem);
562 if (npages < 0)
563 return npages;
564
565 req->num_pages = npages;
566 req->page_offset = offset;
567 return 0;
568}
569
570static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
571 size_t count, loff_t *ppos, int write)
572{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800573 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700574 struct fuse_conn *fc = get_fuse_conn(inode);
575 size_t nmax = write ? fc->max_write : fc->max_read;
576 loff_t pos = *ppos;
577 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800578 struct fuse_req *req;
579
580 if (is_bad_inode(inode))
581 return -EIO;
582
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700583 req = fuse_get_req(fc);
584 if (IS_ERR(req))
585 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700586
587 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700588 size_t nres;
589 size_t nbytes = min(count, nmax);
590 int err = fuse_get_user_pages(req, buf, nbytes, !write);
591 if (err) {
592 res = err;
593 break;
594 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800595 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
596 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700597 if (write)
598 nres = fuse_send_write(req, file, inode, pos, nbytes);
599 else
600 nres = fuse_send_read(req, file, inode, pos, nbytes);
601 fuse_release_user_pages(req, !write);
602 if (req->out.h.error) {
603 if (!res)
604 res = req->out.h.error;
605 break;
606 } else if (nres > nbytes) {
607 res = -EIO;
608 break;
609 }
610 count -= nres;
611 res += nres;
612 pos += nres;
613 buf += nres;
614 if (nres != nbytes)
615 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200616 if (count) {
617 fuse_put_request(fc, req);
618 req = fuse_get_req(fc);
619 if (IS_ERR(req))
620 break;
621 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700622 }
623 fuse_put_request(fc, req);
624 if (res > 0) {
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700625 if (write) {
626 spin_lock(&fc->lock);
627 if (pos > inode->i_size)
628 i_size_write(inode, pos);
629 spin_unlock(&fc->lock);
630 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700631 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700632 }
633 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700634
635 return res;
636}
637
638static ssize_t fuse_direct_read(struct file *file, char __user *buf,
639 size_t count, loff_t *ppos)
640{
641 return fuse_direct_io(file, buf, count, ppos, 0);
642}
643
644static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
645 size_t count, loff_t *ppos)
646{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800647 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700648 ssize_t res;
649 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800650 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -0700651 res = generic_write_checks(file, ppos, &count, 0);
652 if (!res)
653 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800654 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700655 return res;
656}
657
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700658static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
659{
660 if ((vma->vm_flags & VM_SHARED)) {
661 if ((vma->vm_flags & VM_WRITE))
662 return -ENODEV;
663 else
664 vma->vm_flags &= ~VM_MAYWRITE;
665 }
666 return generic_file_mmap(file, vma);
667}
668
669static int fuse_set_page_dirty(struct page *page)
670{
671 printk("fuse_set_page_dirty: should not happen\n");
672 dump_stack();
673 return 0;
674}
675
Miklos Szeredi71421252006-06-25 05:48:52 -0700676static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
677 struct file_lock *fl)
678{
679 switch (ffl->type) {
680 case F_UNLCK:
681 break;
682
683 case F_RDLCK:
684 case F_WRLCK:
685 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
686 ffl->end < ffl->start)
687 return -EIO;
688
689 fl->fl_start = ffl->start;
690 fl->fl_end = ffl->end;
691 fl->fl_pid = ffl->pid;
692 break;
693
694 default:
695 return -EIO;
696 }
697 fl->fl_type = ffl->type;
698 return 0;
699}
700
701static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700702 const struct file_lock *fl, int opcode, pid_t pid,
703 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -0700704{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800705 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700706 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -0700707 struct fuse_file *ff = file->private_data;
708 struct fuse_lk_in *arg = &req->misc.lk_in;
709
710 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700711 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -0700712 arg->lk.start = fl->fl_start;
713 arg->lk.end = fl->fl_end;
714 arg->lk.type = fl->fl_type;
715 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700716 if (flock)
717 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -0700718 req->in.h.opcode = opcode;
719 req->in.h.nodeid = get_node_id(inode);
720 req->in.numargs = 1;
721 req->in.args[0].size = sizeof(*arg);
722 req->in.args[0].value = arg;
723}
724
725static int fuse_getlk(struct file *file, struct file_lock *fl)
726{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800727 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700728 struct fuse_conn *fc = get_fuse_conn(inode);
729 struct fuse_req *req;
730 struct fuse_lk_out outarg;
731 int err;
732
733 req = fuse_get_req(fc);
734 if (IS_ERR(req))
735 return PTR_ERR(req);
736
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700737 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -0700738 req->out.numargs = 1;
739 req->out.args[0].size = sizeof(outarg);
740 req->out.args[0].value = &outarg;
741 request_send(fc, req);
742 err = req->out.h.error;
743 fuse_put_request(fc, req);
744 if (!err)
745 err = convert_fuse_file_lock(&outarg.lk, fl);
746
747 return err;
748}
749
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700750static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -0700751{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800752 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700753 struct fuse_conn *fc = get_fuse_conn(inode);
754 struct fuse_req *req;
755 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
756 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
757 int err;
758
759 /* Unlock on close is handled by the flush method */
760 if (fl->fl_flags & FL_CLOSE)
761 return 0;
762
763 req = fuse_get_req(fc);
764 if (IS_ERR(req))
765 return PTR_ERR(req);
766
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700767 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Miklos Szeredi71421252006-06-25 05:48:52 -0700768 request_send(fc, req);
769 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700770 /* locking is restartable */
771 if (err == -EINTR)
772 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -0700773 fuse_put_request(fc, req);
774 return err;
775}
776
777static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
778{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800779 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700780 struct fuse_conn *fc = get_fuse_conn(inode);
781 int err;
782
783 if (cmd == F_GETLK) {
784 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500785 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -0700786 err = 0;
787 } else
788 err = fuse_getlk(file, fl);
789 } else {
790 if (fc->no_lock)
791 err = posix_lock_file_wait(file, fl);
792 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700793 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -0700794 }
795 return err;
796}
797
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700798static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
799{
800 struct inode *inode = file->f_path.dentry->d_inode;
801 struct fuse_conn *fc = get_fuse_conn(inode);
802 int err;
803
804 if (fc->no_lock) {
805 err = flock_lock_file_wait(file, fl);
806 } else {
807 /* emulate flock with POSIX locks */
808 fl->fl_owner = (fl_owner_t) file;
809 err = fuse_setlk(file, fl, 1);
810 }
811
812 return err;
813}
814
Miklos Szeredib2d22722006-12-06 20:35:51 -0800815static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
816{
817 struct inode *inode = mapping->host;
818 struct fuse_conn *fc = get_fuse_conn(inode);
819 struct fuse_req *req;
820 struct fuse_bmap_in inarg;
821 struct fuse_bmap_out outarg;
822 int err;
823
824 if (!inode->i_sb->s_bdev || fc->no_bmap)
825 return 0;
826
827 req = fuse_get_req(fc);
828 if (IS_ERR(req))
829 return 0;
830
831 memset(&inarg, 0, sizeof(inarg));
832 inarg.block = block;
833 inarg.blocksize = inode->i_sb->s_blocksize;
834 req->in.h.opcode = FUSE_BMAP;
835 req->in.h.nodeid = get_node_id(inode);
836 req->in.numargs = 1;
837 req->in.args[0].size = sizeof(inarg);
838 req->in.args[0].value = &inarg;
839 req->out.numargs = 1;
840 req->out.args[0].size = sizeof(outarg);
841 req->out.args[0].value = &outarg;
842 request_send(fc, req);
843 err = req->out.h.error;
844 fuse_put_request(fc, req);
845 if (err == -ENOSYS)
846 fc->no_bmap = 1;
847
848 return err ? 0 : outarg.block;
849}
850
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800851static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700852 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700853 .read = do_sync_read,
854 .aio_read = generic_file_aio_read,
855 .write = do_sync_write,
856 .aio_write = generic_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700857 .mmap = fuse_file_mmap,
858 .open = fuse_open,
859 .flush = fuse_flush,
860 .release = fuse_release,
861 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700862 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700863 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200864 .splice_read = generic_file_splice_read,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700865};
866
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800867static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700868 .llseek = generic_file_llseek,
869 .read = fuse_direct_read,
870 .write = fuse_direct_write,
871 .open = fuse_open,
872 .flush = fuse_flush,
873 .release = fuse_release,
874 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700875 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -0700876 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200877 /* no mmap and splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700878};
879
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700880static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700881 .readpage = fuse_readpage,
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700882 .write_begin = fuse_write_begin,
883 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700884 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700885 .set_page_dirty = fuse_set_page_dirty,
Miklos Szeredib2d22722006-12-06 20:35:51 -0800886 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700887};
888
889void fuse_init_file_inode(struct inode *inode)
890{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700891 inode->i_fop = &fuse_file_operations;
892 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700893}