blob: 63614ed16336d7dec7bd4902a9c43d582e48842e [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>
14
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080015static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070016
Miklos Szeredifd72faa2005-11-07 00:59:51 -080017static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
18 struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070019{
20 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080022 struct fuse_req *req;
23 int err;
24
Miklos Szeredice1d5a42006-04-10 22:54:58 -070025 req = fuse_get_req(fc);
26 if (IS_ERR(req))
27 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
31 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
32 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080033 req->in.numargs = 1;
34 req->in.args[0].size = sizeof(inarg);
35 req->in.args[0].value = &inarg;
36 req->out.numargs = 1;
37 req->out.args[0].size = sizeof(*outargp);
38 req->out.args[0].value = outargp;
39 request_send(fc, req);
40 err = req->out.h.error;
41 fuse_put_request(fc, req);
42
43 return err;
44}
45
46struct fuse_file *fuse_file_alloc(void)
47{
48 struct fuse_file *ff;
49 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
50 if (ff) {
Miklos Szeredi33649c92006-06-25 05:48:52 -070051 ff->reserved_req = fuse_request_alloc();
52 if (!ff->reserved_req) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080053 kfree(ff);
54 ff = NULL;
55 }
56 }
57 return ff;
58}
59
60void fuse_file_free(struct fuse_file *ff)
61{
Miklos Szeredi33649c92006-06-25 05:48:52 -070062 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080063 kfree(ff);
64}
65
66void fuse_finish_open(struct inode *inode, struct file *file,
67 struct fuse_file *ff, struct fuse_open_out *outarg)
68{
69 if (outarg->open_flags & FOPEN_DIRECT_IO)
70 file->f_op = &fuse_direct_io_file_operations;
71 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
72 invalidate_inode_pages(inode->i_mapping);
73 ff->fh = outarg->fh;
74 file->private_data = ff;
75}
76
77int fuse_open_common(struct inode *inode, struct file *file, int isdir)
78{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070079 struct fuse_open_out outarg;
80 struct fuse_file *ff;
81 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070082
Miklos Szeredidd190d02005-09-30 11:59:02 -070083 /* VFS checks this, but only _after_ ->open() */
84 if (file->f_flags & O_DIRECT)
85 return -EINVAL;
86
Miklos Szeredib6aeade2005-09-09 13:10:30 -070087 err = generic_file_open(inode, file);
88 if (err)
89 return err;
90
91 /* If opening the root node, no lookup has been performed on
92 it, so the attributes must be refreshed */
93 if (get_node_id(inode) == FUSE_ROOT_ID) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080094 err = fuse_do_getattr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070095 if (err)
96 return err;
97 }
98
Miklos Szeredifd72faa2005-11-07 00:59:51 -080099 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700100 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800101 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700102
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800103 err = fuse_send_open(inode, file, isdir, &outarg);
104 if (err)
105 fuse_file_free(ff);
106 else {
107 if (isdir)
108 outarg.open_flags &= ~FOPEN_DIRECT_IO;
109 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700110 }
111
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700112 return err;
113}
114
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700115struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
116 int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800117{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700118 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700119 struct fuse_release_in *inarg = &req->misc.release_in;
120
121 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800122 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700123 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800124 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700125 req->in.numargs = 1;
126 req->in.args[0].size = sizeof(struct fuse_release_in);
127 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700128 kfree(ff);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700129
130 return req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800131}
132
133int fuse_release_common(struct inode *inode, struct file *file, int isdir)
134{
135 struct fuse_file *ff = file->private_data;
136 if (ff) {
137 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700138 struct fuse_req *req;
139
140 req = fuse_release_fill(ff, get_node_id(inode), file->f_flags,
141 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
142
143 /* Hold vfsmount and dentry until release is finished */
144 req->vfsmount = mntget(file->f_vfsmnt);
145 req->dentry = dget(file->f_dentry);
146 request_send_background(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800147 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700148
149 /* Return value is ignored by VFS */
150 return 0;
151}
152
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700153static int fuse_open(struct inode *inode, struct file *file)
154{
155 return fuse_open_common(inode, file, 0);
156}
157
158static int fuse_release(struct inode *inode, struct file *file)
159{
160 return fuse_release_common(inode, file, 0);
161}
162
Miklos Szeredi71421252006-06-25 05:48:52 -0700163/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700164 * Scramble the ID space with XTEA, so that the value of the files_struct
165 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700166 */
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700167static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700168{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700169 u32 *k = fc->scramble_key;
170 u64 v = (unsigned long) id;
171 u32 v0 = v;
172 u32 v1 = v >> 32;
173 u32 sum = 0;
174 int i;
175
176 for (i = 0; i < 32; i++) {
177 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
178 sum += 0x9E3779B9;
179 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
180 }
181
182 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700183}
184
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700185static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700186{
187 struct inode *inode = file->f_dentry->d_inode;
188 struct fuse_conn *fc = get_fuse_conn(inode);
189 struct fuse_file *ff = file->private_data;
190 struct fuse_req *req;
191 struct fuse_flush_in inarg;
192 int err;
193
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800194 if (is_bad_inode(inode))
195 return -EIO;
196
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197 if (fc->no_flush)
198 return 0;
199
Miklos Szeredi33649c92006-06-25 05:48:52 -0700200 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 memset(&inarg, 0, sizeof(inarg));
202 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700203 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204 req->in.h.opcode = FUSE_FLUSH;
205 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700206 req->in.numargs = 1;
207 req->in.args[0].size = sizeof(inarg);
208 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700209 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700210 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700211 err = req->out.h.error;
212 fuse_put_request(fc, req);
213 if (err == -ENOSYS) {
214 fc->no_flush = 1;
215 err = 0;
216 }
217 return err;
218}
219
Miklos Szeredi82547982005-09-09 13:10:38 -0700220int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
221 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700222{
223 struct inode *inode = de->d_inode;
224 struct fuse_conn *fc = get_fuse_conn(inode);
225 struct fuse_file *ff = file->private_data;
226 struct fuse_req *req;
227 struct fuse_fsync_in inarg;
228 int err;
229
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800230 if (is_bad_inode(inode))
231 return -EIO;
232
Miklos Szeredi82547982005-09-09 13:10:38 -0700233 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234 return 0;
235
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700236 req = fuse_get_req(fc);
237 if (IS_ERR(req))
238 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700239
240 memset(&inarg, 0, sizeof(inarg));
241 inarg.fh = ff->fh;
242 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700243 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700244 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700245 req->in.numargs = 1;
246 req->in.args[0].size = sizeof(inarg);
247 req->in.args[0].value = &inarg;
248 request_send(fc, req);
249 err = req->out.h.error;
250 fuse_put_request(fc, req);
251 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700252 if (isdir)
253 fc->no_fsyncdir = 1;
254 else
255 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700256 err = 0;
257 }
258 return err;
259}
260
Miklos Szeredi82547982005-09-09 13:10:38 -0700261static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
262{
263 return fuse_fsync_common(file, de, datasync, 0);
264}
265
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800266void fuse_read_fill(struct fuse_req *req, struct file *file,
267 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700268{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700269 struct fuse_file *ff = file->private_data;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800270 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700271
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800272 inarg->fh = ff->fh;
273 inarg->offset = pos;
274 inarg->size = count;
275 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700276 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(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800279 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700280 req->out.argpages = 1;
281 req->out.argvar = 1;
282 req->out.numargs = 1;
283 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700284}
285
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800286static size_t fuse_send_read(struct fuse_req *req, struct file *file,
287 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700288{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800289 struct fuse_conn *fc = get_fuse_conn(inode);
290 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
291 request_send(fc, req);
292 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700293}
294
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700295static int fuse_readpage(struct file *file, struct page *page)
296{
297 struct inode *inode = page->mapping->host;
298 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800299 struct fuse_req *req;
300 int err;
301
302 err = -EIO;
303 if (is_bad_inode(inode))
304 goto out;
305
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700306 req = fuse_get_req(fc);
307 err = PTR_ERR(req);
308 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700309 goto out;
310
311 req->out.page_zeroing = 1;
312 req->num_pages = 1;
313 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800314 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700315 err = req->out.h.error;
316 fuse_put_request(fc, req);
317 if (!err)
318 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700319 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700320 out:
321 unlock_page(page);
322 return err;
323}
324
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800325static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700326{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800327 int i;
328
329 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
330
Miklos Szeredidb50b962005-09-09 13:10:33 -0700331 for (i = 0; i < req->num_pages; i++) {
332 struct page *page = req->pages[i];
333 if (!req->out.h.error)
334 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800335 else
336 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700337 unlock_page(page);
338 }
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800339 fuse_put_request(fc, req);
340}
341
342static void fuse_send_readpages(struct fuse_req *req, struct file *file,
343 struct inode *inode)
344{
345 struct fuse_conn *fc = get_fuse_conn(inode);
346 loff_t pos = page_offset(req->pages[0]);
347 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
348 req->out.page_zeroing = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800349 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800350 if (fc->async_read) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700351 get_file(file);
352 req->file = file;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800353 req->end = fuse_readpages_end;
354 request_send_background(fc, req);
355 } else {
356 request_send(fc, req);
357 fuse_readpages_end(fc, req);
358 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700359}
360
361struct fuse_readpages_data {
362 struct fuse_req *req;
363 struct file *file;
364 struct inode *inode;
365};
366
367static int fuse_readpages_fill(void *_data, struct page *page)
368{
369 struct fuse_readpages_data *data = _data;
370 struct fuse_req *req = data->req;
371 struct inode *inode = data->inode;
372 struct fuse_conn *fc = get_fuse_conn(inode);
373
374 if (req->num_pages &&
375 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
376 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
377 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800378 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700379 data->req = req = fuse_get_req(fc);
380 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700381 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700382 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700383 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700384 }
385 req->pages[req->num_pages] = page;
386 req->num_pages ++;
387 return 0;
388}
389
390static int fuse_readpages(struct file *file, struct address_space *mapping,
391 struct list_head *pages, unsigned nr_pages)
392{
393 struct inode *inode = mapping->host;
394 struct fuse_conn *fc = get_fuse_conn(inode);
395 struct fuse_readpages_data data;
396 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800397
398 if (is_bad_inode(inode))
399 return -EIO;
400
Miklos Szeredidb50b962005-09-09 13:10:33 -0700401 data.file = file;
402 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700403 data.req = fuse_get_req(fc);
404 if (IS_ERR(data.req))
405 return PTR_ERR(data.req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700406
407 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700408 if (!err) {
409 if (data.req->num_pages)
410 fuse_send_readpages(data.req, file, inode);
411 else
412 fuse_put_request(fc, data.req);
413 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700414 return err;
415}
416
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700417static size_t fuse_send_write(struct fuse_req *req, struct file *file,
418 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700419{
420 struct fuse_conn *fc = get_fuse_conn(inode);
421 struct fuse_file *ff = file->private_data;
422 struct fuse_write_in inarg;
423 struct fuse_write_out outarg;
424
425 memset(&inarg, 0, sizeof(struct fuse_write_in));
426 inarg.fh = ff->fh;
427 inarg.offset = pos;
428 inarg.size = count;
429 req->in.h.opcode = FUSE_WRITE;
430 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 req->in.argpages = 1;
432 req->in.numargs = 2;
433 req->in.args[0].size = sizeof(struct fuse_write_in);
434 req->in.args[0].value = &inarg;
435 req->in.args[1].size = count;
436 req->out.numargs = 1;
437 req->out.args[0].size = sizeof(struct fuse_write_out);
438 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700439 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700440 return outarg.size;
441}
442
443static int fuse_prepare_write(struct file *file, struct page *page,
444 unsigned offset, unsigned to)
445{
446 /* No op */
447 return 0;
448}
449
450static int fuse_commit_write(struct file *file, struct page *page,
451 unsigned offset, unsigned to)
452{
453 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700454 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700455 unsigned count = to - offset;
456 struct inode *inode = page->mapping->host;
457 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800458 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800459 struct fuse_req *req;
460
461 if (is_bad_inode(inode))
462 return -EIO;
463
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700464 req = fuse_get_req(fc);
465 if (IS_ERR(req))
466 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467
468 req->num_pages = 1;
469 req->pages[0] = page;
470 req->page_offset = offset;
471 nres = fuse_send_write(req, file, inode, pos, count);
472 err = req->out.h.error;
473 fuse_put_request(fc, req);
474 if (!err && nres != count)
475 err = -EIO;
476 if (!err) {
477 pos += count;
478 if (pos > i_size_read(inode))
479 i_size_write(inode, pos);
480
481 if (offset == 0 && to == PAGE_CACHE_SIZE) {
482 clear_page_dirty(page);
483 SetPageUptodate(page);
484 }
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700485 }
486 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700487 return err;
488}
489
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700490static void fuse_release_user_pages(struct fuse_req *req, int write)
491{
492 unsigned i;
493
494 for (i = 0; i < req->num_pages; i++) {
495 struct page *page = req->pages[i];
496 if (write)
497 set_page_dirty_lock(page);
498 put_page(page);
499 }
500}
501
502static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
503 unsigned nbytes, int write)
504{
505 unsigned long user_addr = (unsigned long) buf;
506 unsigned offset = user_addr & ~PAGE_MASK;
507 int npages;
508
509 /* This doesn't work with nfsd */
510 if (!current->mm)
511 return -EPERM;
512
513 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
514 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800515 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700516 down_read(&current->mm->mmap_sem);
517 npages = get_user_pages(current, current->mm, user_addr, npages, write,
518 0, req->pages, NULL);
519 up_read(&current->mm->mmap_sem);
520 if (npages < 0)
521 return npages;
522
523 req->num_pages = npages;
524 req->page_offset = offset;
525 return 0;
526}
527
528static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
529 size_t count, loff_t *ppos, int write)
530{
531 struct inode *inode = file->f_dentry->d_inode;
532 struct fuse_conn *fc = get_fuse_conn(inode);
533 size_t nmax = write ? fc->max_write : fc->max_read;
534 loff_t pos = *ppos;
535 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800536 struct fuse_req *req;
537
538 if (is_bad_inode(inode))
539 return -EIO;
540
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700541 req = fuse_get_req(fc);
542 if (IS_ERR(req))
543 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700544
545 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700546 size_t nres;
547 size_t nbytes = min(count, nmax);
548 int err = fuse_get_user_pages(req, buf, nbytes, !write);
549 if (err) {
550 res = err;
551 break;
552 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800553 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
554 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700555 if (write)
556 nres = fuse_send_write(req, file, inode, pos, nbytes);
557 else
558 nres = fuse_send_read(req, file, inode, pos, nbytes);
559 fuse_release_user_pages(req, !write);
560 if (req->out.h.error) {
561 if (!res)
562 res = req->out.h.error;
563 break;
564 } else if (nres > nbytes) {
565 res = -EIO;
566 break;
567 }
568 count -= nres;
569 res += nres;
570 pos += nres;
571 buf += nres;
572 if (nres != nbytes)
573 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200574 if (count) {
575 fuse_put_request(fc, req);
576 req = fuse_get_req(fc);
577 if (IS_ERR(req))
578 break;
579 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700580 }
581 fuse_put_request(fc, req);
582 if (res > 0) {
583 if (write && pos > i_size_read(inode))
584 i_size_write(inode, pos);
585 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700586 }
587 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700588
589 return res;
590}
591
592static ssize_t fuse_direct_read(struct file *file, char __user *buf,
593 size_t count, loff_t *ppos)
594{
595 return fuse_direct_io(file, buf, count, ppos, 0);
596}
597
598static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
599 size_t count, loff_t *ppos)
600{
601 struct inode *inode = file->f_dentry->d_inode;
602 ssize_t res;
603 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800604 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700605 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800606 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700607 return res;
608}
609
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700610static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
611{
612 if ((vma->vm_flags & VM_SHARED)) {
613 if ((vma->vm_flags & VM_WRITE))
614 return -ENODEV;
615 else
616 vma->vm_flags &= ~VM_MAYWRITE;
617 }
618 return generic_file_mmap(file, vma);
619}
620
621static int fuse_set_page_dirty(struct page *page)
622{
623 printk("fuse_set_page_dirty: should not happen\n");
624 dump_stack();
625 return 0;
626}
627
Miklos Szeredi71421252006-06-25 05:48:52 -0700628static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
629 struct file_lock *fl)
630{
631 switch (ffl->type) {
632 case F_UNLCK:
633 break;
634
635 case F_RDLCK:
636 case F_WRLCK:
637 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
638 ffl->end < ffl->start)
639 return -EIO;
640
641 fl->fl_start = ffl->start;
642 fl->fl_end = ffl->end;
643 fl->fl_pid = ffl->pid;
644 break;
645
646 default:
647 return -EIO;
648 }
649 fl->fl_type = ffl->type;
650 return 0;
651}
652
653static void fuse_lk_fill(struct fuse_req *req, struct file *file,
654 const struct file_lock *fl, int opcode, pid_t pid)
655{
656 struct inode *inode = file->f_dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700657 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -0700658 struct fuse_file *ff = file->private_data;
659 struct fuse_lk_in *arg = &req->misc.lk_in;
660
661 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700662 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -0700663 arg->lk.start = fl->fl_start;
664 arg->lk.end = fl->fl_end;
665 arg->lk.type = fl->fl_type;
666 arg->lk.pid = pid;
667 req->in.h.opcode = opcode;
668 req->in.h.nodeid = get_node_id(inode);
669 req->in.numargs = 1;
670 req->in.args[0].size = sizeof(*arg);
671 req->in.args[0].value = arg;
672}
673
674static int fuse_getlk(struct file *file, struct file_lock *fl)
675{
676 struct inode *inode = file->f_dentry->d_inode;
677 struct fuse_conn *fc = get_fuse_conn(inode);
678 struct fuse_req *req;
679 struct fuse_lk_out outarg;
680 int err;
681
682 req = fuse_get_req(fc);
683 if (IS_ERR(req))
684 return PTR_ERR(req);
685
686 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0);
687 req->out.numargs = 1;
688 req->out.args[0].size = sizeof(outarg);
689 req->out.args[0].value = &outarg;
690 request_send(fc, req);
691 err = req->out.h.error;
692 fuse_put_request(fc, req);
693 if (!err)
694 err = convert_fuse_file_lock(&outarg.lk, fl);
695
696 return err;
697}
698
699static int fuse_setlk(struct file *file, struct file_lock *fl)
700{
701 struct inode *inode = file->f_dentry->d_inode;
702 struct fuse_conn *fc = get_fuse_conn(inode);
703 struct fuse_req *req;
704 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
705 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
706 int err;
707
708 /* Unlock on close is handled by the flush method */
709 if (fl->fl_flags & FL_CLOSE)
710 return 0;
711
712 req = fuse_get_req(fc);
713 if (IS_ERR(req))
714 return PTR_ERR(req);
715
716 fuse_lk_fill(req, file, fl, opcode, pid);
717 request_send(fc, req);
718 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700719 /* locking is restartable */
720 if (err == -EINTR)
721 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -0700722 fuse_put_request(fc, req);
723 return err;
724}
725
726static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
727{
728 struct inode *inode = file->f_dentry->d_inode;
729 struct fuse_conn *fc = get_fuse_conn(inode);
730 int err;
731
732 if (cmd == F_GETLK) {
733 if (fc->no_lock) {
734 if (!posix_test_lock(file, fl, fl))
735 fl->fl_type = F_UNLCK;
736 err = 0;
737 } else
738 err = fuse_getlk(file, fl);
739 } else {
740 if (fc->no_lock)
741 err = posix_lock_file_wait(file, fl);
742 else
743 err = fuse_setlk(file, fl);
744 }
745 return err;
746}
747
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800748static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700749 .llseek = generic_file_llseek,
750 .read = generic_file_read,
751 .write = generic_file_write,
752 .mmap = fuse_file_mmap,
753 .open = fuse_open,
754 .flush = fuse_flush,
755 .release = fuse_release,
756 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700757 .lock = fuse_file_lock,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700758 .sendfile = generic_file_sendfile,
759};
760
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800761static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700762 .llseek = generic_file_llseek,
763 .read = fuse_direct_read,
764 .write = fuse_direct_write,
765 .open = fuse_open,
766 .flush = fuse_flush,
767 .release = fuse_release,
768 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700769 .lock = fuse_file_lock,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700770 /* no mmap and sendfile */
771};
772
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700773static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700774 .readpage = fuse_readpage,
775 .prepare_write = fuse_prepare_write,
776 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700777 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700778 .set_page_dirty = fuse_set_page_dirty,
779};
780
781void fuse_init_file_inode(struct inode *inode)
782{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700783 inode->i_fop = &fuse_file_operations;
784 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700785}