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);
65 void fuse_file_free(struct fuse_file
*ff
)
67 fuse_request_free(ff
->reserved_req
);
71 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
73 atomic_inc(&ff
->count
);
77 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
80 mntput(req
->vfsmount
);
81 fuse_put_request(fc
, req
);
84 static void fuse_file_put(struct fuse_file
*ff
)
86 if (atomic_dec_and_test(&ff
->count
)) {
87 struct fuse_req
*req
= ff
->reserved_req
;
88 struct fuse_conn
*fc
= get_fuse_conn(req
->dentry
->d_inode
);
89 req
->end
= fuse_release_end
;
90 request_send_background(fc
, req
);
95 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
96 struct fuse_file
*ff
, struct fuse_open_out
*outarg
)
98 if (outarg
->open_flags
& FOPEN_DIRECT_IO
)
99 file
->f_op
= &fuse_direct_io_file_operations
;
100 if (!(outarg
->open_flags
& FOPEN_KEEP_CACHE
))
101 invalidate_inode_pages2(inode
->i_mapping
);
103 file
->private_data
= fuse_file_get(ff
);
106 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
)
108 struct fuse_open_out outarg
;
109 struct fuse_file
*ff
;
112 /* VFS checks this, but only _after_ ->open() */
113 if (file
->f_flags
& O_DIRECT
)
116 err
= generic_file_open(inode
, file
);
120 ff
= fuse_file_alloc();
124 err
= fuse_send_open(inode
, file
, isdir
, &outarg
);
129 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
130 fuse_finish_open(inode
, file
, ff
, &outarg
);
136 void fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
, int opcode
)
138 struct fuse_req
*req
= ff
->reserved_req
;
139 struct fuse_release_in
*inarg
= &req
->misc
.release_in
;
142 inarg
->flags
= flags
;
143 req
->in
.h
.opcode
= opcode
;
144 req
->in
.h
.nodeid
= nodeid
;
146 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
147 req
->in
.args
[0].value
= inarg
;
150 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
)
152 struct fuse_file
*ff
= file
->private_data
;
154 struct fuse_conn
*fc
= get_fuse_conn(inode
);
156 fuse_release_fill(ff
, get_node_id(inode
), file
->f_flags
,
157 isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
);
159 /* Hold vfsmount and dentry until release is finished */
160 ff
->reserved_req
->vfsmount
= mntget(file
->f_path
.mnt
);
161 ff
->reserved_req
->dentry
= dget(file
->f_path
.dentry
);
163 spin_lock(&fc
->lock
);
164 list_del(&ff
->write_entry
);
165 spin_unlock(&fc
->lock
);
167 * Normally this will send the RELEASE request,
168 * however if some asynchronous READ or WRITE requests
169 * are outstanding, the sending will be delayed
174 /* Return value is ignored by VFS */
178 static int fuse_open(struct inode
*inode
, struct file
*file
)
180 return fuse_open_common(inode
, file
, 0);
183 static int fuse_release(struct inode
*inode
, struct file
*file
)
185 return fuse_release_common(inode
, file
, 0);
189 * Scramble the ID space with XTEA, so that the value of the files_struct
190 * pointer is not exposed to userspace.
192 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
194 u32
*k
= fc
->scramble_key
;
195 u64 v
= (unsigned long) id
;
201 for (i
= 0; i
< 32; i
++) {
202 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
204 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
207 return (u64
) v0
+ ((u64
) v1
<< 32);
210 static int fuse_flush(struct file
*file
, fl_owner_t id
)
212 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
213 struct fuse_conn
*fc
= get_fuse_conn(inode
);
214 struct fuse_file
*ff
= file
->private_data
;
215 struct fuse_req
*req
;
216 struct fuse_flush_in inarg
;
219 if (is_bad_inode(inode
))
225 req
= fuse_get_req_nofail(fc
, file
);
226 memset(&inarg
, 0, sizeof(inarg
));
228 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
229 req
->in
.h
.opcode
= FUSE_FLUSH
;
230 req
->in
.h
.nodeid
= get_node_id(inode
);
232 req
->in
.args
[0].size
= sizeof(inarg
);
233 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
) {
245 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
248 struct inode
*inode
= de
->d_inode
;
249 struct fuse_conn
*fc
= get_fuse_conn(inode
);
250 struct fuse_file
*ff
= file
->private_data
;
251 struct fuse_req
*req
;
252 struct fuse_fsync_in inarg
;
255 if (is_bad_inode(inode
))
258 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
261 req
= fuse_get_req(fc
);
265 memset(&inarg
, 0, sizeof(inarg
));
267 inarg
.fsync_flags
= datasync
? 1 : 0;
268 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
269 req
->in
.h
.nodeid
= get_node_id(inode
);
271 req
->in
.args
[0].size
= sizeof(inarg
);
272 req
->in
.args
[0].value
= &inarg
;
273 request_send(fc
, req
);
274 err
= req
->out
.h
.error
;
275 fuse_put_request(fc
, req
);
276 if (err
== -ENOSYS
) {
286 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
288 return fuse_fsync_common(file
, de
, datasync
, 0);
291 void fuse_read_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
292 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
)
294 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
299 req
->in
.h
.opcode
= opcode
;
300 req
->in
.h
.nodeid
= get_node_id(inode
);
302 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
303 req
->in
.args
[0].value
= inarg
;
304 req
->out
.argpages
= 1;
306 req
->out
.numargs
= 1;
307 req
->out
.args
[0].size
= count
;
310 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
311 struct inode
*inode
, loff_t pos
, size_t count
,
314 struct fuse_conn
*fc
= get_fuse_conn(inode
);
315 struct fuse_file
*ff
= file
->private_data
;
317 fuse_read_fill(req
, ff
, inode
, pos
, count
, FUSE_READ
);
319 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
321 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
322 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
324 request_send(fc
, req
);
325 return req
->out
.args
[0].size
;
328 static int fuse_readpage(struct file
*file
, struct page
*page
)
330 struct inode
*inode
= page
->mapping
->host
;
331 struct fuse_conn
*fc
= get_fuse_conn(inode
);
332 struct fuse_req
*req
;
336 if (is_bad_inode(inode
))
339 req
= fuse_get_req(fc
);
344 req
->out
.page_zeroing
= 1;
346 req
->pages
[0] = page
;
347 fuse_send_read(req
, file
, inode
, page_offset(page
), PAGE_CACHE_SIZE
,
349 err
= req
->out
.h
.error
;
350 fuse_put_request(fc
, req
);
352 SetPageUptodate(page
);
353 fuse_invalidate_attr(inode
); /* atime changed */
359 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
363 fuse_invalidate_attr(req
->pages
[0]->mapping
->host
); /* atime changed */
365 for (i
= 0; i
< req
->num_pages
; i
++) {
366 struct page
*page
= req
->pages
[i
];
367 if (!req
->out
.h
.error
)
368 SetPageUptodate(page
);
374 fuse_file_put(req
->ff
);
375 fuse_put_request(fc
, req
);
378 static void fuse_send_readpages(struct fuse_req
*req
, struct fuse_file
*ff
,
381 struct fuse_conn
*fc
= get_fuse_conn(inode
);
382 loff_t pos
= page_offset(req
->pages
[0]);
383 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
384 req
->out
.page_zeroing
= 1;
385 fuse_read_fill(req
, ff
, inode
, pos
, count
, FUSE_READ
);
386 if (fc
->async_read
) {
387 req
->ff
= fuse_file_get(ff
);
388 req
->end
= fuse_readpages_end
;
389 request_send_background(fc
, req
);
391 request_send(fc
, req
);
392 fuse_readpages_end(fc
, req
);
396 struct fuse_fill_data
{
397 struct fuse_req
*req
;
398 struct fuse_file
*ff
;
402 static int fuse_readpages_fill(void *_data
, struct page
*page
)
404 struct fuse_fill_data
*data
= _data
;
405 struct fuse_req
*req
= data
->req
;
406 struct inode
*inode
= data
->inode
;
407 struct fuse_conn
*fc
= get_fuse_conn(inode
);
409 if (req
->num_pages
&&
410 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
411 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
412 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
413 fuse_send_readpages(req
, data
->ff
, inode
);
414 data
->req
= req
= fuse_get_req(fc
);
420 req
->pages
[req
->num_pages
] = page
;
425 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
426 struct list_head
*pages
, unsigned nr_pages
)
428 struct inode
*inode
= mapping
->host
;
429 struct fuse_conn
*fc
= get_fuse_conn(inode
);
430 struct fuse_fill_data data
;
434 if (is_bad_inode(inode
))
437 data
.ff
= file
->private_data
;
439 data
.req
= fuse_get_req(fc
);
440 err
= PTR_ERR(data
.req
);
441 if (IS_ERR(data
.req
))
444 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
446 if (data
.req
->num_pages
)
447 fuse_send_readpages(data
.req
, data
.ff
, inode
);
449 fuse_put_request(fc
, data
.req
);
455 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
456 struct inode
*inode
, loff_t pos
, size_t count
,
459 struct fuse_conn
*fc
= get_fuse_conn(inode
);
460 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
461 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
463 memset(inarg
, 0, sizeof(struct fuse_write_in
));
467 inarg
->write_flags
= writepage
? FUSE_WRITE_CACHE
: 0;
468 req
->in
.h
.opcode
= FUSE_WRITE
;
469 req
->in
.h
.nodeid
= get_node_id(inode
);
470 req
->in
.argpages
= 1;
473 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
475 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
476 req
->in
.args
[0].value
= inarg
;
477 req
->in
.args
[1].size
= count
;
478 req
->out
.numargs
= 1;
479 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
480 req
->out
.args
[0].value
= outarg
;
483 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
484 struct inode
*inode
, loff_t pos
, size_t count
,
487 struct fuse_conn
*fc
= get_fuse_conn(inode
);
488 fuse_write_fill(req
, file
->private_data
, inode
, pos
, count
, 0);
490 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
491 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
492 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
494 request_send(fc
, req
);
495 return req
->misc
.write
.out
.size
;
498 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
499 loff_t pos
, unsigned len
, unsigned flags
,
500 struct page
**pagep
, void **fsdata
)
502 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
504 *pagep
= __grab_cache_page(mapping
, index
);
510 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
511 loff_t pos
, unsigned count
, struct page
*page
)
515 struct fuse_conn
*fc
= get_fuse_conn(inode
);
516 struct fuse_inode
*fi
= get_fuse_inode(inode
);
517 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
518 struct fuse_req
*req
;
520 if (is_bad_inode(inode
))
523 req
= fuse_get_req(fc
);
528 req
->pages
[0] = page
;
529 req
->page_offset
= offset
;
530 nres
= fuse_send_write(req
, file
, inode
, pos
, count
, NULL
);
531 err
= req
->out
.h
.error
;
532 fuse_put_request(fc
, req
);
537 spin_lock(&fc
->lock
);
538 fi
->attr_version
= ++fc
->attr_version
;
539 if (pos
> inode
->i_size
)
540 i_size_write(inode
, pos
);
541 spin_unlock(&fc
->lock
);
543 if (count
== PAGE_CACHE_SIZE
)
544 SetPageUptodate(page
);
546 fuse_invalidate_attr(inode
);
547 return err
? err
: nres
;
550 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
551 loff_t pos
, unsigned len
, unsigned copied
,
552 struct page
*page
, void *fsdata
)
554 struct inode
*inode
= mapping
->host
;
558 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
561 page_cache_release(page
);
565 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
569 for (i
= 0; i
< req
->num_pages
; i
++) {
570 struct page
*page
= req
->pages
[i
];
572 set_page_dirty_lock(page
);
577 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
578 unsigned nbytes
, int write
)
580 unsigned long user_addr
= (unsigned long) buf
;
581 unsigned offset
= user_addr
& ~PAGE_MASK
;
584 /* This doesn't work with nfsd */
588 nbytes
= min(nbytes
, (unsigned) FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
589 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
590 npages
= min(max(npages
, 1), FUSE_MAX_PAGES_PER_REQ
);
591 down_read(¤t
->mm
->mmap_sem
);
592 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, write
,
593 0, req
->pages
, NULL
);
594 up_read(¤t
->mm
->mmap_sem
);
598 req
->num_pages
= npages
;
599 req
->page_offset
= offset
;
603 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
604 size_t count
, loff_t
*ppos
, int write
)
606 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
607 struct fuse_conn
*fc
= get_fuse_conn(inode
);
608 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
611 struct fuse_req
*req
;
613 if (is_bad_inode(inode
))
616 req
= fuse_get_req(fc
);
622 size_t nbytes
= min(count
, nmax
);
623 int err
= fuse_get_user_pages(req
, buf
, nbytes
, !write
);
628 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
629 nbytes
= min(count
, nbytes
);
631 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
,
634 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
,
636 fuse_release_user_pages(req
, !write
);
637 if (req
->out
.h
.error
) {
639 res
= req
->out
.h
.error
;
641 } else if (nres
> nbytes
) {
652 fuse_put_request(fc
, req
);
653 req
= fuse_get_req(fc
);
658 fuse_put_request(fc
, req
);
661 spin_lock(&fc
->lock
);
662 if (pos
> inode
->i_size
)
663 i_size_write(inode
, pos
);
664 spin_unlock(&fc
->lock
);
668 fuse_invalidate_attr(inode
);
673 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
674 size_t count
, loff_t
*ppos
)
676 return fuse_direct_io(file
, buf
, count
, ppos
, 0);
679 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
680 size_t count
, loff_t
*ppos
)
682 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
684 /* Don't allow parallel writes to the same file */
685 mutex_lock(&inode
->i_mutex
);
686 res
= generic_write_checks(file
, ppos
, &count
, 0);
688 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
689 mutex_unlock(&inode
->i_mutex
);
693 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
695 if ((vma
->vm_flags
& VM_SHARED
)) {
696 if ((vma
->vm_flags
& VM_WRITE
))
699 vma
->vm_flags
&= ~VM_MAYWRITE
;
701 return generic_file_mmap(file
, vma
);
704 static int fuse_set_page_dirty(struct page
*page
)
706 printk("fuse_set_page_dirty: should not happen\n");
711 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
712 struct file_lock
*fl
)
720 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
721 ffl
->end
< ffl
->start
)
724 fl
->fl_start
= ffl
->start
;
725 fl
->fl_end
= ffl
->end
;
726 fl
->fl_pid
= ffl
->pid
;
732 fl
->fl_type
= ffl
->type
;
736 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
737 const struct file_lock
*fl
, int opcode
, pid_t pid
,
740 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
741 struct fuse_conn
*fc
= get_fuse_conn(inode
);
742 struct fuse_file
*ff
= file
->private_data
;
743 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
746 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
747 arg
->lk
.start
= fl
->fl_start
;
748 arg
->lk
.end
= fl
->fl_end
;
749 arg
->lk
.type
= fl
->fl_type
;
752 arg
->lk_flags
|= FUSE_LK_FLOCK
;
753 req
->in
.h
.opcode
= opcode
;
754 req
->in
.h
.nodeid
= get_node_id(inode
);
756 req
->in
.args
[0].size
= sizeof(*arg
);
757 req
->in
.args
[0].value
= arg
;
760 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
762 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
763 struct fuse_conn
*fc
= get_fuse_conn(inode
);
764 struct fuse_req
*req
;
765 struct fuse_lk_out outarg
;
768 req
= fuse_get_req(fc
);
772 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
773 req
->out
.numargs
= 1;
774 req
->out
.args
[0].size
= sizeof(outarg
);
775 req
->out
.args
[0].value
= &outarg
;
776 request_send(fc
, req
);
777 err
= req
->out
.h
.error
;
778 fuse_put_request(fc
, req
);
780 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
785 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
787 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
788 struct fuse_conn
*fc
= get_fuse_conn(inode
);
789 struct fuse_req
*req
;
790 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
791 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
794 /* Unlock on close is handled by the flush method */
795 if (fl
->fl_flags
& FL_CLOSE
)
798 req
= fuse_get_req(fc
);
802 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
803 request_send(fc
, req
);
804 err
= req
->out
.h
.error
;
805 /* locking is restartable */
808 fuse_put_request(fc
, req
);
812 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
814 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
815 struct fuse_conn
*fc
= get_fuse_conn(inode
);
818 if (cmd
== F_GETLK
) {
820 posix_test_lock(file
, fl
);
823 err
= fuse_getlk(file
, fl
);
826 err
= posix_lock_file_wait(file
, fl
);
828 err
= fuse_setlk(file
, fl
, 0);
833 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
835 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
836 struct fuse_conn
*fc
= get_fuse_conn(inode
);
840 err
= flock_lock_file_wait(file
, fl
);
842 /* emulate flock with POSIX locks */
843 fl
->fl_owner
= (fl_owner_t
) file
;
844 err
= fuse_setlk(file
, fl
, 1);
850 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
852 struct inode
*inode
= mapping
->host
;
853 struct fuse_conn
*fc
= get_fuse_conn(inode
);
854 struct fuse_req
*req
;
855 struct fuse_bmap_in inarg
;
856 struct fuse_bmap_out outarg
;
859 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
862 req
= fuse_get_req(fc
);
866 memset(&inarg
, 0, sizeof(inarg
));
868 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
869 req
->in
.h
.opcode
= FUSE_BMAP
;
870 req
->in
.h
.nodeid
= get_node_id(inode
);
872 req
->in
.args
[0].size
= sizeof(inarg
);
873 req
->in
.args
[0].value
= &inarg
;
874 req
->out
.numargs
= 1;
875 req
->out
.args
[0].size
= sizeof(outarg
);
876 req
->out
.args
[0].value
= &outarg
;
877 request_send(fc
, req
);
878 err
= req
->out
.h
.error
;
879 fuse_put_request(fc
, req
);
883 return err
? 0 : outarg
.block
;
886 static const struct file_operations fuse_file_operations
= {
887 .llseek
= generic_file_llseek
,
888 .read
= do_sync_read
,
889 .aio_read
= generic_file_aio_read
,
890 .write
= do_sync_write
,
891 .aio_write
= generic_file_aio_write
,
892 .mmap
= fuse_file_mmap
,
895 .release
= fuse_release
,
897 .lock
= fuse_file_lock
,
898 .flock
= fuse_file_flock
,
899 .splice_read
= generic_file_splice_read
,
902 static const struct file_operations fuse_direct_io_file_operations
= {
903 .llseek
= generic_file_llseek
,
904 .read
= fuse_direct_read
,
905 .write
= fuse_direct_write
,
908 .release
= fuse_release
,
910 .lock
= fuse_file_lock
,
911 .flock
= fuse_file_flock
,
912 /* no mmap and splice_read */
915 static const struct address_space_operations fuse_file_aops
= {
916 .readpage
= fuse_readpage
,
917 .write_begin
= fuse_write_begin
,
918 .write_end
= fuse_write_end
,
919 .readpages
= fuse_readpages
,
920 .set_page_dirty
= fuse_set_page_dirty
,
924 void fuse_init_file_inode(struct inode
*inode
)
926 inode
->i_fop
= &fuse_file_operations
;
927 inode
->i_data
.a_ops
= &fuse_file_aops
;