fuse: refresh stale attributes in fuse_permission()
[linux-2.6/mini2440.git] / fs / fuse / file.c
blobf3ef2bde983b0e9df294126c4f8331d672ba0ebb
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 static const struct file_operations fuse_direct_io_file_operations;
18 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19 struct fuse_open_out *outargp)
21 struct fuse_conn *fc = get_fuse_conn(inode);
22 struct fuse_open_in inarg;
23 struct fuse_req *req;
24 int err;
26 req = fuse_get_req(fc);
27 if (IS_ERR(req))
28 return PTR_ERR(req);
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
32 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
33 req->in.h.nodeid = get_node_id(inode);
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);
44 return err;
47 struct fuse_file *fuse_file_alloc(void)
49 struct fuse_file *ff;
50 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51 if (ff) {
52 ff->reserved_req = fuse_request_alloc();
53 if (!ff->reserved_req) {
54 kfree(ff);
55 ff = NULL;
57 atomic_set(&ff->count, 0);
59 return ff;
62 void fuse_file_free(struct fuse_file *ff)
64 fuse_request_free(ff->reserved_req);
65 kfree(ff);
68 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
70 atomic_inc(&ff->count);
71 return ff;
74 static void fuse_file_put(struct fuse_file *ff)
76 if (atomic_dec_and_test(&ff->count)) {
77 struct fuse_req *req = ff->reserved_req;
78 struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode);
79 request_send_background(fc, req);
80 kfree(ff);
84 void fuse_finish_open(struct inode *inode, struct file *file,
85 struct fuse_file *ff, struct fuse_open_out *outarg)
87 if (outarg->open_flags & FOPEN_DIRECT_IO)
88 file->f_op = &fuse_direct_io_file_operations;
89 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
90 invalidate_inode_pages2(inode->i_mapping);
91 ff->fh = outarg->fh;
92 file->private_data = fuse_file_get(ff);
95 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
97 struct fuse_open_out outarg;
98 struct fuse_file *ff;
99 int err;
101 /* VFS checks this, but only _after_ ->open() */
102 if (file->f_flags & O_DIRECT)
103 return -EINVAL;
105 err = generic_file_open(inode, file);
106 if (err)
107 return err;
109 ff = fuse_file_alloc();
110 if (!ff)
111 return -ENOMEM;
113 err = fuse_send_open(inode, file, isdir, &outarg);
114 if (err)
115 fuse_file_free(ff);
116 else {
117 if (isdir)
118 outarg.open_flags &= ~FOPEN_DIRECT_IO;
119 fuse_finish_open(inode, file, ff, &outarg);
122 return err;
125 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
127 struct fuse_req *req = ff->reserved_req;
128 struct fuse_release_in *inarg = &req->misc.release_in;
130 inarg->fh = ff->fh;
131 inarg->flags = flags;
132 req->in.h.opcode = opcode;
133 req->in.h.nodeid = nodeid;
134 req->in.numargs = 1;
135 req->in.args[0].size = sizeof(struct fuse_release_in);
136 req->in.args[0].value = inarg;
139 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
141 struct fuse_file *ff = file->private_data;
142 if (ff) {
143 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
144 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
146 /* Hold vfsmount and dentry until release is finished */
147 ff->reserved_req->vfsmount = mntget(file->f_path.mnt);
148 ff->reserved_req->dentry = dget(file->f_path.dentry);
150 * Normally this will send the RELEASE request,
151 * however if some asynchronous READ or WRITE requests
152 * are outstanding, the sending will be delayed
154 fuse_file_put(ff);
157 /* Return value is ignored by VFS */
158 return 0;
161 static int fuse_open(struct inode *inode, struct file *file)
163 return fuse_open_common(inode, file, 0);
166 static int fuse_release(struct inode *inode, struct file *file)
168 return fuse_release_common(inode, file, 0);
172 * Scramble the ID space with XTEA, so that the value of the files_struct
173 * pointer is not exposed to userspace.
175 static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
177 u32 *k = fc->scramble_key;
178 u64 v = (unsigned long) id;
179 u32 v0 = v;
180 u32 v1 = v >> 32;
181 u32 sum = 0;
182 int i;
184 for (i = 0; i < 32; i++) {
185 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
186 sum += 0x9E3779B9;
187 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
190 return (u64) v0 + ((u64) v1 << 32);
193 static int fuse_flush(struct file *file, fl_owner_t id)
195 struct inode *inode = file->f_path.dentry->d_inode;
196 struct fuse_conn *fc = get_fuse_conn(inode);
197 struct fuse_file *ff = file->private_data;
198 struct fuse_req *req;
199 struct fuse_flush_in inarg;
200 int err;
202 if (is_bad_inode(inode))
203 return -EIO;
205 if (fc->no_flush)
206 return 0;
208 req = fuse_get_req_nofail(fc, file);
209 memset(&inarg, 0, sizeof(inarg));
210 inarg.fh = ff->fh;
211 inarg.lock_owner = fuse_lock_owner_id(fc, id);
212 req->in.h.opcode = FUSE_FLUSH;
213 req->in.h.nodeid = get_node_id(inode);
214 req->in.numargs = 1;
215 req->in.args[0].size = sizeof(inarg);
216 req->in.args[0].value = &inarg;
217 req->force = 1;
218 request_send(fc, req);
219 err = req->out.h.error;
220 fuse_put_request(fc, req);
221 if (err == -ENOSYS) {
222 fc->no_flush = 1;
223 err = 0;
225 return err;
228 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
229 int isdir)
231 struct inode *inode = de->d_inode;
232 struct fuse_conn *fc = get_fuse_conn(inode);
233 struct fuse_file *ff = file->private_data;
234 struct fuse_req *req;
235 struct fuse_fsync_in inarg;
236 int err;
238 if (is_bad_inode(inode))
239 return -EIO;
241 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
242 return 0;
244 req = fuse_get_req(fc);
245 if (IS_ERR(req))
246 return PTR_ERR(req);
248 memset(&inarg, 0, sizeof(inarg));
249 inarg.fh = ff->fh;
250 inarg.fsync_flags = datasync ? 1 : 0;
251 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
252 req->in.h.nodeid = get_node_id(inode);
253 req->in.numargs = 1;
254 req->in.args[0].size = sizeof(inarg);
255 req->in.args[0].value = &inarg;
256 request_send(fc, req);
257 err = req->out.h.error;
258 fuse_put_request(fc, req);
259 if (err == -ENOSYS) {
260 if (isdir)
261 fc->no_fsyncdir = 1;
262 else
263 fc->no_fsync = 1;
264 err = 0;
266 return err;
269 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
271 return fuse_fsync_common(file, de, datasync, 0);
274 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
275 struct inode *inode, loff_t pos, size_t count, int opcode)
277 struct fuse_read_in *inarg = &req->misc.read_in;
279 inarg->fh = ff->fh;
280 inarg->offset = pos;
281 inarg->size = count;
282 req->in.h.opcode = opcode;
283 req->in.h.nodeid = get_node_id(inode);
284 req->in.numargs = 1;
285 req->in.args[0].size = sizeof(struct fuse_read_in);
286 req->in.args[0].value = inarg;
287 req->out.argpages = 1;
288 req->out.argvar = 1;
289 req->out.numargs = 1;
290 req->out.args[0].size = count;
293 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
294 struct inode *inode, loff_t pos, size_t count)
296 struct fuse_conn *fc = get_fuse_conn(inode);
297 struct fuse_file *ff = file->private_data;
298 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
299 request_send(fc, req);
300 return req->out.args[0].size;
303 static int fuse_readpage(struct file *file, struct page *page)
305 struct inode *inode = page->mapping->host;
306 struct fuse_conn *fc = get_fuse_conn(inode);
307 struct fuse_req *req;
308 int err;
310 err = -EIO;
311 if (is_bad_inode(inode))
312 goto out;
314 req = fuse_get_req(fc);
315 err = PTR_ERR(req);
316 if (IS_ERR(req))
317 goto out;
319 req->out.page_zeroing = 1;
320 req->num_pages = 1;
321 req->pages[0] = page;
322 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
323 err = req->out.h.error;
324 fuse_put_request(fc, req);
325 if (!err)
326 SetPageUptodate(page);
327 fuse_invalidate_attr(inode); /* atime changed */
328 out:
329 unlock_page(page);
330 return err;
333 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
335 int i;
337 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
339 for (i = 0; i < req->num_pages; i++) {
340 struct page *page = req->pages[i];
341 if (!req->out.h.error)
342 SetPageUptodate(page);
343 else
344 SetPageError(page);
345 unlock_page(page);
347 if (req->ff)
348 fuse_file_put(req->ff);
349 fuse_put_request(fc, req);
352 static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff,
353 struct inode *inode)
355 struct fuse_conn *fc = get_fuse_conn(inode);
356 loff_t pos = page_offset(req->pages[0]);
357 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
358 req->out.page_zeroing = 1;
359 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
360 if (fc->async_read) {
361 req->ff = fuse_file_get(ff);
362 req->end = fuse_readpages_end;
363 request_send_background(fc, req);
364 } else {
365 request_send(fc, req);
366 fuse_readpages_end(fc, req);
370 struct fuse_fill_data {
371 struct fuse_req *req;
372 struct fuse_file *ff;
373 struct inode *inode;
376 static int fuse_readpages_fill(void *_data, struct page *page)
378 struct fuse_fill_data *data = _data;
379 struct fuse_req *req = data->req;
380 struct inode *inode = data->inode;
381 struct fuse_conn *fc = get_fuse_conn(inode);
383 if (req->num_pages &&
384 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
385 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
386 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
387 fuse_send_readpages(req, data->ff, inode);
388 data->req = req = fuse_get_req(fc);
389 if (IS_ERR(req)) {
390 unlock_page(page);
391 return PTR_ERR(req);
394 req->pages[req->num_pages] = page;
395 req->num_pages ++;
396 return 0;
399 static int fuse_readpages(struct file *file, struct address_space *mapping,
400 struct list_head *pages, unsigned nr_pages)
402 struct inode *inode = mapping->host;
403 struct fuse_conn *fc = get_fuse_conn(inode);
404 struct fuse_fill_data data;
405 int err;
407 err = -EIO;
408 if (is_bad_inode(inode))
409 goto out;
411 data.ff = file->private_data;
412 data.inode = inode;
413 data.req = fuse_get_req(fc);
414 err = PTR_ERR(data.req);
415 if (IS_ERR(data.req))
416 goto out;
418 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
419 if (!err) {
420 if (data.req->num_pages)
421 fuse_send_readpages(data.req, data.ff, inode);
422 else
423 fuse_put_request(fc, data.req);
425 out:
426 return err;
429 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
430 struct inode *inode, loff_t pos, size_t count)
432 struct fuse_conn *fc = get_fuse_conn(inode);
433 struct fuse_file *ff = file->private_data;
434 struct fuse_write_in inarg;
435 struct fuse_write_out outarg;
437 memset(&inarg, 0, sizeof(struct fuse_write_in));
438 inarg.fh = ff->fh;
439 inarg.offset = pos;
440 inarg.size = count;
441 req->in.h.opcode = FUSE_WRITE;
442 req->in.h.nodeid = get_node_id(inode);
443 req->in.argpages = 1;
444 req->in.numargs = 2;
445 req->in.args[0].size = sizeof(struct fuse_write_in);
446 req->in.args[0].value = &inarg;
447 req->in.args[1].size = count;
448 req->out.numargs = 1;
449 req->out.args[0].size = sizeof(struct fuse_write_out);
450 req->out.args[0].value = &outarg;
451 request_send(fc, req);
452 return outarg.size;
455 static int fuse_write_begin(struct file *file, struct address_space *mapping,
456 loff_t pos, unsigned len, unsigned flags,
457 struct page **pagep, void **fsdata)
459 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
461 *pagep = __grab_cache_page(mapping, index);
462 if (!*pagep)
463 return -ENOMEM;
464 return 0;
467 static int fuse_buffered_write(struct file *file, struct inode *inode,
468 loff_t pos, unsigned count, struct page *page)
470 int err;
471 size_t nres;
472 struct fuse_conn *fc = get_fuse_conn(inode);
473 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
474 struct fuse_req *req;
476 if (is_bad_inode(inode))
477 return -EIO;
479 req = fuse_get_req(fc);
480 if (IS_ERR(req))
481 return PTR_ERR(req);
483 req->num_pages = 1;
484 req->pages[0] = page;
485 req->page_offset = offset;
486 nres = fuse_send_write(req, file, inode, pos, count);
487 err = req->out.h.error;
488 fuse_put_request(fc, req);
489 if (!err && !nres)
490 err = -EIO;
491 if (!err) {
492 pos += nres;
493 spin_lock(&fc->lock);
494 if (pos > inode->i_size)
495 i_size_write(inode, pos);
496 spin_unlock(&fc->lock);
498 if (count == PAGE_CACHE_SIZE)
499 SetPageUptodate(page);
501 fuse_invalidate_attr(inode);
502 return err ? err : nres;
505 static int fuse_write_end(struct file *file, struct address_space *mapping,
506 loff_t pos, unsigned len, unsigned copied,
507 struct page *page, void *fsdata)
509 struct inode *inode = mapping->host;
510 int res = 0;
512 if (copied)
513 res = fuse_buffered_write(file, inode, pos, copied, page);
515 unlock_page(page);
516 page_cache_release(page);
517 return res;
520 static void fuse_release_user_pages(struct fuse_req *req, int write)
522 unsigned i;
524 for (i = 0; i < req->num_pages; i++) {
525 struct page *page = req->pages[i];
526 if (write)
527 set_page_dirty_lock(page);
528 put_page(page);
532 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
533 unsigned nbytes, int write)
535 unsigned long user_addr = (unsigned long) buf;
536 unsigned offset = user_addr & ~PAGE_MASK;
537 int npages;
539 /* This doesn't work with nfsd */
540 if (!current->mm)
541 return -EPERM;
543 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
544 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
545 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
546 down_read(&current->mm->mmap_sem);
547 npages = get_user_pages(current, current->mm, user_addr, npages, write,
548 0, req->pages, NULL);
549 up_read(&current->mm->mmap_sem);
550 if (npages < 0)
551 return npages;
553 req->num_pages = npages;
554 req->page_offset = offset;
555 return 0;
558 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
559 size_t count, loff_t *ppos, int write)
561 struct inode *inode = file->f_path.dentry->d_inode;
562 struct fuse_conn *fc = get_fuse_conn(inode);
563 size_t nmax = write ? fc->max_write : fc->max_read;
564 loff_t pos = *ppos;
565 ssize_t res = 0;
566 struct fuse_req *req;
568 if (is_bad_inode(inode))
569 return -EIO;
571 req = fuse_get_req(fc);
572 if (IS_ERR(req))
573 return PTR_ERR(req);
575 while (count) {
576 size_t nres;
577 size_t nbytes = min(count, nmax);
578 int err = fuse_get_user_pages(req, buf, nbytes, !write);
579 if (err) {
580 res = err;
581 break;
583 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
584 nbytes = min(count, nbytes);
585 if (write)
586 nres = fuse_send_write(req, file, inode, pos, nbytes);
587 else
588 nres = fuse_send_read(req, file, inode, pos, nbytes);
589 fuse_release_user_pages(req, !write);
590 if (req->out.h.error) {
591 if (!res)
592 res = req->out.h.error;
593 break;
594 } else if (nres > nbytes) {
595 res = -EIO;
596 break;
598 count -= nres;
599 res += nres;
600 pos += nres;
601 buf += nres;
602 if (nres != nbytes)
603 break;
604 if (count) {
605 fuse_put_request(fc, req);
606 req = fuse_get_req(fc);
607 if (IS_ERR(req))
608 break;
611 fuse_put_request(fc, req);
612 if (res > 0) {
613 if (write) {
614 spin_lock(&fc->lock);
615 if (pos > inode->i_size)
616 i_size_write(inode, pos);
617 spin_unlock(&fc->lock);
619 *ppos = pos;
621 fuse_invalidate_attr(inode);
623 return res;
626 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
627 size_t count, loff_t *ppos)
629 return fuse_direct_io(file, buf, count, ppos, 0);
632 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
633 size_t count, loff_t *ppos)
635 struct inode *inode = file->f_path.dentry->d_inode;
636 ssize_t res;
637 /* Don't allow parallel writes to the same file */
638 mutex_lock(&inode->i_mutex);
639 res = generic_write_checks(file, ppos, &count, 0);
640 if (!res)
641 res = fuse_direct_io(file, buf, count, ppos, 1);
642 mutex_unlock(&inode->i_mutex);
643 return res;
646 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
648 if ((vma->vm_flags & VM_SHARED)) {
649 if ((vma->vm_flags & VM_WRITE))
650 return -ENODEV;
651 else
652 vma->vm_flags &= ~VM_MAYWRITE;
654 return generic_file_mmap(file, vma);
657 static int fuse_set_page_dirty(struct page *page)
659 printk("fuse_set_page_dirty: should not happen\n");
660 dump_stack();
661 return 0;
664 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
665 struct file_lock *fl)
667 switch (ffl->type) {
668 case F_UNLCK:
669 break;
671 case F_RDLCK:
672 case F_WRLCK:
673 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
674 ffl->end < ffl->start)
675 return -EIO;
677 fl->fl_start = ffl->start;
678 fl->fl_end = ffl->end;
679 fl->fl_pid = ffl->pid;
680 break;
682 default:
683 return -EIO;
685 fl->fl_type = ffl->type;
686 return 0;
689 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
690 const struct file_lock *fl, int opcode, pid_t pid)
692 struct inode *inode = file->f_path.dentry->d_inode;
693 struct fuse_conn *fc = get_fuse_conn(inode);
694 struct fuse_file *ff = file->private_data;
695 struct fuse_lk_in *arg = &req->misc.lk_in;
697 arg->fh = ff->fh;
698 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
699 arg->lk.start = fl->fl_start;
700 arg->lk.end = fl->fl_end;
701 arg->lk.type = fl->fl_type;
702 arg->lk.pid = pid;
703 req->in.h.opcode = opcode;
704 req->in.h.nodeid = get_node_id(inode);
705 req->in.numargs = 1;
706 req->in.args[0].size = sizeof(*arg);
707 req->in.args[0].value = arg;
710 static int fuse_getlk(struct file *file, struct file_lock *fl)
712 struct inode *inode = file->f_path.dentry->d_inode;
713 struct fuse_conn *fc = get_fuse_conn(inode);
714 struct fuse_req *req;
715 struct fuse_lk_out outarg;
716 int err;
718 req = fuse_get_req(fc);
719 if (IS_ERR(req))
720 return PTR_ERR(req);
722 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0);
723 req->out.numargs = 1;
724 req->out.args[0].size = sizeof(outarg);
725 req->out.args[0].value = &outarg;
726 request_send(fc, req);
727 err = req->out.h.error;
728 fuse_put_request(fc, req);
729 if (!err)
730 err = convert_fuse_file_lock(&outarg.lk, fl);
732 return err;
735 static int fuse_setlk(struct file *file, struct file_lock *fl)
737 struct inode *inode = file->f_path.dentry->d_inode;
738 struct fuse_conn *fc = get_fuse_conn(inode);
739 struct fuse_req *req;
740 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
741 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
742 int err;
744 /* Unlock on close is handled by the flush method */
745 if (fl->fl_flags & FL_CLOSE)
746 return 0;
748 req = fuse_get_req(fc);
749 if (IS_ERR(req))
750 return PTR_ERR(req);
752 fuse_lk_fill(req, file, fl, opcode, pid);
753 request_send(fc, req);
754 err = req->out.h.error;
755 /* locking is restartable */
756 if (err == -EINTR)
757 err = -ERESTARTSYS;
758 fuse_put_request(fc, req);
759 return err;
762 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
764 struct inode *inode = file->f_path.dentry->d_inode;
765 struct fuse_conn *fc = get_fuse_conn(inode);
766 int err;
768 if (cmd == F_GETLK) {
769 if (fc->no_lock) {
770 posix_test_lock(file, fl);
771 err = 0;
772 } else
773 err = fuse_getlk(file, fl);
774 } else {
775 if (fc->no_lock)
776 err = posix_lock_file_wait(file, fl);
777 else
778 err = fuse_setlk(file, fl);
780 return err;
783 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
785 struct inode *inode = mapping->host;
786 struct fuse_conn *fc = get_fuse_conn(inode);
787 struct fuse_req *req;
788 struct fuse_bmap_in inarg;
789 struct fuse_bmap_out outarg;
790 int err;
792 if (!inode->i_sb->s_bdev || fc->no_bmap)
793 return 0;
795 req = fuse_get_req(fc);
796 if (IS_ERR(req))
797 return 0;
799 memset(&inarg, 0, sizeof(inarg));
800 inarg.block = block;
801 inarg.blocksize = inode->i_sb->s_blocksize;
802 req->in.h.opcode = FUSE_BMAP;
803 req->in.h.nodeid = get_node_id(inode);
804 req->in.numargs = 1;
805 req->in.args[0].size = sizeof(inarg);
806 req->in.args[0].value = &inarg;
807 req->out.numargs = 1;
808 req->out.args[0].size = sizeof(outarg);
809 req->out.args[0].value = &outarg;
810 request_send(fc, req);
811 err = req->out.h.error;
812 fuse_put_request(fc, req);
813 if (err == -ENOSYS)
814 fc->no_bmap = 1;
816 return err ? 0 : outarg.block;
819 static const struct file_operations fuse_file_operations = {
820 .llseek = generic_file_llseek,
821 .read = do_sync_read,
822 .aio_read = generic_file_aio_read,
823 .write = do_sync_write,
824 .aio_write = generic_file_aio_write,
825 .mmap = fuse_file_mmap,
826 .open = fuse_open,
827 .flush = fuse_flush,
828 .release = fuse_release,
829 .fsync = fuse_fsync,
830 .lock = fuse_file_lock,
831 .splice_read = generic_file_splice_read,
834 static const struct file_operations fuse_direct_io_file_operations = {
835 .llseek = generic_file_llseek,
836 .read = fuse_direct_read,
837 .write = fuse_direct_write,
838 .open = fuse_open,
839 .flush = fuse_flush,
840 .release = fuse_release,
841 .fsync = fuse_fsync,
842 .lock = fuse_file_lock,
843 /* no mmap and splice_read */
846 static const struct address_space_operations fuse_file_aops = {
847 .readpage = fuse_readpage,
848 .write_begin = fuse_write_begin,
849 .write_end = fuse_write_end,
850 .readpages = fuse_readpages,
851 .set_page_dirty = fuse_set_page_dirty,
852 .bmap = fuse_bmap,
855 void fuse_init_file_inode(struct inode *inode)
857 inode->i_fop = &fuse_file_operations;
858 inode->i_data.a_ops = &fuse_file_aops;