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.
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
;
26 req
= fuse_get_req(fc
);
30 memset(&inarg
, 0, sizeof(inarg
));
31 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
32 if (!fc
->atomic_o_trunc
)
33 inarg
.flags
&= ~O_TRUNC
;
34 req
->in
.h
.opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
35 req
->in
.h
.nodeid
= get_node_id(inode
);
37 req
->in
.args
[0].size
= sizeof(inarg
);
38 req
->in
.args
[0].value
= &inarg
;
40 req
->out
.args
[0].size
= sizeof(*outargp
);
41 req
->out
.args
[0].value
= outargp
;
42 request_send(fc
, req
);
43 err
= req
->out
.h
.error
;
44 fuse_put_request(fc
, req
);
49 struct fuse_file
*fuse_file_alloc(void)
52 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
54 ff
->reserved_req
= fuse_request_alloc();
55 if (!ff
->reserved_req
) {
59 INIT_LIST_HEAD(&ff
->write_entry
);
60 atomic_set(&ff
->count
, 0);
66 void fuse_file_free(struct fuse_file
*ff
)
68 fuse_request_free(ff
->reserved_req
);
72 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
74 atomic_inc(&ff
->count
);
78 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
80 dput(req
->misc
.release
.dentry
);
81 mntput(req
->misc
.release
.vfsmount
);
82 fuse_put_request(fc
, req
);
85 static void fuse_file_put(struct fuse_file
*ff
)
87 if (atomic_dec_and_test(&ff
->count
)) {
88 struct fuse_req
*req
= ff
->reserved_req
;
89 struct inode
*inode
= req
->misc
.release
.dentry
->d_inode
;
90 struct fuse_conn
*fc
= get_fuse_conn(inode
);
91 req
->end
= fuse_release_end
;
92 request_send_background(fc
, req
);
97 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
98 struct fuse_file
*ff
, struct fuse_open_out
*outarg
)
100 if (outarg
->open_flags
& FOPEN_DIRECT_IO
)
101 file
->f_op
= &fuse_direct_io_file_operations
;
102 if (!(outarg
->open_flags
& FOPEN_KEEP_CACHE
))
103 invalidate_inode_pages2(inode
->i_mapping
);
105 file
->private_data
= fuse_file_get(ff
);
108 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
)
110 struct fuse_open_out outarg
;
111 struct fuse_file
*ff
;
114 /* VFS checks this, but only _after_ ->open() */
115 if (file
->f_flags
& O_DIRECT
)
118 err
= generic_file_open(inode
, file
);
122 ff
= fuse_file_alloc();
126 err
= fuse_send_open(inode
, file
, isdir
, &outarg
);
131 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
132 fuse_finish_open(inode
, file
, ff
, &outarg
);
138 void fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
, int opcode
)
140 struct fuse_req
*req
= ff
->reserved_req
;
141 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
144 inarg
->flags
= flags
;
145 req
->in
.h
.opcode
= opcode
;
146 req
->in
.h
.nodeid
= nodeid
;
148 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
149 req
->in
.args
[0].value
= inarg
;
152 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
)
154 struct fuse_file
*ff
= file
->private_data
;
156 struct fuse_conn
*fc
= get_fuse_conn(inode
);
157 struct fuse_req
*req
= ff
->reserved_req
;
159 fuse_release_fill(ff
, get_node_id(inode
), file
->f_flags
,
160 isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
);
162 /* Hold vfsmount and dentry until release is finished */
163 req
->misc
.release
.vfsmount
= mntget(file
->f_path
.mnt
);
164 req
->misc
.release
.dentry
= dget(file
->f_path
.dentry
);
166 spin_lock(&fc
->lock
);
167 list_del(&ff
->write_entry
);
168 spin_unlock(&fc
->lock
);
170 * Normally this will send the RELEASE request,
171 * however if some asynchronous READ or WRITE requests
172 * are outstanding, the sending will be delayed
177 /* Return value is ignored by VFS */
181 static int fuse_open(struct inode
*inode
, struct file
*file
)
183 return fuse_open_common(inode
, file
, 0);
186 static int fuse_release(struct inode
*inode
, struct file
*file
)
188 return fuse_release_common(inode
, file
, 0);
192 * Scramble the ID space with XTEA, so that the value of the files_struct
193 * pointer is not exposed to userspace.
195 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
197 u32
*k
= fc
->scramble_key
;
198 u64 v
= (unsigned long) id
;
204 for (i
= 0; i
< 32; i
++) {
205 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
207 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
210 return (u64
) v0
+ ((u64
) v1
<< 32);
213 static int fuse_flush(struct file
*file
, fl_owner_t id
)
215 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
216 struct fuse_conn
*fc
= get_fuse_conn(inode
);
217 struct fuse_file
*ff
= file
->private_data
;
218 struct fuse_req
*req
;
219 struct fuse_flush_in inarg
;
222 if (is_bad_inode(inode
))
228 req
= fuse_get_req_nofail(fc
, file
);
229 memset(&inarg
, 0, sizeof(inarg
));
231 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
232 req
->in
.h
.opcode
= FUSE_FLUSH
;
233 req
->in
.h
.nodeid
= get_node_id(inode
);
235 req
->in
.args
[0].size
= sizeof(inarg
);
236 req
->in
.args
[0].value
= &inarg
;
238 request_send(fc
, req
);
239 err
= req
->out
.h
.error
;
240 fuse_put_request(fc
, req
);
241 if (err
== -ENOSYS
) {
248 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
251 struct inode
*inode
= de
->d_inode
;
252 struct fuse_conn
*fc
= get_fuse_conn(inode
);
253 struct fuse_file
*ff
= file
->private_data
;
254 struct fuse_req
*req
;
255 struct fuse_fsync_in inarg
;
258 if (is_bad_inode(inode
))
261 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
264 req
= fuse_get_req(fc
);
268 memset(&inarg
, 0, sizeof(inarg
));
270 inarg
.fsync_flags
= datasync
? 1 : 0;
271 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
272 req
->in
.h
.nodeid
= get_node_id(inode
);
274 req
->in
.args
[0].size
= sizeof(inarg
);
275 req
->in
.args
[0].value
= &inarg
;
276 request_send(fc
, req
);
277 err
= req
->out
.h
.error
;
278 fuse_put_request(fc
, req
);
279 if (err
== -ENOSYS
) {
289 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
291 return fuse_fsync_common(file
, de
, datasync
, 0);
294 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
295 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
)
297 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
298 struct fuse_file
*ff
= file
->private_data
;
303 inarg
->flags
= file
->f_flags
;
304 req
->in
.h
.opcode
= opcode
;
305 req
->in
.h
.nodeid
= get_node_id(inode
);
307 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
308 req
->in
.args
[0].value
= inarg
;
309 req
->out
.argpages
= 1;
311 req
->out
.numargs
= 1;
312 req
->out
.args
[0].size
= count
;
315 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
316 struct inode
*inode
, loff_t pos
, size_t count
,
319 struct fuse_conn
*fc
= get_fuse_conn(inode
);
321 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
323 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
325 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
326 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
328 request_send(fc
, req
);
329 return req
->out
.args
[0].size
;
332 static int fuse_readpage(struct file
*file
, struct page
*page
)
334 struct inode
*inode
= page
->mapping
->host
;
335 struct fuse_conn
*fc
= get_fuse_conn(inode
);
336 struct fuse_req
*req
;
340 if (is_bad_inode(inode
))
343 req
= fuse_get_req(fc
);
348 req
->out
.page_zeroing
= 1;
350 req
->pages
[0] = page
;
351 fuse_send_read(req
, file
, inode
, page_offset(page
), PAGE_CACHE_SIZE
,
353 err
= req
->out
.h
.error
;
354 fuse_put_request(fc
, req
);
356 SetPageUptodate(page
);
357 fuse_invalidate_attr(inode
); /* atime changed */
363 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
367 fuse_invalidate_attr(req
->pages
[0]->mapping
->host
); /* atime changed */
369 for (i
= 0; i
< req
->num_pages
; i
++) {
370 struct page
*page
= req
->pages
[i
];
371 if (!req
->out
.h
.error
)
372 SetPageUptodate(page
);
378 fuse_file_put(req
->ff
);
379 fuse_put_request(fc
, req
);
382 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
,
385 struct fuse_conn
*fc
= get_fuse_conn(inode
);
386 loff_t pos
= page_offset(req
->pages
[0]);
387 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
388 req
->out
.page_zeroing
= 1;
389 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
390 if (fc
->async_read
) {
391 struct fuse_file
*ff
= file
->private_data
;
392 req
->ff
= fuse_file_get(ff
);
393 req
->end
= fuse_readpages_end
;
394 request_send_background(fc
, req
);
396 request_send(fc
, req
);
397 fuse_readpages_end(fc
, req
);
401 struct fuse_fill_data
{
402 struct fuse_req
*req
;
407 static int fuse_readpages_fill(void *_data
, struct page
*page
)
409 struct fuse_fill_data
*data
= _data
;
410 struct fuse_req
*req
= data
->req
;
411 struct inode
*inode
= data
->inode
;
412 struct fuse_conn
*fc
= get_fuse_conn(inode
);
414 if (req
->num_pages
&&
415 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
416 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
417 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
418 fuse_send_readpages(req
, data
->file
, inode
);
419 data
->req
= req
= fuse_get_req(fc
);
425 req
->pages
[req
->num_pages
] = page
;
430 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
431 struct list_head
*pages
, unsigned nr_pages
)
433 struct inode
*inode
= mapping
->host
;
434 struct fuse_conn
*fc
= get_fuse_conn(inode
);
435 struct fuse_fill_data data
;
439 if (is_bad_inode(inode
))
444 data
.req
= fuse_get_req(fc
);
445 err
= PTR_ERR(data
.req
);
446 if (IS_ERR(data
.req
))
449 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
451 if (data
.req
->num_pages
)
452 fuse_send_readpages(data
.req
, file
, inode
);
454 fuse_put_request(fc
, data
.req
);
460 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
461 unsigned long nr_segs
, loff_t pos
)
463 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
465 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
468 * If trying to read past EOF, make sure the i_size
469 * attribute is up-to-date.
471 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
476 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
479 static void fuse_write_fill(struct fuse_req
*req
, struct file
*file
,
480 struct inode
*inode
, loff_t pos
, size_t count
,
483 struct fuse_conn
*fc
= get_fuse_conn(inode
);
484 struct fuse_file
*ff
= file
->private_data
;
485 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
486 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
488 memset(inarg
, 0, sizeof(struct fuse_write_in
));
492 inarg
->write_flags
= writepage
? FUSE_WRITE_CACHE
: 0;
493 inarg
->flags
= file
->f_flags
;
494 req
->in
.h
.opcode
= FUSE_WRITE
;
495 req
->in
.h
.nodeid
= get_node_id(inode
);
496 req
->in
.argpages
= 1;
499 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
501 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
502 req
->in
.args
[0].value
= inarg
;
503 req
->in
.args
[1].size
= count
;
504 req
->out
.numargs
= 1;
505 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
506 req
->out
.args
[0].value
= outarg
;
509 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
510 struct inode
*inode
, loff_t pos
, size_t count
,
513 struct fuse_conn
*fc
= get_fuse_conn(inode
);
514 fuse_write_fill(req
, file
, inode
, pos
, count
, 0);
516 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
517 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
518 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
520 request_send(fc
, req
);
521 return req
->misc
.write
.out
.size
;
524 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
525 loff_t pos
, unsigned len
, unsigned flags
,
526 struct page
**pagep
, void **fsdata
)
528 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
530 *pagep
= __grab_cache_page(mapping
, index
);
536 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
537 loff_t pos
, unsigned count
, struct page
*page
)
541 struct fuse_conn
*fc
= get_fuse_conn(inode
);
542 struct fuse_inode
*fi
= get_fuse_inode(inode
);
543 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
544 struct fuse_req
*req
;
546 if (is_bad_inode(inode
))
549 req
= fuse_get_req(fc
);
554 req
->pages
[0] = page
;
555 req
->page_offset
= offset
;
556 nres
= fuse_send_write(req
, file
, inode
, pos
, count
, NULL
);
557 err
= req
->out
.h
.error
;
558 fuse_put_request(fc
, req
);
563 spin_lock(&fc
->lock
);
564 fi
->attr_version
= ++fc
->attr_version
;
565 if (pos
> inode
->i_size
)
566 i_size_write(inode
, pos
);
567 spin_unlock(&fc
->lock
);
569 if (count
== PAGE_CACHE_SIZE
)
570 SetPageUptodate(page
);
572 fuse_invalidate_attr(inode
);
573 return err
? err
: nres
;
576 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
577 loff_t pos
, unsigned len
, unsigned copied
,
578 struct page
*page
, void *fsdata
)
580 struct inode
*inode
= mapping
->host
;
584 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
587 page_cache_release(page
);
591 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
595 for (i
= 0; i
< req
->num_pages
; i
++) {
596 struct page
*page
= req
->pages
[i
];
598 set_page_dirty_lock(page
);
603 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
604 unsigned nbytes
, int write
)
606 unsigned long user_addr
= (unsigned long) buf
;
607 unsigned offset
= user_addr
& ~PAGE_MASK
;
610 /* This doesn't work with nfsd */
614 nbytes
= min(nbytes
, (unsigned) FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
615 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
616 npages
= min(max(npages
, 1), FUSE_MAX_PAGES_PER_REQ
);
617 down_read(¤t
->mm
->mmap_sem
);
618 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, write
,
619 0, req
->pages
, NULL
);
620 up_read(¤t
->mm
->mmap_sem
);
624 req
->num_pages
= npages
;
625 req
->page_offset
= offset
;
629 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
630 size_t count
, loff_t
*ppos
, int write
)
632 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
633 struct fuse_conn
*fc
= get_fuse_conn(inode
);
634 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
637 struct fuse_req
*req
;
639 if (is_bad_inode(inode
))
642 req
= fuse_get_req(fc
);
648 size_t nbytes
= min(count
, nmax
);
649 int err
= fuse_get_user_pages(req
, buf
, nbytes
, !write
);
654 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
655 nbytes
= min(count
, nbytes
);
657 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
,
660 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
,
662 fuse_release_user_pages(req
, !write
);
663 if (req
->out
.h
.error
) {
665 res
= req
->out
.h
.error
;
667 } else if (nres
> nbytes
) {
678 fuse_put_request(fc
, req
);
679 req
= fuse_get_req(fc
);
684 fuse_put_request(fc
, req
);
687 spin_lock(&fc
->lock
);
688 if (pos
> inode
->i_size
)
689 i_size_write(inode
, pos
);
690 spin_unlock(&fc
->lock
);
694 fuse_invalidate_attr(inode
);
699 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
700 size_t count
, loff_t
*ppos
)
702 return fuse_direct_io(file
, buf
, count
, ppos
, 0);
705 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
706 size_t count
, loff_t
*ppos
)
708 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
710 /* Don't allow parallel writes to the same file */
711 mutex_lock(&inode
->i_mutex
);
712 res
= generic_write_checks(file
, ppos
, &count
, 0);
714 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
715 mutex_unlock(&inode
->i_mutex
);
719 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
721 if ((vma
->vm_flags
& VM_SHARED
)) {
722 if ((vma
->vm_flags
& VM_WRITE
))
725 vma
->vm_flags
&= ~VM_MAYWRITE
;
727 return generic_file_mmap(file
, vma
);
730 static int fuse_set_page_dirty(struct page
*page
)
732 printk("fuse_set_page_dirty: should not happen\n");
737 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
738 struct file_lock
*fl
)
746 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
747 ffl
->end
< ffl
->start
)
750 fl
->fl_start
= ffl
->start
;
751 fl
->fl_end
= ffl
->end
;
752 fl
->fl_pid
= ffl
->pid
;
758 fl
->fl_type
= ffl
->type
;
762 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
763 const struct file_lock
*fl
, int opcode
, pid_t pid
,
766 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
767 struct fuse_conn
*fc
= get_fuse_conn(inode
);
768 struct fuse_file
*ff
= file
->private_data
;
769 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
772 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
773 arg
->lk
.start
= fl
->fl_start
;
774 arg
->lk
.end
= fl
->fl_end
;
775 arg
->lk
.type
= fl
->fl_type
;
778 arg
->lk_flags
|= FUSE_LK_FLOCK
;
779 req
->in
.h
.opcode
= opcode
;
780 req
->in
.h
.nodeid
= get_node_id(inode
);
782 req
->in
.args
[0].size
= sizeof(*arg
);
783 req
->in
.args
[0].value
= arg
;
786 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
788 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
789 struct fuse_conn
*fc
= get_fuse_conn(inode
);
790 struct fuse_req
*req
;
791 struct fuse_lk_out outarg
;
794 req
= fuse_get_req(fc
);
798 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
799 req
->out
.numargs
= 1;
800 req
->out
.args
[0].size
= sizeof(outarg
);
801 req
->out
.args
[0].value
= &outarg
;
802 request_send(fc
, req
);
803 err
= req
->out
.h
.error
;
804 fuse_put_request(fc
, req
);
806 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
811 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
813 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
814 struct fuse_conn
*fc
= get_fuse_conn(inode
);
815 struct fuse_req
*req
;
816 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
817 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
820 /* Unlock on close is handled by the flush method */
821 if (fl
->fl_flags
& FL_CLOSE
)
824 req
= fuse_get_req(fc
);
828 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
829 request_send(fc
, req
);
830 err
= req
->out
.h
.error
;
831 /* locking is restartable */
834 fuse_put_request(fc
, req
);
838 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
840 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
841 struct fuse_conn
*fc
= get_fuse_conn(inode
);
844 if (cmd
== F_GETLK
) {
846 posix_test_lock(file
, fl
);
849 err
= fuse_getlk(file
, fl
);
852 err
= posix_lock_file_wait(file
, fl
);
854 err
= fuse_setlk(file
, fl
, 0);
859 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
861 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
862 struct fuse_conn
*fc
= get_fuse_conn(inode
);
866 err
= flock_lock_file_wait(file
, fl
);
868 /* emulate flock with POSIX locks */
869 fl
->fl_owner
= (fl_owner_t
) file
;
870 err
= fuse_setlk(file
, fl
, 1);
876 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
878 struct inode
*inode
= mapping
->host
;
879 struct fuse_conn
*fc
= get_fuse_conn(inode
);
880 struct fuse_req
*req
;
881 struct fuse_bmap_in inarg
;
882 struct fuse_bmap_out outarg
;
885 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
888 req
= fuse_get_req(fc
);
892 memset(&inarg
, 0, sizeof(inarg
));
894 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
895 req
->in
.h
.opcode
= FUSE_BMAP
;
896 req
->in
.h
.nodeid
= get_node_id(inode
);
898 req
->in
.args
[0].size
= sizeof(inarg
);
899 req
->in
.args
[0].value
= &inarg
;
900 req
->out
.numargs
= 1;
901 req
->out
.args
[0].size
= sizeof(outarg
);
902 req
->out
.args
[0].value
= &outarg
;
903 request_send(fc
, req
);
904 err
= req
->out
.h
.error
;
905 fuse_put_request(fc
, req
);
909 return err
? 0 : outarg
.block
;
912 static const struct file_operations fuse_file_operations
= {
913 .llseek
= generic_file_llseek
,
914 .read
= do_sync_read
,
915 .aio_read
= fuse_file_aio_read
,
916 .write
= do_sync_write
,
917 .aio_write
= generic_file_aio_write
,
918 .mmap
= fuse_file_mmap
,
921 .release
= fuse_release
,
923 .lock
= fuse_file_lock
,
924 .flock
= fuse_file_flock
,
925 .splice_read
= generic_file_splice_read
,
928 static const struct file_operations fuse_direct_io_file_operations
= {
929 .llseek
= generic_file_llseek
,
930 .read
= fuse_direct_read
,
931 .write
= fuse_direct_write
,
934 .release
= fuse_release
,
936 .lock
= fuse_file_lock
,
937 .flock
= fuse_file_flock
,
938 /* no mmap and splice_read */
941 static const struct address_space_operations fuse_file_aops
= {
942 .readpage
= fuse_readpage
,
943 .write_begin
= fuse_write_begin
,
944 .write_end
= fuse_write_end
,
945 .readpages
= fuse_readpages
,
946 .set_page_dirty
= fuse_set_page_dirty
,
950 void fuse_init_file_inode(struct inode
*inode
)
952 inode
->i_fop
= &fuse_file_operations
;
953 inode
->i_data
.a_ops
= &fuse_file_aops
;