1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/blk-crypto.h>
18 #include <linux/swap.h>
19 #include <linux/prefetch.h>
20 #include <linux/uio.h>
21 #include <linux/cleancache.h>
22 #include <linux/sched/signal.h>
23 #include <linux/fiemap.h>
29 #include <trace/events/f2fs.h>
31 #define NUM_PREALLOC_POST_READ_CTXS 128
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static struct kmem_cache *bio_entry_slab;
35 static mempool_t *bio_post_read_ctx_pool;
36 static struct bio_set f2fs_bioset;
38 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
40 int __init f2fs_init_bioset(void)
42 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS))
48 void f2fs_destroy_bioset(void)
50 bioset_exit(&f2fs_bioset);
53 static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
54 unsigned int nr_iovecs)
56 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
59 struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
62 /* No failure on bio allocation */
63 return __f2fs_bio_alloc(GFP_NOIO, npages);
66 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
67 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
71 return __f2fs_bio_alloc(GFP_KERNEL, npages);
74 static bool __is_cp_guaranteed(struct page *page)
76 struct address_space *mapping = page->mapping;
78 struct f2fs_sb_info *sbi;
83 if (f2fs_is_compressed_page(page))
86 inode = mapping->host;
87 sbi = F2FS_I_SB(inode);
89 if (inode->i_ino == F2FS_META_INO(sbi) ||
90 inode->i_ino == F2FS_NODE_INO(sbi) ||
91 S_ISDIR(inode->i_mode) ||
92 (S_ISREG(inode->i_mode) &&
93 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
99 static enum count_type __read_io_type(struct page *page)
101 struct address_space *mapping = page_file_mapping(page);
104 struct inode *inode = mapping->host;
105 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
107 if (inode->i_ino == F2FS_META_INO(sbi))
110 if (inode->i_ino == F2FS_NODE_INO(sbi))
116 /* postprocessing steps for read bios */
117 enum bio_post_read_step {
119 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
120 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
124 struct bio_post_read_ctx {
126 struct f2fs_sb_info *sbi;
127 struct work_struct work;
128 unsigned int enabled_steps;
131 static void __read_end_io(struct bio *bio, bool compr, bool verity)
135 struct bvec_iter_all iter_all;
137 bio_for_each_segment_all(bv, bio, iter_all) {
140 #ifdef CONFIG_F2FS_FS_COMPRESSION
141 if (compr && f2fs_is_compressed_page(page)) {
142 f2fs_decompress_pages(bio, page, verity);
149 /* PG_error was set if any post_read step failed */
150 if (bio->bi_status || PageError(page)) {
151 ClearPageUptodate(page);
152 /* will re-read again later */
153 ClearPageError(page);
155 SetPageUptodate(page);
157 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
162 static void f2fs_release_read_bio(struct bio *bio);
163 static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
166 __read_end_io(bio, false, verity);
167 f2fs_release_read_bio(bio);
170 static void f2fs_decompress_bio(struct bio *bio, bool verity)
172 __read_end_io(bio, true, verity);
175 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
177 static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
179 fscrypt_decrypt_bio(ctx->bio);
182 static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
184 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
187 #ifdef CONFIG_F2FS_FS_COMPRESSION
188 static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
190 f2fs_decompress_end_io(rpages, cluster_size, false, true);
193 static void f2fs_verify_bio(struct bio *bio)
196 struct bvec_iter_all iter_all;
198 bio_for_each_segment_all(bv, bio, iter_all) {
199 struct page *page = bv->bv_page;
200 struct decompress_io_ctx *dic;
202 dic = (struct decompress_io_ctx *)page_private(page);
205 if (refcount_dec_not_one(&dic->ref))
207 f2fs_verify_pages(dic->rpages,
213 if (bio->bi_status || PageError(page))
216 if (fsverity_verify_page(page)) {
217 SetPageUptodate(page);
221 ClearPageUptodate(page);
222 ClearPageError(page);
224 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
230 static void f2fs_verity_work(struct work_struct *work)
232 struct bio_post_read_ctx *ctx =
233 container_of(work, struct bio_post_read_ctx, work);
234 struct bio *bio = ctx->bio;
235 #ifdef CONFIG_F2FS_FS_COMPRESSION
236 unsigned int enabled_steps = ctx->enabled_steps;
240 * fsverity_verify_bio() may call readpages() again, and while verity
241 * will be disabled for this, decryption may still be needed, resulting
242 * in another bio_post_read_ctx being allocated. So to prevent
243 * deadlocks we need to release the current ctx to the mempool first.
244 * This assumes that verity is the last post-read step.
246 mempool_free(ctx, bio_post_read_ctx_pool);
247 bio->bi_private = NULL;
249 #ifdef CONFIG_F2FS_FS_COMPRESSION
250 /* previous step is decompression */
251 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
252 f2fs_verify_bio(bio);
253 f2fs_release_read_bio(bio);
258 fsverity_verify_bio(bio);
259 __f2fs_read_end_io(bio, false, false);
262 static void f2fs_post_read_work(struct work_struct *work)
264 struct bio_post_read_ctx *ctx =
265 container_of(work, struct bio_post_read_ctx, work);
267 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
268 f2fs_decrypt_work(ctx);
270 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
271 f2fs_decompress_work(ctx);
273 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
274 INIT_WORK(&ctx->work, f2fs_verity_work);
275 fsverity_enqueue_verify_work(&ctx->work);
279 __f2fs_read_end_io(ctx->bio,
280 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
283 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
284 struct work_struct *work)
286 queue_work(sbi->post_read_wq, work);
289 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
292 * We use different work queues for decryption and for verity because
293 * verity may require reading metadata pages that need decryption, and
294 * we shouldn't recurse to the same workqueue.
297 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
298 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
299 INIT_WORK(&ctx->work, f2fs_post_read_work);
300 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
304 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
305 INIT_WORK(&ctx->work, f2fs_verity_work);
306 fsverity_enqueue_verify_work(&ctx->work);
310 __f2fs_read_end_io(ctx->bio, false, false);
313 static bool f2fs_bio_post_read_required(struct bio *bio)
315 return bio->bi_private;
318 static void f2fs_read_end_io(struct bio *bio)
320 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
322 if (time_to_inject(sbi, FAULT_READ_IO)) {
323 f2fs_show_injection_info(sbi, FAULT_READ_IO);
324 bio->bi_status = BLK_STS_IOERR;
327 if (f2fs_bio_post_read_required(bio)) {
328 struct bio_post_read_ctx *ctx = bio->bi_private;
330 bio_post_read_processing(ctx);
334 __f2fs_read_end_io(bio, false, false);
337 static void f2fs_write_end_io(struct bio *bio)
339 struct f2fs_sb_info *sbi = bio->bi_private;
340 struct bio_vec *bvec;
341 struct bvec_iter_all iter_all;
343 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
344 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
345 bio->bi_status = BLK_STS_IOERR;
348 bio_for_each_segment_all(bvec, bio, iter_all) {
349 struct page *page = bvec->bv_page;
350 enum count_type type = WB_DATA_TYPE(page);
352 if (IS_DUMMY_WRITTEN_PAGE(page)) {
353 set_page_private(page, (unsigned long)NULL);
354 ClearPagePrivate(page);
356 mempool_free(page, sbi->write_io_dummy);
358 if (unlikely(bio->bi_status))
359 f2fs_stop_checkpoint(sbi, true);
363 fscrypt_finalize_bounce_page(&page);
365 #ifdef CONFIG_F2FS_FS_COMPRESSION
366 if (f2fs_is_compressed_page(page)) {
367 f2fs_compress_write_end_io(bio, page);
372 if (unlikely(bio->bi_status)) {
373 mapping_set_error(page->mapping, -EIO);
374 if (type == F2FS_WB_CP_DATA)
375 f2fs_stop_checkpoint(sbi, true);
378 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
379 page->index != nid_of_node(page));
381 dec_page_count(sbi, type);
382 if (f2fs_in_warm_node_list(sbi, page))
383 f2fs_del_fsync_node_entry(sbi, page);
384 clear_cold_data(page);
385 end_page_writeback(page);
387 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
388 wq_has_sleeper(&sbi->cp_wait))
389 wake_up(&sbi->cp_wait);
394 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
395 block_t blk_addr, struct bio *bio)
397 struct block_device *bdev = sbi->sb->s_bdev;
400 if (f2fs_is_multi_device(sbi)) {
401 for (i = 0; i < sbi->s_ndevs; i++) {
402 if (FDEV(i).start_blk <= blk_addr &&
403 FDEV(i).end_blk >= blk_addr) {
404 blk_addr -= FDEV(i).start_blk;
411 bio_set_dev(bio, bdev);
412 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
417 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
421 if (!f2fs_is_multi_device(sbi))
424 for (i = 0; i < sbi->s_ndevs; i++)
425 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
431 * Return true, if pre_bio's bdev is same as its target device.
433 static bool __same_bdev(struct f2fs_sb_info *sbi,
434 block_t blk_addr, struct bio *bio)
436 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
437 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
440 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
442 struct f2fs_sb_info *sbi = fio->sbi;
445 bio = f2fs_bio_alloc(sbi, npages, true);
447 f2fs_target_device(sbi, fio->new_blkaddr, bio);
448 if (is_read_io(fio->op)) {
449 bio->bi_end_io = f2fs_read_end_io;
450 bio->bi_private = NULL;
452 bio->bi_end_io = f2fs_write_end_io;
453 bio->bi_private = sbi;
454 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
455 fio->type, fio->temp);
458 wbc_init_bio(fio->io_wbc, bio);
463 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
465 const struct f2fs_io_info *fio,
469 * The f2fs garbage collector sets ->encrypted_page when it wants to
470 * read/write raw data without encryption.
472 if (!fio || !fio->encrypted_page)
473 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
476 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
478 const struct f2fs_io_info *fio)
481 * The f2fs garbage collector sets ->encrypted_page when it wants to
482 * read/write raw data without encryption.
484 if (fio && fio->encrypted_page)
485 return !bio_has_crypt_ctx(bio);
487 return fscrypt_mergeable_bio(bio, inode, next_idx);
490 static inline void __submit_bio(struct f2fs_sb_info *sbi,
491 struct bio *bio, enum page_type type)
493 if (!is_read_io(bio_op(bio))) {
496 if (type != DATA && type != NODE)
499 if (f2fs_lfs_mode(sbi) && current->plug)
500 blk_finish_plug(current->plug);
502 if (F2FS_IO_ALIGNED(sbi))
505 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
506 start %= F2FS_IO_SIZE(sbi);
511 /* fill dummy pages */
512 for (; start < F2FS_IO_SIZE(sbi); start++) {
514 mempool_alloc(sbi->write_io_dummy,
515 GFP_NOIO | __GFP_NOFAIL);
516 f2fs_bug_on(sbi, !page);
518 zero_user_segment(page, 0, PAGE_SIZE);
519 SetPagePrivate(page);
520 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
522 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
526 * In the NODE case, we lose next block address chain. So, we
527 * need to do checkpoint in f2fs_sync_file.
530 set_sbi_flag(sbi, SBI_NEED_CP);
533 if (is_read_io(bio_op(bio)))
534 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
536 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
540 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
541 struct bio *bio, enum page_type type)
543 __submit_bio(sbi, bio, type);
546 static void __attach_io_flag(struct f2fs_io_info *fio)
548 struct f2fs_sb_info *sbi = fio->sbi;
549 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
550 unsigned int io_flag, fua_flag, meta_flag;
552 if (fio->type == DATA)
553 io_flag = sbi->data_io_flag;
554 else if (fio->type == NODE)
555 io_flag = sbi->node_io_flag;
559 fua_flag = io_flag & temp_mask;
560 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
563 * data/node io flag bits per temp:
564 * REQ_META | REQ_FUA |
565 * 5 | 4 | 3 | 2 | 1 | 0 |
566 * Cold | Warm | Hot | Cold | Warm | Hot |
568 if ((1 << fio->temp) & meta_flag)
569 fio->op_flags |= REQ_META;
570 if ((1 << fio->temp) & fua_flag)
571 fio->op_flags |= REQ_FUA;
574 static void __submit_merged_bio(struct f2fs_bio_info *io)
576 struct f2fs_io_info *fio = &io->fio;
581 __attach_io_flag(fio);
582 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
584 if (is_read_io(fio->op))
585 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
587 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
589 __submit_bio(io->sbi, io->bio, fio->type);
593 static bool __has_merged_page(struct bio *bio, struct inode *inode,
594 struct page *page, nid_t ino)
596 struct bio_vec *bvec;
597 struct bvec_iter_all iter_all;
602 if (!inode && !page && !ino)
605 bio_for_each_segment_all(bvec, bio, iter_all) {
606 struct page *target = bvec->bv_page;
608 if (fscrypt_is_bounce_page(target)) {
609 target = fscrypt_pagecache_page(target);
613 if (f2fs_is_compressed_page(target)) {
614 target = f2fs_compress_control_page(target);
619 if (inode && inode == target->mapping->host)
621 if (page && page == target)
623 if (ino && ino == ino_of_node(target))
630 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
631 enum page_type type, enum temp_type temp)
633 enum page_type btype = PAGE_TYPE_OF_BIO(type);
634 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
636 down_write(&io->io_rwsem);
638 /* change META to META_FLUSH in the checkpoint procedure */
639 if (type >= META_FLUSH) {
640 io->fio.type = META_FLUSH;
641 io->fio.op = REQ_OP_WRITE;
642 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
643 if (!test_opt(sbi, NOBARRIER))
644 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
646 __submit_merged_bio(io);
647 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 down_read(&io->io_rwsem);
663 ret = __has_merged_page(io->bio, inode, page, ino);
664 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);
710 f2fs_trace_ios(fio, 0);
712 /* Allocate a new bio */
713 bio = __bio_alloc(fio, 1);
715 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
716 fio->page->index, fio, GFP_NOIO);
718 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
723 if (fio->io_wbc && !is_read_io(fio->op))
724 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
726 __attach_io_flag(fio);
727 bio_set_op_attrs(bio, fio->op, fio->op_flags);
729 inc_page_count(fio->sbi, is_read_io(fio->op) ?
730 __read_io_type(page): WB_DATA_TYPE(fio->page));
732 __submit_bio(fio->sbi, bio, fio->type);
736 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
737 block_t last_blkaddr, block_t cur_blkaddr)
739 if (last_blkaddr + 1 != cur_blkaddr)
741 return __same_bdev(sbi, cur_blkaddr, bio);
744 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
745 struct f2fs_io_info *fio)
747 if (io->fio.op != fio->op)
749 return io->fio.op_flags == fio->op_flags;
752 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
753 struct f2fs_bio_info *io,
754 struct f2fs_io_info *fio,
755 block_t last_blkaddr,
758 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
759 unsigned int filled_blocks =
760 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
761 unsigned int io_size = F2FS_IO_SIZE(sbi);
762 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
764 /* IOs in bio is aligned and left space of vectors is not enough */
765 if (!(filled_blocks % io_size) && left_vecs < io_size)
768 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
770 return io_type_is_mergeable(io, fio);
773 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
774 struct page *page, enum temp_type temp)
776 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
777 struct bio_entry *be;
779 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
783 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
786 down_write(&io->bio_list_lock);
787 list_add_tail(&be->list, &io->bio_list);
788 up_write(&io->bio_list_lock);
791 static void del_bio_entry(struct bio_entry *be)
794 kmem_cache_free(bio_entry_slab, be);
797 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
800 struct f2fs_sb_info *sbi = fio->sbi;
805 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
806 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
807 struct list_head *head = &io->bio_list;
808 struct bio_entry *be;
810 down_write(&io->bio_list_lock);
811 list_for_each_entry(be, head, list) {
817 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
820 if (f2fs_crypt_mergeable_bio(*bio,
821 fio->page->mapping->host,
822 fio->page->index, fio) &&
823 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
829 /* page can't be merged into bio; submit the bio */
831 __submit_bio(sbi, *bio, DATA);
834 up_write(&io->bio_list_lock);
845 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
846 struct bio **bio, struct page *page)
850 struct bio *target = bio ? *bio : NULL;
852 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
853 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
854 struct list_head *head = &io->bio_list;
855 struct bio_entry *be;
857 if (list_empty(head))
860 down_read(&io->bio_list_lock);
861 list_for_each_entry(be, head, list) {
863 found = (target == be->bio);
865 found = __has_merged_page(be->bio, NULL,
870 up_read(&io->bio_list_lock);
877 down_write(&io->bio_list_lock);
878 list_for_each_entry(be, head, list) {
880 found = (target == be->bio);
882 found = __has_merged_page(be->bio, NULL,
890 up_write(&io->bio_list_lock);
894 __submit_bio(sbi, target, DATA);
901 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
903 struct bio *bio = *fio->bio;
904 struct page *page = fio->encrypted_page ?
905 fio->encrypted_page : fio->page;
907 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
908 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
909 return -EFSCORRUPTED;
911 trace_f2fs_submit_page_bio(page, fio);
912 f2fs_trace_ios(fio, 0);
914 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
916 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
919 bio = __bio_alloc(fio, BIO_MAX_PAGES);
920 __attach_io_flag(fio);
921 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
922 fio->page->index, fio, GFP_NOIO);
923 bio_set_op_attrs(bio, fio->op, fio->op_flags);
925 add_bio_entry(fio->sbi, bio, page, fio->temp);
927 if (add_ipu_page(fio, &bio, page))
932 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
934 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
936 *fio->last_block = fio->new_blkaddr;
942 void f2fs_submit_page_write(struct f2fs_io_info *fio)
944 struct f2fs_sb_info *sbi = fio->sbi;
945 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
946 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
947 struct page *bio_page;
949 f2fs_bug_on(sbi, is_read_io(fio->op));
951 down_write(&io->io_rwsem);
954 spin_lock(&io->io_lock);
955 if (list_empty(&io->io_list)) {
956 spin_unlock(&io->io_lock);
959 fio = list_first_entry(&io->io_list,
960 struct f2fs_io_info, list);
961 list_del(&fio->list);
962 spin_unlock(&io->io_lock);
965 verify_fio_blkaddr(fio);
967 if (fio->encrypted_page)
968 bio_page = fio->encrypted_page;
969 else if (fio->compressed_page)
970 bio_page = fio->compressed_page;
972 bio_page = fio->page;
974 /* set submitted = true as a return value */
975 fio->submitted = true;
977 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
980 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
982 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
983 bio_page->index, fio)))
984 __submit_merged_bio(io);
986 if (io->bio == NULL) {
987 if (F2FS_IO_ALIGNED(sbi) &&
988 (fio->type == DATA || fio->type == NODE) &&
989 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
990 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
994 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
995 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
996 bio_page->index, fio, GFP_NOIO);
1000 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
1001 __submit_merged_bio(io);
1006 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
1008 io->last_block_in_bio = fio->new_blkaddr;
1009 f2fs_trace_ios(fio, 0);
1011 trace_f2fs_submit_page_write(fio->page, fio);
1016 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1017 !f2fs_is_checkpoint_ready(sbi))
1018 __submit_merged_bio(io);
1019 up_write(&io->io_rwsem);
1022 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
1024 return fsverity_active(inode) &&
1025 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
1028 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1029 unsigned nr_pages, unsigned op_flag,
1030 pgoff_t first_idx, bool for_write)
1032 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1034 struct bio_post_read_ctx *ctx;
1035 unsigned int post_read_steps = 0;
1037 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
1040 return ERR_PTR(-ENOMEM);
1042 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1044 f2fs_target_device(sbi, blkaddr, bio);
1045 bio->bi_end_io = f2fs_read_end_io;
1046 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1048 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1049 post_read_steps |= 1 << STEP_DECRYPT;
1050 if (f2fs_compressed_file(inode))
1051 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1052 if (f2fs_need_verity(inode, first_idx))
1053 post_read_steps |= 1 << STEP_VERITY;
1055 if (post_read_steps) {
1056 /* Due to the mempool, this never fails. */
1057 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1060 ctx->enabled_steps = post_read_steps;
1061 bio->bi_private = ctx;
1067 static void f2fs_release_read_bio(struct bio *bio)
1069 if (bio->bi_private)
1070 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1074 /* This can handle encryption stuffs */
1075 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1076 block_t blkaddr, int op_flags, bool for_write)
1078 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1081 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1082 page->index, for_write);
1084 return PTR_ERR(bio);
1086 /* wait for GCed page writeback via META_MAPPING */
1087 f2fs_wait_on_block_writeback(inode, blkaddr);
1089 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1093 ClearPageError(page);
1094 inc_page_count(sbi, F2FS_RD_DATA);
1095 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1096 __submit_bio(sbi, bio, DATA);
1100 static void __set_data_blkaddr(struct dnode_of_data *dn)
1102 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1106 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1107 base = get_extra_isize(dn->inode);
1109 /* Get physical address of data block */
1110 addr_array = blkaddr_in_node(rn);
1111 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1115 * Lock ordering for the change of data block address:
1118 * update block addresses in the node page
1120 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1122 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1123 __set_data_blkaddr(dn);
1124 if (set_page_dirty(dn->node_page))
1125 dn->node_changed = true;
1128 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1130 dn->data_blkaddr = blkaddr;
1131 f2fs_set_data_blkaddr(dn);
1132 f2fs_update_extent_cache(dn);
1135 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1136 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1138 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1144 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1146 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1149 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1150 dn->ofs_in_node, count);
1152 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1154 for (; count > 0; dn->ofs_in_node++) {
1155 block_t blkaddr = f2fs_data_blkaddr(dn);
1156 if (blkaddr == NULL_ADDR) {
1157 dn->data_blkaddr = NEW_ADDR;
1158 __set_data_blkaddr(dn);
1163 if (set_page_dirty(dn->node_page))
1164 dn->node_changed = true;
1168 /* Should keep dn->ofs_in_node unchanged */
1169 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1171 unsigned int ofs_in_node = dn->ofs_in_node;
1174 ret = f2fs_reserve_new_blocks(dn, 1);
1175 dn->ofs_in_node = ofs_in_node;
1179 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1181 bool need_put = dn->inode_page ? false : true;
1184 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1188 if (dn->data_blkaddr == NULL_ADDR)
1189 err = f2fs_reserve_new_block(dn);
1190 if (err || need_put)
1195 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1197 struct extent_info ei = {0, 0, 0};
1198 struct inode *inode = dn->inode;
1200 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1201 dn->data_blkaddr = ei.blk + index - ei.fofs;
1205 return f2fs_reserve_block(dn, index);
1208 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1209 int op_flags, bool for_write)
1211 struct address_space *mapping = inode->i_mapping;
1212 struct dnode_of_data dn;
1214 struct extent_info ei = {0,0,0};
1217 page = f2fs_grab_cache_page(mapping, index, for_write);
1219 return ERR_PTR(-ENOMEM);
1221 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1222 dn.data_blkaddr = ei.blk + index - ei.fofs;
1223 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1224 DATA_GENERIC_ENHANCE_READ)) {
1225 err = -EFSCORRUPTED;
1231 set_new_dnode(&dn, inode, NULL, NULL, 0);
1232 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1235 f2fs_put_dnode(&dn);
1237 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1241 if (dn.data_blkaddr != NEW_ADDR &&
1242 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1244 DATA_GENERIC_ENHANCE)) {
1245 err = -EFSCORRUPTED;
1249 if (PageUptodate(page)) {
1255 * A new dentry page is allocated but not able to be written, since its
1256 * new inode page couldn't be allocated due to -ENOSPC.
1257 * In such the case, its blkaddr can be remained as NEW_ADDR.
1258 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1259 * f2fs_init_inode_metadata.
1261 if (dn.data_blkaddr == NEW_ADDR) {
1262 zero_user_segment(page, 0, PAGE_SIZE);
1263 if (!PageUptodate(page))
1264 SetPageUptodate(page);
1269 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1270 op_flags, for_write);
1276 f2fs_put_page(page, 1);
1277 return ERR_PTR(err);
1280 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1282 struct address_space *mapping = inode->i_mapping;
1285 page = find_get_page(mapping, index);
1286 if (page && PageUptodate(page))
1288 f2fs_put_page(page, 0);
1290 page = f2fs_get_read_data_page(inode, index, 0, false);
1294 if (PageUptodate(page))
1297 wait_on_page_locked(page);
1298 if (unlikely(!PageUptodate(page))) {
1299 f2fs_put_page(page, 0);
1300 return ERR_PTR(-EIO);
1306 * If it tries to access a hole, return an error.
1307 * Because, the callers, functions in dir.c and GC, should be able to know
1308 * whether this page exists or not.
1310 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1313 struct address_space *mapping = inode->i_mapping;
1316 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1320 /* wait for read completion */
1322 if (unlikely(page->mapping != mapping)) {
1323 f2fs_put_page(page, 1);
1326 if (unlikely(!PageUptodate(page))) {
1327 f2fs_put_page(page, 1);
1328 return ERR_PTR(-EIO);
1334 * Caller ensures that this data page is never allocated.
1335 * A new zero-filled data page is allocated in the page cache.
1337 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1339 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1340 * ipage should be released by this function.
1342 struct page *f2fs_get_new_data_page(struct inode *inode,
1343 struct page *ipage, pgoff_t index, bool new_i_size)
1345 struct address_space *mapping = inode->i_mapping;
1347 struct dnode_of_data dn;
1350 page = f2fs_grab_cache_page(mapping, index, true);
1353 * before exiting, we should make sure ipage will be released
1354 * if any error occur.
1356 f2fs_put_page(ipage, 1);
1357 return ERR_PTR(-ENOMEM);
1360 set_new_dnode(&dn, inode, ipage, NULL, 0);
1361 err = f2fs_reserve_block(&dn, index);
1363 f2fs_put_page(page, 1);
1364 return ERR_PTR(err);
1367 f2fs_put_dnode(&dn);
1369 if (PageUptodate(page))
1372 if (dn.data_blkaddr == NEW_ADDR) {
1373 zero_user_segment(page, 0, PAGE_SIZE);
1374 if (!PageUptodate(page))
1375 SetPageUptodate(page);
1377 f2fs_put_page(page, 1);
1379 /* if ipage exists, blkaddr should be NEW_ADDR */
1380 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1381 page = f2fs_get_lock_data_page(inode, index, true);
1386 if (new_i_size && i_size_read(inode) <
1387 ((loff_t)(index + 1) << PAGE_SHIFT))
1388 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1392 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1394 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1395 struct f2fs_summary sum;
1396 struct node_info ni;
1397 block_t old_blkaddr;
1401 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1404 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1408 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1409 if (dn->data_blkaddr != NULL_ADDR)
1412 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1416 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1417 old_blkaddr = dn->data_blkaddr;
1418 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1419 &sum, seg_type, NULL);
1420 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1421 invalidate_mapping_pages(META_MAPPING(sbi),
1422 old_blkaddr, old_blkaddr);
1423 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1426 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1427 * data from unwritten block via dio_read.
1432 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1434 struct inode *inode = file_inode(iocb->ki_filp);
1435 struct f2fs_map_blocks map;
1438 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1440 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1441 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1442 if (map.m_len > map.m_lblk)
1443 map.m_len -= map.m_lblk;
1447 map.m_next_pgofs = NULL;
1448 map.m_next_extent = NULL;
1449 map.m_seg_type = NO_CHECK_TYPE;
1450 map.m_may_create = true;
1453 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1454 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1455 F2FS_GET_BLOCK_PRE_AIO :
1456 F2FS_GET_BLOCK_PRE_DIO;
1459 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1460 err = f2fs_convert_inline_inode(inode);
1464 if (f2fs_has_inline_data(inode))
1467 flag = F2FS_GET_BLOCK_PRE_AIO;
1470 err = f2fs_map_blocks(inode, &map, 1, flag);
1471 if (map.m_len > 0 && err == -ENOSPC) {
1473 set_inode_flag(inode, FI_NO_PREALLOC);
1479 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1481 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1483 down_read(&sbi->node_change);
1485 up_read(&sbi->node_change);
1490 f2fs_unlock_op(sbi);
1495 * f2fs_map_blocks() tries to find or build mapping relationship which
1496 * maps continuous logical blocks to physical blocks, and return such
1497 * info via f2fs_map_blocks structure.
1499 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1500 int create, int flag)
1502 unsigned int maxblocks = map->m_len;
1503 struct dnode_of_data dn;
1504 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1505 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1506 pgoff_t pgofs, end_offset, end;
1507 int err = 0, ofs = 1;
1508 unsigned int ofs_in_node, last_ofs_in_node;
1510 struct extent_info ei = {0,0,0};
1512 unsigned int start_pgofs;
1520 /* it only supports block size == page size */
1521 pgofs = (pgoff_t)map->m_lblk;
1522 end = pgofs + maxblocks;
1524 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1525 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1529 map->m_pblk = ei.blk + pgofs - ei.fofs;
1530 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1531 map->m_flags = F2FS_MAP_MAPPED;
1532 if (map->m_next_extent)
1533 *map->m_next_extent = pgofs + map->m_len;
1535 /* for hardware encryption, but to avoid potential issue in future */
1536 if (flag == F2FS_GET_BLOCK_DIO)
1537 f2fs_wait_on_block_writeback_range(inode,
1538 map->m_pblk, map->m_len);
1543 if (map->m_may_create)
1544 f2fs_do_map_lock(sbi, flag, true);
1546 /* When reading holes, we need its node page */
1547 set_new_dnode(&dn, inode, NULL, NULL, 0);
1548 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1550 if (flag == F2FS_GET_BLOCK_BMAP)
1552 if (err == -ENOENT) {
1554 if (map->m_next_pgofs)
1555 *map->m_next_pgofs =
1556 f2fs_get_next_page_offset(&dn, pgofs);
1557 if (map->m_next_extent)
1558 *map->m_next_extent =
1559 f2fs_get_next_page_offset(&dn, pgofs);
1564 start_pgofs = pgofs;
1566 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1567 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1570 blkaddr = f2fs_data_blkaddr(&dn);
1572 if (__is_valid_data_blkaddr(blkaddr) &&
1573 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1574 err = -EFSCORRUPTED;
1578 if (__is_valid_data_blkaddr(blkaddr)) {
1579 /* use out-place-update for driect IO under LFS mode */
1580 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1581 map->m_may_create) {
1582 err = __allocate_data_block(&dn, map->m_seg_type);
1585 blkaddr = dn.data_blkaddr;
1586 set_inode_flag(inode, FI_APPEND_WRITE);
1590 if (unlikely(f2fs_cp_error(sbi))) {
1594 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1595 if (blkaddr == NULL_ADDR) {
1597 last_ofs_in_node = dn.ofs_in_node;
1600 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1601 flag != F2FS_GET_BLOCK_DIO);
1602 err = __allocate_data_block(&dn,
1605 set_inode_flag(inode, FI_APPEND_WRITE);
1609 map->m_flags |= F2FS_MAP_NEW;
1610 blkaddr = dn.data_blkaddr;
1612 if (flag == F2FS_GET_BLOCK_BMAP) {
1616 if (flag == F2FS_GET_BLOCK_PRECACHE)
1618 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1619 blkaddr == NULL_ADDR) {
1620 if (map->m_next_pgofs)
1621 *map->m_next_pgofs = pgofs + 1;
1624 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1625 /* for defragment case */
1626 if (map->m_next_pgofs)
1627 *map->m_next_pgofs = pgofs + 1;
1633 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1636 if (map->m_len == 0) {
1637 /* preallocated unwritten block should be mapped for fiemap. */
1638 if (blkaddr == NEW_ADDR)
1639 map->m_flags |= F2FS_MAP_UNWRITTEN;
1640 map->m_flags |= F2FS_MAP_MAPPED;
1642 map->m_pblk = blkaddr;
1644 } else if ((map->m_pblk != NEW_ADDR &&
1645 blkaddr == (map->m_pblk + ofs)) ||
1646 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1647 flag == F2FS_GET_BLOCK_PRE_DIO) {
1658 /* preallocate blocks in batch for one dnode page */
1659 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1660 (pgofs == end || dn.ofs_in_node == end_offset)) {
1662 dn.ofs_in_node = ofs_in_node;
1663 err = f2fs_reserve_new_blocks(&dn, prealloc);
1667 map->m_len += dn.ofs_in_node - ofs_in_node;
1668 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1672 dn.ofs_in_node = end_offset;
1677 else if (dn.ofs_in_node < end_offset)
1680 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1681 if (map->m_flags & F2FS_MAP_MAPPED) {
1682 unsigned int ofs = start_pgofs - map->m_lblk;
1684 f2fs_update_extent_cache_range(&dn,
1685 start_pgofs, map->m_pblk + ofs,
1690 f2fs_put_dnode(&dn);
1692 if (map->m_may_create) {
1693 f2fs_do_map_lock(sbi, flag, false);
1694 f2fs_balance_fs(sbi, dn.node_changed);
1700 /* for hardware encryption, but to avoid potential issue in future */
1701 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1702 f2fs_wait_on_block_writeback_range(inode,
1703 map->m_pblk, map->m_len);
1705 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1706 if (map->m_flags & F2FS_MAP_MAPPED) {
1707 unsigned int ofs = start_pgofs - map->m_lblk;
1709 f2fs_update_extent_cache_range(&dn,
1710 start_pgofs, map->m_pblk + ofs,
1713 if (map->m_next_extent)
1714 *map->m_next_extent = pgofs + 1;
1716 f2fs_put_dnode(&dn);
1718 if (map->m_may_create) {
1719 f2fs_do_map_lock(sbi, flag, false);
1720 f2fs_balance_fs(sbi, dn.node_changed);
1723 trace_f2fs_map_blocks(inode, map, err);
1727 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1729 struct f2fs_map_blocks map;
1733 if (pos + len > i_size_read(inode))
1736 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1737 map.m_next_pgofs = NULL;
1738 map.m_next_extent = NULL;
1739 map.m_seg_type = NO_CHECK_TYPE;
1740 map.m_may_create = false;
1741 last_lblk = F2FS_BLK_ALIGN(pos + len);
1743 while (map.m_lblk < last_lblk) {
1744 map.m_len = last_lblk - map.m_lblk;
1745 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1746 if (err || map.m_len == 0)
1748 map.m_lblk += map.m_len;
1753 static int __get_data_block(struct inode *inode, sector_t iblock,
1754 struct buffer_head *bh, int create, int flag,
1755 pgoff_t *next_pgofs, int seg_type, bool may_write)
1757 struct f2fs_map_blocks map;
1760 map.m_lblk = iblock;
1761 map.m_len = bh->b_size >> inode->i_blkbits;
1762 map.m_next_pgofs = next_pgofs;
1763 map.m_next_extent = NULL;
1764 map.m_seg_type = seg_type;
1765 map.m_may_create = may_write;
1767 err = f2fs_map_blocks(inode, &map, create, flag);
1769 map_bh(bh, inode->i_sb, map.m_pblk);
1770 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1771 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1776 static int get_data_block(struct inode *inode, sector_t iblock,
1777 struct buffer_head *bh_result, int create, int flag,
1778 pgoff_t *next_pgofs)
1780 return __get_data_block(inode, iblock, bh_result, create,
1782 NO_CHECK_TYPE, create);
1785 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1786 struct buffer_head *bh_result, int create)
1788 return __get_data_block(inode, iblock, bh_result, create,
1789 F2FS_GET_BLOCK_DIO, NULL,
1790 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1791 IS_SWAPFILE(inode) ? false : true);
1794 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1795 struct buffer_head *bh_result, int create)
1797 return __get_data_block(inode, iblock, bh_result, create,
1798 F2FS_GET_BLOCK_DIO, NULL,
1799 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1803 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1804 struct buffer_head *bh_result, int create)
1806 /* Block number less than F2FS MAX BLOCKS */
1807 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1810 return __get_data_block(inode, iblock, bh_result, create,
1811 F2FS_GET_BLOCK_BMAP, NULL,
1812 NO_CHECK_TYPE, create);
1815 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1817 return (offset >> inode->i_blkbits);
1820 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1822 return (blk << inode->i_blkbits);
1825 static int f2fs_xattr_fiemap(struct inode *inode,
1826 struct fiemap_extent_info *fieinfo)
1828 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1830 struct node_info ni;
1831 __u64 phys = 0, len;
1833 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1836 if (f2fs_has_inline_xattr(inode)) {
1839 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1840 inode->i_ino, false);
1844 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1846 f2fs_put_page(page, 1);
1850 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1851 offset = offsetof(struct f2fs_inode, i_addr) +
1852 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1853 get_inline_xattr_addrs(inode));
1856 len = inline_xattr_size(inode);
1858 f2fs_put_page(page, 1);
1860 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1863 flags |= FIEMAP_EXTENT_LAST;
1865 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1866 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1867 if (err || err == 1)
1872 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1876 err = f2fs_get_node_info(sbi, xnid, &ni);
1878 f2fs_put_page(page, 1);
1882 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1883 len = inode->i_sb->s_blocksize;
1885 f2fs_put_page(page, 1);
1887 flags = FIEMAP_EXTENT_LAST;
1891 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1892 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1895 return (err < 0 ? err : 0);
1898 static loff_t max_inode_blocks(struct inode *inode)
1900 loff_t result = ADDRS_PER_INODE(inode);
1901 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1903 /* two direct node blocks */
1904 result += (leaf_count * 2);
1906 /* two indirect node blocks */
1907 leaf_count *= NIDS_PER_BLOCK;
1908 result += (leaf_count * 2);
1910 /* one double indirect node block */
1911 leaf_count *= NIDS_PER_BLOCK;
1912 result += leaf_count;
1917 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1920 struct buffer_head map_bh;
1921 sector_t start_blk, last_blk;
1923 u64 logical = 0, phys = 0, size = 0;
1926 bool compr_cluster = false;
1927 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1929 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1930 ret = f2fs_precache_extents(inode);
1935 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1941 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1942 ret = f2fs_xattr_fiemap(inode, fieinfo);
1946 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1947 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1952 if (logical_to_blk(inode, len) == 0)
1953 len = blk_to_logical(inode, 1);
1955 start_blk = logical_to_blk(inode, start);
1956 last_blk = logical_to_blk(inode, start + len - 1);
1959 memset(&map_bh, 0, sizeof(struct buffer_head));
1960 map_bh.b_size = len;
1963 map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1965 ret = get_data_block(inode, start_blk, &map_bh, 0,
1966 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1971 if (!buffer_mapped(&map_bh)) {
1972 start_blk = next_pgofs;
1974 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1975 max_inode_blocks(inode)))
1978 flags |= FIEMAP_EXTENT_LAST;
1982 if (IS_ENCRYPTED(inode))
1983 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1985 ret = fiemap_fill_next_extent(fieinfo, logical,
1987 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1993 if (start_blk > last_blk)
1996 if (compr_cluster) {
1997 compr_cluster = false;
2000 logical = blk_to_logical(inode, start_blk - 1);
2001 phys = blk_to_logical(inode, map_bh.b_blocknr);
2002 size = blk_to_logical(inode, cluster_size);
2004 flags |= FIEMAP_EXTENT_ENCODED;
2006 start_blk += cluster_size - 1;
2008 if (start_blk > last_blk)
2014 if (map_bh.b_blocknr == COMPRESS_ADDR) {
2015 compr_cluster = true;
2020 logical = blk_to_logical(inode, start_blk);
2021 phys = blk_to_logical(inode, map_bh.b_blocknr);
2022 size = map_bh.b_size;
2024 if (buffer_unwritten(&map_bh))
2025 flags = FIEMAP_EXTENT_UNWRITTEN;
2027 start_blk += logical_to_blk(inode, size);
2031 if (fatal_signal_pending(current))
2039 inode_unlock(inode);
2043 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2045 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2046 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2047 return inode->i_sb->s_maxbytes;
2049 return i_size_read(inode);
2052 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2054 struct f2fs_map_blocks *map,
2055 struct bio **bio_ret,
2056 sector_t *last_block_in_bio,
2059 struct bio *bio = *bio_ret;
2060 const unsigned blkbits = inode->i_blkbits;
2061 const unsigned blocksize = 1 << blkbits;
2062 sector_t block_in_file;
2063 sector_t last_block;
2064 sector_t last_block_in_file;
2068 block_in_file = (sector_t)page_index(page);
2069 last_block = block_in_file + nr_pages;
2070 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
2072 if (last_block > last_block_in_file)
2073 last_block = last_block_in_file;
2075 /* just zeroing out page which is beyond EOF */
2076 if (block_in_file >= last_block)
2079 * Map blocks using the previous result first.
2081 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2082 block_in_file > map->m_lblk &&
2083 block_in_file < (map->m_lblk + map->m_len))
2087 * Then do more f2fs_map_blocks() calls until we are
2088 * done with this page.
2090 map->m_lblk = block_in_file;
2091 map->m_len = last_block - block_in_file;
2093 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2097 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2098 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2099 SetPageMappedToDisk(page);
2101 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2102 !cleancache_get_page(page))) {
2103 SetPageUptodate(page);
2107 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2108 DATA_GENERIC_ENHANCE_READ)) {
2109 ret = -EFSCORRUPTED;
2114 zero_user_segment(page, 0, PAGE_SIZE);
2115 if (f2fs_need_verity(inode, page->index) &&
2116 !fsverity_verify_page(page)) {
2120 if (!PageUptodate(page))
2121 SetPageUptodate(page);
2127 * This page will go to BIO. Do we need to send this
2130 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2131 *last_block_in_bio, block_nr) ||
2132 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2134 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2138 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2139 is_readahead ? REQ_RAHEAD : 0, page->index,
2149 * If the page is under writeback, we need to wait for
2150 * its completion to see the correct decrypted data.
2152 f2fs_wait_on_block_writeback(inode, block_nr);
2154 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2155 goto submit_and_realloc;
2157 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2158 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2159 ClearPageError(page);
2160 *last_block_in_bio = block_nr;
2164 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2173 #ifdef CONFIG_F2FS_FS_COMPRESSION
2174 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2175 unsigned nr_pages, sector_t *last_block_in_bio,
2176 bool is_readahead, bool for_write)
2178 struct dnode_of_data dn;
2179 struct inode *inode = cc->inode;
2180 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2181 struct bio *bio = *bio_ret;
2182 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2183 sector_t last_block_in_file;
2184 const unsigned blkbits = inode->i_blkbits;
2185 const unsigned blocksize = 1 << blkbits;
2186 struct decompress_io_ctx *dic = NULL;
2190 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2192 last_block_in_file = (f2fs_readpage_limit(inode) +
2193 blocksize - 1) >> blkbits;
2195 /* get rid of pages beyond EOF */
2196 for (i = 0; i < cc->cluster_size; i++) {
2197 struct page *page = cc->rpages[i];
2201 if ((sector_t)page->index >= last_block_in_file) {
2202 zero_user_segment(page, 0, PAGE_SIZE);
2203 if (!PageUptodate(page))
2204 SetPageUptodate(page);
2205 } else if (!PageUptodate(page)) {
2209 cc->rpages[i] = NULL;
2213 /* we are done since all pages are beyond EOF */
2214 if (f2fs_cluster_is_empty(cc))
2217 set_new_dnode(&dn, inode, NULL, NULL, 0);
2218 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2222 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2224 for (i = 1; i < cc->cluster_size; i++) {
2227 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2228 dn.ofs_in_node + i);
2230 if (!__is_valid_data_blkaddr(blkaddr))
2233 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2240 /* nothing to decompress */
2241 if (cc->nr_cpages == 0) {
2246 dic = f2fs_alloc_dic(cc);
2252 for (i = 0; i < dic->nr_cpages; i++) {
2253 struct page *page = dic->cpages[i];
2255 struct bio_post_read_ctx *ctx;
2257 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2258 dn.ofs_in_node + i + 1);
2260 if (bio && (!page_is_mergeable(sbi, bio,
2261 *last_block_in_bio, blkaddr) ||
2262 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2264 __submit_bio(sbi, bio, DATA);
2269 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2270 is_readahead ? REQ_RAHEAD : 0,
2271 page->index, for_write);
2275 if (refcount_sub_and_test(dic->nr_cpages - i,
2277 f2fs_decompress_end_io(dic->rpages,
2278 cc->cluster_size, true,
2282 f2fs_put_dnode(&dn);
2288 f2fs_wait_on_block_writeback(inode, blkaddr);
2290 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2291 goto submit_and_realloc;
2293 /* tag STEP_DECOMPRESS to handle IO in wq */
2294 ctx = bio->bi_private;
2295 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2296 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2298 inc_page_count(sbi, F2FS_RD_DATA);
2299 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2300 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2301 ClearPageError(page);
2302 *last_block_in_bio = blkaddr;
2305 f2fs_put_dnode(&dn);
2311 f2fs_put_dnode(&dn);
2313 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2320 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2321 * Major change was from block_size == page_size in f2fs by default.
2323 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2324 * this function ever deviates from doing just read-ahead, it should either
2325 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2328 static int f2fs_mpage_readpages(struct inode *inode,
2329 struct readahead_control *rac, struct page *page)
2331 struct bio *bio = NULL;
2332 sector_t last_block_in_bio = 0;
2333 struct f2fs_map_blocks map;
2334 #ifdef CONFIG_F2FS_FS_COMPRESSION
2335 struct compress_ctx cc = {
2337 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2338 .cluster_size = F2FS_I(inode)->i_cluster_size,
2339 .cluster_idx = NULL_CLUSTER,
2346 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2347 unsigned max_nr_pages = nr_pages;
2349 bool drop_ra = false;
2355 map.m_next_pgofs = NULL;
2356 map.m_next_extent = NULL;
2357 map.m_seg_type = NO_CHECK_TYPE;
2358 map.m_may_create = false;
2361 * Two readahead threads for same address range can cause race condition
2362 * which fragments sequential read IOs. So let's avoid each other.
2364 if (rac && readahead_count(rac)) {
2365 if (READ_ONCE(F2FS_I(inode)->ra_offset) == readahead_index(rac))
2368 WRITE_ONCE(F2FS_I(inode)->ra_offset,
2369 readahead_index(rac));
2372 for (; nr_pages; nr_pages--) {
2374 page = readahead_page(rac);
2375 prefetchw(&page->flags);
2377 f2fs_put_page(page, 1);
2382 #ifdef CONFIG_F2FS_FS_COMPRESSION
2383 if (f2fs_compressed_file(inode)) {
2384 /* there are remained comressed pages, submit them */
2385 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2386 ret = f2fs_read_multi_pages(&cc, &bio,
2389 rac != NULL, false);
2390 f2fs_destroy_compress_ctx(&cc);
2392 goto set_error_page;
2394 ret = f2fs_is_compressed_cluster(inode, page->index);
2396 goto set_error_page;
2398 goto read_single_page;
2400 ret = f2fs_init_compress_ctx(&cc);
2402 goto set_error_page;
2404 f2fs_compress_ctx_add_page(&cc, page);
2411 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2412 &bio, &last_block_in_bio, rac);
2414 #ifdef CONFIG_F2FS_FS_COMPRESSION
2418 zero_user_segment(page, 0, PAGE_SIZE);
2421 #ifdef CONFIG_F2FS_FS_COMPRESSION
2427 #ifdef CONFIG_F2FS_FS_COMPRESSION
2428 if (f2fs_compressed_file(inode)) {
2430 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2431 ret = f2fs_read_multi_pages(&cc, &bio,
2434 rac != NULL, false);
2435 f2fs_destroy_compress_ctx(&cc);
2441 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2443 if (rac && readahead_count(rac) && !drop_ra)
2444 WRITE_ONCE(F2FS_I(inode)->ra_offset, -1);
2448 static int f2fs_read_data_page(struct file *file, struct page *page)
2450 struct inode *inode = page_file_mapping(page)->host;
2453 trace_f2fs_readpage(page, DATA);
2455 if (!f2fs_is_compress_backend_ready(inode)) {
2460 /* If the file has inline data, try to read it directly */
2461 if (f2fs_has_inline_data(inode))
2462 ret = f2fs_read_inline_data(inode, page);
2464 ret = f2fs_mpage_readpages(inode, NULL, page);
2468 static void f2fs_readahead(struct readahead_control *rac)
2470 struct inode *inode = rac->mapping->host;
2472 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2474 if (!f2fs_is_compress_backend_ready(inode))
2477 /* If the file has inline data, skip readpages */
2478 if (f2fs_has_inline_data(inode))
2481 f2fs_mpage_readpages(inode, rac, NULL);
2484 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2486 struct inode *inode = fio->page->mapping->host;
2487 struct page *mpage, *page;
2488 gfp_t gfp_flags = GFP_NOFS;
2490 if (!f2fs_encrypted_file(inode))
2493 page = fio->compressed_page ? fio->compressed_page : fio->page;
2495 /* wait for GCed page writeback via META_MAPPING */
2496 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2498 if (fscrypt_inode_uses_inline_crypto(inode))
2502 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2503 PAGE_SIZE, 0, gfp_flags);
2504 if (IS_ERR(fio->encrypted_page)) {
2505 /* flush pending IOs and wait for a while in the ENOMEM case */
2506 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2507 f2fs_flush_merged_writes(fio->sbi);
2508 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2509 gfp_flags |= __GFP_NOFAIL;
2512 return PTR_ERR(fio->encrypted_page);
2515 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2517 if (PageUptodate(mpage))
2518 memcpy(page_address(mpage),
2519 page_address(fio->encrypted_page), PAGE_SIZE);
2520 f2fs_put_page(mpage, 1);
2525 static inline bool check_inplace_update_policy(struct inode *inode,
2526 struct f2fs_io_info *fio)
2528 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2529 unsigned int policy = SM_I(sbi)->ipu_policy;
2531 if (policy & (0x1 << F2FS_IPU_FORCE))
2533 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2535 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2536 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2538 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2539 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2543 * IPU for rewrite async pages
2545 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2546 fio && fio->op == REQ_OP_WRITE &&
2547 !(fio->op_flags & REQ_SYNC) &&
2548 !IS_ENCRYPTED(inode))
2551 /* this is only set during fdatasync */
2552 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2553 is_inode_flag_set(inode, FI_NEED_IPU))
2556 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2557 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2563 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2565 if (f2fs_is_pinned_file(inode))
2568 /* if this is cold file, we should overwrite to avoid fragmentation */
2569 if (file_is_cold(inode))
2572 return check_inplace_update_policy(inode, fio);
2575 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2577 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2579 if (f2fs_lfs_mode(sbi))
2581 if (S_ISDIR(inode->i_mode))
2583 if (IS_NOQUOTA(inode))
2585 if (f2fs_is_atomic_file(inode))
2588 if (is_cold_data(fio->page))
2590 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2592 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2593 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2599 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2601 struct inode *inode = fio->page->mapping->host;
2603 if (f2fs_should_update_outplace(inode, fio))
2606 return f2fs_should_update_inplace(inode, fio);
2609 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2611 struct page *page = fio->page;
2612 struct inode *inode = page->mapping->host;
2613 struct dnode_of_data dn;
2614 struct extent_info ei = {0,0,0};
2615 struct node_info ni;
2616 bool ipu_force = false;
2619 set_new_dnode(&dn, inode, NULL, NULL, 0);
2620 if (need_inplace_update(fio) &&
2621 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2622 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2624 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2625 DATA_GENERIC_ENHANCE))
2626 return -EFSCORRUPTED;
2629 fio->need_lock = LOCK_DONE;
2633 /* Deadlock due to between page->lock and f2fs_lock_op */
2634 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2637 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2641 fio->old_blkaddr = dn.data_blkaddr;
2643 /* This page is already truncated */
2644 if (fio->old_blkaddr == NULL_ADDR) {
2645 ClearPageUptodate(page);
2646 clear_cold_data(page);
2650 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2651 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2652 DATA_GENERIC_ENHANCE)) {
2653 err = -EFSCORRUPTED;
2657 * If current allocation needs SSR,
2658 * it had better in-place writes for updated data.
2661 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2662 need_inplace_update(fio))) {
2663 err = f2fs_encrypt_one_page(fio);
2667 set_page_writeback(page);
2668 ClearPageError(page);
2669 f2fs_put_dnode(&dn);
2670 if (fio->need_lock == LOCK_REQ)
2671 f2fs_unlock_op(fio->sbi);
2672 err = f2fs_inplace_write_data(fio);
2674 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2675 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2676 if (PageWriteback(page))
2677 end_page_writeback(page);
2679 set_inode_flag(inode, FI_UPDATE_WRITE);
2681 trace_f2fs_do_write_data_page(fio->page, IPU);
2685 if (fio->need_lock == LOCK_RETRY) {
2686 if (!f2fs_trylock_op(fio->sbi)) {
2690 fio->need_lock = LOCK_REQ;
2693 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2697 fio->version = ni.version;
2699 err = f2fs_encrypt_one_page(fio);
2703 set_page_writeback(page);
2704 ClearPageError(page);
2706 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2707 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2709 /* LFS mode write path */
2710 f2fs_outplace_write_data(&dn, fio);
2711 trace_f2fs_do_write_data_page(page, OPU);
2712 set_inode_flag(inode, FI_APPEND_WRITE);
2713 if (page->index == 0)
2714 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2716 f2fs_put_dnode(&dn);
2718 if (fio->need_lock == LOCK_REQ)
2719 f2fs_unlock_op(fio->sbi);
2723 int f2fs_write_single_data_page(struct page *page, int *submitted,
2725 sector_t *last_block,
2726 struct writeback_control *wbc,
2727 enum iostat_type io_type,
2730 struct inode *inode = page->mapping->host;
2731 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2732 loff_t i_size = i_size_read(inode);
2733 const pgoff_t end_index = ((unsigned long long)i_size)
2735 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2736 unsigned offset = 0;
2737 bool need_balance_fs = false;
2739 struct f2fs_io_info fio = {
2741 .ino = inode->i_ino,
2744 .op_flags = wbc_to_write_flags(wbc),
2745 .old_blkaddr = NULL_ADDR,
2747 .encrypted_page = NULL,
2749 .compr_blocks = compr_blocks,
2750 .need_lock = LOCK_RETRY,
2754 .last_block = last_block,
2757 trace_f2fs_writepage(page, DATA);
2759 /* we should bypass data pages to proceed the kworkder jobs */
2760 if (unlikely(f2fs_cp_error(sbi))) {
2761 mapping_set_error(page->mapping, -EIO);
2763 * don't drop any dirty dentry pages for keeping lastest
2764 * directory structure.
2766 if (S_ISDIR(inode->i_mode))
2771 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2774 if (page->index < end_index ||
2775 f2fs_verity_in_progress(inode) ||
2780 * If the offset is out-of-range of file size,
2781 * this page does not have to be written to disk.
2783 offset = i_size & (PAGE_SIZE - 1);
2784 if ((page->index >= end_index + 1) || !offset)
2787 zero_user_segment(page, offset, PAGE_SIZE);
2789 if (f2fs_is_drop_cache(inode))
2791 /* we should not write 0'th page having journal header */
2792 if (f2fs_is_volatile_file(inode) && (!page->index ||
2793 (!wbc->for_reclaim &&
2794 f2fs_available_free_memory(sbi, BASE_CHECK))))
2797 /* Dentry/quota blocks are controlled by checkpoint */
2798 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2800 * We need to wait for node_write to avoid block allocation during
2801 * checkpoint. This can only happen to quota writes which can cause
2802 * the below discard race condition.
2804 if (IS_NOQUOTA(inode))
2805 down_read(&sbi->node_write);
2807 fio.need_lock = LOCK_DONE;
2808 err = f2fs_do_write_data_page(&fio);
2810 if (IS_NOQUOTA(inode))
2811 up_read(&sbi->node_write);
2816 if (!wbc->for_reclaim)
2817 need_balance_fs = true;
2818 else if (has_not_enough_free_secs(sbi, 0, 0))
2821 set_inode_flag(inode, FI_HOT_DATA);
2824 if (f2fs_has_inline_data(inode)) {
2825 err = f2fs_write_inline_data(inode, page);
2830 if (err == -EAGAIN) {
2831 err = f2fs_do_write_data_page(&fio);
2832 if (err == -EAGAIN) {
2833 fio.need_lock = LOCK_REQ;
2834 err = f2fs_do_write_data_page(&fio);
2839 file_set_keep_isize(inode);
2841 spin_lock(&F2FS_I(inode)->i_size_lock);
2842 if (F2FS_I(inode)->last_disk_size < psize)
2843 F2FS_I(inode)->last_disk_size = psize;
2844 spin_unlock(&F2FS_I(inode)->i_size_lock);
2848 if (err && err != -ENOENT)
2852 inode_dec_dirty_pages(inode);
2854 ClearPageUptodate(page);
2855 clear_cold_data(page);
2858 if (wbc->for_reclaim) {
2859 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2860 clear_inode_flag(inode, FI_HOT_DATA);
2861 f2fs_remove_dirty_inode(inode);
2865 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2866 !F2FS_I(inode)->cp_task)
2867 f2fs_balance_fs(sbi, need_balance_fs);
2869 if (unlikely(f2fs_cp_error(sbi))) {
2870 f2fs_submit_merged_write(sbi, DATA);
2871 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2876 *submitted = fio.submitted ? 1 : 0;
2881 redirty_page_for_writepage(wbc, page);
2883 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2884 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2885 * file_write_and_wait_range() will see EIO error, which is critical
2886 * to return value of fsync() followed by atomic_write failure to user.
2888 if (!err || wbc->for_reclaim)
2889 return AOP_WRITEPAGE_ACTIVATE;
2894 static int f2fs_write_data_page(struct page *page,
2895 struct writeback_control *wbc)
2897 #ifdef CONFIG_F2FS_FS_COMPRESSION
2898 struct inode *inode = page->mapping->host;
2900 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2903 if (f2fs_compressed_file(inode)) {
2904 if (f2fs_is_compressed_cluster(inode, page->index)) {
2905 redirty_page_for_writepage(wbc, page);
2906 return AOP_WRITEPAGE_ACTIVATE;
2912 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2913 wbc, FS_DATA_IO, 0);
2917 * This function was copied from write_cche_pages from mm/page-writeback.c.
2918 * The major change is making write step of cold data page separately from
2919 * warm/hot data page.
2921 static int f2fs_write_cache_pages(struct address_space *mapping,
2922 struct writeback_control *wbc,
2923 enum iostat_type io_type)
2926 int done = 0, retry = 0;
2927 struct pagevec pvec;
2928 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2929 struct bio *bio = NULL;
2930 sector_t last_block;
2931 #ifdef CONFIG_F2FS_FS_COMPRESSION
2932 struct inode *inode = mapping->host;
2933 struct compress_ctx cc = {
2935 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2936 .cluster_size = F2FS_I(inode)->i_cluster_size,
2937 .cluster_idx = NULL_CLUSTER,
2943 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2949 pgoff_t end; /* Inclusive */
2951 int range_whole = 0;
2957 pagevec_init(&pvec);
2959 if (get_dirty_pages(mapping->host) <=
2960 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2961 set_inode_flag(mapping->host, FI_HOT_DATA);
2963 clear_inode_flag(mapping->host, FI_HOT_DATA);
2965 if (wbc->range_cyclic) {
2966 index = mapping->writeback_index; /* prev offset */
2969 index = wbc->range_start >> PAGE_SHIFT;
2970 end = wbc->range_end >> PAGE_SHIFT;
2971 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2974 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2975 tag = PAGECACHE_TAG_TOWRITE;
2977 tag = PAGECACHE_TAG_DIRTY;
2980 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2981 tag_pages_for_writeback(mapping, index, end);
2983 while (!done && !retry && (index <= end)) {
2984 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2989 for (i = 0; i < nr_pages; i++) {
2990 struct page *page = pvec.pages[i];
2994 #ifdef CONFIG_F2FS_FS_COMPRESSION
2995 if (f2fs_compressed_file(inode)) {
2996 ret = f2fs_init_compress_ctx(&cc);
3002 if (!f2fs_cluster_can_merge_page(&cc,
3004 ret = f2fs_write_multi_pages(&cc,
3005 &submitted, wbc, io_type);
3011 if (unlikely(f2fs_cp_error(sbi)))
3014 if (f2fs_cluster_is_empty(&cc)) {
3015 void *fsdata = NULL;
3019 ret2 = f2fs_prepare_compress_overwrite(
3021 page->index, &fsdata);
3027 !f2fs_compress_write_end(inode,
3028 fsdata, page->index,
3038 /* give a priority to WB_SYNC threads */
3039 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3040 wbc->sync_mode == WB_SYNC_NONE) {
3044 #ifdef CONFIG_F2FS_FS_COMPRESSION
3047 done_index = page->index;
3051 if (unlikely(page->mapping != mapping)) {
3057 if (!PageDirty(page)) {
3058 /* someone wrote it for us */
3059 goto continue_unlock;
3062 if (PageWriteback(page)) {
3063 if (wbc->sync_mode != WB_SYNC_NONE)
3064 f2fs_wait_on_page_writeback(page,
3067 goto continue_unlock;
3070 if (!clear_page_dirty_for_io(page))
3071 goto continue_unlock;
3073 #ifdef CONFIG_F2FS_FS_COMPRESSION
3074 if (f2fs_compressed_file(inode)) {
3076 f2fs_compress_ctx_add_page(&cc, page);
3080 ret = f2fs_write_single_data_page(page, &submitted,
3081 &bio, &last_block, wbc, io_type, 0);
3082 if (ret == AOP_WRITEPAGE_ACTIVATE)
3084 #ifdef CONFIG_F2FS_FS_COMPRESSION
3087 nwritten += submitted;
3088 wbc->nr_to_write -= submitted;
3090 if (unlikely(ret)) {
3092 * keep nr_to_write, since vfs uses this to
3093 * get # of written pages.
3095 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3098 } else if (ret == -EAGAIN) {
3100 if (wbc->sync_mode == WB_SYNC_ALL) {
3102 congestion_wait(BLK_RW_ASYNC,
3103 DEFAULT_IO_TIMEOUT);
3108 done_index = page->index + 1;
3113 if (wbc->nr_to_write <= 0 &&
3114 wbc->sync_mode == WB_SYNC_NONE) {
3122 pagevec_release(&pvec);
3125 #ifdef CONFIG_F2FS_FS_COMPRESSION
3126 /* flush remained pages in compress cluster */
3127 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3128 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3129 nwritten += submitted;
3130 wbc->nr_to_write -= submitted;
3142 if (wbc->range_cyclic && !done)
3144 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3145 mapping->writeback_index = done_index;
3148 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3150 /* submit cached bio of IPU write */
3152 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3157 static inline bool __should_serialize_io(struct inode *inode,
3158 struct writeback_control *wbc)
3160 /* to avoid deadlock in path of data flush */
3161 if (F2FS_I(inode)->cp_task)
3164 if (!S_ISREG(inode->i_mode))
3166 if (IS_NOQUOTA(inode))
3169 if (f2fs_compressed_file(inode))
3171 if (wbc->sync_mode != WB_SYNC_ALL)
3173 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3178 static int __f2fs_write_data_pages(struct address_space *mapping,
3179 struct writeback_control *wbc,
3180 enum iostat_type io_type)
3182 struct inode *inode = mapping->host;
3183 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3184 struct blk_plug plug;
3186 bool locked = false;
3188 /* deal with chardevs and other special file */
3189 if (!mapping->a_ops->writepage)
3192 /* skip writing if there is no dirty page in this inode */
3193 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3196 /* during POR, we don't need to trigger writepage at all. */
3197 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3200 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3201 wbc->sync_mode == WB_SYNC_NONE &&
3202 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3203 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3206 /* skip writing during file defragment */
3207 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3210 trace_f2fs_writepages(mapping->host, wbc, DATA);
3212 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3213 if (wbc->sync_mode == WB_SYNC_ALL)
3214 atomic_inc(&sbi->wb_sync_req[DATA]);
3215 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3218 if (__should_serialize_io(inode, wbc)) {
3219 mutex_lock(&sbi->writepages);
3223 blk_start_plug(&plug);
3224 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3225 blk_finish_plug(&plug);
3228 mutex_unlock(&sbi->writepages);
3230 if (wbc->sync_mode == WB_SYNC_ALL)
3231 atomic_dec(&sbi->wb_sync_req[DATA]);
3233 * if some pages were truncated, we cannot guarantee its mapping->host
3234 * to detect pending bios.
3237 f2fs_remove_dirty_inode(inode);
3241 wbc->pages_skipped += get_dirty_pages(inode);
3242 trace_f2fs_writepages(mapping->host, wbc, DATA);
3246 static int f2fs_write_data_pages(struct address_space *mapping,
3247 struct writeback_control *wbc)
3249 struct inode *inode = mapping->host;
3251 return __f2fs_write_data_pages(mapping, wbc,
3252 F2FS_I(inode)->cp_task == current ?
3253 FS_CP_DATA_IO : FS_DATA_IO);
3256 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3258 struct inode *inode = mapping->host;
3259 loff_t i_size = i_size_read(inode);
3261 if (IS_NOQUOTA(inode))
3264 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3265 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3266 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3267 down_write(&F2FS_I(inode)->i_mmap_sem);
3269 truncate_pagecache(inode, i_size);
3270 f2fs_truncate_blocks(inode, i_size, true);
3272 up_write(&F2FS_I(inode)->i_mmap_sem);
3273 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3277 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3278 struct page *page, loff_t pos, unsigned len,
3279 block_t *blk_addr, bool *node_changed)
3281 struct inode *inode = page->mapping->host;
3282 pgoff_t index = page->index;
3283 struct dnode_of_data dn;
3285 bool locked = false;
3286 struct extent_info ei = {0,0,0};
3291 * we already allocated all the blocks, so we don't need to get
3292 * the block addresses when there is no need to fill the page.
3294 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3295 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3296 !f2fs_verity_in_progress(inode))
3299 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3300 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3301 flag = F2FS_GET_BLOCK_DEFAULT;
3303 flag = F2FS_GET_BLOCK_PRE_AIO;
3305 if (f2fs_has_inline_data(inode) ||
3306 (pos & PAGE_MASK) >= i_size_read(inode)) {
3307 f2fs_do_map_lock(sbi, flag, true);
3312 /* check inline_data */
3313 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3314 if (IS_ERR(ipage)) {
3315 err = PTR_ERR(ipage);
3319 set_new_dnode(&dn, inode, ipage, ipage, 0);
3321 if (f2fs_has_inline_data(inode)) {
3322 if (pos + len <= MAX_INLINE_DATA(inode)) {
3323 f2fs_do_read_inline_data(page, ipage);
3324 set_inode_flag(inode, FI_DATA_EXIST);
3326 set_inline_node(ipage);
3328 err = f2fs_convert_inline_page(&dn, page);
3331 if (dn.data_blkaddr == NULL_ADDR)
3332 err = f2fs_get_block(&dn, index);
3334 } else if (locked) {
3335 err = f2fs_get_block(&dn, index);
3337 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3338 dn.data_blkaddr = ei.blk + index - ei.fofs;
3341 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3342 if (err || dn.data_blkaddr == NULL_ADDR) {
3343 f2fs_put_dnode(&dn);
3344 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3346 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3353 /* convert_inline_page can make node_changed */
3354 *blk_addr = dn.data_blkaddr;
3355 *node_changed = dn.node_changed;
3357 f2fs_put_dnode(&dn);
3360 f2fs_do_map_lock(sbi, flag, false);
3364 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3365 loff_t pos, unsigned len, unsigned flags,
3366 struct page **pagep, void **fsdata)
3368 struct inode *inode = mapping->host;
3369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3370 struct page *page = NULL;
3371 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3372 bool need_balance = false, drop_atomic = false;
3373 block_t blkaddr = NULL_ADDR;
3376 trace_f2fs_write_begin(inode, pos, len, flags);
3378 if (!f2fs_is_checkpoint_ready(sbi)) {
3383 if ((f2fs_is_atomic_file(inode) &&
3384 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3385 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3392 * We should check this at this moment to avoid deadlock on inode page
3393 * and #0 page. The locking rule for inline_data conversion should be:
3394 * lock_page(page #0) -> lock_page(inode_page)
3397 err = f2fs_convert_inline_inode(inode);
3402 #ifdef CONFIG_F2FS_FS_COMPRESSION
3403 if (f2fs_compressed_file(inode)) {
3408 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3421 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3422 * wait_for_stable_page. Will wait that below with our IO control.
3424 page = f2fs_pagecache_get_page(mapping, index,
3425 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3431 /* TODO: cluster can be compressed due to race with .writepage */
3435 err = prepare_write_begin(sbi, page, pos, len,
3436 &blkaddr, &need_balance);
3440 if (need_balance && !IS_NOQUOTA(inode) &&
3441 has_not_enough_free_secs(sbi, 0, 0)) {
3443 f2fs_balance_fs(sbi, true);
3445 if (page->mapping != mapping) {
3446 /* The page got truncated from under us */
3447 f2fs_put_page(page, 1);
3452 f2fs_wait_on_page_writeback(page, DATA, false, true);
3454 if (len == PAGE_SIZE || PageUptodate(page))
3457 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3458 !f2fs_verity_in_progress(inode)) {
3459 zero_user_segment(page, len, PAGE_SIZE);
3463 if (blkaddr == NEW_ADDR) {
3464 zero_user_segment(page, 0, PAGE_SIZE);
3465 SetPageUptodate(page);
3467 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3468 DATA_GENERIC_ENHANCE_READ)) {
3469 err = -EFSCORRUPTED;
3472 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3477 if (unlikely(page->mapping != mapping)) {
3478 f2fs_put_page(page, 1);
3481 if (unlikely(!PageUptodate(page))) {
3489 f2fs_put_page(page, 1);
3490 f2fs_write_failed(mapping, pos + len);
3492 f2fs_drop_inmem_pages_all(sbi, false);
3496 static int f2fs_write_end(struct file *file,
3497 struct address_space *mapping,
3498 loff_t pos, unsigned len, unsigned copied,
3499 struct page *page, void *fsdata)
3501 struct inode *inode = page->mapping->host;
3503 trace_f2fs_write_end(inode, pos, len, copied);
3506 * This should be come from len == PAGE_SIZE, and we expect copied
3507 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3508 * let generic_perform_write() try to copy data again through copied=0.
3510 if (!PageUptodate(page)) {
3511 if (unlikely(copied != len))
3514 SetPageUptodate(page);
3517 #ifdef CONFIG_F2FS_FS_COMPRESSION
3518 /* overwrite compressed file */
3519 if (f2fs_compressed_file(inode) && fsdata) {
3520 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3521 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3523 if (pos + copied > i_size_read(inode) &&
3524 !f2fs_verity_in_progress(inode))
3525 f2fs_i_size_write(inode, pos + copied);
3533 set_page_dirty(page);
3535 if (pos + copied > i_size_read(inode) &&
3536 !f2fs_verity_in_progress(inode))
3537 f2fs_i_size_write(inode, pos + copied);
3539 f2fs_put_page(page, 1);
3540 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3544 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3547 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3548 unsigned blkbits = i_blkbits;
3549 unsigned blocksize_mask = (1 << blkbits) - 1;
3550 unsigned long align = offset | iov_iter_alignment(iter);
3551 struct block_device *bdev = inode->i_sb->s_bdev;
3553 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3556 if (align & blocksize_mask) {
3558 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3559 blocksize_mask = (1 << blkbits) - 1;
3560 if (align & blocksize_mask)
3567 static void f2fs_dio_end_io(struct bio *bio)
3569 struct f2fs_private_dio *dio = bio->bi_private;
3571 dec_page_count(F2FS_I_SB(dio->inode),
3572 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3574 bio->bi_private = dio->orig_private;
3575 bio->bi_end_io = dio->orig_end_io;
3582 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3585 struct f2fs_private_dio *dio;
3586 bool write = (bio_op(bio) == REQ_OP_WRITE);
3588 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3589 sizeof(struct f2fs_private_dio), GFP_NOFS);
3594 dio->orig_end_io = bio->bi_end_io;
3595 dio->orig_private = bio->bi_private;
3598 bio->bi_end_io = f2fs_dio_end_io;
3599 bio->bi_private = dio;
3601 inc_page_count(F2FS_I_SB(inode),
3602 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3607 bio->bi_status = BLK_STS_IOERR;
3611 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3613 struct address_space *mapping = iocb->ki_filp->f_mapping;
3614 struct inode *inode = mapping->host;
3615 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3616 struct f2fs_inode_info *fi = F2FS_I(inode);
3617 size_t count = iov_iter_count(iter);
3618 loff_t offset = iocb->ki_pos;
3619 int rw = iov_iter_rw(iter);
3621 enum rw_hint hint = iocb->ki_hint;
3622 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3625 err = check_direct_IO(inode, iter, offset);
3627 return err < 0 ? err : 0;
3629 if (f2fs_force_buffered_io(inode, iocb, iter))
3632 do_opu = allow_outplace_dio(inode, iocb, iter);
3634 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3636 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3637 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3639 if (iocb->ki_flags & IOCB_NOWAIT) {
3640 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3641 iocb->ki_hint = hint;
3645 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3646 up_read(&fi->i_gc_rwsem[rw]);
3647 iocb->ki_hint = hint;
3652 down_read(&fi->i_gc_rwsem[rw]);
3654 down_read(&fi->i_gc_rwsem[READ]);
3657 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3658 iter, rw == WRITE ? get_data_block_dio_write :
3659 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3660 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3664 up_read(&fi->i_gc_rwsem[READ]);
3666 up_read(&fi->i_gc_rwsem[rw]);
3669 if (whint_mode == WHINT_MODE_OFF)
3670 iocb->ki_hint = hint;
3672 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3675 set_inode_flag(inode, FI_UPDATE_WRITE);
3676 } else if (err < 0) {
3677 f2fs_write_failed(mapping, offset + count);
3681 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3685 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3690 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3691 unsigned int length)
3693 struct inode *inode = page->mapping->host;
3694 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3696 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3697 (offset % PAGE_SIZE || length != PAGE_SIZE))
3700 if (PageDirty(page)) {
3701 if (inode->i_ino == F2FS_META_INO(sbi)) {
3702 dec_page_count(sbi, F2FS_DIRTY_META);
3703 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3704 dec_page_count(sbi, F2FS_DIRTY_NODES);
3706 inode_dec_dirty_pages(inode);
3707 f2fs_remove_dirty_inode(inode);
3711 clear_cold_data(page);
3713 if (IS_ATOMIC_WRITTEN_PAGE(page))
3714 return f2fs_drop_inmem_page(inode, page);
3716 f2fs_clear_page_private(page);
3719 int f2fs_release_page(struct page *page, gfp_t wait)
3721 /* If this is dirty page, keep PagePrivate */
3722 if (PageDirty(page))
3725 /* This is atomic written page, keep Private */
3726 if (IS_ATOMIC_WRITTEN_PAGE(page))
3729 clear_cold_data(page);
3730 f2fs_clear_page_private(page);
3734 static int f2fs_set_data_page_dirty(struct page *page)
3736 struct inode *inode = page_file_mapping(page)->host;
3738 trace_f2fs_set_page_dirty(page, DATA);
3740 if (!PageUptodate(page))
3741 SetPageUptodate(page);
3742 if (PageSwapCache(page))
3743 return __set_page_dirty_nobuffers(page);
3745 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3746 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3747 f2fs_register_inmem_page(inode, page);
3751 * Previously, this page has been registered, we just
3757 if (!PageDirty(page)) {
3758 __set_page_dirty_nobuffers(page);
3759 f2fs_update_dirty_page(inode, page);
3766 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3768 #ifdef CONFIG_F2FS_FS_COMPRESSION
3769 struct dnode_of_data dn;
3770 sector_t start_idx, blknr = 0;
3773 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3775 set_new_dnode(&dn, inode, NULL, NULL, 0);
3776 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3780 if (dn.data_blkaddr != COMPRESS_ADDR) {
3781 dn.ofs_in_node += block - start_idx;
3782 blknr = f2fs_data_blkaddr(&dn);
3783 if (!__is_valid_data_blkaddr(blknr))
3787 f2fs_put_dnode(&dn);
3795 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3797 struct inode *inode = mapping->host;
3798 struct buffer_head tmp = {
3799 .b_size = i_blocksize(inode),
3803 if (f2fs_has_inline_data(inode))
3806 /* make sure allocating whole blocks */
3807 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3808 filemap_write_and_wait(mapping);
3810 if (f2fs_compressed_file(inode))
3811 blknr = f2fs_bmap_compress(inode, block);
3813 if (!get_data_block_bmap(inode, block, &tmp, 0))
3814 blknr = tmp.b_blocknr;
3816 trace_f2fs_bmap(inode, block, blknr);
3820 #ifdef CONFIG_MIGRATION
3821 #include <linux/migrate.h>
3823 int f2fs_migrate_page(struct address_space *mapping,
3824 struct page *newpage, struct page *page, enum migrate_mode mode)
3826 int rc, extra_count;
3827 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3828 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3830 BUG_ON(PageWriteback(page));
3832 /* migrating an atomic written page is safe with the inmem_lock hold */
3833 if (atomic_written) {
3834 if (mode != MIGRATE_SYNC)
3836 if (!mutex_trylock(&fi->inmem_lock))
3840 /* one extra reference was held for atomic_write page */
3841 extra_count = atomic_written ? 1 : 0;
3842 rc = migrate_page_move_mapping(mapping, newpage,
3844 if (rc != MIGRATEPAGE_SUCCESS) {
3846 mutex_unlock(&fi->inmem_lock);
3850 if (atomic_written) {
3851 struct inmem_pages *cur;
3852 list_for_each_entry(cur, &fi->inmem_pages, list)
3853 if (cur->page == page) {
3854 cur->page = newpage;
3857 mutex_unlock(&fi->inmem_lock);
3862 if (PagePrivate(page)) {
3863 f2fs_set_page_private(newpage, page_private(page));
3864 f2fs_clear_page_private(page);
3867 if (mode != MIGRATE_SYNC_NO_COPY)
3868 migrate_page_copy(newpage, page);
3870 migrate_page_states(newpage, page);
3872 return MIGRATEPAGE_SUCCESS;
3877 /* Copied from generic_swapfile_activate() to check any holes */
3878 static int check_swap_activate(struct swap_info_struct *sis,
3879 struct file *swap_file, sector_t *span)
3881 struct address_space *mapping = swap_file->f_mapping;
3882 struct inode *inode = mapping->host;
3883 unsigned blocks_per_page;
3884 unsigned long page_no;
3886 sector_t probe_block;
3887 sector_t last_block;
3888 sector_t lowest_block = -1;
3889 sector_t highest_block = 0;
3893 blkbits = inode->i_blkbits;
3894 blocks_per_page = PAGE_SIZE >> blkbits;
3897 * Map all the blocks into the extent list. This code doesn't try
3902 last_block = i_size_read(inode) >> blkbits;
3903 while ((probe_block + blocks_per_page) <= last_block &&
3904 page_no < sis->max) {
3905 unsigned block_in_page;
3906 sector_t first_block;
3912 block = probe_block;
3913 err = bmap(inode, &block);
3916 first_block = block;
3919 * It must be PAGE_SIZE aligned on-disk
3921 if (first_block & (blocks_per_page - 1)) {
3926 for (block_in_page = 1; block_in_page < blocks_per_page;
3929 block = probe_block + block_in_page;
3930 err = bmap(inode, &block);
3935 if (block != first_block + block_in_page) {
3942 first_block >>= (PAGE_SHIFT - blkbits);
3943 if (page_no) { /* exclude the header page */
3944 if (first_block < lowest_block)
3945 lowest_block = first_block;
3946 if (first_block > highest_block)
3947 highest_block = first_block;
3951 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3953 ret = add_swap_extent(sis, page_no, 1, first_block);
3958 probe_block += blocks_per_page;
3963 *span = 1 + highest_block - lowest_block;
3965 page_no = 1; /* force Empty message */
3967 sis->pages = page_no - 1;
3968 sis->highest_bit = page_no - 1;
3972 pr_err("swapon: swapfile has holes\n");
3976 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3979 struct inode *inode = file_inode(file);
3982 if (!S_ISREG(inode->i_mode))
3985 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3988 ret = f2fs_convert_inline_inode(inode);
3992 if (f2fs_disable_compressed_file(inode))
3995 ret = check_swap_activate(sis, file, span);
3999 set_inode_flag(inode, FI_PIN_FILE);
4000 f2fs_precache_extents(inode);
4001 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4005 static void f2fs_swap_deactivate(struct file *file)
4007 struct inode *inode = file_inode(file);
4009 clear_inode_flag(inode, FI_PIN_FILE);
4012 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4018 static void f2fs_swap_deactivate(struct file *file)
4023 const struct address_space_operations f2fs_dblock_aops = {
4024 .readpage = f2fs_read_data_page,
4025 .readahead = f2fs_readahead,
4026 .writepage = f2fs_write_data_page,
4027 .writepages = f2fs_write_data_pages,
4028 .write_begin = f2fs_write_begin,
4029 .write_end = f2fs_write_end,
4030 .set_page_dirty = f2fs_set_data_page_dirty,
4031 .invalidatepage = f2fs_invalidate_page,
4032 .releasepage = f2fs_release_page,
4033 .direct_IO = f2fs_direct_IO,
4035 .swap_activate = f2fs_swap_activate,
4036 .swap_deactivate = f2fs_swap_deactivate,
4037 #ifdef CONFIG_MIGRATION
4038 .migratepage = f2fs_migrate_page,
4042 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4044 struct address_space *mapping = page_mapping(page);
4045 unsigned long flags;
4047 xa_lock_irqsave(&mapping->i_pages, flags);
4048 __xa_clear_mark(&mapping->i_pages, page_index(page),
4049 PAGECACHE_TAG_DIRTY);
4050 xa_unlock_irqrestore(&mapping->i_pages, flags);
4053 int __init f2fs_init_post_read_processing(void)
4055 bio_post_read_ctx_cache =
4056 kmem_cache_create("f2fs_bio_post_read_ctx",
4057 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4058 if (!bio_post_read_ctx_cache)
4060 bio_post_read_ctx_pool =
4061 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4062 bio_post_read_ctx_cache);
4063 if (!bio_post_read_ctx_pool)
4064 goto fail_free_cache;
4068 kmem_cache_destroy(bio_post_read_ctx_cache);
4073 void f2fs_destroy_post_read_processing(void)
4075 mempool_destroy(bio_post_read_ctx_pool);
4076 kmem_cache_destroy(bio_post_read_ctx_cache);
4079 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4081 if (!f2fs_sb_has_encrypt(sbi) &&
4082 !f2fs_sb_has_verity(sbi) &&
4083 !f2fs_sb_has_compression(sbi))
4086 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4087 WQ_UNBOUND | WQ_HIGHPRI,
4089 if (!sbi->post_read_wq)
4094 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4096 if (sbi->post_read_wq)
4097 destroy_workqueue(sbi->post_read_wq);
4100 int __init f2fs_init_bio_entry_cache(void)
4102 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4103 sizeof(struct bio_entry));
4104 if (!bio_entry_slab)
4109 void f2fs_destroy_bio_entry_cache(void)
4111 kmem_cache_destroy(bio_entry_slab);