2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 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>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/swap.h>
18 #include <linux/falloc.h>
19 #include <linux/uio.h>
22 static int fuse_send_open(struct fuse_mount
*fm
, u64 nodeid
,
23 unsigned int open_flags
, int opcode
,
24 struct fuse_open_out
*outargp
)
26 struct fuse_open_in inarg
;
29 memset(&inarg
, 0, sizeof(inarg
));
30 inarg
.flags
= open_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
31 if (!fm
->fc
->atomic_o_trunc
)
32 inarg
.flags
&= ~O_TRUNC
;
34 if (fm
->fc
->handle_killpriv_v2
&&
35 (inarg
.flags
& O_TRUNC
) && !capable(CAP_FSETID
)) {
36 inarg
.open_flags
|= FUSE_OPEN_KILL_SUIDGID
;
42 args
.in_args
[0].size
= sizeof(inarg
);
43 args
.in_args
[0].value
= &inarg
;
45 args
.out_args
[0].size
= sizeof(*outargp
);
46 args
.out_args
[0].value
= outargp
;
48 return fuse_simple_request(fm
, &args
);
51 struct fuse_release_args
{
52 struct fuse_args args
;
53 struct fuse_release_in inarg
;
57 struct fuse_file
*fuse_file_alloc(struct fuse_mount
*fm
)
61 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL_ACCOUNT
);
66 ff
->release_args
= kzalloc(sizeof(*ff
->release_args
),
68 if (!ff
->release_args
) {
73 INIT_LIST_HEAD(&ff
->write_entry
);
74 mutex_init(&ff
->readdir
.lock
);
75 refcount_set(&ff
->count
, 1);
76 RB_CLEAR_NODE(&ff
->polled_node
);
77 init_waitqueue_head(&ff
->poll_wait
);
79 ff
->kh
= atomic64_inc_return(&fm
->fc
->khctr
);
84 void fuse_file_free(struct fuse_file
*ff
)
86 kfree(ff
->release_args
);
87 mutex_destroy(&ff
->readdir
.lock
);
91 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
93 refcount_inc(&ff
->count
);
97 static void fuse_release_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
100 struct fuse_release_args
*ra
= container_of(args
, typeof(*ra
), args
);
106 static void fuse_file_put(struct fuse_file
*ff
, bool sync
, bool isdir
)
108 if (refcount_dec_and_test(&ff
->count
)) {
109 struct fuse_args
*args
= &ff
->release_args
->args
;
111 if (isdir
? ff
->fm
->fc
->no_opendir
: ff
->fm
->fc
->no_open
) {
112 /* Do nothing when client does not implement 'open' */
113 fuse_release_end(ff
->fm
, args
, 0);
115 fuse_simple_request(ff
->fm
, args
);
116 fuse_release_end(ff
->fm
, args
, 0);
118 args
->end
= fuse_release_end
;
119 if (fuse_simple_background(ff
->fm
, args
,
120 GFP_KERNEL
| __GFP_NOFAIL
))
121 fuse_release_end(ff
->fm
, args
, -ENOTCONN
);
127 struct fuse_file
*fuse_file_open(struct fuse_mount
*fm
, u64 nodeid
,
128 unsigned int open_flags
, bool isdir
)
130 struct fuse_conn
*fc
= fm
->fc
;
131 struct fuse_file
*ff
;
132 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
134 ff
= fuse_file_alloc(fm
);
136 return ERR_PTR(-ENOMEM
);
139 /* Default for no-open */
140 ff
->open_flags
= FOPEN_KEEP_CACHE
| (isdir
? FOPEN_CACHE_DIR
: 0);
141 if (isdir
? !fc
->no_opendir
: !fc
->no_open
) {
142 struct fuse_open_out outarg
;
145 err
= fuse_send_open(fm
, nodeid
, open_flags
, opcode
, &outarg
);
148 ff
->open_flags
= outarg
.open_flags
;
150 } else if (err
!= -ENOSYS
) {
162 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
169 int fuse_do_open(struct fuse_mount
*fm
, u64 nodeid
, struct file
*file
,
172 struct fuse_file
*ff
= fuse_file_open(fm
, nodeid
, file
->f_flags
, isdir
);
175 file
->private_data
= ff
;
177 return PTR_ERR_OR_ZERO(ff
);
179 EXPORT_SYMBOL_GPL(fuse_do_open
);
181 static void fuse_link_write_file(struct file
*file
)
183 struct inode
*inode
= file_inode(file
);
184 struct fuse_inode
*fi
= get_fuse_inode(inode
);
185 struct fuse_file
*ff
= file
->private_data
;
187 * file may be written through mmap, so chain it onto the
188 * inodes's write_file list
190 spin_lock(&fi
->lock
);
191 if (list_empty(&ff
->write_entry
))
192 list_add(&ff
->write_entry
, &fi
->write_files
);
193 spin_unlock(&fi
->lock
);
196 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
198 struct fuse_file
*ff
= file
->private_data
;
199 struct fuse_conn
*fc
= get_fuse_conn(inode
);
201 if (ff
->open_flags
& FOPEN_STREAM
)
202 stream_open(inode
, file
);
203 else if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
204 nonseekable_open(inode
, file
);
206 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
207 struct fuse_inode
*fi
= get_fuse_inode(inode
);
209 spin_lock(&fi
->lock
);
210 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
211 i_size_write(inode
, 0);
212 spin_unlock(&fi
->lock
);
213 truncate_pagecache(inode
, 0);
214 fuse_invalidate_attr_mask(inode
, FUSE_STATX_MODSIZE
);
215 if (fc
->writeback_cache
)
216 file_update_time(file
);
217 } else if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
)) {
218 invalidate_inode_pages2(inode
->i_mapping
);
221 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
222 fuse_link_write_file(file
);
225 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
227 struct fuse_mount
*fm
= get_fuse_mount(inode
);
228 struct fuse_conn
*fc
= fm
->fc
;
230 bool is_wb_truncate
= (file
->f_flags
& O_TRUNC
) &&
231 fc
->atomic_o_trunc
&&
233 bool dax_truncate
= (file
->f_flags
& O_TRUNC
) &&
234 fc
->atomic_o_trunc
&& FUSE_IS_DAX(inode
);
236 if (fuse_is_bad(inode
))
239 err
= generic_file_open(inode
, file
);
243 if (is_wb_truncate
|| dax_truncate
) {
245 fuse_set_nowrite(inode
);
249 filemap_invalidate_lock(inode
->i_mapping
);
250 err
= fuse_dax_break_layouts(inode
, 0, 0);
255 err
= fuse_do_open(fm
, get_node_id(inode
), file
, isdir
);
257 fuse_finish_open(inode
, file
);
261 filemap_invalidate_unlock(inode
->i_mapping
);
263 if (is_wb_truncate
| dax_truncate
) {
264 fuse_release_nowrite(inode
);
271 static void fuse_prepare_release(struct fuse_inode
*fi
, struct fuse_file
*ff
,
272 unsigned int flags
, int opcode
)
274 struct fuse_conn
*fc
= ff
->fm
->fc
;
275 struct fuse_release_args
*ra
= ff
->release_args
;
277 /* Inode is NULL on error path of fuse_create_open() */
279 spin_lock(&fi
->lock
);
280 list_del(&ff
->write_entry
);
281 spin_unlock(&fi
->lock
);
283 spin_lock(&fc
->lock
);
284 if (!RB_EMPTY_NODE(&ff
->polled_node
))
285 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
286 spin_unlock(&fc
->lock
);
288 wake_up_interruptible_all(&ff
->poll_wait
);
290 ra
->inarg
.fh
= ff
->fh
;
291 ra
->inarg
.flags
= flags
;
292 ra
->args
.in_numargs
= 1;
293 ra
->args
.in_args
[0].size
= sizeof(struct fuse_release_in
);
294 ra
->args
.in_args
[0].value
= &ra
->inarg
;
295 ra
->args
.opcode
= opcode
;
296 ra
->args
.nodeid
= ff
->nodeid
;
297 ra
->args
.force
= true;
298 ra
->args
.nocreds
= true;
301 void fuse_file_release(struct inode
*inode
, struct fuse_file
*ff
,
302 unsigned int open_flags
, fl_owner_t id
, bool isdir
)
304 struct fuse_inode
*fi
= get_fuse_inode(inode
);
305 struct fuse_release_args
*ra
= ff
->release_args
;
306 int opcode
= isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
;
308 fuse_prepare_release(fi
, ff
, open_flags
, opcode
);
311 ra
->inarg
.release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
312 ra
->inarg
.lock_owner
= fuse_lock_owner_id(ff
->fm
->fc
, id
);
314 /* Hold inode until release is finished */
315 ra
->inode
= igrab(inode
);
318 * Normally this will send the RELEASE request, however if
319 * some asynchronous READ or WRITE requests are outstanding,
320 * the sending will be delayed.
322 * Make the release synchronous if this is a fuseblk mount,
323 * synchronous RELEASE is allowed (and desirable) in this case
324 * because the server can be trusted not to screw up.
326 fuse_file_put(ff
, ff
->fm
->fc
->destroy
, isdir
);
329 void fuse_release_common(struct file
*file
, bool isdir
)
331 fuse_file_release(file_inode(file
), file
->private_data
, file
->f_flags
,
332 (fl_owner_t
) file
, isdir
);
335 static int fuse_open(struct inode
*inode
, struct file
*file
)
337 return fuse_open_common(inode
, file
, false);
340 static int fuse_release(struct inode
*inode
, struct file
*file
)
342 fuse_release_common(file
, false);
344 /* return value is ignored by VFS */
348 void fuse_sync_release(struct fuse_inode
*fi
, struct fuse_file
*ff
,
351 WARN_ON(refcount_read(&ff
->count
) > 1);
352 fuse_prepare_release(fi
, ff
, flags
, FUSE_RELEASE
);
354 * iput(NULL) is a no-op and since the refcount is 1 and everything's
355 * synchronous, we are fine with not doing igrab() here"
357 fuse_file_put(ff
, true, false);
359 EXPORT_SYMBOL_GPL(fuse_sync_release
);
362 * Scramble the ID space with XTEA, so that the value of the files_struct
363 * pointer is not exposed to userspace.
365 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
367 u32
*k
= fc
->scramble_key
;
368 u64 v
= (unsigned long) id
;
374 for (i
= 0; i
< 32; i
++) {
375 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
377 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
380 return (u64
) v0
+ ((u64
) v1
<< 32);
383 struct fuse_writepage_args
{
384 struct fuse_io_args ia
;
385 struct rb_node writepages_entry
;
386 struct list_head queue_entry
;
387 struct fuse_writepage_args
*next
;
389 struct fuse_sync_bucket
*bucket
;
392 static struct fuse_writepage_args
*fuse_find_writeback(struct fuse_inode
*fi
,
393 pgoff_t idx_from
, pgoff_t idx_to
)
397 n
= fi
->writepages
.rb_node
;
400 struct fuse_writepage_args
*wpa
;
403 wpa
= rb_entry(n
, struct fuse_writepage_args
, writepages_entry
);
404 WARN_ON(get_fuse_inode(wpa
->inode
) != fi
);
405 curr_index
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
406 if (idx_from
>= curr_index
+ wpa
->ia
.ap
.num_pages
)
408 else if (idx_to
< curr_index
)
417 * Check if any page in a range is under writeback
419 * This is currently done by walking the list of writepage requests
420 * for the inode, which can be pretty inefficient.
422 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
425 struct fuse_inode
*fi
= get_fuse_inode(inode
);
428 spin_lock(&fi
->lock
);
429 found
= fuse_find_writeback(fi
, idx_from
, idx_to
);
430 spin_unlock(&fi
->lock
);
435 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
437 return fuse_range_is_writeback(inode
, index
, index
);
441 * Wait for page writeback to be completed.
443 * Since fuse doesn't rely on the VM writeback tracking, this has to
444 * use some other means.
446 static void fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
448 struct fuse_inode
*fi
= get_fuse_inode(inode
);
450 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
454 * Wait for all pending writepages on the inode to finish.
456 * This is currently done by blocking further writes with FUSE_NOWRITE
457 * and waiting for all sent writes to complete.
459 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
460 * could conflict with truncation.
462 static void fuse_sync_writes(struct inode
*inode
)
464 fuse_set_nowrite(inode
);
465 fuse_release_nowrite(inode
);
468 static int fuse_flush(struct file
*file
, fl_owner_t id
)
470 struct inode
*inode
= file_inode(file
);
471 struct fuse_mount
*fm
= get_fuse_mount(inode
);
472 struct fuse_file
*ff
= file
->private_data
;
473 struct fuse_flush_in inarg
;
477 if (fuse_is_bad(inode
))
480 err
= write_inode_now(inode
, 1);
485 fuse_sync_writes(inode
);
488 err
= filemap_check_errors(file
->f_mapping
);
493 if (fm
->fc
->no_flush
)
496 memset(&inarg
, 0, sizeof(inarg
));
498 inarg
.lock_owner
= fuse_lock_owner_id(fm
->fc
, id
);
499 args
.opcode
= FUSE_FLUSH
;
500 args
.nodeid
= get_node_id(inode
);
502 args
.in_args
[0].size
= sizeof(inarg
);
503 args
.in_args
[0].value
= &inarg
;
506 err
= fuse_simple_request(fm
, &args
);
507 if (err
== -ENOSYS
) {
508 fm
->fc
->no_flush
= 1;
514 * In memory i_blocks is not maintained by fuse, if writeback cache is
515 * enabled, i_blocks from cached attr may not be accurate.
517 if (!err
&& fm
->fc
->writeback_cache
)
518 fuse_invalidate_attr_mask(inode
, STATX_BLOCKS
);
522 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
523 int datasync
, int opcode
)
525 struct inode
*inode
= file
->f_mapping
->host
;
526 struct fuse_mount
*fm
= get_fuse_mount(inode
);
527 struct fuse_file
*ff
= file
->private_data
;
529 struct fuse_fsync_in inarg
;
531 memset(&inarg
, 0, sizeof(inarg
));
533 inarg
.fsync_flags
= datasync
? FUSE_FSYNC_FDATASYNC
: 0;
534 args
.opcode
= opcode
;
535 args
.nodeid
= get_node_id(inode
);
537 args
.in_args
[0].size
= sizeof(inarg
);
538 args
.in_args
[0].value
= &inarg
;
539 return fuse_simple_request(fm
, &args
);
542 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
545 struct inode
*inode
= file
->f_mapping
->host
;
546 struct fuse_conn
*fc
= get_fuse_conn(inode
);
549 if (fuse_is_bad(inode
))
555 * Start writeback against all dirty pages of the inode, then
556 * wait for all outstanding writes, before sending the FSYNC
559 err
= file_write_and_wait_range(file
, start
, end
);
563 fuse_sync_writes(inode
);
566 * Due to implementation of fuse writeback
567 * file_write_and_wait_range() does not catch errors.
568 * We have to do this directly after fuse_sync_writes()
570 err
= file_check_and_advance_wb_err(file
);
574 err
= sync_inode_metadata(inode
, 1);
581 err
= fuse_fsync_common(file
, start
, end
, datasync
, FUSE_FSYNC
);
582 if (err
== -ENOSYS
) {
592 void fuse_read_args_fill(struct fuse_io_args
*ia
, struct file
*file
, loff_t pos
,
593 size_t count
, int opcode
)
595 struct fuse_file
*ff
= file
->private_data
;
596 struct fuse_args
*args
= &ia
->ap
.args
;
598 ia
->read
.in
.fh
= ff
->fh
;
599 ia
->read
.in
.offset
= pos
;
600 ia
->read
.in
.size
= count
;
601 ia
->read
.in
.flags
= file
->f_flags
;
602 args
->opcode
= opcode
;
603 args
->nodeid
= ff
->nodeid
;
604 args
->in_numargs
= 1;
605 args
->in_args
[0].size
= sizeof(ia
->read
.in
);
606 args
->in_args
[0].value
= &ia
->read
.in
;
607 args
->out_argvar
= true;
608 args
->out_numargs
= 1;
609 args
->out_args
[0].size
= count
;
612 static void fuse_release_user_pages(struct fuse_args_pages
*ap
,
617 for (i
= 0; i
< ap
->num_pages
; i
++) {
619 set_page_dirty_lock(ap
->pages
[i
]);
620 put_page(ap
->pages
[i
]);
624 static void fuse_io_release(struct kref
*kref
)
626 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
629 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
634 if (io
->bytes
>= 0 && io
->write
)
637 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
641 * In case of short read, the caller sets 'pos' to the position of
642 * actual end of fuse request in IO request. Otherwise, if bytes_requested
643 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
646 * User requested DIO read of 64K. It was split into two 32K fuse requests,
647 * both submitted asynchronously. The first of them was ACKed by userspace as
648 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
649 * second request was ACKed as short, e.g. only 1K was read, resulting in
652 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
653 * will be equal to the length of the longest contiguous fragment of
654 * transferred data starting from the beginning of IO request.
656 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
660 spin_lock(&io
->lock
);
662 io
->err
= io
->err
? : err
;
663 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
667 if (!left
&& io
->blocking
)
669 spin_unlock(&io
->lock
);
671 if (!left
&& !io
->blocking
) {
672 ssize_t res
= fuse_get_res_by_io(io
);
675 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
676 struct fuse_conn
*fc
= get_fuse_conn(inode
);
677 struct fuse_inode
*fi
= get_fuse_inode(inode
);
679 spin_lock(&fi
->lock
);
680 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
681 spin_unlock(&fi
->lock
);
684 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
687 kref_put(&io
->refcnt
, fuse_io_release
);
690 static struct fuse_io_args
*fuse_io_alloc(struct fuse_io_priv
*io
,
693 struct fuse_io_args
*ia
;
695 ia
= kzalloc(sizeof(*ia
), GFP_KERNEL
);
698 ia
->ap
.pages
= fuse_pages_alloc(npages
, GFP_KERNEL
,
708 static void fuse_io_free(struct fuse_io_args
*ia
)
714 static void fuse_aio_complete_req(struct fuse_mount
*fm
, struct fuse_args
*args
,
717 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
718 struct fuse_io_priv
*io
= ia
->io
;
721 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
725 } else if (io
->write
) {
726 if (ia
->write
.out
.size
> ia
->write
.in
.size
) {
728 } else if (ia
->write
.in
.size
!= ia
->write
.out
.size
) {
729 pos
= ia
->write
.in
.offset
- io
->offset
+
733 u32 outsize
= args
->out_args
[0].size
;
735 if (ia
->read
.in
.size
!= outsize
)
736 pos
= ia
->read
.in
.offset
- io
->offset
+ outsize
;
739 fuse_aio_complete(io
, err
, pos
);
743 static ssize_t
fuse_async_req_send(struct fuse_mount
*fm
,
744 struct fuse_io_args
*ia
, size_t num_bytes
)
747 struct fuse_io_priv
*io
= ia
->io
;
749 spin_lock(&io
->lock
);
750 kref_get(&io
->refcnt
);
751 io
->size
+= num_bytes
;
753 spin_unlock(&io
->lock
);
755 ia
->ap
.args
.end
= fuse_aio_complete_req
;
756 ia
->ap
.args
.may_block
= io
->should_dirty
;
757 err
= fuse_simple_background(fm
, &ia
->ap
.args
, GFP_KERNEL
);
759 fuse_aio_complete_req(fm
, &ia
->ap
.args
, err
);
764 static ssize_t
fuse_send_read(struct fuse_io_args
*ia
, loff_t pos
, size_t count
,
767 struct file
*file
= ia
->io
->iocb
->ki_filp
;
768 struct fuse_file
*ff
= file
->private_data
;
769 struct fuse_mount
*fm
= ff
->fm
;
771 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
773 ia
->read
.in
.read_flags
|= FUSE_READ_LOCKOWNER
;
774 ia
->read
.in
.lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
778 return fuse_async_req_send(fm
, ia
, count
);
780 return fuse_simple_request(fm
, &ia
->ap
.args
);
783 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
786 struct fuse_conn
*fc
= get_fuse_conn(inode
);
787 struct fuse_inode
*fi
= get_fuse_inode(inode
);
789 spin_lock(&fi
->lock
);
790 if (attr_ver
>= fi
->attr_version
&& size
< inode
->i_size
&&
791 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
792 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
793 i_size_write(inode
, size
);
795 spin_unlock(&fi
->lock
);
798 static void fuse_short_read(struct inode
*inode
, u64 attr_ver
, size_t num_read
,
799 struct fuse_args_pages
*ap
)
801 struct fuse_conn
*fc
= get_fuse_conn(inode
);
804 * If writeback_cache is enabled, a short read means there's a hole in
805 * the file. Some data after the hole is in page cache, but has not
806 * reached the client fs yet. So the hole is not present there.
808 if (!fc
->writeback_cache
) {
809 loff_t pos
= page_offset(ap
->pages
[0]) + num_read
;
810 fuse_read_update_size(inode
, pos
, attr_ver
);
814 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
816 struct inode
*inode
= page
->mapping
->host
;
817 struct fuse_mount
*fm
= get_fuse_mount(inode
);
818 loff_t pos
= page_offset(page
);
819 struct fuse_page_desc desc
= { .length
= PAGE_SIZE
};
820 struct fuse_io_args ia
= {
821 .ap
.args
.page_zeroing
= true,
822 .ap
.args
.out_pages
= true,
831 * Page writeback can extend beyond the lifetime of the
832 * page-cache page, so make sure we read a properly synced
835 fuse_wait_on_page_writeback(inode
, page
->index
);
837 attr_ver
= fuse_get_attr_version(fm
->fc
);
839 /* Don't overflow end offset */
840 if (pos
+ (desc
.length
- 1) == LLONG_MAX
)
843 fuse_read_args_fill(&ia
, file
, pos
, desc
.length
, FUSE_READ
);
844 res
= fuse_simple_request(fm
, &ia
.ap
.args
);
848 * Short read means EOF. If file size is larger, truncate it
850 if (res
< desc
.length
)
851 fuse_short_read(inode
, attr_ver
, res
, &ia
.ap
);
853 SetPageUptodate(page
);
858 static int fuse_readpage(struct file
*file
, struct page
*page
)
860 struct inode
*inode
= page
->mapping
->host
;
864 if (fuse_is_bad(inode
))
867 err
= fuse_do_readpage(file
, page
);
868 fuse_invalidate_atime(inode
);
874 static void fuse_readpages_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
878 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
879 struct fuse_args_pages
*ap
= &ia
->ap
;
880 size_t count
= ia
->read
.in
.size
;
881 size_t num_read
= args
->out_args
[0].size
;
882 struct address_space
*mapping
= NULL
;
884 for (i
= 0; mapping
== NULL
&& i
< ap
->num_pages
; i
++)
885 mapping
= ap
->pages
[i
]->mapping
;
888 struct inode
*inode
= mapping
->host
;
891 * Short read means EOF. If file size is larger, truncate it
893 if (!err
&& num_read
< count
)
894 fuse_short_read(inode
, ia
->read
.attr_ver
, num_read
, ap
);
896 fuse_invalidate_atime(inode
);
899 for (i
= 0; i
< ap
->num_pages
; i
++) {
900 struct page
*page
= ap
->pages
[i
];
903 SetPageUptodate(page
);
910 fuse_file_put(ia
->ff
, false, false);
915 static void fuse_send_readpages(struct fuse_io_args
*ia
, struct file
*file
)
917 struct fuse_file
*ff
= file
->private_data
;
918 struct fuse_mount
*fm
= ff
->fm
;
919 struct fuse_args_pages
*ap
= &ia
->ap
;
920 loff_t pos
= page_offset(ap
->pages
[0]);
921 size_t count
= ap
->num_pages
<< PAGE_SHIFT
;
925 ap
->args
.out_pages
= true;
926 ap
->args
.page_zeroing
= true;
927 ap
->args
.page_replace
= true;
929 /* Don't overflow end offset */
930 if (pos
+ (count
- 1) == LLONG_MAX
) {
932 ap
->descs
[ap
->num_pages
- 1].length
--;
934 WARN_ON((loff_t
) (pos
+ count
) < 0);
936 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
937 ia
->read
.attr_ver
= fuse_get_attr_version(fm
->fc
);
938 if (fm
->fc
->async_read
) {
939 ia
->ff
= fuse_file_get(ff
);
940 ap
->args
.end
= fuse_readpages_end
;
941 err
= fuse_simple_background(fm
, &ap
->args
, GFP_KERNEL
);
945 res
= fuse_simple_request(fm
, &ap
->args
);
946 err
= res
< 0 ? res
: 0;
948 fuse_readpages_end(fm
, &ap
->args
, err
);
951 static void fuse_readahead(struct readahead_control
*rac
)
953 struct inode
*inode
= rac
->mapping
->host
;
954 struct fuse_conn
*fc
= get_fuse_conn(inode
);
955 unsigned int i
, max_pages
, nr_pages
= 0;
957 if (fuse_is_bad(inode
))
960 max_pages
= min_t(unsigned int, fc
->max_pages
,
961 fc
->max_read
/ PAGE_SIZE
);
964 struct fuse_io_args
*ia
;
965 struct fuse_args_pages
*ap
;
967 nr_pages
= readahead_count(rac
) - nr_pages
;
968 if (nr_pages
> max_pages
)
969 nr_pages
= max_pages
;
972 ia
= fuse_io_alloc(NULL
, nr_pages
);
976 nr_pages
= __readahead_batch(rac
, ap
->pages
, nr_pages
);
977 for (i
= 0; i
< nr_pages
; i
++) {
978 fuse_wait_on_page_writeback(inode
,
979 readahead_index(rac
) + i
);
980 ap
->descs
[i
].length
= PAGE_SIZE
;
982 ap
->num_pages
= nr_pages
;
983 fuse_send_readpages(ia
, rac
->file
);
987 static ssize_t
fuse_cache_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
989 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
990 struct fuse_conn
*fc
= get_fuse_conn(inode
);
993 * In auto invalidate mode, always update attributes on read.
994 * Otherwise, only update if we attempt to read past EOF (to ensure
995 * i_size is up to date).
997 if (fc
->auto_inval_data
||
998 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
1000 err
= fuse_update_attributes(inode
, iocb
->ki_filp
);
1005 return generic_file_read_iter(iocb
, to
);
1008 static void fuse_write_args_fill(struct fuse_io_args
*ia
, struct fuse_file
*ff
,
1009 loff_t pos
, size_t count
)
1011 struct fuse_args
*args
= &ia
->ap
.args
;
1013 ia
->write
.in
.fh
= ff
->fh
;
1014 ia
->write
.in
.offset
= pos
;
1015 ia
->write
.in
.size
= count
;
1016 args
->opcode
= FUSE_WRITE
;
1017 args
->nodeid
= ff
->nodeid
;
1018 args
->in_numargs
= 2;
1019 if (ff
->fm
->fc
->minor
< 9)
1020 args
->in_args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
1022 args
->in_args
[0].size
= sizeof(ia
->write
.in
);
1023 args
->in_args
[0].value
= &ia
->write
.in
;
1024 args
->in_args
[1].size
= count
;
1025 args
->out_numargs
= 1;
1026 args
->out_args
[0].size
= sizeof(ia
->write
.out
);
1027 args
->out_args
[0].value
= &ia
->write
.out
;
1030 static unsigned int fuse_write_flags(struct kiocb
*iocb
)
1032 unsigned int flags
= iocb
->ki_filp
->f_flags
;
1034 if (iocb
->ki_flags
& IOCB_DSYNC
)
1036 if (iocb
->ki_flags
& IOCB_SYNC
)
1042 static ssize_t
fuse_send_write(struct fuse_io_args
*ia
, loff_t pos
,
1043 size_t count
, fl_owner_t owner
)
1045 struct kiocb
*iocb
= ia
->io
->iocb
;
1046 struct file
*file
= iocb
->ki_filp
;
1047 struct fuse_file
*ff
= file
->private_data
;
1048 struct fuse_mount
*fm
= ff
->fm
;
1049 struct fuse_write_in
*inarg
= &ia
->write
.in
;
1052 fuse_write_args_fill(ia
, ff
, pos
, count
);
1053 inarg
->flags
= fuse_write_flags(iocb
);
1054 if (owner
!= NULL
) {
1055 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1056 inarg
->lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
1060 return fuse_async_req_send(fm
, ia
, count
);
1062 err
= fuse_simple_request(fm
, &ia
->ap
.args
);
1063 if (!err
&& ia
->write
.out
.size
> count
)
1066 return err
?: ia
->write
.out
.size
;
1069 bool fuse_write_update_attr(struct inode
*inode
, loff_t pos
, ssize_t written
)
1071 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1072 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1075 spin_lock(&fi
->lock
);
1076 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
1077 if (written
> 0 && pos
> inode
->i_size
) {
1078 i_size_write(inode
, pos
);
1081 spin_unlock(&fi
->lock
);
1083 fuse_invalidate_attr_mask(inode
, FUSE_STATX_MODSIZE
);
1088 static ssize_t
fuse_send_write_pages(struct fuse_io_args
*ia
,
1089 struct kiocb
*iocb
, struct inode
*inode
,
1090 loff_t pos
, size_t count
)
1092 struct fuse_args_pages
*ap
= &ia
->ap
;
1093 struct file
*file
= iocb
->ki_filp
;
1094 struct fuse_file
*ff
= file
->private_data
;
1095 struct fuse_mount
*fm
= ff
->fm
;
1096 unsigned int offset
, i
;
1100 for (i
= 0; i
< ap
->num_pages
; i
++)
1101 fuse_wait_on_page_writeback(inode
, ap
->pages
[i
]->index
);
1103 fuse_write_args_fill(ia
, ff
, pos
, count
);
1104 ia
->write
.in
.flags
= fuse_write_flags(iocb
);
1105 if (fm
->fc
->handle_killpriv_v2
&& !capable(CAP_FSETID
))
1106 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_SUIDGID
;
1108 err
= fuse_simple_request(fm
, &ap
->args
);
1109 if (!err
&& ia
->write
.out
.size
> count
)
1112 short_write
= ia
->write
.out
.size
< count
;
1113 offset
= ap
->descs
[0].offset
;
1114 count
= ia
->write
.out
.size
;
1115 for (i
= 0; i
< ap
->num_pages
; i
++) {
1116 struct page
*page
= ap
->pages
[i
];
1119 ClearPageUptodate(page
);
1121 if (count
>= PAGE_SIZE
- offset
)
1122 count
-= PAGE_SIZE
- offset
;
1125 ClearPageUptodate(page
);
1130 if (ia
->write
.page_locked
&& (i
== ap
->num_pages
- 1))
1138 static ssize_t
fuse_fill_write_pages(struct fuse_io_args
*ia
,
1139 struct address_space
*mapping
,
1140 struct iov_iter
*ii
, loff_t pos
,
1141 unsigned int max_pages
)
1143 struct fuse_args_pages
*ap
= &ia
->ap
;
1144 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1145 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1149 ap
->args
.in_pages
= true;
1150 ap
->descs
[0].offset
= offset
;
1155 pgoff_t index
= pos
>> PAGE_SHIFT
;
1156 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1157 iov_iter_count(ii
));
1159 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1163 if (iov_iter_fault_in_readable(ii
, bytes
))
1167 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1171 if (mapping_writably_mapped(mapping
))
1172 flush_dcache_page(page
);
1174 tmp
= copy_page_from_iter_atomic(page
, offset
, bytes
, ii
);
1175 flush_dcache_page(page
);
1184 ap
->pages
[ap
->num_pages
] = page
;
1185 ap
->descs
[ap
->num_pages
].length
= tmp
;
1191 if (offset
== PAGE_SIZE
)
1194 /* If we copied full page, mark it uptodate */
1195 if (tmp
== PAGE_SIZE
)
1196 SetPageUptodate(page
);
1198 if (PageUptodate(page
)) {
1201 ia
->write
.page_locked
= true;
1204 if (!fc
->big_writes
)
1206 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1207 ap
->num_pages
< max_pages
&& offset
== 0);
1209 return count
> 0 ? count
: err
;
1212 static inline unsigned int fuse_wr_pages(loff_t pos
, size_t len
,
1213 unsigned int max_pages
)
1215 return min_t(unsigned int,
1216 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1217 (pos
>> PAGE_SHIFT
) + 1,
1221 static ssize_t
fuse_perform_write(struct kiocb
*iocb
,
1222 struct address_space
*mapping
,
1223 struct iov_iter
*ii
, loff_t pos
)
1225 struct inode
*inode
= mapping
->host
;
1226 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1227 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1231 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1232 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1236 struct fuse_io_args ia
= {};
1237 struct fuse_args_pages
*ap
= &ia
.ap
;
1238 unsigned int nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
),
1241 ap
->pages
= fuse_pages_alloc(nr_pages
, GFP_KERNEL
, &ap
->descs
);
1247 count
= fuse_fill_write_pages(&ia
, mapping
, ii
, pos
, nr_pages
);
1251 err
= fuse_send_write_pages(&ia
, iocb
, inode
,
1254 size_t num_written
= ia
.write
.out
.size
;
1259 /* break out of the loop on short write */
1260 if (num_written
!= count
)
1265 } while (!err
&& iov_iter_count(ii
));
1267 fuse_write_update_attr(inode
, pos
, res
);
1268 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1270 return res
> 0 ? res
: err
;
1273 static ssize_t
fuse_cache_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1275 struct file
*file
= iocb
->ki_filp
;
1276 struct address_space
*mapping
= file
->f_mapping
;
1277 ssize_t written
= 0;
1278 ssize_t written_buffered
= 0;
1279 struct inode
*inode
= mapping
->host
;
1281 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1284 if (fc
->writeback_cache
) {
1285 /* Update size (EOF optimization) and mode (SUID clearing) */
1286 err
= fuse_update_attributes(mapping
->host
, file
);
1290 if (fc
->handle_killpriv_v2
&&
1291 should_remove_suid(file_dentry(file
))) {
1295 return generic_file_write_iter(iocb
, from
);
1301 /* We can write back this queue in page reclaim */
1302 current
->backing_dev_info
= inode_to_bdi(inode
);
1304 err
= generic_write_checks(iocb
, from
);
1308 err
= file_remove_privs(file
);
1312 err
= file_update_time(file
);
1316 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1317 loff_t pos
= iocb
->ki_pos
;
1318 written
= generic_file_direct_write(iocb
, from
);
1319 if (written
< 0 || !iov_iter_count(from
))
1324 written_buffered
= fuse_perform_write(iocb
, mapping
, from
, pos
);
1325 if (written_buffered
< 0) {
1326 err
= written_buffered
;
1329 endbyte
= pos
+ written_buffered
- 1;
1331 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1336 invalidate_mapping_pages(file
->f_mapping
,
1338 endbyte
>> PAGE_SHIFT
);
1340 written
+= written_buffered
;
1341 iocb
->ki_pos
= pos
+ written_buffered
;
1343 written
= fuse_perform_write(iocb
, mapping
, from
, iocb
->ki_pos
);
1345 iocb
->ki_pos
+= written
;
1348 current
->backing_dev_info
= NULL
;
1349 inode_unlock(inode
);
1351 written
= generic_write_sync(iocb
, written
);
1353 return written
? written
: err
;
1356 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1358 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1361 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1364 return min(iov_iter_single_seg_count(ii
), max_size
);
1367 static int fuse_get_user_pages(struct fuse_args_pages
*ap
, struct iov_iter
*ii
,
1368 size_t *nbytesp
, int write
,
1369 unsigned int max_pages
)
1371 size_t nbytes
= 0; /* # bytes already packed in req */
1374 /* Special case for kernel I/O: can copy directly into the buffer */
1375 if (iov_iter_is_kvec(ii
)) {
1376 unsigned long user_addr
= fuse_get_user_addr(ii
);
1377 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1380 ap
->args
.in_args
[1].value
= (void *) user_addr
;
1382 ap
->args
.out_args
[0].value
= (void *) user_addr
;
1384 iov_iter_advance(ii
, frag_size
);
1385 *nbytesp
= frag_size
;
1389 while (nbytes
< *nbytesp
&& ap
->num_pages
< max_pages
) {
1392 ret
= iov_iter_get_pages(ii
, &ap
->pages
[ap
->num_pages
],
1394 max_pages
- ap
->num_pages
,
1399 iov_iter_advance(ii
, ret
);
1403 npages
= DIV_ROUND_UP(ret
, PAGE_SIZE
);
1405 ap
->descs
[ap
->num_pages
].offset
= start
;
1406 fuse_page_descs_length_init(ap
->descs
, ap
->num_pages
, npages
);
1408 ap
->num_pages
+= npages
;
1409 ap
->descs
[ap
->num_pages
- 1].length
-=
1410 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1414 ap
->args
.in_pages
= true;
1416 ap
->args
.out_pages
= true;
1420 return ret
< 0 ? ret
: 0;
1423 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1424 loff_t
*ppos
, int flags
)
1426 int write
= flags
& FUSE_DIO_WRITE
;
1427 int cuse
= flags
& FUSE_DIO_CUSE
;
1428 struct file
*file
= io
->iocb
->ki_filp
;
1429 struct inode
*inode
= file
->f_mapping
->host
;
1430 struct fuse_file
*ff
= file
->private_data
;
1431 struct fuse_conn
*fc
= ff
->fm
->fc
;
1432 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1434 size_t count
= iov_iter_count(iter
);
1435 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1436 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1439 struct fuse_io_args
*ia
;
1440 unsigned int max_pages
;
1442 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1443 ia
= fuse_io_alloc(io
, max_pages
);
1447 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1450 fuse_sync_writes(inode
);
1452 inode_unlock(inode
);
1455 io
->should_dirty
= !write
&& iter_is_iovec(iter
);
1458 fl_owner_t owner
= current
->files
;
1459 size_t nbytes
= min(count
, nmax
);
1461 err
= fuse_get_user_pages(&ia
->ap
, iter
, &nbytes
, write
,
1467 if (!capable(CAP_FSETID
))
1468 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_SUIDGID
;
1470 nres
= fuse_send_write(ia
, pos
, nbytes
, owner
);
1472 nres
= fuse_send_read(ia
, pos
, nbytes
, owner
);
1475 if (!io
->async
|| nres
< 0) {
1476 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
1481 iov_iter_revert(iter
, nbytes
);
1485 WARN_ON(nres
> nbytes
);
1490 if (nres
!= nbytes
) {
1491 iov_iter_revert(iter
, nbytes
- nres
);
1495 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1496 ia
= fuse_io_alloc(io
, max_pages
);
1506 return res
> 0 ? res
: err
;
1508 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1510 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1511 struct iov_iter
*iter
,
1515 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
1517 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1519 fuse_invalidate_atime(inode
);
1524 static ssize_t
fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
);
1526 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1530 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1531 res
= fuse_direct_IO(iocb
, to
);
1533 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1535 res
= __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1541 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1543 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1544 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1547 /* Don't allow parallel writes to the same file */
1549 res
= generic_write_checks(iocb
, from
);
1551 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1552 res
= fuse_direct_IO(iocb
, from
);
1554 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
,
1556 fuse_write_update_attr(inode
, iocb
->ki_pos
, res
);
1559 inode_unlock(inode
);
1564 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1566 struct file
*file
= iocb
->ki_filp
;
1567 struct fuse_file
*ff
= file
->private_data
;
1568 struct inode
*inode
= file_inode(file
);
1570 if (fuse_is_bad(inode
))
1573 if (FUSE_IS_DAX(inode
))
1574 return fuse_dax_read_iter(iocb
, to
);
1576 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1577 return fuse_cache_read_iter(iocb
, to
);
1579 return fuse_direct_read_iter(iocb
, to
);
1582 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1584 struct file
*file
= iocb
->ki_filp
;
1585 struct fuse_file
*ff
= file
->private_data
;
1586 struct inode
*inode
= file_inode(file
);
1588 if (fuse_is_bad(inode
))
1591 if (FUSE_IS_DAX(inode
))
1592 return fuse_dax_write_iter(iocb
, from
);
1594 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1595 return fuse_cache_write_iter(iocb
, from
);
1597 return fuse_direct_write_iter(iocb
, from
);
1600 static void fuse_writepage_free(struct fuse_writepage_args
*wpa
)
1602 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1606 fuse_sync_bucket_dec(wpa
->bucket
);
1608 for (i
= 0; i
< ap
->num_pages
; i
++)
1609 __free_page(ap
->pages
[i
]);
1612 fuse_file_put(wpa
->ia
.ff
, false, false);
1618 static void fuse_writepage_finish(struct fuse_mount
*fm
,
1619 struct fuse_writepage_args
*wpa
)
1621 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1622 struct inode
*inode
= wpa
->inode
;
1623 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1624 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1627 for (i
= 0; i
< ap
->num_pages
; i
++) {
1628 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1629 dec_node_page_state(ap
->pages
[i
], NR_WRITEBACK_TEMP
);
1630 wb_writeout_inc(&bdi
->wb
);
1632 wake_up(&fi
->page_waitq
);
1635 /* Called under fi->lock, may release and reacquire it */
1636 static void fuse_send_writepage(struct fuse_mount
*fm
,
1637 struct fuse_writepage_args
*wpa
, loff_t size
)
1638 __releases(fi
->lock
)
1639 __acquires(fi
->lock
)
1641 struct fuse_writepage_args
*aux
, *next
;
1642 struct fuse_inode
*fi
= get_fuse_inode(wpa
->inode
);
1643 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1644 struct fuse_args
*args
= &wpa
->ia
.ap
.args
;
1645 __u64 data_size
= wpa
->ia
.ap
.num_pages
* PAGE_SIZE
;
1649 if (inarg
->offset
+ data_size
<= size
) {
1650 inarg
->size
= data_size
;
1651 } else if (inarg
->offset
< size
) {
1652 inarg
->size
= size
- inarg
->offset
;
1654 /* Got truncated off completely */
1658 args
->in_args
[1].size
= inarg
->size
;
1660 args
->nocreds
= true;
1662 err
= fuse_simple_background(fm
, args
, GFP_ATOMIC
);
1663 if (err
== -ENOMEM
) {
1664 spin_unlock(&fi
->lock
);
1665 err
= fuse_simple_background(fm
, args
, GFP_NOFS
| __GFP_NOFAIL
);
1666 spin_lock(&fi
->lock
);
1669 /* Fails on broken connection only */
1677 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1678 fuse_writepage_finish(fm
, wpa
);
1679 spin_unlock(&fi
->lock
);
1681 /* After fuse_writepage_finish() aux request list is private */
1682 for (aux
= wpa
->next
; aux
; aux
= next
) {
1685 fuse_writepage_free(aux
);
1688 fuse_writepage_free(wpa
);
1689 spin_lock(&fi
->lock
);
1693 * If fi->writectr is positive (no truncate or fsync going on) send
1694 * all queued writepage requests.
1696 * Called with fi->lock
1698 void fuse_flush_writepages(struct inode
*inode
)
1699 __releases(fi
->lock
)
1700 __acquires(fi
->lock
)
1702 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1703 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1704 loff_t crop
= i_size_read(inode
);
1705 struct fuse_writepage_args
*wpa
;
1707 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1708 wpa
= list_entry(fi
->queued_writes
.next
,
1709 struct fuse_writepage_args
, queue_entry
);
1710 list_del_init(&wpa
->queue_entry
);
1711 fuse_send_writepage(fm
, wpa
, crop
);
1715 static struct fuse_writepage_args
*fuse_insert_writeback(struct rb_root
*root
,
1716 struct fuse_writepage_args
*wpa
)
1718 pgoff_t idx_from
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1719 pgoff_t idx_to
= idx_from
+ wpa
->ia
.ap
.num_pages
- 1;
1720 struct rb_node
**p
= &root
->rb_node
;
1721 struct rb_node
*parent
= NULL
;
1723 WARN_ON(!wpa
->ia
.ap
.num_pages
);
1725 struct fuse_writepage_args
*curr
;
1729 curr
= rb_entry(parent
, struct fuse_writepage_args
,
1731 WARN_ON(curr
->inode
!= wpa
->inode
);
1732 curr_index
= curr
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1734 if (idx_from
>= curr_index
+ curr
->ia
.ap
.num_pages
)
1735 p
= &(*p
)->rb_right
;
1736 else if (idx_to
< curr_index
)
1742 rb_link_node(&wpa
->writepages_entry
, parent
, p
);
1743 rb_insert_color(&wpa
->writepages_entry
, root
);
1747 static void tree_insert(struct rb_root
*root
, struct fuse_writepage_args
*wpa
)
1749 WARN_ON(fuse_insert_writeback(root
, wpa
));
1752 static void fuse_writepage_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
1755 struct fuse_writepage_args
*wpa
=
1756 container_of(args
, typeof(*wpa
), ia
.ap
.args
);
1757 struct inode
*inode
= wpa
->inode
;
1758 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1759 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1761 mapping_set_error(inode
->i_mapping
, error
);
1763 * A writeback finished and this might have updated mtime/ctime on
1764 * server making local mtime/ctime stale. Hence invalidate attrs.
1765 * Do this only if writeback_cache is not enabled. If writeback_cache
1766 * is enabled, we trust local ctime/mtime.
1768 if (!fc
->writeback_cache
)
1769 fuse_invalidate_attr_mask(inode
, FUSE_STATX_MODIFY
);
1770 spin_lock(&fi
->lock
);
1771 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1773 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1774 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1775 struct fuse_writepage_args
*next
= wpa
->next
;
1777 wpa
->next
= next
->next
;
1779 next
->ia
.ff
= fuse_file_get(wpa
->ia
.ff
);
1780 tree_insert(&fi
->writepages
, next
);
1783 * Skip fuse_flush_writepages() to make it easy to crop requests
1784 * based on primary request size.
1786 * 1st case (trivial): there are no concurrent activities using
1787 * fuse_set/release_nowrite. Then we're on safe side because
1788 * fuse_flush_writepages() would call fuse_send_writepage()
1791 * 2nd case: someone called fuse_set_nowrite and it is waiting
1792 * now for completion of all in-flight requests. This happens
1793 * rarely and no more than once per page, so this should be
1796 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1797 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1798 * that fuse_set_nowrite returned implies that all in-flight
1799 * requests were completed along with all of their secondary
1800 * requests. Further primary requests are blocked by negative
1801 * writectr. Hence there cannot be any in-flight requests and
1802 * no invocations of fuse_writepage_end() while we're in
1803 * fuse_set_nowrite..fuse_release_nowrite section.
1805 fuse_send_writepage(fm
, next
, inarg
->offset
+ inarg
->size
);
1808 fuse_writepage_finish(fm
, wpa
);
1809 spin_unlock(&fi
->lock
);
1810 fuse_writepage_free(wpa
);
1813 static struct fuse_file
*__fuse_write_file_get(struct fuse_inode
*fi
)
1815 struct fuse_file
*ff
;
1817 spin_lock(&fi
->lock
);
1818 ff
= list_first_entry_or_null(&fi
->write_files
, struct fuse_file
,
1822 spin_unlock(&fi
->lock
);
1827 static struct fuse_file
*fuse_write_file_get(struct fuse_inode
*fi
)
1829 struct fuse_file
*ff
= __fuse_write_file_get(fi
);
1834 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1836 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1837 struct fuse_file
*ff
;
1841 * Inode is always written before the last reference is dropped and
1842 * hence this should not be reached from reclaim.
1844 * Writing back the inode from reclaim can deadlock if the request
1845 * processing itself needs an allocation. Allocations triggering
1846 * reclaim while serving a request can't be prevented, because it can
1847 * involve any number of unrelated userspace processes.
1849 WARN_ON(wbc
->for_reclaim
);
1851 ff
= __fuse_write_file_get(fi
);
1852 err
= fuse_flush_times(inode
, ff
);
1854 fuse_file_put(ff
, false, false);
1859 static struct fuse_writepage_args
*fuse_writepage_args_alloc(void)
1861 struct fuse_writepage_args
*wpa
;
1862 struct fuse_args_pages
*ap
;
1864 wpa
= kzalloc(sizeof(*wpa
), GFP_NOFS
);
1868 ap
->pages
= fuse_pages_alloc(1, GFP_NOFS
, &ap
->descs
);
1878 static void fuse_writepage_add_to_bucket(struct fuse_conn
*fc
,
1879 struct fuse_writepage_args
*wpa
)
1885 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
1887 wpa
->bucket
= rcu_dereference(fc
->curr_bucket
);
1888 } while (unlikely(!atomic_inc_not_zero(&wpa
->bucket
->count
)));
1892 static int fuse_writepage_locked(struct page
*page
)
1894 struct address_space
*mapping
= page
->mapping
;
1895 struct inode
*inode
= mapping
->host
;
1896 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1897 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1898 struct fuse_writepage_args
*wpa
;
1899 struct fuse_args_pages
*ap
;
1900 struct page
*tmp_page
;
1901 int error
= -ENOMEM
;
1903 set_page_writeback(page
);
1905 wpa
= fuse_writepage_args_alloc();
1910 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1915 wpa
->ia
.ff
= fuse_write_file_get(fi
);
1919 fuse_writepage_add_to_bucket(fc
, wpa
);
1920 fuse_write_args_fill(&wpa
->ia
, wpa
->ia
.ff
, page_offset(page
), 0);
1922 copy_highpage(tmp_page
, page
);
1923 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1925 ap
->args
.in_pages
= true;
1927 ap
->pages
[0] = tmp_page
;
1928 ap
->descs
[0].offset
= 0;
1929 ap
->descs
[0].length
= PAGE_SIZE
;
1930 ap
->args
.end
= fuse_writepage_end
;
1933 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1934 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1936 spin_lock(&fi
->lock
);
1937 tree_insert(&fi
->writepages
, wpa
);
1938 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1939 fuse_flush_writepages(inode
);
1940 spin_unlock(&fi
->lock
);
1942 end_page_writeback(page
);
1947 __free_page(tmp_page
);
1951 mapping_set_error(page
->mapping
, error
);
1952 end_page_writeback(page
);
1956 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1960 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1962 * ->writepages() should be called for sync() and friends. We
1963 * should only get here on direct reclaim and then we are
1964 * allowed to skip a page which is already in flight
1966 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1968 redirty_page_for_writepage(wbc
, page
);
1973 err
= fuse_writepage_locked(page
);
1979 struct fuse_fill_wb_data
{
1980 struct fuse_writepage_args
*wpa
;
1981 struct fuse_file
*ff
;
1982 struct inode
*inode
;
1983 struct page
**orig_pages
;
1984 unsigned int max_pages
;
1987 static bool fuse_pages_realloc(struct fuse_fill_wb_data
*data
)
1989 struct fuse_args_pages
*ap
= &data
->wpa
->ia
.ap
;
1990 struct fuse_conn
*fc
= get_fuse_conn(data
->inode
);
1991 struct page
**pages
;
1992 struct fuse_page_desc
*descs
;
1993 unsigned int npages
= min_t(unsigned int,
1994 max_t(unsigned int, data
->max_pages
* 2,
1995 FUSE_DEFAULT_MAX_PAGES_PER_REQ
),
1997 WARN_ON(npages
<= data
->max_pages
);
1999 pages
= fuse_pages_alloc(npages
, GFP_NOFS
, &descs
);
2003 memcpy(pages
, ap
->pages
, sizeof(struct page
*) * ap
->num_pages
);
2004 memcpy(descs
, ap
->descs
, sizeof(struct fuse_page_desc
) * ap
->num_pages
);
2008 data
->max_pages
= npages
;
2013 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
2015 struct fuse_writepage_args
*wpa
= data
->wpa
;
2016 struct inode
*inode
= data
->inode
;
2017 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2018 int num_pages
= wpa
->ia
.ap
.num_pages
;
2021 wpa
->ia
.ff
= fuse_file_get(data
->ff
);
2022 spin_lock(&fi
->lock
);
2023 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
2024 fuse_flush_writepages(inode
);
2025 spin_unlock(&fi
->lock
);
2027 for (i
= 0; i
< num_pages
; i
++)
2028 end_page_writeback(data
->orig_pages
[i
]);
2032 * Check under fi->lock if the page is under writeback, and insert it onto the
2033 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2034 * one already added for a page at this offset. If there's none, then insert
2035 * this new request onto the auxiliary list, otherwise reuse the existing one by
2036 * swapping the new temp page with the old one.
2038 static bool fuse_writepage_add(struct fuse_writepage_args
*new_wpa
,
2041 struct fuse_inode
*fi
= get_fuse_inode(new_wpa
->inode
);
2042 struct fuse_writepage_args
*tmp
;
2043 struct fuse_writepage_args
*old_wpa
;
2044 struct fuse_args_pages
*new_ap
= &new_wpa
->ia
.ap
;
2046 WARN_ON(new_ap
->num_pages
!= 0);
2047 new_ap
->num_pages
= 1;
2049 spin_lock(&fi
->lock
);
2050 old_wpa
= fuse_insert_writeback(&fi
->writepages
, new_wpa
);
2052 spin_unlock(&fi
->lock
);
2056 for (tmp
= old_wpa
->next
; tmp
; tmp
= tmp
->next
) {
2059 WARN_ON(tmp
->inode
!= new_wpa
->inode
);
2060 curr_index
= tmp
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
2061 if (curr_index
== page
->index
) {
2062 WARN_ON(tmp
->ia
.ap
.num_pages
!= 1);
2063 swap(tmp
->ia
.ap
.pages
[0], new_ap
->pages
[0]);
2069 new_wpa
->next
= old_wpa
->next
;
2070 old_wpa
->next
= new_wpa
;
2073 spin_unlock(&fi
->lock
);
2076 struct backing_dev_info
*bdi
= inode_to_bdi(new_wpa
->inode
);
2078 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
2079 dec_node_page_state(new_ap
->pages
[0], NR_WRITEBACK_TEMP
);
2080 wb_writeout_inc(&bdi
->wb
);
2081 fuse_writepage_free(new_wpa
);
2087 static bool fuse_writepage_need_send(struct fuse_conn
*fc
, struct page
*page
,
2088 struct fuse_args_pages
*ap
,
2089 struct fuse_fill_wb_data
*data
)
2091 WARN_ON(!ap
->num_pages
);
2094 * Being under writeback is unlikely but possible. For example direct
2095 * read to an mmaped fuse file will set the page dirty twice; once when
2096 * the pages are faulted with get_user_pages(), and then after the read
2099 if (fuse_page_is_writeback(data
->inode
, page
->index
))
2102 /* Reached max pages */
2103 if (ap
->num_pages
== fc
->max_pages
)
2106 /* Reached max write bytes */
2107 if ((ap
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
)
2111 if (data
->orig_pages
[ap
->num_pages
- 1]->index
+ 1 != page
->index
)
2114 /* Need to grow the pages array? If so, did the expansion fail? */
2115 if (ap
->num_pages
== data
->max_pages
&& !fuse_pages_realloc(data
))
2121 static int fuse_writepages_fill(struct page
*page
,
2122 struct writeback_control
*wbc
, void *_data
)
2124 struct fuse_fill_wb_data
*data
= _data
;
2125 struct fuse_writepage_args
*wpa
= data
->wpa
;
2126 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
2127 struct inode
*inode
= data
->inode
;
2128 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2129 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2130 struct page
*tmp_page
;
2135 data
->ff
= fuse_write_file_get(fi
);
2140 if (wpa
&& fuse_writepage_need_send(fc
, page
, ap
, data
)) {
2141 fuse_writepages_send(data
);
2146 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
2151 * The page must not be redirtied until the writeout is completed
2152 * (i.e. userspace has sent a reply to the write request). Otherwise
2153 * there could be more than one temporary page instance for each real
2156 * This is ensured by holding the page lock in page_mkwrite() while
2157 * checking fuse_page_is_writeback(). We already hold the page lock
2158 * since clear_page_dirty_for_io() and keep it held until we add the
2159 * request to the fi->writepages list and increment ap->num_pages.
2160 * After this fuse_page_is_writeback() will indicate that the page is
2161 * under writeback, so we can release the page lock.
2163 if (data
->wpa
== NULL
) {
2165 wpa
= fuse_writepage_args_alloc();
2167 __free_page(tmp_page
);
2170 fuse_writepage_add_to_bucket(fc
, wpa
);
2172 data
->max_pages
= 1;
2175 fuse_write_args_fill(&wpa
->ia
, data
->ff
, page_offset(page
), 0);
2176 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
2178 ap
->args
.in_pages
= true;
2179 ap
->args
.end
= fuse_writepage_end
;
2183 set_page_writeback(page
);
2185 copy_highpage(tmp_page
, page
);
2186 ap
->pages
[ap
->num_pages
] = tmp_page
;
2187 ap
->descs
[ap
->num_pages
].offset
= 0;
2188 ap
->descs
[ap
->num_pages
].length
= PAGE_SIZE
;
2189 data
->orig_pages
[ap
->num_pages
] = page
;
2191 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
2192 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
2197 * Protected by fi->lock against concurrent access by
2198 * fuse_page_is_writeback().
2200 spin_lock(&fi
->lock
);
2202 spin_unlock(&fi
->lock
);
2203 } else if (fuse_writepage_add(wpa
, page
)) {
2206 end_page_writeback(page
);
2214 static int fuse_writepages(struct address_space
*mapping
,
2215 struct writeback_control
*wbc
)
2217 struct inode
*inode
= mapping
->host
;
2218 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2219 struct fuse_fill_wb_data data
;
2223 if (fuse_is_bad(inode
))
2231 data
.orig_pages
= kcalloc(fc
->max_pages
,
2232 sizeof(struct page
*),
2234 if (!data
.orig_pages
)
2237 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
2239 WARN_ON(!data
.wpa
->ia
.ap
.num_pages
);
2240 fuse_writepages_send(&data
);
2243 fuse_file_put(data
.ff
, false, false);
2245 kfree(data
.orig_pages
);
2251 * It's worthy to make sure that space is reserved on disk for the write,
2252 * but how to implement it without killing performance need more thinking.
2254 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
2255 loff_t pos
, unsigned len
, unsigned flags
,
2256 struct page
**pagep
, void **fsdata
)
2258 pgoff_t index
= pos
>> PAGE_SHIFT
;
2259 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
2264 WARN_ON(!fc
->writeback_cache
);
2266 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2270 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2272 if (PageUptodate(page
) || len
== PAGE_SIZE
)
2275 * Check if the start this page comes after the end of file, in which
2276 * case the readpage can be optimized away.
2278 fsize
= i_size_read(mapping
->host
);
2279 if (fsize
<= (pos
& PAGE_MASK
)) {
2280 size_t off
= pos
& ~PAGE_MASK
;
2282 zero_user_segment(page
, 0, off
);
2285 err
= fuse_do_readpage(file
, page
);
2299 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2300 loff_t pos
, unsigned len
, unsigned copied
,
2301 struct page
*page
, void *fsdata
)
2303 struct inode
*inode
= page
->mapping
->host
;
2305 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2310 if (!PageUptodate(page
)) {
2311 /* Zero any unwritten bytes at the end of the page */
2312 size_t endoff
= pos
& ~PAGE_MASK
;
2314 zero_user_segment(page
, endoff
, PAGE_SIZE
);
2315 SetPageUptodate(page
);
2318 if (pos
> inode
->i_size
)
2319 i_size_write(inode
, pos
);
2321 set_page_dirty(page
);
2330 static int fuse_launder_page(struct page
*page
)
2333 if (clear_page_dirty_for_io(page
)) {
2334 struct inode
*inode
= page
->mapping
->host
;
2336 /* Serialize with pending writeback for the same page */
2337 fuse_wait_on_page_writeback(inode
, page
->index
);
2338 err
= fuse_writepage_locked(page
);
2340 fuse_wait_on_page_writeback(inode
, page
->index
);
2346 * Write back dirty data/metadata now (there may not be any suitable
2347 * open files later for data)
2349 static void fuse_vma_close(struct vm_area_struct
*vma
)
2353 err
= write_inode_now(vma
->vm_file
->f_mapping
->host
, 1);
2354 mapping_set_error(vma
->vm_file
->f_mapping
, err
);
2358 * Wait for writeback against this page to complete before allowing it
2359 * to be marked dirty again, and hence written back again, possibly
2360 * before the previous writepage completed.
2362 * Block here, instead of in ->writepage(), so that the userspace fs
2363 * can only block processes actually operating on the filesystem.
2365 * Otherwise unprivileged userspace fs would be able to block
2370 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2372 static vm_fault_t
fuse_page_mkwrite(struct vm_fault
*vmf
)
2374 struct page
*page
= vmf
->page
;
2375 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
2377 file_update_time(vmf
->vma
->vm_file
);
2379 if (page
->mapping
!= inode
->i_mapping
) {
2381 return VM_FAULT_NOPAGE
;
2384 fuse_wait_on_page_writeback(inode
, page
->index
);
2385 return VM_FAULT_LOCKED
;
2388 static const struct vm_operations_struct fuse_file_vm_ops
= {
2389 .close
= fuse_vma_close
,
2390 .fault
= filemap_fault
,
2391 .map_pages
= filemap_map_pages
,
2392 .page_mkwrite
= fuse_page_mkwrite
,
2395 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2397 struct fuse_file
*ff
= file
->private_data
;
2399 /* DAX mmap is superior to direct_io mmap */
2400 if (FUSE_IS_DAX(file_inode(file
)))
2401 return fuse_dax_mmap(file
, vma
);
2403 if (ff
->open_flags
& FOPEN_DIRECT_IO
) {
2404 /* Can't provide the coherency needed for MAP_SHARED */
2405 if (vma
->vm_flags
& VM_MAYSHARE
)
2408 invalidate_inode_pages2(file
->f_mapping
);
2410 return generic_file_mmap(file
, vma
);
2413 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2414 fuse_link_write_file(file
);
2416 file_accessed(file
);
2417 vma
->vm_ops
= &fuse_file_vm_ops
;
2421 static int convert_fuse_file_lock(struct fuse_conn
*fc
,
2422 const struct fuse_file_lock
*ffl
,
2423 struct file_lock
*fl
)
2425 switch (ffl
->type
) {
2431 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2432 ffl
->end
< ffl
->start
)
2435 fl
->fl_start
= ffl
->start
;
2436 fl
->fl_end
= ffl
->end
;
2439 * Convert pid into init's pid namespace. The locks API will
2440 * translate it into the caller's pid namespace.
2443 fl
->fl_pid
= pid_nr_ns(find_pid_ns(ffl
->pid
, fc
->pid_ns
), &init_pid_ns
);
2450 fl
->fl_type
= ffl
->type
;
2454 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2455 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2456 int flock
, struct fuse_lk_in
*inarg
)
2458 struct inode
*inode
= file_inode(file
);
2459 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2460 struct fuse_file
*ff
= file
->private_data
;
2462 memset(inarg
, 0, sizeof(*inarg
));
2464 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2465 inarg
->lk
.start
= fl
->fl_start
;
2466 inarg
->lk
.end
= fl
->fl_end
;
2467 inarg
->lk
.type
= fl
->fl_type
;
2468 inarg
->lk
.pid
= pid
;
2470 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2471 args
->opcode
= opcode
;
2472 args
->nodeid
= get_node_id(inode
);
2473 args
->in_numargs
= 1;
2474 args
->in_args
[0].size
= sizeof(*inarg
);
2475 args
->in_args
[0].value
= inarg
;
2478 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2480 struct inode
*inode
= file_inode(file
);
2481 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2483 struct fuse_lk_in inarg
;
2484 struct fuse_lk_out outarg
;
2487 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2488 args
.out_numargs
= 1;
2489 args
.out_args
[0].size
= sizeof(outarg
);
2490 args
.out_args
[0].value
= &outarg
;
2491 err
= fuse_simple_request(fm
, &args
);
2493 err
= convert_fuse_file_lock(fm
->fc
, &outarg
.lk
, fl
);
2498 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2500 struct inode
*inode
= file_inode(file
);
2501 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2503 struct fuse_lk_in inarg
;
2504 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2505 struct pid
*pid
= fl
->fl_type
!= F_UNLCK
? task_tgid(current
) : NULL
;
2506 pid_t pid_nr
= pid_nr_ns(pid
, fm
->fc
->pid_ns
);
2509 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2510 /* NLM needs asynchronous locks, which we don't support yet */
2514 /* Unlock on close is handled by the flush method */
2515 if ((fl
->fl_flags
& FL_CLOSE_POSIX
) == FL_CLOSE_POSIX
)
2518 fuse_lk_fill(&args
, file
, fl
, opcode
, pid_nr
, flock
, &inarg
);
2519 err
= fuse_simple_request(fm
, &args
);
2521 /* locking is restartable */
2528 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2530 struct inode
*inode
= file_inode(file
);
2531 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2534 if (cmd
== F_CANCELLK
) {
2536 } else if (cmd
== F_GETLK
) {
2538 posix_test_lock(file
, fl
);
2541 err
= fuse_getlk(file
, fl
);
2544 err
= posix_lock_file(file
, fl
, NULL
);
2546 err
= fuse_setlk(file
, fl
, 0);
2551 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2553 struct inode
*inode
= file_inode(file
);
2554 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2558 err
= locks_lock_file_wait(file
, fl
);
2560 struct fuse_file
*ff
= file
->private_data
;
2562 /* emulate flock with POSIX locks */
2564 err
= fuse_setlk(file
, fl
, 1);
2570 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2572 struct inode
*inode
= mapping
->host
;
2573 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2575 struct fuse_bmap_in inarg
;
2576 struct fuse_bmap_out outarg
;
2579 if (!inode
->i_sb
->s_bdev
|| fm
->fc
->no_bmap
)
2582 memset(&inarg
, 0, sizeof(inarg
));
2583 inarg
.block
= block
;
2584 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2585 args
.opcode
= FUSE_BMAP
;
2586 args
.nodeid
= get_node_id(inode
);
2587 args
.in_numargs
= 1;
2588 args
.in_args
[0].size
= sizeof(inarg
);
2589 args
.in_args
[0].value
= &inarg
;
2590 args
.out_numargs
= 1;
2591 args
.out_args
[0].size
= sizeof(outarg
);
2592 args
.out_args
[0].value
= &outarg
;
2593 err
= fuse_simple_request(fm
, &args
);
2595 fm
->fc
->no_bmap
= 1;
2597 return err
? 0 : outarg
.block
;
2600 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2602 struct inode
*inode
= file
->f_mapping
->host
;
2603 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2604 struct fuse_file
*ff
= file
->private_data
;
2606 struct fuse_lseek_in inarg
= {
2611 struct fuse_lseek_out outarg
;
2614 if (fm
->fc
->no_lseek
)
2617 args
.opcode
= FUSE_LSEEK
;
2618 args
.nodeid
= ff
->nodeid
;
2619 args
.in_numargs
= 1;
2620 args
.in_args
[0].size
= sizeof(inarg
);
2621 args
.in_args
[0].value
= &inarg
;
2622 args
.out_numargs
= 1;
2623 args
.out_args
[0].size
= sizeof(outarg
);
2624 args
.out_args
[0].value
= &outarg
;
2625 err
= fuse_simple_request(fm
, &args
);
2627 if (err
== -ENOSYS
) {
2628 fm
->fc
->no_lseek
= 1;
2634 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2637 err
= fuse_update_attributes(inode
, file
);
2639 return generic_file_llseek(file
, offset
, whence
);
2644 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2647 struct inode
*inode
= file_inode(file
);
2652 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2653 retval
= generic_file_llseek(file
, offset
, whence
);
2657 retval
= fuse_update_attributes(inode
, file
);
2659 retval
= generic_file_llseek(file
, offset
, whence
);
2660 inode_unlock(inode
);
2665 retval
= fuse_lseek(file
, offset
, whence
);
2666 inode_unlock(inode
);
2676 * All files which have been polled are linked to RB tree
2677 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2678 * find the matching one.
2680 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2681 struct rb_node
**parent_out
)
2683 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2684 struct rb_node
*last
= NULL
;
2687 struct fuse_file
*ff
;
2690 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2693 link
= &last
->rb_left
;
2694 else if (kh
> ff
->kh
)
2695 link
= &last
->rb_right
;
2706 * The file is about to be polled. Make sure it's on the polled_files
2707 * RB tree. Note that files once added to the polled_files tree are
2708 * not removed before the file is released. This is because a file
2709 * polled once is likely to be polled again.
2711 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2712 struct fuse_file
*ff
)
2714 spin_lock(&fc
->lock
);
2715 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2716 struct rb_node
**link
, *parent
;
2718 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2720 rb_link_node(&ff
->polled_node
, parent
, link
);
2721 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2723 spin_unlock(&fc
->lock
);
2726 __poll_t
fuse_file_poll(struct file
*file
, poll_table
*wait
)
2728 struct fuse_file
*ff
= file
->private_data
;
2729 struct fuse_mount
*fm
= ff
->fm
;
2730 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2731 struct fuse_poll_out outarg
;
2735 if (fm
->fc
->no_poll
)
2736 return DEFAULT_POLLMASK
;
2738 poll_wait(file
, &ff
->poll_wait
, wait
);
2739 inarg
.events
= mangle_poll(poll_requested_events(wait
));
2742 * Ask for notification iff there's someone waiting for it.
2743 * The client may ignore the flag and always notify.
2745 if (waitqueue_active(&ff
->poll_wait
)) {
2746 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2747 fuse_register_polled_file(fm
->fc
, ff
);
2750 args
.opcode
= FUSE_POLL
;
2751 args
.nodeid
= ff
->nodeid
;
2752 args
.in_numargs
= 1;
2753 args
.in_args
[0].size
= sizeof(inarg
);
2754 args
.in_args
[0].value
= &inarg
;
2755 args
.out_numargs
= 1;
2756 args
.out_args
[0].size
= sizeof(outarg
);
2757 args
.out_args
[0].value
= &outarg
;
2758 err
= fuse_simple_request(fm
, &args
);
2761 return demangle_poll(outarg
.revents
);
2762 if (err
== -ENOSYS
) {
2763 fm
->fc
->no_poll
= 1;
2764 return DEFAULT_POLLMASK
;
2768 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2771 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2772 * wakes up the poll waiters.
2774 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2775 struct fuse_notify_poll_wakeup_out
*outarg
)
2777 u64 kh
= outarg
->kh
;
2778 struct rb_node
**link
;
2780 spin_lock(&fc
->lock
);
2782 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2784 struct fuse_file
*ff
;
2786 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2787 wake_up_interruptible_sync(&ff
->poll_wait
);
2790 spin_unlock(&fc
->lock
);
2794 static void fuse_do_truncate(struct file
*file
)
2796 struct inode
*inode
= file
->f_mapping
->host
;
2799 attr
.ia_valid
= ATTR_SIZE
;
2800 attr
.ia_size
= i_size_read(inode
);
2802 attr
.ia_file
= file
;
2803 attr
.ia_valid
|= ATTR_FILE
;
2805 fuse_do_setattr(file_dentry(file
), &attr
, file
);
2808 static inline loff_t
fuse_round_up(struct fuse_conn
*fc
, loff_t off
)
2810 return round_up(off
, fc
->max_pages
<< PAGE_SHIFT
);
2814 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2816 DECLARE_COMPLETION_ONSTACK(wait
);
2818 struct file
*file
= iocb
->ki_filp
;
2819 struct fuse_file
*ff
= file
->private_data
;
2821 struct inode
*inode
;
2823 size_t count
= iov_iter_count(iter
), shortened
= 0;
2824 loff_t offset
= iocb
->ki_pos
;
2825 struct fuse_io_priv
*io
;
2828 inode
= file
->f_mapping
->host
;
2829 i_size
= i_size_read(inode
);
2831 if ((iov_iter_rw(iter
) == READ
) && (offset
>= i_size
))
2834 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2837 spin_lock_init(&io
->lock
);
2838 kref_init(&io
->refcnt
);
2842 io
->offset
= offset
;
2843 io
->write
= (iov_iter_rw(iter
) == WRITE
);
2846 * By default, we want to optimize all I/Os with async request
2847 * submission to the client filesystem if supported.
2849 io
->async
= ff
->fm
->fc
->async_dio
;
2851 io
->blocking
= is_sync_kiocb(iocb
);
2853 /* optimization for short read */
2854 if (io
->async
&& !io
->write
&& offset
+ count
> i_size
) {
2855 iov_iter_truncate(iter
, fuse_round_up(ff
->fm
->fc
, i_size
- offset
));
2856 shortened
= count
- iov_iter_count(iter
);
2861 * We cannot asynchronously extend the size of a file.
2862 * In such case the aio will behave exactly like sync io.
2864 if ((offset
+ count
> i_size
) && io
->write
)
2865 io
->blocking
= true;
2867 if (io
->async
&& io
->blocking
) {
2869 * Additional reference to keep io around after
2870 * calling fuse_aio_complete()
2872 kref_get(&io
->refcnt
);
2876 if (iov_iter_rw(iter
) == WRITE
) {
2877 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
2878 fuse_invalidate_attr_mask(inode
, FUSE_STATX_MODSIZE
);
2880 ret
= __fuse_direct_read(io
, iter
, &pos
);
2882 iov_iter_reexpand(iter
, iov_iter_count(iter
) + shortened
);
2885 bool blocking
= io
->blocking
;
2887 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2889 /* we have a non-extending, async request, so return */
2891 return -EIOCBQUEUED
;
2893 wait_for_completion(&wait
);
2894 ret
= fuse_get_res_by_io(io
);
2897 kref_put(&io
->refcnt
, fuse_io_release
);
2899 if (iov_iter_rw(iter
) == WRITE
) {
2900 fuse_write_update_attr(inode
, pos
, ret
);
2901 if (ret
< 0 && offset
+ count
> i_size
)
2902 fuse_do_truncate(file
);
2908 static int fuse_writeback_range(struct inode
*inode
, loff_t start
, loff_t end
)
2910 int err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, -1);
2913 fuse_sync_writes(inode
);
2918 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2921 struct fuse_file
*ff
= file
->private_data
;
2922 struct inode
*inode
= file_inode(file
);
2923 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2924 struct fuse_mount
*fm
= ff
->fm
;
2926 struct fuse_fallocate_in inarg
= {
2933 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2934 (mode
& (FALLOC_FL_PUNCH_HOLE
|
2935 FALLOC_FL_ZERO_RANGE
));
2937 bool block_faults
= FUSE_IS_DAX(inode
) && lock_inode
;
2939 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
2940 FALLOC_FL_ZERO_RANGE
))
2943 if (fm
->fc
->no_fallocate
)
2949 filemap_invalidate_lock(inode
->i_mapping
);
2950 err
= fuse_dax_break_layouts(inode
, 0, 0);
2955 if (mode
& (FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_ZERO_RANGE
)) {
2956 loff_t endbyte
= offset
+ length
- 1;
2958 err
= fuse_writeback_range(inode
, offset
, endbyte
);
2964 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
2965 offset
+ length
> i_size_read(inode
)) {
2966 err
= inode_newsize_ok(inode
, offset
+ length
);
2971 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2972 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2974 args
.opcode
= FUSE_FALLOCATE
;
2975 args
.nodeid
= ff
->nodeid
;
2976 args
.in_numargs
= 1;
2977 args
.in_args
[0].size
= sizeof(inarg
);
2978 args
.in_args
[0].value
= &inarg
;
2979 err
= fuse_simple_request(fm
, &args
);
2980 if (err
== -ENOSYS
) {
2981 fm
->fc
->no_fallocate
= 1;
2987 /* we could have extended the file */
2988 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
2989 bool changed
= fuse_write_update_attr(inode
, offset
+ length
,
2992 if (changed
&& fm
->fc
->writeback_cache
)
2993 file_update_time(file
);
2996 if (mode
& (FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_ZERO_RANGE
))
2997 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2999 fuse_invalidate_attr_mask(inode
, FUSE_STATX_MODSIZE
);
3002 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3003 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3006 filemap_invalidate_unlock(inode
->i_mapping
);
3009 inode_unlock(inode
);
3011 fuse_flush_time_update(inode
);
3016 static ssize_t
__fuse_copy_file_range(struct file
*file_in
, loff_t pos_in
,
3017 struct file
*file_out
, loff_t pos_out
,
3018 size_t len
, unsigned int flags
)
3020 struct fuse_file
*ff_in
= file_in
->private_data
;
3021 struct fuse_file
*ff_out
= file_out
->private_data
;
3022 struct inode
*inode_in
= file_inode(file_in
);
3023 struct inode
*inode_out
= file_inode(file_out
);
3024 struct fuse_inode
*fi_out
= get_fuse_inode(inode_out
);
3025 struct fuse_mount
*fm
= ff_in
->fm
;
3026 struct fuse_conn
*fc
= fm
->fc
;
3028 struct fuse_copy_file_range_in inarg
= {
3031 .nodeid_out
= ff_out
->nodeid
,
3032 .fh_out
= ff_out
->fh
,
3037 struct fuse_write_out outarg
;
3039 /* mark unstable when write-back is not used, and file_out gets
3041 bool is_unstable
= (!fc
->writeback_cache
) &&
3042 ((pos_out
+ len
) > inode_out
->i_size
);
3044 if (fc
->no_copy_file_range
)
3047 if (file_inode(file_in
)->i_sb
!= file_inode(file_out
)->i_sb
)
3050 inode_lock(inode_in
);
3051 err
= fuse_writeback_range(inode_in
, pos_in
, pos_in
+ len
- 1);
3052 inode_unlock(inode_in
);
3056 inode_lock(inode_out
);
3058 err
= file_modified(file_out
);
3063 * Write out dirty pages in the destination file before sending the COPY
3064 * request to userspace. After the request is completed, truncate off
3065 * pages (including partial ones) from the cache that have been copied,
3066 * since these contain stale data at that point.
3068 * This should be mostly correct, but if the COPY writes to partial
3069 * pages (at the start or end) and the parts not covered by the COPY are
3070 * written through a memory map after calling fuse_writeback_range(),
3071 * then these partial page modifications will be lost on truncation.
3073 * It is unlikely that someone would rely on such mixed style
3074 * modifications. Yet this does give less guarantees than if the
3075 * copying was performed with write(2).
3077 * To fix this a mapping->invalidate_lock could be used to prevent new
3078 * faults while the copy is ongoing.
3080 err
= fuse_writeback_range(inode_out
, pos_out
, pos_out
+ len
- 1);
3085 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3087 args
.opcode
= FUSE_COPY_FILE_RANGE
;
3088 args
.nodeid
= ff_in
->nodeid
;
3089 args
.in_numargs
= 1;
3090 args
.in_args
[0].size
= sizeof(inarg
);
3091 args
.in_args
[0].value
= &inarg
;
3092 args
.out_numargs
= 1;
3093 args
.out_args
[0].size
= sizeof(outarg
);
3094 args
.out_args
[0].value
= &outarg
;
3095 err
= fuse_simple_request(fm
, &args
);
3096 if (err
== -ENOSYS
) {
3097 fc
->no_copy_file_range
= 1;
3103 truncate_inode_pages_range(inode_out
->i_mapping
,
3104 ALIGN_DOWN(pos_out
, PAGE_SIZE
),
3105 ALIGN(pos_out
+ outarg
.size
, PAGE_SIZE
) - 1);
3107 if (fc
->writeback_cache
) {
3108 fuse_write_update_attr(inode_out
, pos_out
+ outarg
.size
,
3110 file_update_time(file_out
);
3113 fuse_invalidate_attr_mask(inode_out
, FUSE_STATX_MODSIZE
);
3118 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3120 inode_unlock(inode_out
);
3121 file_accessed(file_in
);
3123 fuse_flush_time_update(inode_out
);
3128 static ssize_t
fuse_copy_file_range(struct file
*src_file
, loff_t src_off
,
3129 struct file
*dst_file
, loff_t dst_off
,
3130 size_t len
, unsigned int flags
)
3134 ret
= __fuse_copy_file_range(src_file
, src_off
, dst_file
, dst_off
,
3137 if (ret
== -EOPNOTSUPP
|| ret
== -EXDEV
)
3138 ret
= generic_copy_file_range(src_file
, src_off
, dst_file
,
3139 dst_off
, len
, flags
);
3143 static const struct file_operations fuse_file_operations
= {
3144 .llseek
= fuse_file_llseek
,
3145 .read_iter
= fuse_file_read_iter
,
3146 .write_iter
= fuse_file_write_iter
,
3147 .mmap
= fuse_file_mmap
,
3149 .flush
= fuse_flush
,
3150 .release
= fuse_release
,
3151 .fsync
= fuse_fsync
,
3152 .lock
= fuse_file_lock
,
3153 .get_unmapped_area
= thp_get_unmapped_area
,
3154 .flock
= fuse_file_flock
,
3155 .splice_read
= generic_file_splice_read
,
3156 .splice_write
= iter_file_splice_write
,
3157 .unlocked_ioctl
= fuse_file_ioctl
,
3158 .compat_ioctl
= fuse_file_compat_ioctl
,
3159 .poll
= fuse_file_poll
,
3160 .fallocate
= fuse_file_fallocate
,
3161 .copy_file_range
= fuse_copy_file_range
,
3164 static const struct address_space_operations fuse_file_aops
= {
3165 .readpage
= fuse_readpage
,
3166 .readahead
= fuse_readahead
,
3167 .writepage
= fuse_writepage
,
3168 .writepages
= fuse_writepages
,
3169 .launder_page
= fuse_launder_page
,
3170 .set_page_dirty
= __set_page_dirty_nobuffers
,
3172 .direct_IO
= fuse_direct_IO
,
3173 .write_begin
= fuse_write_begin
,
3174 .write_end
= fuse_write_end
,
3177 void fuse_init_file_inode(struct inode
*inode
)
3179 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3181 inode
->i_fop
= &fuse_file_operations
;
3182 inode
->i_data
.a_ops
= &fuse_file_aops
;
3184 INIT_LIST_HEAD(&fi
->write_files
);
3185 INIT_LIST_HEAD(&fi
->queued_writes
);
3187 init_waitqueue_head(&fi
->page_waitq
);
3188 fi
->writepages
= RB_ROOT
;
3190 if (IS_ENABLED(CONFIG_FUSE_DAX
))
3191 fuse_dax_inode_init(inode
);