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
)
194 struct page
*page
= bio_first_page_all(bio
);
195 struct decompress_io_ctx
*dic
=
196 (struct decompress_io_ctx
*)page_private(page
);
198 f2fs_verify_pages(dic
->rpages
, dic
->cluster_size
);
203 static void f2fs_verity_work(struct work_struct
*work
)
205 struct bio_post_read_ctx
*ctx
=
206 container_of(work
, struct bio_post_read_ctx
, work
);
207 struct bio
*bio
= ctx
->bio
;
208 #ifdef CONFIG_F2FS_FS_COMPRESSION
209 unsigned int enabled_steps
= ctx
->enabled_steps
;
213 * fsverity_verify_bio() may call readpages() again, and while verity
214 * will be disabled for this, decryption may still be needed, resulting
215 * in another bio_post_read_ctx being allocated. So to prevent
216 * deadlocks we need to release the current ctx to the mempool first.
217 * This assumes that verity is the last post-read step.
219 mempool_free(ctx
, bio_post_read_ctx_pool
);
220 bio
->bi_private
= NULL
;
222 #ifdef CONFIG_F2FS_FS_COMPRESSION
223 /* previous step is decompression */
224 if (enabled_steps
& (1 << STEP_DECOMPRESS
)) {
225 f2fs_verify_bio(bio
);
226 f2fs_release_read_bio(bio
);
231 fsverity_verify_bio(bio
);
232 __f2fs_read_end_io(bio
, false, false);
235 static void f2fs_post_read_work(struct work_struct
*work
)
237 struct bio_post_read_ctx
*ctx
=
238 container_of(work
, struct bio_post_read_ctx
, work
);
240 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
))
241 f2fs_decrypt_work(ctx
);
243 if (ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
))
244 f2fs_decompress_work(ctx
);
246 if (ctx
->enabled_steps
& (1 << STEP_VERITY
)) {
247 INIT_WORK(&ctx
->work
, f2fs_verity_work
);
248 fsverity_enqueue_verify_work(&ctx
->work
);
252 __f2fs_read_end_io(ctx
->bio
,
253 ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
), false);
256 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info
*sbi
,
257 struct work_struct
*work
)
259 queue_work(sbi
->post_read_wq
, work
);
262 static void bio_post_read_processing(struct bio_post_read_ctx
*ctx
)
265 * We use different work queues for decryption and for verity because
266 * verity may require reading metadata pages that need decryption, and
267 * we shouldn't recurse to the same workqueue.
270 if (ctx
->enabled_steps
& (1 << STEP_DECRYPT
) ||
271 ctx
->enabled_steps
& (1 << STEP_DECOMPRESS
)) {
272 INIT_WORK(&ctx
->work
, f2fs_post_read_work
);
273 f2fs_enqueue_post_read_work(ctx
->sbi
, &ctx
->work
);
277 if (ctx
->enabled_steps
& (1 << STEP_VERITY
)) {
278 INIT_WORK(&ctx
->work
, f2fs_verity_work
);
279 fsverity_enqueue_verify_work(&ctx
->work
);
283 __f2fs_read_end_io(ctx
->bio
, false, false);
286 static bool f2fs_bio_post_read_required(struct bio
*bio
)
288 return bio
->bi_private
;
291 static void f2fs_read_end_io(struct bio
*bio
)
293 struct f2fs_sb_info
*sbi
= F2FS_P_SB(bio_first_page_all(bio
));
295 if (time_to_inject(sbi
, FAULT_READ_IO
)) {
296 f2fs_show_injection_info(sbi
, FAULT_READ_IO
);
297 bio
->bi_status
= BLK_STS_IOERR
;
300 if (f2fs_bio_post_read_required(bio
)) {
301 struct bio_post_read_ctx
*ctx
= bio
->bi_private
;
303 bio_post_read_processing(ctx
);
307 __f2fs_read_end_io(bio
, false, false);
310 static void f2fs_write_end_io(struct bio
*bio
)
312 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
313 struct bio_vec
*bvec
;
314 struct bvec_iter_all iter_all
;
316 if (time_to_inject(sbi
, FAULT_WRITE_IO
)) {
317 f2fs_show_injection_info(sbi
, FAULT_WRITE_IO
);
318 bio
->bi_status
= BLK_STS_IOERR
;
321 bio_for_each_segment_all(bvec
, bio
, iter_all
) {
322 struct page
*page
= bvec
->bv_page
;
323 enum count_type type
= WB_DATA_TYPE(page
);
325 if (IS_DUMMY_WRITTEN_PAGE(page
)) {
326 set_page_private(page
, (unsigned long)NULL
);
327 ClearPagePrivate(page
);
329 mempool_free(page
, sbi
->write_io_dummy
);
331 if (unlikely(bio
->bi_status
))
332 f2fs_stop_checkpoint(sbi
, true);
336 fscrypt_finalize_bounce_page(&page
);
338 #ifdef CONFIG_F2FS_FS_COMPRESSION
339 if (f2fs_is_compressed_page(page
)) {
340 f2fs_compress_write_end_io(bio
, page
);
345 if (unlikely(bio
->bi_status
)) {
346 mapping_set_error(page
->mapping
, -EIO
);
347 if (type
== F2FS_WB_CP_DATA
)
348 f2fs_stop_checkpoint(sbi
, true);
351 f2fs_bug_on(sbi
, page
->mapping
== NODE_MAPPING(sbi
) &&
352 page
->index
!= nid_of_node(page
));
354 dec_page_count(sbi
, type
);
355 if (f2fs_in_warm_node_list(sbi
, page
))
356 f2fs_del_fsync_node_entry(sbi
, page
);
357 clear_cold_data(page
);
358 end_page_writeback(page
);
360 if (!get_pages(sbi
, F2FS_WB_CP_DATA
) &&
361 wq_has_sleeper(&sbi
->cp_wait
))
362 wake_up(&sbi
->cp_wait
);
368 * Return true, if pre_bio's bdev is same as its target device.
370 struct block_device
*f2fs_target_device(struct f2fs_sb_info
*sbi
,
371 block_t blk_addr
, struct bio
*bio
)
373 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
376 if (f2fs_is_multi_device(sbi
)) {
377 for (i
= 0; i
< sbi
->s_ndevs
; i
++) {
378 if (FDEV(i
).start_blk
<= blk_addr
&&
379 FDEV(i
).end_blk
>= blk_addr
) {
380 blk_addr
-= FDEV(i
).start_blk
;
387 bio_set_dev(bio
, bdev
);
388 bio
->bi_iter
.bi_sector
= SECTOR_FROM_BLOCK(blk_addr
);
393 int f2fs_target_device_index(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
397 if (!f2fs_is_multi_device(sbi
))
400 for (i
= 0; i
< sbi
->s_ndevs
; i
++)
401 if (FDEV(i
).start_blk
<= blkaddr
&& FDEV(i
).end_blk
>= blkaddr
)
406 static bool __same_bdev(struct f2fs_sb_info
*sbi
,
407 block_t blk_addr
, struct bio
*bio
)
409 struct block_device
*b
= f2fs_target_device(sbi
, blk_addr
, NULL
);
410 return bio
->bi_disk
== b
->bd_disk
&& bio
->bi_partno
== b
->bd_partno
;
414 * Low-level block read/write IO operations.
416 static struct bio
*__bio_alloc(struct f2fs_io_info
*fio
, int npages
)
418 struct f2fs_sb_info
*sbi
= fio
->sbi
;
421 bio
= f2fs_bio_alloc(sbi
, npages
, true);
423 f2fs_target_device(sbi
, fio
->new_blkaddr
, bio
);
424 if (is_read_io(fio
->op
)) {
425 bio
->bi_end_io
= f2fs_read_end_io
;
426 bio
->bi_private
= NULL
;
428 bio
->bi_end_io
= f2fs_write_end_io
;
429 bio
->bi_private
= sbi
;
430 bio
->bi_write_hint
= f2fs_io_type_to_rw_hint(sbi
,
431 fio
->type
, fio
->temp
);
434 wbc_init_bio(fio
->io_wbc
, bio
);
439 static inline void __submit_bio(struct f2fs_sb_info
*sbi
,
440 struct bio
*bio
, enum page_type type
)
442 if (!is_read_io(bio_op(bio
))) {
445 if (type
!= DATA
&& type
!= NODE
)
448 if (test_opt(sbi
, LFS
) && current
->plug
)
449 blk_finish_plug(current
->plug
);
451 if (F2FS_IO_ALIGNED(sbi
))
454 start
= bio
->bi_iter
.bi_size
>> F2FS_BLKSIZE_BITS
;
455 start
%= F2FS_IO_SIZE(sbi
);
460 /* fill dummy pages */
461 for (; start
< F2FS_IO_SIZE(sbi
); start
++) {
463 mempool_alloc(sbi
->write_io_dummy
,
464 GFP_NOIO
| __GFP_NOFAIL
);
465 f2fs_bug_on(sbi
, !page
);
467 zero_user_segment(page
, 0, PAGE_SIZE
);
468 SetPagePrivate(page
);
469 set_page_private(page
, (unsigned long)DUMMY_WRITTEN_PAGE
);
471 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
)
475 * In the NODE case, we lose next block address chain. So, we
476 * need to do checkpoint in f2fs_sync_file.
479 set_sbi_flag(sbi
, SBI_NEED_CP
);
482 if (is_read_io(bio_op(bio
)))
483 trace_f2fs_submit_read_bio(sbi
->sb
, type
, bio
);
485 trace_f2fs_submit_write_bio(sbi
->sb
, type
, bio
);
489 void f2fs_submit_bio(struct f2fs_sb_info
*sbi
,
490 struct bio
*bio
, enum page_type type
)
492 __submit_bio(sbi
, bio
, type
);
495 static void __submit_merged_bio(struct f2fs_bio_info
*io
)
497 struct f2fs_io_info
*fio
= &io
->fio
;
502 bio_set_op_attrs(io
->bio
, fio
->op
, fio
->op_flags
);
504 if (is_read_io(fio
->op
))
505 trace_f2fs_prepare_read_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
507 trace_f2fs_prepare_write_bio(io
->sbi
->sb
, fio
->type
, io
->bio
);
509 __submit_bio(io
->sbi
, io
->bio
, fio
->type
);
513 static bool __has_merged_page(struct bio
*bio
, struct inode
*inode
,
514 struct page
*page
, nid_t ino
)
516 struct bio_vec
*bvec
;
517 struct bvec_iter_all iter_all
;
522 if (!inode
&& !page
&& !ino
)
525 bio_for_each_segment_all(bvec
, bio
, iter_all
) {
526 struct page
*target
= bvec
->bv_page
;
528 if (fscrypt_is_bounce_page(target
)) {
529 target
= fscrypt_pagecache_page(target
);
533 if (f2fs_is_compressed_page(target
)) {
534 target
= f2fs_compress_control_page(target
);
539 if (inode
&& inode
== target
->mapping
->host
)
541 if (page
&& page
== target
)
543 if (ino
&& ino
== ino_of_node(target
))
550 static void __f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
,
551 enum page_type type
, enum temp_type temp
)
553 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
554 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
556 down_write(&io
->io_rwsem
);
558 /* change META to META_FLUSH in the checkpoint procedure */
559 if (type
>= META_FLUSH
) {
560 io
->fio
.type
= META_FLUSH
;
561 io
->fio
.op
= REQ_OP_WRITE
;
562 io
->fio
.op_flags
= REQ_META
| REQ_PRIO
| REQ_SYNC
;
563 if (!test_opt(sbi
, NOBARRIER
))
564 io
->fio
.op_flags
|= REQ_PREFLUSH
| REQ_FUA
;
566 __submit_merged_bio(io
);
567 up_write(&io
->io_rwsem
);
570 static void __submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
571 struct inode
*inode
, struct page
*page
,
572 nid_t ino
, enum page_type type
, bool force
)
577 for (temp
= HOT
; temp
< NR_TEMP_TYPE
; temp
++) {
579 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
580 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + temp
;
582 down_read(&io
->io_rwsem
);
583 ret
= __has_merged_page(io
->bio
, inode
, page
, ino
);
584 up_read(&io
->io_rwsem
);
587 __f2fs_submit_merged_write(sbi
, type
, temp
);
589 /* TODO: use HOT temp only for meta pages now. */
595 void f2fs_submit_merged_write(struct f2fs_sb_info
*sbi
, enum page_type type
)
597 __submit_merged_write_cond(sbi
, NULL
, NULL
, 0, type
, true);
600 void f2fs_submit_merged_write_cond(struct f2fs_sb_info
*sbi
,
601 struct inode
*inode
, struct page
*page
,
602 nid_t ino
, enum page_type type
)
604 __submit_merged_write_cond(sbi
, inode
, page
, ino
, type
, false);
607 void f2fs_flush_merged_writes(struct f2fs_sb_info
*sbi
)
609 f2fs_submit_merged_write(sbi
, DATA
);
610 f2fs_submit_merged_write(sbi
, NODE
);
611 f2fs_submit_merged_write(sbi
, META
);
615 * Fill the locked page with data located in the block address.
616 * A caller needs to unlock the page on failure.
618 int f2fs_submit_page_bio(struct f2fs_io_info
*fio
)
621 struct page
*page
= fio
->encrypted_page
?
622 fio
->encrypted_page
: fio
->page
;
624 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
625 fio
->is_por
? META_POR
: (__is_meta_io(fio
) ?
626 META_GENERIC
: DATA_GENERIC_ENHANCE
)))
627 return -EFSCORRUPTED
;
629 trace_f2fs_submit_page_bio(page
, fio
);
630 f2fs_trace_ios(fio
, 0);
632 /* Allocate a new bio */
633 bio
= __bio_alloc(fio
, 1);
635 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
640 if (fio
->io_wbc
&& !is_read_io(fio
->op
))
641 wbc_account_cgroup_owner(fio
->io_wbc
, page
, PAGE_SIZE
);
643 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
645 inc_page_count(fio
->sbi
, is_read_io(fio
->op
) ?
646 __read_io_type(page
): WB_DATA_TYPE(fio
->page
));
648 __submit_bio(fio
->sbi
, bio
, fio
->type
);
652 static bool page_is_mergeable(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
653 block_t last_blkaddr
, block_t cur_blkaddr
)
655 if (last_blkaddr
+ 1 != cur_blkaddr
)
657 return __same_bdev(sbi
, cur_blkaddr
, bio
);
660 static bool io_type_is_mergeable(struct f2fs_bio_info
*io
,
661 struct f2fs_io_info
*fio
)
663 if (io
->fio
.op
!= fio
->op
)
665 return io
->fio
.op_flags
== fio
->op_flags
;
668 static bool io_is_mergeable(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
669 struct f2fs_bio_info
*io
,
670 struct f2fs_io_info
*fio
,
671 block_t last_blkaddr
,
674 if (F2FS_IO_ALIGNED(sbi
) && (fio
->type
== DATA
|| fio
->type
== NODE
)) {
675 unsigned int filled_blocks
=
676 F2FS_BYTES_TO_BLK(bio
->bi_iter
.bi_size
);
677 unsigned int io_size
= F2FS_IO_SIZE(sbi
);
678 unsigned int left_vecs
= bio
->bi_max_vecs
- bio
->bi_vcnt
;
680 /* IOs in bio is aligned and left space of vectors is not enough */
681 if (!(filled_blocks
% io_size
) && left_vecs
< io_size
)
684 if (!page_is_mergeable(sbi
, bio
, last_blkaddr
, cur_blkaddr
))
686 return io_type_is_mergeable(io
, fio
);
689 static void add_bio_entry(struct f2fs_sb_info
*sbi
, struct bio
*bio
,
690 struct page
*page
, enum temp_type temp
)
692 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
693 struct bio_entry
*be
;
695 be
= f2fs_kmem_cache_alloc(bio_entry_slab
, GFP_NOFS
);
699 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) != PAGE_SIZE
)
702 down_write(&io
->bio_list_lock
);
703 list_add_tail(&be
->list
, &io
->bio_list
);
704 up_write(&io
->bio_list_lock
);
707 static void del_bio_entry(struct bio_entry
*be
)
710 kmem_cache_free(bio_entry_slab
, be
);
713 static int add_ipu_page(struct f2fs_sb_info
*sbi
, struct bio
**bio
,
720 for (temp
= HOT
; temp
< NR_TEMP_TYPE
&& !found
; temp
++) {
721 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
722 struct list_head
*head
= &io
->bio_list
;
723 struct bio_entry
*be
;
725 down_write(&io
->bio_list_lock
);
726 list_for_each_entry(be
, head
, list
) {
732 if (bio_add_page(*bio
, page
, PAGE_SIZE
, 0) ==
740 __submit_bio(sbi
, *bio
, DATA
);
743 up_write(&io
->bio_list_lock
);
754 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info
*sbi
,
755 struct bio
**bio
, struct page
*page
)
759 struct bio
*target
= bio
? *bio
: NULL
;
761 for (temp
= HOT
; temp
< NR_TEMP_TYPE
&& !found
; temp
++) {
762 struct f2fs_bio_info
*io
= sbi
->write_io
[DATA
] + temp
;
763 struct list_head
*head
= &io
->bio_list
;
764 struct bio_entry
*be
;
766 if (list_empty(head
))
769 down_read(&io
->bio_list_lock
);
770 list_for_each_entry(be
, head
, list
) {
772 found
= (target
== be
->bio
);
774 found
= __has_merged_page(be
->bio
, NULL
,
779 up_read(&io
->bio_list_lock
);
786 down_write(&io
->bio_list_lock
);
787 list_for_each_entry(be
, head
, list
) {
789 found
= (target
== be
->bio
);
791 found
= __has_merged_page(be
->bio
, NULL
,
799 up_write(&io
->bio_list_lock
);
803 __submit_bio(sbi
, target
, DATA
);
810 int f2fs_merge_page_bio(struct f2fs_io_info
*fio
)
812 struct bio
*bio
= *fio
->bio
;
813 struct page
*page
= fio
->encrypted_page
?
814 fio
->encrypted_page
: fio
->page
;
816 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->new_blkaddr
,
817 __is_meta_io(fio
) ? META_GENERIC
: DATA_GENERIC
))
818 return -EFSCORRUPTED
;
820 trace_f2fs_submit_page_bio(page
, fio
);
821 f2fs_trace_ios(fio
, 0);
823 if (bio
&& !page_is_mergeable(fio
->sbi
, bio
, *fio
->last_block
,
825 f2fs_submit_merged_ipu_write(fio
->sbi
, &bio
, NULL
);
828 bio
= __bio_alloc(fio
, BIO_MAX_PAGES
);
829 bio_set_op_attrs(bio
, fio
->op
, fio
->op_flags
);
831 add_bio_entry(fio
->sbi
, bio
, page
, fio
->temp
);
833 if (add_ipu_page(fio
->sbi
, &bio
, page
))
838 wbc_account_cgroup_owner(fio
->io_wbc
, page
, PAGE_SIZE
);
840 inc_page_count(fio
->sbi
, WB_DATA_TYPE(page
));
842 *fio
->last_block
= fio
->new_blkaddr
;
848 void f2fs_submit_page_write(struct f2fs_io_info
*fio
)
850 struct f2fs_sb_info
*sbi
= fio
->sbi
;
851 enum page_type btype
= PAGE_TYPE_OF_BIO(fio
->type
);
852 struct f2fs_bio_info
*io
= sbi
->write_io
[btype
] + fio
->temp
;
853 struct page
*bio_page
;
855 f2fs_bug_on(sbi
, is_read_io(fio
->op
));
857 down_write(&io
->io_rwsem
);
860 spin_lock(&io
->io_lock
);
861 if (list_empty(&io
->io_list
)) {
862 spin_unlock(&io
->io_lock
);
865 fio
= list_first_entry(&io
->io_list
,
866 struct f2fs_io_info
, list
);
867 list_del(&fio
->list
);
868 spin_unlock(&io
->io_lock
);
871 verify_fio_blkaddr(fio
);
873 if (fio
->encrypted_page
)
874 bio_page
= fio
->encrypted_page
;
875 else if (fio
->compressed_page
)
876 bio_page
= fio
->compressed_page
;
878 bio_page
= fio
->page
;
880 /* set submitted = true as a return value */
881 fio
->submitted
= true;
883 inc_page_count(sbi
, WB_DATA_TYPE(bio_page
));
885 if (io
->bio
&& !io_is_mergeable(sbi
, io
->bio
, io
, fio
,
886 io
->last_block_in_bio
, fio
->new_blkaddr
))
887 __submit_merged_bio(io
);
889 if (io
->bio
== NULL
) {
890 if (F2FS_IO_ALIGNED(sbi
) &&
891 (fio
->type
== DATA
|| fio
->type
== NODE
) &&
892 fio
->new_blkaddr
& F2FS_IO_SIZE_MASK(sbi
)) {
893 dec_page_count(sbi
, WB_DATA_TYPE(bio_page
));
897 io
->bio
= __bio_alloc(fio
, BIO_MAX_PAGES
);
901 if (bio_add_page(io
->bio
, bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
902 __submit_merged_bio(io
);
907 wbc_account_cgroup_owner(fio
->io_wbc
, bio_page
, PAGE_SIZE
);
909 io
->last_block_in_bio
= fio
->new_blkaddr
;
910 f2fs_trace_ios(fio
, 0);
912 trace_f2fs_submit_page_write(fio
->page
, fio
);
917 if (is_sbi_flag_set(sbi
, SBI_IS_SHUTDOWN
) ||
918 !f2fs_is_checkpoint_ready(sbi
))
919 __submit_merged_bio(io
);
920 up_write(&io
->io_rwsem
);
923 static inline bool f2fs_need_verity(const struct inode
*inode
, pgoff_t idx
)
925 return fsverity_active(inode
) &&
926 idx
< DIV_ROUND_UP(inode
->i_size
, PAGE_SIZE
);
929 static struct bio
*f2fs_grab_read_bio(struct inode
*inode
, block_t blkaddr
,
930 unsigned nr_pages
, unsigned op_flag
,
933 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
935 struct bio_post_read_ctx
*ctx
;
936 unsigned int post_read_steps
= 0;
938 bio
= f2fs_bio_alloc(sbi
, min_t(int, nr_pages
, BIO_MAX_PAGES
), false);
940 return ERR_PTR(-ENOMEM
);
941 f2fs_target_device(sbi
, blkaddr
, bio
);
942 bio
->bi_end_io
= f2fs_read_end_io
;
943 bio_set_op_attrs(bio
, REQ_OP_READ
, op_flag
);
945 if (f2fs_encrypted_file(inode
))
946 post_read_steps
|= 1 << STEP_DECRYPT
;
947 if (f2fs_compressed_file(inode
))
948 post_read_steps
|= 1 << STEP_DECOMPRESS
;
949 if (f2fs_need_verity(inode
, first_idx
))
950 post_read_steps
|= 1 << STEP_VERITY
;
952 if (post_read_steps
) {
953 /* Due to the mempool, this never fails. */
954 ctx
= mempool_alloc(bio_post_read_ctx_pool
, GFP_NOFS
);
957 ctx
->enabled_steps
= post_read_steps
;
958 bio
->bi_private
= ctx
;
964 static void f2fs_release_read_bio(struct bio
*bio
)
967 mempool_free(bio
->bi_private
, bio_post_read_ctx_pool
);
971 /* This can handle encryption stuffs */
972 static int f2fs_submit_page_read(struct inode
*inode
, struct page
*page
,
975 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
978 bio
= f2fs_grab_read_bio(inode
, blkaddr
, 1, 0, page
->index
);
982 /* wait for GCed page writeback via META_MAPPING */
983 f2fs_wait_on_block_writeback(inode
, blkaddr
);
985 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
989 ClearPageError(page
);
990 inc_page_count(sbi
, F2FS_RD_DATA
);
991 __submit_bio(sbi
, bio
, DATA
);
995 static void __set_data_blkaddr(struct dnode_of_data
*dn
)
997 struct f2fs_node
*rn
= F2FS_NODE(dn
->node_page
);
1001 if (IS_INODE(dn
->node_page
) && f2fs_has_extra_attr(dn
->inode
))
1002 base
= get_extra_isize(dn
->inode
);
1004 /* Get physical address of data block */
1005 addr_array
= blkaddr_in_node(rn
);
1006 addr_array
[base
+ dn
->ofs_in_node
] = cpu_to_le32(dn
->data_blkaddr
);
1010 * Lock ordering for the change of data block address:
1013 * update block addresses in the node page
1015 void f2fs_set_data_blkaddr(struct dnode_of_data
*dn
)
1017 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true, true);
1018 __set_data_blkaddr(dn
);
1019 if (set_page_dirty(dn
->node_page
))
1020 dn
->node_changed
= true;
1023 void f2fs_update_data_blkaddr(struct dnode_of_data
*dn
, block_t blkaddr
)
1025 dn
->data_blkaddr
= blkaddr
;
1026 f2fs_set_data_blkaddr(dn
);
1027 f2fs_update_extent_cache(dn
);
1030 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1031 int f2fs_reserve_new_blocks(struct dnode_of_data
*dn
, blkcnt_t count
)
1033 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1039 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
1041 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
1044 trace_f2fs_reserve_new_blocks(dn
->inode
, dn
->nid
,
1045 dn
->ofs_in_node
, count
);
1047 f2fs_wait_on_page_writeback(dn
->node_page
, NODE
, true, true);
1049 for (; count
> 0; dn
->ofs_in_node
++) {
1050 block_t blkaddr
= datablock_addr(dn
->inode
,
1051 dn
->node_page
, dn
->ofs_in_node
);
1052 if (blkaddr
== NULL_ADDR
) {
1053 dn
->data_blkaddr
= NEW_ADDR
;
1054 __set_data_blkaddr(dn
);
1059 if (set_page_dirty(dn
->node_page
))
1060 dn
->node_changed
= true;
1064 /* Should keep dn->ofs_in_node unchanged */
1065 int f2fs_reserve_new_block(struct dnode_of_data
*dn
)
1067 unsigned int ofs_in_node
= dn
->ofs_in_node
;
1070 ret
= f2fs_reserve_new_blocks(dn
, 1);
1071 dn
->ofs_in_node
= ofs_in_node
;
1075 int f2fs_reserve_block(struct dnode_of_data
*dn
, pgoff_t index
)
1077 bool need_put
= dn
->inode_page
? false : true;
1080 err
= f2fs_get_dnode_of_data(dn
, index
, ALLOC_NODE
);
1084 if (dn
->data_blkaddr
== NULL_ADDR
)
1085 err
= f2fs_reserve_new_block(dn
);
1086 if (err
|| need_put
)
1091 int f2fs_get_block(struct dnode_of_data
*dn
, pgoff_t index
)
1093 struct extent_info ei
= {0,0,0};
1094 struct inode
*inode
= dn
->inode
;
1096 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1097 dn
->data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1101 return f2fs_reserve_block(dn
, index
);
1104 struct page
*f2fs_get_read_data_page(struct inode
*inode
, pgoff_t index
,
1105 int op_flags
, bool for_write
)
1107 struct address_space
*mapping
= inode
->i_mapping
;
1108 struct dnode_of_data dn
;
1110 struct extent_info ei
= {0,0,0};
1113 page
= f2fs_grab_cache_page(mapping
, index
, for_write
);
1115 return ERR_PTR(-ENOMEM
);
1117 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
1118 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
1119 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), dn
.data_blkaddr
,
1120 DATA_GENERIC_ENHANCE_READ
)) {
1121 err
= -EFSCORRUPTED
;
1127 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1128 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
1131 f2fs_put_dnode(&dn
);
1133 if (unlikely(dn
.data_blkaddr
== NULL_ADDR
)) {
1137 if (dn
.data_blkaddr
!= NEW_ADDR
&&
1138 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode
),
1140 DATA_GENERIC_ENHANCE
)) {
1141 err
= -EFSCORRUPTED
;
1145 if (PageUptodate(page
)) {
1151 * A new dentry page is allocated but not able to be written, since its
1152 * new inode page couldn't be allocated due to -ENOSPC.
1153 * In such the case, its blkaddr can be remained as NEW_ADDR.
1154 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1155 * f2fs_init_inode_metadata.
1157 if (dn
.data_blkaddr
== NEW_ADDR
) {
1158 zero_user_segment(page
, 0, PAGE_SIZE
);
1159 if (!PageUptodate(page
))
1160 SetPageUptodate(page
);
1165 err
= f2fs_submit_page_read(inode
, page
, dn
.data_blkaddr
);
1171 f2fs_put_page(page
, 1);
1172 return ERR_PTR(err
);
1175 struct page
*f2fs_find_data_page(struct inode
*inode
, pgoff_t index
)
1177 struct address_space
*mapping
= inode
->i_mapping
;
1180 page
= find_get_page(mapping
, index
);
1181 if (page
&& PageUptodate(page
))
1183 f2fs_put_page(page
, 0);
1185 page
= f2fs_get_read_data_page(inode
, index
, 0, false);
1189 if (PageUptodate(page
))
1192 wait_on_page_locked(page
);
1193 if (unlikely(!PageUptodate(page
))) {
1194 f2fs_put_page(page
, 0);
1195 return ERR_PTR(-EIO
);
1201 * If it tries to access a hole, return an error.
1202 * Because, the callers, functions in dir.c and GC, should be able to know
1203 * whether this page exists or not.
1205 struct page
*f2fs_get_lock_data_page(struct inode
*inode
, pgoff_t index
,
1208 struct address_space
*mapping
= inode
->i_mapping
;
1211 page
= f2fs_get_read_data_page(inode
, index
, 0, for_write
);
1215 /* wait for read completion */
1217 if (unlikely(page
->mapping
!= mapping
)) {
1218 f2fs_put_page(page
, 1);
1221 if (unlikely(!PageUptodate(page
))) {
1222 f2fs_put_page(page
, 1);
1223 return ERR_PTR(-EIO
);
1229 * Caller ensures that this data page is never allocated.
1230 * A new zero-filled data page is allocated in the page cache.
1232 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1234 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1235 * ipage should be released by this function.
1237 struct page
*f2fs_get_new_data_page(struct inode
*inode
,
1238 struct page
*ipage
, pgoff_t index
, bool new_i_size
)
1240 struct address_space
*mapping
= inode
->i_mapping
;
1242 struct dnode_of_data dn
;
1245 page
= f2fs_grab_cache_page(mapping
, index
, true);
1248 * before exiting, we should make sure ipage will be released
1249 * if any error occur.
1251 f2fs_put_page(ipage
, 1);
1252 return ERR_PTR(-ENOMEM
);
1255 set_new_dnode(&dn
, inode
, ipage
, NULL
, 0);
1256 err
= f2fs_reserve_block(&dn
, index
);
1258 f2fs_put_page(page
, 1);
1259 return ERR_PTR(err
);
1262 f2fs_put_dnode(&dn
);
1264 if (PageUptodate(page
))
1267 if (dn
.data_blkaddr
== NEW_ADDR
) {
1268 zero_user_segment(page
, 0, PAGE_SIZE
);
1269 if (!PageUptodate(page
))
1270 SetPageUptodate(page
);
1272 f2fs_put_page(page
, 1);
1274 /* if ipage exists, blkaddr should be NEW_ADDR */
1275 f2fs_bug_on(F2FS_I_SB(inode
), ipage
);
1276 page
= f2fs_get_lock_data_page(inode
, index
, true);
1281 if (new_i_size
&& i_size_read(inode
) <
1282 ((loff_t
)(index
+ 1) << PAGE_SHIFT
))
1283 f2fs_i_size_write(inode
, ((loff_t
)(index
+ 1) << PAGE_SHIFT
));
1287 static int __allocate_data_block(struct dnode_of_data
*dn
, int seg_type
)
1289 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dn
->inode
);
1290 struct f2fs_summary sum
;
1291 struct node_info ni
;
1292 block_t old_blkaddr
;
1296 if (unlikely(is_inode_flag_set(dn
->inode
, FI_NO_ALLOC
)))
1299 err
= f2fs_get_node_info(sbi
, dn
->nid
, &ni
);
1303 dn
->data_blkaddr
= datablock_addr(dn
->inode
,
1304 dn
->node_page
, dn
->ofs_in_node
);
1305 if (dn
->data_blkaddr
!= NULL_ADDR
)
1308 if (unlikely((err
= inc_valid_block_count(sbi
, dn
->inode
, &count
))))
1312 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
1313 old_blkaddr
= dn
->data_blkaddr
;
1314 f2fs_allocate_data_block(sbi
, NULL
, old_blkaddr
, &dn
->data_blkaddr
,
1315 &sum
, seg_type
, NULL
, false);
1316 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
1317 invalidate_mapping_pages(META_MAPPING(sbi
),
1318 old_blkaddr
, old_blkaddr
);
1319 f2fs_update_data_blkaddr(dn
, dn
->data_blkaddr
);
1322 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1323 * data from unwritten block via dio_read.
1328 int f2fs_preallocate_blocks(struct kiocb
*iocb
, struct iov_iter
*from
)
1330 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1331 struct f2fs_map_blocks map
;
1334 bool direct_io
= iocb
->ki_flags
& IOCB_DIRECT
;
1336 map
.m_lblk
= F2FS_BLK_ALIGN(iocb
->ki_pos
);
1337 map
.m_len
= F2FS_BYTES_TO_BLK(iocb
->ki_pos
+ iov_iter_count(from
));
1338 if (map
.m_len
> map
.m_lblk
)
1339 map
.m_len
-= map
.m_lblk
;
1343 map
.m_next_pgofs
= NULL
;
1344 map
.m_next_extent
= NULL
;
1345 map
.m_seg_type
= NO_CHECK_TYPE
;
1346 map
.m_may_create
= true;
1349 map
.m_seg_type
= f2fs_rw_hint_to_seg_type(iocb
->ki_hint
);
1350 flag
= f2fs_force_buffered_io(inode
, iocb
, from
) ?
1351 F2FS_GET_BLOCK_PRE_AIO
:
1352 F2FS_GET_BLOCK_PRE_DIO
;
1355 if (iocb
->ki_pos
+ iov_iter_count(from
) > MAX_INLINE_DATA(inode
)) {
1356 err
= f2fs_convert_inline_inode(inode
);
1360 if (f2fs_has_inline_data(inode
))
1363 flag
= F2FS_GET_BLOCK_PRE_AIO
;
1366 err
= f2fs_map_blocks(inode
, &map
, 1, flag
);
1367 if (map
.m_len
> 0 && err
== -ENOSPC
) {
1369 set_inode_flag(inode
, FI_NO_PREALLOC
);
1375 void __do_map_lock(struct f2fs_sb_info
*sbi
, int flag
, bool lock
)
1377 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1379 down_read(&sbi
->node_change
);
1381 up_read(&sbi
->node_change
);
1386 f2fs_unlock_op(sbi
);
1391 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1392 * f2fs_map_blocks structure.
1393 * If original data blocks are allocated, then give them to blockdev.
1395 * a. preallocate requested block addresses
1396 * b. do not use extent cache for better performance
1397 * c. give the block addresses to blockdev
1399 int f2fs_map_blocks(struct inode
*inode
, struct f2fs_map_blocks
*map
,
1400 int create
, int flag
)
1402 unsigned int maxblocks
= map
->m_len
;
1403 struct dnode_of_data dn
;
1404 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1405 int mode
= map
->m_may_create
? ALLOC_NODE
: LOOKUP_NODE
;
1406 pgoff_t pgofs
, end_offset
, end
;
1407 int err
= 0, ofs
= 1;
1408 unsigned int ofs_in_node
, last_ofs_in_node
;
1410 struct extent_info ei
= {0,0,0};
1412 unsigned int start_pgofs
;
1420 /* it only supports block size == page size */
1421 pgofs
= (pgoff_t
)map
->m_lblk
;
1422 end
= pgofs
+ maxblocks
;
1424 if (!create
&& f2fs_lookup_extent_cache(inode
, pgofs
, &ei
)) {
1425 if (test_opt(sbi
, LFS
) && flag
== F2FS_GET_BLOCK_DIO
&&
1429 map
->m_pblk
= ei
.blk
+ pgofs
- ei
.fofs
;
1430 map
->m_len
= min((pgoff_t
)maxblocks
, ei
.fofs
+ ei
.len
- pgofs
);
1431 map
->m_flags
= F2FS_MAP_MAPPED
;
1432 if (map
->m_next_extent
)
1433 *map
->m_next_extent
= pgofs
+ map
->m_len
;
1435 /* for hardware encryption, but to avoid potential issue in future */
1436 if (flag
== F2FS_GET_BLOCK_DIO
)
1437 f2fs_wait_on_block_writeback_range(inode
,
1438 map
->m_pblk
, map
->m_len
);
1443 if (map
->m_may_create
)
1444 __do_map_lock(sbi
, flag
, true);
1446 /* When reading holes, we need its node page */
1447 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
1448 err
= f2fs_get_dnode_of_data(&dn
, pgofs
, mode
);
1450 if (flag
== F2FS_GET_BLOCK_BMAP
)
1452 if (err
== -ENOENT
) {
1454 if (map
->m_next_pgofs
)
1455 *map
->m_next_pgofs
=
1456 f2fs_get_next_page_offset(&dn
, pgofs
);
1457 if (map
->m_next_extent
)
1458 *map
->m_next_extent
=
1459 f2fs_get_next_page_offset(&dn
, pgofs
);
1464 start_pgofs
= pgofs
;
1466 last_ofs_in_node
= ofs_in_node
= dn
.ofs_in_node
;
1467 end_offset
= ADDRS_PER_PAGE(dn
.node_page
, inode
);
1470 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
1472 if (__is_valid_data_blkaddr(blkaddr
) &&
1473 !f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC_ENHANCE
)) {
1474 err
= -EFSCORRUPTED
;
1478 if (__is_valid_data_blkaddr(blkaddr
)) {
1479 /* use out-place-update for driect IO under LFS mode */
1480 if (test_opt(sbi
, LFS
) && flag
== F2FS_GET_BLOCK_DIO
&&
1481 map
->m_may_create
) {
1482 err
= __allocate_data_block(&dn
, map
->m_seg_type
);
1485 blkaddr
= dn
.data_blkaddr
;
1486 set_inode_flag(inode
, FI_APPEND_WRITE
);
1490 if (unlikely(f2fs_cp_error(sbi
))) {
1494 if (flag
== F2FS_GET_BLOCK_PRE_AIO
) {
1495 if (blkaddr
== NULL_ADDR
) {
1497 last_ofs_in_node
= dn
.ofs_in_node
;
1500 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_DIO
&&
1501 flag
!= F2FS_GET_BLOCK_DIO
);
1502 err
= __allocate_data_block(&dn
,
1505 set_inode_flag(inode
, FI_APPEND_WRITE
);
1509 map
->m_flags
|= F2FS_MAP_NEW
;
1510 blkaddr
= dn
.data_blkaddr
;
1512 if (flag
== F2FS_GET_BLOCK_BMAP
) {
1516 if (flag
== F2FS_GET_BLOCK_PRECACHE
)
1518 if (flag
== F2FS_GET_BLOCK_FIEMAP
&&
1519 blkaddr
== NULL_ADDR
) {
1520 if (map
->m_next_pgofs
)
1521 *map
->m_next_pgofs
= pgofs
+ 1;
1524 if (flag
!= F2FS_GET_BLOCK_FIEMAP
) {
1525 /* for defragment case */
1526 if (map
->m_next_pgofs
)
1527 *map
->m_next_pgofs
= pgofs
+ 1;
1533 if (flag
== F2FS_GET_BLOCK_PRE_AIO
)
1536 if (map
->m_len
== 0) {
1537 /* preallocated unwritten block should be mapped for fiemap. */
1538 if (blkaddr
== NEW_ADDR
)
1539 map
->m_flags
|= F2FS_MAP_UNWRITTEN
;
1540 map
->m_flags
|= F2FS_MAP_MAPPED
;
1542 map
->m_pblk
= blkaddr
;
1544 } else if ((map
->m_pblk
!= NEW_ADDR
&&
1545 blkaddr
== (map
->m_pblk
+ ofs
)) ||
1546 (map
->m_pblk
== NEW_ADDR
&& blkaddr
== NEW_ADDR
) ||
1547 flag
== F2FS_GET_BLOCK_PRE_DIO
) {
1558 /* preallocate blocks in batch for one dnode page */
1559 if (flag
== F2FS_GET_BLOCK_PRE_AIO
&&
1560 (pgofs
== end
|| dn
.ofs_in_node
== end_offset
)) {
1562 dn
.ofs_in_node
= ofs_in_node
;
1563 err
= f2fs_reserve_new_blocks(&dn
, prealloc
);
1567 map
->m_len
+= dn
.ofs_in_node
- ofs_in_node
;
1568 if (prealloc
&& dn
.ofs_in_node
!= last_ofs_in_node
+ 1) {
1572 dn
.ofs_in_node
= end_offset
;
1577 else if (dn
.ofs_in_node
< end_offset
)
1580 if (flag
== F2FS_GET_BLOCK_PRECACHE
) {
1581 if (map
->m_flags
& F2FS_MAP_MAPPED
) {
1582 unsigned int ofs
= start_pgofs
- map
->m_lblk
;
1584 f2fs_update_extent_cache_range(&dn
,
1585 start_pgofs
, map
->m_pblk
+ ofs
,
1590 f2fs_put_dnode(&dn
);
1592 if (map
->m_may_create
) {
1593 __do_map_lock(sbi
, flag
, false);
1594 f2fs_balance_fs(sbi
, dn
.node_changed
);
1600 /* for hardware encryption, but to avoid potential issue in future */
1601 if (flag
== F2FS_GET_BLOCK_DIO
&& map
->m_flags
& F2FS_MAP_MAPPED
)
1602 f2fs_wait_on_block_writeback_range(inode
,
1603 map
->m_pblk
, map
->m_len
);
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
,
1613 if (map
->m_next_extent
)
1614 *map
->m_next_extent
= pgofs
+ 1;
1616 f2fs_put_dnode(&dn
);
1618 if (map
->m_may_create
) {
1619 __do_map_lock(sbi
, flag
, false);
1620 f2fs_balance_fs(sbi
, dn
.node_changed
);
1623 trace_f2fs_map_blocks(inode
, map
, err
);
1627 bool f2fs_overwrite_io(struct inode
*inode
, loff_t pos
, size_t len
)
1629 struct f2fs_map_blocks map
;
1633 if (pos
+ len
> i_size_read(inode
))
1636 map
.m_lblk
= F2FS_BYTES_TO_BLK(pos
);
1637 map
.m_next_pgofs
= NULL
;
1638 map
.m_next_extent
= NULL
;
1639 map
.m_seg_type
= NO_CHECK_TYPE
;
1640 map
.m_may_create
= false;
1641 last_lblk
= F2FS_BLK_ALIGN(pos
+ len
);
1643 while (map
.m_lblk
< last_lblk
) {
1644 map
.m_len
= last_lblk
- map
.m_lblk
;
1645 err
= f2fs_map_blocks(inode
, &map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1646 if (err
|| map
.m_len
== 0)
1648 map
.m_lblk
+= map
.m_len
;
1653 static int __get_data_block(struct inode
*inode
, sector_t iblock
,
1654 struct buffer_head
*bh
, int create
, int flag
,
1655 pgoff_t
*next_pgofs
, int seg_type
, bool may_write
)
1657 struct f2fs_map_blocks map
;
1660 map
.m_lblk
= iblock
;
1661 map
.m_len
= bh
->b_size
>> inode
->i_blkbits
;
1662 map
.m_next_pgofs
= next_pgofs
;
1663 map
.m_next_extent
= NULL
;
1664 map
.m_seg_type
= seg_type
;
1665 map
.m_may_create
= may_write
;
1667 err
= f2fs_map_blocks(inode
, &map
, create
, flag
);
1669 map_bh(bh
, inode
->i_sb
, map
.m_pblk
);
1670 bh
->b_state
= (bh
->b_state
& ~F2FS_MAP_FLAGS
) | map
.m_flags
;
1671 bh
->b_size
= (u64
)map
.m_len
<< inode
->i_blkbits
;
1676 static int get_data_block(struct inode
*inode
, sector_t iblock
,
1677 struct buffer_head
*bh_result
, int create
, int flag
,
1678 pgoff_t
*next_pgofs
)
1680 return __get_data_block(inode
, iblock
, bh_result
, create
,
1682 NO_CHECK_TYPE
, create
);
1685 static int get_data_block_dio_write(struct inode
*inode
, sector_t iblock
,
1686 struct buffer_head
*bh_result
, int create
)
1688 return __get_data_block(inode
, iblock
, bh_result
, create
,
1689 F2FS_GET_BLOCK_DIO
, NULL
,
1690 f2fs_rw_hint_to_seg_type(inode
->i_write_hint
),
1691 IS_SWAPFILE(inode
) ? false : true);
1694 static int get_data_block_dio(struct inode
*inode
, sector_t iblock
,
1695 struct buffer_head
*bh_result
, int create
)
1697 return __get_data_block(inode
, iblock
, bh_result
, create
,
1698 F2FS_GET_BLOCK_DIO
, NULL
,
1699 f2fs_rw_hint_to_seg_type(inode
->i_write_hint
),
1703 static int get_data_block_bmap(struct inode
*inode
, sector_t iblock
,
1704 struct buffer_head
*bh_result
, int create
)
1706 /* Block number less than F2FS MAX BLOCKS */
1707 if (unlikely(iblock
>= F2FS_I_SB(inode
)->max_file_blocks
))
1710 return __get_data_block(inode
, iblock
, bh_result
, create
,
1711 F2FS_GET_BLOCK_BMAP
, NULL
,
1712 NO_CHECK_TYPE
, create
);
1715 static inline sector_t
logical_to_blk(struct inode
*inode
, loff_t offset
)
1717 return (offset
>> inode
->i_blkbits
);
1720 static inline loff_t
blk_to_logical(struct inode
*inode
, sector_t blk
)
1722 return (blk
<< inode
->i_blkbits
);
1725 static int f2fs_xattr_fiemap(struct inode
*inode
,
1726 struct fiemap_extent_info
*fieinfo
)
1728 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
1730 struct node_info ni
;
1731 __u64 phys
= 0, len
;
1733 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
1736 if (f2fs_has_inline_xattr(inode
)) {
1739 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
),
1740 inode
->i_ino
, false);
1744 err
= f2fs_get_node_info(sbi
, inode
->i_ino
, &ni
);
1746 f2fs_put_page(page
, 1);
1750 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1751 offset
= offsetof(struct f2fs_inode
, i_addr
) +
1752 sizeof(__le32
) * (DEF_ADDRS_PER_INODE
-
1753 get_inline_xattr_addrs(inode
));
1756 len
= inline_xattr_size(inode
);
1758 f2fs_put_page(page
, 1);
1760 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
;
1763 flags
|= FIEMAP_EXTENT_LAST
;
1765 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1766 if (err
|| err
== 1)
1771 page
= f2fs_grab_cache_page(NODE_MAPPING(sbi
), xnid
, false);
1775 err
= f2fs_get_node_info(sbi
, xnid
, &ni
);
1777 f2fs_put_page(page
, 1);
1781 phys
= (__u64
)blk_to_logical(inode
, ni
.blk_addr
);
1782 len
= inode
->i_sb
->s_blocksize
;
1784 f2fs_put_page(page
, 1);
1786 flags
= FIEMAP_EXTENT_LAST
;
1790 err
= fiemap_fill_next_extent(fieinfo
, 0, phys
, len
, flags
);
1792 return (err
< 0 ? err
: 0);
1795 int f2fs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1798 struct buffer_head map_bh
;
1799 sector_t start_blk
, last_blk
;
1801 u64 logical
= 0, phys
= 0, size
= 0;
1805 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
1806 ret
= f2fs_precache_extents(inode
);
1811 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
| FIEMAP_FLAG_XATTR
);
1817 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
1818 ret
= f2fs_xattr_fiemap(inode
, fieinfo
);
1822 if (f2fs_has_inline_data(inode
) || f2fs_has_inline_dentry(inode
)) {
1823 ret
= f2fs_inline_data_fiemap(inode
, fieinfo
, start
, len
);
1828 if (logical_to_blk(inode
, len
) == 0)
1829 len
= blk_to_logical(inode
, 1);
1831 start_blk
= logical_to_blk(inode
, start
);
1832 last_blk
= logical_to_blk(inode
, start
+ len
- 1);
1835 memset(&map_bh
, 0, sizeof(struct buffer_head
));
1836 map_bh
.b_size
= len
;
1838 ret
= get_data_block(inode
, start_blk
, &map_bh
, 0,
1839 F2FS_GET_BLOCK_FIEMAP
, &next_pgofs
);
1844 if (!buffer_mapped(&map_bh
)) {
1845 start_blk
= next_pgofs
;
1847 if (blk_to_logical(inode
, start_blk
) < blk_to_logical(inode
,
1848 F2FS_I_SB(inode
)->max_file_blocks
))
1851 flags
|= FIEMAP_EXTENT_LAST
;
1855 if (IS_ENCRYPTED(inode
))
1856 flags
|= FIEMAP_EXTENT_DATA_ENCRYPTED
;
1858 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
1862 if (start_blk
> last_blk
|| ret
)
1865 logical
= blk_to_logical(inode
, start_blk
);
1866 phys
= blk_to_logical(inode
, map_bh
.b_blocknr
);
1867 size
= map_bh
.b_size
;
1869 if (buffer_unwritten(&map_bh
))
1870 flags
= FIEMAP_EXTENT_UNWRITTEN
;
1872 start_blk
+= logical_to_blk(inode
, size
);
1876 if (fatal_signal_pending(current
))
1884 inode_unlock(inode
);
1888 static inline loff_t
f2fs_readpage_limit(struct inode
*inode
)
1890 if (IS_ENABLED(CONFIG_FS_VERITY
) &&
1891 (IS_VERITY(inode
) || f2fs_verity_in_progress(inode
)))
1892 return inode
->i_sb
->s_maxbytes
;
1894 return i_size_read(inode
);
1897 static int f2fs_read_single_page(struct inode
*inode
, struct page
*page
,
1899 struct f2fs_map_blocks
*map
,
1900 struct bio
**bio_ret
,
1901 sector_t
*last_block_in_bio
,
1904 struct bio
*bio
= *bio_ret
;
1905 const unsigned blkbits
= inode
->i_blkbits
;
1906 const unsigned blocksize
= 1 << blkbits
;
1907 sector_t block_in_file
;
1908 sector_t last_block
;
1909 sector_t last_block_in_file
;
1913 block_in_file
= (sector_t
)page_index(page
);
1914 last_block
= block_in_file
+ nr_pages
;
1915 last_block_in_file
= (f2fs_readpage_limit(inode
) + blocksize
- 1) >>
1917 if (last_block
> last_block_in_file
)
1918 last_block
= last_block_in_file
;
1920 /* just zeroing out page which is beyond EOF */
1921 if (block_in_file
>= last_block
)
1924 * Map blocks using the previous result first.
1926 if ((map
->m_flags
& F2FS_MAP_MAPPED
) &&
1927 block_in_file
> map
->m_lblk
&&
1928 block_in_file
< (map
->m_lblk
+ map
->m_len
))
1932 * Then do more f2fs_map_blocks() calls until we are
1933 * done with this page.
1935 map
->m_lblk
= block_in_file
;
1936 map
->m_len
= last_block
- block_in_file
;
1938 ret
= f2fs_map_blocks(inode
, map
, 0, F2FS_GET_BLOCK_DEFAULT
);
1942 if ((map
->m_flags
& F2FS_MAP_MAPPED
)) {
1943 block_nr
= map
->m_pblk
+ block_in_file
- map
->m_lblk
;
1944 SetPageMappedToDisk(page
);
1946 if (!PageUptodate(page
) && (!PageSwapCache(page
) &&
1947 !cleancache_get_page(page
))) {
1948 SetPageUptodate(page
);
1952 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode
), block_nr
,
1953 DATA_GENERIC_ENHANCE_READ
)) {
1954 ret
= -EFSCORRUPTED
;
1959 zero_user_segment(page
, 0, PAGE_SIZE
);
1960 if (f2fs_need_verity(inode
, page
->index
) &&
1961 !fsverity_verify_page(page
)) {
1965 if (!PageUptodate(page
))
1966 SetPageUptodate(page
);
1972 * This page will go to BIO. Do we need to send this
1975 if (bio
&& !page_is_mergeable(F2FS_I_SB(inode
), bio
,
1976 *last_block_in_bio
, block_nr
)) {
1978 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
1982 bio
= f2fs_grab_read_bio(inode
, block_nr
, nr_pages
,
1983 is_readahead
? REQ_RAHEAD
: 0, page
->index
);
1992 * If the page is under writeback, we need to wait for
1993 * its completion to see the correct decrypted data.
1995 f2fs_wait_on_block_writeback(inode
, block_nr
);
1997 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
1998 goto submit_and_realloc
;
2000 inc_page_count(F2FS_I_SB(inode
), F2FS_RD_DATA
);
2001 ClearPageError(page
);
2002 *last_block_in_bio
= block_nr
;
2006 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
2015 #ifdef CONFIG_F2FS_FS_COMPRESSION
2016 int f2fs_read_multi_pages(struct compress_ctx
*cc
, struct bio
**bio_ret
,
2017 unsigned nr_pages
, sector_t
*last_block_in_bio
,
2020 struct dnode_of_data dn
;
2021 struct inode
*inode
= cc
->inode
;
2022 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2023 struct bio
*bio
= *bio_ret
;
2024 unsigned int start_idx
= cc
->cluster_idx
<< cc
->log_cluster_size
;
2025 sector_t last_block_in_file
;
2026 const unsigned blkbits
= inode
->i_blkbits
;
2027 const unsigned blocksize
= 1 << blkbits
;
2028 struct decompress_io_ctx
*dic
= NULL
;
2032 f2fs_bug_on(sbi
, f2fs_cluster_is_empty(cc
));
2034 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >> blkbits
;
2036 /* get rid of pages beyond EOF */
2037 for (i
= 0; i
< cc
->cluster_size
; i
++) {
2038 struct page
*page
= cc
->rpages
[i
];
2042 if ((sector_t
)page
->index
>= last_block_in_file
) {
2043 zero_user_segment(page
, 0, PAGE_SIZE
);
2044 if (!PageUptodate(page
))
2045 SetPageUptodate(page
);
2046 } else if (!PageUptodate(page
)) {
2050 cc
->rpages
[i
] = NULL
;
2054 /* we are done since all pages are beyond EOF */
2055 if (f2fs_cluster_is_empty(cc
))
2058 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
2059 ret
= f2fs_get_dnode_of_data(&dn
, start_idx
, LOOKUP_NODE
);
2063 /* cluster was overwritten as normal cluster */
2064 if (dn
.data_blkaddr
!= COMPRESS_ADDR
)
2067 for (i
= 1; i
< cc
->cluster_size
; i
++) {
2070 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
,
2071 dn
.ofs_in_node
+ i
);
2073 if (!__is_valid_data_blkaddr(blkaddr
))
2076 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, DATA_GENERIC
)) {
2083 /* nothing to decompress */
2084 if (cc
->nr_cpages
== 0) {
2089 dic
= f2fs_alloc_dic(cc
);
2095 for (i
= 0; i
< dic
->nr_cpages
; i
++) {
2096 struct page
*page
= dic
->cpages
[i
];
2099 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
,
2100 dn
.ofs_in_node
+ i
+ 1);
2102 if (bio
&& !page_is_mergeable(sbi
, bio
,
2103 *last_block_in_bio
, blkaddr
)) {
2105 __submit_bio(sbi
, bio
, DATA
);
2110 bio
= f2fs_grab_read_bio(inode
, blkaddr
, nr_pages
,
2111 is_readahead
? REQ_RAHEAD
: 0,
2117 if (refcount_sub_and_test(dic
->nr_cpages
- i
,
2119 f2fs_decompress_end_io(dic
->rpages
,
2120 cc
->cluster_size
, true,
2123 f2fs_put_dnode(&dn
);
2129 f2fs_wait_on_block_writeback(inode
, blkaddr
);
2131 if (bio_add_page(bio
, page
, blocksize
, 0) < blocksize
)
2132 goto submit_and_realloc
;
2134 inc_page_count(sbi
, F2FS_RD_DATA
);
2135 ClearPageError(page
);
2136 *last_block_in_bio
= blkaddr
;
2139 f2fs_put_dnode(&dn
);
2145 f2fs_put_dnode(&dn
);
2147 f2fs_decompress_end_io(cc
->rpages
, cc
->cluster_size
, true, false);
2154 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2155 * Major change was from block_size == page_size in f2fs by default.
2157 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2158 * this function ever deviates from doing just read-ahead, it should either
2159 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2162 int f2fs_mpage_readpages(struct address_space
*mapping
,
2163 struct list_head
*pages
, struct page
*page
,
2164 unsigned nr_pages
, bool is_readahead
)
2166 struct bio
*bio
= NULL
;
2167 sector_t last_block_in_bio
= 0;
2168 struct inode
*inode
= mapping
->host
;
2169 struct f2fs_map_blocks map
;
2170 #ifdef CONFIG_F2FS_FS_COMPRESSION
2171 struct compress_ctx cc
= {
2173 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
2174 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
2175 .cluster_idx
= NULL_CLUSTER
,
2182 unsigned max_nr_pages
= nr_pages
;
2189 map
.m_next_pgofs
= NULL
;
2190 map
.m_next_extent
= NULL
;
2191 map
.m_seg_type
= NO_CHECK_TYPE
;
2192 map
.m_may_create
= false;
2194 for (; nr_pages
; nr_pages
--) {
2196 page
= list_last_entry(pages
, struct page
, lru
);
2198 prefetchw(&page
->flags
);
2199 list_del(&page
->lru
);
2200 if (add_to_page_cache_lru(page
, mapping
,
2202 readahead_gfp_mask(mapping
)))
2206 #ifdef CONFIG_F2FS_FS_COMPRESSION
2207 if (f2fs_compressed_file(inode
)) {
2208 /* there are remained comressed pages, submit them */
2209 if (!f2fs_cluster_can_merge_page(&cc
, page
->index
)) {
2210 ret
= f2fs_read_multi_pages(&cc
, &bio
,
2214 f2fs_destroy_compress_ctx(&cc
);
2216 goto set_error_page
;
2218 ret
= f2fs_is_compressed_cluster(inode
, page
->index
);
2220 goto set_error_page
;
2222 goto read_single_page
;
2224 ret
= f2fs_init_compress_ctx(&cc
);
2226 goto set_error_page
;
2228 f2fs_compress_ctx_add_page(&cc
, page
);
2235 ret
= f2fs_read_single_page(inode
, page
, max_nr_pages
, &map
,
2236 &bio
, &last_block_in_bio
, is_readahead
);
2238 #ifdef CONFIG_F2FS_FS_COMPRESSION
2242 zero_user_segment(page
, 0, PAGE_SIZE
);
2249 #ifdef CONFIG_F2FS_FS_COMPRESSION
2250 if (f2fs_compressed_file(inode
)) {
2252 if (nr_pages
== 1 && !f2fs_cluster_is_empty(&cc
)) {
2253 ret
= f2fs_read_multi_pages(&cc
, &bio
,
2257 f2fs_destroy_compress_ctx(&cc
);
2262 BUG_ON(pages
&& !list_empty(pages
));
2264 __submit_bio(F2FS_I_SB(inode
), bio
, DATA
);
2265 return pages
? 0 : ret
;
2268 static int f2fs_read_data_page(struct file
*file
, struct page
*page
)
2270 struct inode
*inode
= page_file_mapping(page
)->host
;
2273 trace_f2fs_readpage(page
, DATA
);
2275 if (!f2fs_is_compress_backend_ready(inode
)) {
2280 /* If the file has inline data, try to read it directly */
2281 if (f2fs_has_inline_data(inode
))
2282 ret
= f2fs_read_inline_data(inode
, page
);
2284 ret
= f2fs_mpage_readpages(page_file_mapping(page
),
2285 NULL
, page
, 1, false);
2289 static int f2fs_read_data_pages(struct file
*file
,
2290 struct address_space
*mapping
,
2291 struct list_head
*pages
, unsigned nr_pages
)
2293 struct inode
*inode
= mapping
->host
;
2294 struct page
*page
= list_last_entry(pages
, struct page
, lru
);
2296 trace_f2fs_readpages(inode
, page
, nr_pages
);
2298 if (!f2fs_is_compress_backend_ready(inode
))
2301 /* If the file has inline data, skip readpages */
2302 if (f2fs_has_inline_data(inode
))
2305 return f2fs_mpage_readpages(mapping
, pages
, NULL
, nr_pages
, true);
2308 int f2fs_encrypt_one_page(struct f2fs_io_info
*fio
)
2310 struct inode
*inode
= fio
->page
->mapping
->host
;
2311 struct page
*mpage
, *page
;
2312 gfp_t gfp_flags
= GFP_NOFS
;
2314 if (!f2fs_encrypted_file(inode
))
2317 page
= fio
->compressed_page
? fio
->compressed_page
: fio
->page
;
2319 /* wait for GCed page writeback via META_MAPPING */
2320 f2fs_wait_on_block_writeback(inode
, fio
->old_blkaddr
);
2323 fio
->encrypted_page
= fscrypt_encrypt_pagecache_blocks(page
,
2324 PAGE_SIZE
, 0, gfp_flags
);
2325 if (IS_ERR(fio
->encrypted_page
)) {
2326 /* flush pending IOs and wait for a while in the ENOMEM case */
2327 if (PTR_ERR(fio
->encrypted_page
) == -ENOMEM
) {
2328 f2fs_flush_merged_writes(fio
->sbi
);
2329 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
2330 gfp_flags
|= __GFP_NOFAIL
;
2333 return PTR_ERR(fio
->encrypted_page
);
2336 mpage
= find_lock_page(META_MAPPING(fio
->sbi
), fio
->old_blkaddr
);
2338 if (PageUptodate(mpage
))
2339 memcpy(page_address(mpage
),
2340 page_address(fio
->encrypted_page
), PAGE_SIZE
);
2341 f2fs_put_page(mpage
, 1);
2346 static inline bool check_inplace_update_policy(struct inode
*inode
,
2347 struct f2fs_io_info
*fio
)
2349 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2350 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
2352 if (policy
& (0x1 << F2FS_IPU_FORCE
))
2354 if (policy
& (0x1 << F2FS_IPU_SSR
) && f2fs_need_SSR(sbi
))
2356 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
2357 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
2359 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && f2fs_need_SSR(sbi
) &&
2360 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
2364 * IPU for rewrite async pages
2366 if (policy
& (0x1 << F2FS_IPU_ASYNC
) &&
2367 fio
&& fio
->op
== REQ_OP_WRITE
&&
2368 !(fio
->op_flags
& REQ_SYNC
) &&
2369 !IS_ENCRYPTED(inode
))
2372 /* this is only set during fdatasync */
2373 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
2374 is_inode_flag_set(inode
, FI_NEED_IPU
))
2377 if (unlikely(fio
&& is_sbi_flag_set(sbi
, SBI_CP_DISABLED
) &&
2378 !f2fs_is_checkpointed_data(sbi
, fio
->old_blkaddr
)))
2384 bool f2fs_should_update_inplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
2386 if (f2fs_is_pinned_file(inode
))
2389 /* if this is cold file, we should overwrite to avoid fragmentation */
2390 if (file_is_cold(inode
))
2393 return check_inplace_update_policy(inode
, fio
);
2396 bool f2fs_should_update_outplace(struct inode
*inode
, struct f2fs_io_info
*fio
)
2398 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2400 if (test_opt(sbi
, LFS
))
2402 if (S_ISDIR(inode
->i_mode
))
2404 if (IS_NOQUOTA(inode
))
2406 if (f2fs_is_atomic_file(inode
))
2409 if (is_cold_data(fio
->page
))
2411 if (IS_ATOMIC_WRITTEN_PAGE(fio
->page
))
2413 if (unlikely(is_sbi_flag_set(sbi
, SBI_CP_DISABLED
) &&
2414 f2fs_is_checkpointed_data(sbi
, fio
->old_blkaddr
)))
2420 static inline bool need_inplace_update(struct f2fs_io_info
*fio
)
2422 struct inode
*inode
= fio
->page
->mapping
->host
;
2424 if (f2fs_should_update_outplace(inode
, fio
))
2427 return f2fs_should_update_inplace(inode
, fio
);
2430 int f2fs_do_write_data_page(struct f2fs_io_info
*fio
)
2432 struct page
*page
= fio
->page
;
2433 struct inode
*inode
= page
->mapping
->host
;
2434 struct dnode_of_data dn
;
2435 struct extent_info ei
= {0,0,0};
2436 struct node_info ni
;
2437 bool ipu_force
= false;
2440 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
2441 if (need_inplace_update(fio
) &&
2442 f2fs_lookup_extent_cache(inode
, page
->index
, &ei
)) {
2443 fio
->old_blkaddr
= ei
.blk
+ page
->index
- ei
.fofs
;
2445 if (!f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
2446 DATA_GENERIC_ENHANCE
))
2447 return -EFSCORRUPTED
;
2450 fio
->need_lock
= LOCK_DONE
;
2454 /* Deadlock due to between page->lock and f2fs_lock_op */
2455 if (fio
->need_lock
== LOCK_REQ
&& !f2fs_trylock_op(fio
->sbi
))
2458 err
= f2fs_get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
2462 fio
->old_blkaddr
= dn
.data_blkaddr
;
2464 /* This page is already truncated */
2465 if (fio
->old_blkaddr
== NULL_ADDR
) {
2466 ClearPageUptodate(page
);
2467 clear_cold_data(page
);
2471 if (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
2472 !f2fs_is_valid_blkaddr(fio
->sbi
, fio
->old_blkaddr
,
2473 DATA_GENERIC_ENHANCE
)) {
2474 err
= -EFSCORRUPTED
;
2478 * If current allocation needs SSR,
2479 * it had better in-place writes for updated data.
2482 (__is_valid_data_blkaddr(fio
->old_blkaddr
) &&
2483 need_inplace_update(fio
))) {
2484 err
= f2fs_encrypt_one_page(fio
);
2488 set_page_writeback(page
);
2489 ClearPageError(page
);
2490 f2fs_put_dnode(&dn
);
2491 if (fio
->need_lock
== LOCK_REQ
)
2492 f2fs_unlock_op(fio
->sbi
);
2493 err
= f2fs_inplace_write_data(fio
);
2495 if (f2fs_encrypted_file(inode
))
2496 fscrypt_finalize_bounce_page(&fio
->encrypted_page
);
2497 if (PageWriteback(page
))
2498 end_page_writeback(page
);
2500 set_inode_flag(inode
, FI_UPDATE_WRITE
);
2502 trace_f2fs_do_write_data_page(fio
->page
, IPU
);
2506 if (fio
->need_lock
== LOCK_RETRY
) {
2507 if (!f2fs_trylock_op(fio
->sbi
)) {
2511 fio
->need_lock
= LOCK_REQ
;
2514 err
= f2fs_get_node_info(fio
->sbi
, dn
.nid
, &ni
);
2518 fio
->version
= ni
.version
;
2520 err
= f2fs_encrypt_one_page(fio
);
2524 set_page_writeback(page
);
2525 ClearPageError(page
);
2527 if (fio
->compr_blocks
&& fio
->old_blkaddr
== COMPRESS_ADDR
)
2528 f2fs_i_compr_blocks_update(inode
, fio
->compr_blocks
- 1, false);
2530 /* LFS mode write path */
2531 f2fs_outplace_write_data(&dn
, fio
);
2532 trace_f2fs_do_write_data_page(page
, OPU
);
2533 set_inode_flag(inode
, FI_APPEND_WRITE
);
2534 if (page
->index
== 0)
2535 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
2537 f2fs_put_dnode(&dn
);
2539 if (fio
->need_lock
== LOCK_REQ
)
2540 f2fs_unlock_op(fio
->sbi
);
2544 int f2fs_write_single_data_page(struct page
*page
, int *submitted
,
2546 sector_t
*last_block
,
2547 struct writeback_control
*wbc
,
2548 enum iostat_type io_type
,
2551 struct inode
*inode
= page
->mapping
->host
;
2552 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2553 loff_t i_size
= i_size_read(inode
);
2554 const pgoff_t end_index
= ((unsigned long long)i_size
)
2556 loff_t psize
= (loff_t
)(page
->index
+ 1) << PAGE_SHIFT
;
2557 unsigned offset
= 0;
2558 bool need_balance_fs
= false;
2560 struct f2fs_io_info fio
= {
2562 .ino
= inode
->i_ino
,
2565 .op_flags
= wbc_to_write_flags(wbc
),
2566 .old_blkaddr
= NULL_ADDR
,
2568 .encrypted_page
= NULL
,
2570 .compr_blocks
= compr_blocks
,
2571 .need_lock
= LOCK_RETRY
,
2575 .last_block
= last_block
,
2578 trace_f2fs_writepage(page
, DATA
);
2580 /* we should bypass data pages to proceed the kworkder jobs */
2581 if (unlikely(f2fs_cp_error(sbi
))) {
2582 mapping_set_error(page
->mapping
, -EIO
);
2584 * don't drop any dirty dentry pages for keeping lastest
2585 * directory structure.
2587 if (S_ISDIR(inode
->i_mode
))
2592 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
2595 if (page
->index
< end_index
||
2596 f2fs_verity_in_progress(inode
) ||
2601 * If the offset is out-of-range of file size,
2602 * this page does not have to be written to disk.
2604 offset
= i_size
& (PAGE_SIZE
- 1);
2605 if ((page
->index
>= end_index
+ 1) || !offset
)
2608 zero_user_segment(page
, offset
, PAGE_SIZE
);
2610 if (f2fs_is_drop_cache(inode
))
2612 /* we should not write 0'th page having journal header */
2613 if (f2fs_is_volatile_file(inode
) && (!page
->index
||
2614 (!wbc
->for_reclaim
&&
2615 f2fs_available_free_memory(sbi
, BASE_CHECK
))))
2618 /* Dentry blocks are controlled by checkpoint */
2619 if (S_ISDIR(inode
->i_mode
)) {
2620 fio
.need_lock
= LOCK_DONE
;
2621 err
= f2fs_do_write_data_page(&fio
);
2625 if (!wbc
->for_reclaim
)
2626 need_balance_fs
= true;
2627 else if (has_not_enough_free_secs(sbi
, 0, 0))
2630 set_inode_flag(inode
, FI_HOT_DATA
);
2633 if (f2fs_has_inline_data(inode
)) {
2634 err
= f2fs_write_inline_data(inode
, page
);
2639 if (err
== -EAGAIN
) {
2640 err
= f2fs_do_write_data_page(&fio
);
2641 if (err
== -EAGAIN
) {
2642 fio
.need_lock
= LOCK_REQ
;
2643 err
= f2fs_do_write_data_page(&fio
);
2648 file_set_keep_isize(inode
);
2650 down_write(&F2FS_I(inode
)->i_sem
);
2651 if (F2FS_I(inode
)->last_disk_size
< psize
)
2652 F2FS_I(inode
)->last_disk_size
= psize
;
2653 up_write(&F2FS_I(inode
)->i_sem
);
2657 if (err
&& err
!= -ENOENT
)
2661 inode_dec_dirty_pages(inode
);
2663 ClearPageUptodate(page
);
2664 clear_cold_data(page
);
2667 if (wbc
->for_reclaim
) {
2668 f2fs_submit_merged_write_cond(sbi
, NULL
, page
, 0, DATA
);
2669 clear_inode_flag(inode
, FI_HOT_DATA
);
2670 f2fs_remove_dirty_inode(inode
);
2674 if (!S_ISDIR(inode
->i_mode
) && !IS_NOQUOTA(inode
) &&
2675 !F2FS_I(inode
)->cp_task
)
2676 f2fs_balance_fs(sbi
, need_balance_fs
);
2678 if (unlikely(f2fs_cp_error(sbi
))) {
2679 f2fs_submit_merged_write(sbi
, DATA
);
2680 f2fs_submit_merged_ipu_write(sbi
, bio
, NULL
);
2685 *submitted
= fio
.submitted
? 1 : 0;
2690 redirty_page_for_writepage(wbc
, page
);
2692 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2693 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2694 * file_write_and_wait_range() will see EIO error, which is critical
2695 * to return value of fsync() followed by atomic_write failure to user.
2697 if (!err
|| wbc
->for_reclaim
)
2698 return AOP_WRITEPAGE_ACTIVATE
;
2703 static int f2fs_write_data_page(struct page
*page
,
2704 struct writeback_control
*wbc
)
2706 #ifdef CONFIG_F2FS_FS_COMPRESSION
2707 struct inode
*inode
= page
->mapping
->host
;
2709 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode
))))
2712 if (f2fs_compressed_file(inode
)) {
2713 if (f2fs_is_compressed_cluster(inode
, page
->index
)) {
2714 redirty_page_for_writepage(wbc
, page
);
2715 return AOP_WRITEPAGE_ACTIVATE
;
2721 return f2fs_write_single_data_page(page
, NULL
, NULL
, NULL
,
2722 wbc
, FS_DATA_IO
, 0);
2726 * This function was copied from write_cche_pages from mm/page-writeback.c.
2727 * The major change is making write step of cold data page separately from
2728 * warm/hot data page.
2730 static int f2fs_write_cache_pages(struct address_space
*mapping
,
2731 struct writeback_control
*wbc
,
2732 enum iostat_type io_type
)
2735 int done
= 0, retry
= 0;
2736 struct pagevec pvec
;
2737 struct f2fs_sb_info
*sbi
= F2FS_M_SB(mapping
);
2738 struct bio
*bio
= NULL
;
2739 sector_t last_block
;
2740 #ifdef CONFIG_F2FS_FS_COMPRESSION
2741 struct inode
*inode
= mapping
->host
;
2742 struct compress_ctx cc
= {
2744 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
2745 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
2746 .cluster_idx
= NULL_CLUSTER
,
2752 .rlen
= PAGE_SIZE
* F2FS_I(inode
)->i_cluster_size
,
2757 pgoff_t
uninitialized_var(writeback_index
);
2759 pgoff_t end
; /* Inclusive */
2762 int range_whole
= 0;
2768 pagevec_init(&pvec
);
2770 if (get_dirty_pages(mapping
->host
) <=
2771 SM_I(F2FS_M_SB(mapping
))->min_hot_blocks
)
2772 set_inode_flag(mapping
->host
, FI_HOT_DATA
);
2774 clear_inode_flag(mapping
->host
, FI_HOT_DATA
);
2776 if (wbc
->range_cyclic
) {
2777 writeback_index
= mapping
->writeback_index
; /* prev offset */
2778 index
= writeback_index
;
2785 index
= wbc
->range_start
>> PAGE_SHIFT
;
2786 end
= wbc
->range_end
>> PAGE_SHIFT
;
2787 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2789 cycled
= 1; /* ignore range_cyclic tests */
2791 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2792 tag
= PAGECACHE_TAG_TOWRITE
;
2794 tag
= PAGECACHE_TAG_DIRTY
;
2797 if (wbc
->sync_mode
== WB_SYNC_ALL
|| wbc
->tagged_writepages
)
2798 tag_pages_for_writeback(mapping
, index
, end
);
2800 while (!done
&& !retry
&& (index
<= end
)) {
2801 nr_pages
= pagevec_lookup_range_tag(&pvec
, mapping
, &index
, end
,
2806 for (i
= 0; i
< nr_pages
; i
++) {
2807 struct page
*page
= pvec
.pages
[i
];
2811 #ifdef CONFIG_F2FS_FS_COMPRESSION
2812 if (f2fs_compressed_file(inode
)) {
2813 ret
= f2fs_init_compress_ctx(&cc
);
2819 if (!f2fs_cluster_can_merge_page(&cc
,
2821 ret
= f2fs_write_multi_pages(&cc
,
2822 &submitted
, wbc
, io_type
);
2828 if (unlikely(f2fs_cp_error(sbi
)))
2831 if (f2fs_cluster_is_empty(&cc
)) {
2832 void *fsdata
= NULL
;
2836 ret2
= f2fs_prepare_compress_overwrite(
2838 page
->index
, &fsdata
);
2844 !f2fs_compress_write_end(inode
,
2845 fsdata
, page
->index
,
2855 /* give a priority to WB_SYNC threads */
2856 if (atomic_read(&sbi
->wb_sync_req
[DATA
]) &&
2857 wbc
->sync_mode
== WB_SYNC_NONE
) {
2861 #ifdef CONFIG_F2FS_FS_COMPRESSION
2864 done_index
= page
->index
;
2868 if (unlikely(page
->mapping
!= mapping
)) {
2874 if (!PageDirty(page
)) {
2875 /* someone wrote it for us */
2876 goto continue_unlock
;
2879 if (PageWriteback(page
)) {
2880 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
2881 f2fs_wait_on_page_writeback(page
,
2884 goto continue_unlock
;
2887 if (!clear_page_dirty_for_io(page
))
2888 goto continue_unlock
;
2890 #ifdef CONFIG_F2FS_FS_COMPRESSION
2891 if (f2fs_compressed_file(inode
)) {
2893 f2fs_compress_ctx_add_page(&cc
, page
);
2897 ret
= f2fs_write_single_data_page(page
, &submitted
,
2898 &bio
, &last_block
, wbc
, io_type
, 0);
2899 if (ret
== AOP_WRITEPAGE_ACTIVATE
)
2901 #ifdef CONFIG_F2FS_FS_COMPRESSION
2904 nwritten
+= submitted
;
2905 wbc
->nr_to_write
-= submitted
;
2907 if (unlikely(ret
)) {
2909 * keep nr_to_write, since vfs uses this to
2910 * get # of written pages.
2912 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
2915 } else if (ret
== -EAGAIN
) {
2917 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
2919 congestion_wait(BLK_RW_ASYNC
,
2925 done_index
= page
->index
+ 1;
2930 if (wbc
->nr_to_write
<= 0 &&
2931 wbc
->sync_mode
== WB_SYNC_NONE
) {
2939 pagevec_release(&pvec
);
2942 #ifdef CONFIG_F2FS_FS_COMPRESSION
2943 /* flush remained pages in compress cluster */
2944 if (f2fs_compressed_file(inode
) && !f2fs_cluster_is_empty(&cc
)) {
2945 ret
= f2fs_write_multi_pages(&cc
, &submitted
, wbc
, io_type
);
2946 nwritten
+= submitted
;
2947 wbc
->nr_to_write
-= submitted
;
2954 if ((!cycled
&& !done
) || retry
) {
2957 end
= writeback_index
- 1;
2960 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
2961 mapping
->writeback_index
= done_index
;
2964 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping
), mapping
->host
,
2966 /* submit cached bio of IPU write */
2968 f2fs_submit_merged_ipu_write(sbi
, &bio
, NULL
);
2973 static inline bool __should_serialize_io(struct inode
*inode
,
2974 struct writeback_control
*wbc
)
2976 if (!S_ISREG(inode
->i_mode
))
2978 if (f2fs_compressed_file(inode
))
2980 if (IS_NOQUOTA(inode
))
2982 /* to avoid deadlock in path of data flush */
2983 if (F2FS_I(inode
)->cp_task
)
2985 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
2987 if (get_dirty_pages(inode
) >= SM_I(F2FS_I_SB(inode
))->min_seq_blocks
)
2992 static int __f2fs_write_data_pages(struct address_space
*mapping
,
2993 struct writeback_control
*wbc
,
2994 enum iostat_type io_type
)
2996 struct inode
*inode
= mapping
->host
;
2997 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
2998 struct blk_plug plug
;
3000 bool locked
= false;
3002 /* deal with chardevs and other special file */
3003 if (!mapping
->a_ops
->writepage
)
3006 /* skip writing if there is no dirty page in this inode */
3007 if (!get_dirty_pages(inode
) && wbc
->sync_mode
== WB_SYNC_NONE
)
3010 /* during POR, we don't need to trigger writepage at all. */
3011 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
3014 if ((S_ISDIR(inode
->i_mode
) || IS_NOQUOTA(inode
)) &&
3015 wbc
->sync_mode
== WB_SYNC_NONE
&&
3016 get_dirty_pages(inode
) < nr_pages_to_skip(sbi
, DATA
) &&
3017 f2fs_available_free_memory(sbi
, DIRTY_DENTS
))
3020 /* skip writing during file defragment */
3021 if (is_inode_flag_set(inode
, FI_DO_DEFRAG
))
3024 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
3026 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3027 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3028 atomic_inc(&sbi
->wb_sync_req
[DATA
]);
3029 else if (atomic_read(&sbi
->wb_sync_req
[DATA
]))
3032 if (__should_serialize_io(inode
, wbc
)) {
3033 mutex_lock(&sbi
->writepages
);
3037 blk_start_plug(&plug
);
3038 ret
= f2fs_write_cache_pages(mapping
, wbc
, io_type
);
3039 blk_finish_plug(&plug
);
3042 mutex_unlock(&sbi
->writepages
);
3044 if (wbc
->sync_mode
== WB_SYNC_ALL
)
3045 atomic_dec(&sbi
->wb_sync_req
[DATA
]);
3047 * if some pages were truncated, we cannot guarantee its mapping->host
3048 * to detect pending bios.
3051 f2fs_remove_dirty_inode(inode
);
3055 wbc
->pages_skipped
+= get_dirty_pages(inode
);
3056 trace_f2fs_writepages(mapping
->host
, wbc
, DATA
);
3060 static int f2fs_write_data_pages(struct address_space
*mapping
,
3061 struct writeback_control
*wbc
)
3063 struct inode
*inode
= mapping
->host
;
3065 return __f2fs_write_data_pages(mapping
, wbc
,
3066 F2FS_I(inode
)->cp_task
== current
?
3067 FS_CP_DATA_IO
: FS_DATA_IO
);
3070 static void f2fs_write_failed(struct address_space
*mapping
, loff_t to
)
3072 struct inode
*inode
= mapping
->host
;
3073 loff_t i_size
= i_size_read(inode
);
3075 if (IS_NOQUOTA(inode
))
3078 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3079 if (to
> i_size
&& !f2fs_verity_in_progress(inode
)) {
3080 down_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
3081 down_write(&F2FS_I(inode
)->i_mmap_sem
);
3083 truncate_pagecache(inode
, i_size
);
3084 f2fs_truncate_blocks(inode
, i_size
, true);
3086 up_write(&F2FS_I(inode
)->i_mmap_sem
);
3087 up_write(&F2FS_I(inode
)->i_gc_rwsem
[WRITE
]);
3091 static int prepare_write_begin(struct f2fs_sb_info
*sbi
,
3092 struct page
*page
, loff_t pos
, unsigned len
,
3093 block_t
*blk_addr
, bool *node_changed
)
3095 struct inode
*inode
= page
->mapping
->host
;
3096 pgoff_t index
= page
->index
;
3097 struct dnode_of_data dn
;
3099 bool locked
= false;
3100 struct extent_info ei
= {0,0,0};
3105 * we already allocated all the blocks, so we don't need to get
3106 * the block addresses when there is no need to fill the page.
3108 if (!f2fs_has_inline_data(inode
) && len
== PAGE_SIZE
&&
3109 !is_inode_flag_set(inode
, FI_NO_PREALLOC
) &&
3110 !f2fs_verity_in_progress(inode
))
3113 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3114 if (f2fs_has_inline_data(inode
) && pos
+ len
> MAX_INLINE_DATA(inode
))
3115 flag
= F2FS_GET_BLOCK_DEFAULT
;
3117 flag
= F2FS_GET_BLOCK_PRE_AIO
;
3119 if (f2fs_has_inline_data(inode
) ||
3120 (pos
& PAGE_MASK
) >= i_size_read(inode
)) {
3121 __do_map_lock(sbi
, flag
, true);
3126 /* check inline_data */
3127 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
3128 if (IS_ERR(ipage
)) {
3129 err
= PTR_ERR(ipage
);
3133 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
3135 if (f2fs_has_inline_data(inode
)) {
3136 if (pos
+ len
<= MAX_INLINE_DATA(inode
)) {
3137 f2fs_do_read_inline_data(page
, ipage
);
3138 set_inode_flag(inode
, FI_DATA_EXIST
);
3140 set_inline_node(ipage
);
3142 err
= f2fs_convert_inline_page(&dn
, page
);
3145 if (dn
.data_blkaddr
== NULL_ADDR
)
3146 err
= f2fs_get_block(&dn
, index
);
3148 } else if (locked
) {
3149 err
= f2fs_get_block(&dn
, index
);
3151 if (f2fs_lookup_extent_cache(inode
, index
, &ei
)) {
3152 dn
.data_blkaddr
= ei
.blk
+ index
- ei
.fofs
;
3155 err
= f2fs_get_dnode_of_data(&dn
, index
, LOOKUP_NODE
);
3156 if (err
|| dn
.data_blkaddr
== NULL_ADDR
) {
3157 f2fs_put_dnode(&dn
);
3158 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
,
3160 WARN_ON(flag
!= F2FS_GET_BLOCK_PRE_AIO
);
3167 /* convert_inline_page can make node_changed */
3168 *blk_addr
= dn
.data_blkaddr
;
3169 *node_changed
= dn
.node_changed
;
3171 f2fs_put_dnode(&dn
);
3174 __do_map_lock(sbi
, flag
, false);
3178 static int f2fs_write_begin(struct file
*file
, struct address_space
*mapping
,
3179 loff_t pos
, unsigned len
, unsigned flags
,
3180 struct page
**pagep
, void **fsdata
)
3182 struct inode
*inode
= mapping
->host
;
3183 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3184 struct page
*page
= NULL
;
3185 pgoff_t index
= ((unsigned long long) pos
) >> PAGE_SHIFT
;
3186 bool need_balance
= false, drop_atomic
= false;
3187 block_t blkaddr
= NULL_ADDR
;
3190 trace_f2fs_write_begin(inode
, pos
, len
, flags
);
3192 if (!f2fs_is_checkpoint_ready(sbi
)) {
3197 if ((f2fs_is_atomic_file(inode
) &&
3198 !f2fs_available_free_memory(sbi
, INMEM_PAGES
)) ||
3199 is_inode_flag_set(inode
, FI_ATOMIC_REVOKE_REQUEST
)) {
3206 * We should check this at this moment to avoid deadlock on inode page
3207 * and #0 page. The locking rule for inline_data conversion should be:
3208 * lock_page(page #0) -> lock_page(inode_page)
3211 err
= f2fs_convert_inline_inode(inode
);
3216 #ifdef CONFIG_F2FS_FS_COMPRESSION
3217 if (f2fs_compressed_file(inode
)) {
3222 ret
= f2fs_prepare_compress_overwrite(inode
, pagep
,
3235 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3236 * wait_for_stable_page. Will wait that below with our IO control.
3238 page
= f2fs_pagecache_get_page(mapping
, index
,
3239 FGP_LOCK
| FGP_WRITE
| FGP_CREAT
, GFP_NOFS
);
3245 /* TODO: cluster can be compressed due to race with .writepage */
3249 err
= prepare_write_begin(sbi
, page
, pos
, len
,
3250 &blkaddr
, &need_balance
);
3254 if (need_balance
&& !IS_NOQUOTA(inode
) &&
3255 has_not_enough_free_secs(sbi
, 0, 0)) {
3257 f2fs_balance_fs(sbi
, true);
3259 if (page
->mapping
!= mapping
) {
3260 /* The page got truncated from under us */
3261 f2fs_put_page(page
, 1);
3266 f2fs_wait_on_page_writeback(page
, DATA
, false, true);
3268 if (len
== PAGE_SIZE
|| PageUptodate(page
))
3271 if (!(pos
& (PAGE_SIZE
- 1)) && (pos
+ len
) >= i_size_read(inode
) &&
3272 !f2fs_verity_in_progress(inode
)) {
3273 zero_user_segment(page
, len
, PAGE_SIZE
);
3277 if (blkaddr
== NEW_ADDR
) {
3278 zero_user_segment(page
, 0, PAGE_SIZE
);
3279 SetPageUptodate(page
);
3281 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
,
3282 DATA_GENERIC_ENHANCE_READ
)) {
3283 err
= -EFSCORRUPTED
;
3286 err
= f2fs_submit_page_read(inode
, page
, blkaddr
);
3291 if (unlikely(page
->mapping
!= mapping
)) {
3292 f2fs_put_page(page
, 1);
3295 if (unlikely(!PageUptodate(page
))) {
3303 f2fs_put_page(page
, 1);
3304 f2fs_write_failed(mapping
, pos
+ len
);
3306 f2fs_drop_inmem_pages_all(sbi
, false);
3310 static int f2fs_write_end(struct file
*file
,
3311 struct address_space
*mapping
,
3312 loff_t pos
, unsigned len
, unsigned copied
,
3313 struct page
*page
, void *fsdata
)
3315 struct inode
*inode
= page
->mapping
->host
;
3317 trace_f2fs_write_end(inode
, pos
, len
, copied
);
3320 * This should be come from len == PAGE_SIZE, and we expect copied
3321 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3322 * let generic_perform_write() try to copy data again through copied=0.
3324 if (!PageUptodate(page
)) {
3325 if (unlikely(copied
!= len
))
3328 SetPageUptodate(page
);
3331 #ifdef CONFIG_F2FS_FS_COMPRESSION
3332 /* overwrite compressed file */
3333 if (f2fs_compressed_file(inode
) && fsdata
) {
3334 f2fs_compress_write_end(inode
, fsdata
, page
->index
, copied
);
3335 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3343 set_page_dirty(page
);
3345 if (pos
+ copied
> i_size_read(inode
) &&
3346 !f2fs_verity_in_progress(inode
))
3347 f2fs_i_size_write(inode
, pos
+ copied
);
3349 f2fs_put_page(page
, 1);
3350 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3354 static int check_direct_IO(struct inode
*inode
, struct iov_iter
*iter
,
3357 unsigned i_blkbits
= READ_ONCE(inode
->i_blkbits
);
3358 unsigned blkbits
= i_blkbits
;
3359 unsigned blocksize_mask
= (1 << blkbits
) - 1;
3360 unsigned long align
= offset
| iov_iter_alignment(iter
);
3361 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
3363 if (align
& blocksize_mask
) {
3365 blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
3366 blocksize_mask
= (1 << blkbits
) - 1;
3367 if (align
& blocksize_mask
)
3374 static void f2fs_dio_end_io(struct bio
*bio
)
3376 struct f2fs_private_dio
*dio
= bio
->bi_private
;
3378 dec_page_count(F2FS_I_SB(dio
->inode
),
3379 dio
->write
? F2FS_DIO_WRITE
: F2FS_DIO_READ
);
3381 bio
->bi_private
= dio
->orig_private
;
3382 bio
->bi_end_io
= dio
->orig_end_io
;
3389 static void f2fs_dio_submit_bio(struct bio
*bio
, struct inode
*inode
,
3392 struct f2fs_private_dio
*dio
;
3393 bool write
= (bio_op(bio
) == REQ_OP_WRITE
);
3395 dio
= f2fs_kzalloc(F2FS_I_SB(inode
),
3396 sizeof(struct f2fs_private_dio
), GFP_NOFS
);
3401 dio
->orig_end_io
= bio
->bi_end_io
;
3402 dio
->orig_private
= bio
->bi_private
;
3405 bio
->bi_end_io
= f2fs_dio_end_io
;
3406 bio
->bi_private
= dio
;
3408 inc_page_count(F2FS_I_SB(inode
),
3409 write
? F2FS_DIO_WRITE
: F2FS_DIO_READ
);
3414 bio
->bi_status
= BLK_STS_IOERR
;
3418 static ssize_t
f2fs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3420 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
3421 struct inode
*inode
= mapping
->host
;
3422 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3423 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
3424 size_t count
= iov_iter_count(iter
);
3425 loff_t offset
= iocb
->ki_pos
;
3426 int rw
= iov_iter_rw(iter
);
3428 enum rw_hint hint
= iocb
->ki_hint
;
3429 int whint_mode
= F2FS_OPTION(sbi
).whint_mode
;
3432 err
= check_direct_IO(inode
, iter
, offset
);
3434 return err
< 0 ? err
: 0;
3436 if (f2fs_force_buffered_io(inode
, iocb
, iter
))
3439 do_opu
= allow_outplace_dio(inode
, iocb
, iter
);
3441 trace_f2fs_direct_IO_enter(inode
, offset
, count
, rw
);
3443 if (rw
== WRITE
&& whint_mode
== WHINT_MODE_OFF
)
3444 iocb
->ki_hint
= WRITE_LIFE_NOT_SET
;
3446 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
3447 if (!down_read_trylock(&fi
->i_gc_rwsem
[rw
])) {
3448 iocb
->ki_hint
= hint
;
3452 if (do_opu
&& !down_read_trylock(&fi
->i_gc_rwsem
[READ
])) {
3453 up_read(&fi
->i_gc_rwsem
[rw
]);
3454 iocb
->ki_hint
= hint
;
3459 down_read(&fi
->i_gc_rwsem
[rw
]);
3461 down_read(&fi
->i_gc_rwsem
[READ
]);
3464 err
= __blockdev_direct_IO(iocb
, inode
, inode
->i_sb
->s_bdev
,
3465 iter
, rw
== WRITE
? get_data_block_dio_write
:
3466 get_data_block_dio
, NULL
, f2fs_dio_submit_bio
,
3467 DIO_LOCKING
| DIO_SKIP_HOLES
);
3470 up_read(&fi
->i_gc_rwsem
[READ
]);
3472 up_read(&fi
->i_gc_rwsem
[rw
]);
3475 if (whint_mode
== WHINT_MODE_OFF
)
3476 iocb
->ki_hint
= hint
;
3478 f2fs_update_iostat(F2FS_I_SB(inode
), APP_DIRECT_IO
,
3481 set_inode_flag(inode
, FI_UPDATE_WRITE
);
3482 } else if (err
< 0) {
3483 f2fs_write_failed(mapping
, offset
+ count
);
3488 trace_f2fs_direct_IO_exit(inode
, offset
, count
, rw
, err
);
3493 void f2fs_invalidate_page(struct page
*page
, unsigned int offset
,
3494 unsigned int length
)
3496 struct inode
*inode
= page
->mapping
->host
;
3497 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
3499 if (inode
->i_ino
>= F2FS_ROOT_INO(sbi
) &&
3500 (offset
% PAGE_SIZE
|| length
!= PAGE_SIZE
))
3503 if (PageDirty(page
)) {
3504 if (inode
->i_ino
== F2FS_META_INO(sbi
)) {
3505 dec_page_count(sbi
, F2FS_DIRTY_META
);
3506 } else if (inode
->i_ino
== F2FS_NODE_INO(sbi
)) {
3507 dec_page_count(sbi
, F2FS_DIRTY_NODES
);
3509 inode_dec_dirty_pages(inode
);
3510 f2fs_remove_dirty_inode(inode
);
3514 clear_cold_data(page
);
3516 if (IS_ATOMIC_WRITTEN_PAGE(page
))
3517 return f2fs_drop_inmem_page(inode
, page
);
3519 f2fs_clear_page_private(page
);
3522 int f2fs_release_page(struct page
*page
, gfp_t wait
)
3524 /* If this is dirty page, keep PagePrivate */
3525 if (PageDirty(page
))
3528 /* This is atomic written page, keep Private */
3529 if (IS_ATOMIC_WRITTEN_PAGE(page
))
3532 clear_cold_data(page
);
3533 f2fs_clear_page_private(page
);
3537 static int f2fs_set_data_page_dirty(struct page
*page
)
3539 struct inode
*inode
= page_file_mapping(page
)->host
;
3541 trace_f2fs_set_page_dirty(page
, DATA
);
3543 if (!PageUptodate(page
))
3544 SetPageUptodate(page
);
3545 if (PageSwapCache(page
))
3546 return __set_page_dirty_nobuffers(page
);
3548 if (f2fs_is_atomic_file(inode
) && !f2fs_is_commit_atomic_write(inode
)) {
3549 if (!IS_ATOMIC_WRITTEN_PAGE(page
)) {
3550 f2fs_register_inmem_page(inode
, page
);
3554 * Previously, this page has been registered, we just
3560 if (!PageDirty(page
)) {
3561 __set_page_dirty_nobuffers(page
);
3562 f2fs_update_dirty_page(inode
, page
);
3568 static sector_t
f2fs_bmap(struct address_space
*mapping
, sector_t block
)
3570 struct inode
*inode
= mapping
->host
;
3572 if (f2fs_has_inline_data(inode
))
3575 /* make sure allocating whole blocks */
3576 if (mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
3577 filemap_write_and_wait(mapping
);
3579 return generic_block_bmap(mapping
, block
, get_data_block_bmap
);
3582 #ifdef CONFIG_MIGRATION
3583 #include <linux/migrate.h>
3585 int f2fs_migrate_page(struct address_space
*mapping
,
3586 struct page
*newpage
, struct page
*page
, enum migrate_mode mode
)
3588 int rc
, extra_count
;
3589 struct f2fs_inode_info
*fi
= F2FS_I(mapping
->host
);
3590 bool atomic_written
= IS_ATOMIC_WRITTEN_PAGE(page
);
3592 BUG_ON(PageWriteback(page
));
3594 /* migrating an atomic written page is safe with the inmem_lock hold */
3595 if (atomic_written
) {
3596 if (mode
!= MIGRATE_SYNC
)
3598 if (!mutex_trylock(&fi
->inmem_lock
))
3602 /* one extra reference was held for atomic_write page */
3603 extra_count
= atomic_written
? 1 : 0;
3604 rc
= migrate_page_move_mapping(mapping
, newpage
,
3606 if (rc
!= MIGRATEPAGE_SUCCESS
) {
3608 mutex_unlock(&fi
->inmem_lock
);
3612 if (atomic_written
) {
3613 struct inmem_pages
*cur
;
3614 list_for_each_entry(cur
, &fi
->inmem_pages
, list
)
3615 if (cur
->page
== page
) {
3616 cur
->page
= newpage
;
3619 mutex_unlock(&fi
->inmem_lock
);
3624 if (PagePrivate(page
)) {
3625 f2fs_set_page_private(newpage
, page_private(page
));
3626 f2fs_clear_page_private(page
);
3629 if (mode
!= MIGRATE_SYNC_NO_COPY
)
3630 migrate_page_copy(newpage
, page
);
3632 migrate_page_states(newpage
, page
);
3634 return MIGRATEPAGE_SUCCESS
;
3639 /* Copied from generic_swapfile_activate() to check any holes */
3640 static int check_swap_activate(struct swap_info_struct
*sis
,
3641 struct file
*swap_file
, sector_t
*span
)
3643 struct address_space
*mapping
= swap_file
->f_mapping
;
3644 struct inode
*inode
= mapping
->host
;
3645 unsigned blocks_per_page
;
3646 unsigned long page_no
;
3648 sector_t probe_block
;
3649 sector_t last_block
;
3650 sector_t lowest_block
= -1;
3651 sector_t highest_block
= 0;
3655 blkbits
= inode
->i_blkbits
;
3656 blocks_per_page
= PAGE_SIZE
>> blkbits
;
3659 * Map all the blocks into the extent list. This code doesn't try
3664 last_block
= i_size_read(inode
) >> blkbits
;
3665 while ((probe_block
+ blocks_per_page
) <= last_block
&&
3666 page_no
< sis
->max
) {
3667 unsigned block_in_page
;
3668 sector_t first_block
;
3674 block
= probe_block
;
3675 err
= bmap(inode
, &block
);
3678 first_block
= block
;
3681 * It must be PAGE_SIZE aligned on-disk
3683 if (first_block
& (blocks_per_page
- 1)) {
3688 for (block_in_page
= 1; block_in_page
< blocks_per_page
;
3691 block
= probe_block
+ block_in_page
;
3692 err
= bmap(inode
, &block
);
3697 if (block
!= first_block
+ block_in_page
) {
3704 first_block
>>= (PAGE_SHIFT
- blkbits
);
3705 if (page_no
) { /* exclude the header page */
3706 if (first_block
< lowest_block
)
3707 lowest_block
= first_block
;
3708 if (first_block
> highest_block
)
3709 highest_block
= first_block
;
3713 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3715 ret
= add_swap_extent(sis
, page_no
, 1, first_block
);
3720 probe_block
+= blocks_per_page
;
3725 *span
= 1 + highest_block
- lowest_block
;
3727 page_no
= 1; /* force Empty message */
3729 sis
->pages
= page_no
- 1;
3730 sis
->highest_bit
= page_no
- 1;
3734 pr_err("swapon: swapfile has holes\n");
3738 static int f2fs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
3741 struct inode
*inode
= file_inode(file
);
3744 if (!S_ISREG(inode
->i_mode
))
3747 if (f2fs_readonly(F2FS_I_SB(inode
)->sb
))
3750 ret
= f2fs_convert_inline_inode(inode
);
3754 if (f2fs_disable_compressed_file(inode
))
3757 ret
= check_swap_activate(sis
, file
, span
);
3761 set_inode_flag(inode
, FI_PIN_FILE
);
3762 f2fs_precache_extents(inode
);
3763 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
3767 static void f2fs_swap_deactivate(struct file
*file
)
3769 struct inode
*inode
= file_inode(file
);
3771 clear_inode_flag(inode
, FI_PIN_FILE
);
3774 static int f2fs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
3780 static void f2fs_swap_deactivate(struct file
*file
)
3785 const struct address_space_operations f2fs_dblock_aops
= {
3786 .readpage
= f2fs_read_data_page
,
3787 .readpages
= f2fs_read_data_pages
,
3788 .writepage
= f2fs_write_data_page
,
3789 .writepages
= f2fs_write_data_pages
,
3790 .write_begin
= f2fs_write_begin
,
3791 .write_end
= f2fs_write_end
,
3792 .set_page_dirty
= f2fs_set_data_page_dirty
,
3793 .invalidatepage
= f2fs_invalidate_page
,
3794 .releasepage
= f2fs_release_page
,
3795 .direct_IO
= f2fs_direct_IO
,
3797 .swap_activate
= f2fs_swap_activate
,
3798 .swap_deactivate
= f2fs_swap_deactivate
,
3799 #ifdef CONFIG_MIGRATION
3800 .migratepage
= f2fs_migrate_page
,
3804 void f2fs_clear_page_cache_dirty_tag(struct page
*page
)
3806 struct address_space
*mapping
= page_mapping(page
);
3807 unsigned long flags
;
3809 xa_lock_irqsave(&mapping
->i_pages
, flags
);
3810 __xa_clear_mark(&mapping
->i_pages
, page_index(page
),
3811 PAGECACHE_TAG_DIRTY
);
3812 xa_unlock_irqrestore(&mapping
->i_pages
, flags
);
3815 int __init
f2fs_init_post_read_processing(void)
3817 bio_post_read_ctx_cache
=
3818 kmem_cache_create("f2fs_bio_post_read_ctx",
3819 sizeof(struct bio_post_read_ctx
), 0, 0, NULL
);
3820 if (!bio_post_read_ctx_cache
)
3822 bio_post_read_ctx_pool
=
3823 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS
,
3824 bio_post_read_ctx_cache
);
3825 if (!bio_post_read_ctx_pool
)
3826 goto fail_free_cache
;
3830 kmem_cache_destroy(bio_post_read_ctx_cache
);
3835 void f2fs_destroy_post_read_processing(void)
3837 mempool_destroy(bio_post_read_ctx_pool
);
3838 kmem_cache_destroy(bio_post_read_ctx_cache
);
3841 int f2fs_init_post_read_wq(struct f2fs_sb_info
*sbi
)
3843 if (!f2fs_sb_has_encrypt(sbi
) &&
3844 !f2fs_sb_has_verity(sbi
) &&
3845 !f2fs_sb_has_compression(sbi
))
3848 sbi
->post_read_wq
= alloc_workqueue("f2fs_post_read_wq",
3849 WQ_UNBOUND
| WQ_HIGHPRI
,
3851 if (!sbi
->post_read_wq
)
3856 void f2fs_destroy_post_read_wq(struct f2fs_sb_info
*sbi
)
3858 if (sbi
->post_read_wq
)
3859 destroy_workqueue(sbi
->post_read_wq
);
3862 int __init
f2fs_init_bio_entry_cache(void)
3864 bio_entry_slab
= f2fs_kmem_cache_create("bio_entry_slab",
3865 sizeof(struct bio_entry
));
3866 if (!bio_entry_slab
)
3871 void f2fs_destroy_bio_entry_cache(void)
3873 kmem_cache_destroy(bio_entry_slab
);