1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/sched/mm.h>
12 #include <linux/mpage.h>
13 #include <linux/writeback.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/blk-crypto.h>
18 #include <linux/swap.h>
19 #include <linux/prefetch.h>
20 #include <linux/uio.h>
21 #include <linux/sched/signal.h>
22 #include <linux/fiemap.h>
23 #include <linux/iomap.h>
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static struct kmem_cache *bio_entry_slab;
35 static mempool_t *bio_post_read_ctx_pool;
36 static struct bio_set f2fs_bioset;
38 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
40 int __init f2fs_init_bioset(void)
42 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS))
48 void f2fs_destroy_bioset(void)
50 bioset_exit(&f2fs_bioset);
53 static bool __is_cp_guaranteed(struct page *page)
55 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi;
62 inode = mapping->host;
63 sbi = F2FS_I_SB(inode);
65 if (inode->i_ino == F2FS_META_INO(sbi) ||
66 inode->i_ino == F2FS_NODE_INO(sbi) ||
67 S_ISDIR(inode->i_mode))
70 if (f2fs_is_compressed_page(page))
72 if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
73 page_private_gcing(page))
78 static enum count_type __read_io_type(struct page *page)
80 struct address_space *mapping = page_file_mapping(page);
83 struct inode *inode = mapping->host;
84 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
86 if (inode->i_ino == F2FS_META_INO(sbi))
89 if (inode->i_ino == F2FS_NODE_INO(sbi))
95 /* postprocessing steps for read bios */
96 enum bio_post_read_step {
97 #ifdef CONFIG_FS_ENCRYPTION
98 STEP_DECRYPT = 1 << 0,
100 STEP_DECRYPT = 0, /* compile out the decryption-related code */
102 #ifdef CONFIG_F2FS_FS_COMPRESSION
103 STEP_DECOMPRESS = 1 << 1,
105 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
107 #ifdef CONFIG_FS_VERITY
108 STEP_VERITY = 1 << 2,
110 STEP_VERITY = 0, /* compile out the verity-related code */
114 struct bio_post_read_ctx {
116 struct f2fs_sb_info *sbi;
117 struct work_struct work;
118 unsigned int enabled_steps;
122 static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
125 struct bvec_iter_all iter_all;
128 * Update and unlock the bio's pagecache pages, and put the
129 * decompression context for any compressed pages.
131 bio_for_each_segment_all(bv, bio, iter_all) {
132 struct page *page = bv->bv_page;
134 if (f2fs_is_compressed_page(page)) {
136 f2fs_end_read_compressed_page(page, true, 0,
138 f2fs_put_page_dic(page, in_task);
142 /* PG_error was set if decryption or verity failed. */
143 if (bio->bi_status || PageError(page)) {
144 ClearPageUptodate(page);
145 /* will re-read again later */
146 ClearPageError(page);
148 SetPageUptodate(page);
150 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
155 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
159 static void f2fs_verify_bio(struct work_struct *work)
161 struct bio_post_read_ctx *ctx =
162 container_of(work, struct bio_post_read_ctx, work);
163 struct bio *bio = ctx->bio;
164 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
167 * fsverity_verify_bio() may call readahead() again, and while verity
168 * will be disabled for this, decryption and/or decompression may still
169 * be needed, resulting in another bio_post_read_ctx being allocated.
170 * So to prevent deadlocks we need to release the current ctx to the
171 * mempool first. This assumes that verity is the last post-read step.
173 mempool_free(ctx, bio_post_read_ctx_pool);
174 bio->bi_private = NULL;
177 * Verify the bio's pages with fs-verity. Exclude compressed pages,
178 * as those were handled separately by f2fs_end_read_compressed_page().
180 if (may_have_compressed_pages) {
182 struct bvec_iter_all iter_all;
184 bio_for_each_segment_all(bv, bio, iter_all) {
185 struct page *page = bv->bv_page;
187 if (!f2fs_is_compressed_page(page) &&
188 !PageError(page) && !fsverity_verify_page(page))
192 fsverity_verify_bio(bio);
195 f2fs_finish_read_bio(bio, true);
199 * If the bio's data needs to be verified with fs-verity, then enqueue the
200 * verity work for the bio. Otherwise finish the bio now.
202 * Note that to avoid deadlocks, the verity work can't be done on the
203 * decryption/decompression workqueue. This is because verifying the data pages
204 * can involve reading verity metadata pages from the file, and these verity
205 * metadata pages may be encrypted and/or compressed.
207 static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
209 struct bio_post_read_ctx *ctx = bio->bi_private;
211 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
212 INIT_WORK(&ctx->work, f2fs_verify_bio);
213 fsverity_enqueue_verify_work(&ctx->work);
215 f2fs_finish_read_bio(bio, in_task);
220 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
221 * remaining page was read by @ctx->bio.
223 * Note that a bio may span clusters (even a mix of compressed and uncompressed
224 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
225 * that the bio includes at least one compressed page. The actual decompression
226 * is done on a per-cluster basis, not a per-bio basis.
228 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
232 struct bvec_iter_all iter_all;
233 bool all_compressed = true;
234 block_t blkaddr = ctx->fs_blkaddr;
236 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
237 struct page *page = bv->bv_page;
239 /* PG_error was set if decryption failed. */
240 if (f2fs_is_compressed_page(page))
241 f2fs_end_read_compressed_page(page, PageError(page),
244 all_compressed = false;
250 * Optimization: if all the bio's pages are compressed, then scheduling
251 * the per-bio verity work is unnecessary, as verity will be fully
252 * handled at the compression cluster level.
255 ctx->enabled_steps &= ~STEP_VERITY;
258 static void f2fs_post_read_work(struct work_struct *work)
260 struct bio_post_read_ctx *ctx =
261 container_of(work, struct bio_post_read_ctx, work);
263 if (ctx->enabled_steps & STEP_DECRYPT)
264 fscrypt_decrypt_bio(ctx->bio);
266 if (ctx->enabled_steps & STEP_DECOMPRESS)
267 f2fs_handle_step_decompress(ctx, true);
269 f2fs_verify_and_finish_bio(ctx->bio, true);
272 static void f2fs_read_end_io(struct bio *bio)
274 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
275 struct bio_post_read_ctx *ctx;
276 bool intask = in_task();
278 iostat_update_and_unbind_ctx(bio, 0);
279 ctx = bio->bi_private;
281 if (time_to_inject(sbi, FAULT_READ_IO)) {
282 f2fs_show_injection_info(sbi, FAULT_READ_IO);
283 bio->bi_status = BLK_STS_IOERR;
286 if (bio->bi_status) {
287 f2fs_finish_read_bio(bio, intask);
292 unsigned int enabled_steps = ctx->enabled_steps &
293 (STEP_DECRYPT | STEP_DECOMPRESS);
296 * If we have only decompression step between decompression and
297 * decrypt, we don't need post processing for this.
299 if (enabled_steps == STEP_DECOMPRESS &&
300 !f2fs_low_mem_mode(sbi)) {
301 f2fs_handle_step_decompress(ctx, intask);
302 } else if (enabled_steps) {
303 INIT_WORK(&ctx->work, f2fs_post_read_work);
304 queue_work(ctx->sbi->post_read_wq, &ctx->work);
309 f2fs_verify_and_finish_bio(bio, intask);
312 static void f2fs_write_end_io(struct bio *bio)
314 struct f2fs_sb_info *sbi;
315 struct bio_vec *bvec;
316 struct bvec_iter_all iter_all;
318 iostat_update_and_unbind_ctx(bio, 1);
319 sbi = bio->bi_private;
321 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
322 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
323 bio->bi_status = BLK_STS_IOERR;
326 bio_for_each_segment_all(bvec, bio, iter_all) {
327 struct page *page = bvec->bv_page;
328 enum count_type type = WB_DATA_TYPE(page);
330 if (page_private_dummy(page)) {
331 clear_page_private_dummy(page);
333 mempool_free(page, sbi->write_io_dummy);
335 if (unlikely(bio->bi_status))
336 f2fs_stop_checkpoint(sbi, true);
340 fscrypt_finalize_bounce_page(&page);
342 #ifdef CONFIG_F2FS_FS_COMPRESSION
343 if (f2fs_is_compressed_page(page)) {
344 f2fs_compress_write_end_io(bio, page);
349 if (unlikely(bio->bi_status)) {
350 mapping_set_error(page->mapping, -EIO);
351 if (type == F2FS_WB_CP_DATA)
352 f2fs_stop_checkpoint(sbi, true);
355 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
356 page->index != nid_of_node(page));
358 dec_page_count(sbi, type);
359 if (f2fs_in_warm_node_list(sbi, page))
360 f2fs_del_fsync_node_entry(sbi, page);
361 clear_page_private_gcing(page);
362 end_page_writeback(page);
364 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
365 wq_has_sleeper(&sbi->cp_wait))
366 wake_up(&sbi->cp_wait);
371 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
372 block_t blk_addr, sector_t *sector)
374 struct block_device *bdev = sbi->sb->s_bdev;
377 if (f2fs_is_multi_device(sbi)) {
378 for (i = 0; i < sbi->s_ndevs; i++) {
379 if (FDEV(i).start_blk <= blk_addr &&
380 FDEV(i).end_blk >= blk_addr) {
381 blk_addr -= FDEV(i).start_blk;
389 *sector = SECTOR_FROM_BLOCK(blk_addr);
393 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
397 if (!f2fs_is_multi_device(sbi))
400 for (i = 0; i < sbi->s_ndevs; i++)
401 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
406 static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio)
408 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
409 unsigned int fua_flag, meta_flag, io_flag;
410 blk_opf_t op_flags = 0;
412 if (fio->op != REQ_OP_WRITE)
414 if (fio->type == DATA)
415 io_flag = fio->sbi->data_io_flag;
416 else if (fio->type == NODE)
417 io_flag = fio->sbi->node_io_flag;
421 fua_flag = io_flag & temp_mask;
422 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
425 * data/node io flag bits per temp:
426 * REQ_META | REQ_FUA |
427 * 5 | 4 | 3 | 2 | 1 | 0 |
428 * Cold | Warm | Hot | Cold | Warm | Hot |
430 if ((1 << fio->temp) & meta_flag)
431 op_flags |= REQ_META;
432 if ((1 << fio->temp) & fua_flag)
437 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
439 struct f2fs_sb_info *sbi = fio->sbi;
440 struct block_device *bdev;
444 bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or);
445 bio = bio_alloc_bioset(bdev, npages,
446 fio->op | fio->op_flags | f2fs_io_flags(fio),
447 GFP_NOIO, &f2fs_bioset);
448 bio->bi_iter.bi_sector = sector;
449 if (is_read_io(fio->op)) {
450 bio->bi_end_io = f2fs_read_end_io;
451 bio->bi_private = NULL;
453 bio->bi_end_io = f2fs_write_end_io;
454 bio->bi_private = sbi;
456 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
459 wbc_init_bio(fio->io_wbc, bio);
464 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
466 const struct f2fs_io_info *fio,
470 * The f2fs garbage collector sets ->encrypted_page when it wants to
471 * read/write raw data without encryption.
473 if (!fio || !fio->encrypted_page)
474 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
477 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
479 const struct f2fs_io_info *fio)
482 * The f2fs garbage collector sets ->encrypted_page when it wants to
483 * read/write raw data without encryption.
485 if (fio && fio->encrypted_page)
486 return !bio_has_crypt_ctx(bio);
488 return fscrypt_mergeable_bio(bio, inode, next_idx);
491 static inline void __submit_bio(struct f2fs_sb_info *sbi,
492 struct bio *bio, enum page_type type)
494 if (!is_read_io(bio_op(bio))) {
497 if (type != DATA && type != NODE)
500 if (f2fs_lfs_mode(sbi) && current->plug)
501 blk_finish_plug(current->plug);
503 if (!F2FS_IO_ALIGNED(sbi))
506 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
507 start %= F2FS_IO_SIZE(sbi);
512 /* fill dummy pages */
513 for (; start < F2FS_IO_SIZE(sbi); start++) {
515 mempool_alloc(sbi->write_io_dummy,
516 GFP_NOIO | __GFP_NOFAIL);
517 f2fs_bug_on(sbi, !page);
521 zero_user_segment(page, 0, PAGE_SIZE);
522 set_page_private_dummy(page);
524 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
528 * In the NODE case, we lose next block address chain. So, we
529 * need to do checkpoint in f2fs_sync_file.
532 set_sbi_flag(sbi, SBI_NEED_CP);
535 if (is_read_io(bio_op(bio)))
536 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
538 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
540 iostat_update_submit_ctx(bio, type);
544 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
545 struct bio *bio, enum page_type type)
547 __submit_bio(sbi, bio, type);
550 static void __submit_merged_bio(struct f2fs_bio_info *io)
552 struct f2fs_io_info *fio = &io->fio;
557 if (is_read_io(fio->op))
558 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
560 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
562 __submit_bio(io->sbi, io->bio, fio->type);
566 static bool __has_merged_page(struct bio *bio, struct inode *inode,
567 struct page *page, nid_t ino)
569 struct bio_vec *bvec;
570 struct bvec_iter_all iter_all;
575 if (!inode && !page && !ino)
578 bio_for_each_segment_all(bvec, bio, iter_all) {
579 struct page *target = bvec->bv_page;
581 if (fscrypt_is_bounce_page(target)) {
582 target = fscrypt_pagecache_page(target);
586 if (f2fs_is_compressed_page(target)) {
587 target = f2fs_compress_control_page(target);
592 if (inode && inode == target->mapping->host)
594 if (page && page == target)
596 if (ino && ino == ino_of_node(target))
603 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
607 for (i = 0; i < NR_PAGE_TYPE; i++) {
608 int n = (i == META) ? 1 : NR_TEMP_TYPE;
611 sbi->write_io[i] = f2fs_kmalloc(sbi,
612 array_size(n, sizeof(struct f2fs_bio_info)),
614 if (!sbi->write_io[i])
617 for (j = HOT; j < n; j++) {
618 init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem);
619 sbi->write_io[i][j].sbi = sbi;
620 sbi->write_io[i][j].bio = NULL;
621 spin_lock_init(&sbi->write_io[i][j].io_lock);
622 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
623 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
624 init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock);
631 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
632 enum page_type type, enum temp_type temp)
634 enum page_type btype = PAGE_TYPE_OF_BIO(type);
635 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
637 f2fs_down_write(&io->io_rwsem);
639 /* change META to META_FLUSH in the checkpoint procedure */
640 if (type >= META_FLUSH) {
641 io->fio.type = META_FLUSH;
642 io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
643 if (!test_opt(sbi, NOBARRIER))
644 io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
646 __submit_merged_bio(io);
647 f2fs_up_write(&io->io_rwsem);
650 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
651 struct inode *inode, struct page *page,
652 nid_t ino, enum page_type type, bool force)
657 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
659 enum page_type btype = PAGE_TYPE_OF_BIO(type);
660 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
662 f2fs_down_read(&io->io_rwsem);
663 ret = __has_merged_page(io->bio, inode, page, ino);
664 f2fs_up_read(&io->io_rwsem);
667 __f2fs_submit_merged_write(sbi, type, temp);
669 /* TODO: use HOT temp only for meta pages now. */
675 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
677 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
680 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
681 struct inode *inode, struct page *page,
682 nid_t ino, enum page_type type)
684 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
687 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
689 f2fs_submit_merged_write(sbi, DATA);
690 f2fs_submit_merged_write(sbi, NODE);
691 f2fs_submit_merged_write(sbi, META);
695 * Fill the locked page with data located in the block address.
696 * A caller needs to unlock the page on failure.
698 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
701 struct page *page = fio->encrypted_page ?
702 fio->encrypted_page : fio->page;
704 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
705 fio->is_por ? META_POR : (__is_meta_io(fio) ?
706 META_GENERIC : DATA_GENERIC_ENHANCE)))
707 return -EFSCORRUPTED;
709 trace_f2fs_submit_page_bio(page, fio);
711 /* Allocate a new bio */
712 bio = __bio_alloc(fio, 1);
714 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
715 fio->page->index, fio, GFP_NOIO);
717 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
722 if (fio->io_wbc && !is_read_io(fio->op))
723 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
725 inc_page_count(fio->sbi, is_read_io(fio->op) ?
726 __read_io_type(page): WB_DATA_TYPE(fio->page));
728 __submit_bio(fio->sbi, bio, fio->type);
732 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
733 block_t last_blkaddr, block_t cur_blkaddr)
735 if (unlikely(sbi->max_io_bytes &&
736 bio->bi_iter.bi_size >= sbi->max_io_bytes))
738 if (last_blkaddr + 1 != cur_blkaddr)
740 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
743 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
744 struct f2fs_io_info *fio)
746 if (io->fio.op != fio->op)
748 return io->fio.op_flags == fio->op_flags;
751 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
752 struct f2fs_bio_info *io,
753 struct f2fs_io_info *fio,
754 block_t last_blkaddr,
757 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
758 unsigned int filled_blocks =
759 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
760 unsigned int io_size = F2FS_IO_SIZE(sbi);
761 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
763 /* IOs in bio is aligned and left space of vectors is not enough */
764 if (!(filled_blocks % io_size) && left_vecs < io_size)
767 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
769 return io_type_is_mergeable(io, fio);
772 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
773 struct page *page, enum temp_type temp)
775 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
776 struct bio_entry *be;
778 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
782 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
785 f2fs_down_write(&io->bio_list_lock);
786 list_add_tail(&be->list, &io->bio_list);
787 f2fs_up_write(&io->bio_list_lock);
790 static void del_bio_entry(struct bio_entry *be)
793 kmem_cache_free(bio_entry_slab, be);
796 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
799 struct f2fs_sb_info *sbi = fio->sbi;
804 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
805 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
806 struct list_head *head = &io->bio_list;
807 struct bio_entry *be;
809 f2fs_down_write(&io->bio_list_lock);
810 list_for_each_entry(be, head, list) {
816 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
819 if (f2fs_crypt_mergeable_bio(*bio,
820 fio->page->mapping->host,
821 fio->page->index, fio) &&
822 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
828 /* page can't be merged into bio; submit the bio */
830 __submit_bio(sbi, *bio, DATA);
833 f2fs_up_write(&io->bio_list_lock);
844 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
845 struct bio **bio, struct page *page)
849 struct bio *target = bio ? *bio : NULL;
851 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
852 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
853 struct list_head *head = &io->bio_list;
854 struct bio_entry *be;
856 if (list_empty(head))
859 f2fs_down_read(&io->bio_list_lock);
860 list_for_each_entry(be, head, list) {
862 found = (target == be->bio);
864 found = __has_merged_page(be->bio, NULL,
869 f2fs_up_read(&io->bio_list_lock);
876 f2fs_down_write(&io->bio_list_lock);
877 list_for_each_entry(be, head, list) {
879 found = (target == be->bio);
881 found = __has_merged_page(be->bio, NULL,
889 f2fs_up_write(&io->bio_list_lock);
893 __submit_bio(sbi, target, DATA);
900 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
902 struct bio *bio = *fio->bio;
903 struct page *page = fio->encrypted_page ?
904 fio->encrypted_page : fio->page;
906 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
907 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
908 return -EFSCORRUPTED;
910 trace_f2fs_submit_page_bio(page, fio);
912 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
914 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
917 bio = __bio_alloc(fio, BIO_MAX_VECS);
918 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
919 fio->page->index, fio, GFP_NOIO);
921 add_bio_entry(fio->sbi, bio, page, fio->temp);
923 if (add_ipu_page(fio, &bio, page))
928 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
930 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
932 *fio->last_block = fio->new_blkaddr;
938 void f2fs_submit_page_write(struct f2fs_io_info *fio)
940 struct f2fs_sb_info *sbi = fio->sbi;
941 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
942 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
943 struct page *bio_page;
945 f2fs_bug_on(sbi, is_read_io(fio->op));
947 f2fs_down_write(&io->io_rwsem);
950 spin_lock(&io->io_lock);
951 if (list_empty(&io->io_list)) {
952 spin_unlock(&io->io_lock);
955 fio = list_first_entry(&io->io_list,
956 struct f2fs_io_info, list);
957 list_del(&fio->list);
958 spin_unlock(&io->io_lock);
961 verify_fio_blkaddr(fio);
963 if (fio->encrypted_page)
964 bio_page = fio->encrypted_page;
965 else if (fio->compressed_page)
966 bio_page = fio->compressed_page;
968 bio_page = fio->page;
970 /* set submitted = true as a return value */
971 fio->submitted = true;
973 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
976 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
978 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
979 bio_page->index, fio)))
980 __submit_merged_bio(io);
982 if (io->bio == NULL) {
983 if (F2FS_IO_ALIGNED(sbi) &&
984 (fio->type == DATA || fio->type == NODE) &&
985 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
986 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
990 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
991 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
992 bio_page->index, fio, GFP_NOIO);
996 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
997 __submit_merged_bio(io);
1002 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
1004 io->last_block_in_bio = fio->new_blkaddr;
1006 trace_f2fs_submit_page_write(fio->page, fio);
1011 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1012 !f2fs_is_checkpoint_ready(sbi))
1013 __submit_merged_bio(io);
1014 f2fs_up_write(&io->io_rwsem);
1017 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1018 unsigned nr_pages, blk_opf_t op_flag,
1019 pgoff_t first_idx, bool for_write)
1021 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1023 struct bio_post_read_ctx *ctx = NULL;
1024 unsigned int post_read_steps = 0;
1026 struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or);
1028 bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
1029 REQ_OP_READ | op_flag,
1030 for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
1032 return ERR_PTR(-ENOMEM);
1033 bio->bi_iter.bi_sector = sector;
1034 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1035 bio->bi_end_io = f2fs_read_end_io;
1037 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1038 post_read_steps |= STEP_DECRYPT;
1040 if (f2fs_need_verity(inode, first_idx))
1041 post_read_steps |= STEP_VERITY;
1044 * STEP_DECOMPRESS is handled specially, since a compressed file might
1045 * contain both compressed and uncompressed clusters. We'll allocate a
1046 * bio_post_read_ctx if the file is compressed, but the caller is
1047 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1050 if (post_read_steps || f2fs_compressed_file(inode)) {
1051 /* Due to the mempool, this never fails. */
1052 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1055 ctx->enabled_steps = post_read_steps;
1056 ctx->fs_blkaddr = blkaddr;
1057 bio->bi_private = ctx;
1059 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1064 /* This can handle encryption stuffs */
1065 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1066 block_t blkaddr, blk_opf_t op_flags,
1069 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1072 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1073 page->index, for_write);
1075 return PTR_ERR(bio);
1077 /* wait for GCed page writeback via META_MAPPING */
1078 f2fs_wait_on_block_writeback(inode, blkaddr);
1080 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1084 ClearPageError(page);
1085 inc_page_count(sbi, F2FS_RD_DATA);
1086 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1087 __submit_bio(sbi, bio, DATA);
1091 static void __set_data_blkaddr(struct dnode_of_data *dn)
1093 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1097 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1098 base = get_extra_isize(dn->inode);
1100 /* Get physical address of data block */
1101 addr_array = blkaddr_in_node(rn);
1102 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1106 * Lock ordering for the change of data block address:
1109 * update block addresses in the node page
1111 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1113 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1114 __set_data_blkaddr(dn);
1115 if (set_page_dirty(dn->node_page))
1116 dn->node_changed = true;
1119 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1121 dn->data_blkaddr = blkaddr;
1122 f2fs_set_data_blkaddr(dn);
1123 f2fs_update_extent_cache(dn);
1126 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1127 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1129 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1135 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1137 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1140 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1141 dn->ofs_in_node, count);
1143 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1145 for (; count > 0; dn->ofs_in_node++) {
1146 block_t blkaddr = f2fs_data_blkaddr(dn);
1148 if (blkaddr == NULL_ADDR) {
1149 dn->data_blkaddr = NEW_ADDR;
1150 __set_data_blkaddr(dn);
1155 if (set_page_dirty(dn->node_page))
1156 dn->node_changed = true;
1160 /* Should keep dn->ofs_in_node unchanged */
1161 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1163 unsigned int ofs_in_node = dn->ofs_in_node;
1166 ret = f2fs_reserve_new_blocks(dn, 1);
1167 dn->ofs_in_node = ofs_in_node;
1171 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1173 bool need_put = dn->inode_page ? false : true;
1176 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1180 if (dn->data_blkaddr == NULL_ADDR)
1181 err = f2fs_reserve_new_block(dn);
1182 if (err || need_put)
1187 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1189 struct extent_info ei = {0, };
1190 struct inode *inode = dn->inode;
1192 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1193 dn->data_blkaddr = ei.blk + index - ei.fofs;
1197 return f2fs_reserve_block(dn, index);
1200 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1201 blk_opf_t op_flags, bool for_write)
1203 struct address_space *mapping = inode->i_mapping;
1204 struct dnode_of_data dn;
1206 struct extent_info ei = {0, };
1209 page = f2fs_grab_cache_page(mapping, index, for_write);
1211 return ERR_PTR(-ENOMEM);
1213 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1214 dn.data_blkaddr = ei.blk + index - ei.fofs;
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);
1227 f2fs_put_dnode(&dn);
1229 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1233 if (dn.data_blkaddr != NEW_ADDR &&
1234 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1236 DATA_GENERIC_ENHANCE)) {
1237 err = -EFSCORRUPTED;
1241 if (PageUptodate(page)) {
1247 * A new dentry page is allocated but not able to be written, since its
1248 * new inode page couldn't be allocated due to -ENOSPC.
1249 * In such the case, its blkaddr can be remained as NEW_ADDR.
1250 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1251 * f2fs_init_inode_metadata.
1253 if (dn.data_blkaddr == NEW_ADDR) {
1254 zero_user_segment(page, 0, PAGE_SIZE);
1255 if (!PageUptodate(page))
1256 SetPageUptodate(page);
1261 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1262 op_flags, for_write);
1268 f2fs_put_page(page, 1);
1269 return ERR_PTR(err);
1272 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1274 struct address_space *mapping = inode->i_mapping;
1277 page = find_get_page(mapping, index);
1278 if (page && PageUptodate(page))
1280 f2fs_put_page(page, 0);
1282 page = f2fs_get_read_data_page(inode, index, 0, false);
1286 if (PageUptodate(page))
1289 wait_on_page_locked(page);
1290 if (unlikely(!PageUptodate(page))) {
1291 f2fs_put_page(page, 0);
1292 return ERR_PTR(-EIO);
1298 * If it tries to access a hole, return an error.
1299 * Because, the callers, functions in dir.c and GC, should be able to know
1300 * whether this page exists or not.
1302 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1305 struct address_space *mapping = inode->i_mapping;
1308 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1312 /* wait for read completion */
1314 if (unlikely(page->mapping != mapping)) {
1315 f2fs_put_page(page, 1);
1318 if (unlikely(!PageUptodate(page))) {
1319 f2fs_put_page(page, 1);
1320 return ERR_PTR(-EIO);
1326 * Caller ensures that this data page is never allocated.
1327 * A new zero-filled data page is allocated in the page cache.
1329 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1331 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1332 * ipage should be released by this function.
1334 struct page *f2fs_get_new_data_page(struct inode *inode,
1335 struct page *ipage, pgoff_t index, bool new_i_size)
1337 struct address_space *mapping = inode->i_mapping;
1339 struct dnode_of_data dn;
1342 page = f2fs_grab_cache_page(mapping, index, true);
1345 * before exiting, we should make sure ipage will be released
1346 * if any error occur.
1348 f2fs_put_page(ipage, 1);
1349 return ERR_PTR(-ENOMEM);
1352 set_new_dnode(&dn, inode, ipage, NULL, 0);
1353 err = f2fs_reserve_block(&dn, index);
1355 f2fs_put_page(page, 1);
1356 return ERR_PTR(err);
1359 f2fs_put_dnode(&dn);
1361 if (PageUptodate(page))
1364 if (dn.data_blkaddr == NEW_ADDR) {
1365 zero_user_segment(page, 0, PAGE_SIZE);
1366 if (!PageUptodate(page))
1367 SetPageUptodate(page);
1369 f2fs_put_page(page, 1);
1371 /* if ipage exists, blkaddr should be NEW_ADDR */
1372 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1373 page = f2fs_get_lock_data_page(inode, index, true);
1378 if (new_i_size && i_size_read(inode) <
1379 ((loff_t)(index + 1) << PAGE_SHIFT))
1380 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1384 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1386 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1387 struct f2fs_summary sum;
1388 struct node_info ni;
1389 block_t old_blkaddr;
1393 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1396 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
1400 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1401 if (dn->data_blkaddr != NULL_ADDR)
1404 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1408 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1409 old_blkaddr = dn->data_blkaddr;
1410 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1411 &sum, seg_type, NULL);
1412 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1413 invalidate_mapping_pages(META_MAPPING(sbi),
1414 old_blkaddr, old_blkaddr);
1415 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1417 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1421 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1423 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1425 f2fs_down_read(&sbi->node_change);
1427 f2fs_up_read(&sbi->node_change);
1432 f2fs_unlock_op(sbi);
1437 * f2fs_map_blocks() tries to find or build mapping relationship which
1438 * maps continuous logical blocks to physical blocks, and return such
1439 * info via f2fs_map_blocks structure.
1441 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1442 int create, int flag)
1444 unsigned int maxblocks = map->m_len;
1445 struct dnode_of_data dn;
1446 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1447 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1448 pgoff_t pgofs, end_offset, end;
1449 int err = 0, ofs = 1;
1450 unsigned int ofs_in_node, last_ofs_in_node;
1452 struct extent_info ei = {0, };
1454 unsigned int start_pgofs;
1460 map->m_bdev = inode->i_sb->s_bdev;
1461 map->m_multidev_dio =
1462 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1467 /* it only supports block size == page size */
1468 pgofs = (pgoff_t)map->m_lblk;
1469 end = pgofs + maxblocks;
1471 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1472 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1476 map->m_pblk = ei.blk + pgofs - ei.fofs;
1477 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1478 map->m_flags = F2FS_MAP_MAPPED;
1479 if (map->m_next_extent)
1480 *map->m_next_extent = pgofs + map->m_len;
1482 /* for hardware encryption, but to avoid potential issue in future */
1483 if (flag == F2FS_GET_BLOCK_DIO)
1484 f2fs_wait_on_block_writeback_range(inode,
1485 map->m_pblk, map->m_len);
1487 if (map->m_multidev_dio) {
1488 block_t blk_addr = map->m_pblk;
1490 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1492 map->m_bdev = FDEV(bidx).bdev;
1493 map->m_pblk -= FDEV(bidx).start_blk;
1494 map->m_len = min(map->m_len,
1495 FDEV(bidx).end_blk + 1 - map->m_pblk);
1497 if (map->m_may_create)
1498 f2fs_update_device_state(sbi, inode->i_ino,
1499 blk_addr, map->m_len);
1505 if (map->m_may_create)
1506 f2fs_do_map_lock(sbi, flag, true);
1508 /* When reading holes, we need its node page */
1509 set_new_dnode(&dn, inode, NULL, NULL, 0);
1510 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1512 if (flag == F2FS_GET_BLOCK_BMAP)
1515 if (err == -ENOENT) {
1517 * There is one exceptional case that read_node_page()
1518 * may return -ENOENT due to filesystem has been
1519 * shutdown or cp_error, so force to convert error
1520 * number to EIO for such case.
1522 if (map->m_may_create &&
1523 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1524 f2fs_cp_error(sbi))) {
1530 if (map->m_next_pgofs)
1531 *map->m_next_pgofs =
1532 f2fs_get_next_page_offset(&dn, pgofs);
1533 if (map->m_next_extent)
1534 *map->m_next_extent =
1535 f2fs_get_next_page_offset(&dn, pgofs);
1540 start_pgofs = pgofs;
1542 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1543 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1546 blkaddr = f2fs_data_blkaddr(&dn);
1548 if (__is_valid_data_blkaddr(blkaddr) &&
1549 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1550 err = -EFSCORRUPTED;
1554 if (__is_valid_data_blkaddr(blkaddr)) {
1555 /* use out-place-update for driect IO under LFS mode */
1556 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1557 map->m_may_create) {
1558 err = __allocate_data_block(&dn, map->m_seg_type);
1561 blkaddr = dn.data_blkaddr;
1562 set_inode_flag(inode, FI_APPEND_WRITE);
1566 if (unlikely(f2fs_cp_error(sbi))) {
1570 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1571 if (blkaddr == NULL_ADDR) {
1573 last_ofs_in_node = dn.ofs_in_node;
1576 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1577 flag != F2FS_GET_BLOCK_DIO);
1578 err = __allocate_data_block(&dn,
1581 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1582 file_need_truncate(inode);
1583 set_inode_flag(inode, FI_APPEND_WRITE);
1588 map->m_flags |= F2FS_MAP_NEW;
1589 blkaddr = dn.data_blkaddr;
1591 if (f2fs_compressed_file(inode) &&
1592 f2fs_sanity_check_cluster(&dn) &&
1593 (flag != F2FS_GET_BLOCK_FIEMAP ||
1594 IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1595 err = -EFSCORRUPTED;
1598 if (flag == F2FS_GET_BLOCK_BMAP) {
1602 if (flag == F2FS_GET_BLOCK_PRECACHE)
1604 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1605 blkaddr == NULL_ADDR) {
1606 if (map->m_next_pgofs)
1607 *map->m_next_pgofs = pgofs + 1;
1610 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1611 /* for defragment case */
1612 if (map->m_next_pgofs)
1613 *map->m_next_pgofs = pgofs + 1;
1619 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1622 if (map->m_multidev_dio)
1623 bidx = f2fs_target_device_index(sbi, blkaddr);
1625 if (map->m_len == 0) {
1626 /* preallocated unwritten block should be mapped for fiemap. */
1627 if (blkaddr == NEW_ADDR)
1628 map->m_flags |= F2FS_MAP_UNWRITTEN;
1629 map->m_flags |= F2FS_MAP_MAPPED;
1631 map->m_pblk = blkaddr;
1634 if (map->m_multidev_dio)
1635 map->m_bdev = FDEV(bidx).bdev;
1636 } else if ((map->m_pblk != NEW_ADDR &&
1637 blkaddr == (map->m_pblk + ofs)) ||
1638 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1639 flag == F2FS_GET_BLOCK_PRE_DIO) {
1640 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1652 /* preallocate blocks in batch for one dnode page */
1653 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1654 (pgofs == end || dn.ofs_in_node == end_offset)) {
1656 dn.ofs_in_node = ofs_in_node;
1657 err = f2fs_reserve_new_blocks(&dn, prealloc);
1661 map->m_len += dn.ofs_in_node - ofs_in_node;
1662 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1666 dn.ofs_in_node = end_offset;
1671 else if (dn.ofs_in_node < end_offset)
1674 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1675 if (map->m_flags & F2FS_MAP_MAPPED) {
1676 unsigned int ofs = start_pgofs - map->m_lblk;
1678 f2fs_update_extent_cache_range(&dn,
1679 start_pgofs, map->m_pblk + ofs,
1684 f2fs_put_dnode(&dn);
1686 if (map->m_may_create) {
1687 f2fs_do_map_lock(sbi, flag, false);
1688 f2fs_balance_fs(sbi, dn.node_changed);
1694 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1696 * for hardware encryption, but to avoid potential issue
1699 f2fs_wait_on_block_writeback_range(inode,
1700 map->m_pblk, map->m_len);
1702 if (map->m_multidev_dio) {
1703 block_t blk_addr = map->m_pblk;
1705 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1707 map->m_bdev = FDEV(bidx).bdev;
1708 map->m_pblk -= FDEV(bidx).start_blk;
1710 if (map->m_may_create)
1711 f2fs_update_device_state(sbi, inode->i_ino,
1712 blk_addr, map->m_len);
1714 f2fs_bug_on(sbi, blk_addr + map->m_len >
1715 FDEV(bidx).end_blk + 1);
1719 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1720 if (map->m_flags & F2FS_MAP_MAPPED) {
1721 unsigned int ofs = start_pgofs - map->m_lblk;
1723 f2fs_update_extent_cache_range(&dn,
1724 start_pgofs, map->m_pblk + ofs,
1727 if (map->m_next_extent)
1728 *map->m_next_extent = pgofs + 1;
1730 f2fs_put_dnode(&dn);
1732 if (map->m_may_create) {
1733 f2fs_do_map_lock(sbi, flag, false);
1734 f2fs_balance_fs(sbi, dn.node_changed);
1737 trace_f2fs_map_blocks(inode, map, create, flag, err);
1741 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1743 struct f2fs_map_blocks map;
1747 if (pos + len > i_size_read(inode))
1750 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1751 map.m_next_pgofs = NULL;
1752 map.m_next_extent = NULL;
1753 map.m_seg_type = NO_CHECK_TYPE;
1754 map.m_may_create = false;
1755 last_lblk = F2FS_BLK_ALIGN(pos + len);
1757 while (map.m_lblk < last_lblk) {
1758 map.m_len = last_lblk - map.m_lblk;
1759 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1760 if (err || map.m_len == 0)
1762 map.m_lblk += map.m_len;
1767 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1769 return (bytes >> inode->i_blkbits);
1772 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1774 return (blks << inode->i_blkbits);
1777 static int f2fs_xattr_fiemap(struct inode *inode,
1778 struct fiemap_extent_info *fieinfo)
1780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1782 struct node_info ni;
1783 __u64 phys = 0, len;
1785 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1788 if (f2fs_has_inline_xattr(inode)) {
1791 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1792 inode->i_ino, false);
1796 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
1798 f2fs_put_page(page, 1);
1802 phys = blks_to_bytes(inode, ni.blk_addr);
1803 offset = offsetof(struct f2fs_inode, i_addr) +
1804 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1805 get_inline_xattr_addrs(inode));
1808 len = inline_xattr_size(inode);
1810 f2fs_put_page(page, 1);
1812 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1815 flags |= FIEMAP_EXTENT_LAST;
1817 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1818 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1819 if (err || err == 1)
1824 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1828 err = f2fs_get_node_info(sbi, xnid, &ni, false);
1830 f2fs_put_page(page, 1);
1834 phys = blks_to_bytes(inode, ni.blk_addr);
1835 len = inode->i_sb->s_blocksize;
1837 f2fs_put_page(page, 1);
1839 flags = FIEMAP_EXTENT_LAST;
1843 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1844 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1847 return (err < 0 ? err : 0);
1850 static loff_t max_inode_blocks(struct inode *inode)
1852 loff_t result = ADDRS_PER_INODE(inode);
1853 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1855 /* two direct node blocks */
1856 result += (leaf_count * 2);
1858 /* two indirect node blocks */
1859 leaf_count *= NIDS_PER_BLOCK;
1860 result += (leaf_count * 2);
1862 /* one double indirect node block */
1863 leaf_count *= NIDS_PER_BLOCK;
1864 result += leaf_count;
1869 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1872 struct f2fs_map_blocks map;
1873 sector_t start_blk, last_blk;
1875 u64 logical = 0, phys = 0, size = 0;
1878 bool compr_cluster = false, compr_appended;
1879 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1880 unsigned int count_in_cluster = 0;
1883 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1884 ret = f2fs_precache_extents(inode);
1889 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1895 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1896 if (start > maxbytes) {
1901 if (len > maxbytes || (maxbytes - len) < start)
1902 len = maxbytes - start;
1904 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1905 ret = f2fs_xattr_fiemap(inode, fieinfo);
1909 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1910 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1915 if (bytes_to_blks(inode, len) == 0)
1916 len = blks_to_bytes(inode, 1);
1918 start_blk = bytes_to_blks(inode, start);
1919 last_blk = bytes_to_blks(inode, start + len - 1);
1922 memset(&map, 0, sizeof(map));
1923 map.m_lblk = start_blk;
1924 map.m_len = bytes_to_blks(inode, len);
1925 map.m_next_pgofs = &next_pgofs;
1926 map.m_seg_type = NO_CHECK_TYPE;
1928 if (compr_cluster) {
1930 map.m_len = cluster_size - count_in_cluster;
1933 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1938 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1939 start_blk = next_pgofs;
1941 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1942 max_inode_blocks(inode)))
1945 flags |= FIEMAP_EXTENT_LAST;
1948 compr_appended = false;
1949 /* In a case of compressed cluster, append this to the last extent */
1950 if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1951 !(map.m_flags & F2FS_MAP_FLAGS))) {
1952 compr_appended = true;
1957 flags |= FIEMAP_EXTENT_MERGED;
1958 if (IS_ENCRYPTED(inode))
1959 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1961 ret = fiemap_fill_next_extent(fieinfo, logical,
1963 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1969 if (start_blk > last_blk)
1973 if (map.m_pblk == COMPRESS_ADDR) {
1974 compr_cluster = true;
1975 count_in_cluster = 1;
1976 } else if (compr_appended) {
1977 unsigned int appended_blks = cluster_size -
1978 count_in_cluster + 1;
1979 size += blks_to_bytes(inode, appended_blks);
1980 start_blk += appended_blks;
1981 compr_cluster = false;
1983 logical = blks_to_bytes(inode, start_blk);
1984 phys = __is_valid_data_blkaddr(map.m_pblk) ?
1985 blks_to_bytes(inode, map.m_pblk) : 0;
1986 size = blks_to_bytes(inode, map.m_len);
1989 if (compr_cluster) {
1990 flags = FIEMAP_EXTENT_ENCODED;
1991 count_in_cluster += map.m_len;
1992 if (count_in_cluster == cluster_size) {
1993 compr_cluster = false;
1994 size += blks_to_bytes(inode, 1);
1996 } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
1997 flags = FIEMAP_EXTENT_UNWRITTEN;
2000 start_blk += bytes_to_blks(inode, size);
2005 if (fatal_signal_pending(current))
2013 inode_unlock(inode);
2017 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2019 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2020 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2021 return inode->i_sb->s_maxbytes;
2023 return i_size_read(inode);
2026 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2028 struct f2fs_map_blocks *map,
2029 struct bio **bio_ret,
2030 sector_t *last_block_in_bio,
2033 struct bio *bio = *bio_ret;
2034 const unsigned blocksize = blks_to_bytes(inode, 1);
2035 sector_t block_in_file;
2036 sector_t last_block;
2037 sector_t last_block_in_file;
2041 block_in_file = (sector_t)page_index(page);
2042 last_block = block_in_file + nr_pages;
2043 last_block_in_file = bytes_to_blks(inode,
2044 f2fs_readpage_limit(inode) + blocksize - 1);
2045 if (last_block > last_block_in_file)
2046 last_block = last_block_in_file;
2048 /* just zeroing out page which is beyond EOF */
2049 if (block_in_file >= last_block)
2052 * Map blocks using the previous result first.
2054 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2055 block_in_file > map->m_lblk &&
2056 block_in_file < (map->m_lblk + map->m_len))
2060 * Then do more f2fs_map_blocks() calls until we are
2061 * done with this page.
2063 map->m_lblk = block_in_file;
2064 map->m_len = last_block - block_in_file;
2066 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2070 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2071 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2072 SetPageMappedToDisk(page);
2074 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2075 DATA_GENERIC_ENHANCE_READ)) {
2076 ret = -EFSCORRUPTED;
2081 zero_user_segment(page, 0, PAGE_SIZE);
2082 if (f2fs_need_verity(inode, page->index) &&
2083 !fsverity_verify_page(page)) {
2087 if (!PageUptodate(page))
2088 SetPageUptodate(page);
2094 * This page will go to BIO. Do we need to send this
2097 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2098 *last_block_in_bio, block_nr) ||
2099 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2101 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2105 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2106 is_readahead ? REQ_RAHEAD : 0, page->index,
2116 * If the page is under writeback, we need to wait for
2117 * its completion to see the correct decrypted data.
2119 f2fs_wait_on_block_writeback(inode, block_nr);
2121 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2122 goto submit_and_realloc;
2124 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2125 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2126 ClearPageError(page);
2127 *last_block_in_bio = block_nr;
2134 #ifdef CONFIG_F2FS_FS_COMPRESSION
2135 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2136 unsigned nr_pages, sector_t *last_block_in_bio,
2137 bool is_readahead, bool for_write)
2139 struct dnode_of_data dn;
2140 struct inode *inode = cc->inode;
2141 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2142 struct bio *bio = *bio_ret;
2143 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2144 sector_t last_block_in_file;
2145 const unsigned blocksize = blks_to_bytes(inode, 1);
2146 struct decompress_io_ctx *dic = NULL;
2147 struct extent_info ei = {0, };
2148 bool from_dnode = true;
2152 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2154 last_block_in_file = bytes_to_blks(inode,
2155 f2fs_readpage_limit(inode) + blocksize - 1);
2157 /* get rid of pages beyond EOF */
2158 for (i = 0; i < cc->cluster_size; i++) {
2159 struct page *page = cc->rpages[i];
2163 if ((sector_t)page->index >= last_block_in_file) {
2164 zero_user_segment(page, 0, PAGE_SIZE);
2165 if (!PageUptodate(page))
2166 SetPageUptodate(page);
2167 } else if (!PageUptodate(page)) {
2173 cc->rpages[i] = NULL;
2177 /* we are done since all pages are beyond EOF */
2178 if (f2fs_cluster_is_empty(cc))
2181 if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2185 goto skip_reading_dnode;
2187 set_new_dnode(&dn, inode, NULL, NULL, 0);
2188 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2192 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2195 for (i = 1; i < cc->cluster_size; i++) {
2198 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2199 dn.ofs_in_node + i) :
2202 if (!__is_valid_data_blkaddr(blkaddr))
2205 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2211 if (!from_dnode && i >= ei.c_len)
2215 /* nothing to decompress */
2216 if (cc->nr_cpages == 0) {
2221 dic = f2fs_alloc_dic(cc);
2227 for (i = 0; i < cc->nr_cpages; i++) {
2228 struct page *page = dic->cpages[i];
2230 struct bio_post_read_ctx *ctx;
2232 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2233 dn.ofs_in_node + i + 1) :
2236 f2fs_wait_on_block_writeback(inode, blkaddr);
2238 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2239 if (atomic_dec_and_test(&dic->remaining_pages))
2240 f2fs_decompress_cluster(dic, true);
2244 if (bio && (!page_is_mergeable(sbi, bio,
2245 *last_block_in_bio, blkaddr) ||
2246 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2248 __submit_bio(sbi, bio, DATA);
2253 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2254 is_readahead ? REQ_RAHEAD : 0,
2255 page->index, for_write);
2258 f2fs_decompress_end_io(dic, ret, true);
2259 f2fs_put_dnode(&dn);
2265 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2266 goto submit_and_realloc;
2268 ctx = get_post_read_ctx(bio);
2269 ctx->enabled_steps |= STEP_DECOMPRESS;
2270 refcount_inc(&dic->refcnt);
2272 inc_page_count(sbi, F2FS_RD_DATA);
2273 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2274 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2275 ClearPageError(page);
2276 *last_block_in_bio = blkaddr;
2280 f2fs_put_dnode(&dn);
2287 f2fs_put_dnode(&dn);
2289 for (i = 0; i < cc->cluster_size; i++) {
2290 if (cc->rpages[i]) {
2291 ClearPageUptodate(cc->rpages[i]);
2292 ClearPageError(cc->rpages[i]);
2293 unlock_page(cc->rpages[i]);
2302 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2303 * Major change was from block_size == page_size in f2fs by default.
2305 static int f2fs_mpage_readpages(struct inode *inode,
2306 struct readahead_control *rac, struct page *page)
2308 struct bio *bio = NULL;
2309 sector_t last_block_in_bio = 0;
2310 struct f2fs_map_blocks map;
2311 #ifdef CONFIG_F2FS_FS_COMPRESSION
2312 struct compress_ctx cc = {
2314 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2315 .cluster_size = F2FS_I(inode)->i_cluster_size,
2316 .cluster_idx = NULL_CLUSTER,
2322 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2324 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2325 unsigned max_nr_pages = nr_pages;
2332 map.m_next_pgofs = NULL;
2333 map.m_next_extent = NULL;
2334 map.m_seg_type = NO_CHECK_TYPE;
2335 map.m_may_create = false;
2337 for (; nr_pages; nr_pages--) {
2339 page = readahead_page(rac);
2340 prefetchw(&page->flags);
2343 #ifdef CONFIG_F2FS_FS_COMPRESSION
2344 if (f2fs_compressed_file(inode)) {
2345 /* there are remained comressed pages, submit them */
2346 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2347 ret = f2fs_read_multi_pages(&cc, &bio,
2350 rac != NULL, false);
2351 f2fs_destroy_compress_ctx(&cc, false);
2353 goto set_error_page;
2355 if (cc.cluster_idx == NULL_CLUSTER) {
2356 if (nc_cluster_idx ==
2357 page->index >> cc.log_cluster_size) {
2358 goto read_single_page;
2361 ret = f2fs_is_compressed_cluster(inode, page->index);
2363 goto set_error_page;
2366 page->index >> cc.log_cluster_size;
2367 goto read_single_page;
2370 nc_cluster_idx = NULL_CLUSTER;
2372 ret = f2fs_init_compress_ctx(&cc);
2374 goto set_error_page;
2376 f2fs_compress_ctx_add_page(&cc, page);
2383 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2384 &bio, &last_block_in_bio, rac);
2386 #ifdef CONFIG_F2FS_FS_COMPRESSION
2390 zero_user_segment(page, 0, PAGE_SIZE);
2393 #ifdef CONFIG_F2FS_FS_COMPRESSION
2399 #ifdef CONFIG_F2FS_FS_COMPRESSION
2400 if (f2fs_compressed_file(inode)) {
2402 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2403 ret = f2fs_read_multi_pages(&cc, &bio,
2406 rac != NULL, false);
2407 f2fs_destroy_compress_ctx(&cc, false);
2413 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2417 static int f2fs_read_data_folio(struct file *file, struct folio *folio)
2419 struct page *page = &folio->page;
2420 struct inode *inode = page_file_mapping(page)->host;
2423 trace_f2fs_readpage(page, DATA);
2425 if (!f2fs_is_compress_backend_ready(inode)) {
2430 /* If the file has inline data, try to read it directly */
2431 if (f2fs_has_inline_data(inode))
2432 ret = f2fs_read_inline_data(inode, page);
2434 ret = f2fs_mpage_readpages(inode, NULL, page);
2438 static void f2fs_readahead(struct readahead_control *rac)
2440 struct inode *inode = rac->mapping->host;
2442 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2444 if (!f2fs_is_compress_backend_ready(inode))
2447 /* If the file has inline data, skip readahead */
2448 if (f2fs_has_inline_data(inode))
2451 f2fs_mpage_readpages(inode, rac, NULL);
2454 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2456 struct inode *inode = fio->page->mapping->host;
2457 struct page *mpage, *page;
2458 gfp_t gfp_flags = GFP_NOFS;
2460 if (!f2fs_encrypted_file(inode))
2463 page = fio->compressed_page ? fio->compressed_page : fio->page;
2465 /* wait for GCed page writeback via META_MAPPING */
2466 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2468 if (fscrypt_inode_uses_inline_crypto(inode))
2472 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2473 PAGE_SIZE, 0, gfp_flags);
2474 if (IS_ERR(fio->encrypted_page)) {
2475 /* flush pending IOs and wait for a while in the ENOMEM case */
2476 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2477 f2fs_flush_merged_writes(fio->sbi);
2478 memalloc_retry_wait(GFP_NOFS);
2479 gfp_flags |= __GFP_NOFAIL;
2482 return PTR_ERR(fio->encrypted_page);
2485 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2487 if (PageUptodate(mpage))
2488 memcpy(page_address(mpage),
2489 page_address(fio->encrypted_page), PAGE_SIZE);
2490 f2fs_put_page(mpage, 1);
2495 static inline bool check_inplace_update_policy(struct inode *inode,
2496 struct f2fs_io_info *fio)
2498 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2499 unsigned int policy = SM_I(sbi)->ipu_policy;
2501 if (policy & (0x1 << F2FS_IPU_HONOR_OPU_WRITE) &&
2502 is_inode_flag_set(inode, FI_OPU_WRITE))
2504 if (policy & (0x1 << F2FS_IPU_FORCE))
2506 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2508 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2509 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2511 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2512 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2516 * IPU for rewrite async pages
2518 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2519 fio && fio->op == REQ_OP_WRITE &&
2520 !(fio->op_flags & REQ_SYNC) &&
2521 !IS_ENCRYPTED(inode))
2524 /* this is only set during fdatasync */
2525 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2526 is_inode_flag_set(inode, FI_NEED_IPU))
2529 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2530 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2536 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2538 /* swap file is migrating in aligned write mode */
2539 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2542 if (f2fs_is_pinned_file(inode))
2545 /* if this is cold file, we should overwrite to avoid fragmentation */
2546 if (file_is_cold(inode))
2549 return check_inplace_update_policy(inode, fio);
2552 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2554 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2556 /* The below cases were checked when setting it. */
2557 if (f2fs_is_pinned_file(inode))
2559 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2561 if (f2fs_lfs_mode(sbi))
2563 if (S_ISDIR(inode->i_mode))
2565 if (IS_NOQUOTA(inode))
2567 if (f2fs_is_atomic_file(inode))
2570 /* swap file is migrating in aligned write mode */
2571 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2574 if (is_inode_flag_set(inode, FI_OPU_WRITE))
2578 if (page_private_gcing(fio->page))
2580 if (page_private_dummy(fio->page))
2582 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2583 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2589 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2591 struct inode *inode = fio->page->mapping->host;
2593 if (f2fs_should_update_outplace(inode, fio))
2596 return f2fs_should_update_inplace(inode, fio);
2599 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2601 struct page *page = fio->page;
2602 struct inode *inode = page->mapping->host;
2603 struct dnode_of_data dn;
2604 struct extent_info ei = {0, };
2605 struct node_info ni;
2606 bool ipu_force = false;
2609 /* Use COW inode to make dnode_of_data for atomic write */
2610 if (f2fs_is_atomic_file(inode))
2611 set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
2613 set_new_dnode(&dn, inode, NULL, NULL, 0);
2615 if (need_inplace_update(fio) &&
2616 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2617 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2619 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2620 DATA_GENERIC_ENHANCE))
2621 return -EFSCORRUPTED;
2624 fio->need_lock = LOCK_DONE;
2628 /* Deadlock due to between page->lock and f2fs_lock_op */
2629 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2632 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2636 fio->old_blkaddr = dn.data_blkaddr;
2638 /* This page is already truncated */
2639 if (fio->old_blkaddr == NULL_ADDR) {
2640 ClearPageUptodate(page);
2641 clear_page_private_gcing(page);
2645 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2646 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2647 DATA_GENERIC_ENHANCE)) {
2648 err = -EFSCORRUPTED;
2653 * If current allocation needs SSR,
2654 * it had better in-place writes for updated data.
2657 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2658 need_inplace_update(fio))) {
2659 err = f2fs_encrypt_one_page(fio);
2663 set_page_writeback(page);
2664 ClearPageError(page);
2665 f2fs_put_dnode(&dn);
2666 if (fio->need_lock == LOCK_REQ)
2667 f2fs_unlock_op(fio->sbi);
2668 err = f2fs_inplace_write_data(fio);
2670 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2671 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2672 if (PageWriteback(page))
2673 end_page_writeback(page);
2675 set_inode_flag(inode, FI_UPDATE_WRITE);
2677 trace_f2fs_do_write_data_page(fio->page, IPU);
2681 if (fio->need_lock == LOCK_RETRY) {
2682 if (!f2fs_trylock_op(fio->sbi)) {
2686 fio->need_lock = LOCK_REQ;
2689 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
2693 fio->version = ni.version;
2695 err = f2fs_encrypt_one_page(fio);
2699 set_page_writeback(page);
2700 ClearPageError(page);
2702 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2703 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2705 /* LFS mode write path */
2706 f2fs_outplace_write_data(&dn, fio);
2707 trace_f2fs_do_write_data_page(page, OPU);
2708 set_inode_flag(inode, FI_APPEND_WRITE);
2709 if (page->index == 0)
2710 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2712 f2fs_put_dnode(&dn);
2714 if (fio->need_lock == LOCK_REQ)
2715 f2fs_unlock_op(fio->sbi);
2719 int f2fs_write_single_data_page(struct page *page, int *submitted,
2721 sector_t *last_block,
2722 struct writeback_control *wbc,
2723 enum iostat_type io_type,
2727 struct inode *inode = page->mapping->host;
2728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2729 loff_t i_size = i_size_read(inode);
2730 const pgoff_t end_index = ((unsigned long long)i_size)
2732 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2733 unsigned offset = 0;
2734 bool need_balance_fs = false;
2736 struct f2fs_io_info fio = {
2738 .ino = inode->i_ino,
2741 .op_flags = wbc_to_write_flags(wbc),
2742 .old_blkaddr = NULL_ADDR,
2744 .encrypted_page = NULL,
2746 .compr_blocks = compr_blocks,
2747 .need_lock = LOCK_RETRY,
2748 .post_read = f2fs_post_read_required(inode),
2752 .last_block = last_block,
2755 trace_f2fs_writepage(page, DATA);
2757 /* we should bypass data pages to proceed the kworkder jobs */
2758 if (unlikely(f2fs_cp_error(sbi))) {
2759 mapping_set_error(page->mapping, -EIO);
2761 * don't drop any dirty dentry pages for keeping lastest
2762 * directory structure.
2764 if (S_ISDIR(inode->i_mode))
2769 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2772 if (page->index < end_index ||
2773 f2fs_verity_in_progress(inode) ||
2778 * If the offset is out-of-range of file size,
2779 * this page does not have to be written to disk.
2781 offset = i_size & (PAGE_SIZE - 1);
2782 if ((page->index >= end_index + 1) || !offset)
2785 zero_user_segment(page, offset, PAGE_SIZE);
2787 if (f2fs_is_drop_cache(inode))
2790 /* Dentry/quota blocks are controlled by checkpoint */
2791 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2793 * We need to wait for node_write to avoid block allocation during
2794 * checkpoint. This can only happen to quota writes which can cause
2795 * the below discard race condition.
2797 if (IS_NOQUOTA(inode))
2798 f2fs_down_read(&sbi->node_write);
2800 fio.need_lock = LOCK_DONE;
2801 err = f2fs_do_write_data_page(&fio);
2803 if (IS_NOQUOTA(inode))
2804 f2fs_up_read(&sbi->node_write);
2809 if (!wbc->for_reclaim)
2810 need_balance_fs = true;
2811 else if (has_not_enough_free_secs(sbi, 0, 0))
2814 set_inode_flag(inode, FI_HOT_DATA);
2817 if (f2fs_has_inline_data(inode)) {
2818 err = f2fs_write_inline_data(inode, page);
2823 if (err == -EAGAIN) {
2824 err = f2fs_do_write_data_page(&fio);
2825 if (err == -EAGAIN) {
2826 fio.need_lock = LOCK_REQ;
2827 err = f2fs_do_write_data_page(&fio);
2832 file_set_keep_isize(inode);
2834 spin_lock(&F2FS_I(inode)->i_size_lock);
2835 if (F2FS_I(inode)->last_disk_size < psize)
2836 F2FS_I(inode)->last_disk_size = psize;
2837 spin_unlock(&F2FS_I(inode)->i_size_lock);
2841 if (err && err != -ENOENT)
2845 inode_dec_dirty_pages(inode);
2847 ClearPageUptodate(page);
2848 clear_page_private_gcing(page);
2851 if (wbc->for_reclaim) {
2852 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2853 clear_inode_flag(inode, FI_HOT_DATA);
2854 f2fs_remove_dirty_inode(inode);
2858 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2859 !F2FS_I(inode)->cp_task && allow_balance)
2860 f2fs_balance_fs(sbi, need_balance_fs);
2862 if (unlikely(f2fs_cp_error(sbi))) {
2863 f2fs_submit_merged_write(sbi, DATA);
2864 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2869 *submitted = fio.submitted ? 1 : 0;
2874 redirty_page_for_writepage(wbc, page);
2876 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2877 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2878 * file_write_and_wait_range() will see EIO error, which is critical
2879 * to return value of fsync() followed by atomic_write failure to user.
2881 if (!err || wbc->for_reclaim)
2882 return AOP_WRITEPAGE_ACTIVATE;
2887 static int f2fs_write_data_page(struct page *page,
2888 struct writeback_control *wbc)
2890 #ifdef CONFIG_F2FS_FS_COMPRESSION
2891 struct inode *inode = page->mapping->host;
2893 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2896 if (f2fs_compressed_file(inode)) {
2897 if (f2fs_is_compressed_cluster(inode, page->index)) {
2898 redirty_page_for_writepage(wbc, page);
2899 return AOP_WRITEPAGE_ACTIVATE;
2905 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2906 wbc, FS_DATA_IO, 0, true);
2910 * This function was copied from write_cche_pages from mm/page-writeback.c.
2911 * The major change is making write step of cold data page separately from
2912 * warm/hot data page.
2914 static int f2fs_write_cache_pages(struct address_space *mapping,
2915 struct writeback_control *wbc,
2916 enum iostat_type io_type)
2919 int done = 0, retry = 0;
2920 struct page *pages[F2FS_ONSTACK_PAGES];
2921 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2922 struct bio *bio = NULL;
2923 sector_t last_block;
2924 #ifdef CONFIG_F2FS_FS_COMPRESSION
2925 struct inode *inode = mapping->host;
2926 struct compress_ctx cc = {
2928 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2929 .cluster_size = F2FS_I(inode)->i_cluster_size,
2930 .cluster_idx = NULL_CLUSTER,
2934 .valid_nr_cpages = 0,
2937 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2943 pgoff_t end; /* Inclusive */
2945 int range_whole = 0;
2951 if (get_dirty_pages(mapping->host) <=
2952 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2953 set_inode_flag(mapping->host, FI_HOT_DATA);
2955 clear_inode_flag(mapping->host, FI_HOT_DATA);
2957 if (wbc->range_cyclic) {
2958 index = mapping->writeback_index; /* prev offset */
2961 index = wbc->range_start >> PAGE_SHIFT;
2962 end = wbc->range_end >> PAGE_SHIFT;
2963 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2966 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2967 tag = PAGECACHE_TAG_TOWRITE;
2969 tag = PAGECACHE_TAG_DIRTY;
2972 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2973 tag_pages_for_writeback(mapping, index, end);
2975 while (!done && !retry && (index <= end)) {
2976 nr_pages = find_get_pages_range_tag(mapping, &index, end,
2977 tag, F2FS_ONSTACK_PAGES, pages);
2981 for (i = 0; i < nr_pages; i++) {
2982 struct page *page = pages[i];
2986 #ifdef CONFIG_F2FS_FS_COMPRESSION
2987 if (f2fs_compressed_file(inode)) {
2988 void *fsdata = NULL;
2992 ret = f2fs_init_compress_ctx(&cc);
2998 if (!f2fs_cluster_can_merge_page(&cc,
3000 ret = f2fs_write_multi_pages(&cc,
3001 &submitted, wbc, io_type);
3007 if (unlikely(f2fs_cp_error(sbi)))
3010 if (!f2fs_cluster_is_empty(&cc))
3013 if (f2fs_all_cluster_page_ready(&cc,
3014 pages, i, nr_pages, true))
3017 ret2 = f2fs_prepare_compress_overwrite(
3019 page->index, &fsdata);
3025 (!f2fs_compress_write_end(inode,
3026 fsdata, page->index, 1) ||
3027 !f2fs_all_cluster_page_ready(&cc,
3028 pages, i, nr_pages, false))) {
3034 /* give a priority to WB_SYNC threads */
3035 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3036 wbc->sync_mode == WB_SYNC_NONE) {
3040 #ifdef CONFIG_F2FS_FS_COMPRESSION
3043 done_index = page->index;
3047 if (unlikely(page->mapping != mapping)) {
3053 if (!PageDirty(page)) {
3054 /* someone wrote it for us */
3055 goto continue_unlock;
3058 if (PageWriteback(page)) {
3059 if (wbc->sync_mode != WB_SYNC_NONE)
3060 f2fs_wait_on_page_writeback(page,
3063 goto continue_unlock;
3066 if (!clear_page_dirty_for_io(page))
3067 goto continue_unlock;
3069 #ifdef CONFIG_F2FS_FS_COMPRESSION
3070 if (f2fs_compressed_file(inode)) {
3072 f2fs_compress_ctx_add_page(&cc, page);
3076 ret = f2fs_write_single_data_page(page, &submitted,
3077 &bio, &last_block, wbc, io_type,
3079 if (ret == AOP_WRITEPAGE_ACTIVATE)
3081 #ifdef CONFIG_F2FS_FS_COMPRESSION
3084 nwritten += submitted;
3085 wbc->nr_to_write -= submitted;
3087 if (unlikely(ret)) {
3089 * keep nr_to_write, since vfs uses this to
3090 * get # of written pages.
3092 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3095 } else if (ret == -EAGAIN) {
3097 if (wbc->sync_mode == WB_SYNC_ALL) {
3098 f2fs_io_schedule_timeout(
3099 DEFAULT_IO_TIMEOUT);
3104 done_index = page->index + 1;
3109 if (wbc->nr_to_write <= 0 &&
3110 wbc->sync_mode == WB_SYNC_NONE) {
3118 release_pages(pages, nr_pages);
3121 #ifdef CONFIG_F2FS_FS_COMPRESSION
3122 /* flush remained pages in compress cluster */
3123 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3124 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3125 nwritten += submitted;
3126 wbc->nr_to_write -= submitted;
3132 if (f2fs_compressed_file(inode))
3133 f2fs_destroy_compress_ctx(&cc, false);
3140 if (wbc->range_cyclic && !done)
3142 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3143 mapping->writeback_index = done_index;
3146 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3148 /* submit cached bio of IPU write */
3150 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3155 static inline bool __should_serialize_io(struct inode *inode,
3156 struct writeback_control *wbc)
3158 /* to avoid deadlock in path of data flush */
3159 if (F2FS_I(inode)->cp_task)
3162 if (!S_ISREG(inode->i_mode))
3164 if (IS_NOQUOTA(inode))
3167 if (f2fs_need_compress_data(inode))
3169 if (wbc->sync_mode != WB_SYNC_ALL)
3171 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3176 static int __f2fs_write_data_pages(struct address_space *mapping,
3177 struct writeback_control *wbc,
3178 enum iostat_type io_type)
3180 struct inode *inode = mapping->host;
3181 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3182 struct blk_plug plug;
3184 bool locked = false;
3186 /* deal with chardevs and other special file */
3187 if (!mapping->a_ops->writepage)
3190 /* skip writing if there is no dirty page in this inode */
3191 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3194 /* during POR, we don't need to trigger writepage at all. */
3195 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3198 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3199 wbc->sync_mode == WB_SYNC_NONE &&
3200 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3201 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3204 /* skip writing in file defragment preparing stage */
3205 if (is_inode_flag_set(inode, FI_SKIP_WRITES))
3208 trace_f2fs_writepages(mapping->host, wbc, DATA);
3210 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3211 if (wbc->sync_mode == WB_SYNC_ALL)
3212 atomic_inc(&sbi->wb_sync_req[DATA]);
3213 else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3214 /* to avoid potential deadlock */
3216 blk_finish_plug(current->plug);
3220 if (__should_serialize_io(inode, wbc)) {
3221 mutex_lock(&sbi->writepages);
3225 blk_start_plug(&plug);
3226 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3227 blk_finish_plug(&plug);
3230 mutex_unlock(&sbi->writepages);
3232 if (wbc->sync_mode == WB_SYNC_ALL)
3233 atomic_dec(&sbi->wb_sync_req[DATA]);
3235 * if some pages were truncated, we cannot guarantee its mapping->host
3236 * to detect pending bios.
3239 f2fs_remove_dirty_inode(inode);
3243 wbc->pages_skipped += get_dirty_pages(inode);
3244 trace_f2fs_writepages(mapping->host, wbc, DATA);
3248 static int f2fs_write_data_pages(struct address_space *mapping,
3249 struct writeback_control *wbc)
3251 struct inode *inode = mapping->host;
3253 return __f2fs_write_data_pages(mapping, wbc,
3254 F2FS_I(inode)->cp_task == current ?
3255 FS_CP_DATA_IO : FS_DATA_IO);
3258 void f2fs_write_failed(struct inode *inode, loff_t to)
3260 loff_t i_size = i_size_read(inode);
3262 if (IS_NOQUOTA(inode))
3265 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3266 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3267 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3268 filemap_invalidate_lock(inode->i_mapping);
3270 truncate_pagecache(inode, i_size);
3271 f2fs_truncate_blocks(inode, i_size, true);
3273 filemap_invalidate_unlock(inode->i_mapping);
3274 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3278 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3279 struct page *page, loff_t pos, unsigned len,
3280 block_t *blk_addr, bool *node_changed)
3282 struct inode *inode = page->mapping->host;
3283 pgoff_t index = page->index;
3284 struct dnode_of_data dn;
3286 bool locked = false;
3287 struct extent_info ei = {0, };
3292 * If a whole page is being written and we already preallocated all the
3293 * blocks, then there is no need to get a block address now.
3295 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
3298 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3299 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3300 flag = F2FS_GET_BLOCK_DEFAULT;
3302 flag = F2FS_GET_BLOCK_PRE_AIO;
3304 if (f2fs_has_inline_data(inode) ||
3305 (pos & PAGE_MASK) >= i_size_read(inode)) {
3306 f2fs_do_map_lock(sbi, flag, true);
3311 /* check inline_data */
3312 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3313 if (IS_ERR(ipage)) {
3314 err = PTR_ERR(ipage);
3318 set_new_dnode(&dn, inode, ipage, ipage, 0);
3320 if (f2fs_has_inline_data(inode)) {
3321 if (pos + len <= MAX_INLINE_DATA(inode)) {
3322 f2fs_do_read_inline_data(page, ipage);
3323 set_inode_flag(inode, FI_DATA_EXIST);
3325 set_page_private_inline(ipage);
3327 err = f2fs_convert_inline_page(&dn, page);
3330 if (dn.data_blkaddr == NULL_ADDR)
3331 err = f2fs_get_block(&dn, index);
3333 } else if (locked) {
3334 err = f2fs_get_block(&dn, index);
3336 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3337 dn.data_blkaddr = ei.blk + index - ei.fofs;
3340 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3341 if (err || dn.data_blkaddr == NULL_ADDR) {
3342 f2fs_put_dnode(&dn);
3343 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3345 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3352 /* convert_inline_page can make node_changed */
3353 *blk_addr = dn.data_blkaddr;
3354 *node_changed = dn.node_changed;
3356 f2fs_put_dnode(&dn);
3359 f2fs_do_map_lock(sbi, flag, false);
3363 static int __find_data_block(struct inode *inode, pgoff_t index,
3366 struct dnode_of_data dn;
3368 struct extent_info ei = {0, };
3371 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
3373 return PTR_ERR(ipage);
3375 set_new_dnode(&dn, inode, ipage, ipage, 0);
3377 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3378 dn.data_blkaddr = ei.blk + index - ei.fofs;
3381 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3383 dn.data_blkaddr = NULL_ADDR;
3387 *blk_addr = dn.data_blkaddr;
3388 f2fs_put_dnode(&dn);
3392 static int __reserve_data_block(struct inode *inode, pgoff_t index,
3393 block_t *blk_addr, bool *node_changed)
3395 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3396 struct dnode_of_data dn;
3400 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
3402 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3403 if (IS_ERR(ipage)) {
3404 err = PTR_ERR(ipage);
3407 set_new_dnode(&dn, inode, ipage, ipage, 0);
3409 err = f2fs_get_block(&dn, index);
3411 *blk_addr = dn.data_blkaddr;
3412 *node_changed = dn.node_changed;
3413 f2fs_put_dnode(&dn);
3416 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
3420 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
3421 struct page *page, loff_t pos, unsigned int len,
3422 block_t *blk_addr, bool *node_changed)
3424 struct inode *inode = page->mapping->host;
3425 struct inode *cow_inode = F2FS_I(inode)->cow_inode;
3426 pgoff_t index = page->index;
3428 block_t ori_blk_addr = NULL_ADDR;
3430 /* If pos is beyond the end of file, reserve a new block in COW inode */
3431 if ((pos & PAGE_MASK) >= i_size_read(inode))
3434 /* Look for the block in COW inode first */
3435 err = __find_data_block(cow_inode, index, blk_addr);
3438 else if (*blk_addr != NULL_ADDR)
3441 /* Look for the block in the original inode */
3442 err = __find_data_block(inode, index, &ori_blk_addr);
3447 /* Finally, we should reserve a new block in COW inode for the update */
3448 err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3451 inc_atomic_write_cnt(inode);
3453 if (ori_blk_addr != NULL_ADDR)
3454 *blk_addr = ori_blk_addr;
3458 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3459 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
3461 struct inode *inode = mapping->host;
3462 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3463 struct page *page = NULL;
3464 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3465 bool need_balance = false;
3466 block_t blkaddr = NULL_ADDR;
3469 trace_f2fs_write_begin(inode, pos, len);
3471 if (!f2fs_is_checkpoint_ready(sbi)) {
3477 * We should check this at this moment to avoid deadlock on inode page
3478 * and #0 page. The locking rule for inline_data conversion should be:
3479 * lock_page(page #0) -> lock_page(inode_page)
3482 err = f2fs_convert_inline_inode(inode);
3487 #ifdef CONFIG_F2FS_FS_COMPRESSION
3488 if (f2fs_compressed_file(inode)) {
3493 if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3496 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3509 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3510 * wait_for_stable_page. Will wait that below with our IO control.
3512 page = f2fs_pagecache_get_page(mapping, index,
3513 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3519 /* TODO: cluster can be compressed due to race with .writepage */
3523 if (f2fs_is_atomic_file(inode))
3524 err = prepare_atomic_write_begin(sbi, page, pos, len,
3525 &blkaddr, &need_balance);
3527 err = prepare_write_begin(sbi, page, pos, len,
3528 &blkaddr, &need_balance);
3532 if (need_balance && !IS_NOQUOTA(inode) &&
3533 has_not_enough_free_secs(sbi, 0, 0)) {
3535 f2fs_balance_fs(sbi, true);
3537 if (page->mapping != mapping) {
3538 /* The page got truncated from under us */
3539 f2fs_put_page(page, 1);
3544 f2fs_wait_on_page_writeback(page, DATA, false, true);
3546 if (len == PAGE_SIZE || PageUptodate(page))
3549 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3550 !f2fs_verity_in_progress(inode)) {
3551 zero_user_segment(page, len, PAGE_SIZE);
3555 if (blkaddr == NEW_ADDR) {
3556 zero_user_segment(page, 0, PAGE_SIZE);
3557 SetPageUptodate(page);
3559 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3560 DATA_GENERIC_ENHANCE_READ)) {
3561 err = -EFSCORRUPTED;
3564 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3569 if (unlikely(page->mapping != mapping)) {
3570 f2fs_put_page(page, 1);
3573 if (unlikely(!PageUptodate(page))) {
3581 f2fs_put_page(page, 1);
3582 f2fs_write_failed(inode, pos + len);
3586 static int f2fs_write_end(struct file *file,
3587 struct address_space *mapping,
3588 loff_t pos, unsigned len, unsigned copied,
3589 struct page *page, void *fsdata)
3591 struct inode *inode = page->mapping->host;
3593 trace_f2fs_write_end(inode, pos, len, copied);
3596 * This should be come from len == PAGE_SIZE, and we expect copied
3597 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3598 * let generic_perform_write() try to copy data again through copied=0.
3600 if (!PageUptodate(page)) {
3601 if (unlikely(copied != len))
3604 SetPageUptodate(page);
3607 #ifdef CONFIG_F2FS_FS_COMPRESSION
3608 /* overwrite compressed file */
3609 if (f2fs_compressed_file(inode) && fsdata) {
3610 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3611 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3613 if (pos + copied > i_size_read(inode) &&
3614 !f2fs_verity_in_progress(inode))
3615 f2fs_i_size_write(inode, pos + copied);
3623 set_page_dirty(page);
3625 if (pos + copied > i_size_read(inode) &&
3626 !f2fs_verity_in_progress(inode)) {
3627 f2fs_i_size_write(inode, pos + copied);
3628 if (f2fs_is_atomic_file(inode))
3629 f2fs_i_size_write(F2FS_I(inode)->cow_inode,
3633 f2fs_put_page(page, 1);
3634 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3638 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
3640 struct inode *inode = folio->mapping->host;
3641 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3643 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3644 (offset || length != folio_size(folio)))
3647 if (folio_test_dirty(folio)) {
3648 if (inode->i_ino == F2FS_META_INO(sbi)) {
3649 dec_page_count(sbi, F2FS_DIRTY_META);
3650 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3651 dec_page_count(sbi, F2FS_DIRTY_NODES);
3653 inode_dec_dirty_pages(inode);
3654 f2fs_remove_dirty_inode(inode);
3658 clear_page_private_gcing(&folio->page);
3660 if (test_opt(sbi, COMPRESS_CACHE) &&
3661 inode->i_ino == F2FS_COMPRESS_INO(sbi))
3662 clear_page_private_data(&folio->page);
3664 folio_detach_private(folio);
3667 bool f2fs_release_folio(struct folio *folio, gfp_t wait)
3669 struct f2fs_sb_info *sbi;
3671 /* If this is dirty folio, keep private data */
3672 if (folio_test_dirty(folio))
3675 sbi = F2FS_M_SB(folio->mapping);
3676 if (test_opt(sbi, COMPRESS_CACHE)) {
3677 struct inode *inode = folio->mapping->host;
3679 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3680 clear_page_private_data(&folio->page);
3683 clear_page_private_gcing(&folio->page);
3685 folio_detach_private(folio);
3689 static bool f2fs_dirty_data_folio(struct address_space *mapping,
3690 struct folio *folio)
3692 struct inode *inode = mapping->host;
3694 trace_f2fs_set_page_dirty(&folio->page, DATA);
3696 if (!folio_test_uptodate(folio))
3697 folio_mark_uptodate(folio);
3698 BUG_ON(folio_test_swapcache(folio));
3700 if (!folio_test_dirty(folio)) {
3701 filemap_dirty_folio(mapping, folio);
3702 f2fs_update_dirty_folio(inode, folio);
3709 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3711 #ifdef CONFIG_F2FS_FS_COMPRESSION
3712 struct dnode_of_data dn;
3713 sector_t start_idx, blknr = 0;
3716 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3718 set_new_dnode(&dn, inode, NULL, NULL, 0);
3719 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3723 if (dn.data_blkaddr != COMPRESS_ADDR) {
3724 dn.ofs_in_node += block - start_idx;
3725 blknr = f2fs_data_blkaddr(&dn);
3726 if (!__is_valid_data_blkaddr(blknr))
3730 f2fs_put_dnode(&dn);
3738 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3740 struct inode *inode = mapping->host;
3743 if (f2fs_has_inline_data(inode))
3746 /* make sure allocating whole blocks */
3747 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3748 filemap_write_and_wait(mapping);
3750 /* Block number less than F2FS MAX BLOCKS */
3751 if (unlikely(block >= max_file_blocks(inode)))
3754 if (f2fs_compressed_file(inode)) {
3755 blknr = f2fs_bmap_compress(inode, block);
3757 struct f2fs_map_blocks map;
3759 memset(&map, 0, sizeof(map));
3762 map.m_next_pgofs = NULL;
3763 map.m_seg_type = NO_CHECK_TYPE;
3765 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3769 trace_f2fs_bmap(inode, block, blknr);
3774 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3775 unsigned int blkcnt)
3777 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3778 unsigned int blkofs;
3779 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3780 unsigned int secidx = start_blk / blk_per_sec;
3781 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3784 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3785 filemap_invalidate_lock(inode->i_mapping);
3787 set_inode_flag(inode, FI_ALIGNED_WRITE);
3788 set_inode_flag(inode, FI_OPU_WRITE);
3790 for (; secidx < end_sec; secidx++) {
3791 f2fs_down_write(&sbi->pin_sem);
3794 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3795 f2fs_unlock_op(sbi);
3797 set_inode_flag(inode, FI_SKIP_WRITES);
3799 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3801 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3803 page = f2fs_get_lock_data_page(inode, blkidx, true);
3805 f2fs_up_write(&sbi->pin_sem);
3806 ret = PTR_ERR(page);
3810 set_page_dirty(page);
3811 f2fs_put_page(page, 1);
3814 clear_inode_flag(inode, FI_SKIP_WRITES);
3816 ret = filemap_fdatawrite(inode->i_mapping);
3818 f2fs_up_write(&sbi->pin_sem);
3825 clear_inode_flag(inode, FI_SKIP_WRITES);
3826 clear_inode_flag(inode, FI_OPU_WRITE);
3827 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3829 filemap_invalidate_unlock(inode->i_mapping);
3830 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3835 static int check_swap_activate(struct swap_info_struct *sis,
3836 struct file *swap_file, sector_t *span)
3838 struct address_space *mapping = swap_file->f_mapping;
3839 struct inode *inode = mapping->host;
3840 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3841 sector_t cur_lblock;
3842 sector_t last_lblock;
3844 sector_t lowest_pblock = -1;
3845 sector_t highest_pblock = 0;
3847 unsigned long nr_pblocks;
3848 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3849 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3850 unsigned int not_aligned = 0;
3854 * Map all the blocks into the extent list. This code doesn't try
3858 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3860 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3861 struct f2fs_map_blocks map;
3865 memset(&map, 0, sizeof(map));
3866 map.m_lblk = cur_lblock;
3867 map.m_len = last_lblock - cur_lblock;
3868 map.m_next_pgofs = NULL;
3869 map.m_next_extent = NULL;
3870 map.m_seg_type = NO_CHECK_TYPE;
3871 map.m_may_create = false;
3873 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3878 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3879 f2fs_err(sbi, "Swapfile has holes");
3884 pblock = map.m_pblk;
3885 nr_pblocks = map.m_len;
3887 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3888 nr_pblocks & sec_blks_mask) {
3891 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3892 if (cur_lblock + nr_pblocks > sis->max)
3893 nr_pblocks -= blks_per_sec;
3896 /* this extent is last one */
3897 nr_pblocks = map.m_len;
3898 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
3902 ret = f2fs_migrate_blocks(inode, cur_lblock,
3909 if (cur_lblock + nr_pblocks >= sis->max)
3910 nr_pblocks = sis->max - cur_lblock;
3912 if (cur_lblock) { /* exclude the header page */
3913 if (pblock < lowest_pblock)
3914 lowest_pblock = pblock;
3915 if (pblock + nr_pblocks - 1 > highest_pblock)
3916 highest_pblock = pblock + nr_pblocks - 1;
3920 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3922 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3926 cur_lblock += nr_pblocks;
3929 *span = 1 + highest_pblock - lowest_pblock;
3930 if (cur_lblock == 0)
3931 cur_lblock = 1; /* force Empty message */
3932 sis->max = cur_lblock;
3933 sis->pages = cur_lblock - 1;
3934 sis->highest_bit = cur_lblock - 1;
3937 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
3938 not_aligned, blks_per_sec * F2FS_BLKSIZE);
3942 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3945 struct inode *inode = file_inode(file);
3948 if (!S_ISREG(inode->i_mode))
3951 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3954 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
3955 f2fs_err(F2FS_I_SB(inode),
3956 "Swapfile not supported in LFS mode");
3960 ret = f2fs_convert_inline_inode(inode);
3964 if (!f2fs_disable_compressed_file(inode))
3967 f2fs_precache_extents(inode);
3969 ret = check_swap_activate(sis, file, span);
3973 set_inode_flag(inode, FI_PIN_FILE);
3974 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3978 static void f2fs_swap_deactivate(struct file *file)
3980 struct inode *inode = file_inode(file);
3982 clear_inode_flag(inode, FI_PIN_FILE);
3985 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3991 static void f2fs_swap_deactivate(struct file *file)
3996 const struct address_space_operations f2fs_dblock_aops = {
3997 .read_folio = f2fs_read_data_folio,
3998 .readahead = f2fs_readahead,
3999 .writepage = f2fs_write_data_page,
4000 .writepages = f2fs_write_data_pages,
4001 .write_begin = f2fs_write_begin,
4002 .write_end = f2fs_write_end,
4003 .dirty_folio = f2fs_dirty_data_folio,
4004 .migrate_folio = filemap_migrate_folio,
4005 .invalidate_folio = f2fs_invalidate_folio,
4006 .release_folio = f2fs_release_folio,
4007 .direct_IO = noop_direct_IO,
4009 .swap_activate = f2fs_swap_activate,
4010 .swap_deactivate = f2fs_swap_deactivate,
4013 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4015 struct address_space *mapping = page_mapping(page);
4016 unsigned long flags;
4018 xa_lock_irqsave(&mapping->i_pages, flags);
4019 __xa_clear_mark(&mapping->i_pages, page_index(page),
4020 PAGECACHE_TAG_DIRTY);
4021 xa_unlock_irqrestore(&mapping->i_pages, flags);
4024 int __init f2fs_init_post_read_processing(void)
4026 bio_post_read_ctx_cache =
4027 kmem_cache_create("f2fs_bio_post_read_ctx",
4028 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4029 if (!bio_post_read_ctx_cache)
4031 bio_post_read_ctx_pool =
4032 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4033 bio_post_read_ctx_cache);
4034 if (!bio_post_read_ctx_pool)
4035 goto fail_free_cache;
4039 kmem_cache_destroy(bio_post_read_ctx_cache);
4044 void f2fs_destroy_post_read_processing(void)
4046 mempool_destroy(bio_post_read_ctx_pool);
4047 kmem_cache_destroy(bio_post_read_ctx_cache);
4050 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4052 if (!f2fs_sb_has_encrypt(sbi) &&
4053 !f2fs_sb_has_verity(sbi) &&
4054 !f2fs_sb_has_compression(sbi))
4057 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4058 WQ_UNBOUND | WQ_HIGHPRI,
4060 if (!sbi->post_read_wq)
4065 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4067 if (sbi->post_read_wq)
4068 destroy_workqueue(sbi->post_read_wq);
4071 int __init f2fs_init_bio_entry_cache(void)
4073 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4074 sizeof(struct bio_entry));
4075 if (!bio_entry_slab)
4080 void f2fs_destroy_bio_entry_cache(void)
4082 kmem_cache_destroy(bio_entry_slab);
4085 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4086 unsigned int flags, struct iomap *iomap,
4087 struct iomap *srcmap)
4089 struct f2fs_map_blocks map = {};
4090 pgoff_t next_pgofs = 0;
4093 map.m_lblk = bytes_to_blks(inode, offset);
4094 map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1;
4095 map.m_next_pgofs = &next_pgofs;
4096 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4097 if (flags & IOMAP_WRITE)
4098 map.m_may_create = true;
4100 err = f2fs_map_blocks(inode, &map, flags & IOMAP_WRITE,
4101 F2FS_GET_BLOCK_DIO);
4105 iomap->offset = blks_to_bytes(inode, map.m_lblk);
4108 * When inline encryption is enabled, sometimes I/O to an encrypted file
4109 * has to be broken up to guarantee DUN contiguity. Handle this by
4110 * limiting the length of the mapping returned.
4112 map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4114 if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) {
4115 iomap->length = blks_to_bytes(inode, map.m_len);
4116 if (map.m_flags & F2FS_MAP_MAPPED) {
4117 iomap->type = IOMAP_MAPPED;
4118 iomap->flags |= IOMAP_F_MERGED;
4120 iomap->type = IOMAP_UNWRITTEN;
4122 if (WARN_ON_ONCE(!__is_valid_data_blkaddr(map.m_pblk)))
4125 iomap->bdev = map.m_bdev;
4126 iomap->addr = blks_to_bytes(inode, map.m_pblk);
4128 iomap->length = blks_to_bytes(inode, next_pgofs) -
4130 iomap->type = IOMAP_HOLE;
4131 iomap->addr = IOMAP_NULL_ADDR;
4134 if (map.m_flags & F2FS_MAP_NEW)
4135 iomap->flags |= IOMAP_F_NEW;
4136 if ((inode->i_state & I_DIRTY_DATASYNC) ||
4137 offset + length > i_size_read(inode))
4138 iomap->flags |= IOMAP_F_DIRTY;
4143 const struct iomap_ops f2fs_iomap_ops = {
4144 .iomap_begin = f2fs_iomap_begin,