blob: 043d5b36846d8c7f24464158b60ad3b2a12e92c4 [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 Szeredi64c6d8e2006-01-16 22:14:42 -0800116/* Special case for failed iget in CREATE */
117static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
118{
119 u64 nodeid = req->in.h.nodeid;
120 fuse_reset_request(req);
121 fuse_send_forget(fc, req, nodeid, 1);
122}
123
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800124void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
125 u64 nodeid, struct inode *inode, int flags, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700126{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800127 struct fuse_req * req = ff->release_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700128 struct fuse_release_in *inarg = &req->misc.release_in;
129
130 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800131 inarg->flags = flags;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700132 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800133 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700134 req->inode = inode;
135 req->in.numargs = 1;
136 req->in.args[0].size = sizeof(struct fuse_release_in);
137 req->in.args[0].value = inarg;
138 request_send_background(fc, req);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800139 if (!inode)
140 req->end = fuse_release_end;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700141 kfree(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800142}
143
144int fuse_release_common(struct inode *inode, struct file *file, int isdir)
145{
146 struct fuse_file *ff = file->private_data;
147 if (ff) {
148 struct fuse_conn *fc = get_fuse_conn(inode);
149 u64 nodeid = get_node_id(inode);
150 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
151 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700152
153 /* Return value is ignored by VFS */
154 return 0;
155}
156
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700157static int fuse_open(struct inode *inode, struct file *file)
158{
159 return fuse_open_common(inode, file, 0);
160}
161
162static int fuse_release(struct inode *inode, struct file *file)
163{
164 return fuse_release_common(inode, file, 0);
165}
166
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700167static int fuse_flush(struct file *file)
168{
169 struct inode *inode = file->f_dentry->d_inode;
170 struct fuse_conn *fc = get_fuse_conn(inode);
171 struct fuse_file *ff = file->private_data;
172 struct fuse_req *req;
173 struct fuse_flush_in inarg;
174 int err;
175
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800176 if (is_bad_inode(inode))
177 return -EIO;
178
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700179 if (fc->no_flush)
180 return 0;
181
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700182 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700183 if (!req)
184 return -EINTR;
185
186 memset(&inarg, 0, sizeof(inarg));
187 inarg.fh = ff->fh;
188 req->in.h.opcode = FUSE_FLUSH;
189 req->in.h.nodeid = get_node_id(inode);
190 req->inode = inode;
191 req->file = file;
192 req->in.numargs = 1;
193 req->in.args[0].size = sizeof(inarg);
194 req->in.args[0].value = &inarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700195 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700196 err = req->out.h.error;
197 fuse_put_request(fc, req);
198 if (err == -ENOSYS) {
199 fc->no_flush = 1;
200 err = 0;
201 }
202 return err;
203}
204
Miklos Szeredi82547982005-09-09 13:10:38 -0700205int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
206 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700207{
208 struct inode *inode = de->d_inode;
209 struct fuse_conn *fc = get_fuse_conn(inode);
210 struct fuse_file *ff = file->private_data;
211 struct fuse_req *req;
212 struct fuse_fsync_in inarg;
213 int err;
214
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800215 if (is_bad_inode(inode))
216 return -EIO;
217
Miklos Szeredi82547982005-09-09 13:10:38 -0700218 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219 return 0;
220
221 req = fuse_get_request(fc);
222 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700223 return -EINTR;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700224
225 memset(&inarg, 0, sizeof(inarg));
226 inarg.fh = ff->fh;
227 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700228 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229 req->in.h.nodeid = get_node_id(inode);
230 req->inode = inode;
231 req->file = file;
232 req->in.numargs = 1;
233 req->in.args[0].size = sizeof(inarg);
234 req->in.args[0].value = &inarg;
235 request_send(fc, req);
236 err = req->out.h.error;
237 fuse_put_request(fc, req);
238 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700239 if (isdir)
240 fc->no_fsyncdir = 1;
241 else
242 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700243 err = 0;
244 }
245 return err;
246}
247
Miklos Szeredi82547982005-09-09 13:10:38 -0700248static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
249{
250 return fuse_fsync_common(file, de, datasync, 0);
251}
252
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700253size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
254 struct inode *inode, loff_t pos, size_t count,
255 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700256{
257 struct fuse_conn *fc = get_fuse_conn(inode);
258 struct fuse_file *ff = file->private_data;
259 struct fuse_read_in inarg;
260
261 memset(&inarg, 0, sizeof(struct fuse_read_in));
262 inarg.fh = ff->fh;
263 inarg.offset = pos;
264 inarg.size = count;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700265 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266 req->in.h.nodeid = get_node_id(inode);
267 req->inode = inode;
268 req->file = file;
269 req->in.numargs = 1;
270 req->in.args[0].size = sizeof(struct fuse_read_in);
271 req->in.args[0].value = &inarg;
272 req->out.argpages = 1;
273 req->out.argvar = 1;
274 req->out.numargs = 1;
275 req->out.args[0].size = count;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700276 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277 return req->out.args[0].size;
278}
279
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800280static size_t fuse_send_read(struct fuse_req *req, struct file *file,
281 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700282{
283 return fuse_send_read_common(req, file, inode, pos, count, 0);
284}
285
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700286static int fuse_readpage(struct file *file, struct page *page)
287{
288 struct inode *inode = page->mapping->host;
289 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800290 struct fuse_req *req;
291 int err;
292
293 err = -EIO;
294 if (is_bad_inode(inode))
295 goto out;
296
297 err = -EINTR;
298 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700299 if (!req)
300 goto out;
301
302 req->out.page_zeroing = 1;
303 req->num_pages = 1;
304 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800305 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700306 err = req->out.h.error;
307 fuse_put_request(fc, req);
308 if (!err)
309 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700310 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700311 out:
312 unlock_page(page);
313 return err;
314}
315
Miklos Szeredidb50b962005-09-09 13:10:33 -0700316static int fuse_send_readpages(struct fuse_req *req, struct file *file,
317 struct inode *inode)
318{
Miklos Szeredi4633a222006-01-06 00:19:36 -0800319 loff_t pos = page_offset(req->pages[0]);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700320 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
321 unsigned i;
322 req->out.page_zeroing = 1;
323 fuse_send_read(req, file, inode, pos, count);
324 for (i = 0; i < req->num_pages; i++) {
325 struct page *page = req->pages[i];
326 if (!req->out.h.error)
327 SetPageUptodate(page);
328 unlock_page(page);
329 }
330 return req->out.h.error;
331}
332
333struct fuse_readpages_data {
334 struct fuse_req *req;
335 struct file *file;
336 struct inode *inode;
337};
338
339static int fuse_readpages_fill(void *_data, struct page *page)
340{
341 struct fuse_readpages_data *data = _data;
342 struct fuse_req *req = data->req;
343 struct inode *inode = data->inode;
344 struct fuse_conn *fc = get_fuse_conn(inode);
345
346 if (req->num_pages &&
347 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
348 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
349 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
350 int err = fuse_send_readpages(req, data->file, inode);
351 if (err) {
352 unlock_page(page);
353 return err;
354 }
355 fuse_reset_request(req);
356 }
357 req->pages[req->num_pages] = page;
358 req->num_pages ++;
359 return 0;
360}
361
362static int fuse_readpages(struct file *file, struct address_space *mapping,
363 struct list_head *pages, unsigned nr_pages)
364{
365 struct inode *inode = mapping->host;
366 struct fuse_conn *fc = get_fuse_conn(inode);
367 struct fuse_readpages_data data;
368 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800369
370 if (is_bad_inode(inode))
371 return -EIO;
372
Miklos Szeredidb50b962005-09-09 13:10:33 -0700373 data.file = file;
374 data.inode = inode;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700375 data.req = fuse_get_request(fc);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700376 if (!data.req)
377 return -EINTR;
378
379 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
380 if (!err && data.req->num_pages)
381 err = fuse_send_readpages(data.req, file, inode);
382 fuse_put_request(fc, data.req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700383 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredidb50b962005-09-09 13:10:33 -0700384 return err;
385}
386
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700387static size_t fuse_send_write(struct fuse_req *req, struct file *file,
388 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700389{
390 struct fuse_conn *fc = get_fuse_conn(inode);
391 struct fuse_file *ff = file->private_data;
392 struct fuse_write_in inarg;
393 struct fuse_write_out outarg;
394
395 memset(&inarg, 0, sizeof(struct fuse_write_in));
396 inarg.fh = ff->fh;
397 inarg.offset = pos;
398 inarg.size = count;
399 req->in.h.opcode = FUSE_WRITE;
400 req->in.h.nodeid = get_node_id(inode);
401 req->inode = inode;
402 req->file = file;
403 req->in.argpages = 1;
404 req->in.numargs = 2;
405 req->in.args[0].size = sizeof(struct fuse_write_in);
406 req->in.args[0].value = &inarg;
407 req->in.args[1].size = count;
408 req->out.numargs = 1;
409 req->out.args[0].size = sizeof(struct fuse_write_out);
410 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700411 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700412 return outarg.size;
413}
414
415static int fuse_prepare_write(struct file *file, struct page *page,
416 unsigned offset, unsigned to)
417{
418 /* No op */
419 return 0;
420}
421
422static int fuse_commit_write(struct file *file, struct page *page,
423 unsigned offset, unsigned to)
424{
425 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700426 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 unsigned count = to - offset;
428 struct inode *inode = page->mapping->host;
429 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800430 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800431 struct fuse_req *req;
432
433 if (is_bad_inode(inode))
434 return -EIO;
435
436 req = fuse_get_request(fc);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700437 if (!req)
438 return -EINTR;
439
440 req->num_pages = 1;
441 req->pages[0] = page;
442 req->page_offset = offset;
443 nres = fuse_send_write(req, file, inode, pos, count);
444 err = req->out.h.error;
445 fuse_put_request(fc, req);
446 if (!err && nres != count)
447 err = -EIO;
448 if (!err) {
449 pos += count;
450 if (pos > i_size_read(inode))
451 i_size_write(inode, pos);
452
453 if (offset == 0 && to == PAGE_CACHE_SIZE) {
454 clear_page_dirty(page);
455 SetPageUptodate(page);
456 }
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700457 }
458 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700459 return err;
460}
461
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700462static void fuse_release_user_pages(struct fuse_req *req, int write)
463{
464 unsigned i;
465
466 for (i = 0; i < req->num_pages; i++) {
467 struct page *page = req->pages[i];
468 if (write)
469 set_page_dirty_lock(page);
470 put_page(page);
471 }
472}
473
474static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
475 unsigned nbytes, int write)
476{
477 unsigned long user_addr = (unsigned long) buf;
478 unsigned offset = user_addr & ~PAGE_MASK;
479 int npages;
480
481 /* This doesn't work with nfsd */
482 if (!current->mm)
483 return -EPERM;
484
485 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
486 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800487 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700488 down_read(&current->mm->mmap_sem);
489 npages = get_user_pages(current, current->mm, user_addr, npages, write,
490 0, req->pages, NULL);
491 up_read(&current->mm->mmap_sem);
492 if (npages < 0)
493 return npages;
494
495 req->num_pages = npages;
496 req->page_offset = offset;
497 return 0;
498}
499
500static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
501 size_t count, loff_t *ppos, int write)
502{
503 struct inode *inode = file->f_dentry->d_inode;
504 struct fuse_conn *fc = get_fuse_conn(inode);
505 size_t nmax = write ? fc->max_write : fc->max_read;
506 loff_t pos = *ppos;
507 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800508 struct fuse_req *req;
509
510 if (is_bad_inode(inode))
511 return -EIO;
512
513 req = fuse_get_request(fc);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700514 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700515 return -EINTR;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700516
517 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700518 size_t nres;
519 size_t nbytes = min(count, nmax);
520 int err = fuse_get_user_pages(req, buf, nbytes, !write);
521 if (err) {
522 res = err;
523 break;
524 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800525 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
526 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700527 if (write)
528 nres = fuse_send_write(req, file, inode, pos, nbytes);
529 else
530 nres = fuse_send_read(req, file, inode, pos, nbytes);
531 fuse_release_user_pages(req, !write);
532 if (req->out.h.error) {
533 if (!res)
534 res = req->out.h.error;
535 break;
536 } else if (nres > nbytes) {
537 res = -EIO;
538 break;
539 }
540 count -= nres;
541 res += nres;
542 pos += nres;
543 buf += nres;
544 if (nres != nbytes)
545 break;
546 if (count)
547 fuse_reset_request(req);
548 }
549 fuse_put_request(fc, req);
550 if (res > 0) {
551 if (write && pos > i_size_read(inode))
552 i_size_write(inode, pos);
553 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700554 }
555 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700556
557 return res;
558}
559
560static ssize_t fuse_direct_read(struct file *file, char __user *buf,
561 size_t count, loff_t *ppos)
562{
563 return fuse_direct_io(file, buf, count, ppos, 0);
564}
565
566static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
567 size_t count, loff_t *ppos)
568{
569 struct inode *inode = file->f_dentry->d_inode;
570 ssize_t res;
571 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800572 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700573 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800574 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700575 return res;
576}
577
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700578static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
579{
580 if ((vma->vm_flags & VM_SHARED)) {
581 if ((vma->vm_flags & VM_WRITE))
582 return -ENODEV;
583 else
584 vma->vm_flags &= ~VM_MAYWRITE;
585 }
586 return generic_file_mmap(file, vma);
587}
588
589static int fuse_set_page_dirty(struct page *page)
590{
591 printk("fuse_set_page_dirty: should not happen\n");
592 dump_stack();
593 return 0;
594}
595
596static struct file_operations fuse_file_operations = {
597 .llseek = generic_file_llseek,
598 .read = generic_file_read,
599 .write = generic_file_write,
600 .mmap = fuse_file_mmap,
601 .open = fuse_open,
602 .flush = fuse_flush,
603 .release = fuse_release,
604 .fsync = fuse_fsync,
605 .sendfile = generic_file_sendfile,
606};
607
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700608static struct file_operations fuse_direct_io_file_operations = {
609 .llseek = generic_file_llseek,
610 .read = fuse_direct_read,
611 .write = fuse_direct_write,
612 .open = fuse_open,
613 .flush = fuse_flush,
614 .release = fuse_release,
615 .fsync = fuse_fsync,
616 /* no mmap and sendfile */
617};
618
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700619static struct address_space_operations fuse_file_aops = {
620 .readpage = fuse_readpage,
621 .prepare_write = fuse_prepare_write,
622 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700623 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700624 .set_page_dirty = fuse_set_page_dirty,
625};
626
627void fuse_init_file_inode(struct inode *inode)
628{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700629 inode->i_fop = &fuse_file_operations;
630 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700631}