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 return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS);
46 void f2fs_destroy_bioset(void)
48 bioset_exit(&f2fs_bioset);
51 bool f2fs_is_cp_guaranteed(struct page *page)
53 struct address_space *mapping = page->mapping;
55 struct f2fs_sb_info *sbi;
60 inode = mapping->host;
61 sbi = F2FS_I_SB(inode);
63 if (inode->i_ino == F2FS_META_INO(sbi) ||
64 inode->i_ino == F2FS_NODE_INO(sbi) ||
65 S_ISDIR(inode->i_mode))
68 if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
69 page_private_gcing(page))
74 static enum count_type __read_io_type(struct page *page)
76 struct address_space *mapping = page_file_mapping(page);
79 struct inode *inode = mapping->host;
80 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
82 if (inode->i_ino == F2FS_META_INO(sbi))
85 if (inode->i_ino == F2FS_NODE_INO(sbi))
91 /* postprocessing steps for read bios */
92 enum bio_post_read_step {
93 #ifdef CONFIG_FS_ENCRYPTION
94 STEP_DECRYPT = BIT(0),
96 STEP_DECRYPT = 0, /* compile out the decryption-related code */
98 #ifdef CONFIG_F2FS_FS_COMPRESSION
99 STEP_DECOMPRESS = BIT(1),
101 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
103 #ifdef CONFIG_FS_VERITY
104 STEP_VERITY = BIT(2),
106 STEP_VERITY = 0, /* compile out the verity-related code */
110 struct bio_post_read_ctx {
112 struct f2fs_sb_info *sbi;
113 struct work_struct work;
114 unsigned int enabled_steps;
116 * decompression_attempted keeps track of whether
117 * f2fs_end_read_compressed_page() has been called on the pages in the
118 * bio that belong to a compressed cluster yet.
120 bool decompression_attempted;
125 * Update and unlock a bio's pages, and free the bio.
127 * This marks pages up-to-date only if there was no error in the bio (I/O error,
128 * decryption error, or verity error), as indicated by bio->bi_status.
130 * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk)
131 * aren't marked up-to-date here, as decompression is done on a per-compression-
132 * cluster basis rather than a per-bio basis. Instead, we only must do two
133 * things for each compressed page here: call f2fs_end_read_compressed_page()
134 * with failed=true if an error occurred before it would have normally gotten
135 * called (i.e., I/O error or decryption error, but *not* verity error), and
136 * release the bio's reference to the decompress_io_ctx of the page's cluster.
138 static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
141 struct bvec_iter_all iter_all;
142 struct bio_post_read_ctx *ctx = bio->bi_private;
144 bio_for_each_segment_all(bv, bio, iter_all) {
145 struct page *page = bv->bv_page;
147 if (f2fs_is_compressed_page(page)) {
148 if (ctx && !ctx->decompression_attempted)
149 f2fs_end_read_compressed_page(page, true, 0,
151 f2fs_put_page_dic(page, in_task);
156 ClearPageUptodate(page);
158 SetPageUptodate(page);
159 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
164 mempool_free(ctx, bio_post_read_ctx_pool);
168 static void f2fs_verify_bio(struct work_struct *work)
170 struct bio_post_read_ctx *ctx =
171 container_of(work, struct bio_post_read_ctx, work);
172 struct bio *bio = ctx->bio;
173 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
176 * fsverity_verify_bio() may call readahead() again, and while verity
177 * will be disabled for this, decryption and/or decompression may still
178 * be needed, resulting in another bio_post_read_ctx being allocated.
179 * So to prevent deadlocks we need to release the current ctx to the
180 * mempool first. This assumes that verity is the last post-read step.
182 mempool_free(ctx, bio_post_read_ctx_pool);
183 bio->bi_private = NULL;
186 * Verify the bio's pages with fs-verity. Exclude compressed pages,
187 * as those were handled separately by f2fs_end_read_compressed_page().
189 if (may_have_compressed_pages) {
191 struct bvec_iter_all iter_all;
193 bio_for_each_segment_all(bv, bio, iter_all) {
194 struct page *page = bv->bv_page;
196 if (!f2fs_is_compressed_page(page) &&
197 !fsverity_verify_page(page)) {
198 bio->bi_status = BLK_STS_IOERR;
203 fsverity_verify_bio(bio);
206 f2fs_finish_read_bio(bio, true);
210 * If the bio's data needs to be verified with fs-verity, then enqueue the
211 * verity work for the bio. Otherwise finish the bio now.
213 * Note that to avoid deadlocks, the verity work can't be done on the
214 * decryption/decompression workqueue. This is because verifying the data pages
215 * can involve reading verity metadata pages from the file, and these verity
216 * metadata pages may be encrypted and/or compressed.
218 static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
220 struct bio_post_read_ctx *ctx = bio->bi_private;
222 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
223 INIT_WORK(&ctx->work, f2fs_verify_bio);
224 fsverity_enqueue_verify_work(&ctx->work);
226 f2fs_finish_read_bio(bio, in_task);
231 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
232 * remaining page was read by @ctx->bio.
234 * Note that a bio may span clusters (even a mix of compressed and uncompressed
235 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
236 * that the bio includes at least one compressed page. The actual decompression
237 * is done on a per-cluster basis, not a per-bio basis.
239 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
243 struct bvec_iter_all iter_all;
244 bool all_compressed = true;
245 block_t blkaddr = ctx->fs_blkaddr;
247 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
248 struct page *page = bv->bv_page;
250 if (f2fs_is_compressed_page(page))
251 f2fs_end_read_compressed_page(page, false, blkaddr,
254 all_compressed = false;
259 ctx->decompression_attempted = true;
262 * Optimization: if all the bio's pages are compressed, then scheduling
263 * the per-bio verity work is unnecessary, as verity will be fully
264 * handled at the compression cluster level.
267 ctx->enabled_steps &= ~STEP_VERITY;
270 static void f2fs_post_read_work(struct work_struct *work)
272 struct bio_post_read_ctx *ctx =
273 container_of(work, struct bio_post_read_ctx, work);
274 struct bio *bio = ctx->bio;
276 if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) {
277 f2fs_finish_read_bio(bio, true);
281 if (ctx->enabled_steps & STEP_DECOMPRESS)
282 f2fs_handle_step_decompress(ctx, true);
284 f2fs_verify_and_finish_bio(bio, true);
287 static void f2fs_read_end_io(struct bio *bio)
289 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
290 struct bio_post_read_ctx *ctx;
291 bool intask = in_task();
293 iostat_update_and_unbind_ctx(bio);
294 ctx = bio->bi_private;
296 if (time_to_inject(sbi, FAULT_READ_IO))
297 bio->bi_status = BLK_STS_IOERR;
299 if (bio->bi_status) {
300 f2fs_finish_read_bio(bio, intask);
305 unsigned int enabled_steps = ctx->enabled_steps &
306 (STEP_DECRYPT | STEP_DECOMPRESS);
309 * If we have only decompression step between decompression and
310 * decrypt, we don't need post processing for this.
312 if (enabled_steps == STEP_DECOMPRESS &&
313 !f2fs_low_mem_mode(sbi)) {
314 f2fs_handle_step_decompress(ctx, intask);
315 } else if (enabled_steps) {
316 INIT_WORK(&ctx->work, f2fs_post_read_work);
317 queue_work(ctx->sbi->post_read_wq, &ctx->work);
322 f2fs_verify_and_finish_bio(bio, intask);
325 static void f2fs_write_end_io(struct bio *bio)
327 struct f2fs_sb_info *sbi;
328 struct bio_vec *bvec;
329 struct bvec_iter_all iter_all;
331 iostat_update_and_unbind_ctx(bio);
332 sbi = bio->bi_private;
334 if (time_to_inject(sbi, FAULT_WRITE_IO))
335 bio->bi_status = BLK_STS_IOERR;
337 bio_for_each_segment_all(bvec, bio, iter_all) {
338 struct page *page = bvec->bv_page;
339 enum count_type type = WB_DATA_TYPE(page, false);
341 fscrypt_finalize_bounce_page(&page);
343 #ifdef CONFIG_F2FS_FS_COMPRESSION
344 if (f2fs_is_compressed_page(page)) {
345 f2fs_compress_write_end_io(bio, page);
350 if (unlikely(bio->bi_status)) {
351 mapping_set_error(page->mapping, -EIO);
352 if (type == F2FS_WB_CP_DATA)
353 f2fs_stop_checkpoint(sbi, true,
354 STOP_CP_REASON_WRITE_FAIL);
357 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
358 page->index != nid_of_node(page));
360 dec_page_count(sbi, type);
361 if (f2fs_in_warm_node_list(sbi, page))
362 f2fs_del_fsync_node_entry(sbi, page);
363 clear_page_private_gcing(page);
364 end_page_writeback(page);
366 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
367 wq_has_sleeper(&sbi->cp_wait))
368 wake_up(&sbi->cp_wait);
373 #ifdef CONFIG_BLK_DEV_ZONED
374 static void f2fs_zone_write_end_io(struct bio *bio)
376 struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private;
378 bio->bi_private = io->bi_private;
379 complete(&io->zone_wait);
380 f2fs_write_end_io(bio);
384 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
385 block_t blk_addr, sector_t *sector)
387 struct block_device *bdev = sbi->sb->s_bdev;
390 if (f2fs_is_multi_device(sbi)) {
391 for (i = 0; i < sbi->s_ndevs; i++) {
392 if (FDEV(i).start_blk <= blk_addr &&
393 FDEV(i).end_blk >= blk_addr) {
394 blk_addr -= FDEV(i).start_blk;
402 *sector = SECTOR_FROM_BLOCK(blk_addr);
406 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
410 if (!f2fs_is_multi_device(sbi))
413 for (i = 0; i < sbi->s_ndevs; i++)
414 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
419 static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio)
421 unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0);
422 unsigned int fua_flag, meta_flag, io_flag;
423 blk_opf_t op_flags = 0;
425 if (fio->op != REQ_OP_WRITE)
427 if (fio->type == DATA)
428 io_flag = fio->sbi->data_io_flag;
429 else if (fio->type == NODE)
430 io_flag = fio->sbi->node_io_flag;
434 fua_flag = io_flag & temp_mask;
435 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
438 * data/node io flag bits per temp:
439 * REQ_META | REQ_FUA |
440 * 5 | 4 | 3 | 2 | 1 | 0 |
441 * Cold | Warm | Hot | Cold | Warm | Hot |
443 if (BIT(fio->temp) & meta_flag)
444 op_flags |= REQ_META;
445 if (BIT(fio->temp) & fua_flag)
450 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
452 struct f2fs_sb_info *sbi = fio->sbi;
453 struct block_device *bdev;
457 bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or);
458 bio = bio_alloc_bioset(bdev, npages,
459 fio->op | fio->op_flags | f2fs_io_flags(fio),
460 GFP_NOIO, &f2fs_bioset);
461 bio->bi_iter.bi_sector = sector;
462 if (is_read_io(fio->op)) {
463 bio->bi_end_io = f2fs_read_end_io;
464 bio->bi_private = NULL;
466 bio->bi_end_io = f2fs_write_end_io;
467 bio->bi_private = sbi;
469 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
472 wbc_init_bio(fio->io_wbc, bio);
477 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
479 const struct f2fs_io_info *fio,
483 * The f2fs garbage collector sets ->encrypted_page when it wants to
484 * read/write raw data without encryption.
486 if (!fio || !fio->encrypted_page)
487 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
490 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
492 const struct f2fs_io_info *fio)
495 * The f2fs garbage collector sets ->encrypted_page when it wants to
496 * read/write raw data without encryption.
498 if (fio && fio->encrypted_page)
499 return !bio_has_crypt_ctx(bio);
501 return fscrypt_mergeable_bio(bio, inode, next_idx);
504 void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
507 WARN_ON_ONCE(!is_read_io(bio_op(bio)));
508 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
510 iostat_update_submit_ctx(bio, type);
514 static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio,
517 WARN_ON_ONCE(is_read_io(bio_op(bio)));
519 if (f2fs_lfs_mode(sbi) && current->plug && PAGE_TYPE_ON_MAIN(type))
520 blk_finish_plug(current->plug);
522 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
523 iostat_update_submit_ctx(bio, type);
527 static void __submit_merged_bio(struct f2fs_bio_info *io)
529 struct f2fs_io_info *fio = &io->fio;
534 if (is_read_io(fio->op)) {
535 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
536 f2fs_submit_read_bio(io->sbi, io->bio, fio->type);
538 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
539 f2fs_submit_write_bio(io->sbi, io->bio, fio->type);
544 static bool __has_merged_page(struct bio *bio, struct inode *inode,
545 struct page *page, nid_t ino)
547 struct bio_vec *bvec;
548 struct bvec_iter_all iter_all;
553 if (!inode && !page && !ino)
556 bio_for_each_segment_all(bvec, bio, iter_all) {
557 struct page *target = bvec->bv_page;
559 if (fscrypt_is_bounce_page(target)) {
560 target = fscrypt_pagecache_page(target);
564 if (f2fs_is_compressed_page(target)) {
565 target = f2fs_compress_control_page(target);
570 if (inode && inode == target->mapping->host)
572 if (page && page == target)
574 if (ino && ino == ino_of_node(target))
581 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
585 for (i = 0; i < NR_PAGE_TYPE; i++) {
586 int n = (i == META) ? 1 : NR_TEMP_TYPE;
589 sbi->write_io[i] = f2fs_kmalloc(sbi,
590 array_size(n, sizeof(struct f2fs_bio_info)),
592 if (!sbi->write_io[i])
595 for (j = HOT; j < n; j++) {
596 init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem);
597 sbi->write_io[i][j].sbi = sbi;
598 sbi->write_io[i][j].bio = NULL;
599 spin_lock_init(&sbi->write_io[i][j].io_lock);
600 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
601 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
602 init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock);
603 #ifdef CONFIG_BLK_DEV_ZONED
604 init_completion(&sbi->write_io[i][j].zone_wait);
605 sbi->write_io[i][j].zone_pending_bio = NULL;
606 sbi->write_io[i][j].bi_private = NULL;
614 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
615 enum page_type type, enum temp_type temp)
617 enum page_type btype = PAGE_TYPE_OF_BIO(type);
618 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
620 f2fs_down_write(&io->io_rwsem);
625 /* change META to META_FLUSH in the checkpoint procedure */
626 if (type >= META_FLUSH) {
627 io->fio.type = META_FLUSH;
628 io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
629 if (!test_opt(sbi, NOBARRIER))
630 io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
632 __submit_merged_bio(io);
634 f2fs_up_write(&io->io_rwsem);
637 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
638 struct inode *inode, struct page *page,
639 nid_t ino, enum page_type type, bool force)
644 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
646 enum page_type btype = PAGE_TYPE_OF_BIO(type);
647 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
649 f2fs_down_read(&io->io_rwsem);
650 ret = __has_merged_page(io->bio, inode, page, ino);
651 f2fs_up_read(&io->io_rwsem);
654 __f2fs_submit_merged_write(sbi, type, temp);
656 /* TODO: use HOT temp only for meta pages now. */
662 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
664 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
667 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
668 struct inode *inode, struct page *page,
669 nid_t ino, enum page_type type)
671 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
674 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
676 f2fs_submit_merged_write(sbi, DATA);
677 f2fs_submit_merged_write(sbi, NODE);
678 f2fs_submit_merged_write(sbi, META);
682 * Fill the locked page with data located in the block address.
683 * A caller needs to unlock the page on failure.
685 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
688 struct page *page = fio->encrypted_page ?
689 fio->encrypted_page : fio->page;
691 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
692 fio->is_por ? META_POR : (__is_meta_io(fio) ?
693 META_GENERIC : DATA_GENERIC_ENHANCE)))
694 return -EFSCORRUPTED;
696 trace_f2fs_submit_page_bio(page, fio);
698 /* Allocate a new bio */
699 bio = __bio_alloc(fio, 1);
701 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
702 fio->page->index, fio, GFP_NOIO);
704 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
709 if (fio->io_wbc && !is_read_io(fio->op))
710 wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE);
712 inc_page_count(fio->sbi, is_read_io(fio->op) ?
713 __read_io_type(page) : WB_DATA_TYPE(fio->page, false));
715 if (is_read_io(bio_op(bio)))
716 f2fs_submit_read_bio(fio->sbi, bio, fio->type);
718 f2fs_submit_write_bio(fio->sbi, bio, fio->type);
722 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
723 block_t last_blkaddr, block_t cur_blkaddr)
725 if (unlikely(sbi->max_io_bytes &&
726 bio->bi_iter.bi_size >= sbi->max_io_bytes))
728 if (last_blkaddr + 1 != cur_blkaddr)
730 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
733 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
734 struct f2fs_io_info *fio)
736 if (io->fio.op != fio->op)
738 return io->fio.op_flags == fio->op_flags;
741 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
742 struct f2fs_bio_info *io,
743 struct f2fs_io_info *fio,
744 block_t last_blkaddr,
747 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
749 return io_type_is_mergeable(io, fio);
752 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
753 struct page *page, enum temp_type temp)
755 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
756 struct bio_entry *be;
758 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
762 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
765 f2fs_down_write(&io->bio_list_lock);
766 list_add_tail(&be->list, &io->bio_list);
767 f2fs_up_write(&io->bio_list_lock);
770 static void del_bio_entry(struct bio_entry *be)
773 kmem_cache_free(bio_entry_slab, be);
776 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
779 struct f2fs_sb_info *sbi = fio->sbi;
784 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
785 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
786 struct list_head *head = &io->bio_list;
787 struct bio_entry *be;
789 f2fs_down_write(&io->bio_list_lock);
790 list_for_each_entry(be, head, list) {
796 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
799 if (f2fs_crypt_mergeable_bio(*bio,
800 fio->page->mapping->host,
801 fio->page->index, fio) &&
802 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
808 /* page can't be merged into bio; submit the bio */
810 f2fs_submit_write_bio(sbi, *bio, DATA);
813 f2fs_up_write(&io->bio_list_lock);
824 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
825 struct bio **bio, struct page *page)
829 struct bio *target = bio ? *bio : NULL;
831 f2fs_bug_on(sbi, !target && !page);
833 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
834 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
835 struct list_head *head = &io->bio_list;
836 struct bio_entry *be;
838 if (list_empty(head))
841 f2fs_down_read(&io->bio_list_lock);
842 list_for_each_entry(be, head, list) {
844 found = (target == be->bio);
846 found = __has_merged_page(be->bio, NULL,
851 f2fs_up_read(&io->bio_list_lock);
858 f2fs_down_write(&io->bio_list_lock);
859 list_for_each_entry(be, head, list) {
861 found = (target == be->bio);
863 found = __has_merged_page(be->bio, NULL,
871 f2fs_up_write(&io->bio_list_lock);
875 f2fs_submit_write_bio(sbi, target, DATA);
882 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
884 struct bio *bio = *fio->bio;
885 struct page *page = fio->encrypted_page ?
886 fio->encrypted_page : fio->page;
888 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
889 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
890 return -EFSCORRUPTED;
892 trace_f2fs_submit_page_bio(page, fio);
894 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
896 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
899 bio = __bio_alloc(fio, BIO_MAX_VECS);
900 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
901 fio->page->index, fio, GFP_NOIO);
903 add_bio_entry(fio->sbi, bio, page, fio->temp);
905 if (add_ipu_page(fio, &bio, page))
910 wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE);
912 inc_page_count(fio->sbi, WB_DATA_TYPE(page, false));
914 *fio->last_block = fio->new_blkaddr;
920 #ifdef CONFIG_BLK_DEV_ZONED
921 static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
925 if (f2fs_is_multi_device(sbi)) {
926 devi = f2fs_target_device_index(sbi, blkaddr);
927 if (blkaddr < FDEV(devi).start_blk ||
928 blkaddr > FDEV(devi).end_blk) {
929 f2fs_err(sbi, "Invalid block %x", blkaddr);
932 blkaddr -= FDEV(devi).start_blk;
934 return bdev_is_zoned(FDEV(devi).bdev) &&
935 f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
936 (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
940 void f2fs_submit_page_write(struct f2fs_io_info *fio)
942 struct f2fs_sb_info *sbi = fio->sbi;
943 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
944 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
945 struct page *bio_page;
946 enum count_type type;
948 f2fs_bug_on(sbi, is_read_io(fio->op));
950 f2fs_down_write(&io->io_rwsem);
952 #ifdef CONFIG_BLK_DEV_ZONED
953 if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
954 wait_for_completion_io(&io->zone_wait);
955 bio_put(io->zone_pending_bio);
956 io->zone_pending_bio = NULL;
957 io->bi_private = NULL;
962 spin_lock(&io->io_lock);
963 if (list_empty(&io->io_list)) {
964 spin_unlock(&io->io_lock);
967 fio = list_first_entry(&io->io_list,
968 struct f2fs_io_info, list);
969 list_del(&fio->list);
970 spin_unlock(&io->io_lock);
973 verify_fio_blkaddr(fio);
975 if (fio->encrypted_page)
976 bio_page = fio->encrypted_page;
977 else if (fio->compressed_page)
978 bio_page = fio->compressed_page;
980 bio_page = fio->page;
982 /* set submitted = true as a return value */
985 type = WB_DATA_TYPE(bio_page, fio->compressed_page);
986 inc_page_count(sbi, type);
989 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
991 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
992 bio_page->index, fio)))
993 __submit_merged_bio(io);
995 if (io->bio == NULL) {
996 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
997 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
998 bio_page->index, fio, GFP_NOIO);
1002 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
1003 __submit_merged_bio(io);
1008 wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE);
1010 io->last_block_in_bio = fio->new_blkaddr;
1012 trace_f2fs_submit_page_write(fio->page, fio);
1013 #ifdef CONFIG_BLK_DEV_ZONED
1014 if (f2fs_sb_has_blkzoned(sbi) && btype < META &&
1015 is_end_zone_blkaddr(sbi, fio->new_blkaddr)) {
1017 reinit_completion(&io->zone_wait);
1018 io->bi_private = io->bio->bi_private;
1019 io->bio->bi_private = io;
1020 io->bio->bi_end_io = f2fs_zone_write_end_io;
1021 io->zone_pending_bio = io->bio;
1022 __submit_merged_bio(io);
1028 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1029 !f2fs_is_checkpoint_ready(sbi))
1030 __submit_merged_bio(io);
1031 f2fs_up_write(&io->io_rwsem);
1034 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1035 unsigned nr_pages, blk_opf_t op_flag,
1036 pgoff_t first_idx, bool for_write)
1038 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1040 struct bio_post_read_ctx *ctx = NULL;
1041 unsigned int post_read_steps = 0;
1043 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or);
1045 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
1046 REQ_OP_READ | op_flag,
1047 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
1049 return ERR_PTR(-ENOMEM);
1050 bio->bi_iter.bi_sector = sector;
1051 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1052 bio->bi_end_io = f2fs_read_end_io;
1054 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1055 post_read_steps |= STEP_DECRYPT;
1057 if (f2fs_need_verity(inode, first_idx))
1058 post_read_steps |= STEP_VERITY;
1061 * STEP_DECOMPRESS is handled specially, since a compressed file might
1062 * contain both compressed and uncompressed clusters. We'll allocate a
1063 * bio_post_read_ctx if the file is compressed, but the caller is
1064 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1067 if (post_read_steps || f2fs_compressed_file(inode)) {
1068 /* Due to the mempool, this never fails. */
1069 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1072 ctx->enabled_steps = post_read_steps;
1073 ctx->fs_blkaddr = blkaddr;
1074 ctx->decompression_attempted = false;
1075 bio->bi_private = ctx;
1077 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1082 /* This can handle encryption stuffs */
1083 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1084 block_t blkaddr, blk_opf_t op_flags,
1087 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1090 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1091 page->index, for_write);
1093 return PTR_ERR(bio);
1095 /* wait for GCed page writeback via META_MAPPING */
1096 f2fs_wait_on_block_writeback(inode, blkaddr);
1098 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1099 iostat_update_and_unbind_ctx(bio);
1100 if (bio->bi_private)
1101 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1105 inc_page_count(sbi, F2FS_RD_DATA);
1106 f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE);
1107 f2fs_submit_read_bio(sbi, bio, DATA);
1111 static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1113 __le32 *addr = get_dnode_addr(dn->inode, dn->node_page);
1115 dn->data_blkaddr = blkaddr;
1116 addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1120 * Lock ordering for the change of data block address:
1123 * update block addresses in the node page
1125 void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1127 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1128 __set_data_blkaddr(dn, blkaddr);
1129 if (set_page_dirty(dn->node_page))
1130 dn->node_changed = true;
1133 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1135 f2fs_set_data_blkaddr(dn, blkaddr);
1136 f2fs_update_read_extent_cache(dn);
1139 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1140 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1142 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1148 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1150 err = inc_valid_block_count(sbi, dn->inode, &count, true);
1154 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1155 dn->ofs_in_node, count);
1157 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1159 for (; count > 0; dn->ofs_in_node++) {
1160 block_t blkaddr = f2fs_data_blkaddr(dn);
1162 if (blkaddr == NULL_ADDR) {
1163 __set_data_blkaddr(dn, NEW_ADDR);
1168 if (set_page_dirty(dn->node_page))
1169 dn->node_changed = true;
1173 /* Should keep dn->ofs_in_node unchanged */
1174 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1176 unsigned int ofs_in_node = dn->ofs_in_node;
1179 ret = f2fs_reserve_new_blocks(dn, 1);
1180 dn->ofs_in_node = ofs_in_node;
1184 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1186 bool need_put = dn->inode_page ? false : true;
1189 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1193 if (dn->data_blkaddr == NULL_ADDR)
1194 err = f2fs_reserve_new_block(dn);
1195 if (err || need_put)
1200 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1201 blk_opf_t op_flags, bool for_write,
1202 pgoff_t *next_pgofs)
1204 struct address_space *mapping = inode->i_mapping;
1205 struct dnode_of_data dn;
1209 page = f2fs_grab_cache_page(mapping, index, for_write);
1211 return ERR_PTR(-ENOMEM);
1213 if (f2fs_lookup_read_extent_cache_block(inode, index,
1214 &dn.data_blkaddr)) {
1215 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1216 DATA_GENERIC_ENHANCE_READ)) {
1217 err = -EFSCORRUPTED;
1223 set_new_dnode(&dn, inode, NULL, NULL, 0);
1224 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1226 if (err == -ENOENT && next_pgofs)
1227 *next_pgofs = f2fs_get_next_page_offset(&dn, index);
1230 f2fs_put_dnode(&dn);
1232 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1235 *next_pgofs = index + 1;
1238 if (dn.data_blkaddr != NEW_ADDR &&
1239 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1241 DATA_GENERIC_ENHANCE)) {
1242 err = -EFSCORRUPTED;
1246 if (PageUptodate(page)) {
1252 * A new dentry page is allocated but not able to be written, since its
1253 * new inode page couldn't be allocated due to -ENOSPC.
1254 * In such the case, its blkaddr can be remained as NEW_ADDR.
1255 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1256 * f2fs_init_inode_metadata.
1258 if (dn.data_blkaddr == NEW_ADDR) {
1259 zero_user_segment(page, 0, PAGE_SIZE);
1260 if (!PageUptodate(page))
1261 SetPageUptodate(page);
1266 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1267 op_flags, for_write);
1273 f2fs_put_page(page, 1);
1274 return ERR_PTR(err);
1277 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index,
1278 pgoff_t *next_pgofs)
1280 struct address_space *mapping = inode->i_mapping;
1283 page = find_get_page(mapping, index);
1284 if (page && PageUptodate(page))
1286 f2fs_put_page(page, 0);
1288 page = f2fs_get_read_data_page(inode, index, 0, false, next_pgofs);
1292 if (PageUptodate(page))
1295 wait_on_page_locked(page);
1296 if (unlikely(!PageUptodate(page))) {
1297 f2fs_put_page(page, 0);
1298 return ERR_PTR(-EIO);
1304 * If it tries to access a hole, return an error.
1305 * Because, the callers, functions in dir.c and GC, should be able to know
1306 * whether this page exists or not.
1308 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1311 struct address_space *mapping = inode->i_mapping;
1314 page = f2fs_get_read_data_page(inode, index, 0, for_write, NULL);
1318 /* wait for read completion */
1320 if (unlikely(page->mapping != mapping || !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, false);
1402 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1403 if (dn->data_blkaddr == NULL_ADDR) {
1404 err = inc_valid_block_count(sbi, dn->inode, &count, true);
1409 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1410 old_blkaddr = dn->data_blkaddr;
1411 err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
1412 &dn->data_blkaddr, &sum, seg_type, NULL);
1416 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1417 f2fs_invalidate_internal_cache(sbi, old_blkaddr);
1419 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1423 static void f2fs_map_lock(struct f2fs_sb_info *sbi, int flag)
1425 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1426 f2fs_down_read(&sbi->node_change);
1431 static void f2fs_map_unlock(struct f2fs_sb_info *sbi, int flag)
1433 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1434 f2fs_up_read(&sbi->node_change);
1436 f2fs_unlock_op(sbi);
1439 int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index)
1441 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1444 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
1445 if (!f2fs_lookup_read_extent_cache_block(dn->inode, index,
1447 err = f2fs_reserve_block(dn, index);
1448 f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
1453 static int f2fs_map_no_dnode(struct inode *inode,
1454 struct f2fs_map_blocks *map, struct dnode_of_data *dn,
1457 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1460 * There is one exceptional case that read_node_page() may return
1461 * -ENOENT due to filesystem has been shutdown or cp_error, return
1462 * -EIO in that case.
1464 if (map->m_may_create &&
1465 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi)))
1468 if (map->m_next_pgofs)
1469 *map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff);
1470 if (map->m_next_extent)
1471 *map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff);
1475 static bool f2fs_map_blocks_cached(struct inode *inode,
1476 struct f2fs_map_blocks *map, int flag)
1478 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1479 unsigned int maxblocks = map->m_len;
1480 pgoff_t pgoff = (pgoff_t)map->m_lblk;
1481 struct extent_info ei = {};
1483 if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei))
1486 map->m_pblk = ei.blk + pgoff - ei.fofs;
1487 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff);
1488 map->m_flags = F2FS_MAP_MAPPED;
1489 if (map->m_next_extent)
1490 *map->m_next_extent = pgoff + map->m_len;
1492 /* for hardware encryption, but to avoid potential issue in future */
1493 if (flag == F2FS_GET_BLOCK_DIO)
1494 f2fs_wait_on_block_writeback_range(inode,
1495 map->m_pblk, map->m_len);
1497 if (f2fs_allow_multi_device_dio(sbi, flag)) {
1498 int bidx = f2fs_target_device_index(sbi, map->m_pblk);
1499 struct f2fs_dev_info *dev = &sbi->devs[bidx];
1501 map->m_bdev = dev->bdev;
1502 map->m_pblk -= dev->start_blk;
1503 map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk);
1505 map->m_bdev = inode->i_sb->s_bdev;
1511 * f2fs_map_blocks() tries to find or build mapping relationship which
1512 * maps continuous logical blocks to physical blocks, and return such
1513 * info via f2fs_map_blocks structure.
1515 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
1517 unsigned int maxblocks = map->m_len;
1518 struct dnode_of_data dn;
1519 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1520 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1521 pgoff_t pgofs, end_offset, end;
1522 int err = 0, ofs = 1;
1523 unsigned int ofs_in_node, last_ofs_in_node;
1526 unsigned int start_pgofs;
1533 if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag))
1536 map->m_bdev = inode->i_sb->s_bdev;
1537 map->m_multidev_dio =
1538 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1543 /* it only supports block size == page size */
1544 pgofs = (pgoff_t)map->m_lblk;
1545 end = pgofs + maxblocks;
1548 if (map->m_may_create)
1549 f2fs_map_lock(sbi, flag);
1551 /* When reading holes, we need its node page */
1552 set_new_dnode(&dn, inode, NULL, NULL, 0);
1553 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1555 if (flag == F2FS_GET_BLOCK_BMAP)
1558 err = f2fs_map_no_dnode(inode, map, &dn, pgofs);
1562 start_pgofs = pgofs;
1564 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1565 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1568 blkaddr = f2fs_data_blkaddr(&dn);
1569 is_hole = !__is_valid_data_blkaddr(blkaddr);
1571 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1572 err = -EFSCORRUPTED;
1576 /* use out-place-update for direct IO under LFS mode */
1577 if (map->m_may_create &&
1578 (is_hole || (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO))) {
1579 if (unlikely(f2fs_cp_error(sbi))) {
1585 case F2FS_GET_BLOCK_PRE_AIO:
1586 if (blkaddr == NULL_ADDR) {
1588 last_ofs_in_node = dn.ofs_in_node;
1591 case F2FS_GET_BLOCK_PRE_DIO:
1592 case F2FS_GET_BLOCK_DIO:
1593 err = __allocate_data_block(&dn, map->m_seg_type);
1596 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1597 file_need_truncate(inode);
1598 set_inode_flag(inode, FI_APPEND_WRITE);
1606 blkaddr = dn.data_blkaddr;
1608 map->m_flags |= F2FS_MAP_NEW;
1609 } else if (is_hole) {
1610 if (f2fs_compressed_file(inode) &&
1611 f2fs_sanity_check_cluster(&dn)) {
1612 err = -EFSCORRUPTED;
1613 f2fs_handle_error(sbi,
1614 ERROR_CORRUPTED_CLUSTER);
1619 case F2FS_GET_BLOCK_PRECACHE:
1621 case F2FS_GET_BLOCK_BMAP:
1624 case F2FS_GET_BLOCK_FIEMAP:
1625 if (blkaddr == NULL_ADDR) {
1626 if (map->m_next_pgofs)
1627 *map->m_next_pgofs = pgofs + 1;
1632 /* for defragment case */
1633 if (map->m_next_pgofs)
1634 *map->m_next_pgofs = pgofs + 1;
1639 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1642 if (map->m_multidev_dio)
1643 bidx = f2fs_target_device_index(sbi, blkaddr);
1645 if (map->m_len == 0) {
1646 /* reserved delalloc block should be mapped for fiemap. */
1647 if (blkaddr == NEW_ADDR)
1648 map->m_flags |= F2FS_MAP_DELALLOC;
1649 map->m_flags |= F2FS_MAP_MAPPED;
1651 map->m_pblk = blkaddr;
1654 if (map->m_multidev_dio)
1655 map->m_bdev = FDEV(bidx).bdev;
1656 } else if ((map->m_pblk != NEW_ADDR &&
1657 blkaddr == (map->m_pblk + ofs)) ||
1658 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1659 flag == F2FS_GET_BLOCK_PRE_DIO) {
1660 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1672 /* preallocate blocks in batch for one dnode page */
1673 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1674 (pgofs == end || dn.ofs_in_node == end_offset)) {
1676 dn.ofs_in_node = ofs_in_node;
1677 err = f2fs_reserve_new_blocks(&dn, prealloc);
1681 map->m_len += dn.ofs_in_node - ofs_in_node;
1682 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1686 dn.ofs_in_node = end_offset;
1691 else if (dn.ofs_in_node < end_offset)
1694 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1695 if (map->m_flags & F2FS_MAP_MAPPED) {
1696 unsigned int ofs = start_pgofs - map->m_lblk;
1698 f2fs_update_read_extent_cache_range(&dn,
1699 start_pgofs, map->m_pblk + ofs,
1704 f2fs_put_dnode(&dn);
1706 if (map->m_may_create) {
1707 f2fs_map_unlock(sbi, flag);
1708 f2fs_balance_fs(sbi, dn.node_changed);
1714 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1716 * for hardware encryption, but to avoid potential issue
1719 f2fs_wait_on_block_writeback_range(inode,
1720 map->m_pblk, map->m_len);
1722 if (map->m_multidev_dio) {
1723 block_t blk_addr = map->m_pblk;
1725 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1727 map->m_bdev = FDEV(bidx).bdev;
1728 map->m_pblk -= FDEV(bidx).start_blk;
1730 if (map->m_may_create)
1731 f2fs_update_device_state(sbi, inode->i_ino,
1732 blk_addr, map->m_len);
1734 f2fs_bug_on(sbi, blk_addr + map->m_len >
1735 FDEV(bidx).end_blk + 1);
1739 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1740 if (map->m_flags & F2FS_MAP_MAPPED) {
1741 unsigned int ofs = start_pgofs - map->m_lblk;
1743 f2fs_update_read_extent_cache_range(&dn,
1744 start_pgofs, map->m_pblk + ofs,
1747 if (map->m_next_extent)
1748 *map->m_next_extent = pgofs + 1;
1750 f2fs_put_dnode(&dn);
1752 if (map->m_may_create) {
1753 f2fs_map_unlock(sbi, flag);
1754 f2fs_balance_fs(sbi, dn.node_changed);
1757 trace_f2fs_map_blocks(inode, map, flag, err);
1761 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1763 struct f2fs_map_blocks map;
1767 if (pos + len > i_size_read(inode))
1770 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1771 map.m_next_pgofs = NULL;
1772 map.m_next_extent = NULL;
1773 map.m_seg_type = NO_CHECK_TYPE;
1774 map.m_may_create = false;
1775 last_lblk = F2FS_BLK_ALIGN(pos + len);
1777 while (map.m_lblk < last_lblk) {
1778 map.m_len = last_lblk - map.m_lblk;
1779 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
1780 if (err || map.m_len == 0)
1782 map.m_lblk += map.m_len;
1787 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1789 return (bytes >> inode->i_blkbits);
1792 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1794 return (blks << inode->i_blkbits);
1797 static int f2fs_xattr_fiemap(struct inode *inode,
1798 struct fiemap_extent_info *fieinfo)
1800 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1802 struct node_info ni;
1803 __u64 phys = 0, len;
1805 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1808 if (f2fs_has_inline_xattr(inode)) {
1811 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1812 inode->i_ino, false);
1816 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
1818 f2fs_put_page(page, 1);
1822 phys = blks_to_bytes(inode, ni.blk_addr);
1823 offset = offsetof(struct f2fs_inode, i_addr) +
1824 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1825 get_inline_xattr_addrs(inode));
1828 len = inline_xattr_size(inode);
1830 f2fs_put_page(page, 1);
1832 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1835 flags |= FIEMAP_EXTENT_LAST;
1837 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1838 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1844 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1848 err = f2fs_get_node_info(sbi, xnid, &ni, false);
1850 f2fs_put_page(page, 1);
1854 phys = blks_to_bytes(inode, ni.blk_addr);
1855 len = inode->i_sb->s_blocksize;
1857 f2fs_put_page(page, 1);
1859 flags = FIEMAP_EXTENT_LAST;
1863 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1864 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1867 return (err < 0 ? err : 0);
1870 static loff_t max_inode_blocks(struct inode *inode)
1872 loff_t result = ADDRS_PER_INODE(inode);
1873 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1875 /* two direct node blocks */
1876 result += (leaf_count * 2);
1878 /* two indirect node blocks */
1879 leaf_count *= NIDS_PER_BLOCK;
1880 result += (leaf_count * 2);
1882 /* one double indirect node block */
1883 leaf_count *= NIDS_PER_BLOCK;
1884 result += leaf_count;
1889 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1892 struct f2fs_map_blocks map;
1893 sector_t start_blk, last_blk;
1895 u64 logical = 0, phys = 0, size = 0;
1898 bool compr_cluster = false, compr_appended;
1899 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1900 unsigned int count_in_cluster = 0;
1903 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1904 ret = f2fs_precache_extents(inode);
1909 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1913 inode_lock_shared(inode);
1915 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1916 if (start > maxbytes) {
1921 if (len > maxbytes || (maxbytes - len) < start)
1922 len = maxbytes - start;
1924 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1925 ret = f2fs_xattr_fiemap(inode, fieinfo);
1929 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1930 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1935 if (bytes_to_blks(inode, len) == 0)
1936 len = blks_to_bytes(inode, 1);
1938 start_blk = bytes_to_blks(inode, start);
1939 last_blk = bytes_to_blks(inode, start + len - 1);
1942 memset(&map, 0, sizeof(map));
1943 map.m_lblk = start_blk;
1944 map.m_len = bytes_to_blks(inode, len);
1945 map.m_next_pgofs = &next_pgofs;
1946 map.m_seg_type = NO_CHECK_TYPE;
1948 if (compr_cluster) {
1950 map.m_len = cluster_size - count_in_cluster;
1953 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
1958 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1959 start_blk = next_pgofs;
1961 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1962 max_inode_blocks(inode)))
1965 flags |= FIEMAP_EXTENT_LAST;
1968 compr_appended = false;
1969 /* In a case of compressed cluster, append this to the last extent */
1970 if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) ||
1971 !(map.m_flags & F2FS_MAP_FLAGS))) {
1972 compr_appended = true;
1977 flags |= FIEMAP_EXTENT_MERGED;
1978 if (IS_ENCRYPTED(inode))
1979 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1981 ret = fiemap_fill_next_extent(fieinfo, logical,
1983 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1989 if (start_blk > last_blk)
1993 if (map.m_pblk == COMPRESS_ADDR) {
1994 compr_cluster = true;
1995 count_in_cluster = 1;
1996 } else if (compr_appended) {
1997 unsigned int appended_blks = cluster_size -
1998 count_in_cluster + 1;
1999 size += blks_to_bytes(inode, appended_blks);
2000 start_blk += appended_blks;
2001 compr_cluster = false;
2003 logical = blks_to_bytes(inode, start_blk);
2004 phys = __is_valid_data_blkaddr(map.m_pblk) ?
2005 blks_to_bytes(inode, map.m_pblk) : 0;
2006 size = blks_to_bytes(inode, map.m_len);
2009 if (compr_cluster) {
2010 flags = FIEMAP_EXTENT_ENCODED;
2011 count_in_cluster += map.m_len;
2012 if (count_in_cluster == cluster_size) {
2013 compr_cluster = false;
2014 size += blks_to_bytes(inode, 1);
2016 } else if (map.m_flags & F2FS_MAP_DELALLOC) {
2017 flags = FIEMAP_EXTENT_UNWRITTEN;
2020 start_blk += bytes_to_blks(inode, size);
2025 if (fatal_signal_pending(current))
2033 inode_unlock_shared(inode);
2037 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2039 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
2040 return inode->i_sb->s_maxbytes;
2042 return i_size_read(inode);
2045 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2047 struct f2fs_map_blocks *map,
2048 struct bio **bio_ret,
2049 sector_t *last_block_in_bio,
2052 struct bio *bio = *bio_ret;
2053 const unsigned blocksize = blks_to_bytes(inode, 1);
2054 sector_t block_in_file;
2055 sector_t last_block;
2056 sector_t last_block_in_file;
2060 block_in_file = (sector_t)page_index(page);
2061 last_block = block_in_file + nr_pages;
2062 last_block_in_file = bytes_to_blks(inode,
2063 f2fs_readpage_limit(inode) + blocksize - 1);
2064 if (last_block > last_block_in_file)
2065 last_block = last_block_in_file;
2067 /* just zeroing out page which is beyond EOF */
2068 if (block_in_file >= last_block)
2071 * Map blocks using the previous result first.
2073 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2074 block_in_file > map->m_lblk &&
2075 block_in_file < (map->m_lblk + map->m_len))
2079 * Then do more f2fs_map_blocks() calls until we are
2080 * done with this page.
2082 map->m_lblk = block_in_file;
2083 map->m_len = last_block - block_in_file;
2085 ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT);
2089 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2090 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2091 SetPageMappedToDisk(page);
2093 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2094 DATA_GENERIC_ENHANCE_READ)) {
2095 ret = -EFSCORRUPTED;
2100 zero_user_segment(page, 0, PAGE_SIZE);
2101 if (f2fs_need_verity(inode, page->index) &&
2102 !fsverity_verify_page(page)) {
2106 if (!PageUptodate(page))
2107 SetPageUptodate(page);
2113 * This page will go to BIO. Do we need to send this
2116 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2117 *last_block_in_bio, block_nr) ||
2118 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2120 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2124 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2125 is_readahead ? REQ_RAHEAD : 0, page->index,
2135 * If the page is under writeback, we need to wait for
2136 * its completion to see the correct decrypted data.
2138 f2fs_wait_on_block_writeback(inode, block_nr);
2140 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2141 goto submit_and_realloc;
2143 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2144 f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO,
2146 *last_block_in_bio = block_nr;
2152 #ifdef CONFIG_F2FS_FS_COMPRESSION
2153 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2154 unsigned nr_pages, sector_t *last_block_in_bio,
2155 bool is_readahead, bool for_write)
2157 struct dnode_of_data dn;
2158 struct inode *inode = cc->inode;
2159 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2160 struct bio *bio = *bio_ret;
2161 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2162 sector_t last_block_in_file;
2163 const unsigned blocksize = blks_to_bytes(inode, 1);
2164 struct decompress_io_ctx *dic = NULL;
2165 struct extent_info ei = {};
2166 bool from_dnode = true;
2170 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2172 last_block_in_file = bytes_to_blks(inode,
2173 f2fs_readpage_limit(inode) + blocksize - 1);
2175 /* get rid of pages beyond EOF */
2176 for (i = 0; i < cc->cluster_size; i++) {
2177 struct page *page = cc->rpages[i];
2181 if ((sector_t)page->index >= last_block_in_file) {
2182 zero_user_segment(page, 0, PAGE_SIZE);
2183 if (!PageUptodate(page))
2184 SetPageUptodate(page);
2185 } else if (!PageUptodate(page)) {
2191 cc->rpages[i] = NULL;
2195 /* we are done since all pages are beyond EOF */
2196 if (f2fs_cluster_is_empty(cc))
2199 if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei))
2203 goto skip_reading_dnode;
2205 set_new_dnode(&dn, inode, NULL, NULL, 0);
2206 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2210 if (unlikely(f2fs_cp_error(sbi))) {
2214 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2217 for (i = 1; i < cc->cluster_size; i++) {
2220 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2221 dn.ofs_in_node + i) :
2224 if (!__is_valid_data_blkaddr(blkaddr))
2227 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2233 if (!from_dnode && i >= ei.c_len)
2237 /* nothing to decompress */
2238 if (cc->nr_cpages == 0) {
2243 dic = f2fs_alloc_dic(cc);
2249 for (i = 0; i < cc->nr_cpages; i++) {
2250 struct page *page = dic->cpages[i];
2252 struct bio_post_read_ctx *ctx;
2254 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2255 dn.ofs_in_node + i + 1) :
2258 f2fs_wait_on_block_writeback(inode, blkaddr);
2260 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2261 if (atomic_dec_and_test(&dic->remaining_pages)) {
2262 f2fs_decompress_cluster(dic, true);
2268 if (bio && (!page_is_mergeable(sbi, bio,
2269 *last_block_in_bio, blkaddr) ||
2270 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2272 f2fs_submit_read_bio(sbi, bio, DATA);
2277 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2278 is_readahead ? REQ_RAHEAD : 0,
2279 page->index, for_write);
2282 f2fs_decompress_end_io(dic, ret, true);
2283 f2fs_put_dnode(&dn);
2289 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2290 goto submit_and_realloc;
2292 ctx = get_post_read_ctx(bio);
2293 ctx->enabled_steps |= STEP_DECOMPRESS;
2294 refcount_inc(&dic->refcnt);
2296 inc_page_count(sbi, F2FS_RD_DATA);
2297 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
2298 *last_block_in_bio = blkaddr;
2302 f2fs_put_dnode(&dn);
2309 f2fs_put_dnode(&dn);
2311 for (i = 0; i < cc->cluster_size; i++) {
2312 if (cc->rpages[i]) {
2313 ClearPageUptodate(cc->rpages[i]);
2314 unlock_page(cc->rpages[i]);
2323 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2324 * Major change was from block_size == page_size in f2fs by default.
2326 static int f2fs_mpage_readpages(struct inode *inode,
2327 struct readahead_control *rac, struct page *page)
2329 struct bio *bio = NULL;
2330 sector_t last_block_in_bio = 0;
2331 struct f2fs_map_blocks map;
2332 #ifdef CONFIG_F2FS_FS_COMPRESSION
2333 struct compress_ctx cc = {
2335 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2336 .cluster_size = F2FS_I(inode)->i_cluster_size,
2337 .cluster_idx = NULL_CLUSTER,
2343 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2345 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2346 unsigned max_nr_pages = nr_pages;
2353 map.m_next_pgofs = NULL;
2354 map.m_next_extent = NULL;
2355 map.m_seg_type = NO_CHECK_TYPE;
2356 map.m_may_create = false;
2358 for (; nr_pages; nr_pages--) {
2360 page = readahead_page(rac);
2361 prefetchw(&page->flags);
2364 #ifdef CONFIG_F2FS_FS_COMPRESSION
2365 if (f2fs_compressed_file(inode)) {
2366 /* there are remained compressed pages, submit them */
2367 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2368 ret = f2fs_read_multi_pages(&cc, &bio,
2371 rac != NULL, false);
2372 f2fs_destroy_compress_ctx(&cc, false);
2374 goto set_error_page;
2376 if (cc.cluster_idx == NULL_CLUSTER) {
2377 if (nc_cluster_idx ==
2378 page->index >> cc.log_cluster_size) {
2379 goto read_single_page;
2382 ret = f2fs_is_compressed_cluster(inode, page->index);
2384 goto set_error_page;
2387 page->index >> cc.log_cluster_size;
2388 goto read_single_page;
2391 nc_cluster_idx = NULL_CLUSTER;
2393 ret = f2fs_init_compress_ctx(&cc);
2395 goto set_error_page;
2397 f2fs_compress_ctx_add_page(&cc, page);
2404 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2405 &bio, &last_block_in_bio, rac);
2407 #ifdef CONFIG_F2FS_FS_COMPRESSION
2410 zero_user_segment(page, 0, PAGE_SIZE);
2413 #ifdef CONFIG_F2FS_FS_COMPRESSION
2419 #ifdef CONFIG_F2FS_FS_COMPRESSION
2420 if (f2fs_compressed_file(inode)) {
2422 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2423 ret = f2fs_read_multi_pages(&cc, &bio,
2426 rac != NULL, false);
2427 f2fs_destroy_compress_ctx(&cc, false);
2433 f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2437 static int f2fs_read_data_folio(struct file *file, struct folio *folio)
2439 struct page *page = &folio->page;
2440 struct inode *inode = page_file_mapping(page)->host;
2443 trace_f2fs_readpage(page, DATA);
2445 if (!f2fs_is_compress_backend_ready(inode)) {
2450 /* If the file has inline data, try to read it directly */
2451 if (f2fs_has_inline_data(inode))
2452 ret = f2fs_read_inline_data(inode, page);
2454 ret = f2fs_mpage_readpages(inode, NULL, page);
2458 static void f2fs_readahead(struct readahead_control *rac)
2460 struct inode *inode = rac->mapping->host;
2462 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2464 if (!f2fs_is_compress_backend_ready(inode))
2467 /* If the file has inline data, skip readahead */
2468 if (f2fs_has_inline_data(inode))
2471 f2fs_mpage_readpages(inode, rac, NULL);
2474 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2476 struct inode *inode = fio->page->mapping->host;
2477 struct page *mpage, *page;
2478 gfp_t gfp_flags = GFP_NOFS;
2480 if (!f2fs_encrypted_file(inode))
2483 page = fio->compressed_page ? fio->compressed_page : fio->page;
2485 if (fscrypt_inode_uses_inline_crypto(inode))
2489 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2490 PAGE_SIZE, 0, gfp_flags);
2491 if (IS_ERR(fio->encrypted_page)) {
2492 /* flush pending IOs and wait for a while in the ENOMEM case */
2493 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2494 f2fs_flush_merged_writes(fio->sbi);
2495 memalloc_retry_wait(GFP_NOFS);
2496 gfp_flags |= __GFP_NOFAIL;
2499 return PTR_ERR(fio->encrypted_page);
2502 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2504 if (PageUptodate(mpage))
2505 memcpy(page_address(mpage),
2506 page_address(fio->encrypted_page), PAGE_SIZE);
2507 f2fs_put_page(mpage, 1);
2512 static inline bool check_inplace_update_policy(struct inode *inode,
2513 struct f2fs_io_info *fio)
2515 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2517 if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) &&
2518 is_inode_flag_set(inode, FI_OPU_WRITE))
2520 if (IS_F2FS_IPU_FORCE(sbi))
2522 if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi))
2524 if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util)
2526 if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) &&
2527 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2531 * IPU for rewrite async pages
2533 if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE &&
2534 !(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode))
2537 /* this is only set during fdatasync */
2538 if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU))
2541 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2542 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2548 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2550 /* swap file is migrating in aligned write mode */
2551 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2554 if (f2fs_is_pinned_file(inode))
2557 /* if this is cold file, we should overwrite to avoid fragmentation */
2558 if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE))
2561 return check_inplace_update_policy(inode, fio);
2564 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2566 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2568 /* The below cases were checked when setting it. */
2569 if (f2fs_is_pinned_file(inode))
2571 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2573 if (f2fs_lfs_mode(sbi))
2575 if (S_ISDIR(inode->i_mode))
2577 if (IS_NOQUOTA(inode))
2579 if (f2fs_is_atomic_file(inode))
2581 /* rewrite low ratio compress data w/ OPU mode to avoid fragmentation */
2582 if (f2fs_compressed_file(inode) &&
2583 F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER &&
2584 is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
2587 /* swap file is migrating in aligned write mode */
2588 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2591 if (is_inode_flag_set(inode, FI_OPU_WRITE))
2595 if (page_private_gcing(fio->page))
2597 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2598 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2604 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2606 struct inode *inode = fio->page->mapping->host;
2608 if (f2fs_should_update_outplace(inode, fio))
2611 return f2fs_should_update_inplace(inode, fio);
2614 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2616 struct page *page = fio->page;
2617 struct inode *inode = page->mapping->host;
2618 struct dnode_of_data dn;
2619 struct node_info ni;
2620 bool ipu_force = false;
2623 /* Use COW inode to make dnode_of_data for atomic write */
2624 if (f2fs_is_atomic_file(inode))
2625 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
2627 set_new_dnode(&dn, inode, NULL, NULL, 0);
2629 if (need_inplace_update(fio) &&
2630 f2fs_lookup_read_extent_cache_block(inode, page->index,
2631 &fio->old_blkaddr)) {
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_page_private_gcing(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 /* wait for GCed page writeback via META_MAPPING */
2667 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2670 * If current allocation needs SSR,
2671 * it had better in-place writes for updated data.
2674 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2675 need_inplace_update(fio))) {
2676 err = f2fs_encrypt_one_page(fio);
2680 set_page_writeback(page);
2681 f2fs_put_dnode(&dn);
2682 if (fio->need_lock == LOCK_REQ)
2683 f2fs_unlock_op(fio->sbi);
2684 err = f2fs_inplace_write_data(fio);
2686 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2687 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2688 if (PageWriteback(page))
2689 end_page_writeback(page);
2691 set_inode_flag(inode, FI_UPDATE_WRITE);
2693 trace_f2fs_do_write_data_page(fio->page, IPU);
2697 if (fio->need_lock == LOCK_RETRY) {
2698 if (!f2fs_trylock_op(fio->sbi)) {
2702 fio->need_lock = LOCK_REQ;
2705 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
2709 fio->version = ni.version;
2711 err = f2fs_encrypt_one_page(fio);
2715 set_page_writeback(page);
2717 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2718 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2720 /* LFS mode write path */
2721 f2fs_outplace_write_data(&dn, fio);
2722 trace_f2fs_do_write_data_page(page, OPU);
2723 set_inode_flag(inode, FI_APPEND_WRITE);
2725 f2fs_put_dnode(&dn);
2727 if (fio->need_lock == LOCK_REQ)
2728 f2fs_unlock_op(fio->sbi);
2732 int f2fs_write_single_data_page(struct page *page, int *submitted,
2734 sector_t *last_block,
2735 struct writeback_control *wbc,
2736 enum iostat_type io_type,
2740 struct inode *inode = page->mapping->host;
2741 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2742 loff_t i_size = i_size_read(inode);
2743 const pgoff_t end_index = ((unsigned long long)i_size)
2745 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2746 unsigned offset = 0;
2747 bool need_balance_fs = false;
2748 bool quota_inode = IS_NOQUOTA(inode);
2750 struct f2fs_io_info fio = {
2752 .ino = inode->i_ino,
2755 .op_flags = wbc_to_write_flags(wbc),
2756 .old_blkaddr = NULL_ADDR,
2758 .encrypted_page = NULL,
2760 .compr_blocks = compr_blocks,
2761 .need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY,
2762 .post_read = f2fs_post_read_required(inode) ? 1 : 0,
2766 .last_block = last_block,
2769 trace_f2fs_writepage(page, DATA);
2771 /* we should bypass data pages to proceed the kworker jobs */
2772 if (unlikely(f2fs_cp_error(sbi))) {
2773 mapping_set_error(page->mapping, -EIO);
2775 * don't drop any dirty dentry pages for keeping lastest
2776 * directory structure.
2778 if (S_ISDIR(inode->i_mode) &&
2779 !is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2782 /* keep data pages in remount-ro mode */
2783 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2788 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2791 if (page->index < end_index ||
2792 f2fs_verity_in_progress(inode) ||
2797 * If the offset is out-of-range of file size,
2798 * this page does not have to be written to disk.
2800 offset = i_size & (PAGE_SIZE - 1);
2801 if ((page->index >= end_index + 1) || !offset)
2804 zero_user_segment(page, offset, PAGE_SIZE);
2806 /* Dentry/quota blocks are controlled by checkpoint */
2807 if (S_ISDIR(inode->i_mode) || quota_inode) {
2809 * We need to wait for node_write to avoid block allocation during
2810 * checkpoint. This can only happen to quota writes which can cause
2811 * the below discard race condition.
2814 f2fs_down_read(&sbi->node_write);
2816 fio.need_lock = LOCK_DONE;
2817 err = f2fs_do_write_data_page(&fio);
2820 f2fs_up_read(&sbi->node_write);
2825 if (!wbc->for_reclaim)
2826 need_balance_fs = true;
2827 else if (has_not_enough_free_secs(sbi, 0, 0))
2830 set_inode_flag(inode, FI_HOT_DATA);
2833 if (f2fs_has_inline_data(inode)) {
2834 err = f2fs_write_inline_data(inode, page);
2839 if (err == -EAGAIN) {
2840 err = f2fs_do_write_data_page(&fio);
2841 if (err == -EAGAIN) {
2842 f2fs_bug_on(sbi, compr_blocks);
2843 fio.need_lock = LOCK_REQ;
2844 err = f2fs_do_write_data_page(&fio);
2849 file_set_keep_isize(inode);
2851 spin_lock(&F2FS_I(inode)->i_size_lock);
2852 if (F2FS_I(inode)->last_disk_size < psize)
2853 F2FS_I(inode)->last_disk_size = psize;
2854 spin_unlock(&F2FS_I(inode)->i_size_lock);
2858 if (err && err != -ENOENT)
2862 inode_dec_dirty_pages(inode);
2864 ClearPageUptodate(page);
2865 clear_page_private_gcing(page);
2868 if (wbc->for_reclaim) {
2869 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2870 clear_inode_flag(inode, FI_HOT_DATA);
2871 f2fs_remove_dirty_inode(inode);
2875 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2876 !F2FS_I(inode)->wb_task && allow_balance)
2877 f2fs_balance_fs(sbi, need_balance_fs);
2879 if (unlikely(f2fs_cp_error(sbi))) {
2880 f2fs_submit_merged_write(sbi, DATA);
2882 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2887 *submitted = fio.submitted;
2892 redirty_page_for_writepage(wbc, page);
2894 * pageout() in MM translates EAGAIN, so calls handle_write_error()
2895 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2896 * file_write_and_wait_range() will see EIO error, which is critical
2897 * to return value of fsync() followed by atomic_write failure to user.
2899 if (!err || wbc->for_reclaim)
2900 return AOP_WRITEPAGE_ACTIVATE;
2905 static int f2fs_write_data_page(struct page *page,
2906 struct writeback_control *wbc)
2908 #ifdef CONFIG_F2FS_FS_COMPRESSION
2909 struct inode *inode = page->mapping->host;
2911 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2914 if (f2fs_compressed_file(inode)) {
2915 if (f2fs_is_compressed_cluster(inode, page->index)) {
2916 redirty_page_for_writepage(wbc, page);
2917 return AOP_WRITEPAGE_ACTIVATE;
2923 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2924 wbc, FS_DATA_IO, 0, true);
2928 * This function was copied from write_cache_pages from mm/page-writeback.c.
2929 * The major change is making write step of cold data page separately from
2930 * warm/hot data page.
2932 static int f2fs_write_cache_pages(struct address_space *mapping,
2933 struct writeback_control *wbc,
2934 enum iostat_type io_type)
2937 int done = 0, retry = 0;
2938 struct page *pages_local[F2FS_ONSTACK_PAGES];
2939 struct page **pages = pages_local;
2940 struct folio_batch fbatch;
2941 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2942 struct bio *bio = NULL;
2943 sector_t last_block;
2944 #ifdef CONFIG_F2FS_FS_COMPRESSION
2945 struct inode *inode = mapping->host;
2946 struct compress_ctx cc = {
2948 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2949 .cluster_size = F2FS_I(inode)->i_cluster_size,
2950 .cluster_idx = NULL_CLUSTER,
2954 .valid_nr_cpages = 0,
2957 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2961 int nr_folios, p, idx;
2963 unsigned int max_pages = F2FS_ONSTACK_PAGES;
2965 pgoff_t end; /* Inclusive */
2967 int range_whole = 0;
2973 #ifdef CONFIG_F2FS_FS_COMPRESSION
2974 if (f2fs_compressed_file(inode) &&
2975 1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) {
2976 pages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
2977 cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL);
2978 max_pages = 1 << cc.log_cluster_size;
2982 folio_batch_init(&fbatch);
2984 if (get_dirty_pages(mapping->host) <=
2985 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2986 set_inode_flag(mapping->host, FI_HOT_DATA);
2988 clear_inode_flag(mapping->host, FI_HOT_DATA);
2990 if (wbc->range_cyclic) {
2991 index = mapping->writeback_index; /* prev offset */
2994 index = wbc->range_start >> PAGE_SHIFT;
2995 end = wbc->range_end >> PAGE_SHIFT;
2996 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2999 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3000 tag = PAGECACHE_TAG_TOWRITE;
3002 tag = PAGECACHE_TAG_DIRTY;
3005 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3006 tag_pages_for_writeback(mapping, index, end);
3008 while (!done && !retry && (index <= end)) {
3011 nr_folios = filemap_get_folios_tag(mapping, &index, end,
3013 if (nr_folios == 0) {
3019 for (i = 0; i < nr_folios; i++) {
3020 struct folio *folio = fbatch.folios[i];
3023 p = folio_nr_pages(folio);
3025 pages[nr_pages] = folio_page(folio, idx);
3027 if (++nr_pages == max_pages) {
3028 index = folio->index + idx + 1;
3029 folio_batch_release(&fbatch);
3035 folio_batch_release(&fbatch);
3038 for (i = 0; i < nr_pages; i++) {
3039 struct page *page = pages[i];
3040 struct folio *folio = page_folio(page);
3044 #ifdef CONFIG_F2FS_FS_COMPRESSION
3045 if (f2fs_compressed_file(inode)) {
3046 void *fsdata = NULL;
3050 ret = f2fs_init_compress_ctx(&cc);
3056 if (!f2fs_cluster_can_merge_page(&cc,
3058 ret = f2fs_write_multi_pages(&cc,
3059 &submitted, wbc, io_type);
3065 if (unlikely(f2fs_cp_error(sbi)))
3068 if (!f2fs_cluster_is_empty(&cc))
3071 if (f2fs_all_cluster_page_ready(&cc,
3072 pages, i, nr_pages, true))
3075 ret2 = f2fs_prepare_compress_overwrite(
3077 folio->index, &fsdata);
3083 (!f2fs_compress_write_end(inode,
3084 fsdata, folio->index, 1) ||
3085 !f2fs_all_cluster_page_ready(&cc,
3093 /* give a priority to WB_SYNC threads */
3094 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3095 wbc->sync_mode == WB_SYNC_NONE) {
3099 #ifdef CONFIG_F2FS_FS_COMPRESSION
3102 done_index = folio->index;
3106 if (unlikely(folio->mapping != mapping)) {
3108 folio_unlock(folio);
3112 if (!folio_test_dirty(folio)) {
3113 /* someone wrote it for us */
3114 goto continue_unlock;
3117 if (folio_test_writeback(folio)) {
3118 if (wbc->sync_mode == WB_SYNC_NONE)
3119 goto continue_unlock;
3120 f2fs_wait_on_page_writeback(&folio->page, DATA, true, true);
3123 if (!folio_clear_dirty_for_io(folio))
3124 goto continue_unlock;
3126 #ifdef CONFIG_F2FS_FS_COMPRESSION
3127 if (f2fs_compressed_file(inode)) {
3129 f2fs_compress_ctx_add_page(&cc, &folio->page);
3133 ret = f2fs_write_single_data_page(&folio->page,
3134 &submitted, &bio, &last_block,
3135 wbc, io_type, 0, true);
3136 if (ret == AOP_WRITEPAGE_ACTIVATE)
3137 folio_unlock(folio);
3138 #ifdef CONFIG_F2FS_FS_COMPRESSION
3141 nwritten += submitted;
3142 wbc->nr_to_write -= submitted;
3144 if (unlikely(ret)) {
3146 * keep nr_to_write, since vfs uses this to
3147 * get # of written pages.
3149 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3152 } else if (ret == -EAGAIN) {
3154 if (wbc->sync_mode == WB_SYNC_ALL) {
3155 f2fs_io_schedule_timeout(
3156 DEFAULT_IO_TIMEOUT);
3161 done_index = folio_next_index(folio);
3166 if (wbc->nr_to_write <= 0 &&
3167 wbc->sync_mode == WB_SYNC_NONE) {
3175 release_pages(pages, nr_pages);
3178 #ifdef CONFIG_F2FS_FS_COMPRESSION
3179 /* flush remained pages in compress cluster */
3180 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3181 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3182 nwritten += submitted;
3183 wbc->nr_to_write -= submitted;
3189 if (f2fs_compressed_file(inode))
3190 f2fs_destroy_compress_ctx(&cc, false);
3197 if (wbc->range_cyclic && !done)
3199 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3200 mapping->writeback_index = done_index;
3203 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3205 /* submit cached bio of IPU write */
3207 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3209 #ifdef CONFIG_F2FS_FS_COMPRESSION
3210 if (pages != pages_local)
3217 static inline bool __should_serialize_io(struct inode *inode,
3218 struct writeback_control *wbc)
3220 /* to avoid deadlock in path of data flush */
3221 if (F2FS_I(inode)->wb_task)
3224 if (!S_ISREG(inode->i_mode))
3226 if (IS_NOQUOTA(inode))
3229 if (f2fs_need_compress_data(inode))
3231 if (wbc->sync_mode != WB_SYNC_ALL)
3233 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3238 static int __f2fs_write_data_pages(struct address_space *mapping,
3239 struct writeback_control *wbc,
3240 enum iostat_type io_type)
3242 struct inode *inode = mapping->host;
3243 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3244 struct blk_plug plug;
3246 bool locked = false;
3248 /* deal with chardevs and other special file */
3249 if (!mapping->a_ops->writepage)
3252 /* skip writing if there is no dirty page in this inode */
3253 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3256 /* during POR, we don't need to trigger writepage at all. */
3257 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3260 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3261 wbc->sync_mode == WB_SYNC_NONE &&
3262 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3263 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3266 /* skip writing in file defragment preparing stage */
3267 if (is_inode_flag_set(inode, FI_SKIP_WRITES))
3270 trace_f2fs_writepages(mapping->host, wbc, DATA);
3272 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3273 if (wbc->sync_mode == WB_SYNC_ALL)
3274 atomic_inc(&sbi->wb_sync_req[DATA]);
3275 else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3276 /* to avoid potential deadlock */
3278 blk_finish_plug(current->plug);
3282 if (__should_serialize_io(inode, wbc)) {
3283 mutex_lock(&sbi->writepages);
3287 blk_start_plug(&plug);
3288 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3289 blk_finish_plug(&plug);
3292 mutex_unlock(&sbi->writepages);
3294 if (wbc->sync_mode == WB_SYNC_ALL)
3295 atomic_dec(&sbi->wb_sync_req[DATA]);
3297 * if some pages were truncated, we cannot guarantee its mapping->host
3298 * to detect pending bios.
3301 f2fs_remove_dirty_inode(inode);
3305 wbc->pages_skipped += get_dirty_pages(inode);
3306 trace_f2fs_writepages(mapping->host, wbc, DATA);
3310 static int f2fs_write_data_pages(struct address_space *mapping,
3311 struct writeback_control *wbc)
3313 struct inode *inode = mapping->host;
3315 return __f2fs_write_data_pages(mapping, wbc,
3316 F2FS_I(inode)->cp_task == current ?
3317 FS_CP_DATA_IO : FS_DATA_IO);
3320 void f2fs_write_failed(struct inode *inode, loff_t to)
3322 loff_t i_size = i_size_read(inode);
3324 if (IS_NOQUOTA(inode))
3327 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3328 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3329 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3330 filemap_invalidate_lock(inode->i_mapping);
3332 truncate_pagecache(inode, i_size);
3333 f2fs_truncate_blocks(inode, i_size, true);
3335 filemap_invalidate_unlock(inode->i_mapping);
3336 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3340 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3341 struct page *page, loff_t pos, unsigned len,
3342 block_t *blk_addr, bool *node_changed)
3344 struct inode *inode = page->mapping->host;
3345 pgoff_t index = page->index;
3346 struct dnode_of_data dn;
3348 bool locked = false;
3349 int flag = F2FS_GET_BLOCK_PRE_AIO;
3353 * If a whole page is being written and we already preallocated all the
3354 * blocks, then there is no need to get a block address now.
3356 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
3359 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3360 if (f2fs_has_inline_data(inode)) {
3361 if (pos + len > MAX_INLINE_DATA(inode))
3362 flag = F2FS_GET_BLOCK_DEFAULT;
3363 f2fs_map_lock(sbi, flag);
3365 } else if ((pos & PAGE_MASK) >= i_size_read(inode)) {
3366 f2fs_map_lock(sbi, flag);
3371 /* check inline_data */
3372 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3373 if (IS_ERR(ipage)) {
3374 err = PTR_ERR(ipage);
3378 set_new_dnode(&dn, inode, ipage, ipage, 0);
3380 if (f2fs_has_inline_data(inode)) {
3381 if (pos + len <= MAX_INLINE_DATA(inode)) {
3382 f2fs_do_read_inline_data(page, ipage);
3383 set_inode_flag(inode, FI_DATA_EXIST);
3385 set_page_private_inline(ipage);
3388 err = f2fs_convert_inline_page(&dn, page);
3389 if (err || dn.data_blkaddr != NULL_ADDR)
3393 if (!f2fs_lookup_read_extent_cache_block(inode, index,
3394 &dn.data_blkaddr)) {
3396 err = f2fs_reserve_block(&dn, index);
3401 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3402 if (!err && dn.data_blkaddr != NULL_ADDR)
3404 f2fs_put_dnode(&dn);
3405 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3406 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3412 /* convert_inline_page can make node_changed */
3413 *blk_addr = dn.data_blkaddr;
3414 *node_changed = dn.node_changed;
3416 f2fs_put_dnode(&dn);
3419 f2fs_map_unlock(sbi, flag);
3423 static int __find_data_block(struct inode *inode, pgoff_t index,
3426 struct dnode_of_data dn;
3430 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
3432 return PTR_ERR(ipage);
3434 set_new_dnode(&dn, inode, ipage, ipage, 0);
3436 if (!f2fs_lookup_read_extent_cache_block(inode, index,
3437 &dn.data_blkaddr)) {
3439 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3441 dn.data_blkaddr = NULL_ADDR;
3445 *blk_addr = dn.data_blkaddr;
3446 f2fs_put_dnode(&dn);
3450 static int __reserve_data_block(struct inode *inode, pgoff_t index,
3451 block_t *blk_addr, bool *node_changed)
3453 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3454 struct dnode_of_data dn;
3458 f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3460 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3461 if (IS_ERR(ipage)) {
3462 err = PTR_ERR(ipage);
3465 set_new_dnode(&dn, inode, ipage, ipage, 0);
3467 if (!f2fs_lookup_read_extent_cache_block(dn.inode, index,
3469 err = f2fs_reserve_block(&dn, index);
3471 *blk_addr = dn.data_blkaddr;
3472 *node_changed = dn.node_changed;
3473 f2fs_put_dnode(&dn);
3476 f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
3480 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
3481 struct page *page, loff_t pos, unsigned int len,
3482 block_t *blk_addr, bool *node_changed, bool *use_cow)
3484 struct inode *inode = page->mapping->host;
3485 struct inode *cow_inode = F2FS_I(inode)->cow_inode;
3486 pgoff_t index = page->index;
3488 block_t ori_blk_addr = NULL_ADDR;
3490 /* If pos is beyond the end of file, reserve a new block in COW inode */
3491 if ((pos & PAGE_MASK) >= i_size_read(inode))
3494 /* Look for the block in COW inode first */
3495 err = __find_data_block(cow_inode, index, blk_addr);
3498 } else if (*blk_addr != NULL_ADDR) {
3503 if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE))
3506 /* Look for the block in the original inode */
3507 err = __find_data_block(inode, index, &ori_blk_addr);
3512 /* Finally, we should reserve a new block in COW inode for the update */
3513 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3516 inc_atomic_write_cnt(inode);
3518 if (ori_blk_addr != NULL_ADDR)
3519 *blk_addr = ori_blk_addr;
3523 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3524 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
3526 struct inode *inode = mapping->host;
3527 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3528 struct page *page = NULL;
3529 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3530 bool need_balance = false;
3531 bool use_cow = false;
3532 block_t blkaddr = NULL_ADDR;
3535 trace_f2fs_write_begin(inode, pos, len);
3537 if (!f2fs_is_checkpoint_ready(sbi)) {
3543 * We should check this at this moment to avoid deadlock on inode page
3544 * and #0 page. The locking rule for inline_data conversion should be:
3545 * lock_page(page #0) -> lock_page(inode_page)
3548 err = f2fs_convert_inline_inode(inode);
3553 #ifdef CONFIG_F2FS_FS_COMPRESSION
3554 if (f2fs_compressed_file(inode)) {
3559 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3562 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3575 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3576 * wait_for_stable_page. Will wait that below with our IO control.
3578 page = f2fs_pagecache_get_page(mapping, index,
3579 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3585 /* TODO: cluster can be compressed due to race with .writepage */
3589 if (f2fs_is_atomic_file(inode))
3590 err = prepare_atomic_write_begin(sbi, page, pos, len,
3591 &blkaddr, &need_balance, &use_cow);
3593 err = prepare_write_begin(sbi, page, pos, len,
3594 &blkaddr, &need_balance);
3598 if (need_balance && !IS_NOQUOTA(inode) &&
3599 has_not_enough_free_secs(sbi, 0, 0)) {
3601 f2fs_balance_fs(sbi, true);
3603 if (page->mapping != mapping) {
3604 /* The page got truncated from under us */
3605 f2fs_put_page(page, 1);
3610 f2fs_wait_on_page_writeback(page, DATA, false, true);
3612 if (len == PAGE_SIZE || PageUptodate(page))
3615 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3616 !f2fs_verity_in_progress(inode)) {
3617 zero_user_segment(page, len, PAGE_SIZE);
3621 if (blkaddr == NEW_ADDR) {
3622 zero_user_segment(page, 0, PAGE_SIZE);
3623 SetPageUptodate(page);
3625 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3626 DATA_GENERIC_ENHANCE_READ)) {
3627 err = -EFSCORRUPTED;
3630 err = f2fs_submit_page_read(use_cow ?
3631 F2FS_I(inode)->cow_inode : inode, page,
3637 if (unlikely(page->mapping != mapping)) {
3638 f2fs_put_page(page, 1);
3641 if (unlikely(!PageUptodate(page))) {
3649 f2fs_put_page(page, 1);
3650 f2fs_write_failed(inode, pos + len);
3654 static int f2fs_write_end(struct file *file,
3655 struct address_space *mapping,
3656 loff_t pos, unsigned len, unsigned copied,
3657 struct page *page, void *fsdata)
3659 struct inode *inode = page->mapping->host;
3661 trace_f2fs_write_end(inode, pos, len, copied);
3664 * This should be come from len == PAGE_SIZE, and we expect copied
3665 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3666 * let generic_perform_write() try to copy data again through copied=0.
3668 if (!PageUptodate(page)) {
3669 if (unlikely(copied != len))
3672 SetPageUptodate(page);
3675 #ifdef CONFIG_F2FS_FS_COMPRESSION
3676 /* overwrite compressed file */
3677 if (f2fs_compressed_file(inode) && fsdata) {
3678 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3679 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3681 if (pos + copied > i_size_read(inode) &&
3682 !f2fs_verity_in_progress(inode))
3683 f2fs_i_size_write(inode, pos + copied);
3691 set_page_dirty(page);
3693 if (pos + copied > i_size_read(inode) &&
3694 !f2fs_verity_in_progress(inode)) {
3695 f2fs_i_size_write(inode, pos + copied);
3696 if (f2fs_is_atomic_file(inode))
3697 f2fs_i_size_write(F2FS_I(inode)->cow_inode,
3701 f2fs_put_page(page, 1);
3702 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3706 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
3708 struct inode *inode = folio->mapping->host;
3709 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3711 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3712 (offset || length != folio_size(folio)))
3715 if (folio_test_dirty(folio)) {
3716 if (inode->i_ino == F2FS_META_INO(sbi)) {
3717 dec_page_count(sbi, F2FS_DIRTY_META);
3718 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3719 dec_page_count(sbi, F2FS_DIRTY_NODES);
3721 inode_dec_dirty_pages(inode);
3722 f2fs_remove_dirty_inode(inode);
3725 clear_page_private_all(&folio->page);
3728 bool f2fs_release_folio(struct folio *folio, gfp_t wait)
3730 /* If this is dirty folio, keep private data */
3731 if (folio_test_dirty(folio))
3734 clear_page_private_all(&folio->page);
3738 static bool f2fs_dirty_data_folio(struct address_space *mapping,
3739 struct folio *folio)
3741 struct inode *inode = mapping->host;
3743 trace_f2fs_set_page_dirty(&folio->page, DATA);
3745 if (!folio_test_uptodate(folio))
3746 folio_mark_uptodate(folio);
3747 BUG_ON(folio_test_swapcache(folio));
3749 if (filemap_dirty_folio(mapping, folio)) {
3750 f2fs_update_dirty_folio(inode, folio);
3757 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3759 #ifdef CONFIG_F2FS_FS_COMPRESSION
3760 struct dnode_of_data dn;
3761 sector_t start_idx, blknr = 0;
3764 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3766 set_new_dnode(&dn, inode, NULL, NULL, 0);
3767 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3771 if (dn.data_blkaddr != COMPRESS_ADDR) {
3772 dn.ofs_in_node += block - start_idx;
3773 blknr = f2fs_data_blkaddr(&dn);
3774 if (!__is_valid_data_blkaddr(blknr))
3778 f2fs_put_dnode(&dn);
3786 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3788 struct inode *inode = mapping->host;
3791 if (f2fs_has_inline_data(inode))
3794 /* make sure allocating whole blocks */
3795 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3796 filemap_write_and_wait(mapping);
3798 /* Block number less than F2FS MAX BLOCKS */
3799 if (unlikely(block >= max_file_blocks(inode)))
3802 if (f2fs_compressed_file(inode)) {
3803 blknr = f2fs_bmap_compress(inode, block);
3805 struct f2fs_map_blocks map;
3807 memset(&map, 0, sizeof(map));
3810 map.m_next_pgofs = NULL;
3811 map.m_seg_type = NO_CHECK_TYPE;
3813 if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP))
3817 trace_f2fs_bmap(inode, block, blknr);
3822 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3823 unsigned int blkcnt)
3825 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3826 unsigned int blkofs;
3827 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3828 unsigned int end_blk = start_blk + blkcnt - 1;
3829 unsigned int secidx = start_blk / blk_per_sec;
3830 unsigned int end_sec;
3835 end_sec = end_blk / blk_per_sec;
3837 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3838 filemap_invalidate_lock(inode->i_mapping);
3840 set_inode_flag(inode, FI_ALIGNED_WRITE);
3841 set_inode_flag(inode, FI_OPU_WRITE);
3843 for (; secidx <= end_sec; secidx++) {
3844 unsigned int blkofs_end = secidx == end_sec ?
3845 end_blk % blk_per_sec : blk_per_sec - 1;
3847 f2fs_down_write(&sbi->pin_sem);
3849 ret = f2fs_allocate_pinning_section(sbi);
3851 f2fs_up_write(&sbi->pin_sem);
3855 set_inode_flag(inode, FI_SKIP_WRITES);
3857 for (blkofs = 0; blkofs <= blkofs_end; blkofs++) {
3859 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3861 page = f2fs_get_lock_data_page(inode, blkidx, true);
3863 f2fs_up_write(&sbi->pin_sem);
3864 ret = PTR_ERR(page);
3868 set_page_dirty(page);
3869 f2fs_put_page(page, 1);
3872 clear_inode_flag(inode, FI_SKIP_WRITES);
3874 ret = filemap_fdatawrite(inode->i_mapping);
3876 f2fs_up_write(&sbi->pin_sem);
3883 clear_inode_flag(inode, FI_SKIP_WRITES);
3884 clear_inode_flag(inode, FI_OPU_WRITE);
3885 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3887 filemap_invalidate_unlock(inode->i_mapping);
3888 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3893 static int check_swap_activate(struct swap_info_struct *sis,
3894 struct file *swap_file, sector_t *span)
3896 struct address_space *mapping = swap_file->f_mapping;
3897 struct inode *inode = mapping->host;
3898 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3899 sector_t cur_lblock;
3900 sector_t last_lblock;
3902 sector_t lowest_pblock = -1;
3903 sector_t highest_pblock = 0;
3905 unsigned long nr_pblocks;
3906 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3907 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3908 unsigned int not_aligned = 0;
3912 * Map all the blocks into the extent list. This code doesn't try
3916 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3918 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3919 struct f2fs_map_blocks map;
3923 memset(&map, 0, sizeof(map));
3924 map.m_lblk = cur_lblock;
3925 map.m_len = last_lblock - cur_lblock;
3926 map.m_next_pgofs = NULL;
3927 map.m_next_extent = NULL;
3928 map.m_seg_type = NO_CHECK_TYPE;
3929 map.m_may_create = false;
3931 ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
3936 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3937 f2fs_err(sbi, "Swapfile has holes");
3942 pblock = map.m_pblk;
3943 nr_pblocks = map.m_len;
3945 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3946 nr_pblocks & sec_blks_mask ||
3947 !f2fs_valid_pinned_area(sbi, pblock)) {
3948 bool last_extent = false;
3952 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3953 if (cur_lblock + nr_pblocks > sis->max)
3954 nr_pblocks -= blks_per_sec;
3956 /* this extent is last one */
3958 nr_pblocks = last_lblock - cur_lblock;
3962 ret = f2fs_migrate_blocks(inode, cur_lblock,
3974 if (cur_lblock + nr_pblocks >= sis->max)
3975 nr_pblocks = sis->max - cur_lblock;
3977 if (cur_lblock) { /* exclude the header page */
3978 if (pblock < lowest_pblock)
3979 lowest_pblock = pblock;
3980 if (pblock + nr_pblocks - 1 > highest_pblock)
3981 highest_pblock = pblock + nr_pblocks - 1;
3985 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3987 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3991 cur_lblock += nr_pblocks;
3994 *span = 1 + highest_pblock - lowest_pblock;
3995 if (cur_lblock == 0)
3996 cur_lblock = 1; /* force Empty message */
3997 sis->max = cur_lblock;
3998 sis->pages = cur_lblock - 1;
3999 sis->highest_bit = cur_lblock - 1;
4002 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)",
4003 not_aligned, blks_per_sec * F2FS_BLKSIZE);
4007 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4010 struct inode *inode = file_inode(file);
4011 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4014 if (!S_ISREG(inode->i_mode))
4017 if (f2fs_readonly(sbi->sb))
4020 if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) {
4021 f2fs_err(sbi, "Swapfile not supported in LFS mode");
4025 ret = f2fs_convert_inline_inode(inode);
4029 if (!f2fs_disable_compressed_file(inode))
4032 ret = filemap_fdatawrite(inode->i_mapping);
4036 f2fs_precache_extents(inode);
4038 ret = check_swap_activate(sis, file, span);
4042 stat_inc_swapfile_inode(inode);
4043 set_inode_flag(inode, FI_PIN_FILE);
4044 f2fs_update_time(sbi, REQ_TIME);
4048 static void f2fs_swap_deactivate(struct file *file)
4050 struct inode *inode = file_inode(file);
4052 stat_dec_swapfile_inode(inode);
4053 clear_inode_flag(inode, FI_PIN_FILE);
4056 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4062 static void f2fs_swap_deactivate(struct file *file)
4067 const struct address_space_operations f2fs_dblock_aops = {
4068 .read_folio = f2fs_read_data_folio,
4069 .readahead = f2fs_readahead,
4070 .writepage = f2fs_write_data_page,
4071 .writepages = f2fs_write_data_pages,
4072 .write_begin = f2fs_write_begin,
4073 .write_end = f2fs_write_end,
4074 .dirty_folio = f2fs_dirty_data_folio,
4075 .migrate_folio = filemap_migrate_folio,
4076 .invalidate_folio = f2fs_invalidate_folio,
4077 .release_folio = f2fs_release_folio,
4079 .swap_activate = f2fs_swap_activate,
4080 .swap_deactivate = f2fs_swap_deactivate,
4083 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4085 struct address_space *mapping = page_mapping(page);
4086 unsigned long flags;
4088 xa_lock_irqsave(&mapping->i_pages, flags);
4089 __xa_clear_mark(&mapping->i_pages, page_index(page),
4090 PAGECACHE_TAG_DIRTY);
4091 xa_unlock_irqrestore(&mapping->i_pages, flags);
4094 int __init f2fs_init_post_read_processing(void)
4096 bio_post_read_ctx_cache =
4097 kmem_cache_create("f2fs_bio_post_read_ctx",
4098 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4099 if (!bio_post_read_ctx_cache)
4101 bio_post_read_ctx_pool =
4102 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4103 bio_post_read_ctx_cache);
4104 if (!bio_post_read_ctx_pool)
4105 goto fail_free_cache;
4109 kmem_cache_destroy(bio_post_read_ctx_cache);
4114 void f2fs_destroy_post_read_processing(void)
4116 mempool_destroy(bio_post_read_ctx_pool);
4117 kmem_cache_destroy(bio_post_read_ctx_cache);
4120 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4122 if (!f2fs_sb_has_encrypt(sbi) &&
4123 !f2fs_sb_has_verity(sbi) &&
4124 !f2fs_sb_has_compression(sbi))
4127 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4128 WQ_UNBOUND | WQ_HIGHPRI,
4130 return sbi->post_read_wq ? 0 : -ENOMEM;
4133 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4135 if (sbi->post_read_wq)
4136 destroy_workqueue(sbi->post_read_wq);
4139 int __init f2fs_init_bio_entry_cache(void)
4141 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4142 sizeof(struct bio_entry));
4143 return bio_entry_slab ? 0 : -ENOMEM;
4146 void f2fs_destroy_bio_entry_cache(void)
4148 kmem_cache_destroy(bio_entry_slab);
4151 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4152 unsigned int flags, struct iomap *iomap,
4153 struct iomap *srcmap)
4155 struct f2fs_map_blocks map = {};
4156 pgoff_t next_pgofs = 0;
4159 map.m_lblk = bytes_to_blks(inode, offset);
4160 map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1;
4161 map.m_next_pgofs = &next_pgofs;
4162 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4163 if (flags & IOMAP_WRITE)
4164 map.m_may_create = true;
4166 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO);
4170 iomap->offset = blks_to_bytes(inode, map.m_lblk);
4173 * When inline encryption is enabled, sometimes I/O to an encrypted file
4174 * has to be broken up to guarantee DUN contiguity. Handle this by
4175 * limiting the length of the mapping returned.
4177 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4180 * We should never see delalloc or compressed extents here based on
4181 * prior flushing and checks.
4183 if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR))
4185 if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR))
4188 if (map.m_pblk != NULL_ADDR) {
4189 iomap->length = blks_to_bytes(inode, map.m_len);
4190 iomap->type = IOMAP_MAPPED;
4191 iomap->flags |= IOMAP_F_MERGED;
4192 iomap->bdev = map.m_bdev;
4193 iomap->addr = blks_to_bytes(inode, map.m_pblk);
4195 if (flags & IOMAP_WRITE)
4197 iomap->length = blks_to_bytes(inode, next_pgofs) -
4199 iomap->type = IOMAP_HOLE;
4200 iomap->addr = IOMAP_NULL_ADDR;
4203 if (map.m_flags & F2FS_MAP_NEW)
4204 iomap->flags |= IOMAP_F_NEW;
4205 if ((inode->i_state & I_DIRTY_DATASYNC) ||
4206 offset + length > i_size_read(inode))
4207 iomap->flags |= IOMAP_F_DIRTY;
4212 const struct iomap_ops f2fs_iomap_ops = {
4213 .iomap_begin = f2fs_iomap_begin,