1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/swap.h>
18 #include <linux/prefetch.h>
19 #include <linux/uio.h>
20 #include <linux/cleancache.h>
21 #include <linux/sched/signal.h>
27 #include <trace/events/f2fs.h>
29 #define NUM_PREALLOC_POST_READ_CTXS 128
31 static struct kmem_cache
*bio_post_read_ctx_cache
;
32 static struct kmem_cache
*bio_entry_slab
;
33 static mempool_t
*bio_post_read_ctx_pool
;
34 static struct bio_set f2fs_bioset
;
36 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
38 int __init
f2fs_init_bioset(void)
40 if (bioset_init(&f2fs_bioset
, F2FS_BIO_POOL_SIZE
,
41 0, BIOSET_NEED_BVECS
))
46 void f2fs_destroy_bioset(void)
48 bioset_exit(&f2fs_bioset
);
51 static inline struct bio
*__f2fs_bio_alloc(gfp_t gfp_mask
,
52 unsigned int nr_iovecs
)
54 return bio_alloc_bioset(gfp_mask
, nr_iovecs
, &f2fs_bioset
);
57 struct bio
*f2fs_bio_alloc(struct f2fs_sb_info
*sbi
, int npages
, bool no_fail
)
62 /* No failure on bio allocation */
63 bio
= __f2fs_bio_alloc(GFP_NOIO
, npages
);
65 bio
= __f2fs_bio_alloc(GFP_NOIO
| __GFP_NOFAIL
, npages
);
68 if (time_to_inject(sbi
, FAULT_ALLOC_BIO
)) {
69 f2fs_show_injection_info(sbi
, FAULT_ALLOC_BIO
);
73 return __f2fs_bio_alloc(GFP_KERNEL
, npages
);
76 static bool __is_cp_guaranteed(struct page
*page
)
78 struct address_space
*mapping
= page
->mapping
;
80 struct f2fs_sb_info
*sbi
;
85 if (f2fs_is_compressed_page(page
))
88 inode
= mapping
->host
;
89 sbi
= F2FS_I_SB(inode
);
91 if (inode
->i_ino
== F2FS_META_INO(sbi
) ||
92 inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
93 S_ISDIR(inode
->i_mode
) ||
94 (S_ISREG(inode
->i_mode
) &&
95 (f2fs_is_atomic_file(inode
) || IS_NOQUOTA(inode
))) ||
101 static enum count_type
__read_io_type(struct page
*page
)
103 struct address_space
*mapping
= page_file_mapping(page
);
106 struct inode
*inode
= mapping
->host
;
107 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
109 if (inode
->i_ino
== F2FS_META_INO(sbi
))
112 if (inode
->i_ino
== F2FS_NODE_INO(sbi
))
118 /* postprocessing steps for read bios */
119 enum bio_post_read_step
{
125 struct bio_post_read_ctx
{
127 struct f2fs_sb_info
*sbi
;
128 struct work_struct work
;
129 unsigned int enabled_steps
;
132 static void __read_end_io(struct bio
*bio
, bool compr
, bool verity
)
136 struct bvec_iter_all iter_all
;
138 bio_for_each_segment_all(bv
, bio
, iter_all
) {
141 #ifdef CONFIG_F2FS_FS_COMPRESSION
142 if (compr
&& f2fs_is_compressed_page(page
)) {
143 f2fs_decompress_pages(bio
, page
, verity
);
148 /* PG_error was set if any post_read step failed */
149 if (bio
->bi_status
|| PageError(page
)) {
150 ClearPageUptodate(page
);
151 /* will re-read again later */
152 ClearPageError(page
);
154 SetPageUptodate(page
);
156 dec_page_count(F2FS_P_SB(page
), __read_io_type(page
));
161 static void f2fs_release_read_bio(struct bio
*bio
);
162 static void __f2fs_read_end_io(struct bio
*bio
, bool compr
, bool verity
)
165 __read_end_io(bio
, false, verity
);
166 f2fs_release_read_bio(bio
);
169 static void f2fs_decompress_bio(struct bio
*bio
, bool verity
)
171 __read_end_io(bio
, true, verity
);
174 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
);
176 static void f2fs_decrypt_work(struct bio_post_read_ctx
*ctx
)
178 fscrypt_decrypt_bio(ctx
->bio
);
181 static void f2fs_decompress_work(struct bio_post_read_ctx
*ctx
)
183 f2fs_decompress_bio(ctx
->bio
, ctx
->enabled_steps
& (1 << STEP_VERITY
));
186 #ifdef CONFIG_F2FS_FS_COMPRESSION
187 static void f2fs_verify_pages(struct page
**rpages
, unsigned int cluster_size
)
189 f2fs_decompress_end_io(rpages
, cluster_size
, false, true);
192 static void f2fs_verify_bio(struct bio
*bio
)
195 struct bvec_iter_all iter_all
;
197 bio_for_each_segment_all(bv
, bio
, iter_all
) {
198 struct page
*page
= bv
->bv_page
;
199 struct decompress_io_ctx
*dic
;
201 dic
= (struct decompress_io_ctx
*)page_private(page
);
204 if (refcount_dec_not_one(&dic
->ref
))
206 f2fs_verify_pages(dic
->rpages
,
212 if (bio
->bi_status
|| PageError(page
))
215 if (fsverity_verify_page(page
)) {
216 SetPageUptodate(page
);
220 ClearPageUptodate(page
);
221 ClearPageError(page
);
228 static void f2fs_verity_work(struct work_struct
*work
)
230 struct bio_post_read_ctx
*ctx
=
231 container_of(work
, struct bio_post_read_ctx
, work
);
232 struct bio
*bio
= ctx
->bio
;
233 #ifdef CONFIG_F2FS_FS_COMPRESSION
234 unsigned int enabled_steps
= ctx
->enabled_steps
;
238 * fsverity_verify_bio() may call readpages() again, and while verity
239 * will be disabled for this, decryption may still be needed, resulting
240 * in another bio_post_read_ctx being allocated. So to prevent
241 * deadlocks we need to release the current ctx to the mempool first.
242 * This assumes that verity is the last post-read step.
244 mempool_free(ctx
, bio_post_read_ctx_pool
);
245 bio
->bi_private
= NULL
;
247 #ifdef CONFIG_F2FS_FS_COMPRESSION
248 /* previous step is decompression */
249 if (enabled_steps
& (1 << STEP_DECOMPRESS
)) {
250 f2fs_verify_bio(bio
);
251 f2fs_release_read_bio(bio
);
256 fsverity_verify_bio(bio
);
257 __f2fs_read_end_io(bio
, false, false);
260 static void f2fs_post_read_work(struct work_struct
*work
)
262 struct bio_post_read_ctx
*ctx
=
263 container_of(work
, struct bio_post_read_ctx
, work
);
265 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
))
266 f2fs_decrypt_work(ctx
);
268 if (ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
))
269 f2fs_decompress_work(ctx
);
271 if (ctx
->enabled_steps
& (1 << STEP_VERITY
)) {
272 INIT_WORK(&ctx
->work
, f2fs_verity_work
);
273 fsverity_enqueue_verify_work(&ctx
->work
);
277 __f2fs_read_end_io(ctx
->bio
,
278 ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
), false);
281 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info
*sbi
,
282 struct work_struct
*work
)
284 queue_work(sbi
->post_read_wq
, work
);
287 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
)
290 * We use different work queues for decryption and for verity because
291 * verity may require reading metadata pages that need decryption, and
292 * we shouldn't recurse to the same workqueue.
295 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
) ||
296 ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
)) {
297 INIT_WORK(&ctx
->work
, f2fs_post_read_work
);
298 f2fs_enqueue_post_read_work(ctx
->sbi
, &ctx
->work
);
302 if (ctx
->enabled_steps
& (1 << STEP_VERITY
)) {
303 INIT_WORK(&ctx
->work
, f2fs_verity_work
);
304 fsverity_enqueue_verify_work(&ctx
->work
);
308 __f2fs_read_end_io(ctx
->bio
, false, false);
311 static bool f2fs_bio_post_read_required(struct bio
*bio
)
313 return bio
->bi_private
;
316 static void f2fs_read_end_io(struct bio
*bio
)
318 struct f2fs_sb_info
*sbi
= F2FS_P_SB(bio_first_page_all(bio
));
320 if (time_to_inject(sbi
, FAULT_READ_IO
)) {
321 f2fs_show_injection_info(sbi
, FAULT_READ_IO
);
322 bio
->bi_status
= BLK_STS_IOERR
;
325 if (f2fs_bio_post_read_required(bio
)) {
326 struct bio_post_read_ctx
*ctx
= bio
->bi_private
;
328 bio_post_read_processing(ctx
);
332 __f2fs_read_end_io(bio
, false, false);
335 static void f2fs_write_end_io(struct bio
*bio
)
337 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
338 struct bio_vec
*bvec
;
339 struct bvec_iter_all iter_all
;
341 if (time_to_inject(sbi
, FAULT_WRITE_IO
)) {
342 f2fs_show_injection_info(sbi
, FAULT_WRITE_IO
);
343 bio
->bi_status
= BLK_STS_IOERR
;
346 bio_for_each_segment_all(bvec
, bio
, iter_all
) {
347 struct page
*page
= bvec
->bv_page
;
348 enum count_type type
= WB_DATA_TYPE(page
);
350 if (IS_DUMMY_WRITTEN_PAGE(page
)) {
351 set_page_private(page
, (unsigned long)NULL
);
352 ClearPagePrivate(page
);
354 mempool_free(page
, sbi
->write_io_dummy
);
356 if (unlikely(bio
->bi_status
))
357 f2fs_stop_checkpoint(sbi
, true);
361 fscrypt_finalize_bounce_page(&page
);
363 #ifdef CONFIG_F2FS_FS_COMPRESSION
364 if (f2fs_is_compressed_page(page
)) {
365 f2fs_compress_write_end_io(bio
, page
);
370 if (unlikely(bio
->bi_status
)) {
371 mapping_set_error(page
->mapping
, -EIO
);
372 if (type
== F2FS_WB_CP_DATA
)
373 f2fs_stop_checkpoint(sbi
, true);
376 f2fs_bug_on(sbi
, page
->mapping
== NODE_MAPPING(sbi
) &&
377 page
->index
!= nid_of_node(page
));
379 dec_page_count(sbi
, type
);
380 if (f2fs_in_warm_node_list(sbi
, page
))
381 f2fs_del_fsync_node_entry(sbi
, page
);
382 clear_cold_data(page
);
383 end_page_writeback(page
);
385 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
386 wq_has_sleeper(&sbi
->cp_wait
))
387 wake_up(&sbi
->cp_wait
);
393 * Return true, if pre_bio's bdev is same as its target device.
395 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
396 block_t blk_addr
, struct bio
*bio
)
398 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
401 if (f2fs_is_multi_device(sbi
)) {
402 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
403 if (FDEV(i
).start_blk
<= blk_addr
&&
404 FDEV(i
).end_blk
>= blk_addr
) {
405 blk_addr
-= FDEV(i
).start_blk
;
412 bio_set_dev(bio
, bdev
);
413 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
418 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
422 if (!f2fs_is_multi_device(sbi
))
425 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
426 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
431 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
432 block_t blk_addr
, struct bio
*bio
)
434 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
435 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
439 * Low-level block read/write IO operations.
441 static struct bio
*__bio_alloc(struct f2fs_io_info
*fio
, int npages
)
443 struct f2fs_sb_info
*sbi
= fio
->sbi
;
446 bio
= f2fs_bio_alloc(sbi
, npages
, true);
448 f2fs_target_device(sbi
, fio
->new_blkaddr
, bio
);
449 if (is_read_io(fio
->op
)) {
450 bio
->bi_end_io
= f2fs_read_end_io
;
451 bio
->bi_private
= NULL
;
453 bio
->bi_end_io
= f2fs_write_end_io
;
454 bio
->bi_private
= sbi
;
455 bio
->bi_write_hint
= f2fs_io_type_to_rw_hint(sbi
,
456 fio
->type
, fio
->temp
);
459 wbc_init_bio(fio
->io_wbc
, bio
);
464 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
465 struct bio
*bio
, enum page_type type
)
467 if (!is_read_io(bio_op(bio
))) {
470 if (type
!= DATA
&& type
!= NODE
)
473 if (test_opt(sbi
, LFS
) && current
->plug
)
474 blk_finish_plug(current
->plug
);
476 if (F2FS_IO_ALIGNED(sbi
))
479 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
480 start
%= F2FS_IO_SIZE(sbi
);
485 /* fill dummy pages */
486 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
488 mempool_alloc(sbi
->write_io_dummy
,
489 GFP_NOIO
| __GFP_NOFAIL
);
490 f2fs_bug_on(sbi
, !page
);
492 zero_user_segment(page
, 0, PAGE_SIZE
);
493 SetPagePrivate(page
);
494 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
496 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
500 * In the NODE case, we lose next block address chain. So, we
501 * need to do checkpoint in f2fs_sync_file.
504 set_sbi_flag(sbi
, SBI_NEED_CP
);
507 if (is_read_io(bio_op(bio
)))
508 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
510 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
514 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
,
515 struct bio
*bio
, enum page_type type
)
517 __submit_bio(sbi
, bio
, type
);
520 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
522 struct f2fs_io_info
*fio
= &io
->fio
;
527 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
529 if (is_read_io(fio
->op
))
530 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
532 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
534 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
538 static bool __has_merged_page(struct bio
*bio
, struct inode
*inode
,
539 struct page
*page
, nid_t ino
)
541 struct bio_vec
*bvec
;
542 struct bvec_iter_all iter_all
;
547 if (!inode
&& !page
&& !ino
)
550 bio_for_each_segment_all(bvec
, bio
, iter_all
) {
551 struct page
*target
= bvec
->bv_page
;
553 if (fscrypt_is_bounce_page(target
)) {
554 target
= fscrypt_pagecache_page(target
);
558 if (f2fs_is_compressed_page(target
)) {
559 target
= f2fs_compress_control_page(target
);
564 if (inode
&& inode
== target
->mapping
->host
)
566 if (page
&& page
== target
)
568 if (ino
&& ino
== ino_of_node(target
))
575 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
576 enum page_type type
, enum temp_type temp
)
578 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
579 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
581 down_write(&io
->io_rwsem
);
583 /* change META to META_FLUSH in the checkpoint procedure */
584 if (type
>= META_FLUSH
) {
585 io
->fio
.type
= META_FLUSH
;
586 io
->fio
.op
= REQ_OP_WRITE
;
587 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
588 if (!test_opt(sbi
, NOBARRIER
))
589 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
591 __submit_merged_bio(io
);
592 up_write(&io
->io_rwsem
);
595 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
596 struct inode
*inode
, struct page
*page
,
597 nid_t ino
, enum page_type type
, bool force
)
602 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
604 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
605 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
607 down_read(&io
->io_rwsem
);
608 ret
= __has_merged_page(io
->bio
, inode
, page
, ino
);
609 up_read(&io
->io_rwsem
);
612 __f2fs_submit_merged_write(sbi
, type
, temp
);
614 /* TODO: use HOT temp only for meta pages now. */
620 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
622 __submit_merged_write_cond(sbi
, NULL
, NULL
, 0, type
, true);
625 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
626 struct inode
*inode
, struct page
*page
,
627 nid_t ino
, enum page_type type
)
629 __submit_merged_write_cond(sbi
, inode
, page
, ino
, type
, false);
632 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
634 f2fs_submit_merged_write(sbi
, DATA
);
635 f2fs_submit_merged_write(sbi
, NODE
);
636 f2fs_submit_merged_write(sbi
, META
);
640 * Fill the locked page with data located in the block address.
641 * A caller needs to unlock the page on failure.
643 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
646 struct page
*page
= fio
->encrypted_page
?
647 fio
->encrypted_page
: fio
->page
;
649 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
650 fio
->is_por
? META_POR
: (__is_meta_io(fio
) ?
651 META_GENERIC
: DATA_GENERIC_ENHANCE
)))
652 return -EFSCORRUPTED
;
654 trace_f2fs_submit_page_bio(page
, fio
);
655 f2fs_trace_ios(fio
, 0);
657 /* Allocate a new bio */
658 bio
= __bio_alloc(fio
, 1);
660 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
665 if (fio
->io_wbc
&& !is_read_io(fio
->op
))
666 wbc_account_cgroup_owner(fio
->io_wbc
, page
, PAGE_SIZE
);
668 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
670 inc_page_count(fio
->sbi
, is_read_io(fio
->op
) ?
671 __read_io_type(page
): WB_DATA_TYPE(fio
->page
));
673 __submit_bio(fio
->sbi
, bio
, fio
->type
);
677 static bool page_is_mergeable(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
678 block_t last_blkaddr
, block_t cur_blkaddr
)
680 if (last_blkaddr
+ 1 != cur_blkaddr
)
682 return __same_bdev(sbi
, cur_blkaddr
, bio
);
685 static bool io_type_is_mergeable(struct f2fs_bio_info
*io
,
686 struct f2fs_io_info
*fio
)
688 if (io
->fio
.op
!= fio
->op
)
690 return io
->fio
.op_flags
== fio
->op_flags
;
693 static bool io_is_mergeable(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
694 struct f2fs_bio_info
*io
,
695 struct f2fs_io_info
*fio
,
696 block_t last_blkaddr
,
699 if (F2FS_IO_ALIGNED(sbi
) && (fio
->type
== DATA
|| fio
->type
== NODE
)) {
700 unsigned int filled_blocks
=
701 F2FS_BYTES_TO_BLK(bio
->bi_iter
.bi_size
);
702 unsigned int io_size
= F2FS_IO_SIZE(sbi
);
703 unsigned int left_vecs
= bio
->bi_max_vecs
- bio
->bi_vcnt
;
705 /* IOs in bio is aligned and left space of vectors is not enough */
706 if (!(filled_blocks
% io_size
) && left_vecs
< io_size
)
709 if (!page_is_mergeable(sbi
, bio
, last_blkaddr
, cur_blkaddr
))
711 return io_type_is_mergeable(io
, fio
);
714 static void add_bio_entry(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
715 struct page
*page
, enum temp_type temp
)
717 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
718 struct bio_entry
*be
;
720 be
= f2fs_kmem_cache_alloc(bio_entry_slab
, GFP_NOFS
);
724 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) != PAGE_SIZE
)
727 down_write(&io
->bio_list_lock
);
728 list_add_tail(&be
->list
, &io
->bio_list
);
729 up_write(&io
->bio_list_lock
);
732 static void del_bio_entry(struct bio_entry
*be
)
735 kmem_cache_free(bio_entry_slab
, be
);
738 static int add_ipu_page(struct f2fs_sb_info
*sbi
, struct bio
**bio
,
745 for (temp
= HOT
; temp
< NR_TEMP_TYPE
&& !found
; temp
++) {
746 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
747 struct list_head
*head
= &io
->bio_list
;
748 struct bio_entry
*be
;
750 down_write(&io
->bio_list_lock
);
751 list_for_each_entry(be
, head
, list
) {
757 if (bio_add_page(*bio
, page
, PAGE_SIZE
, 0) ==
765 __submit_bio(sbi
, *bio
, DATA
);
768 up_write(&io
->bio_list_lock
);
779 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info
*sbi
,
780 struct bio
**bio
, struct page
*page
)
784 struct bio
*target
= bio
? *bio
: NULL
;
786 for (temp
= HOT
; temp
< NR_TEMP_TYPE
&& !found
; temp
++) {
787 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
788 struct list_head
*head
= &io
->bio_list
;
789 struct bio_entry
*be
;
791 if (list_empty(head
))
794 down_read(&io
->bio_list_lock
);
795 list_for_each_entry(be
, head
, list
) {
797 found
= (target
== be
->bio
);
799 found
= __has_merged_page(be
->bio
, NULL
,
804 up_read(&io
->bio_list_lock
);
811 down_write(&io
->bio_list_lock
);
812 list_for_each_entry(be
, head
, list
) {
814 found
= (target
== be
->bio
);
816 found
= __has_merged_page(be
->bio
, NULL
,
824 up_write(&io
->bio_list_lock
);
828 __submit_bio(sbi
, target
, DATA
);
835 int f2fs_merge_page_bio(struct f2fs_io_info
*fio
)
837 struct bio
*bio
= *fio
->bio
;
838 struct page
*page
= fio
->encrypted_page
?
839 fio
->encrypted_page
: fio
->page
;
841 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
842 __is_meta_io(fio
) ? META_GENERIC
: DATA_GENERIC
))
843 return -EFSCORRUPTED
;
845 trace_f2fs_submit_page_bio(page
, fio
);
846 f2fs_trace_ios(fio
, 0);
848 if (bio
&& !page_is_mergeable(fio
->sbi
, bio
, *fio
->last_block
,
850 f2fs_submit_merged_ipu_write(fio
->sbi
, &bio
, NULL
);
853 bio
= __bio_alloc(fio
, BIO_MAX_PAGES
);
854 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
856 add_bio_entry(fio
->sbi
, bio
, page
, fio
->temp
);
858 if (add_ipu_page(fio
->sbi
, &bio
, page
))
863 wbc_account_cgroup_owner(fio
->io_wbc
, page
, PAGE_SIZE
);
865 inc_page_count(fio
->sbi
, WB_DATA_TYPE(page
));
867 *fio
->last_block
= fio
->new_blkaddr
;
873 void f2fs_submit_page_write(struct f2fs_io_info
*fio
)
875 struct f2fs_sb_info
*sbi
= fio
->sbi
;
876 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
877 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
878 struct page
*bio_page
;
880 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
882 down_write(&io
->io_rwsem
);
885 spin_lock(&io
->io_lock
);
886 if (list_empty(&io
->io_list
)) {
887 spin_unlock(&io
->io_lock
);
890 fio
= list_first_entry(&io
->io_list
,
891 struct f2fs_io_info
, list
);
892 list_del(&fio
->list
);
893 spin_unlock(&io
->io_lock
);
896 verify_fio_blkaddr(fio
);
898 if (fio
->encrypted_page
)
899 bio_page
= fio
->encrypted_page
;
900 else if (fio
->compressed_page
)
901 bio_page
= fio
->compressed_page
;
903 bio_page
= fio
->page
;
905 /* set submitted = true as a return value */
906 fio
->submitted
= true;
908 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
910 if (io
->bio
&& !io_is_mergeable(sbi
, io
->bio
, io
, fio
,
911 io
->last_block_in_bio
, fio
->new_blkaddr
))
912 __submit_merged_bio(io
);
914 if (io
->bio
== NULL
) {
915 if (F2FS_IO_ALIGNED(sbi
) &&
916 (fio
->type
== DATA
|| fio
->type
== NODE
) &&
917 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
918 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
922 io
->bio
= __bio_alloc(fio
, BIO_MAX_PAGES
);
926 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
927 __submit_merged_bio(io
);
932 wbc_account_cgroup_owner(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
934 io
->last_block_in_bio
= fio
->new_blkaddr
;
935 f2fs_trace_ios(fio
, 0);
937 trace_f2fs_submit_page_write(fio
->page
, fio
);
942 if (is_sbi_flag_set(sbi
, SBI_IS_SHUTDOWN
) ||
943 !f2fs_is_checkpoint_ready(sbi
))
944 __submit_merged_bio(io
);
945 up_write(&io
->io_rwsem
);
948 static inline bool f2fs_need_verity(const struct inode
*inode
, pgoff_t idx
)
950 return fsverity_active(inode
) &&
951 idx
< DIV_ROUND_UP(inode
->i_size
, PAGE_SIZE
);
954 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
955 unsigned nr_pages
, unsigned op_flag
,
958 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
960 struct bio_post_read_ctx
*ctx
;
961 unsigned int post_read_steps
= 0;
963 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
965 return ERR_PTR(-ENOMEM
);
966 f2fs_target_device(sbi
, blkaddr
, bio
);
967 bio
->bi_end_io
= f2fs_read_end_io
;
968 bio_set_op_attrs(bio
, REQ_OP_READ
, op_flag
);
970 if (f2fs_encrypted_file(inode
))
971 post_read_steps
|= 1 << STEP_DECRYPT
;
972 if (f2fs_compressed_file(inode
))
973 post_read_steps
|= 1 << STEP_DECOMPRESS
;
974 if (f2fs_need_verity(inode
, first_idx
))
975 post_read_steps
|= 1 << STEP_VERITY
;
977 if (post_read_steps
) {
978 /* Due to the mempool, this never fails. */
979 ctx
= mempool_alloc(bio_post_read_ctx_pool
, GFP_NOFS
);
982 ctx
->enabled_steps
= post_read_steps
;
983 bio
->bi_private
= ctx
;
989 static void f2fs_release_read_bio(struct bio
*bio
)
992 mempool_free(bio
->bi_private
, bio_post_read_ctx_pool
);
996 /* This can handle encryption stuffs */
997 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
1000 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1003 bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1, 0, page
->index
);
1005 return PTR_ERR(bio
);
1007 /* wait for GCed page writeback via META_MAPPING */
1008 f2fs_wait_on_block_writeback(inode
, blkaddr
);
1010 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
1014 ClearPageError(page
);
1015 inc_page_count(sbi
, F2FS_RD_DATA
);
1016 __submit_bio(sbi
, bio
, DATA
);
1020 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
1022 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
1026 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
1027 base
= get_extra_isize(dn
->inode
);
1029 /* Get physical address of data block */
1030 addr_array
= blkaddr_in_node(rn
);
1031 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
1035 * Lock ordering for the change of data block address:
1038 * update block addresses in the node page
1040 void f2fs_set_data_blkaddr(struct dnode_of_data
*dn
)
1042 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true, true);
1043 __set_data_blkaddr(dn
);
1044 if (set_page_dirty(dn
->node_page
))
1045 dn
->node_changed
= true;
1048 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
1050 dn
->data_blkaddr
= blkaddr
;
1051 f2fs_set_data_blkaddr(dn
);
1052 f2fs_update_extent_cache(dn
);
1055 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1056 int f2fs_reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
1058 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1064 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
1066 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
1069 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
1070 dn
->ofs_in_node
, count
);
1072 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true, true);
1074 for (; count
> 0; dn
->ofs_in_node
++) {
1075 block_t blkaddr
= datablock_addr(dn
->inode
,
1076 dn
->node_page
, dn
->ofs_in_node
);
1077 if (blkaddr
== NULL_ADDR
) {
1078 dn
->data_blkaddr
= NEW_ADDR
;
1079 __set_data_blkaddr(dn
);
1084 if (set_page_dirty(dn
->node_page
))
1085 dn
->node_changed
= true;
1089 /* Should keep dn->ofs_in_node unchanged */
1090 int f2fs_reserve_new_block(struct dnode_of_data
*dn
)
1092 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1095 ret
= f2fs_reserve_new_blocks(dn
, 1);
1096 dn
->ofs_in_node
= ofs_in_node
;
1100 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
1102 bool need_put
= dn
->inode_page
? false : true;
1105 err
= f2fs_get_dnode_of_data(dn
, index
, ALLOC_NODE
);
1109 if (dn
->data_blkaddr
== NULL_ADDR
)
1110 err
= f2fs_reserve_new_block(dn
);
1111 if (err
|| need_put
)
1116 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
1118 struct extent_info ei
= {0,0,0};
1119 struct inode
*inode
= dn
->inode
;
1121 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1122 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1126 return f2fs_reserve_block(dn
, index
);
1129 struct page
*f2fs_get_read_data_page(struct inode
*inode
, pgoff_t index
,
1130 int op_flags
, bool for_write
)
1132 struct address_space
*mapping
= inode
->i_mapping
;
1133 struct dnode_of_data dn
;
1135 struct extent_info ei
= {0,0,0};
1138 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
1140 return ERR_PTR(-ENOMEM
);
1142 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1143 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1144 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), dn
.data_blkaddr
,
1145 DATA_GENERIC_ENHANCE_READ
)) {
1146 err
= -EFSCORRUPTED
;
1152 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1153 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1156 f2fs_put_dnode(&dn
);
1158 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
1162 if (dn
.data_blkaddr
!= NEW_ADDR
&&
1163 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode
),
1165 DATA_GENERIC_ENHANCE
)) {
1166 err
= -EFSCORRUPTED
;
1170 if (PageUptodate(page
)) {
1176 * A new dentry page is allocated but not able to be written, since its
1177 * new inode page couldn't be allocated due to -ENOSPC.
1178 * In such the case, its blkaddr can be remained as NEW_ADDR.
1179 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1180 * f2fs_init_inode_metadata.
1182 if (dn
.data_blkaddr
== NEW_ADDR
) {
1183 zero_user_segment(page
, 0, PAGE_SIZE
);
1184 if (!PageUptodate(page
))
1185 SetPageUptodate(page
);
1190 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
1196 f2fs_put_page(page
, 1);
1197 return ERR_PTR(err
);
1200 struct page
*f2fs_find_data_page(struct inode
*inode
, pgoff_t index
)
1202 struct address_space
*mapping
= inode
->i_mapping
;
1205 page
= find_get_page(mapping
, index
);
1206 if (page
&& PageUptodate(page
))
1208 f2fs_put_page(page
, 0);
1210 page
= f2fs_get_read_data_page(inode
, index
, 0, false);
1214 if (PageUptodate(page
))
1217 wait_on_page_locked(page
);
1218 if (unlikely(!PageUptodate(page
))) {
1219 f2fs_put_page(page
, 0);
1220 return ERR_PTR(-EIO
);
1226 * If it tries to access a hole, return an error.
1227 * Because, the callers, functions in dir.c and GC, should be able to know
1228 * whether this page exists or not.
1230 struct page
*f2fs_get_lock_data_page(struct inode
*inode
, pgoff_t index
,
1233 struct address_space
*mapping
= inode
->i_mapping
;
1236 page
= f2fs_get_read_data_page(inode
, index
, 0, for_write
);
1240 /* wait for read completion */
1242 if (unlikely(page
->mapping
!= mapping
)) {
1243 f2fs_put_page(page
, 1);
1246 if (unlikely(!PageUptodate(page
))) {
1247 f2fs_put_page(page
, 1);
1248 return ERR_PTR(-EIO
);
1254 * Caller ensures that this data page is never allocated.
1255 * A new zero-filled data page is allocated in the page cache.
1257 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1259 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1260 * ipage should be released by this function.
1262 struct page
*f2fs_get_new_data_page(struct inode
*inode
,
1263 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
1265 struct address_space
*mapping
= inode
->i_mapping
;
1267 struct dnode_of_data dn
;
1270 page
= f2fs_grab_cache_page(mapping
, index
, true);
1273 * before exiting, we should make sure ipage will be released
1274 * if any error occur.
1276 f2fs_put_page(ipage
, 1);
1277 return ERR_PTR(-ENOMEM
);
1280 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
1281 err
= f2fs_reserve_block(&dn
, index
);
1283 f2fs_put_page(page
, 1);
1284 return ERR_PTR(err
);
1287 f2fs_put_dnode(&dn
);
1289 if (PageUptodate(page
))
1292 if (dn
.data_blkaddr
== NEW_ADDR
) {
1293 zero_user_segment(page
, 0, PAGE_SIZE
);
1294 if (!PageUptodate(page
))
1295 SetPageUptodate(page
);
1297 f2fs_put_page(page
, 1);
1299 /* if ipage exists, blkaddr should be NEW_ADDR */
1300 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
1301 page
= f2fs_get_lock_data_page(inode
, index
, true);
1306 if (new_i_size
&& i_size_read(inode
) <
1307 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
1308 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
1312 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
1314 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1315 struct f2fs_summary sum
;
1316 struct node_info ni
;
1317 block_t old_blkaddr
;
1321 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
1324 err
= f2fs_get_node_info(sbi
, dn
->nid
, &ni
);
1328 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
1329 dn
->node_page
, dn
->ofs_in_node
);
1330 if (dn
->data_blkaddr
!= NULL_ADDR
)
1333 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
1337 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
1338 old_blkaddr
= dn
->data_blkaddr
;
1339 f2fs_allocate_data_block(sbi
, NULL
, old_blkaddr
, &dn
->data_blkaddr
,
1340 &sum
, seg_type
, NULL
, false);
1341 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
1342 invalidate_mapping_pages(META_MAPPING(sbi
),
1343 old_blkaddr
, old_blkaddr
);
1344 f2fs_update_data_blkaddr(dn
, dn
->data_blkaddr
);
1347 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1348 * data from unwritten block via dio_read.
1353 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
1355 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1356 struct f2fs_map_blocks map
;
1359 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
1361 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
1362 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
1363 if (map
.m_len
> map
.m_lblk
)
1364 map
.m_len
-= map
.m_lblk
;
1368 map
.m_next_pgofs
= NULL
;
1369 map
.m_next_extent
= NULL
;
1370 map
.m_seg_type
= NO_CHECK_TYPE
;
1371 map
.m_may_create
= true;
1374 map
.m_seg_type
= f2fs_rw_hint_to_seg_type(iocb
->ki_hint
);
1375 flag
= f2fs_force_buffered_io(inode
, iocb
, from
) ?
1376 F2FS_GET_BLOCK_PRE_AIO
:
1377 F2FS_GET_BLOCK_PRE_DIO
;
1380 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
1381 err
= f2fs_convert_inline_inode(inode
);
1385 if (f2fs_has_inline_data(inode
))
1388 flag
= F2FS_GET_BLOCK_PRE_AIO
;
1391 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
1392 if (map
.m_len
> 0 && err
== -ENOSPC
) {
1394 set_inode_flag(inode
, FI_NO_PREALLOC
);
1400 void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
1402 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1404 down_read(&sbi
->node_change
);
1406 up_read(&sbi
->node_change
);
1411 f2fs_unlock_op(sbi
);
1416 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1417 * f2fs_map_blocks structure.
1418 * If original data blocks are allocated, then give them to blockdev.
1420 * a. preallocate requested block addresses
1421 * b. do not use extent cache for better performance
1422 * c. give the block addresses to blockdev
1424 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
1425 int create
, int flag
)
1427 unsigned int maxblocks
= map
->m_len
;
1428 struct dnode_of_data dn
;
1429 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1430 int mode
= map
->m_may_create
? ALLOC_NODE
: LOOKUP_NODE
;
1431 pgoff_t pgofs
, end_offset
, end
;
1432 int err
= 0, ofs
= 1;
1433 unsigned int ofs_in_node
, last_ofs_in_node
;
1435 struct extent_info ei
= {0,0,0};
1437 unsigned int start_pgofs
;
1445 /* it only supports block size == page size */
1446 pgofs
= (pgoff_t
)map
->m_lblk
;
1447 end
= pgofs
+ maxblocks
;
1449 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
1450 if (test_opt(sbi
, LFS
) && flag
== F2FS_GET_BLOCK_DIO
&&
1454 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
1455 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
1456 map
->m_flags
= F2FS_MAP_MAPPED
;
1457 if (map
->m_next_extent
)
1458 *map
->m_next_extent
= pgofs
+ map
->m_len
;
1460 /* for hardware encryption, but to avoid potential issue in future */
1461 if (flag
== F2FS_GET_BLOCK_DIO
)
1462 f2fs_wait_on_block_writeback_range(inode
,
1463 map
->m_pblk
, map
->m_len
);
1468 if (map
->m_may_create
)
1469 __do_map_lock(sbi
, flag
, true);
1471 /* When reading holes, we need its node page */
1472 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1473 err
= f2fs_get_dnode_of_data(&dn
, pgofs
, mode
);
1475 if (flag
== F2FS_GET_BLOCK_BMAP
)
1477 if (err
== -ENOENT
) {
1479 if (map
->m_next_pgofs
)
1480 *map
->m_next_pgofs
=
1481 f2fs_get_next_page_offset(&dn
, pgofs
);
1482 if (map
->m_next_extent
)
1483 *map
->m_next_extent
=
1484 f2fs_get_next_page_offset(&dn
, pgofs
);
1489 start_pgofs
= pgofs
;
1491 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
1492 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1495 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
1497 if (__is_valid_data_blkaddr(blkaddr
) &&
1498 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC_ENHANCE
)) {
1499 err
= -EFSCORRUPTED
;
1503 if (__is_valid_data_blkaddr(blkaddr
)) {
1504 /* use out-place-update for driect IO under LFS mode */
1505 if (test_opt(sbi
, LFS
) && flag
== F2FS_GET_BLOCK_DIO
&&
1506 map
->m_may_create
) {
1507 err
= __allocate_data_block(&dn
, map
->m_seg_type
);
1510 blkaddr
= dn
.data_blkaddr
;
1511 set_inode_flag(inode
, FI_APPEND_WRITE
);
1515 if (unlikely(f2fs_cp_error(sbi
))) {
1519 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1520 if (blkaddr
== NULL_ADDR
) {
1522 last_ofs_in_node
= dn
.ofs_in_node
;
1525 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_DIO
&&
1526 flag
!= F2FS_GET_BLOCK_DIO
);
1527 err
= __allocate_data_block(&dn
,
1530 set_inode_flag(inode
, FI_APPEND_WRITE
);
1534 map
->m_flags
|= F2FS_MAP_NEW
;
1535 blkaddr
= dn
.data_blkaddr
;
1537 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1541 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1543 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1544 blkaddr
== NULL_ADDR
) {
1545 if (map
->m_next_pgofs
)
1546 *map
->m_next_pgofs
= pgofs
+ 1;
1549 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1550 /* for defragment case */
1551 if (map
->m_next_pgofs
)
1552 *map
->m_next_pgofs
= pgofs
+ 1;
1558 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1561 if (map
->m_len
== 0) {
1562 /* preallocated unwritten block should be mapped for fiemap. */
1563 if (blkaddr
== NEW_ADDR
)
1564 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1565 map
->m_flags
|= F2FS_MAP_MAPPED
;
1567 map
->m_pblk
= blkaddr
;
1569 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1570 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1571 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1572 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1583 /* preallocate blocks in batch for one dnode page */
1584 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1585 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1587 dn
.ofs_in_node
= ofs_in_node
;
1588 err
= f2fs_reserve_new_blocks(&dn
, prealloc
);
1592 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1593 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1597 dn
.ofs_in_node
= end_offset
;
1602 else if (dn
.ofs_in_node
< end_offset
)
1605 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1606 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1607 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1609 f2fs_update_extent_cache_range(&dn
,
1610 start_pgofs
, map
->m_pblk
+ ofs
,
1615 f2fs_put_dnode(&dn
);
1617 if (map
->m_may_create
) {
1618 __do_map_lock(sbi
, flag
, false);
1619 f2fs_balance_fs(sbi
, dn
.node_changed
);
1625 /* for hardware encryption, but to avoid potential issue in future */
1626 if (flag
== F2FS_GET_BLOCK_DIO
&& map
->m_flags
& F2FS_MAP_MAPPED
)
1627 f2fs_wait_on_block_writeback_range(inode
,
1628 map
->m_pblk
, map
->m_len
);
1630 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1631 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1632 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1634 f2fs_update_extent_cache_range(&dn
,
1635 start_pgofs
, map
->m_pblk
+ ofs
,
1638 if (map
->m_next_extent
)
1639 *map
->m_next_extent
= pgofs
+ 1;
1641 f2fs_put_dnode(&dn
);
1643 if (map
->m_may_create
) {
1644 __do_map_lock(sbi
, flag
, false);
1645 f2fs_balance_fs(sbi
, dn
.node_changed
);
1648 trace_f2fs_map_blocks(inode
, map
, err
);
1652 bool f2fs_overwrite_io(struct inode
*inode
, loff_t pos
, size_t len
)
1654 struct f2fs_map_blocks map
;
1658 if (pos
+ len
> i_size_read(inode
))
1661 map
.m_lblk
= F2FS_BYTES_TO_BLK(pos
);
1662 map
.m_next_pgofs
= NULL
;
1663 map
.m_next_extent
= NULL
;
1664 map
.m_seg_type
= NO_CHECK_TYPE
;
1665 map
.m_may_create
= false;
1666 last_lblk
= F2FS_BLK_ALIGN(pos
+ len
);
1668 while (map
.m_lblk
< last_lblk
) {
1669 map
.m_len
= last_lblk
- map
.m_lblk
;
1670 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1671 if (err
|| map
.m_len
== 0)
1673 map
.m_lblk
+= map
.m_len
;
1678 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1679 struct buffer_head
*bh
, int create
, int flag
,
1680 pgoff_t
*next_pgofs
, int seg_type
, bool may_write
)
1682 struct f2fs_map_blocks map
;
1685 map
.m_lblk
= iblock
;
1686 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1687 map
.m_next_pgofs
= next_pgofs
;
1688 map
.m_next_extent
= NULL
;
1689 map
.m_seg_type
= seg_type
;
1690 map
.m_may_create
= may_write
;
1692 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1694 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1695 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1696 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1701 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1702 struct buffer_head
*bh_result
, int create
, int flag
,
1703 pgoff_t
*next_pgofs
)
1705 return __get_data_block(inode
, iblock
, bh_result
, create
,
1707 NO_CHECK_TYPE
, create
);
1710 static int get_data_block_dio_write(struct inode
*inode
, sector_t iblock
,
1711 struct buffer_head
*bh_result
, int create
)
1713 return __get_data_block(inode
, iblock
, bh_result
, create
,
1714 F2FS_GET_BLOCK_DIO
, NULL
,
1715 f2fs_rw_hint_to_seg_type(inode
->i_write_hint
),
1716 IS_SWAPFILE(inode
) ? false : true);
1719 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1720 struct buffer_head
*bh_result
, int create
)
1722 return __get_data_block(inode
, iblock
, bh_result
, create
,
1723 F2FS_GET_BLOCK_DIO
, NULL
,
1724 f2fs_rw_hint_to_seg_type(inode
->i_write_hint
),
1728 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1729 struct buffer_head
*bh_result
, int create
)
1731 /* Block number less than F2FS MAX BLOCKS */
1732 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1735 return __get_data_block(inode
, iblock
, bh_result
, create
,
1736 F2FS_GET_BLOCK_BMAP
, NULL
,
1737 NO_CHECK_TYPE
, create
);
1740 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1742 return (offset
>> inode
->i_blkbits
);
1745 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1747 return (blk
<< inode
->i_blkbits
);
1750 static int f2fs_xattr_fiemap(struct inode
*inode
,
1751 struct fiemap_extent_info
*fieinfo
)
1753 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1755 struct node_info ni
;
1756 __u64 phys
= 0, len
;
1758 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1761 if (f2fs_has_inline_xattr(inode
)) {
1764 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1765 inode
->i_ino
, false);
1769 err
= f2fs_get_node_info(sbi
, inode
->i_ino
, &ni
);
1771 f2fs_put_page(page
, 1);
1775 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1776 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1777 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1778 get_inline_xattr_addrs(inode
));
1781 len
= inline_xattr_size(inode
);
1783 f2fs_put_page(page
, 1);
1785 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1788 flags
|= FIEMAP_EXTENT_LAST
;
1790 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1791 if (err
|| err
== 1)
1796 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1800 err
= f2fs_get_node_info(sbi
, xnid
, &ni
);
1802 f2fs_put_page(page
, 1);
1806 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1807 len
= inode
->i_sb
->s_blocksize
;
1809 f2fs_put_page(page
, 1);
1811 flags
= FIEMAP_EXTENT_LAST
;
1815 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1817 return (err
< 0 ? err
: 0);
1820 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1823 struct buffer_head map_bh
;
1824 sector_t start_blk
, last_blk
;
1826 u64 logical
= 0, phys
= 0, size
= 0;
1830 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1831 ret
= f2fs_precache_extents(inode
);
1836 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1842 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1843 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1847 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
1848 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1853 if (logical_to_blk(inode
, len
) == 0)
1854 len
= blk_to_logical(inode
, 1);
1856 start_blk
= logical_to_blk(inode
, start
);
1857 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1860 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1861 map_bh
.b_size
= len
;
1863 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1864 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1869 if (!buffer_mapped(&map_bh
)) {
1870 start_blk
= next_pgofs
;
1872 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1873 F2FS_I_SB(inode
)->max_file_blocks
))
1876 flags
|= FIEMAP_EXTENT_LAST
;
1880 if (IS_ENCRYPTED(inode
))
1881 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1883 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1887 if (start_blk
> last_blk
|| ret
)
1890 logical
= blk_to_logical(inode
, start_blk
);
1891 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1892 size
= map_bh
.b_size
;
1894 if (buffer_unwritten(&map_bh
))
1895 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1897 start_blk
+= logical_to_blk(inode
, size
);
1901 if (fatal_signal_pending(current
))
1909 inode_unlock(inode
);
1913 static inline loff_t
f2fs_readpage_limit(struct inode
*inode
)
1915 if (IS_ENABLED(CONFIG_FS_VERITY
) &&
1916 (IS_VERITY(inode
) || f2fs_verity_in_progress(inode
)))
1917 return inode
->i_sb
->s_maxbytes
;
1919 return i_size_read(inode
);
1922 static int f2fs_read_single_page(struct inode
*inode
, struct page
*page
,
1924 struct f2fs_map_blocks
*map
,
1925 struct bio
**bio_ret
,
1926 sector_t
*last_block_in_bio
,
1929 struct bio
*bio
= *bio_ret
;
1930 const unsigned blkbits
= inode
->i_blkbits
;
1931 const unsigned blocksize
= 1 << blkbits
;
1932 sector_t block_in_file
;
1933 sector_t last_block
;
1934 sector_t last_block_in_file
;
1938 block_in_file
= (sector_t
)page_index(page
);
1939 last_block
= block_in_file
+ nr_pages
;
1940 last_block_in_file
= (f2fs_readpage_limit(inode
) + blocksize
- 1) >>
1942 if (last_block
> last_block_in_file
)
1943 last_block
= last_block_in_file
;
1945 /* just zeroing out page which is beyond EOF */
1946 if (block_in_file
>= last_block
)
1949 * Map blocks using the previous result first.
1951 if ((map
->m_flags
& F2FS_MAP_MAPPED
) &&
1952 block_in_file
> map
->m_lblk
&&
1953 block_in_file
< (map
->m_lblk
+ map
->m_len
))
1957 * Then do more f2fs_map_blocks() calls until we are
1958 * done with this page.
1960 map
->m_lblk
= block_in_file
;
1961 map
->m_len
= last_block
- block_in_file
;
1963 ret
= f2fs_map_blocks(inode
, map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1967 if ((map
->m_flags
& F2FS_MAP_MAPPED
)) {
1968 block_nr
= map
->m_pblk
+ block_in_file
- map
->m_lblk
;
1969 SetPageMappedToDisk(page
);
1971 if (!PageUptodate(page
) && (!PageSwapCache(page
) &&
1972 !cleancache_get_page(page
))) {
1973 SetPageUptodate(page
);
1977 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), block_nr
,
1978 DATA_GENERIC_ENHANCE_READ
)) {
1979 ret
= -EFSCORRUPTED
;
1984 zero_user_segment(page
, 0, PAGE_SIZE
);
1985 if (f2fs_need_verity(inode
, page
->index
) &&
1986 !fsverity_verify_page(page
)) {
1990 if (!PageUptodate(page
))
1991 SetPageUptodate(page
);
1997 * This page will go to BIO. Do we need to send this
2000 if (bio
&& !page_is_mergeable(F2FS_I_SB(inode
), bio
,
2001 *last_block_in_bio
, block_nr
)) {
2003 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
2007 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
,
2008 is_readahead
? REQ_RAHEAD
: 0, page
->index
);
2017 * If the page is under writeback, we need to wait for
2018 * its completion to see the correct decrypted data.
2020 f2fs_wait_on_block_writeback(inode
, block_nr
);
2022 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
2023 goto submit_and_realloc
;
2025 inc_page_count(F2FS_I_SB(inode
), F2FS_RD_DATA
);
2026 ClearPageError(page
);
2027 *last_block_in_bio
= block_nr
;
2031 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
2040 #ifdef CONFIG_F2FS_FS_COMPRESSION
2041 int f2fs_read_multi_pages(struct compress_ctx
*cc
, struct bio
**bio_ret
,
2042 unsigned nr_pages
, sector_t
*last_block_in_bio
,
2045 struct dnode_of_data dn
;
2046 struct inode
*inode
= cc
->inode
;
2047 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2048 struct bio
*bio
= *bio_ret
;
2049 unsigned int start_idx
= cc
->cluster_idx
<< cc
->log_cluster_size
;
2050 sector_t last_block_in_file
;
2051 const unsigned blkbits
= inode
->i_blkbits
;
2052 const unsigned blocksize
= 1 << blkbits
;
2053 struct decompress_io_ctx
*dic
= NULL
;
2057 f2fs_bug_on(sbi
, f2fs_cluster_is_empty(cc
));
2059 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >> blkbits
;
2061 /* get rid of pages beyond EOF */
2062 for (i
= 0; i
< cc
->cluster_size
; i
++) {
2063 struct page
*page
= cc
->rpages
[i
];
2067 if ((sector_t
)page
->index
>= last_block_in_file
) {
2068 zero_user_segment(page
, 0, PAGE_SIZE
);
2069 if (!PageUptodate(page
))
2070 SetPageUptodate(page
);
2071 } else if (!PageUptodate(page
)) {
2075 cc
->rpages
[i
] = NULL
;
2079 /* we are done since all pages are beyond EOF */
2080 if (f2fs_cluster_is_empty(cc
))
2083 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
2084 ret
= f2fs_get_dnode_of_data(&dn
, start_idx
, LOOKUP_NODE
);
2088 /* cluster was overwritten as normal cluster */
2089 if (dn
.data_blkaddr
!= COMPRESS_ADDR
)
2092 for (i
= 1; i
< cc
->cluster_size
; i
++) {
2095 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
,
2096 dn
.ofs_in_node
+ i
);
2098 if (!__is_valid_data_blkaddr(blkaddr
))
2101 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
)) {
2108 /* nothing to decompress */
2109 if (cc
->nr_cpages
== 0) {
2114 dic
= f2fs_alloc_dic(cc
);
2120 for (i
= 0; i
< dic
->nr_cpages
; i
++) {
2121 struct page
*page
= dic
->cpages
[i
];
2124 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
,
2125 dn
.ofs_in_node
+ i
+ 1);
2127 if (bio
&& !page_is_mergeable(sbi
, bio
,
2128 *last_block_in_bio
, blkaddr
)) {
2130 __submit_bio(sbi
, bio
, DATA
);
2135 bio
= f2fs_grab_read_bio(inode
, blkaddr
, nr_pages
,
2136 is_readahead
? REQ_RAHEAD
: 0,
2142 if (refcount_sub_and_test(dic
->nr_cpages
- i
,
2144 f2fs_decompress_end_io(dic
->rpages
,
2145 cc
->cluster_size
, true,
2148 f2fs_put_dnode(&dn
);
2154 f2fs_wait_on_block_writeback(inode
, blkaddr
);
2156 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
2157 goto submit_and_realloc
;
2159 inc_page_count(sbi
, F2FS_RD_DATA
);
2160 ClearPageError(page
);
2161 *last_block_in_bio
= blkaddr
;
2164 f2fs_put_dnode(&dn
);
2170 f2fs_put_dnode(&dn
);
2172 f2fs_decompress_end_io(cc
->rpages
, cc
->cluster_size
, true, false);
2179 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2180 * Major change was from block_size == page_size in f2fs by default.
2182 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2183 * this function ever deviates from doing just read-ahead, it should either
2184 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2187 int f2fs_mpage_readpages(struct address_space
*mapping
,
2188 struct list_head
*pages
, struct page
*page
,
2189 unsigned nr_pages
, bool is_readahead
)
2191 struct bio
*bio
= NULL
;
2192 sector_t last_block_in_bio
= 0;
2193 struct inode
*inode
= mapping
->host
;
2194 struct f2fs_map_blocks map
;
2195 #ifdef CONFIG_F2FS_FS_COMPRESSION
2196 struct compress_ctx cc
= {
2198 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
2199 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
2200 .cluster_idx
= NULL_CLUSTER
,
2207 unsigned max_nr_pages
= nr_pages
;
2214 map
.m_next_pgofs
= NULL
;
2215 map
.m_next_extent
= NULL
;
2216 map
.m_seg_type
= NO_CHECK_TYPE
;
2217 map
.m_may_create
= false;
2219 for (; nr_pages
; nr_pages
--) {
2221 page
= list_last_entry(pages
, struct page
, lru
);
2223 prefetchw(&page
->flags
);
2224 list_del(&page
->lru
);
2225 if (add_to_page_cache_lru(page
, mapping
,
2227 readahead_gfp_mask(mapping
)))
2231 #ifdef CONFIG_F2FS_FS_COMPRESSION
2232 if (f2fs_compressed_file(inode
)) {
2233 /* there are remained comressed pages, submit them */
2234 if (!f2fs_cluster_can_merge_page(&cc
, page
->index
)) {
2235 ret
= f2fs_read_multi_pages(&cc
, &bio
,
2239 f2fs_destroy_compress_ctx(&cc
);
2241 goto set_error_page
;
2243 ret
= f2fs_is_compressed_cluster(inode
, page
->index
);
2245 goto set_error_page
;
2247 goto read_single_page
;
2249 ret
= f2fs_init_compress_ctx(&cc
);
2251 goto set_error_page
;
2253 f2fs_compress_ctx_add_page(&cc
, page
);
2260 ret
= f2fs_read_single_page(inode
, page
, max_nr_pages
, &map
,
2261 &bio
, &last_block_in_bio
, is_readahead
);
2263 #ifdef CONFIG_F2FS_FS_COMPRESSION
2267 zero_user_segment(page
, 0, PAGE_SIZE
);
2274 #ifdef CONFIG_F2FS_FS_COMPRESSION
2275 if (f2fs_compressed_file(inode
)) {
2277 if (nr_pages
== 1 && !f2fs_cluster_is_empty(&cc
)) {
2278 ret
= f2fs_read_multi_pages(&cc
, &bio
,
2282 f2fs_destroy_compress_ctx(&cc
);
2287 BUG_ON(pages
&& !list_empty(pages
));
2289 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
2290 return pages
? 0 : ret
;
2293 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
2295 struct inode
*inode
= page_file_mapping(page
)->host
;
2298 trace_f2fs_readpage(page
, DATA
);
2300 if (!f2fs_is_compress_backend_ready(inode
)) {
2305 /* If the file has inline data, try to read it directly */
2306 if (f2fs_has_inline_data(inode
))
2307 ret
= f2fs_read_inline_data(inode
, page
);
2309 ret
= f2fs_mpage_readpages(page_file_mapping(page
),
2310 NULL
, page
, 1, false);
2314 static int f2fs_read_data_pages(struct file
*file
,
2315 struct address_space
*mapping
,
2316 struct list_head
*pages
, unsigned nr_pages
)
2318 struct inode
*inode
= mapping
->host
;
2319 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
2321 trace_f2fs_readpages(inode
, page
, nr_pages
);
2323 if (!f2fs_is_compress_backend_ready(inode
))
2326 /* If the file has inline data, skip readpages */
2327 if (f2fs_has_inline_data(inode
))
2330 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
, true);
2333 int f2fs_encrypt_one_page(struct f2fs_io_info
*fio
)
2335 struct inode
*inode
= fio
->page
->mapping
->host
;
2336 struct page
*mpage
, *page
;
2337 gfp_t gfp_flags
= GFP_NOFS
;
2339 if (!f2fs_encrypted_file(inode
))
2342 page
= fio
->compressed_page
? fio
->compressed_page
: fio
->page
;
2344 /* wait for GCed page writeback via META_MAPPING */
2345 f2fs_wait_on_block_writeback(inode
, fio
->old_blkaddr
);
2348 fio
->encrypted_page
= fscrypt_encrypt_pagecache_blocks(page
,
2349 PAGE_SIZE
, 0, gfp_flags
);
2350 if (IS_ERR(fio
->encrypted_page
)) {
2351 /* flush pending IOs and wait for a while in the ENOMEM case */
2352 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
2353 f2fs_flush_merged_writes(fio
->sbi
);
2354 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
2355 gfp_flags
|= __GFP_NOFAIL
;
2358 return PTR_ERR(fio
->encrypted_page
);
2361 mpage
= find_lock_page(META_MAPPING(fio
->sbi
), fio
->old_blkaddr
);
2363 if (PageUptodate(mpage
))
2364 memcpy(page_address(mpage
),
2365 page_address(fio
->encrypted_page
), PAGE_SIZE
);
2366 f2fs_put_page(mpage
, 1);
2371 static inline bool check_inplace_update_policy(struct inode
*inode
,
2372 struct f2fs_io_info
*fio
)
2374 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2375 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
2377 if (policy
& (0x1 << F2FS_IPU_FORCE
))
2379 if (policy
& (0x1 << F2FS_IPU_SSR
) && f2fs_need_SSR(sbi
))
2381 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
2382 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
2384 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && f2fs_need_SSR(sbi
) &&
2385 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
2389 * IPU for rewrite async pages
2391 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
2392 fio
&& fio
->op
== REQ_OP_WRITE
&&
2393 !(fio
->op_flags
& REQ_SYNC
) &&
2394 !IS_ENCRYPTED(inode
))
2397 /* this is only set during fdatasync */
2398 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
2399 is_inode_flag_set(inode
, FI_NEED_IPU
))
2402 if (unlikely(fio
&& is_sbi_flag_set(sbi
, SBI_CP_DISABLED
) &&
2403 !f2fs_is_checkpointed_data(sbi
, fio
->old_blkaddr
)))
2409 bool f2fs_should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
2411 if (f2fs_is_pinned_file(inode
))
2414 /* if this is cold file, we should overwrite to avoid fragmentation */
2415 if (file_is_cold(inode
))
2418 return check_inplace_update_policy(inode
, fio
);
2421 bool f2fs_should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
2423 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2425 if (test_opt(sbi
, LFS
))
2427 if (S_ISDIR(inode
->i_mode
))
2429 if (IS_NOQUOTA(inode
))
2431 if (f2fs_is_atomic_file(inode
))
2434 if (is_cold_data(fio
->page
))
2436 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
2438 if (unlikely(is_sbi_flag_set(sbi
, SBI_CP_DISABLED
) &&
2439 f2fs_is_checkpointed_data(sbi
, fio
->old_blkaddr
)))
2445 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
2447 struct inode
*inode
= fio
->page
->mapping
->host
;
2449 if (f2fs_should_update_outplace(inode
, fio
))
2452 return f2fs_should_update_inplace(inode
, fio
);
2455 int f2fs_do_write_data_page(struct f2fs_io_info
*fio
)
2457 struct page
*page
= fio
->page
;
2458 struct inode
*inode
= page
->mapping
->host
;
2459 struct dnode_of_data dn
;
2460 struct extent_info ei
= {0,0,0};
2461 struct node_info ni
;
2462 bool ipu_force
= false;
2465 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
2466 if (need_inplace_update(fio
) &&
2467 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
2468 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
2470 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
2471 DATA_GENERIC_ENHANCE
))
2472 return -EFSCORRUPTED
;
2475 fio
->need_lock
= LOCK_DONE
;
2479 /* Deadlock due to between page->lock and f2fs_lock_op */
2480 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
2483 err
= f2fs_get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
2487 fio
->old_blkaddr
= dn
.data_blkaddr
;
2489 /* This page is already truncated */
2490 if (fio
->old_blkaddr
== NULL_ADDR
) {
2491 ClearPageUptodate(page
);
2492 clear_cold_data(page
);
2496 if (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
2497 !f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
2498 DATA_GENERIC_ENHANCE
)) {
2499 err
= -EFSCORRUPTED
;
2503 * If current allocation needs SSR,
2504 * it had better in-place writes for updated data.
2507 (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
2508 need_inplace_update(fio
))) {
2509 err
= f2fs_encrypt_one_page(fio
);
2513 set_page_writeback(page
);
2514 ClearPageError(page
);
2515 f2fs_put_dnode(&dn
);
2516 if (fio
->need_lock
== LOCK_REQ
)
2517 f2fs_unlock_op(fio
->sbi
);
2518 err
= f2fs_inplace_write_data(fio
);
2520 if (f2fs_encrypted_file(inode
))
2521 fscrypt_finalize_bounce_page(&fio
->encrypted_page
);
2522 if (PageWriteback(page
))
2523 end_page_writeback(page
);
2525 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2527 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
2531 if (fio
->need_lock
== LOCK_RETRY
) {
2532 if (!f2fs_trylock_op(fio
->sbi
)) {
2536 fio
->need_lock
= LOCK_REQ
;
2539 err
= f2fs_get_node_info(fio
->sbi
, dn
.nid
, &ni
);
2543 fio
->version
= ni
.version
;
2545 err
= f2fs_encrypt_one_page(fio
);
2549 set_page_writeback(page
);
2550 ClearPageError(page
);
2552 if (fio
->compr_blocks
&& fio
->old_blkaddr
== COMPRESS_ADDR
)
2553 f2fs_i_compr_blocks_update(inode
, fio
->compr_blocks
- 1, false);
2555 /* LFS mode write path */
2556 f2fs_outplace_write_data(&dn
, fio
);
2557 trace_f2fs_do_write_data_page(page
, OPU
);
2558 set_inode_flag(inode
, FI_APPEND_WRITE
);
2559 if (page
->index
== 0)
2560 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
2562 f2fs_put_dnode(&dn
);
2564 if (fio
->need_lock
== LOCK_REQ
)
2565 f2fs_unlock_op(fio
->sbi
);
2569 int f2fs_write_single_data_page(struct page
*page
, int *submitted
,
2571 sector_t
*last_block
,
2572 struct writeback_control
*wbc
,
2573 enum iostat_type io_type
,
2576 struct inode
*inode
= page
->mapping
->host
;
2577 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2578 loff_t i_size
= i_size_read(inode
);
2579 const pgoff_t end_index
= ((unsigned long long)i_size
)
2581 loff_t psize
= (loff_t
)(page
->index
+ 1) << PAGE_SHIFT
;
2582 unsigned offset
= 0;
2583 bool need_balance_fs
= false;
2585 struct f2fs_io_info fio
= {
2587 .ino
= inode
->i_ino
,
2590 .op_flags
= wbc_to_write_flags(wbc
),
2591 .old_blkaddr
= NULL_ADDR
,
2593 .encrypted_page
= NULL
,
2595 .compr_blocks
= compr_blocks
,
2596 .need_lock
= LOCK_RETRY
,
2600 .last_block
= last_block
,
2603 trace_f2fs_writepage(page
, DATA
);
2605 /* we should bypass data pages to proceed the kworkder jobs */
2606 if (unlikely(f2fs_cp_error(sbi
))) {
2607 mapping_set_error(page
->mapping
, -EIO
);
2609 * don't drop any dirty dentry pages for keeping lastest
2610 * directory structure.
2612 if (S_ISDIR(inode
->i_mode
))
2617 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
2620 if (page
->index
< end_index
||
2621 f2fs_verity_in_progress(inode
) ||
2626 * If the offset is out-of-range of file size,
2627 * this page does not have to be written to disk.
2629 offset
= i_size
& (PAGE_SIZE
- 1);
2630 if ((page
->index
>= end_index
+ 1) || !offset
)
2633 zero_user_segment(page
, offset
, PAGE_SIZE
);
2635 if (f2fs_is_drop_cache(inode
))
2637 /* we should not write 0'th page having journal header */
2638 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
2639 (!wbc
->for_reclaim
&&
2640 f2fs_available_free_memory(sbi
, BASE_CHECK
))))
2643 /* Dentry blocks are controlled by checkpoint */
2644 if (S_ISDIR(inode
->i_mode
)) {
2645 fio
.need_lock
= LOCK_DONE
;
2646 err
= f2fs_do_write_data_page(&fio
);
2650 if (!wbc
->for_reclaim
)
2651 need_balance_fs
= true;
2652 else if (has_not_enough_free_secs(sbi
, 0, 0))
2655 set_inode_flag(inode
, FI_HOT_DATA
);
2658 if (f2fs_has_inline_data(inode
)) {
2659 err
= f2fs_write_inline_data(inode
, page
);
2664 if (err
== -EAGAIN
) {
2665 err
= f2fs_do_write_data_page(&fio
);
2666 if (err
== -EAGAIN
) {
2667 fio
.need_lock
= LOCK_REQ
;
2668 err
= f2fs_do_write_data_page(&fio
);
2673 file_set_keep_isize(inode
);
2675 down_write(&F2FS_I(inode
)->i_sem
);
2676 if (F2FS_I(inode
)->last_disk_size
< psize
)
2677 F2FS_I(inode
)->last_disk_size
= psize
;
2678 up_write(&F2FS_I(inode
)->i_sem
);
2682 if (err
&& err
!= -ENOENT
)
2686 inode_dec_dirty_pages(inode
);
2688 ClearPageUptodate(page
);
2689 clear_cold_data(page
);
2692 if (wbc
->for_reclaim
) {
2693 f2fs_submit_merged_write_cond(sbi
, NULL
, page
, 0, DATA
);
2694 clear_inode_flag(inode
, FI_HOT_DATA
);
2695 f2fs_remove_dirty_inode(inode
);
2699 if (!S_ISDIR(inode
->i_mode
) && !IS_NOQUOTA(inode
) &&
2700 !F2FS_I(inode
)->cp_task
)
2701 f2fs_balance_fs(sbi
, need_balance_fs
);
2703 if (unlikely(f2fs_cp_error(sbi
))) {
2704 f2fs_submit_merged_write(sbi
, DATA
);
2705 f2fs_submit_merged_ipu_write(sbi
, bio
, NULL
);
2710 *submitted
= fio
.submitted
? 1 : 0;
2715 redirty_page_for_writepage(wbc
, page
);
2717 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2718 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2719 * file_write_and_wait_range() will see EIO error, which is critical
2720 * to return value of fsync() followed by atomic_write failure to user.
2722 if (!err
|| wbc
->for_reclaim
)
2723 return AOP_WRITEPAGE_ACTIVATE
;
2728 static int f2fs_write_data_page(struct page
*page
,
2729 struct writeback_control
*wbc
)
2731 #ifdef CONFIG_F2FS_FS_COMPRESSION
2732 struct inode
*inode
= page
->mapping
->host
;
2734 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
2737 if (f2fs_compressed_file(inode
)) {
2738 if (f2fs_is_compressed_cluster(inode
, page
->index
)) {
2739 redirty_page_for_writepage(wbc
, page
);
2740 return AOP_WRITEPAGE_ACTIVATE
;
2746 return f2fs_write_single_data_page(page
, NULL
, NULL
, NULL
,
2747 wbc
, FS_DATA_IO
, 0);
2751 * This function was copied from write_cche_pages from mm/page-writeback.c.
2752 * The major change is making write step of cold data page separately from
2753 * warm/hot data page.
2755 static int f2fs_write_cache_pages(struct address_space
*mapping
,
2756 struct writeback_control
*wbc
,
2757 enum iostat_type io_type
)
2760 int done
= 0, retry
= 0;
2761 struct pagevec pvec
;
2762 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
2763 struct bio
*bio
= NULL
;
2764 sector_t last_block
;
2765 #ifdef CONFIG_F2FS_FS_COMPRESSION
2766 struct inode
*inode
= mapping
->host
;
2767 struct compress_ctx cc
= {
2769 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
2770 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
2771 .cluster_idx
= NULL_CLUSTER
,
2777 .rlen
= PAGE_SIZE
* F2FS_I(inode
)->i_cluster_size
,
2782 pgoff_t
uninitialized_var(writeback_index
);
2784 pgoff_t end
; /* Inclusive */
2787 int range_whole
= 0;
2793 pagevec_init(&pvec
);
2795 if (get_dirty_pages(mapping
->host
) <=
2796 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
2797 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
2799 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
2801 if (wbc
->range_cyclic
) {
2802 writeback_index
= mapping
->writeback_index
; /* prev offset */
2803 index
= writeback_index
;
2810 index
= wbc
->range_start
>> PAGE_SHIFT
;
2811 end
= wbc
->range_end
>> PAGE_SHIFT
;
2812 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2814 cycled
= 1; /* ignore range_cyclic tests */
2816 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2817 tag
= PAGECACHE_TAG_TOWRITE
;
2819 tag
= PAGECACHE_TAG_DIRTY
;
2822 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2823 tag_pages_for_writeback(mapping
, index
, end
);
2825 while (!done
&& !retry
&& (index
<= end
)) {
2826 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
2831 for (i
= 0; i
< nr_pages
; i
++) {
2832 struct page
*page
= pvec
.pages
[i
];
2836 #ifdef CONFIG_F2FS_FS_COMPRESSION
2837 if (f2fs_compressed_file(inode
)) {
2838 ret
= f2fs_init_compress_ctx(&cc
);
2844 if (!f2fs_cluster_can_merge_page(&cc
,
2846 ret
= f2fs_write_multi_pages(&cc
,
2847 &submitted
, wbc
, io_type
);
2853 if (unlikely(f2fs_cp_error(sbi
)))
2856 if (f2fs_cluster_is_empty(&cc
)) {
2857 void *fsdata
= NULL
;
2861 ret2
= f2fs_prepare_compress_overwrite(
2863 page
->index
, &fsdata
);
2869 !f2fs_compress_write_end(inode
,
2870 fsdata
, page
->index
,
2880 /* give a priority to WB_SYNC threads */
2881 if (atomic_read(&sbi
->wb_sync_req
[DATA
]) &&
2882 wbc
->sync_mode
== WB_SYNC_NONE
) {
2886 #ifdef CONFIG_F2FS_FS_COMPRESSION
2889 done_index
= page
->index
;
2893 if (unlikely(page
->mapping
!= mapping
)) {
2899 if (!PageDirty(page
)) {
2900 /* someone wrote it for us */
2901 goto continue_unlock
;
2904 if (PageWriteback(page
)) {
2905 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
2906 f2fs_wait_on_page_writeback(page
,
2909 goto continue_unlock
;
2912 if (!clear_page_dirty_for_io(page
))
2913 goto continue_unlock
;
2915 #ifdef CONFIG_F2FS_FS_COMPRESSION
2916 if (f2fs_compressed_file(inode
)) {
2918 f2fs_compress_ctx_add_page(&cc
, page
);
2922 ret
= f2fs_write_single_data_page(page
, &submitted
,
2923 &bio
, &last_block
, wbc
, io_type
, 0);
2924 if (ret
== AOP_WRITEPAGE_ACTIVATE
)
2926 #ifdef CONFIG_F2FS_FS_COMPRESSION
2929 nwritten
+= submitted
;
2930 wbc
->nr_to_write
-= submitted
;
2932 if (unlikely(ret
)) {
2934 * keep nr_to_write, since vfs uses this to
2935 * get # of written pages.
2937 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
2940 } else if (ret
== -EAGAIN
) {
2942 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
2944 congestion_wait(BLK_RW_ASYNC
,
2950 done_index
= page
->index
+ 1;
2955 if (wbc
->nr_to_write
<= 0 &&
2956 wbc
->sync_mode
== WB_SYNC_NONE
) {
2964 pagevec_release(&pvec
);
2967 #ifdef CONFIG_F2FS_FS_COMPRESSION
2968 /* flush remained pages in compress cluster */
2969 if (f2fs_compressed_file(inode
) && !f2fs_cluster_is_empty(&cc
)) {
2970 ret
= f2fs_write_multi_pages(&cc
, &submitted
, wbc
, io_type
);
2971 nwritten
+= submitted
;
2972 wbc
->nr_to_write
-= submitted
;
2979 if ((!cycled
&& !done
) || retry
) {
2982 end
= writeback_index
- 1;
2985 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2986 mapping
->writeback_index
= done_index
;
2989 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
2991 /* submit cached bio of IPU write */
2993 f2fs_submit_merged_ipu_write(sbi
, &bio
, NULL
);
2998 static inline bool __should_serialize_io(struct inode
*inode
,
2999 struct writeback_control
*wbc
)
3001 if (!S_ISREG(inode
->i_mode
))
3003 if (f2fs_compressed_file(inode
))
3005 if (IS_NOQUOTA(inode
))
3007 /* to avoid deadlock in path of data flush */
3008 if (F2FS_I(inode
)->cp_task
)
3010 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
3012 if (get_dirty_pages(inode
) >= SM_I(F2FS_I_SB(inode
))->min_seq_blocks
)
3017 static int __f2fs_write_data_pages(struct address_space
*mapping
,
3018 struct writeback_control
*wbc
,
3019 enum iostat_type io_type
)
3021 struct inode
*inode
= mapping
->host
;
3022 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3023 struct blk_plug plug
;
3025 bool locked
= false;
3027 /* deal with chardevs and other special file */
3028 if (!mapping
->a_ops
->writepage
)
3031 /* skip writing if there is no dirty page in this inode */
3032 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
3035 /* during POR, we don't need to trigger writepage at all. */
3036 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
3039 if ((S_ISDIR(inode
->i_mode
) || IS_NOQUOTA(inode
)) &&
3040 wbc
->sync_mode
== WB_SYNC_NONE
&&
3041 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
3042 f2fs_available_free_memory(sbi
, DIRTY_DENTS
))
3045 /* skip writing during file defragment */
3046 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
3049 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
3051 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3052 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3053 atomic_inc(&sbi
->wb_sync_req
[DATA
]);
3054 else if (atomic_read(&sbi
->wb_sync_req
[DATA
]))
3057 if (__should_serialize_io(inode
, wbc
)) {
3058 mutex_lock(&sbi
->writepages
);
3062 blk_start_plug(&plug
);
3063 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
3064 blk_finish_plug(&plug
);
3067 mutex_unlock(&sbi
->writepages
);
3069 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3070 atomic_dec(&sbi
->wb_sync_req
[DATA
]);
3072 * if some pages were truncated, we cannot guarantee its mapping->host
3073 * to detect pending bios.
3076 f2fs_remove_dirty_inode(inode
);
3080 wbc
->pages_skipped
+= get_dirty_pages(inode
);
3081 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
3085 static int f2fs_write_data_pages(struct address_space
*mapping
,
3086 struct writeback_control
*wbc
)
3088 struct inode
*inode
= mapping
->host
;
3090 return __f2fs_write_data_pages(mapping
, wbc
,
3091 F2FS_I(inode
)->cp_task
== current
?
3092 FS_CP_DATA_IO
: FS_DATA_IO
);
3095 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
3097 struct inode
*inode
= mapping
->host
;
3098 loff_t i_size
= i_size_read(inode
);
3100 if (IS_NOQUOTA(inode
))
3103 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3104 if (to
> i_size
&& !f2fs_verity_in_progress(inode
)) {
3105 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
3106 down_write(&F2FS_I(inode
)->i_mmap_sem
);
3108 truncate_pagecache(inode
, i_size
);
3109 f2fs_truncate_blocks(inode
, i_size
, true);
3111 up_write(&F2FS_I(inode
)->i_mmap_sem
);
3112 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
3116 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
3117 struct page
*page
, loff_t pos
, unsigned len
,
3118 block_t
*blk_addr
, bool *node_changed
)
3120 struct inode
*inode
= page
->mapping
->host
;
3121 pgoff_t index
= page
->index
;
3122 struct dnode_of_data dn
;
3124 bool locked
= false;
3125 struct extent_info ei
= {0,0,0};
3130 * we already allocated all the blocks, so we don't need to get
3131 * the block addresses when there is no need to fill the page.
3133 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
3134 !is_inode_flag_set(inode
, FI_NO_PREALLOC
) &&
3135 !f2fs_verity_in_progress(inode
))
3138 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3139 if (f2fs_has_inline_data(inode
) && pos
+ len
> MAX_INLINE_DATA(inode
))
3140 flag
= F2FS_GET_BLOCK_DEFAULT
;
3142 flag
= F2FS_GET_BLOCK_PRE_AIO
;
3144 if (f2fs_has_inline_data(inode
) ||
3145 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
3146 __do_map_lock(sbi
, flag
, true);
3151 /* check inline_data */
3152 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
3153 if (IS_ERR(ipage
)) {
3154 err
= PTR_ERR(ipage
);
3158 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
3160 if (f2fs_has_inline_data(inode
)) {
3161 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
3162 f2fs_do_read_inline_data(page
, ipage
);
3163 set_inode_flag(inode
, FI_DATA_EXIST
);
3165 set_inline_node(ipage
);
3167 err
= f2fs_convert_inline_page(&dn
, page
);
3170 if (dn
.data_blkaddr
== NULL_ADDR
)
3171 err
= f2fs_get_block(&dn
, index
);
3173 } else if (locked
) {
3174 err
= f2fs_get_block(&dn
, index
);
3176 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
3177 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
3180 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
3181 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
3182 f2fs_put_dnode(&dn
);
3183 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
3185 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_AIO
);
3192 /* convert_inline_page can make node_changed */
3193 *blk_addr
= dn
.data_blkaddr
;
3194 *node_changed
= dn
.node_changed
;
3196 f2fs_put_dnode(&dn
);
3199 __do_map_lock(sbi
, flag
, false);
3203 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
3204 loff_t pos
, unsigned len
, unsigned flags
,
3205 struct page
**pagep
, void **fsdata
)
3207 struct inode
*inode
= mapping
->host
;
3208 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3209 struct page
*page
= NULL
;
3210 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
3211 bool need_balance
= false, drop_atomic
= false;
3212 block_t blkaddr
= NULL_ADDR
;
3215 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
3217 if (!f2fs_is_checkpoint_ready(sbi
)) {
3222 if ((f2fs_is_atomic_file(inode
) &&
3223 !f2fs_available_free_memory(sbi
, INMEM_PAGES
)) ||
3224 is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
)) {
3231 * We should check this at this moment to avoid deadlock on inode page
3232 * and #0 page. The locking rule for inline_data conversion should be:
3233 * lock_page(page #0) -> lock_page(inode_page)
3236 err
= f2fs_convert_inline_inode(inode
);
3241 #ifdef CONFIG_F2FS_FS_COMPRESSION
3242 if (f2fs_compressed_file(inode
)) {
3247 ret
= f2fs_prepare_compress_overwrite(inode
, pagep
,
3260 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3261 * wait_for_stable_page. Will wait that below with our IO control.
3263 page
= f2fs_pagecache_get_page(mapping
, index
,
3264 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
3270 /* TODO: cluster can be compressed due to race with .writepage */
3274 err
= prepare_write_begin(sbi
, page
, pos
, len
,
3275 &blkaddr
, &need_balance
);
3279 if (need_balance
&& !IS_NOQUOTA(inode
) &&
3280 has_not_enough_free_secs(sbi
, 0, 0)) {
3282 f2fs_balance_fs(sbi
, true);
3284 if (page
->mapping
!= mapping
) {
3285 /* The page got truncated from under us */
3286 f2fs_put_page(page
, 1);
3291 f2fs_wait_on_page_writeback(page
, DATA
, false, true);
3293 if (len
== PAGE_SIZE
|| PageUptodate(page
))
3296 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
) &&
3297 !f2fs_verity_in_progress(inode
)) {
3298 zero_user_segment(page
, len
, PAGE_SIZE
);
3302 if (blkaddr
== NEW_ADDR
) {
3303 zero_user_segment(page
, 0, PAGE_SIZE
);
3304 SetPageUptodate(page
);
3306 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
,
3307 DATA_GENERIC_ENHANCE_READ
)) {
3308 err
= -EFSCORRUPTED
;
3311 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
3316 if (unlikely(page
->mapping
!= mapping
)) {
3317 f2fs_put_page(page
, 1);
3320 if (unlikely(!PageUptodate(page
))) {
3328 f2fs_put_page(page
, 1);
3329 f2fs_write_failed(mapping
, pos
+ len
);
3331 f2fs_drop_inmem_pages_all(sbi
, false);
3335 static int f2fs_write_end(struct file
*file
,
3336 struct address_space
*mapping
,
3337 loff_t pos
, unsigned len
, unsigned copied
,
3338 struct page
*page
, void *fsdata
)
3340 struct inode
*inode
= page
->mapping
->host
;
3342 trace_f2fs_write_end(inode
, pos
, len
, copied
);
3345 * This should be come from len == PAGE_SIZE, and we expect copied
3346 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3347 * let generic_perform_write() try to copy data again through copied=0.
3349 if (!PageUptodate(page
)) {
3350 if (unlikely(copied
!= len
))
3353 SetPageUptodate(page
);
3356 #ifdef CONFIG_F2FS_FS_COMPRESSION
3357 /* overwrite compressed file */
3358 if (f2fs_compressed_file(inode
) && fsdata
) {
3359 f2fs_compress_write_end(inode
, fsdata
, page
->index
, copied
);
3360 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3368 set_page_dirty(page
);
3370 if (pos
+ copied
> i_size_read(inode
) &&
3371 !f2fs_verity_in_progress(inode
))
3372 f2fs_i_size_write(inode
, pos
+ copied
);
3374 f2fs_put_page(page
, 1);
3375 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3379 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
3382 unsigned i_blkbits
= READ_ONCE(inode
->i_blkbits
);
3383 unsigned blkbits
= i_blkbits
;
3384 unsigned blocksize_mask
= (1 << blkbits
) - 1;
3385 unsigned long align
= offset
| iov_iter_alignment(iter
);
3386 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
3388 if (align
& blocksize_mask
) {
3390 blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
3391 blocksize_mask
= (1 << blkbits
) - 1;
3392 if (align
& blocksize_mask
)
3399 static void f2fs_dio_end_io(struct bio
*bio
)
3401 struct f2fs_private_dio
*dio
= bio
->bi_private
;
3403 dec_page_count(F2FS_I_SB(dio
->inode
),
3404 dio
->write
? F2FS_DIO_WRITE
: F2FS_DIO_READ
);
3406 bio
->bi_private
= dio
->orig_private
;
3407 bio
->bi_end_io
= dio
->orig_end_io
;
3414 static void f2fs_dio_submit_bio(struct bio
*bio
, struct inode
*inode
,
3417 struct f2fs_private_dio
*dio
;
3418 bool write
= (bio_op(bio
) == REQ_OP_WRITE
);
3420 dio
= f2fs_kzalloc(F2FS_I_SB(inode
),
3421 sizeof(struct f2fs_private_dio
), GFP_NOFS
);
3426 dio
->orig_end_io
= bio
->bi_end_io
;
3427 dio
->orig_private
= bio
->bi_private
;
3430 bio
->bi_end_io
= f2fs_dio_end_io
;
3431 bio
->bi_private
= dio
;
3433 inc_page_count(F2FS_I_SB(inode
),
3434 write
? F2FS_DIO_WRITE
: F2FS_DIO_READ
);
3439 bio
->bi_status
= BLK_STS_IOERR
;
3443 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3445 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
3446 struct inode
*inode
= mapping
->host
;
3447 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3448 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
3449 size_t count
= iov_iter_count(iter
);
3450 loff_t offset
= iocb
->ki_pos
;
3451 int rw
= iov_iter_rw(iter
);
3453 enum rw_hint hint
= iocb
->ki_hint
;
3454 int whint_mode
= F2FS_OPTION(sbi
).whint_mode
;
3457 err
= check_direct_IO(inode
, iter
, offset
);
3459 return err
< 0 ? err
: 0;
3461 if (f2fs_force_buffered_io(inode
, iocb
, iter
))
3464 do_opu
= allow_outplace_dio(inode
, iocb
, iter
);
3466 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
3468 if (rw
== WRITE
&& whint_mode
== WHINT_MODE_OFF
)
3469 iocb
->ki_hint
= WRITE_LIFE_NOT_SET
;
3471 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
3472 if (!down_read_trylock(&fi
->i_gc_rwsem
[rw
])) {
3473 iocb
->ki_hint
= hint
;
3477 if (do_opu
&& !down_read_trylock(&fi
->i_gc_rwsem
[READ
])) {
3478 up_read(&fi
->i_gc_rwsem
[rw
]);
3479 iocb
->ki_hint
= hint
;
3484 down_read(&fi
->i_gc_rwsem
[rw
]);
3486 down_read(&fi
->i_gc_rwsem
[READ
]);
3489 err
= __blockdev_direct_IO(iocb
, inode
, inode
->i_sb
->s_bdev
,
3490 iter
, rw
== WRITE
? get_data_block_dio_write
:
3491 get_data_block_dio
, NULL
, f2fs_dio_submit_bio
,
3492 DIO_LOCKING
| DIO_SKIP_HOLES
);
3495 up_read(&fi
->i_gc_rwsem
[READ
]);
3497 up_read(&fi
->i_gc_rwsem
[rw
]);
3500 if (whint_mode
== WHINT_MODE_OFF
)
3501 iocb
->ki_hint
= hint
;
3503 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
3506 set_inode_flag(inode
, FI_UPDATE_WRITE
);
3507 } else if (err
< 0) {
3508 f2fs_write_failed(mapping
, offset
+ count
);
3513 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
3518 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
3519 unsigned int length
)
3521 struct inode
*inode
= page
->mapping
->host
;
3522 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3524 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
3525 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
3528 if (PageDirty(page
)) {
3529 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
3530 dec_page_count(sbi
, F2FS_DIRTY_META
);
3531 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
3532 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
3534 inode_dec_dirty_pages(inode
);
3535 f2fs_remove_dirty_inode(inode
);
3539 clear_cold_data(page
);
3541 if (IS_ATOMIC_WRITTEN_PAGE(page
))
3542 return f2fs_drop_inmem_page(inode
, page
);
3544 f2fs_clear_page_private(page
);
3547 int f2fs_release_page(struct page
*page
, gfp_t wait
)
3549 /* If this is dirty page, keep PagePrivate */
3550 if (PageDirty(page
))
3553 /* This is atomic written page, keep Private */
3554 if (IS_ATOMIC_WRITTEN_PAGE(page
))
3557 clear_cold_data(page
);
3558 f2fs_clear_page_private(page
);
3562 static int f2fs_set_data_page_dirty(struct page
*page
)
3564 struct inode
*inode
= page_file_mapping(page
)->host
;
3566 trace_f2fs_set_page_dirty(page
, DATA
);
3568 if (!PageUptodate(page
))
3569 SetPageUptodate(page
);
3570 if (PageSwapCache(page
))
3571 return __set_page_dirty_nobuffers(page
);
3573 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
3574 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
3575 f2fs_register_inmem_page(inode
, page
);
3579 * Previously, this page has been registered, we just
3585 if (!PageDirty(page
)) {
3586 __set_page_dirty_nobuffers(page
);
3587 f2fs_update_dirty_page(inode
, page
);
3593 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
3595 struct inode
*inode
= mapping
->host
;
3597 if (f2fs_has_inline_data(inode
))
3600 /* make sure allocating whole blocks */
3601 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
3602 filemap_write_and_wait(mapping
);
3604 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
3607 #ifdef CONFIG_MIGRATION
3608 #include <linux/migrate.h>
3610 int f2fs_migrate_page(struct address_space
*mapping
,
3611 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
3613 int rc
, extra_count
;
3614 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
3615 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
3617 BUG_ON(PageWriteback(page
));
3619 /* migrating an atomic written page is safe with the inmem_lock hold */
3620 if (atomic_written
) {
3621 if (mode
!= MIGRATE_SYNC
)
3623 if (!mutex_trylock(&fi
->inmem_lock
))
3627 /* one extra reference was held for atomic_write page */
3628 extra_count
= atomic_written
? 1 : 0;
3629 rc
= migrate_page_move_mapping(mapping
, newpage
,
3631 if (rc
!= MIGRATEPAGE_SUCCESS
) {
3633 mutex_unlock(&fi
->inmem_lock
);
3637 if (atomic_written
) {
3638 struct inmem_pages
*cur
;
3639 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
3640 if (cur
->page
== page
) {
3641 cur
->page
= newpage
;
3644 mutex_unlock(&fi
->inmem_lock
);
3649 if (PagePrivate(page
)) {
3650 f2fs_set_page_private(newpage
, page_private(page
));
3651 f2fs_clear_page_private(page
);
3654 if (mode
!= MIGRATE_SYNC_NO_COPY
)
3655 migrate_page_copy(newpage
, page
);
3657 migrate_page_states(newpage
, page
);
3659 return MIGRATEPAGE_SUCCESS
;
3664 /* Copied from generic_swapfile_activate() to check any holes */
3665 static int check_swap_activate(struct swap_info_struct
*sis
,
3666 struct file
*swap_file
, sector_t
*span
)
3668 struct address_space
*mapping
= swap_file
->f_mapping
;
3669 struct inode
*inode
= mapping
->host
;
3670 unsigned blocks_per_page
;
3671 unsigned long page_no
;
3673 sector_t probe_block
;
3674 sector_t last_block
;
3675 sector_t lowest_block
= -1;
3676 sector_t highest_block
= 0;
3680 blkbits
= inode
->i_blkbits
;
3681 blocks_per_page
= PAGE_SIZE
>> blkbits
;
3684 * Map all the blocks into the extent list. This code doesn't try
3689 last_block
= i_size_read(inode
) >> blkbits
;
3690 while ((probe_block
+ blocks_per_page
) <= last_block
&&
3691 page_no
< sis
->max
) {
3692 unsigned block_in_page
;
3693 sector_t first_block
;
3699 block
= probe_block
;
3700 err
= bmap(inode
, &block
);
3703 first_block
= block
;
3706 * It must be PAGE_SIZE aligned on-disk
3708 if (first_block
& (blocks_per_page
- 1)) {
3713 for (block_in_page
= 1; block_in_page
< blocks_per_page
;
3716 block
= probe_block
+ block_in_page
;
3717 err
= bmap(inode
, &block
);
3722 if (block
!= first_block
+ block_in_page
) {
3729 first_block
>>= (PAGE_SHIFT
- blkbits
);
3730 if (page_no
) { /* exclude the header page */
3731 if (first_block
< lowest_block
)
3732 lowest_block
= first_block
;
3733 if (first_block
> highest_block
)
3734 highest_block
= first_block
;
3738 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3740 ret
= add_swap_extent(sis
, page_no
, 1, first_block
);
3745 probe_block
+= blocks_per_page
;
3750 *span
= 1 + highest_block
- lowest_block
;
3752 page_no
= 1; /* force Empty message */
3754 sis
->pages
= page_no
- 1;
3755 sis
->highest_bit
= page_no
- 1;
3759 pr_err("swapon: swapfile has holes\n");
3763 static int f2fs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
3766 struct inode
*inode
= file_inode(file
);
3769 if (!S_ISREG(inode
->i_mode
))
3772 if (f2fs_readonly(F2FS_I_SB(inode
)->sb
))
3775 ret
= f2fs_convert_inline_inode(inode
);
3779 if (f2fs_disable_compressed_file(inode
))
3782 ret
= check_swap_activate(sis
, file
, span
);
3786 set_inode_flag(inode
, FI_PIN_FILE
);
3787 f2fs_precache_extents(inode
);
3788 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3792 static void f2fs_swap_deactivate(struct file
*file
)
3794 struct inode
*inode
= file_inode(file
);
3796 clear_inode_flag(inode
, FI_PIN_FILE
);
3799 static int f2fs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
3805 static void f2fs_swap_deactivate(struct file
*file
)
3810 const struct address_space_operations f2fs_dblock_aops
= {
3811 .readpage
= f2fs_read_data_page
,
3812 .readpages
= f2fs_read_data_pages
,
3813 .writepage
= f2fs_write_data_page
,
3814 .writepages
= f2fs_write_data_pages
,
3815 .write_begin
= f2fs_write_begin
,
3816 .write_end
= f2fs_write_end
,
3817 .set_page_dirty
= f2fs_set_data_page_dirty
,
3818 .invalidatepage
= f2fs_invalidate_page
,
3819 .releasepage
= f2fs_release_page
,
3820 .direct_IO
= f2fs_direct_IO
,
3822 .swap_activate
= f2fs_swap_activate
,
3823 .swap_deactivate
= f2fs_swap_deactivate
,
3824 #ifdef CONFIG_MIGRATION
3825 .migratepage
= f2fs_migrate_page
,
3829 void f2fs_clear_page_cache_dirty_tag(struct page
*page
)
3831 struct address_space
*mapping
= page_mapping(page
);
3832 unsigned long flags
;
3834 xa_lock_irqsave(&mapping
->i_pages
, flags
);
3835 __xa_clear_mark(&mapping
->i_pages
, page_index(page
),
3836 PAGECACHE_TAG_DIRTY
);
3837 xa_unlock_irqrestore(&mapping
->i_pages
, flags
);
3840 int __init
f2fs_init_post_read_processing(void)
3842 bio_post_read_ctx_cache
=
3843 kmem_cache_create("f2fs_bio_post_read_ctx",
3844 sizeof(struct bio_post_read_ctx
), 0, 0, NULL
);
3845 if (!bio_post_read_ctx_cache
)
3847 bio_post_read_ctx_pool
=
3848 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS
,
3849 bio_post_read_ctx_cache
);
3850 if (!bio_post_read_ctx_pool
)
3851 goto fail_free_cache
;
3855 kmem_cache_destroy(bio_post_read_ctx_cache
);
3860 void f2fs_destroy_post_read_processing(void)
3862 mempool_destroy(bio_post_read_ctx_pool
);
3863 kmem_cache_destroy(bio_post_read_ctx_cache
);
3866 int f2fs_init_post_read_wq(struct f2fs_sb_info
*sbi
)
3868 if (!f2fs_sb_has_encrypt(sbi
) &&
3869 !f2fs_sb_has_verity(sbi
) &&
3870 !f2fs_sb_has_compression(sbi
))
3873 sbi
->post_read_wq
= alloc_workqueue("f2fs_post_read_wq",
3874 WQ_UNBOUND
| WQ_HIGHPRI
,
3876 if (!sbi
->post_read_wq
)
3881 void f2fs_destroy_post_read_wq(struct f2fs_sb_info
*sbi
)
3883 if (sbi
->post_read_wq
)
3884 destroy_workqueue(sbi
->post_read_wq
);
3887 int __init
f2fs_init_bio_entry_cache(void)
3889 bio_entry_slab
= f2fs_kmem_cache_create("bio_entry_slab",
3890 sizeof(struct bio_entry
));
3891 if (!bio_entry_slab
)
3896 void f2fs_destroy_bio_entry_cache(void)
3898 kmem_cache_destroy(bio_entry_slab
);