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 bool __is_cp_guaranteed(struct page *page)
55 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi;
62 if (f2fs_is_compressed_page(page))
65 inode = mapping->host;
66 sbi = F2FS_I_SB(inode);
68 if (inode->i_ino == F2FS_META_INO(sbi) ||
69 inode->i_ino == F2FS_NODE_INO(sbi) ||
70 S_ISDIR(inode->i_mode) ||
71 (S_ISREG(inode->i_mode) &&
72 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
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 {
98 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
99 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
103 struct bio_post_read_ctx {
105 struct f2fs_sb_info *sbi;
106 struct work_struct work;
107 unsigned int enabled_steps;
110 static void __read_end_io(struct bio *bio, bool compr, bool verity)
114 struct bvec_iter_all iter_all;
116 bio_for_each_segment_all(bv, bio, iter_all) {
119 #ifdef CONFIG_F2FS_FS_COMPRESSION
120 if (compr && f2fs_is_compressed_page(page)) {
121 f2fs_decompress_pages(bio, page, verity);
128 /* PG_error was set if any post_read step failed */
129 if (bio->bi_status || PageError(page)) {
130 ClearPageUptodate(page);
131 /* will re-read again later */
132 ClearPageError(page);
134 SetPageUptodate(page);
136 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
141 static void f2fs_release_read_bio(struct bio *bio);
142 static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
145 __read_end_io(bio, false, verity);
146 f2fs_release_read_bio(bio);
149 static void f2fs_decompress_bio(struct bio *bio, bool verity)
151 __read_end_io(bio, true, verity);
154 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
156 static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
158 fscrypt_decrypt_bio(ctx->bio);
161 static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
163 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
166 #ifdef CONFIG_F2FS_FS_COMPRESSION
167 static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
169 f2fs_decompress_end_io(rpages, cluster_size, false, true);
172 static void f2fs_verify_bio(struct bio *bio)
175 struct bvec_iter_all iter_all;
177 bio_for_each_segment_all(bv, bio, iter_all) {
178 struct page *page = bv->bv_page;
179 struct decompress_io_ctx *dic;
181 dic = (struct decompress_io_ctx *)page_private(page);
184 if (atomic_dec_return(&dic->verity_pages))
186 f2fs_verify_pages(dic->rpages,
192 if (bio->bi_status || PageError(page))
195 if (fsverity_verify_page(page)) {
196 SetPageUptodate(page);
200 ClearPageUptodate(page);
201 ClearPageError(page);
203 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
209 static void f2fs_verity_work(struct work_struct *work)
211 struct bio_post_read_ctx *ctx =
212 container_of(work, struct bio_post_read_ctx, work);
213 struct bio *bio = ctx->bio;
214 #ifdef CONFIG_F2FS_FS_COMPRESSION
215 unsigned int enabled_steps = ctx->enabled_steps;
219 * fsverity_verify_bio() may call readpages() again, and while verity
220 * will be disabled for this, decryption may still be needed, resulting
221 * in another bio_post_read_ctx being allocated. So to prevent
222 * deadlocks we need to release the current ctx to the mempool first.
223 * This assumes that verity is the last post-read step.
225 mempool_free(ctx, bio_post_read_ctx_pool);
226 bio->bi_private = NULL;
228 #ifdef CONFIG_F2FS_FS_COMPRESSION
229 /* previous step is decompression */
230 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
231 f2fs_verify_bio(bio);
232 f2fs_release_read_bio(bio);
237 fsverity_verify_bio(bio);
238 __f2fs_read_end_io(bio, false, false);
241 static void f2fs_post_read_work(struct work_struct *work)
243 struct bio_post_read_ctx *ctx =
244 container_of(work, struct bio_post_read_ctx, work);
246 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
247 f2fs_decrypt_work(ctx);
249 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
250 f2fs_decompress_work(ctx);
252 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
253 INIT_WORK(&ctx->work, f2fs_verity_work);
254 fsverity_enqueue_verify_work(&ctx->work);
258 __f2fs_read_end_io(ctx->bio,
259 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
262 static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
263 struct work_struct *work)
265 queue_work(sbi->post_read_wq, work);
268 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
271 * We use different work queues for decryption and for verity because
272 * verity may require reading metadata pages that need decryption, and
273 * we shouldn't recurse to the same workqueue.
276 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
277 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
278 INIT_WORK(&ctx->work, f2fs_post_read_work);
279 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
283 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
284 INIT_WORK(&ctx->work, f2fs_verity_work);
285 fsverity_enqueue_verify_work(&ctx->work);
289 __f2fs_read_end_io(ctx->bio, false, false);
292 static bool f2fs_bio_post_read_required(struct bio *bio)
294 return bio->bi_private;
297 static void f2fs_read_end_io(struct bio *bio)
299 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
301 if (time_to_inject(sbi, FAULT_READ_IO)) {
302 f2fs_show_injection_info(sbi, FAULT_READ_IO);
303 bio->bi_status = BLK_STS_IOERR;
306 if (f2fs_bio_post_read_required(bio)) {
307 struct bio_post_read_ctx *ctx = bio->bi_private;
309 bio_post_read_processing(ctx);
313 __f2fs_read_end_io(bio, false, false);
316 static void f2fs_write_end_io(struct bio *bio)
318 struct f2fs_sb_info *sbi = bio->bi_private;
319 struct bio_vec *bvec;
320 struct bvec_iter_all iter_all;
322 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
323 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
324 bio->bi_status = BLK_STS_IOERR;
327 bio_for_each_segment_all(bvec, bio, iter_all) {
328 struct page *page = bvec->bv_page;
329 enum count_type type = WB_DATA_TYPE(page);
331 if (IS_DUMMY_WRITTEN_PAGE(page)) {
332 set_page_private(page, (unsigned long)NULL);
333 ClearPagePrivate(page);
335 mempool_free(page, sbi->write_io_dummy);
337 if (unlikely(bio->bi_status))
338 f2fs_stop_checkpoint(sbi, true);
342 fscrypt_finalize_bounce_page(&page);
344 #ifdef CONFIG_F2FS_FS_COMPRESSION
345 if (f2fs_is_compressed_page(page)) {
346 f2fs_compress_write_end_io(bio, page);
351 if (unlikely(bio->bi_status)) {
352 mapping_set_error(page->mapping, -EIO);
353 if (type == F2FS_WB_CP_DATA)
354 f2fs_stop_checkpoint(sbi, true);
357 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
358 page->index != nid_of_node(page));
360 dec_page_count(sbi, type);
361 if (f2fs_in_warm_node_list(sbi, page))
362 f2fs_del_fsync_node_entry(sbi, page);
363 clear_cold_data(page);
364 end_page_writeback(page);
366 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
367 wq_has_sleeper(&sbi->cp_wait))
368 wake_up(&sbi->cp_wait);
373 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
374 block_t blk_addr, struct bio *bio)
376 struct block_device *bdev = sbi->sb->s_bdev;
379 if (f2fs_is_multi_device(sbi)) {
380 for (i = 0; i < sbi->s_ndevs; i++) {
381 if (FDEV(i).start_blk <= blk_addr &&
382 FDEV(i).end_blk >= blk_addr) {
383 blk_addr -= FDEV(i).start_blk;
390 bio_set_dev(bio, bdev);
391 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
396 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
400 if (!f2fs_is_multi_device(sbi))
403 for (i = 0; i < sbi->s_ndevs; i++)
404 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
409 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
411 struct f2fs_sb_info *sbi = fio->sbi;
414 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
416 f2fs_target_device(sbi, fio->new_blkaddr, bio);
417 if (is_read_io(fio->op)) {
418 bio->bi_end_io = f2fs_read_end_io;
419 bio->bi_private = NULL;
421 bio->bi_end_io = f2fs_write_end_io;
422 bio->bi_private = sbi;
423 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
424 fio->type, fio->temp);
427 wbc_init_bio(fio->io_wbc, bio);
432 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
434 const struct f2fs_io_info *fio,
438 * The f2fs garbage collector sets ->encrypted_page when it wants to
439 * read/write raw data without encryption.
441 if (!fio || !fio->encrypted_page)
442 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
445 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
447 const struct f2fs_io_info *fio)
450 * The f2fs garbage collector sets ->encrypted_page when it wants to
451 * read/write raw data without encryption.
453 if (fio && fio->encrypted_page)
454 return !bio_has_crypt_ctx(bio);
456 return fscrypt_mergeable_bio(bio, inode, next_idx);
459 static inline void __submit_bio(struct f2fs_sb_info *sbi,
460 struct bio *bio, enum page_type type)
462 if (!is_read_io(bio_op(bio))) {
465 if (type != DATA && type != NODE)
468 if (f2fs_lfs_mode(sbi) && current->plug)
469 blk_finish_plug(current->plug);
471 if (F2FS_IO_ALIGNED(sbi))
474 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
475 start %= F2FS_IO_SIZE(sbi);
480 /* fill dummy pages */
481 for (; start < F2FS_IO_SIZE(sbi); start++) {
483 mempool_alloc(sbi->write_io_dummy,
484 GFP_NOIO | __GFP_NOFAIL);
485 f2fs_bug_on(sbi, !page);
487 zero_user_segment(page, 0, PAGE_SIZE);
488 SetPagePrivate(page);
489 set_page_private(page, DUMMY_WRITTEN_PAGE);
491 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
495 * In the NODE case, we lose next block address chain. So, we
496 * need to do checkpoint in f2fs_sync_file.
499 set_sbi_flag(sbi, SBI_NEED_CP);
502 if (is_read_io(bio_op(bio)))
503 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
505 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
509 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
510 struct bio *bio, enum page_type type)
512 __submit_bio(sbi, bio, type);
515 static void __attach_io_flag(struct f2fs_io_info *fio)
517 struct f2fs_sb_info *sbi = fio->sbi;
518 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
519 unsigned int io_flag, fua_flag, meta_flag;
521 if (fio->type == DATA)
522 io_flag = sbi->data_io_flag;
523 else if (fio->type == NODE)
524 io_flag = sbi->node_io_flag;
528 fua_flag = io_flag & temp_mask;
529 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
532 * data/node io flag bits per temp:
533 * REQ_META | REQ_FUA |
534 * 5 | 4 | 3 | 2 | 1 | 0 |
535 * Cold | Warm | Hot | Cold | Warm | Hot |
537 if ((1 << fio->temp) & meta_flag)
538 fio->op_flags |= REQ_META;
539 if ((1 << fio->temp) & fua_flag)
540 fio->op_flags |= REQ_FUA;
543 static void __submit_merged_bio(struct f2fs_bio_info *io)
545 struct f2fs_io_info *fio = &io->fio;
550 __attach_io_flag(fio);
551 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
553 if (is_read_io(fio->op))
554 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
556 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
558 __submit_bio(io->sbi, io->bio, fio->type);
562 static bool __has_merged_page(struct bio *bio, struct inode *inode,
563 struct page *page, nid_t ino)
565 struct bio_vec *bvec;
566 struct bvec_iter_all iter_all;
571 if (!inode && !page && !ino)
574 bio_for_each_segment_all(bvec, bio, iter_all) {
575 struct page *target = bvec->bv_page;
577 if (fscrypt_is_bounce_page(target)) {
578 target = fscrypt_pagecache_page(target);
582 if (f2fs_is_compressed_page(target)) {
583 target = f2fs_compress_control_page(target);
588 if (inode && inode == target->mapping->host)
590 if (page && page == target)
592 if (ino && ino == ino_of_node(target))
599 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
600 enum page_type type, enum temp_type temp)
602 enum page_type btype = PAGE_TYPE_OF_BIO(type);
603 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
605 down_write(&io->io_rwsem);
607 /* change META to META_FLUSH in the checkpoint procedure */
608 if (type >= META_FLUSH) {
609 io->fio.type = META_FLUSH;
610 io->fio.op = REQ_OP_WRITE;
611 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
612 if (!test_opt(sbi, NOBARRIER))
613 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
615 __submit_merged_bio(io);
616 up_write(&io->io_rwsem);
619 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
620 struct inode *inode, struct page *page,
621 nid_t ino, enum page_type type, bool force)
626 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
628 enum page_type btype = PAGE_TYPE_OF_BIO(type);
629 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
631 down_read(&io->io_rwsem);
632 ret = __has_merged_page(io->bio, inode, page, ino);
633 up_read(&io->io_rwsem);
636 __f2fs_submit_merged_write(sbi, type, temp);
638 /* TODO: use HOT temp only for meta pages now. */
644 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
646 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
649 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
650 struct inode *inode, struct page *page,
651 nid_t ino, enum page_type type)
653 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
656 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
658 f2fs_submit_merged_write(sbi, DATA);
659 f2fs_submit_merged_write(sbi, NODE);
660 f2fs_submit_merged_write(sbi, META);
664 * Fill the locked page with data located in the block address.
665 * A caller needs to unlock the page on failure.
667 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
670 struct page *page = fio->encrypted_page ?
671 fio->encrypted_page : fio->page;
673 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
674 fio->is_por ? META_POR : (__is_meta_io(fio) ?
675 META_GENERIC : DATA_GENERIC_ENHANCE)))
676 return -EFSCORRUPTED;
678 trace_f2fs_submit_page_bio(page, fio);
679 f2fs_trace_ios(fio, 0);
681 /* Allocate a new bio */
682 bio = __bio_alloc(fio, 1);
684 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
685 fio->page->index, fio, GFP_NOIO);
687 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
692 if (fio->io_wbc && !is_read_io(fio->op))
693 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
695 __attach_io_flag(fio);
696 bio_set_op_attrs(bio, fio->op, fio->op_flags);
698 inc_page_count(fio->sbi, is_read_io(fio->op) ?
699 __read_io_type(page): WB_DATA_TYPE(fio->page));
701 __submit_bio(fio->sbi, bio, fio->type);
705 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
706 block_t last_blkaddr, block_t cur_blkaddr)
708 if (unlikely(sbi->max_io_bytes &&
709 bio->bi_iter.bi_size >= sbi->max_io_bytes))
711 if (last_blkaddr + 1 != cur_blkaddr)
713 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
716 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
717 struct f2fs_io_info *fio)
719 if (io->fio.op != fio->op)
721 return io->fio.op_flags == fio->op_flags;
724 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
725 struct f2fs_bio_info *io,
726 struct f2fs_io_info *fio,
727 block_t last_blkaddr,
730 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
731 unsigned int filled_blocks =
732 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
733 unsigned int io_size = F2FS_IO_SIZE(sbi);
734 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
736 /* IOs in bio is aligned and left space of vectors is not enough */
737 if (!(filled_blocks % io_size) && left_vecs < io_size)
740 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
742 return io_type_is_mergeable(io, fio);
745 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
746 struct page *page, enum temp_type temp)
748 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
749 struct bio_entry *be;
751 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
755 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
758 down_write(&io->bio_list_lock);
759 list_add_tail(&be->list, &io->bio_list);
760 up_write(&io->bio_list_lock);
763 static void del_bio_entry(struct bio_entry *be)
766 kmem_cache_free(bio_entry_slab, be);
769 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
772 struct f2fs_sb_info *sbi = fio->sbi;
777 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
778 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
779 struct list_head *head = &io->bio_list;
780 struct bio_entry *be;
782 down_write(&io->bio_list_lock);
783 list_for_each_entry(be, head, list) {
789 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
792 if (f2fs_crypt_mergeable_bio(*bio,
793 fio->page->mapping->host,
794 fio->page->index, fio) &&
795 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
801 /* page can't be merged into bio; submit the bio */
803 __submit_bio(sbi, *bio, DATA);
806 up_write(&io->bio_list_lock);
817 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
818 struct bio **bio, struct page *page)
822 struct bio *target = bio ? *bio : NULL;
824 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
825 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
826 struct list_head *head = &io->bio_list;
827 struct bio_entry *be;
829 if (list_empty(head))
832 down_read(&io->bio_list_lock);
833 list_for_each_entry(be, head, list) {
835 found = (target == be->bio);
837 found = __has_merged_page(be->bio, NULL,
842 up_read(&io->bio_list_lock);
849 down_write(&io->bio_list_lock);
850 list_for_each_entry(be, head, list) {
852 found = (target == be->bio);
854 found = __has_merged_page(be->bio, NULL,
862 up_write(&io->bio_list_lock);
866 __submit_bio(sbi, target, DATA);
873 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
875 struct bio *bio = *fio->bio;
876 struct page *page = fio->encrypted_page ?
877 fio->encrypted_page : fio->page;
879 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
880 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
881 return -EFSCORRUPTED;
883 trace_f2fs_submit_page_bio(page, fio);
884 f2fs_trace_ios(fio, 0);
886 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
888 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
891 bio = __bio_alloc(fio, BIO_MAX_PAGES);
892 __attach_io_flag(fio);
893 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
894 fio->page->index, fio, GFP_NOIO);
895 bio_set_op_attrs(bio, fio->op, fio->op_flags);
897 add_bio_entry(fio->sbi, bio, page, fio->temp);
899 if (add_ipu_page(fio, &bio, page))
904 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
906 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
908 *fio->last_block = fio->new_blkaddr;
914 void f2fs_submit_page_write(struct f2fs_io_info *fio)
916 struct f2fs_sb_info *sbi = fio->sbi;
917 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
918 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
919 struct page *bio_page;
921 f2fs_bug_on(sbi, is_read_io(fio->op));
923 down_write(&io->io_rwsem);
926 spin_lock(&io->io_lock);
927 if (list_empty(&io->io_list)) {
928 spin_unlock(&io->io_lock);
931 fio = list_first_entry(&io->io_list,
932 struct f2fs_io_info, list);
933 list_del(&fio->list);
934 spin_unlock(&io->io_lock);
937 verify_fio_blkaddr(fio);
939 if (fio->encrypted_page)
940 bio_page = fio->encrypted_page;
941 else if (fio->compressed_page)
942 bio_page = fio->compressed_page;
944 bio_page = fio->page;
946 /* set submitted = true as a return value */
947 fio->submitted = true;
949 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
952 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
954 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
955 bio_page->index, fio)))
956 __submit_merged_bio(io);
958 if (io->bio == NULL) {
959 if (F2FS_IO_ALIGNED(sbi) &&
960 (fio->type == DATA || fio->type == NODE) &&
961 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
962 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
966 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
967 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
968 bio_page->index, fio, GFP_NOIO);
972 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
973 __submit_merged_bio(io);
978 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
980 io->last_block_in_bio = fio->new_blkaddr;
981 f2fs_trace_ios(fio, 0);
983 trace_f2fs_submit_page_write(fio->page, fio);
988 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
989 !f2fs_is_checkpoint_ready(sbi))
990 __submit_merged_bio(io);
991 up_write(&io->io_rwsem);
994 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
996 return fsverity_active(inode) &&
997 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
1000 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1001 unsigned nr_pages, unsigned op_flag,
1002 pgoff_t first_idx, bool for_write,
1005 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1007 struct bio_post_read_ctx *ctx;
1008 unsigned int post_read_steps = 0;
1010 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
1011 min_t(int, nr_pages, BIO_MAX_PAGES),
1014 return ERR_PTR(-ENOMEM);
1016 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1018 f2fs_target_device(sbi, blkaddr, bio);
1019 bio->bi_end_io = f2fs_read_end_io;
1020 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1022 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1023 post_read_steps |= 1 << STEP_DECRYPT;
1024 if (f2fs_compressed_file(inode))
1025 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1026 if (for_verity && f2fs_need_verity(inode, first_idx))
1027 post_read_steps |= 1 << STEP_VERITY;
1029 if (post_read_steps) {
1030 /* Due to the mempool, this never fails. */
1031 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1034 ctx->enabled_steps = post_read_steps;
1035 bio->bi_private = ctx;
1041 static void f2fs_release_read_bio(struct bio *bio)
1043 if (bio->bi_private)
1044 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1048 /* This can handle encryption stuffs */
1049 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1050 block_t blkaddr, int op_flags, bool for_write)
1052 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1055 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1056 page->index, for_write, true);
1058 return PTR_ERR(bio);
1060 /* wait for GCed page writeback via META_MAPPING */
1061 f2fs_wait_on_block_writeback(inode, blkaddr);
1063 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1067 ClearPageError(page);
1068 inc_page_count(sbi, F2FS_RD_DATA);
1069 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1070 __submit_bio(sbi, bio, DATA);
1074 static void __set_data_blkaddr(struct dnode_of_data *dn)
1076 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1080 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1081 base = get_extra_isize(dn->inode);
1083 /* Get physical address of data block */
1084 addr_array = blkaddr_in_node(rn);
1085 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1089 * Lock ordering for the change of data block address:
1092 * update block addresses in the node page
1094 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1096 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1097 __set_data_blkaddr(dn);
1098 if (set_page_dirty(dn->node_page))
1099 dn->node_changed = true;
1102 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1104 dn->data_blkaddr = blkaddr;
1105 f2fs_set_data_blkaddr(dn);
1106 f2fs_update_extent_cache(dn);
1109 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1110 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1112 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1118 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1120 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1123 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1124 dn->ofs_in_node, count);
1126 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1128 for (; count > 0; dn->ofs_in_node++) {
1129 block_t blkaddr = f2fs_data_blkaddr(dn);
1130 if (blkaddr == NULL_ADDR) {
1131 dn->data_blkaddr = NEW_ADDR;
1132 __set_data_blkaddr(dn);
1137 if (set_page_dirty(dn->node_page))
1138 dn->node_changed = true;
1142 /* Should keep dn->ofs_in_node unchanged */
1143 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1145 unsigned int ofs_in_node = dn->ofs_in_node;
1148 ret = f2fs_reserve_new_blocks(dn, 1);
1149 dn->ofs_in_node = ofs_in_node;
1153 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1155 bool need_put = dn->inode_page ? false : true;
1158 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1162 if (dn->data_blkaddr == NULL_ADDR)
1163 err = f2fs_reserve_new_block(dn);
1164 if (err || need_put)
1169 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1171 struct extent_info ei = {0, 0, 0};
1172 struct inode *inode = dn->inode;
1174 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1175 dn->data_blkaddr = ei.blk + index - ei.fofs;
1179 return f2fs_reserve_block(dn, index);
1182 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1183 int op_flags, bool for_write)
1185 struct address_space *mapping = inode->i_mapping;
1186 struct dnode_of_data dn;
1188 struct extent_info ei = {0,0,0};
1191 page = f2fs_grab_cache_page(mapping, index, for_write);
1193 return ERR_PTR(-ENOMEM);
1195 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1196 dn.data_blkaddr = ei.blk + index - ei.fofs;
1197 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1198 DATA_GENERIC_ENHANCE_READ)) {
1199 err = -EFSCORRUPTED;
1205 set_new_dnode(&dn, inode, NULL, NULL, 0);
1206 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1209 f2fs_put_dnode(&dn);
1211 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1215 if (dn.data_blkaddr != NEW_ADDR &&
1216 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1218 DATA_GENERIC_ENHANCE)) {
1219 err = -EFSCORRUPTED;
1223 if (PageUptodate(page)) {
1229 * A new dentry page is allocated but not able to be written, since its
1230 * new inode page couldn't be allocated due to -ENOSPC.
1231 * In such the case, its blkaddr can be remained as NEW_ADDR.
1232 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1233 * f2fs_init_inode_metadata.
1235 if (dn.data_blkaddr == NEW_ADDR) {
1236 zero_user_segment(page, 0, PAGE_SIZE);
1237 if (!PageUptodate(page))
1238 SetPageUptodate(page);
1243 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1244 op_flags, for_write);
1250 f2fs_put_page(page, 1);
1251 return ERR_PTR(err);
1254 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1256 struct address_space *mapping = inode->i_mapping;
1259 page = find_get_page(mapping, index);
1260 if (page && PageUptodate(page))
1262 f2fs_put_page(page, 0);
1264 page = f2fs_get_read_data_page(inode, index, 0, false);
1268 if (PageUptodate(page))
1271 wait_on_page_locked(page);
1272 if (unlikely(!PageUptodate(page))) {
1273 f2fs_put_page(page, 0);
1274 return ERR_PTR(-EIO);
1280 * If it tries to access a hole, return an error.
1281 * Because, the callers, functions in dir.c and GC, should be able to know
1282 * whether this page exists or not.
1284 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1287 struct address_space *mapping = inode->i_mapping;
1290 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1294 /* wait for read completion */
1296 if (unlikely(page->mapping != mapping)) {
1297 f2fs_put_page(page, 1);
1300 if (unlikely(!PageUptodate(page))) {
1301 f2fs_put_page(page, 1);
1302 return ERR_PTR(-EIO);
1308 * Caller ensures that this data page is never allocated.
1309 * A new zero-filled data page is allocated in the page cache.
1311 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1313 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1314 * ipage should be released by this function.
1316 struct page *f2fs_get_new_data_page(struct inode *inode,
1317 struct page *ipage, pgoff_t index, bool new_i_size)
1319 struct address_space *mapping = inode->i_mapping;
1321 struct dnode_of_data dn;
1324 page = f2fs_grab_cache_page(mapping, index, true);
1327 * before exiting, we should make sure ipage will be released
1328 * if any error occur.
1330 f2fs_put_page(ipage, 1);
1331 return ERR_PTR(-ENOMEM);
1334 set_new_dnode(&dn, inode, ipage, NULL, 0);
1335 err = f2fs_reserve_block(&dn, index);
1337 f2fs_put_page(page, 1);
1338 return ERR_PTR(err);
1341 f2fs_put_dnode(&dn);
1343 if (PageUptodate(page))
1346 if (dn.data_blkaddr == NEW_ADDR) {
1347 zero_user_segment(page, 0, PAGE_SIZE);
1348 if (!PageUptodate(page))
1349 SetPageUptodate(page);
1351 f2fs_put_page(page, 1);
1353 /* if ipage exists, blkaddr should be NEW_ADDR */
1354 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1355 page = f2fs_get_lock_data_page(inode, index, true);
1360 if (new_i_size && i_size_read(inode) <
1361 ((loff_t)(index + 1) << PAGE_SHIFT))
1362 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1366 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1368 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1369 struct f2fs_summary sum;
1370 struct node_info ni;
1371 block_t old_blkaddr;
1375 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1378 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1382 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1383 if (dn->data_blkaddr != NULL_ADDR)
1386 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1390 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1391 old_blkaddr = dn->data_blkaddr;
1392 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1393 &sum, seg_type, NULL);
1394 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1395 invalidate_mapping_pages(META_MAPPING(sbi),
1396 old_blkaddr, old_blkaddr);
1397 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1400 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1401 * data from unwritten block via dio_read.
1406 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1408 struct inode *inode = file_inode(iocb->ki_filp);
1409 struct f2fs_map_blocks map;
1412 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1414 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1415 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1416 if (map.m_len > map.m_lblk)
1417 map.m_len -= map.m_lblk;
1421 map.m_next_pgofs = NULL;
1422 map.m_next_extent = NULL;
1423 map.m_seg_type = NO_CHECK_TYPE;
1424 map.m_may_create = true;
1427 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1428 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1429 F2FS_GET_BLOCK_PRE_AIO :
1430 F2FS_GET_BLOCK_PRE_DIO;
1433 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1434 err = f2fs_convert_inline_inode(inode);
1438 if (f2fs_has_inline_data(inode))
1441 flag = F2FS_GET_BLOCK_PRE_AIO;
1444 err = f2fs_map_blocks(inode, &map, 1, flag);
1445 if (map.m_len > 0 && err == -ENOSPC) {
1447 set_inode_flag(inode, FI_NO_PREALLOC);
1453 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1455 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1457 down_read(&sbi->node_change);
1459 up_read(&sbi->node_change);
1464 f2fs_unlock_op(sbi);
1469 * f2fs_map_blocks() tries to find or build mapping relationship which
1470 * maps continuous logical blocks to physical blocks, and return such
1471 * info via f2fs_map_blocks structure.
1473 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1474 int create, int flag)
1476 unsigned int maxblocks = map->m_len;
1477 struct dnode_of_data dn;
1478 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1479 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1480 pgoff_t pgofs, end_offset, end;
1481 int err = 0, ofs = 1;
1482 unsigned int ofs_in_node, last_ofs_in_node;
1484 struct extent_info ei = {0,0,0};
1486 unsigned int start_pgofs;
1494 /* it only supports block size == page size */
1495 pgofs = (pgoff_t)map->m_lblk;
1496 end = pgofs + maxblocks;
1498 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1499 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1503 map->m_pblk = ei.blk + pgofs - ei.fofs;
1504 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1505 map->m_flags = F2FS_MAP_MAPPED;
1506 if (map->m_next_extent)
1507 *map->m_next_extent = pgofs + map->m_len;
1509 /* for hardware encryption, but to avoid potential issue in future */
1510 if (flag == F2FS_GET_BLOCK_DIO)
1511 f2fs_wait_on_block_writeback_range(inode,
1512 map->m_pblk, map->m_len);
1517 if (map->m_may_create)
1518 f2fs_do_map_lock(sbi, flag, true);
1520 /* When reading holes, we need its node page */
1521 set_new_dnode(&dn, inode, NULL, NULL, 0);
1522 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1524 if (flag == F2FS_GET_BLOCK_BMAP)
1526 if (err == -ENOENT) {
1528 if (map->m_next_pgofs)
1529 *map->m_next_pgofs =
1530 f2fs_get_next_page_offset(&dn, pgofs);
1531 if (map->m_next_extent)
1532 *map->m_next_extent =
1533 f2fs_get_next_page_offset(&dn, pgofs);
1538 start_pgofs = pgofs;
1540 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1541 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1544 blkaddr = f2fs_data_blkaddr(&dn);
1546 if (__is_valid_data_blkaddr(blkaddr) &&
1547 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1548 err = -EFSCORRUPTED;
1552 if (__is_valid_data_blkaddr(blkaddr)) {
1553 /* use out-place-update for driect IO under LFS mode */
1554 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1555 map->m_may_create) {
1556 err = __allocate_data_block(&dn, map->m_seg_type);
1559 blkaddr = dn.data_blkaddr;
1560 set_inode_flag(inode, FI_APPEND_WRITE);
1564 if (unlikely(f2fs_cp_error(sbi))) {
1568 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1569 if (blkaddr == NULL_ADDR) {
1571 last_ofs_in_node = dn.ofs_in_node;
1574 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1575 flag != F2FS_GET_BLOCK_DIO);
1576 err = __allocate_data_block(&dn,
1579 set_inode_flag(inode, FI_APPEND_WRITE);
1583 map->m_flags |= F2FS_MAP_NEW;
1584 blkaddr = dn.data_blkaddr;
1586 if (flag == F2FS_GET_BLOCK_BMAP) {
1590 if (flag == F2FS_GET_BLOCK_PRECACHE)
1592 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1593 blkaddr == NULL_ADDR) {
1594 if (map->m_next_pgofs)
1595 *map->m_next_pgofs = pgofs + 1;
1598 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1599 /* for defragment case */
1600 if (map->m_next_pgofs)
1601 *map->m_next_pgofs = pgofs + 1;
1607 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1610 if (map->m_len == 0) {
1611 /* preallocated unwritten block should be mapped for fiemap. */
1612 if (blkaddr == NEW_ADDR)
1613 map->m_flags |= F2FS_MAP_UNWRITTEN;
1614 map->m_flags |= F2FS_MAP_MAPPED;
1616 map->m_pblk = blkaddr;
1618 } else if ((map->m_pblk != NEW_ADDR &&
1619 blkaddr == (map->m_pblk + ofs)) ||
1620 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1621 flag == F2FS_GET_BLOCK_PRE_DIO) {
1632 /* preallocate blocks in batch for one dnode page */
1633 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1634 (pgofs == end || dn.ofs_in_node == end_offset)) {
1636 dn.ofs_in_node = ofs_in_node;
1637 err = f2fs_reserve_new_blocks(&dn, prealloc);
1641 map->m_len += dn.ofs_in_node - ofs_in_node;
1642 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1646 dn.ofs_in_node = end_offset;
1651 else if (dn.ofs_in_node < end_offset)
1654 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1655 if (map->m_flags & F2FS_MAP_MAPPED) {
1656 unsigned int ofs = start_pgofs - map->m_lblk;
1658 f2fs_update_extent_cache_range(&dn,
1659 start_pgofs, map->m_pblk + ofs,
1664 f2fs_put_dnode(&dn);
1666 if (map->m_may_create) {
1667 f2fs_do_map_lock(sbi, flag, false);
1668 f2fs_balance_fs(sbi, dn.node_changed);
1674 /* for hardware encryption, but to avoid potential issue in future */
1675 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1676 f2fs_wait_on_block_writeback_range(inode,
1677 map->m_pblk, map->m_len);
1679 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1680 if (map->m_flags & F2FS_MAP_MAPPED) {
1681 unsigned int ofs = start_pgofs - map->m_lblk;
1683 f2fs_update_extent_cache_range(&dn,
1684 start_pgofs, map->m_pblk + ofs,
1687 if (map->m_next_extent)
1688 *map->m_next_extent = pgofs + 1;
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);
1697 trace_f2fs_map_blocks(inode, map, err);
1701 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1703 struct f2fs_map_blocks map;
1707 if (pos + len > i_size_read(inode))
1710 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1711 map.m_next_pgofs = NULL;
1712 map.m_next_extent = NULL;
1713 map.m_seg_type = NO_CHECK_TYPE;
1714 map.m_may_create = false;
1715 last_lblk = F2FS_BLK_ALIGN(pos + len);
1717 while (map.m_lblk < last_lblk) {
1718 map.m_len = last_lblk - map.m_lblk;
1719 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1720 if (err || map.m_len == 0)
1722 map.m_lblk += map.m_len;
1727 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1729 return (bytes >> inode->i_blkbits);
1732 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1734 return (blks << inode->i_blkbits);
1737 static int __get_data_block(struct inode *inode, sector_t iblock,
1738 struct buffer_head *bh, int create, int flag,
1739 pgoff_t *next_pgofs, int seg_type, bool may_write)
1741 struct f2fs_map_blocks map;
1744 map.m_lblk = iblock;
1745 map.m_len = bytes_to_blks(inode, bh->b_size);
1746 map.m_next_pgofs = next_pgofs;
1747 map.m_next_extent = NULL;
1748 map.m_seg_type = seg_type;
1749 map.m_may_create = may_write;
1751 err = f2fs_map_blocks(inode, &map, create, flag);
1753 map_bh(bh, inode->i_sb, map.m_pblk);
1754 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1755 bh->b_size = blks_to_bytes(inode, map.m_len);
1760 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1761 struct buffer_head *bh_result, int create)
1763 return __get_data_block(inode, iblock, bh_result, create,
1764 F2FS_GET_BLOCK_DIO, NULL,
1765 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1766 IS_SWAPFILE(inode) ? false : true);
1769 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1770 struct buffer_head *bh_result, int create)
1772 return __get_data_block(inode, iblock, bh_result, create,
1773 F2FS_GET_BLOCK_DIO, NULL,
1774 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1778 static int f2fs_xattr_fiemap(struct inode *inode,
1779 struct fiemap_extent_info *fieinfo)
1781 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1783 struct node_info ni;
1784 __u64 phys = 0, len;
1786 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1789 if (f2fs_has_inline_xattr(inode)) {
1792 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1793 inode->i_ino, false);
1797 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1799 f2fs_put_page(page, 1);
1803 phys = blks_to_bytes(inode, ni.blk_addr);
1804 offset = offsetof(struct f2fs_inode, i_addr) +
1805 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1806 get_inline_xattr_addrs(inode));
1809 len = inline_xattr_size(inode);
1811 f2fs_put_page(page, 1);
1813 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1816 flags |= FIEMAP_EXTENT_LAST;
1818 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1819 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1820 if (err || err == 1)
1825 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1829 err = f2fs_get_node_info(sbi, xnid, &ni);
1831 f2fs_put_page(page, 1);
1835 phys = blks_to_bytes(inode, ni.blk_addr);
1836 len = inode->i_sb->s_blocksize;
1838 f2fs_put_page(page, 1);
1840 flags = FIEMAP_EXTENT_LAST;
1844 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1845 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1848 return (err < 0 ? err : 0);
1851 static loff_t max_inode_blocks(struct inode *inode)
1853 loff_t result = ADDRS_PER_INODE(inode);
1854 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1856 /* two direct node blocks */
1857 result += (leaf_count * 2);
1859 /* two indirect node blocks */
1860 leaf_count *= NIDS_PER_BLOCK;
1861 result += (leaf_count * 2);
1863 /* one double indirect node block */
1864 leaf_count *= NIDS_PER_BLOCK;
1865 result += leaf_count;
1870 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1873 struct f2fs_map_blocks map;
1874 sector_t start_blk, last_blk;
1876 u64 logical = 0, phys = 0, size = 0;
1879 bool compr_cluster = false;
1880 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1882 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1883 ret = f2fs_precache_extents(inode);
1888 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1894 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1895 ret = f2fs_xattr_fiemap(inode, fieinfo);
1899 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1900 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1905 if (bytes_to_blks(inode, len) == 0)
1906 len = blks_to_bytes(inode, 1);
1908 start_blk = bytes_to_blks(inode, start);
1909 last_blk = bytes_to_blks(inode, start + len - 1);
1912 memset(&map, 0, sizeof(map));
1913 map.m_lblk = start_blk;
1914 map.m_len = bytes_to_blks(inode, len);
1915 map.m_next_pgofs = &next_pgofs;
1916 map.m_seg_type = NO_CHECK_TYPE;
1919 map.m_len = cluster_size - 1;
1921 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1926 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1927 start_blk = next_pgofs;
1929 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1930 max_inode_blocks(inode)))
1933 flags |= FIEMAP_EXTENT_LAST;
1937 if (IS_ENCRYPTED(inode))
1938 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1940 ret = fiemap_fill_next_extent(fieinfo, logical,
1942 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1948 if (start_blk > last_blk)
1951 if (compr_cluster) {
1952 compr_cluster = false;
1955 logical = blks_to_bytes(inode, start_blk - 1);
1956 phys = blks_to_bytes(inode, map.m_pblk);
1957 size = blks_to_bytes(inode, cluster_size);
1959 flags |= FIEMAP_EXTENT_ENCODED;
1961 start_blk += cluster_size - 1;
1963 if (start_blk > last_blk)
1969 if (map.m_pblk == COMPRESS_ADDR) {
1970 compr_cluster = true;
1975 logical = blks_to_bytes(inode, start_blk);
1976 phys = blks_to_bytes(inode, map.m_pblk);
1977 size = blks_to_bytes(inode, map.m_len);
1979 if (map.m_flags & F2FS_MAP_UNWRITTEN)
1980 flags = FIEMAP_EXTENT_UNWRITTEN;
1982 start_blk += bytes_to_blks(inode, size);
1986 if (fatal_signal_pending(current))
1994 inode_unlock(inode);
1998 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2000 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2001 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2002 return inode->i_sb->s_maxbytes;
2004 return i_size_read(inode);
2007 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2009 struct f2fs_map_blocks *map,
2010 struct bio **bio_ret,
2011 sector_t *last_block_in_bio,
2014 struct bio *bio = *bio_ret;
2015 const unsigned blocksize = blks_to_bytes(inode, 1);
2016 sector_t block_in_file;
2017 sector_t last_block;
2018 sector_t last_block_in_file;
2022 block_in_file = (sector_t)page_index(page);
2023 last_block = block_in_file + nr_pages;
2024 last_block_in_file = bytes_to_blks(inode,
2025 f2fs_readpage_limit(inode) + blocksize - 1);
2026 if (last_block > last_block_in_file)
2027 last_block = last_block_in_file;
2029 /* just zeroing out page which is beyond EOF */
2030 if (block_in_file >= last_block)
2033 * Map blocks using the previous result first.
2035 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2036 block_in_file > map->m_lblk &&
2037 block_in_file < (map->m_lblk + map->m_len))
2041 * Then do more f2fs_map_blocks() calls until we are
2042 * done with this page.
2044 map->m_lblk = block_in_file;
2045 map->m_len = last_block - block_in_file;
2047 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2051 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2052 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2053 SetPageMappedToDisk(page);
2055 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2056 !cleancache_get_page(page))) {
2057 SetPageUptodate(page);
2061 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2062 DATA_GENERIC_ENHANCE_READ)) {
2063 ret = -EFSCORRUPTED;
2068 zero_user_segment(page, 0, PAGE_SIZE);
2069 if (f2fs_need_verity(inode, page->index) &&
2070 !fsverity_verify_page(page)) {
2074 if (!PageUptodate(page))
2075 SetPageUptodate(page);
2081 * This page will go to BIO. Do we need to send this
2084 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2085 *last_block_in_bio, block_nr) ||
2086 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2088 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2092 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2093 is_readahead ? REQ_RAHEAD : 0, page->index,
2103 * If the page is under writeback, we need to wait for
2104 * its completion to see the correct decrypted data.
2106 f2fs_wait_on_block_writeback(inode, block_nr);
2108 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2109 goto submit_and_realloc;
2111 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2112 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2113 ClearPageError(page);
2114 *last_block_in_bio = block_nr;
2118 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2127 #ifdef CONFIG_F2FS_FS_COMPRESSION
2128 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2129 unsigned nr_pages, sector_t *last_block_in_bio,
2130 bool is_readahead, bool for_write)
2132 struct dnode_of_data dn;
2133 struct inode *inode = cc->inode;
2134 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2135 struct bio *bio = *bio_ret;
2136 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2137 sector_t last_block_in_file;
2138 const unsigned blocksize = blks_to_bytes(inode, 1);
2139 struct decompress_io_ctx *dic = NULL;
2140 struct bio_post_read_ctx *ctx;
2141 bool for_verity = false;
2145 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2147 last_block_in_file = bytes_to_blks(inode,
2148 f2fs_readpage_limit(inode) + blocksize - 1);
2150 /* get rid of pages beyond EOF */
2151 for (i = 0; i < cc->cluster_size; i++) {
2152 struct page *page = cc->rpages[i];
2156 if ((sector_t)page->index >= last_block_in_file) {
2157 zero_user_segment(page, 0, PAGE_SIZE);
2158 if (!PageUptodate(page))
2159 SetPageUptodate(page);
2160 } else if (!PageUptodate(page)) {
2164 cc->rpages[i] = NULL;
2168 /* we are done since all pages are beyond EOF */
2169 if (f2fs_cluster_is_empty(cc))
2172 set_new_dnode(&dn, inode, NULL, NULL, 0);
2173 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2177 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2179 for (i = 1; i < cc->cluster_size; i++) {
2182 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2183 dn.ofs_in_node + i);
2185 if (!__is_valid_data_blkaddr(blkaddr))
2188 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2195 /* nothing to decompress */
2196 if (cc->nr_cpages == 0) {
2201 dic = f2fs_alloc_dic(cc);
2208 * It's possible to enable fsverity on the fly when handling a cluster,
2209 * which requires complicated error handling. Instead of adding more
2210 * complexity, let's give a rule where end_io post-processes fsverity
2211 * per cluster. In order to do that, we need to submit bio, if previous
2212 * bio sets a different post-process policy.
2214 if (fsverity_active(cc->inode)) {
2215 atomic_set(&dic->verity_pages, cc->nr_cpages);
2219 ctx = bio->bi_private;
2220 if (!(ctx->enabled_steps & (1 << STEP_VERITY))) {
2221 __submit_bio(sbi, bio, DATA);
2227 for (i = 0; i < dic->nr_cpages; i++) {
2228 struct page *page = dic->cpages[i];
2231 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2232 dn.ofs_in_node + i + 1);
2234 if (bio && (!page_is_mergeable(sbi, bio,
2235 *last_block_in_bio, blkaddr) ||
2236 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2238 __submit_bio(sbi, bio, DATA);
2243 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2244 is_readahead ? REQ_RAHEAD : 0,
2245 page->index, for_write, for_verity);
2247 unsigned int remained = dic->nr_cpages - i;
2248 bool release = false;
2254 if (!atomic_sub_return(remained,
2255 &dic->verity_pages))
2258 if (!atomic_sub_return(remained,
2259 &dic->pending_pages))
2264 f2fs_decompress_end_io(dic->rpages,
2265 cc->cluster_size, true,
2270 f2fs_put_dnode(&dn);
2276 f2fs_wait_on_block_writeback(inode, blkaddr);
2278 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2279 goto submit_and_realloc;
2281 /* tag STEP_DECOMPRESS to handle IO in wq */
2282 ctx = bio->bi_private;
2283 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2284 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2286 inc_page_count(sbi, F2FS_RD_DATA);
2287 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2288 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2289 ClearPageError(page);
2290 *last_block_in_bio = blkaddr;
2293 f2fs_put_dnode(&dn);
2299 f2fs_put_dnode(&dn);
2301 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2308 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2309 * Major change was from block_size == page_size in f2fs by default.
2311 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2312 * this function ever deviates from doing just read-ahead, it should either
2313 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2316 static int f2fs_mpage_readpages(struct inode *inode,
2317 struct readahead_control *rac, struct page *page)
2319 struct bio *bio = NULL;
2320 sector_t last_block_in_bio = 0;
2321 struct f2fs_map_blocks map;
2322 #ifdef CONFIG_F2FS_FS_COMPRESSION
2323 struct compress_ctx cc = {
2325 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2326 .cluster_size = F2FS_I(inode)->i_cluster_size,
2327 .cluster_idx = NULL_CLUSTER,
2334 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2335 unsigned max_nr_pages = nr_pages;
2337 bool drop_ra = false;
2343 map.m_next_pgofs = NULL;
2344 map.m_next_extent = NULL;
2345 map.m_seg_type = NO_CHECK_TYPE;
2346 map.m_may_create = false;
2349 * Two readahead threads for same address range can cause race condition
2350 * which fragments sequential read IOs. So let's avoid each other.
2352 if (rac && readahead_count(rac)) {
2353 if (READ_ONCE(F2FS_I(inode)->ra_offset) == readahead_index(rac))
2356 WRITE_ONCE(F2FS_I(inode)->ra_offset,
2357 readahead_index(rac));
2360 for (; nr_pages; nr_pages--) {
2362 page = readahead_page(rac);
2363 prefetchw(&page->flags);
2365 f2fs_put_page(page, 1);
2370 #ifdef CONFIG_F2FS_FS_COMPRESSION
2371 if (f2fs_compressed_file(inode)) {
2372 /* there are remained comressed pages, submit them */
2373 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2374 ret = f2fs_read_multi_pages(&cc, &bio,
2377 rac != NULL, false);
2378 f2fs_destroy_compress_ctx(&cc);
2380 goto set_error_page;
2382 ret = f2fs_is_compressed_cluster(inode, page->index);
2384 goto set_error_page;
2386 goto read_single_page;
2388 ret = f2fs_init_compress_ctx(&cc);
2390 goto set_error_page;
2392 f2fs_compress_ctx_add_page(&cc, page);
2399 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2400 &bio, &last_block_in_bio, rac);
2402 #ifdef CONFIG_F2FS_FS_COMPRESSION
2406 zero_user_segment(page, 0, PAGE_SIZE);
2409 #ifdef CONFIG_F2FS_FS_COMPRESSION
2415 #ifdef CONFIG_F2FS_FS_COMPRESSION
2416 if (f2fs_compressed_file(inode)) {
2418 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2419 ret = f2fs_read_multi_pages(&cc, &bio,
2422 rac != NULL, false);
2423 f2fs_destroy_compress_ctx(&cc);
2429 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2431 if (rac && readahead_count(rac) && !drop_ra)
2432 WRITE_ONCE(F2FS_I(inode)->ra_offset, -1);
2436 static int f2fs_read_data_page(struct file *file, struct page *page)
2438 struct inode *inode = page_file_mapping(page)->host;
2441 trace_f2fs_readpage(page, DATA);
2443 if (!f2fs_is_compress_backend_ready(inode)) {
2448 /* If the file has inline data, try to read it directly */
2449 if (f2fs_has_inline_data(inode))
2450 ret = f2fs_read_inline_data(inode, page);
2452 ret = f2fs_mpage_readpages(inode, NULL, page);
2456 static void f2fs_readahead(struct readahead_control *rac)
2458 struct inode *inode = rac->mapping->host;
2460 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2462 if (!f2fs_is_compress_backend_ready(inode))
2465 /* If the file has inline data, skip readpages */
2466 if (f2fs_has_inline_data(inode))
2469 f2fs_mpage_readpages(inode, rac, NULL);
2472 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2474 struct inode *inode = fio->page->mapping->host;
2475 struct page *mpage, *page;
2476 gfp_t gfp_flags = GFP_NOFS;
2478 if (!f2fs_encrypted_file(inode))
2481 page = fio->compressed_page ? fio->compressed_page : fio->page;
2483 /* wait for GCed page writeback via META_MAPPING */
2484 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2486 if (fscrypt_inode_uses_inline_crypto(inode))
2490 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2491 PAGE_SIZE, 0, gfp_flags);
2492 if (IS_ERR(fio->encrypted_page)) {
2493 /* flush pending IOs and wait for a while in the ENOMEM case */
2494 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2495 f2fs_flush_merged_writes(fio->sbi);
2496 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2497 gfp_flags |= __GFP_NOFAIL;
2500 return PTR_ERR(fio->encrypted_page);
2503 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2505 if (PageUptodate(mpage))
2506 memcpy(page_address(mpage),
2507 page_address(fio->encrypted_page), PAGE_SIZE);
2508 f2fs_put_page(mpage, 1);
2513 static inline bool check_inplace_update_policy(struct inode *inode,
2514 struct f2fs_io_info *fio)
2516 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2517 unsigned int policy = SM_I(sbi)->ipu_policy;
2519 if (policy & (0x1 << F2FS_IPU_FORCE))
2521 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2523 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2524 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2526 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2527 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2531 * IPU for rewrite async pages
2533 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2534 fio && fio->op == REQ_OP_WRITE &&
2535 !(fio->op_flags & REQ_SYNC) &&
2536 !IS_ENCRYPTED(inode))
2539 /* this is only set during fdatasync */
2540 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2541 is_inode_flag_set(inode, FI_NEED_IPU))
2544 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2545 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2551 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2553 if (f2fs_is_pinned_file(inode))
2556 /* if this is cold file, we should overwrite to avoid fragmentation */
2557 if (file_is_cold(inode))
2560 return check_inplace_update_policy(inode, fio);
2563 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2565 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2567 if (f2fs_lfs_mode(sbi))
2569 if (S_ISDIR(inode->i_mode))
2571 if (IS_NOQUOTA(inode))
2573 if (f2fs_is_atomic_file(inode))
2576 if (is_cold_data(fio->page))
2578 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2580 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2581 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2587 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2589 struct inode *inode = fio->page->mapping->host;
2591 if (f2fs_should_update_outplace(inode, fio))
2594 return f2fs_should_update_inplace(inode, fio);
2597 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2599 struct page *page = fio->page;
2600 struct inode *inode = page->mapping->host;
2601 struct dnode_of_data dn;
2602 struct extent_info ei = {0,0,0};
2603 struct node_info ni;
2604 bool ipu_force = false;
2607 set_new_dnode(&dn, inode, NULL, NULL, 0);
2608 if (need_inplace_update(fio) &&
2609 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2610 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2612 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2613 DATA_GENERIC_ENHANCE))
2614 return -EFSCORRUPTED;
2617 fio->need_lock = LOCK_DONE;
2621 /* Deadlock due to between page->lock and f2fs_lock_op */
2622 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2625 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2629 fio->old_blkaddr = dn.data_blkaddr;
2631 /* This page is already truncated */
2632 if (fio->old_blkaddr == NULL_ADDR) {
2633 ClearPageUptodate(page);
2634 clear_cold_data(page);
2638 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2639 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2640 DATA_GENERIC_ENHANCE)) {
2641 err = -EFSCORRUPTED;
2645 * If current allocation needs SSR,
2646 * it had better in-place writes for updated data.
2649 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2650 need_inplace_update(fio))) {
2651 err = f2fs_encrypt_one_page(fio);
2655 set_page_writeback(page);
2656 ClearPageError(page);
2657 f2fs_put_dnode(&dn);
2658 if (fio->need_lock == LOCK_REQ)
2659 f2fs_unlock_op(fio->sbi);
2660 err = f2fs_inplace_write_data(fio);
2662 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2663 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2664 if (PageWriteback(page))
2665 end_page_writeback(page);
2667 set_inode_flag(inode, FI_UPDATE_WRITE);
2669 trace_f2fs_do_write_data_page(fio->page, IPU);
2673 if (fio->need_lock == LOCK_RETRY) {
2674 if (!f2fs_trylock_op(fio->sbi)) {
2678 fio->need_lock = LOCK_REQ;
2681 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2685 fio->version = ni.version;
2687 err = f2fs_encrypt_one_page(fio);
2691 set_page_writeback(page);
2692 ClearPageError(page);
2694 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2695 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2697 /* LFS mode write path */
2698 f2fs_outplace_write_data(&dn, fio);
2699 trace_f2fs_do_write_data_page(page, OPU);
2700 set_inode_flag(inode, FI_APPEND_WRITE);
2701 if (page->index == 0)
2702 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2704 f2fs_put_dnode(&dn);
2706 if (fio->need_lock == LOCK_REQ)
2707 f2fs_unlock_op(fio->sbi);
2711 int f2fs_write_single_data_page(struct page *page, int *submitted,
2713 sector_t *last_block,
2714 struct writeback_control *wbc,
2715 enum iostat_type io_type,
2718 struct inode *inode = page->mapping->host;
2719 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2720 loff_t i_size = i_size_read(inode);
2721 const pgoff_t end_index = ((unsigned long long)i_size)
2723 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2724 unsigned offset = 0;
2725 bool need_balance_fs = false;
2727 struct f2fs_io_info fio = {
2729 .ino = inode->i_ino,
2732 .op_flags = wbc_to_write_flags(wbc),
2733 .old_blkaddr = NULL_ADDR,
2735 .encrypted_page = NULL,
2737 .compr_blocks = compr_blocks,
2738 .need_lock = LOCK_RETRY,
2742 .last_block = last_block,
2745 trace_f2fs_writepage(page, DATA);
2747 /* we should bypass data pages to proceed the kworkder jobs */
2748 if (unlikely(f2fs_cp_error(sbi))) {
2749 mapping_set_error(page->mapping, -EIO);
2751 * don't drop any dirty dentry pages for keeping lastest
2752 * directory structure.
2754 if (S_ISDIR(inode->i_mode))
2759 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2762 if (page->index < end_index ||
2763 f2fs_verity_in_progress(inode) ||
2768 * If the offset is out-of-range of file size,
2769 * this page does not have to be written to disk.
2771 offset = i_size & (PAGE_SIZE - 1);
2772 if ((page->index >= end_index + 1) || !offset)
2775 zero_user_segment(page, offset, PAGE_SIZE);
2777 if (f2fs_is_drop_cache(inode))
2779 /* we should not write 0'th page having journal header */
2780 if (f2fs_is_volatile_file(inode) && (!page->index ||
2781 (!wbc->for_reclaim &&
2782 f2fs_available_free_memory(sbi, BASE_CHECK))))
2785 /* Dentry/quota blocks are controlled by checkpoint */
2786 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2788 * We need to wait for node_write to avoid block allocation during
2789 * checkpoint. This can only happen to quota writes which can cause
2790 * the below discard race condition.
2792 if (IS_NOQUOTA(inode))
2793 down_read(&sbi->node_write);
2795 fio.need_lock = LOCK_DONE;
2796 err = f2fs_do_write_data_page(&fio);
2798 if (IS_NOQUOTA(inode))
2799 up_read(&sbi->node_write);
2804 if (!wbc->for_reclaim)
2805 need_balance_fs = true;
2806 else if (has_not_enough_free_secs(sbi, 0, 0))
2809 set_inode_flag(inode, FI_HOT_DATA);
2812 if (f2fs_has_inline_data(inode)) {
2813 err = f2fs_write_inline_data(inode, page);
2818 if (err == -EAGAIN) {
2819 err = f2fs_do_write_data_page(&fio);
2820 if (err == -EAGAIN) {
2821 fio.need_lock = LOCK_REQ;
2822 err = f2fs_do_write_data_page(&fio);
2827 file_set_keep_isize(inode);
2829 spin_lock(&F2FS_I(inode)->i_size_lock);
2830 if (F2FS_I(inode)->last_disk_size < psize)
2831 F2FS_I(inode)->last_disk_size = psize;
2832 spin_unlock(&F2FS_I(inode)->i_size_lock);
2836 if (err && err != -ENOENT)
2840 inode_dec_dirty_pages(inode);
2842 ClearPageUptodate(page);
2843 clear_cold_data(page);
2846 if (wbc->for_reclaim) {
2847 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2848 clear_inode_flag(inode, FI_HOT_DATA);
2849 f2fs_remove_dirty_inode(inode);
2853 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2854 !F2FS_I(inode)->cp_task)
2855 f2fs_balance_fs(sbi, need_balance_fs);
2857 if (unlikely(f2fs_cp_error(sbi))) {
2858 f2fs_submit_merged_write(sbi, DATA);
2859 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2864 *submitted = fio.submitted ? 1 : 0;
2869 redirty_page_for_writepage(wbc, page);
2871 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2872 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2873 * file_write_and_wait_range() will see EIO error, which is critical
2874 * to return value of fsync() followed by atomic_write failure to user.
2876 if (!err || wbc->for_reclaim)
2877 return AOP_WRITEPAGE_ACTIVATE;
2882 static int f2fs_write_data_page(struct page *page,
2883 struct writeback_control *wbc)
2885 #ifdef CONFIG_F2FS_FS_COMPRESSION
2886 struct inode *inode = page->mapping->host;
2888 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2891 if (f2fs_compressed_file(inode)) {
2892 if (f2fs_is_compressed_cluster(inode, page->index)) {
2893 redirty_page_for_writepage(wbc, page);
2894 return AOP_WRITEPAGE_ACTIVATE;
2900 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2901 wbc, FS_DATA_IO, 0);
2905 * This function was copied from write_cche_pages from mm/page-writeback.c.
2906 * The major change is making write step of cold data page separately from
2907 * warm/hot data page.
2909 static int f2fs_write_cache_pages(struct address_space *mapping,
2910 struct writeback_control *wbc,
2911 enum iostat_type io_type)
2914 int done = 0, retry = 0;
2915 struct pagevec pvec;
2916 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2917 struct bio *bio = NULL;
2918 sector_t last_block;
2919 #ifdef CONFIG_F2FS_FS_COMPRESSION
2920 struct inode *inode = mapping->host;
2921 struct compress_ctx cc = {
2923 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2924 .cluster_size = F2FS_I(inode)->i_cluster_size,
2925 .cluster_idx = NULL_CLUSTER,
2931 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2937 pgoff_t end; /* Inclusive */
2939 int range_whole = 0;
2945 pagevec_init(&pvec);
2947 if (get_dirty_pages(mapping->host) <=
2948 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2949 set_inode_flag(mapping->host, FI_HOT_DATA);
2951 clear_inode_flag(mapping->host, FI_HOT_DATA);
2953 if (wbc->range_cyclic) {
2954 index = mapping->writeback_index; /* prev offset */
2957 index = wbc->range_start >> PAGE_SHIFT;
2958 end = wbc->range_end >> PAGE_SHIFT;
2959 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2962 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2963 tag = PAGECACHE_TAG_TOWRITE;
2965 tag = PAGECACHE_TAG_DIRTY;
2968 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2969 tag_pages_for_writeback(mapping, index, end);
2971 while (!done && !retry && (index <= end)) {
2972 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2977 for (i = 0; i < nr_pages; i++) {
2978 struct page *page = pvec.pages[i];
2982 #ifdef CONFIG_F2FS_FS_COMPRESSION
2983 if (f2fs_compressed_file(inode)) {
2984 ret = f2fs_init_compress_ctx(&cc);
2990 if (!f2fs_cluster_can_merge_page(&cc,
2992 ret = f2fs_write_multi_pages(&cc,
2993 &submitted, wbc, io_type);
2999 if (unlikely(f2fs_cp_error(sbi)))
3002 if (f2fs_cluster_is_empty(&cc)) {
3003 void *fsdata = NULL;
3007 ret2 = f2fs_prepare_compress_overwrite(
3009 page->index, &fsdata);
3015 !f2fs_compress_write_end(inode,
3016 fsdata, page->index,
3026 /* give a priority to WB_SYNC threads */
3027 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3028 wbc->sync_mode == WB_SYNC_NONE) {
3032 #ifdef CONFIG_F2FS_FS_COMPRESSION
3035 done_index = page->index;
3039 if (unlikely(page->mapping != mapping)) {
3045 if (!PageDirty(page)) {
3046 /* someone wrote it for us */
3047 goto continue_unlock;
3050 if (PageWriteback(page)) {
3051 if (wbc->sync_mode != WB_SYNC_NONE)
3052 f2fs_wait_on_page_writeback(page,
3055 goto continue_unlock;
3058 if (!clear_page_dirty_for_io(page))
3059 goto continue_unlock;
3061 #ifdef CONFIG_F2FS_FS_COMPRESSION
3062 if (f2fs_compressed_file(inode)) {
3064 f2fs_compress_ctx_add_page(&cc, page);
3068 ret = f2fs_write_single_data_page(page, &submitted,
3069 &bio, &last_block, wbc, io_type, 0);
3070 if (ret == AOP_WRITEPAGE_ACTIVATE)
3072 #ifdef CONFIG_F2FS_FS_COMPRESSION
3075 nwritten += submitted;
3076 wbc->nr_to_write -= submitted;
3078 if (unlikely(ret)) {
3080 * keep nr_to_write, since vfs uses this to
3081 * get # of written pages.
3083 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3086 } else if (ret == -EAGAIN) {
3088 if (wbc->sync_mode == WB_SYNC_ALL) {
3090 congestion_wait(BLK_RW_ASYNC,
3091 DEFAULT_IO_TIMEOUT);
3096 done_index = page->index + 1;
3101 if (wbc->nr_to_write <= 0 &&
3102 wbc->sync_mode == WB_SYNC_NONE) {
3110 pagevec_release(&pvec);
3113 #ifdef CONFIG_F2FS_FS_COMPRESSION
3114 /* flush remained pages in compress cluster */
3115 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3116 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3117 nwritten += submitted;
3118 wbc->nr_to_write -= submitted;
3124 if (f2fs_compressed_file(inode))
3125 f2fs_destroy_compress_ctx(&cc);
3132 if (wbc->range_cyclic && !done)
3134 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3135 mapping->writeback_index = done_index;
3138 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3140 /* submit cached bio of IPU write */
3142 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3147 static inline bool __should_serialize_io(struct inode *inode,
3148 struct writeback_control *wbc)
3150 /* to avoid deadlock in path of data flush */
3151 if (F2FS_I(inode)->cp_task)
3154 if (!S_ISREG(inode->i_mode))
3156 if (IS_NOQUOTA(inode))
3159 if (f2fs_need_compress_data(inode))
3161 if (wbc->sync_mode != WB_SYNC_ALL)
3163 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3168 static int __f2fs_write_data_pages(struct address_space *mapping,
3169 struct writeback_control *wbc,
3170 enum iostat_type io_type)
3172 struct inode *inode = mapping->host;
3173 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3174 struct blk_plug plug;
3176 bool locked = false;
3178 /* deal with chardevs and other special file */
3179 if (!mapping->a_ops->writepage)
3182 /* skip writing if there is no dirty page in this inode */
3183 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3186 /* during POR, we don't need to trigger writepage at all. */
3187 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3190 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3191 wbc->sync_mode == WB_SYNC_NONE &&
3192 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3193 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3196 /* skip writing during file defragment */
3197 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3200 trace_f2fs_writepages(mapping->host, wbc, DATA);
3202 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3203 if (wbc->sync_mode == WB_SYNC_ALL)
3204 atomic_inc(&sbi->wb_sync_req[DATA]);
3205 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3208 if (__should_serialize_io(inode, wbc)) {
3209 mutex_lock(&sbi->writepages);
3213 blk_start_plug(&plug);
3214 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3215 blk_finish_plug(&plug);
3218 mutex_unlock(&sbi->writepages);
3220 if (wbc->sync_mode == WB_SYNC_ALL)
3221 atomic_dec(&sbi->wb_sync_req[DATA]);
3223 * if some pages were truncated, we cannot guarantee its mapping->host
3224 * to detect pending bios.
3227 f2fs_remove_dirty_inode(inode);
3231 wbc->pages_skipped += get_dirty_pages(inode);
3232 trace_f2fs_writepages(mapping->host, wbc, DATA);
3236 static int f2fs_write_data_pages(struct address_space *mapping,
3237 struct writeback_control *wbc)
3239 struct inode *inode = mapping->host;
3241 return __f2fs_write_data_pages(mapping, wbc,
3242 F2FS_I(inode)->cp_task == current ?
3243 FS_CP_DATA_IO : FS_DATA_IO);
3246 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3248 struct inode *inode = mapping->host;
3249 loff_t i_size = i_size_read(inode);
3251 if (IS_NOQUOTA(inode))
3254 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3255 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3256 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3257 down_write(&F2FS_I(inode)->i_mmap_sem);
3259 truncate_pagecache(inode, i_size);
3260 f2fs_truncate_blocks(inode, i_size, true);
3262 up_write(&F2FS_I(inode)->i_mmap_sem);
3263 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3267 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3268 struct page *page, loff_t pos, unsigned len,
3269 block_t *blk_addr, bool *node_changed)
3271 struct inode *inode = page->mapping->host;
3272 pgoff_t index = page->index;
3273 struct dnode_of_data dn;
3275 bool locked = false;
3276 struct extent_info ei = {0,0,0};
3281 * we already allocated all the blocks, so we don't need to get
3282 * the block addresses when there is no need to fill the page.
3284 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3285 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3286 !f2fs_verity_in_progress(inode))
3289 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3290 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3291 flag = F2FS_GET_BLOCK_DEFAULT;
3293 flag = F2FS_GET_BLOCK_PRE_AIO;
3295 if (f2fs_has_inline_data(inode) ||
3296 (pos & PAGE_MASK) >= i_size_read(inode)) {
3297 f2fs_do_map_lock(sbi, flag, true);
3302 /* check inline_data */
3303 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3304 if (IS_ERR(ipage)) {
3305 err = PTR_ERR(ipage);
3309 set_new_dnode(&dn, inode, ipage, ipage, 0);
3311 if (f2fs_has_inline_data(inode)) {
3312 if (pos + len <= MAX_INLINE_DATA(inode)) {
3313 f2fs_do_read_inline_data(page, ipage);
3314 set_inode_flag(inode, FI_DATA_EXIST);
3316 set_inline_node(ipage);
3318 err = f2fs_convert_inline_page(&dn, page);
3321 if (dn.data_blkaddr == NULL_ADDR)
3322 err = f2fs_get_block(&dn, index);
3324 } else if (locked) {
3325 err = f2fs_get_block(&dn, index);
3327 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3328 dn.data_blkaddr = ei.blk + index - ei.fofs;
3331 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3332 if (err || dn.data_blkaddr == NULL_ADDR) {
3333 f2fs_put_dnode(&dn);
3334 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3336 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3343 /* convert_inline_page can make node_changed */
3344 *blk_addr = dn.data_blkaddr;
3345 *node_changed = dn.node_changed;
3347 f2fs_put_dnode(&dn);
3350 f2fs_do_map_lock(sbi, flag, false);
3354 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3355 loff_t pos, unsigned len, unsigned flags,
3356 struct page **pagep, void **fsdata)
3358 struct inode *inode = mapping->host;
3359 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3360 struct page *page = NULL;
3361 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3362 bool need_balance = false, drop_atomic = false;
3363 block_t blkaddr = NULL_ADDR;
3366 trace_f2fs_write_begin(inode, pos, len, flags);
3368 if (!f2fs_is_checkpoint_ready(sbi)) {
3373 if ((f2fs_is_atomic_file(inode) &&
3374 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3375 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3382 * We should check this at this moment to avoid deadlock on inode page
3383 * and #0 page. The locking rule for inline_data conversion should be:
3384 * lock_page(page #0) -> lock_page(inode_page)
3387 err = f2fs_convert_inline_inode(inode);
3392 #ifdef CONFIG_F2FS_FS_COMPRESSION
3393 if (f2fs_compressed_file(inode)) {
3398 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3411 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3412 * wait_for_stable_page. Will wait that below with our IO control.
3414 page = f2fs_pagecache_get_page(mapping, index,
3415 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3421 /* TODO: cluster can be compressed due to race with .writepage */
3425 err = prepare_write_begin(sbi, page, pos, len,
3426 &blkaddr, &need_balance);
3430 if (need_balance && !IS_NOQUOTA(inode) &&
3431 has_not_enough_free_secs(sbi, 0, 0)) {
3433 f2fs_balance_fs(sbi, true);
3435 if (page->mapping != mapping) {
3436 /* The page got truncated from under us */
3437 f2fs_put_page(page, 1);
3442 f2fs_wait_on_page_writeback(page, DATA, false, true);
3444 if (len == PAGE_SIZE || PageUptodate(page))
3447 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3448 !f2fs_verity_in_progress(inode)) {
3449 zero_user_segment(page, len, PAGE_SIZE);
3453 if (blkaddr == NEW_ADDR) {
3454 zero_user_segment(page, 0, PAGE_SIZE);
3455 SetPageUptodate(page);
3457 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3458 DATA_GENERIC_ENHANCE_READ)) {
3459 err = -EFSCORRUPTED;
3462 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3467 if (unlikely(page->mapping != mapping)) {
3468 f2fs_put_page(page, 1);
3471 if (unlikely(!PageUptodate(page))) {
3479 f2fs_put_page(page, 1);
3480 f2fs_write_failed(mapping, pos + len);
3482 f2fs_drop_inmem_pages_all(sbi, false);
3486 static int f2fs_write_end(struct file *file,
3487 struct address_space *mapping,
3488 loff_t pos, unsigned len, unsigned copied,
3489 struct page *page, void *fsdata)
3491 struct inode *inode = page->mapping->host;
3493 trace_f2fs_write_end(inode, pos, len, copied);
3496 * This should be come from len == PAGE_SIZE, and we expect copied
3497 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3498 * let generic_perform_write() try to copy data again through copied=0.
3500 if (!PageUptodate(page)) {
3501 if (unlikely(copied != len))
3504 SetPageUptodate(page);
3507 #ifdef CONFIG_F2FS_FS_COMPRESSION
3508 /* overwrite compressed file */
3509 if (f2fs_compressed_file(inode) && fsdata) {
3510 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3511 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3513 if (pos + copied > i_size_read(inode) &&
3514 !f2fs_verity_in_progress(inode))
3515 f2fs_i_size_write(inode, pos + copied);
3523 set_page_dirty(page);
3525 if (pos + copied > i_size_read(inode) &&
3526 !f2fs_verity_in_progress(inode))
3527 f2fs_i_size_write(inode, pos + copied);
3529 f2fs_put_page(page, 1);
3530 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3534 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3537 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3538 unsigned blkbits = i_blkbits;
3539 unsigned blocksize_mask = (1 << blkbits) - 1;
3540 unsigned long align = offset | iov_iter_alignment(iter);
3541 struct block_device *bdev = inode->i_sb->s_bdev;
3543 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3546 if (align & blocksize_mask) {
3548 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3549 blocksize_mask = (1 << blkbits) - 1;
3550 if (align & blocksize_mask)
3557 static void f2fs_dio_end_io(struct bio *bio)
3559 struct f2fs_private_dio *dio = bio->bi_private;
3561 dec_page_count(F2FS_I_SB(dio->inode),
3562 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3564 bio->bi_private = dio->orig_private;
3565 bio->bi_end_io = dio->orig_end_io;
3572 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3575 struct f2fs_private_dio *dio;
3576 bool write = (bio_op(bio) == REQ_OP_WRITE);
3578 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3579 sizeof(struct f2fs_private_dio), GFP_NOFS);
3584 dio->orig_end_io = bio->bi_end_io;
3585 dio->orig_private = bio->bi_private;
3588 bio->bi_end_io = f2fs_dio_end_io;
3589 bio->bi_private = dio;
3591 inc_page_count(F2FS_I_SB(inode),
3592 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3597 bio->bi_status = BLK_STS_IOERR;
3601 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3603 struct address_space *mapping = iocb->ki_filp->f_mapping;
3604 struct inode *inode = mapping->host;
3605 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3606 struct f2fs_inode_info *fi = F2FS_I(inode);
3607 size_t count = iov_iter_count(iter);
3608 loff_t offset = iocb->ki_pos;
3609 int rw = iov_iter_rw(iter);
3611 enum rw_hint hint = iocb->ki_hint;
3612 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3615 err = check_direct_IO(inode, iter, offset);
3617 return err < 0 ? err : 0;
3619 if (f2fs_force_buffered_io(inode, iocb, iter))
3622 do_opu = allow_outplace_dio(inode, iocb, iter);
3624 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3626 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3627 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3629 if (iocb->ki_flags & IOCB_NOWAIT) {
3630 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3631 iocb->ki_hint = hint;
3635 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3636 up_read(&fi->i_gc_rwsem[rw]);
3637 iocb->ki_hint = hint;
3642 down_read(&fi->i_gc_rwsem[rw]);
3644 down_read(&fi->i_gc_rwsem[READ]);
3647 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3648 iter, rw == WRITE ? get_data_block_dio_write :
3649 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3650 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3654 up_read(&fi->i_gc_rwsem[READ]);
3656 up_read(&fi->i_gc_rwsem[rw]);
3659 if (whint_mode == WHINT_MODE_OFF)
3660 iocb->ki_hint = hint;
3662 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3665 set_inode_flag(inode, FI_UPDATE_WRITE);
3666 } else if (err == -EIOCBQUEUED) {
3667 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3668 count - iov_iter_count(iter));
3669 } else if (err < 0) {
3670 f2fs_write_failed(mapping, offset + count);
3674 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3675 else if (err == -EIOCBQUEUED)
3676 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3677 count - iov_iter_count(iter));
3681 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3686 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3687 unsigned int length)
3689 struct inode *inode = page->mapping->host;
3690 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3692 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3693 (offset % PAGE_SIZE || length != PAGE_SIZE))
3696 if (PageDirty(page)) {
3697 if (inode->i_ino == F2FS_META_INO(sbi)) {
3698 dec_page_count(sbi, F2FS_DIRTY_META);
3699 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3700 dec_page_count(sbi, F2FS_DIRTY_NODES);
3702 inode_dec_dirty_pages(inode);
3703 f2fs_remove_dirty_inode(inode);
3707 clear_cold_data(page);
3709 if (IS_ATOMIC_WRITTEN_PAGE(page))
3710 return f2fs_drop_inmem_page(inode, page);
3712 f2fs_clear_page_private(page);
3715 int f2fs_release_page(struct page *page, gfp_t wait)
3717 /* If this is dirty page, keep PagePrivate */
3718 if (PageDirty(page))
3721 /* This is atomic written page, keep Private */
3722 if (IS_ATOMIC_WRITTEN_PAGE(page))
3725 clear_cold_data(page);
3726 f2fs_clear_page_private(page);
3730 static int f2fs_set_data_page_dirty(struct page *page)
3732 struct inode *inode = page_file_mapping(page)->host;
3734 trace_f2fs_set_page_dirty(page, DATA);
3736 if (!PageUptodate(page))
3737 SetPageUptodate(page);
3738 if (PageSwapCache(page))
3739 return __set_page_dirty_nobuffers(page);
3741 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3742 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3743 f2fs_register_inmem_page(inode, page);
3747 * Previously, this page has been registered, we just
3753 if (!PageDirty(page)) {
3754 __set_page_dirty_nobuffers(page);
3755 f2fs_update_dirty_page(inode, page);
3762 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3764 #ifdef CONFIG_F2FS_FS_COMPRESSION
3765 struct dnode_of_data dn;
3766 sector_t start_idx, blknr = 0;
3769 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3771 set_new_dnode(&dn, inode, NULL, NULL, 0);
3772 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3776 if (dn.data_blkaddr != COMPRESS_ADDR) {
3777 dn.ofs_in_node += block - start_idx;
3778 blknr = f2fs_data_blkaddr(&dn);
3779 if (!__is_valid_data_blkaddr(blknr))
3783 f2fs_put_dnode(&dn);
3791 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3793 struct inode *inode = mapping->host;
3796 if (f2fs_has_inline_data(inode))
3799 /* make sure allocating whole blocks */
3800 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3801 filemap_write_and_wait(mapping);
3803 /* Block number less than F2FS MAX BLOCKS */
3804 if (unlikely(block >= F2FS_I_SB(inode)->max_file_blocks))
3807 if (f2fs_compressed_file(inode)) {
3808 blknr = f2fs_bmap_compress(inode, block);
3810 struct f2fs_map_blocks map;
3812 memset(&map, 0, sizeof(map));
3815 map.m_next_pgofs = NULL;
3816 map.m_seg_type = NO_CHECK_TYPE;
3818 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3822 trace_f2fs_bmap(inode, block, blknr);
3826 #ifdef CONFIG_MIGRATION
3827 #include <linux/migrate.h>
3829 int f2fs_migrate_page(struct address_space *mapping,
3830 struct page *newpage, struct page *page, enum migrate_mode mode)
3832 int rc, extra_count;
3833 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3834 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3836 BUG_ON(PageWriteback(page));
3838 /* migrating an atomic written page is safe with the inmem_lock hold */
3839 if (atomic_written) {
3840 if (mode != MIGRATE_SYNC)
3842 if (!mutex_trylock(&fi->inmem_lock))
3846 /* one extra reference was held for atomic_write page */
3847 extra_count = atomic_written ? 1 : 0;
3848 rc = migrate_page_move_mapping(mapping, newpage,
3850 if (rc != MIGRATEPAGE_SUCCESS) {
3852 mutex_unlock(&fi->inmem_lock);
3856 if (atomic_written) {
3857 struct inmem_pages *cur;
3858 list_for_each_entry(cur, &fi->inmem_pages, list)
3859 if (cur->page == page) {
3860 cur->page = newpage;
3863 mutex_unlock(&fi->inmem_lock);
3868 if (PagePrivate(page)) {
3869 f2fs_set_page_private(newpage, page_private(page));
3870 f2fs_clear_page_private(page);
3873 if (mode != MIGRATE_SYNC_NO_COPY)
3874 migrate_page_copy(newpage, page);
3876 migrate_page_states(newpage, page);
3878 return MIGRATEPAGE_SUCCESS;
3883 static int check_swap_activate_fast(struct swap_info_struct *sis,
3884 struct file *swap_file, sector_t *span)
3886 struct address_space *mapping = swap_file->f_mapping;
3887 struct inode *inode = mapping->host;
3888 sector_t cur_lblock;
3889 sector_t last_lblock;
3891 sector_t lowest_pblock = -1;
3892 sector_t highest_pblock = 0;
3894 unsigned long nr_pblocks;
3899 * Map all the blocks into the extent list. This code doesn't try
3903 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3904 len = i_size_read(inode);
3906 while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3907 struct f2fs_map_blocks map;
3912 memset(&map, 0, sizeof(map));
3913 map.m_lblk = cur_lblock;
3914 map.m_len = bytes_to_blks(inode, len) - cur_lblock;
3915 map.m_next_pgofs = &next_pgofs;
3916 map.m_seg_type = NO_CHECK_TYPE;
3918 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3923 if (!(map.m_flags & F2FS_MAP_FLAGS))
3926 pblock = map.m_pblk;
3927 nr_pblocks = map.m_len;
3929 if (cur_lblock + nr_pblocks >= sis->max)
3930 nr_pblocks = sis->max - cur_lblock;
3932 if (cur_lblock) { /* exclude the header page */
3933 if (pblock < lowest_pblock)
3934 lowest_pblock = pblock;
3935 if (pblock + nr_pblocks - 1 > highest_pblock)
3936 highest_pblock = pblock + nr_pblocks - 1;
3940 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3942 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3946 cur_lblock += nr_pblocks;
3949 *span = 1 + highest_pblock - lowest_pblock;
3950 if (cur_lblock == 0)
3951 cur_lblock = 1; /* force Empty message */
3952 sis->max = cur_lblock;
3953 sis->pages = cur_lblock - 1;
3954 sis->highest_bit = cur_lblock - 1;
3958 pr_err("swapon: swapfile has holes\n");
3962 /* Copied from generic_swapfile_activate() to check any holes */
3963 static int check_swap_activate(struct swap_info_struct *sis,
3964 struct file *swap_file, sector_t *span)
3966 struct address_space *mapping = swap_file->f_mapping;
3967 struct inode *inode = mapping->host;
3968 unsigned blocks_per_page;
3969 unsigned long page_no;
3970 sector_t probe_block;
3971 sector_t last_block;
3972 sector_t lowest_block = -1;
3973 sector_t highest_block = 0;
3977 if (PAGE_SIZE == F2FS_BLKSIZE)
3978 return check_swap_activate_fast(sis, swap_file, span);
3980 blocks_per_page = bytes_to_blks(inode, PAGE_SIZE);
3983 * Map all the blocks into the extent list. This code doesn't try
3988 last_block = bytes_to_blks(inode, i_size_read(inode));
3989 while ((probe_block + blocks_per_page) <= last_block &&
3990 page_no < sis->max) {
3991 unsigned block_in_page;
3992 sector_t first_block;
3998 block = probe_block;
3999 err = bmap(inode, &block);
4002 first_block = block;
4005 * It must be PAGE_SIZE aligned on-disk
4007 if (first_block & (blocks_per_page - 1)) {
4012 for (block_in_page = 1; block_in_page < blocks_per_page;
4015 block = probe_block + block_in_page;
4016 err = bmap(inode, &block);
4021 if (block != first_block + block_in_page) {
4028 first_block >>= (PAGE_SHIFT - inode->i_blkbits);
4029 if (page_no) { /* exclude the header page */
4030 if (first_block < lowest_block)
4031 lowest_block = first_block;
4032 if (first_block > highest_block)
4033 highest_block = first_block;
4037 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4039 ret = add_swap_extent(sis, page_no, 1, first_block);
4044 probe_block += blocks_per_page;
4049 *span = 1 + highest_block - lowest_block;
4051 page_no = 1; /* force Empty message */
4053 sis->pages = page_no - 1;
4054 sis->highest_bit = page_no - 1;
4058 pr_err("swapon: swapfile has holes\n");
4062 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4065 struct inode *inode = file_inode(file);
4068 if (!S_ISREG(inode->i_mode))
4071 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4074 ret = f2fs_convert_inline_inode(inode);
4078 if (!f2fs_disable_compressed_file(inode))
4081 ret = check_swap_activate(sis, file, span);
4085 set_inode_flag(inode, FI_PIN_FILE);
4086 f2fs_precache_extents(inode);
4087 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4091 static void f2fs_swap_deactivate(struct file *file)
4093 struct inode *inode = file_inode(file);
4095 clear_inode_flag(inode, FI_PIN_FILE);
4098 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4104 static void f2fs_swap_deactivate(struct file *file)
4109 const struct address_space_operations f2fs_dblock_aops = {
4110 .readpage = f2fs_read_data_page,
4111 .readahead = f2fs_readahead,
4112 .writepage = f2fs_write_data_page,
4113 .writepages = f2fs_write_data_pages,
4114 .write_begin = f2fs_write_begin,
4115 .write_end = f2fs_write_end,
4116 .set_page_dirty = f2fs_set_data_page_dirty,
4117 .invalidatepage = f2fs_invalidate_page,
4118 .releasepage = f2fs_release_page,
4119 .direct_IO = f2fs_direct_IO,
4121 .swap_activate = f2fs_swap_activate,
4122 .swap_deactivate = f2fs_swap_deactivate,
4123 #ifdef CONFIG_MIGRATION
4124 .migratepage = f2fs_migrate_page,
4128 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4130 struct address_space *mapping = page_mapping(page);
4131 unsigned long flags;
4133 xa_lock_irqsave(&mapping->i_pages, flags);
4134 __xa_clear_mark(&mapping->i_pages, page_index(page),
4135 PAGECACHE_TAG_DIRTY);
4136 xa_unlock_irqrestore(&mapping->i_pages, flags);
4139 int __init f2fs_init_post_read_processing(void)
4141 bio_post_read_ctx_cache =
4142 kmem_cache_create("f2fs_bio_post_read_ctx",
4143 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4144 if (!bio_post_read_ctx_cache)
4146 bio_post_read_ctx_pool =
4147 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4148 bio_post_read_ctx_cache);
4149 if (!bio_post_read_ctx_pool)
4150 goto fail_free_cache;
4154 kmem_cache_destroy(bio_post_read_ctx_cache);
4159 void f2fs_destroy_post_read_processing(void)
4161 mempool_destroy(bio_post_read_ctx_pool);
4162 kmem_cache_destroy(bio_post_read_ctx_cache);
4165 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4167 if (!f2fs_sb_has_encrypt(sbi) &&
4168 !f2fs_sb_has_verity(sbi) &&
4169 !f2fs_sb_has_compression(sbi))
4172 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4173 WQ_UNBOUND | WQ_HIGHPRI,
4175 if (!sbi->post_read_wq)
4180 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4182 if (sbi->post_read_wq)
4183 destroy_workqueue(sbi->post_read_wq);
4186 int __init f2fs_init_bio_entry_cache(void)
4188 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4189 sizeof(struct bio_entry));
4190 if (!bio_entry_slab)
4195 void f2fs_destroy_bio_entry_cache(void)
4197 kmem_cache_destroy(bio_entry_slab);