blob: 90ce7c5f7b593157bfffc4881bb57c691e687e84 [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));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
32 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
33 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080034 req->in.numargs = 1;
35 req->in.args[0].size = sizeof(inarg);
36 req->in.args[0].value = &inarg;
37 req->out.numargs = 1;
38 req->out.args[0].size = sizeof(*outargp);
39 req->out.args[0].value = outargp;
40 request_send(fc, req);
41 err = req->out.h.error;
42 fuse_put_request(fc, req);
43
44 return err;
45}
46
47struct fuse_file *fuse_file_alloc(void)
48{
49 struct fuse_file *ff;
50 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51 if (ff) {
Miklos Szeredi33649c92006-06-25 05:48:52 -070052 ff->reserved_req = fuse_request_alloc();
53 if (!ff->reserved_req) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054 kfree(ff);
55 ff = NULL;
56 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -070057 atomic_set(&ff->count, 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080058 }
59 return ff;
60}
61
62void fuse_file_free(struct fuse_file *ff)
63{
Miklos Szeredi33649c92006-06-25 05:48:52 -070064 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080065 kfree(ff);
66}
67
Miklos Szeredic756e0a2007-10-16 23:31:00 -070068static struct fuse_file *fuse_file_get(struct fuse_file *ff)
69{
70 atomic_inc(&ff->count);
71 return ff;
72}
73
74static void fuse_file_put(struct fuse_file *ff)
75{
76 if (atomic_dec_and_test(&ff->count)) {
77 struct fuse_req *req = ff->reserved_req;
78 struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode);
79 request_send_background(fc, req);
80 kfree(ff);
81 }
82}
83
Miklos Szeredifd72faa2005-11-07 00:59:51 -080084void fuse_finish_open(struct inode *inode, struct file *file,
85 struct fuse_file *ff, struct fuse_open_out *outarg)
86{
87 if (outarg->open_flags & FOPEN_DIRECT_IO)
88 file->f_op = &fuse_direct_io_file_operations;
89 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -080090 invalidate_mapping_pages(inode->i_mapping, 0, -1);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080091 ff->fh = outarg->fh;
Miklos Szeredic756e0a2007-10-16 23:31:00 -070092 file->private_data = fuse_file_get(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080093}
94
95int fuse_open_common(struct inode *inode, struct file *file, int isdir)
96{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070097 struct fuse_open_out outarg;
98 struct fuse_file *ff;
99 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700100
Miklos Szeredidd190d02005-09-30 11:59:02 -0700101 /* VFS checks this, but only _after_ ->open() */
102 if (file->f_flags & O_DIRECT)
103 return -EINVAL;
104
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700105 err = generic_file_open(inode, file);
106 if (err)
107 return err;
108
109 /* If opening the root node, no lookup has been performed on
110 it, so the attributes must be refreshed */
111 if (get_node_id(inode) == FUSE_ROOT_ID) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800112 err = fuse_do_getattr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700113 if (err)
114 return err;
115 }
116
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800117 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700118 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800119 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700120
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800121 err = fuse_send_open(inode, file, isdir, &outarg);
122 if (err)
123 fuse_file_free(ff);
124 else {
125 if (isdir)
126 outarg.open_flags &= ~FOPEN_DIRECT_IO;
127 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700128 }
129
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700130 return err;
131}
132
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700133void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800134{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700135 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700136 struct fuse_release_in *inarg = &req->misc.release_in;
137
138 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800139 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700140 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800141 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700142 req->in.numargs = 1;
143 req->in.args[0].size = sizeof(struct fuse_release_in);
144 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800145}
146
147int fuse_release_common(struct inode *inode, struct file *file, int isdir)
148{
149 struct fuse_file *ff = file->private_data;
150 if (ff) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700151 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
152 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700153
154 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700155 ff->reserved_req->vfsmount = mntget(file->f_path.mnt);
156 ff->reserved_req->dentry = dget(file->f_path.dentry);
157 /*
158 * Normally this will send the RELEASE request,
159 * however if some asynchronous READ or WRITE requests
160 * are outstanding, the sending will be delayed
161 */
162 fuse_file_put(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800163 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700164
165 /* Return value is ignored by VFS */
166 return 0;
167}
168
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700169static int fuse_open(struct inode *inode, struct file *file)
170{
171 return fuse_open_common(inode, file, 0);
172}
173
174static int fuse_release(struct inode *inode, struct file *file)
175{
176 return fuse_release_common(inode, file, 0);
177}
178
Miklos Szeredi71421252006-06-25 05:48:52 -0700179/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700180 * Scramble the ID space with XTEA, so that the value of the files_struct
181 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700182 */
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700183static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700184{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700185 u32 *k = fc->scramble_key;
186 u64 v = (unsigned long) id;
187 u32 v0 = v;
188 u32 v1 = v >> 32;
189 u32 sum = 0;
190 int i;
191
192 for (i = 0; i < 32; i++) {
193 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
194 sum += 0x9E3779B9;
195 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
196 }
197
198 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700199}
200
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700201static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700202{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800203 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204 struct fuse_conn *fc = get_fuse_conn(inode);
205 struct fuse_file *ff = file->private_data;
206 struct fuse_req *req;
207 struct fuse_flush_in inarg;
208 int err;
209
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800210 if (is_bad_inode(inode))
211 return -EIO;
212
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700213 if (fc->no_flush)
214 return 0;
215
Miklos Szeredi33649c92006-06-25 05:48:52 -0700216 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700217 memset(&inarg, 0, sizeof(inarg));
218 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700219 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700220 req->in.h.opcode = FUSE_FLUSH;
221 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222 req->in.numargs = 1;
223 req->in.args[0].size = sizeof(inarg);
224 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700225 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700226 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700227 err = req->out.h.error;
228 fuse_put_request(fc, req);
229 if (err == -ENOSYS) {
230 fc->no_flush = 1;
231 err = 0;
232 }
233 return err;
234}
235
Miklos Szeredi82547982005-09-09 13:10:38 -0700236int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
237 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700238{
239 struct inode *inode = de->d_inode;
240 struct fuse_conn *fc = get_fuse_conn(inode);
241 struct fuse_file *ff = file->private_data;
242 struct fuse_req *req;
243 struct fuse_fsync_in inarg;
244 int err;
245
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800246 if (is_bad_inode(inode))
247 return -EIO;
248
Miklos Szeredi82547982005-09-09 13:10:38 -0700249 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700250 return 0;
251
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700252 req = fuse_get_req(fc);
253 if (IS_ERR(req))
254 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700255
256 memset(&inarg, 0, sizeof(inarg));
257 inarg.fh = ff->fh;
258 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700259 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700260 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700261 req->in.numargs = 1;
262 req->in.args[0].size = sizeof(inarg);
263 req->in.args[0].value = &inarg;
264 request_send(fc, req);
265 err = req->out.h.error;
266 fuse_put_request(fc, req);
267 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700268 if (isdir)
269 fc->no_fsyncdir = 1;
270 else
271 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700272 err = 0;
273 }
274 return err;
275}
276
Miklos Szeredi82547982005-09-09 13:10:38 -0700277static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
278{
279 return fuse_fsync_common(file, de, datasync, 0);
280}
281
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700282void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800283 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700284{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800285 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700286
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800287 inarg->fh = ff->fh;
288 inarg->offset = pos;
289 inarg->size = count;
290 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700291 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700292 req->in.numargs = 1;
293 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800294 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700295 req->out.argpages = 1;
296 req->out.argvar = 1;
297 req->out.numargs = 1;
298 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700299}
300
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800301static size_t fuse_send_read(struct fuse_req *req, struct file *file,
302 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700303{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800304 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700305 struct fuse_file *ff = file->private_data;
306 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800307 request_send(fc, req);
308 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700309}
310
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700311static int fuse_readpage(struct file *file, struct page *page)
312{
313 struct inode *inode = page->mapping->host;
314 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800315 struct fuse_req *req;
316 int err;
317
318 err = -EIO;
319 if (is_bad_inode(inode))
320 goto out;
321
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700322 req = fuse_get_req(fc);
323 err = PTR_ERR(req);
324 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700325 goto out;
326
327 req->out.page_zeroing = 1;
328 req->num_pages = 1;
329 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800330 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700331 err = req->out.h.error;
332 fuse_put_request(fc, req);
333 if (!err)
334 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700335 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700336 out:
337 unlock_page(page);
338 return err;
339}
340
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800341static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700342{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800343 int i;
344
345 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
346
Miklos Szeredidb50b962005-09-09 13:10:33 -0700347 for (i = 0; i < req->num_pages; i++) {
348 struct page *page = req->pages[i];
349 if (!req->out.h.error)
350 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800351 else
352 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700353 unlock_page(page);
354 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700355 if (req->ff)
356 fuse_file_put(req->ff);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800357 fuse_put_request(fc, req);
358}
359
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700360static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800361 struct inode *inode)
362{
363 struct fuse_conn *fc = get_fuse_conn(inode);
364 loff_t pos = page_offset(req->pages[0]);
365 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
366 req->out.page_zeroing = 1;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700367 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800368 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700369 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800370 req->end = fuse_readpages_end;
371 request_send_background(fc, req);
372 } else {
373 request_send(fc, req);
374 fuse_readpages_end(fc, req);
375 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700376}
377
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700378struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700379 struct fuse_req *req;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700380 struct fuse_file *ff;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700381 struct inode *inode;
382};
383
384static int fuse_readpages_fill(void *_data, struct page *page)
385{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700386 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700387 struct fuse_req *req = data->req;
388 struct inode *inode = data->inode;
389 struct fuse_conn *fc = get_fuse_conn(inode);
390
391 if (req->num_pages &&
392 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
393 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
394 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700395 fuse_send_readpages(req, data->ff, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700396 data->req = req = fuse_get_req(fc);
397 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700398 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700399 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700400 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700401 }
402 req->pages[req->num_pages] = page;
403 req->num_pages ++;
404 return 0;
405}
406
407static int fuse_readpages(struct file *file, struct address_space *mapping,
408 struct list_head *pages, unsigned nr_pages)
409{
410 struct inode *inode = mapping->host;
411 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700412 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700413 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800414
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700415 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800416 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800417 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800418
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700419 data.ff = file->private_data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700420 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700421 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700422 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700423 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800424 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700425
426 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700427 if (!err) {
428 if (data.req->num_pages)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700429 fuse_send_readpages(data.req, data.ff, inode);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700430 else
431 fuse_put_request(fc, data.req);
432 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800433out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700434 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700435}
436
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700437static size_t fuse_send_write(struct fuse_req *req, struct file *file,
438 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700439{
440 struct fuse_conn *fc = get_fuse_conn(inode);
441 struct fuse_file *ff = file->private_data;
442 struct fuse_write_in inarg;
443 struct fuse_write_out outarg;
444
445 memset(&inarg, 0, sizeof(struct fuse_write_in));
446 inarg.fh = ff->fh;
447 inarg.offset = pos;
448 inarg.size = count;
449 req->in.h.opcode = FUSE_WRITE;
450 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 req->in.argpages = 1;
452 req->in.numargs = 2;
453 req->in.args[0].size = sizeof(struct fuse_write_in);
454 req->in.args[0].value = &inarg;
455 req->in.args[1].size = count;
456 req->out.numargs = 1;
457 req->out.args[0].size = sizeof(struct fuse_write_out);
458 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700459 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460 return outarg.size;
461}
462
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700463static int fuse_write_begin(struct file *file, struct address_space *mapping,
464 loff_t pos, unsigned len, unsigned flags,
465 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700466{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700467 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
468
469 *pagep = __grab_cache_page(mapping, index);
470 if (!*pagep)
471 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472 return 0;
473}
474
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700475static int fuse_buffered_write(struct file *file, struct inode *inode,
476 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700477{
478 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700479 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700481 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800482 struct fuse_req *req;
483
484 if (is_bad_inode(inode))
485 return -EIO;
486
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700487 req = fuse_get_req(fc);
488 if (IS_ERR(req))
489 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700490
491 req->num_pages = 1;
492 req->pages[0] = page;
493 req->page_offset = offset;
494 nres = fuse_send_write(req, file, inode, pos, count);
495 err = req->out.h.error;
496 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700497 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498 err = -EIO;
499 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700500 pos += nres;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700501 spin_lock(&fc->lock);
502 if (pos > inode->i_size)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700503 i_size_write(inode, pos);
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700504 spin_unlock(&fc->lock);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700505
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700506 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700507 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700508 }
509 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700510 return err ? err : nres;
511}
512
513static int fuse_write_end(struct file *file, struct address_space *mapping,
514 loff_t pos, unsigned len, unsigned copied,
515 struct page *page, void *fsdata)
516{
517 struct inode *inode = mapping->host;
518 int res = 0;
519
520 if (copied)
521 res = fuse_buffered_write(file, inode, pos, copied, page);
522
523 unlock_page(page);
524 page_cache_release(page);
525 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526}
527
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700528static void fuse_release_user_pages(struct fuse_req *req, int write)
529{
530 unsigned i;
531
532 for (i = 0; i < req->num_pages; i++) {
533 struct page *page = req->pages[i];
534 if (write)
535 set_page_dirty_lock(page);
536 put_page(page);
537 }
538}
539
540static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
541 unsigned nbytes, int write)
542{
543 unsigned long user_addr = (unsigned long) buf;
544 unsigned offset = user_addr & ~PAGE_MASK;
545 int npages;
546
547 /* This doesn't work with nfsd */
548 if (!current->mm)
549 return -EPERM;
550
551 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
552 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800553 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700554 down_read(&current->mm->mmap_sem);
555 npages = get_user_pages(current, current->mm, user_addr, npages, write,
556 0, req->pages, NULL);
557 up_read(&current->mm->mmap_sem);
558 if (npages < 0)
559 return npages;
560
561 req->num_pages = npages;
562 req->page_offset = offset;
563 return 0;
564}
565
566static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
567 size_t count, loff_t *ppos, int write)
568{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800569 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700570 struct fuse_conn *fc = get_fuse_conn(inode);
571 size_t nmax = write ? fc->max_write : fc->max_read;
572 loff_t pos = *ppos;
573 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800574 struct fuse_req *req;
575
576 if (is_bad_inode(inode))
577 return -EIO;
578
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700579 req = fuse_get_req(fc);
580 if (IS_ERR(req))
581 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700582
583 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700584 size_t nres;
585 size_t nbytes = min(count, nmax);
586 int err = fuse_get_user_pages(req, buf, nbytes, !write);
587 if (err) {
588 res = err;
589 break;
590 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800591 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
592 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700593 if (write)
594 nres = fuse_send_write(req, file, inode, pos, nbytes);
595 else
596 nres = fuse_send_read(req, file, inode, pos, nbytes);
597 fuse_release_user_pages(req, !write);
598 if (req->out.h.error) {
599 if (!res)
600 res = req->out.h.error;
601 break;
602 } else if (nres > nbytes) {
603 res = -EIO;
604 break;
605 }
606 count -= nres;
607 res += nres;
608 pos += nres;
609 buf += nres;
610 if (nres != nbytes)
611 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200612 if (count) {
613 fuse_put_request(fc, req);
614 req = fuse_get_req(fc);
615 if (IS_ERR(req))
616 break;
617 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700618 }
619 fuse_put_request(fc, req);
620 if (res > 0) {
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700621 if (write) {
622 spin_lock(&fc->lock);
623 if (pos > inode->i_size)
624 i_size_write(inode, pos);
625 spin_unlock(&fc->lock);
626 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700627 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700628 }
629 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700630
631 return res;
632}
633
634static ssize_t fuse_direct_read(struct file *file, char __user *buf,
635 size_t count, loff_t *ppos)
636{
637 return fuse_direct_io(file, buf, count, ppos, 0);
638}
639
640static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
641 size_t count, loff_t *ppos)
642{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800643 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700644 ssize_t res;
645 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800646 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -0700647 res = generic_write_checks(file, ppos, &count, 0);
648 if (!res)
649 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800650 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700651 return res;
652}
653
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700654static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
655{
656 if ((vma->vm_flags & VM_SHARED)) {
657 if ((vma->vm_flags & VM_WRITE))
658 return -ENODEV;
659 else
660 vma->vm_flags &= ~VM_MAYWRITE;
661 }
662 return generic_file_mmap(file, vma);
663}
664
665static int fuse_set_page_dirty(struct page *page)
666{
667 printk("fuse_set_page_dirty: should not happen\n");
668 dump_stack();
669 return 0;
670}
671
Miklos Szeredi71421252006-06-25 05:48:52 -0700672static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
673 struct file_lock *fl)
674{
675 switch (ffl->type) {
676 case F_UNLCK:
677 break;
678
679 case F_RDLCK:
680 case F_WRLCK:
681 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
682 ffl->end < ffl->start)
683 return -EIO;
684
685 fl->fl_start = ffl->start;
686 fl->fl_end = ffl->end;
687 fl->fl_pid = ffl->pid;
688 break;
689
690 default:
691 return -EIO;
692 }
693 fl->fl_type = ffl->type;
694 return 0;
695}
696
697static void fuse_lk_fill(struct fuse_req *req, struct file *file,
698 const struct file_lock *fl, int opcode, pid_t pid)
699{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800700 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700701 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -0700702 struct fuse_file *ff = file->private_data;
703 struct fuse_lk_in *arg = &req->misc.lk_in;
704
705 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700706 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -0700707 arg->lk.start = fl->fl_start;
708 arg->lk.end = fl->fl_end;
709 arg->lk.type = fl->fl_type;
710 arg->lk.pid = pid;
711 req->in.h.opcode = opcode;
712 req->in.h.nodeid = get_node_id(inode);
713 req->in.numargs = 1;
714 req->in.args[0].size = sizeof(*arg);
715 req->in.args[0].value = arg;
716}
717
718static int fuse_getlk(struct file *file, struct file_lock *fl)
719{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800720 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700721 struct fuse_conn *fc = get_fuse_conn(inode);
722 struct fuse_req *req;
723 struct fuse_lk_out outarg;
724 int err;
725
726 req = fuse_get_req(fc);
727 if (IS_ERR(req))
728 return PTR_ERR(req);
729
730 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0);
731 req->out.numargs = 1;
732 req->out.args[0].size = sizeof(outarg);
733 req->out.args[0].value = &outarg;
734 request_send(fc, req);
735 err = req->out.h.error;
736 fuse_put_request(fc, req);
737 if (!err)
738 err = convert_fuse_file_lock(&outarg.lk, fl);
739
740 return err;
741}
742
743static int fuse_setlk(struct file *file, struct file_lock *fl)
744{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800745 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700746 struct fuse_conn *fc = get_fuse_conn(inode);
747 struct fuse_req *req;
748 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
749 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
750 int err;
751
752 /* Unlock on close is handled by the flush method */
753 if (fl->fl_flags & FL_CLOSE)
754 return 0;
755
756 req = fuse_get_req(fc);
757 if (IS_ERR(req))
758 return PTR_ERR(req);
759
760 fuse_lk_fill(req, file, fl, opcode, pid);
761 request_send(fc, req);
762 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700763 /* locking is restartable */
764 if (err == -EINTR)
765 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -0700766 fuse_put_request(fc, req);
767 return err;
768}
769
770static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
771{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800772 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700773 struct fuse_conn *fc = get_fuse_conn(inode);
774 int err;
775
776 if (cmd == F_GETLK) {
777 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500778 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -0700779 err = 0;
780 } else
781 err = fuse_getlk(file, fl);
782 } else {
783 if (fc->no_lock)
784 err = posix_lock_file_wait(file, fl);
785 else
786 err = fuse_setlk(file, fl);
787 }
788 return err;
789}
790
Miklos Szeredib2d22722006-12-06 20:35:51 -0800791static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
792{
793 struct inode *inode = mapping->host;
794 struct fuse_conn *fc = get_fuse_conn(inode);
795 struct fuse_req *req;
796 struct fuse_bmap_in inarg;
797 struct fuse_bmap_out outarg;
798 int err;
799
800 if (!inode->i_sb->s_bdev || fc->no_bmap)
801 return 0;
802
803 req = fuse_get_req(fc);
804 if (IS_ERR(req))
805 return 0;
806
807 memset(&inarg, 0, sizeof(inarg));
808 inarg.block = block;
809 inarg.blocksize = inode->i_sb->s_blocksize;
810 req->in.h.opcode = FUSE_BMAP;
811 req->in.h.nodeid = get_node_id(inode);
812 req->in.numargs = 1;
813 req->in.args[0].size = sizeof(inarg);
814 req->in.args[0].value = &inarg;
815 req->out.numargs = 1;
816 req->out.args[0].size = sizeof(outarg);
817 req->out.args[0].value = &outarg;
818 request_send(fc, req);
819 err = req->out.h.error;
820 fuse_put_request(fc, req);
821 if (err == -ENOSYS)
822 fc->no_bmap = 1;
823
824 return err ? 0 : outarg.block;
825}
826
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800827static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700828 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700829 .read = do_sync_read,
830 .aio_read = generic_file_aio_read,
831 .write = do_sync_write,
832 .aio_write = generic_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700833 .mmap = fuse_file_mmap,
834 .open = fuse_open,
835 .flush = fuse_flush,
836 .release = fuse_release,
837 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700838 .lock = fuse_file_lock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200839 .splice_read = generic_file_splice_read,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700840};
841
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800842static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700843 .llseek = generic_file_llseek,
844 .read = fuse_direct_read,
845 .write = fuse_direct_write,
846 .open = fuse_open,
847 .flush = fuse_flush,
848 .release = fuse_release,
849 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700850 .lock = fuse_file_lock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200851 /* no mmap and splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700852};
853
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700854static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700855 .readpage = fuse_readpage,
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700856 .write_begin = fuse_write_begin,
857 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700859 .set_page_dirty = fuse_set_page_dirty,
Miklos Szeredib2d22722006-12-06 20:35:51 -0800860 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700861};
862
863void fuse_init_file_inode(struct inode *inode)
864{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700865 inode->i_fop = &fuse_file_operations;
866 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700867}