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/blk-crypto.h>
18 #include <linux/swap.h>
19 #include <linux/prefetch.h>
20 #include <linux/uio.h>
21 #include <linux/cleancache.h>
22 #include <linux/sched/signal.h>
23 #include <linux/fiemap.h>
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static struct kmem_cache *bio_entry_slab;
35 static mempool_t *bio_post_read_ctx_pool;
36 static struct bio_set f2fs_bioset;
38 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
40 int __init f2fs_init_bioset(void)
42 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS))
48 void f2fs_destroy_bioset(void)
50 bioset_exit(&f2fs_bioset);
53 static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
54 unsigned int nr_iovecs)
56 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
59 struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
62 /* No failure on bio allocation */
63 return __f2fs_bio_alloc(GFP_NOIO, npages);
66 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
67 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
71 return __f2fs_bio_alloc(GFP_KERNEL, npages);
74 static bool __is_cp_guaranteed(struct page *page)
76 struct address_space *mapping = page->mapping;
78 struct f2fs_sb_info *sbi;
83 if (f2fs_is_compressed_page(page))
86 inode = mapping->host;
87 sbi = F2FS_I_SB(inode);
89 if (inode->i_ino == F2FS_META_INO(sbi) ||
90 inode->i_ino == F2FS_NODE_INO(sbi) ||
91 S_ISDIR(inode->i_mode) ||
92 (S_ISREG(inode->i_mode) &&
93 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
99 static enum count_type __read_io_type(struct page *page)
101 struct address_space *mapping = page_file_mapping(page);
104 struct inode *inode = mapping->host;
105 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
107 if (inode->i_ino == F2FS_META_INO(sbi))
110 if (inode->i_ino == F2FS_NODE_INO(sbi))
116 /* postprocessing steps for read bios */
117 enum bio_post_read_step {
119 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
120 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
124 struct bio_post_read_ctx {
126 struct f2fs_sb_info *sbi;
127 struct work_struct work;
128 unsigned int enabled_steps;
131 static void __read_end_io(struct bio *bio, bool compr, bool verity)
135 struct bvec_iter_all iter_all;
137 bio_for_each_segment_all(bv, bio, iter_all) {
140 #ifdef CONFIG_F2FS_FS_COMPRESSION
141 if (compr && f2fs_is_compressed_page(page)) {
142 f2fs_decompress_pages(bio, page, verity);
149 /* PG_error was set if any post_read step failed */
150 if (bio->bi_status || PageError(page)) {
151 ClearPageUptodate(page);
152 /* will re-read again later */
153 ClearPageError(page);
155 SetPageUptodate(page);
157 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
162 static void f2fs_release_read_bio(struct bio *bio);
163 static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
166 __read_end_io(bio, false, verity);
167 f2fs_release_read_bio(bio);
170 static void f2fs_decompress_bio(struct bio *bio, bool verity)
172 __read_end_io(bio, true, verity);
175 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
177 static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
179 fscrypt_decrypt_bio(ctx->bio);
182 static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
184 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
187 #ifdef CONFIG_F2FS_FS_COMPRESSION
188 static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
190 f2fs_decompress_end_io(rpages, cluster_size, false, true);
193 static void f2fs_verify_bio(struct bio *bio)
196 struct bvec_iter_all iter_all;
198 bio_for_each_segment_all(bv, bio, iter_all) {
199 struct page *page = bv->bv_page;
200 struct decompress_io_ctx *dic;
202 dic = (struct decompress_io_ctx *)page_private(page);
205 if (atomic_dec_return(&dic->verity_pages))
207 f2fs_verify_pages(dic->rpages,
213 if (bio->bi_status || PageError(page))
216 if (fsverity_verify_page(page)) {
217 SetPageUptodate(page);
221 ClearPageUptodate(page);
222 ClearPageError(page);
224 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
230 static void f2fs_verity_work(struct work_struct *work)
232 struct bio_post_read_ctx *ctx =
233 container_of(work, struct bio_post_read_ctx, work);
234 struct bio *bio = ctx->bio;
235 #ifdef CONFIG_F2FS_FS_COMPRESSION
236 unsigned int enabled_steps = ctx->enabled_steps;
240 * fsverity_verify_bio() may call readpages() again, and while verity
241 * will be disabled for this, decryption may still be needed, resulting
242 * in another bio_post_read_ctx being allocated. So to prevent
243 * deadlocks we need to release the current ctx to the mempool first.
244 * This assumes that verity is the last post-read step.
246 mempool_free(ctx, bio_post_read_ctx_pool);
247 bio->bi_private = NULL;
249 #ifdef CONFIG_F2FS_FS_COMPRESSION
250 /* previous step is decompression */
251 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
252 f2fs_verify_bio(bio);
253 f2fs_release_read_bio(bio);
258 fsverity_verify_bio(bio);
259 __f2fs_read_end_io(bio, false, false);
262 static void f2fs_post_read_work(struct work_struct *work)
264 struct bio_post_read_ctx *ctx =
265 container_of(work, struct bio_post_read_ctx, work);
267 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
268 f2fs_decrypt_work(ctx);
270 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
271 f2fs_decompress_work(ctx);
273 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
274 INIT_WORK(&ctx->work, f2fs_verity_work);
275 fsverity_enqueue_verify_work(&ctx->work);
279 __f2fs_read_end_io(ctx->bio,
280 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
283 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
284 struct work_struct *work)
286 queue_work(sbi->post_read_wq, work);
289 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
292 * We use different work queues for decryption and for verity because
293 * verity may require reading metadata pages that need decryption, and
294 * we shouldn't recurse to the same workqueue.
297 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
298 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
299 INIT_WORK(&ctx->work, f2fs_post_read_work);
300 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
304 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
305 INIT_WORK(&ctx->work, f2fs_verity_work);
306 fsverity_enqueue_verify_work(&ctx->work);
310 __f2fs_read_end_io(ctx->bio, false, false);
313 static bool f2fs_bio_post_read_required(struct bio *bio)
315 return bio->bi_private;
318 static void f2fs_read_end_io(struct bio *bio)
320 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
322 if (time_to_inject(sbi, FAULT_READ_IO)) {
323 f2fs_show_injection_info(sbi, FAULT_READ_IO);
324 bio->bi_status = BLK_STS_IOERR;
327 if (f2fs_bio_post_read_required(bio)) {
328 struct bio_post_read_ctx *ctx = bio->bi_private;
330 bio_post_read_processing(ctx);
334 __f2fs_read_end_io(bio, false, false);
337 static void f2fs_write_end_io(struct bio *bio)
339 struct f2fs_sb_info *sbi = bio->bi_private;
340 struct bio_vec *bvec;
341 struct bvec_iter_all iter_all;
343 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
344 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
345 bio->bi_status = BLK_STS_IOERR;
348 bio_for_each_segment_all(bvec, bio, iter_all) {
349 struct page *page = bvec->bv_page;
350 enum count_type type = WB_DATA_TYPE(page);
352 if (IS_DUMMY_WRITTEN_PAGE(page)) {
353 set_page_private(page, (unsigned long)NULL);
354 ClearPagePrivate(page);
356 mempool_free(page, sbi->write_io_dummy);
358 if (unlikely(bio->bi_status))
359 f2fs_stop_checkpoint(sbi, true);
363 fscrypt_finalize_bounce_page(&page);
365 #ifdef CONFIG_F2FS_FS_COMPRESSION
366 if (f2fs_is_compressed_page(page)) {
367 f2fs_compress_write_end_io(bio, page);
372 if (unlikely(bio->bi_status)) {
373 mapping_set_error(page->mapping, -EIO);
374 if (type == F2FS_WB_CP_DATA)
375 f2fs_stop_checkpoint(sbi, true);
378 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
379 page->index != nid_of_node(page));
381 dec_page_count(sbi, type);
382 if (f2fs_in_warm_node_list(sbi, page))
383 f2fs_del_fsync_node_entry(sbi, page);
384 clear_cold_data(page);
385 end_page_writeback(page);
387 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
388 wq_has_sleeper(&sbi->cp_wait))
389 wake_up(&sbi->cp_wait);
394 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
395 block_t blk_addr, struct bio *bio)
397 struct block_device *bdev = sbi->sb->s_bdev;
400 if (f2fs_is_multi_device(sbi)) {
401 for (i = 0; i < sbi->s_ndevs; i++) {
402 if (FDEV(i).start_blk <= blk_addr &&
403 FDEV(i).end_blk >= blk_addr) {
404 blk_addr -= FDEV(i).start_blk;
411 bio_set_dev(bio, bdev);
412 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
417 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
421 if (!f2fs_is_multi_device(sbi))
424 for (i = 0; i < sbi->s_ndevs; i++)
425 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
430 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
432 struct f2fs_sb_info *sbi = fio->sbi;
435 bio = f2fs_bio_alloc(sbi, npages, true);
437 f2fs_target_device(sbi, fio->new_blkaddr, bio);
438 if (is_read_io(fio->op)) {
439 bio->bi_end_io = f2fs_read_end_io;
440 bio->bi_private = NULL;
442 bio->bi_end_io = f2fs_write_end_io;
443 bio->bi_private = sbi;
444 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
445 fio->type, fio->temp);
448 wbc_init_bio(fio->io_wbc, bio);
453 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
455 const struct f2fs_io_info *fio,
459 * The f2fs garbage collector sets ->encrypted_page when it wants to
460 * read/write raw data without encryption.
462 if (!fio || !fio->encrypted_page)
463 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
466 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
468 const struct f2fs_io_info *fio)
471 * The f2fs garbage collector sets ->encrypted_page when it wants to
472 * read/write raw data without encryption.
474 if (fio && fio->encrypted_page)
475 return !bio_has_crypt_ctx(bio);
477 return fscrypt_mergeable_bio(bio, inode, next_idx);
480 static inline void __submit_bio(struct f2fs_sb_info *sbi,
481 struct bio *bio, enum page_type type)
483 if (!is_read_io(bio_op(bio))) {
486 if (type != DATA && type != NODE)
489 if (f2fs_lfs_mode(sbi) && current->plug)
490 blk_finish_plug(current->plug);
492 if (F2FS_IO_ALIGNED(sbi))
495 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
496 start %= F2FS_IO_SIZE(sbi);
501 /* fill dummy pages */
502 for (; start < F2FS_IO_SIZE(sbi); start++) {
504 mempool_alloc(sbi->write_io_dummy,
505 GFP_NOIO | __GFP_NOFAIL);
506 f2fs_bug_on(sbi, !page);
508 zero_user_segment(page, 0, PAGE_SIZE);
509 SetPagePrivate(page);
510 set_page_private(page, DUMMY_WRITTEN_PAGE);
512 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
516 * In the NODE case, we lose next block address chain. So, we
517 * need to do checkpoint in f2fs_sync_file.
520 set_sbi_flag(sbi, SBI_NEED_CP);
523 if (is_read_io(bio_op(bio)))
524 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
526 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
530 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
531 struct bio *bio, enum page_type type)
533 __submit_bio(sbi, bio, type);
536 static void __attach_io_flag(struct f2fs_io_info *fio)
538 struct f2fs_sb_info *sbi = fio->sbi;
539 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
540 unsigned int io_flag, fua_flag, meta_flag;
542 if (fio->type == DATA)
543 io_flag = sbi->data_io_flag;
544 else if (fio->type == NODE)
545 io_flag = sbi->node_io_flag;
549 fua_flag = io_flag & temp_mask;
550 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
553 * data/node io flag bits per temp:
554 * REQ_META | REQ_FUA |
555 * 5 | 4 | 3 | 2 | 1 | 0 |
556 * Cold | Warm | Hot | Cold | Warm | Hot |
558 if ((1 << fio->temp) & meta_flag)
559 fio->op_flags |= REQ_META;
560 if ((1 << fio->temp) & fua_flag)
561 fio->op_flags |= REQ_FUA;
564 static void __submit_merged_bio(struct f2fs_bio_info *io)
566 struct f2fs_io_info *fio = &io->fio;
571 __attach_io_flag(fio);
572 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
574 if (is_read_io(fio->op))
575 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
577 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
579 __submit_bio(io->sbi, io->bio, fio->type);
583 static bool __has_merged_page(struct bio *bio, struct inode *inode,
584 struct page *page, nid_t ino)
586 struct bio_vec *bvec;
587 struct bvec_iter_all iter_all;
592 if (!inode && !page && !ino)
595 bio_for_each_segment_all(bvec, bio, iter_all) {
596 struct page *target = bvec->bv_page;
598 if (fscrypt_is_bounce_page(target)) {
599 target = fscrypt_pagecache_page(target);
603 if (f2fs_is_compressed_page(target)) {
604 target = f2fs_compress_control_page(target);
609 if (inode && inode == target->mapping->host)
611 if (page && page == target)
613 if (ino && ino == ino_of_node(target))
620 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
621 enum page_type type, enum temp_type temp)
623 enum page_type btype = PAGE_TYPE_OF_BIO(type);
624 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
626 down_write(&io->io_rwsem);
628 /* change META to META_FLUSH in the checkpoint procedure */
629 if (type >= META_FLUSH) {
630 io->fio.type = META_FLUSH;
631 io->fio.op = REQ_OP_WRITE;
632 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
633 if (!test_opt(sbi, NOBARRIER))
634 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
636 __submit_merged_bio(io);
637 up_write(&io->io_rwsem);
640 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
641 struct inode *inode, struct page *page,
642 nid_t ino, enum page_type type, bool force)
647 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
649 enum page_type btype = PAGE_TYPE_OF_BIO(type);
650 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
652 down_read(&io->io_rwsem);
653 ret = __has_merged_page(io->bio, inode, page, ino);
654 up_read(&io->io_rwsem);
657 __f2fs_submit_merged_write(sbi, type, temp);
659 /* TODO: use HOT temp only for meta pages now. */
665 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
667 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
670 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
671 struct inode *inode, struct page *page,
672 nid_t ino, enum page_type type)
674 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
677 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
679 f2fs_submit_merged_write(sbi, DATA);
680 f2fs_submit_merged_write(sbi, NODE);
681 f2fs_submit_merged_write(sbi, META);
685 * Fill the locked page with data located in the block address.
686 * A caller needs to unlock the page on failure.
688 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
691 struct page *page = fio->encrypted_page ?
692 fio->encrypted_page : fio->page;
694 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
695 fio->is_por ? META_POR : (__is_meta_io(fio) ?
696 META_GENERIC : DATA_GENERIC_ENHANCE)))
697 return -EFSCORRUPTED;
699 trace_f2fs_submit_page_bio(page, fio);
700 f2fs_trace_ios(fio, 0);
702 /* Allocate a new bio */
703 bio = __bio_alloc(fio, 1);
705 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
706 fio->page->index, fio, GFP_NOIO);
708 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
713 if (fio->io_wbc && !is_read_io(fio->op))
714 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
716 __attach_io_flag(fio);
717 bio_set_op_attrs(bio, fio->op, fio->op_flags);
719 inc_page_count(fio->sbi, is_read_io(fio->op) ?
720 __read_io_type(page): WB_DATA_TYPE(fio->page));
722 __submit_bio(fio->sbi, bio, fio->type);
726 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
727 block_t last_blkaddr, block_t cur_blkaddr)
729 if (unlikely(sbi->max_io_bytes &&
730 bio->bi_iter.bi_size >= sbi->max_io_bytes))
732 if (last_blkaddr + 1 != cur_blkaddr)
734 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
737 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
738 struct f2fs_io_info *fio)
740 if (io->fio.op != fio->op)
742 return io->fio.op_flags == fio->op_flags;
745 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
746 struct f2fs_bio_info *io,
747 struct f2fs_io_info *fio,
748 block_t last_blkaddr,
751 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
752 unsigned int filled_blocks =
753 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
754 unsigned int io_size = F2FS_IO_SIZE(sbi);
755 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
757 /* IOs in bio is aligned and left space of vectors is not enough */
758 if (!(filled_blocks % io_size) && left_vecs < io_size)
761 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
763 return io_type_is_mergeable(io, fio);
766 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
767 struct page *page, enum temp_type temp)
769 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
770 struct bio_entry *be;
772 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
776 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
779 down_write(&io->bio_list_lock);
780 list_add_tail(&be->list, &io->bio_list);
781 up_write(&io->bio_list_lock);
784 static void del_bio_entry(struct bio_entry *be)
787 kmem_cache_free(bio_entry_slab, be);
790 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
793 struct f2fs_sb_info *sbi = fio->sbi;
798 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
799 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
800 struct list_head *head = &io->bio_list;
801 struct bio_entry *be;
803 down_write(&io->bio_list_lock);
804 list_for_each_entry(be, head, list) {
810 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
813 if (f2fs_crypt_mergeable_bio(*bio,
814 fio->page->mapping->host,
815 fio->page->index, fio) &&
816 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
822 /* page can't be merged into bio; submit the bio */
824 __submit_bio(sbi, *bio, DATA);
827 up_write(&io->bio_list_lock);
838 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
839 struct bio **bio, struct page *page)
843 struct bio *target = bio ? *bio : NULL;
845 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
846 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
847 struct list_head *head = &io->bio_list;
848 struct bio_entry *be;
850 if (list_empty(head))
853 down_read(&io->bio_list_lock);
854 list_for_each_entry(be, head, list) {
856 found = (target == be->bio);
858 found = __has_merged_page(be->bio, NULL,
863 up_read(&io->bio_list_lock);
870 down_write(&io->bio_list_lock);
871 list_for_each_entry(be, head, list) {
873 found = (target == be->bio);
875 found = __has_merged_page(be->bio, NULL,
883 up_write(&io->bio_list_lock);
887 __submit_bio(sbi, target, DATA);
894 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
896 struct bio *bio = *fio->bio;
897 struct page *page = fio->encrypted_page ?
898 fio->encrypted_page : fio->page;
900 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
901 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
902 return -EFSCORRUPTED;
904 trace_f2fs_submit_page_bio(page, fio);
905 f2fs_trace_ios(fio, 0);
907 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
909 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
912 bio = __bio_alloc(fio, BIO_MAX_PAGES);
913 __attach_io_flag(fio);
914 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
915 fio->page->index, fio, GFP_NOIO);
916 bio_set_op_attrs(bio, fio->op, fio->op_flags);
918 add_bio_entry(fio->sbi, bio, page, fio->temp);
920 if (add_ipu_page(fio, &bio, page))
925 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
927 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
929 *fio->last_block = fio->new_blkaddr;
935 void f2fs_submit_page_write(struct f2fs_io_info *fio)
937 struct f2fs_sb_info *sbi = fio->sbi;
938 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
939 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
940 struct page *bio_page;
942 f2fs_bug_on(sbi, is_read_io(fio->op));
944 down_write(&io->io_rwsem);
947 spin_lock(&io->io_lock);
948 if (list_empty(&io->io_list)) {
949 spin_unlock(&io->io_lock);
952 fio = list_first_entry(&io->io_list,
953 struct f2fs_io_info, list);
954 list_del(&fio->list);
955 spin_unlock(&io->io_lock);
958 verify_fio_blkaddr(fio);
960 if (fio->encrypted_page)
961 bio_page = fio->encrypted_page;
962 else if (fio->compressed_page)
963 bio_page = fio->compressed_page;
965 bio_page = fio->page;
967 /* set submitted = true as a return value */
968 fio->submitted = true;
970 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
973 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
975 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
976 bio_page->index, fio)))
977 __submit_merged_bio(io);
979 if (io->bio == NULL) {
980 if (F2FS_IO_ALIGNED(sbi) &&
981 (fio->type == DATA || fio->type == NODE) &&
982 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
983 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
987 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
988 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
989 bio_page->index, fio, GFP_NOIO);
993 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
994 __submit_merged_bio(io);
999 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
1001 io->last_block_in_bio = fio->new_blkaddr;
1002 f2fs_trace_ios(fio, 0);
1004 trace_f2fs_submit_page_write(fio->page, fio);
1009 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1010 !f2fs_is_checkpoint_ready(sbi))
1011 __submit_merged_bio(io);
1012 up_write(&io->io_rwsem);
1015 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
1017 return fsverity_active(inode) &&
1018 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
1021 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1022 unsigned nr_pages, unsigned op_flag,
1023 pgoff_t first_idx, bool for_write,
1026 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1028 struct bio_post_read_ctx *ctx;
1029 unsigned int post_read_steps = 0;
1031 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
1034 return ERR_PTR(-ENOMEM);
1036 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1038 f2fs_target_device(sbi, blkaddr, bio);
1039 bio->bi_end_io = f2fs_read_end_io;
1040 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1042 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1043 post_read_steps |= 1 << STEP_DECRYPT;
1044 if (f2fs_compressed_file(inode))
1045 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1046 if (for_verity && f2fs_need_verity(inode, first_idx))
1047 post_read_steps |= 1 << STEP_VERITY;
1049 if (post_read_steps) {
1050 /* Due to the mempool, this never fails. */
1051 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1054 ctx->enabled_steps = post_read_steps;
1055 bio->bi_private = ctx;
1061 static void f2fs_release_read_bio(struct bio *bio)
1063 if (bio->bi_private)
1064 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1068 /* This can handle encryption stuffs */
1069 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1070 block_t blkaddr, int op_flags, bool for_write)
1072 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1075 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1076 page->index, for_write, true);
1078 return PTR_ERR(bio);
1080 /* wait for GCed page writeback via META_MAPPING */
1081 f2fs_wait_on_block_writeback(inode, blkaddr);
1083 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1087 ClearPageError(page);
1088 inc_page_count(sbi, F2FS_RD_DATA);
1089 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1090 __submit_bio(sbi, bio, DATA);
1094 static void __set_data_blkaddr(struct dnode_of_data *dn)
1096 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1100 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1101 base = get_extra_isize(dn->inode);
1103 /* Get physical address of data block */
1104 addr_array = blkaddr_in_node(rn);
1105 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1109 * Lock ordering for the change of data block address:
1112 * update block addresses in the node page
1114 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1116 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1117 __set_data_blkaddr(dn);
1118 if (set_page_dirty(dn->node_page))
1119 dn->node_changed = true;
1122 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1124 dn->data_blkaddr = blkaddr;
1125 f2fs_set_data_blkaddr(dn);
1126 f2fs_update_extent_cache(dn);
1129 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1130 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1132 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1138 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1140 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1143 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1144 dn->ofs_in_node, count);
1146 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1148 for (; count > 0; dn->ofs_in_node++) {
1149 block_t blkaddr = f2fs_data_blkaddr(dn);
1150 if (blkaddr == NULL_ADDR) {
1151 dn->data_blkaddr = NEW_ADDR;
1152 __set_data_blkaddr(dn);
1157 if (set_page_dirty(dn->node_page))
1158 dn->node_changed = true;
1162 /* Should keep dn->ofs_in_node unchanged */
1163 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1165 unsigned int ofs_in_node = dn->ofs_in_node;
1168 ret = f2fs_reserve_new_blocks(dn, 1);
1169 dn->ofs_in_node = ofs_in_node;
1173 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1175 bool need_put = dn->inode_page ? false : true;
1178 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1182 if (dn->data_blkaddr == NULL_ADDR)
1183 err = f2fs_reserve_new_block(dn);
1184 if (err || need_put)
1189 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1191 struct extent_info ei = {0, 0, 0};
1192 struct inode *inode = dn->inode;
1194 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1195 dn->data_blkaddr = ei.blk + index - ei.fofs;
1199 return f2fs_reserve_block(dn, index);
1202 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1203 int op_flags, bool for_write)
1205 struct address_space *mapping = inode->i_mapping;
1206 struct dnode_of_data dn;
1208 struct extent_info ei = {0,0,0};
1211 page = f2fs_grab_cache_page(mapping, index, for_write);
1213 return ERR_PTR(-ENOMEM);
1215 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1216 dn.data_blkaddr = ei.blk + index - ei.fofs;
1217 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1218 DATA_GENERIC_ENHANCE_READ)) {
1219 err = -EFSCORRUPTED;
1225 set_new_dnode(&dn, inode, NULL, NULL, 0);
1226 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1229 f2fs_put_dnode(&dn);
1231 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1235 if (dn.data_blkaddr != NEW_ADDR &&
1236 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1238 DATA_GENERIC_ENHANCE)) {
1239 err = -EFSCORRUPTED;
1243 if (PageUptodate(page)) {
1249 * A new dentry page is allocated but not able to be written, since its
1250 * new inode page couldn't be allocated due to -ENOSPC.
1251 * In such the case, its blkaddr can be remained as NEW_ADDR.
1252 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1253 * f2fs_init_inode_metadata.
1255 if (dn.data_blkaddr == NEW_ADDR) {
1256 zero_user_segment(page, 0, PAGE_SIZE);
1257 if (!PageUptodate(page))
1258 SetPageUptodate(page);
1263 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1264 op_flags, for_write);
1270 f2fs_put_page(page, 1);
1271 return ERR_PTR(err);
1274 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1276 struct address_space *mapping = inode->i_mapping;
1279 page = find_get_page(mapping, index);
1280 if (page && PageUptodate(page))
1282 f2fs_put_page(page, 0);
1284 page = f2fs_get_read_data_page(inode, index, 0, false);
1288 if (PageUptodate(page))
1291 wait_on_page_locked(page);
1292 if (unlikely(!PageUptodate(page))) {
1293 f2fs_put_page(page, 0);
1294 return ERR_PTR(-EIO);
1300 * If it tries to access a hole, return an error.
1301 * Because, the callers, functions in dir.c and GC, should be able to know
1302 * whether this page exists or not.
1304 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1307 struct address_space *mapping = inode->i_mapping;
1310 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1314 /* wait for read completion */
1316 if (unlikely(page->mapping != mapping)) {
1317 f2fs_put_page(page, 1);
1320 if (unlikely(!PageUptodate(page))) {
1321 f2fs_put_page(page, 1);
1322 return ERR_PTR(-EIO);
1328 * Caller ensures that this data page is never allocated.
1329 * A new zero-filled data page is allocated in the page cache.
1331 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1333 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1334 * ipage should be released by this function.
1336 struct page *f2fs_get_new_data_page(struct inode *inode,
1337 struct page *ipage, pgoff_t index, bool new_i_size)
1339 struct address_space *mapping = inode->i_mapping;
1341 struct dnode_of_data dn;
1344 page = f2fs_grab_cache_page(mapping, index, true);
1347 * before exiting, we should make sure ipage will be released
1348 * if any error occur.
1350 f2fs_put_page(ipage, 1);
1351 return ERR_PTR(-ENOMEM);
1354 set_new_dnode(&dn, inode, ipage, NULL, 0);
1355 err = f2fs_reserve_block(&dn, index);
1357 f2fs_put_page(page, 1);
1358 return ERR_PTR(err);
1361 f2fs_put_dnode(&dn);
1363 if (PageUptodate(page))
1366 if (dn.data_blkaddr == NEW_ADDR) {
1367 zero_user_segment(page, 0, PAGE_SIZE);
1368 if (!PageUptodate(page))
1369 SetPageUptodate(page);
1371 f2fs_put_page(page, 1);
1373 /* if ipage exists, blkaddr should be NEW_ADDR */
1374 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1375 page = f2fs_get_lock_data_page(inode, index, true);
1380 if (new_i_size && i_size_read(inode) <
1381 ((loff_t)(index + 1) << PAGE_SHIFT))
1382 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1386 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1388 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1389 struct f2fs_summary sum;
1390 struct node_info ni;
1391 block_t old_blkaddr;
1395 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1398 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1402 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1403 if (dn->data_blkaddr != NULL_ADDR)
1406 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1410 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1411 old_blkaddr = dn->data_blkaddr;
1412 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1413 &sum, seg_type, NULL);
1414 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1415 invalidate_mapping_pages(META_MAPPING(sbi),
1416 old_blkaddr, old_blkaddr);
1417 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1420 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1421 * data from unwritten block via dio_read.
1426 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1428 struct inode *inode = file_inode(iocb->ki_filp);
1429 struct f2fs_map_blocks map;
1432 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1434 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1435 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1436 if (map.m_len > map.m_lblk)
1437 map.m_len -= map.m_lblk;
1441 map.m_next_pgofs = NULL;
1442 map.m_next_extent = NULL;
1443 map.m_seg_type = NO_CHECK_TYPE;
1444 map.m_may_create = true;
1447 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1448 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1449 F2FS_GET_BLOCK_PRE_AIO :
1450 F2FS_GET_BLOCK_PRE_DIO;
1453 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1454 err = f2fs_convert_inline_inode(inode);
1458 if (f2fs_has_inline_data(inode))
1461 flag = F2FS_GET_BLOCK_PRE_AIO;
1464 err = f2fs_map_blocks(inode, &map, 1, flag);
1465 if (map.m_len > 0 && err == -ENOSPC) {
1467 set_inode_flag(inode, FI_NO_PREALLOC);
1473 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1475 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1477 down_read(&sbi->node_change);
1479 up_read(&sbi->node_change);
1484 f2fs_unlock_op(sbi);
1489 * f2fs_map_blocks() tries to find or build mapping relationship which
1490 * maps continuous logical blocks to physical blocks, and return such
1491 * info via f2fs_map_blocks structure.
1493 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1494 int create, int flag)
1496 unsigned int maxblocks = map->m_len;
1497 struct dnode_of_data dn;
1498 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1499 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1500 pgoff_t pgofs, end_offset, end;
1501 int err = 0, ofs = 1;
1502 unsigned int ofs_in_node, last_ofs_in_node;
1504 struct extent_info ei = {0,0,0};
1506 unsigned int start_pgofs;
1514 /* it only supports block size == page size */
1515 pgofs = (pgoff_t)map->m_lblk;
1516 end = pgofs + maxblocks;
1518 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1519 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1523 map->m_pblk = ei.blk + pgofs - ei.fofs;
1524 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1525 map->m_flags = F2FS_MAP_MAPPED;
1526 if (map->m_next_extent)
1527 *map->m_next_extent = pgofs + map->m_len;
1529 /* for hardware encryption, but to avoid potential issue in future */
1530 if (flag == F2FS_GET_BLOCK_DIO)
1531 f2fs_wait_on_block_writeback_range(inode,
1532 map->m_pblk, map->m_len);
1537 if (map->m_may_create)
1538 f2fs_do_map_lock(sbi, flag, true);
1540 /* When reading holes, we need its node page */
1541 set_new_dnode(&dn, inode, NULL, NULL, 0);
1542 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1544 if (flag == F2FS_GET_BLOCK_BMAP)
1546 if (err == -ENOENT) {
1548 if (map->m_next_pgofs)
1549 *map->m_next_pgofs =
1550 f2fs_get_next_page_offset(&dn, pgofs);
1551 if (map->m_next_extent)
1552 *map->m_next_extent =
1553 f2fs_get_next_page_offset(&dn, pgofs);
1558 start_pgofs = pgofs;
1560 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1561 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1564 blkaddr = f2fs_data_blkaddr(&dn);
1566 if (__is_valid_data_blkaddr(blkaddr) &&
1567 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1568 err = -EFSCORRUPTED;
1572 if (__is_valid_data_blkaddr(blkaddr)) {
1573 /* use out-place-update for driect IO under LFS mode */
1574 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1575 map->m_may_create) {
1576 err = __allocate_data_block(&dn, map->m_seg_type);
1579 blkaddr = dn.data_blkaddr;
1580 set_inode_flag(inode, FI_APPEND_WRITE);
1584 if (unlikely(f2fs_cp_error(sbi))) {
1588 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1589 if (blkaddr == NULL_ADDR) {
1591 last_ofs_in_node = dn.ofs_in_node;
1594 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1595 flag != F2FS_GET_BLOCK_DIO);
1596 err = __allocate_data_block(&dn,
1599 set_inode_flag(inode, FI_APPEND_WRITE);
1603 map->m_flags |= F2FS_MAP_NEW;
1604 blkaddr = dn.data_blkaddr;
1606 if (flag == F2FS_GET_BLOCK_BMAP) {
1610 if (flag == F2FS_GET_BLOCK_PRECACHE)
1612 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1613 blkaddr == NULL_ADDR) {
1614 if (map->m_next_pgofs)
1615 *map->m_next_pgofs = pgofs + 1;
1618 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1619 /* for defragment case */
1620 if (map->m_next_pgofs)
1621 *map->m_next_pgofs = pgofs + 1;
1627 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1630 if (map->m_len == 0) {
1631 /* preallocated unwritten block should be mapped for fiemap. */
1632 if (blkaddr == NEW_ADDR)
1633 map->m_flags |= F2FS_MAP_UNWRITTEN;
1634 map->m_flags |= F2FS_MAP_MAPPED;
1636 map->m_pblk = blkaddr;
1638 } else if ((map->m_pblk != NEW_ADDR &&
1639 blkaddr == (map->m_pblk + ofs)) ||
1640 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1641 flag == F2FS_GET_BLOCK_PRE_DIO) {
1652 /* preallocate blocks in batch for one dnode page */
1653 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1654 (pgofs == end || dn.ofs_in_node == end_offset)) {
1656 dn.ofs_in_node = ofs_in_node;
1657 err = f2fs_reserve_new_blocks(&dn, prealloc);
1661 map->m_len += dn.ofs_in_node - ofs_in_node;
1662 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1666 dn.ofs_in_node = end_offset;
1671 else if (dn.ofs_in_node < end_offset)
1674 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1675 if (map->m_flags & F2FS_MAP_MAPPED) {
1676 unsigned int ofs = start_pgofs - map->m_lblk;
1678 f2fs_update_extent_cache_range(&dn,
1679 start_pgofs, map->m_pblk + ofs,
1684 f2fs_put_dnode(&dn);
1686 if (map->m_may_create) {
1687 f2fs_do_map_lock(sbi, flag, false);
1688 f2fs_balance_fs(sbi, dn.node_changed);
1694 /* for hardware encryption, but to avoid potential issue in future */
1695 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1696 f2fs_wait_on_block_writeback_range(inode,
1697 map->m_pblk, map->m_len);
1699 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1700 if (map->m_flags & F2FS_MAP_MAPPED) {
1701 unsigned int ofs = start_pgofs - map->m_lblk;
1703 f2fs_update_extent_cache_range(&dn,
1704 start_pgofs, map->m_pblk + ofs,
1707 if (map->m_next_extent)
1708 *map->m_next_extent = pgofs + 1;
1710 f2fs_put_dnode(&dn);
1712 if (map->m_may_create) {
1713 f2fs_do_map_lock(sbi, flag, false);
1714 f2fs_balance_fs(sbi, dn.node_changed);
1717 trace_f2fs_map_blocks(inode, map, err);
1721 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1723 struct f2fs_map_blocks map;
1727 if (pos + len > i_size_read(inode))
1730 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1731 map.m_next_pgofs = NULL;
1732 map.m_next_extent = NULL;
1733 map.m_seg_type = NO_CHECK_TYPE;
1734 map.m_may_create = false;
1735 last_lblk = F2FS_BLK_ALIGN(pos + len);
1737 while (map.m_lblk < last_lblk) {
1738 map.m_len = last_lblk - map.m_lblk;
1739 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1740 if (err || map.m_len == 0)
1742 map.m_lblk += map.m_len;
1747 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1749 return (bytes >> inode->i_blkbits);
1752 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1754 return (blks << inode->i_blkbits);
1757 static int __get_data_block(struct inode *inode, sector_t iblock,
1758 struct buffer_head *bh, int create, int flag,
1759 pgoff_t *next_pgofs, int seg_type, bool may_write)
1761 struct f2fs_map_blocks map;
1764 map.m_lblk = iblock;
1765 map.m_len = bytes_to_blks(inode, bh->b_size);
1766 map.m_next_pgofs = next_pgofs;
1767 map.m_next_extent = NULL;
1768 map.m_seg_type = seg_type;
1769 map.m_may_create = may_write;
1771 err = f2fs_map_blocks(inode, &map, create, flag);
1773 map_bh(bh, inode->i_sb, map.m_pblk);
1774 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1775 bh->b_size = blks_to_bytes(inode, map.m_len);
1780 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1781 struct buffer_head *bh_result, int create)
1783 return __get_data_block(inode, iblock, bh_result, create,
1784 F2FS_GET_BLOCK_DIO, NULL,
1785 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1786 IS_SWAPFILE(inode) ? false : true);
1789 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1790 struct buffer_head *bh_result, int create)
1792 return __get_data_block(inode, iblock, bh_result, create,
1793 F2FS_GET_BLOCK_DIO, NULL,
1794 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1798 static int f2fs_xattr_fiemap(struct inode *inode,
1799 struct fiemap_extent_info *fieinfo)
1801 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1803 struct node_info ni;
1804 __u64 phys = 0, len;
1806 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1809 if (f2fs_has_inline_xattr(inode)) {
1812 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1813 inode->i_ino, false);
1817 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1819 f2fs_put_page(page, 1);
1823 phys = blks_to_bytes(inode, ni.blk_addr);
1824 offset = offsetof(struct f2fs_inode, i_addr) +
1825 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1826 get_inline_xattr_addrs(inode));
1829 len = inline_xattr_size(inode);
1831 f2fs_put_page(page, 1);
1833 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1836 flags |= FIEMAP_EXTENT_LAST;
1838 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1839 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1840 if (err || err == 1)
1845 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1849 err = f2fs_get_node_info(sbi, xnid, &ni);
1851 f2fs_put_page(page, 1);
1855 phys = blks_to_bytes(inode, ni.blk_addr);
1856 len = inode->i_sb->s_blocksize;
1858 f2fs_put_page(page, 1);
1860 flags = FIEMAP_EXTENT_LAST;
1864 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1865 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1868 return (err < 0 ? err : 0);
1871 static loff_t max_inode_blocks(struct inode *inode)
1873 loff_t result = ADDRS_PER_INODE(inode);
1874 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1876 /* two direct node blocks */
1877 result += (leaf_count * 2);
1879 /* two indirect node blocks */
1880 leaf_count *= NIDS_PER_BLOCK;
1881 result += (leaf_count * 2);
1883 /* one double indirect node block */
1884 leaf_count *= NIDS_PER_BLOCK;
1885 result += leaf_count;
1890 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1893 struct f2fs_map_blocks map;
1894 sector_t start_blk, last_blk;
1896 u64 logical = 0, phys = 0, size = 0;
1899 bool compr_cluster = false;
1900 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1902 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1903 ret = f2fs_precache_extents(inode);
1908 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1914 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1915 ret = f2fs_xattr_fiemap(inode, fieinfo);
1919 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1920 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1925 if (bytes_to_blks(inode, len) == 0)
1926 len = blks_to_bytes(inode, 1);
1928 start_blk = bytes_to_blks(inode, start);
1929 last_blk = bytes_to_blks(inode, start + len - 1);
1932 memset(&map, 0, sizeof(map));
1933 map.m_lblk = start_blk;
1934 map.m_len = bytes_to_blks(inode, len);
1935 map.m_next_pgofs = &next_pgofs;
1936 map.m_seg_type = NO_CHECK_TYPE;
1939 map.m_len = cluster_size - 1;
1941 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1946 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1947 start_blk = next_pgofs;
1949 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1950 max_inode_blocks(inode)))
1953 flags |= FIEMAP_EXTENT_LAST;
1957 if (IS_ENCRYPTED(inode))
1958 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1960 ret = fiemap_fill_next_extent(fieinfo, logical,
1962 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1968 if (start_blk > last_blk)
1971 if (compr_cluster) {
1972 compr_cluster = false;
1975 logical = blks_to_bytes(inode, start_blk - 1);
1976 phys = blks_to_bytes(inode, map.m_pblk);
1977 size = blks_to_bytes(inode, cluster_size);
1979 flags |= FIEMAP_EXTENT_ENCODED;
1981 start_blk += cluster_size - 1;
1983 if (start_blk > last_blk)
1989 if (map.m_pblk == COMPRESS_ADDR) {
1990 compr_cluster = true;
1995 logical = blks_to_bytes(inode, start_blk);
1996 phys = blks_to_bytes(inode, map.m_pblk);
1997 size = blks_to_bytes(inode, map.m_len);
1999 if (map.m_flags & F2FS_MAP_UNWRITTEN)
2000 flags = FIEMAP_EXTENT_UNWRITTEN;
2002 start_blk += bytes_to_blks(inode, size);
2006 if (fatal_signal_pending(current))
2014 inode_unlock(inode);
2018 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2020 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2021 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2022 return inode->i_sb->s_maxbytes;
2024 return i_size_read(inode);
2027 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2029 struct f2fs_map_blocks *map,
2030 struct bio **bio_ret,
2031 sector_t *last_block_in_bio,
2034 struct bio *bio = *bio_ret;
2035 const unsigned blocksize = blks_to_bytes(inode, 1);
2036 sector_t block_in_file;
2037 sector_t last_block;
2038 sector_t last_block_in_file;
2042 block_in_file = (sector_t)page_index(page);
2043 last_block = block_in_file + nr_pages;
2044 last_block_in_file = bytes_to_blks(inode,
2045 f2fs_readpage_limit(inode) + blocksize - 1);
2046 if (last_block > last_block_in_file)
2047 last_block = last_block_in_file;
2049 /* just zeroing out page which is beyond EOF */
2050 if (block_in_file >= last_block)
2053 * Map blocks using the previous result first.
2055 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2056 block_in_file > map->m_lblk &&
2057 block_in_file < (map->m_lblk + map->m_len))
2061 * Then do more f2fs_map_blocks() calls until we are
2062 * done with this page.
2064 map->m_lblk = block_in_file;
2065 map->m_len = last_block - block_in_file;
2067 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2071 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2072 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2073 SetPageMappedToDisk(page);
2075 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2076 !cleancache_get_page(page))) {
2077 SetPageUptodate(page);
2081 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2082 DATA_GENERIC_ENHANCE_READ)) {
2083 ret = -EFSCORRUPTED;
2088 zero_user_segment(page, 0, PAGE_SIZE);
2089 if (f2fs_need_verity(inode, page->index) &&
2090 !fsverity_verify_page(page)) {
2094 if (!PageUptodate(page))
2095 SetPageUptodate(page);
2101 * This page will go to BIO. Do we need to send this
2104 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2105 *last_block_in_bio, block_nr) ||
2106 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2108 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2112 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2113 is_readahead ? REQ_RAHEAD : 0, page->index,
2123 * If the page is under writeback, we need to wait for
2124 * its completion to see the correct decrypted data.
2126 f2fs_wait_on_block_writeback(inode, block_nr);
2128 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2129 goto submit_and_realloc;
2131 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2132 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2133 ClearPageError(page);
2134 *last_block_in_bio = block_nr;
2138 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2147 #ifdef CONFIG_F2FS_FS_COMPRESSION
2148 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2149 unsigned nr_pages, sector_t *last_block_in_bio,
2150 bool is_readahead, bool for_write)
2152 struct dnode_of_data dn;
2153 struct inode *inode = cc->inode;
2154 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2155 struct bio *bio = *bio_ret;
2156 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2157 sector_t last_block_in_file;
2158 const unsigned blocksize = blks_to_bytes(inode, 1);
2159 struct decompress_io_ctx *dic = NULL;
2160 struct bio_post_read_ctx *ctx;
2161 bool for_verity = false;
2165 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2167 last_block_in_file = bytes_to_blks(inode,
2168 f2fs_readpage_limit(inode) + blocksize - 1);
2170 /* get rid of pages beyond EOF */
2171 for (i = 0; i < cc->cluster_size; i++) {
2172 struct page *page = cc->rpages[i];
2176 if ((sector_t)page->index >= last_block_in_file) {
2177 zero_user_segment(page, 0, PAGE_SIZE);
2178 if (!PageUptodate(page))
2179 SetPageUptodate(page);
2180 } else if (!PageUptodate(page)) {
2184 cc->rpages[i] = NULL;
2188 /* we are done since all pages are beyond EOF */
2189 if (f2fs_cluster_is_empty(cc))
2192 set_new_dnode(&dn, inode, NULL, NULL, 0);
2193 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2197 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2199 for (i = 1; i < cc->cluster_size; i++) {
2202 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2203 dn.ofs_in_node + i);
2205 if (!__is_valid_data_blkaddr(blkaddr))
2208 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2215 /* nothing to decompress */
2216 if (cc->nr_cpages == 0) {
2221 dic = f2fs_alloc_dic(cc);
2228 * It's possible to enable fsverity on the fly when handling a cluster,
2229 * which requires complicated error handling. Instead of adding more
2230 * complexity, let's give a rule where end_io post-processes fsverity
2231 * per cluster. In order to do that, we need to submit bio, if previous
2232 * bio sets a different post-process policy.
2234 if (fsverity_active(cc->inode)) {
2235 atomic_set(&dic->verity_pages, cc->nr_cpages);
2239 ctx = bio->bi_private;
2240 if (!(ctx->enabled_steps & (1 << STEP_VERITY))) {
2241 __submit_bio(sbi, bio, DATA);
2247 for (i = 0; i < dic->nr_cpages; i++) {
2248 struct page *page = dic->cpages[i];
2251 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2252 dn.ofs_in_node + i + 1);
2254 if (bio && (!page_is_mergeable(sbi, bio,
2255 *last_block_in_bio, blkaddr) ||
2256 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2258 __submit_bio(sbi, bio, DATA);
2263 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2264 is_readahead ? REQ_RAHEAD : 0,
2265 page->index, for_write, for_verity);
2267 unsigned int remained = dic->nr_cpages - i;
2268 bool release = false;
2274 if (!atomic_sub_return(remained,
2275 &dic->verity_pages))
2278 if (!atomic_sub_return(remained,
2279 &dic->pending_pages))
2284 f2fs_decompress_end_io(dic->rpages,
2285 cc->cluster_size, true,
2290 f2fs_put_dnode(&dn);
2296 f2fs_wait_on_block_writeback(inode, blkaddr);
2298 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2299 goto submit_and_realloc;
2301 /* tag STEP_DECOMPRESS to handle IO in wq */
2302 ctx = bio->bi_private;
2303 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2304 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2306 inc_page_count(sbi, F2FS_RD_DATA);
2307 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2308 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2309 ClearPageError(page);
2310 *last_block_in_bio = blkaddr;
2313 f2fs_put_dnode(&dn);
2319 f2fs_put_dnode(&dn);
2321 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2328 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2329 * Major change was from block_size == page_size in f2fs by default.
2331 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2332 * this function ever deviates from doing just read-ahead, it should either
2333 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2336 static int f2fs_mpage_readpages(struct inode *inode,
2337 struct readahead_control *rac, struct page *page)
2339 struct bio *bio = NULL;
2340 sector_t last_block_in_bio = 0;
2341 struct f2fs_map_blocks map;
2342 #ifdef CONFIG_F2FS_FS_COMPRESSION
2343 struct compress_ctx cc = {
2345 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2346 .cluster_size = F2FS_I(inode)->i_cluster_size,
2347 .cluster_idx = NULL_CLUSTER,
2354 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2355 unsigned max_nr_pages = nr_pages;
2357 bool drop_ra = false;
2363 map.m_next_pgofs = NULL;
2364 map.m_next_extent = NULL;
2365 map.m_seg_type = NO_CHECK_TYPE;
2366 map.m_may_create = false;
2369 * Two readahead threads for same address range can cause race condition
2370 * which fragments sequential read IOs. So let's avoid each other.
2372 if (rac && readahead_count(rac)) {
2373 if (READ_ONCE(F2FS_I(inode)->ra_offset) == readahead_index(rac))
2376 WRITE_ONCE(F2FS_I(inode)->ra_offset,
2377 readahead_index(rac));
2380 for (; nr_pages; nr_pages--) {
2382 page = readahead_page(rac);
2383 prefetchw(&page->flags);
2385 f2fs_put_page(page, 1);
2390 #ifdef CONFIG_F2FS_FS_COMPRESSION
2391 if (f2fs_compressed_file(inode)) {
2392 /* there are remained comressed pages, submit them */
2393 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2394 ret = f2fs_read_multi_pages(&cc, &bio,
2397 rac != NULL, false);
2398 f2fs_destroy_compress_ctx(&cc);
2400 goto set_error_page;
2402 ret = f2fs_is_compressed_cluster(inode, page->index);
2404 goto set_error_page;
2406 goto read_single_page;
2408 ret = f2fs_init_compress_ctx(&cc);
2410 goto set_error_page;
2412 f2fs_compress_ctx_add_page(&cc, page);
2419 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2420 &bio, &last_block_in_bio, rac);
2422 #ifdef CONFIG_F2FS_FS_COMPRESSION
2426 zero_user_segment(page, 0, PAGE_SIZE);
2429 #ifdef CONFIG_F2FS_FS_COMPRESSION
2435 #ifdef CONFIG_F2FS_FS_COMPRESSION
2436 if (f2fs_compressed_file(inode)) {
2438 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2439 ret = f2fs_read_multi_pages(&cc, &bio,
2442 rac != NULL, false);
2443 f2fs_destroy_compress_ctx(&cc);
2449 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2451 if (rac && readahead_count(rac) && !drop_ra)
2452 WRITE_ONCE(F2FS_I(inode)->ra_offset, -1);
2456 static int f2fs_read_data_page(struct file *file, struct page *page)
2458 struct inode *inode = page_file_mapping(page)->host;
2461 trace_f2fs_readpage(page, DATA);
2463 if (!f2fs_is_compress_backend_ready(inode)) {
2468 /* If the file has inline data, try to read it directly */
2469 if (f2fs_has_inline_data(inode))
2470 ret = f2fs_read_inline_data(inode, page);
2472 ret = f2fs_mpage_readpages(inode, NULL, page);
2476 static void f2fs_readahead(struct readahead_control *rac)
2478 struct inode *inode = rac->mapping->host;
2480 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2482 if (!f2fs_is_compress_backend_ready(inode))
2485 /* If the file has inline data, skip readpages */
2486 if (f2fs_has_inline_data(inode))
2489 f2fs_mpage_readpages(inode, rac, NULL);
2492 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2494 struct inode *inode = fio->page->mapping->host;
2495 struct page *mpage, *page;
2496 gfp_t gfp_flags = GFP_NOFS;
2498 if (!f2fs_encrypted_file(inode))
2501 page = fio->compressed_page ? fio->compressed_page : fio->page;
2503 /* wait for GCed page writeback via META_MAPPING */
2504 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2506 if (fscrypt_inode_uses_inline_crypto(inode))
2510 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2511 PAGE_SIZE, 0, gfp_flags);
2512 if (IS_ERR(fio->encrypted_page)) {
2513 /* flush pending IOs and wait for a while in the ENOMEM case */
2514 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2515 f2fs_flush_merged_writes(fio->sbi);
2516 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2517 gfp_flags |= __GFP_NOFAIL;
2520 return PTR_ERR(fio->encrypted_page);
2523 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2525 if (PageUptodate(mpage))
2526 memcpy(page_address(mpage),
2527 page_address(fio->encrypted_page), PAGE_SIZE);
2528 f2fs_put_page(mpage, 1);
2533 static inline bool check_inplace_update_policy(struct inode *inode,
2534 struct f2fs_io_info *fio)
2536 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2537 unsigned int policy = SM_I(sbi)->ipu_policy;
2539 if (policy & (0x1 << F2FS_IPU_FORCE))
2541 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2543 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2544 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2546 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2547 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2551 * IPU for rewrite async pages
2553 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2554 fio && fio->op == REQ_OP_WRITE &&
2555 !(fio->op_flags & REQ_SYNC) &&
2556 !IS_ENCRYPTED(inode))
2559 /* this is only set during fdatasync */
2560 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2561 is_inode_flag_set(inode, FI_NEED_IPU))
2564 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2565 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2571 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2573 if (f2fs_is_pinned_file(inode))
2576 /* if this is cold file, we should overwrite to avoid fragmentation */
2577 if (file_is_cold(inode))
2580 return check_inplace_update_policy(inode, fio);
2583 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2585 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2587 if (f2fs_lfs_mode(sbi))
2589 if (S_ISDIR(inode->i_mode))
2591 if (IS_NOQUOTA(inode))
2593 if (f2fs_is_atomic_file(inode))
2596 if (is_cold_data(fio->page))
2598 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2600 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2601 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2607 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2609 struct inode *inode = fio->page->mapping->host;
2611 if (f2fs_should_update_outplace(inode, fio))
2614 return f2fs_should_update_inplace(inode, fio);
2617 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2619 struct page *page = fio->page;
2620 struct inode *inode = page->mapping->host;
2621 struct dnode_of_data dn;
2622 struct extent_info ei = {0,0,0};
2623 struct node_info ni;
2624 bool ipu_force = false;
2627 set_new_dnode(&dn, inode, NULL, NULL, 0);
2628 if (need_inplace_update(fio) &&
2629 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2630 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2632 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2633 DATA_GENERIC_ENHANCE))
2634 return -EFSCORRUPTED;
2637 fio->need_lock = LOCK_DONE;
2641 /* Deadlock due to between page->lock and f2fs_lock_op */
2642 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2645 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2649 fio->old_blkaddr = dn.data_blkaddr;
2651 /* This page is already truncated */
2652 if (fio->old_blkaddr == NULL_ADDR) {
2653 ClearPageUptodate(page);
2654 clear_cold_data(page);
2658 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2659 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2660 DATA_GENERIC_ENHANCE)) {
2661 err = -EFSCORRUPTED;
2665 * If current allocation needs SSR,
2666 * it had better in-place writes for updated data.
2669 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2670 need_inplace_update(fio))) {
2671 err = f2fs_encrypt_one_page(fio);
2675 set_page_writeback(page);
2676 ClearPageError(page);
2677 f2fs_put_dnode(&dn);
2678 if (fio->need_lock == LOCK_REQ)
2679 f2fs_unlock_op(fio->sbi);
2680 err = f2fs_inplace_write_data(fio);
2682 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2683 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2684 if (PageWriteback(page))
2685 end_page_writeback(page);
2687 set_inode_flag(inode, FI_UPDATE_WRITE);
2689 trace_f2fs_do_write_data_page(fio->page, IPU);
2693 if (fio->need_lock == LOCK_RETRY) {
2694 if (!f2fs_trylock_op(fio->sbi)) {
2698 fio->need_lock = LOCK_REQ;
2701 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2705 fio->version = ni.version;
2707 err = f2fs_encrypt_one_page(fio);
2711 set_page_writeback(page);
2712 ClearPageError(page);
2714 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2715 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2717 /* LFS mode write path */
2718 f2fs_outplace_write_data(&dn, fio);
2719 trace_f2fs_do_write_data_page(page, OPU);
2720 set_inode_flag(inode, FI_APPEND_WRITE);
2721 if (page->index == 0)
2722 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2724 f2fs_put_dnode(&dn);
2726 if (fio->need_lock == LOCK_REQ)
2727 f2fs_unlock_op(fio->sbi);
2731 int f2fs_write_single_data_page(struct page *page, int *submitted,
2733 sector_t *last_block,
2734 struct writeback_control *wbc,
2735 enum iostat_type io_type,
2738 struct inode *inode = page->mapping->host;
2739 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2740 loff_t i_size = i_size_read(inode);
2741 const pgoff_t end_index = ((unsigned long long)i_size)
2743 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2744 unsigned offset = 0;
2745 bool need_balance_fs = false;
2747 struct f2fs_io_info fio = {
2749 .ino = inode->i_ino,
2752 .op_flags = wbc_to_write_flags(wbc),
2753 .old_blkaddr = NULL_ADDR,
2755 .encrypted_page = NULL,
2757 .compr_blocks = compr_blocks,
2758 .need_lock = LOCK_RETRY,
2762 .last_block = last_block,
2765 trace_f2fs_writepage(page, DATA);
2767 /* we should bypass data pages to proceed the kworkder jobs */
2768 if (unlikely(f2fs_cp_error(sbi))) {
2769 mapping_set_error(page->mapping, -EIO);
2771 * don't drop any dirty dentry pages for keeping lastest
2772 * directory structure.
2774 if (S_ISDIR(inode->i_mode))
2779 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2782 if (page->index < end_index ||
2783 f2fs_verity_in_progress(inode) ||
2788 * If the offset is out-of-range of file size,
2789 * this page does not have to be written to disk.
2791 offset = i_size & (PAGE_SIZE - 1);
2792 if ((page->index >= end_index + 1) || !offset)
2795 zero_user_segment(page, offset, PAGE_SIZE);
2797 if (f2fs_is_drop_cache(inode))
2799 /* we should not write 0'th page having journal header */
2800 if (f2fs_is_volatile_file(inode) && (!page->index ||
2801 (!wbc->for_reclaim &&
2802 f2fs_available_free_memory(sbi, BASE_CHECK))))
2805 /* Dentry/quota blocks are controlled by checkpoint */
2806 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2808 * We need to wait for node_write to avoid block allocation during
2809 * checkpoint. This can only happen to quota writes which can cause
2810 * the below discard race condition.
2812 if (IS_NOQUOTA(inode))
2813 down_read(&sbi->node_write);
2815 fio.need_lock = LOCK_DONE;
2816 err = f2fs_do_write_data_page(&fio);
2818 if (IS_NOQUOTA(inode))
2819 up_read(&sbi->node_write);
2824 if (!wbc->for_reclaim)
2825 need_balance_fs = true;
2826 else if (has_not_enough_free_secs(sbi, 0, 0))
2829 set_inode_flag(inode, FI_HOT_DATA);
2832 if (f2fs_has_inline_data(inode)) {
2833 err = f2fs_write_inline_data(inode, page);
2838 if (err == -EAGAIN) {
2839 err = f2fs_do_write_data_page(&fio);
2840 if (err == -EAGAIN) {
2841 fio.need_lock = LOCK_REQ;
2842 err = f2fs_do_write_data_page(&fio);
2847 file_set_keep_isize(inode);
2849 spin_lock(&F2FS_I(inode)->i_size_lock);
2850 if (F2FS_I(inode)->last_disk_size < psize)
2851 F2FS_I(inode)->last_disk_size = psize;
2852 spin_unlock(&F2FS_I(inode)->i_size_lock);
2856 if (err && err != -ENOENT)
2860 inode_dec_dirty_pages(inode);
2862 ClearPageUptodate(page);
2863 clear_cold_data(page);
2866 if (wbc->for_reclaim) {
2867 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2868 clear_inode_flag(inode, FI_HOT_DATA);
2869 f2fs_remove_dirty_inode(inode);
2873 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2874 !F2FS_I(inode)->cp_task)
2875 f2fs_balance_fs(sbi, need_balance_fs);
2877 if (unlikely(f2fs_cp_error(sbi))) {
2878 f2fs_submit_merged_write(sbi, DATA);
2879 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2884 *submitted = fio.submitted ? 1 : 0;
2889 redirty_page_for_writepage(wbc, page);
2891 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2892 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2893 * file_write_and_wait_range() will see EIO error, which is critical
2894 * to return value of fsync() followed by atomic_write failure to user.
2896 if (!err || wbc->for_reclaim)
2897 return AOP_WRITEPAGE_ACTIVATE;
2902 static int f2fs_write_data_page(struct page *page,
2903 struct writeback_control *wbc)
2905 #ifdef CONFIG_F2FS_FS_COMPRESSION
2906 struct inode *inode = page->mapping->host;
2908 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2911 if (f2fs_compressed_file(inode)) {
2912 if (f2fs_is_compressed_cluster(inode, page->index)) {
2913 redirty_page_for_writepage(wbc, page);
2914 return AOP_WRITEPAGE_ACTIVATE;
2920 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2921 wbc, FS_DATA_IO, 0);
2925 * This function was copied from write_cche_pages from mm/page-writeback.c.
2926 * The major change is making write step of cold data page separately from
2927 * warm/hot data page.
2929 static int f2fs_write_cache_pages(struct address_space *mapping,
2930 struct writeback_control *wbc,
2931 enum iostat_type io_type)
2934 int done = 0, retry = 0;
2935 struct pagevec pvec;
2936 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2937 struct bio *bio = NULL;
2938 sector_t last_block;
2939 #ifdef CONFIG_F2FS_FS_COMPRESSION
2940 struct inode *inode = mapping->host;
2941 struct compress_ctx cc = {
2943 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2944 .cluster_size = F2FS_I(inode)->i_cluster_size,
2945 .cluster_idx = NULL_CLUSTER,
2951 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2957 pgoff_t end; /* Inclusive */
2959 int range_whole = 0;
2965 pagevec_init(&pvec);
2967 if (get_dirty_pages(mapping->host) <=
2968 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2969 set_inode_flag(mapping->host, FI_HOT_DATA);
2971 clear_inode_flag(mapping->host, FI_HOT_DATA);
2973 if (wbc->range_cyclic) {
2974 index = mapping->writeback_index; /* prev offset */
2977 index = wbc->range_start >> PAGE_SHIFT;
2978 end = wbc->range_end >> PAGE_SHIFT;
2979 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2982 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2983 tag = PAGECACHE_TAG_TOWRITE;
2985 tag = PAGECACHE_TAG_DIRTY;
2988 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2989 tag_pages_for_writeback(mapping, index, end);
2991 while (!done && !retry && (index <= end)) {
2992 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2997 for (i = 0; i < nr_pages; i++) {
2998 struct page *page = pvec.pages[i];
3002 #ifdef CONFIG_F2FS_FS_COMPRESSION
3003 if (f2fs_compressed_file(inode)) {
3004 ret = f2fs_init_compress_ctx(&cc);
3010 if (!f2fs_cluster_can_merge_page(&cc,
3012 ret = f2fs_write_multi_pages(&cc,
3013 &submitted, wbc, io_type);
3019 if (unlikely(f2fs_cp_error(sbi)))
3022 if (f2fs_cluster_is_empty(&cc)) {
3023 void *fsdata = NULL;
3027 ret2 = f2fs_prepare_compress_overwrite(
3029 page->index, &fsdata);
3035 !f2fs_compress_write_end(inode,
3036 fsdata, page->index,
3046 /* give a priority to WB_SYNC threads */
3047 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3048 wbc->sync_mode == WB_SYNC_NONE) {
3052 #ifdef CONFIG_F2FS_FS_COMPRESSION
3055 done_index = page->index;
3059 if (unlikely(page->mapping != mapping)) {
3065 if (!PageDirty(page)) {
3066 /* someone wrote it for us */
3067 goto continue_unlock;
3070 if (PageWriteback(page)) {
3071 if (wbc->sync_mode != WB_SYNC_NONE)
3072 f2fs_wait_on_page_writeback(page,
3075 goto continue_unlock;
3078 if (!clear_page_dirty_for_io(page))
3079 goto continue_unlock;
3081 #ifdef CONFIG_F2FS_FS_COMPRESSION
3082 if (f2fs_compressed_file(inode)) {
3084 f2fs_compress_ctx_add_page(&cc, page);
3088 ret = f2fs_write_single_data_page(page, &submitted,
3089 &bio, &last_block, wbc, io_type, 0);
3090 if (ret == AOP_WRITEPAGE_ACTIVATE)
3092 #ifdef CONFIG_F2FS_FS_COMPRESSION
3095 nwritten += submitted;
3096 wbc->nr_to_write -= submitted;
3098 if (unlikely(ret)) {
3100 * keep nr_to_write, since vfs uses this to
3101 * get # of written pages.
3103 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3106 } else if (ret == -EAGAIN) {
3108 if (wbc->sync_mode == WB_SYNC_ALL) {
3110 congestion_wait(BLK_RW_ASYNC,
3111 DEFAULT_IO_TIMEOUT);
3116 done_index = page->index + 1;
3121 if (wbc->nr_to_write <= 0 &&
3122 wbc->sync_mode == WB_SYNC_NONE) {
3130 pagevec_release(&pvec);
3133 #ifdef CONFIG_F2FS_FS_COMPRESSION
3134 /* flush remained pages in compress cluster */
3135 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3136 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3137 nwritten += submitted;
3138 wbc->nr_to_write -= submitted;
3144 if (f2fs_compressed_file(inode))
3145 f2fs_destroy_compress_ctx(&cc);
3152 if (wbc->range_cyclic && !done)
3154 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3155 mapping->writeback_index = done_index;
3158 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3160 /* submit cached bio of IPU write */
3162 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3167 static inline bool __should_serialize_io(struct inode *inode,
3168 struct writeback_control *wbc)
3170 /* to avoid deadlock in path of data flush */
3171 if (F2FS_I(inode)->cp_task)
3174 if (!S_ISREG(inode->i_mode))
3176 if (IS_NOQUOTA(inode))
3179 if (f2fs_need_compress_data(inode))
3181 if (wbc->sync_mode != WB_SYNC_ALL)
3183 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3188 static int __f2fs_write_data_pages(struct address_space *mapping,
3189 struct writeback_control *wbc,
3190 enum iostat_type io_type)
3192 struct inode *inode = mapping->host;
3193 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3194 struct blk_plug plug;
3196 bool locked = false;
3198 /* deal with chardevs and other special file */
3199 if (!mapping->a_ops->writepage)
3202 /* skip writing if there is no dirty page in this inode */
3203 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3206 /* during POR, we don't need to trigger writepage at all. */
3207 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3210 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3211 wbc->sync_mode == WB_SYNC_NONE &&
3212 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3213 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3216 /* skip writing during file defragment */
3217 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3220 trace_f2fs_writepages(mapping->host, wbc, DATA);
3222 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3223 if (wbc->sync_mode == WB_SYNC_ALL)
3224 atomic_inc(&sbi->wb_sync_req[DATA]);
3225 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3228 if (__should_serialize_io(inode, wbc)) {
3229 mutex_lock(&sbi->writepages);
3233 blk_start_plug(&plug);
3234 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3235 blk_finish_plug(&plug);
3238 mutex_unlock(&sbi->writepages);
3240 if (wbc->sync_mode == WB_SYNC_ALL)
3241 atomic_dec(&sbi->wb_sync_req[DATA]);
3243 * if some pages were truncated, we cannot guarantee its mapping->host
3244 * to detect pending bios.
3247 f2fs_remove_dirty_inode(inode);
3251 wbc->pages_skipped += get_dirty_pages(inode);
3252 trace_f2fs_writepages(mapping->host, wbc, DATA);
3256 static int f2fs_write_data_pages(struct address_space *mapping,
3257 struct writeback_control *wbc)
3259 struct inode *inode = mapping->host;
3261 return __f2fs_write_data_pages(mapping, wbc,
3262 F2FS_I(inode)->cp_task == current ?
3263 FS_CP_DATA_IO : FS_DATA_IO);
3266 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3268 struct inode *inode = mapping->host;
3269 loff_t i_size = i_size_read(inode);
3271 if (IS_NOQUOTA(inode))
3274 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3275 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3276 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3277 down_write(&F2FS_I(inode)->i_mmap_sem);
3279 truncate_pagecache(inode, i_size);
3280 f2fs_truncate_blocks(inode, i_size, true);
3282 up_write(&F2FS_I(inode)->i_mmap_sem);
3283 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3287 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3288 struct page *page, loff_t pos, unsigned len,
3289 block_t *blk_addr, bool *node_changed)
3291 struct inode *inode = page->mapping->host;
3292 pgoff_t index = page->index;
3293 struct dnode_of_data dn;
3295 bool locked = false;
3296 struct extent_info ei = {0,0,0};
3301 * we already allocated all the blocks, so we don't need to get
3302 * the block addresses when there is no need to fill the page.
3304 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3305 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3306 !f2fs_verity_in_progress(inode))
3309 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3310 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3311 flag = F2FS_GET_BLOCK_DEFAULT;
3313 flag = F2FS_GET_BLOCK_PRE_AIO;
3315 if (f2fs_has_inline_data(inode) ||
3316 (pos & PAGE_MASK) >= i_size_read(inode)) {
3317 f2fs_do_map_lock(sbi, flag, true);
3322 /* check inline_data */
3323 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3324 if (IS_ERR(ipage)) {
3325 err = PTR_ERR(ipage);
3329 set_new_dnode(&dn, inode, ipage, ipage, 0);
3331 if (f2fs_has_inline_data(inode)) {
3332 if (pos + len <= MAX_INLINE_DATA(inode)) {
3333 f2fs_do_read_inline_data(page, ipage);
3334 set_inode_flag(inode, FI_DATA_EXIST);
3336 set_inline_node(ipage);
3338 err = f2fs_convert_inline_page(&dn, page);
3341 if (dn.data_blkaddr == NULL_ADDR)
3342 err = f2fs_get_block(&dn, index);
3344 } else if (locked) {
3345 err = f2fs_get_block(&dn, index);
3347 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3348 dn.data_blkaddr = ei.blk + index - ei.fofs;
3351 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3352 if (err || dn.data_blkaddr == NULL_ADDR) {
3353 f2fs_put_dnode(&dn);
3354 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3356 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3363 /* convert_inline_page can make node_changed */
3364 *blk_addr = dn.data_blkaddr;
3365 *node_changed = dn.node_changed;
3367 f2fs_put_dnode(&dn);
3370 f2fs_do_map_lock(sbi, flag, false);
3374 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3375 loff_t pos, unsigned len, unsigned flags,
3376 struct page **pagep, void **fsdata)
3378 struct inode *inode = mapping->host;
3379 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3380 struct page *page = NULL;
3381 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3382 bool need_balance = false, drop_atomic = false;
3383 block_t blkaddr = NULL_ADDR;
3386 trace_f2fs_write_begin(inode, pos, len, flags);
3388 if (!f2fs_is_checkpoint_ready(sbi)) {
3393 if ((f2fs_is_atomic_file(inode) &&
3394 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3395 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3402 * We should check this at this moment to avoid deadlock on inode page
3403 * and #0 page. The locking rule for inline_data conversion should be:
3404 * lock_page(page #0) -> lock_page(inode_page)
3407 err = f2fs_convert_inline_inode(inode);
3412 #ifdef CONFIG_F2FS_FS_COMPRESSION
3413 if (f2fs_compressed_file(inode)) {
3418 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3431 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3432 * wait_for_stable_page. Will wait that below with our IO control.
3434 page = f2fs_pagecache_get_page(mapping, index,
3435 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3441 /* TODO: cluster can be compressed due to race with .writepage */
3445 err = prepare_write_begin(sbi, page, pos, len,
3446 &blkaddr, &need_balance);
3450 if (need_balance && !IS_NOQUOTA(inode) &&
3451 has_not_enough_free_secs(sbi, 0, 0)) {
3453 f2fs_balance_fs(sbi, true);
3455 if (page->mapping != mapping) {
3456 /* The page got truncated from under us */
3457 f2fs_put_page(page, 1);
3462 f2fs_wait_on_page_writeback(page, DATA, false, true);
3464 if (len == PAGE_SIZE || PageUptodate(page))
3467 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3468 !f2fs_verity_in_progress(inode)) {
3469 zero_user_segment(page, len, PAGE_SIZE);
3473 if (blkaddr == NEW_ADDR) {
3474 zero_user_segment(page, 0, PAGE_SIZE);
3475 SetPageUptodate(page);
3477 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3478 DATA_GENERIC_ENHANCE_READ)) {
3479 err = -EFSCORRUPTED;
3482 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3487 if (unlikely(page->mapping != mapping)) {
3488 f2fs_put_page(page, 1);
3491 if (unlikely(!PageUptodate(page))) {
3499 f2fs_put_page(page, 1);
3500 f2fs_write_failed(mapping, pos + len);
3502 f2fs_drop_inmem_pages_all(sbi, false);
3506 static int f2fs_write_end(struct file *file,
3507 struct address_space *mapping,
3508 loff_t pos, unsigned len, unsigned copied,
3509 struct page *page, void *fsdata)
3511 struct inode *inode = page->mapping->host;
3513 trace_f2fs_write_end(inode, pos, len, copied);
3516 * This should be come from len == PAGE_SIZE, and we expect copied
3517 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3518 * let generic_perform_write() try to copy data again through copied=0.
3520 if (!PageUptodate(page)) {
3521 if (unlikely(copied != len))
3524 SetPageUptodate(page);
3527 #ifdef CONFIG_F2FS_FS_COMPRESSION
3528 /* overwrite compressed file */
3529 if (f2fs_compressed_file(inode) && fsdata) {
3530 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3531 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3533 if (pos + copied > i_size_read(inode) &&
3534 !f2fs_verity_in_progress(inode))
3535 f2fs_i_size_write(inode, pos + copied);
3543 set_page_dirty(page);
3545 if (pos + copied > i_size_read(inode) &&
3546 !f2fs_verity_in_progress(inode))
3547 f2fs_i_size_write(inode, pos + copied);
3549 f2fs_put_page(page, 1);
3550 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3554 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3557 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3558 unsigned blkbits = i_blkbits;
3559 unsigned blocksize_mask = (1 << blkbits) - 1;
3560 unsigned long align = offset | iov_iter_alignment(iter);
3561 struct block_device *bdev = inode->i_sb->s_bdev;
3563 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3566 if (align & blocksize_mask) {
3568 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3569 blocksize_mask = (1 << blkbits) - 1;
3570 if (align & blocksize_mask)
3577 static void f2fs_dio_end_io(struct bio *bio)
3579 struct f2fs_private_dio *dio = bio->bi_private;
3581 dec_page_count(F2FS_I_SB(dio->inode),
3582 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3584 bio->bi_private = dio->orig_private;
3585 bio->bi_end_io = dio->orig_end_io;
3592 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3595 struct f2fs_private_dio *dio;
3596 bool write = (bio_op(bio) == REQ_OP_WRITE);
3598 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3599 sizeof(struct f2fs_private_dio), GFP_NOFS);
3604 dio->orig_end_io = bio->bi_end_io;
3605 dio->orig_private = bio->bi_private;
3608 bio->bi_end_io = f2fs_dio_end_io;
3609 bio->bi_private = dio;
3611 inc_page_count(F2FS_I_SB(inode),
3612 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3617 bio->bi_status = BLK_STS_IOERR;
3621 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3623 struct address_space *mapping = iocb->ki_filp->f_mapping;
3624 struct inode *inode = mapping->host;
3625 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3626 struct f2fs_inode_info *fi = F2FS_I(inode);
3627 size_t count = iov_iter_count(iter);
3628 loff_t offset = iocb->ki_pos;
3629 int rw = iov_iter_rw(iter);
3631 enum rw_hint hint = iocb->ki_hint;
3632 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3635 err = check_direct_IO(inode, iter, offset);
3637 return err < 0 ? err : 0;
3639 if (f2fs_force_buffered_io(inode, iocb, iter))
3642 do_opu = allow_outplace_dio(inode, iocb, iter);
3644 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3646 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3647 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3649 if (iocb->ki_flags & IOCB_NOWAIT) {
3650 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3651 iocb->ki_hint = hint;
3655 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3656 up_read(&fi->i_gc_rwsem[rw]);
3657 iocb->ki_hint = hint;
3662 down_read(&fi->i_gc_rwsem[rw]);
3664 down_read(&fi->i_gc_rwsem[READ]);
3667 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3668 iter, rw == WRITE ? get_data_block_dio_write :
3669 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3670 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3674 up_read(&fi->i_gc_rwsem[READ]);
3676 up_read(&fi->i_gc_rwsem[rw]);
3679 if (whint_mode == WHINT_MODE_OFF)
3680 iocb->ki_hint = hint;
3682 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3685 set_inode_flag(inode, FI_UPDATE_WRITE);
3686 } else if (err == -EIOCBQUEUED) {
3687 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3688 count - iov_iter_count(iter));
3689 } else if (err < 0) {
3690 f2fs_write_failed(mapping, offset + count);
3694 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3695 else if (err == -EIOCBQUEUED)
3696 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3697 count - iov_iter_count(iter));
3701 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3706 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3707 unsigned int length)
3709 struct inode *inode = page->mapping->host;
3710 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3712 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3713 (offset % PAGE_SIZE || length != PAGE_SIZE))
3716 if (PageDirty(page)) {
3717 if (inode->i_ino == F2FS_META_INO(sbi)) {
3718 dec_page_count(sbi, F2FS_DIRTY_META);
3719 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3720 dec_page_count(sbi, F2FS_DIRTY_NODES);
3722 inode_dec_dirty_pages(inode);
3723 f2fs_remove_dirty_inode(inode);
3727 clear_cold_data(page);
3729 if (IS_ATOMIC_WRITTEN_PAGE(page))
3730 return f2fs_drop_inmem_page(inode, page);
3732 f2fs_clear_page_private(page);
3735 int f2fs_release_page(struct page *page, gfp_t wait)
3737 /* If this is dirty page, keep PagePrivate */
3738 if (PageDirty(page))
3741 /* This is atomic written page, keep Private */
3742 if (IS_ATOMIC_WRITTEN_PAGE(page))
3745 clear_cold_data(page);
3746 f2fs_clear_page_private(page);
3750 static int f2fs_set_data_page_dirty(struct page *page)
3752 struct inode *inode = page_file_mapping(page)->host;
3754 trace_f2fs_set_page_dirty(page, DATA);
3756 if (!PageUptodate(page))
3757 SetPageUptodate(page);
3758 if (PageSwapCache(page))
3759 return __set_page_dirty_nobuffers(page);
3761 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3762 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3763 f2fs_register_inmem_page(inode, page);
3767 * Previously, this page has been registered, we just
3773 if (!PageDirty(page)) {
3774 __set_page_dirty_nobuffers(page);
3775 f2fs_update_dirty_page(inode, page);
3782 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3784 #ifdef CONFIG_F2FS_FS_COMPRESSION
3785 struct dnode_of_data dn;
3786 sector_t start_idx, blknr = 0;
3789 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3791 set_new_dnode(&dn, inode, NULL, NULL, 0);
3792 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3796 if (dn.data_blkaddr != COMPRESS_ADDR) {
3797 dn.ofs_in_node += block - start_idx;
3798 blknr = f2fs_data_blkaddr(&dn);
3799 if (!__is_valid_data_blkaddr(blknr))
3803 f2fs_put_dnode(&dn);
3811 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3813 struct inode *inode = mapping->host;
3816 if (f2fs_has_inline_data(inode))
3819 /* make sure allocating whole blocks */
3820 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3821 filemap_write_and_wait(mapping);
3823 /* Block number less than F2FS MAX BLOCKS */
3824 if (unlikely(block >= F2FS_I_SB(inode)->max_file_blocks))
3827 if (f2fs_compressed_file(inode)) {
3828 blknr = f2fs_bmap_compress(inode, block);
3830 struct f2fs_map_blocks map;
3832 memset(&map, 0, sizeof(map));
3835 map.m_next_pgofs = NULL;
3836 map.m_seg_type = NO_CHECK_TYPE;
3838 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3842 trace_f2fs_bmap(inode, block, blknr);
3846 #ifdef CONFIG_MIGRATION
3847 #include <linux/migrate.h>
3849 int f2fs_migrate_page(struct address_space *mapping,
3850 struct page *newpage, struct page *page, enum migrate_mode mode)
3852 int rc, extra_count;
3853 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3854 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3856 BUG_ON(PageWriteback(page));
3858 /* migrating an atomic written page is safe with the inmem_lock hold */
3859 if (atomic_written) {
3860 if (mode != MIGRATE_SYNC)
3862 if (!mutex_trylock(&fi->inmem_lock))
3866 /* one extra reference was held for atomic_write page */
3867 extra_count = atomic_written ? 1 : 0;
3868 rc = migrate_page_move_mapping(mapping, newpage,
3870 if (rc != MIGRATEPAGE_SUCCESS) {
3872 mutex_unlock(&fi->inmem_lock);
3876 if (atomic_written) {
3877 struct inmem_pages *cur;
3878 list_for_each_entry(cur, &fi->inmem_pages, list)
3879 if (cur->page == page) {
3880 cur->page = newpage;
3883 mutex_unlock(&fi->inmem_lock);
3888 if (PagePrivate(page)) {
3889 f2fs_set_page_private(newpage, page_private(page));
3890 f2fs_clear_page_private(page);
3893 if (mode != MIGRATE_SYNC_NO_COPY)
3894 migrate_page_copy(newpage, page);
3896 migrate_page_states(newpage, page);
3898 return MIGRATEPAGE_SUCCESS;
3903 static int check_swap_activate_fast(struct swap_info_struct *sis,
3904 struct file *swap_file, sector_t *span)
3906 struct address_space *mapping = swap_file->f_mapping;
3907 struct inode *inode = mapping->host;
3908 sector_t cur_lblock;
3909 sector_t last_lblock;
3911 sector_t lowest_pblock = -1;
3912 sector_t highest_pblock = 0;
3914 unsigned long nr_pblocks;
3919 * Map all the blocks into the extent list. This code doesn't try
3923 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3924 len = i_size_read(inode);
3926 while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3927 struct f2fs_map_blocks map;
3932 memset(&map, 0, sizeof(map));
3933 map.m_lblk = cur_lblock;
3934 map.m_len = bytes_to_blks(inode, len) - cur_lblock;
3935 map.m_next_pgofs = &next_pgofs;
3936 map.m_seg_type = NO_CHECK_TYPE;
3938 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3943 if (!(map.m_flags & F2FS_MAP_FLAGS))
3946 pblock = map.m_pblk;
3947 nr_pblocks = map.m_len;
3949 if (cur_lblock + nr_pblocks >= sis->max)
3950 nr_pblocks = sis->max - cur_lblock;
3952 if (cur_lblock) { /* exclude the header page */
3953 if (pblock < lowest_pblock)
3954 lowest_pblock = pblock;
3955 if (pblock + nr_pblocks - 1 > highest_pblock)
3956 highest_pblock = pblock + nr_pblocks - 1;
3960 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3962 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3966 cur_lblock += nr_pblocks;
3969 *span = 1 + highest_pblock - lowest_pblock;
3970 if (cur_lblock == 0)
3971 cur_lblock = 1; /* force Empty message */
3972 sis->max = cur_lblock;
3973 sis->pages = cur_lblock - 1;
3974 sis->highest_bit = cur_lblock - 1;
3978 pr_err("swapon: swapfile has holes\n");
3982 /* Copied from generic_swapfile_activate() to check any holes */
3983 static int check_swap_activate(struct swap_info_struct *sis,
3984 struct file *swap_file, sector_t *span)
3986 struct address_space *mapping = swap_file->f_mapping;
3987 struct inode *inode = mapping->host;
3988 unsigned blocks_per_page;
3989 unsigned long page_no;
3990 sector_t probe_block;
3991 sector_t last_block;
3992 sector_t lowest_block = -1;
3993 sector_t highest_block = 0;
3997 if (PAGE_SIZE == F2FS_BLKSIZE)
3998 return check_swap_activate_fast(sis, swap_file, span);
4000 blocks_per_page = bytes_to_blks(inode, PAGE_SIZE);
4003 * Map all the blocks into the extent list. This code doesn't try
4008 last_block = bytes_to_blks(inode, i_size_read(inode));
4009 while ((probe_block + blocks_per_page) <= last_block &&
4010 page_no < sis->max) {
4011 unsigned block_in_page;
4012 sector_t first_block;
4018 block = probe_block;
4019 err = bmap(inode, &block);
4022 first_block = block;
4025 * It must be PAGE_SIZE aligned on-disk
4027 if (first_block & (blocks_per_page - 1)) {
4032 for (block_in_page = 1; block_in_page < blocks_per_page;
4035 block = probe_block + block_in_page;
4036 err = bmap(inode, &block);
4041 if (block != first_block + block_in_page) {
4048 first_block >>= (PAGE_SHIFT - inode->i_blkbits);
4049 if (page_no) { /* exclude the header page */
4050 if (first_block < lowest_block)
4051 lowest_block = first_block;
4052 if (first_block > highest_block)
4053 highest_block = first_block;
4057 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4059 ret = add_swap_extent(sis, page_no, 1, first_block);
4064 probe_block += blocks_per_page;
4069 *span = 1 + highest_block - lowest_block;
4071 page_no = 1; /* force Empty message */
4073 sis->pages = page_no - 1;
4074 sis->highest_bit = page_no - 1;
4078 pr_err("swapon: swapfile has holes\n");
4082 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4085 struct inode *inode = file_inode(file);
4088 if (!S_ISREG(inode->i_mode))
4091 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4094 ret = f2fs_convert_inline_inode(inode);
4098 if (!f2fs_disable_compressed_file(inode))
4101 ret = check_swap_activate(sis, file, span);
4105 set_inode_flag(inode, FI_PIN_FILE);
4106 f2fs_precache_extents(inode);
4107 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4111 static void f2fs_swap_deactivate(struct file *file)
4113 struct inode *inode = file_inode(file);
4115 clear_inode_flag(inode, FI_PIN_FILE);
4118 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4124 static void f2fs_swap_deactivate(struct file *file)
4129 const struct address_space_operations f2fs_dblock_aops = {
4130 .readpage = f2fs_read_data_page,
4131 .readahead = f2fs_readahead,
4132 .writepage = f2fs_write_data_page,
4133 .writepages = f2fs_write_data_pages,
4134 .write_begin = f2fs_write_begin,
4135 .write_end = f2fs_write_end,
4136 .set_page_dirty = f2fs_set_data_page_dirty,
4137 .invalidatepage = f2fs_invalidate_page,
4138 .releasepage = f2fs_release_page,
4139 .direct_IO = f2fs_direct_IO,
4141 .swap_activate = f2fs_swap_activate,
4142 .swap_deactivate = f2fs_swap_deactivate,
4143 #ifdef CONFIG_MIGRATION
4144 .migratepage = f2fs_migrate_page,
4148 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4150 struct address_space *mapping = page_mapping(page);
4151 unsigned long flags;
4153 xa_lock_irqsave(&mapping->i_pages, flags);
4154 __xa_clear_mark(&mapping->i_pages, page_index(page),
4155 PAGECACHE_TAG_DIRTY);
4156 xa_unlock_irqrestore(&mapping->i_pages, flags);
4159 int __init f2fs_init_post_read_processing(void)
4161 bio_post_read_ctx_cache =
4162 kmem_cache_create("f2fs_bio_post_read_ctx",
4163 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4164 if (!bio_post_read_ctx_cache)
4166 bio_post_read_ctx_pool =
4167 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4168 bio_post_read_ctx_cache);
4169 if (!bio_post_read_ctx_pool)
4170 goto fail_free_cache;
4174 kmem_cache_destroy(bio_post_read_ctx_cache);
4179 void f2fs_destroy_post_read_processing(void)
4181 mempool_destroy(bio_post_read_ctx_pool);
4182 kmem_cache_destroy(bio_post_read_ctx_cache);
4185 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4187 if (!f2fs_sb_has_encrypt(sbi) &&
4188 !f2fs_sb_has_verity(sbi) &&
4189 !f2fs_sb_has_compression(sbi))
4192 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4193 WQ_UNBOUND | WQ_HIGHPRI,
4195 if (!sbi->post_read_wq)
4200 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4202 if (sbi->post_read_wq)
4203 destroy_workqueue(sbi->post_read_wq);
4206 int __init f2fs_init_bio_entry_cache(void)
4208 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4209 sizeof(struct bio_entry));
4210 if (!bio_entry_slab)
4215 void f2fs_destroy_bio_entry_cache(void)
4217 kmem_cache_destroy(bio_entry_slab);