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/sched/mm.h>
12 #include <linux/mpage.h>
13 #include <linux/writeback.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/sched/signal.h>
22 #include <linux/fiemap.h>
23 #include <linux/iomap.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 bool __is_cp_guaranteed(struct page *page)
55 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi;
62 inode = mapping->host;
63 sbi = F2FS_I_SB(inode);
65 if (inode->i_ino == F2FS_META_INO(sbi) ||
66 inode->i_ino == F2FS_NODE_INO(sbi) ||
67 S_ISDIR(inode->i_mode))
70 if (f2fs_is_compressed_page(page))
72 if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
73 page_private_gcing(page))
78 static enum count_type __read_io_type(struct page *page)
80 struct address_space *mapping = page_file_mapping(page);
83 struct inode *inode = mapping->host;
84 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
86 if (inode->i_ino == F2FS_META_INO(sbi))
89 if (inode->i_ino == F2FS_NODE_INO(sbi))
95 /* postprocessing steps for read bios */
96 enum bio_post_read_step {
97 #ifdef CONFIG_FS_ENCRYPTION
98 STEP_DECRYPT = 1 << 0,
100 STEP_DECRYPT = 0, /* compile out the decryption-related code */
102 #ifdef CONFIG_F2FS_FS_COMPRESSION
103 STEP_DECOMPRESS = 1 << 1,
105 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
107 #ifdef CONFIG_FS_VERITY
108 STEP_VERITY = 1 << 2,
110 STEP_VERITY = 0, /* compile out the verity-related code */
114 struct bio_post_read_ctx {
116 struct f2fs_sb_info *sbi;
117 struct work_struct work;
118 unsigned int enabled_steps;
122 static void f2fs_finish_read_bio(struct bio *bio)
125 struct bvec_iter_all iter_all;
128 * Update and unlock the bio's pagecache pages, and put the
129 * decompression context for any compressed pages.
131 bio_for_each_segment_all(bv, bio, iter_all) {
132 struct page *page = bv->bv_page;
134 if (f2fs_is_compressed_page(page)) {
136 f2fs_end_read_compressed_page(page, true, 0);
137 f2fs_put_page_dic(page);
141 /* PG_error was set if decryption or verity failed. */
142 if (bio->bi_status || PageError(page)) {
143 ClearPageUptodate(page);
144 /* will re-read again later */
145 ClearPageError(page);
147 SetPageUptodate(page);
149 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
154 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
158 static void f2fs_verify_bio(struct work_struct *work)
160 struct bio_post_read_ctx *ctx =
161 container_of(work, struct bio_post_read_ctx, work);
162 struct bio *bio = ctx->bio;
163 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
166 * fsverity_verify_bio() may call readahead() again, and while verity
167 * will be disabled for this, decryption and/or decompression may still
168 * be needed, resulting in another bio_post_read_ctx being allocated.
169 * So to prevent deadlocks we need to release the current ctx to the
170 * mempool first. This assumes that verity is the last post-read step.
172 mempool_free(ctx, bio_post_read_ctx_pool);
173 bio->bi_private = NULL;
176 * Verify the bio's pages with fs-verity. Exclude compressed pages,
177 * as those were handled separately by f2fs_end_read_compressed_page().
179 if (may_have_compressed_pages) {
181 struct bvec_iter_all iter_all;
183 bio_for_each_segment_all(bv, bio, iter_all) {
184 struct page *page = bv->bv_page;
186 if (!f2fs_is_compressed_page(page) &&
187 !PageError(page) && !fsverity_verify_page(page))
191 fsverity_verify_bio(bio);
194 f2fs_finish_read_bio(bio);
198 * If the bio's data needs to be verified with fs-verity, then enqueue the
199 * verity work for the bio. Otherwise finish the bio now.
201 * Note that to avoid deadlocks, the verity work can't be done on the
202 * decryption/decompression workqueue. This is because verifying the data pages
203 * can involve reading verity metadata pages from the file, and these verity
204 * metadata pages may be encrypted and/or compressed.
206 static void f2fs_verify_and_finish_bio(struct bio *bio)
208 struct bio_post_read_ctx *ctx = bio->bi_private;
210 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
211 INIT_WORK(&ctx->work, f2fs_verify_bio);
212 fsverity_enqueue_verify_work(&ctx->work);
214 f2fs_finish_read_bio(bio);
219 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
220 * remaining page was read by @ctx->bio.
222 * Note that a bio may span clusters (even a mix of compressed and uncompressed
223 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
224 * that the bio includes at least one compressed page. The actual decompression
225 * is done on a per-cluster basis, not a per-bio basis.
227 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
230 struct bvec_iter_all iter_all;
231 bool all_compressed = true;
232 block_t blkaddr = ctx->fs_blkaddr;
234 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
235 struct page *page = bv->bv_page;
237 /* PG_error was set if decryption failed. */
238 if (f2fs_is_compressed_page(page))
239 f2fs_end_read_compressed_page(page, PageError(page),
242 all_compressed = false;
248 * Optimization: if all the bio's pages are compressed, then scheduling
249 * the per-bio verity work is unnecessary, as verity will be fully
250 * handled at the compression cluster level.
253 ctx->enabled_steps &= ~STEP_VERITY;
256 static void f2fs_post_read_work(struct work_struct *work)
258 struct bio_post_read_ctx *ctx =
259 container_of(work, struct bio_post_read_ctx, work);
261 if (ctx->enabled_steps & STEP_DECRYPT)
262 fscrypt_decrypt_bio(ctx->bio);
264 if (ctx->enabled_steps & STEP_DECOMPRESS)
265 f2fs_handle_step_decompress(ctx);
267 f2fs_verify_and_finish_bio(ctx->bio);
270 static void f2fs_read_end_io(struct bio *bio)
272 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
273 struct bio_post_read_ctx *ctx;
275 iostat_update_and_unbind_ctx(bio, 0);
276 ctx = bio->bi_private;
278 if (time_to_inject(sbi, FAULT_READ_IO)) {
279 f2fs_show_injection_info(sbi, FAULT_READ_IO);
280 bio->bi_status = BLK_STS_IOERR;
283 if (bio->bi_status) {
284 f2fs_finish_read_bio(bio);
288 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
289 INIT_WORK(&ctx->work, f2fs_post_read_work);
290 queue_work(ctx->sbi->post_read_wq, &ctx->work);
292 f2fs_verify_and_finish_bio(bio);
296 static void f2fs_write_end_io(struct bio *bio)
298 struct f2fs_sb_info *sbi;
299 struct bio_vec *bvec;
300 struct bvec_iter_all iter_all;
302 iostat_update_and_unbind_ctx(bio, 1);
303 sbi = bio->bi_private;
305 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
306 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
307 bio->bi_status = BLK_STS_IOERR;
310 bio_for_each_segment_all(bvec, bio, iter_all) {
311 struct page *page = bvec->bv_page;
312 enum count_type type = WB_DATA_TYPE(page);
314 if (page_private_dummy(page)) {
315 clear_page_private_dummy(page);
317 mempool_free(page, sbi->write_io_dummy);
319 if (unlikely(bio->bi_status))
320 f2fs_stop_checkpoint(sbi, true);
324 fscrypt_finalize_bounce_page(&page);
326 #ifdef CONFIG_F2FS_FS_COMPRESSION
327 if (f2fs_is_compressed_page(page)) {
328 f2fs_compress_write_end_io(bio, page);
333 if (unlikely(bio->bi_status)) {
334 mapping_set_error(page->mapping, -EIO);
335 if (type == F2FS_WB_CP_DATA)
336 f2fs_stop_checkpoint(sbi, true);
339 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
340 page->index != nid_of_node(page));
342 dec_page_count(sbi, type);
343 if (f2fs_in_warm_node_list(sbi, page))
344 f2fs_del_fsync_node_entry(sbi, page);
345 clear_page_private_gcing(page);
346 end_page_writeback(page);
348 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
349 wq_has_sleeper(&sbi->cp_wait))
350 wake_up(&sbi->cp_wait);
355 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
356 block_t blk_addr, sector_t *sector)
358 struct block_device *bdev = sbi->sb->s_bdev;
361 if (f2fs_is_multi_device(sbi)) {
362 for (i = 0; i < sbi->s_ndevs; i++) {
363 if (FDEV(i).start_blk <= blk_addr &&
364 FDEV(i).end_blk >= blk_addr) {
365 blk_addr -= FDEV(i).start_blk;
373 *sector = SECTOR_FROM_BLOCK(blk_addr);
377 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
381 if (!f2fs_is_multi_device(sbi))
384 for (i = 0; i < sbi->s_ndevs; i++)
385 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
390 static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio)
392 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
393 unsigned int fua_flag, meta_flag, io_flag;
394 blk_opf_t op_flags = 0;
396 if (fio->op != REQ_OP_WRITE)
398 if (fio->type == DATA)
399 io_flag = fio->sbi->data_io_flag;
400 else if (fio->type == NODE)
401 io_flag = fio->sbi->node_io_flag;
405 fua_flag = io_flag & temp_mask;
406 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
409 * data/node io flag bits per temp:
410 * REQ_META | REQ_FUA |
411 * 5 | 4 | 3 | 2 | 1 | 0 |
412 * Cold | Warm | Hot | Cold | Warm | Hot |
414 if ((1 << fio->temp) & meta_flag)
415 op_flags |= REQ_META;
416 if ((1 << fio->temp) & fua_flag)
421 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
423 struct f2fs_sb_info *sbi = fio->sbi;
424 struct block_device *bdev;
428 bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or);
429 bio = bio_alloc_bioset(bdev, npages,
430 fio->op | fio->op_flags | f2fs_io_flags(fio),
431 GFP_NOIO, &f2fs_bioset);
432 bio->bi_iter.bi_sector = sector;
433 if (is_read_io(fio->op)) {
434 bio->bi_end_io = f2fs_read_end_io;
435 bio->bi_private = NULL;
437 bio->bi_end_io = f2fs_write_end_io;
438 bio->bi_private = sbi;
440 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
443 wbc_init_bio(fio->io_wbc, bio);
448 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
450 const struct f2fs_io_info *fio,
454 * The f2fs garbage collector sets ->encrypted_page when it wants to
455 * read/write raw data without encryption.
457 if (!fio || !fio->encrypted_page)
458 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
461 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
463 const struct f2fs_io_info *fio)
466 * The f2fs garbage collector sets ->encrypted_page when it wants to
467 * read/write raw data without encryption.
469 if (fio && fio->encrypted_page)
470 return !bio_has_crypt_ctx(bio);
472 return fscrypt_mergeable_bio(bio, inode, next_idx);
475 static inline void __submit_bio(struct f2fs_sb_info *sbi,
476 struct bio *bio, enum page_type type)
478 if (!is_read_io(bio_op(bio))) {
481 if (type != DATA && type != NODE)
484 if (f2fs_lfs_mode(sbi) && current->plug)
485 blk_finish_plug(current->plug);
487 if (!F2FS_IO_ALIGNED(sbi))
490 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
491 start %= F2FS_IO_SIZE(sbi);
496 /* fill dummy pages */
497 for (; start < F2FS_IO_SIZE(sbi); start++) {
499 mempool_alloc(sbi->write_io_dummy,
500 GFP_NOIO | __GFP_NOFAIL);
501 f2fs_bug_on(sbi, !page);
505 zero_user_segment(page, 0, PAGE_SIZE);
506 set_page_private_dummy(page);
508 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
512 * In the NODE case, we lose next block address chain. So, we
513 * need to do checkpoint in f2fs_sync_file.
516 set_sbi_flag(sbi, SBI_NEED_CP);
519 if (is_read_io(bio_op(bio)))
520 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
522 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
524 iostat_update_submit_ctx(bio, type);
528 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
529 struct bio *bio, enum page_type type)
531 __submit_bio(sbi, bio, type);
534 static void __submit_merged_bio(struct f2fs_bio_info *io)
536 struct f2fs_io_info *fio = &io->fio;
541 if (is_read_io(fio->op))
542 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
544 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
546 __submit_bio(io->sbi, io->bio, fio->type);
550 static bool __has_merged_page(struct bio *bio, struct inode *inode,
551 struct page *page, nid_t ino)
553 struct bio_vec *bvec;
554 struct bvec_iter_all iter_all;
559 if (!inode && !page && !ino)
562 bio_for_each_segment_all(bvec, bio, iter_all) {
563 struct page *target = bvec->bv_page;
565 if (fscrypt_is_bounce_page(target)) {
566 target = fscrypt_pagecache_page(target);
570 if (f2fs_is_compressed_page(target)) {
571 target = f2fs_compress_control_page(target);
576 if (inode && inode == target->mapping->host)
578 if (page && page == target)
580 if (ino && ino == ino_of_node(target))
587 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
591 for (i = 0; i < NR_PAGE_TYPE; i++) {
592 int n = (i == META) ? 1 : NR_TEMP_TYPE;
595 sbi->write_io[i] = f2fs_kmalloc(sbi,
596 array_size(n, sizeof(struct f2fs_bio_info)),
598 if (!sbi->write_io[i])
601 for (j = HOT; j < n; j++) {
602 init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem);
603 sbi->write_io[i][j].sbi = sbi;
604 sbi->write_io[i][j].bio = NULL;
605 spin_lock_init(&sbi->write_io[i][j].io_lock);
606 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
607 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
608 init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock);
615 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
616 enum page_type type, enum temp_type temp)
618 enum page_type btype = PAGE_TYPE_OF_BIO(type);
619 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
621 f2fs_down_write(&io->io_rwsem);
623 /* change META to META_FLUSH in the checkpoint procedure */
624 if (type >= META_FLUSH) {
625 io->fio.type = META_FLUSH;
626 io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
627 if (!test_opt(sbi, NOBARRIER))
628 io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
630 __submit_merged_bio(io);
631 f2fs_up_write(&io->io_rwsem);
634 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
635 struct inode *inode, struct page *page,
636 nid_t ino, enum page_type type, bool force)
641 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
643 enum page_type btype = PAGE_TYPE_OF_BIO(type);
644 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
646 f2fs_down_read(&io->io_rwsem);
647 ret = __has_merged_page(io->bio, inode, page, ino);
648 f2fs_up_read(&io->io_rwsem);
651 __f2fs_submit_merged_write(sbi, type, temp);
653 /* TODO: use HOT temp only for meta pages now. */
659 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
661 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
664 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
665 struct inode *inode, struct page *page,
666 nid_t ino, enum page_type type)
668 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
671 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
673 f2fs_submit_merged_write(sbi, DATA);
674 f2fs_submit_merged_write(sbi, NODE);
675 f2fs_submit_merged_write(sbi, META);
679 * Fill the locked page with data located in the block address.
680 * A caller needs to unlock the page on failure.
682 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
685 struct page *page = fio->encrypted_page ?
686 fio->encrypted_page : fio->page;
688 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
689 fio->is_por ? META_POR : (__is_meta_io(fio) ?
690 META_GENERIC : DATA_GENERIC_ENHANCE)))
691 return -EFSCORRUPTED;
693 trace_f2fs_submit_page_bio(page, fio);
695 /* Allocate a new bio */
696 bio = __bio_alloc(fio, 1);
698 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
699 fio->page->index, fio, GFP_NOIO);
701 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
706 if (fio->io_wbc && !is_read_io(fio->op))
707 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
709 inc_page_count(fio->sbi, is_read_io(fio->op) ?
710 __read_io_type(page): WB_DATA_TYPE(fio->page));
712 __submit_bio(fio->sbi, bio, fio->type);
716 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
717 block_t last_blkaddr, block_t cur_blkaddr)
719 if (unlikely(sbi->max_io_bytes &&
720 bio->bi_iter.bi_size >= sbi->max_io_bytes))
722 if (last_blkaddr + 1 != cur_blkaddr)
724 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
727 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
728 struct f2fs_io_info *fio)
730 if (io->fio.op != fio->op)
732 return io->fio.op_flags == fio->op_flags;
735 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
736 struct f2fs_bio_info *io,
737 struct f2fs_io_info *fio,
738 block_t last_blkaddr,
741 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
742 unsigned int filled_blocks =
743 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
744 unsigned int io_size = F2FS_IO_SIZE(sbi);
745 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
747 /* IOs in bio is aligned and left space of vectors is not enough */
748 if (!(filled_blocks % io_size) && left_vecs < io_size)
751 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
753 return io_type_is_mergeable(io, fio);
756 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
757 struct page *page, enum temp_type temp)
759 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
760 struct bio_entry *be;
762 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
766 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
769 f2fs_down_write(&io->bio_list_lock);
770 list_add_tail(&be->list, &io->bio_list);
771 f2fs_up_write(&io->bio_list_lock);
774 static void del_bio_entry(struct bio_entry *be)
777 kmem_cache_free(bio_entry_slab, be);
780 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
783 struct f2fs_sb_info *sbi = fio->sbi;
788 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
789 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
790 struct list_head *head = &io->bio_list;
791 struct bio_entry *be;
793 f2fs_down_write(&io->bio_list_lock);
794 list_for_each_entry(be, head, list) {
800 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
803 if (f2fs_crypt_mergeable_bio(*bio,
804 fio->page->mapping->host,
805 fio->page->index, fio) &&
806 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
812 /* page can't be merged into bio; submit the bio */
814 __submit_bio(sbi, *bio, DATA);
817 f2fs_up_write(&io->bio_list_lock);
828 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
829 struct bio **bio, struct page *page)
833 struct bio *target = bio ? *bio : NULL;
835 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
836 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
837 struct list_head *head = &io->bio_list;
838 struct bio_entry *be;
840 if (list_empty(head))
843 f2fs_down_read(&io->bio_list_lock);
844 list_for_each_entry(be, head, list) {
846 found = (target == be->bio);
848 found = __has_merged_page(be->bio, NULL,
853 f2fs_up_read(&io->bio_list_lock);
860 f2fs_down_write(&io->bio_list_lock);
861 list_for_each_entry(be, head, list) {
863 found = (target == be->bio);
865 found = __has_merged_page(be->bio, NULL,
873 f2fs_up_write(&io->bio_list_lock);
877 __submit_bio(sbi, target, DATA);
884 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
886 struct bio *bio = *fio->bio;
887 struct page *page = fio->encrypted_page ?
888 fio->encrypted_page : fio->page;
890 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
891 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
892 return -EFSCORRUPTED;
894 trace_f2fs_submit_page_bio(page, fio);
896 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
898 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
901 bio = __bio_alloc(fio, BIO_MAX_VECS);
902 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
903 fio->page->index, fio, GFP_NOIO);
905 add_bio_entry(fio->sbi, bio, page, fio->temp);
907 if (add_ipu_page(fio, &bio, page))
912 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
914 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
916 *fio->last_block = fio->new_blkaddr;
922 void f2fs_submit_page_write(struct f2fs_io_info *fio)
924 struct f2fs_sb_info *sbi = fio->sbi;
925 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
926 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
927 struct page *bio_page;
929 f2fs_bug_on(sbi, is_read_io(fio->op));
931 f2fs_down_write(&io->io_rwsem);
934 spin_lock(&io->io_lock);
935 if (list_empty(&io->io_list)) {
936 spin_unlock(&io->io_lock);
939 fio = list_first_entry(&io->io_list,
940 struct f2fs_io_info, list);
941 list_del(&fio->list);
942 spin_unlock(&io->io_lock);
945 verify_fio_blkaddr(fio);
947 if (fio->encrypted_page)
948 bio_page = fio->encrypted_page;
949 else if (fio->compressed_page)
950 bio_page = fio->compressed_page;
952 bio_page = fio->page;
954 /* set submitted = true as a return value */
955 fio->submitted = true;
957 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
960 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
962 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
963 bio_page->index, fio)))
964 __submit_merged_bio(io);
966 if (io->bio == NULL) {
967 if (F2FS_IO_ALIGNED(sbi) &&
968 (fio->type == DATA || fio->type == NODE) &&
969 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
970 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
974 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
975 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
976 bio_page->index, fio, GFP_NOIO);
980 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
981 __submit_merged_bio(io);
986 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
988 io->last_block_in_bio = fio->new_blkaddr;
990 trace_f2fs_submit_page_write(fio->page, fio);
995 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
996 !f2fs_is_checkpoint_ready(sbi))
997 __submit_merged_bio(io);
998 f2fs_up_write(&io->io_rwsem);
1001 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1002 unsigned nr_pages, blk_opf_t op_flag,
1003 pgoff_t first_idx, bool for_write)
1005 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1007 struct bio_post_read_ctx *ctx = NULL;
1008 unsigned int post_read_steps = 0;
1010 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or);
1012 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
1013 REQ_OP_READ | op_flag,
1014 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
1016 return ERR_PTR(-ENOMEM);
1017 bio->bi_iter.bi_sector = sector;
1018 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1019 bio->bi_end_io = f2fs_read_end_io;
1021 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1022 post_read_steps |= STEP_DECRYPT;
1024 if (f2fs_need_verity(inode, first_idx))
1025 post_read_steps |= STEP_VERITY;
1028 * STEP_DECOMPRESS is handled specially, since a compressed file might
1029 * contain both compressed and uncompressed clusters. We'll allocate a
1030 * bio_post_read_ctx if the file is compressed, but the caller is
1031 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1034 if (post_read_steps || f2fs_compressed_file(inode)) {
1035 /* Due to the mempool, this never fails. */
1036 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1039 ctx->enabled_steps = post_read_steps;
1040 ctx->fs_blkaddr = blkaddr;
1041 bio->bi_private = ctx;
1043 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1048 /* This can handle encryption stuffs */
1049 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1050 block_t blkaddr, blk_opf_t op_flags,
1053 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1056 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1057 page->index, for_write);
1059 return PTR_ERR(bio);
1061 /* wait for GCed page writeback via META_MAPPING */
1062 f2fs_wait_on_block_writeback(inode, blkaddr);
1064 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1068 ClearPageError(page);
1069 inc_page_count(sbi, F2FS_RD_DATA);
1070 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1071 __submit_bio(sbi, bio, DATA);
1075 static void __set_data_blkaddr(struct dnode_of_data *dn)
1077 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1081 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1082 base = get_extra_isize(dn->inode);
1084 /* Get physical address of data block */
1085 addr_array = blkaddr_in_node(rn);
1086 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1090 * Lock ordering for the change of data block address:
1093 * update block addresses in the node page
1095 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1097 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1098 __set_data_blkaddr(dn);
1099 if (set_page_dirty(dn->node_page))
1100 dn->node_changed = true;
1103 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1105 dn->data_blkaddr = blkaddr;
1106 f2fs_set_data_blkaddr(dn);
1107 f2fs_update_extent_cache(dn);
1110 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1111 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1113 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1119 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1121 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1124 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1125 dn->ofs_in_node, count);
1127 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1129 for (; count > 0; dn->ofs_in_node++) {
1130 block_t blkaddr = f2fs_data_blkaddr(dn);
1132 if (blkaddr == NULL_ADDR) {
1133 dn->data_blkaddr = NEW_ADDR;
1134 __set_data_blkaddr(dn);
1139 if (set_page_dirty(dn->node_page))
1140 dn->node_changed = true;
1144 /* Should keep dn->ofs_in_node unchanged */
1145 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1147 unsigned int ofs_in_node = dn->ofs_in_node;
1150 ret = f2fs_reserve_new_blocks(dn, 1);
1151 dn->ofs_in_node = ofs_in_node;
1155 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1157 bool need_put = dn->inode_page ? false : true;
1160 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1164 if (dn->data_blkaddr == NULL_ADDR)
1165 err = f2fs_reserve_new_block(dn);
1166 if (err || need_put)
1171 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1173 struct extent_info ei = {0, };
1174 struct inode *inode = dn->inode;
1176 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1177 dn->data_blkaddr = ei.blk + index - ei.fofs;
1181 return f2fs_reserve_block(dn, index);
1184 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1185 blk_opf_t op_flags, bool for_write)
1187 struct address_space *mapping = inode->i_mapping;
1188 struct dnode_of_data dn;
1190 struct extent_info ei = {0, };
1193 page = f2fs_grab_cache_page(mapping, index, for_write);
1195 return ERR_PTR(-ENOMEM);
1197 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1198 dn.data_blkaddr = ei.blk + index - ei.fofs;
1199 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1200 DATA_GENERIC_ENHANCE_READ)) {
1201 err = -EFSCORRUPTED;
1207 set_new_dnode(&dn, inode, NULL, NULL, 0);
1208 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1211 f2fs_put_dnode(&dn);
1213 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1217 if (dn.data_blkaddr != NEW_ADDR &&
1218 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1220 DATA_GENERIC_ENHANCE)) {
1221 err = -EFSCORRUPTED;
1225 if (PageUptodate(page)) {
1231 * A new dentry page is allocated but not able to be written, since its
1232 * new inode page couldn't be allocated due to -ENOSPC.
1233 * In such the case, its blkaddr can be remained as NEW_ADDR.
1234 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1235 * f2fs_init_inode_metadata.
1237 if (dn.data_blkaddr == NEW_ADDR) {
1238 zero_user_segment(page, 0, PAGE_SIZE);
1239 if (!PageUptodate(page))
1240 SetPageUptodate(page);
1245 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1246 op_flags, for_write);
1252 f2fs_put_page(page, 1);
1253 return ERR_PTR(err);
1256 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1258 struct address_space *mapping = inode->i_mapping;
1261 page = find_get_page(mapping, index);
1262 if (page && PageUptodate(page))
1264 f2fs_put_page(page, 0);
1266 page = f2fs_get_read_data_page(inode, index, 0, false);
1270 if (PageUptodate(page))
1273 wait_on_page_locked(page);
1274 if (unlikely(!PageUptodate(page))) {
1275 f2fs_put_page(page, 0);
1276 return ERR_PTR(-EIO);
1282 * If it tries to access a hole, return an error.
1283 * Because, the callers, functions in dir.c and GC, should be able to know
1284 * whether this page exists or not.
1286 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1289 struct address_space *mapping = inode->i_mapping;
1292 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1296 /* wait for read completion */
1298 if (unlikely(page->mapping != mapping)) {
1299 f2fs_put_page(page, 1);
1302 if (unlikely(!PageUptodate(page))) {
1303 f2fs_put_page(page, 1);
1304 return ERR_PTR(-EIO);
1310 * Caller ensures that this data page is never allocated.
1311 * A new zero-filled data page is allocated in the page cache.
1313 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1315 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1316 * ipage should be released by this function.
1318 struct page *f2fs_get_new_data_page(struct inode *inode,
1319 struct page *ipage, pgoff_t index, bool new_i_size)
1321 struct address_space *mapping = inode->i_mapping;
1323 struct dnode_of_data dn;
1326 page = f2fs_grab_cache_page(mapping, index, true);
1329 * before exiting, we should make sure ipage will be released
1330 * if any error occur.
1332 f2fs_put_page(ipage, 1);
1333 return ERR_PTR(-ENOMEM);
1336 set_new_dnode(&dn, inode, ipage, NULL, 0);
1337 err = f2fs_reserve_block(&dn, index);
1339 f2fs_put_page(page, 1);
1340 return ERR_PTR(err);
1343 f2fs_put_dnode(&dn);
1345 if (PageUptodate(page))
1348 if (dn.data_blkaddr == NEW_ADDR) {
1349 zero_user_segment(page, 0, PAGE_SIZE);
1350 if (!PageUptodate(page))
1351 SetPageUptodate(page);
1353 f2fs_put_page(page, 1);
1355 /* if ipage exists, blkaddr should be NEW_ADDR */
1356 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1357 page = f2fs_get_lock_data_page(inode, index, true);
1362 if (new_i_size && i_size_read(inode) <
1363 ((loff_t)(index + 1) << PAGE_SHIFT))
1364 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1368 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1370 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1371 struct f2fs_summary sum;
1372 struct node_info ni;
1373 block_t old_blkaddr;
1377 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1380 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
1384 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1385 if (dn->data_blkaddr != NULL_ADDR)
1388 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1392 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1393 old_blkaddr = dn->data_blkaddr;
1394 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1395 &sum, seg_type, NULL);
1396 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1397 invalidate_mapping_pages(META_MAPPING(sbi),
1398 old_blkaddr, old_blkaddr);
1399 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1401 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1405 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1407 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1409 f2fs_down_read(&sbi->node_change);
1411 f2fs_up_read(&sbi->node_change);
1416 f2fs_unlock_op(sbi);
1421 * f2fs_map_blocks() tries to find or build mapping relationship which
1422 * maps continuous logical blocks to physical blocks, and return such
1423 * info via f2fs_map_blocks structure.
1425 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1426 int create, int flag)
1428 unsigned int maxblocks = map->m_len;
1429 struct dnode_of_data dn;
1430 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1431 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1432 pgoff_t pgofs, end_offset, end;
1433 int err = 0, ofs = 1;
1434 unsigned int ofs_in_node, last_ofs_in_node;
1436 struct extent_info ei = {0, };
1438 unsigned int start_pgofs;
1444 map->m_bdev = inode->i_sb->s_bdev;
1445 map->m_multidev_dio =
1446 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1451 /* it only supports block size == page size */
1452 pgofs = (pgoff_t)map->m_lblk;
1453 end = pgofs + maxblocks;
1455 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1456 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1460 map->m_pblk = ei.blk + pgofs - ei.fofs;
1461 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1462 map->m_flags = F2FS_MAP_MAPPED;
1463 if (map->m_next_extent)
1464 *map->m_next_extent = pgofs + map->m_len;
1466 /* for hardware encryption, but to avoid potential issue in future */
1467 if (flag == F2FS_GET_BLOCK_DIO)
1468 f2fs_wait_on_block_writeback_range(inode,
1469 map->m_pblk, map->m_len);
1471 if (map->m_multidev_dio) {
1472 block_t blk_addr = map->m_pblk;
1474 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1476 map->m_bdev = FDEV(bidx).bdev;
1477 map->m_pblk -= FDEV(bidx).start_blk;
1478 map->m_len = min(map->m_len,
1479 FDEV(bidx).end_blk + 1 - map->m_pblk);
1481 if (map->m_may_create)
1482 f2fs_update_device_state(sbi, inode->i_ino,
1483 blk_addr, map->m_len);
1489 if (map->m_may_create)
1490 f2fs_do_map_lock(sbi, flag, true);
1492 /* When reading holes, we need its node page */
1493 set_new_dnode(&dn, inode, NULL, NULL, 0);
1494 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1496 if (flag == F2FS_GET_BLOCK_BMAP)
1499 if (err == -ENOENT) {
1501 * There is one exceptional case that read_node_page()
1502 * may return -ENOENT due to filesystem has been
1503 * shutdown or cp_error, so force to convert error
1504 * number to EIO for such case.
1506 if (map->m_may_create &&
1507 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1508 f2fs_cp_error(sbi))) {
1514 if (map->m_next_pgofs)
1515 *map->m_next_pgofs =
1516 f2fs_get_next_page_offset(&dn, pgofs);
1517 if (map->m_next_extent)
1518 *map->m_next_extent =
1519 f2fs_get_next_page_offset(&dn, pgofs);
1524 start_pgofs = pgofs;
1526 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1527 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1530 blkaddr = f2fs_data_blkaddr(&dn);
1532 if (__is_valid_data_blkaddr(blkaddr) &&
1533 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1534 err = -EFSCORRUPTED;
1538 if (__is_valid_data_blkaddr(blkaddr)) {
1539 /* use out-place-update for driect IO under LFS mode */
1540 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1541 map->m_may_create) {
1542 err = __allocate_data_block(&dn, map->m_seg_type);
1545 blkaddr = dn.data_blkaddr;
1546 set_inode_flag(inode, FI_APPEND_WRITE);
1550 if (unlikely(f2fs_cp_error(sbi))) {
1554 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1555 if (blkaddr == NULL_ADDR) {
1557 last_ofs_in_node = dn.ofs_in_node;
1560 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1561 flag != F2FS_GET_BLOCK_DIO);
1562 err = __allocate_data_block(&dn,
1565 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1566 file_need_truncate(inode);
1567 set_inode_flag(inode, FI_APPEND_WRITE);
1572 map->m_flags |= F2FS_MAP_NEW;
1573 blkaddr = dn.data_blkaddr;
1575 if (f2fs_compressed_file(inode) &&
1576 f2fs_sanity_check_cluster(&dn) &&
1577 (flag != F2FS_GET_BLOCK_FIEMAP ||
1578 IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1579 err = -EFSCORRUPTED;
1582 if (flag == F2FS_GET_BLOCK_BMAP) {
1586 if (flag == F2FS_GET_BLOCK_PRECACHE)
1588 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1589 blkaddr == NULL_ADDR) {
1590 if (map->m_next_pgofs)
1591 *map->m_next_pgofs = pgofs + 1;
1594 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1595 /* for defragment case */
1596 if (map->m_next_pgofs)
1597 *map->m_next_pgofs = pgofs + 1;
1603 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1606 if (map->m_multidev_dio)
1607 bidx = f2fs_target_device_index(sbi, blkaddr);
1609 if (map->m_len == 0) {
1610 /* preallocated unwritten block should be mapped for fiemap. */
1611 if (blkaddr == NEW_ADDR)
1612 map->m_flags |= F2FS_MAP_UNWRITTEN;
1613 map->m_flags |= F2FS_MAP_MAPPED;
1615 map->m_pblk = blkaddr;
1618 if (map->m_multidev_dio)
1619 map->m_bdev = FDEV(bidx).bdev;
1620 } else if ((map->m_pblk != NEW_ADDR &&
1621 blkaddr == (map->m_pblk + ofs)) ||
1622 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1623 flag == F2FS_GET_BLOCK_PRE_DIO) {
1624 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1636 /* preallocate blocks in batch for one dnode page */
1637 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1638 (pgofs == end || dn.ofs_in_node == end_offset)) {
1640 dn.ofs_in_node = ofs_in_node;
1641 err = f2fs_reserve_new_blocks(&dn, prealloc);
1645 map->m_len += dn.ofs_in_node - ofs_in_node;
1646 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1650 dn.ofs_in_node = end_offset;
1655 else if (dn.ofs_in_node < end_offset)
1658 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1659 if (map->m_flags & F2FS_MAP_MAPPED) {
1660 unsigned int ofs = start_pgofs - map->m_lblk;
1662 f2fs_update_extent_cache_range(&dn,
1663 start_pgofs, map->m_pblk + ofs,
1668 f2fs_put_dnode(&dn);
1670 if (map->m_may_create) {
1671 f2fs_do_map_lock(sbi, flag, false);
1672 f2fs_balance_fs(sbi, dn.node_changed);
1678 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1680 * for hardware encryption, but to avoid potential issue
1683 f2fs_wait_on_block_writeback_range(inode,
1684 map->m_pblk, map->m_len);
1685 invalidate_mapping_pages(META_MAPPING(sbi),
1686 map->m_pblk, map->m_pblk);
1688 if (map->m_multidev_dio) {
1689 block_t blk_addr = map->m_pblk;
1691 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1693 map->m_bdev = FDEV(bidx).bdev;
1694 map->m_pblk -= FDEV(bidx).start_blk;
1696 if (map->m_may_create)
1697 f2fs_update_device_state(sbi, inode->i_ino,
1698 blk_addr, map->m_len);
1700 f2fs_bug_on(sbi, blk_addr + map->m_len >
1701 FDEV(bidx).end_blk + 1);
1705 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1706 if (map->m_flags & F2FS_MAP_MAPPED) {
1707 unsigned int ofs = start_pgofs - map->m_lblk;
1709 f2fs_update_extent_cache_range(&dn,
1710 start_pgofs, map->m_pblk + ofs,
1713 if (map->m_next_extent)
1714 *map->m_next_extent = pgofs + 1;
1716 f2fs_put_dnode(&dn);
1718 if (map->m_may_create) {
1719 f2fs_do_map_lock(sbi, flag, false);
1720 f2fs_balance_fs(sbi, dn.node_changed);
1723 trace_f2fs_map_blocks(inode, map, create, flag, err);
1727 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1729 struct f2fs_map_blocks map;
1733 if (pos + len > i_size_read(inode))
1736 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1737 map.m_next_pgofs = NULL;
1738 map.m_next_extent = NULL;
1739 map.m_seg_type = NO_CHECK_TYPE;
1740 map.m_may_create = false;
1741 last_lblk = F2FS_BLK_ALIGN(pos + len);
1743 while (map.m_lblk < last_lblk) {
1744 map.m_len = last_lblk - map.m_lblk;
1745 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1746 if (err || map.m_len == 0)
1748 map.m_lblk += map.m_len;
1753 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1755 return (bytes >> inode->i_blkbits);
1758 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1760 return (blks << inode->i_blkbits);
1763 static int f2fs_xattr_fiemap(struct inode *inode,
1764 struct fiemap_extent_info *fieinfo)
1766 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1768 struct node_info ni;
1769 __u64 phys = 0, len;
1771 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1774 if (f2fs_has_inline_xattr(inode)) {
1777 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1778 inode->i_ino, false);
1782 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
1784 f2fs_put_page(page, 1);
1788 phys = blks_to_bytes(inode, ni.blk_addr);
1789 offset = offsetof(struct f2fs_inode, i_addr) +
1790 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1791 get_inline_xattr_addrs(inode));
1794 len = inline_xattr_size(inode);
1796 f2fs_put_page(page, 1);
1798 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1801 flags |= FIEMAP_EXTENT_LAST;
1803 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1804 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1805 if (err || err == 1)
1810 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1814 err = f2fs_get_node_info(sbi, xnid, &ni, false);
1816 f2fs_put_page(page, 1);
1820 phys = blks_to_bytes(inode, ni.blk_addr);
1821 len = inode->i_sb->s_blocksize;
1823 f2fs_put_page(page, 1);
1825 flags = FIEMAP_EXTENT_LAST;
1829 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1830 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1833 return (err < 0 ? err : 0);
1836 static loff_t max_inode_blocks(struct inode *inode)
1838 loff_t result = ADDRS_PER_INODE(inode);
1839 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1841 /* two direct node blocks */
1842 result += (leaf_count * 2);
1844 /* two indirect node blocks */
1845 leaf_count *= NIDS_PER_BLOCK;
1846 result += (leaf_count * 2);
1848 /* one double indirect node block */
1849 leaf_count *= NIDS_PER_BLOCK;
1850 result += leaf_count;
1855 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1858 struct f2fs_map_blocks map;
1859 sector_t start_blk, last_blk;
1861 u64 logical = 0, phys = 0, size = 0;
1864 bool compr_cluster = false, compr_appended;
1865 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1866 unsigned int count_in_cluster = 0;
1869 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1870 ret = f2fs_precache_extents(inode);
1875 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1881 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1882 if (start > maxbytes) {
1887 if (len > maxbytes || (maxbytes - len) < start)
1888 len = maxbytes - start;
1890 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1891 ret = f2fs_xattr_fiemap(inode, fieinfo);
1895 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1896 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1901 if (bytes_to_blks(inode, len) == 0)
1902 len = blks_to_bytes(inode, 1);
1904 start_blk = bytes_to_blks(inode, start);
1905 last_blk = bytes_to_blks(inode, start + len - 1);
1908 memset(&map, 0, sizeof(map));
1909 map.m_lblk = start_blk;
1910 map.m_len = bytes_to_blks(inode, len);
1911 map.m_next_pgofs = &next_pgofs;
1912 map.m_seg_type = NO_CHECK_TYPE;
1914 if (compr_cluster) {
1916 map.m_len = cluster_size - count_in_cluster;
1919 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1924 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1925 start_blk = next_pgofs;
1927 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1928 max_inode_blocks(inode)))
1931 flags |= FIEMAP_EXTENT_LAST;
1934 compr_appended = false;
1935 /* In a case of compressed cluster, append this to the last extent */
1936 if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1937 !(map.m_flags & F2FS_MAP_FLAGS))) {
1938 compr_appended = true;
1943 flags |= FIEMAP_EXTENT_MERGED;
1944 if (IS_ENCRYPTED(inode))
1945 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1947 ret = fiemap_fill_next_extent(fieinfo, logical,
1949 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1955 if (start_blk > last_blk)
1959 if (map.m_pblk == COMPRESS_ADDR) {
1960 compr_cluster = true;
1961 count_in_cluster = 1;
1962 } else if (compr_appended) {
1963 unsigned int appended_blks = cluster_size -
1964 count_in_cluster + 1;
1965 size += blks_to_bytes(inode, appended_blks);
1966 start_blk += appended_blks;
1967 compr_cluster = false;
1969 logical = blks_to_bytes(inode, start_blk);
1970 phys = __is_valid_data_blkaddr(map.m_pblk) ?
1971 blks_to_bytes(inode, map.m_pblk) : 0;
1972 size = blks_to_bytes(inode, map.m_len);
1975 if (compr_cluster) {
1976 flags = FIEMAP_EXTENT_ENCODED;
1977 count_in_cluster += map.m_len;
1978 if (count_in_cluster == cluster_size) {
1979 compr_cluster = false;
1980 size += blks_to_bytes(inode, 1);
1982 } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
1983 flags = FIEMAP_EXTENT_UNWRITTEN;
1986 start_blk += bytes_to_blks(inode, size);
1991 if (fatal_signal_pending(current))
1999 inode_unlock(inode);
2003 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2005 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2006 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2007 return inode->i_sb->s_maxbytes;
2009 return i_size_read(inode);
2012 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2014 struct f2fs_map_blocks *map,
2015 struct bio **bio_ret,
2016 sector_t *last_block_in_bio,
2019 struct bio *bio = *bio_ret;
2020 const unsigned blocksize = blks_to_bytes(inode, 1);
2021 sector_t block_in_file;
2022 sector_t last_block;
2023 sector_t last_block_in_file;
2027 block_in_file = (sector_t)page_index(page);
2028 last_block = block_in_file + nr_pages;
2029 last_block_in_file = bytes_to_blks(inode,
2030 f2fs_readpage_limit(inode) + blocksize - 1);
2031 if (last_block > last_block_in_file)
2032 last_block = last_block_in_file;
2034 /* just zeroing out page which is beyond EOF */
2035 if (block_in_file >= last_block)
2038 * Map blocks using the previous result first.
2040 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2041 block_in_file > map->m_lblk &&
2042 block_in_file < (map->m_lblk + map->m_len))
2046 * Then do more f2fs_map_blocks() calls until we are
2047 * done with this page.
2049 map->m_lblk = block_in_file;
2050 map->m_len = last_block - block_in_file;
2052 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2056 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2057 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2058 SetPageMappedToDisk(page);
2060 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2061 DATA_GENERIC_ENHANCE_READ)) {
2062 ret = -EFSCORRUPTED;
2067 zero_user_segment(page, 0, PAGE_SIZE);
2068 if (f2fs_need_verity(inode, page->index) &&
2069 !fsverity_verify_page(page)) {
2073 if (!PageUptodate(page))
2074 SetPageUptodate(page);
2080 * This page will go to BIO. Do we need to send this
2083 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2084 *last_block_in_bio, block_nr) ||
2085 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2087 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2091 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2092 is_readahead ? REQ_RAHEAD : 0, page->index,
2102 * If the page is under writeback, we need to wait for
2103 * its completion to see the correct decrypted data.
2105 f2fs_wait_on_block_writeback(inode, block_nr);
2107 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2108 goto submit_and_realloc;
2110 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2111 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2112 ClearPageError(page);
2113 *last_block_in_bio = block_nr;
2120 #ifdef CONFIG_F2FS_FS_COMPRESSION
2121 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2122 unsigned nr_pages, sector_t *last_block_in_bio,
2123 bool is_readahead, bool for_write)
2125 struct dnode_of_data dn;
2126 struct inode *inode = cc->inode;
2127 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2128 struct bio *bio = *bio_ret;
2129 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2130 sector_t last_block_in_file;
2131 const unsigned blocksize = blks_to_bytes(inode, 1);
2132 struct decompress_io_ctx *dic = NULL;
2133 struct extent_info ei = {0, };
2134 bool from_dnode = true;
2138 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2140 last_block_in_file = bytes_to_blks(inode,
2141 f2fs_readpage_limit(inode) + blocksize - 1);
2143 /* get rid of pages beyond EOF */
2144 for (i = 0; i < cc->cluster_size; i++) {
2145 struct page *page = cc->rpages[i];
2149 if ((sector_t)page->index >= last_block_in_file) {
2150 zero_user_segment(page, 0, PAGE_SIZE);
2151 if (!PageUptodate(page))
2152 SetPageUptodate(page);
2153 } else if (!PageUptodate(page)) {
2159 cc->rpages[i] = NULL;
2163 /* we are done since all pages are beyond EOF */
2164 if (f2fs_cluster_is_empty(cc))
2167 if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2171 goto skip_reading_dnode;
2173 set_new_dnode(&dn, inode, NULL, NULL, 0);
2174 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2178 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2181 for (i = 1; i < cc->cluster_size; i++) {
2184 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2185 dn.ofs_in_node + i) :
2188 if (!__is_valid_data_blkaddr(blkaddr))
2191 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2197 if (!from_dnode && i >= ei.c_len)
2201 /* nothing to decompress */
2202 if (cc->nr_cpages == 0) {
2207 dic = f2fs_alloc_dic(cc);
2213 for (i = 0; i < cc->nr_cpages; i++) {
2214 struct page *page = dic->cpages[i];
2216 struct bio_post_read_ctx *ctx;
2218 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2219 dn.ofs_in_node + i + 1) :
2222 f2fs_wait_on_block_writeback(inode, blkaddr);
2224 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2225 if (atomic_dec_and_test(&dic->remaining_pages))
2226 f2fs_decompress_cluster(dic);
2230 if (bio && (!page_is_mergeable(sbi, bio,
2231 *last_block_in_bio, blkaddr) ||
2232 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2234 __submit_bio(sbi, bio, DATA);
2239 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2240 is_readahead ? REQ_RAHEAD : 0,
2241 page->index, for_write);
2244 f2fs_decompress_end_io(dic, ret);
2245 f2fs_put_dnode(&dn);
2251 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2252 goto submit_and_realloc;
2254 ctx = get_post_read_ctx(bio);
2255 ctx->enabled_steps |= STEP_DECOMPRESS;
2256 refcount_inc(&dic->refcnt);
2258 inc_page_count(sbi, F2FS_RD_DATA);
2259 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2260 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2261 ClearPageError(page);
2262 *last_block_in_bio = blkaddr;
2266 f2fs_put_dnode(&dn);
2273 f2fs_put_dnode(&dn);
2275 for (i = 0; i < cc->cluster_size; i++) {
2276 if (cc->rpages[i]) {
2277 ClearPageUptodate(cc->rpages[i]);
2278 ClearPageError(cc->rpages[i]);
2279 unlock_page(cc->rpages[i]);
2288 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2289 * Major change was from block_size == page_size in f2fs by default.
2291 static int f2fs_mpage_readpages(struct inode *inode,
2292 struct readahead_control *rac, struct page *page)
2294 struct bio *bio = NULL;
2295 sector_t last_block_in_bio = 0;
2296 struct f2fs_map_blocks map;
2297 #ifdef CONFIG_F2FS_FS_COMPRESSION
2298 struct compress_ctx cc = {
2300 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2301 .cluster_size = F2FS_I(inode)->i_cluster_size,
2302 .cluster_idx = NULL_CLUSTER,
2308 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2310 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2311 unsigned max_nr_pages = nr_pages;
2318 map.m_next_pgofs = NULL;
2319 map.m_next_extent = NULL;
2320 map.m_seg_type = NO_CHECK_TYPE;
2321 map.m_may_create = false;
2323 for (; nr_pages; nr_pages--) {
2325 page = readahead_page(rac);
2326 prefetchw(&page->flags);
2329 #ifdef CONFIG_F2FS_FS_COMPRESSION
2330 if (f2fs_compressed_file(inode)) {
2331 /* there are remained comressed pages, submit them */
2332 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2333 ret = f2fs_read_multi_pages(&cc, &bio,
2336 rac != NULL, false);
2337 f2fs_destroy_compress_ctx(&cc, false);
2339 goto set_error_page;
2341 if (cc.cluster_idx == NULL_CLUSTER) {
2342 if (nc_cluster_idx ==
2343 page->index >> cc.log_cluster_size) {
2344 goto read_single_page;
2347 ret = f2fs_is_compressed_cluster(inode, page->index);
2349 goto set_error_page;
2352 page->index >> cc.log_cluster_size;
2353 goto read_single_page;
2356 nc_cluster_idx = NULL_CLUSTER;
2358 ret = f2fs_init_compress_ctx(&cc);
2360 goto set_error_page;
2362 f2fs_compress_ctx_add_page(&cc, page);
2369 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2370 &bio, &last_block_in_bio, rac);
2372 #ifdef CONFIG_F2FS_FS_COMPRESSION
2376 zero_user_segment(page, 0, PAGE_SIZE);
2379 #ifdef CONFIG_F2FS_FS_COMPRESSION
2385 #ifdef CONFIG_F2FS_FS_COMPRESSION
2386 if (f2fs_compressed_file(inode)) {
2388 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2389 ret = f2fs_read_multi_pages(&cc, &bio,
2392 rac != NULL, false);
2393 f2fs_destroy_compress_ctx(&cc, false);
2399 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2403 static int f2fs_read_data_folio(struct file *file, struct folio *folio)
2405 struct page *page = &folio->page;
2406 struct inode *inode = page_file_mapping(page)->host;
2409 trace_f2fs_readpage(page, DATA);
2411 if (!f2fs_is_compress_backend_ready(inode)) {
2416 /* If the file has inline data, try to read it directly */
2417 if (f2fs_has_inline_data(inode))
2418 ret = f2fs_read_inline_data(inode, page);
2420 ret = f2fs_mpage_readpages(inode, NULL, page);
2424 static void f2fs_readahead(struct readahead_control *rac)
2426 struct inode *inode = rac->mapping->host;
2428 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2430 if (!f2fs_is_compress_backend_ready(inode))
2433 /* If the file has inline data, skip readahead */
2434 if (f2fs_has_inline_data(inode))
2437 f2fs_mpage_readpages(inode, rac, NULL);
2440 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2442 struct inode *inode = fio->page->mapping->host;
2443 struct page *mpage, *page;
2444 gfp_t gfp_flags = GFP_NOFS;
2446 if (!f2fs_encrypted_file(inode))
2449 page = fio->compressed_page ? fio->compressed_page : fio->page;
2451 /* wait for GCed page writeback via META_MAPPING */
2452 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2454 if (fscrypt_inode_uses_inline_crypto(inode))
2458 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2459 PAGE_SIZE, 0, gfp_flags);
2460 if (IS_ERR(fio->encrypted_page)) {
2461 /* flush pending IOs and wait for a while in the ENOMEM case */
2462 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2463 f2fs_flush_merged_writes(fio->sbi);
2464 memalloc_retry_wait(GFP_NOFS);
2465 gfp_flags |= __GFP_NOFAIL;
2468 return PTR_ERR(fio->encrypted_page);
2471 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2473 if (PageUptodate(mpage))
2474 memcpy(page_address(mpage),
2475 page_address(fio->encrypted_page), PAGE_SIZE);
2476 f2fs_put_page(mpage, 1);
2481 static inline bool check_inplace_update_policy(struct inode *inode,
2482 struct f2fs_io_info *fio)
2484 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2485 unsigned int policy = SM_I(sbi)->ipu_policy;
2487 if (policy & (0x1 << F2FS_IPU_HONOR_OPU_WRITE) &&
2488 is_inode_flag_set(inode, FI_OPU_WRITE))
2490 if (policy & (0x1 << F2FS_IPU_FORCE))
2492 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2494 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2495 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2497 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2498 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2502 * IPU for rewrite async pages
2504 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2505 fio && fio->op == REQ_OP_WRITE &&
2506 !(fio->op_flags & REQ_SYNC) &&
2507 !IS_ENCRYPTED(inode))
2510 /* this is only set during fdatasync */
2511 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2512 is_inode_flag_set(inode, FI_NEED_IPU))
2515 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2516 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2522 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2524 /* swap file is migrating in aligned write mode */
2525 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2528 if (f2fs_is_pinned_file(inode))
2531 /* if this is cold file, we should overwrite to avoid fragmentation */
2532 if (file_is_cold(inode))
2535 return check_inplace_update_policy(inode, fio);
2538 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2540 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2542 /* The below cases were checked when setting it. */
2543 if (f2fs_is_pinned_file(inode))
2545 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2547 if (f2fs_lfs_mode(sbi))
2549 if (S_ISDIR(inode->i_mode))
2551 if (IS_NOQUOTA(inode))
2553 if (f2fs_is_atomic_file(inode))
2556 /* swap file is migrating in aligned write mode */
2557 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2560 if (is_inode_flag_set(inode, FI_OPU_WRITE))
2564 if (page_private_gcing(fio->page))
2566 if (page_private_dummy(fio->page))
2568 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2569 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2575 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2577 struct inode *inode = fio->page->mapping->host;
2579 if (f2fs_should_update_outplace(inode, fio))
2582 return f2fs_should_update_inplace(inode, fio);
2585 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2587 struct page *page = fio->page;
2588 struct inode *inode = page->mapping->host;
2589 struct dnode_of_data dn;
2590 struct extent_info ei = {0, };
2591 struct node_info ni;
2592 bool ipu_force = false;
2595 /* Use COW inode to make dnode_of_data for atomic write */
2596 if (f2fs_is_atomic_file(inode))
2597 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
2599 set_new_dnode(&dn, inode, NULL, NULL, 0);
2601 if (need_inplace_update(fio) &&
2602 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2603 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2605 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2606 DATA_GENERIC_ENHANCE))
2607 return -EFSCORRUPTED;
2610 fio->need_lock = LOCK_DONE;
2614 /* Deadlock due to between page->lock and f2fs_lock_op */
2615 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2618 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2622 fio->old_blkaddr = dn.data_blkaddr;
2624 /* This page is already truncated */
2625 if (fio->old_blkaddr == NULL_ADDR) {
2626 ClearPageUptodate(page);
2627 clear_page_private_gcing(page);
2631 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2632 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2633 DATA_GENERIC_ENHANCE)) {
2634 err = -EFSCORRUPTED;
2639 * If current allocation needs SSR,
2640 * it had better in-place writes for updated data.
2643 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2644 need_inplace_update(fio))) {
2645 err = f2fs_encrypt_one_page(fio);
2649 set_page_writeback(page);
2650 ClearPageError(page);
2651 f2fs_put_dnode(&dn);
2652 if (fio->need_lock == LOCK_REQ)
2653 f2fs_unlock_op(fio->sbi);
2654 err = f2fs_inplace_write_data(fio);
2656 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2657 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2658 if (PageWriteback(page))
2659 end_page_writeback(page);
2661 set_inode_flag(inode, FI_UPDATE_WRITE);
2663 trace_f2fs_do_write_data_page(fio->page, IPU);
2667 if (fio->need_lock == LOCK_RETRY) {
2668 if (!f2fs_trylock_op(fio->sbi)) {
2672 fio->need_lock = LOCK_REQ;
2675 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
2679 fio->version = ni.version;
2681 err = f2fs_encrypt_one_page(fio);
2685 set_page_writeback(page);
2686 ClearPageError(page);
2688 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2689 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2691 /* LFS mode write path */
2692 f2fs_outplace_write_data(&dn, fio);
2693 trace_f2fs_do_write_data_page(page, OPU);
2694 set_inode_flag(inode, FI_APPEND_WRITE);
2695 if (page->index == 0)
2696 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2698 f2fs_put_dnode(&dn);
2700 if (fio->need_lock == LOCK_REQ)
2701 f2fs_unlock_op(fio->sbi);
2705 int f2fs_write_single_data_page(struct page *page, int *submitted,
2707 sector_t *last_block,
2708 struct writeback_control *wbc,
2709 enum iostat_type io_type,
2713 struct inode *inode = page->mapping->host;
2714 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2715 loff_t i_size = i_size_read(inode);
2716 const pgoff_t end_index = ((unsigned long long)i_size)
2718 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2719 unsigned offset = 0;
2720 bool need_balance_fs = false;
2722 struct f2fs_io_info fio = {
2724 .ino = inode->i_ino,
2727 .op_flags = wbc_to_write_flags(wbc),
2728 .old_blkaddr = NULL_ADDR,
2730 .encrypted_page = NULL,
2732 .compr_blocks = compr_blocks,
2733 .need_lock = LOCK_RETRY,
2737 .last_block = last_block,
2740 trace_f2fs_writepage(page, DATA);
2742 /* we should bypass data pages to proceed the kworkder jobs */
2743 if (unlikely(f2fs_cp_error(sbi))) {
2744 mapping_set_error(page->mapping, -EIO);
2746 * don't drop any dirty dentry pages for keeping lastest
2747 * directory structure.
2749 if (S_ISDIR(inode->i_mode))
2754 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2757 if (page->index < end_index ||
2758 f2fs_verity_in_progress(inode) ||
2763 * If the offset is out-of-range of file size,
2764 * this page does not have to be written to disk.
2766 offset = i_size & (PAGE_SIZE - 1);
2767 if ((page->index >= end_index + 1) || !offset)
2770 zero_user_segment(page, offset, PAGE_SIZE);
2772 if (f2fs_is_drop_cache(inode))
2775 /* Dentry/quota blocks are controlled by checkpoint */
2776 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2778 * We need to wait for node_write to avoid block allocation during
2779 * checkpoint. This can only happen to quota writes which can cause
2780 * the below discard race condition.
2782 if (IS_NOQUOTA(inode))
2783 f2fs_down_read(&sbi->node_write);
2785 fio.need_lock = LOCK_DONE;
2786 err = f2fs_do_write_data_page(&fio);
2788 if (IS_NOQUOTA(inode))
2789 f2fs_up_read(&sbi->node_write);
2794 if (!wbc->for_reclaim)
2795 need_balance_fs = true;
2796 else if (has_not_enough_free_secs(sbi, 0, 0))
2799 set_inode_flag(inode, FI_HOT_DATA);
2802 if (f2fs_has_inline_data(inode)) {
2803 err = f2fs_write_inline_data(inode, page);
2808 if (err == -EAGAIN) {
2809 err = f2fs_do_write_data_page(&fio);
2810 if (err == -EAGAIN) {
2811 fio.need_lock = LOCK_REQ;
2812 err = f2fs_do_write_data_page(&fio);
2817 file_set_keep_isize(inode);
2819 spin_lock(&F2FS_I(inode)->i_size_lock);
2820 if (F2FS_I(inode)->last_disk_size < psize)
2821 F2FS_I(inode)->last_disk_size = psize;
2822 spin_unlock(&F2FS_I(inode)->i_size_lock);
2826 if (err && err != -ENOENT)
2830 inode_dec_dirty_pages(inode);
2832 ClearPageUptodate(page);
2833 clear_page_private_gcing(page);
2836 if (wbc->for_reclaim) {
2837 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2838 clear_inode_flag(inode, FI_HOT_DATA);
2839 f2fs_remove_dirty_inode(inode);
2843 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2844 !F2FS_I(inode)->cp_task && allow_balance)
2845 f2fs_balance_fs(sbi, need_balance_fs);
2847 if (unlikely(f2fs_cp_error(sbi))) {
2848 f2fs_submit_merged_write(sbi, DATA);
2849 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2854 *submitted = fio.submitted ? 1 : 0;
2859 redirty_page_for_writepage(wbc, page);
2861 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2862 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2863 * file_write_and_wait_range() will see EIO error, which is critical
2864 * to return value of fsync() followed by atomic_write failure to user.
2866 if (!err || wbc->for_reclaim)
2867 return AOP_WRITEPAGE_ACTIVATE;
2872 static int f2fs_write_data_page(struct page *page,
2873 struct writeback_control *wbc)
2875 #ifdef CONFIG_F2FS_FS_COMPRESSION
2876 struct inode *inode = page->mapping->host;
2878 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2881 if (f2fs_compressed_file(inode)) {
2882 if (f2fs_is_compressed_cluster(inode, page->index)) {
2883 redirty_page_for_writepage(wbc, page);
2884 return AOP_WRITEPAGE_ACTIVATE;
2890 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2891 wbc, FS_DATA_IO, 0, true);
2895 * This function was copied from write_cche_pages from mm/page-writeback.c.
2896 * The major change is making write step of cold data page separately from
2897 * warm/hot data page.
2899 static int f2fs_write_cache_pages(struct address_space *mapping,
2900 struct writeback_control *wbc,
2901 enum iostat_type io_type)
2904 int done = 0, retry = 0;
2905 struct pagevec pvec;
2906 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2907 struct bio *bio = NULL;
2908 sector_t last_block;
2909 #ifdef CONFIG_F2FS_FS_COMPRESSION
2910 struct inode *inode = mapping->host;
2911 struct compress_ctx cc = {
2913 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2914 .cluster_size = F2FS_I(inode)->i_cluster_size,
2915 .cluster_idx = NULL_CLUSTER,
2919 .valid_nr_cpages = 0,
2922 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2928 pgoff_t end; /* Inclusive */
2930 int range_whole = 0;
2936 pagevec_init(&pvec);
2938 if (get_dirty_pages(mapping->host) <=
2939 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2940 set_inode_flag(mapping->host, FI_HOT_DATA);
2942 clear_inode_flag(mapping->host, FI_HOT_DATA);
2944 if (wbc->range_cyclic) {
2945 index = mapping->writeback_index; /* prev offset */
2948 index = wbc->range_start >> PAGE_SHIFT;
2949 end = wbc->range_end >> PAGE_SHIFT;
2950 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2953 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2954 tag = PAGECACHE_TAG_TOWRITE;
2956 tag = PAGECACHE_TAG_DIRTY;
2959 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2960 tag_pages_for_writeback(mapping, index, end);
2962 while (!done && !retry && (index <= end)) {
2963 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2968 for (i = 0; i < nr_pages; i++) {
2969 struct page *page = pvec.pages[i];
2973 #ifdef CONFIG_F2FS_FS_COMPRESSION
2974 if (f2fs_compressed_file(inode)) {
2975 void *fsdata = NULL;
2979 ret = f2fs_init_compress_ctx(&cc);
2985 if (!f2fs_cluster_can_merge_page(&cc,
2987 ret = f2fs_write_multi_pages(&cc,
2988 &submitted, wbc, io_type);
2994 if (unlikely(f2fs_cp_error(sbi)))
2997 if (!f2fs_cluster_is_empty(&cc))
3000 ret2 = f2fs_prepare_compress_overwrite(
3002 page->index, &fsdata);
3008 (!f2fs_compress_write_end(inode,
3009 fsdata, page->index, 1) ||
3010 !f2fs_all_cluster_page_loaded(&cc,
3011 &pvec, i, nr_pages))) {
3017 /* give a priority to WB_SYNC threads */
3018 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3019 wbc->sync_mode == WB_SYNC_NONE) {
3023 #ifdef CONFIG_F2FS_FS_COMPRESSION
3026 done_index = page->index;
3030 if (unlikely(page->mapping != mapping)) {
3036 if (!PageDirty(page)) {
3037 /* someone wrote it for us */
3038 goto continue_unlock;
3041 if (PageWriteback(page)) {
3042 if (wbc->sync_mode != WB_SYNC_NONE)
3043 f2fs_wait_on_page_writeback(page,
3046 goto continue_unlock;
3049 if (!clear_page_dirty_for_io(page))
3050 goto continue_unlock;
3052 #ifdef CONFIG_F2FS_FS_COMPRESSION
3053 if (f2fs_compressed_file(inode)) {
3055 f2fs_compress_ctx_add_page(&cc, page);
3059 ret = f2fs_write_single_data_page(page, &submitted,
3060 &bio, &last_block, wbc, io_type,
3062 if (ret == AOP_WRITEPAGE_ACTIVATE)
3064 #ifdef CONFIG_F2FS_FS_COMPRESSION
3067 nwritten += submitted;
3068 wbc->nr_to_write -= submitted;
3070 if (unlikely(ret)) {
3072 * keep nr_to_write, since vfs uses this to
3073 * get # of written pages.
3075 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3078 } else if (ret == -EAGAIN) {
3080 if (wbc->sync_mode == WB_SYNC_ALL) {
3081 f2fs_io_schedule_timeout(
3082 DEFAULT_IO_TIMEOUT);
3087 done_index = page->index + 1;
3092 if (wbc->nr_to_write <= 0 &&
3093 wbc->sync_mode == WB_SYNC_NONE) {
3101 pagevec_release(&pvec);
3104 #ifdef CONFIG_F2FS_FS_COMPRESSION
3105 /* flush remained pages in compress cluster */
3106 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3107 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3108 nwritten += submitted;
3109 wbc->nr_to_write -= submitted;
3115 if (f2fs_compressed_file(inode))
3116 f2fs_destroy_compress_ctx(&cc, false);
3123 if (wbc->range_cyclic && !done)
3125 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3126 mapping->writeback_index = done_index;
3129 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3131 /* submit cached bio of IPU write */
3133 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3138 static inline bool __should_serialize_io(struct inode *inode,
3139 struct writeback_control *wbc)
3141 /* to avoid deadlock in path of data flush */
3142 if (F2FS_I(inode)->cp_task)
3145 if (!S_ISREG(inode->i_mode))
3147 if (IS_NOQUOTA(inode))
3150 if (f2fs_need_compress_data(inode))
3152 if (wbc->sync_mode != WB_SYNC_ALL)
3154 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3159 static int __f2fs_write_data_pages(struct address_space *mapping,
3160 struct writeback_control *wbc,
3161 enum iostat_type io_type)
3163 struct inode *inode = mapping->host;
3164 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3165 struct blk_plug plug;
3167 bool locked = false;
3169 /* deal with chardevs and other special file */
3170 if (!mapping->a_ops->writepage)
3173 /* skip writing if there is no dirty page in this inode */
3174 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3177 /* during POR, we don't need to trigger writepage at all. */
3178 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3181 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3182 wbc->sync_mode == WB_SYNC_NONE &&
3183 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3184 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3187 /* skip writing in file defragment preparing stage */
3188 if (is_inode_flag_set(inode, FI_SKIP_WRITES))
3191 trace_f2fs_writepages(mapping->host, wbc, DATA);
3193 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3194 if (wbc->sync_mode == WB_SYNC_ALL)
3195 atomic_inc(&sbi->wb_sync_req[DATA]);
3196 else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3197 /* to avoid potential deadlock */
3199 blk_finish_plug(current->plug);
3203 if (__should_serialize_io(inode, wbc)) {
3204 mutex_lock(&sbi->writepages);
3208 blk_start_plug(&plug);
3209 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3210 blk_finish_plug(&plug);
3213 mutex_unlock(&sbi->writepages);
3215 if (wbc->sync_mode == WB_SYNC_ALL)
3216 atomic_dec(&sbi->wb_sync_req[DATA]);
3218 * if some pages were truncated, we cannot guarantee its mapping->host
3219 * to detect pending bios.
3222 f2fs_remove_dirty_inode(inode);
3226 wbc->pages_skipped += get_dirty_pages(inode);
3227 trace_f2fs_writepages(mapping->host, wbc, DATA);
3231 static int f2fs_write_data_pages(struct address_space *mapping,
3232 struct writeback_control *wbc)
3234 struct inode *inode = mapping->host;
3236 return __f2fs_write_data_pages(mapping, wbc,
3237 F2FS_I(inode)->cp_task == current ?
3238 FS_CP_DATA_IO : FS_DATA_IO);
3241 void f2fs_write_failed(struct inode *inode, loff_t to)
3243 loff_t i_size = i_size_read(inode);
3245 if (IS_NOQUOTA(inode))
3248 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3249 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3250 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3251 filemap_invalidate_lock(inode->i_mapping);
3253 truncate_pagecache(inode, i_size);
3254 f2fs_truncate_blocks(inode, i_size, true);
3256 filemap_invalidate_unlock(inode->i_mapping);
3257 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3261 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3262 struct page *page, loff_t pos, unsigned len,
3263 block_t *blk_addr, bool *node_changed)
3265 struct inode *inode = page->mapping->host;
3266 pgoff_t index = page->index;
3267 struct dnode_of_data dn;
3269 bool locked = false;
3270 struct extent_info ei = {0, };
3275 * If a whole page is being written and we already preallocated all the
3276 * blocks, then there is no need to get a block address now.
3278 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
3281 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3282 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3283 flag = F2FS_GET_BLOCK_DEFAULT;
3285 flag = F2FS_GET_BLOCK_PRE_AIO;
3287 if (f2fs_has_inline_data(inode) ||
3288 (pos & PAGE_MASK) >= i_size_read(inode)) {
3289 f2fs_do_map_lock(sbi, flag, true);
3294 /* check inline_data */
3295 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3296 if (IS_ERR(ipage)) {
3297 err = PTR_ERR(ipage);
3301 set_new_dnode(&dn, inode, ipage, ipage, 0);
3303 if (f2fs_has_inline_data(inode)) {
3304 if (pos + len <= MAX_INLINE_DATA(inode)) {
3305 f2fs_do_read_inline_data(page, ipage);
3306 set_inode_flag(inode, FI_DATA_EXIST);
3308 set_page_private_inline(ipage);
3310 err = f2fs_convert_inline_page(&dn, page);
3313 if (dn.data_blkaddr == NULL_ADDR)
3314 err = f2fs_get_block(&dn, index);
3316 } else if (locked) {
3317 err = f2fs_get_block(&dn, index);
3319 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3320 dn.data_blkaddr = ei.blk + index - ei.fofs;
3323 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3324 if (err || dn.data_blkaddr == NULL_ADDR) {
3325 f2fs_put_dnode(&dn);
3326 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3328 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3335 /* convert_inline_page can make node_changed */
3336 *blk_addr = dn.data_blkaddr;
3337 *node_changed = dn.node_changed;
3339 f2fs_put_dnode(&dn);
3342 f2fs_do_map_lock(sbi, flag, false);
3346 static int __find_data_block(struct inode *inode, pgoff_t index,
3349 struct dnode_of_data dn;
3351 struct extent_info ei = {0, };
3354 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
3356 return PTR_ERR(ipage);
3358 set_new_dnode(&dn, inode, ipage, ipage, 0);
3360 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3361 dn.data_blkaddr = ei.blk + index - ei.fofs;
3364 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3366 dn.data_blkaddr = NULL_ADDR;
3370 *blk_addr = dn.data_blkaddr;
3371 f2fs_put_dnode(&dn);
3375 static int __reserve_data_block(struct inode *inode, pgoff_t index,
3376 block_t *blk_addr, bool *node_changed)
3378 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3379 struct dnode_of_data dn;
3383 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
3385 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3386 if (IS_ERR(ipage)) {
3387 err = PTR_ERR(ipage);
3390 set_new_dnode(&dn, inode, ipage, ipage, 0);
3392 err = f2fs_get_block(&dn, index);
3394 *blk_addr = dn.data_blkaddr;
3395 *node_changed = dn.node_changed;
3396 f2fs_put_dnode(&dn);
3399 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
3403 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
3404 struct page *page, loff_t pos, unsigned int len,
3405 block_t *blk_addr, bool *node_changed)
3407 struct inode *inode = page->mapping->host;
3408 struct inode *cow_inode = F2FS_I(inode)->cow_inode;
3409 pgoff_t index = page->index;
3411 block_t ori_blk_addr;
3413 /* If pos is beyond the end of file, reserve a new block in COW inode */
3414 if ((pos & PAGE_MASK) >= i_size_read(inode))
3415 return __reserve_data_block(cow_inode, index, blk_addr,
3418 /* Look for the block in COW inode first */
3419 err = __find_data_block(cow_inode, index, blk_addr);
3422 else if (*blk_addr != NULL_ADDR)
3425 /* Look for the block in the original inode */
3426 err = __find_data_block(inode, index, &ori_blk_addr);
3430 /* Finally, we should reserve a new block in COW inode for the update */
3431 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3435 if (ori_blk_addr != NULL_ADDR)
3436 *blk_addr = ori_blk_addr;
3440 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3441 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
3443 struct inode *inode = mapping->host;
3444 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3445 struct page *page = NULL;
3446 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3447 bool need_balance = false;
3448 block_t blkaddr = NULL_ADDR;
3451 trace_f2fs_write_begin(inode, pos, len);
3453 if (!f2fs_is_checkpoint_ready(sbi)) {
3459 * We should check this at this moment to avoid deadlock on inode page
3460 * and #0 page. The locking rule for inline_data conversion should be:
3461 * lock_page(page #0) -> lock_page(inode_page)
3464 err = f2fs_convert_inline_inode(inode);
3469 #ifdef CONFIG_F2FS_FS_COMPRESSION
3470 if (f2fs_compressed_file(inode)) {
3475 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3478 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3491 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3492 * wait_for_stable_page. Will wait that below with our IO control.
3494 page = f2fs_pagecache_get_page(mapping, index,
3495 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3501 /* TODO: cluster can be compressed due to race with .writepage */
3505 if (f2fs_is_atomic_file(inode))
3506 err = prepare_atomic_write_begin(sbi, page, pos, len,
3507 &blkaddr, &need_balance);
3509 err = prepare_write_begin(sbi, page, pos, len,
3510 &blkaddr, &need_balance);
3514 if (need_balance && !IS_NOQUOTA(inode) &&
3515 has_not_enough_free_secs(sbi, 0, 0)) {
3517 f2fs_balance_fs(sbi, true);
3519 if (page->mapping != mapping) {
3520 /* The page got truncated from under us */
3521 f2fs_put_page(page, 1);
3526 f2fs_wait_on_page_writeback(page, DATA, false, true);
3528 if (len == PAGE_SIZE || PageUptodate(page))
3531 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3532 !f2fs_verity_in_progress(inode)) {
3533 zero_user_segment(page, len, PAGE_SIZE);
3537 if (blkaddr == NEW_ADDR) {
3538 zero_user_segment(page, 0, PAGE_SIZE);
3539 SetPageUptodate(page);
3541 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3542 DATA_GENERIC_ENHANCE_READ)) {
3543 err = -EFSCORRUPTED;
3546 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3551 if (unlikely(page->mapping != mapping)) {
3552 f2fs_put_page(page, 1);
3555 if (unlikely(!PageUptodate(page))) {
3563 f2fs_put_page(page, 1);
3564 f2fs_write_failed(inode, pos + len);
3568 static int f2fs_write_end(struct file *file,
3569 struct address_space *mapping,
3570 loff_t pos, unsigned len, unsigned copied,
3571 struct page *page, void *fsdata)
3573 struct inode *inode = page->mapping->host;
3575 trace_f2fs_write_end(inode, pos, len, copied);
3578 * This should be come from len == PAGE_SIZE, and we expect copied
3579 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3580 * let generic_perform_write() try to copy data again through copied=0.
3582 if (!PageUptodate(page)) {
3583 if (unlikely(copied != len))
3586 SetPageUptodate(page);
3589 #ifdef CONFIG_F2FS_FS_COMPRESSION
3590 /* overwrite compressed file */
3591 if (f2fs_compressed_file(inode) && fsdata) {
3592 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3593 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3595 if (pos + copied > i_size_read(inode) &&
3596 !f2fs_verity_in_progress(inode))
3597 f2fs_i_size_write(inode, pos + copied);
3605 set_page_dirty(page);
3607 if (pos + copied > i_size_read(inode) &&
3608 !f2fs_verity_in_progress(inode)) {
3609 f2fs_i_size_write(inode, pos + copied);
3610 if (f2fs_is_atomic_file(inode))
3611 f2fs_i_size_write(F2FS_I(inode)->cow_inode,
3615 f2fs_put_page(page, 1);
3616 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3620 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
3622 struct inode *inode = folio->mapping->host;
3623 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3625 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3626 (offset || length != folio_size(folio)))
3629 if (folio_test_dirty(folio)) {
3630 if (inode->i_ino == F2FS_META_INO(sbi)) {
3631 dec_page_count(sbi, F2FS_DIRTY_META);
3632 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3633 dec_page_count(sbi, F2FS_DIRTY_NODES);
3635 inode_dec_dirty_pages(inode);
3636 f2fs_remove_dirty_inode(inode);
3640 clear_page_private_gcing(&folio->page);
3642 if (test_opt(sbi, COMPRESS_CACHE) &&
3643 inode->i_ino == F2FS_COMPRESS_INO(sbi))
3644 clear_page_private_data(&folio->page);
3646 folio_detach_private(folio);
3649 bool f2fs_release_folio(struct folio *folio, gfp_t wait)
3651 struct f2fs_sb_info *sbi;
3653 /* If this is dirty folio, keep private data */
3654 if (folio_test_dirty(folio))
3657 sbi = F2FS_M_SB(folio->mapping);
3658 if (test_opt(sbi, COMPRESS_CACHE)) {
3659 struct inode *inode = folio->mapping->host;
3661 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3662 clear_page_private_data(&folio->page);
3665 clear_page_private_gcing(&folio->page);
3667 folio_detach_private(folio);
3671 static bool f2fs_dirty_data_folio(struct address_space *mapping,
3672 struct folio *folio)
3674 struct inode *inode = mapping->host;
3676 trace_f2fs_set_page_dirty(&folio->page, DATA);
3678 if (!folio_test_uptodate(folio))
3679 folio_mark_uptodate(folio);
3680 BUG_ON(folio_test_swapcache(folio));
3682 if (!folio_test_dirty(folio)) {
3683 filemap_dirty_folio(mapping, folio);
3684 f2fs_update_dirty_folio(inode, folio);
3691 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3693 #ifdef CONFIG_F2FS_FS_COMPRESSION
3694 struct dnode_of_data dn;
3695 sector_t start_idx, blknr = 0;
3698 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3700 set_new_dnode(&dn, inode, NULL, NULL, 0);
3701 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3705 if (dn.data_blkaddr != COMPRESS_ADDR) {
3706 dn.ofs_in_node += block - start_idx;
3707 blknr = f2fs_data_blkaddr(&dn);
3708 if (!__is_valid_data_blkaddr(blknr))
3712 f2fs_put_dnode(&dn);
3720 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3722 struct inode *inode = mapping->host;
3725 if (f2fs_has_inline_data(inode))
3728 /* make sure allocating whole blocks */
3729 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3730 filemap_write_and_wait(mapping);
3732 /* Block number less than F2FS MAX BLOCKS */
3733 if (unlikely(block >= max_file_blocks(inode)))
3736 if (f2fs_compressed_file(inode)) {
3737 blknr = f2fs_bmap_compress(inode, block);
3739 struct f2fs_map_blocks map;
3741 memset(&map, 0, sizeof(map));
3744 map.m_next_pgofs = NULL;
3745 map.m_seg_type = NO_CHECK_TYPE;
3747 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3751 trace_f2fs_bmap(inode, block, blknr);
3755 #ifdef CONFIG_MIGRATION
3756 #include <linux/migrate.h>
3758 int f2fs_migrate_page(struct address_space *mapping,
3759 struct page *newpage, struct page *page, enum migrate_mode mode)
3761 int rc, extra_count = 0;
3763 BUG_ON(PageWriteback(page));
3765 rc = migrate_page_move_mapping(mapping, newpage,
3767 if (rc != MIGRATEPAGE_SUCCESS)
3770 /* guarantee to start from no stale private field */
3771 set_page_private(newpage, 0);
3772 if (PagePrivate(page)) {
3773 set_page_private(newpage, page_private(page));
3774 SetPagePrivate(newpage);
3777 set_page_private(page, 0);
3778 ClearPagePrivate(page);
3782 if (mode != MIGRATE_SYNC_NO_COPY)
3783 migrate_page_copy(newpage, page);
3785 migrate_page_states(newpage, page);
3787 return MIGRATEPAGE_SUCCESS;
3792 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3793 unsigned int blkcnt)
3795 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3796 unsigned int blkofs;
3797 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3798 unsigned int secidx = start_blk / blk_per_sec;
3799 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3802 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3803 filemap_invalidate_lock(inode->i_mapping);
3805 set_inode_flag(inode, FI_ALIGNED_WRITE);
3806 set_inode_flag(inode, FI_OPU_WRITE);
3808 for (; secidx < end_sec; secidx++) {
3809 f2fs_down_write(&sbi->pin_sem);
3812 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3813 f2fs_unlock_op(sbi);
3815 set_inode_flag(inode, FI_SKIP_WRITES);
3817 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3819 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3821 page = f2fs_get_lock_data_page(inode, blkidx, true);
3823 f2fs_up_write(&sbi->pin_sem);
3824 ret = PTR_ERR(page);
3828 set_page_dirty(page);
3829 f2fs_put_page(page, 1);
3832 clear_inode_flag(inode, FI_SKIP_WRITES);
3834 ret = filemap_fdatawrite(inode->i_mapping);
3836 f2fs_up_write(&sbi->pin_sem);
3843 clear_inode_flag(inode, FI_SKIP_WRITES);
3844 clear_inode_flag(inode, FI_OPU_WRITE);
3845 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3847 filemap_invalidate_unlock(inode->i_mapping);
3848 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3853 static int check_swap_activate(struct swap_info_struct *sis,
3854 struct file *swap_file, sector_t *span)
3856 struct address_space *mapping = swap_file->f_mapping;
3857 struct inode *inode = mapping->host;
3858 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3859 sector_t cur_lblock;
3860 sector_t last_lblock;
3862 sector_t lowest_pblock = -1;
3863 sector_t highest_pblock = 0;
3865 unsigned long nr_pblocks;
3866 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3867 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3868 unsigned int not_aligned = 0;
3872 * Map all the blocks into the extent list. This code doesn't try
3876 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3878 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3879 struct f2fs_map_blocks map;
3883 memset(&map, 0, sizeof(map));
3884 map.m_lblk = cur_lblock;
3885 map.m_len = last_lblock - cur_lblock;
3886 map.m_next_pgofs = NULL;
3887 map.m_next_extent = NULL;
3888 map.m_seg_type = NO_CHECK_TYPE;
3889 map.m_may_create = false;
3891 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3896 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3897 f2fs_err(sbi, "Swapfile has holes");
3902 pblock = map.m_pblk;
3903 nr_pblocks = map.m_len;
3905 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3906 nr_pblocks & sec_blks_mask) {
3909 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3910 if (cur_lblock + nr_pblocks > sis->max)
3911 nr_pblocks -= blks_per_sec;
3914 /* this extent is last one */
3915 nr_pblocks = map.m_len;
3916 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
3920 ret = f2fs_migrate_blocks(inode, cur_lblock,
3927 if (cur_lblock + nr_pblocks >= sis->max)
3928 nr_pblocks = sis->max - cur_lblock;
3930 if (cur_lblock) { /* exclude the header page */
3931 if (pblock < lowest_pblock)
3932 lowest_pblock = pblock;
3933 if (pblock + nr_pblocks - 1 > highest_pblock)
3934 highest_pblock = pblock + nr_pblocks - 1;
3938 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3940 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3944 cur_lblock += nr_pblocks;
3947 *span = 1 + highest_pblock - lowest_pblock;
3948 if (cur_lblock == 0)
3949 cur_lblock = 1; /* force Empty message */
3950 sis->max = cur_lblock;
3951 sis->pages = cur_lblock - 1;
3952 sis->highest_bit = cur_lblock - 1;
3955 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
3956 not_aligned, blks_per_sec * F2FS_BLKSIZE);
3960 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3963 struct inode *inode = file_inode(file);
3966 if (!S_ISREG(inode->i_mode))
3969 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3972 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
3973 f2fs_err(F2FS_I_SB(inode),
3974 "Swapfile not supported in LFS mode");
3978 ret = f2fs_convert_inline_inode(inode);
3982 if (!f2fs_disable_compressed_file(inode))
3985 f2fs_precache_extents(inode);
3987 ret = check_swap_activate(sis, file, span);
3991 set_inode_flag(inode, FI_PIN_FILE);
3992 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3996 static void f2fs_swap_deactivate(struct file *file)
3998 struct inode *inode = file_inode(file);
4000 clear_inode_flag(inode, FI_PIN_FILE);
4003 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4009 static void f2fs_swap_deactivate(struct file *file)
4014 const struct address_space_operations f2fs_dblock_aops = {
4015 .read_folio = f2fs_read_data_folio,
4016 .readahead = f2fs_readahead,
4017 .writepage = f2fs_write_data_page,
4018 .writepages = f2fs_write_data_pages,
4019 .write_begin = f2fs_write_begin,
4020 .write_end = f2fs_write_end,
4021 .dirty_folio = f2fs_dirty_data_folio,
4022 .invalidate_folio = f2fs_invalidate_folio,
4023 .release_folio = f2fs_release_folio,
4024 .direct_IO = noop_direct_IO,
4026 .swap_activate = f2fs_swap_activate,
4027 .swap_deactivate = f2fs_swap_deactivate,
4028 #ifdef CONFIG_MIGRATION
4029 .migratepage = f2fs_migrate_page,
4033 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4035 struct address_space *mapping = page_mapping(page);
4036 unsigned long flags;
4038 xa_lock_irqsave(&mapping->i_pages, flags);
4039 __xa_clear_mark(&mapping->i_pages, page_index(page),
4040 PAGECACHE_TAG_DIRTY);
4041 xa_unlock_irqrestore(&mapping->i_pages, flags);
4044 int __init f2fs_init_post_read_processing(void)
4046 bio_post_read_ctx_cache =
4047 kmem_cache_create("f2fs_bio_post_read_ctx",
4048 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4049 if (!bio_post_read_ctx_cache)
4051 bio_post_read_ctx_pool =
4052 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4053 bio_post_read_ctx_cache);
4054 if (!bio_post_read_ctx_pool)
4055 goto fail_free_cache;
4059 kmem_cache_destroy(bio_post_read_ctx_cache);
4064 void f2fs_destroy_post_read_processing(void)
4066 mempool_destroy(bio_post_read_ctx_pool);
4067 kmem_cache_destroy(bio_post_read_ctx_cache);
4070 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4072 if (!f2fs_sb_has_encrypt(sbi) &&
4073 !f2fs_sb_has_verity(sbi) &&
4074 !f2fs_sb_has_compression(sbi))
4077 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4078 WQ_UNBOUND | WQ_HIGHPRI,
4080 if (!sbi->post_read_wq)
4085 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4087 if (sbi->post_read_wq)
4088 destroy_workqueue(sbi->post_read_wq);
4091 int __init f2fs_init_bio_entry_cache(void)
4093 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4094 sizeof(struct bio_entry));
4095 if (!bio_entry_slab)
4100 void f2fs_destroy_bio_entry_cache(void)
4102 kmem_cache_destroy(bio_entry_slab);
4105 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4106 unsigned int flags, struct iomap *iomap,
4107 struct iomap *srcmap)
4109 struct f2fs_map_blocks map = {};
4110 pgoff_t next_pgofs = 0;
4113 map.m_lblk = bytes_to_blks(inode, offset);
4114 map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1;
4115 map.m_next_pgofs = &next_pgofs;
4116 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4117 if (flags & IOMAP_WRITE)
4118 map.m_may_create = true;
4120 err = f2fs_map_blocks(inode, &map, flags & IOMAP_WRITE,
4121 F2FS_GET_BLOCK_DIO);
4125 iomap->offset = blks_to_bytes(inode, map.m_lblk);
4128 * When inline encryption is enabled, sometimes I/O to an encrypted file
4129 * has to be broken up to guarantee DUN contiguity. Handle this by
4130 * limiting the length of the mapping returned.
4132 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4134 if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) {
4135 iomap->length = blks_to_bytes(inode, map.m_len);
4136 if (map.m_flags & F2FS_MAP_MAPPED) {
4137 iomap->type = IOMAP_MAPPED;
4138 iomap->flags |= IOMAP_F_MERGED;
4140 iomap->type = IOMAP_UNWRITTEN;
4142 if (WARN_ON_ONCE(!__is_valid_data_blkaddr(map.m_pblk)))
4145 iomap->bdev = map.m_bdev;
4146 iomap->addr = blks_to_bytes(inode, map.m_pblk);
4148 iomap->length = blks_to_bytes(inode, next_pgofs) -
4150 iomap->type = IOMAP_HOLE;
4151 iomap->addr = IOMAP_NULL_ADDR;
4154 if (map.m_flags & F2FS_MAP_NEW)
4155 iomap->flags |= IOMAP_F_NEW;
4156 if ((inode->i_state & I_DIRTY_DATASYNC) ||
4157 offset + length > i_size_read(inode))
4158 iomap->flags |= IOMAP_F_DIRTY;
4163 const struct iomap_ops f2fs_iomap_ops = {
4164 .iomap_begin = f2fs_iomap_begin,