blob: d9a8289297c0c17da3d3d8e35b62b72c5d42cde5 [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) {
51 ff->release_req = fuse_request_alloc();
52 if (!ff->release_req) {
53 kfree(ff);
54 ff = NULL;
55 }
56 }
57 return ff;
58}
59
60void fuse_file_free(struct fuse_file *ff)
61{
62 fuse_request_free(ff->release_req);
63 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 Szeredi51eb01e2006-06-25 05:48:50 -0700118 struct fuse_req *req = ff->release_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/*
164 * It would be nice to scramble the ID space, so that the value of the
165 * files_struct pointer is not exposed to userspace. Symmetric crypto
166 * functions are overkill, since the inverse function doesn't need to
167 * be implemented (though it does have to exist). Is there something
168 * simpler?
169 */
170static inline u64 fuse_lock_owner_id(fl_owner_t id)
171{
172 return (unsigned long) id;
173}
174
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700175static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700176{
177 struct inode *inode = file->f_dentry->d_inode;
178 struct fuse_conn *fc = get_fuse_conn(inode);
179 struct fuse_file *ff = file->private_data;
180 struct fuse_req *req;
181 struct fuse_flush_in inarg;
182 int err;
183
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800184 if (is_bad_inode(inode))
185 return -EIO;
186
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700187 if (fc->no_flush)
188 return 0;
189
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700190 req = fuse_get_req(fc);
191 if (IS_ERR(req))
192 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700193
194 memset(&inarg, 0, sizeof(inarg));
195 inarg.fh = ff->fh;
Miklos Szeredi71421252006-06-25 05:48:52 -0700196 inarg.lock_owner = fuse_lock_owner_id(id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197 req->in.h.opcode = FUSE_FLUSH;
198 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700199 req->in.numargs = 1;
200 req->in.args[0].size = sizeof(inarg);
201 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700202 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700203 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700204 err = req->out.h.error;
205 fuse_put_request(fc, req);
206 if (err == -ENOSYS) {
207 fc->no_flush = 1;
208 err = 0;
209 }
210 return err;
211}
212
Miklos Szeredi82547982005-09-09 13:10:38 -0700213int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
214 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700215{
216 struct inode *inode = de->d_inode;
217 struct fuse_conn *fc = get_fuse_conn(inode);
218 struct fuse_file *ff = file->private_data;
219 struct fuse_req *req;
220 struct fuse_fsync_in inarg;
221 int err;
222
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800223 if (is_bad_inode(inode))
224 return -EIO;
225
Miklos Szeredi82547982005-09-09 13:10:38 -0700226 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700227 return 0;
228
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700229 req = fuse_get_req(fc);
230 if (IS_ERR(req))
231 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700232
233 memset(&inarg, 0, sizeof(inarg));
234 inarg.fh = ff->fh;
235 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700236 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700237 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700238 req->in.numargs = 1;
239 req->in.args[0].size = sizeof(inarg);
240 req->in.args[0].value = &inarg;
241 request_send(fc, req);
242 err = req->out.h.error;
243 fuse_put_request(fc, req);
244 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700245 if (isdir)
246 fc->no_fsyncdir = 1;
247 else
248 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700249 err = 0;
250 }
251 return err;
252}
253
Miklos Szeredi82547982005-09-09 13:10:38 -0700254static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
255{
256 return fuse_fsync_common(file, de, datasync, 0);
257}
258
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800259void fuse_read_fill(struct fuse_req *req, struct file *file,
260 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700261{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700262 struct fuse_file *ff = file->private_data;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800263 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700264
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800265 inarg->fh = ff->fh;
266 inarg->offset = pos;
267 inarg->size = count;
268 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700269 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700270 req->in.numargs = 1;
271 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800272 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700273 req->out.argpages = 1;
274 req->out.argvar = 1;
275 req->out.numargs = 1;
276 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277}
278
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800279static size_t fuse_send_read(struct fuse_req *req, struct file *file,
280 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700281{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800282 struct fuse_conn *fc = get_fuse_conn(inode);
283 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
284 request_send(fc, req);
285 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700286}
287
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700288static int fuse_readpage(struct file *file, struct page *page)
289{
290 struct inode *inode = page->mapping->host;
291 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800292 struct fuse_req *req;
293 int err;
294
295 err = -EIO;
296 if (is_bad_inode(inode))
297 goto out;
298
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700299 req = fuse_get_req(fc);
300 err = PTR_ERR(req);
301 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700302 goto out;
303
304 req->out.page_zeroing = 1;
305 req->num_pages = 1;
306 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800307 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700308 err = req->out.h.error;
309 fuse_put_request(fc, req);
310 if (!err)
311 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700312 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700313 out:
314 unlock_page(page);
315 return err;
316}
317
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800318static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700319{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800320 int i;
321
322 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
323
Miklos Szeredidb50b962005-09-09 13:10:33 -0700324 for (i = 0; i < req->num_pages; i++) {
325 struct page *page = req->pages[i];
326 if (!req->out.h.error)
327 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800328 else
329 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700330 unlock_page(page);
331 }
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800332 fuse_put_request(fc, req);
333}
334
335static void fuse_send_readpages(struct fuse_req *req, struct file *file,
336 struct inode *inode)
337{
338 struct fuse_conn *fc = get_fuse_conn(inode);
339 loff_t pos = page_offset(req->pages[0]);
340 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
341 req->out.page_zeroing = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800342 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800343 if (fc->async_read) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700344 get_file(file);
345 req->file = file;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800346 req->end = fuse_readpages_end;
347 request_send_background(fc, req);
348 } else {
349 request_send(fc, req);
350 fuse_readpages_end(fc, req);
351 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700352}
353
354struct fuse_readpages_data {
355 struct fuse_req *req;
356 struct file *file;
357 struct inode *inode;
358};
359
360static int fuse_readpages_fill(void *_data, struct page *page)
361{
362 struct fuse_readpages_data *data = _data;
363 struct fuse_req *req = data->req;
364 struct inode *inode = data->inode;
365 struct fuse_conn *fc = get_fuse_conn(inode);
366
367 if (req->num_pages &&
368 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
369 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
370 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800371 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700372 data->req = req = fuse_get_req(fc);
373 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700374 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700375 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700376 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700377 }
378 req->pages[req->num_pages] = page;
379 req->num_pages ++;
380 return 0;
381}
382
383static int fuse_readpages(struct file *file, struct address_space *mapping,
384 struct list_head *pages, unsigned nr_pages)
385{
386 struct inode *inode = mapping->host;
387 struct fuse_conn *fc = get_fuse_conn(inode);
388 struct fuse_readpages_data data;
389 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800390
391 if (is_bad_inode(inode))
392 return -EIO;
393
Miklos Szeredidb50b962005-09-09 13:10:33 -0700394 data.file = file;
395 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700396 data.req = fuse_get_req(fc);
397 if (IS_ERR(data.req))
398 return PTR_ERR(data.req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700399
400 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700401 if (!err) {
402 if (data.req->num_pages)
403 fuse_send_readpages(data.req, file, inode);
404 else
405 fuse_put_request(fc, data.req);
406 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700407 return err;
408}
409
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700410static size_t fuse_send_write(struct fuse_req *req, struct file *file,
411 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700412{
413 struct fuse_conn *fc = get_fuse_conn(inode);
414 struct fuse_file *ff = file->private_data;
415 struct fuse_write_in inarg;
416 struct fuse_write_out outarg;
417
418 memset(&inarg, 0, sizeof(struct fuse_write_in));
419 inarg.fh = ff->fh;
420 inarg.offset = pos;
421 inarg.size = count;
422 req->in.h.opcode = FUSE_WRITE;
423 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700424 req->in.argpages = 1;
425 req->in.numargs = 2;
426 req->in.args[0].size = sizeof(struct fuse_write_in);
427 req->in.args[0].value = &inarg;
428 req->in.args[1].size = count;
429 req->out.numargs = 1;
430 req->out.args[0].size = sizeof(struct fuse_write_out);
431 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700432 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700433 return outarg.size;
434}
435
436static int fuse_prepare_write(struct file *file, struct page *page,
437 unsigned offset, unsigned to)
438{
439 /* No op */
440 return 0;
441}
442
443static int fuse_commit_write(struct file *file, struct page *page,
444 unsigned offset, unsigned to)
445{
446 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700447 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 unsigned count = to - offset;
449 struct inode *inode = page->mapping->host;
450 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800451 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800452 struct fuse_req *req;
453
454 if (is_bad_inode(inode))
455 return -EIO;
456
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700457 req = fuse_get_req(fc);
458 if (IS_ERR(req))
459 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460
461 req->num_pages = 1;
462 req->pages[0] = page;
463 req->page_offset = offset;
464 nres = fuse_send_write(req, file, inode, pos, count);
465 err = req->out.h.error;
466 fuse_put_request(fc, req);
467 if (!err && nres != count)
468 err = -EIO;
469 if (!err) {
470 pos += count;
471 if (pos > i_size_read(inode))
472 i_size_write(inode, pos);
473
474 if (offset == 0 && to == PAGE_CACHE_SIZE) {
475 clear_page_dirty(page);
476 SetPageUptodate(page);
477 }
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700478 }
479 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480 return err;
481}
482
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700483static void fuse_release_user_pages(struct fuse_req *req, int write)
484{
485 unsigned i;
486
487 for (i = 0; i < req->num_pages; i++) {
488 struct page *page = req->pages[i];
489 if (write)
490 set_page_dirty_lock(page);
491 put_page(page);
492 }
493}
494
495static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
496 unsigned nbytes, int write)
497{
498 unsigned long user_addr = (unsigned long) buf;
499 unsigned offset = user_addr & ~PAGE_MASK;
500 int npages;
501
502 /* This doesn't work with nfsd */
503 if (!current->mm)
504 return -EPERM;
505
506 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
507 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800508 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700509 down_read(&current->mm->mmap_sem);
510 npages = get_user_pages(current, current->mm, user_addr, npages, write,
511 0, req->pages, NULL);
512 up_read(&current->mm->mmap_sem);
513 if (npages < 0)
514 return npages;
515
516 req->num_pages = npages;
517 req->page_offset = offset;
518 return 0;
519}
520
521static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
522 size_t count, loff_t *ppos, int write)
523{
524 struct inode *inode = file->f_dentry->d_inode;
525 struct fuse_conn *fc = get_fuse_conn(inode);
526 size_t nmax = write ? fc->max_write : fc->max_read;
527 loff_t pos = *ppos;
528 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800529 struct fuse_req *req;
530
531 if (is_bad_inode(inode))
532 return -EIO;
533
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700534 req = fuse_get_req(fc);
535 if (IS_ERR(req))
536 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700537
538 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700539 size_t nres;
540 size_t nbytes = min(count, nmax);
541 int err = fuse_get_user_pages(req, buf, nbytes, !write);
542 if (err) {
543 res = err;
544 break;
545 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800546 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
547 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700548 if (write)
549 nres = fuse_send_write(req, file, inode, pos, nbytes);
550 else
551 nres = fuse_send_read(req, file, inode, pos, nbytes);
552 fuse_release_user_pages(req, !write);
553 if (req->out.h.error) {
554 if (!res)
555 res = req->out.h.error;
556 break;
557 } else if (nres > nbytes) {
558 res = -EIO;
559 break;
560 }
561 count -= nres;
562 res += nres;
563 pos += nres;
564 buf += nres;
565 if (nres != nbytes)
566 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200567 if (count) {
568 fuse_put_request(fc, req);
569 req = fuse_get_req(fc);
570 if (IS_ERR(req))
571 break;
572 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700573 }
574 fuse_put_request(fc, req);
575 if (res > 0) {
576 if (write && pos > i_size_read(inode))
577 i_size_write(inode, pos);
578 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700579 }
580 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700581
582 return res;
583}
584
585static ssize_t fuse_direct_read(struct file *file, char __user *buf,
586 size_t count, loff_t *ppos)
587{
588 return fuse_direct_io(file, buf, count, ppos, 0);
589}
590
591static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
592 size_t count, loff_t *ppos)
593{
594 struct inode *inode = file->f_dentry->d_inode;
595 ssize_t res;
596 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800597 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700598 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800599 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700600 return res;
601}
602
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700603static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
604{
605 if ((vma->vm_flags & VM_SHARED)) {
606 if ((vma->vm_flags & VM_WRITE))
607 return -ENODEV;
608 else
609 vma->vm_flags &= ~VM_MAYWRITE;
610 }
611 return generic_file_mmap(file, vma);
612}
613
614static int fuse_set_page_dirty(struct page *page)
615{
616 printk("fuse_set_page_dirty: should not happen\n");
617 dump_stack();
618 return 0;
619}
620
Miklos Szeredi71421252006-06-25 05:48:52 -0700621static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
622 struct file_lock *fl)
623{
624 switch (ffl->type) {
625 case F_UNLCK:
626 break;
627
628 case F_RDLCK:
629 case F_WRLCK:
630 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
631 ffl->end < ffl->start)
632 return -EIO;
633
634 fl->fl_start = ffl->start;
635 fl->fl_end = ffl->end;
636 fl->fl_pid = ffl->pid;
637 break;
638
639 default:
640 return -EIO;
641 }
642 fl->fl_type = ffl->type;
643 return 0;
644}
645
646static void fuse_lk_fill(struct fuse_req *req, struct file *file,
647 const struct file_lock *fl, int opcode, pid_t pid)
648{
649 struct inode *inode = file->f_dentry->d_inode;
650 struct fuse_file *ff = file->private_data;
651 struct fuse_lk_in *arg = &req->misc.lk_in;
652
653 arg->fh = ff->fh;
654 arg->owner = fuse_lock_owner_id(fl->fl_owner);
655 arg->lk.start = fl->fl_start;
656 arg->lk.end = fl->fl_end;
657 arg->lk.type = fl->fl_type;
658 arg->lk.pid = pid;
659 req->in.h.opcode = opcode;
660 req->in.h.nodeid = get_node_id(inode);
661 req->in.numargs = 1;
662 req->in.args[0].size = sizeof(*arg);
663 req->in.args[0].value = arg;
664}
665
666static int fuse_getlk(struct file *file, struct file_lock *fl)
667{
668 struct inode *inode = file->f_dentry->d_inode;
669 struct fuse_conn *fc = get_fuse_conn(inode);
670 struct fuse_req *req;
671 struct fuse_lk_out outarg;
672 int err;
673
674 req = fuse_get_req(fc);
675 if (IS_ERR(req))
676 return PTR_ERR(req);
677
678 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0);
679 req->out.numargs = 1;
680 req->out.args[0].size = sizeof(outarg);
681 req->out.args[0].value = &outarg;
682 request_send(fc, req);
683 err = req->out.h.error;
684 fuse_put_request(fc, req);
685 if (!err)
686 err = convert_fuse_file_lock(&outarg.lk, fl);
687
688 return err;
689}
690
691static int fuse_setlk(struct file *file, struct file_lock *fl)
692{
693 struct inode *inode = file->f_dentry->d_inode;
694 struct fuse_conn *fc = get_fuse_conn(inode);
695 struct fuse_req *req;
696 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
697 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
698 int err;
699
700 /* Unlock on close is handled by the flush method */
701 if (fl->fl_flags & FL_CLOSE)
702 return 0;
703
704 req = fuse_get_req(fc);
705 if (IS_ERR(req))
706 return PTR_ERR(req);
707
708 fuse_lk_fill(req, file, fl, opcode, pid);
709 request_send(fc, req);
710 err = req->out.h.error;
711 fuse_put_request(fc, req);
712 return err;
713}
714
715static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
716{
717 struct inode *inode = file->f_dentry->d_inode;
718 struct fuse_conn *fc = get_fuse_conn(inode);
719 int err;
720
721 if (cmd == F_GETLK) {
722 if (fc->no_lock) {
723 if (!posix_test_lock(file, fl, fl))
724 fl->fl_type = F_UNLCK;
725 err = 0;
726 } else
727 err = fuse_getlk(file, fl);
728 } else {
729 if (fc->no_lock)
730 err = posix_lock_file_wait(file, fl);
731 else
732 err = fuse_setlk(file, fl);
733 }
734 return err;
735}
736
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800737static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700738 .llseek = generic_file_llseek,
739 .read = generic_file_read,
740 .write = generic_file_write,
741 .mmap = fuse_file_mmap,
742 .open = fuse_open,
743 .flush = fuse_flush,
744 .release = fuse_release,
745 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700746 .lock = fuse_file_lock,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700747 .sendfile = generic_file_sendfile,
748};
749
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800750static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700751 .llseek = generic_file_llseek,
752 .read = fuse_direct_read,
753 .write = fuse_direct_write,
754 .open = fuse_open,
755 .flush = fuse_flush,
756 .release = fuse_release,
757 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700758 .lock = fuse_file_lock,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700759 /* no mmap and sendfile */
760};
761
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762static struct address_space_operations fuse_file_aops = {
763 .readpage = fuse_readpage,
764 .prepare_write = fuse_prepare_write,
765 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700766 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700767 .set_page_dirty = fuse_set_page_dirty,
768};
769
770void fuse_init_file_inode(struct inode *inode)
771{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700772 inode->i_fop = &fuse_file_operations;
773 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700774}