blob: 52557664a89e250210382f006887b282e2913fc4 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
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
Miklos Szeredi45323fb2005-09-09 13:10:37 -070015static struct file_operations fuse_direct_io_file_operations;
16
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
25 req = fuse_get_request(fc);
26 if (!req)
27 return -EINTR;
28
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);
33 req->inode = inode;
34 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) {
52 ff->release_req = fuse_request_alloc();
53 if (!ff->release_req) {
54 kfree(ff);
55 ff = NULL;
56 }
57 }
58 return ff;
59}
60
61void fuse_file_free(struct fuse_file *ff)
62{
63 fuse_request_free(ff->release_req);
64 kfree(ff);
65}
66
67void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg)
69{
70 if (outarg->open_flags & FOPEN_DIRECT_IO)
71 file->f_op = &fuse_direct_io_file_operations;
72 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
73 invalidate_inode_pages(inode->i_mapping);
74 ff->fh = outarg->fh;
75 file->private_data = ff;
76}
77
78int fuse_open_common(struct inode *inode, struct file *file, int isdir)
79{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070080 struct fuse_open_out outarg;
81 struct fuse_file *ff;
82 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070083
Miklos Szeredidd190d02005-09-30 11:59:02 -070084 /* VFS checks this, but only _after_ ->open() */
85 if (file->f_flags & O_DIRECT)
86 return -EINVAL;
87
Miklos Szeredib6aeade2005-09-09 13:10:30 -070088 err = generic_file_open(inode, file);
89 if (err)
90 return err;
91
92 /* If opening the root node, no lookup has been performed on
93 it, so the attributes must be refreshed */
94 if (get_node_id(inode) == FUSE_ROOT_ID) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080095 err = fuse_do_getattr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070096 if (err)
97 return err;
98 }
99
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800100 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700101 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800102 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700103
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800104 err = fuse_send_open(inode, file, isdir, &outarg);
105 if (err)
106 fuse_file_free(ff);
107 else {
108 if (isdir)
109 outarg.open_flags &= ~FOPEN_DIRECT_IO;
110 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700111 }
112
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700113 return err;
114}
115
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800116void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
117 u64 nodeid, struct inode *inode, int flags, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700118{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800119 struct fuse_req * req = ff->release_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700120 struct fuse_release_in *inarg = &req->misc.release_in;
121
122 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800123 inarg->flags = flags;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700124 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800125 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700126 req->inode = inode;
127 req->in.numargs = 1;
128 req->in.args[0].size = sizeof(struct fuse_release_in);
129 req->in.args[0].value = inarg;
130 request_send_background(fc, req);
131 kfree(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800132}
133
134int fuse_release_common(struct inode *inode, struct file *file, int isdir)
135{
136 struct fuse_file *ff = file->private_data;
137 if (ff) {
138 struct fuse_conn *fc = get_fuse_conn(inode);
139 u64 nodeid = get_node_id(inode);
140 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
141 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700142
143 /* Return value is ignored by VFS */
144 return 0;
145}
146
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700147static int fuse_open(struct inode *inode, struct file *file)
148{
149 return fuse_open_common(inode, file, 0);
150}
151
152static int fuse_release(struct inode *inode, struct file *file)
153{
154 return fuse_release_common(inode, file, 0);
155}
156
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700157static int fuse_flush(struct file *file)
158{
159 struct inode *inode = file->f_dentry->d_inode;
160 struct fuse_conn *fc = get_fuse_conn(inode);
161 struct fuse_file *ff = file->private_data;
162 struct fuse_req *req;
163 struct fuse_flush_in inarg;
164 int err;
165
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800166 if (is_bad_inode(inode))
167 return -EIO;
168
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700169 if (fc->no_flush)
170 return 0;
171
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700172 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700173 if (!req)
174 return -EINTR;
175
176 memset(&inarg, 0, sizeof(inarg));
177 inarg.fh = ff->fh;
178 req->in.h.opcode = FUSE_FLUSH;
179 req->in.h.nodeid = get_node_id(inode);
180 req->inode = inode;
181 req->file = file;
182 req->in.numargs = 1;
183 req->in.args[0].size = sizeof(inarg);
184 req->in.args[0].value = &inarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700185 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700186 err = req->out.h.error;
187 fuse_put_request(fc, req);
188 if (err == -ENOSYS) {
189 fc->no_flush = 1;
190 err = 0;
191 }
192 return err;
193}
194
Miklos Szeredi82547982005-09-09 13:10:38 -0700195int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
196 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197{
198 struct inode *inode = de->d_inode;
199 struct fuse_conn *fc = get_fuse_conn(inode);
200 struct fuse_file *ff = file->private_data;
201 struct fuse_req *req;
202 struct fuse_fsync_in inarg;
203 int err;
204
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800205 if (is_bad_inode(inode))
206 return -EIO;
207
Miklos Szeredi82547982005-09-09 13:10:38 -0700208 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700209 return 0;
210
211 req = fuse_get_request(fc);
212 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700213 return -EINTR;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700214
215 memset(&inarg, 0, sizeof(inarg));
216 inarg.fh = ff->fh;
217 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700218 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219 req->in.h.nodeid = get_node_id(inode);
220 req->inode = inode;
221 req->file = file;
222 req->in.numargs = 1;
223 req->in.args[0].size = sizeof(inarg);
224 req->in.args[0].value = &inarg;
225 request_send(fc, req);
226 err = req->out.h.error;
227 fuse_put_request(fc, req);
228 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700229 if (isdir)
230 fc->no_fsyncdir = 1;
231 else
232 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700233 err = 0;
234 }
235 return err;
236}
237
Miklos Szeredi82547982005-09-09 13:10:38 -0700238static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
239{
240 return fuse_fsync_common(file, de, datasync, 0);
241}
242
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700243size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
244 struct inode *inode, loff_t pos, size_t count,
245 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700246{
247 struct fuse_conn *fc = get_fuse_conn(inode);
248 struct fuse_file *ff = file->private_data;
249 struct fuse_read_in inarg;
250
251 memset(&inarg, 0, sizeof(struct fuse_read_in));
252 inarg.fh = ff->fh;
253 inarg.offset = pos;
254 inarg.size = count;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700255 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700256 req->in.h.nodeid = get_node_id(inode);
257 req->inode = inode;
258 req->file = file;
259 req->in.numargs = 1;
260 req->in.args[0].size = sizeof(struct fuse_read_in);
261 req->in.args[0].value = &inarg;
262 req->out.argpages = 1;
263 req->out.argvar = 1;
264 req->out.numargs = 1;
265 req->out.args[0].size = count;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700266 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700267 return req->out.args[0].size;
268}
269
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800270static size_t fuse_send_read(struct fuse_req *req, struct file *file,
271 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700272{
273 return fuse_send_read_common(req, file, inode, pos, count, 0);
274}
275
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700276static int fuse_readpage(struct file *file, struct page *page)
277{
278 struct inode *inode = page->mapping->host;
279 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800280 struct fuse_req *req;
281 int err;
282
283 err = -EIO;
284 if (is_bad_inode(inode))
285 goto out;
286
287 err = -EINTR;
288 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700289 if (!req)
290 goto out;
291
292 req->out.page_zeroing = 1;
293 req->num_pages = 1;
294 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800295 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296 err = req->out.h.error;
297 fuse_put_request(fc, req);
298 if (!err)
299 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700300 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700301 out:
302 unlock_page(page);
303 return err;
304}
305
Miklos Szeredidb50b962005-09-09 13:10:33 -0700306static int fuse_send_readpages(struct fuse_req *req, struct file *file,
307 struct inode *inode)
308{
Miklos Szeredi4633a222006-01-06 00:19:36 -0800309 loff_t pos = page_offset(req->pages[0]);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700310 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
311 unsigned i;
312 req->out.page_zeroing = 1;
313 fuse_send_read(req, file, inode, pos, count);
314 for (i = 0; i < req->num_pages; i++) {
315 struct page *page = req->pages[i];
316 if (!req->out.h.error)
317 SetPageUptodate(page);
318 unlock_page(page);
319 }
320 return req->out.h.error;
321}
322
323struct fuse_readpages_data {
324 struct fuse_req *req;
325 struct file *file;
326 struct inode *inode;
327};
328
329static int fuse_readpages_fill(void *_data, struct page *page)
330{
331 struct fuse_readpages_data *data = _data;
332 struct fuse_req *req = data->req;
333 struct inode *inode = data->inode;
334 struct fuse_conn *fc = get_fuse_conn(inode);
335
336 if (req->num_pages &&
337 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
338 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
339 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
340 int err = fuse_send_readpages(req, data->file, inode);
341 if (err) {
342 unlock_page(page);
343 return err;
344 }
345 fuse_reset_request(req);
346 }
347 req->pages[req->num_pages] = page;
348 req->num_pages ++;
349 return 0;
350}
351
352static int fuse_readpages(struct file *file, struct address_space *mapping,
353 struct list_head *pages, unsigned nr_pages)
354{
355 struct inode *inode = mapping->host;
356 struct fuse_conn *fc = get_fuse_conn(inode);
357 struct fuse_readpages_data data;
358 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800359
360 if (is_bad_inode(inode))
361 return -EIO;
362
Miklos Szeredidb50b962005-09-09 13:10:33 -0700363 data.file = file;
364 data.inode = inode;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700365 data.req = fuse_get_request(fc);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700366 if (!data.req)
367 return -EINTR;
368
369 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
370 if (!err && data.req->num_pages)
371 err = fuse_send_readpages(data.req, file, inode);
372 fuse_put_request(fc, data.req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700373 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredidb50b962005-09-09 13:10:33 -0700374 return err;
375}
376
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700377static size_t fuse_send_write(struct fuse_req *req, struct file *file,
378 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700379{
380 struct fuse_conn *fc = get_fuse_conn(inode);
381 struct fuse_file *ff = file->private_data;
382 struct fuse_write_in inarg;
383 struct fuse_write_out outarg;
384
385 memset(&inarg, 0, sizeof(struct fuse_write_in));
386 inarg.fh = ff->fh;
387 inarg.offset = pos;
388 inarg.size = count;
389 req->in.h.opcode = FUSE_WRITE;
390 req->in.h.nodeid = get_node_id(inode);
391 req->inode = inode;
392 req->file = file;
393 req->in.argpages = 1;
394 req->in.numargs = 2;
395 req->in.args[0].size = sizeof(struct fuse_write_in);
396 req->in.args[0].value = &inarg;
397 req->in.args[1].size = count;
398 req->out.numargs = 1;
399 req->out.args[0].size = sizeof(struct fuse_write_out);
400 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700401 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700402 return outarg.size;
403}
404
405static int fuse_prepare_write(struct file *file, struct page *page,
406 unsigned offset, unsigned to)
407{
408 /* No op */
409 return 0;
410}
411
412static int fuse_commit_write(struct file *file, struct page *page,
413 unsigned offset, unsigned to)
414{
415 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700416 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700417 unsigned count = to - offset;
418 struct inode *inode = page->mapping->host;
419 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800420 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800421 struct fuse_req *req;
422
423 if (is_bad_inode(inode))
424 return -EIO;
425
426 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 if (!req)
428 return -EINTR;
429
430 req->num_pages = 1;
431 req->pages[0] = page;
432 req->page_offset = offset;
433 nres = fuse_send_write(req, file, inode, pos, count);
434 err = req->out.h.error;
435 fuse_put_request(fc, req);
436 if (!err && nres != count)
437 err = -EIO;
438 if (!err) {
439 pos += count;
440 if (pos > i_size_read(inode))
441 i_size_write(inode, pos);
442
443 if (offset == 0 && to == PAGE_CACHE_SIZE) {
444 clear_page_dirty(page);
445 SetPageUptodate(page);
446 }
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700447 }
448 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 return err;
450}
451
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700452static void fuse_release_user_pages(struct fuse_req *req, int write)
453{
454 unsigned i;
455
456 for (i = 0; i < req->num_pages; i++) {
457 struct page *page = req->pages[i];
458 if (write)
459 set_page_dirty_lock(page);
460 put_page(page);
461 }
462}
463
464static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
465 unsigned nbytes, int write)
466{
467 unsigned long user_addr = (unsigned long) buf;
468 unsigned offset = user_addr & ~PAGE_MASK;
469 int npages;
470
471 /* This doesn't work with nfsd */
472 if (!current->mm)
473 return -EPERM;
474
475 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
476 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800477 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700478 down_read(&current->mm->mmap_sem);
479 npages = get_user_pages(current, current->mm, user_addr, npages, write,
480 0, req->pages, NULL);
481 up_read(&current->mm->mmap_sem);
482 if (npages < 0)
483 return npages;
484
485 req->num_pages = npages;
486 req->page_offset = offset;
487 return 0;
488}
489
490static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
491 size_t count, loff_t *ppos, int write)
492{
493 struct inode *inode = file->f_dentry->d_inode;
494 struct fuse_conn *fc = get_fuse_conn(inode);
495 size_t nmax = write ? fc->max_write : fc->max_read;
496 loff_t pos = *ppos;
497 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800498 struct fuse_req *req;
499
500 if (is_bad_inode(inode))
501 return -EIO;
502
503 req = fuse_get_request(fc);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700504 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700505 return -EINTR;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700506
507 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700508 size_t nres;
509 size_t nbytes = min(count, nmax);
510 int err = fuse_get_user_pages(req, buf, nbytes, !write);
511 if (err) {
512 res = err;
513 break;
514 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800515 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
516 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700517 if (write)
518 nres = fuse_send_write(req, file, inode, pos, nbytes);
519 else
520 nres = fuse_send_read(req, file, inode, pos, nbytes);
521 fuse_release_user_pages(req, !write);
522 if (req->out.h.error) {
523 if (!res)
524 res = req->out.h.error;
525 break;
526 } else if (nres > nbytes) {
527 res = -EIO;
528 break;
529 }
530 count -= nres;
531 res += nres;
532 pos += nres;
533 buf += nres;
534 if (nres != nbytes)
535 break;
536 if (count)
537 fuse_reset_request(req);
538 }
539 fuse_put_request(fc, req);
540 if (res > 0) {
541 if (write && pos > i_size_read(inode))
542 i_size_write(inode, pos);
543 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700544 }
545 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700546
547 return res;
548}
549
550static ssize_t fuse_direct_read(struct file *file, char __user *buf,
551 size_t count, loff_t *ppos)
552{
553 return fuse_direct_io(file, buf, count, ppos, 0);
554}
555
556static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
557 size_t count, loff_t *ppos)
558{
559 struct inode *inode = file->f_dentry->d_inode;
560 ssize_t res;
561 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800562 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700563 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800564 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700565 return res;
566}
567
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700568static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
569{
570 if ((vma->vm_flags & VM_SHARED)) {
571 if ((vma->vm_flags & VM_WRITE))
572 return -ENODEV;
573 else
574 vma->vm_flags &= ~VM_MAYWRITE;
575 }
576 return generic_file_mmap(file, vma);
577}
578
579static int fuse_set_page_dirty(struct page *page)
580{
581 printk("fuse_set_page_dirty: should not happen\n");
582 dump_stack();
583 return 0;
584}
585
586static struct file_operations fuse_file_operations = {
587 .llseek = generic_file_llseek,
588 .read = generic_file_read,
589 .write = generic_file_write,
590 .mmap = fuse_file_mmap,
591 .open = fuse_open,
592 .flush = fuse_flush,
593 .release = fuse_release,
594 .fsync = fuse_fsync,
595 .sendfile = generic_file_sendfile,
596};
597
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700598static struct file_operations fuse_direct_io_file_operations = {
599 .llseek = generic_file_llseek,
600 .read = fuse_direct_read,
601 .write = fuse_direct_write,
602 .open = fuse_open,
603 .flush = fuse_flush,
604 .release = fuse_release,
605 .fsync = fuse_fsync,
606 /* no mmap and sendfile */
607};
608
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700609static struct address_space_operations fuse_file_aops = {
610 .readpage = fuse_readpage,
611 .prepare_write = fuse_prepare_write,
612 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700613 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700614 .set_page_dirty = fuse_set_page_dirty,
615};
616
617void fuse_init_file_inode(struct inode *inode)
618{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700619 inode->i_fop = &fuse_file_operations;
620 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700621}