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>
28 #include <trace/events/f2fs.h>
30 #define NUM_PREALLOC_POST_READ_CTXS 128
32 static struct kmem_cache *bio_post_read_ctx_cache;
33 static struct kmem_cache *bio_entry_slab;
34 static mempool_t *bio_post_read_ctx_pool;
35 static struct bio_set f2fs_bioset;
37 #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
39 int __init f2fs_init_bioset(void)
41 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
42 0, BIOSET_NEED_BVECS))
47 void f2fs_destroy_bioset(void)
49 bioset_exit(&f2fs_bioset);
52 static bool __is_cp_guaranteed(struct page *page)
54 struct address_space *mapping = page->mapping;
56 struct f2fs_sb_info *sbi;
61 inode = mapping->host;
62 sbi = F2FS_I_SB(inode);
64 if (inode->i_ino == F2FS_META_INO(sbi) ||
65 inode->i_ino == F2FS_NODE_INO(sbi) ||
66 S_ISDIR(inode->i_mode))
69 if (f2fs_is_compressed_page(page))
71 if ((S_ISREG(inode->i_mode) &&
72 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
73 page_private_gcing(page))
78 static enum count_type __read_io_type(struct page *page)
80 struct address_space *mapping = page_file_mapping(page);
83 struct inode *inode = mapping->host;
84 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
86 if (inode->i_ino == F2FS_META_INO(sbi))
89 if (inode->i_ino == F2FS_NODE_INO(sbi))
95 /* postprocessing steps for read bios */
96 enum bio_post_read_step {
97 #ifdef CONFIG_FS_ENCRYPTION
98 STEP_DECRYPT = 1 << 0,
100 STEP_DECRYPT = 0, /* compile out the decryption-related code */
102 #ifdef CONFIG_F2FS_FS_COMPRESSION
103 STEP_DECOMPRESS = 1 << 1,
105 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
107 #ifdef CONFIG_FS_VERITY
108 STEP_VERITY = 1 << 2,
110 STEP_VERITY = 0, /* compile out the verity-related code */
114 struct bio_post_read_ctx {
116 struct f2fs_sb_info *sbi;
117 struct work_struct work;
118 unsigned int enabled_steps;
122 static void f2fs_finish_read_bio(struct bio *bio)
125 struct bvec_iter_all iter_all;
128 * Update and unlock the bio's pagecache pages, and put the
129 * decompression context for any compressed pages.
131 bio_for_each_segment_all(bv, bio, iter_all) {
132 struct page *page = bv->bv_page;
134 if (f2fs_is_compressed_page(page)) {
136 f2fs_end_read_compressed_page(page, true, 0);
137 f2fs_put_page_dic(page);
141 /* PG_error was set if decryption or verity failed. */
142 if (bio->bi_status || PageError(page)) {
143 ClearPageUptodate(page);
144 /* will re-read again later */
145 ClearPageError(page);
147 SetPageUptodate(page);
149 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
154 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
158 static void f2fs_verify_bio(struct work_struct *work)
160 struct bio_post_read_ctx *ctx =
161 container_of(work, struct bio_post_read_ctx, work);
162 struct bio *bio = ctx->bio;
163 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
166 * fsverity_verify_bio() may call readpages() again, and while verity
167 * will be disabled for this, decryption and/or decompression may still
168 * be needed, resulting in another bio_post_read_ctx being allocated.
169 * So to prevent deadlocks we need to release the current ctx to the
170 * mempool first. This assumes that verity is the last post-read step.
172 mempool_free(ctx, bio_post_read_ctx_pool);
173 bio->bi_private = NULL;
176 * Verify the bio's pages with fs-verity. Exclude compressed pages,
177 * as those were handled separately by f2fs_end_read_compressed_page().
179 if (may_have_compressed_pages) {
181 struct bvec_iter_all iter_all;
183 bio_for_each_segment_all(bv, bio, iter_all) {
184 struct page *page = bv->bv_page;
186 if (!f2fs_is_compressed_page(page) &&
187 !PageError(page) && !fsverity_verify_page(page))
191 fsverity_verify_bio(bio);
194 f2fs_finish_read_bio(bio);
198 * If the bio's data needs to be verified with fs-verity, then enqueue the
199 * verity work for the bio. Otherwise finish the bio now.
201 * Note that to avoid deadlocks, the verity work can't be done on the
202 * decryption/decompression workqueue. This is because verifying the data pages
203 * can involve reading verity metadata pages from the file, and these verity
204 * metadata pages may be encrypted and/or compressed.
206 static void f2fs_verify_and_finish_bio(struct bio *bio)
208 struct bio_post_read_ctx *ctx = bio->bi_private;
210 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
211 INIT_WORK(&ctx->work, f2fs_verify_bio);
212 fsverity_enqueue_verify_work(&ctx->work);
214 f2fs_finish_read_bio(bio);
219 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
220 * remaining page was read by @ctx->bio.
222 * Note that a bio may span clusters (even a mix of compressed and uncompressed
223 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
224 * that the bio includes at least one compressed page. The actual decompression
225 * is done on a per-cluster basis, not a per-bio basis.
227 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
230 struct bvec_iter_all iter_all;
231 bool all_compressed = true;
232 block_t blkaddr = ctx->fs_blkaddr;
234 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
235 struct page *page = bv->bv_page;
237 /* PG_error was set if decryption failed. */
238 if (f2fs_is_compressed_page(page))
239 f2fs_end_read_compressed_page(page, PageError(page),
242 all_compressed = false;
248 * Optimization: if all the bio's pages are compressed, then scheduling
249 * the per-bio verity work is unnecessary, as verity will be fully
250 * handled at the compression cluster level.
253 ctx->enabled_steps &= ~STEP_VERITY;
256 static void f2fs_post_read_work(struct work_struct *work)
258 struct bio_post_read_ctx *ctx =
259 container_of(work, struct bio_post_read_ctx, work);
261 if (ctx->enabled_steps & STEP_DECRYPT)
262 fscrypt_decrypt_bio(ctx->bio);
264 if (ctx->enabled_steps & STEP_DECOMPRESS)
265 f2fs_handle_step_decompress(ctx);
267 f2fs_verify_and_finish_bio(ctx->bio);
270 static void f2fs_read_end_io(struct bio *bio)
272 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
273 struct bio_post_read_ctx *ctx = bio->bi_private;
275 if (time_to_inject(sbi, FAULT_READ_IO)) {
276 f2fs_show_injection_info(sbi, FAULT_READ_IO);
277 bio->bi_status = BLK_STS_IOERR;
280 if (bio->bi_status) {
281 f2fs_finish_read_bio(bio);
285 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
286 INIT_WORK(&ctx->work, f2fs_post_read_work);
287 queue_work(ctx->sbi->post_read_wq, &ctx->work);
289 f2fs_verify_and_finish_bio(bio);
293 static void f2fs_write_end_io(struct bio *bio)
295 struct f2fs_sb_info *sbi = bio->bi_private;
296 struct bio_vec *bvec;
297 struct bvec_iter_all iter_all;
299 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
300 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
301 bio->bi_status = BLK_STS_IOERR;
304 bio_for_each_segment_all(bvec, bio, iter_all) {
305 struct page *page = bvec->bv_page;
306 enum count_type type = WB_DATA_TYPE(page);
308 if (page_private_dummy(page)) {
309 clear_page_private_dummy(page);
311 mempool_free(page, sbi->write_io_dummy);
313 if (unlikely(bio->bi_status))
314 f2fs_stop_checkpoint(sbi, true);
318 fscrypt_finalize_bounce_page(&page);
320 #ifdef CONFIG_F2FS_FS_COMPRESSION
321 if (f2fs_is_compressed_page(page)) {
322 f2fs_compress_write_end_io(bio, page);
327 if (unlikely(bio->bi_status)) {
328 mapping_set_error(page->mapping, -EIO);
329 if (type == F2FS_WB_CP_DATA)
330 f2fs_stop_checkpoint(sbi, true);
333 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
334 page->index != nid_of_node(page));
336 dec_page_count(sbi, type);
337 if (f2fs_in_warm_node_list(sbi, page))
338 f2fs_del_fsync_node_entry(sbi, page);
339 clear_page_private_gcing(page);
340 end_page_writeback(page);
342 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
343 wq_has_sleeper(&sbi->cp_wait))
344 wake_up(&sbi->cp_wait);
349 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
350 block_t blk_addr, struct bio *bio)
352 struct block_device *bdev = sbi->sb->s_bdev;
355 if (f2fs_is_multi_device(sbi)) {
356 for (i = 0; i < sbi->s_ndevs; i++) {
357 if (FDEV(i).start_blk <= blk_addr &&
358 FDEV(i).end_blk >= blk_addr) {
359 blk_addr -= FDEV(i).start_blk;
366 bio_set_dev(bio, bdev);
367 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
372 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
376 if (!f2fs_is_multi_device(sbi))
379 for (i = 0; i < sbi->s_ndevs; i++)
380 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
385 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
387 struct f2fs_sb_info *sbi = fio->sbi;
390 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
392 f2fs_target_device(sbi, fio->new_blkaddr, bio);
393 if (is_read_io(fio->op)) {
394 bio->bi_end_io = f2fs_read_end_io;
395 bio->bi_private = NULL;
397 bio->bi_end_io = f2fs_write_end_io;
398 bio->bi_private = sbi;
399 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
400 fio->type, fio->temp);
403 wbc_init_bio(fio->io_wbc, bio);
408 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
410 const struct f2fs_io_info *fio,
414 * The f2fs garbage collector sets ->encrypted_page when it wants to
415 * read/write raw data without encryption.
417 if (!fio || !fio->encrypted_page)
418 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
421 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
423 const struct f2fs_io_info *fio)
426 * The f2fs garbage collector sets ->encrypted_page when it wants to
427 * read/write raw data without encryption.
429 if (fio && fio->encrypted_page)
430 return !bio_has_crypt_ctx(bio);
432 return fscrypt_mergeable_bio(bio, inode, next_idx);
435 static inline void __submit_bio(struct f2fs_sb_info *sbi,
436 struct bio *bio, enum page_type type)
438 if (!is_read_io(bio_op(bio))) {
441 if (type != DATA && type != NODE)
444 if (f2fs_lfs_mode(sbi) && current->plug)
445 blk_finish_plug(current->plug);
447 if (!F2FS_IO_ALIGNED(sbi))
450 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
451 start %= F2FS_IO_SIZE(sbi);
456 /* fill dummy pages */
457 for (; start < F2FS_IO_SIZE(sbi); start++) {
459 mempool_alloc(sbi->write_io_dummy,
460 GFP_NOIO | __GFP_NOFAIL);
461 f2fs_bug_on(sbi, !page);
465 zero_user_segment(page, 0, PAGE_SIZE);
466 set_page_private_dummy(page);
468 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
472 * In the NODE case, we lose next block address chain. So, we
473 * need to do checkpoint in f2fs_sync_file.
476 set_sbi_flag(sbi, SBI_NEED_CP);
479 if (is_read_io(bio_op(bio)))
480 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
482 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
486 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
487 struct bio *bio, enum page_type type)
489 __submit_bio(sbi, bio, type);
492 static void __attach_io_flag(struct f2fs_io_info *fio)
494 struct f2fs_sb_info *sbi = fio->sbi;
495 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
496 unsigned int io_flag, fua_flag, meta_flag;
498 if (fio->type == DATA)
499 io_flag = sbi->data_io_flag;
500 else if (fio->type == NODE)
501 io_flag = sbi->node_io_flag;
505 fua_flag = io_flag & temp_mask;
506 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
509 * data/node io flag bits per temp:
510 * REQ_META | REQ_FUA |
511 * 5 | 4 | 3 | 2 | 1 | 0 |
512 * Cold | Warm | Hot | Cold | Warm | Hot |
514 if ((1 << fio->temp) & meta_flag)
515 fio->op_flags |= REQ_META;
516 if ((1 << fio->temp) & fua_flag)
517 fio->op_flags |= REQ_FUA;
520 static void __submit_merged_bio(struct f2fs_bio_info *io)
522 struct f2fs_io_info *fio = &io->fio;
527 __attach_io_flag(fio);
528 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
530 if (is_read_io(fio->op))
531 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
533 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
535 __submit_bio(io->sbi, io->bio, fio->type);
539 static bool __has_merged_page(struct bio *bio, struct inode *inode,
540 struct page *page, nid_t ino)
542 struct bio_vec *bvec;
543 struct bvec_iter_all iter_all;
548 if (!inode && !page && !ino)
551 bio_for_each_segment_all(bvec, bio, iter_all) {
552 struct page *target = bvec->bv_page;
554 if (fscrypt_is_bounce_page(target)) {
555 target = fscrypt_pagecache_page(target);
559 if (f2fs_is_compressed_page(target)) {
560 target = f2fs_compress_control_page(target);
565 if (inode && inode == target->mapping->host)
567 if (page && page == target)
569 if (ino && ino == ino_of_node(target))
576 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
577 enum page_type type, enum temp_type temp)
579 enum page_type btype = PAGE_TYPE_OF_BIO(type);
580 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
582 down_write(&io->io_rwsem);
584 /* change META to META_FLUSH in the checkpoint procedure */
585 if (type >= META_FLUSH) {
586 io->fio.type = META_FLUSH;
587 io->fio.op = REQ_OP_WRITE;
588 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
589 if (!test_opt(sbi, NOBARRIER))
590 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
592 __submit_merged_bio(io);
593 up_write(&io->io_rwsem);
596 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
597 struct inode *inode, struct page *page,
598 nid_t ino, enum page_type type, bool force)
603 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
605 enum page_type btype = PAGE_TYPE_OF_BIO(type);
606 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
608 down_read(&io->io_rwsem);
609 ret = __has_merged_page(io->bio, inode, page, ino);
610 up_read(&io->io_rwsem);
613 __f2fs_submit_merged_write(sbi, type, temp);
615 /* TODO: use HOT temp only for meta pages now. */
621 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
623 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
626 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
627 struct inode *inode, struct page *page,
628 nid_t ino, enum page_type type)
630 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
633 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
635 f2fs_submit_merged_write(sbi, DATA);
636 f2fs_submit_merged_write(sbi, NODE);
637 f2fs_submit_merged_write(sbi, META);
641 * Fill the locked page with data located in the block address.
642 * A caller needs to unlock the page on failure.
644 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
647 struct page *page = fio->encrypted_page ?
648 fio->encrypted_page : fio->page;
650 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
651 fio->is_por ? META_POR : (__is_meta_io(fio) ?
652 META_GENERIC : DATA_GENERIC_ENHANCE)))
653 return -EFSCORRUPTED;
655 trace_f2fs_submit_page_bio(page, fio);
657 /* Allocate a new bio */
658 bio = __bio_alloc(fio, 1);
660 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
661 fio->page->index, fio, GFP_NOIO);
663 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
668 if (fio->io_wbc && !is_read_io(fio->op))
669 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
671 __attach_io_flag(fio);
672 bio_set_op_attrs(bio, fio->op, fio->op_flags);
674 inc_page_count(fio->sbi, is_read_io(fio->op) ?
675 __read_io_type(page): WB_DATA_TYPE(fio->page));
677 __submit_bio(fio->sbi, bio, fio->type);
681 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
682 block_t last_blkaddr, block_t cur_blkaddr)
684 if (unlikely(sbi->max_io_bytes &&
685 bio->bi_iter.bi_size >= sbi->max_io_bytes))
687 if (last_blkaddr + 1 != cur_blkaddr)
689 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
692 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
693 struct f2fs_io_info *fio)
695 if (io->fio.op != fio->op)
697 return io->fio.op_flags == fio->op_flags;
700 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
701 struct f2fs_bio_info *io,
702 struct f2fs_io_info *fio,
703 block_t last_blkaddr,
706 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
707 unsigned int filled_blocks =
708 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
709 unsigned int io_size = F2FS_IO_SIZE(sbi);
710 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
712 /* IOs in bio is aligned and left space of vectors is not enough */
713 if (!(filled_blocks % io_size) && left_vecs < io_size)
716 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
718 return io_type_is_mergeable(io, fio);
721 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
722 struct page *page, enum temp_type temp)
724 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
725 struct bio_entry *be;
727 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
731 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
734 down_write(&io->bio_list_lock);
735 list_add_tail(&be->list, &io->bio_list);
736 up_write(&io->bio_list_lock);
739 static void del_bio_entry(struct bio_entry *be)
742 kmem_cache_free(bio_entry_slab, be);
745 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
748 struct f2fs_sb_info *sbi = fio->sbi;
753 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
754 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
755 struct list_head *head = &io->bio_list;
756 struct bio_entry *be;
758 down_write(&io->bio_list_lock);
759 list_for_each_entry(be, head, list) {
765 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
768 if (f2fs_crypt_mergeable_bio(*bio,
769 fio->page->mapping->host,
770 fio->page->index, fio) &&
771 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
777 /* page can't be merged into bio; submit the bio */
779 __submit_bio(sbi, *bio, DATA);
782 up_write(&io->bio_list_lock);
793 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
794 struct bio **bio, struct page *page)
798 struct bio *target = bio ? *bio : NULL;
800 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
801 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
802 struct list_head *head = &io->bio_list;
803 struct bio_entry *be;
805 if (list_empty(head))
808 down_read(&io->bio_list_lock);
809 list_for_each_entry(be, head, list) {
811 found = (target == be->bio);
813 found = __has_merged_page(be->bio, NULL,
818 up_read(&io->bio_list_lock);
825 down_write(&io->bio_list_lock);
826 list_for_each_entry(be, head, list) {
828 found = (target == be->bio);
830 found = __has_merged_page(be->bio, NULL,
838 up_write(&io->bio_list_lock);
842 __submit_bio(sbi, target, DATA);
849 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
851 struct bio *bio = *fio->bio;
852 struct page *page = fio->encrypted_page ?
853 fio->encrypted_page : fio->page;
855 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
856 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
857 return -EFSCORRUPTED;
859 trace_f2fs_submit_page_bio(page, fio);
861 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
863 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
866 bio = __bio_alloc(fio, BIO_MAX_VECS);
867 __attach_io_flag(fio);
868 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
869 fio->page->index, fio, GFP_NOIO);
870 bio_set_op_attrs(bio, fio->op, fio->op_flags);
872 add_bio_entry(fio->sbi, bio, page, fio->temp);
874 if (add_ipu_page(fio, &bio, page))
879 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
881 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
883 *fio->last_block = fio->new_blkaddr;
889 void f2fs_submit_page_write(struct f2fs_io_info *fio)
891 struct f2fs_sb_info *sbi = fio->sbi;
892 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
893 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
894 struct page *bio_page;
896 f2fs_bug_on(sbi, is_read_io(fio->op));
898 down_write(&io->io_rwsem);
901 spin_lock(&io->io_lock);
902 if (list_empty(&io->io_list)) {
903 spin_unlock(&io->io_lock);
906 fio = list_first_entry(&io->io_list,
907 struct f2fs_io_info, list);
908 list_del(&fio->list);
909 spin_unlock(&io->io_lock);
912 verify_fio_blkaddr(fio);
914 if (fio->encrypted_page)
915 bio_page = fio->encrypted_page;
916 else if (fio->compressed_page)
917 bio_page = fio->compressed_page;
919 bio_page = fio->page;
921 /* set submitted = true as a return value */
922 fio->submitted = true;
924 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
927 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
929 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
930 bio_page->index, fio)))
931 __submit_merged_bio(io);
933 if (io->bio == NULL) {
934 if (F2FS_IO_ALIGNED(sbi) &&
935 (fio->type == DATA || fio->type == NODE) &&
936 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
937 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
941 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
942 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
943 bio_page->index, fio, GFP_NOIO);
947 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
948 __submit_merged_bio(io);
953 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
955 io->last_block_in_bio = fio->new_blkaddr;
957 trace_f2fs_submit_page_write(fio->page, fio);
962 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
963 !f2fs_is_checkpoint_ready(sbi))
964 __submit_merged_bio(io);
965 up_write(&io->io_rwsem);
968 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
969 unsigned nr_pages, unsigned op_flag,
970 pgoff_t first_idx, bool for_write)
972 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
974 struct bio_post_read_ctx *ctx;
975 unsigned int post_read_steps = 0;
977 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
978 bio_max_segs(nr_pages), &f2fs_bioset);
980 return ERR_PTR(-ENOMEM);
982 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
984 f2fs_target_device(sbi, blkaddr, bio);
985 bio->bi_end_io = f2fs_read_end_io;
986 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
988 if (fscrypt_inode_uses_fs_layer_crypto(inode))
989 post_read_steps |= STEP_DECRYPT;
991 if (f2fs_need_verity(inode, first_idx))
992 post_read_steps |= STEP_VERITY;
995 * STEP_DECOMPRESS is handled specially, since a compressed file might
996 * contain both compressed and uncompressed clusters. We'll allocate a
997 * bio_post_read_ctx if the file is compressed, but the caller is
998 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1001 if (post_read_steps || f2fs_compressed_file(inode)) {
1002 /* Due to the mempool, this never fails. */
1003 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1006 ctx->enabled_steps = post_read_steps;
1007 ctx->fs_blkaddr = blkaddr;
1008 bio->bi_private = ctx;
1014 /* This can handle encryption stuffs */
1015 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1016 block_t blkaddr, int op_flags, bool for_write)
1018 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1021 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1022 page->index, for_write);
1024 return PTR_ERR(bio);
1026 /* wait for GCed page writeback via META_MAPPING */
1027 f2fs_wait_on_block_writeback(inode, blkaddr);
1029 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1033 ClearPageError(page);
1034 inc_page_count(sbi, F2FS_RD_DATA);
1035 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1036 __submit_bio(sbi, bio, DATA);
1040 static void __set_data_blkaddr(struct dnode_of_data *dn)
1042 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1046 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1047 base = get_extra_isize(dn->inode);
1049 /* Get physical address of data block */
1050 addr_array = blkaddr_in_node(rn);
1051 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1055 * Lock ordering for the change of data block address:
1058 * update block addresses in the node page
1060 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1062 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1063 __set_data_blkaddr(dn);
1064 if (set_page_dirty(dn->node_page))
1065 dn->node_changed = true;
1068 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1070 dn->data_blkaddr = blkaddr;
1071 f2fs_set_data_blkaddr(dn);
1072 f2fs_update_extent_cache(dn);
1075 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
1076 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1078 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1084 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1086 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1089 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1090 dn->ofs_in_node, count);
1092 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1094 for (; count > 0; dn->ofs_in_node++) {
1095 block_t blkaddr = f2fs_data_blkaddr(dn);
1097 if (blkaddr == NULL_ADDR) {
1098 dn->data_blkaddr = NEW_ADDR;
1099 __set_data_blkaddr(dn);
1104 if (set_page_dirty(dn->node_page))
1105 dn->node_changed = true;
1109 /* Should keep dn->ofs_in_node unchanged */
1110 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1112 unsigned int ofs_in_node = dn->ofs_in_node;
1115 ret = f2fs_reserve_new_blocks(dn, 1);
1116 dn->ofs_in_node = ofs_in_node;
1120 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1122 bool need_put = dn->inode_page ? false : true;
1125 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1129 if (dn->data_blkaddr == NULL_ADDR)
1130 err = f2fs_reserve_new_block(dn);
1131 if (err || need_put)
1136 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1138 struct extent_info ei = {0, };
1139 struct inode *inode = dn->inode;
1141 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1142 dn->data_blkaddr = ei.blk + index - ei.fofs;
1146 return f2fs_reserve_block(dn, index);
1149 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1150 int op_flags, bool for_write)
1152 struct address_space *mapping = inode->i_mapping;
1153 struct dnode_of_data dn;
1155 struct extent_info ei = {0, };
1158 page = f2fs_grab_cache_page(mapping, index, for_write);
1160 return ERR_PTR(-ENOMEM);
1162 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1163 dn.data_blkaddr = ei.blk + index - ei.fofs;
1164 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1165 DATA_GENERIC_ENHANCE_READ)) {
1166 err = -EFSCORRUPTED;
1172 set_new_dnode(&dn, inode, NULL, NULL, 0);
1173 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1176 f2fs_put_dnode(&dn);
1178 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1182 if (dn.data_blkaddr != NEW_ADDR &&
1183 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1185 DATA_GENERIC_ENHANCE)) {
1186 err = -EFSCORRUPTED;
1190 if (PageUptodate(page)) {
1196 * A new dentry page is allocated but not able to be written, since its
1197 * new inode page couldn't be allocated due to -ENOSPC.
1198 * In such the case, its blkaddr can be remained as NEW_ADDR.
1199 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1200 * f2fs_init_inode_metadata.
1202 if (dn.data_blkaddr == NEW_ADDR) {
1203 zero_user_segment(page, 0, PAGE_SIZE);
1204 if (!PageUptodate(page))
1205 SetPageUptodate(page);
1210 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1211 op_flags, for_write);
1217 f2fs_put_page(page, 1);
1218 return ERR_PTR(err);
1221 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1223 struct address_space *mapping = inode->i_mapping;
1226 page = find_get_page(mapping, index);
1227 if (page && PageUptodate(page))
1229 f2fs_put_page(page, 0);
1231 page = f2fs_get_read_data_page(inode, index, 0, false);
1235 if (PageUptodate(page))
1238 wait_on_page_locked(page);
1239 if (unlikely(!PageUptodate(page))) {
1240 f2fs_put_page(page, 0);
1241 return ERR_PTR(-EIO);
1247 * If it tries to access a hole, return an error.
1248 * Because, the callers, functions in dir.c and GC, should be able to know
1249 * whether this page exists or not.
1251 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1254 struct address_space *mapping = inode->i_mapping;
1257 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1261 /* wait for read completion */
1263 if (unlikely(page->mapping != mapping)) {
1264 f2fs_put_page(page, 1);
1267 if (unlikely(!PageUptodate(page))) {
1268 f2fs_put_page(page, 1);
1269 return ERR_PTR(-EIO);
1275 * Caller ensures that this data page is never allocated.
1276 * A new zero-filled data page is allocated in the page cache.
1278 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1280 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1281 * ipage should be released by this function.
1283 struct page *f2fs_get_new_data_page(struct inode *inode,
1284 struct page *ipage, pgoff_t index, bool new_i_size)
1286 struct address_space *mapping = inode->i_mapping;
1288 struct dnode_of_data dn;
1291 page = f2fs_grab_cache_page(mapping, index, true);
1294 * before exiting, we should make sure ipage will be released
1295 * if any error occur.
1297 f2fs_put_page(ipage, 1);
1298 return ERR_PTR(-ENOMEM);
1301 set_new_dnode(&dn, inode, ipage, NULL, 0);
1302 err = f2fs_reserve_block(&dn, index);
1304 f2fs_put_page(page, 1);
1305 return ERR_PTR(err);
1308 f2fs_put_dnode(&dn);
1310 if (PageUptodate(page))
1313 if (dn.data_blkaddr == NEW_ADDR) {
1314 zero_user_segment(page, 0, PAGE_SIZE);
1315 if (!PageUptodate(page))
1316 SetPageUptodate(page);
1318 f2fs_put_page(page, 1);
1320 /* if ipage exists, blkaddr should be NEW_ADDR */
1321 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1322 page = f2fs_get_lock_data_page(inode, index, true);
1327 if (new_i_size && i_size_read(inode) <
1328 ((loff_t)(index + 1) << PAGE_SHIFT))
1329 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1333 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1335 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1336 struct f2fs_summary sum;
1337 struct node_info ni;
1338 block_t old_blkaddr;
1342 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1345 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1349 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1350 if (dn->data_blkaddr != NULL_ADDR)
1353 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1357 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1358 old_blkaddr = dn->data_blkaddr;
1359 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1360 &sum, seg_type, NULL);
1361 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1362 invalidate_mapping_pages(META_MAPPING(sbi),
1363 old_blkaddr, old_blkaddr);
1364 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1366 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1369 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1370 * data from unwritten block via dio_read.
1375 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1377 struct inode *inode = file_inode(iocb->ki_filp);
1378 struct f2fs_map_blocks map;
1381 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1383 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1384 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1385 if (map.m_len > map.m_lblk)
1386 map.m_len -= map.m_lblk;
1390 map.m_next_pgofs = NULL;
1391 map.m_next_extent = NULL;
1392 map.m_seg_type = NO_CHECK_TYPE;
1393 map.m_may_create = true;
1396 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1397 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1398 F2FS_GET_BLOCK_PRE_AIO :
1399 F2FS_GET_BLOCK_PRE_DIO;
1402 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1403 err = f2fs_convert_inline_inode(inode);
1407 if (f2fs_has_inline_data(inode))
1410 flag = F2FS_GET_BLOCK_PRE_AIO;
1413 err = f2fs_map_blocks(inode, &map, 1, flag);
1414 if (map.m_len > 0 && err == -ENOSPC) {
1416 set_inode_flag(inode, FI_NO_PREALLOC);
1422 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1424 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1426 down_read(&sbi->node_change);
1428 up_read(&sbi->node_change);
1433 f2fs_unlock_op(sbi);
1438 * f2fs_map_blocks() tries to find or build mapping relationship which
1439 * maps continuous logical blocks to physical blocks, and return such
1440 * info via f2fs_map_blocks structure.
1442 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1443 int create, int flag)
1445 unsigned int maxblocks = map->m_len;
1446 struct dnode_of_data dn;
1447 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1448 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1449 pgoff_t pgofs, end_offset, end;
1450 int err = 0, ofs = 1;
1451 unsigned int ofs_in_node, last_ofs_in_node;
1453 struct extent_info ei = {0, };
1455 unsigned int start_pgofs;
1463 /* it only supports block size == page size */
1464 pgofs = (pgoff_t)map->m_lblk;
1465 end = pgofs + maxblocks;
1467 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1468 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1472 map->m_pblk = ei.blk + pgofs - ei.fofs;
1473 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1474 map->m_flags = F2FS_MAP_MAPPED;
1475 if (map->m_next_extent)
1476 *map->m_next_extent = pgofs + map->m_len;
1478 /* for hardware encryption, but to avoid potential issue in future */
1479 if (flag == F2FS_GET_BLOCK_DIO)
1480 f2fs_wait_on_block_writeback_range(inode,
1481 map->m_pblk, map->m_len);
1486 if (map->m_may_create)
1487 f2fs_do_map_lock(sbi, flag, true);
1489 /* When reading holes, we need its node page */
1490 set_new_dnode(&dn, inode, NULL, NULL, 0);
1491 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1493 if (flag == F2FS_GET_BLOCK_BMAP)
1495 if (err == -ENOENT) {
1497 if (map->m_next_pgofs)
1498 *map->m_next_pgofs =
1499 f2fs_get_next_page_offset(&dn, pgofs);
1500 if (map->m_next_extent)
1501 *map->m_next_extent =
1502 f2fs_get_next_page_offset(&dn, pgofs);
1507 start_pgofs = pgofs;
1509 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1510 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1513 blkaddr = f2fs_data_blkaddr(&dn);
1515 if (__is_valid_data_blkaddr(blkaddr) &&
1516 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1517 err = -EFSCORRUPTED;
1521 if (__is_valid_data_blkaddr(blkaddr)) {
1522 /* use out-place-update for driect IO under LFS mode */
1523 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1524 map->m_may_create) {
1525 err = __allocate_data_block(&dn, map->m_seg_type);
1528 blkaddr = dn.data_blkaddr;
1529 set_inode_flag(inode, FI_APPEND_WRITE);
1533 if (unlikely(f2fs_cp_error(sbi))) {
1537 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1538 if (blkaddr == NULL_ADDR) {
1540 last_ofs_in_node = dn.ofs_in_node;
1543 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1544 flag != F2FS_GET_BLOCK_DIO);
1545 err = __allocate_data_block(&dn,
1548 set_inode_flag(inode, FI_APPEND_WRITE);
1552 map->m_flags |= F2FS_MAP_NEW;
1553 blkaddr = dn.data_blkaddr;
1555 if (f2fs_compressed_file(inode) &&
1556 f2fs_sanity_check_cluster(&dn) &&
1557 (flag != F2FS_GET_BLOCK_FIEMAP ||
1558 IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1559 err = -EFSCORRUPTED;
1562 if (flag == F2FS_GET_BLOCK_BMAP) {
1566 if (flag == F2FS_GET_BLOCK_PRECACHE)
1568 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1569 blkaddr == NULL_ADDR) {
1570 if (map->m_next_pgofs)
1571 *map->m_next_pgofs = pgofs + 1;
1574 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1575 /* for defragment case */
1576 if (map->m_next_pgofs)
1577 *map->m_next_pgofs = pgofs + 1;
1583 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1586 if (map->m_len == 0) {
1587 /* preallocated unwritten block should be mapped for fiemap. */
1588 if (blkaddr == NEW_ADDR)
1589 map->m_flags |= F2FS_MAP_UNWRITTEN;
1590 map->m_flags |= F2FS_MAP_MAPPED;
1592 map->m_pblk = blkaddr;
1594 } else if ((map->m_pblk != NEW_ADDR &&
1595 blkaddr == (map->m_pblk + ofs)) ||
1596 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1597 flag == F2FS_GET_BLOCK_PRE_DIO) {
1608 /* preallocate blocks in batch for one dnode page */
1609 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1610 (pgofs == end || dn.ofs_in_node == end_offset)) {
1612 dn.ofs_in_node = ofs_in_node;
1613 err = f2fs_reserve_new_blocks(&dn, prealloc);
1617 map->m_len += dn.ofs_in_node - ofs_in_node;
1618 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1622 dn.ofs_in_node = end_offset;
1627 else if (dn.ofs_in_node < end_offset)
1630 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1631 if (map->m_flags & F2FS_MAP_MAPPED) {
1632 unsigned int ofs = start_pgofs - map->m_lblk;
1634 f2fs_update_extent_cache_range(&dn,
1635 start_pgofs, map->m_pblk + ofs,
1640 f2fs_put_dnode(&dn);
1642 if (map->m_may_create) {
1643 f2fs_do_map_lock(sbi, flag, false);
1644 f2fs_balance_fs(sbi, dn.node_changed);
1650 /* for hardware encryption, but to avoid potential issue in future */
1651 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1652 f2fs_wait_on_block_writeback_range(inode,
1653 map->m_pblk, map->m_len);
1655 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1656 if (map->m_flags & F2FS_MAP_MAPPED) {
1657 unsigned int ofs = start_pgofs - map->m_lblk;
1659 f2fs_update_extent_cache_range(&dn,
1660 start_pgofs, map->m_pblk + ofs,
1663 if (map->m_next_extent)
1664 *map->m_next_extent = pgofs + 1;
1666 f2fs_put_dnode(&dn);
1668 if (map->m_may_create) {
1669 f2fs_do_map_lock(sbi, flag, false);
1670 f2fs_balance_fs(sbi, dn.node_changed);
1673 trace_f2fs_map_blocks(inode, map, err);
1677 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1679 struct f2fs_map_blocks map;
1683 if (pos + len > i_size_read(inode))
1686 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1687 map.m_next_pgofs = NULL;
1688 map.m_next_extent = NULL;
1689 map.m_seg_type = NO_CHECK_TYPE;
1690 map.m_may_create = false;
1691 last_lblk = F2FS_BLK_ALIGN(pos + len);
1693 while (map.m_lblk < last_lblk) {
1694 map.m_len = last_lblk - map.m_lblk;
1695 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1696 if (err || map.m_len == 0)
1698 map.m_lblk += map.m_len;
1703 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1705 return (bytes >> inode->i_blkbits);
1708 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1710 return (blks << inode->i_blkbits);
1713 static int __get_data_block(struct inode *inode, sector_t iblock,
1714 struct buffer_head *bh, int create, int flag,
1715 pgoff_t *next_pgofs, int seg_type, bool may_write)
1717 struct f2fs_map_blocks map;
1720 map.m_lblk = iblock;
1721 map.m_len = bytes_to_blks(inode, bh->b_size);
1722 map.m_next_pgofs = next_pgofs;
1723 map.m_next_extent = NULL;
1724 map.m_seg_type = seg_type;
1725 map.m_may_create = may_write;
1727 err = f2fs_map_blocks(inode, &map, create, flag);
1729 map_bh(bh, inode->i_sb, map.m_pblk);
1730 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1731 bh->b_size = blks_to_bytes(inode, map.m_len);
1736 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1737 struct buffer_head *bh_result, int create)
1739 return __get_data_block(inode, iblock, bh_result, create,
1740 F2FS_GET_BLOCK_DIO, NULL,
1741 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1745 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1746 struct buffer_head *bh_result, int create)
1748 return __get_data_block(inode, iblock, bh_result, create,
1749 F2FS_GET_BLOCK_DIO, NULL,
1750 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1754 static int f2fs_xattr_fiemap(struct inode *inode,
1755 struct fiemap_extent_info *fieinfo)
1757 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1759 struct node_info ni;
1760 __u64 phys = 0, len;
1762 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1765 if (f2fs_has_inline_xattr(inode)) {
1768 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1769 inode->i_ino, false);
1773 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1775 f2fs_put_page(page, 1);
1779 phys = blks_to_bytes(inode, ni.blk_addr);
1780 offset = offsetof(struct f2fs_inode, i_addr) +
1781 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1782 get_inline_xattr_addrs(inode));
1785 len = inline_xattr_size(inode);
1787 f2fs_put_page(page, 1);
1789 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1792 flags |= FIEMAP_EXTENT_LAST;
1794 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1795 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1796 if (err || err == 1)
1801 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1805 err = f2fs_get_node_info(sbi, xnid, &ni);
1807 f2fs_put_page(page, 1);
1811 phys = blks_to_bytes(inode, ni.blk_addr);
1812 len = inode->i_sb->s_blocksize;
1814 f2fs_put_page(page, 1);
1816 flags = FIEMAP_EXTENT_LAST;
1820 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1821 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1824 return (err < 0 ? err : 0);
1827 static loff_t max_inode_blocks(struct inode *inode)
1829 loff_t result = ADDRS_PER_INODE(inode);
1830 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1832 /* two direct node blocks */
1833 result += (leaf_count * 2);
1835 /* two indirect node blocks */
1836 leaf_count *= NIDS_PER_BLOCK;
1837 result += (leaf_count * 2);
1839 /* one double indirect node block */
1840 leaf_count *= NIDS_PER_BLOCK;
1841 result += leaf_count;
1846 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1849 struct f2fs_map_blocks map;
1850 sector_t start_blk, last_blk;
1852 u64 logical = 0, phys = 0, size = 0;
1855 bool compr_cluster = false, compr_appended;
1856 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1857 unsigned int count_in_cluster = 0;
1860 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1861 ret = f2fs_precache_extents(inode);
1866 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1872 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1873 if (start > maxbytes) {
1878 if (len > maxbytes || (maxbytes - len) < start)
1879 len = maxbytes - start;
1881 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1882 ret = f2fs_xattr_fiemap(inode, fieinfo);
1886 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1887 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1892 if (bytes_to_blks(inode, len) == 0)
1893 len = blks_to_bytes(inode, 1);
1895 start_blk = bytes_to_blks(inode, start);
1896 last_blk = bytes_to_blks(inode, start + len - 1);
1899 memset(&map, 0, sizeof(map));
1900 map.m_lblk = start_blk;
1901 map.m_len = bytes_to_blks(inode, len);
1902 map.m_next_pgofs = &next_pgofs;
1903 map.m_seg_type = NO_CHECK_TYPE;
1905 if (compr_cluster) {
1907 map.m_len = cluster_size - count_in_cluster;
1910 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1915 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1916 start_blk = next_pgofs;
1918 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1919 max_inode_blocks(inode)))
1922 flags |= FIEMAP_EXTENT_LAST;
1925 compr_appended = false;
1926 /* In a case of compressed cluster, append this to the last extent */
1927 if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1928 !(map.m_flags & F2FS_MAP_FLAGS))) {
1929 compr_appended = true;
1934 flags |= FIEMAP_EXTENT_MERGED;
1935 if (IS_ENCRYPTED(inode))
1936 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1938 ret = fiemap_fill_next_extent(fieinfo, logical,
1940 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1946 if (start_blk > last_blk)
1950 if (map.m_pblk == COMPRESS_ADDR) {
1951 compr_cluster = true;
1952 count_in_cluster = 1;
1953 } else if (compr_appended) {
1954 unsigned int appended_blks = cluster_size -
1955 count_in_cluster + 1;
1956 size += blks_to_bytes(inode, appended_blks);
1957 start_blk += appended_blks;
1958 compr_cluster = false;
1960 logical = blks_to_bytes(inode, start_blk);
1961 phys = __is_valid_data_blkaddr(map.m_pblk) ?
1962 blks_to_bytes(inode, map.m_pblk) : 0;
1963 size = blks_to_bytes(inode, map.m_len);
1966 if (compr_cluster) {
1967 flags = FIEMAP_EXTENT_ENCODED;
1968 count_in_cluster += map.m_len;
1969 if (count_in_cluster == cluster_size) {
1970 compr_cluster = false;
1971 size += blks_to_bytes(inode, 1);
1973 } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
1974 flags = FIEMAP_EXTENT_UNWRITTEN;
1977 start_blk += bytes_to_blks(inode, size);
1982 if (fatal_signal_pending(current))
1990 inode_unlock(inode);
1994 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1996 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1997 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1998 return inode->i_sb->s_maxbytes;
2000 return i_size_read(inode);
2003 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2005 struct f2fs_map_blocks *map,
2006 struct bio **bio_ret,
2007 sector_t *last_block_in_bio,
2010 struct bio *bio = *bio_ret;
2011 const unsigned blocksize = blks_to_bytes(inode, 1);
2012 sector_t block_in_file;
2013 sector_t last_block;
2014 sector_t last_block_in_file;
2018 block_in_file = (sector_t)page_index(page);
2019 last_block = block_in_file + nr_pages;
2020 last_block_in_file = bytes_to_blks(inode,
2021 f2fs_readpage_limit(inode) + blocksize - 1);
2022 if (last_block > last_block_in_file)
2023 last_block = last_block_in_file;
2025 /* just zeroing out page which is beyond EOF */
2026 if (block_in_file >= last_block)
2029 * Map blocks using the previous result first.
2031 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2032 block_in_file > map->m_lblk &&
2033 block_in_file < (map->m_lblk + map->m_len))
2037 * Then do more f2fs_map_blocks() calls until we are
2038 * done with this page.
2040 map->m_lblk = block_in_file;
2041 map->m_len = last_block - block_in_file;
2043 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2047 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2048 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2049 SetPageMappedToDisk(page);
2051 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2052 !cleancache_get_page(page))) {
2053 SetPageUptodate(page);
2057 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2058 DATA_GENERIC_ENHANCE_READ)) {
2059 ret = -EFSCORRUPTED;
2064 zero_user_segment(page, 0, PAGE_SIZE);
2065 if (f2fs_need_verity(inode, page->index) &&
2066 !fsverity_verify_page(page)) {
2070 if (!PageUptodate(page))
2071 SetPageUptodate(page);
2077 * This page will go to BIO. Do we need to send this
2080 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2081 *last_block_in_bio, block_nr) ||
2082 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2084 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2088 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2089 is_readahead ? REQ_RAHEAD : 0, page->index,
2099 * If the page is under writeback, we need to wait for
2100 * its completion to see the correct decrypted data.
2102 f2fs_wait_on_block_writeback(inode, block_nr);
2104 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2105 goto submit_and_realloc;
2107 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2108 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2109 ClearPageError(page);
2110 *last_block_in_bio = block_nr;
2114 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2123 #ifdef CONFIG_F2FS_FS_COMPRESSION
2124 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2125 unsigned nr_pages, sector_t *last_block_in_bio,
2126 bool is_readahead, bool for_write)
2128 struct dnode_of_data dn;
2129 struct inode *inode = cc->inode;
2130 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2131 struct bio *bio = *bio_ret;
2132 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2133 sector_t last_block_in_file;
2134 const unsigned blocksize = blks_to_bytes(inode, 1);
2135 struct decompress_io_ctx *dic = NULL;
2136 struct extent_info ei = {0, };
2137 bool from_dnode = true;
2141 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2143 last_block_in_file = bytes_to_blks(inode,
2144 f2fs_readpage_limit(inode) + blocksize - 1);
2146 /* get rid of pages beyond EOF */
2147 for (i = 0; i < cc->cluster_size; i++) {
2148 struct page *page = cc->rpages[i];
2152 if ((sector_t)page->index >= last_block_in_file) {
2153 zero_user_segment(page, 0, PAGE_SIZE);
2154 if (!PageUptodate(page))
2155 SetPageUptodate(page);
2156 } else if (!PageUptodate(page)) {
2160 cc->rpages[i] = NULL;
2164 /* we are done since all pages are beyond EOF */
2165 if (f2fs_cluster_is_empty(cc))
2168 if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2172 goto skip_reading_dnode;
2174 set_new_dnode(&dn, inode, NULL, NULL, 0);
2175 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2179 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2182 for (i = 1; i < cc->cluster_size; i++) {
2185 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2186 dn.ofs_in_node + i) :
2189 if (!__is_valid_data_blkaddr(blkaddr))
2192 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2198 if (!from_dnode && i >= ei.c_len)
2202 /* nothing to decompress */
2203 if (cc->nr_cpages == 0) {
2208 dic = f2fs_alloc_dic(cc);
2214 for (i = 0; i < cc->nr_cpages; i++) {
2215 struct page *page = dic->cpages[i];
2217 struct bio_post_read_ctx *ctx;
2219 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2220 dn.ofs_in_node + i + 1) :
2223 f2fs_wait_on_block_writeback(inode, blkaddr);
2225 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2226 if (atomic_dec_and_test(&dic->remaining_pages))
2227 f2fs_decompress_cluster(dic);
2231 if (bio && (!page_is_mergeable(sbi, bio,
2232 *last_block_in_bio, blkaddr) ||
2233 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2235 __submit_bio(sbi, bio, DATA);
2240 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2241 is_readahead ? REQ_RAHEAD : 0,
2242 page->index, for_write);
2245 f2fs_decompress_end_io(dic, ret);
2246 f2fs_put_dnode(&dn);
2252 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2253 goto submit_and_realloc;
2255 ctx = bio->bi_private;
2256 ctx->enabled_steps |= STEP_DECOMPRESS;
2257 refcount_inc(&dic->refcnt);
2259 inc_page_count(sbi, F2FS_RD_DATA);
2260 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2261 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2262 ClearPageError(page);
2263 *last_block_in_bio = blkaddr;
2267 f2fs_put_dnode(&dn);
2274 f2fs_put_dnode(&dn);
2276 for (i = 0; i < cc->cluster_size; i++) {
2277 if (cc->rpages[i]) {
2278 ClearPageUptodate(cc->rpages[i]);
2279 ClearPageError(cc->rpages[i]);
2280 unlock_page(cc->rpages[i]);
2289 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2290 * Major change was from block_size == page_size in f2fs by default.
2292 static int f2fs_mpage_readpages(struct inode *inode,
2293 struct readahead_control *rac, struct page *page)
2295 struct bio *bio = NULL;
2296 sector_t last_block_in_bio = 0;
2297 struct f2fs_map_blocks map;
2298 #ifdef CONFIG_F2FS_FS_COMPRESSION
2299 struct compress_ctx cc = {
2301 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2302 .cluster_size = F2FS_I(inode)->i_cluster_size,
2303 .cluster_idx = NULL_CLUSTER,
2309 pgoff_t nc_cluster_idx = NULL_CLUSTER;
2311 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2312 unsigned max_nr_pages = nr_pages;
2319 map.m_next_pgofs = NULL;
2320 map.m_next_extent = NULL;
2321 map.m_seg_type = NO_CHECK_TYPE;
2322 map.m_may_create = false;
2324 for (; nr_pages; nr_pages--) {
2326 page = readahead_page(rac);
2327 prefetchw(&page->flags);
2330 #ifdef CONFIG_F2FS_FS_COMPRESSION
2331 if (f2fs_compressed_file(inode)) {
2332 /* there are remained comressed pages, submit them */
2333 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2334 ret = f2fs_read_multi_pages(&cc, &bio,
2337 rac != NULL, false);
2338 f2fs_destroy_compress_ctx(&cc, false);
2340 goto set_error_page;
2342 if (cc.cluster_idx == NULL_CLUSTER) {
2343 if (nc_cluster_idx ==
2344 page->index >> cc.log_cluster_size) {
2345 goto read_single_page;
2348 ret = f2fs_is_compressed_cluster(inode, page->index);
2350 goto set_error_page;
2353 page->index >> cc.log_cluster_size;
2354 goto read_single_page;
2357 nc_cluster_idx = NULL_CLUSTER;
2359 ret = f2fs_init_compress_ctx(&cc);
2361 goto set_error_page;
2363 f2fs_compress_ctx_add_page(&cc, page);
2370 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2371 &bio, &last_block_in_bio, rac);
2373 #ifdef CONFIG_F2FS_FS_COMPRESSION
2377 zero_user_segment(page, 0, PAGE_SIZE);
2380 #ifdef CONFIG_F2FS_FS_COMPRESSION
2386 #ifdef CONFIG_F2FS_FS_COMPRESSION
2387 if (f2fs_compressed_file(inode)) {
2389 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2390 ret = f2fs_read_multi_pages(&cc, &bio,
2393 rac != NULL, false);
2394 f2fs_destroy_compress_ctx(&cc, false);
2400 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2404 static int f2fs_read_data_page(struct file *file, struct page *page)
2406 struct inode *inode = page_file_mapping(page)->host;
2409 trace_f2fs_readpage(page, DATA);
2411 if (!f2fs_is_compress_backend_ready(inode)) {
2416 /* If the file has inline data, try to read it directly */
2417 if (f2fs_has_inline_data(inode))
2418 ret = f2fs_read_inline_data(inode, page);
2420 ret = f2fs_mpage_readpages(inode, NULL, page);
2424 static void f2fs_readahead(struct readahead_control *rac)
2426 struct inode *inode = rac->mapping->host;
2428 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2430 if (!f2fs_is_compress_backend_ready(inode))
2433 /* If the file has inline data, skip readpages */
2434 if (f2fs_has_inline_data(inode))
2437 f2fs_mpage_readpages(inode, rac, NULL);
2440 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2442 struct inode *inode = fio->page->mapping->host;
2443 struct page *mpage, *page;
2444 gfp_t gfp_flags = GFP_NOFS;
2446 if (!f2fs_encrypted_file(inode))
2449 page = fio->compressed_page ? fio->compressed_page : fio->page;
2451 /* wait for GCed page writeback via META_MAPPING */
2452 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2454 if (fscrypt_inode_uses_inline_crypto(inode))
2458 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2459 PAGE_SIZE, 0, gfp_flags);
2460 if (IS_ERR(fio->encrypted_page)) {
2461 /* flush pending IOs and wait for a while in the ENOMEM case */
2462 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2463 f2fs_flush_merged_writes(fio->sbi);
2464 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2465 gfp_flags |= __GFP_NOFAIL;
2468 return PTR_ERR(fio->encrypted_page);
2471 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2473 if (PageUptodate(mpage))
2474 memcpy(page_address(mpage),
2475 page_address(fio->encrypted_page), PAGE_SIZE);
2476 f2fs_put_page(mpage, 1);
2481 static inline bool check_inplace_update_policy(struct inode *inode,
2482 struct f2fs_io_info *fio)
2484 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2485 unsigned int policy = SM_I(sbi)->ipu_policy;
2487 if (policy & (0x1 << F2FS_IPU_FORCE))
2489 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2491 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2492 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2494 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2495 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2499 * IPU for rewrite async pages
2501 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2502 fio && fio->op == REQ_OP_WRITE &&
2503 !(fio->op_flags & REQ_SYNC) &&
2504 !IS_ENCRYPTED(inode))
2507 /* this is only set during fdatasync */
2508 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2509 is_inode_flag_set(inode, FI_NEED_IPU))
2512 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2513 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2519 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2521 /* swap file is migrating in aligned write mode */
2522 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2525 if (f2fs_is_pinned_file(inode))
2528 /* if this is cold file, we should overwrite to avoid fragmentation */
2529 if (file_is_cold(inode))
2532 return check_inplace_update_policy(inode, fio);
2535 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2537 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2539 if (f2fs_lfs_mode(sbi))
2541 if (S_ISDIR(inode->i_mode))
2543 if (IS_NOQUOTA(inode))
2545 if (f2fs_is_atomic_file(inode))
2547 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2550 /* swap file is migrating in aligned write mode */
2551 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2555 if (page_private_gcing(fio->page))
2557 if (page_private_dummy(fio->page))
2559 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2560 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2566 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2568 struct inode *inode = fio->page->mapping->host;
2570 if (f2fs_should_update_outplace(inode, fio))
2573 return f2fs_should_update_inplace(inode, fio);
2576 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2578 struct page *page = fio->page;
2579 struct inode *inode = page->mapping->host;
2580 struct dnode_of_data dn;
2581 struct extent_info ei = {0, };
2582 struct node_info ni;
2583 bool ipu_force = false;
2586 set_new_dnode(&dn, inode, NULL, NULL, 0);
2587 if (need_inplace_update(fio) &&
2588 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2589 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2591 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2592 DATA_GENERIC_ENHANCE))
2593 return -EFSCORRUPTED;
2596 fio->need_lock = LOCK_DONE;
2600 /* Deadlock due to between page->lock and f2fs_lock_op */
2601 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2604 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2608 fio->old_blkaddr = dn.data_blkaddr;
2610 /* This page is already truncated */
2611 if (fio->old_blkaddr == NULL_ADDR) {
2612 ClearPageUptodate(page);
2613 clear_page_private_gcing(page);
2617 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2618 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2619 DATA_GENERIC_ENHANCE)) {
2620 err = -EFSCORRUPTED;
2624 * If current allocation needs SSR,
2625 * it had better in-place writes for updated data.
2628 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2629 need_inplace_update(fio))) {
2630 err = f2fs_encrypt_one_page(fio);
2634 set_page_writeback(page);
2635 ClearPageError(page);
2636 f2fs_put_dnode(&dn);
2637 if (fio->need_lock == LOCK_REQ)
2638 f2fs_unlock_op(fio->sbi);
2639 err = f2fs_inplace_write_data(fio);
2641 if (fscrypt_inode_uses_fs_layer_crypto(inode))
2642 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2643 if (PageWriteback(page))
2644 end_page_writeback(page);
2646 set_inode_flag(inode, FI_UPDATE_WRITE);
2648 trace_f2fs_do_write_data_page(fio->page, IPU);
2652 if (fio->need_lock == LOCK_RETRY) {
2653 if (!f2fs_trylock_op(fio->sbi)) {
2657 fio->need_lock = LOCK_REQ;
2660 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2664 fio->version = ni.version;
2666 err = f2fs_encrypt_one_page(fio);
2670 set_page_writeback(page);
2671 ClearPageError(page);
2673 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2674 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2676 /* LFS mode write path */
2677 f2fs_outplace_write_data(&dn, fio);
2678 trace_f2fs_do_write_data_page(page, OPU);
2679 set_inode_flag(inode, FI_APPEND_WRITE);
2680 if (page->index == 0)
2681 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2683 f2fs_put_dnode(&dn);
2685 if (fio->need_lock == LOCK_REQ)
2686 f2fs_unlock_op(fio->sbi);
2690 int f2fs_write_single_data_page(struct page *page, int *submitted,
2692 sector_t *last_block,
2693 struct writeback_control *wbc,
2694 enum iostat_type io_type,
2698 struct inode *inode = page->mapping->host;
2699 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2700 loff_t i_size = i_size_read(inode);
2701 const pgoff_t end_index = ((unsigned long long)i_size)
2703 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2704 unsigned offset = 0;
2705 bool need_balance_fs = false;
2707 struct f2fs_io_info fio = {
2709 .ino = inode->i_ino,
2712 .op_flags = wbc_to_write_flags(wbc),
2713 .old_blkaddr = NULL_ADDR,
2715 .encrypted_page = NULL,
2717 .compr_blocks = compr_blocks,
2718 .need_lock = LOCK_RETRY,
2722 .last_block = last_block,
2725 trace_f2fs_writepage(page, DATA);
2727 /* we should bypass data pages to proceed the kworkder jobs */
2728 if (unlikely(f2fs_cp_error(sbi))) {
2729 mapping_set_error(page->mapping, -EIO);
2731 * don't drop any dirty dentry pages for keeping lastest
2732 * directory structure.
2734 if (S_ISDIR(inode->i_mode))
2739 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2742 if (page->index < end_index ||
2743 f2fs_verity_in_progress(inode) ||
2748 * If the offset is out-of-range of file size,
2749 * this page does not have to be written to disk.
2751 offset = i_size & (PAGE_SIZE - 1);
2752 if ((page->index >= end_index + 1) || !offset)
2755 zero_user_segment(page, offset, PAGE_SIZE);
2757 if (f2fs_is_drop_cache(inode))
2759 /* we should not write 0'th page having journal header */
2760 if (f2fs_is_volatile_file(inode) && (!page->index ||
2761 (!wbc->for_reclaim &&
2762 f2fs_available_free_memory(sbi, BASE_CHECK))))
2765 /* Dentry/quota blocks are controlled by checkpoint */
2766 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2768 * We need to wait for node_write to avoid block allocation during
2769 * checkpoint. This can only happen to quota writes which can cause
2770 * the below discard race condition.
2772 if (IS_NOQUOTA(inode))
2773 down_read(&sbi->node_write);
2775 fio.need_lock = LOCK_DONE;
2776 err = f2fs_do_write_data_page(&fio);
2778 if (IS_NOQUOTA(inode))
2779 up_read(&sbi->node_write);
2784 if (!wbc->for_reclaim)
2785 need_balance_fs = true;
2786 else if (has_not_enough_free_secs(sbi, 0, 0))
2789 set_inode_flag(inode, FI_HOT_DATA);
2792 if (f2fs_has_inline_data(inode)) {
2793 err = f2fs_write_inline_data(inode, page);
2798 if (err == -EAGAIN) {
2799 err = f2fs_do_write_data_page(&fio);
2800 if (err == -EAGAIN) {
2801 fio.need_lock = LOCK_REQ;
2802 err = f2fs_do_write_data_page(&fio);
2807 file_set_keep_isize(inode);
2809 spin_lock(&F2FS_I(inode)->i_size_lock);
2810 if (F2FS_I(inode)->last_disk_size < psize)
2811 F2FS_I(inode)->last_disk_size = psize;
2812 spin_unlock(&F2FS_I(inode)->i_size_lock);
2816 if (err && err != -ENOENT)
2820 inode_dec_dirty_pages(inode);
2822 ClearPageUptodate(page);
2823 clear_page_private_gcing(page);
2826 if (wbc->for_reclaim) {
2827 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2828 clear_inode_flag(inode, FI_HOT_DATA);
2829 f2fs_remove_dirty_inode(inode);
2833 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2834 !F2FS_I(inode)->cp_task && allow_balance)
2835 f2fs_balance_fs(sbi, need_balance_fs);
2837 if (unlikely(f2fs_cp_error(sbi))) {
2838 f2fs_submit_merged_write(sbi, DATA);
2839 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2844 *submitted = fio.submitted ? 1 : 0;
2849 redirty_page_for_writepage(wbc, page);
2851 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2852 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2853 * file_write_and_wait_range() will see EIO error, which is critical
2854 * to return value of fsync() followed by atomic_write failure to user.
2856 if (!err || wbc->for_reclaim)
2857 return AOP_WRITEPAGE_ACTIVATE;
2862 static int f2fs_write_data_page(struct page *page,
2863 struct writeback_control *wbc)
2865 #ifdef CONFIG_F2FS_FS_COMPRESSION
2866 struct inode *inode = page->mapping->host;
2868 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2871 if (f2fs_compressed_file(inode)) {
2872 if (f2fs_is_compressed_cluster(inode, page->index)) {
2873 redirty_page_for_writepage(wbc, page);
2874 return AOP_WRITEPAGE_ACTIVATE;
2880 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2881 wbc, FS_DATA_IO, 0, true);
2885 * This function was copied from write_cche_pages from mm/page-writeback.c.
2886 * The major change is making write step of cold data page separately from
2887 * warm/hot data page.
2889 static int f2fs_write_cache_pages(struct address_space *mapping,
2890 struct writeback_control *wbc,
2891 enum iostat_type io_type)
2894 int done = 0, retry = 0;
2895 struct pagevec pvec;
2896 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2897 struct bio *bio = NULL;
2898 sector_t last_block;
2899 #ifdef CONFIG_F2FS_FS_COMPRESSION
2900 struct inode *inode = mapping->host;
2901 struct compress_ctx cc = {
2903 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2904 .cluster_size = F2FS_I(inode)->i_cluster_size,
2905 .cluster_idx = NULL_CLUSTER,
2911 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2917 pgoff_t end; /* Inclusive */
2919 int range_whole = 0;
2925 pagevec_init(&pvec);
2927 if (get_dirty_pages(mapping->host) <=
2928 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2929 set_inode_flag(mapping->host, FI_HOT_DATA);
2931 clear_inode_flag(mapping->host, FI_HOT_DATA);
2933 if (wbc->range_cyclic) {
2934 index = mapping->writeback_index; /* prev offset */
2937 index = wbc->range_start >> PAGE_SHIFT;
2938 end = wbc->range_end >> PAGE_SHIFT;
2939 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2942 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2943 tag = PAGECACHE_TAG_TOWRITE;
2945 tag = PAGECACHE_TAG_DIRTY;
2948 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2949 tag_pages_for_writeback(mapping, index, end);
2951 while (!done && !retry && (index <= end)) {
2952 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2957 for (i = 0; i < nr_pages; i++) {
2958 struct page *page = pvec.pages[i];
2962 #ifdef CONFIG_F2FS_FS_COMPRESSION
2963 if (f2fs_compressed_file(inode)) {
2964 ret = f2fs_init_compress_ctx(&cc);
2970 if (!f2fs_cluster_can_merge_page(&cc,
2972 ret = f2fs_write_multi_pages(&cc,
2973 &submitted, wbc, io_type);
2979 if (unlikely(f2fs_cp_error(sbi)))
2982 if (f2fs_cluster_is_empty(&cc)) {
2983 void *fsdata = NULL;
2987 ret2 = f2fs_prepare_compress_overwrite(
2989 page->index, &fsdata);
2995 !f2fs_compress_write_end(inode,
2996 fsdata, page->index,
3006 /* give a priority to WB_SYNC threads */
3007 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3008 wbc->sync_mode == WB_SYNC_NONE) {
3012 #ifdef CONFIG_F2FS_FS_COMPRESSION
3015 done_index = page->index;
3019 if (unlikely(page->mapping != mapping)) {
3025 if (!PageDirty(page)) {
3026 /* someone wrote it for us */
3027 goto continue_unlock;
3030 if (PageWriteback(page)) {
3031 if (wbc->sync_mode != WB_SYNC_NONE)
3032 f2fs_wait_on_page_writeback(page,
3035 goto continue_unlock;
3038 if (!clear_page_dirty_for_io(page))
3039 goto continue_unlock;
3041 #ifdef CONFIG_F2FS_FS_COMPRESSION
3042 if (f2fs_compressed_file(inode)) {
3044 f2fs_compress_ctx_add_page(&cc, page);
3048 ret = f2fs_write_single_data_page(page, &submitted,
3049 &bio, &last_block, wbc, io_type,
3051 if (ret == AOP_WRITEPAGE_ACTIVATE)
3053 #ifdef CONFIG_F2FS_FS_COMPRESSION
3056 nwritten += submitted;
3057 wbc->nr_to_write -= submitted;
3059 if (unlikely(ret)) {
3061 * keep nr_to_write, since vfs uses this to
3062 * get # of written pages.
3064 if (ret == AOP_WRITEPAGE_ACTIVATE) {
3067 } else if (ret == -EAGAIN) {
3069 if (wbc->sync_mode == WB_SYNC_ALL) {
3071 congestion_wait(BLK_RW_ASYNC,
3072 DEFAULT_IO_TIMEOUT);
3077 done_index = page->index + 1;
3082 if (wbc->nr_to_write <= 0 &&
3083 wbc->sync_mode == WB_SYNC_NONE) {
3091 pagevec_release(&pvec);
3094 #ifdef CONFIG_F2FS_FS_COMPRESSION
3095 /* flush remained pages in compress cluster */
3096 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3097 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3098 nwritten += submitted;
3099 wbc->nr_to_write -= submitted;
3105 if (f2fs_compressed_file(inode))
3106 f2fs_destroy_compress_ctx(&cc, false);
3113 if (wbc->range_cyclic && !done)
3115 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3116 mapping->writeback_index = done_index;
3119 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3121 /* submit cached bio of IPU write */
3123 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3128 static inline bool __should_serialize_io(struct inode *inode,
3129 struct writeback_control *wbc)
3131 /* to avoid deadlock in path of data flush */
3132 if (F2FS_I(inode)->cp_task)
3135 if (!S_ISREG(inode->i_mode))
3137 if (IS_NOQUOTA(inode))
3140 if (f2fs_need_compress_data(inode))
3142 if (wbc->sync_mode != WB_SYNC_ALL)
3144 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3149 static int __f2fs_write_data_pages(struct address_space *mapping,
3150 struct writeback_control *wbc,
3151 enum iostat_type io_type)
3153 struct inode *inode = mapping->host;
3154 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3155 struct blk_plug plug;
3157 bool locked = false;
3159 /* deal with chardevs and other special file */
3160 if (!mapping->a_ops->writepage)
3163 /* skip writing if there is no dirty page in this inode */
3164 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3167 /* during POR, we don't need to trigger writepage at all. */
3168 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3171 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3172 wbc->sync_mode == WB_SYNC_NONE &&
3173 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3174 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3177 /* skip writing during file defragment */
3178 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3181 trace_f2fs_writepages(mapping->host, wbc, DATA);
3183 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3184 if (wbc->sync_mode == WB_SYNC_ALL)
3185 atomic_inc(&sbi->wb_sync_req[DATA]);
3186 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3189 if (__should_serialize_io(inode, wbc)) {
3190 mutex_lock(&sbi->writepages);
3194 blk_start_plug(&plug);
3195 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3196 blk_finish_plug(&plug);
3199 mutex_unlock(&sbi->writepages);
3201 if (wbc->sync_mode == WB_SYNC_ALL)
3202 atomic_dec(&sbi->wb_sync_req[DATA]);
3204 * if some pages were truncated, we cannot guarantee its mapping->host
3205 * to detect pending bios.
3208 f2fs_remove_dirty_inode(inode);
3212 wbc->pages_skipped += get_dirty_pages(inode);
3213 trace_f2fs_writepages(mapping->host, wbc, DATA);
3217 static int f2fs_write_data_pages(struct address_space *mapping,
3218 struct writeback_control *wbc)
3220 struct inode *inode = mapping->host;
3222 return __f2fs_write_data_pages(mapping, wbc,
3223 F2FS_I(inode)->cp_task == current ?
3224 FS_CP_DATA_IO : FS_DATA_IO);
3227 static void f2fs_write_failed(struct inode *inode, loff_t to)
3229 loff_t i_size = i_size_read(inode);
3231 if (IS_NOQUOTA(inode))
3234 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3235 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3236 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3237 down_write(&F2FS_I(inode)->i_mmap_sem);
3239 truncate_pagecache(inode, i_size);
3240 f2fs_truncate_blocks(inode, i_size, true);
3242 up_write(&F2FS_I(inode)->i_mmap_sem);
3243 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3247 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3248 struct page *page, loff_t pos, unsigned len,
3249 block_t *blk_addr, bool *node_changed)
3251 struct inode *inode = page->mapping->host;
3252 pgoff_t index = page->index;
3253 struct dnode_of_data dn;
3255 bool locked = false;
3256 struct extent_info ei = {0, };
3261 * we already allocated all the blocks, so we don't need to get
3262 * the block addresses when there is no need to fill the page.
3264 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3265 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3266 !f2fs_verity_in_progress(inode))
3269 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3270 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3271 flag = F2FS_GET_BLOCK_DEFAULT;
3273 flag = F2FS_GET_BLOCK_PRE_AIO;
3275 if (f2fs_has_inline_data(inode) ||
3276 (pos & PAGE_MASK) >= i_size_read(inode)) {
3277 f2fs_do_map_lock(sbi, flag, true);
3282 /* check inline_data */
3283 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3284 if (IS_ERR(ipage)) {
3285 err = PTR_ERR(ipage);
3289 set_new_dnode(&dn, inode, ipage, ipage, 0);
3291 if (f2fs_has_inline_data(inode)) {
3292 if (pos + len <= MAX_INLINE_DATA(inode)) {
3293 f2fs_do_read_inline_data(page, ipage);
3294 set_inode_flag(inode, FI_DATA_EXIST);
3296 set_page_private_inline(ipage);
3298 err = f2fs_convert_inline_page(&dn, page);
3301 if (dn.data_blkaddr == NULL_ADDR)
3302 err = f2fs_get_block(&dn, index);
3304 } else if (locked) {
3305 err = f2fs_get_block(&dn, index);
3307 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3308 dn.data_blkaddr = ei.blk + index - ei.fofs;
3311 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3312 if (err || dn.data_blkaddr == NULL_ADDR) {
3313 f2fs_put_dnode(&dn);
3314 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3316 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3323 /* convert_inline_page can make node_changed */
3324 *blk_addr = dn.data_blkaddr;
3325 *node_changed = dn.node_changed;
3327 f2fs_put_dnode(&dn);
3330 f2fs_do_map_lock(sbi, flag, false);
3334 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3335 loff_t pos, unsigned len, unsigned flags,
3336 struct page **pagep, void **fsdata)
3338 struct inode *inode = mapping->host;
3339 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3340 struct page *page = NULL;
3341 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3342 bool need_balance = false, drop_atomic = false;
3343 block_t blkaddr = NULL_ADDR;
3346 trace_f2fs_write_begin(inode, pos, len, flags);
3348 if (!f2fs_is_checkpoint_ready(sbi)) {
3353 if ((f2fs_is_atomic_file(inode) &&
3354 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3355 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3362 * We should check this at this moment to avoid deadlock on inode page
3363 * and #0 page. The locking rule for inline_data conversion should be:
3364 * lock_page(page #0) -> lock_page(inode_page)
3367 err = f2fs_convert_inline_inode(inode);
3372 #ifdef CONFIG_F2FS_FS_COMPRESSION
3373 if (f2fs_compressed_file(inode)) {
3378 if (len == PAGE_SIZE)
3381 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3394 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3395 * wait_for_stable_page. Will wait that below with our IO control.
3397 page = f2fs_pagecache_get_page(mapping, index,
3398 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3404 /* TODO: cluster can be compressed due to race with .writepage */
3408 err = prepare_write_begin(sbi, page, pos, len,
3409 &blkaddr, &need_balance);
3413 if (need_balance && !IS_NOQUOTA(inode) &&
3414 has_not_enough_free_secs(sbi, 0, 0)) {
3416 f2fs_balance_fs(sbi, true);
3418 if (page->mapping != mapping) {
3419 /* The page got truncated from under us */
3420 f2fs_put_page(page, 1);
3425 f2fs_wait_on_page_writeback(page, DATA, false, true);
3427 if (len == PAGE_SIZE || PageUptodate(page))
3430 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3431 !f2fs_verity_in_progress(inode)) {
3432 zero_user_segment(page, len, PAGE_SIZE);
3436 if (blkaddr == NEW_ADDR) {
3437 zero_user_segment(page, 0, PAGE_SIZE);
3438 SetPageUptodate(page);
3440 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3441 DATA_GENERIC_ENHANCE_READ)) {
3442 err = -EFSCORRUPTED;
3445 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3450 if (unlikely(page->mapping != mapping)) {
3451 f2fs_put_page(page, 1);
3454 if (unlikely(!PageUptodate(page))) {
3462 f2fs_put_page(page, 1);
3463 f2fs_write_failed(inode, pos + len);
3465 f2fs_drop_inmem_pages_all(sbi, false);
3469 static int f2fs_write_end(struct file *file,
3470 struct address_space *mapping,
3471 loff_t pos, unsigned len, unsigned copied,
3472 struct page *page, void *fsdata)
3474 struct inode *inode = page->mapping->host;
3476 trace_f2fs_write_end(inode, pos, len, copied);
3479 * This should be come from len == PAGE_SIZE, and we expect copied
3480 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3481 * let generic_perform_write() try to copy data again through copied=0.
3483 if (!PageUptodate(page)) {
3484 if (unlikely(copied != len))
3487 SetPageUptodate(page);
3490 #ifdef CONFIG_F2FS_FS_COMPRESSION
3491 /* overwrite compressed file */
3492 if (f2fs_compressed_file(inode) && fsdata) {
3493 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3494 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3496 if (pos + copied > i_size_read(inode) &&
3497 !f2fs_verity_in_progress(inode))
3498 f2fs_i_size_write(inode, pos + copied);
3506 set_page_dirty(page);
3508 if (pos + copied > i_size_read(inode) &&
3509 !f2fs_verity_in_progress(inode))
3510 f2fs_i_size_write(inode, pos + copied);
3512 f2fs_put_page(page, 1);
3513 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3517 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3520 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3521 unsigned blkbits = i_blkbits;
3522 unsigned blocksize_mask = (1 << blkbits) - 1;
3523 unsigned long align = offset | iov_iter_alignment(iter);
3524 struct block_device *bdev = inode->i_sb->s_bdev;
3526 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3529 if (align & blocksize_mask) {
3531 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3532 blocksize_mask = (1 << blkbits) - 1;
3533 if (align & blocksize_mask)
3540 static void f2fs_dio_end_io(struct bio *bio)
3542 struct f2fs_private_dio *dio = bio->bi_private;
3544 dec_page_count(F2FS_I_SB(dio->inode),
3545 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3547 bio->bi_private = dio->orig_private;
3548 bio->bi_end_io = dio->orig_end_io;
3555 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3558 struct f2fs_private_dio *dio;
3559 bool write = (bio_op(bio) == REQ_OP_WRITE);
3561 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3562 sizeof(struct f2fs_private_dio), GFP_NOFS);
3567 dio->orig_end_io = bio->bi_end_io;
3568 dio->orig_private = bio->bi_private;
3571 bio->bi_end_io = f2fs_dio_end_io;
3572 bio->bi_private = dio;
3574 inc_page_count(F2FS_I_SB(inode),
3575 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3580 bio->bi_status = BLK_STS_IOERR;
3584 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3586 struct address_space *mapping = iocb->ki_filp->f_mapping;
3587 struct inode *inode = mapping->host;
3588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3589 struct f2fs_inode_info *fi = F2FS_I(inode);
3590 size_t count = iov_iter_count(iter);
3591 loff_t offset = iocb->ki_pos;
3592 int rw = iov_iter_rw(iter);
3594 enum rw_hint hint = iocb->ki_hint;
3595 int whint_mode = F2FS_OPTION(sbi).whint_mode;
3598 err = check_direct_IO(inode, iter, offset);
3600 return err < 0 ? err : 0;
3602 if (f2fs_force_buffered_io(inode, iocb, iter))
3605 do_opu = rw == WRITE && f2fs_lfs_mode(sbi);
3607 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3609 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3610 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3612 if (iocb->ki_flags & IOCB_NOWAIT) {
3613 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3614 iocb->ki_hint = hint;
3618 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3619 up_read(&fi->i_gc_rwsem[rw]);
3620 iocb->ki_hint = hint;
3625 down_read(&fi->i_gc_rwsem[rw]);
3627 down_read(&fi->i_gc_rwsem[READ]);
3630 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3631 iter, rw == WRITE ? get_data_block_dio_write :
3632 get_data_block_dio, NULL, f2fs_dio_submit_bio,
3633 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3637 up_read(&fi->i_gc_rwsem[READ]);
3639 up_read(&fi->i_gc_rwsem[rw]);
3642 if (whint_mode == WHINT_MODE_OFF)
3643 iocb->ki_hint = hint;
3645 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3648 set_inode_flag(inode, FI_UPDATE_WRITE);
3649 } else if (err == -EIOCBQUEUED) {
3650 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3651 count - iov_iter_count(iter));
3652 } else if (err < 0) {
3653 f2fs_write_failed(inode, offset + count);
3657 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3658 else if (err == -EIOCBQUEUED)
3659 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3660 count - iov_iter_count(iter));
3664 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3669 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3670 unsigned int length)
3672 struct inode *inode = page->mapping->host;
3673 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3675 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3676 (offset % PAGE_SIZE || length != PAGE_SIZE))
3679 if (PageDirty(page)) {
3680 if (inode->i_ino == F2FS_META_INO(sbi)) {
3681 dec_page_count(sbi, F2FS_DIRTY_META);
3682 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3683 dec_page_count(sbi, F2FS_DIRTY_NODES);
3685 inode_dec_dirty_pages(inode);
3686 f2fs_remove_dirty_inode(inode);
3690 clear_page_private_gcing(page);
3692 if (test_opt(sbi, COMPRESS_CACHE)) {
3693 if (f2fs_compressed_file(inode))
3694 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3695 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3696 clear_page_private_data(page);
3699 if (page_private_atomic(page))
3700 return f2fs_drop_inmem_page(inode, page);
3702 detach_page_private(page);
3703 set_page_private(page, 0);
3706 int f2fs_release_page(struct page *page, gfp_t wait)
3708 /* If this is dirty page, keep PagePrivate */
3709 if (PageDirty(page))
3712 /* This is atomic written page, keep Private */
3713 if (page_private_atomic(page))
3716 if (test_opt(F2FS_P_SB(page), COMPRESS_CACHE)) {
3717 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3718 struct inode *inode = page->mapping->host;
3720 if (f2fs_compressed_file(inode))
3721 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3722 if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3723 clear_page_private_data(page);
3726 clear_page_private_gcing(page);
3728 detach_page_private(page);
3729 set_page_private(page, 0);
3733 static int f2fs_set_data_page_dirty(struct page *page)
3735 struct inode *inode = page_file_mapping(page)->host;
3737 trace_f2fs_set_page_dirty(page, DATA);
3739 if (!PageUptodate(page))
3740 SetPageUptodate(page);
3741 if (PageSwapCache(page))
3742 return __set_page_dirty_nobuffers(page);
3744 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3745 if (!page_private_atomic(page)) {
3746 f2fs_register_inmem_page(inode, page);
3750 * Previously, this page has been registered, we just
3756 if (!PageDirty(page)) {
3757 __set_page_dirty_nobuffers(page);
3758 f2fs_update_dirty_page(inode, page);
3765 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3767 #ifdef CONFIG_F2FS_FS_COMPRESSION
3768 struct dnode_of_data dn;
3769 sector_t start_idx, blknr = 0;
3772 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3774 set_new_dnode(&dn, inode, NULL, NULL, 0);
3775 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3779 if (dn.data_blkaddr != COMPRESS_ADDR) {
3780 dn.ofs_in_node += block - start_idx;
3781 blknr = f2fs_data_blkaddr(&dn);
3782 if (!__is_valid_data_blkaddr(blknr))
3786 f2fs_put_dnode(&dn);
3794 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3796 struct inode *inode = mapping->host;
3799 if (f2fs_has_inline_data(inode))
3802 /* make sure allocating whole blocks */
3803 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3804 filemap_write_and_wait(mapping);
3806 /* Block number less than F2FS MAX BLOCKS */
3807 if (unlikely(block >= max_file_blocks(inode)))
3810 if (f2fs_compressed_file(inode)) {
3811 blknr = f2fs_bmap_compress(inode, block);
3813 struct f2fs_map_blocks map;
3815 memset(&map, 0, sizeof(map));
3818 map.m_next_pgofs = NULL;
3819 map.m_seg_type = NO_CHECK_TYPE;
3821 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3825 trace_f2fs_bmap(inode, block, blknr);
3829 #ifdef CONFIG_MIGRATION
3830 #include <linux/migrate.h>
3832 int f2fs_migrate_page(struct address_space *mapping,
3833 struct page *newpage, struct page *page, enum migrate_mode mode)
3835 int rc, extra_count;
3836 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3837 bool atomic_written = page_private_atomic(page);
3839 BUG_ON(PageWriteback(page));
3841 /* migrating an atomic written page is safe with the inmem_lock hold */
3842 if (atomic_written) {
3843 if (mode != MIGRATE_SYNC)
3845 if (!mutex_trylock(&fi->inmem_lock))
3849 /* one extra reference was held for atomic_write page */
3850 extra_count = atomic_written ? 1 : 0;
3851 rc = migrate_page_move_mapping(mapping, newpage,
3853 if (rc != MIGRATEPAGE_SUCCESS) {
3855 mutex_unlock(&fi->inmem_lock);
3859 if (atomic_written) {
3860 struct inmem_pages *cur;
3862 list_for_each_entry(cur, &fi->inmem_pages, list)
3863 if (cur->page == page) {
3864 cur->page = newpage;
3867 mutex_unlock(&fi->inmem_lock);
3872 /* guarantee to start from no stale private field */
3873 set_page_private(newpage, 0);
3874 if (PagePrivate(page)) {
3875 set_page_private(newpage, page_private(page));
3876 SetPagePrivate(newpage);
3879 set_page_private(page, 0);
3880 ClearPagePrivate(page);
3884 if (mode != MIGRATE_SYNC_NO_COPY)
3885 migrate_page_copy(newpage, page);
3887 migrate_page_states(newpage, page);
3889 return MIGRATEPAGE_SUCCESS;
3894 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3895 unsigned int blkcnt)
3897 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3898 unsigned int blkofs;
3899 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3900 unsigned int secidx = start_blk / blk_per_sec;
3901 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3904 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3905 down_write(&F2FS_I(inode)->i_mmap_sem);
3907 set_inode_flag(inode, FI_ALIGNED_WRITE);
3909 for (; secidx < end_sec; secidx++) {
3910 down_write(&sbi->pin_sem);
3913 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3914 f2fs_unlock_op(sbi);
3916 set_inode_flag(inode, FI_DO_DEFRAG);
3918 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3920 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3922 page = f2fs_get_lock_data_page(inode, blkidx, true);
3924 up_write(&sbi->pin_sem);
3925 ret = PTR_ERR(page);
3929 set_page_dirty(page);
3930 f2fs_put_page(page, 1);
3933 clear_inode_flag(inode, FI_DO_DEFRAG);
3935 ret = filemap_fdatawrite(inode->i_mapping);
3937 up_write(&sbi->pin_sem);
3944 clear_inode_flag(inode, FI_DO_DEFRAG);
3945 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3947 up_write(&F2FS_I(inode)->i_mmap_sem);
3948 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3953 static int check_swap_activate(struct swap_info_struct *sis,
3954 struct file *swap_file, sector_t *span)
3956 struct address_space *mapping = swap_file->f_mapping;
3957 struct inode *inode = mapping->host;
3958 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3959 sector_t cur_lblock;
3960 sector_t last_lblock;
3962 sector_t lowest_pblock = -1;
3963 sector_t highest_pblock = 0;
3965 unsigned long nr_pblocks;
3966 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3967 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3968 unsigned int not_aligned = 0;
3972 * Map all the blocks into the extent list. This code doesn't try
3976 last_lblock = bytes_to_blks(inode, i_size_read(inode));
3978 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3979 struct f2fs_map_blocks map;
3983 memset(&map, 0, sizeof(map));
3984 map.m_lblk = cur_lblock;
3985 map.m_len = last_lblock - cur_lblock;
3986 map.m_next_pgofs = NULL;
3987 map.m_next_extent = NULL;
3988 map.m_seg_type = NO_CHECK_TYPE;
3989 map.m_may_create = false;
3991 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3996 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3997 f2fs_err(sbi, "Swapfile has holes");
4002 pblock = map.m_pblk;
4003 nr_pblocks = map.m_len;
4005 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
4006 nr_pblocks & sec_blks_mask) {
4009 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
4010 if (cur_lblock + nr_pblocks > sis->max)
4011 nr_pblocks -= blks_per_sec;
4014 /* this extent is last one */
4015 nr_pblocks = map.m_len;
4016 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
4020 ret = f2fs_migrate_blocks(inode, cur_lblock,
4027 if (cur_lblock + nr_pblocks >= sis->max)
4028 nr_pblocks = sis->max - cur_lblock;
4030 if (cur_lblock) { /* exclude the header page */
4031 if (pblock < lowest_pblock)
4032 lowest_pblock = pblock;
4033 if (pblock + nr_pblocks - 1 > highest_pblock)
4034 highest_pblock = pblock + nr_pblocks - 1;
4038 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4040 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
4044 cur_lblock += nr_pblocks;
4047 *span = 1 + highest_pblock - lowest_pblock;
4048 if (cur_lblock == 0)
4049 cur_lblock = 1; /* force Empty message */
4050 sis->max = cur_lblock;
4051 sis->pages = cur_lblock - 1;
4052 sis->highest_bit = cur_lblock - 1;
4055 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
4056 not_aligned, blks_per_sec * F2FS_BLKSIZE);
4060 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4063 struct inode *inode = file_inode(file);
4066 if (!S_ISREG(inode->i_mode))
4069 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4072 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
4073 f2fs_err(F2FS_I_SB(inode),
4074 "Swapfile not supported in LFS mode");
4078 ret = f2fs_convert_inline_inode(inode);
4082 if (!f2fs_disable_compressed_file(inode))
4085 f2fs_precache_extents(inode);
4087 ret = check_swap_activate(sis, file, span);
4091 set_inode_flag(inode, FI_PIN_FILE);
4092 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4096 static void f2fs_swap_deactivate(struct file *file)
4098 struct inode *inode = file_inode(file);
4100 clear_inode_flag(inode, FI_PIN_FILE);
4103 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4109 static void f2fs_swap_deactivate(struct file *file)
4114 const struct address_space_operations f2fs_dblock_aops = {
4115 .readpage = f2fs_read_data_page,
4116 .readahead = f2fs_readahead,
4117 .writepage = f2fs_write_data_page,
4118 .writepages = f2fs_write_data_pages,
4119 .write_begin = f2fs_write_begin,
4120 .write_end = f2fs_write_end,
4121 .set_page_dirty = f2fs_set_data_page_dirty,
4122 .invalidatepage = f2fs_invalidate_page,
4123 .releasepage = f2fs_release_page,
4124 .direct_IO = f2fs_direct_IO,
4126 .swap_activate = f2fs_swap_activate,
4127 .swap_deactivate = f2fs_swap_deactivate,
4128 #ifdef CONFIG_MIGRATION
4129 .migratepage = f2fs_migrate_page,
4133 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4135 struct address_space *mapping = page_mapping(page);
4136 unsigned long flags;
4138 xa_lock_irqsave(&mapping->i_pages, flags);
4139 __xa_clear_mark(&mapping->i_pages, page_index(page),
4140 PAGECACHE_TAG_DIRTY);
4141 xa_unlock_irqrestore(&mapping->i_pages, flags);
4144 int __init f2fs_init_post_read_processing(void)
4146 bio_post_read_ctx_cache =
4147 kmem_cache_create("f2fs_bio_post_read_ctx",
4148 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4149 if (!bio_post_read_ctx_cache)
4151 bio_post_read_ctx_pool =
4152 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4153 bio_post_read_ctx_cache);
4154 if (!bio_post_read_ctx_pool)
4155 goto fail_free_cache;
4159 kmem_cache_destroy(bio_post_read_ctx_cache);
4164 void f2fs_destroy_post_read_processing(void)
4166 mempool_destroy(bio_post_read_ctx_pool);
4167 kmem_cache_destroy(bio_post_read_ctx_cache);
4170 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4172 if (!f2fs_sb_has_encrypt(sbi) &&
4173 !f2fs_sb_has_verity(sbi) &&
4174 !f2fs_sb_has_compression(sbi))
4177 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4178 WQ_UNBOUND | WQ_HIGHPRI,
4180 if (!sbi->post_read_wq)
4185 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4187 if (sbi->post_read_wq)
4188 destroy_workqueue(sbi->post_read_wq);
4191 int __init f2fs_init_bio_entry_cache(void)
4193 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4194 sizeof(struct bio_entry));
4195 if (!bio_entry_slab)
4200 void f2fs_destroy_bio_entry_cache(void)
4202 kmem_cache_destroy(bio_entry_slab);